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