navigation MDL-22749 Added course information to a users navigation when viewing...
[moodle.git] / mod / wiki / lib.php
CommitLineData
e5cc530b 1<?php
2
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
00710f4c 12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e5cc530b 13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
00710f4c 16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
39fcb981 17
e5cc530b 18/**
19 * Library of functions and constants for module wiki
14a960ac 20 *
00710f4c
DC
21 * It contains the great majority of functions defined by Moodle
22 * that are mandatory to develop a module.
23 *
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
27 *
28 * @author Jordi Piguillem
29 * @author Marc Alier
30 * @author David Jimenez
31 * @author Josep Arus
32 * @author Kenneth Riba
33 *
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
e5cc530b 35 */
ee3cc995 36
532d319d
JP
37
38
ee3cc995 39
e5cc530b 40/**
41 * Given an object containing all the necessary data,
00710f4c 42 * (defined by the form in mod.html) this function
e5cc530b 43 * will create a new instance and return the id number
44 * of the new instance.
45 *
00710f4c
DC
46 * @param object $instance An object from the form in mod.html
47 * @return int The id of the newly inserted wiki record
48 **/
39fcb981 49function wiki_add_instance($wiki) {
c18269c7 50 global $DB;
14a960ac 51
39fcb981 52 $wiki->timemodified = time();
39fcb981 53 # May have to add extra stuff in here #
54
00710f4c 55 return $DB->insert_record('wiki', $wiki);
39fcb981 56}
57
e5cc530b 58/**
59 * Given an object containing all the necessary data,
00710f4c 60 * (defined by the form in mod.html) this function
e5cc530b 61 * will update an existing instance with new data.
62 *
00710f4c
DC
63 * @param object $instance An object from the form in mod.html
64 * @return boolean Success/Fail
65 **/
39fcb981 66function wiki_update_instance($wiki) {
c18269c7 67 global $DB;
39fcb981 68
39fcb981 69 $wiki->timemodified = time();
70 $wiki->id = $wiki->instance;
00710f4c
DC
71 if (empty($wiki->forceformat)) {
72 $wiki->forceformat = 0;
39fcb981 73 }
00710f4c
DC
74
75 # May have to add extra stuff in here #
76
77 return $DB->update_record('wiki', $wiki);
39fcb981 78}
79
e5cc530b 80/**
81 * Given an ID of an instance of this module,
82 * this function will permanently delete the instance
83 * and any data that depends on it.
84 *
00710f4c
DC
85 * @param int $id Id of the module instance
86 * @return boolean Success/Failure
87 **/
39fcb981 88function wiki_delete_instance($id) {
00710f4c 89 global $DB;
8f0cd6ef 90
00710f4c 91 if (!$wiki = $DB->get_record('wiki', array('id' => $id))) {
39fcb981 92 return false;
93 }
94
95 $result = true;
96
00710f4c
DC
97 # Get subwiki information #
98 $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id));
8f0cd6ef 99
00710f4c
DC
100 foreach ($subwikis as $subwiki) {
101 # Get existing links, and delete them #
102 if (!$DB->delete_records('wiki_links', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
103 $result = false;
104 }
22e846cd 105
00710f4c
DC
106 # Get existing pages #
107 if ($pages = $DB->get_records('wiki_pages', array('subwikiid' => $subwiki->id))) {
108 foreach ($pages as $page) {
109 # Get locks, and delete them #
110 if (!$DB->delete_records('wiki_locks', array('pageid' => $page->id), IGNORE_MISSING)) {
111 $result = false;
112 }
39fcb981 113
00710f4c
DC
114 # Get versions, and delete them #
115 if (!$DB->delete_records('wiki_versions', array('pageid' => $page->id), IGNORE_MISSING)) {
116 $result = false;
117 }
39fcb981 118 }
00710f4c
DC
119
120 # Delete pages #
121 if (!$DB->delete_records('wiki_pages', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
39fcb981 122 $result = false;
123 }
124 }
00710f4c
DC
125
126 # Get existing synonyms, and delete them #
127 if (!$DB->delete_records('wiki_synonyms', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) {
128 $result = false;
129 }
130
131 # Delete any subwikis #
132 if (!$DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING)) {
133 $result = false;
134 }
135 }
136
137 # Delete any dependent records here #
138 if (!$DB->delete_records('wiki', array('id' => $wiki->id))) {
139 $result = false;
39fcb981 140 }
8f0cd6ef 141
39fcb981 142 return $result;
143}
144
e5cc530b 145/**
146 * Return a small object with summary information about what a
147 * user has done with a given particular instance of this module
148 * Used for user activity reports.
149 * $return->time = the time they did it
150 * $return->info = a short text description
151 *
e5cc530b 152 * @return null
00710f4c
DC
153 * @todo Finish documenting this function
154 **/
39fcb981 155function wiki_user_outline($course, $user, $mod, $wiki) {
e8930396 156 $return = NULL;
39fcb981 157 return $return;
158}
159
e5cc530b 160/**
00710f4c 161 * Print a detailed representation of what a user has done with
e5cc530b 162 * a given particular instance of this module, for user activity reports.
163 *
00710f4c
DC
164 * @return boolean
165 * @todo Finish documenting this function
166 **/
39fcb981 167function wiki_user_complete($course, $user, $mod, $wiki) {
39fcb981 168 return true;
169}
170
00710f4c
DC
171/**
172 * Indicates API features that the wiki supports.
173 *
174 * @uses FEATURE_GROUPS
175 * @uses FEATURE_GROUPINGS
176 * @uses FEATURE_GROUPMEMBERSONLY
177 * @uses FEATURE_MOD_INTRO
178 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
179 * @uses FEATURE_COMPLETION_HAS_RULES
180 * @uses FEATURE_GRADE_HAS_GRADE
181 * @uses FEATURE_GRADE_OUTCOMES
182 * @param string $feature
183 * @return mixed True if yes (some features may use other values)
184 */
185function wiki_supports($feature) {
186 switch ($feature) {
187 case FEATURE_GROUPS:
188 return true;
189 case FEATURE_GROUPINGS:
190 return true;
191 case FEATURE_GROUPMEMBERSONLY:
192 return true;
193 case FEATURE_MOD_INTRO:
194 return true;
195 case FEATURE_COMPLETION_TRACKS_VIEWS:
196 return true;
197 case FEATURE_COMPLETION_HAS_RULES:
198 return true;
199 case FEATURE_GRADE_HAS_GRADE:
200 return true;
201 case FEATURE_GRADE_OUTCOMES:
202 return true;
203 case FEATURE_RATE:
204 return false;
ebe93f74
JP
205 case FEATURE_BACKUP_MOODLE2:
206 return true;
00710f4c
DC
207
208 default:
209 return null;
210 }
211}
212
e5cc530b 213/**
214 * Given a course and a time, this module should find recent activity
215 * that has occurred in wiki activities and print it out.
216 * Return true if there was output, or false is there was none.
217 *
00710f4c
DC
218 * @global $CFG
219 * @global $DB
220 * @uses CONTEXT_MODULE
221 * @uses VISIBLEGROUPS
e5cc530b 222 * @param object $course
00710f4c 223 * @param bool $viewfullnames capability
e5cc530b 224 * @param int $timestart
00710f4c
DC
225 * @return boolean
226 **/
227function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
9ab0a4fa 228 global $CFG, $DB, $OUTPUT;
14a960ac 229
00710f4c 230 if (!$pages = $DB->get_records_sql("SELECT p.*, w.id as wikiid, sw.groupid
ebe93f74
JP
231 FROM {wiki_pages} p
232 JOIN {wiki_subwikis} sw ON sw.id = p.subwikiid
233 JOIN {wiki} w ON w.id = sw.wikiid
234 WHERE p.timemodified > ? AND w.course = ?
235 ORDER BY p.timemodified ASC", array($timestart, $course->id))) {
1fb5d4b0 236 return false;
237 }
00710f4c 238 $modinfo =& get_fast_modinfo($course);
1fb5d4b0 239
dd97c328 240 $wikis = array();
241
00710f4c
DC
242 $modinfo = get_fast_modinfo($course);
243
244 foreach ($pages as $page) {
245 if (!isset($modinfo->instances['wiki'][$page->wikiid])) {
246 // not visible
247 continue;
248 }
249 $cm = $modinfo->instances['wiki'][$page->wikiid];
dd97c328 250 if (!$cm->uservisible) {
251 continue;
1fb5d4b0 252 }
00710f4c
DC
253 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
254
255 if (!has_capability('mod/wiki:viewpage', $context)) {
256 continue;
257 }
258
259 $groupmode = groups_get_activity_groupmode($cm, $course);
260
261 if ($groupmode) {
262 if ($groupmode == SEPARATEGROUPS and !has_capability('mod/wiki:managewiki', $context)) {
263 // separate mode
264 if (isguestuser()) {
265 // shortcut
266 continue;
267 }
dd97c328 268
00710f4c
DC
269 if (is_null($modinfo->groups)) {
270 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
271 }
b09f731c 272
00710f4c
DC
273 if (!in_array($page->groupid, $modinfo->groups[0])) {
274 continue;
275 }
276 }
277 }
278 $wikis[] = $page;
1fb5d4b0 279 }
00710f4c 280 unset($pages);
39fcb981 281
dd97c328 282 if (!$wikis) {
283 return false;
284 }
00710f4c 285 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 3);
dd97c328 286 foreach ($wikis as $wiki) {
00710f4c
DC
287 $cm = $modinfo->instances['wiki'][$wiki->wikiid];
288 $link = $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $wiki->id;
289 print_recent_activity_note($wiki->timemodified, $wiki, $cm->name, $link, false, $viewfullnames);
1fb5d4b0 290 }
dd97c328 291
00710f4c 292 return true; // True if anything was printed, otherwise false
39fcb981 293}
e5cc530b 294/**
295 * Function to be run periodically according to the moodle cron
296 * This function searches for things that need to be done, such
297 * as sending out mail, toggling flags etc ...
298 *
00710f4c
DC
299 * @uses $CFG
300 * @return boolean
301 * @todo Finish documenting this function
302 **/
303function wiki_cron() {
304 global $CFG;
39fcb981 305
00710f4c 306 return true;
39fcb981 307}
308
e5cc530b 309/**
00710f4c
DC
310 * Must return an array of grades for a given instance of this module,
311 * indexed by user. It also returns a maximum allowed grade.
e5cc530b 312 *
00710f4c
DC
313 * Example:
314 * $return->grades = array of grades;
315 * $return->maxgrade = maximum allowed grade;
e5cc530b 316 *
00710f4c 317 * return $return;
e5cc530b 318 *
00710f4c
DC
319 * @param int $wikiid ID of an instance of this module
320 * @return mixed Null or object with an array of grades and with the maximum grade
321 **/
322function wiki_grades($wikiid) {
323 return null;
39fcb981 324}
325
e5cc530b 326/**
00710f4c
DC
327 * Must return an array of user records (all data) who are participants
328 * for a given instance of wiki. Must include every user involved
329 * in the instance, independient of his role (student, teacher, admin...)
330 * See other modules as example.
331 *
332 * @param int $wikiid ID of an instance of this module
333 * @return mixed boolean/array of students
334 **/
335function wiki_get_participants($wikiid) {
336 return false;
39fcb981 337}
338
e5cc530b 339/**
00710f4c
DC
340 * This function returns if a scale is being used by one wiki
341 * it it has support for grading and scales. Commented code should be
342 * modified if necessary. See forum, glossary or journal modules
343 * as reference.
344 *
345 * @param int $wikiid ID of an instance of this module
346 * @return mixed
347 * @todo Finish documenting this function
348 **/
349function wiki_scale_used($wikiid, $scaleid) {
350 $return = false;
351
352 //$rec = get_record("wiki","id","$wikiid","scale","-$scaleid");
353 //
354 //if (!empty($rec) && !empty($scaleid)) {
355 // $return = true;
356 //}
39fcb981 357
00710f4c 358 return $return;
39fcb981 359}
360
e5cc530b 361/**
00710f4c
DC
362 * Checks if scale is being used by any instance of wiki.
363 * This function was added in 1.9
e5cc530b 364 *
00710f4c
DC
365 * This is used to find out if scale used anywhere
366 * @param $scaleid int
367 * @return boolean True if the scale is used by any wiki
e5cc530b 368 */
00710f4c 369function wiki_scale_used_anywhere($scaleid) {
655b09ca 370 global $DB;
c513f599 371
75a8ba54
DM
372 //if ($scaleid and $DB->record_exists('wiki', array('grade' => -$scaleid))) {
373 // return true;
374 //} else {
375 // return false;
376 //}
377
378 return false;
39fcb981 379}
380
e5cc530b 381/**
00710f4c 382 * Pluginfile hook
e5cc530b 383 *
00710f4c 384 * @author Josep Arus
e5cc530b 385 */
64f93798 386function wiki_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
00710f4c 387 global $CFG;
14a960ac 388
64f93798
PS
389 if ($context->contextlevel != CONTEXT_MODULE) {
390 return false;
391 }
392
393 require_course_login($course, true, $cm);
394
00710f4c 395 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
39fcb981 396
64f93798 397 if ($filearea == 'attachments') {
e8cf27e8 398 $swid = (int) array_shift($args);
b82fcee1 399
e8cf27e8 400 if (!$subwiki = wiki_get_subwiki($swid)) {
00710f4c 401 return false;
c513f599 402 }
c513f599 403
00710f4c 404 require_capability('mod/wiki:viewpage', $context);
f84d6a8d 405
64f93798 406 $relativepath = implode('/', $args);
39fcb981 407
64f93798 408 $fullpath = "/$context->id/mod_wiki/attachments/$swid/$relativepath";
87a65403 409
00710f4c
DC
410 $fs = get_file_storage();
411 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
412 return false;
87a65403 413 }
414
00710f4c 415 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
39fcb981 416
00710f4c 417 send_stored_file($file, $lifetime, 0);
39fcb981 418 }
39fcb981 419}
64f93798 420
00710f4c
DC
421function wiki_search_form($cm, $search='') {
422 global $CFG, $OUTPUT;
39fcb981 423
00710f4c
DC
424 $output = '<div class="wikisearch">';
425 $output .= '<form method="post" action="'.$CFG->wwwroot.'/mod/wiki/search.php" style="display:inline">';
426 $output .= '<fieldset class="invisiblefieldset">';
427 $output .= '<input name="searchstring" type="text" size="18" value="'.s($search, true).'" alt="search" />';
428 $output .= '<input name="courseid" type="hidden" value="'.$cm->course.'" />';
429 $output .= '<input name="cmid" type="hidden" value="'.$cm->id.'" />';
430 $output .= '<input name="searchwikicontent" type="hidden" value="1" />';
431 $output .= ' <input value="'.get_string('searchwikis', 'wiki').'" type="submit" />';
432 $output .= '</fieldset>';
433 $output .= '</form>';
434 $output .= '</div>';
39fcb981 435
00710f4c 436 return $output;
39fcb981 437}
00710f4c 438function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm) {
532d319d
JP
439 global $CFG, $PAGE, $USER;
440
441 require_once ($CFG->dirroot . '/mod/wiki/locallib.php');
442
00710f4c
DC
443 $url = $PAGE->url;
444 $userid = 0;
445 if ($module->wikimode == 'individual') {
39fcb981 446 $userid = $USER->id;
447 }
39fcb981 448
00710f4c
DC
449 if(!$wiki = wiki_get_wiki($cm->instance)) {
450 return false;
87a65403 451 }
00710f4c 452
3ca72eb7
JP
453 if (!$gid = groups_get_activity_group($cm)){
454 $gid = 0;
455 }
00710f4c
DC
456 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)){
457 return null;
458 } else {
459 $swid = $subwiki->id;
27978302 460 }
14a960ac 461
00710f4c
DC
462 $pageid = $url->param('pageid');
463 $cmid = $url->param('id');
464 if (empty($pageid) && !empty($cmid)) {
465 // wiki main page
466 $page = wiki_get_page_by_title($swid, $wiki->firstpagetitle);
467 $pageid = $page->id;
468 }
469 $link = new moodle_url('/mod/wiki/create.php', array('action'=>'new', 'swid'=>$swid));
470 $node = $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING);
14a960ac 471
00710f4c 472 if (is_numeric($pageid)) {
39fcb981 473
00710f4c
DC
474 $link = new moodle_url('/mod/wiki/view.php', array('pageid'=>$pageid));
475 $node = $navref->add(get_string('view', 'wiki'), $link, navigation_node::TYPE_SETTING);
14a960ac 476
00710f4c
DC
477 $link = new moodle_url('/mod/wiki/edit.php', array('pageid'=>$pageid));
478 $node = $navref->add(get_string('edit', 'wiki'), $link, navigation_node::TYPE_SETTING);
14a960ac 479
00710f4c
DC
480 $link = new moodle_url('/mod/wiki/comments.php', array('pageid'=>$pageid));
481 $node = $navref->add(get_string('comments', 'wiki'), $link, navigation_node::TYPE_SETTING);
14a960ac 482
00710f4c
DC
483 $link = new moodle_url('/mod/wiki/history.php', array('pageid'=>$pageid));
484 $node = $navref->add(get_string('history', 'wiki'), $link, navigation_node::TYPE_SETTING);
39fcb981 485
00710f4c
DC
486 $link = new moodle_url('/mod/wiki/map.php', array('pageid'=>$pageid));
487 $node = $navref->add(get_string('map', 'wiki'), $link, navigation_node::TYPE_SETTING);
488 }
17da2e6f 489}
16b86ae4
DC
490/**
491 * Returns all other caps used in wiki module
492 *
493 * @return array
494 */
495function wiki_get_extra_capabilities() {
496 return array('moodle/comment:view', 'moodle/comment:post', 'moodle/comment:delete');
497}