weekly release 2.1dev
[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
e5cc530b 37/**
38 * Given an object containing all the necessary data,
00710f4c 39 * (defined by the form in mod.html) this function
e5cc530b 40 * will create a new instance and return the id number
41 * of the new instance.
42 *
00710f4c
DC
43 * @param object $instance An object from the form in mod.html
44 * @return int The id of the newly inserted wiki record
45 **/
39fcb981 46function wiki_add_instance($wiki) {
c18269c7 47 global $DB;
14a960ac 48
39fcb981 49 $wiki->timemodified = time();
39fcb981 50 # May have to add extra stuff in here #
f6e56f28
JP
51 if (empty($wiki->forceformat)) {
52 $wiki->forceformat = 0;
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
0cdbbc17
AB
144function 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');
148
149 $componentstr = get_string('modulenameplural', 'wiki');
150 $status = array();
151
152 //get the wiki(s) in this course.
153 if (!$wikis = $DB->get_records('wiki', array('course' => $data->courseid))) {
154 return false;
155 }
156 $errors = false;
157 foreach ($wikis as $wiki) {
7c61b94c
DC
158
159 // remove all comments
160 if (!empty($data->reset_wiki_comments)) {
161 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id)) {
162 continue;
163 }
164 $context = get_context_instance(CONTEXT_MODULE, $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);
167 }
168
169 if (!empty($data->reset_wiki_tags)) {
170 # Get subwiki information #
171 $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id));
172
173 foreach ($subwikis as $subwiki) {
174 if ($pages = $DB->get_records('wiki_pages', array('subwikiid' => $subwiki->id))) {
175 foreach ($pages as $page) {
f0f77dfa 176 $tags = tag_get_tags_array('wiki_pages', $page->id);
7c61b94c
DC
177 foreach ($tags as $tagid => $tagname) {
178 // Delete the related tag_instances related to the wiki page.
f0f77dfa 179 $errors = tag_delete_instance('wiki_pages', $page->id, $tagid);
7c61b94c
DC
180 $status[] = array('component' => $componentstr, 'item' => get_string('tagsdeleted', 'wiki'), 'error' => $errors);
181 }
0cdbbc17
AB
182 }
183 }
184 }
185 }
186 }
187 return $status;
188}
189
190
191function wiki_reset_course_form_definition(&$mform) {
192 $mform->addElement('header', 'wikiheader', get_string('modulenameplural', 'wiki'));
7c61b94c
DC
193 $mform->addElement('advcheckbox', 'reset_wiki_tags', get_string('removeallwikitags', 'wiki'));
194 $mform->addElement('advcheckbox', 'reset_wiki_comments', get_string('deleteallcomments'));
0cdbbc17
AB
195}
196
e5cc530b 197/**
198 * Return a small object with summary information about what a
199 * user has done with a given particular instance of this module
200 * Used for user activity reports.
201 * $return->time = the time they did it
202 * $return->info = a short text description
203 *
e5cc530b 204 * @return null
00710f4c
DC
205 * @todo Finish documenting this function
206 **/
39fcb981 207function wiki_user_outline($course, $user, $mod, $wiki) {
e8930396 208 $return = NULL;
39fcb981 209 return $return;
210}
211
e5cc530b 212/**
00710f4c 213 * Print a detailed representation of what a user has done with
e5cc530b 214 * a given particular instance of this module, for user activity reports.
215 *
00710f4c
DC
216 * @return boolean
217 * @todo Finish documenting this function
218 **/
39fcb981 219function wiki_user_complete($course, $user, $mod, $wiki) {
39fcb981 220 return true;
221}
222
00710f4c
DC
223/**
224 * Indicates API features that the wiki supports.
225 *
226 * @uses FEATURE_GROUPS
227 * @uses FEATURE_GROUPINGS
228 * @uses FEATURE_GROUPMEMBERSONLY
229 * @uses FEATURE_MOD_INTRO
230 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
231 * @uses FEATURE_COMPLETION_HAS_RULES
232 * @uses FEATURE_GRADE_HAS_GRADE
233 * @uses FEATURE_GRADE_OUTCOMES
234 * @param string $feature
235 * @return mixed True if yes (some features may use other values)
236 */
237function wiki_supports($feature) {
238 switch ($feature) {
239 case FEATURE_GROUPS:
240 return true;
241 case FEATURE_GROUPINGS:
242 return true;
243 case FEATURE_GROUPMEMBERSONLY:
244 return true;
245 case FEATURE_MOD_INTRO:
246 return true;
247 case FEATURE_COMPLETION_TRACKS_VIEWS:
248 return true;
249 case FEATURE_COMPLETION_HAS_RULES:
250 return true;
251 case FEATURE_GRADE_HAS_GRADE:
1c79d7db 252 return false;
00710f4c 253 case FEATURE_GRADE_OUTCOMES:
1c79d7db 254 return false;
00710f4c
DC
255 case FEATURE_RATE:
256 return false;
ebe93f74
JP
257 case FEATURE_BACKUP_MOODLE2:
258 return true;
00710f4c
DC
259
260 default:
261 return null;
262 }
263}
264
e5cc530b 265/**
266 * Given a course and a time, this module should find recent activity
267 * that has occurred in wiki activities and print it out.
268 * Return true if there was output, or false is there was none.
269 *
00710f4c
DC
270 * @global $CFG
271 * @global $DB
272 * @uses CONTEXT_MODULE
273 * @uses VISIBLEGROUPS
e5cc530b 274 * @param object $course
00710f4c 275 * @param bool $viewfullnames capability
e5cc530b 276 * @param int $timestart
00710f4c
DC
277 * @return boolean
278 **/
279function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
9ab0a4fa 280 global $CFG, $DB, $OUTPUT;
14a960ac 281
621f3ef5
JP
282 $sql = "SELECT p.*, w.id as wikiid, sw.groupid
283 FROM {wiki_pages} p
284 JOIN {wiki_subwikis} sw ON sw.id = p.subwikiid
285 JOIN {wiki} w ON w.id = sw.wikiid
286 WHERE p.timemodified > ? AND w.course = ?
287 ORDER BY p.timemodified ASC";
288 if (!$pages = $DB->get_records_sql($sql, array($timestart, $course->id))) {
1fb5d4b0 289 return false;
290 }
00710f4c 291 $modinfo =& get_fast_modinfo($course);
1fb5d4b0 292
dd97c328 293 $wikis = array();
294
00710f4c
DC
295 $modinfo = get_fast_modinfo($course);
296
297 foreach ($pages as $page) {
298 if (!isset($modinfo->instances['wiki'][$page->wikiid])) {
299 // not visible
300 continue;
301 }
302 $cm = $modinfo->instances['wiki'][$page->wikiid];
dd97c328 303 if (!$cm->uservisible) {
304 continue;
1fb5d4b0 305 }
00710f4c
DC
306 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
307
308 if (!has_capability('mod/wiki:viewpage', $context)) {
309 continue;
310 }
311
312 $groupmode = groups_get_activity_groupmode($cm, $course);
313
314 if ($groupmode) {
315 if ($groupmode == SEPARATEGROUPS and !has_capability('mod/wiki:managewiki', $context)) {
316 // separate mode
317 if (isguestuser()) {
318 // shortcut
319 continue;
320 }
dd97c328 321
00710f4c
DC
322 if (is_null($modinfo->groups)) {
323 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
324 }
b09f731c 325
00710f4c
DC
326 if (!in_array($page->groupid, $modinfo->groups[0])) {
327 continue;
328 }
329 }
330 }
331 $wikis[] = $page;
1fb5d4b0 332 }
00710f4c 333 unset($pages);
39fcb981 334
dd97c328 335 if (!$wikis) {
336 return false;
337 }
00710f4c 338 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 3);
dd97c328 339 foreach ($wikis as $wiki) {
00710f4c
DC
340 $cm = $modinfo->instances['wiki'][$wiki->wikiid];
341 $link = $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $wiki->id;
342 print_recent_activity_note($wiki->timemodified, $wiki, $cm->name, $link, false, $viewfullnames);
1fb5d4b0 343 }
dd97c328 344
00710f4c 345 return true; // True if anything was printed, otherwise false
39fcb981 346}
e5cc530b 347/**
348 * Function to be run periodically according to the moodle cron
349 * This function searches for things that need to be done, such
350 * as sending out mail, toggling flags etc ...
351 *
00710f4c
DC
352 * @uses $CFG
353 * @return boolean
354 * @todo Finish documenting this function
355 **/
356function wiki_cron() {
357 global $CFG;
39fcb981 358
00710f4c 359 return true;
39fcb981 360}
361
e5cc530b 362/**
00710f4c
DC
363 * Must return an array of grades for a given instance of this module,
364 * indexed by user. It also returns a maximum allowed grade.
e5cc530b 365 *
00710f4c
DC
366 * Example:
367 * $return->grades = array of grades;
368 * $return->maxgrade = maximum allowed grade;
e5cc530b 369 *
00710f4c 370 * return $return;
e5cc530b 371 *
00710f4c
DC
372 * @param int $wikiid ID of an instance of this module
373 * @return mixed Null or object with an array of grades and with the maximum grade
374 **/
375function wiki_grades($wikiid) {
376 return null;
39fcb981 377}
378
e5cc530b 379/**
00710f4c
DC
380 * Must return an array of user records (all data) who are participants
381 * for a given instance of wiki. Must include every user involved
382 * in the instance, independient of his role (student, teacher, admin...)
383 * See other modules as example.
384 *
385 * @param int $wikiid ID of an instance of this module
386 * @return mixed boolean/array of students
387 **/
388function wiki_get_participants($wikiid) {
389 return false;
39fcb981 390}
391
e5cc530b 392/**
00710f4c
DC
393 * This function returns if a scale is being used by one wiki
394 * it it has support for grading and scales. Commented code should be
395 * modified if necessary. See forum, glossary or journal modules
396 * as reference.
397 *
398 * @param int $wikiid ID of an instance of this module
399 * @return mixed
400 * @todo Finish documenting this function
401 **/
402function wiki_scale_used($wikiid, $scaleid) {
403 $return = false;
404
405 //$rec = get_record("wiki","id","$wikiid","scale","-$scaleid");
406 //
407 //if (!empty($rec) && !empty($scaleid)) {
408 // $return = true;
409 //}
39fcb981 410
00710f4c 411 return $return;
39fcb981 412}
413
e5cc530b 414/**
00710f4c
DC
415 * Checks if scale is being used by any instance of wiki.
416 * This function was added in 1.9
e5cc530b 417 *
00710f4c
DC
418 * This is used to find out if scale used anywhere
419 * @param $scaleid int
420 * @return boolean True if the scale is used by any wiki
e5cc530b 421 */
00710f4c 422function wiki_scale_used_anywhere($scaleid) {
655b09ca 423 global $DB;
c513f599 424
75a8ba54
DM
425 //if ($scaleid and $DB->record_exists('wiki', array('grade' => -$scaleid))) {
426 // return true;
427 //} else {
428 // return false;
429 //}
430
431 return false;
39fcb981 432}
433
e5cc530b 434/**
00710f4c 435 * Pluginfile hook
e5cc530b 436 *
00710f4c 437 * @author Josep Arus
e5cc530b 438 */
64f93798 439function wiki_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
00710f4c 440 global $CFG;
14a960ac 441
64f93798
PS
442 if ($context->contextlevel != CONTEXT_MODULE) {
443 return false;
444 }
445
446 require_course_login($course, true, $cm);
447
00710f4c 448 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
39fcb981 449
64f93798 450 if ($filearea == 'attachments') {
e8cf27e8 451 $swid = (int) array_shift($args);
b82fcee1 452
e8cf27e8 453 if (!$subwiki = wiki_get_subwiki($swid)) {
00710f4c 454 return false;
c513f599 455 }
c513f599 456
00710f4c 457 require_capability('mod/wiki:viewpage', $context);
f84d6a8d 458
64f93798 459 $relativepath = implode('/', $args);
39fcb981 460
64f93798 461 $fullpath = "/$context->id/mod_wiki/attachments/$swid/$relativepath";
87a65403 462
00710f4c
DC
463 $fs = get_file_storage();
464 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
465 return false;
87a65403 466 }
467
00710f4c 468 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
39fcb981 469
00710f4c 470 send_stored_file($file, $lifetime, 0);
39fcb981 471 }
39fcb981 472}
64f93798 473
621f3ef5 474function wiki_search_form($cm, $search = '') {
00710f4c 475 global $CFG, $OUTPUT;
39fcb981 476
621f3ef5
JP
477 $output = '<div class="wikisearch">';
478 $output .= '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/search.php" style="display:inline">';
00710f4c 479 $output .= '<fieldset class="invisiblefieldset">';
621f3ef5
JP
480 $output .= '<input name="searchstring" type="text" size="18" value="' . s($search, true) . '" alt="search" />';
481 $output .= '<input name="courseid" type="hidden" value="' . $cm->course . '" />';
482 $output .= '<input name="cmid" type="hidden" value="' . $cm->id . '" />';
00710f4c 483 $output .= '<input name="searchwikicontent" type="hidden" value="1" />';
621f3ef5 484 $output .= ' <input value="' . get_string('searchwikis', 'wiki') . '" type="submit" />';
00710f4c
DC
485 $output .= '</fieldset>';
486 $output .= '</form>';
487 $output .= '</div>';
39fcb981 488
00710f4c 489 return $output;
39fcb981 490}
00710f4c 491function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm) {
532d319d
JP
492 global $CFG, $PAGE, $USER;
493
621f3ef5 494 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
532d319d 495
00710f4c
DC
496 $url = $PAGE->url;
497 $userid = 0;
498 if ($module->wikimode == 'individual') {
39fcb981 499 $userid = $USER->id;
500 }
39fcb981 501
621f3ef5 502 if (!$wiki = wiki_get_wiki($cm->instance)) {
00710f4c 503 return false;
87a65403 504 }
00710f4c 505
621f3ef5 506 if (!$gid = groups_get_activity_group($cm)) {
3ca72eb7
JP
507 $gid = 0;
508 }
621f3ef5 509 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)) {
00710f4c
DC
510 return null;
511 } else {
512 $swid = $subwiki->id;
27978302 513 }
14a960ac 514
00710f4c
DC
515 $pageid = $url->param('pageid');
516 $cmid = $url->param('id');
517 if (empty($pageid) && !empty($cmid)) {
518 // wiki main page
519 $page = wiki_get_page_by_title($swid, $wiki->firstpagetitle);
520 $pageid = $page->id;
521 }
621f3ef5 522 $link = new moodle_url('/mod/wiki/create.php', array('action' => 'new', 'swid' => $swid));
00710f4c 523 $node = $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING);
14a960ac 524
00710f4c 525 if (is_numeric($pageid)) {
39fcb981 526
621f3ef5 527 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $pageid));
00710f4c 528 $node = $navref->add(get_string('view', 'wiki'), $link, navigation_node::TYPE_SETTING);
14a960ac 529
621f3ef5 530 $link = new moodle_url('/mod/wiki/edit.php', array('pageid' => $pageid));
00710f4c 531 $node = $navref->add(get_string('edit', 'wiki'), $link, navigation_node::TYPE_SETTING);
14a960ac 532
621f3ef5 533 $link = new moodle_url('/mod/wiki/comments.php', array('pageid' => $pageid));
00710f4c 534 $node = $navref->add(get_string('comments', 'wiki'), $link, navigation_node::TYPE_SETTING);
14a960ac 535
621f3ef5 536 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid));
00710f4c 537 $node = $navref->add(get_string('history', 'wiki'), $link, navigation_node::TYPE_SETTING);
39fcb981 538
621f3ef5 539 $link = new moodle_url('/mod/wiki/map.php', array('pageid' => $pageid));
00710f4c
DC
540 $node = $navref->add(get_string('map', 'wiki'), $link, navigation_node::TYPE_SETTING);
541 }
17da2e6f 542}
16b86ae4
DC
543/**
544 * Returns all other caps used in wiki module
545 *
546 * @return array
547 */
548function wiki_get_extra_capabilities() {
549 return array('moodle/comment:view', 'moodle/comment:post', 'moodle/comment:delete');
550}