MDL-69454 core_search: consistent mod searches
[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 }
b3bd7a66
MN
56
57 $id = $DB->insert_record('wiki', $wiki);
58
59 $completiontimeexpected = !empty($wiki->completionexpected) ? $wiki->completionexpected : null;
60 \core_completion\api::update_completion_date_event($wiki->coursemodule, 'wiki', $id, $completiontimeexpected);
61
62 return $id;
39fcb981 63}
64
e5cc530b 65/**
66 * Given an object containing all the necessary data,
00710f4c 67 * (defined by the form in mod.html) this function
e5cc530b 68 * will update an existing instance with new data.
69 *
00710f4c
DC
70 * @param object $instance An object from the form in mod.html
71 * @return boolean Success/Fail
72 **/
39fcb981 73function wiki_update_instance($wiki) {
c18269c7 74 global $DB;
39fcb981 75
39fcb981 76 $wiki->timemodified = time();
77 $wiki->id = $wiki->instance;
00710f4c
DC
78 if (empty($wiki->forceformat)) {
79 $wiki->forceformat = 0;
39fcb981 80 }
00710f4c 81
b3bd7a66
MN
82 $completiontimeexpected = !empty($wiki->completionexpected) ? $wiki->completionexpected : null;
83 \core_completion\api::update_completion_date_event($wiki->coursemodule, 'wiki', $wiki->id, $completiontimeexpected);
84
00710f4c
DC
85 # May have to add extra stuff in here #
86
87 return $DB->update_record('wiki', $wiki);
39fcb981 88}
89
e5cc530b 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.
94 *
00710f4c
DC
95 * @param int $id Id of the module instance
96 * @return boolean Success/Failure
97 **/
39fcb981 98function wiki_delete_instance($id) {
00710f4c 99 global $DB;
8f0cd6ef 100
00710f4c 101 if (!$wiki = $DB->get_record('wiki', array('id' => $id))) {
39fcb981 102 return false;
103 }
104
105 $result = true;
106
00710f4c
DC
107 # Get subwiki information #
108 $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id));
8f0cd6ef 109
00710f4c
DC
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;
114 }
22e846cd 115
00710f4c
DC
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;
122 }
39fcb981 123
00710f4c
DC
124 # Get versions, and delete them #
125 if (!$DB->delete_records('wiki_versions', array('pageid' => $page->id), IGNORE_MISSING)) {
126 $result = false;
127 }
39fcb981 128 }
00710f4c
DC
129
130 # Delete pages #
131 if (!$DB->delete_records('wiki_pages', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
39fcb981 132 $result = false;
133 }
134 }
00710f4c
DC
135
136 # Get existing synonyms, and delete them #
137 if (!$DB->delete_records('wiki_synonyms', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
138 $result = false;
139 }
140
141 # Delete any subwikis #
142 if (!$DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING)) {
143 $result = false;
144 }
145 }
146
b3bd7a66
MN
147 $cm = get_coursemodule_from_instance('wiki', $id);
148 \core_completion\api::update_completion_date_event($cm->id, 'wiki', $wiki->id, null);
149
00710f4c
DC
150 # Delete any dependent records here #
151 if (!$DB->delete_records('wiki', array('id' => $wiki->id))) {
152 $result = false;
39fcb981 153 }
8f0cd6ef 154
39fcb981 155 return $result;
156}
157
b0b2c93d
MG
158/**
159 * Implements callback to reset course
160 *
161 * @param stdClass $data
162 * @return boolean|array
163 */
0cdbbc17
AB
164function wiki_reset_userdata($data) {
165 global $CFG,$DB;
166 require_once($CFG->dirroot . '/mod/wiki/pagelib.php');
30836b17 167 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
0cdbbc17
AB
168
169 $componentstr = get_string('modulenameplural', 'wiki');
170 $status = array();
171
172 //get the wiki(s) in this course.
173 if (!$wikis = $DB->get_records('wiki', array('course' => $data->courseid))) {
174 return false;
175 }
b0b2c93d
MG
176 if (empty($data->reset_wiki_comments) && empty($data->reset_wiki_tags) && empty($data->reset_wiki_pages)) {
177 return $status;
178 }
7c61b94c 179
b0b2c93d
MG
180 foreach ($wikis as $wiki) {
181 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $data->courseid)) {
30836b17 182 continue;
7c61b94c 183 }
30836b17
AG
184 $context = context_module::instance($cm->id);
185
186 // Remove tags or all pages.
187 if (!empty($data->reset_wiki_pages) || !empty($data->reset_wiki_tags)) {
7c61b94c 188
30836b17
AG
189 // Get subwiki information.
190 $subwikis = wiki_get_subwikis($wiki->id);
7c61b94c
DC
191
192 foreach ($subwikis as $subwiki) {
30836b17
AG
193 // Get existing pages.
194 if ($pages = wiki_get_page_list($subwiki->id)) {
33a2512a
AG
195 // If the wiki page isn't selected then we are only removing tags.
196 if (empty($data->reset_wiki_pages)) {
30836b17
AG
197 // Go through each page and delete the tags.
198 foreach ($pages as $page) {
b0b2c93d 199 core_tag_tag::remove_all_item_tags('mod_wiki', 'wiki_pages', $page->id);
7c61b94c 200 }
30836b17 201 } else {
33a2512a 202 // Otherwise we are removing pages and tags.
30836b17 203 wiki_delete_pages($context, $pages, $subwiki->id);
0cdbbc17
AB
204 }
205 }
30836b17
AG
206 if (!empty($data->reset_wiki_pages)) {
207 // Delete any subwikis.
208 $DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING);
209
210 // Delete any attached files.
211 $fs = get_file_storage();
212 $fs->delete_area_files($context->id, 'mod_wiki', 'attachments');
30836b17 213 }
0cdbbc17 214 }
b0b2c93d
MG
215
216 if (!empty($data->reset_wiki_pages)) {
217 $status[] = array('component' => $componentstr, 'item' => get_string('deleteallpages', 'wiki'),
218 'error' => false);
219 }
220 if (!empty($data->reset_wiki_tags)) {
221 $status[] = array('component' => $componentstr, 'item' => get_string('tagsdeleted', 'wiki'), 'error' => false);
222 }
0cdbbc17 223 }
30836b17
AG
224
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));
b0b2c93d
MG
228 if (!empty($data->reset_wiki_comments)) {
229 $status[] = array('component' => $componentstr, 'item' => get_string('deleteallcomments'), 'error' => false);
230 }
30836b17 231 }
0cdbbc17 232 }
cb416dbf
AA
233
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);
39b8357c 237 $status[] = array('component' => $componentstr, 'item' => get_string('datechanged'), 'error' => false);
cb416dbf 238
0cdbbc17
AB
239 return $status;
240}
241
242
243function wiki_reset_course_form_definition(&$mform) {
244 $mform->addElement('header', 'wikiheader', get_string('modulenameplural', 'wiki'));
30836b17 245 $mform->addElement('advcheckbox', 'reset_wiki_pages', get_string('deleteallpages', 'wiki'));
7c61b94c
DC
246 $mform->addElement('advcheckbox', 'reset_wiki_tags', get_string('removeallwikitags', 'wiki'));
247 $mform->addElement('advcheckbox', 'reset_wiki_comments', get_string('deleteallcomments'));
0cdbbc17
AB
248}
249
00710f4c
DC
250/**
251 * Indicates API features that the wiki supports.
252 *
253 * @uses FEATURE_GROUPS
254 * @uses FEATURE_GROUPINGS
00710f4c
DC
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)
262 */
263function wiki_supports($feature) {
264 switch ($feature) {
265 case FEATURE_GROUPS:
266 return true;
267 case FEATURE_GROUPINGS:
268 return true;
00710f4c
DC
269 case FEATURE_MOD_INTRO:
270 return true;
271 case FEATURE_COMPLETION_TRACKS_VIEWS:
272 return true;
00710f4c 273 case FEATURE_GRADE_HAS_GRADE:
1c79d7db 274 return false;
00710f4c 275 case FEATURE_GRADE_OUTCOMES:
1c79d7db 276 return false;
00710f4c
DC
277 case FEATURE_RATE:
278 return false;
ebe93f74
JP
279 case FEATURE_BACKUP_MOODLE2:
280 return true;
3e4c2435 281 case FEATURE_SHOW_DESCRIPTION:
282 return true;
6492401d
JL
283 case FEATURE_COMMENT:
284 return true;
00710f4c
DC
285
286 default:
287 return null;
288 }
289}
290
e5cc530b 291/**
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.
295 *
00710f4c
DC
296 * @global $CFG
297 * @global $DB
298 * @uses CONTEXT_MODULE
299 * @uses VISIBLEGROUPS
e5cc530b 300 * @param object $course
00710f4c 301 * @param bool $viewfullnames capability
e5cc530b 302 * @param int $timestart
00710f4c
DC
303 * @return boolean
304 **/
305function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
9ab0a4fa 306 global $CFG, $DB, $OUTPUT;
14a960ac 307
c7a8cef6 308 $sql = "SELECT p.id, p.timemodified, p.subwikiid, sw.wikiid, w.wikimode, sw.userid, sw.groupid
621f3ef5
JP
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))) {
1fb5d4b0 315 return false;
316 }
3a7b9b76 317 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
1fb5d4b0 318
dd97c328 319 $wikis = array();
320
00710f4c
DC
321 $modinfo = get_fast_modinfo($course);
322
3a7b9b76 323 $subwikivisible = array();
00710f4c 324 foreach ($pages as $page) {
3a7b9b76
MG
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);
00710f4c 330 }
3a7b9b76
MG
331 if ($subwikivisible[$page->subwikiid]) {
332 $wikis[] = $page;
00710f4c 333 }
1fb5d4b0 334 }
3a7b9b76 335 unset($subwikivisible);
00710f4c 336 unset($pages);
39fcb981 337
dd97c328 338 if (!$wikis) {
339 return false;
340 }
439c8c59 341 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 6);
dd97c328 342 foreach ($wikis as $wiki) {
00710f4c
DC
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);
1fb5d4b0 346 }
dd97c328 347
00710f4c 348 return true; // True if anything was printed, otherwise false
39fcb981 349}
39fcb981 350
e5cc530b 351/**
00710f4c
DC
352 * Must return an array of grades for a given instance of this module,
353 * indexed by user. It also returns a maximum allowed grade.
e5cc530b 354 *
00710f4c
DC
355 * Example:
356 * $return->grades = array of grades;
357 * $return->maxgrade = maximum allowed grade;
e5cc530b 358 *
00710f4c 359 * return $return;
e5cc530b 360 *
00710f4c
DC
361 * @param int $wikiid ID of an instance of this module
362 * @return mixed Null or object with an array of grades and with the maximum grade
363 **/
364function wiki_grades($wikiid) {
365 return null;
39fcb981 366}
367
e5cc530b 368/**
c0375ba7
AA
369 * @deprecated since Moodle 3.8
370 */
371function wiki_scale_used() {
372 throw new coding_exception('wiki_scale_used() can not be used anymore. Plugins can implement ' .
373 '<modname>_scale_used_anywhere, all implementations of <modname>_scale_used are now ignored');
39fcb981 374}
375
e5cc530b 376/**
00710f4c
DC
377 * Checks if scale is being used by any instance of wiki.
378 * This function was added in 1.9
e5cc530b 379 *
00710f4c
DC
380 * This is used to find out if scale used anywhere
381 * @param $scaleid int
382 * @return boolean True if the scale is used by any wiki
e5cc530b 383 */
00710f4c 384function wiki_scale_used_anywhere($scaleid) {
655b09ca 385 global $DB;
c513f599 386
75a8ba54
DM
387 //if ($scaleid and $DB->record_exists('wiki', array('grade' => -$scaleid))) {
388 // return true;
389 //} else {
390 // return false;
391 //}
392
393 return false;
39fcb981 394}
395
e5cc530b 396/**
d2b7803e 397 * file serving callback
e5cc530b 398 *
d2b7803e
DC
399 * @copyright Josep Arus
400 * @package mod_wiki
401 * @category files
402 * @param stdClass $course course object
403 * @param stdClass $cm course module object
404 * @param stdClass $context context object
405 * @param string $filearea file area
406 * @param array $args extra arguments
407 * @param bool $forcedownload whether or not force download
261cbbac
DM
408 * @param array $options additional options affecting the file serving
409 * @return bool false if the file was not found, just send the file otherwise and do not return anything
e5cc530b 410 */
261cbbac 411function wiki_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
00710f4c 412 global $CFG;
14a960ac 413
64f93798
PS
414 if ($context->contextlevel != CONTEXT_MODULE) {
415 return false;
416 }
417
12c9bbbd 418 require_login($course, true, $cm);
64f93798 419
00710f4c 420 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
39fcb981 421
64f93798 422 if ($filearea == 'attachments') {
e8cf27e8 423 $swid = (int) array_shift($args);
b82fcee1 424
e8cf27e8 425 if (!$subwiki = wiki_get_subwiki($swid)) {
00710f4c 426 return false;
c513f599 427 }
c513f599 428
00710f4c 429 require_capability('mod/wiki:viewpage', $context);
f84d6a8d 430
64f93798 431 $relativepath = implode('/', $args);
39fcb981 432
64f93798 433 $fullpath = "/$context->id/mod_wiki/attachments/$swid/$relativepath";
87a65403 434
00710f4c
DC
435 $fs = get_file_storage();
436 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
437 return false;
87a65403 438 }
439
0c431257 440 send_stored_file($file, null, 0, $options);
39fcb981 441 }
39fcb981 442}
64f93798 443
4c71e1d0
BB
444/**
445 * Search for wiki
446 *
447 * @param stdClass $cm course module object
448 * @param string $search searchword.
449 * @param stdClass $subwiki Optional Subwiki.
450 * @return Search wiki input form
451 */
d3adc16e 452function wiki_search_form($cm, $search = '', $subwiki = null) {
4c71e1d0
BB
453 global $OUTPUT;
454
455 $hiddenfields = [
456 (object) ['type' => 'hidden', 'name' => 'courseid', 'value' => $cm->course],
457 (object) ['type' => 'hidden', 'name' => 'cmid', 'value' => $cm->id],
458 (object) ['type' => 'hidden', 'name' => 'searchwikicontent', 'value' => 1],
459 ];
d3adc16e 460 if (!empty($subwiki->id)) {
4c71e1d0 461 $hiddenfields[] = (object) ['type' => 'hidden', 'name' => 'subwikiid', 'value' => $subwiki->id];
d3adc16e 462 }
4c71e1d0
BB
463 $data = [
464 'action' => new moodle_url('/mod/wiki/search.php'),
465 'hiddenfields' => $hiddenfields,
466 'inputname' => 'searchstring',
467 'query' => s($search, true),
468 'searchstring' => get_string('searchwikis', 'wiki'),
469 'extraclasses' => 'mt-2'
470 ];
471 return $OUTPUT->render_from_template('core/search_input', $data);
39fcb981 472}
4c71e1d0 473
00710f4c 474function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm) {
532d319d
JP
475 global $CFG, $PAGE, $USER;
476
621f3ef5 477 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
532d319d 478
1df23626 479 $context = context_module::instance($cm->id);
00710f4c
DC
480 $url = $PAGE->url;
481 $userid = 0;
482 if ($module->wikimode == 'individual') {
39fcb981 483 $userid = $USER->id;
484 }
39fcb981 485
621f3ef5 486 if (!$wiki = wiki_get_wiki($cm->instance)) {
00710f4c 487 return false;
87a65403 488 }
00710f4c 489
621f3ef5 490 if (!$gid = groups_get_activity_group($cm)) {
3ca72eb7
JP
491 $gid = 0;
492 }
621f3ef5 493 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)) {
00710f4c
DC
494 return null;
495 } else {
496 $swid = $subwiki->id;
27978302 497 }
14a960ac 498
00710f4c
DC
499 $pageid = $url->param('pageid');
500 $cmid = $url->param('id');
501 if (empty($pageid) && !empty($cmid)) {
502 // wiki main page
503 $page = wiki_get_page_by_title($swid, $wiki->firstpagetitle);
504 $pageid = $page->id;
505 }
12c9bbbd 506
23cbaefb 507 if (wiki_can_create_pages($context)) {
12c9bbbd
DC
508 $link = new moodle_url('/mod/wiki/create.php', array('action' => 'new', 'swid' => $swid));
509 $node = $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING);
510 }
14a960ac 511
00710f4c 512 if (is_numeric($pageid)) {
39fcb981 513
12c9bbbd
DC
514 if (has_capability('mod/wiki:viewpage', $context)) {
515 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $pageid));
516 $node = $navref->add(get_string('view', 'wiki'), $link, navigation_node::TYPE_SETTING);
517 }
14a960ac 518
033e8461 519 if (wiki_user_can_edit($subwiki)) {
12c9bbbd
DC
520 $link = new moodle_url('/mod/wiki/edit.php', array('pageid' => $pageid));
521 $node = $navref->add(get_string('edit', 'wiki'), $link, navigation_node::TYPE_SETTING);
522 }
14a960ac 523
12c9bbbd
DC
524 if (has_capability('mod/wiki:viewcomment', $context)) {
525 $link = new moodle_url('/mod/wiki/comments.php', array('pageid' => $pageid));
526 $node = $navref->add(get_string('comments', 'wiki'), $link, navigation_node::TYPE_SETTING);
527 }
14a960ac 528
12c9bbbd
DC
529 if (has_capability('mod/wiki:viewpage', $context)) {
530 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
531 $node = $navref->add(get_string('history', 'wiki'), $link, navigation_node::TYPE_SETTING);
532 }
533
534 if (has_capability('mod/wiki:viewpage', $context)) {
535 $link = new moodle_url('/mod/wiki/map.php', array('pageid' => $pageid));
536 $node = $navref->add(get_string('map', 'wiki'), $link, navigation_node::TYPE_SETTING);
537 }
39fcb981 538
12c9bbbd
DC
539 if (has_capability('mod/wiki:viewpage', $context)) {
540 $link = new moodle_url('/mod/wiki/files.php', array('pageid' => $pageid));
541 $node = $navref->add(get_string('files', 'wiki'), $link, navigation_node::TYPE_SETTING);
542 }
ac0a82cf
RT
543
544 if (has_capability('mod/wiki:managewiki', $context)) {
545 $link = new moodle_url('/mod/wiki/admin.php', array('pageid' => $pageid));
546 $node = $navref->add(get_string('admin', 'wiki'), $link, navigation_node::TYPE_SETTING);
547 }
00710f4c 548 }
17da2e6f 549}
16b86ae4
DC
550/**
551 * Returns all other caps used in wiki module
552 *
553 * @return array
554 */
555function wiki_get_extra_capabilities() {
556 return array('moodle/comment:view', 'moodle/comment:post', 'moodle/comment:delete');
557}
c1951ea9
DC
558
559/**
560 * Running addtional permission check on plugin, for example, plugins
561 * may have switch to turn on/off comments option, this callback will
562 * affect UI display, not like pluginname_comment_validate only throw
563 * exceptions.
564 * Capability check has been done in comment->check_permissions(), we
565 * don't need to do it again here.
566 *
35453657
DC
567 * @package mod_wiki
568 * @category comment
569 *
c1951ea9
DC
570 * @param stdClass $comment_param {
571 * context => context the context object
572 * courseid => int course id
573 * cm => stdClass course module object
574 * commentarea => string comment area
575 * itemid => int itemid
576 * }
577 * @return array
578 */
579function wiki_comment_permissions($comment_param) {
580 return array('post'=>true, 'view'=>true);
581}
582
583/**
584 * Validate comment parameter before perform other comments actions
585 *
586 * @param stdClass $comment_param {
587 * context => context the context object
588 * courseid => int course id
589 * cm => stdClass course module object
590 * commentarea => string comment area
591 * itemid => int itemid
592 * }
35453657
DC
593 *
594 * @package mod_wiki
595 * @category comment
596 *
c1951ea9
DC
597 * @return boolean
598 */
599function wiki_comment_validate($comment_param) {
600 global $DB, $CFG;
601 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
602 // validate comment area
603 if ($comment_param->commentarea != 'wiki_page') {
604 throw new comment_exception('invalidcommentarea');
605 }
606 // validate itemid
607 if (!$record = $DB->get_record('wiki_pages', array('id'=>$comment_param->itemid))) {
608 throw new comment_exception('invalidcommentitemid');
609 }
610 if (!$subwiki = wiki_get_subwiki($record->subwikiid)) {
611 throw new comment_exception('invalidsubwikiid');
612 }
613 if (!$wiki = wiki_get_wiki_from_pageid($comment_param->itemid)) {
614 throw new comment_exception('invalidid', 'data');
615 }
616 if (!$course = $DB->get_record('course', array('id'=>$wiki->course))) {
617 throw new comment_exception('coursemisconf');
618 }
619 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $course->id)) {
620 throw new comment_exception('invalidcoursemodule');
621 }
1df23626 622 $context = context_module::instance($cm->id);
c1951ea9
DC
623 // group access
624 if ($subwiki->groupid) {
625 $groupmode = groups_get_activity_groupmode($cm, $course);
626 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
627 if (!groups_is_member($subwiki->groupid)) {
628 throw new comment_exception('notmemberofgroup');
629 }
630 }
631 }
632 // validate context id
633 if ($context->id != $comment_param->context->id) {
634 throw new comment_exception('invalidcontext');
635 }
636 // validation for comment deletion
637 if (!empty($comment_param->commentid)) {
638 if ($comment = $DB->get_record('comments', array('id'=>$comment_param->commentid))) {
639 if ($comment->commentarea != 'wiki_page') {
640 throw new comment_exception('invalidcommentarea');
641 }
642 if ($comment->contextid != $context->id) {
643 throw new comment_exception('invalidcontext');
644 }
645 if ($comment->itemid != $comment_param->itemid) {
646 throw new comment_exception('invalidcommentitemid');
647 }
648 } else {
649 throw new comment_exception('invalidcommentid');
650 }
651 }
652 return true;
653}
b1627a92
DC
654
655/**
656 * Return a list of page types
657 * @param string $pagetype current page type
658 * @param stdClass $parentcontext Block's parent context
659 * @param stdClass $currentcontext Current context of block
660 */
b38e2e28 661function wiki_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92
DC
662 $module_pagetype = array(
663 'mod-wiki-*'=>get_string('page-mod-wiki-x', 'wiki'),
664 'mod-wiki-view'=>get_string('page-mod-wiki-view', 'wiki'),
665 'mod-wiki-comments'=>get_string('page-mod-wiki-comments', 'wiki'),
666 'mod-wiki-history'=>get_string('page-mod-wiki-history', 'wiki'),
667 'mod-wiki-map'=>get_string('page-mod-wiki-map', 'wiki')
668 );
669 return $module_pagetype;
670}
557b58b0
DP
671
672/**
673 * Mark the activity completed (if required) and trigger the course_module_viewed event.
674 *
675 * @param stdClass $wiki Wiki object.
676 * @param stdClass $course Course object.
677 * @param stdClass $cm Course module object.
678 * @param stdClass $context Context object.
679 * @since Moodle 3.1
680 */
681function wiki_view($wiki, $course, $cm, $context) {
682 // Trigger course_module_viewed event.
683 $params = array(
684 'context' => $context,
685 'objectid' => $wiki->id
686 );
687 $event = \mod_wiki\event\course_module_viewed::create($params);
688 $event->add_record_snapshot('course_modules', $cm);
689 $event->add_record_snapshot('course', $course);
690 $event->add_record_snapshot('wiki', $wiki);
691 $event->trigger();
692
693 // Completion.
694 $completion = new completion_info($course);
695 $completion->set_module_viewed($cm);
696}
697
698/**
699 * Mark the activity completed (if required) and trigger the page_viewed event.
700 *
701 * @param stdClass $wiki Wiki object.
702 * @param stdClass $page Page object.
703 * @param stdClass $course Course object.
704 * @param stdClass $cm Course module object.
705 * @param stdClass $context Context object.
706 * @param int $uid Optional User ID.
707 * @param array $other Optional Other params: title, wiki ID, group ID, groupanduser, prettyview.
708 * @param stdClass $subwiki Optional Subwiki.
709 * @since Moodle 3.1
710 */
711function wiki_page_view($wiki, $page, $course, $cm, $context, $uid = null, $other = null, $subwiki = null) {
712
713 // Trigger course_module_viewed event.
714 $params = array(
715 'context' => $context,
716 'objectid' => $page->id
717 );
718 if ($uid != null) {
719 $params['relateduserid'] = $uid;
720 }
721 if ($other != null) {
722 $params['other'] = $other;
723 }
724
725 $event = \mod_wiki\event\page_viewed::create($params);
726
727 $event->add_record_snapshot('wiki_pages', $page);
728 $event->add_record_snapshot('course_modules', $cm);
729 $event->add_record_snapshot('course', $course);
730 $event->add_record_snapshot('wiki', $wiki);
731 if ($subwiki != null) {
732 $event->add_record_snapshot('wiki_subwikis', $subwiki);
733 }
734 $event->trigger();
735
736 // Completion.
737 $completion = new completion_info($course);
738 $completion->set_module_viewed($cm);
739}
02a73d76
JL
740
741/**
742 * Check if the module has any update that affects the current user since a given time.
743 *
744 * @param cm_info $cm course module data
745 * @param int $from the time to check updates from
746 * @param array $filter if we need to check only specific updates
747 * @return stdClass an object with the different type of areas indicating if they were updated or not
748 * @since Moodle 3.2
749 */
750function wiki_check_updates_since(cm_info $cm, $from, $filter = array()) {
751 global $DB, $CFG;
752 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
753
754 $updates = new stdClass();
755 if (!has_capability('mod/wiki:viewpage', $cm->context)) {
756 return $updates;
757 }
758 $updates = course_check_module_updates_since($cm, $from, array('attachments'), $filter);
759
760 // Check only pages updated in subwikis the user can access.
65b2669d 761 $updates->pages = (object) array('updated' => false);
02a73d76
JL
762 $wiki = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST);
763 if ($subwikis = wiki_get_visible_subwikis($wiki, $cm, $cm->context)) {
764 $subwikisids = array();
765 foreach ($subwikis as $subwiki) {
766 $subwikisids[] = $subwiki->id;
767 }
768 list($subwikissql, $params) = $DB->get_in_or_equal($subwikisids, SQL_PARAMS_NAMED);
769 $select = 'subwikiid ' . $subwikissql . ' AND (timemodified > :since1 OR timecreated > :since2)';
770 $params['since1'] = $from;
771 $params['since2'] = $from;
65b2669d
JL
772 $pages = $DB->get_records_select('wiki_pages', $select, $params, '', 'id');
773 if (!empty($pages)) {
774 $updates->pages->updated = true;
775 $updates->pages->itemids = array_keys($pages);
776 }
02a73d76
JL
777 }
778 return $updates;
779}
2b931458
DW
780
781/**
782 * Get icon mapping for font-awesome.
783 */
784function mod_wiki_get_fontawesome_icon_map() {
785 return [
786 'mod_wiki:attachment' => 'fa-paperclip',
787 ];
788}
b3bd7a66
MN
789
790/**
59391e80
MN
791 * This function receives a calendar event and returns the action associated with it, or null if there is none.
792 *
793 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
794 * is not displayed on the block.
b3bd7a66 795 *
e1cd93ce 796 * @param calendar_event $event
b3bd7a66 797 * @param \core_calendar\action_factory $factory
1d146d7a 798 * @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
01f96180 799 * @return \core_calendar\local\event\entities\action_interface|null
b3bd7a66 800 */
e1cd93ce 801function mod_wiki_core_calendar_provide_event_action(calendar_event $event,
1d146d7a
SR
802 \core_calendar\action_factory $factory,
803 int $userid = 0) {
804 global $USER;
805
806 if (!$userid) {
807 $userid = $USER->id;
808 }
809
810 $cm = get_fast_modinfo($event->courseid, $userid)->instances['wiki'][$event->instance];
b3bd7a66 811
ed1f61f3
SR
812 if (!$cm->uservisible) {
813 // The module is not visible to the user for any reason.
814 return null;
815 }
816
50d845e3 817 $completion = new \completion_info($cm->get_course());
b3bd7a66 818
1d146d7a 819 $completiondata = $completion->get_data($cm, false, $userid);
b3bd7a66
MN
820
821 if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
822 return null;
823 }
824
825 return $factory->create_instance(
826 get_string('view'),
827 new \moodle_url('/mod/wiki/view.php', ['id' => $cm->id]),
828 1,
829 true
830 );
3a93df8b
MG
831}
832
833/**
834 * Sets dynamic information about a course module
835 *
836 * This callback is called from cm_info when checking module availability (incl. $cm->uservisible)
837 *
838 * Main viewing capability in mod_wiki is 'mod/wiki:viewpage' instead of the expected standardised 'mod/wiki:view'.
839 * The method cm_info::is_user_access_restricted_by_capability() does not work for wiki, we need to implement
840 * this callback.
841 *
842 * @param cm_info $cm
843 */
844function wiki_cm_info_dynamic(cm_info $cm) {
845 if (!has_capability('mod/wiki:viewpage', $cm->context, $cm->get_modinfo()->get_user_id())) {
846 $cm->set_available(false);
847 }
848}