MDL-49028 mod_wiki: Fully reset wiki pages.
[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
e5cc530b 37/**
38 * Given an object containing all the necessary data,
00710f4c 39 * (defined by the form in mod.html) this function
e5cc530b 40 * will create a new instance and return the id number
41 * of the new instance.
42 *
00710f4c
DC
43 * @param object $instance An object from the form in mod.html
44 * @return int The id of the newly inserted wiki record
45 **/
39fcb981 46function wiki_add_instance($wiki) {
c18269c7 47 global $DB;
14a960ac 48
39fcb981 49 $wiki->timemodified = time();
39fcb981 50 # May have to add extra stuff in here #
f6e56f28
JP
51 if (empty($wiki->forceformat)) {
52 $wiki->forceformat = 0;
53 }
00710f4c 54 return $DB->insert_record('wiki', $wiki);
39fcb981 55}
56
e5cc530b 57/**
58 * Given an object containing all the necessary data,
00710f4c 59 * (defined by the form in mod.html) this function
e5cc530b 60 * will update an existing instance with new data.
61 *
00710f4c
DC
62 * @param object $instance An object from the form in mod.html
63 * @return boolean Success/Fail
64 **/
39fcb981 65function wiki_update_instance($wiki) {
c18269c7 66 global $DB;
39fcb981 67
39fcb981 68 $wiki->timemodified = time();
69 $wiki->id = $wiki->instance;
00710f4c
DC
70 if (empty($wiki->forceformat)) {
71 $wiki->forceformat = 0;
39fcb981 72 }
00710f4c
DC
73
74 # May have to add extra stuff in here #
75
76 return $DB->update_record('wiki', $wiki);
39fcb981 77}
78
e5cc530b 79/**
80 * Given an ID of an instance of this module,
81 * this function will permanently delete the instance
82 * and any data that depends on it.
83 *
00710f4c
DC
84 * @param int $id Id of the module instance
85 * @return boolean Success/Failure
86 **/
39fcb981 87function wiki_delete_instance($id) {
00710f4c 88 global $DB;
8f0cd6ef 89
00710f4c 90 if (!$wiki = $DB->get_record('wiki', array('id' => $id))) {
39fcb981 91 return false;
92 }
93
94 $result = true;
95
00710f4c
DC
96 # Get subwiki information #
97 $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id));
8f0cd6ef 98
00710f4c
DC
99 foreach ($subwikis as $subwiki) {
100 # Get existing links, and delete them #
101 if (!$DB->delete_records('wiki_links', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
102 $result = false;
103 }
22e846cd 104
00710f4c
DC
105 # Get existing pages #
106 if ($pages = $DB->get_records('wiki_pages', array('subwikiid' => $subwiki->id))) {
107 foreach ($pages as $page) {
108 # Get locks, and delete them #
109 if (!$DB->delete_records('wiki_locks', array('pageid' => $page->id), IGNORE_MISSING)) {
110 $result = false;
111 }
39fcb981 112
00710f4c
DC
113 # Get versions, and delete them #
114 if (!$DB->delete_records('wiki_versions', array('pageid' => $page->id), IGNORE_MISSING)) {
115 $result = false;
116 }
39fcb981 117 }
00710f4c
DC
118
119 # Delete pages #
120 if (!$DB->delete_records('wiki_pages', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
39fcb981 121 $result = false;
122 }
123 }
00710f4c
DC
124
125 # Get existing synonyms, and delete them #
126 if (!$DB->delete_records('wiki_synonyms', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
127 $result = false;
128 }
129
130 # Delete any subwikis #
131 if (!$DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING)) {
132 $result = false;
133 }
134 }
135
136 # Delete any dependent records here #
137 if (!$DB->delete_records('wiki', array('id' => $wiki->id))) {
138 $result = false;
39fcb981 139 }
8f0cd6ef 140
39fcb981 141 return $result;
142}
143
0cdbbc17
AB
144function wiki_reset_userdata($data) {
145 global $CFG,$DB;
146 require_once($CFG->dirroot . '/mod/wiki/pagelib.php');
147 require_once($CFG->dirroot . '/tag/lib.php');
30836b17 148 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
0cdbbc17
AB
149
150 $componentstr = get_string('modulenameplural', 'wiki');
151 $status = array();
152
153 //get the wiki(s) in this course.
154 if (!$wikis = $DB->get_records('wiki', array('course' => $data->courseid))) {
155 return false;
156 }
157 $errors = false;
158 foreach ($wikis as $wiki) {
7c61b94c 159
30836b17
AG
160 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id)) {
161 continue;
7c61b94c 162 }
30836b17
AG
163 $context = context_module::instance($cm->id);
164
165 // Remove tags or all pages.
166 if (!empty($data->reset_wiki_pages) || !empty($data->reset_wiki_tags)) {
7c61b94c 167
30836b17
AG
168 // Get subwiki information.
169 $subwikis = wiki_get_subwikis($wiki->id);
7c61b94c
DC
170
171 foreach ($subwikis as $subwiki) {
30836b17
AG
172 // Get existing pages.
173 if ($pages = wiki_get_page_list($subwiki->id)) {
174 if (!empty($data->reset_wiki_tags)) {
175 // Go through each page and delete the tags.
176 foreach ($pages as $page) {
177
178 $tags = tag_get_tags_array('wiki_pages', $page->id);
179 foreach ($tags as $tagid => $tagname) {
180 // Delete the related tag_instances related to the wiki page.
181 $errors = tag_delete_instance('wiki_pages', $page->id, $tagid);
182 $status[] = array('component' => $componentstr, 'item' => get_string('tagsdeleted', 'wiki'),
183 'error' => $errors);
184 }
7c61b94c 185 }
30836b17
AG
186 } else {
187 // Delete pages.
188 wiki_delete_pages($context, $pages, $subwiki->id);
0cdbbc17
AB
189 }
190 }
30836b17
AG
191 if (!empty($data->reset_wiki_pages)) {
192 // Delete any subwikis.
193 $DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING);
194
195 // Delete any attached files.
196 $fs = get_file_storage();
197 $fs->delete_area_files($context->id, 'mod_wiki', 'attachments');
198
199 $status[] = array('component' => $componentstr, 'item' => get_string('deleteallpages', 'wiki'),
200 'error' => $errors);
201 }
0cdbbc17
AB
202 }
203 }
30836b17
AG
204
205 // Remove all comments.
206 if (!empty($data->reset_wiki_comments) || !empty($data->reset_wiki_pages)) {
207 $DB->delete_records_select('comments', "contextid = ? AND commentarea='wiki_page'", array($context->id));
208 $status[] = array('component' => $componentstr, 'item' => get_string('deleteallcomments'), 'error' => false);
209 }
0cdbbc17
AB
210 }
211 return $status;
212}
213
214
215function wiki_reset_course_form_definition(&$mform) {
216 $mform->addElement('header', 'wikiheader', get_string('modulenameplural', 'wiki'));
30836b17 217 $mform->addElement('advcheckbox', 'reset_wiki_pages', get_string('deleteallpages', 'wiki'));
7c61b94c
DC
218 $mform->addElement('advcheckbox', 'reset_wiki_tags', get_string('removeallwikitags', 'wiki'));
219 $mform->addElement('advcheckbox', 'reset_wiki_comments', get_string('deleteallcomments'));
0cdbbc17
AB
220}
221
00710f4c
DC
222/**
223 * Indicates API features that the wiki supports.
224 *
225 * @uses FEATURE_GROUPS
226 * @uses FEATURE_GROUPINGS
00710f4c
DC
227 * @uses FEATURE_MOD_INTRO
228 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
229 * @uses FEATURE_COMPLETION_HAS_RULES
230 * @uses FEATURE_GRADE_HAS_GRADE
231 * @uses FEATURE_GRADE_OUTCOMES
232 * @param string $feature
233 * @return mixed True if yes (some features may use other values)
234 */
235function wiki_supports($feature) {
236 switch ($feature) {
237 case FEATURE_GROUPS:
238 return true;
239 case FEATURE_GROUPINGS:
240 return true;
00710f4c
DC
241 case FEATURE_MOD_INTRO:
242 return true;
243 case FEATURE_COMPLETION_TRACKS_VIEWS:
244 return true;
00710f4c 245 case FEATURE_GRADE_HAS_GRADE:
1c79d7db 246 return false;
00710f4c 247 case FEATURE_GRADE_OUTCOMES:
1c79d7db 248 return false;
00710f4c
DC
249 case FEATURE_RATE:
250 return false;
ebe93f74
JP
251 case FEATURE_BACKUP_MOODLE2:
252 return true;
3e4c2435 253 case FEATURE_SHOW_DESCRIPTION:
254 return true;
00710f4c
DC
255
256 default:
257 return null;
258 }
259}
260
e5cc530b 261/**
262 * Given a course and a time, this module should find recent activity
263 * that has occurred in wiki activities and print it out.
264 * Return true if there was output, or false is there was none.
265 *
00710f4c
DC
266 * @global $CFG
267 * @global $DB
268 * @uses CONTEXT_MODULE
269 * @uses VISIBLEGROUPS
e5cc530b 270 * @param object $course
00710f4c 271 * @param bool $viewfullnames capability
e5cc530b 272 * @param int $timestart
00710f4c
DC
273 * @return boolean
274 **/
275function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
9ab0a4fa 276 global $CFG, $DB, $OUTPUT;
14a960ac 277
c7a8cef6 278 $sql = "SELECT p.id, p.timemodified, p.subwikiid, sw.wikiid, w.wikimode, sw.userid, sw.groupid
621f3ef5
JP
279 FROM {wiki_pages} p
280 JOIN {wiki_subwikis} sw ON sw.id = p.subwikiid
281 JOIN {wiki} w ON w.id = sw.wikiid
282 WHERE p.timemodified > ? AND w.course = ?
283 ORDER BY p.timemodified ASC";
284 if (!$pages = $DB->get_records_sql($sql, array($timestart, $course->id))) {
1fb5d4b0 285 return false;
286 }
3a7b9b76 287 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
1fb5d4b0 288
dd97c328 289 $wikis = array();
290
00710f4c
DC
291 $modinfo = get_fast_modinfo($course);
292
3a7b9b76 293 $subwikivisible = array();
00710f4c 294 foreach ($pages as $page) {
3a7b9b76
MG
295 if (!isset($subwikivisible[$page->subwikiid])) {
296 $subwiki = (object)array('id' => $page->subwikiid, 'wikiid' => $page->wikiid,
297 'groupid' => $page->groupid, 'userid' => $page->userid);
298 $wiki = (object)array('id' => $page->wikiid, 'course' => $course->id, 'wikimode' => $page->wikimode);
299 $subwikivisible[$page->subwikiid] = wiki_user_can_view($subwiki, $wiki);
00710f4c 300 }
3a7b9b76
MG
301 if ($subwikivisible[$page->subwikiid]) {
302 $wikis[] = $page;
00710f4c 303 }
1fb5d4b0 304 }
3a7b9b76 305 unset($subwikivisible);
00710f4c 306 unset($pages);
39fcb981 307
dd97c328 308 if (!$wikis) {
309 return false;
310 }
00710f4c 311 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 3);
dd97c328 312 foreach ($wikis as $wiki) {
00710f4c
DC
313 $cm = $modinfo->instances['wiki'][$wiki->wikiid];
314 $link = $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $wiki->id;
315 print_recent_activity_note($wiki->timemodified, $wiki, $cm->name, $link, false, $viewfullnames);
1fb5d4b0 316 }
dd97c328 317
00710f4c 318 return true; // True if anything was printed, otherwise false
39fcb981 319}
e5cc530b 320/**
321 * Function to be run periodically according to the moodle cron
322 * This function searches for things that need to be done, such
323 * as sending out mail, toggling flags etc ...
324 *
00710f4c
DC
325 * @uses $CFG
326 * @return boolean
327 * @todo Finish documenting this function
328 **/
329function wiki_cron() {
330 global $CFG;
39fcb981 331
00710f4c 332 return true;
39fcb981 333}
334
e5cc530b 335/**
00710f4c
DC
336 * Must return an array of grades for a given instance of this module,
337 * indexed by user. It also returns a maximum allowed grade.
e5cc530b 338 *
00710f4c
DC
339 * Example:
340 * $return->grades = array of grades;
341 * $return->maxgrade = maximum allowed grade;
e5cc530b 342 *
00710f4c 343 * return $return;
e5cc530b 344 *
00710f4c
DC
345 * @param int $wikiid ID of an instance of this module
346 * @return mixed Null or object with an array of grades and with the maximum grade
347 **/
348function wiki_grades($wikiid) {
349 return null;
39fcb981 350}
351
e5cc530b 352/**
00710f4c
DC
353 * This function returns if a scale is being used by one wiki
354 * it it has support for grading and scales. Commented code should be
355 * modified if necessary. See forum, glossary or journal modules
356 * as reference.
357 *
358 * @param int $wikiid ID of an instance of this module
359 * @return mixed
360 * @todo Finish documenting this function
361 **/
362function wiki_scale_used($wikiid, $scaleid) {
363 $return = false;
364
365 //$rec = get_record("wiki","id","$wikiid","scale","-$scaleid");
366 //
367 //if (!empty($rec) && !empty($scaleid)) {
368 // $return = true;
369 //}
39fcb981 370
00710f4c 371 return $return;
39fcb981 372}
373
e5cc530b 374/**
00710f4c
DC
375 * Checks if scale is being used by any instance of wiki.
376 * This function was added in 1.9
e5cc530b 377 *
00710f4c
DC
378 * This is used to find out if scale used anywhere
379 * @param $scaleid int
380 * @return boolean True if the scale is used by any wiki
e5cc530b 381 */
00710f4c 382function wiki_scale_used_anywhere($scaleid) {
655b09ca 383 global $DB;
c513f599 384
75a8ba54
DM
385 //if ($scaleid and $DB->record_exists('wiki', array('grade' => -$scaleid))) {
386 // return true;
387 //} else {
388 // return false;
389 //}
390
391 return false;
39fcb981 392}
393
e5cc530b 394/**
d2b7803e 395 * file serving callback
e5cc530b 396 *
d2b7803e
DC
397 * @copyright Josep Arus
398 * @package mod_wiki
399 * @category files
400 * @param stdClass $course course object
401 * @param stdClass $cm course module object
402 * @param stdClass $context context object
403 * @param string $filearea file area
404 * @param array $args extra arguments
405 * @param bool $forcedownload whether or not force download
261cbbac
DM
406 * @param array $options additional options affecting the file serving
407 * @return bool false if the file was not found, just send the file otherwise and do not return anything
e5cc530b 408 */
261cbbac 409function wiki_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
00710f4c 410 global $CFG;
14a960ac 411
64f93798
PS
412 if ($context->contextlevel != CONTEXT_MODULE) {
413 return false;
414 }
415
12c9bbbd 416 require_login($course, true, $cm);
64f93798 417
00710f4c 418 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
39fcb981 419
64f93798 420 if ($filearea == 'attachments') {
e8cf27e8 421 $swid = (int) array_shift($args);
b82fcee1 422
e8cf27e8 423 if (!$subwiki = wiki_get_subwiki($swid)) {
00710f4c 424 return false;
c513f599 425 }
c513f599 426
00710f4c 427 require_capability('mod/wiki:viewpage', $context);
f84d6a8d 428
64f93798 429 $relativepath = implode('/', $args);
39fcb981 430
64f93798 431 $fullpath = "/$context->id/mod_wiki/attachments/$swid/$relativepath";
87a65403 432
00710f4c
DC
433 $fs = get_file_storage();
434 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
435 return false;
87a65403 436 }
437
0c431257 438 send_stored_file($file, null, 0, $options);
39fcb981 439 }
39fcb981 440}
64f93798 441
d3adc16e 442function wiki_search_form($cm, $search = '', $subwiki = null) {
00710f4c 443 global $CFG, $OUTPUT;
39fcb981 444
621f3ef5
JP
445 $output = '<div class="wikisearch">';
446 $output .= '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/search.php" style="display:inline">';
00710f4c 447 $output .= '<fieldset class="invisiblefieldset">';
beff3a9d
JF
448 $output .= '<legend class="accesshide">'. get_string('searchwikis', 'wiki') .'</legend>';
449 $output .= '<label class="accesshide" for="searchwiki">' . get_string("searchterms", "wiki") . '</label>';
85bc1f48 450 $output .= '<input id="searchwiki" name="searchstring" type="text" size="18" value="' . s($search, true) . '" alt="search" />';
621f3ef5
JP
451 $output .= '<input name="courseid" type="hidden" value="' . $cm->course . '" />';
452 $output .= '<input name="cmid" type="hidden" value="' . $cm->id . '" />';
d3adc16e
MG
453 if (!empty($subwiki->id)) {
454 $output .= '<input name="subwikiid" type="hidden" value="' . $subwiki->id . '" />';
455 }
00710f4c 456 $output .= '<input name="searchwikicontent" type="hidden" value="1" />';
e566bfa1 457 $output .= '<input value="' . get_string('searchwikis', 'wiki') . '" type="submit" />';
00710f4c
DC
458 $output .= '</fieldset>';
459 $output .= '</form>';
460 $output .= '</div>';
39fcb981 461
00710f4c 462 return $output;
39fcb981 463}
00710f4c 464function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm) {
532d319d
JP
465 global $CFG, $PAGE, $USER;
466
621f3ef5 467 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
532d319d 468
1df23626 469 $context = context_module::instance($cm->id);
00710f4c
DC
470 $url = $PAGE->url;
471 $userid = 0;
472 if ($module->wikimode == 'individual') {
39fcb981 473 $userid = $USER->id;
474 }
39fcb981 475
621f3ef5 476 if (!$wiki = wiki_get_wiki($cm->instance)) {
00710f4c 477 return false;
87a65403 478 }
00710f4c 479
621f3ef5 480 if (!$gid = groups_get_activity_group($cm)) {
3ca72eb7
JP
481 $gid = 0;
482 }
621f3ef5 483 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)) {
00710f4c
DC
484 return null;
485 } else {
486 $swid = $subwiki->id;
27978302 487 }
14a960ac 488
00710f4c
DC
489 $pageid = $url->param('pageid');
490 $cmid = $url->param('id');
491 if (empty($pageid) && !empty($cmid)) {
492 // wiki main page
493 $page = wiki_get_page_by_title($swid, $wiki->firstpagetitle);
494 $pageid = $page->id;
495 }
12c9bbbd
DC
496
497 if (has_capability('mod/wiki:createpage', $context)) {
498 $link = new moodle_url('/mod/wiki/create.php', array('action' => 'new', 'swid' => $swid));
499 $node = $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING);
500 }
14a960ac 501
00710f4c 502 if (is_numeric($pageid)) {
39fcb981 503
12c9bbbd
DC
504 if (has_capability('mod/wiki:viewpage', $context)) {
505 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $pageid));
506 $node = $navref->add(get_string('view', 'wiki'), $link, navigation_node::TYPE_SETTING);
507 }
14a960ac 508
033e8461 509 if (wiki_user_can_edit($subwiki)) {
12c9bbbd
DC
510 $link = new moodle_url('/mod/wiki/edit.php', array('pageid' => $pageid));
511 $node = $navref->add(get_string('edit', 'wiki'), $link, navigation_node::TYPE_SETTING);
512 }
14a960ac 513
12c9bbbd
DC
514 if (has_capability('mod/wiki:viewcomment', $context)) {
515 $link = new moodle_url('/mod/wiki/comments.php', array('pageid' => $pageid));
516 $node = $navref->add(get_string('comments', 'wiki'), $link, navigation_node::TYPE_SETTING);
517 }
14a960ac 518
12c9bbbd
DC
519 if (has_capability('mod/wiki:viewpage', $context)) {
520 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
521 $node = $navref->add(get_string('history', 'wiki'), $link, navigation_node::TYPE_SETTING);
522 }
523
524 if (has_capability('mod/wiki:viewpage', $context)) {
525 $link = new moodle_url('/mod/wiki/map.php', array('pageid' => $pageid));
526 $node = $navref->add(get_string('map', 'wiki'), $link, navigation_node::TYPE_SETTING);
527 }
39fcb981 528
12c9bbbd
DC
529 if (has_capability('mod/wiki:viewpage', $context)) {
530 $link = new moodle_url('/mod/wiki/files.php', array('pageid' => $pageid));
531 $node = $navref->add(get_string('files', 'wiki'), $link, navigation_node::TYPE_SETTING);
532 }
ac0a82cf
RT
533
534 if (has_capability('mod/wiki:managewiki', $context)) {
535 $link = new moodle_url('/mod/wiki/admin.php', array('pageid' => $pageid));
536 $node = $navref->add(get_string('admin', 'wiki'), $link, navigation_node::TYPE_SETTING);
537 }
00710f4c 538 }
17da2e6f 539}
16b86ae4
DC
540/**
541 * Returns all other caps used in wiki module
542 *
543 * @return array
544 */
545function wiki_get_extra_capabilities() {
546 return array('moodle/comment:view', 'moodle/comment:post', 'moodle/comment:delete');
547}
c1951ea9
DC
548
549/**
550 * Running addtional permission check on plugin, for example, plugins
551 * may have switch to turn on/off comments option, this callback will
552 * affect UI display, not like pluginname_comment_validate only throw
553 * exceptions.
554 * Capability check has been done in comment->check_permissions(), we
555 * don't need to do it again here.
556 *
35453657
DC
557 * @package mod_wiki
558 * @category comment
559 *
c1951ea9
DC
560 * @param stdClass $comment_param {
561 * context => context the context object
562 * courseid => int course id
563 * cm => stdClass course module object
564 * commentarea => string comment area
565 * itemid => int itemid
566 * }
567 * @return array
568 */
569function wiki_comment_permissions($comment_param) {
570 return array('post'=>true, 'view'=>true);
571}
572
573/**
574 * Validate comment parameter before perform other comments actions
575 *
576 * @param stdClass $comment_param {
577 * context => context the context object
578 * courseid => int course id
579 * cm => stdClass course module object
580 * commentarea => string comment area
581 * itemid => int itemid
582 * }
35453657
DC
583 *
584 * @package mod_wiki
585 * @category comment
586 *
c1951ea9
DC
587 * @return boolean
588 */
589function wiki_comment_validate($comment_param) {
590 global $DB, $CFG;
591 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
592 // validate comment area
593 if ($comment_param->commentarea != 'wiki_page') {
594 throw new comment_exception('invalidcommentarea');
595 }
596 // validate itemid
597 if (!$record = $DB->get_record('wiki_pages', array('id'=>$comment_param->itemid))) {
598 throw new comment_exception('invalidcommentitemid');
599 }
600 if (!$subwiki = wiki_get_subwiki($record->subwikiid)) {
601 throw new comment_exception('invalidsubwikiid');
602 }
603 if (!$wiki = wiki_get_wiki_from_pageid($comment_param->itemid)) {
604 throw new comment_exception('invalidid', 'data');
605 }
606 if (!$course = $DB->get_record('course', array('id'=>$wiki->course))) {
607 throw new comment_exception('coursemisconf');
608 }
609 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $course->id)) {
610 throw new comment_exception('invalidcoursemodule');
611 }
1df23626 612 $context = context_module::instance($cm->id);
c1951ea9
DC
613 // group access
614 if ($subwiki->groupid) {
615 $groupmode = groups_get_activity_groupmode($cm, $course);
616 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
617 if (!groups_is_member($subwiki->groupid)) {
618 throw new comment_exception('notmemberofgroup');
619 }
620 }
621 }
622 // validate context id
623 if ($context->id != $comment_param->context->id) {
624 throw new comment_exception('invalidcontext');
625 }
626 // validation for comment deletion
627 if (!empty($comment_param->commentid)) {
628 if ($comment = $DB->get_record('comments', array('id'=>$comment_param->commentid))) {
629 if ($comment->commentarea != 'wiki_page') {
630 throw new comment_exception('invalidcommentarea');
631 }
632 if ($comment->contextid != $context->id) {
633 throw new comment_exception('invalidcontext');
634 }
635 if ($comment->itemid != $comment_param->itemid) {
636 throw new comment_exception('invalidcommentitemid');
637 }
638 } else {
639 throw new comment_exception('invalidcommentid');
640 }
641 }
642 return true;
643}
b1627a92
DC
644
645/**
646 * Return a list of page types
647 * @param string $pagetype current page type
648 * @param stdClass $parentcontext Block's parent context
649 * @param stdClass $currentcontext Current context of block
650 */
b38e2e28 651function wiki_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92
DC
652 $module_pagetype = array(
653 'mod-wiki-*'=>get_string('page-mod-wiki-x', 'wiki'),
654 'mod-wiki-view'=>get_string('page-mod-wiki-view', 'wiki'),
655 'mod-wiki-comments'=>get_string('page-mod-wiki-comments', 'wiki'),
656 'mod-wiki-history'=>get_string('page-mod-wiki-history', 'wiki'),
657 'mod-wiki-map'=>get_string('page-mod-wiki-map', 'wiki')
658 );
659 return $module_pagetype;
660}