Commit | Line | Data |
---|---|---|
23cbaefb DP |
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 | * Wiki module external API. | |
19 | * | |
20 | * @package mod_wiki | |
21 | * @category external | |
22 | * @copyright 2015 Dani Palou <dani@moodle.com> | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | * @since Moodle 3.1 | |
25 | */ | |
26 | ||
27 | defined('MOODLE_INTERNAL') || die; | |
28 | ||
29 | require_once($CFG->libdir . '/externallib.php'); | |
30 | require_once($CFG->dirroot . '/mod/wiki/lib.php'); | |
31 | require_once($CFG->dirroot . '/mod/wiki/locallib.php'); | |
32 | ||
33 | /** | |
34 | * Wiki module external functions. | |
35 | * | |
36 | * @package mod_wiki | |
37 | * @category external | |
38 | * @copyright 2015 Dani Palou <dani@moodle.com> | |
39 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
40 | * @since Moodle 3.1 | |
41 | */ | |
42 | class mod_wiki_external extends external_api { | |
43 | ||
44 | /** | |
45 | * Describes the parameters for get_wikis_by_courses. | |
46 | * | |
47 | * @return external_function_parameters | |
48 | * @since Moodle 3.1 | |
49 | */ | |
50 | public static function get_wikis_by_courses_parameters() { | |
51 | return new external_function_parameters ( | |
52 | array( | |
53 | 'courseids' => new external_multiple_structure( | |
54 | new external_value(PARAM_INT, 'Course ID'), 'Array of course ids.', VALUE_DEFAULT, array() | |
55 | ), | |
56 | ) | |
57 | ); | |
58 | } | |
59 | ||
60 | /** | |
61 | * Returns a list of wikis in a provided list of courses, | |
62 | * if no list is provided all wikis that the user can view will be returned. | |
63 | * | |
64 | * @param array $courseids The courses IDs. | |
65 | * @return array Containing a list of warnings and a list of wikis. | |
66 | * @since Moodle 3.1 | |
67 | */ | |
68 | public static function get_wikis_by_courses($courseids = array()) { | |
69 | ||
70 | $returnedwikis = array(); | |
71 | $warnings = array(); | |
72 | ||
73 | $params = self::validate_parameters(self::get_wikis_by_courses_parameters(), array('courseids' => $courseids)); | |
74 | ||
75 | $mycourses = array(); | |
76 | if (empty($params['courseids'])) { | |
77 | $mycourses = enrol_get_my_courses(); | |
78 | $params['courseids'] = array_keys($mycourses); | |
79 | } | |
80 | ||
81 | // Ensure there are courseids to loop through. | |
82 | if (!empty($params['courseids'])) { | |
83 | ||
84 | list($courses, $warnings) = external_util::validate_courses($params['courseids'], $mycourses); | |
85 | ||
86 | // Get the wikis in this course, this function checks users visibility permissions. | |
87 | // We can avoid then additional validate_context calls. | |
88 | $wikis = get_all_instances_in_courses('wiki', $courses); | |
89 | ||
90 | foreach ($wikis as $wiki) { | |
91 | ||
92 | $context = context_module::instance($wiki->coursemodule); | |
93 | ||
94 | // Entry to return. | |
95 | $module = array(); | |
96 | ||
97 | // First, we return information that any user can see in (or can deduce from) the web interface. | |
98 | $module['id'] = $wiki->id; | |
99 | $module['coursemodule'] = $wiki->coursemodule; | |
100 | $module['course'] = $wiki->course; | |
101 | $module['name'] = external_format_string($wiki->name, $context->id); | |
102 | ||
103 | $viewablefields = []; | |
104 | if (has_capability('mod/wiki:viewpage', $context)) { | |
105 | list($module['intro'], $module['introformat']) = | |
106 | external_format_text($wiki->intro, $wiki->introformat, $context->id, 'mod_wiki', 'intro', $wiki->id); | |
107 | ||
108 | $viewablefields = array('firstpagetitle', 'wikimode', 'defaultformat', 'forceformat', 'editbegin', 'editend', | |
109 | 'section', 'visible', 'groupmode', 'groupingid'); | |
110 | } | |
111 | ||
112 | // Check additional permissions for returning optional private settings. | |
113 | if (has_capability('moodle/course:manageactivities', $context)) { | |
114 | $additionalfields = array('timecreated', 'timemodified'); | |
115 | $viewablefields = array_merge($viewablefields, $additionalfields); | |
116 | } | |
117 | ||
118 | foreach ($viewablefields as $field) { | |
119 | $module[$field] = $wiki->{$field}; | |
120 | } | |
121 | ||
122 | // Check if user can add new pages. | |
123 | $module['cancreatepages'] = wiki_can_create_pages($context); | |
124 | ||
125 | $returnedwikis[] = $module; | |
126 | } | |
127 | } | |
128 | ||
129 | $result = array(); | |
130 | $result['wikis'] = $returnedwikis; | |
131 | $result['warnings'] = $warnings; | |
132 | return $result; | |
133 | } | |
134 | ||
135 | /** | |
136 | * Describes the get_wikis_by_courses return value. | |
137 | * | |
138 | * @return external_single_structure | |
139 | * @since Moodle 3.1 | |
140 | */ | |
141 | public static function get_wikis_by_courses_returns() { | |
142 | ||
143 | return new external_single_structure( | |
144 | array( | |
145 | 'wikis' => new external_multiple_structure( | |
146 | new external_single_structure( | |
147 | array( | |
148 | 'id' => new external_value(PARAM_INT, 'Wiki ID.'), | |
149 | 'coursemodule' => new external_value(PARAM_INT, 'Course module ID.'), | |
150 | 'course' => new external_value(PARAM_INT, 'Course ID.'), | |
151 | 'name' => new external_value(PARAM_RAW, 'Wiki name.'), | |
152 | 'intro' => new external_value(PARAM_RAW, 'Wiki intro.', VALUE_OPTIONAL), | |
153 | 'introformat' => new external_format_value('Wiki intro format.', VALUE_OPTIONAL), | |
154 | 'timecreated' => new external_value(PARAM_INT, 'Time of creation.', VALUE_OPTIONAL), | |
155 | 'timemodified' => new external_value(PARAM_INT, 'Time of last modification.', VALUE_OPTIONAL), | |
156 | 'firstpagetitle' => new external_value(PARAM_RAW, 'First page title.', VALUE_OPTIONAL), | |
157 | 'wikimode' => new external_value(PARAM_TEXT, 'Wiki mode (individual, collaborative).', VALUE_OPTIONAL), | |
158 | 'defaultformat' => new external_value(PARAM_TEXT, 'Wiki\'s default format (html, creole, nwiki).', | |
159 | VALUE_OPTIONAL), | |
160 | 'forceformat' => new external_value(PARAM_INT, '1 if format is forced, 0 otherwise.', | |
161 | VALUE_OPTIONAL), | |
162 | 'editbegin' => new external_value(PARAM_INT, 'Edit begin.', VALUE_OPTIONAL), | |
163 | 'editend' => new external_value(PARAM_INT, 'Edit end.', VALUE_OPTIONAL), | |
164 | 'section' => new external_value(PARAM_INT, 'Course section ID.', VALUE_OPTIONAL), | |
165 | 'visible' => new external_value(PARAM_INT, '1 if visible, 0 otherwise.', VALUE_OPTIONAL), | |
166 | 'groupmode' => new external_value(PARAM_INT, 'Group mode.', VALUE_OPTIONAL), | |
167 | 'groupingid' => new external_value(PARAM_INT, 'Group ID.', VALUE_OPTIONAL), | |
168 | 'cancreatepages' => new external_value(PARAM_BOOL, 'True if user can create pages.'), | |
169 | ), 'Wikis' | |
170 | ) | |
171 | ), | |
172 | 'warnings' => new external_warnings(), | |
173 | ) | |
174 | ); | |
175 | } | |
176 | ||
557b58b0 DP |
177 | /** |
178 | * Describes the parameters for view_wiki. | |
179 | * | |
180 | * @return external_function_parameters | |
181 | * @since Moodle 3.1 | |
182 | */ | |
183 | public static function view_wiki_parameters() { | |
184 | return new external_function_parameters ( | |
185 | array( | |
186 | 'wikiid' => new external_value(PARAM_INT, 'Wiki instance ID.') | |
187 | ) | |
188 | ); | |
189 | } | |
190 | ||
191 | /** | |
192 | * Trigger the course module viewed event and update the module completion status. | |
193 | * | |
194 | * @param int $wikiid The wiki instance ID. | |
195 | * @return array of warnings and status result. | |
196 | * @since Moodle 3.1 | |
197 | */ | |
198 | public static function view_wiki($wikiid) { | |
199 | ||
200 | $params = self::validate_parameters(self::view_wiki_parameters(), | |
201 | array( | |
202 | 'wikiid' => $wikiid | |
203 | )); | |
204 | $warnings = array(); | |
205 | ||
206 | // Get wiki instance. | |
207 | if (!$wiki = wiki_get_wiki($params['wikiid'])) { | |
208 | throw new moodle_exception('incorrectwikiid', 'wiki'); | |
209 | } | |
210 | ||
211 | // Permission validation. | |
212 | list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki'); | |
213 | $context = context_module::instance($cm->id); | |
214 | self::validate_context($context); | |
215 | ||
216 | // Check if user can view this wiki. | |
217 | // We don't use wiki_user_can_view because it requires to have a valid subwiki for the user. | |
218 | if (!has_capability('mod/wiki:viewpage', $context)) { | |
219 | throw new moodle_exception('cannotviewpage', 'wiki'); | |
220 | } | |
221 | ||
222 | // Trigger course_module_viewed event and completion. | |
223 | wiki_view($wiki, $course, $cm, $context); | |
224 | ||
225 | $result = array(); | |
226 | $result['status'] = true; | |
227 | $result['warnings'] = $warnings; | |
228 | return $result; | |
229 | } | |
230 | ||
231 | /** | |
232 | * Describes the view_wiki return value. | |
233 | * | |
234 | * @return external_single_structure | |
235 | * @since Moodle 3.1 | |
236 | */ | |
237 | public static function view_wiki_returns() { | |
238 | return new external_single_structure( | |
239 | array( | |
240 | 'status' => new external_value(PARAM_BOOL, 'Status: true if success.'), | |
241 | 'warnings' => new external_warnings() | |
242 | ) | |
243 | ); | |
244 | } | |
245 | ||
246 | /** | |
247 | * Describes the parameters for view_page. | |
248 | * | |
249 | * @return external_function_parameters | |
250 | * @since Moodle 3.1 | |
251 | */ | |
252 | public static function view_page_parameters() { | |
253 | return new external_function_parameters ( | |
254 | array( | |
255 | 'pageid' => new external_value(PARAM_INT, 'Wiki page ID.'), | |
256 | ) | |
257 | ); | |
258 | } | |
259 | ||
260 | /** | |
261 | * Trigger the page viewed event and update the module completion status. | |
262 | * | |
263 | * @param int $pageid The page ID. | |
264 | * @return array of warnings and status result. | |
265 | * @since Moodle 3.1 | |
266 | * @throws moodle_exception if page is not valid. | |
267 | */ | |
268 | public static function view_page($pageid) { | |
269 | ||
270 | $params = self::validate_parameters(self::view_page_parameters(), | |
271 | array( | |
272 | 'pageid' => $pageid | |
273 | )); | |
274 | $warnings = array(); | |
275 | ||
276 | // Get wiki page. | |
277 | if (!$page = wiki_get_page($params['pageid'])) { | |
278 | throw new moodle_exception('incorrectpageid', 'wiki'); | |
279 | } | |
280 | ||
281 | // Get wiki instance. | |
282 | if (!$wiki = wiki_get_wiki_from_pageid($params['pageid'])) { | |
283 | throw new moodle_exception('incorrectwikiid', 'wiki'); | |
284 | } | |
285 | ||
286 | // Permission validation. | |
287 | list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki'); | |
288 | $context = context_module::instance($cm->id); | |
289 | self::validate_context($context); | |
290 | ||
291 | // Check if user can view this wiki. | |
292 | if (!$subwiki = wiki_get_subwiki($page->subwikiid)) { | |
293 | throw new moodle_exception('incorrectsubwikiid', 'wiki'); | |
294 | } | |
295 | if (!wiki_user_can_view($subwiki, $wiki)) { | |
296 | throw new moodle_exception('cannotviewpage', 'wiki'); | |
297 | } | |
298 | ||
299 | // Trigger page_viewed event and completion. | |
300 | wiki_page_view($wiki, $page, $course, $cm, $context); | |
301 | ||
302 | $result = array(); | |
303 | $result['status'] = true; | |
304 | $result['warnings'] = $warnings; | |
305 | return $result; | |
306 | } | |
307 | ||
308 | /** | |
309 | * Describes the view_page return value. | |
310 | * | |
311 | * @return external_single_structure | |
312 | * @since Moodle 3.1 | |
313 | */ | |
314 | public static function view_page_returns() { | |
315 | return new external_single_structure( | |
316 | array( | |
317 | 'status' => new external_value(PARAM_BOOL, 'Status: true if success.'), | |
318 | 'warnings' => new external_warnings() | |
319 | ) | |
320 | ); | |
321 | } | |
322 | ||
44f1b701 DP |
323 | /** |
324 | * Describes the parameters for get_subwikis. | |
325 | * | |
326 | * @return external_function_parameters | |
327 | * @since Moodle 3.1 | |
328 | */ | |
329 | public static function get_subwikis_parameters() { | |
330 | return new external_function_parameters ( | |
331 | array( | |
332 | 'wikiid' => new external_value(PARAM_INT, 'Wiki instance ID.') | |
333 | ) | |
334 | ); | |
335 | } | |
336 | ||
337 | /** | |
338 | * Returns the list of subwikis the user can see in a specific wiki. | |
339 | * | |
340 | * @param int $wikiid The wiki instance ID. | |
341 | * @return array Containing a list of warnings and a list of subwikis. | |
342 | * @since Moodle 3.1 | |
343 | */ | |
344 | public static function get_subwikis($wikiid) { | |
345 | global $USER; | |
346 | ||
347 | $warnings = array(); | |
348 | ||
349 | $params = self::validate_parameters(self::get_subwikis_parameters(), array('wikiid' => $wikiid)); | |
350 | ||
351 | // Get wiki instance. | |
352 | if (!$wiki = wiki_get_wiki($params['wikiid'])) { | |
353 | throw new moodle_exception('incorrectwikiid', 'wiki'); | |
354 | } | |
355 | ||
356 | // Validate context and capabilities. | |
357 | list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki'); | |
358 | $context = context_module::instance($cm->id); | |
359 | self::validate_context($context); | |
360 | require_capability('mod/wiki:viewpage', $context); | |
361 | ||
362 | $returnedsubwikis = wiki_get_visible_subwikis($wiki, $cm, $context); | |
363 | foreach ($returnedsubwikis as $subwiki) { | |
364 | $subwiki->canedit = wiki_user_can_edit($subwiki); | |
365 | } | |
366 | ||
367 | $result = array(); | |
368 | $result['subwikis'] = $returnedsubwikis; | |
369 | $result['warnings'] = $warnings; | |
370 | return $result; | |
371 | } | |
372 | ||
373 | /** | |
374 | * Describes the get_subwikis return value. | |
375 | * | |
376 | * @return external_single_structure | |
377 | * @since Moodle 3.1 | |
378 | */ | |
379 | public static function get_subwikis_returns() { | |
380 | return new external_single_structure( | |
381 | array( | |
382 | 'subwikis' => new external_multiple_structure( | |
383 | new external_single_structure( | |
384 | array( | |
385 | 'id' => new external_value(PARAM_INT, 'Subwiki ID.'), | |
386 | 'wikiid' => new external_value(PARAM_INT, 'Wiki ID.'), | |
387 | 'groupid' => new external_value(PARAM_RAW, 'Group ID.'), | |
388 | 'userid' => new external_value(PARAM_INT, 'User ID.'), | |
389 | 'canedit' => new external_value(PARAM_BOOL, 'True if user can edit the subwiki.'), | |
390 | ), 'Subwikis' | |
391 | ) | |
392 | ), | |
393 | 'warnings' => new external_warnings(), | |
394 | ) | |
395 | ); | |
396 | } | |
397 | ||
96abf9af DP |
398 | /** |
399 | * Describes the parameters for get_subwiki_pages. | |
400 | * | |
401 | * @return external_function_parameters | |
402 | * @since Moodle 3.1 | |
403 | */ | |
404 | public static function get_subwiki_pages_parameters() { | |
405 | return new external_function_parameters ( | |
406 | array( | |
407 | 'wikiid' => new external_value(PARAM_INT, 'Wiki instance ID.'), | |
408 | 'groupid' => new external_value(PARAM_INT, 'Subwiki\'s group ID, -1 means current group. It will be ignored' | |
409 | . ' if the wiki doesn\'t use groups.', VALUE_DEFAULT, -1), | |
410 | 'userid' => new external_value(PARAM_INT, 'Subwiki\'s user ID, 0 means current user. It will be ignored' | |
411 | .' in collaborative wikis.', VALUE_DEFAULT, 0), | |
412 | 'options' => new external_single_structure( | |
413 | array( | |
414 | 'sortby' => new external_value(PARAM_ALPHA, | |
415 | 'Field to sort by (id, title, ...).', VALUE_DEFAULT, 'title'), | |
416 | 'sortdirection' => new external_value(PARAM_ALPHA, | |
417 | 'Sort direction: ASC or DESC.', VALUE_DEFAULT, 'ASC'), | |
418 | 'includecontent' => new external_value(PARAM_INT, | |
419 | 'Include each page contents or not.', VALUE_DEFAULT, 1), | |
420 | ), 'Options', VALUE_DEFAULT, array()), | |
421 | ) | |
422 | ); | |
423 | } | |
424 | ||
425 | /** | |
426 | * Returns the list of pages from a specific subwiki. | |
427 | * | |
428 | * @param int $wikiid The wiki instance ID. | |
429 | * @param int $groupid The group ID. If not defined, use current group. | |
430 | * @param int $userid The user ID. If not defined, use current user. | |
431 | * @param array $options Several options like sort by, sort direction, ... | |
432 | * @return array Containing a list of warnings and a list of pages. | |
433 | * @since Moodle 3.1 | |
434 | */ | |
435 | public static function get_subwiki_pages($wikiid, $groupid = -1, $userid = 0, $options = array()) { | |
436 | global $USER, $DB; | |
437 | ||
438 | $returnedpages = array(); | |
439 | $warnings = array(); | |
440 | ||
441 | $params = self::validate_parameters(self::get_subwiki_pages_parameters(), | |
442 | array( | |
443 | 'wikiid' => $wikiid, | |
444 | 'groupid' => $groupid, | |
445 | 'userid' => $userid, | |
446 | 'options' => $options | |
447 | ) | |
448 | ); | |
449 | ||
450 | // Get wiki instance. | |
451 | if (!$wiki = wiki_get_wiki($params['wikiid'])) { | |
452 | throw new moodle_exception('incorrectwikiid', 'wiki'); | |
453 | } | |
454 | list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki'); | |
455 | $context = context_module::instance($cm->id); | |
456 | self::validate_context($context); | |
457 | ||
458 | // Determine group. | |
459 | $groupmode = groups_get_activity_groupmode($cm); | |
460 | if ($groupmode == NOGROUPS) { | |
461 | $groupid = 0; | |
462 | } else if ($params['groupid'] == -1) { | |
463 | // Use current group. | |
464 | $groupid = groups_get_activity_group($cm); | |
465 | $groupid = !empty($groupid) ? $groupid : 0; | |
466 | } else { | |
467 | $groupid = $params['groupid']; | |
468 | } | |
469 | ||
470 | // Determine user. | |
471 | if ($wiki->wikimode == 'collaborative') { | |
472 | // Collaborative wikis don't use userid in subwikis. | |
473 | $userid = 0; | |
474 | } else if (empty($params['userid'])) { | |
475 | // Use current user. | |
476 | $userid = $USER->id; | |
477 | } else { | |
478 | $userid = $params['userid']; | |
479 | } | |
480 | ||
481 | // Get subwiki based on group and user. | |
482 | if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $groupid, $userid)) { | |
483 | // The subwiki doesn't exist. | |
484 | // Validate if user is valid. | |
485 | if ($userid != 0 && $userid != $USER->id && !$user = $DB->get_record('user', array('id' => $userid))) { | |
486 | throw new moodle_exception('invaliduserid', 'error'); | |
487 | } | |
488 | ||
489 | // Validate that groupid is valid. | |
490 | if ($groupid != 0 && !groups_group_exists($groupid)) { | |
491 | throw new moodle_exception('cannotfindgroup', 'error'); | |
492 | } | |
493 | ||
494 | // Valid data but subwiki not found. We'll simulate a subwiki object to check if the user would be able to see it | |
495 | // if it existed. If he's able to see it then we'll return an empty array because the subwiki has no pages. | |
496 | $subwiki = new stdClass(); | |
497 | $subwiki->wikiid = $wiki->id; | |
498 | $subwiki->userid = $userid; | |
499 | $subwiki->groupid = $groupid; | |
500 | ||
501 | // Check that the user can view the subwiki. This function checks capabilities. | |
502 | if (!wiki_user_can_view($subwiki, $wiki)) { | |
503 | throw new moodle_exception('cannotviewpage', 'wiki'); | |
504 | } | |
505 | } else { | |
506 | // Check that the user can view the subwiki. This function checks capabilities. | |
507 | if (!wiki_user_can_view($subwiki, $wiki)) { | |
508 | throw new moodle_exception('cannotviewpage', 'wiki'); | |
509 | } | |
510 | ||
511 | // Set sort param. | |
512 | $options = $params['options']; | |
513 | if (!empty($options['sortby'])) { | |
514 | if ($options['sortdirection'] != 'ASC' && $options['sortdirection'] != 'DESC') { | |
515 | // Invalid sort direction. Use default. | |
516 | $options['sortdirection'] = 'ASC'; | |
517 | } | |
518 | $sort = $options['sortby'] . ' ' . $options['sortdirection']; | |
519 | } | |
520 | ||
521 | $pages = wiki_get_page_list($subwiki->id, $sort); | |
522 | $caneditpages = wiki_user_can_edit($subwiki); | |
523 | $firstpage = wiki_get_first_page($subwiki->id); | |
524 | ||
525 | foreach ($pages as $page) { | |
526 | $retpage = array( | |
527 | 'id' => $page->id, | |
528 | 'subwikiid' => $page->subwikiid, | |
529 | 'title' => external_format_string($page->title, $context->id), | |
530 | 'timecreated' => $page->timecreated, | |
531 | 'timemodified' => $page->timemodified, | |
532 | 'timerendered' => $page->timerendered, | |
533 | 'userid' => $page->userid, | |
534 | 'pageviews' => $page->pageviews, | |
535 | 'readonly' => $page->readonly, | |
536 | 'caneditpage' => $caneditpages, | |
537 | 'firstpage' => $page->id == $firstpage->id | |
538 | ); | |
539 | ||
540 | if ($options['includecontent']) { | |
541 | // Refresh page cached content if needed. | |
542 | if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) { | |
543 | if ($content = wiki_refresh_cachedcontent($page)) { | |
544 | $page = $content['page']; | |
545 | } | |
546 | } | |
547 | ||
548 | list($retpage['cachedcontent'], $retpage['contentformat']) = external_format_text( | |
549 | $page->cachedcontent, FORMAT_HTML, $context->id, 'mod_wiki', 'attachments', $subwiki->id); | |
550 | } | |
551 | ||
552 | $returnedpages[] = $retpage; | |
553 | } | |
554 | ||
555 | } | |
556 | ||
557 | $result = array(); | |
558 | $result['pages'] = $returnedpages; | |
559 | $result['warnings'] = $warnings; | |
560 | return $result; | |
561 | } | |
562 | ||
563 | /** | |
564 | * Describes the get_subwiki_pages return value. | |
565 | * | |
566 | * @return external_single_structure | |
567 | * @since Moodle 3.1 | |
568 | */ | |
569 | public static function get_subwiki_pages_returns() { | |
570 | ||
571 | return new external_single_structure( | |
572 | array( | |
573 | 'pages' => new external_multiple_structure( | |
574 | new external_single_structure( | |
575 | array( | |
576 | 'id' => new external_value(PARAM_INT, 'Page ID.'), | |
577 | 'subwikiid' => new external_value(PARAM_INT, 'Page\'s subwiki ID.'), | |
578 | 'title' => new external_value(PARAM_RAW, 'Page title.'), | |
579 | 'timecreated' => new external_value(PARAM_INT, 'Time of creation.'), | |
580 | 'timemodified' => new external_value(PARAM_INT, 'Time of last modification.'), | |
581 | 'timerendered' => new external_value(PARAM_INT, 'Time of last renderization.'), | |
582 | 'userid' => new external_value(PARAM_INT, 'ID of the user that last modified the page.'), | |
583 | 'pageviews' => new external_value(PARAM_INT, 'Number of times the page has been viewed.'), | |
584 | 'readonly' => new external_value(PARAM_INT, '1 if readonly, 0 otherwise.'), | |
585 | 'caneditpage' => new external_value(PARAM_BOOL, 'True if user can edit the page.'), | |
586 | 'firstpage' => new external_value(PARAM_BOOL, 'True if it\'s the first page.'), | |
587 | 'cachedcontent' => new external_value(PARAM_RAW, 'Page contents.', VALUE_OPTIONAL), | |
588 | 'contentformat' => new external_format_value('cachedcontent', VALUE_OPTIONAL), | |
589 | ), 'Pages' | |
590 | ) | |
591 | ), | |
592 | 'warnings' => new external_warnings(), | |
593 | ) | |
594 | ); | |
595 | } | |
596 | ||
5d94e343 DP |
597 | /** |
598 | * Describes the parameters for get_page_contents. | |
599 | * | |
600 | * @return external_function_parameters | |
601 | * @since Moodle 3.1 | |
602 | */ | |
603 | public static function get_page_contents_parameters() { | |
604 | return new external_function_parameters ( | |
605 | array( | |
606 | 'pageid' => new external_value(PARAM_INT, 'Page ID.') | |
607 | ) | |
608 | ); | |
609 | } | |
610 | ||
611 | /** | |
612 | * Get a page contents. | |
613 | * | |
614 | * @param int $pageid The page ID. | |
615 | * @return array of warnings and page data. | |
616 | * @since Moodle 3.1 | |
617 | */ | |
618 | public static function get_page_contents($pageid) { | |
619 | ||
620 | $params = self::validate_parameters(self::get_page_contents_parameters(), | |
621 | array( | |
622 | 'pageid' => $pageid | |
623 | ) | |
624 | ); | |
625 | $warnings = array(); | |
626 | ||
627 | // Get wiki page. | |
628 | if (!$page = wiki_get_page($params['pageid'])) { | |
629 | throw new moodle_exception('incorrectpageid', 'wiki'); | |
630 | } | |
631 | ||
632 | // Get wiki instance. | |
633 | if (!$wiki = wiki_get_wiki_from_pageid($params['pageid'])) { | |
634 | throw new moodle_exception('incorrectwikiid', 'wiki'); | |
635 | } | |
636 | ||
637 | // Permission validation. | |
638 | $cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course); | |
639 | $context = context_module::instance($cm->id); | |
640 | self::validate_context($context); | |
641 | ||
642 | // Check if user can view this wiki. | |
643 | if (!$subwiki = wiki_get_subwiki($page->subwikiid)) { | |
644 | throw new moodle_exception('incorrectsubwikiid', 'wiki'); | |
645 | } | |
646 | if (!wiki_user_can_view($subwiki, $wiki)) { | |
647 | throw new moodle_exception('cannotviewpage', 'wiki'); | |
648 | } | |
649 | ||
650 | $returnedpage = array(); | |
651 | $returnedpage['id'] = $page->id; | |
652 | $returnedpage['wikiid'] = $wiki->id; | |
653 | $returnedpage['subwikiid'] = $page->subwikiid; | |
654 | $returnedpage['groupid'] = $subwiki->groupid; | |
655 | $returnedpage['userid'] = $subwiki->userid; | |
656 | $returnedpage['title'] = $page->title; | |
657 | ||
658 | // Refresh page cached content if needed. | |
659 | if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) { | |
660 | if ($content = wiki_refresh_cachedcontent($page)) { | |
661 | $page = $content['page']; | |
662 | } | |
663 | } | |
664 | ||
665 | list($returnedpage['cachedcontent'], $returnedpage['contentformat']) = external_format_text( | |
666 | $page->cachedcontent, FORMAT_HTML, $context->id, 'mod_wiki', 'attachments', $subwiki->id); | |
667 | $returnedpage['caneditpage'] = wiki_user_can_edit($subwiki); | |
668 | ||
669 | $result = array(); | |
670 | $result['page'] = $returnedpage; | |
671 | $result['warnings'] = $warnings; | |
672 | return $result; | |
673 | } | |
674 | ||
675 | /** | |
676 | * Describes the get_page_contents return value. | |
677 | * | |
678 | * @return external_single_structure | |
679 | * @since Moodle 3.1 | |
680 | */ | |
681 | public static function get_page_contents_returns() { | |
682 | return new external_single_structure( | |
683 | array( | |
684 | 'page' => new external_single_structure( | |
685 | array( | |
686 | 'id' => new external_value(PARAM_INT, 'Page ID.'), | |
687 | 'wikiid' => new external_value(PARAM_INT, 'Page\'s wiki ID.'), | |
688 | 'subwikiid' => new external_value(PARAM_INT, 'Page\'s subwiki ID.'), | |
689 | 'groupid' => new external_value(PARAM_INT, 'Page\'s group ID.'), | |
690 | 'userid' => new external_value(PARAM_INT, 'Page\'s user ID.'), | |
691 | 'title' => new external_value(PARAM_RAW, 'Page title.'), | |
692 | 'cachedcontent' => new external_value(PARAM_RAW, 'Page contents.'), | |
693 | 'contentformat' => new external_format_value('cachedcontent', VALUE_OPTIONAL), | |
694 | 'caneditpage' => new external_value(PARAM_BOOL, 'True if user can edit the page.') | |
695 | ), 'Page' | |
696 | ), | |
697 | 'warnings' => new external_warnings() | |
698 | ) | |
699 | ); | |
700 | } | |
701 | ||
23cbaefb | 702 | } |