MDL-21695 adding help string
[moodle.git] / mod / wiki / lib.php
blobd2cacf1fc864f15cc12b7453120d55d2f13a4fcc
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 require_once ($CFG->dirroot . '/mod/wiki/locallib.php');
39 /**
40 * Given an object containing all the necessary data,
41 * (defined by the form in mod.html) this function
42 * will create a new instance and return the id number
43 * of the new instance.
45 * @param object $instance An object from the form in mod.html
46 * @return int The id of the newly inserted wiki record
47 **/
48 function wiki_add_instance($wiki) {
49 global $DB;
51 $wiki->timemodified = time();
52 # May have to add extra stuff in here #
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;
145 * Return a small object with summary information about what a
146 * user has done with a given particular instance of this module
147 * Used for user activity reports.
148 * $return->time = the time they did it
149 * $return->info = a short text description
151 * @return null
152 * @todo Finish documenting this function
154 function wiki_user_outline($course, $user, $mod, $wiki) {
155 return $return;
159 * Print a detailed representation of what a user has done with
160 * a given particular instance of this module, for user activity reports.
162 * @return boolean
163 * @todo Finish documenting this function
165 function wiki_user_complete($course, $user, $mod, $wiki) {
166 return true;
170 * Indicates API features that the wiki supports.
172 * @uses FEATURE_GROUPS
173 * @uses FEATURE_GROUPINGS
174 * @uses FEATURE_GROUPMEMBERSONLY
175 * @uses FEATURE_MOD_INTRO
176 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
177 * @uses FEATURE_COMPLETION_HAS_RULES
178 * @uses FEATURE_GRADE_HAS_GRADE
179 * @uses FEATURE_GRADE_OUTCOMES
180 * @param string $feature
181 * @return mixed True if yes (some features may use other values)
183 function wiki_supports($feature) {
184 switch ($feature) {
185 case FEATURE_GROUPS:
186 return true;
187 case FEATURE_GROUPINGS:
188 return true;
189 case FEATURE_GROUPMEMBERSONLY:
190 return true;
191 case FEATURE_MOD_INTRO:
192 return true;
193 case FEATURE_COMPLETION_TRACKS_VIEWS:
194 return true;
195 case FEATURE_COMPLETION_HAS_RULES:
196 return true;
197 case FEATURE_GRADE_HAS_GRADE:
198 return true;
199 case FEATURE_GRADE_OUTCOMES:
200 return true;
201 case FEATURE_RATE:
202 return false;
203 case FEATURE_BACKUP_MOODLE2:
204 return true;
206 default:
207 return null;
212 * Given a course and a time, this module should find recent activity
213 * that has occurred in wiki activities and print it out.
214 * Return true if there was output, or false is there was none.
216 * @global $CFG
217 * @global $DB
218 * @uses CONTEXT_MODULE
219 * @uses VISIBLEGROUPS
220 * @param object $course
221 * @param bool $viewfullnames capability
222 * @param int $timestart
223 * @return boolean
225 function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
226 global $CFG, $DB, $OUTPUT;
228 if (!$pages = $DB->get_records_sql("SELECT p.*, w.id as wikiid, sw.groupid
229 FROM {wiki_pages} p
230 JOIN {wiki_subwikis} sw ON sw.id = p.subwikiid
231 JOIN {wiki} w ON w.id = sw.wikiid
232 WHERE p.timemodified > ? AND w.course = ?
233 ORDER BY p.timemodified ASC", array($timestart, $course->id))) {
234 return false;
236 $modinfo =& get_fast_modinfo($course);
238 $wikis = array();
240 $modinfo = get_fast_modinfo($course);
242 foreach ($pages as $page) {
243 if (!isset($modinfo->instances['wiki'][$page->wikiid])) {
244 // not visible
245 continue;
247 $cm = $modinfo->instances['wiki'][$page->wikiid];
248 if (!$cm->uservisible) {
249 continue;
251 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
253 if (!has_capability('mod/wiki:viewpage', $context)) {
254 continue;
257 $groupmode = groups_get_activity_groupmode($cm, $course);
259 if ($groupmode) {
260 if ($groupmode == SEPARATEGROUPS and !has_capability('mod/wiki:managewiki', $context)) {
261 // separate mode
262 if (isguestuser()) {
263 // shortcut
264 continue;
267 if (is_null($modinfo->groups)) {
268 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
271 if (!in_array($page->groupid, $modinfo->groups[0])) {
272 continue;
276 $wikis[] = $page;
278 unset($pages);
280 if (!$wikis) {
281 return false;
283 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 3);
284 foreach ($wikis as $wiki) {
285 $cm = $modinfo->instances['wiki'][$wiki->wikiid];
286 $link = $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $wiki->id;
287 print_recent_activity_note($wiki->timemodified, $wiki, $cm->name, $link, false, $viewfullnames);
290 return true; // True if anything was printed, otherwise false
293 * Function to be run periodically according to the moodle cron
294 * This function searches for things that need to be done, such
295 * as sending out mail, toggling flags etc ...
297 * @uses $CFG
298 * @return boolean
299 * @todo Finish documenting this function
301 function wiki_cron() {
302 global $CFG;
304 return true;
308 * Must return an array of grades for a given instance of this module,
309 * indexed by user. It also returns a maximum allowed grade.
311 * Example:
312 * $return->grades = array of grades;
313 * $return->maxgrade = maximum allowed grade;
315 * return $return;
317 * @param int $wikiid ID of an instance of this module
318 * @return mixed Null or object with an array of grades and with the maximum grade
320 function wiki_grades($wikiid) {
321 return null;
325 * Must return an array of user records (all data) who are participants
326 * for a given instance of wiki. Must include every user involved
327 * in the instance, independient of his role (student, teacher, admin...)
328 * See other modules as example.
330 * @param int $wikiid ID of an instance of this module
331 * @return mixed boolean/array of students
333 function wiki_get_participants($wikiid) {
334 return false;
338 * This function returns if a scale is being used by one wiki
339 * it it has support for grading and scales. Commented code should be
340 * modified if necessary. See forum, glossary or journal modules
341 * as reference.
343 * @param int $wikiid ID of an instance of this module
344 * @return mixed
345 * @todo Finish documenting this function
347 function wiki_scale_used($wikiid, $scaleid) {
348 $return = false;
350 //$rec = get_record("wiki","id","$wikiid","scale","-$scaleid");
352 //if (!empty($rec) && !empty($scaleid)) {
353 // $return = true;
356 return $return;
360 * Checks if scale is being used by any instance of wiki.
361 * This function was added in 1.9
363 * This is used to find out if scale used anywhere
364 * @param $scaleid int
365 * @return boolean True if the scale is used by any wiki
367 function wiki_scale_used_anywhere($scaleid) {
368 global $DB;
370 if ($scaleid and $DB->record_exists('wiki', array('grade', $scaleid))) {
371 return true;
372 } else {
373 return false;
378 * Pluginfile hook
380 * @author Josep Arus
382 function wiki_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedownload) {
383 global $CFG;
385 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
387 if ($filearea == 'wiki_attachments') {
388 $swid = (int) array_shift($args);
390 if (!$subwiki = wiki_get_subwiki($swid)) {
391 return false;
394 require_course_login($course->id, true, $cm);
396 require_capability('mod/wiki:viewpage', $context);
398 $relativepath = '/' . implode('/', $args);
400 $fullpath = $context->id . 'wiki_attachments' . $swid . $relativepath;
402 $fs = get_file_storage();
403 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
404 return false;
407 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
409 send_stored_file($file, $lifetime, 0);
412 function wiki_search_form($cm, $search='') {
413 global $CFG, $OUTPUT;
415 $output = '<div class="wikisearch">';
416 $output .= '<form method="post" action="'.$CFG->wwwroot.'/mod/wiki/search.php" style="display:inline">';
417 $output .= '<fieldset class="invisiblefieldset">';
418 $output .= '<input name="searchstring" type="text" size="18" value="'.s($search, true).'" alt="search" />';
419 $output .= '<input name="courseid" type="hidden" value="'.$cm->course.'" />';
420 $output .= '<input name="cmid" type="hidden" value="'.$cm->id.'" />';
421 $output .= '<input name="searchwikicontent" type="hidden" value="1" />';
422 $output .= ' <input value="'.get_string('searchwikis', 'wiki').'" type="submit" />';
423 $output .= '</fieldset>';
424 $output .= '</form>';
425 $output .= '</div>';
427 return $output;
429 function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm) {
430 global $PAGE, $USER;
431 $url = $PAGE->url;
432 $userid = 0;
433 if ($module->wikimode == 'individual') {
434 $userid = $USER->id;
437 if(!$wiki = wiki_get_wiki($cm->instance)) {
438 return false;
441 if (!$gid = groups_get_activity_group($cm)){
442 $gid = 0;
444 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)){
445 return null;
446 } else {
447 $swid = $subwiki->id;
450 $pageid = $url->param('pageid');
451 $cmid = $url->param('id');
452 if (empty($pageid) && !empty($cmid)) {
453 // wiki main page
454 $page = wiki_get_page_by_title($swid, $wiki->firstpagetitle);
455 $pageid = $page->id;
457 $link = new moodle_url('/mod/wiki/create.php', array('action'=>'new', 'swid'=>$swid));
458 $node = $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING);
460 if (is_numeric($pageid)) {
462 $link = new moodle_url('/mod/wiki/view.php', array('pageid'=>$pageid));
463 $node = $navref->add(get_string('view', 'wiki'), $link, navigation_node::TYPE_SETTING);
465 $link = new moodle_url('/mod/wiki/edit.php', array('pageid'=>$pageid));
466 $node = $navref->add(get_string('edit', 'wiki'), $link, navigation_node::TYPE_SETTING);
468 $link = new moodle_url('/mod/wiki/comments.php', array('pageid'=>$pageid));
469 $node = $navref->add(get_string('comments', 'wiki'), $link, navigation_node::TYPE_SETTING);
471 $link = new moodle_url('/mod/wiki/history.php', array('pageid'=>$pageid));
472 $node = $navref->add(get_string('history', 'wiki'), $link, navigation_node::TYPE_SETTING);
474 $link = new moodle_url('/mod/wiki/map.php', array('pageid'=>$pageid));
475 $node = $navref->add(get_string('map', 'wiki'), $link, navigation_node::TYPE_SETTING);
478 //if ($page = wiki_get_first_page($swid, $module)) {
479 //$node = $navref->add(get_string('pageindex', 'wiki'));
480 //$link = new moodle_url('/mod/wiki/view.php', array('pageid'=>$page->id));
481 //$icon = new pix_icon('f/odt', '');
482 //$parent = $node->add($page->title, $link, null, null, "index_$page->id", $icon);
483 //$keys = array();
484 //wiki_build_tree($page, $parent, $keys);