MDL-30431 behat: Fixed wiki behat
[moodle.git] / mod / wiki / lib.php
blob8db22c8f794a10295f13797401682b2587038578
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');
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 * Indicates API features that the wiki supports.
200 * @uses FEATURE_GROUPS
201 * @uses FEATURE_GROUPINGS
202 * @uses FEATURE_GROUPMEMBERSONLY
203 * @uses FEATURE_MOD_INTRO
204 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
205 * @uses FEATURE_COMPLETION_HAS_RULES
206 * @uses FEATURE_GRADE_HAS_GRADE
207 * @uses FEATURE_GRADE_OUTCOMES
208 * @param string $feature
209 * @return mixed True if yes (some features may use other values)
211 function wiki_supports($feature) {
212 switch ($feature) {
213 case FEATURE_GROUPS:
214 return true;
215 case FEATURE_GROUPINGS:
216 return true;
217 case FEATURE_GROUPMEMBERSONLY:
218 return true;
219 case FEATURE_MOD_INTRO:
220 return true;
221 case FEATURE_COMPLETION_TRACKS_VIEWS:
222 return true;
223 case FEATURE_GRADE_HAS_GRADE:
224 return false;
225 case FEATURE_GRADE_OUTCOMES:
226 return false;
227 case FEATURE_RATE:
228 return false;
229 case FEATURE_BACKUP_MOODLE2:
230 return true;
231 case FEATURE_SHOW_DESCRIPTION:
232 return true;
234 default:
235 return null;
240 * Given a course and a time, this module should find recent activity
241 * that has occurred in wiki activities and print it out.
242 * Return true if there was output, or false is there was none.
244 * @global $CFG
245 * @global $DB
246 * @uses CONTEXT_MODULE
247 * @uses VISIBLEGROUPS
248 * @param object $course
249 * @param bool $viewfullnames capability
250 * @param int $timestart
251 * @return boolean
253 function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
254 global $CFG, $DB, $OUTPUT;
256 $sql = "SELECT p.id, p.timemodified, p.subwikiid, sw.wikiid, w.wikimode, sw.userid, sw.groupid
257 FROM {wiki_pages} p
258 JOIN {wiki_subwikis} sw ON sw.id = p.subwikiid
259 JOIN {wiki} w ON w.id = sw.wikiid
260 WHERE p.timemodified > ? AND w.course = ?
261 ORDER BY p.timemodified ASC";
262 if (!$pages = $DB->get_records_sql($sql, array($timestart, $course->id))) {
263 return false;
265 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
267 $wikis = array();
269 $modinfo = get_fast_modinfo($course);
271 $subwikivisible = array();
272 foreach ($pages as $page) {
273 if (!isset($subwikivisible[$page->subwikiid])) {
274 $subwiki = (object)array('id' => $page->subwikiid, 'wikiid' => $page->wikiid,
275 'groupid' => $page->groupid, 'userid' => $page->userid);
276 $wiki = (object)array('id' => $page->wikiid, 'course' => $course->id, 'wikimode' => $page->wikimode);
277 $subwikivisible[$page->subwikiid] = wiki_user_can_view($subwiki, $wiki);
279 if ($subwikivisible[$page->subwikiid]) {
280 $wikis[] = $page;
283 unset($subwikivisible);
284 unset($pages);
286 if (!$wikis) {
287 return false;
289 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 3);
290 foreach ($wikis as $wiki) {
291 $cm = $modinfo->instances['wiki'][$wiki->wikiid];
292 $link = $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $wiki->id;
293 print_recent_activity_note($wiki->timemodified, $wiki, $cm->name, $link, false, $viewfullnames);
296 return true; // True if anything was printed, otherwise false
299 * Function to be run periodically according to the moodle cron
300 * This function searches for things that need to be done, such
301 * as sending out mail, toggling flags etc ...
303 * @uses $CFG
304 * @return boolean
305 * @todo Finish documenting this function
307 function wiki_cron() {
308 global $CFG;
310 return true;
314 * Must return an array of grades for a given instance of this module,
315 * indexed by user. It also returns a maximum allowed grade.
317 * Example:
318 * $return->grades = array of grades;
319 * $return->maxgrade = maximum allowed grade;
321 * return $return;
323 * @param int $wikiid ID of an instance of this module
324 * @return mixed Null or object with an array of grades and with the maximum grade
326 function wiki_grades($wikiid) {
327 return null;
331 * This function returns if a scale is being used by one wiki
332 * it it has support for grading and scales. Commented code should be
333 * modified if necessary. See forum, glossary or journal modules
334 * as reference.
336 * @param int $wikiid ID of an instance of this module
337 * @return mixed
338 * @todo Finish documenting this function
340 function wiki_scale_used($wikiid, $scaleid) {
341 $return = false;
343 //$rec = get_record("wiki","id","$wikiid","scale","-$scaleid");
345 //if (!empty($rec) && !empty($scaleid)) {
346 // $return = true;
349 return $return;
353 * Checks if scale is being used by any instance of wiki.
354 * This function was added in 1.9
356 * This is used to find out if scale used anywhere
357 * @param $scaleid int
358 * @return boolean True if the scale is used by any wiki
360 function wiki_scale_used_anywhere($scaleid) {
361 global $DB;
363 //if ($scaleid and $DB->record_exists('wiki', array('grade' => -$scaleid))) {
364 // return true;
365 //} else {
366 // return false;
369 return false;
373 * file serving callback
375 * @copyright Josep Arus
376 * @package mod_wiki
377 * @category files
378 * @param stdClass $course course object
379 * @param stdClass $cm course module object
380 * @param stdClass $context context object
381 * @param string $filearea file area
382 * @param array $args extra arguments
383 * @param bool $forcedownload whether or not force download
384 * @param array $options additional options affecting the file serving
385 * @return bool false if the file was not found, just send the file otherwise and do not return anything
387 function wiki_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
388 global $CFG;
390 if ($context->contextlevel != CONTEXT_MODULE) {
391 return false;
394 require_login($course, true, $cm);
396 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
398 if ($filearea == 'attachments') {
399 $swid = (int) array_shift($args);
401 if (!$subwiki = wiki_get_subwiki($swid)) {
402 return false;
405 require_capability('mod/wiki:viewpage', $context);
407 $relativepath = implode('/', $args);
409 $fullpath = "/$context->id/mod_wiki/attachments/$swid/$relativepath";
411 $fs = get_file_storage();
412 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
413 return false;
416 send_stored_file($file, null, 0, $options);
420 function wiki_search_form($cm, $search = '', $subwiki = null) {
421 global $CFG, $OUTPUT;
423 $output = '<div class="wikisearch">';
424 $output .= '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/search.php" style="display:inline">';
425 $output .= '<fieldset class="invisiblefieldset">';
426 $output .= '<legend class="accesshide">'. get_string('searchwikis', 'wiki') .'</legend>';
427 $output .= '<label class="accesshide" for="searchwiki">' . get_string("searchterms", "wiki") . '</label>';
428 $output .= '<input id="searchwiki" name="searchstring" type="text" size="18" value="' . s($search, true) . '" alt="search" />';
429 $output .= '<input name="courseid" type="hidden" value="' . $cm->course . '" />';
430 $output .= '<input name="cmid" type="hidden" value="' . $cm->id . '" />';
431 if (!empty($subwiki->id)) {
432 $output .= '<input name="subwikiid" type="hidden" value="' . $subwiki->id . '" />';
434 $output .= '<input name="searchwikicontent" type="hidden" value="1" />';
435 $output .= '<input value="' . get_string('searchwikis', 'wiki') . '" type="submit" />';
436 $output .= '</fieldset>';
437 $output .= '</form>';
438 $output .= '</div>';
440 return $output;
442 function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm) {
443 global $CFG, $PAGE, $USER;
445 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
447 $context = context_module::instance($cm->id);
448 $url = $PAGE->url;
449 $userid = 0;
450 if ($module->wikimode == 'individual') {
451 $userid = $USER->id;
454 if (!$wiki = wiki_get_wiki($cm->instance)) {
455 return false;
458 if (!$gid = groups_get_activity_group($cm)) {
459 $gid = 0;
461 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)) {
462 return null;
463 } else {
464 $swid = $subwiki->id;
467 $pageid = $url->param('pageid');
468 $cmid = $url->param('id');
469 if (empty($pageid) && !empty($cmid)) {
470 // wiki main page
471 $page = wiki_get_page_by_title($swid, $wiki->firstpagetitle);
472 $pageid = $page->id;
475 if (has_capability('mod/wiki:createpage', $context)) {
476 $link = new moodle_url('/mod/wiki/create.php', array('action' => 'new', 'swid' => $swid));
477 $node = $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING);
480 if (is_numeric($pageid)) {
482 if (has_capability('mod/wiki:viewpage', $context)) {
483 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $pageid));
484 $node = $navref->add(get_string('view', 'wiki'), $link, navigation_node::TYPE_SETTING);
487 if (wiki_user_can_edit($subwiki)) {
488 $link = new moodle_url('/mod/wiki/edit.php', array('pageid' => $pageid));
489 $node = $navref->add(get_string('edit', 'wiki'), $link, navigation_node::TYPE_SETTING);
492 if (has_capability('mod/wiki:viewcomment', $context)) {
493 $link = new moodle_url('/mod/wiki/comments.php', array('pageid' => $pageid));
494 $node = $navref->add(get_string('comments', 'wiki'), $link, navigation_node::TYPE_SETTING);
497 if (has_capability('mod/wiki:viewpage', $context)) {
498 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
499 $node = $navref->add(get_string('history', 'wiki'), $link, navigation_node::TYPE_SETTING);
502 if (has_capability('mod/wiki:viewpage', $context)) {
503 $link = new moodle_url('/mod/wiki/map.php', array('pageid' => $pageid));
504 $node = $navref->add(get_string('map', 'wiki'), $link, navigation_node::TYPE_SETTING);
507 if (has_capability('mod/wiki:viewpage', $context)) {
508 $link = new moodle_url('/mod/wiki/files.php', array('pageid' => $pageid));
509 $node = $navref->add(get_string('files', 'wiki'), $link, navigation_node::TYPE_SETTING);
512 if (has_capability('mod/wiki:managewiki', $context)) {
513 $link = new moodle_url('/mod/wiki/admin.php', array('pageid' => $pageid));
514 $node = $navref->add(get_string('admin', 'wiki'), $link, navigation_node::TYPE_SETTING);
519 * Returns all other caps used in wiki module
521 * @return array
523 function wiki_get_extra_capabilities() {
524 return array('moodle/comment:view', 'moodle/comment:post', 'moodle/comment:delete');
528 * Running addtional permission check on plugin, for example, plugins
529 * may have switch to turn on/off comments option, this callback will
530 * affect UI display, not like pluginname_comment_validate only throw
531 * exceptions.
532 * Capability check has been done in comment->check_permissions(), we
533 * don't need to do it again here.
535 * @package mod_wiki
536 * @category comment
538 * @param stdClass $comment_param {
539 * context => context the context object
540 * courseid => int course id
541 * cm => stdClass course module object
542 * commentarea => string comment area
543 * itemid => int itemid
545 * @return array
547 function wiki_comment_permissions($comment_param) {
548 return array('post'=>true, 'view'=>true);
552 * Validate comment parameter before perform other comments actions
554 * @param stdClass $comment_param {
555 * context => context the context object
556 * courseid => int course id
557 * cm => stdClass course module object
558 * commentarea => string comment area
559 * itemid => int itemid
562 * @package mod_wiki
563 * @category comment
565 * @return boolean
567 function wiki_comment_validate($comment_param) {
568 global $DB, $CFG;
569 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
570 // validate comment area
571 if ($comment_param->commentarea != 'wiki_page') {
572 throw new comment_exception('invalidcommentarea');
574 // validate itemid
575 if (!$record = $DB->get_record('wiki_pages', array('id'=>$comment_param->itemid))) {
576 throw new comment_exception('invalidcommentitemid');
578 if (!$subwiki = wiki_get_subwiki($record->subwikiid)) {
579 throw new comment_exception('invalidsubwikiid');
581 if (!$wiki = wiki_get_wiki_from_pageid($comment_param->itemid)) {
582 throw new comment_exception('invalidid', 'data');
584 if (!$course = $DB->get_record('course', array('id'=>$wiki->course))) {
585 throw new comment_exception('coursemisconf');
587 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $course->id)) {
588 throw new comment_exception('invalidcoursemodule');
590 $context = context_module::instance($cm->id);
591 // group access
592 if ($subwiki->groupid) {
593 $groupmode = groups_get_activity_groupmode($cm, $course);
594 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
595 if (!groups_is_member($subwiki->groupid)) {
596 throw new comment_exception('notmemberofgroup');
600 // validate context id
601 if ($context->id != $comment_param->context->id) {
602 throw new comment_exception('invalidcontext');
604 // validation for comment deletion
605 if (!empty($comment_param->commentid)) {
606 if ($comment = $DB->get_record('comments', array('id'=>$comment_param->commentid))) {
607 if ($comment->commentarea != 'wiki_page') {
608 throw new comment_exception('invalidcommentarea');
610 if ($comment->contextid != $context->id) {
611 throw new comment_exception('invalidcontext');
613 if ($comment->itemid != $comment_param->itemid) {
614 throw new comment_exception('invalidcommentitemid');
616 } else {
617 throw new comment_exception('invalidcommentid');
620 return true;
624 * Return a list of page types
625 * @param string $pagetype current page type
626 * @param stdClass $parentcontext Block's parent context
627 * @param stdClass $currentcontext Current context of block
629 function wiki_page_type_list($pagetype, $parentcontext, $currentcontext) {
630 $module_pagetype = array(
631 'mod-wiki-*'=>get_string('page-mod-wiki-x', 'wiki'),
632 'mod-wiki-view'=>get_string('page-mod-wiki-view', 'wiki'),
633 'mod-wiki-comments'=>get_string('page-mod-wiki-comments', 'wiki'),
634 'mod-wiki-history'=>get_string('page-mod-wiki-history', 'wiki'),
635 'mod-wiki-map'=>get_string('page-mod-wiki-map', 'wiki')
637 return $module_pagetype;