See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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 $lastchapterid = 0; 93 94 foreach ($chapters as $ch) { 95 if ($ch->hidden) { 96 continue; 97 } 98 if (!$firstchapterid) { 99 $firstchapterid = $ch->id; 100 } 101 $lastchapterid = $ch->id; 102 } 103 104 if (!$chapterid) { 105 // Trigger the module viewed events since we are displaying the book. 106 book_view($book, null, false, $course, $cm, $context); 107 $chapterid = $firstchapterid; 108 } 109 110 // Check if book is empty (warning). 111 if (!$chapterid) { 112 $warnings[] = array( 113 'item' => 'book', 114 'itemid' => $book->id, 115 'warningcode' => '1', 116 'message' => get_string('nocontent', 'mod_book') 117 ); 118 } else { 119 $chapter = $DB->get_record('book_chapters', array('id' => $chapterid, 'bookid' => $book->id)); 120 $viewhidden = has_capability('mod/book:viewhiddenchapters', $context); 121 122 if (!$chapter or ($chapter->hidden and !$viewhidden)) { 123 throw new moodle_exception('errorchapter', 'mod_book'); 124 } 125 126 // Trigger the chapter viewed event. 127 $islastchapter = ($chapter->id == $lastchapterid) ? true : false; 128 book_view($book, $chapter, $islastchapter, $course, $cm, $context); 129 } 130 131 $result = array(); 132 $result['status'] = true; 133 $result['warnings'] = $warnings; 134 return $result; 135 } 136 137 /** 138 * Returns description of method result value 139 * 140 * @return external_description 141 * @since Moodle 3.0 142 */ 143 public static function view_book_returns() { 144 return new external_single_structure( 145 array( 146 'status' => new external_value(PARAM_BOOL, 'status: true if success'), 147 'warnings' => new external_warnings() 148 ) 149 ); 150 } 151 152 /** 153 * Describes the parameters for get_books_by_courses. 154 * 155 * @return external_function_parameters 156 * @since Moodle 3.0 157 */ 158 public static function get_books_by_courses_parameters() { 159 return new external_function_parameters ( 160 array( 161 'courseids' => new external_multiple_structure( 162 new external_value(PARAM_INT, 'course id'), 'Array of course ids', VALUE_DEFAULT, array() 163 ), 164 ) 165 ); 166 } 167 168 /** 169 * Returns a list of books in a provided list of courses, 170 * if no list is provided all books that the user can view will be returned. 171 * 172 * @param array $courseids the course ids 173 * @return array of books details 174 * @since Moodle 3.0 175 */ 176 public static function get_books_by_courses($courseids = array()) { 177 global $CFG; 178 179 $returnedbooks = array(); 180 $warnings = array(); 181 182 $params = self::validate_parameters(self::get_books_by_courses_parameters(), array('courseids' => $courseids)); 183 184 $courses = array(); 185 if (empty($params['courseids'])) { 186 $courses = enrol_get_my_courses(); 187 $params['courseids'] = array_keys($courses); 188 } 189 190 // Ensure there are courseids to loop through. 191 if (!empty($params['courseids'])) { 192 193 list($courses, $warnings) = external_util::validate_courses($params['courseids'], $courses); 194 195 // Get the books in this course, this function checks users visibility permissions. 196 // We can avoid then additional validate_context calls. 197 $books = get_all_instances_in_courses("book", $courses); 198 foreach ($books as $book) { 199 $context = context_module::instance($book->coursemodule); 200 // Entry to return. 201 $bookdetails = array(); 202 // First, we return information that any user can see in the web interface. 203 $bookdetails['id'] = $book->id; 204 $bookdetails['coursemodule'] = $book->coursemodule; 205 $bookdetails['course'] = $book->course; 206 $bookdetails['name'] = external_format_string($book->name, $context->id); 207 // Format intro. 208 $options = array('noclean' => true); 209 list($bookdetails['intro'], $bookdetails['introformat']) = 210 external_format_text($book->intro, $book->introformat, $context->id, 'mod_book', 'intro', null, $options); 211 $bookdetails['introfiles'] = external_util::get_area_files($context->id, 'mod_book', 'intro', false, false); 212 $bookdetails['numbering'] = $book->numbering; 213 $bookdetails['navstyle'] = $book->navstyle; 214 $bookdetails['customtitles'] = $book->customtitles; 215 216 if (has_capability('moodle/course:manageactivities', $context)) { 217 $bookdetails['revision'] = $book->revision; 218 $bookdetails['timecreated'] = $book->timecreated; 219 $bookdetails['timemodified'] = $book->timemodified; 220 $bookdetails['section'] = $book->section; 221 $bookdetails['visible'] = $book->visible; 222 $bookdetails['groupmode'] = $book->groupmode; 223 $bookdetails['groupingid'] = $book->groupingid; 224 } 225 $returnedbooks[] = $bookdetails; 226 } 227 } 228 $result = array(); 229 $result['books'] = $returnedbooks; 230 $result['warnings'] = $warnings; 231 return $result; 232 } 233 234 /** 235 * Describes the get_books_by_courses return value. 236 * 237 * @return external_single_structure 238 * @since Moodle 3.0 239 */ 240 public static function get_books_by_courses_returns() { 241 return new external_single_structure( 242 array( 243 'books' => new external_multiple_structure( 244 new external_single_structure( 245 array( 246 'id' => new external_value(PARAM_INT, 'Book id'), 247 'coursemodule' => new external_value(PARAM_INT, 'Course module id'), 248 'course' => new external_value(PARAM_INT, 'Course id'), 249 'name' => new external_value(PARAM_RAW, 'Book name'), 250 'intro' => new external_value(PARAM_RAW, 'The Book intro'), 251 'introformat' => new external_format_value('intro'), 252 'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL), 253 'numbering' => new external_value(PARAM_INT, 'Book numbering configuration'), 254 'navstyle' => new external_value(PARAM_INT, 'Book navigation style configuration'), 255 'customtitles' => new external_value(PARAM_INT, 'Book custom titles type'), 256 'revision' => new external_value(PARAM_INT, 'Book revision', VALUE_OPTIONAL), 257 'timecreated' => new external_value(PARAM_INT, 'Time of creation', VALUE_OPTIONAL), 258 'timemodified' => new external_value(PARAM_INT, 'Time of last modification', VALUE_OPTIONAL), 259 'section' => new external_value(PARAM_INT, 'Course section id', VALUE_OPTIONAL), 260 'visible' => new external_value(PARAM_BOOL, 'Visible', VALUE_OPTIONAL), 261 'groupmode' => new external_value(PARAM_INT, 'Group mode', VALUE_OPTIONAL), 262 'groupingid' => new external_value(PARAM_INT, 'Group id', VALUE_OPTIONAL), 263 ), 'Books' 264 ) 265 ), 266 'warnings' => new external_warnings(), 267 ) 268 ); 269 } 270 271 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body