MDL-62660 tool_dataprivacy: Add scheduled task to expire data requests
[moodle.git] / mod / wiki / lib.php
blob3634cb1be24a28fcae5dbd796250e5ed10a33169
1 <?php
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
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Library of functions and constants for module wiki
21 * It contains the great majority of functions defined by Moodle
22 * that are mandatory to develop a module.
24 * @package mod_wiki
25 * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
26 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
28 * @author Jordi Piguillem
29 * @author Marc Alier
30 * @author David Jimenez
31 * @author Josep Arus
32 * @author Kenneth Riba
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 defined('MOODLE_INTERNAL') || die();
39 /**
40 * Given an object containing all the necessary data,
41 * (defined by the form in mod.html) this function
42 * will create a new instance and return the id number
43 * of the new instance.
45 * @param object $instance An object from the form in mod.html
46 * @return int The id of the newly inserted wiki record
47 **/
48 function wiki_add_instance($wiki) {
49 global $DB;
51 $wiki->timemodified = time();
52 # May have to add extra stuff in here #
53 if (empty($wiki->forceformat)) {
54 $wiki->forceformat = 0;
57 $id = $DB->insert_record('wiki', $wiki);
59 $completiontimeexpected = !empty($wiki->completionexpected) ? $wiki->completionexpected : null;
60 \core_completion\api::update_completion_date_event($wiki->coursemodule, 'wiki', $id, $completiontimeexpected);
62 return $id;
65 /**
66 * Given an object containing all the necessary data,
67 * (defined by the form in mod.html) this function
68 * will update an existing instance with new data.
70 * @param object $instance An object from the form in mod.html
71 * @return boolean Success/Fail
72 **/
73 function wiki_update_instance($wiki) {
74 global $DB;
76 $wiki->timemodified = time();
77 $wiki->id = $wiki->instance;
78 if (empty($wiki->forceformat)) {
79 $wiki->forceformat = 0;
82 $completiontimeexpected = !empty($wiki->completionexpected) ? $wiki->completionexpected : null;
83 \core_completion\api::update_completion_date_event($wiki->coursemodule, 'wiki', $wiki->id, $completiontimeexpected);
85 # May have to add extra stuff in here #
87 return $DB->update_record('wiki', $wiki);
90 /**
91 * Given an ID of an instance of this module,
92 * this function will permanently delete the instance
93 * and any data that depends on it.
95 * @param int $id Id of the module instance
96 * @return boolean Success/Failure
97 **/
98 function wiki_delete_instance($id) {
99 global $DB;
101 if (!$wiki = $DB->get_record('wiki', array('id' => $id))) {
102 return false;
105 $result = true;
107 # Get subwiki information #
108 $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id));
110 foreach ($subwikis as $subwiki) {
111 # Get existing links, and delete them #
112 if (!$DB->delete_records('wiki_links', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
113 $result = false;
116 # Get existing pages #
117 if ($pages = $DB->get_records('wiki_pages', array('subwikiid' => $subwiki->id))) {
118 foreach ($pages as $page) {
119 # Get locks, and delete them #
120 if (!$DB->delete_records('wiki_locks', array('pageid' => $page->id), IGNORE_MISSING)) {
121 $result = false;
124 # Get versions, and delete them #
125 if (!$DB->delete_records('wiki_versions', array('pageid' => $page->id), IGNORE_MISSING)) {
126 $result = false;
130 # Delete pages #
131 if (!$DB->delete_records('wiki_pages', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
132 $result = false;
136 # Get existing synonyms, and delete them #
137 if (!$DB->delete_records('wiki_synonyms', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
138 $result = false;
141 # Delete any subwikis #
142 if (!$DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING)) {
143 $result = false;
147 $cm = get_coursemodule_from_instance('wiki', $id);
148 \core_completion\api::update_completion_date_event($cm->id, 'wiki', $wiki->id, null);
150 # Delete any dependent records here #
151 if (!$DB->delete_records('wiki', array('id' => $wiki->id))) {
152 $result = false;
155 return $result;
159 * Implements callback to reset course
161 * @param stdClass $data
162 * @return boolean|array
164 function wiki_reset_userdata($data) {
165 global $CFG,$DB;
166 require_once($CFG->dirroot . '/mod/wiki/pagelib.php');
167 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
169 $componentstr = get_string('modulenameplural', 'wiki');
170 $status = array();
172 //get the wiki(s) in this course.
173 if (!$wikis = $DB->get_records('wiki', array('course' => $data->courseid))) {
174 return false;
176 if (empty($data->reset_wiki_comments) && empty($data->reset_wiki_tags) && empty($data->reset_wiki_pages)) {
177 return $status;
180 foreach ($wikis as $wiki) {
181 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $data->courseid)) {
182 continue;
184 $context = context_module::instance($cm->id);
186 // Remove tags or all pages.
187 if (!empty($data->reset_wiki_pages) || !empty($data->reset_wiki_tags)) {
189 // Get subwiki information.
190 $subwikis = wiki_get_subwikis($wiki->id);
192 foreach ($subwikis as $subwiki) {
193 // Get existing pages.
194 if ($pages = wiki_get_page_list($subwiki->id)) {
195 // If the wiki page isn't selected then we are only removing tags.
196 if (empty($data->reset_wiki_pages)) {
197 // Go through each page and delete the tags.
198 foreach ($pages as $page) {
199 core_tag_tag::remove_all_item_tags('mod_wiki', 'wiki_pages', $page->id);
201 } else {
202 // Otherwise we are removing pages and tags.
203 wiki_delete_pages($context, $pages, $subwiki->id);
206 if (!empty($data->reset_wiki_pages)) {
207 // Delete any subwikis.
208 $DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING);
210 // Delete any attached files.
211 $fs = get_file_storage();
212 $fs->delete_area_files($context->id, 'mod_wiki', 'attachments');
216 if (!empty($data->reset_wiki_pages)) {
217 $status[] = array('component' => $componentstr, 'item' => get_string('deleteallpages', 'wiki'),
218 'error' => false);
220 if (!empty($data->reset_wiki_tags)) {
221 $status[] = array('component' => $componentstr, 'item' => get_string('tagsdeleted', 'wiki'), 'error' => false);
225 // Remove all comments.
226 if (!empty($data->reset_wiki_comments) || !empty($data->reset_wiki_pages)) {
227 $DB->delete_records_select('comments', "contextid = ? AND commentarea='wiki_page'", array($context->id));
228 if (!empty($data->reset_wiki_comments)) {
229 $status[] = array('component' => $componentstr, 'item' => get_string('deleteallcomments'), 'error' => false);
234 // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
235 // See MDL-9367.
236 shift_course_mod_dates('wiki', array('editbegin', 'editend'), $data->timeshift, $data->courseid);
237 $status[] = array('component' => $componentstr, 'item' => get_string('datechanged'), 'error' => false);
239 return $status;
243 function wiki_reset_course_form_definition(&$mform) {
244 $mform->addElement('header', 'wikiheader', get_string('modulenameplural', 'wiki'));
245 $mform->addElement('advcheckbox', 'reset_wiki_pages', get_string('deleteallpages', 'wiki'));
246 $mform->addElement('advcheckbox', 'reset_wiki_tags', get_string('removeallwikitags', 'wiki'));
247 $mform->addElement('advcheckbox', 'reset_wiki_comments', get_string('deleteallcomments'));
251 * Indicates API features that the wiki supports.
253 * @uses FEATURE_GROUPS
254 * @uses FEATURE_GROUPINGS
255 * @uses FEATURE_MOD_INTRO
256 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
257 * @uses FEATURE_COMPLETION_HAS_RULES
258 * @uses FEATURE_GRADE_HAS_GRADE
259 * @uses FEATURE_GRADE_OUTCOMES
260 * @param string $feature
261 * @return mixed True if yes (some features may use other values)
263 function wiki_supports($feature) {
264 switch ($feature) {
265 case FEATURE_GROUPS:
266 return true;
267 case FEATURE_GROUPINGS:
268 return true;
269 case FEATURE_MOD_INTRO:
270 return true;
271 case FEATURE_COMPLETION_TRACKS_VIEWS:
272 return true;
273 case FEATURE_GRADE_HAS_GRADE:
274 return false;
275 case FEATURE_GRADE_OUTCOMES:
276 return false;
277 case FEATURE_RATE:
278 return false;
279 case FEATURE_BACKUP_MOODLE2:
280 return true;
281 case FEATURE_SHOW_DESCRIPTION:
282 return true;
283 case FEATURE_COMMENT:
284 return true;
286 default:
287 return null;
292 * Given a course and a time, this module should find recent activity
293 * that has occurred in wiki activities and print it out.
294 * Return true if there was output, or false is there was none.
296 * @global $CFG
297 * @global $DB
298 * @uses CONTEXT_MODULE
299 * @uses VISIBLEGROUPS
300 * @param object $course
301 * @param bool $viewfullnames capability
302 * @param int $timestart
303 * @return boolean
305 function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
306 global $CFG, $DB, $OUTPUT;
308 $sql = "SELECT p.id, p.timemodified, p.subwikiid, sw.wikiid, w.wikimode, sw.userid, sw.groupid
309 FROM {wiki_pages} p
310 JOIN {wiki_subwikis} sw ON sw.id = p.subwikiid
311 JOIN {wiki} w ON w.id = sw.wikiid
312 WHERE p.timemodified > ? AND w.course = ?
313 ORDER BY p.timemodified ASC";
314 if (!$pages = $DB->get_records_sql($sql, array($timestart, $course->id))) {
315 return false;
317 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
319 $wikis = array();
321 $modinfo = get_fast_modinfo($course);
323 $subwikivisible = array();
324 foreach ($pages as $page) {
325 if (!isset($subwikivisible[$page->subwikiid])) {
326 $subwiki = (object)array('id' => $page->subwikiid, 'wikiid' => $page->wikiid,
327 'groupid' => $page->groupid, 'userid' => $page->userid);
328 $wiki = (object)array('id' => $page->wikiid, 'course' => $course->id, 'wikimode' => $page->wikimode);
329 $subwikivisible[$page->subwikiid] = wiki_user_can_view($subwiki, $wiki);
331 if ($subwikivisible[$page->subwikiid]) {
332 $wikis[] = $page;
335 unset($subwikivisible);
336 unset($pages);
338 if (!$wikis) {
339 return false;
341 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 3);
342 foreach ($wikis as $wiki) {
343 $cm = $modinfo->instances['wiki'][$wiki->wikiid];
344 $link = $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $wiki->id;
345 print_recent_activity_note($wiki->timemodified, $wiki, $cm->name, $link, false, $viewfullnames);
348 return true; // True if anything was printed, otherwise false
351 * Function to be run periodically according to the moodle cron
352 * This function searches for things that need to be done, such
353 * as sending out mail, toggling flags etc ...
355 * @uses $CFG
356 * @return boolean
357 * @todo Finish documenting this function
359 function wiki_cron() {
360 global $CFG;
362 return true;
366 * Must return an array of grades for a given instance of this module,
367 * indexed by user. It also returns a maximum allowed grade.
369 * Example:
370 * $return->grades = array of grades;
371 * $return->maxgrade = maximum allowed grade;
373 * return $return;
375 * @param int $wikiid ID of an instance of this module
376 * @return mixed Null or object with an array of grades and with the maximum grade
378 function wiki_grades($wikiid) {
379 return null;
383 * This function returns if a scale is being used by one wiki
384 * it it has support for grading and scales. Commented code should be
385 * modified if necessary. See forum, glossary or journal modules
386 * as reference.
388 * @param int $wikiid ID of an instance of this module
389 * @return mixed
390 * @todo Finish documenting this function
392 function wiki_scale_used($wikiid, $scaleid) {
393 $return = false;
395 //$rec = get_record("wiki","id","$wikiid","scale","-$scaleid");
397 //if (!empty($rec) && !empty($scaleid)) {
398 // $return = true;
401 return $return;
405 * Checks if scale is being used by any instance of wiki.
406 * This function was added in 1.9
408 * This is used to find out if scale used anywhere
409 * @param $scaleid int
410 * @return boolean True if the scale is used by any wiki
412 function wiki_scale_used_anywhere($scaleid) {
413 global $DB;
415 //if ($scaleid and $DB->record_exists('wiki', array('grade' => -$scaleid))) {
416 // return true;
417 //} else {
418 // return false;
421 return false;
425 * file serving callback
427 * @copyright Josep Arus
428 * @package mod_wiki
429 * @category files
430 * @param stdClass $course course object
431 * @param stdClass $cm course module object
432 * @param stdClass $context context object
433 * @param string $filearea file area
434 * @param array $args extra arguments
435 * @param bool $forcedownload whether or not force download
436 * @param array $options additional options affecting the file serving
437 * @return bool false if the file was not found, just send the file otherwise and do not return anything
439 function wiki_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
440 global $CFG;
442 if ($context->contextlevel != CONTEXT_MODULE) {
443 return false;
446 require_login($course, true, $cm);
448 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
450 if ($filearea == 'attachments') {
451 $swid = (int) array_shift($args);
453 if (!$subwiki = wiki_get_subwiki($swid)) {
454 return false;
457 require_capability('mod/wiki:viewpage', $context);
459 $relativepath = implode('/', $args);
461 $fullpath = "/$context->id/mod_wiki/attachments/$swid/$relativepath";
463 $fs = get_file_storage();
464 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
465 return false;
468 send_stored_file($file, null, 0, $options);
472 function wiki_search_form($cm, $search = '', $subwiki = null) {
473 global $CFG, $OUTPUT;
475 $output = '<div class="wikisearch">';
476 $output .= '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/search.php" style="display:inline">';
477 $output .= '<fieldset class="invisiblefieldset">';
478 $output .= '<legend class="accesshide">'. get_string('searchwikis', 'wiki') .'</legend>';
479 $output .= '<label class="accesshide" for="searchwiki">' . get_string("searchterms", "wiki") . '</label>';
480 $output .= '<input id="searchwiki" name="searchstring" type="text" size="18" value="' . s($search, true) . '" alt="search" />';
481 $output .= '<input name="courseid" type="hidden" value="' . $cm->course . '" />';
482 $output .= '<input name="cmid" type="hidden" value="' . $cm->id . '" />';
483 if (!empty($subwiki->id)) {
484 $output .= '<input name="subwikiid" type="hidden" value="' . $subwiki->id . '" />';
486 $output .= '<input name="searchwikicontent" type="hidden" value="1" />';
487 $output .= '<input value="' . get_string('searchwikis', 'wiki') . '" class="btn btn-secondary" type="submit" />';
488 $output .= '</fieldset>';
489 $output .= '</form>';
490 $output .= '</div>';
492 return $output;
494 function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm) {
495 global $CFG, $PAGE, $USER;
497 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
499 $context = context_module::instance($cm->id);
500 $url = $PAGE->url;
501 $userid = 0;
502 if ($module->wikimode == 'individual') {
503 $userid = $USER->id;
506 if (!$wiki = wiki_get_wiki($cm->instance)) {
507 return false;
510 if (!$gid = groups_get_activity_group($cm)) {
511 $gid = 0;
513 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)) {
514 return null;
515 } else {
516 $swid = $subwiki->id;
519 $pageid = $url->param('pageid');
520 $cmid = $url->param('id');
521 if (empty($pageid) && !empty($cmid)) {
522 // wiki main page
523 $page = wiki_get_page_by_title($swid, $wiki->firstpagetitle);
524 $pageid = $page->id;
527 if (wiki_can_create_pages($context)) {
528 $link = new moodle_url('/mod/wiki/create.php', array('action' => 'new', 'swid' => $swid));
529 $node = $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING);
532 if (is_numeric($pageid)) {
534 if (has_capability('mod/wiki:viewpage', $context)) {
535 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $pageid));
536 $node = $navref->add(get_string('view', 'wiki'), $link, navigation_node::TYPE_SETTING);
539 if (wiki_user_can_edit($subwiki)) {
540 $link = new moodle_url('/mod/wiki/edit.php', array('pageid' => $pageid));
541 $node = $navref->add(get_string('edit', 'wiki'), $link, navigation_node::TYPE_SETTING);
544 if (has_capability('mod/wiki:viewcomment', $context)) {
545 $link = new moodle_url('/mod/wiki/comments.php', array('pageid' => $pageid));
546 $node = $navref->add(get_string('comments', 'wiki'), $link, navigation_node::TYPE_SETTING);
549 if (has_capability('mod/wiki:viewpage', $context)) {
550 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
551 $node = $navref->add(get_string('history', 'wiki'), $link, navigation_node::TYPE_SETTING);
554 if (has_capability('mod/wiki:viewpage', $context)) {
555 $link = new moodle_url('/mod/wiki/map.php', array('pageid' => $pageid));
556 $node = $navref->add(get_string('map', 'wiki'), $link, navigation_node::TYPE_SETTING);
559 if (has_capability('mod/wiki:viewpage', $context)) {
560 $link = new moodle_url('/mod/wiki/files.php', array('pageid' => $pageid));
561 $node = $navref->add(get_string('files', 'wiki'), $link, navigation_node::TYPE_SETTING);
564 if (has_capability('mod/wiki:managewiki', $context)) {
565 $link = new moodle_url('/mod/wiki/admin.php', array('pageid' => $pageid));
566 $node = $navref->add(get_string('admin', 'wiki'), $link, navigation_node::TYPE_SETTING);
571 * Returns all other caps used in wiki module
573 * @return array
575 function wiki_get_extra_capabilities() {
576 return array('moodle/comment:view', 'moodle/comment:post', 'moodle/comment:delete');
580 * Running addtional permission check on plugin, for example, plugins
581 * may have switch to turn on/off comments option, this callback will
582 * affect UI display, not like pluginname_comment_validate only throw
583 * exceptions.
584 * Capability check has been done in comment->check_permissions(), we
585 * don't need to do it again here.
587 * @package mod_wiki
588 * @category comment
590 * @param stdClass $comment_param {
591 * context => context the context object
592 * courseid => int course id
593 * cm => stdClass course module object
594 * commentarea => string comment area
595 * itemid => int itemid
597 * @return array
599 function wiki_comment_permissions($comment_param) {
600 return array('post'=>true, 'view'=>true);
604 * Validate comment parameter before perform other comments actions
606 * @param stdClass $comment_param {
607 * context => context the context object
608 * courseid => int course id
609 * cm => stdClass course module object
610 * commentarea => string comment area
611 * itemid => int itemid
614 * @package mod_wiki
615 * @category comment
617 * @return boolean
619 function wiki_comment_validate($comment_param) {
620 global $DB, $CFG;
621 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
622 // validate comment area
623 if ($comment_param->commentarea != 'wiki_page') {
624 throw new comment_exception('invalidcommentarea');
626 // validate itemid
627 if (!$record = $DB->get_record('wiki_pages', array('id'=>$comment_param->itemid))) {
628 throw new comment_exception('invalidcommentitemid');
630 if (!$subwiki = wiki_get_subwiki($record->subwikiid)) {
631 throw new comment_exception('invalidsubwikiid');
633 if (!$wiki = wiki_get_wiki_from_pageid($comment_param->itemid)) {
634 throw new comment_exception('invalidid', 'data');
636 if (!$course = $DB->get_record('course', array('id'=>$wiki->course))) {
637 throw new comment_exception('coursemisconf');
639 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $course->id)) {
640 throw new comment_exception('invalidcoursemodule');
642 $context = context_module::instance($cm->id);
643 // group access
644 if ($subwiki->groupid) {
645 $groupmode = groups_get_activity_groupmode($cm, $course);
646 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
647 if (!groups_is_member($subwiki->groupid)) {
648 throw new comment_exception('notmemberofgroup');
652 // validate context id
653 if ($context->id != $comment_param->context->id) {
654 throw new comment_exception('invalidcontext');
656 // validation for comment deletion
657 if (!empty($comment_param->commentid)) {
658 if ($comment = $DB->get_record('comments', array('id'=>$comment_param->commentid))) {
659 if ($comment->commentarea != 'wiki_page') {
660 throw new comment_exception('invalidcommentarea');
662 if ($comment->contextid != $context->id) {
663 throw new comment_exception('invalidcontext');
665 if ($comment->itemid != $comment_param->itemid) {
666 throw new comment_exception('invalidcommentitemid');
668 } else {
669 throw new comment_exception('invalidcommentid');
672 return true;
676 * Return a list of page types
677 * @param string $pagetype current page type
678 * @param stdClass $parentcontext Block's parent context
679 * @param stdClass $currentcontext Current context of block
681 function wiki_page_type_list($pagetype, $parentcontext, $currentcontext) {
682 $module_pagetype = array(
683 'mod-wiki-*'=>get_string('page-mod-wiki-x', 'wiki'),
684 'mod-wiki-view'=>get_string('page-mod-wiki-view', 'wiki'),
685 'mod-wiki-comments'=>get_string('page-mod-wiki-comments', 'wiki'),
686 'mod-wiki-history'=>get_string('page-mod-wiki-history', 'wiki'),
687 'mod-wiki-map'=>get_string('page-mod-wiki-map', 'wiki')
689 return $module_pagetype;
693 * Mark the activity completed (if required) and trigger the course_module_viewed event.
695 * @param stdClass $wiki Wiki object.
696 * @param stdClass $course Course object.
697 * @param stdClass $cm Course module object.
698 * @param stdClass $context Context object.
699 * @since Moodle 3.1
701 function wiki_view($wiki, $course, $cm, $context) {
702 // Trigger course_module_viewed event.
703 $params = array(
704 'context' => $context,
705 'objectid' => $wiki->id
707 $event = \mod_wiki\event\course_module_viewed::create($params);
708 $event->add_record_snapshot('course_modules', $cm);
709 $event->add_record_snapshot('course', $course);
710 $event->add_record_snapshot('wiki', $wiki);
711 $event->trigger();
713 // Completion.
714 $completion = new completion_info($course);
715 $completion->set_module_viewed($cm);
719 * Mark the activity completed (if required) and trigger the page_viewed event.
721 * @param stdClass $wiki Wiki object.
722 * @param stdClass $page Page object.
723 * @param stdClass $course Course object.
724 * @param stdClass $cm Course module object.
725 * @param stdClass $context Context object.
726 * @param int $uid Optional User ID.
727 * @param array $other Optional Other params: title, wiki ID, group ID, groupanduser, prettyview.
728 * @param stdClass $subwiki Optional Subwiki.
729 * @since Moodle 3.1
731 function wiki_page_view($wiki, $page, $course, $cm, $context, $uid = null, $other = null, $subwiki = null) {
733 // Trigger course_module_viewed event.
734 $params = array(
735 'context' => $context,
736 'objectid' => $page->id
738 if ($uid != null) {
739 $params['relateduserid'] = $uid;
741 if ($other != null) {
742 $params['other'] = $other;
745 $event = \mod_wiki\event\page_viewed::create($params);
747 $event->add_record_snapshot('wiki_pages', $page);
748 $event->add_record_snapshot('course_modules', $cm);
749 $event->add_record_snapshot('course', $course);
750 $event->add_record_snapshot('wiki', $wiki);
751 if ($subwiki != null) {
752 $event->add_record_snapshot('wiki_subwikis', $subwiki);
754 $event->trigger();
756 // Completion.
757 $completion = new completion_info($course);
758 $completion->set_module_viewed($cm);
762 * Check if the module has any update that affects the current user since a given time.
764 * @param cm_info $cm course module data
765 * @param int $from the time to check updates from
766 * @param array $filter if we need to check only specific updates
767 * @return stdClass an object with the different type of areas indicating if they were updated or not
768 * @since Moodle 3.2
770 function wiki_check_updates_since(cm_info $cm, $from, $filter = array()) {
771 global $DB, $CFG;
772 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
774 $updates = new stdClass();
775 if (!has_capability('mod/wiki:viewpage', $cm->context)) {
776 return $updates;
778 $updates = course_check_module_updates_since($cm, $from, array('attachments'), $filter);
780 // Check only pages updated in subwikis the user can access.
781 $updates->pages = (object) array('updated' => false);
782 $wiki = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST);
783 if ($subwikis = wiki_get_visible_subwikis($wiki, $cm, $cm->context)) {
784 $subwikisids = array();
785 foreach ($subwikis as $subwiki) {
786 $subwikisids[] = $subwiki->id;
788 list($subwikissql, $params) = $DB->get_in_or_equal($subwikisids, SQL_PARAMS_NAMED);
789 $select = 'subwikiid ' . $subwikissql . ' AND (timemodified > :since1 OR timecreated > :since2)';
790 $params['since1'] = $from;
791 $params['since2'] = $from;
792 $pages = $DB->get_records_select('wiki_pages', $select, $params, '', 'id');
793 if (!empty($pages)) {
794 $updates->pages->updated = true;
795 $updates->pages->itemids = array_keys($pages);
798 return $updates;
802 * Get icon mapping for font-awesome.
804 function mod_wiki_get_fontawesome_icon_map() {
805 return [
806 'mod_wiki:attachment' => 'fa-paperclip',
811 * This function receives a calendar event and returns the action associated with it, or null if there is none.
813 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
814 * is not displayed on the block.
816 * @param calendar_event $event
817 * @param \core_calendar\action_factory $factory
818 * @return \core_calendar\local\event\entities\action_interface|null
820 function mod_wiki_core_calendar_provide_event_action(calendar_event $event,
821 \core_calendar\action_factory $factory) {
822 $cm = get_fast_modinfo($event->courseid)->instances['wiki'][$event->instance];
824 $completion = new \completion_info($cm->get_course());
826 $completiondata = $completion->get_data($cm, false);
828 if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
829 return null;
832 return $factory->create_instance(
833 get_string('view'),
834 new \moodle_url('/mod/wiki/view.php', ['id' => $cm->id]),
836 true
841 * Sets dynamic information about a course module
843 * This callback is called from cm_info when checking module availability (incl. $cm->uservisible)
845 * Main viewing capability in mod_wiki is 'mod/wiki:viewpage' instead of the expected standardised 'mod/wiki:view'.
846 * The method cm_info::is_user_access_restricted_by_capability() does not work for wiki, we need to implement
847 * this callback.
849 * @param cm_info $cm
851 function wiki_cm_info_dynamic(cm_info $cm) {
852 if (!has_capability('mod/wiki:viewpage', $cm->context, $cm->get_modinfo()->get_user_id())) {
853 $cm->set_available(false);