MDL-57878 core_calendar: additional unit test for bailout callback
[moodle.git] / mod / wiki / lib.php
CommitLineData
e5cc530b 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
00710f4c 12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e5cc530b 13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
00710f4c 16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
39fcb981 17
e5cc530b 18/**
19 * Library of functions and constants for module wiki
14a960ac 20 *
00710f4c
DC
21 * It contains the great majority of functions defined by Moodle
22 * that are mandatory to develop a module.
23 *
593b8385 24 * @package mod_wiki
7db38cd5
SH
25 * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
26 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
00710f4c
DC
27 *
28 * @author Jordi Piguillem
29 * @author Marc Alier
30 * @author David Jimenez
31 * @author Josep Arus
32 * @author Kenneth Riba
33 *
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
e5cc530b 35 */
ee3cc995 36
6ffb8fa2
SB
37defined('MOODLE_INTERNAL') || die();
38
e5cc530b 39/**
40 * Given an object containing all the necessary data,
00710f4c 41 * (defined by the form in mod.html) this function
e5cc530b 42 * will create a new instance and return the id number
43 * of the new instance.
44 *
00710f4c
DC
45 * @param object $instance An object from the form in mod.html
46 * @return int The id of the newly inserted wiki record
47 **/
39fcb981 48function wiki_add_instance($wiki) {
c18269c7 49 global $DB;
14a960ac 50
39fcb981 51 $wiki->timemodified = time();
39fcb981 52 # May have to add extra stuff in here #
f6e56f28
JP
53 if (empty($wiki->forceformat)) {
54 $wiki->forceformat = 0;
55 }
00710f4c 56 return $DB->insert_record('wiki', $wiki);
39fcb981 57}
58
e5cc530b 59/**
60 * Given an object containing all the necessary data,
00710f4c 61 * (defined by the form in mod.html) this function
e5cc530b 62 * will update an existing instance with new data.
63 *
00710f4c
DC
64 * @param object $instance An object from the form in mod.html
65 * @return boolean Success/Fail
66 **/
39fcb981 67function wiki_update_instance($wiki) {
c18269c7 68 global $DB;
39fcb981 69
39fcb981 70 $wiki->timemodified = time();
71 $wiki->id = $wiki->instance;
00710f4c
DC
72 if (empty($wiki->forceformat)) {
73 $wiki->forceformat = 0;
39fcb981 74 }
00710f4c
DC
75
76 # May have to add extra stuff in here #
77
78 return $DB->update_record('wiki', $wiki);
39fcb981 79}
80
e5cc530b 81/**
82 * Given an ID of an instance of this module,
83 * this function will permanently delete the instance
84 * and any data that depends on it.
85 *
00710f4c
DC
86 * @param int $id Id of the module instance
87 * @return boolean Success/Failure
88 **/
39fcb981 89function wiki_delete_instance($id) {
00710f4c 90 global $DB;
8f0cd6ef 91
00710f4c 92 if (!$wiki = $DB->get_record('wiki', array('id' => $id))) {
39fcb981 93 return false;
94 }
95
96 $result = true;
97
00710f4c
DC
98 # Get subwiki information #
99 $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id));
8f0cd6ef 100
00710f4c
DC
101 foreach ($subwikis as $subwiki) {
102 # Get existing links, and delete them #
103 if (!$DB->delete_records('wiki_links', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
104 $result = false;
105 }
22e846cd 106
00710f4c
DC
107 # Get existing pages #
108 if ($pages = $DB->get_records('wiki_pages', array('subwikiid' => $subwiki->id))) {
109 foreach ($pages as $page) {
110 # Get locks, and delete them #
111 if (!$DB->delete_records('wiki_locks', array('pageid' => $page->id), IGNORE_MISSING)) {
112 $result = false;
113 }
39fcb981 114
00710f4c
DC
115 # Get versions, and delete them #
116 if (!$DB->delete_records('wiki_versions', array('pageid' => $page->id), IGNORE_MISSING)) {
117 $result = false;
118 }
39fcb981 119 }
00710f4c
DC
120
121 # Delete pages #
122 if (!$DB->delete_records('wiki_pages', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
39fcb981 123 $result = false;
124 }
125 }
00710f4c
DC
126
127 # Get existing synonyms, and delete them #
128 if (!$DB->delete_records('wiki_synonyms', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
129 $result = false;
130 }
131
132 # Delete any subwikis #
133 if (!$DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING)) {
134 $result = false;
135 }
136 }
137
138 # Delete any dependent records here #
139 if (!$DB->delete_records('wiki', array('id' => $wiki->id))) {
140 $result = false;
39fcb981 141 }
8f0cd6ef 142
39fcb981 143 return $result;
144}
145
b0b2c93d
MG
146/**
147 * Implements callback to reset course
148 *
149 * @param stdClass $data
150 * @return boolean|array
151 */
0cdbbc17
AB
152function wiki_reset_userdata($data) {
153 global $CFG,$DB;
154 require_once($CFG->dirroot . '/mod/wiki/pagelib.php');
30836b17 155 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
0cdbbc17
AB
156
157 $componentstr = get_string('modulenameplural', 'wiki');
158 $status = array();
159
160 //get the wiki(s) in this course.
161 if (!$wikis = $DB->get_records('wiki', array('course' => $data->courseid))) {
162 return false;
163 }
b0b2c93d
MG
164 if (empty($data->reset_wiki_comments) && empty($data->reset_wiki_tags) && empty($data->reset_wiki_pages)) {
165 return $status;
166 }
7c61b94c 167
b0b2c93d
MG
168 foreach ($wikis as $wiki) {
169 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $data->courseid)) {
30836b17 170 continue;
7c61b94c 171 }
30836b17
AG
172 $context = context_module::instance($cm->id);
173
174 // Remove tags or all pages.
175 if (!empty($data->reset_wiki_pages) || !empty($data->reset_wiki_tags)) {
7c61b94c 176
30836b17
AG
177 // Get subwiki information.
178 $subwikis = wiki_get_subwikis($wiki->id);
7c61b94c
DC
179
180 foreach ($subwikis as $subwiki) {
30836b17
AG
181 // Get existing pages.
182 if ($pages = wiki_get_page_list($subwiki->id)) {
33a2512a
AG
183 // If the wiki page isn't selected then we are only removing tags.
184 if (empty($data->reset_wiki_pages)) {
30836b17
AG
185 // Go through each page and delete the tags.
186 foreach ($pages as $page) {
b0b2c93d 187 core_tag_tag::remove_all_item_tags('mod_wiki', 'wiki_pages', $page->id);
7c61b94c 188 }
30836b17 189 } else {
33a2512a 190 // Otherwise we are removing pages and tags.
30836b17 191 wiki_delete_pages($context, $pages, $subwiki->id);
0cdbbc17
AB
192 }
193 }
30836b17
AG
194 if (!empty($data->reset_wiki_pages)) {
195 // Delete any subwikis.
196 $DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING);
197
198 // Delete any attached files.
199 $fs = get_file_storage();
200 $fs->delete_area_files($context->id, 'mod_wiki', 'attachments');
30836b17 201 }
0cdbbc17 202 }
b0b2c93d
MG
203
204 if (!empty($data->reset_wiki_pages)) {
205 $status[] = array('component' => $componentstr, 'item' => get_string('deleteallpages', 'wiki'),
206 'error' => false);
207 }
208 if (!empty($data->reset_wiki_tags)) {
209 $status[] = array('component' => $componentstr, 'item' => get_string('tagsdeleted', 'wiki'), 'error' => false);
210 }
0cdbbc17 211 }
30836b17
AG
212
213 // Remove all comments.
214 if (!empty($data->reset_wiki_comments) || !empty($data->reset_wiki_pages)) {
215 $DB->delete_records_select('comments', "contextid = ? AND commentarea='wiki_page'", array($context->id));
b0b2c93d
MG
216 if (!empty($data->reset_wiki_comments)) {
217 $status[] = array('component' => $componentstr, 'item' => get_string('deleteallcomments'), 'error' => false);
218 }
30836b17 219 }
0cdbbc17
AB
220 }
221 return $status;
222}
223
224
225function wiki_reset_course_form_definition(&$mform) {
226 $mform->addElement('header', 'wikiheader', get_string('modulenameplural', 'wiki'));
30836b17 227 $mform->addElement('advcheckbox', 'reset_wiki_pages', get_string('deleteallpages', 'wiki'));
7c61b94c
DC
228 $mform->addElement('advcheckbox', 'reset_wiki_tags', get_string('removeallwikitags', 'wiki'));
229 $mform->addElement('advcheckbox', 'reset_wiki_comments', get_string('deleteallcomments'));
0cdbbc17
AB
230}
231
00710f4c
DC
232/**
233 * Indicates API features that the wiki supports.
234 *
235 * @uses FEATURE_GROUPS
236 * @uses FEATURE_GROUPINGS
00710f4c
DC
237 * @uses FEATURE_MOD_INTRO
238 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
239 * @uses FEATURE_COMPLETION_HAS_RULES
240 * @uses FEATURE_GRADE_HAS_GRADE
241 * @uses FEATURE_GRADE_OUTCOMES
242 * @param string $feature
243 * @return mixed True if yes (some features may use other values)
244 */
245function wiki_supports($feature) {
246 switch ($feature) {
247 case FEATURE_GROUPS:
248 return true;
249 case FEATURE_GROUPINGS:
250 return true;
00710f4c
DC
251 case FEATURE_MOD_INTRO:
252 return true;
253 case FEATURE_COMPLETION_TRACKS_VIEWS:
254 return true;
00710f4c 255 case FEATURE_GRADE_HAS_GRADE:
1c79d7db 256 return false;
00710f4c 257 case FEATURE_GRADE_OUTCOMES:
1c79d7db 258 return false;
00710f4c
DC
259 case FEATURE_RATE:
260 return false;
ebe93f74
JP
261 case FEATURE_BACKUP_MOODLE2:
262 return true;
3e4c2435 263 case FEATURE_SHOW_DESCRIPTION:
264 return true;
6492401d
JL
265 case FEATURE_COMMENT:
266 return true;
00710f4c
DC
267
268 default:
269 return null;
270 }
271}
272
e5cc530b 273/**
274 * Given a course and a time, this module should find recent activity
275 * that has occurred in wiki activities and print it out.
276 * Return true if there was output, or false is there was none.
277 *
00710f4c
DC
278 * @global $CFG
279 * @global $DB
280 * @uses CONTEXT_MODULE
281 * @uses VISIBLEGROUPS
e5cc530b 282 * @param object $course
00710f4c 283 * @param bool $viewfullnames capability
e5cc530b 284 * @param int $timestart
00710f4c
DC
285 * @return boolean
286 **/
287function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
9ab0a4fa 288 global $CFG, $DB, $OUTPUT;
14a960ac 289
c7a8cef6 290 $sql = "SELECT p.id, p.timemodified, p.subwikiid, sw.wikiid, w.wikimode, sw.userid, sw.groupid
621f3ef5
JP
291 FROM {wiki_pages} p
292 JOIN {wiki_subwikis} sw ON sw.id = p.subwikiid
293 JOIN {wiki} w ON w.id = sw.wikiid
294 WHERE p.timemodified > ? AND w.course = ?
295 ORDER BY p.timemodified ASC";
296 if (!$pages = $DB->get_records_sql($sql, array($timestart, $course->id))) {
1fb5d4b0 297 return false;
298 }
3a7b9b76 299 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
1fb5d4b0 300
dd97c328 301 $wikis = array();
302
00710f4c
DC
303 $modinfo = get_fast_modinfo($course);
304
3a7b9b76 305 $subwikivisible = array();
00710f4c 306 foreach ($pages as $page) {
3a7b9b76
MG
307 if (!isset($subwikivisible[$page->subwikiid])) {
308 $subwiki = (object)array('id' => $page->subwikiid, 'wikiid' => $page->wikiid,
309 'groupid' => $page->groupid, 'userid' => $page->userid);
310 $wiki = (object)array('id' => $page->wikiid, 'course' => $course->id, 'wikimode' => $page->wikimode);
311 $subwikivisible[$page->subwikiid] = wiki_user_can_view($subwiki, $wiki);
00710f4c 312 }
3a7b9b76
MG
313 if ($subwikivisible[$page->subwikiid]) {
314 $wikis[] = $page;
00710f4c 315 }
1fb5d4b0 316 }
3a7b9b76 317 unset($subwikivisible);
00710f4c 318 unset($pages);
39fcb981 319
dd97c328 320 if (!$wikis) {
321 return false;
322 }
00710f4c 323 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 3);
dd97c328 324 foreach ($wikis as $wiki) {
00710f4c
DC
325 $cm = $modinfo->instances['wiki'][$wiki->wikiid];
326 $link = $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $wiki->id;
327 print_recent_activity_note($wiki->timemodified, $wiki, $cm->name, $link, false, $viewfullnames);
1fb5d4b0 328 }
dd97c328 329
00710f4c 330 return true; // True if anything was printed, otherwise false
39fcb981 331}
e5cc530b 332/**
333 * Function to be run periodically according to the moodle cron
334 * This function searches for things that need to be done, such
335 * as sending out mail, toggling flags etc ...
336 *
00710f4c
DC
337 * @uses $CFG
338 * @return boolean
339 * @todo Finish documenting this function
340 **/
341function wiki_cron() {
342 global $CFG;
39fcb981 343
00710f4c 344 return true;
39fcb981 345}
346
e5cc530b 347/**
00710f4c
DC
348 * Must return an array of grades for a given instance of this module,
349 * indexed by user. It also returns a maximum allowed grade.
e5cc530b 350 *
00710f4c
DC
351 * Example:
352 * $return->grades = array of grades;
353 * $return->maxgrade = maximum allowed grade;
e5cc530b 354 *
00710f4c 355 * return $return;
e5cc530b 356 *
00710f4c
DC
357 * @param int $wikiid ID of an instance of this module
358 * @return mixed Null or object with an array of grades and with the maximum grade
359 **/
360function wiki_grades($wikiid) {
361 return null;
39fcb981 362}
363
e5cc530b 364/**
00710f4c
DC
365 * This function returns if a scale is being used by one wiki
366 * it it has support for grading and scales. Commented code should be
367 * modified if necessary. See forum, glossary or journal modules
368 * as reference.
369 *
370 * @param int $wikiid ID of an instance of this module
371 * @return mixed
372 * @todo Finish documenting this function
373 **/
374function wiki_scale_used($wikiid, $scaleid) {
375 $return = false;
376
377 //$rec = get_record("wiki","id","$wikiid","scale","-$scaleid");
378 //
379 //if (!empty($rec) && !empty($scaleid)) {
380 // $return = true;
381 //}
39fcb981 382
00710f4c 383 return $return;
39fcb981 384}
385
e5cc530b 386/**
00710f4c
DC
387 * Checks if scale is being used by any instance of wiki.
388 * This function was added in 1.9
e5cc530b 389 *
00710f4c
DC
390 * This is used to find out if scale used anywhere
391 * @param $scaleid int
392 * @return boolean True if the scale is used by any wiki
e5cc530b 393 */
00710f4c 394function wiki_scale_used_anywhere($scaleid) {
655b09ca 395 global $DB;
c513f599 396
75a8ba54
DM
397 //if ($scaleid and $DB->record_exists('wiki', array('grade' => -$scaleid))) {
398 // return true;
399 //} else {
400 // return false;
401 //}
402
403 return false;
39fcb981 404}
405
e5cc530b 406/**
d2b7803e 407 * file serving callback
e5cc530b 408 *
d2b7803e
DC
409 * @copyright Josep Arus
410 * @package mod_wiki
411 * @category files
412 * @param stdClass $course course object
413 * @param stdClass $cm course module object
414 * @param stdClass $context context object
415 * @param string $filearea file area
416 * @param array $args extra arguments
417 * @param bool $forcedownload whether or not force download
261cbbac
DM
418 * @param array $options additional options affecting the file serving
419 * @return bool false if the file was not found, just send the file otherwise and do not return anything
e5cc530b 420 */
261cbbac 421function wiki_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
00710f4c 422 global $CFG;
14a960ac 423
64f93798
PS
424 if ($context->contextlevel != CONTEXT_MODULE) {
425 return false;
426 }
427
12c9bbbd 428 require_login($course, true, $cm);
64f93798 429
00710f4c 430 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
39fcb981 431
64f93798 432 if ($filearea == 'attachments') {
e8cf27e8 433 $swid = (int) array_shift($args);
b82fcee1 434
e8cf27e8 435 if (!$subwiki = wiki_get_subwiki($swid)) {
00710f4c 436 return false;
c513f599 437 }
c513f599 438
00710f4c 439 require_capability('mod/wiki:viewpage', $context);
f84d6a8d 440
64f93798 441 $relativepath = implode('/', $args);
39fcb981 442
64f93798 443 $fullpath = "/$context->id/mod_wiki/attachments/$swid/$relativepath";
87a65403 444
00710f4c
DC
445 $fs = get_file_storage();
446 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
447 return false;
87a65403 448 }
449
0c431257 450 send_stored_file($file, null, 0, $options);
39fcb981 451 }
39fcb981 452}
64f93798 453
d3adc16e 454function wiki_search_form($cm, $search = '', $subwiki = null) {
00710f4c 455 global $CFG, $OUTPUT;
39fcb981 456
621f3ef5
JP
457 $output = '<div class="wikisearch">';
458 $output .= '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/search.php" style="display:inline">';
00710f4c 459 $output .= '<fieldset class="invisiblefieldset">';
beff3a9d
JF
460 $output .= '<legend class="accesshide">'. get_string('searchwikis', 'wiki') .'</legend>';
461 $output .= '<label class="accesshide" for="searchwiki">' . get_string("searchterms", "wiki") . '</label>';
85bc1f48 462 $output .= '<input id="searchwiki" name="searchstring" type="text" size="18" value="' . s($search, true) . '" alt="search" />';
621f3ef5
JP
463 $output .= '<input name="courseid" type="hidden" value="' . $cm->course . '" />';
464 $output .= '<input name="cmid" type="hidden" value="' . $cm->id . '" />';
d3adc16e
MG
465 if (!empty($subwiki->id)) {
466 $output .= '<input name="subwikiid" type="hidden" value="' . $subwiki->id . '" />';
467 }
00710f4c 468 $output .= '<input name="searchwikicontent" type="hidden" value="1" />';
e566bfa1 469 $output .= '<input value="' . get_string('searchwikis', 'wiki') . '" type="submit" />';
00710f4c
DC
470 $output .= '</fieldset>';
471 $output .= '</form>';
472 $output .= '</div>';
39fcb981 473
00710f4c 474 return $output;
39fcb981 475}
00710f4c 476function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm) {
532d319d
JP
477 global $CFG, $PAGE, $USER;
478
621f3ef5 479 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
532d319d 480
1df23626 481 $context = context_module::instance($cm->id);
00710f4c
DC
482 $url = $PAGE->url;
483 $userid = 0;
484 if ($module->wikimode == 'individual') {
39fcb981 485 $userid = $USER->id;
486 }
39fcb981 487
621f3ef5 488 if (!$wiki = wiki_get_wiki($cm->instance)) {
00710f4c 489 return false;
87a65403 490 }
00710f4c 491
621f3ef5 492 if (!$gid = groups_get_activity_group($cm)) {
3ca72eb7
JP
493 $gid = 0;
494 }
621f3ef5 495 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)) {
00710f4c
DC
496 return null;
497 } else {
498 $swid = $subwiki->id;
27978302 499 }
14a960ac 500
00710f4c
DC
501 $pageid = $url->param('pageid');
502 $cmid = $url->param('id');
503 if (empty($pageid) && !empty($cmid)) {
504 // wiki main page
505 $page = wiki_get_page_by_title($swid, $wiki->firstpagetitle);
506 $pageid = $page->id;
507 }
12c9bbbd 508
23cbaefb 509 if (wiki_can_create_pages($context)) {
12c9bbbd
DC
510 $link = new moodle_url('/mod/wiki/create.php', array('action' => 'new', 'swid' => $swid));
511 $node = $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING);
512 }
14a960ac 513
00710f4c 514 if (is_numeric($pageid)) {
39fcb981 515
12c9bbbd
DC
516 if (has_capability('mod/wiki:viewpage', $context)) {
517 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $pageid));
518 $node = $navref->add(get_string('view', 'wiki'), $link, navigation_node::TYPE_SETTING);
519 }
14a960ac 520
033e8461 521 if (wiki_user_can_edit($subwiki)) {
12c9bbbd
DC
522 $link = new moodle_url('/mod/wiki/edit.php', array('pageid' => $pageid));
523 $node = $navref->add(get_string('edit', 'wiki'), $link, navigation_node::TYPE_SETTING);
524 }
14a960ac 525
12c9bbbd
DC
526 if (has_capability('mod/wiki:viewcomment', $context)) {
527 $link = new moodle_url('/mod/wiki/comments.php', array('pageid' => $pageid));
528 $node = $navref->add(get_string('comments', 'wiki'), $link, navigation_node::TYPE_SETTING);
529 }
14a960ac 530
12c9bbbd
DC
531 if (has_capability('mod/wiki:viewpage', $context)) {
532 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
533 $node = $navref->add(get_string('history', 'wiki'), $link, navigation_node::TYPE_SETTING);
534 }
535
536 if (has_capability('mod/wiki:viewpage', $context)) {
537 $link = new moodle_url('/mod/wiki/map.php', array('pageid' => $pageid));
538 $node = $navref->add(get_string('map', 'wiki'), $link, navigation_node::TYPE_SETTING);
539 }
39fcb981 540
12c9bbbd
DC
541 if (has_capability('mod/wiki:viewpage', $context)) {
542 $link = new moodle_url('/mod/wiki/files.php', array('pageid' => $pageid));
543 $node = $navref->add(get_string('files', 'wiki'), $link, navigation_node::TYPE_SETTING);
544 }
ac0a82cf
RT
545
546 if (has_capability('mod/wiki:managewiki', $context)) {
547 $link = new moodle_url('/mod/wiki/admin.php', array('pageid' => $pageid));
548 $node = $navref->add(get_string('admin', 'wiki'), $link, navigation_node::TYPE_SETTING);
549 }
00710f4c 550 }
17da2e6f 551}
16b86ae4
DC
552/**
553 * Returns all other caps used in wiki module
554 *
555 * @return array
556 */
557function wiki_get_extra_capabilities() {
558 return array('moodle/comment:view', 'moodle/comment:post', 'moodle/comment:delete');
559}
c1951ea9
DC
560
561/**
562 * Running addtional permission check on plugin, for example, plugins
563 * may have switch to turn on/off comments option, this callback will
564 * affect UI display, not like pluginname_comment_validate only throw
565 * exceptions.
566 * Capability check has been done in comment->check_permissions(), we
567 * don't need to do it again here.
568 *
35453657
DC
569 * @package mod_wiki
570 * @category comment
571 *
c1951ea9
DC
572 * @param stdClass $comment_param {
573 * context => context the context object
574 * courseid => int course id
575 * cm => stdClass course module object
576 * commentarea => string comment area
577 * itemid => int itemid
578 * }
579 * @return array
580 */
581function wiki_comment_permissions($comment_param) {
582 return array('post'=>true, 'view'=>true);
583}
584
585/**
586 * Validate comment parameter before perform other comments actions
587 *
588 * @param stdClass $comment_param {
589 * context => context the context object
590 * courseid => int course id
591 * cm => stdClass course module object
592 * commentarea => string comment area
593 * itemid => int itemid
594 * }
35453657
DC
595 *
596 * @package mod_wiki
597 * @category comment
598 *
c1951ea9
DC
599 * @return boolean
600 */
601function wiki_comment_validate($comment_param) {
602 global $DB, $CFG;
603 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
604 // validate comment area
605 if ($comment_param->commentarea != 'wiki_page') {
606 throw new comment_exception('invalidcommentarea');
607 }
608 // validate itemid
609 if (!$record = $DB->get_record('wiki_pages', array('id'=>$comment_param->itemid))) {
610 throw new comment_exception('invalidcommentitemid');
611 }
612 if (!$subwiki = wiki_get_subwiki($record->subwikiid)) {
613 throw new comment_exception('invalidsubwikiid');
614 }
615 if (!$wiki = wiki_get_wiki_from_pageid($comment_param->itemid)) {
616 throw new comment_exception('invalidid', 'data');
617 }
618 if (!$course = $DB->get_record('course', array('id'=>$wiki->course))) {
619 throw new comment_exception('coursemisconf');
620 }
621 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $course->id)) {
622 throw new comment_exception('invalidcoursemodule');
623 }
1df23626 624 $context = context_module::instance($cm->id);
c1951ea9
DC
625 // group access
626 if ($subwiki->groupid) {
627 $groupmode = groups_get_activity_groupmode($cm, $course);
628 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
629 if (!groups_is_member($subwiki->groupid)) {
630 throw new comment_exception('notmemberofgroup');
631 }
632 }
633 }
634 // validate context id
635 if ($context->id != $comment_param->context->id) {
636 throw new comment_exception('invalidcontext');
637 }
638 // validation for comment deletion
639 if (!empty($comment_param->commentid)) {
640 if ($comment = $DB->get_record('comments', array('id'=>$comment_param->commentid))) {
641 if ($comment->commentarea != 'wiki_page') {
642 throw new comment_exception('invalidcommentarea');
643 }
644 if ($comment->contextid != $context->id) {
645 throw new comment_exception('invalidcontext');
646 }
647 if ($comment->itemid != $comment_param->itemid) {
648 throw new comment_exception('invalidcommentitemid');
649 }
650 } else {
651 throw new comment_exception('invalidcommentid');
652 }
653 }
654 return true;
655}
b1627a92
DC
656
657/**
658 * Return a list of page types
659 * @param string $pagetype current page type
660 * @param stdClass $parentcontext Block's parent context
661 * @param stdClass $currentcontext Current context of block
662 */
b38e2e28 663function wiki_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92
DC
664 $module_pagetype = array(
665 'mod-wiki-*'=>get_string('page-mod-wiki-x', 'wiki'),
666 'mod-wiki-view'=>get_string('page-mod-wiki-view', 'wiki'),
667 'mod-wiki-comments'=>get_string('page-mod-wiki-comments', 'wiki'),
668 'mod-wiki-history'=>get_string('page-mod-wiki-history', 'wiki'),
669 'mod-wiki-map'=>get_string('page-mod-wiki-map', 'wiki')
670 );
671 return $module_pagetype;
672}
557b58b0
DP
673
674/**
675 * Mark the activity completed (if required) and trigger the course_module_viewed event.
676 *
677 * @param stdClass $wiki Wiki object.
678 * @param stdClass $course Course object.
679 * @param stdClass $cm Course module object.
680 * @param stdClass $context Context object.
681 * @since Moodle 3.1
682 */
683function wiki_view($wiki, $course, $cm, $context) {
684 // Trigger course_module_viewed event.
685 $params = array(
686 'context' => $context,
687 'objectid' => $wiki->id
688 );
689 $event = \mod_wiki\event\course_module_viewed::create($params);
690 $event->add_record_snapshot('course_modules', $cm);
691 $event->add_record_snapshot('course', $course);
692 $event->add_record_snapshot('wiki', $wiki);
693 $event->trigger();
694
695 // Completion.
696 $completion = new completion_info($course);
697 $completion->set_module_viewed($cm);
698}
699
700/**
701 * Mark the activity completed (if required) and trigger the page_viewed event.
702 *
703 * @param stdClass $wiki Wiki object.
704 * @param stdClass $page Page object.
705 * @param stdClass $course Course object.
706 * @param stdClass $cm Course module object.
707 * @param stdClass $context Context object.
708 * @param int $uid Optional User ID.
709 * @param array $other Optional Other params: title, wiki ID, group ID, groupanduser, prettyview.
710 * @param stdClass $subwiki Optional Subwiki.
711 * @since Moodle 3.1
712 */
713function wiki_page_view($wiki, $page, $course, $cm, $context, $uid = null, $other = null, $subwiki = null) {
714
715 // Trigger course_module_viewed event.
716 $params = array(
717 'context' => $context,
718 'objectid' => $page->id
719 );
720 if ($uid != null) {
721 $params['relateduserid'] = $uid;
722 }
723 if ($other != null) {
724 $params['other'] = $other;
725 }
726
727 $event = \mod_wiki\event\page_viewed::create($params);
728
729 $event->add_record_snapshot('wiki_pages', $page);
730 $event->add_record_snapshot('course_modules', $cm);
731 $event->add_record_snapshot('course', $course);
732 $event->add_record_snapshot('wiki', $wiki);
733 if ($subwiki != null) {
734 $event->add_record_snapshot('wiki_subwikis', $subwiki);
735 }
736 $event->trigger();
737
738 // Completion.
739 $completion = new completion_info($course);
740 $completion->set_module_viewed($cm);
741}
02a73d76
JL
742
743/**
744 * Check if the module has any update that affects the current user since a given time.
745 *
746 * @param cm_info $cm course module data
747 * @param int $from the time to check updates from
748 * @param array $filter if we need to check only specific updates
749 * @return stdClass an object with the different type of areas indicating if they were updated or not
750 * @since Moodle 3.2
751 */
752function wiki_check_updates_since(cm_info $cm, $from, $filter = array()) {
753 global $DB, $CFG;
754 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
755
756 $updates = new stdClass();
757 if (!has_capability('mod/wiki:viewpage', $cm->context)) {
758 return $updates;
759 }
760 $updates = course_check_module_updates_since($cm, $from, array('attachments'), $filter);
761
762 // Check only pages updated in subwikis the user can access.
65b2669d 763 $updates->pages = (object) array('updated' => false);
02a73d76
JL
764 $wiki = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST);
765 if ($subwikis = wiki_get_visible_subwikis($wiki, $cm, $cm->context)) {
766 $subwikisids = array();
767 foreach ($subwikis as $subwiki) {
768 $subwikisids[] = $subwiki->id;
769 }
770 list($subwikissql, $params) = $DB->get_in_or_equal($subwikisids, SQL_PARAMS_NAMED);
771 $select = 'subwikiid ' . $subwikissql . ' AND (timemodified > :since1 OR timecreated > :since2)';
772 $params['since1'] = $from;
773 $params['since2'] = $from;
65b2669d
JL
774 $pages = $DB->get_records_select('wiki_pages', $select, $params, '', 'id');
775 if (!empty($pages)) {
776 $updates->pages->updated = true;
777 $updates->pages->itemids = array_keys($pages);
778 }
02a73d76
JL
779 }
780 return $updates;
781}
2b931458
DW
782
783/**
784 * Get icon mapping for font-awesome.
785 */
786function mod_wiki_get_fontawesome_icon_map() {
787 return [
788 'mod_wiki:attachment' => 'fa-paperclip',
789 ];
790}