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()) { | |
96abf9af DP |
436 | |
437 | $returnedpages = array(); | |
438 | $warnings = array(); | |
439 | ||
440 | $params = self::validate_parameters(self::get_subwiki_pages_parameters(), | |
441 | array( | |
442 | 'wikiid' => $wikiid, | |
443 | 'groupid' => $groupid, | |
444 | 'userid' => $userid, | |
445 | 'options' => $options | |
446 | ) | |
447 | ); | |
448 | ||
449 | // Get wiki instance. | |
450 | if (!$wiki = wiki_get_wiki($params['wikiid'])) { | |
451 | throw new moodle_exception('incorrectwikiid', 'wiki'); | |
452 | } | |
453 | list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki'); | |
454 | $context = context_module::instance($cm->id); | |
455 | self::validate_context($context); | |
456 | ||
62e20cfe DP |
457 | // Determine groupid and userid to use. |
458 | list($groupid, $userid) = self::determine_group_and_user($cm, $wiki, $params['groupid'], $params['userid']); | |
96abf9af | 459 | |
62e20cfe DP |
460 | // Get subwiki and validate it. |
461 | $subwiki = wiki_get_subwiki_by_group_and_user_with_validation($wiki, $groupid, $userid); | |
96abf9af | 462 | |
62e20cfe DP |
463 | if ($subwiki === false) { |
464 | throw new moodle_exception('cannotviewpage', 'wiki'); | |
465 | } else if ($subwiki->id != -1) { | |
96abf9af DP |
466 | |
467 | // Set sort param. | |
468 | $options = $params['options']; | |
469 | if (!empty($options['sortby'])) { | |
470 | if ($options['sortdirection'] != 'ASC' && $options['sortdirection'] != 'DESC') { | |
471 | // Invalid sort direction. Use default. | |
472 | $options['sortdirection'] = 'ASC'; | |
473 | } | |
474 | $sort = $options['sortby'] . ' ' . $options['sortdirection']; | |
475 | } | |
476 | ||
477 | $pages = wiki_get_page_list($subwiki->id, $sort); | |
478 | $caneditpages = wiki_user_can_edit($subwiki); | |
479 | $firstpage = wiki_get_first_page($subwiki->id); | |
480 | ||
481 | foreach ($pages as $page) { | |
482 | $retpage = array( | |
483 | 'id' => $page->id, | |
484 | 'subwikiid' => $page->subwikiid, | |
485 | 'title' => external_format_string($page->title, $context->id), | |
486 | 'timecreated' => $page->timecreated, | |
487 | 'timemodified' => $page->timemodified, | |
488 | 'timerendered' => $page->timerendered, | |
489 | 'userid' => $page->userid, | |
490 | 'pageviews' => $page->pageviews, | |
491 | 'readonly' => $page->readonly, | |
492 | 'caneditpage' => $caneditpages, | |
493 | 'firstpage' => $page->id == $firstpage->id | |
494 | ); | |
495 | ||
31bfb008 DP |
496 | // Refresh page cached content if needed. |
497 | if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) { | |
498 | if ($content = wiki_refresh_cachedcontent($page)) { | |
499 | $page = $content['page']; | |
96abf9af | 500 | } |
31bfb008 DP |
501 | } |
502 | list($cachedcontent, $contentformat) = external_format_text( | |
503 | $page->cachedcontent, FORMAT_HTML, $context->id, 'mod_wiki', 'attachments', $subwiki->id); | |
96abf9af | 504 | |
31bfb008 DP |
505 | if ($options['includecontent']) { |
506 | // Return the page content. | |
507 | $retpage['cachedcontent'] = $cachedcontent; | |
508 | $retpage['contentformat'] = $contentformat; | |
509 | } else { | |
510 | // Return the size of the content. | |
511 | if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) { | |
512 | $retpage['contentsize'] = mb_strlen($cachedcontent, '8bit'); | |
513 | } else { | |
514 | $retpage['contentsize'] = strlen($cachedcontent); | |
515 | } | |
96abf9af DP |
516 | } |
517 | ||
518 | $returnedpages[] = $retpage; | |
519 | } | |
96abf9af DP |
520 | } |
521 | ||
522 | $result = array(); | |
523 | $result['pages'] = $returnedpages; | |
524 | $result['warnings'] = $warnings; | |
525 | return $result; | |
526 | } | |
527 | ||
528 | /** | |
529 | * Describes the get_subwiki_pages return value. | |
530 | * | |
531 | * @return external_single_structure | |
532 | * @since Moodle 3.1 | |
533 | */ | |
534 | public static function get_subwiki_pages_returns() { | |
535 | ||
536 | return new external_single_structure( | |
537 | array( | |
538 | 'pages' => new external_multiple_structure( | |
539 | new external_single_structure( | |
540 | array( | |
541 | 'id' => new external_value(PARAM_INT, 'Page ID.'), | |
542 | 'subwikiid' => new external_value(PARAM_INT, 'Page\'s subwiki ID.'), | |
543 | 'title' => new external_value(PARAM_RAW, 'Page title.'), | |
544 | 'timecreated' => new external_value(PARAM_INT, 'Time of creation.'), | |
545 | 'timemodified' => new external_value(PARAM_INT, 'Time of last modification.'), | |
546 | 'timerendered' => new external_value(PARAM_INT, 'Time of last renderization.'), | |
547 | 'userid' => new external_value(PARAM_INT, 'ID of the user that last modified the page.'), | |
548 | 'pageviews' => new external_value(PARAM_INT, 'Number of times the page has been viewed.'), | |
549 | 'readonly' => new external_value(PARAM_INT, '1 if readonly, 0 otherwise.'), | |
550 | 'caneditpage' => new external_value(PARAM_BOOL, 'True if user can edit the page.'), | |
551 | 'firstpage' => new external_value(PARAM_BOOL, 'True if it\'s the first page.'), | |
552 | 'cachedcontent' => new external_value(PARAM_RAW, 'Page contents.', VALUE_OPTIONAL), | |
553 | 'contentformat' => new external_format_value('cachedcontent', VALUE_OPTIONAL), | |
31bfb008 DP |
554 | 'contentsize' => new external_value(PARAM_INT, 'Size of page contents in bytes (doesn\'t include'. |
555 | ' size of attached files).', VALUE_OPTIONAL), | |
96abf9af DP |
556 | ), 'Pages' |
557 | ) | |
558 | ), | |
559 | 'warnings' => new external_warnings(), | |
560 | ) | |
561 | ); | |
562 | } | |
563 | ||
5d94e343 DP |
564 | /** |
565 | * Describes the parameters for get_page_contents. | |
566 | * | |
567 | * @return external_function_parameters | |
568 | * @since Moodle 3.1 | |
569 | */ | |
570 | public static function get_page_contents_parameters() { | |
571 | return new external_function_parameters ( | |
572 | array( | |
573 | 'pageid' => new external_value(PARAM_INT, 'Page ID.') | |
574 | ) | |
575 | ); | |
576 | } | |
577 | ||
578 | /** | |
579 | * Get a page contents. | |
580 | * | |
581 | * @param int $pageid The page ID. | |
582 | * @return array of warnings and page data. | |
583 | * @since Moodle 3.1 | |
584 | */ | |
585 | public static function get_page_contents($pageid) { | |
586 | ||
587 | $params = self::validate_parameters(self::get_page_contents_parameters(), | |
588 | array( | |
589 | 'pageid' => $pageid | |
590 | ) | |
591 | ); | |
592 | $warnings = array(); | |
593 | ||
594 | // Get wiki page. | |
595 | if (!$page = wiki_get_page($params['pageid'])) { | |
596 | throw new moodle_exception('incorrectpageid', 'wiki'); | |
597 | } | |
598 | ||
599 | // Get wiki instance. | |
600 | if (!$wiki = wiki_get_wiki_from_pageid($params['pageid'])) { | |
601 | throw new moodle_exception('incorrectwikiid', 'wiki'); | |
602 | } | |
603 | ||
604 | // Permission validation. | |
605 | $cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course); | |
606 | $context = context_module::instance($cm->id); | |
607 | self::validate_context($context); | |
608 | ||
609 | // Check if user can view this wiki. | |
610 | if (!$subwiki = wiki_get_subwiki($page->subwikiid)) { | |
611 | throw new moodle_exception('incorrectsubwikiid', 'wiki'); | |
612 | } | |
613 | if (!wiki_user_can_view($subwiki, $wiki)) { | |
614 | throw new moodle_exception('cannotviewpage', 'wiki'); | |
615 | } | |
616 | ||
617 | $returnedpage = array(); | |
618 | $returnedpage['id'] = $page->id; | |
619 | $returnedpage['wikiid'] = $wiki->id; | |
620 | $returnedpage['subwikiid'] = $page->subwikiid; | |
621 | $returnedpage['groupid'] = $subwiki->groupid; | |
622 | $returnedpage['userid'] = $subwiki->userid; | |
623 | $returnedpage['title'] = $page->title; | |
624 | ||
625 | // Refresh page cached content if needed. | |
626 | if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) { | |
627 | if ($content = wiki_refresh_cachedcontent($page)) { | |
628 | $page = $content['page']; | |
629 | } | |
630 | } | |
631 | ||
632 | list($returnedpage['cachedcontent'], $returnedpage['contentformat']) = external_format_text( | |
633 | $page->cachedcontent, FORMAT_HTML, $context->id, 'mod_wiki', 'attachments', $subwiki->id); | |
634 | $returnedpage['caneditpage'] = wiki_user_can_edit($subwiki); | |
635 | ||
636 | $result = array(); | |
637 | $result['page'] = $returnedpage; | |
638 | $result['warnings'] = $warnings; | |
639 | return $result; | |
640 | } | |
641 | ||
642 | /** | |
643 | * Describes the get_page_contents return value. | |
644 | * | |
645 | * @return external_single_structure | |
646 | * @since Moodle 3.1 | |
647 | */ | |
648 | public static function get_page_contents_returns() { | |
649 | return new external_single_structure( | |
650 | array( | |
651 | 'page' => new external_single_structure( | |
652 | array( | |
653 | 'id' => new external_value(PARAM_INT, 'Page ID.'), | |
654 | 'wikiid' => new external_value(PARAM_INT, 'Page\'s wiki ID.'), | |
655 | 'subwikiid' => new external_value(PARAM_INT, 'Page\'s subwiki ID.'), | |
656 | 'groupid' => new external_value(PARAM_INT, 'Page\'s group ID.'), | |
657 | 'userid' => new external_value(PARAM_INT, 'Page\'s user ID.'), | |
658 | 'title' => new external_value(PARAM_RAW, 'Page title.'), | |
659 | 'cachedcontent' => new external_value(PARAM_RAW, 'Page contents.'), | |
660 | 'contentformat' => new external_format_value('cachedcontent', VALUE_OPTIONAL), | |
661 | 'caneditpage' => new external_value(PARAM_BOOL, 'True if user can edit the page.') | |
662 | ), 'Page' | |
663 | ), | |
664 | 'warnings' => new external_warnings() | |
665 | ) | |
666 | ); | |
667 | } | |
668 | ||
62e20cfe DP |
669 | /** |
670 | * Describes the parameters for get_subwiki_files. | |
671 | * | |
672 | * @return external_function_parameters | |
673 | * @since Moodle 3.1 | |
674 | */ | |
675 | public static function get_subwiki_files_parameters() { | |
676 | return new external_function_parameters ( | |
677 | array( | |
678 | 'wikiid' => new external_value(PARAM_INT, 'Wiki instance ID.'), | |
679 | 'groupid' => new external_value(PARAM_INT, 'Subwiki\'s group ID, -1 means current group. It will be ignored' | |
680 | . ' if the wiki doesn\'t use groups.', VALUE_DEFAULT, -1), | |
681 | 'userid' => new external_value(PARAM_INT, 'Subwiki\'s user ID, 0 means current user. It will be ignored' | |
682 | .' in collaborative wikis.', VALUE_DEFAULT, 0) | |
683 | ) | |
684 | ); | |
685 | } | |
686 | ||
687 | /** | |
688 | * Returns the list of files from a specific subwiki. | |
689 | * | |
690 | * @param int $wikiid The wiki instance ID. | |
691 | * @param int $groupid The group ID. If not defined, use current group. | |
692 | * @param int $userid The user ID. If not defined, use current user. | |
693 | * @return array Containing a list of warnings and a list of files. | |
694 | * @since Moodle 3.1 | |
695 | * @throws moodle_exception | |
696 | */ | |
697 | public static function get_subwiki_files($wikiid, $groupid = -1, $userid = 0) { | |
698 | ||
699 | $returnedfiles = array(); | |
700 | $warnings = array(); | |
701 | ||
702 | $params = self::validate_parameters(self::get_subwiki_files_parameters(), | |
703 | array( | |
704 | 'wikiid' => $wikiid, | |
705 | 'groupid' => $groupid, | |
706 | 'userid' => $userid | |
707 | ) | |
708 | ); | |
709 | ||
710 | // Get wiki instance. | |
711 | if (!$wiki = wiki_get_wiki($params['wikiid'])) { | |
712 | throw new moodle_exception('incorrectwikiid', 'wiki'); | |
713 | } | |
714 | list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki'); | |
715 | $context = context_module::instance($cm->id); | |
716 | self::validate_context($context); | |
717 | ||
718 | // Determine groupid and userid to use. | |
719 | list($groupid, $userid) = self::determine_group_and_user($cm, $wiki, $params['groupid'], $params['userid']); | |
720 | ||
721 | // Get subwiki and validate it. | |
722 | $subwiki = wiki_get_subwiki_by_group_and_user_with_validation($wiki, $groupid, $userid); | |
723 | ||
724 | // Get subwiki based on group and user. | |
725 | if ($subwiki === false) { | |
726 | throw new moodle_exception('cannotviewfiles', 'wiki'); | |
727 | } else if ($subwiki->id != -1) { | |
728 | // The subwiki exists, let's get the files. | |
729 | $fs = get_file_storage(); | |
730 | if ($files = $fs->get_area_files($context->id, 'mod_wiki', 'attachments', $subwiki->id, 'filename', false)) { | |
731 | foreach ($files as $file) { | |
732 | $filename = $file->get_filename(); | |
733 | $fileurl = moodle_url::make_webservice_pluginfile_url( | |
734 | $context->id, 'mod_wiki', 'attachments', $subwiki->id, '/', $filename); | |
735 | ||
736 | $returnedfiles[] = array( | |
737 | 'filename' => $filename, | |
738 | 'mimetype' => $file->get_mimetype(), | |
739 | 'fileurl' => $fileurl->out(false), | |
740 | 'filepath' => $file->get_filepath(), | |
741 | 'filesize' => $file->get_filesize(), | |
742 | 'timemodified' => $file->get_timemodified() | |
743 | ); | |
744 | } | |
745 | } | |
746 | } | |
747 | ||
748 | $result = array(); | |
749 | $result['files'] = $returnedfiles; | |
750 | $result['warnings'] = $warnings; | |
751 | return $result; | |
752 | } | |
753 | ||
754 | /** | |
755 | * Describes the get_subwiki_pages return value. | |
756 | * | |
757 | * @return external_single_structure | |
758 | * @since Moodle 3.1 | |
759 | */ | |
760 | public static function get_subwiki_files_returns() { | |
761 | ||
762 | return new external_single_structure( | |
763 | array( | |
764 | 'files' => new external_multiple_structure( | |
765 | new external_single_structure( | |
766 | array( | |
767 | 'filename' => new external_value(PARAM_FILE, 'File name.'), | |
768 | 'filepath' => new external_value(PARAM_PATH, 'File path.'), | |
769 | 'filesize' => new external_value(PARAM_INT, 'File size.'), | |
770 | 'fileurl' => new external_value(PARAM_URL, 'Downloadable file url.'), | |
771 | 'timemodified' => new external_value(PARAM_INT, 'Time modified.'), | |
772 | 'mimetype' => new external_value(PARAM_RAW, 'File mime type.'), | |
773 | ), 'Files' | |
774 | ) | |
775 | ), | |
776 | 'warnings' => new external_warnings(), | |
777 | ) | |
778 | ); | |
779 | } | |
780 | ||
781 | /** | |
782 | * Utility function for determining the groupid and userid to use. | |
783 | * | |
784 | * @param stdClass $cm The course module. | |
785 | * @param stdClass $wiki The wiki. | |
786 | * @param int $groupid Group ID. If not defined, use current group. | |
787 | * @param int $userid User ID. If not defined, use current user. | |
788 | * @return array Array containing the courseid and userid. | |
789 | * @since Moodle 3.1 | |
790 | */ | |
791 | protected static function determine_group_and_user($cm, $wiki, $groupid = -1, $userid = 0) { | |
792 | global $USER; | |
793 | ||
794 | $currentgroup = groups_get_activity_group($cm); | |
795 | if ($currentgroup === false) { | |
796 | // Activity doesn't use groups. | |
797 | $groupid = 0; | |
798 | } else if ($groupid == -1) { | |
799 | // Use current group. | |
800 | $groupid = !empty($currentgroup) ? $currentgroup : 0; | |
801 | } | |
802 | ||
803 | // Determine user. | |
804 | if ($wiki->wikimode == 'collaborative') { | |
805 | // Collaborative wikis don't use userid in subwikis. | |
806 | $userid = 0; | |
807 | } else if (empty($userid)) { | |
808 | // Use current user. | |
809 | $userid = $USER->id; | |
810 | } | |
811 | ||
812 | return array($groupid, $userid); | |
813 | } | |
814 | ||
23cbaefb | 815 | } |