MDL-20445 - change the logic to get 'title' attribute for the link in navbar and...
[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;
203
204 default:
205 return null;
206 }
207}
208
e5cc530b 209/**
210 * Given a course and a time, this module should find recent activity
211 * that has occurred in wiki activities and print it out.
212 * Return true if there was output, or false is there was none.
213 *
00710f4c
DC
214 * @global $CFG
215 * @global $DB
216 * @uses CONTEXT_MODULE
217 * @uses VISIBLEGROUPS
e5cc530b 218 * @param object $course
00710f4c 219 * @param bool $viewfullnames capability
e5cc530b 220 * @param int $timestart
00710f4c
DC
221 * @return boolean
222 **/
223function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
9ab0a4fa 224 global $CFG, $DB, $OUTPUT;
14a960ac 225
00710f4c
DC
226 if (!$pages = $DB->get_records_sql("SELECT p.*, w.id as wikiid, sw.groupid
227 FROM {wiki_pages} p
228 JOIN {wiki_subwikis} sw ON sw.id = p.subwikiid
229 JOIN {wiki} w ON w.id = sw.wikiid
230 WHERE p.timemodified > ? AND w.course = ?
231 ORDER BY p.timemodified ASC", array($timestart, $course->id))) {
1fb5d4b0 232 return false;
233 }
00710f4c 234 $modinfo =& get_fast_modinfo($course);
1fb5d4b0 235
dd97c328 236 $wikis = array();
237
00710f4c
DC
238 $modinfo = get_fast_modinfo($course);
239
240 foreach ($pages as $page) {
241 if (!isset($modinfo->instances['wiki'][$page->wikiid])) {
242 // not visible
243 continue;
244 }
245 $cm = $modinfo->instances['wiki'][$page->wikiid];
dd97c328 246 if (!$cm->uservisible) {
247 continue;
1fb5d4b0 248 }
00710f4c
DC
249 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
250
251 if (!has_capability('mod/wiki:viewpage', $context)) {
252 continue;
253 }
254
255 $groupmode = groups_get_activity_groupmode($cm, $course);
256
257 if ($groupmode) {
258 if ($groupmode == SEPARATEGROUPS and !has_capability('mod/wiki:managewiki', $context)) {
259 // separate mode
260 if (isguestuser()) {
261 // shortcut
262 continue;
263 }
dd97c328 264
00710f4c
DC
265 if (is_null($modinfo->groups)) {
266 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
267 }
b09f731c 268
00710f4c
DC
269 if (!in_array($page->groupid, $modinfo->groups[0])) {
270 continue;
271 }
272 }
273 }
274 $wikis[] = $page;
1fb5d4b0 275 }
00710f4c 276 unset($pages);
39fcb981 277
dd97c328 278 if (!$wikis) {
279 return false;
280 }
00710f4c 281 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 3);
dd97c328 282 foreach ($wikis as $wiki) {
00710f4c
DC
283 $cm = $modinfo->instances['wiki'][$wiki->wikiid];
284 $link = $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $wiki->id;
285 print_recent_activity_note($wiki->timemodified, $wiki, $cm->name, $link, false, $viewfullnames);
1fb5d4b0 286 }
dd97c328 287
00710f4c 288 return true; // True if anything was printed, otherwise false
39fcb981 289}
e5cc530b 290/**
291 * Function to be run periodically according to the moodle cron
292 * This function searches for things that need to be done, such
293 * as sending out mail, toggling flags etc ...
294 *
00710f4c
DC
295 * @uses $CFG
296 * @return boolean
297 * @todo Finish documenting this function
298 **/
299function wiki_cron() {
300 global $CFG;
39fcb981 301
00710f4c 302 return true;
39fcb981 303}
304
e5cc530b 305/**
00710f4c
DC
306 * Must return an array of grades for a given instance of this module,
307 * indexed by user. It also returns a maximum allowed grade.
e5cc530b 308 *
00710f4c
DC
309 * Example:
310 * $return->grades = array of grades;
311 * $return->maxgrade = maximum allowed grade;
e5cc530b 312 *
00710f4c 313 * return $return;
e5cc530b 314 *
00710f4c
DC
315 * @param int $wikiid ID of an instance of this module
316 * @return mixed Null or object with an array of grades and with the maximum grade
317 **/
318function wiki_grades($wikiid) {
319 return null;
39fcb981 320}
321
e5cc530b 322/**
00710f4c
DC
323 * Must return an array of user records (all data) who are participants
324 * for a given instance of wiki. Must include every user involved
325 * in the instance, independient of his role (student, teacher, admin...)
326 * See other modules as example.
327 *
328 * @param int $wikiid ID of an instance of this module
329 * @return mixed boolean/array of students
330 **/
331function wiki_get_participants($wikiid) {
332 return false;
39fcb981 333}
334
e5cc530b 335/**
00710f4c
DC
336 * This function returns if a scale is being used by one wiki
337 * it it has support for grading and scales. Commented code should be
338 * modified if necessary. See forum, glossary or journal modules
339 * as reference.
340 *
341 * @param int $wikiid ID of an instance of this module
342 * @return mixed
343 * @todo Finish documenting this function
344 **/
345function wiki_scale_used($wikiid, $scaleid) {
346 $return = false;
347
348 //$rec = get_record("wiki","id","$wikiid","scale","-$scaleid");
349 //
350 //if (!empty($rec) && !empty($scaleid)) {
351 // $return = true;
352 //}
39fcb981 353
00710f4c 354 return $return;
39fcb981 355}
356
e5cc530b 357/**
00710f4c
DC
358 * Checks if scale is being used by any instance of wiki.
359 * This function was added in 1.9
e5cc530b 360 *
00710f4c
DC
361 * This is used to find out if scale used anywhere
362 * @param $scaleid int
363 * @return boolean True if the scale is used by any wiki
e5cc530b 364 */
00710f4c 365function wiki_scale_used_anywhere($scaleid) {
655b09ca 366 global $DB;
c513f599 367
00710f4c
DC
368 if ($scaleid and $DB->record_exists('wiki', array('grade', $scaleid))) {
369 return true;
370 } else {
371 return false;
c513f599 372 }
39fcb981 373}
374
e5cc530b 375/**
00710f4c 376 * Pluginfile hook
e5cc530b 377 *
00710f4c 378 * @author Josep Arus
e5cc530b 379 */
00710f4c
DC
380function wiki_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedownload) {
381 global $CFG;
14a960ac 382
00710f4c 383 require_once($CFG->dirroot . "/mod/wiki/locallib.php");
39fcb981 384
e8cf27e8
DC
385 if ($filearea == 'wiki_attachments') {
386 $swid = (int) array_shift($args);
b82fcee1 387
e8cf27e8 388 if (!$subwiki = wiki_get_subwiki($swid)) {
00710f4c 389 return false;
c513f599 390 }
c513f599 391
00710f4c 392 require_course_login($course->id, true, $cm);
39fcb981 393
00710f4c 394 require_capability('mod/wiki:viewpage', $context);
f84d6a8d 395
00710f4c 396 $relativepath = '/' . implode('/', $args);
39fcb981 397
e8cf27e8 398 $fullpath = $context->id . 'wiki_attachments' . $swid . $relativepath;
87a65403 399
00710f4c
DC
400 $fs = get_file_storage();
401 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
402 return false;
87a65403 403 }
404
00710f4c 405 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
39fcb981 406
00710f4c 407 send_stored_file($file, $lifetime, 0);
39fcb981 408 }
39fcb981 409}
00710f4c
DC
410function wiki_search_form($cm, $search='') {
411 global $CFG, $OUTPUT;
39fcb981 412
00710f4c
DC
413 $output = '<div class="wikisearch">';
414 $output .= '<form method="post" action="'.$CFG->wwwroot.'/mod/wiki/search.php" style="display:inline">';
415 $output .= '<fieldset class="invisiblefieldset">';
416 $output .= '<input name="searchstring" type="text" size="18" value="'.s($search, true).'" alt="search" />';
417 $output .= '<input name="courseid" type="hidden" value="'.$cm->course.'" />';
418 $output .= '<input name="cmid" type="hidden" value="'.$cm->id.'" />';
419 $output .= '<input name="searchwikicontent" type="hidden" value="1" />';
420 $output .= ' <input value="'.get_string('searchwikis', 'wiki').'" type="submit" />';
421 $output .= '</fieldset>';
422 $output .= '</form>';
423 $output .= '</div>';
39fcb981 424
00710f4c 425 return $output;
39fcb981 426}
00710f4c
DC
427function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm) {
428 global $PAGE, $USER;
429 $url = $PAGE->url;
430 $userid = 0;
431 if ($module->wikimode == 'individual') {
39fcb981 432 $userid = $USER->id;
433 }
39fcb981 434
00710f4c
DC
435 if(!$wiki = wiki_get_wiki($cm->instance)) {
436 return false;
87a65403 437 }
00710f4c
DC
438
439 $gid = groups_get_activity_group($cm);
440 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)){
441 return null;
442 } else {
443 $swid = $subwiki->id;
27978302 444 }
14a960ac 445
00710f4c
DC
446 $pageid = $url->param('pageid');
447 $cmid = $url->param('id');
448 if (empty($pageid) && !empty($cmid)) {
449 // wiki main page
450 $page = wiki_get_page_by_title($swid, $wiki->firstpagetitle);
451 $pageid = $page->id;
452 }
453 $link = new moodle_url('/mod/wiki/create.php', array('action'=>'new', 'swid'=>$swid));
454 $node = $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING);
14a960ac 455
00710f4c 456 if (is_numeric($pageid)) {
39fcb981 457
00710f4c
DC
458 $link = new moodle_url('/mod/wiki/view.php', array('pageid'=>$pageid));
459 $node = $navref->add(get_string('view', 'wiki'), $link, navigation_node::TYPE_SETTING);
14a960ac 460
00710f4c
DC
461 $link = new moodle_url('/mod/wiki/edit.php', array('pageid'=>$pageid));
462 $node = $navref->add(get_string('edit', 'wiki'), $link, navigation_node::TYPE_SETTING);
14a960ac 463
00710f4c
DC
464 $link = new moodle_url('/mod/wiki/comments.php', array('pageid'=>$pageid));
465 $node = $navref->add(get_string('comments', 'wiki'), $link, navigation_node::TYPE_SETTING);
14a960ac 466
00710f4c
DC
467 $link = new moodle_url('/mod/wiki/history.php', array('pageid'=>$pageid));
468 $node = $navref->add(get_string('history', 'wiki'), $link, navigation_node::TYPE_SETTING);
39fcb981 469
00710f4c
DC
470 $link = new moodle_url('/mod/wiki/map.php', array('pageid'=>$pageid));
471 $node = $navref->add(get_string('map', 'wiki'), $link, navigation_node::TYPE_SETTING);
472 }
14a960ac 473
00710f4c
DC
474 //if ($page = wiki_get_first_page($swid, $module)) {
475 //$node = $navref->add(get_string('pageindex', 'wiki'));
476 //$link = new moodle_url('/mod/wiki/view.php', array('pageid'=>$page->id));
477 //$icon = new pix_icon('f/odt', '');
478 //$parent = $node->add($page->title, $link, null, null, "index_$page->id", $icon);
479 //$keys = array();
480 //wiki_build_tree($page, $parent, $keys);
481 //}
17da2e6f 482}