MDL-33486 wiki: allow searching individual wikis
[moodle.git] / mod / wiki / lib.php
blob5f81dddf07881baa8f8d80e4448e62c586d11c2d
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-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
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');
149 $componentstr = get_string('modulenameplural', 'wiki');
150 $status = array();
152 //get the wiki(s) in this course.
153 if (!$wikis = $DB->get_records('wiki', array('course' => $data->courseid))) {
154 return false;
156 $errors = false;
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)) {
162 continue;
164 $context = context_module::instance($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);
187 return $status;
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
204 * @return null
205 * @todo Finish documenting this function
207 function wiki_user_outline($course, $user, $mod, $wiki) {
208 $return = NULL;
209 return $return;
213 * Print a detailed representation of what a user has done with
214 * a given particular instance of this module, for user activity reports.
216 * @return boolean
217 * @todo Finish documenting this function
219 function wiki_user_complete($course, $user, $mod, $wiki) {
220 return true;
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) {
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_GRADE_HAS_GRADE:
250 return false;
251 case FEATURE_GRADE_OUTCOMES:
252 return false;
253 case FEATURE_RATE:
254 return false;
255 case FEATURE_BACKUP_MOODLE2:
256 return true;
257 case FEATURE_SHOW_DESCRIPTION:
258 return true;
260 default:
261 return null;
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.
270 * @global $CFG
271 * @global $DB
272 * @uses CONTEXT_MODULE
273 * @uses VISIBLEGROUPS
274 * @param object $course
275 * @param bool $viewfullnames capability
276 * @param int $timestart
277 * @return boolean
279 function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
280 global $CFG, $DB, $OUTPUT;
282 $sql = "SELECT p.id, p.timemodified, p.subwikiid, sw.wikiid, w.wikimode, sw.userid, 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))) {
289 return false;
291 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
293 $wikis = array();
295 $modinfo = get_fast_modinfo($course);
297 $subwikivisible = array();
298 foreach ($pages as $page) {
299 if (!isset($subwikivisible[$page->subwikiid])) {
300 $subwiki = (object)array('id' => $page->subwikiid, 'wikiid' => $page->wikiid,
301 'groupid' => $page->groupid, 'userid' => $page->userid);
302 $wiki = (object)array('id' => $page->wikiid, 'course' => $course->id, 'wikimode' => $page->wikimode);
303 $subwikivisible[$page->subwikiid] = wiki_user_can_view($subwiki, $wiki);
305 if ($subwikivisible[$page->subwikiid]) {
306 $wikis[] = $page;
309 unset($subwikivisible);
310 unset($pages);
312 if (!$wikis) {
313 return false;
315 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 3);
316 foreach ($wikis as $wiki) {
317 $cm = $modinfo->instances['wiki'][$wiki->wikiid];
318 $link = $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $wiki->id;
319 print_recent_activity_note($wiki->timemodified, $wiki, $cm->name, $link, false, $viewfullnames);
322 return true; // True if anything was printed, otherwise false
325 * Function to be run periodically according to the moodle cron
326 * This function searches for things that need to be done, such
327 * as sending out mail, toggling flags etc ...
329 * @uses $CFG
330 * @return boolean
331 * @todo Finish documenting this function
333 function wiki_cron() {
334 global $CFG;
336 return true;
340 * Must return an array of grades for a given instance of this module,
341 * indexed by user. It also returns a maximum allowed grade.
343 * Example:
344 * $return->grades = array of grades;
345 * $return->maxgrade = maximum allowed grade;
347 * return $return;
349 * @param int $wikiid ID of an instance of this module
350 * @return mixed Null or object with an array of grades and with the maximum grade
352 function wiki_grades($wikiid) {
353 return null;
357 * This function returns if a scale is being used by one wiki
358 * it it has support for grading and scales. Commented code should be
359 * modified if necessary. See forum, glossary or journal modules
360 * as reference.
362 * @param int $wikiid ID of an instance of this module
363 * @return mixed
364 * @todo Finish documenting this function
366 function wiki_scale_used($wikiid, $scaleid) {
367 $return = false;
369 //$rec = get_record("wiki","id","$wikiid","scale","-$scaleid");
371 //if (!empty($rec) && !empty($scaleid)) {
372 // $return = true;
375 return $return;
379 * Checks if scale is being used by any instance of wiki.
380 * This function was added in 1.9
382 * This is used to find out if scale used anywhere
383 * @param $scaleid int
384 * @return boolean True if the scale is used by any wiki
386 function wiki_scale_used_anywhere($scaleid) {
387 global $DB;
389 //if ($scaleid and $DB->record_exists('wiki', array('grade' => -$scaleid))) {
390 // return true;
391 //} else {
392 // return false;
395 return false;
399 * file serving callback
401 * @copyright Josep Arus
402 * @package mod_wiki
403 * @category files
404 * @param stdClass $course course object
405 * @param stdClass $cm course module object
406 * @param stdClass $context context object
407 * @param string $filearea file area
408 * @param array $args extra arguments
409 * @param bool $forcedownload whether or not force download
410 * @param array $options additional options affecting the file serving
411 * @return bool false if the file was not found, just send the file otherwise and do not return anything
413 function wiki_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
414 global $CFG;
416 if ($context->contextlevel != CONTEXT_MODULE) {
417 return false;
420 require_login($course, true, $cm);
422 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
424 if ($filearea == 'attachments') {
425 $swid = (int) array_shift($args);
427 if (!$subwiki = wiki_get_subwiki($swid)) {
428 return false;
431 require_capability('mod/wiki:viewpage', $context);
433 $relativepath = implode('/', $args);
435 $fullpath = "/$context->id/mod_wiki/attachments/$swid/$relativepath";
437 $fs = get_file_storage();
438 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
439 return false;
442 send_stored_file($file, null, 0, $options);
446 function wiki_search_form($cm, $search = '', $subwiki = null) {
447 global $CFG, $OUTPUT;
449 $output = '<div class="wikisearch">';
450 $output .= '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/search.php" style="display:inline">';
451 $output .= '<fieldset class="invisiblefieldset">';
452 $output .= '<legend class="accesshide">'. get_string('searchwikis', 'wiki') .'</legend>';
453 $output .= '<label class="accesshide" for="searchwiki">' . get_string("searchterms", "wiki") . '</label>';
454 $output .= '<input id="searchwiki" name="searchstring" type="text" size="18" value="' . s($search, true) . '" alt="search" />';
455 $output .= '<input name="courseid" type="hidden" value="' . $cm->course . '" />';
456 $output .= '<input name="cmid" type="hidden" value="' . $cm->id . '" />';
457 if (!empty($subwiki->id)) {
458 $output .= '<input name="subwikiid" type="hidden" value="' . $subwiki->id . '" />';
460 $output .= '<input name="searchwikicontent" type="hidden" value="1" />';
461 $output .= '<input value="' . get_string('searchwikis', 'wiki') . '" type="submit" />';
462 $output .= '</fieldset>';
463 $output .= '</form>';
464 $output .= '</div>';
466 return $output;
468 function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm) {
469 global $CFG, $PAGE, $USER;
471 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
473 $context = context_module::instance($cm->id);
474 $url = $PAGE->url;
475 $userid = 0;
476 if ($module->wikimode == 'individual') {
477 $userid = $USER->id;
480 if (!$wiki = wiki_get_wiki($cm->instance)) {
481 return false;
484 if (!$gid = groups_get_activity_group($cm)) {
485 $gid = 0;
487 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)) {
488 return null;
489 } else {
490 $swid = $subwiki->id;
493 $pageid = $url->param('pageid');
494 $cmid = $url->param('id');
495 if (empty($pageid) && !empty($cmid)) {
496 // wiki main page
497 $page = wiki_get_page_by_title($swid, $wiki->firstpagetitle);
498 $pageid = $page->id;
501 if (has_capability('mod/wiki:createpage', $context)) {
502 $link = new moodle_url('/mod/wiki/create.php', array('action' => 'new', 'swid' => $swid));
503 $node = $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING);
506 if (is_numeric($pageid)) {
508 if (has_capability('mod/wiki:viewpage', $context)) {
509 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $pageid));
510 $node = $navref->add(get_string('view', 'wiki'), $link, navigation_node::TYPE_SETTING);
513 if (wiki_user_can_edit($subwiki)) {
514 $link = new moodle_url('/mod/wiki/edit.php', array('pageid' => $pageid));
515 $node = $navref->add(get_string('edit', 'wiki'), $link, navigation_node::TYPE_SETTING);
518 if (has_capability('mod/wiki:viewcomment', $context)) {
519 $link = new moodle_url('/mod/wiki/comments.php', array('pageid' => $pageid));
520 $node = $navref->add(get_string('comments', 'wiki'), $link, navigation_node::TYPE_SETTING);
523 if (has_capability('mod/wiki:viewpage', $context)) {
524 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
525 $node = $navref->add(get_string('history', 'wiki'), $link, navigation_node::TYPE_SETTING);
528 if (has_capability('mod/wiki:viewpage', $context)) {
529 $link = new moodle_url('/mod/wiki/map.php', array('pageid' => $pageid));
530 $node = $navref->add(get_string('map', 'wiki'), $link, navigation_node::TYPE_SETTING);
533 if (has_capability('mod/wiki:viewpage', $context)) {
534 $link = new moodle_url('/mod/wiki/files.php', array('pageid' => $pageid));
535 $node = $navref->add(get_string('files', 'wiki'), $link, navigation_node::TYPE_SETTING);
538 if (has_capability('mod/wiki:managewiki', $context)) {
539 $link = new moodle_url('/mod/wiki/admin.php', array('pageid' => $pageid));
540 $node = $navref->add(get_string('admin', 'wiki'), $link, navigation_node::TYPE_SETTING);
545 * Returns all other caps used in wiki module
547 * @return array
549 function wiki_get_extra_capabilities() {
550 return array('moodle/comment:view', 'moodle/comment:post', 'moodle/comment:delete');
554 * Running addtional permission check on plugin, for example, plugins
555 * may have switch to turn on/off comments option, this callback will
556 * affect UI display, not like pluginname_comment_validate only throw
557 * exceptions.
558 * Capability check has been done in comment->check_permissions(), we
559 * don't need to do it again here.
561 * @package mod_wiki
562 * @category comment
564 * @param stdClass $comment_param {
565 * context => context the context object
566 * courseid => int course id
567 * cm => stdClass course module object
568 * commentarea => string comment area
569 * itemid => int itemid
571 * @return array
573 function wiki_comment_permissions($comment_param) {
574 return array('post'=>true, 'view'=>true);
578 * Validate comment parameter before perform other comments actions
580 * @param stdClass $comment_param {
581 * context => context the context object
582 * courseid => int course id
583 * cm => stdClass course module object
584 * commentarea => string comment area
585 * itemid => int itemid
588 * @package mod_wiki
589 * @category comment
591 * @return boolean
593 function wiki_comment_validate($comment_param) {
594 global $DB, $CFG;
595 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
596 // validate comment area
597 if ($comment_param->commentarea != 'wiki_page') {
598 throw new comment_exception('invalidcommentarea');
600 // validate itemid
601 if (!$record = $DB->get_record('wiki_pages', array('id'=>$comment_param->itemid))) {
602 throw new comment_exception('invalidcommentitemid');
604 if (!$subwiki = wiki_get_subwiki($record->subwikiid)) {
605 throw new comment_exception('invalidsubwikiid');
607 if (!$wiki = wiki_get_wiki_from_pageid($comment_param->itemid)) {
608 throw new comment_exception('invalidid', 'data');
610 if (!$course = $DB->get_record('course', array('id'=>$wiki->course))) {
611 throw new comment_exception('coursemisconf');
613 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $course->id)) {
614 throw new comment_exception('invalidcoursemodule');
616 $context = context_module::instance($cm->id);
617 // group access
618 if ($subwiki->groupid) {
619 $groupmode = groups_get_activity_groupmode($cm, $course);
620 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
621 if (!groups_is_member($subwiki->groupid)) {
622 throw new comment_exception('notmemberofgroup');
626 // validate context id
627 if ($context->id != $comment_param->context->id) {
628 throw new comment_exception('invalidcontext');
630 // validation for comment deletion
631 if (!empty($comment_param->commentid)) {
632 if ($comment = $DB->get_record('comments', array('id'=>$comment_param->commentid))) {
633 if ($comment->commentarea != 'wiki_page') {
634 throw new comment_exception('invalidcommentarea');
636 if ($comment->contextid != $context->id) {
637 throw new comment_exception('invalidcontext');
639 if ($comment->itemid != $comment_param->itemid) {
640 throw new comment_exception('invalidcommentitemid');
642 } else {
643 throw new comment_exception('invalidcommentid');
646 return true;
650 * Return a list of page types
651 * @param string $pagetype current page type
652 * @param stdClass $parentcontext Block's parent context
653 * @param stdClass $currentcontext Current context of block
655 function wiki_page_type_list($pagetype, $parentcontext, $currentcontext) {
656 $module_pagetype = array(
657 'mod-wiki-*'=>get_string('page-mod-wiki-x', 'wiki'),
658 'mod-wiki-view'=>get_string('page-mod-wiki-view', 'wiki'),
659 'mod-wiki-comments'=>get_string('page-mod-wiki-comments', 'wiki'),
660 'mod-wiki-history'=>get_string('page-mod-wiki-history', 'wiki'),
661 'mod-wiki-map'=>get_string('page-mod-wiki-map', 'wiki')
663 return $module_pagetype;