PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/book/classes/external.php

https://bitbucket.org/moodle/moodle
PHP | 271 lines | 158 code | 30 blank | 83 comment | 11 complexity | 725a54842d9acbfb04260f0eb200b901 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Book external API
  18. *
  19. * @package mod_book
  20. * @category external
  21. * @copyright 2015 Juan Leyva <juan@moodle.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. * @since Moodle 3.0
  24. */
  25. defined('MOODLE_INTERNAL') || die;
  26. require_once("$CFG->libdir/externallib.php");
  27. /**
  28. * Book external functions
  29. *
  30. * @package mod_book
  31. * @category external
  32. * @copyright 2015 Juan Leyva <juan@moodle.com>
  33. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34. * @since Moodle 3.0
  35. */
  36. class mod_book_external extends external_api {
  37. /**
  38. * Returns description of method parameters
  39. *
  40. * @return external_function_parameters
  41. * @since Moodle 3.0
  42. */
  43. public static function view_book_parameters() {
  44. return new external_function_parameters(
  45. array(
  46. 'bookid' => new external_value(PARAM_INT, 'book instance id'),
  47. 'chapterid' => new external_value(PARAM_INT, 'chapter id', VALUE_DEFAULT, 0)
  48. )
  49. );
  50. }
  51. /**
  52. * Simulate the book/view.php web interface page: trigger events, completion, etc...
  53. *
  54. * @param int $bookid the book instance id
  55. * @param int $chapterid the book chapter id
  56. * @return array of warnings and status result
  57. * @since Moodle 3.0
  58. * @throws moodle_exception
  59. */
  60. public static function view_book($bookid, $chapterid = 0) {
  61. global $DB, $CFG;
  62. require_once($CFG->dirroot . "/mod/book/lib.php");
  63. require_once($CFG->dirroot . "/mod/book/locallib.php");
  64. $params = self::validate_parameters(self::view_book_parameters(),
  65. array(
  66. 'bookid' => $bookid,
  67. 'chapterid' => $chapterid
  68. ));
  69. $bookid = $params['bookid'];
  70. $chapterid = $params['chapterid'];
  71. $warnings = array();
  72. // Request and permission validation.
  73. $book = $DB->get_record('book', array('id' => $bookid), '*', MUST_EXIST);
  74. list($course, $cm) = get_course_and_cm_from_instance($book, 'book');
  75. $context = context_module::instance($cm->id);
  76. self::validate_context($context);
  77. require_capability('mod/book:read', $context);
  78. $chapters = book_preload_chapters($book);
  79. $firstchapterid = 0;
  80. $lastchapterid = 0;
  81. foreach ($chapters as $ch) {
  82. if ($ch->hidden) {
  83. continue;
  84. }
  85. if (!$firstchapterid) {
  86. $firstchapterid = $ch->id;
  87. }
  88. $lastchapterid = $ch->id;
  89. }
  90. if (!$chapterid) {
  91. // Trigger the module viewed events since we are displaying the book.
  92. book_view($book, null, false, $course, $cm, $context);
  93. $chapterid = $firstchapterid;
  94. }
  95. // Check if book is empty (warning).
  96. if (!$chapterid) {
  97. $warnings[] = array(
  98. 'item' => 'book',
  99. 'itemid' => $book->id,
  100. 'warningcode' => '1',
  101. 'message' => get_string('nocontent', 'mod_book')
  102. );
  103. } else {
  104. $chapter = $DB->get_record('book_chapters', array('id' => $chapterid, 'bookid' => $book->id));
  105. $viewhidden = has_capability('mod/book:viewhiddenchapters', $context);
  106. if (!$chapter or ($chapter->hidden and !$viewhidden)) {
  107. throw new moodle_exception('errorchapter', 'mod_book');
  108. }
  109. // Trigger the chapter viewed event.
  110. $islastchapter = ($chapter->id == $lastchapterid) ? true : false;
  111. book_view($book, $chapter, $islastchapter, $course, $cm, $context);
  112. }
  113. $result = array();
  114. $result['status'] = true;
  115. $result['warnings'] = $warnings;
  116. return $result;
  117. }
  118. /**
  119. * Returns description of method result value
  120. *
  121. * @return external_description
  122. * @since Moodle 3.0
  123. */
  124. public static function view_book_returns() {
  125. return new external_single_structure(
  126. array(
  127. 'status' => new external_value(PARAM_BOOL, 'status: true if success'),
  128. 'warnings' => new external_warnings()
  129. )
  130. );
  131. }
  132. /**
  133. * Describes the parameters for get_books_by_courses.
  134. *
  135. * @return external_function_parameters
  136. * @since Moodle 3.0
  137. */
  138. public static function get_books_by_courses_parameters() {
  139. return new external_function_parameters (
  140. array(
  141. 'courseids' => new external_multiple_structure(
  142. new external_value(PARAM_INT, 'course id'), 'Array of course ids', VALUE_DEFAULT, array()
  143. ),
  144. )
  145. );
  146. }
  147. /**
  148. * Returns a list of books in a provided list of courses,
  149. * if no list is provided all books that the user can view will be returned.
  150. *
  151. * @param array $courseids the course ids
  152. * @return array of books details
  153. * @since Moodle 3.0
  154. */
  155. public static function get_books_by_courses($courseids = array()) {
  156. global $CFG;
  157. $returnedbooks = array();
  158. $warnings = array();
  159. $params = self::validate_parameters(self::get_books_by_courses_parameters(), array('courseids' => $courseids));
  160. $courses = array();
  161. if (empty($params['courseids'])) {
  162. $courses = enrol_get_my_courses();
  163. $params['courseids'] = array_keys($courses);
  164. }
  165. // Ensure there are courseids to loop through.
  166. if (!empty($params['courseids'])) {
  167. list($courses, $warnings) = external_util::validate_courses($params['courseids'], $courses);
  168. // Get the books in this course, this function checks users visibility permissions.
  169. // We can avoid then additional validate_context calls.
  170. $books = get_all_instances_in_courses("book", $courses);
  171. foreach ($books as $book) {
  172. $context = context_module::instance($book->coursemodule);
  173. // Entry to return.
  174. $bookdetails = array();
  175. // First, we return information that any user can see in the web interface.
  176. $bookdetails['id'] = $book->id;
  177. $bookdetails['coursemodule'] = $book->coursemodule;
  178. $bookdetails['course'] = $book->course;
  179. $bookdetails['name'] = external_format_string($book->name, $context->id);
  180. // Format intro.
  181. $options = array('noclean' => true);
  182. list($bookdetails['intro'], $bookdetails['introformat']) =
  183. external_format_text($book->intro, $book->introformat, $context->id, 'mod_book', 'intro', null, $options);
  184. $bookdetails['introfiles'] = external_util::get_area_files($context->id, 'mod_book', 'intro', false, false);
  185. $bookdetails['numbering'] = $book->numbering;
  186. $bookdetails['navstyle'] = $book->navstyle;
  187. $bookdetails['customtitles'] = $book->customtitles;
  188. if (has_capability('moodle/course:manageactivities', $context)) {
  189. $bookdetails['revision'] = $book->revision;
  190. $bookdetails['timecreated'] = $book->timecreated;
  191. $bookdetails['timemodified'] = $book->timemodified;
  192. $bookdetails['section'] = $book->section;
  193. $bookdetails['visible'] = $book->visible;
  194. $bookdetails['groupmode'] = $book->groupmode;
  195. $bookdetails['groupingid'] = $book->groupingid;
  196. }
  197. $returnedbooks[] = $bookdetails;
  198. }
  199. }
  200. $result = array();
  201. $result['books'] = $returnedbooks;
  202. $result['warnings'] = $warnings;
  203. return $result;
  204. }
  205. /**
  206. * Describes the get_books_by_courses return value.
  207. *
  208. * @return external_single_structure
  209. * @since Moodle 3.0
  210. */
  211. public static function get_books_by_courses_returns() {
  212. return new external_single_structure(
  213. array(
  214. 'books' => new external_multiple_structure(
  215. new external_single_structure(
  216. array(
  217. 'id' => new external_value(PARAM_INT, 'Book id'),
  218. 'coursemodule' => new external_value(PARAM_INT, 'Course module id'),
  219. 'course' => new external_value(PARAM_INT, 'Course id'),
  220. 'name' => new external_value(PARAM_RAW, 'Book name'),
  221. 'intro' => new external_value(PARAM_RAW, 'The Book intro'),
  222. 'introformat' => new external_format_value('intro'),
  223. 'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL),
  224. 'numbering' => new external_value(PARAM_INT, 'Book numbering configuration'),
  225. 'navstyle' => new external_value(PARAM_INT, 'Book navigation style configuration'),
  226. 'customtitles' => new external_value(PARAM_INT, 'Book custom titles type'),
  227. 'revision' => new external_value(PARAM_INT, 'Book revision', VALUE_OPTIONAL),
  228. 'timecreated' => new external_value(PARAM_INT, 'Time of creation', VALUE_OPTIONAL),
  229. 'timemodified' => new external_value(PARAM_INT, 'Time of last modification', VALUE_OPTIONAL),
  230. 'section' => new external_value(PARAM_INT, 'Course section id', VALUE_OPTIONAL),
  231. 'visible' => new external_value(PARAM_BOOL, 'Visible', VALUE_OPTIONAL),
  232. 'groupmode' => new external_value(PARAM_INT, 'Group mode', VALUE_OPTIONAL),
  233. 'groupingid' => new external_value(PARAM_INT, 'Group id', VALUE_OPTIONAL),
  234. ), 'Books'
  235. )
  236. ),
  237. 'warnings' => new external_warnings(),
  238. )
  239. );
  240. }
  241. }