MDL-56103 wiki: Return version in get_page_contents
[moodle.git] / mod / wiki / classes / external.php
CommitLineData
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
27defined('MOODLE_INTERNAL') || die;
28
29require_once($CFG->libdir . '/externallib.php');
30require_once($CFG->dirroot . '/mod/wiki/lib.php');
31require_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 */
42class 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);
7ef49bd3 107 $module['introfiles'] = external_util::get_area_files($context->id, 'mod_wiki', 'intro', false, false);
23cbaefb
DP
108
109 $viewablefields = array('firstpagetitle', 'wikimode', 'defaultformat', 'forceformat', 'editbegin', 'editend',
110 'section', 'visible', 'groupmode', 'groupingid');
111 }
112
113 // Check additional permissions for returning optional private settings.
114 if (has_capability('moodle/course:manageactivities', $context)) {
115 $additionalfields = array('timecreated', 'timemodified');
116 $viewablefields = array_merge($viewablefields, $additionalfields);
117 }
118
119 foreach ($viewablefields as $field) {
120 $module[$field] = $wiki->{$field};
121 }
122
123 // Check if user can add new pages.
124 $module['cancreatepages'] = wiki_can_create_pages($context);
125
126 $returnedwikis[] = $module;
127 }
128 }
129
130 $result = array();
131 $result['wikis'] = $returnedwikis;
132 $result['warnings'] = $warnings;
133 return $result;
134 }
135
136 /**
137 * Describes the get_wikis_by_courses return value.
138 *
139 * @return external_single_structure
140 * @since Moodle 3.1
141 */
142 public static function get_wikis_by_courses_returns() {
143
144 return new external_single_structure(
145 array(
146 'wikis' => new external_multiple_structure(
147 new external_single_structure(
148 array(
149 'id' => new external_value(PARAM_INT, 'Wiki ID.'),
150 'coursemodule' => new external_value(PARAM_INT, 'Course module ID.'),
151 'course' => new external_value(PARAM_INT, 'Course ID.'),
152 'name' => new external_value(PARAM_RAW, 'Wiki name.'),
153 'intro' => new external_value(PARAM_RAW, 'Wiki intro.', VALUE_OPTIONAL),
154 'introformat' => new external_format_value('Wiki intro format.', VALUE_OPTIONAL),
7ef49bd3 155 'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL),
23cbaefb
DP
156 'timecreated' => new external_value(PARAM_INT, 'Time of creation.', VALUE_OPTIONAL),
157 'timemodified' => new external_value(PARAM_INT, 'Time of last modification.', VALUE_OPTIONAL),
158 'firstpagetitle' => new external_value(PARAM_RAW, 'First page title.', VALUE_OPTIONAL),
159 'wikimode' => new external_value(PARAM_TEXT, 'Wiki mode (individual, collaborative).', VALUE_OPTIONAL),
160 'defaultformat' => new external_value(PARAM_TEXT, 'Wiki\'s default format (html, creole, nwiki).',
161 VALUE_OPTIONAL),
162 'forceformat' => new external_value(PARAM_INT, '1 if format is forced, 0 otherwise.',
163 VALUE_OPTIONAL),
164 'editbegin' => new external_value(PARAM_INT, 'Edit begin.', VALUE_OPTIONAL),
165 'editend' => new external_value(PARAM_INT, 'Edit end.', VALUE_OPTIONAL),
166 'section' => new external_value(PARAM_INT, 'Course section ID.', VALUE_OPTIONAL),
167 'visible' => new external_value(PARAM_INT, '1 if visible, 0 otherwise.', VALUE_OPTIONAL),
168 'groupmode' => new external_value(PARAM_INT, 'Group mode.', VALUE_OPTIONAL),
169 'groupingid' => new external_value(PARAM_INT, 'Group ID.', VALUE_OPTIONAL),
170 'cancreatepages' => new external_value(PARAM_BOOL, 'True if user can create pages.'),
171 ), 'Wikis'
172 )
173 ),
174 'warnings' => new external_warnings(),
175 )
176 );
177 }
178
557b58b0
DP
179 /**
180 * Describes the parameters for view_wiki.
181 *
182 * @return external_function_parameters
183 * @since Moodle 3.1
184 */
185 public static function view_wiki_parameters() {
186 return new external_function_parameters (
187 array(
188 'wikiid' => new external_value(PARAM_INT, 'Wiki instance ID.')
189 )
190 );
191 }
192
193 /**
194 * Trigger the course module viewed event and update the module completion status.
195 *
196 * @param int $wikiid The wiki instance ID.
197 * @return array of warnings and status result.
198 * @since Moodle 3.1
199 */
200 public static function view_wiki($wikiid) {
201
202 $params = self::validate_parameters(self::view_wiki_parameters(),
203 array(
204 'wikiid' => $wikiid
205 ));
206 $warnings = array();
207
208 // Get wiki instance.
209 if (!$wiki = wiki_get_wiki($params['wikiid'])) {
210 throw new moodle_exception('incorrectwikiid', 'wiki');
211 }
212
213 // Permission validation.
214 list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki');
215 $context = context_module::instance($cm->id);
216 self::validate_context($context);
217
218 // Check if user can view this wiki.
219 // We don't use wiki_user_can_view because it requires to have a valid subwiki for the user.
220 if (!has_capability('mod/wiki:viewpage', $context)) {
221 throw new moodle_exception('cannotviewpage', 'wiki');
222 }
223
224 // Trigger course_module_viewed event and completion.
225 wiki_view($wiki, $course, $cm, $context);
226
227 $result = array();
228 $result['status'] = true;
229 $result['warnings'] = $warnings;
230 return $result;
231 }
232
233 /**
234 * Describes the view_wiki return value.
235 *
236 * @return external_single_structure
237 * @since Moodle 3.1
238 */
239 public static function view_wiki_returns() {
240 return new external_single_structure(
241 array(
242 'status' => new external_value(PARAM_BOOL, 'Status: true if success.'),
243 'warnings' => new external_warnings()
244 )
245 );
246 }
247
248 /**
249 * Describes the parameters for view_page.
250 *
251 * @return external_function_parameters
252 * @since Moodle 3.1
253 */
254 public static function view_page_parameters() {
255 return new external_function_parameters (
256 array(
257 'pageid' => new external_value(PARAM_INT, 'Wiki page ID.'),
258 )
259 );
260 }
261
262 /**
263 * Trigger the page viewed event and update the module completion status.
264 *
265 * @param int $pageid The page ID.
266 * @return array of warnings and status result.
267 * @since Moodle 3.1
268 * @throws moodle_exception if page is not valid.
269 */
270 public static function view_page($pageid) {
271
272 $params = self::validate_parameters(self::view_page_parameters(),
273 array(
274 'pageid' => $pageid
275 ));
276 $warnings = array();
277
278 // Get wiki page.
279 if (!$page = wiki_get_page($params['pageid'])) {
280 throw new moodle_exception('incorrectpageid', 'wiki');
281 }
282
283 // Get wiki instance.
284 if (!$wiki = wiki_get_wiki_from_pageid($params['pageid'])) {
285 throw new moodle_exception('incorrectwikiid', 'wiki');
286 }
287
288 // Permission validation.
289 list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki');
290 $context = context_module::instance($cm->id);
291 self::validate_context($context);
292
293 // Check if user can view this wiki.
294 if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
295 throw new moodle_exception('incorrectsubwikiid', 'wiki');
296 }
297 if (!wiki_user_can_view($subwiki, $wiki)) {
298 throw new moodle_exception('cannotviewpage', 'wiki');
299 }
300
301 // Trigger page_viewed event and completion.
302 wiki_page_view($wiki, $page, $course, $cm, $context);
303
304 $result = array();
305 $result['status'] = true;
306 $result['warnings'] = $warnings;
307 return $result;
308 }
309
310 /**
311 * Describes the view_page return value.
312 *
313 * @return external_single_structure
314 * @since Moodle 3.1
315 */
316 public static function view_page_returns() {
317 return new external_single_structure(
318 array(
319 'status' => new external_value(PARAM_BOOL, 'Status: true if success.'),
320 'warnings' => new external_warnings()
321 )
322 );
323 }
324
44f1b701
DP
325 /**
326 * Describes the parameters for get_subwikis.
327 *
328 * @return external_function_parameters
329 * @since Moodle 3.1
330 */
331 public static function get_subwikis_parameters() {
332 return new external_function_parameters (
333 array(
334 'wikiid' => new external_value(PARAM_INT, 'Wiki instance ID.')
335 )
336 );
337 }
338
339 /**
340 * Returns the list of subwikis the user can see in a specific wiki.
341 *
342 * @param int $wikiid The wiki instance ID.
343 * @return array Containing a list of warnings and a list of subwikis.
344 * @since Moodle 3.1
345 */
346 public static function get_subwikis($wikiid) {
347 global $USER;
348
349 $warnings = array();
350
351 $params = self::validate_parameters(self::get_subwikis_parameters(), array('wikiid' => $wikiid));
352
353 // Get wiki instance.
354 if (!$wiki = wiki_get_wiki($params['wikiid'])) {
355 throw new moodle_exception('incorrectwikiid', 'wiki');
356 }
357
358 // Validate context and capabilities.
359 list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki');
360 $context = context_module::instance($cm->id);
361 self::validate_context($context);
362 require_capability('mod/wiki:viewpage', $context);
363
364 $returnedsubwikis = wiki_get_visible_subwikis($wiki, $cm, $context);
365 foreach ($returnedsubwikis as $subwiki) {
366 $subwiki->canedit = wiki_user_can_edit($subwiki);
367 }
368
369 $result = array();
370 $result['subwikis'] = $returnedsubwikis;
371 $result['warnings'] = $warnings;
372 return $result;
373 }
374
375 /**
376 * Describes the get_subwikis return value.
377 *
378 * @return external_single_structure
379 * @since Moodle 3.1
380 */
381 public static function get_subwikis_returns() {
382 return new external_single_structure(
383 array(
384 'subwikis' => new external_multiple_structure(
385 new external_single_structure(
386 array(
387 'id' => new external_value(PARAM_INT, 'Subwiki ID.'),
388 'wikiid' => new external_value(PARAM_INT, 'Wiki ID.'),
389 'groupid' => new external_value(PARAM_RAW, 'Group ID.'),
390 'userid' => new external_value(PARAM_INT, 'User ID.'),
391 'canedit' => new external_value(PARAM_BOOL, 'True if user can edit the subwiki.'),
392 ), 'Subwikis'
393 )
394 ),
395 'warnings' => new external_warnings(),
396 )
397 );
398 }
399
96abf9af
DP
400 /**
401 * Describes the parameters for get_subwiki_pages.
402 *
403 * @return external_function_parameters
404 * @since Moodle 3.1
405 */
406 public static function get_subwiki_pages_parameters() {
407 return new external_function_parameters (
408 array(
409 'wikiid' => new external_value(PARAM_INT, 'Wiki instance ID.'),
410 'groupid' => new external_value(PARAM_INT, 'Subwiki\'s group ID, -1 means current group. It will be ignored'
411 . ' if the wiki doesn\'t use groups.', VALUE_DEFAULT, -1),
412 'userid' => new external_value(PARAM_INT, 'Subwiki\'s user ID, 0 means current user. It will be ignored'
413 .' in collaborative wikis.', VALUE_DEFAULT, 0),
414 'options' => new external_single_structure(
415 array(
416 'sortby' => new external_value(PARAM_ALPHA,
417 'Field to sort by (id, title, ...).', VALUE_DEFAULT, 'title'),
418 'sortdirection' => new external_value(PARAM_ALPHA,
419 'Sort direction: ASC or DESC.', VALUE_DEFAULT, 'ASC'),
420 'includecontent' => new external_value(PARAM_INT,
2a84fae0 421 'Include each page contents or just the contents size.', VALUE_DEFAULT, 1),
96abf9af
DP
422 ), 'Options', VALUE_DEFAULT, array()),
423 )
424 );
425 }
426
427 /**
428 * Returns the list of pages from a specific subwiki.
429 *
430 * @param int $wikiid The wiki instance ID.
431 * @param int $groupid The group ID. If not defined, use current group.
432 * @param int $userid The user ID. If not defined, use current user.
433 * @param array $options Several options like sort by, sort direction, ...
434 * @return array Containing a list of warnings and a list of pages.
435 * @since Moodle 3.1
436 */
437 public static function get_subwiki_pages($wikiid, $groupid = -1, $userid = 0, $options = array()) {
96abf9af
DP
438
439 $returnedpages = array();
440 $warnings = array();
441
442 $params = self::validate_parameters(self::get_subwiki_pages_parameters(),
443 array(
444 'wikiid' => $wikiid,
445 'groupid' => $groupid,
446 'userid' => $userid,
447 'options' => $options
448 )
449 );
450
451 // Get wiki instance.
452 if (!$wiki = wiki_get_wiki($params['wikiid'])) {
453 throw new moodle_exception('incorrectwikiid', 'wiki');
454 }
455 list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki');
456 $context = context_module::instance($cm->id);
457 self::validate_context($context);
458
62e20cfe
DP
459 // Determine groupid and userid to use.
460 list($groupid, $userid) = self::determine_group_and_user($cm, $wiki, $params['groupid'], $params['userid']);
96abf9af 461
62e20cfe
DP
462 // Get subwiki and validate it.
463 $subwiki = wiki_get_subwiki_by_group_and_user_with_validation($wiki, $groupid, $userid);
96abf9af 464
62e20cfe
DP
465 if ($subwiki === false) {
466 throw new moodle_exception('cannotviewpage', 'wiki');
467 } else if ($subwiki->id != -1) {
96abf9af
DP
468
469 // Set sort param.
470 $options = $params['options'];
471 if (!empty($options['sortby'])) {
472 if ($options['sortdirection'] != 'ASC' && $options['sortdirection'] != 'DESC') {
473 // Invalid sort direction. Use default.
474 $options['sortdirection'] = 'ASC';
475 }
476 $sort = $options['sortby'] . ' ' . $options['sortdirection'];
477 }
478
479 $pages = wiki_get_page_list($subwiki->id, $sort);
480 $caneditpages = wiki_user_can_edit($subwiki);
481 $firstpage = wiki_get_first_page($subwiki->id);
482
483 foreach ($pages as $page) {
484 $retpage = array(
485 'id' => $page->id,
486 'subwikiid' => $page->subwikiid,
487 'title' => external_format_string($page->title, $context->id),
488 'timecreated' => $page->timecreated,
489 'timemodified' => $page->timemodified,
490 'timerendered' => $page->timerendered,
491 'userid' => $page->userid,
492 'pageviews' => $page->pageviews,
493 'readonly' => $page->readonly,
494 'caneditpage' => $caneditpages,
495 'firstpage' => $page->id == $firstpage->id
496 );
497
b1be9b20
DP
498 // Refresh page cached content if needed.
499 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
500 if ($content = wiki_refresh_cachedcontent($page)) {
501 $page = $content['page'];
96abf9af 502 }
b1be9b20
DP
503 }
504 list($cachedcontent, $contentformat) = external_format_text(
505 $page->cachedcontent, FORMAT_HTML, $context->id, 'mod_wiki', 'attachments', $subwiki->id);
96abf9af 506
b1be9b20
DP
507 if ($options['includecontent']) {
508 // Return the page content.
509 $retpage['cachedcontent'] = $cachedcontent;
510 $retpage['contentformat'] = $contentformat;
511 } else {
512 // Return the size of the content.
513 if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
514 $retpage['contentsize'] = mb_strlen($cachedcontent, '8bit');
515 } else {
516 $retpage['contentsize'] = strlen($cachedcontent);
517 }
96abf9af
DP
518 }
519
520 $returnedpages[] = $retpage;
521 }
96abf9af
DP
522 }
523
524 $result = array();
525 $result['pages'] = $returnedpages;
526 $result['warnings'] = $warnings;
527 return $result;
528 }
529
530 /**
531 * Describes the get_subwiki_pages return value.
532 *
533 * @return external_single_structure
534 * @since Moodle 3.1
535 */
536 public static function get_subwiki_pages_returns() {
537
538 return new external_single_structure(
539 array(
540 'pages' => new external_multiple_structure(
541 new external_single_structure(
542 array(
543 'id' => new external_value(PARAM_INT, 'Page ID.'),
544 'subwikiid' => new external_value(PARAM_INT, 'Page\'s subwiki ID.'),
545 'title' => new external_value(PARAM_RAW, 'Page title.'),
546 'timecreated' => new external_value(PARAM_INT, 'Time of creation.'),
547 'timemodified' => new external_value(PARAM_INT, 'Time of last modification.'),
548 'timerendered' => new external_value(PARAM_INT, 'Time of last renderization.'),
549 'userid' => new external_value(PARAM_INT, 'ID of the user that last modified the page.'),
550 'pageviews' => new external_value(PARAM_INT, 'Number of times the page has been viewed.'),
551 'readonly' => new external_value(PARAM_INT, '1 if readonly, 0 otherwise.'),
552 'caneditpage' => new external_value(PARAM_BOOL, 'True if user can edit the page.'),
553 'firstpage' => new external_value(PARAM_BOOL, 'True if it\'s the first page.'),
554 'cachedcontent' => new external_value(PARAM_RAW, 'Page contents.', VALUE_OPTIONAL),
555 'contentformat' => new external_format_value('cachedcontent', VALUE_OPTIONAL),
b1be9b20
DP
556 'contentsize' => new external_value(PARAM_INT, 'Size of page contents in bytes (doesn\'t include'.
557 ' size of attached files).', VALUE_OPTIONAL),
96abf9af
DP
558 ), 'Pages'
559 )
560 ),
561 'warnings' => new external_warnings(),
562 )
563 );
564 }
565
5d94e343
DP
566 /**
567 * Describes the parameters for get_page_contents.
568 *
569 * @return external_function_parameters
570 * @since Moodle 3.1
571 */
572 public static function get_page_contents_parameters() {
573 return new external_function_parameters (
574 array(
575 'pageid' => new external_value(PARAM_INT, 'Page ID.')
576 )
577 );
578 }
579
580 /**
581 * Get a page contents.
582 *
583 * @param int $pageid The page ID.
584 * @return array of warnings and page data.
585 * @since Moodle 3.1
586 */
587 public static function get_page_contents($pageid) {
588
589 $params = self::validate_parameters(self::get_page_contents_parameters(),
590 array(
591 'pageid' => $pageid
592 )
593 );
594 $warnings = array();
595
596 // Get wiki page.
597 if (!$page = wiki_get_page($params['pageid'])) {
598 throw new moodle_exception('incorrectpageid', 'wiki');
599 }
600
601 // Get wiki instance.
602 if (!$wiki = wiki_get_wiki_from_pageid($params['pageid'])) {
603 throw new moodle_exception('incorrectwikiid', 'wiki');
604 }
605
606 // Permission validation.
607 $cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course);
608 $context = context_module::instance($cm->id);
609 self::validate_context($context);
610
611 // Check if user can view this wiki.
612 if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
613 throw new moodle_exception('incorrectsubwikiid', 'wiki');
614 }
615 if (!wiki_user_can_view($subwiki, $wiki)) {
616 throw new moodle_exception('cannotviewpage', 'wiki');
617 }
618
619 $returnedpage = array();
620 $returnedpage['id'] = $page->id;
621 $returnedpage['wikiid'] = $wiki->id;
622 $returnedpage['subwikiid'] = $page->subwikiid;
623 $returnedpage['groupid'] = $subwiki->groupid;
624 $returnedpage['userid'] = $subwiki->userid;
625 $returnedpage['title'] = $page->title;
626
627 // Refresh page cached content if needed.
628 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
629 if ($content = wiki_refresh_cachedcontent($page)) {
630 $page = $content['page'];
631 }
632 }
633
634 list($returnedpage['cachedcontent'], $returnedpage['contentformat']) = external_format_text(
635 $page->cachedcontent, FORMAT_HTML, $context->id, 'mod_wiki', 'attachments', $subwiki->id);
636 $returnedpage['caneditpage'] = wiki_user_can_edit($subwiki);
637
7f98de7d
DP
638 // Get page version.
639 $version = wiki_get_current_version($page->id);
640 if (!empty($version)) {
641 $returnedpage['version'] = $version->version;
642 }
643
5d94e343
DP
644 $result = array();
645 $result['page'] = $returnedpage;
646 $result['warnings'] = $warnings;
647 return $result;
648 }
649
650 /**
651 * Describes the get_page_contents return value.
652 *
653 * @return external_single_structure
654 * @since Moodle 3.1
655 */
656 public static function get_page_contents_returns() {
657 return new external_single_structure(
658 array(
659 'page' => new external_single_structure(
660 array(
661 'id' => new external_value(PARAM_INT, 'Page ID.'),
662 'wikiid' => new external_value(PARAM_INT, 'Page\'s wiki ID.'),
663 'subwikiid' => new external_value(PARAM_INT, 'Page\'s subwiki ID.'),
664 'groupid' => new external_value(PARAM_INT, 'Page\'s group ID.'),
665 'userid' => new external_value(PARAM_INT, 'Page\'s user ID.'),
666 'title' => new external_value(PARAM_RAW, 'Page title.'),
667 'cachedcontent' => new external_value(PARAM_RAW, 'Page contents.'),
668 'contentformat' => new external_format_value('cachedcontent', VALUE_OPTIONAL),
7f98de7d
DP
669 'caneditpage' => new external_value(PARAM_BOOL, 'True if user can edit the page.'),
670 'version' => new external_value(PARAM_INT, 'Latest version of the page.', VALUE_OPTIONAL),
5d94e343
DP
671 ), 'Page'
672 ),
673 'warnings' => new external_warnings()
674 )
675 );
676 }
677
62e20cfe
DP
678 /**
679 * Describes the parameters for get_subwiki_files.
680 *
681 * @return external_function_parameters
682 * @since Moodle 3.1
683 */
684 public static function get_subwiki_files_parameters() {
685 return new external_function_parameters (
686 array(
687 'wikiid' => new external_value(PARAM_INT, 'Wiki instance ID.'),
688 'groupid' => new external_value(PARAM_INT, 'Subwiki\'s group ID, -1 means current group. It will be ignored'
689 . ' if the wiki doesn\'t use groups.', VALUE_DEFAULT, -1),
690 'userid' => new external_value(PARAM_INT, 'Subwiki\'s user ID, 0 means current user. It will be ignored'
691 .' in collaborative wikis.', VALUE_DEFAULT, 0)
692 )
693 );
694 }
695
696 /**
697 * Returns the list of files from a specific subwiki.
698 *
699 * @param int $wikiid The wiki instance ID.
700 * @param int $groupid The group ID. If not defined, use current group.
701 * @param int $userid The user ID. If not defined, use current user.
702 * @return array Containing a list of warnings and a list of files.
703 * @since Moodle 3.1
704 * @throws moodle_exception
705 */
706 public static function get_subwiki_files($wikiid, $groupid = -1, $userid = 0) {
707
708 $returnedfiles = array();
709 $warnings = array();
710
711 $params = self::validate_parameters(self::get_subwiki_files_parameters(),
712 array(
713 'wikiid' => $wikiid,
714 'groupid' => $groupid,
715 'userid' => $userid
716 )
717 );
718
719 // Get wiki instance.
720 if (!$wiki = wiki_get_wiki($params['wikiid'])) {
721 throw new moodle_exception('incorrectwikiid', 'wiki');
722 }
723 list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki');
724 $context = context_module::instance($cm->id);
725 self::validate_context($context);
726
727 // Determine groupid and userid to use.
728 list($groupid, $userid) = self::determine_group_and_user($cm, $wiki, $params['groupid'], $params['userid']);
729
730 // Get subwiki and validate it.
731 $subwiki = wiki_get_subwiki_by_group_and_user_with_validation($wiki, $groupid, $userid);
732
733 // Get subwiki based on group and user.
734 if ($subwiki === false) {
735 throw new moodle_exception('cannotviewfiles', 'wiki');
736 } else if ($subwiki->id != -1) {
737 // The subwiki exists, let's get the files.
14590070 738 $returnedfiles = external_util::get_area_files($context->id, 'mod_wiki', 'attachments', $subwiki->id);
62e20cfe
DP
739 }
740
741 $result = array();
742 $result['files'] = $returnedfiles;
743 $result['warnings'] = $warnings;
744 return $result;
745 }
746
747 /**
748 * Describes the get_subwiki_pages return value.
749 *
750 * @return external_single_structure
751 * @since Moodle 3.1
752 */
753 public static function get_subwiki_files_returns() {
754
755 return new external_single_structure(
756 array(
14590070 757 'files' => new external_files('Files'),
62e20cfe
DP
758 'warnings' => new external_warnings(),
759 )
760 );
761 }
762
763 /**
764 * Utility function for determining the groupid and userid to use.
765 *
766 * @param stdClass $cm The course module.
767 * @param stdClass $wiki The wiki.
768 * @param int $groupid Group ID. If not defined, use current group.
769 * @param int $userid User ID. If not defined, use current user.
770 * @return array Array containing the courseid and userid.
771 * @since Moodle 3.1
772 */
773 protected static function determine_group_and_user($cm, $wiki, $groupid = -1, $userid = 0) {
774 global $USER;
775
776 $currentgroup = groups_get_activity_group($cm);
777 if ($currentgroup === false) {
778 // Activity doesn't use groups.
779 $groupid = 0;
780 } else if ($groupid == -1) {
781 // Use current group.
782 $groupid = !empty($currentgroup) ? $currentgroup : 0;
783 }
784
785 // Determine user.
786 if ($wiki->wikimode == 'collaborative') {
787 // Collaborative wikis don't use userid in subwikis.
788 $userid = 0;
789 } else if (empty($userid)) {
790 // Use current user.
791 $userid = $USER->id;
792 }
793
794 return array($groupid, $userid);
795 }
796
20dca621
PFO
797 /**
798 * Describes the parameters for get_page_for_editing.
799 *
800 * @return external_function_parameters
801 * @since Moodle 3.1
802 */
803 public static function get_page_for_editing_parameters() {
804 return new external_function_parameters (
805 array(
806 'pageid' => new external_value(PARAM_INT, 'Page ID to edit.'),
c13c21b9
PFO
807 'section' => new external_value(PARAM_RAW, 'Section page title.', VALUE_DEFAULT, null),
808 'lockonly' => new external_value(PARAM_BOOL, 'Just renew lock and not return content.', VALUE_DEFAULT, false)
20dca621
PFO
809 )
810 );
811 }
812
813 /**
814 * Locks and retrieves info of page-section to be edited.
815 *
816 * @param int $pageid The page ID.
817 * @param string $section Section page title.
c13c21b9 818 * @param boolean $lockonly If true: Just renew lock and not return content.
20dca621
PFO
819 * @return array of warnings and page data.
820 * @since Moodle 3.1
821 */
c13c21b9 822 public static function get_page_for_editing($pageid, $section = null, $lockonly = false) {
20dca621
PFO
823 global $USER;
824
825 $params = self::validate_parameters(self::get_page_for_editing_parameters(),
826 array(
827 'pageid' => $pageid,
c13c21b9
PFO
828 'section' => $section,
829 'lockonly' => $lockonly
20dca621
PFO
830 )
831 );
832
833 $warnings = array();
834
835 // Get wiki page.
836 if (!$page = wiki_get_page($params['pageid'])) {
837 throw new moodle_exception('incorrectpageid', 'wiki');
838 }
839
840 // Get wiki instance.
841 if (!$wiki = wiki_get_wiki_from_pageid($params['pageid'])) {
842 throw new moodle_exception('incorrectwikiid', 'wiki');
843 }
844
845 // Get subwiki instance.
846 if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
847 throw new moodle_exception('incorrectsubwikiid', 'wiki');
848 }
849
850 // Permission validation.
851 $cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course);
852 $context = context_module::instance($cm->id);
853 self::validate_context($context);
854
855 if (!wiki_user_can_edit($subwiki)) {
856 throw new moodle_exception('cannoteditpage', 'wiki');
857 }
858
859 if (!wiki_set_lock($params['pageid'], $USER->id, $params['section'], true)) {
860 throw new moodle_exception('pageislocked', 'wiki');
861 }
862
863 $version = wiki_get_current_version($page->id);
864 if (empty($version)) {
865 throw new moodle_exception('versionerror', 'wiki');
866 }
867
20dca621 868 $pagesection = array();
20dca621
PFO
869 $pagesection['version'] = $version->version;
870
c13c21b9
PFO
871 // Content requested to be returned.
872 if (!$lockonly) {
873 if (!is_null($params['section'])) {
874 $content = wiki_parser_proxy::get_section($version->content, $version->contentformat, $params['section']);
875 } else {
876 $content = $version->content;
877 }
878
879 $pagesection['content'] = $content;
880 $pagesection['contentformat'] = $version->contentformat;
881 }
882
20dca621
PFO
883 $result = array();
884 $result['pagesection'] = $pagesection;
885 $result['warnings'] = $warnings;
886 return $result;
887
888 }
889
890 /**
891 * Describes the get_page_for_editing return value.
892 *
893 * @return external_single_structure
894 * @since Moodle 3.1
895 */
896 public static function get_page_for_editing_returns() {
897 return new external_single_structure(
898 array(
899 'pagesection' => new external_single_structure(
900 array(
c13c21b9
PFO
901 'content' => new external_value(PARAM_RAW, 'The contents of the page-section to be edited.',
902 VALUE_OPTIONAL),
903 'contentformat' => new external_value(PARAM_TEXT, 'Format of the original content of the page.',
904 VALUE_OPTIONAL),
20dca621
PFO
905 'version' => new external_value(PARAM_INT, 'Latest version of the page.'),
906 'warnings' => new external_warnings()
907 )
908 )
909 )
910 );
911 }
912
a6b6f0d2
PFO
913 /**
914 * Describes the parameters for new_page.
915 *
916 * @return external_function_parameters
917 * @since Moodle 3.1
918 */
919 public static function new_page_parameters() {
920 return new external_function_parameters (
921 array(
922 'title' => new external_value(PARAM_TEXT, 'New page title.'),
923 'content' => new external_value(PARAM_RAW, 'Page contents.'),
924 'contentformat' => new external_value(PARAM_TEXT, 'Page contents format. If an invalid format is provided, default
925 wiki format is used.', VALUE_DEFAULT, null),
926 'subwikiid' => new external_value(PARAM_INT, 'Page\'s subwiki ID.', VALUE_DEFAULT, null),
927 'wikiid' => new external_value(PARAM_INT, 'Page\'s wiki ID. Used if subwiki does not exists.', VALUE_DEFAULT,
928 null),
929 'userid' => new external_value(PARAM_INT, 'Subwiki\'s user ID. Used if subwiki does not exists.', VALUE_DEFAULT,
930 null),
931 'groupid' => new external_value(PARAM_INT, 'Subwiki\'s group ID. Used if subwiki does not exists.', VALUE_DEFAULT,
932 null)
933 )
934 );
935 }
936
937 /**
938 * Creates a new page.
939 *
940 * @param string $title New page title.
941 * @param string $content Page contents.
942 * @param int $contentformat Page contents format. If an invalid format is provided, default wiki format is used.
943 * @param int $subwikiid The Subwiki ID where to store the page.
944 * @param int $wikiid Page\'s wiki ID. Used if subwiki does not exists.
945 * @param int $userid Subwiki\'s user ID. Used if subwiki does not exists.
946 * @param int $groupid Subwiki\'s group ID. Used if subwiki does not exists.
947 * @return array of warnings and page data.
948 * @since Moodle 3.1
949 */
950 public static function new_page($title, $content, $contentformat = null, $subwikiid = null, $wikiid = null, $userid = null,
951 $groupid = null) {
952 global $USER;
953
954 $params = self::validate_parameters(self::new_page_parameters(),
955 array(
956 'title' => $title,
957 'content' => $content,
958 'contentformat' => $contentformat,
959 'subwikiid' => $subwikiid,
960 'wikiid' => $wikiid,
961 'userid' => $userid,
962 'groupid' => $groupid
963 )
964 );
965
966 $warnings = array();
967
968 // Get wiki and subwiki instances.
969 if (!empty($params['subwikiid'])) {
970 if (!$subwiki = wiki_get_subwiki($params['subwikiid'])) {
971 throw new moodle_exception('incorrectsubwikiid', 'wiki');
972 }
973
974 if (!$wiki = wiki_get_wiki($subwiki->wikiid)) {
975 throw new moodle_exception('incorrectwikiid', 'wiki');
976 }
977
978 // Permission validation.
979 $cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course);
980 $context = context_module::instance($cm->id);
981 self::validate_context($context);
982
983 } else {
984 if (!$wiki = wiki_get_wiki($params['wikiid'])) {
985 throw new moodle_exception('incorrectwikiid', 'wiki');
986 }
987
988 // Permission validation.
989 $cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course);
990 $context = context_module::instance($cm->id);
991 self::validate_context($context);
992
993 // Determine groupid and userid to use.
994 list($groupid, $userid) = self::determine_group_and_user($cm, $wiki, $params['groupid'], $params['userid']);
995
996 // Get subwiki and validate it.
997 $subwiki = wiki_get_subwiki_by_group_and_user_with_validation($wiki, $groupid, $userid);
998
999 if ($subwiki === false) {
1000 // User cannot view page.
1001 throw new moodle_exception('cannoteditpage', 'wiki');
1002 } else if ($subwiki->id < 0) {
1003 // Subwiki needed to check edit permissions.
1004 if (!wiki_user_can_edit($subwiki)) {
1005 throw new moodle_exception('cannoteditpage', 'wiki');
1006 }
1007
1008 // Subwiki does not exists and it can be created.
1009 $swid = wiki_add_subwiki($wiki->id, $groupid, $userid);
1010 if (!$subwiki = wiki_get_subwiki($swid)) {
1011 throw new moodle_exception('incorrectsubwikiid', 'wiki');
1012 }
1013 }
1014 }
1015
1016 // Subwiki needed to check edit permissions.
1017 if (!wiki_user_can_edit($subwiki)) {
1018 throw new moodle_exception('cannoteditpage', 'wiki');
1019 }
1020
1021 if ($page = wiki_get_page_by_title($subwiki->id, $params['title'])) {
1022 throw new moodle_exception('pageexists', 'wiki');
1023 }
1024
1025 // Ignore invalid formats and use default instead.
1026 if (!$params['contentformat'] || $wiki->forceformat) {
1027 $params['contentformat'] = $wiki->defaultformat;
1028 } else {
1029 $formats = wiki_get_formats();
1030 if (!in_array($params['contentformat'], $formats)) {
1031 $params['contentformat'] = $wiki->defaultformat;
1032 }
1033 }
1034
1035 $newpageid = wiki_create_page($subwiki->id, $params['title'], $params['contentformat'], $USER->id);
1036
1037 if (!$page = wiki_get_page($newpageid)) {
1038 throw new moodle_exception('incorrectpageid', 'wiki');
1039 }
1040
1041 // Save content.
1042 $save = wiki_save_page($page, $params['content'], $USER->id);
1043
1044 if (!$save) {
1045 throw new moodle_exception('savingerror', 'wiki');
1046 }
1047
1048 $result = array();
1049 $result['pageid'] = $page->id;
1050 $result['warnings'] = $warnings;
1051 return $result;
1052 }
1053
1054 /**
1055 * Describes the new_page return value.
1056 *
1057 * @return external_single_structure
1058 * @since Moodle 3.1
1059 */
1060 public static function new_page_returns() {
1061 return new external_single_structure(
1062 array(
1063 'pageid' => new external_value(PARAM_INT, 'New page id.'),
1064 'warnings' => new external_warnings()
1065 )
1066 );
1067 }
1068
cf16f6ed
PFO
1069 /**
1070 * Describes the parameters for edit_page.
1071 *
1072 * @return external_function_parameters
1073 * @since Moodle 3.1
1074 */
1075 public static function edit_page_parameters() {
1076 return new external_function_parameters (
1077 array(
1078 'pageid' => new external_value(PARAM_INT, 'Page ID.'),
1079 'content' => new external_value(PARAM_RAW, 'Page contents.'),
3b40103c 1080 'section' => new external_value(PARAM_RAW, 'Section page title.', VALUE_DEFAULT, null)
cf16f6ed
PFO
1081 )
1082 );
1083 }
1084
1085 /**
1086 * Edit a page contents.
1087 *
1088 * @param int $pageid The page ID.
1089 * @param string $content Page contents.
1090 * @param int $section Section to be edited.
1091 * @return array of warnings and page data.
1092 * @since Moodle 3.1
1093 */
1094 public static function edit_page($pageid, $content, $section = null) {
1095 global $USER;
1096
1097 $params = self::validate_parameters(self::edit_page_parameters(),
1098 array(
1099 'pageid' => $pageid,
1100 'content' => $content,
1101 'section' => $section
1102 )
1103 );
1104 $warnings = array();
1105
1106 // Get wiki page.
1107 if (!$page = wiki_get_page($params['pageid'])) {
1108 throw new moodle_exception('incorrectpageid', 'wiki');
1109 }
1110
1111 // Get wiki instance.
1112 if (!$wiki = wiki_get_wiki_from_pageid($params['pageid'])) {
1113 throw new moodle_exception('incorrectwikiid', 'wiki');
1114 }
1115
1116 // Get subwiki instance.
1117 if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
1118 throw new moodle_exception('incorrectsubwikiid', 'wiki');
1119 }
1120
1121 // Permission validation.
1122 $cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course);
1123 $context = context_module::instance($cm->id);
1124 self::validate_context($context);
1125
1126 if (!wiki_user_can_edit($subwiki)) {
1127 throw new moodle_exception('cannoteditpage', 'wiki');
1128 }
1129
1130 if (wiki_is_page_section_locked($page->id, $USER->id, $params['section'])) {
1131 throw new moodle_exception('pageislocked', 'wiki');
1132 }
1133
1134 // Save content.
1135 if (!is_null($params['section'])) {
1136 $version = wiki_get_current_version($page->id);
1137 $content = wiki_parser_proxy::get_section($version->content, $version->contentformat, $params['section'], false);
1138 if (!$content) {
1139 throw new moodle_exception('invalidsection', 'wiki');
1140 }
1141
1142 $save = wiki_save_section($page, $params['section'], $params['content'], $USER->id);
1143 } else {
1144 $save = wiki_save_page($page, $params['content'], $USER->id);
1145 }
1146
1147 wiki_delete_locks($page->id, $USER->id, $params['section']);
1148
1149 if (!$save) {
1150 throw new moodle_exception('savingerror', 'wiki');
1151 }
1152
1153 $result = array();
1154 $result['pageid'] = $page->id;
1155 $result['warnings'] = $warnings;
1156 return $result;
1157 }
1158
1159 /**
1160 * Describes the edit_page return value.
1161 *
1162 * @return external_single_structure
1163 * @since Moodle 3.1
1164 */
1165 public static function edit_page_returns() {
1166 return new external_single_structure(
1167 array(
1168 'pageid' => new external_value(PARAM_INT, 'Edited page id.'),
1169 'warnings' => new external_warnings()
1170 )
1171 );
1172 }
1173
23cbaefb 1174}