3 // This file is part of Moodle - http://moodle.org/
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.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Library of functions and constants for module wiki
21 * It contains the great majority of functions defined by Moodle
22 * that are mandatory to develop a module.
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
28 * @author Jordi Piguillem
30 * @author David Jimenez
32 * @author Kenneth Riba
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 require_once ($CFG->dirroot
. '/mod/wiki/locallib.php');
40 * Given an object containing all the necessary data,
41 * (defined by the form in mod.html) this function
42 * will create a new instance and return the id number
43 * of the new instance.
45 * @param object $instance An object from the form in mod.html
46 * @return int The id of the newly inserted wiki record
48 function wiki_add_instance($wiki) {
51 $wiki->timemodified
= time();
52 # May have to add extra stuff in here #
54 return $DB->insert_record('wiki', $wiki);
58 * Given an object containing all the necessary data,
59 * (defined by the form in mod.html) this function
60 * will update an existing instance with new data.
62 * @param object $instance An object from the form in mod.html
63 * @return boolean Success/Fail
65 function wiki_update_instance($wiki) {
68 $wiki->timemodified
= time();
69 $wiki->id
= $wiki->instance
;
70 if (empty($wiki->forceformat
)) {
71 $wiki->forceformat
= 0;
74 # May have to add extra stuff in here #
76 return $DB->update_record('wiki', $wiki);
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.
84 * @param int $id Id of the module instance
85 * @return boolean Success/Failure
87 function wiki_delete_instance($id) {
90 if (!$wiki = $DB->get_record('wiki', array('id' => $id))) {
96 # Get subwiki information #
97 $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id
));
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
)) {
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
)) {
113 # Get versions, and delete them #
114 if (!$DB->delete_records('wiki_versions', array('pageid' => $page->id
), IGNORE_MISSING
)) {
120 if (!$DB->delete_records('wiki_pages', array('subwikiid' => $subwiki->id
), IGNORE_MISSING
)) {
125 # Get existing synonyms, and delete them #
126 if (!$DB->delete_records('wiki_synonyms', array('subwikiid' => $subwiki->id
), IGNORE_MISSING
)) {
130 # Delete any subwikis #
131 if (!$DB->delete_records('wiki_subwikis', array('id' => $subwiki->id
), IGNORE_MISSING
)) {
136 # Delete any dependent records here #
137 if (!$DB->delete_records('wiki', array('id' => $wiki->id
))) {
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
152 * @todo Finish documenting this function
154 function wiki_user_outline($course, $user, $mod, $wiki) {
159 * Print a detailed representation of what a user has done with
160 * a given particular instance of this module, for user activity reports.
163 * @todo Finish documenting this function
165 function wiki_user_complete($course, $user, $mod, $wiki) {
170 * Indicates API features that the wiki supports.
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)
183 function wiki_supports($feature) {
187 case FEATURE_GROUPINGS
:
189 case FEATURE_GROUPMEMBERSONLY
:
191 case FEATURE_MOD_INTRO
:
193 case FEATURE_COMPLETION_TRACKS_VIEWS
:
195 case FEATURE_COMPLETION_HAS_RULES
:
197 case FEATURE_GRADE_HAS_GRADE
:
199 case FEATURE_GRADE_OUTCOMES
:
203 case FEATURE_BACKUP_MOODLE2
:
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.
218 * @uses CONTEXT_MODULE
219 * @uses VISIBLEGROUPS
220 * @param object $course
221 * @param bool $viewfullnames capability
222 * @param int $timestart
225 function wiki_print_recent_activity($course, $viewfullnames, $timestart) {
226 global $CFG, $DB, $OUTPUT;
228 if (!$pages = $DB->get_records_sql("SELECT p.*, w.id as wikiid, sw.groupid
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
))) {
236 $modinfo =& get_fast_modinfo($course);
240 $modinfo = get_fast_modinfo($course);
242 foreach ($pages as $page) {
243 if (!isset($modinfo->instances
['wiki'][$page->wikiid
])) {
247 $cm = $modinfo->instances
['wiki'][$page->wikiid
];
248 if (!$cm->uservisible
) {
251 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
253 if (!has_capability('mod/wiki:viewpage', $context)) {
257 $groupmode = groups_get_activity_groupmode($cm, $course);
260 if ($groupmode == SEPARATEGROUPS
and !has_capability('mod/wiki:managewiki', $context)) {
267 if (is_null($modinfo->groups
)) {
268 $modinfo->groups
= groups_get_user_groups($course->id
); // load all my groups and cache it in modinfo
271 if (!in_array($page->groupid
, $modinfo->groups
[0])) {
283 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 3);
284 foreach ($wikis as $wiki) {
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);
290 return true; // True if anything was printed, otherwise false
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 ...
299 * @todo Finish documenting this function
301 function wiki_cron() {
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.
312 * $return->grades = array of grades;
313 * $return->maxgrade = maximum allowed grade;
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
320 function wiki_grades($wikiid) {
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.
330 * @param int $wikiid ID of an instance of this module
331 * @return mixed boolean/array of students
333 function wiki_get_participants($wikiid) {
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
343 * @param int $wikiid ID of an instance of this module
345 * @todo Finish documenting this function
347 function wiki_scale_used($wikiid, $scaleid) {
350 //$rec = get_record("wiki","id","$wikiid","scale","-$scaleid");
352 //if (!empty($rec) && !empty($scaleid)) {
360 * Checks if scale is being used by any instance of wiki.
361 * This function was added in 1.9
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
367 function wiki_scale_used_anywhere($scaleid) {
370 if ($scaleid and $DB->record_exists('wiki', array('grade', $scaleid))) {
382 function wiki_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedownload) {
385 require_once($CFG->dirroot
. "/mod/wiki/locallib.php");
387 if ($filearea == 'wiki_attachments') {
388 $swid = (int) array_shift($args);
390 if (!$subwiki = wiki_get_subwiki($swid)) {
394 require_course_login($course->id
, true, $cm);
396 require_capability('mod/wiki:viewpage', $context);
398 $relativepath = '/' . implode('/', $args);
400 $fullpath = $context->id
. 'wiki_attachments' . $swid . $relativepath;
402 $fs = get_file_storage();
403 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
407 $lifetime = isset($CFG->filelifetime
) ?
$CFG->filelifetime
: 86400;
409 send_stored_file($file, $lifetime, 0);
412 function wiki_search_form($cm, $search='') {
413 global $CFG, $OUTPUT;
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>';
429 function wiki_extend_navigation(navigation_node
$navref, $course, $module, $cm) {
433 if ($module->wikimode
== 'individual') {
437 if(!$wiki = wiki_get_wiki($cm->instance
)) {
441 if (!$gid = groups_get_activity_group($cm)){
444 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance
, $gid, $userid)){
447 $swid = $subwiki->id
;
450 $pageid = $url->param('pageid');
451 $cmid = $url->param('id');
452 if (empty($pageid) && !empty($cmid)) {
454 $page = wiki_get_page_by_title($swid, $wiki->firstpagetitle
);
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
);
460 if (is_numeric($pageid)) {
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
);
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
);
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
);
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
);
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
);
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);
484 //wiki_build_tree($page, $parent, $keys);