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