MDL-21695 Replaced grade/gradepass.html
[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) {
39fcb981 155 return $return;
156}
157
e5cc530b 158/**
00710f4c 159 * Print a detailed representation of what a user has done with
e5cc530b 160 * a given particular instance of this module, for user activity reports.
161 *
00710f4c
DC
162 * @return boolean
163 * @todo Finish documenting this function
164 **/
39fcb981 165function wiki_user_complete($course, $user, $mod, $wiki) {
39fcb981 166 return true;
167}
168
00710f4c
DC
169/**
170 * Indicates API features that the wiki supports.
171 *
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)
182 */
183function 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;
ebe93f74
JP
203 case FEATURE_BACKUP_MOODLE2:
204 return true;
00710f4c
DC
205
206 default:
207 return null;
208 }
209}
210
e5cc530b 211/**
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.
215 *
00710f4c
DC
216 * @global $CFG
217 * @global $DB
218 * @uses CONTEXT_MODULE
219 * @uses VISIBLEGROUPS
e5cc530b 220 * @param object $course
00710f4c 221 * @param bool $viewfullnames capability
e5cc530b 222 * @param int $timestart
00710f4c
DC
223 * @return boolean
224 **/
225function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
9ab0a4fa 226 global $CFG, $DB, $OUTPUT;
14a960ac 227
00710f4c 228 if (!$pages = $DB->get_records_sql("SELECT p.*, w.id as wikiid, sw.groupid
ebe93f74
JP
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))) {
1fb5d4b0 234 return false;
235 }
00710f4c 236 $modinfo =& get_fast_modinfo($course);
1fb5d4b0 237
dd97c328 238 $wikis = array();
239
00710f4c
DC
240 $modinfo = get_fast_modinfo($course);
241
242 foreach ($pages as $page) {
243 if (!isset($modinfo->instances['wiki'][$page->wikiid])) {
244 // not visible
245 continue;
246 }
247 $cm = $modinfo->instances['wiki'][$page->wikiid];
dd97c328 248 if (!$cm->uservisible) {
249 continue;
1fb5d4b0 250 }
00710f4c
DC
251 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
252
253 if (!has_capability('mod/wiki:viewpage', $context)) {
254 continue;
255 }
256
257 $groupmode = groups_get_activity_groupmode($cm, $course);
258
259 if ($groupmode) {
260 if ($groupmode == SEPARATEGROUPS and !has_capability('mod/wiki:managewiki', $context)) {
261 // separate mode
262 if (isguestuser()) {
263 // shortcut
264 continue;
265 }
dd97c328 266
00710f4c
DC
267 if (is_null($modinfo->groups)) {
268 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
269 }
b09f731c 270
00710f4c
DC
271 if (!in_array($page->groupid, $modinfo->groups[0])) {
272 continue;
273 }
274 }
275 }
276 $wikis[] = $page;
1fb5d4b0 277 }
00710f4c 278 unset($pages);
39fcb981 279
dd97c328 280 if (!$wikis) {
281 return false;
282 }
00710f4c 283 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 3);
dd97c328 284 foreach ($wikis as $wiki) {
00710f4c
DC
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);
1fb5d4b0 288 }
dd97c328 289
00710f4c 290 return true; // True if anything was printed, otherwise false
39fcb981 291}
e5cc530b 292/**
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 ...
296 *
00710f4c
DC
297 * @uses $CFG
298 * @return boolean
299 * @todo Finish documenting this function
300 **/
301function wiki_cron() {
302 global $CFG;
39fcb981 303
00710f4c 304 return true;
39fcb981 305}
306
e5cc530b 307/**
00710f4c
DC
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.
e5cc530b 310 *
00710f4c
DC
311 * Example:
312 * $return->grades = array of grades;
313 * $return->maxgrade = maximum allowed grade;
e5cc530b 314 *
00710f4c 315 * return $return;
e5cc530b 316 *
00710f4c
DC
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
319 **/
320function wiki_grades($wikiid) {
321 return null;
39fcb981 322}
323
e5cc530b 324/**
00710f4c
DC
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.
329 *
330 * @param int $wikiid ID of an instance of this module
331 * @return mixed boolean/array of students
332 **/
333function wiki_get_participants($wikiid) {
334 return false;
39fcb981 335}
336
e5cc530b 337/**
00710f4c
DC
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.
342 *
343 * @param int $wikiid ID of an instance of this module
344 * @return mixed
345 * @todo Finish documenting this function
346 **/
347function wiki_scale_used($wikiid, $scaleid) {
348 $return = false;
349
350 //$rec = get_record("wiki","id","$wikiid","scale","-$scaleid");
351 //
352 //if (!empty($rec) && !empty($scaleid)) {
353 // $return = true;
354 //}
39fcb981 355
00710f4c 356 return $return;
39fcb981 357}
358
e5cc530b 359/**
00710f4c
DC
360 * Checks if scale is being used by any instance of wiki.
361 * This function was added in 1.9
e5cc530b 362 *
00710f4c
DC
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
e5cc530b 366 */
00710f4c 367function wiki_scale_used_anywhere($scaleid) {
655b09ca 368 global $DB;
c513f599 369
00710f4c
DC
370 if ($scaleid and $DB->record_exists('wiki', array('grade', $scaleid))) {
371 return true;
372 } else {
373 return false;
c513f599 374 }
39fcb981 375}
376
e5cc530b 377/**
00710f4c 378 * Pluginfile hook
e5cc530b 379 *
00710f4c 380 * @author Josep Arus
e5cc530b 381 */
00710f4c
DC
382function wiki_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedownload) {
383 global $CFG;
14a960ac 384
00710f4c 385 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
39fcb981 386
e8cf27e8
DC
387 if ($filearea == 'wiki_attachments') {
388 $swid = (int) array_shift($args);
b82fcee1 389
e8cf27e8 390 if (!$subwiki = wiki_get_subwiki($swid)) {
00710f4c 391 return false;
c513f599 392 }
c513f599 393
00710f4c 394 require_course_login($course->id, true, $cm);
39fcb981 395
00710f4c 396 require_capability('mod/wiki:viewpage', $context);
f84d6a8d 397
00710f4c 398 $relativepath = '/' . implode('/', $args);
39fcb981 399
e8cf27e8 400 $fullpath = $context->id . 'wiki_attachments' . $swid . $relativepath;
87a65403 401
00710f4c
DC
402 $fs = get_file_storage();
403 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
404 return false;
87a65403 405 }
406
00710f4c 407 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
39fcb981 408
00710f4c 409 send_stored_file($file, $lifetime, 0);
39fcb981 410 }
39fcb981 411}
00710f4c
DC
412function wiki_search_form($cm, $search='') {
413 global $CFG, $OUTPUT;
39fcb981 414
00710f4c
DC
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>';
39fcb981 426
00710f4c 427 return $output;
39fcb981 428}
00710f4c
DC
429function 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') {
39fcb981 434 $userid = $USER->id;
435 }
39fcb981 436
00710f4c
DC
437 if(!$wiki = wiki_get_wiki($cm->instance)) {
438 return false;
87a65403 439 }
00710f4c 440
3ca72eb7
JP
441 if (!$gid = groups_get_activity_group($cm)){
442 $gid = 0;
443 }
00710f4c
DC
444 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)){
445 return null;
446 } else {
447 $swid = $subwiki->id;
27978302 448 }
14a960ac 449
00710f4c
DC
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;
456 }
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);
14a960ac 459
00710f4c 460 if (is_numeric($pageid)) {
39fcb981 461
00710f4c
DC
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);
14a960ac 464
00710f4c
DC
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);
14a960ac 467
00710f4c
DC
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);
14a960ac 470
00710f4c
DC
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);
39fcb981 473
00710f4c
DC
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);
476 }
14a960ac 477
00710f4c
DC
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);
485 //}
17da2e6f 486}