Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   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  /**
  18   * Book external API
  19   *
  20   * @package    mod_book
  21   * @category   external
  22   * @copyright  2015 Juan Leyva <juan@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since      Moodle 3.0
  25   */
  26  
  27  use core_course\external\helper_for_get_mods_by_courses;
  28  use core_external\external_api;
  29  use core_external\external_function_parameters;
  30  use core_external\external_multiple_structure;
  31  use core_external\external_single_structure;
  32  use core_external\external_value;
  33  use core_external\external_warnings;
  34  use core_external\util;
  35  
  36  /**
  37   * Book external functions
  38   *
  39   * @package    mod_book
  40   * @category   external
  41   * @copyright  2015 Juan Leyva <juan@moodle.com>
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   * @since      Moodle 3.0
  44   */
  45  class mod_book_external extends external_api {
  46  
  47      /**
  48       * Returns description of method parameters
  49       *
  50       * @return external_function_parameters
  51       * @since Moodle 3.0
  52       */
  53      public static function view_book_parameters() {
  54          return new external_function_parameters(
  55              array(
  56                  'bookid' => new external_value(PARAM_INT, 'book instance id'),
  57                  'chapterid' => new external_value(PARAM_INT, 'chapter id', VALUE_DEFAULT, 0)
  58              )
  59          );
  60      }
  61  
  62      /**
  63       * Simulate the book/view.php web interface page: trigger events, completion, etc...
  64       *
  65       * @param int $bookid the book instance id
  66       * @param int $chapterid the book chapter id
  67       * @return array of warnings and status result
  68       * @since Moodle 3.0
  69       * @throws moodle_exception
  70       */
  71      public static function view_book($bookid, $chapterid = 0) {
  72          global $DB, $CFG;
  73          require_once($CFG->dirroot . "/mod/book/lib.php");
  74          require_once($CFG->dirroot . "/mod/book/locallib.php");
  75  
  76          $params = self::validate_parameters(self::view_book_parameters(),
  77                                              array(
  78                                                  'bookid' => $bookid,
  79                                                  'chapterid' => $chapterid
  80                                              ));
  81          $bookid = $params['bookid'];
  82          $chapterid = $params['chapterid'];
  83  
  84          $warnings = array();
  85  
  86          // Request and permission validation.
  87          $book = $DB->get_record('book', array('id' => $bookid), '*', MUST_EXIST);
  88          list($course, $cm) = get_course_and_cm_from_instance($book, 'book');
  89  
  90          $context = context_module::instance($cm->id);
  91          self::validate_context($context);
  92  
  93          require_capability('mod/book:read', $context);
  94  
  95          $chapters = book_preload_chapters($book);
  96          $firstchapterid = 0;
  97  
  98          foreach ($chapters as $ch) {
  99              if ($ch->hidden) {
 100                  continue;
 101              }
 102              if (!$firstchapterid) {
 103                  $firstchapterid = $ch->id;
 104              }
 105          }
 106  
 107          if (!$chapterid) {
 108              // Trigger the module viewed events since we are displaying the book.
 109              book_view($book, null, false, $course, $cm, $context);
 110              $chapterid = $firstchapterid;
 111          }
 112  
 113          // Check if book is empty (warning).
 114          if (!$chapterid) {
 115              $warnings[] = array(
 116                  'item' => 'book',
 117                  'itemid' => $book->id,
 118                  'warningcode' => '1',
 119                  'message' => get_string('nocontent', 'mod_book')
 120              );
 121          } else {
 122              $chapter = $DB->get_record('book_chapters', array('id' => $chapterid, 'bookid' => $book->id));
 123              $viewhidden = has_capability('mod/book:viewhiddenchapters', $context);
 124  
 125              if (!$chapter or ($chapter->hidden and !$viewhidden)) {
 126                  throw new moodle_exception('errorchapter', 'mod_book');
 127              }
 128  
 129              // Trigger the chapter viewed event.
 130              book_view($book, $chapter, \mod_book\helper::is_last_visible_chapter($chapterid, $chapters), $course, $cm, $context);
 131          }
 132  
 133          $result = array();
 134          $result['status'] = true;
 135          $result['warnings'] = $warnings;
 136          return $result;
 137      }
 138  
 139      /**
 140       * Returns description of method result value
 141       *
 142       * @return \core_external\external_description
 143       * @since Moodle 3.0
 144       */
 145      public static function view_book_returns() {
 146          return new external_single_structure(
 147              array(
 148                  'status' => new external_value(PARAM_BOOL, 'status: true if success'),
 149                  'warnings' => new external_warnings()
 150              )
 151          );
 152      }
 153  
 154      /**
 155       * Describes the parameters for get_books_by_courses.
 156       *
 157       * @return external_function_parameters
 158       * @since Moodle 3.0
 159       */
 160      public static function get_books_by_courses_parameters() {
 161          return new external_function_parameters (
 162              array(
 163                  'courseids' => new external_multiple_structure(
 164                      new external_value(PARAM_INT, 'course id'), 'Array of course ids', VALUE_DEFAULT, array()
 165                  ),
 166              )
 167          );
 168      }
 169  
 170      /**
 171       * Returns a list of books in a provided list of courses,
 172       * if no list is provided all books that the user can view will be returned.
 173       *
 174       * @param array $courseids the course ids
 175       * @return array of books details
 176       * @since Moodle 3.0
 177       */
 178      public static function get_books_by_courses($courseids = array()) {
 179  
 180          $returnedbooks = array();
 181          $warnings = array();
 182  
 183          $params = self::validate_parameters(self::get_books_by_courses_parameters(), array('courseids' => $courseids));
 184  
 185          $courses = array();
 186          if (empty($params['courseids'])) {
 187              $courses = enrol_get_my_courses();
 188              $params['courseids'] = array_keys($courses);
 189          }
 190  
 191          // Ensure there are courseids to loop through.
 192          if (!empty($params['courseids'])) {
 193  
 194              list($courses, $warnings) = util::validate_courses($params['courseids'], $courses);
 195  
 196              // Get the books in this course, this function checks users visibility permissions.
 197              // We can avoid then additional validate_context calls.
 198              $books = get_all_instances_in_courses("book", $courses);
 199              foreach ($books as $book) {
 200                  // Entry to return.
 201                  $bookdetails = helper_for_get_mods_by_courses::standard_coursemodule_element_values($book, 'mod_book');
 202                  $bookdetails['numbering']         = $book->numbering;
 203                  $bookdetails['navstyle']          = $book->navstyle;
 204                  $bookdetails['customtitles']      = $book->customtitles;
 205  
 206                  if (has_capability('moodle/course:manageactivities', context_module::instance($book->coursemodule))) {
 207                      $bookdetails['revision']      = $book->revision;
 208                      $bookdetails['timecreated']   = $book->timecreated;
 209                      $bookdetails['timemodified']  = $book->timemodified;
 210                  }
 211                  $returnedbooks[] = $bookdetails;
 212              }
 213          }
 214          $result = array();
 215          $result['books'] = $returnedbooks;
 216          $result['warnings'] = $warnings;
 217          return $result;
 218      }
 219  
 220      /**
 221       * Describes the get_books_by_courses return value.
 222       *
 223       * @return external_single_structure
 224       * @since Moodle 3.0
 225       */
 226      public static function get_books_by_courses_returns() {
 227          return new external_single_structure(
 228              array(
 229                  'books' => new external_multiple_structure(
 230                      new external_single_structure(array_merge(
 231                          helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(),
 232                          [
 233                              'numbering' => new external_value(PARAM_INT, 'Book numbering configuration'),
 234                              'navstyle' => new external_value(PARAM_INT, 'Book navigation style configuration'),
 235                              'customtitles' => new external_value(PARAM_INT, 'Book custom titles type'),
 236                              'revision' => new external_value(PARAM_INT, 'Book revision', VALUE_OPTIONAL),
 237                              'timecreated' => new external_value(PARAM_INT, 'Time of creation', VALUE_OPTIONAL),
 238                              'timemodified' => new external_value(PARAM_INT, 'Time of last modification', VALUE_OPTIONAL),
 239                          ]
 240                      ), 'Books')
 241                  ),
 242                  'warnings' => new external_warnings(),
 243              )
 244          );
 245      }
 246  }