Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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