MDL-43987 core: Remove port numbers in cleanremoteaddr
[moodle.git] / mod / wiki / lib.php
blob31298ab5acd02bf28e5df35e2244dd09c8bb815c
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_MOD_INTRO
203 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
204 * @uses FEATURE_COMPLETION_HAS_RULES
205 * @uses FEATURE_GRADE_HAS_GRADE
206 * @uses FEATURE_GRADE_OUTCOMES
207 * @param string $feature
208 * @return mixed True if yes (some features may use other values)
210 function wiki_supports($feature) {
211 switch ($feature) {
212 case FEATURE_GROUPS:
213 return true;
214 case FEATURE_GROUPINGS:
215 return true;
216 case FEATURE_MOD_INTRO:
217 return true;
218 case FEATURE_COMPLETION_TRACKS_VIEWS:
219 return true;
220 case FEATURE_GRADE_HAS_GRADE:
221 return false;
222 case FEATURE_GRADE_OUTCOMES:
223 return false;
224 case FEATURE_RATE:
225 return false;
226 case FEATURE_BACKUP_MOODLE2:
227 return true;
228 case FEATURE_SHOW_DESCRIPTION:
229 return true;
231 default:
232 return null;
237 * Given a course and a time, this module should find recent activity
238 * that has occurred in wiki activities and print it out.
239 * Return true if there was output, or false is there was none.
241 * @global $CFG
242 * @global $DB
243 * @uses CONTEXT_MODULE
244 * @uses VISIBLEGROUPS
245 * @param object $course
246 * @param bool $viewfullnames capability
247 * @param int $timestart
248 * @return boolean
250 function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
251 global $CFG, $DB, $OUTPUT;
253 $sql = "SELECT p.id, p.timemodified, p.subwikiid, sw.wikiid, w.wikimode, sw.userid, sw.groupid
254 FROM {wiki_pages} p
255 JOIN {wiki_subwikis} sw ON sw.id = p.subwikiid
256 JOIN {wiki} w ON w.id = sw.wikiid
257 WHERE p.timemodified > ? AND w.course = ?
258 ORDER BY p.timemodified ASC";
259 if (!$pages = $DB->get_records_sql($sql, array($timestart, $course->id))) {
260 return false;
262 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
264 $wikis = array();
266 $modinfo = get_fast_modinfo($course);
268 $subwikivisible = array();
269 foreach ($pages as $page) {
270 if (!isset($subwikivisible[$page->subwikiid])) {
271 $subwiki = (object)array('id' => $page->subwikiid, 'wikiid' => $page->wikiid,
272 'groupid' => $page->groupid, 'userid' => $page->userid);
273 $wiki = (object)array('id' => $page->wikiid, 'course' => $course->id, 'wikimode' => $page->wikimode);
274 $subwikivisible[$page->subwikiid] = wiki_user_can_view($subwiki, $wiki);
276 if ($subwikivisible[$page->subwikiid]) {
277 $wikis[] = $page;
280 unset($subwikivisible);
281 unset($pages);
283 if (!$wikis) {
284 return false;
286 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 3);
287 foreach ($wikis as $wiki) {
288 $cm = $modinfo->instances['wiki'][$wiki->wikiid];
289 $link = $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $wiki->id;
290 print_recent_activity_note($wiki->timemodified, $wiki, $cm->name, $link, false, $viewfullnames);
293 return true; // True if anything was printed, otherwise false
296 * Function to be run periodically according to the moodle cron
297 * This function searches for things that need to be done, such
298 * as sending out mail, toggling flags etc ...
300 * @uses $CFG
301 * @return boolean
302 * @todo Finish documenting this function
304 function wiki_cron() {
305 global $CFG;
307 return true;
311 * Must return an array of grades for a given instance of this module,
312 * indexed by user. It also returns a maximum allowed grade.
314 * Example:
315 * $return->grades = array of grades;
316 * $return->maxgrade = maximum allowed grade;
318 * return $return;
320 * @param int $wikiid ID of an instance of this module
321 * @return mixed Null or object with an array of grades and with the maximum grade
323 function wiki_grades($wikiid) {
324 return null;
328 * This function returns if a scale is being used by one wiki
329 * it it has support for grading and scales. Commented code should be
330 * modified if necessary. See forum, glossary or journal modules
331 * as reference.
333 * @param int $wikiid ID of an instance of this module
334 * @return mixed
335 * @todo Finish documenting this function
337 function wiki_scale_used($wikiid, $scaleid) {
338 $return = false;
340 //$rec = get_record("wiki","id","$wikiid","scale","-$scaleid");
342 //if (!empty($rec) && !empty($scaleid)) {
343 // $return = true;
346 return $return;
350 * Checks if scale is being used by any instance of wiki.
351 * This function was added in 1.9
353 * This is used to find out if scale used anywhere
354 * @param $scaleid int
355 * @return boolean True if the scale is used by any wiki
357 function wiki_scale_used_anywhere($scaleid) {
358 global $DB;
360 //if ($scaleid and $DB->record_exists('wiki', array('grade' => -$scaleid))) {
361 // return true;
362 //} else {
363 // return false;
366 return false;
370 * file serving callback
372 * @copyright Josep Arus
373 * @package mod_wiki
374 * @category files
375 * @param stdClass $course course object
376 * @param stdClass $cm course module object
377 * @param stdClass $context context object
378 * @param string $filearea file area
379 * @param array $args extra arguments
380 * @param bool $forcedownload whether or not force download
381 * @param array $options additional options affecting the file serving
382 * @return bool false if the file was not found, just send the file otherwise and do not return anything
384 function wiki_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
385 global $CFG;
387 if ($context->contextlevel != CONTEXT_MODULE) {
388 return false;
391 require_login($course, true, $cm);
393 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
395 if ($filearea == 'attachments') {
396 $swid = (int) array_shift($args);
398 if (!$subwiki = wiki_get_subwiki($swid)) {
399 return false;
402 require_capability('mod/wiki:viewpage', $context);
404 $relativepath = implode('/', $args);
406 $fullpath = "/$context->id/mod_wiki/attachments/$swid/$relativepath";
408 $fs = get_file_storage();
409 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
410 return false;
413 send_stored_file($file, null, 0, $options);
417 function wiki_search_form($cm, $search = '', $subwiki = null) {
418 global $CFG, $OUTPUT;
420 $output = '<div class="wikisearch">';
421 $output .= '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/search.php" style="display:inline">';
422 $output .= '<fieldset class="invisiblefieldset">';
423 $output .= '<legend class="accesshide">'. get_string('searchwikis', 'wiki') .'</legend>';
424 $output .= '<label class="accesshide" for="searchwiki">' . get_string("searchterms", "wiki") . '</label>';
425 $output .= '<input id="searchwiki" name="searchstring" type="text" size="18" value="' . s($search, true) . '" alt="search" />';
426 $output .= '<input name="courseid" type="hidden" value="' . $cm->course . '" />';
427 $output .= '<input name="cmid" type="hidden" value="' . $cm->id . '" />';
428 if (!empty($subwiki->id)) {
429 $output .= '<input name="subwikiid" type="hidden" value="' . $subwiki->id . '" />';
431 $output .= '<input name="searchwikicontent" type="hidden" value="1" />';
432 $output .= '<input value="' . get_string('searchwikis', 'wiki') . '" type="submit" />';
433 $output .= '</fieldset>';
434 $output .= '</form>';
435 $output .= '</div>';
437 return $output;
439 function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm) {
440 global $CFG, $PAGE, $USER;
442 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
444 $context = context_module::instance($cm->id);
445 $url = $PAGE->url;
446 $userid = 0;
447 if ($module->wikimode == 'individual') {
448 $userid = $USER->id;
451 if (!$wiki = wiki_get_wiki($cm->instance)) {
452 return false;
455 if (!$gid = groups_get_activity_group($cm)) {
456 $gid = 0;
458 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)) {
459 return null;
460 } else {
461 $swid = $subwiki->id;
464 $pageid = $url->param('pageid');
465 $cmid = $url->param('id');
466 if (empty($pageid) && !empty($cmid)) {
467 // wiki main page
468 $page = wiki_get_page_by_title($swid, $wiki->firstpagetitle);
469 $pageid = $page->id;
472 if (has_capability('mod/wiki:createpage', $context)) {
473 $link = new moodle_url('/mod/wiki/create.php', array('action' => 'new', 'swid' => $swid));
474 $node = $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING);
477 if (is_numeric($pageid)) {
479 if (has_capability('mod/wiki:viewpage', $context)) {
480 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $pageid));
481 $node = $navref->add(get_string('view', 'wiki'), $link, navigation_node::TYPE_SETTING);
484 if (wiki_user_can_edit($subwiki)) {
485 $link = new moodle_url('/mod/wiki/edit.php', array('pageid' => $pageid));
486 $node = $navref->add(get_string('edit', 'wiki'), $link, navigation_node::TYPE_SETTING);
489 if (has_capability('mod/wiki:viewcomment', $context)) {
490 $link = new moodle_url('/mod/wiki/comments.php', array('pageid' => $pageid));
491 $node = $navref->add(get_string('comments', 'wiki'), $link, navigation_node::TYPE_SETTING);
494 if (has_capability('mod/wiki:viewpage', $context)) {
495 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
496 $node = $navref->add(get_string('history', 'wiki'), $link, navigation_node::TYPE_SETTING);
499 if (has_capability('mod/wiki:viewpage', $context)) {
500 $link = new moodle_url('/mod/wiki/map.php', array('pageid' => $pageid));
501 $node = $navref->add(get_string('map', 'wiki'), $link, navigation_node::TYPE_SETTING);
504 if (has_capability('mod/wiki:viewpage', $context)) {
505 $link = new moodle_url('/mod/wiki/files.php', array('pageid' => $pageid));
506 $node = $navref->add(get_string('files', 'wiki'), $link, navigation_node::TYPE_SETTING);
509 if (has_capability('mod/wiki:managewiki', $context)) {
510 $link = new moodle_url('/mod/wiki/admin.php', array('pageid' => $pageid));
511 $node = $navref->add(get_string('admin', 'wiki'), $link, navigation_node::TYPE_SETTING);
516 * Returns all other caps used in wiki module
518 * @return array
520 function wiki_get_extra_capabilities() {
521 return array('moodle/comment:view', 'moodle/comment:post', 'moodle/comment:delete');
525 * Running addtional permission check on plugin, for example, plugins
526 * may have switch to turn on/off comments option, this callback will
527 * affect UI display, not like pluginname_comment_validate only throw
528 * exceptions.
529 * Capability check has been done in comment->check_permissions(), we
530 * don't need to do it again here.
532 * @package mod_wiki
533 * @category comment
535 * @param stdClass $comment_param {
536 * context => context the context object
537 * courseid => int course id
538 * cm => stdClass course module object
539 * commentarea => string comment area
540 * itemid => int itemid
542 * @return array
544 function wiki_comment_permissions($comment_param) {
545 return array('post'=>true, 'view'=>true);
549 * Validate comment parameter before perform other comments actions
551 * @param stdClass $comment_param {
552 * context => context the context object
553 * courseid => int course id
554 * cm => stdClass course module object
555 * commentarea => string comment area
556 * itemid => int itemid
559 * @package mod_wiki
560 * @category comment
562 * @return boolean
564 function wiki_comment_validate($comment_param) {
565 global $DB, $CFG;
566 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
567 // validate comment area
568 if ($comment_param->commentarea != 'wiki_page') {
569 throw new comment_exception('invalidcommentarea');
571 // validate itemid
572 if (!$record = $DB->get_record('wiki_pages', array('id'=>$comment_param->itemid))) {
573 throw new comment_exception('invalidcommentitemid');
575 if (!$subwiki = wiki_get_subwiki($record->subwikiid)) {
576 throw new comment_exception('invalidsubwikiid');
578 if (!$wiki = wiki_get_wiki_from_pageid($comment_param->itemid)) {
579 throw new comment_exception('invalidid', 'data');
581 if (!$course = $DB->get_record('course', array('id'=>$wiki->course))) {
582 throw new comment_exception('coursemisconf');
584 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $course->id)) {
585 throw new comment_exception('invalidcoursemodule');
587 $context = context_module::instance($cm->id);
588 // group access
589 if ($subwiki->groupid) {
590 $groupmode = groups_get_activity_groupmode($cm, $course);
591 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
592 if (!groups_is_member($subwiki->groupid)) {
593 throw new comment_exception('notmemberofgroup');
597 // validate context id
598 if ($context->id != $comment_param->context->id) {
599 throw new comment_exception('invalidcontext');
601 // validation for comment deletion
602 if (!empty($comment_param->commentid)) {
603 if ($comment = $DB->get_record('comments', array('id'=>$comment_param->commentid))) {
604 if ($comment->commentarea != 'wiki_page') {
605 throw new comment_exception('invalidcommentarea');
607 if ($comment->contextid != $context->id) {
608 throw new comment_exception('invalidcontext');
610 if ($comment->itemid != $comment_param->itemid) {
611 throw new comment_exception('invalidcommentitemid');
613 } else {
614 throw new comment_exception('invalidcommentid');
617 return true;
621 * Return a list of page types
622 * @param string $pagetype current page type
623 * @param stdClass $parentcontext Block's parent context
624 * @param stdClass $currentcontext Current context of block
626 function wiki_page_type_list($pagetype, $parentcontext, $currentcontext) {
627 $module_pagetype = array(
628 'mod-wiki-*'=>get_string('page-mod-wiki-x', 'wiki'),
629 'mod-wiki-view'=>get_string('page-mod-wiki-view', 'wiki'),
630 'mod-wiki-comments'=>get_string('page-mod-wiki-comments', 'wiki'),
631 'mod-wiki-history'=>get_string('page-mod-wiki-history', 'wiki'),
632 'mod-wiki-map'=>get_string('page-mod-wiki-map', 'wiki')
634 return $module_pagetype;