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