3 // This file is part of Moodle - http://moodle.org/
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.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Library of functions and constants for module wiki
21 * It contains the great majority of functions defined by Moodle
22 * that are mandatory to develop a module.
24 * @package mod-wiki-2.0
25 * @copyrigth 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
26 * @copyrigth 2009 Universitat Politecnica de Catalunya http://www.upc.edu
28 * @author Jordi Piguillem
30 * @author David Jimenez
32 * @author Kenneth Riba
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * Given an object containing all the necessary data,
39 * (defined by the form in mod.html) this function
40 * will create a new instance and return the id number
41 * of the new instance.
43 * @param object $instance An object from the form in mod.html
44 * @return int The id of the newly inserted wiki record
46 function wiki_add_instance($wiki) {
49 $wiki->timemodified = time();
50 # May have to add extra stuff in here #
51 if (empty($wiki->forceformat)) {
52 $wiki->forceformat = 0;
54 return $DB->insert_record('wiki', $wiki);
58 * Given an object containing all the necessary data,
59 * (defined by the form in mod.html) this function
60 * will update an existing instance with new data.
62 * @param object $instance An object from the form in mod.html
63 * @return boolean Success/Fail
65 function wiki_update_instance($wiki) {
68 $wiki->timemodified = time();
69 $wiki->id = $wiki->instance;
70 if (empty($wiki->forceformat)) {
71 $wiki->forceformat = 0;
74 # May have to add extra stuff in here #
76 return $DB->update_record('wiki', $wiki);
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.
84 * @param int $id Id of the module instance
85 * @return boolean Success/Failure
87 function wiki_delete_instance($id) {
90 if (!$wiki = $DB->get_record('wiki', array('id' => $id))) {
96 # Get subwiki information #
97 $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id));
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)) {
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)) {
113 # Get versions, and delete them #
114 if (!$DB->delete_records('wiki_versions', array('pageid' => $page->id), IGNORE_MISSING)) {
120 if (!$DB->delete_records('wiki_pages', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
125 # Get existing synonyms, and delete them #
126 if (!$DB->delete_records('wiki_synonyms', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
130 # Delete any subwikis #
131 if (!$DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING)) {
136 # Delete any dependent records here #
137 if (!$DB->delete_records('wiki', array('id' => $wiki->id))) {
144 function wiki_reset_userdata($data) {
146 require_once($CFG->dirroot . '/mod/wiki/pagelib.php');
147 require_once($CFG->dirroot . '/tag/lib.php');
149 $componentstr = get_string('modulenameplural', 'wiki');
152 //get the wiki(s) in this course.
153 if (!$wikis = $DB->get_records('wiki', array('course' => $data->courseid))) {
157 foreach ($wikis as $wiki) {
159 // remove all comments
160 if (!empty($data->reset_wiki_comments)) {
161 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id)) {
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);
169 if (!empty($data->reset_wiki_tags)) {
170 # Get subwiki information #
171 $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id));
173 foreach ($subwikis as $subwiki) {
174 if ($pages = $DB->get_records('wiki_pages', array('subwikiid' => $subwiki->id))) {
175 foreach ($pages as $page) {
176 $tags = tag_get_tags_array('wiki_pages', $page->id);
177 foreach ($tags as $tagid => $tagname) {
178 // Delete the related tag_instances related to the wiki page.
179 $errors = tag_delete_instance('wiki_pages', $page->id, $tagid);
180 $status[] = array('component' => $componentstr, 'item' => get_string('tagsdeleted', 'wiki'), 'error' => $errors);
191 function wiki_reset_course_form_definition(&$mform) {
192 $mform->addElement('header', 'wikiheader', get_string('modulenameplural', 'wiki'));
193 $mform->addElement('advcheckbox', 'reset_wiki_tags', get_string('removeallwikitags', 'wiki'));
194 $mform->addElement('advcheckbox', 'reset_wiki_comments', get_string('deleteallcomments'));
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
205 * @todo Finish documenting this function
207 function wiki_user_outline($course, $user, $mod, $wiki) {
213 * Print a detailed representation of what a user has done with
214 * a given particular instance of this module, for user activity reports.
217 * @todo Finish documenting this function
219 function wiki_user_complete($course, $user, $mod, $wiki) {
224 * Indicates API features that the wiki supports.
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)
237 function wiki_supports($feature) {
241 case FEATURE_GROUPINGS:
243 case FEATURE_GROUPMEMBERSONLY:
245 case FEATURE_MOD_INTRO:
247 case FEATURE_COMPLETION_TRACKS_VIEWS:
249 case FEATURE_GRADE_HAS_GRADE:
251 case FEATURE_GRADE_OUTCOMES:
255 case FEATURE_BACKUP_MOODLE2:
264 * Given a course and a time, this module should find recent activity
265 * that has occurred in wiki activities and print it out.
266 * Return true if there was output, or false is there was none.
270 * @uses CONTEXT_MODULE
271 * @uses VISIBLEGROUPS
272 * @param object $course
273 * @param bool $viewfullnames capability
274 * @param int $timestart
277 function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
278 global $CFG, $DB, $OUTPUT;
280 $sql = "SELECT p.*, w.id as wikiid, sw.groupid
282 JOIN {wiki_subwikis} sw ON sw.id = p.subwikiid
283 JOIN {wiki} w ON w.id = sw.wikiid
284 WHERE p.timemodified > ? AND w.course = ?
285 ORDER BY p.timemodified ASC";
286 if (!$pages = $DB->get_records_sql($sql, array($timestart, $course->id))) {
289 $modinfo =& get_fast_modinfo($course);
293 $modinfo = get_fast_modinfo($course);
295 foreach ($pages as $page) {
296 if (!isset($modinfo->instances['wiki'][$page->wikiid])) {
300 $cm = $modinfo->instances['wiki'][$page->wikiid];
301 if (!$cm->uservisible) {
304 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
306 if (!has_capability('mod/wiki:viewpage', $context)) {
310 $groupmode = groups_get_activity_groupmode($cm, $course);
313 if ($groupmode == SEPARATEGROUPS and !has_capability('mod/wiki:managewiki', $context)) {
320 if (is_null($modinfo->groups)) {
321 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
324 if (!in_array($page->groupid, $modinfo->groups[0])) {
336 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 3);
337 foreach ($wikis as $wiki) {
338 $cm = $modinfo->instances['wiki'][$wiki->wikiid];
339 $link = $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $wiki->id;
340 print_recent_activity_note($wiki->timemodified, $wiki, $cm->name, $link, false, $viewfullnames);
343 return true; // True if anything was printed, otherwise false
346 * Function to be run periodically according to the moodle cron
347 * This function searches for things that need to be done, such
348 * as sending out mail, toggling flags etc ...
352 * @todo Finish documenting this function
354 function wiki_cron() {
361 * Must return an array of grades for a given instance of this module,
362 * indexed by user. It also returns a maximum allowed grade.
365 * $return->grades = array of grades;
366 * $return->maxgrade = maximum allowed grade;
370 * @param int $wikiid ID of an instance of this module
371 * @return mixed Null or object with an array of grades and with the maximum grade
373 function wiki_grades($wikiid) {
378 * Must return an array of user records (all data) who are participants
379 * for a given instance of wiki. Must include every user involved
380 * in the instance, independient of his role (student, teacher, admin...)
381 * See other modules as example.
383 * @todo: deprecated - to be deleted in 2.2
385 * @param int $wikiid ID of an instance of this module
386 * @return mixed boolean/array of students
388 function wiki_get_participants($wikiid) {
393 * This function returns if a scale is being used by one wiki
394 * it it has support for grading and scales. Commented code should be
395 * modified if necessary. See forum, glossary or journal modules
398 * @param int $wikiid ID of an instance of this module
400 * @todo Finish documenting this function
402 function wiki_scale_used($wikiid, $scaleid) {
405 //$rec = get_record("wiki","id","$wikiid","scale","-$scaleid");
407 //if (!empty($rec) && !empty($scaleid)) {
415 * Checks if scale is being used by any instance of wiki.
416 * This function was added in 1.9
418 * This is used to find out if scale used anywhere
419 * @param $scaleid int
420 * @return boolean True if the scale is used by any wiki
422 function wiki_scale_used_anywhere($scaleid) {
425 //if ($scaleid and $DB->record_exists('wiki', array('grade' => -$scaleid))) {
439 function wiki_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
442 if ($context->contextlevel != CONTEXT_MODULE) {
446 require_login($course, true, $cm);
448 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
450 if ($filearea == 'attachments') {
451 $swid = (int) array_shift($args);
453 if (!$subwiki = wiki_get_subwiki($swid)) {
457 require_capability('mod/wiki:viewpage', $context);
459 $relativepath = implode('/', $args);
461 $fullpath = "/$context->id/mod_wiki/attachments/$swid/$relativepath";
463 $fs = get_file_storage();
464 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
468 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
470 send_stored_file($file, $lifetime, 0);
474 function wiki_search_form($cm, $search = '') {
475 global $CFG, $OUTPUT;
477 $output = '<div class="wikisearch">';
478 $output .= '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/search.php" style="display:inline">';
479 $output .= '<fieldset class="invisiblefieldset">';
480 $output .= '<input name="searchstring" type="text" size="18" value="' . s($search, true) . '" alt="search" />';
481 $output .= '<input name="courseid" type="hidden" value="' . $cm->course . '" />';
482 $output .= '<input name="cmid" type="hidden" value="' . $cm->id . '" />';
483 $output .= '<input name="searchwikicontent" type="hidden" value="1" />';
484 $output .= ' <input value="' . get_string('searchwikis', 'wiki') . '" type="submit" />';
485 $output .= '</fieldset>';
486 $output .= '</form>';
491 function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm) {
492 global $CFG, $PAGE, $USER;
494 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
496 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
499 if ($module->wikimode == 'individual') {
503 if (!$wiki = wiki_get_wiki($cm->instance)) {
507 if (!$gid = groups_get_activity_group($cm)) {
510 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)) {
513 $swid = $subwiki->id;
516 $pageid = $url->param('pageid');
517 $cmid = $url->param('id');
518 if (empty($pageid) && !empty($cmid)) {
520 $page = wiki_get_page_by_title($swid, $wiki->firstpagetitle);
524 if (has_capability('mod/wiki:createpage', $context)) {
525 $link = new moodle_url('/mod/wiki/create.php', array('action' => 'new', 'swid' => $swid));
526 $node = $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING);
529 if (is_numeric($pageid)) {
531 if (has_capability('mod/wiki:viewpage', $context)) {
532 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $pageid));
533 $node = $navref->add(get_string('view', 'wiki'), $link, navigation_node::TYPE_SETTING);
536 if (has_capability('mod/wiki:editpage', $context)) {
537 $link = new moodle_url('/mod/wiki/edit.php', array('pageid' => $pageid));
538 $node = $navref->add(get_string('edit', 'wiki'), $link, navigation_node::TYPE_SETTING);
541 if (has_capability('mod/wiki:viewcomment', $context)) {
542 $link = new moodle_url('/mod/wiki/comments.php', array('pageid' => $pageid));
543 $node = $navref->add(get_string('comments', 'wiki'), $link, navigation_node::TYPE_SETTING);
546 if (has_capability('mod/wiki:viewpage', $context)) {
547 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
548 $node = $navref->add(get_string('history', 'wiki'), $link, navigation_node::TYPE_SETTING);
551 if (has_capability('mod/wiki:viewpage', $context)) {
552 $link = new moodle_url('/mod/wiki/map.php', array('pageid' => $pageid));
553 $node = $navref->add(get_string('map', 'wiki'), $link, navigation_node::TYPE_SETTING);
556 if (has_capability('mod/wiki:viewpage', $context)) {
557 $link = new moodle_url('/mod/wiki/files.php', array('pageid' => $pageid));
558 $node = $navref->add(get_string('files', 'wiki'), $link, navigation_node::TYPE_SETTING);
563 * Returns all other caps used in wiki module
567 function wiki_get_extra_capabilities() {
568 return array('moodle/comment:view', 'moodle/comment:post', 'moodle/comment:delete');
572 * Running addtional permission check on plugin, for example, plugins
573 * may have switch to turn on/off comments option, this callback will
574 * affect UI display, not like pluginname_comment_validate only throw
576 * Capability check has been done in comment->check_permissions(), we
577 * don't need to do it again here.
579 * @param stdClass $comment_param {
580 * context => context the context object
581 * courseid => int course id
582 * cm => stdClass course module object
583 * commentarea => string comment area
584 * itemid => int itemid
588 function wiki_comment_permissions($comment_param) {
589 return array('post'=>true, 'view'=>true);
593 * Validate comment parameter before perform other comments actions
595 * @param stdClass $comment_param {
596 * context => context the context object
597 * courseid => int course id
598 * cm => stdClass course module object
599 * commentarea => string comment area
600 * itemid => int itemid
604 function wiki_comment_validate($comment_param) {
606 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
607 // validate comment area
608 if ($comment_param->commentarea != 'wiki_page') {
609 throw new comment_exception('invalidcommentarea');
612 if (!$record = $DB->get_record('wiki_pages', array('id'=>$comment_param->itemid))) {
613 throw new comment_exception('invalidcommentitemid');
615 if (!$subwiki = wiki_get_subwiki($record->subwikiid)) {
616 throw new comment_exception('invalidsubwikiid');
618 if (!$wiki = wiki_get_wiki_from_pageid($comment_param->itemid)) {
619 throw new comment_exception('invalidid', 'data');
621 if (!$course = $DB->get_record('course', array('id'=>$wiki->course))) {
622 throw new comment_exception('coursemisconf');
624 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $course->id)) {
625 throw new comment_exception('invalidcoursemodule');
627 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
629 if ($subwiki->groupid) {
630 $groupmode = groups_get_activity_groupmode($cm, $course);
631 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
632 if (!groups_is_member($subwiki->groupid)) {
633 throw new comment_exception('notmemberofgroup');
637 // validate context id
638 if ($context->id != $comment_param->context->id) {
639 throw new comment_exception('invalidcontext');
641 // validation for comment deletion
642 if (!empty($comment_param->commentid)) {
643 if ($comment = $DB->get_record('comments', array('id'=>$comment_param->commentid))) {
644 if ($comment->commentarea != 'wiki_page') {
645 throw new comment_exception('invalidcommentarea');
647 if ($comment->contextid != $context->id) {
648 throw new comment_exception('invalidcontext');
650 if ($comment->itemid != $comment_param->itemid) {
651 throw new comment_exception('invalidcommentitemid');
654 throw new comment_exception('invalidcommentid');
661 * Return a list of page types
662 * @param string $pagetype current page type
663 * @param stdClass $parentcontext Block's parent context
664 * @param stdClass $currentcontext Current context of block
666 function wiki_page_type_list($pagetype, $parentcontext, $currentcontext) {
667 $module_pagetype = array(
668 'mod-wiki-*'=>get_string('page-mod-wiki-x', 'wiki'),
669 'mod-wiki-view'=>get_string('page-mod-wiki-view', 'wiki'),
670 'mod-wiki-comments'=>get_string('page-mod-wiki-comments', 'wiki'),
671 'mod-wiki-history'=>get_string('page-mod-wiki-history', 'wiki'),
672 'mod-wiki-map'=>get_string('page-mod-wiki-map', 'wiki')
674 return $module_pagetype;