Merge branch 'MDL-51582-30' of git://github.com/danpoltawski/moodle into MOODLE_30_STABLE
[moodle.git] / mod / wiki / lib.php
blobddd785c1c194936bd48f3b6a581cfaa10ce9dfc6
1 <?php
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
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/>.
18 /**
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
25 * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
26 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
28 * @author Jordi Piguillem
29 * @author Marc Alier
30 * @author David Jimenez
31 * @author Josep Arus
32 * @author Kenneth Riba
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 /**
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
45 **/
46 function wiki_add_instance($wiki) {
47 global $DB;
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);
57 /**
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
64 **/
65 function wiki_update_instance($wiki) {
66 global $DB;
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);
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.
84 * @param int $id Id of the module instance
85 * @return boolean Success/Failure
86 **/
87 function wiki_delete_instance($id) {
88 global $DB;
90 if (!$wiki = $DB->get_record('wiki', array('id' => $id))) {
91 return false;
94 $result = true;
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)) {
102 $result = false;
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;
113 # Get versions, and delete them #
114 if (!$DB->delete_records('wiki_versions', array('pageid' => $page->id), IGNORE_MISSING)) {
115 $result = false;
119 # Delete pages #
120 if (!$DB->delete_records('wiki_pages', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
121 $result = false;
125 # Get existing synonyms, and delete them #
126 if (!$DB->delete_records('wiki_synonyms', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
127 $result = false;
130 # Delete any subwikis #
131 if (!$DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING)) {
132 $result = false;
136 # Delete any dependent records here #
137 if (!$DB->delete_records('wiki', array('id' => $wiki->id))) {
138 $result = false;
141 return $result;
144 function 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 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
150 $componentstr = get_string('modulenameplural', 'wiki');
151 $status = array();
153 //get the wiki(s) in this course.
154 if (!$wikis = $DB->get_records('wiki', array('course' => $data->courseid))) {
155 return false;
157 $errors = false;
158 foreach ($wikis as $wiki) {
160 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id)) {
161 continue;
163 $context = context_module::instance($cm->id);
165 // Remove tags or all pages.
166 if (!empty($data->reset_wiki_pages) || !empty($data->reset_wiki_tags)) {
168 // Get subwiki information.
169 $subwikis = wiki_get_subwikis($wiki->id);
171 foreach ($subwikis as $subwiki) {
172 // Get existing pages.
173 if ($pages = wiki_get_page_list($subwiki->id)) {
174 // If the wiki page isn't selected then we are only removing tags.
175 if (empty($data->reset_wiki_pages)) {
176 // Go through each page and delete the tags.
177 foreach ($pages as $page) {
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);
187 } else {
188 // Otherwise we are removing pages and tags.
189 wiki_delete_pages($context, $pages, $subwiki->id);
192 if (!empty($data->reset_wiki_pages)) {
193 // Delete any subwikis.
194 $DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING);
196 // Delete any attached files.
197 $fs = get_file_storage();
198 $fs->delete_area_files($context->id, 'mod_wiki', 'attachments');
200 $status[] = array('component' => $componentstr, 'item' => get_string('deleteallpages', 'wiki'),
201 'error' => $errors);
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);
212 return $status;
216 function wiki_reset_course_form_definition(&$mform) {
217 $mform->addElement('header', 'wikiheader', get_string('modulenameplural', 'wiki'));
218 $mform->addElement('advcheckbox', 'reset_wiki_pages', get_string('deleteallpages', 'wiki'));
219 $mform->addElement('advcheckbox', 'reset_wiki_tags', get_string('removeallwikitags', 'wiki'));
220 $mform->addElement('advcheckbox', 'reset_wiki_comments', get_string('deleteallcomments'));
224 * Indicates API features that the wiki supports.
226 * @uses FEATURE_GROUPS
227 * @uses FEATURE_GROUPINGS
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)
236 function wiki_supports($feature) {
237 switch ($feature) {
238 case FEATURE_GROUPS:
239 return true;
240 case FEATURE_GROUPINGS:
241 return true;
242 case FEATURE_MOD_INTRO:
243 return true;
244 case FEATURE_COMPLETION_TRACKS_VIEWS:
245 return true;
246 case FEATURE_GRADE_HAS_GRADE:
247 return false;
248 case FEATURE_GRADE_OUTCOMES:
249 return false;
250 case FEATURE_RATE:
251 return false;
252 case FEATURE_BACKUP_MOODLE2:
253 return true;
254 case FEATURE_SHOW_DESCRIPTION:
255 return true;
257 default:
258 return null;
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.
267 * @global $CFG
268 * @global $DB
269 * @uses CONTEXT_MODULE
270 * @uses VISIBLEGROUPS
271 * @param object $course
272 * @param bool $viewfullnames capability
273 * @param int $timestart
274 * @return boolean
276 function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
277 global $CFG, $DB, $OUTPUT;
279 $sql = "SELECT p.id, p.timemodified, p.subwikiid, sw.wikiid, w.wikimode, sw.userid, sw.groupid
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))) {
286 return false;
288 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
290 $wikis = array();
292 $modinfo = get_fast_modinfo($course);
294 $subwikivisible = array();
295 foreach ($pages as $page) {
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);
302 if ($subwikivisible[$page->subwikiid]) {
303 $wikis[] = $page;
306 unset($subwikivisible);
307 unset($pages);
309 if (!$wikis) {
310 return false;
312 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 3);
313 foreach ($wikis as $wiki) {
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);
319 return true; // True if anything was printed, otherwise false
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 ...
326 * @uses $CFG
327 * @return boolean
328 * @todo Finish documenting this function
330 function wiki_cron() {
331 global $CFG;
333 return true;
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.
340 * Example:
341 * $return->grades = array of grades;
342 * $return->maxgrade = maximum allowed grade;
344 * return $return;
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
349 function wiki_grades($wikiid) {
350 return null;
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.
359 * @param int $wikiid ID of an instance of this module
360 * @return mixed
361 * @todo Finish documenting this function
363 function wiki_scale_used($wikiid, $scaleid) {
364 $return = false;
366 //$rec = get_record("wiki","id","$wikiid","scale","-$scaleid");
368 //if (!empty($rec) && !empty($scaleid)) {
369 // $return = true;
372 return $return;
376 * Checks if scale is being used by any instance of wiki.
377 * This function was added in 1.9
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
383 function wiki_scale_used_anywhere($scaleid) {
384 global $DB;
386 //if ($scaleid and $DB->record_exists('wiki', array('grade' => -$scaleid))) {
387 // return true;
388 //} else {
389 // return false;
392 return false;
396 * file serving callback
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
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
410 function wiki_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
411 global $CFG;
413 if ($context->contextlevel != CONTEXT_MODULE) {
414 return false;
417 require_login($course, true, $cm);
419 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
421 if ($filearea == 'attachments') {
422 $swid = (int) array_shift($args);
424 if (!$subwiki = wiki_get_subwiki($swid)) {
425 return false;
428 require_capability('mod/wiki:viewpage', $context);
430 $relativepath = implode('/', $args);
432 $fullpath = "/$context->id/mod_wiki/attachments/$swid/$relativepath";
434 $fs = get_file_storage();
435 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
436 return false;
439 send_stored_file($file, null, 0, $options);
443 function wiki_search_form($cm, $search = '', $subwiki = null) {
444 global $CFG, $OUTPUT;
446 $output = '<div class="wikisearch">';
447 $output .= '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/search.php" style="display:inline">';
448 $output .= '<fieldset class="invisiblefieldset">';
449 $output .= '<legend class="accesshide">'. get_string('searchwikis', 'wiki') .'</legend>';
450 $output .= '<label class="accesshide" for="searchwiki">' . get_string("searchterms", "wiki") . '</label>';
451 $output .= '<input id="searchwiki" name="searchstring" type="text" size="18" value="' . s($search, true) . '" alt="search" />';
452 $output .= '<input name="courseid" type="hidden" value="' . $cm->course . '" />';
453 $output .= '<input name="cmid" type="hidden" value="' . $cm->id . '" />';
454 if (!empty($subwiki->id)) {
455 $output .= '<input name="subwikiid" type="hidden" value="' . $subwiki->id . '" />';
457 $output .= '<input name="searchwikicontent" type="hidden" value="1" />';
458 $output .= '<input value="' . get_string('searchwikis', 'wiki') . '" type="submit" />';
459 $output .= '</fieldset>';
460 $output .= '</form>';
461 $output .= '</div>';
463 return $output;
465 function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm) {
466 global $CFG, $PAGE, $USER;
468 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
470 $context = context_module::instance($cm->id);
471 $url = $PAGE->url;
472 $userid = 0;
473 if ($module->wikimode == 'individual') {
474 $userid = $USER->id;
477 if (!$wiki = wiki_get_wiki($cm->instance)) {
478 return false;
481 if (!$gid = groups_get_activity_group($cm)) {
482 $gid = 0;
484 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)) {
485 return null;
486 } else {
487 $swid = $subwiki->id;
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;
498 if (has_capability('mod/wiki:createpage', $context)) {
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);
503 if (is_numeric($pageid)) {
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);
510 if (wiki_user_can_edit($subwiki)) {
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);
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);
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);
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);
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);
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);
542 * Returns all other caps used in wiki module
544 * @return array
546 function wiki_get_extra_capabilities() {
547 return array('moodle/comment:view', 'moodle/comment:post', 'moodle/comment:delete');
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.
558 * @package mod_wiki
559 * @category comment
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
568 * @return array
570 function wiki_comment_permissions($comment_param) {
571 return array('post'=>true, 'view'=>true);
575 * Validate comment parameter before perform other comments actions
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
585 * @package mod_wiki
586 * @category comment
588 * @return boolean
590 function 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');
597 // validate itemid
598 if (!$record = $DB->get_record('wiki_pages', array('id'=>$comment_param->itemid))) {
599 throw new comment_exception('invalidcommentitemid');
601 if (!$subwiki = wiki_get_subwiki($record->subwikiid)) {
602 throw new comment_exception('invalidsubwikiid');
604 if (!$wiki = wiki_get_wiki_from_pageid($comment_param->itemid)) {
605 throw new comment_exception('invalidid', 'data');
607 if (!$course = $DB->get_record('course', array('id'=>$wiki->course))) {
608 throw new comment_exception('coursemisconf');
610 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $course->id)) {
611 throw new comment_exception('invalidcoursemodule');
613 $context = context_module::instance($cm->id);
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');
623 // validate context id
624 if ($context->id != $comment_param->context->id) {
625 throw new comment_exception('invalidcontext');
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');
633 if ($comment->contextid != $context->id) {
634 throw new comment_exception('invalidcontext');
636 if ($comment->itemid != $comment_param->itemid) {
637 throw new comment_exception('invalidcommentitemid');
639 } else {
640 throw new comment_exception('invalidcommentid');
643 return true;
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
652 function wiki_page_type_list($pagetype, $parentcontext, $currentcontext) {
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')
660 return $module_pagetype;