MDL-18910 moving modedit features to modname_supports()
[moodle.git] / mod / wiki / lib.php
CommitLineData
472a28ca 1<?php // $Id$
39fcb981 2
3/// Library of functions and constants for module wiki
4/// (replace wiki with the name of your module and delete this line)
5
6
7$wiki_CONSTANT = 7; /// for example
8$site = get_site();
713e7568 9$WIKI_TYPES = array ('teacher' => get_string('defaultcourseteacher'),
39fcb981 10 'group' => get_string('groups',"wiki"),
713e7568 11 'student' => get_string('defaultcoursestudent') );
4a9df373 12define("EWIKI_ESCAPE_AT", 0); # For the algebraic filter
39fcb981 13
22e846cd 14// How long locks stay around without being confirmed (seconds)
4db1861a 15define("WIKI_LOCK_PERSISTENCE",120);
22e846cd 16
17// How often to confirm that you still want a lock
4db1861a 18define("WIKI_LOCK_RECONFIRM",60);
22e846cd 19
4db1861a 20// Session variable used to store wiki locks
21define('SESSION_WIKI_LOCKS','wikilocks');
22e846cd 22
ee3cc995 23/*** Moodle 1.7 compatibility functions *****
24 *
25 ********************************************/
26function wiki_context($wiki) {
27 //TODO: add some $cm caching if needed
28 if (is_object($wiki)) {
29 $wiki = $wiki->id;
30 }
31 if (! $cm = get_coursemodule_from_instance('wiki', $wiki)) {
4d6ba0f0 32 print_error('invalidcoursemodule');
ee3cc995 33 }
34
35 return get_context_instance(CONTEXT_MODULE, $cm->id);
36}
37
38function wiki_is_teacher($wiki, $userid=NULL) {
39 return has_capability('mod/wiki:manage', wiki_context($wiki), $userid);
40}
41
42function wiki_is_teacheredit($wiki, $userid=NULL) {
33e7885e 43 return has_capability('mod/wiki:manage', wiki_context($wiki), $userid)
44 and has_capability('moodle/site:accessallgroups', wiki_context($wiki), $userid);
ee3cc995 45}
46
47function wiki_is_student($wiki, $userid=NULL) {
48 return has_capability('mod/wiki:participate', wiki_context($wiki), $userid);
49}
50
4da8ff66 51function wiki_get_students($wiki, $groups='', $sort='u.lastaccess', $fields='u.*') {
52 return $users = get_users_by_capability(wiki_context($wiki), 'mod/wiki:participate', $fields, $sort, '', '', $groups);
ee3cc995 53}
54
55/* end of compatibility functions */
56
57
39fcb981 58function wiki_add_instance($wiki) {
c18269c7 59 global $DB;
8f0cd6ef 60/// Given an object containing all the necessary data,
7cac0c4b 61/// (defined by the form in mod_form.php) this function
8f0cd6ef 62/// will create a new instance and return the id number
39fcb981 63/// of the new instance.
64
65 $wiki->timemodified = time();
66
67 # May have to add extra stuff in here #
68
526efb55 69 /// Determine the pagename for this wiki and save.
70 $wiki->pagename = wiki_page_name($wiki);
c513f599 71
c18269c7 72 return $DB->insert_record("wiki", $wiki);
39fcb981 73}
74
75
76function wiki_update_instance($wiki) {
c18269c7 77 global $DB;
8f0cd6ef 78/// Given an object containing all the necessary data,
7cac0c4b 79/// (defined by the form in mod_form.php) this function
39fcb981 80/// will update an existing instance with new data.
81
526efb55 82 /// Determine the pagename for this wiki.
83 $wiki->pagename = wiki_page_name($wiki);
39fcb981 84
85 $wiki->timemodified = time();
86 $wiki->id = $wiki->instance;
c18269c7 87 return $DB->update_record("wiki", $wiki);
39fcb981 88}
89
90/// Delete all Directories recursively
91function wiki_rmdir($basedir) {
92 $handle = @opendir($basedir);
93 if($handle) {
94 while (false!==($folder = readdir($handle))) {
ee3cc995 95 if($folder != "." && $folder != ".." && $folder != "CVS") {
39fcb981 96 wiki_rmdir("$basedir/$folder"); // recursive
8f0cd6ef 97 }
39fcb981 98 }
99 closedir($handle);
100 }
101 @rmdir($basedir);
102}
103
104function wiki_delete_instance($id) {
105/// Given an ID of an instance of this module,
8f0cd6ef 106/// this function will permanently delete the instance
107/// and any data that depends on it.
c18269c7 108 global $CFG, $DB;
8f0cd6ef 109
c18269c7 110 if (! $wiki = $DB->get_record("wiki", array("id"=>$id))) {
39fcb981 111 return false;
112 }
113
114 $result = true;
115
116 #Delete Files
117### Should probably check regardless of this setting in case its been changed...
118 if($wiki->ewikiacceptbinary) {
119 if ($basedir = $CFG->dataroot."/".$wiki->course."/".$CFG->moddata."/wiki/$id") {
120 if ($files = get_directory_list($basedir)) {
121 foreach ($files as $file) {
122 #if ($file != $exception) {
123 unlink("$basedir/$file");
124 notify("Existing file '$file' has been deleted!");
125 #}
126 }
127 }
128 #if (!$exception) { // Delete directory as well, if empty
8f0cd6ef 129 wiki_rmdir("$basedir");
39fcb981 130 #}
8f0cd6ef 131 }
39fcb981 132 }
8f0cd6ef 133
39fcb981 134 # Delete any dependent records here #
c18269c7 135 if(!$DB->delete_records("wiki_locks", array("wikiid"=>$wiki->id))) {
22e846cd 136 $result = false;
137 }
138
c18269c7 139 if (! $DB->delete_records("wiki", array("id"=>$wiki->id))) {
39fcb981 140 $result = false;
141 }
142
143 /// Delete all wiki_entries and wiki_pages.
144 if (($wiki_entries = wiki_get_entries($wiki)) !== false) {
145 foreach ($wiki_entries as $wiki_entry) {
c18269c7 146 if (! $DB->delete_records("wiki_pages", array("wiki"=>$wiki_entry->id))) {
39fcb981 147 $result = false;
148 }
c18269c7 149 if (! $DB->delete_records("wiki_entries", array("id"=>$wiki_entry->id))) {
39fcb981 150 $result = false;
151 }
152 }
153 }
8f0cd6ef 154
39fcb981 155 return $result;
156}
157
158function wiki_user_outline($course, $user, $mod, $wiki) {
159/// Return a small object with summary information about what a
160/// user has done with a given particular instance of this module
161/// Used for user activity reports.
162/// $return->time = the time they did it
163/// $return->info = a short text description
164
763692fd 165 $return = NULL;
39fcb981 166 return $return;
167}
168
169function wiki_user_complete($course, $user, $mod, $wiki) {
170/// Print a detailed representation of what a user has done with
171/// a given particular instance of this module, for user activity reports.
172
173 return true;
174}
175
176function wiki_print_recent_activity($course, $isteacher, $timestart) {
8f0cd6ef 177/// Given a course and a time, this module should find recent activity
178/// that has occurred in wiki activities and print it out.
39fcb981 179/// Return true if there was output, or false is there was none.
180
655b09ca 181 global $CFG, $DB;
27978302 182
655b09ca 183 $sql = "SELECT l.*, cm.instance
184 FROM {log} l JOIN {course_modules} cm ON l.cmid = cm.id
185 WHERE l.time > ? AND l.course = ?
186 AND l.module = 'wiki' AND action LIKE 'edit%'
187 ORDER BY l.time ASC";
27978302 188
655b09ca 189 if (!$logs = $DB->get_records_sql($sql, array($timestart, $course->id))){
1fb5d4b0 190 return false;
191 }
192
dd97c328 193 $modinfo = get_fast_modinfo($course);
194 $wikis = array();
195
1fb5d4b0 196 foreach ($logs as $log) {
dd97c328 197 $cm = $modinfo->instances['wiki'][$log->instance];
198 if (!$cm->uservisible) {
199 continue;
1fb5d4b0 200 }
dd97c328 201
b09f731c 202 /// Process log->url and rebuild it here to properly clean the pagename - MDL-15896
203 $extractedpage = preg_replace('/^.*&page=/', '', $log->url);
204 $log->url = preg_replace('/page=.*$/', 'page='.urlencode($extractedpage), $log->url);
205
dd97c328 206 $wikis[$log->info] = wiki_log_info($log);
207 $wikis[$log->info]->pagename = $log->info;
208 $wikis[$log->info]->time = $log->time;
209 $wikis[$log->info]->url = str_replace('&', '&amp;', $log->url);
1fb5d4b0 210 }
39fcb981 211
dd97c328 212 if (!$wikis) {
213 return false;
214 }
215 print_headline(get_string('updatedwikipages', 'wiki').':', 3);
216 foreach ($wikis as $wiki) {
217 print_recent_activity_note($wiki->time, $wiki, $wiki->pagename,
218 $CFG->wwwroot.'/mod/wiki/'.$wiki->url);
1fb5d4b0 219 }
dd97c328 220
221 return false;
1fb5d4b0 222}
223
224function wiki_log_info($log) {
655b09ca 225 global $CFG, $DB;
226 return $DB->get_record_sql("SELECT u.firstname, u.lastname
227 FROM {user} u
228 WHERE u.id = ?", array($log->userid));
39fcb981 229}
230
231function wiki_cron () {
232/// Function to be run periodically according to the moodle cron
8f0cd6ef 233/// This function searches for things that need to be done, such
234/// as sending out mail, toggling flags etc ...
655b09ca 235 global $DB;
39fcb981 236
22e846cd 237 // Delete expired locks
655b09ca 238 $result = $DB->delete_records_select('wiki_locks','lockedseen < '.(time()-WIKI_LOCK_PERSISTENCE));
39fcb981 239
22e846cd 240 return $result;
39fcb981 241}
242
243function wiki_grades($wikiid) {
8f0cd6ef 244/// Must return an array of grades for a given instance of this module,
39fcb981 245/// indexed by user. It also returns a maximum allowed grade.
246
4a9df373 247 return NULL;
39fcb981 248}
249
250function wiki_get_participants($wikiid) {
472a28ca 251//Returns the users with data in one wiki
252//(users with records in wiki_pages and wiki_entries)
655b09ca 253 global $CFG, $DB;
472a28ca 254
255 //Get users from wiki_pages
655b09ca 256 $st_pages = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
257 FROM {user} u, {wiki_entries} e, {wiki_pages} p
258 WHERE e.wikiid = ? and
259 p.wiki = e.id and
260 u.id = p.userid", array($wikiid));
472a28ca 261
262 //Get users from wiki_entries
655b09ca 263 $st_entries = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
264 FROM {user} u, {wiki_entries} e
265 WHERE e.wikiid = ? and
266 u.id = e.userid", array($wikiid));
472a28ca 267
268 //Add entries to pages
269 if ($st_entries) {
270 foreach ($st_entries as $st_entry) {
271 $st_pages[$st_entry->id] = $st_entry;
272 }
273 }
274
275 return $st_pages;
39fcb981 276}
277
472a28ca 278
39fcb981 279//////////////////////////////////////////////////////////////////////////////////////
8f0cd6ef 280/// Any other wiki functions go here. Each of them must have a name that
39fcb981 281/// starts with wiki_
282
283function wiki_wiki_name($wikiname) {
284/// Return the passed in string in Wiki name format.
285/// Remove any leading and trailing whitespace, capitalize all the words
286/// and then remove any internal whitespace.
287
288 if (wiki_is_wiki_name($wikiname)) {
289 return $wikiname;
290 }
291 else {
292 /// Create uppercase words and remove whitespace.
293 $wikiname = preg_replace("/(\w+)\s/", "$1", ucwords(trim($wikiname)));
294
295 /// Check again - there may only be one word.
296 if (wiki_is_wiki_name($wikiname)) {
297 return $wikiname;
298 }
299 /// If there is only one word, append default wiki name to it.
300 else {
301 return $wikiname.get_string('wikidefaultpagename', 'wiki');
302 }
303 }
304}
305
306function wiki_is_wiki_name($wikiname) {
307/// Check for correct wikiname syntax and return true or false.
308
8f0cd6ef 309 /// If there are spaces between the words, incorrect format.
39fcb981 310 if (preg_match_all('/\w+/', $wikiname, $out) > 1) {
311 return false;
312 }
313 /// If there isn't more than one group of uppercase letters separated by
314 /// lowercase letters or '_', incorrect format.
315 else if (preg_match_all('/[A-Z]+[a-z_]+/', $wikiname, $out) > 1) {
316 return true;
317 }
8f0cd6ef 318 else {
39fcb981 319 return false;
320 }
321}
322
323function wiki_page_name(&$wiki) {
324/// Determines the wiki's page name and returns it.
325 if (!empty($wiki->initialcontent)) {
326 $ppos = strrpos($wiki->initialcontent, '/');
327 if ($ppos === false) {
328 $pagename = $wiki->initialcontent;
329 }
330 else {
331 $pagename = substr($wiki->initialcontent, $ppos+1);
332 }
333 }
334 else if (!empty($wiki->pagename)) {
335 $pagename = $wiki->pagename;
336 }
337 else {
526efb55 338 $pagename = $wiki->name;
39fcb981 339 }
340 return $pagename;
341}
342
343function wiki_content_dir(&$wiki) {
344/// Determines the wiki's default content directory (if there is one).
345 global $CFG;
346
347 if (!empty($wiki->initialcontent)) {
348 $ppos = strrpos($wiki->initialcontent, '/');
349 if ($ppos === false) {
350 $subdir = '';
351 }
352 else {
353 $subdir = substr($wiki->initialcontent, 0, $ppos+1);
354 }
355 $contentdir = $CFG->dataroot.'/'.$wiki->course.'/'.$subdir;
356 }
357 else {
358 $contentdir = false;
359 }
360 return $contentdir;
361}
362
c513f599 363function wiki_get_course_wikis($courseid, $wtype='*') {
364/// Returns all wikis for the specified course and optionally of the specified type.
655b09ca 365 global $DB;
c513f599 366
655b09ca 367 $select = 'course = ?';
368 $params = array($courseid);
c513f599 369 if ($wtype != '*') {
655b09ca 370 $select .= ' AND wtype = ?';
371 $params[] = $wtype;
c513f599 372 }
655b09ca 373 return $DB->get_records_select('wiki', $select, $params, 'id');
c513f599 374}
375
39fcb981 376function wiki_has_entries(&$wiki) {
377/// Returns true if wiki already has wiki entries; otherwise false.
655b09ca 378 global $DB;
39fcb981 379
655b09ca 380 return $DB->record_exists('wiki_entries', array('wikiid'=>$wiki->id));
39fcb981 381}
382
383function wiki_get_entries(&$wiki, $byindex=NULL) {
384/// Returns an array with all wiki entries indexed by entry id; false if there are none.
385/// If the optional $byindex is specified, returns the entries indexed by that field.
386/// Valid values for $byindex are 'student', 'group'.
655b09ca 387 global $CFG, $DB;
27978302 388
39fcb981 389 if ($byindex == 'student') {
655b09ca 390 return $DB->get_records('wiki_entries', array('wikiid'=>$wiki->id), '',
39fcb981 391 'userid,id,wikiid,course,groupid,pagename,timemodified');
392 }
393 else if ($byindex == 'group') {
655b09ca 394 return $DB->get_records('wiki_entries', array('wikiid'=>$wiki->id), '',
39fcb981 395 'groupid,id,wikiid,course,userid,pagename,timemodified');
396 }
397 else {
655b09ca 398 return $DB->get_records('wiki_entries', array('wikiid'=>$wiki->id));
39fcb981 399 }
400}
401
c513f599 402function wiki_get_default_entry(&$wiki, &$course, $userid=0, $groupid=0) {
403/// Returns the wiki entry according to the wiki type.
404/// Optionally, will return wiki entry for $userid student wiki, or
405/// $groupid group or teacher wiki.
406/// Creates one if it needs to and it can.
c513f599 407 global $USER;
b82fcee1 408 /// If there is a groupmode, get the user's group id.
27978302 409 $groupmode = groups_get_activity_groupmode($wiki);
b82fcee1 410 // if groups mode is in use and no group supplied, use the first one found
411 if ($groupmode && !$groupid) {
412 if(($mygroupids=mygroupid($course->id)) && count($mygroupids)>0) {
413 // Use first group. They ought to be able to change later
414 $groupid=$mygroupids[0];
415 } else {
416 // Whatever groups are in the course, pick one
2c386f82 417 $coursegroups = groups_get_all_groups($course->id);
b82fcee1 418 if(!$coursegroups || count($coursegroups)==0) {
4d6ba0f0 419 print_error('groupmodeerror', 'wiki');
f84d6a8d 420 }
b82fcee1 421 $unkeyed=array_values($coursegroups); // Make sure first item is index 0
422 $groupid=$unkeyed[0]->id;
423 }
424 }
425
c513f599 426 /// If the wiki entry doesn't exist, can this user create it?
427 if (($wiki_entry = wiki_get_entry($wiki, $course, $userid, $groupid)) === false) {
c513f599 428 if (wiki_can_add_entry($wiki, $USER, $course, $userid, $groupid)) {
429 wiki_add_entry($wiki, $course, $userid, $groupid);
430 if (($wiki_entry = wiki_get_entry($wiki, $course, $userid, $groupid)) === false) {
4d6ba0f0 431 print_error('cannotaddentry', 'wiki');
c513f599 432 }
433 }
434 }
fa22fd5f 435 //print_object($wiki_entry);
c513f599 436 return $wiki_entry;
437}
438
39fcb981 439function wiki_get_entry(&$wiki, &$course, $userid=0, $groupid=0) {
440/// Returns the wiki entry according to the wiki type.
441/// Optionally, will return wiki entry for $userid student wiki, or
442/// $groupid group or teacher wiki.
443 global $USER;
444
445 switch ($wiki->wtype) {
446 case 'student':
447 /// If a specific user was requested, return it, if allowed.
448 if ($userid and wiki_user_can_access_student_wiki($wiki, $userid, $course)) {
449 $wentry = wiki_get_student_entry($wiki, $userid);
450 }
451
452 /// If there is no entry for this user, check if this user is a teacher.
453 else if (!$wentry = wiki_get_student_entry($wiki, $USER->id)) {
ee3cc995 454/* if (wiki_is_teacher($wiki, $USER->id)) {
39fcb981 455 /// If this user is a teacher, return the first entry.
456 if ($wentries = wiki_get_entries($wiki)) {
457 $wentry = current($wentries);
458 }
459 }*/
460 }
461 break;
462
463 case 'group':
464 /// If there is a groupmode, get the user's group id.
27978302 465 $groupmode = groups_get_activity_groupmode($wiki);
745c1968 466 if($groupmode) {
467 if(!$groupid) {
468 if(($mygroupids=mygroupid($course->id)) && count($mygroupids)>0) {
469 // Use first group. They ought to be able to change later
470 $groupid=$mygroupids[0];
471 } else {
472 // Whatever groups are in the course, pick one
2c386f82 473 $coursegroups = groups_get_all_groups($course->id);
745c1968 474 if(!$coursegroups || count($coursegroups)==0) {
4d6ba0f0 475 print_error('groupmodeerror', 'wiki');
f84d6a8d 476 }
745c1968 477 $unkeyed=array_values($coursegroups); // Make sure first item is index 0
478 $groupid=$unkeyed[0]->id;
479 }
480 }
f84d6a8d 481
745c1968 482 //echo "groupid is in wiki_get_entry ".$groupid."<br />";
483 /// If a specific group was requested, return it, if allowed.
484 if ($groupid and wiki_user_can_access_group_wiki($wiki, $groupid, $course)) {
485 $wentry = wiki_get_group_entry($wiki, $groupid);
486 } else {
4d6ba0f0 487 print_error('cannotaccessgroup', 'wiki');
39fcb981 488 }
489 }
490 /// If mode is 'nogroups', then groupid is zero.
491 else {
492 $wentry = wiki_get_group_entry($wiki, 0);
493 }
494 break;
495
496 case 'teacher':
497 /// If there is a groupmode, get the user's group id.
498 if (groupmode($course, $wiki)) {
fa22fd5f 499 $mygroupids = mygroupid($course->id);//same here, default to the first one
500 $groupid = $groupid ? $groupid : $mygroupids[0]/*mygroupid($course->id)*/;
39fcb981 501 }
502
503 /// If a specific group was requested, return it, if allowed.
504 if (wiki_user_can_access_teacher_wiki($wiki, $groupid, $course)) {
505 $wentry = wiki_get_teacher_entry($wiki, $groupid);
506 }
507 break;
508 }
509 return $wentry;
510}
511
512function wiki_get_teacher_entry(&$wiki, $groupid=0) {
655b09ca 513 global $DB;
39fcb981 514/// Returns the wiki entry for the wiki teacher type.
655b09ca 515 return $DB->get_record('wiki_entries', array('wikiid'=>$wiki->id, 'course'=>$wiki->course, 'groupid'=>$groupid));
39fcb981 516}
517
518function wiki_get_group_entry(&$wiki, $groupid=null) {
655b09ca 519 global $DB;
39fcb981 520/// Returns the wiki entry for the given group.
655b09ca 521 return $DB->get_record('wiki_entries', array('wikiid'=>$wiki->id, 'groupid'=>$groupid));
39fcb981 522}
523
524function wiki_get_student_entry(&$wiki, $userid=null) {
525/// Returns the wiki entry for the given student.
655b09ca 526 global $USER, $DB;
39fcb981 527
528 if (is_null($userid)) {
529 $userid = $USER->id;
530 }
655b09ca 531 return $DB->get_record('wiki_entries', array('wikiid'=>$wiki->id, 'userid'=>$userid));
39fcb981 532}
533
534function wiki_get_other_wikis(&$wiki, &$user, &$course, $currentid=0) {
535 /// Returns a list of other wikis to display, depending on the type, group and user.
536 /// Returns the key containing the currently selected entry as well.
537
655b09ca 538 global $CFG, $id, $DB;
39fcb981 539
540 $wikis = false;
541
27978302 542 $groupmode = groups_get_activity_groupmode($wiki);
39fcb981 543 $mygroupid = mygroupid($course->id);
ee3cc995 544 $isteacher = wiki_is_teacher($wiki, $user->id);
545 $isteacheredit = wiki_is_teacheredit($wiki, $user->id);
39fcb981 546
27978302 547 $groupingid = null;
548 $cm = new stdClass;
549 $cm->id = $wiki->cmid;
550 $cm->groupmode = $wiki->groupmode;
551 $cm->groupingid = $wiki->groupingid;
552 $cm->groupmembersonly = $wiki->groupmembersonly;
553 if (!empty($CFG->enablegroupings) && !empty($cm->groupingid)) {
554 $groupingid = $wiki->groupingid;
555 }
556
557
39fcb981 558 switch ($wiki->wtype) {
559
560 case 'student':
561 /// Get all the existing entries for this wiki.
562 $wiki_entries = wiki_get_entries($wiki, 'student');
27978302 563
564 if (!empty($CFG->enablegroupings) && !empty($wiki->groupingid)) {
655b09ca 565 $sql = "SELECT gm.userid FROM {groups_members} gm " .
566 "INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid " .
567 "WHERE gg.groupingid = ? ";
27978302 568
655b09ca 569 $groupingmembers = $DB->get_records_sql($sql, array($wiki->groupingid));
27978302 570 }
571
222ac91b 572 if ($isteacher and (SITEID != $course->id)) {
39fcb981 573
574 /// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all student
575 /// wikis, regardless of creation.
222ac91b 576 if ((SITEID != $course->id) and ($isteacheredit or ($groupmode == NOGROUPS))) {
e2fc9a88 577 if ($students = get_users_by_capability(get_context_instance(CONTEXT_COURSE, $course->id), 'moodle/course:view', '', '', '', '', '', '', false)) {
39fcb981 578 /// Default pagename is dependent on the wiki settings.
579 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
580
581 foreach ($students as $student) {
27978302 582 if (!empty($CFG->enablegroupings) && !empty($wiki->groupingid) && empty($groupingmembers[$student->id])) {
583 continue;
584 }
39fcb981 585 /// If this student already has an entry, use its pagename.
586 if ($wiki_entries[$student->id]) {
587 $pagename = $wiki_entries[$student->id]->pagename;
588 }
589 else {
590 $pagename = $defpagename;
591 }
592
d0b95287 593 $key = 'view.php?id='.$id.'&userid='.$student->id.'&page='.$pagename;
39fcb981 594 $wikis[$key] = fullname($student).':'.$pagename;
595 }
596 }
597 }
598 else if ($groupmode == SEPARATEGROUPS) {
fa22fd5f 599
ee3cc995 600 if ($students = wiki_get_students($wiki, $mygroupid)) {
39fcb981 601 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
602 foreach ($students as $student) {
27978302 603 if (!empty($CFG->enablegroupings) && !empty($wiki->groupingid) && empty($groupingmembers[$student->id])) {
604 continue;
605 }
39fcb981 606 /// If this student already has an entry, use its pagename.
607 if ($wiki_entries[$student->id]) {
608 $pagename = $wiki_entries[$student->id]->pagename;
609 }
610 else {
611 $pagename = $defpagename;
612 }
613
d0b95287 614 $key = 'view.php?id='.$id.'&userid='.$student->id.'&page='.$pagename;
39fcb981 615 $wikis[$key] = fullname($student).':'.$pagename;
616 }
617 }
618 }
619 else if ($groupmode == VISIBLEGROUPS) {
8f0cd6ef 620 /// Get all students in your group.
ee3cc995 621 if ($students = wiki_get_students($wiki, $mygroupid)) {
39fcb981 622 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
623 foreach ($students as $student) {
27978302 624 if (!empty($CFG->enablegroupings) && !empty($wiki->groupingid) && empty($groupingmembers[$student->id])) {
625 continue;
626 }
39fcb981 627 /// If this student already has an entry, use its pagename.
628 if ($wiki_entries[$student->id]) {
629 $pagename = $wiki_entries[$student->id]->pagename;
630 }
631 else {
632 $pagename = $defpagename;
633 }
d0b95287 634 $key = 'view.php?id='.$id.'&userid='.$student->id.'&page='.$pagename;
39fcb981 635 $wikis[$key] = fullname($student).':'.$pagename;
636 }
637 }
638 /// Get all student wikis created, regardless of group.
27978302 639 if (!empty($CFG->enablegroupings) && !empty($wiki->groupingid)) {
640 $sql = 'SELECT w.id, w.userid, w.pagename, u.firstname, u.lastname '
655b09ca 641 .' FROM {wiki_entries} w '
642 .' INNER JOIN {user} u ON w.userid = u.id '
643 .' INNER JOIN {groups_members} gm ON gm.userid = u.id '
644 .' INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid '
645 .' WHERE w.wikiid = ? AND gg.groupingid = ?'
27978302 646 .' ORDER BY w.id';
655b09ca 647 $params = array($wiki->id, $wiki->groupingid);
27978302 648 } else {
649 $sql = 'SELECT w.id, w.userid, w.pagename, u.firstname, u.lastname '
655b09ca 650 .' FROM {wiki_entries} w, {user} u '
651 .' WHERE w.wikiid = ? AND u.id = w.userid '
27978302 652 .' ORDER BY w.id';
655b09ca 653 $params = array($wiki->id);
27978302 654 }
655b09ca 655 $wiki_entries = $DB->get_records_sql($sql, $params);
39fcb981 656 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
657 foreach ($wiki_entries as $wiki_entry) {
d0b95287 658 $key = 'view.php?id='.$id.'&userid='.$wiki_entry->userid.'&page='.$wiki_entry->pagename;
39fcb981 659 $wikis[$key] = fullname($wiki_entry).':'.$wiki_entry->pagename;
660 if ($currentid == $wiki_entry->id) {
661 $wikis['selected'] = $key;
662 }
663 }
8f0cd6ef 664 }
39fcb981 665 }
666 else {
667 /// A user can see other student wikis if they are a member of the same
668 /// group (for separate groups) or there are visible groups, or if this is
669 /// a site-level wiki, and they are an administrator.
1d126f4f 670 if (($groupmode == VISIBLEGROUPS) or wiki_is_teacheredit($wiki)) {
39fcb981 671 $viewall = true;
672 }
673 else if ($groupmode == SEPARATEGROUPS) {
674 $viewall = mygroupid($course->id);
675 }
676 else {
677 $viewall = false;
678 }
679
680 if ($viewall !== false) {
27978302 681 if (!empty($CFG->enablegroupings) && !empty($wiki->groupingid)) {
682 $sql = 'SELECT w.id, w.userid, w.pagename, u.firstname, u.lastname '
655b09ca 683 .' FROM {wiki_entries} w '
684 .' INNER JOIN {user} u ON w.userid = u.id '
685 .' INNER JOIN {groups_members} gm ON gm.userid = u.id '
686 .' INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid '
687 .' WHERE w.wikiid = ? AND gg.groupingid = ?'
27978302 688 .' ORDER BY w.id';
655b09ca 689 $params = array($wiki->id, $wiki->groupingid);
27978302 690 } else {
691 $sql = 'SELECT w.id, w.userid, w.pagename, u.firstname, u.lastname '
655b09ca 692 .' FROM {wiki_entries} w, {user} u '
693 .' WHERE w.wikiid = ? AND u.id = w.userid '
27978302 694 .' ORDER BY w.id';
655b09ca 695 $params = array($wiki->id);
27978302 696 }
655b09ca 697 $wiki_entries = $DB->get_records_sql($sql, $params);
39fcb981 698 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
699 foreach ($wiki_entries as $wiki_entry) {
27978302 700 if (!empty($CFG->enablegroupings) && !empty($wiki->groupingid) && empty($groupingmembers[$wiki_entry->userid])) {
701 continue;
702 }
703
2c386f82 704 if (($viewall === true) or groups_is_member($viewall, $wiki_entry->userid)) {
d0b95287 705 $key = 'view.php?id='.$id.'&userid='.$wiki_entry->userid.'&page='.$wiki_entry->pagename;
39fcb981 706 $wikis[$key] = fullname($wiki_entry).':'.$wiki_entry->pagename;
707 if ($currentid == $wiki_entry->id) {
708 $wikis['selected'] = $key;
709 }
710 }
711 }
712 }
713 }
714 break;
715
716 case 'group':
717 /// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all group
718 /// wikis, regardless of creation.
f84d6a8d 719
fa22fd5f 720 /// If user is a member of multiple groups, need to show current group etc?
39fcb981 721
722 /// Get all the existing entries for this wiki.
723 $wiki_entries = wiki_get_entries($wiki, 'group');
27978302 724
39fcb981 725 if ($groupmode and ($isteacheredit or ($isteacher and !$mygroupid))) {
27978302 726 if ($groups = groups_get_all_groups($course->id, null, $groupingid)) {
39fcb981 727 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
728 foreach ($groups as $group) {
729
730 /// If this group already has an entry, use its pagename.
731 if (isset($wiki_entries[$group->id])) {
732 $pagename = $wiki_entries[$group->id]->pagename;
733 }
734 else {
735 $pagename = $defpagename;
736 }
737
d0b95287 738 $key = 'view.php?id='.$id.($group->id?"&groupid=".$group->id:"").'&page='.$pagename;
39fcb981 739 $wikis[$key] = $group->name.':'.$pagename;
740 }
741 }
742 }
fa22fd5f 743 //if a studnet with multiple groups in SPG
744 else if ($groupmode == SEPARATEGROUPS){
27978302 745 if ($groups = groups_get_all_groups($course->id, $user->id, $groupingid)){
fa22fd5f 746
747 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
748 foreach ($groups as $group) {
749 /// If this group already has an entry, use its pagename.
750 if (isset($wiki_entries[$group->id])) {
751 $pagename = $wiki_entries[$group->id]->pagename;
752 }
753 else {
754 $pagename = $defpagename;
755 }
756 $key = 'view.php?id='.$id.($group->id?"&groupid=".$group->id:"").'&page='.$pagename;
757 $wikis[$key] = $group->name.':'.$pagename;
758 }
759
760 }
761
762 }
39fcb981 763 /// A user can see other group wikis if there are visible groups.
764 else if ($groupmode == VISIBLEGROUPS) {
27978302 765 if (!empty($CFG->enablegroupings) && !empty($wiki->groupingid)) {
766 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
655b09ca 767 .' FROM {wiki_entries} w '
768 .' INNER JOIN {groups} g ON g.id = w.groupid '
769 .' INNER JOIN {groupings_groups} gg ON g.id = gg.groupid '
770 .' WHERE w.wikiid = ? AND gg.groupingid = ?'
27978302 771 .' ORDER BY w.groupid';
655b09ca 772 $params = array($wiki->id, $wiki->groupingid);
27978302 773 } else {
774 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
655b09ca 775 .' FROM {wiki_entries} w, {groups} g '
776 .' WHERE w.wikiid = ? AND g.id = w.groupid '
27978302 777 .' ORDER BY w.groupid';
655b09ca 778 $params = array($wiki->id);
27978302 779 }
655b09ca 780 $wiki_entries = $DB->get_records_sql($sql, $params);
39fcb981 781 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
782 foreach ($wiki_entries as $wiki_entry) {
d0b95287 783 $key = 'view.php?id='.$id.($wiki_entry->groupid?"&groupid=".$wiki_entry->groupid:"").'&page='.$wiki_entry->pagename;
39fcb981 784 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
785 if ($currentid == $wiki_entry->id) {
786 $wikis['selected'] = $key;
787 }
788 }
789 }
790 break;
791
792 case 'teacher':
793 if ($isteacher) {
794 /// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all
795 /// teacher wikis, regardless of creation.
796 if ($groupmode and ($isteacheredit or ($isteacher and !$mygroupid))) {
27978302 797 if ($groups = groups_get_all_groups($course->id, null, $groupingid)) {
39fcb981 798 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
39fcb981 799 foreach ($groups as $group) {
800 /// If this group already has an entry, use its pagename.
801 if ($wiki_entries[$group->id]) {
802 $pagename = $wiki_entries[$group->id]->pagename;
803 }
804 else {
805 $pagename = $defpagename;
806 }
807
d0b95287 808 $key = 'view.php?id='.$id.($group->id?"&groupid=".$group->id:"").'&page='.$pagename;
39fcb981 809 $wikis[$key] = $group->name.':'.$pagename;
810 }
811 }
812 }
813 /// A teacher can see all other group teacher wikis.
814 else if ($groupmode) {
27978302 815 if (!empty($CFG->enablegroupings) && !empty($wiki->groupingid)) {
816 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
655b09ca 817 .' FROM {wiki_entries} w '
818 .' INNER JOIN {groups} g ON g.id = w.groupid '
819 .' INNER JOIN {groupings_groups} gg ON g.id = gg.groupid '
820 .' WHERE w.wikiid = ? AND gg.groupingid = ?'
27978302 821 .' ORDER BY w.groupid';
655b09ca 822 $params = array($wiki->id, $wiki->groupingid);
27978302 823 } else {
39fcb981 824 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
655b09ca 825 .' FROM {wiki_entries} w, {groups} g '
826 .' WHERE w.wikiid = ? AND g.id = w.groupid '
39fcb981 827 .' ORDER BY w.groupid';
655b09ca 828 $params = array($wiki->id);
27978302 829 }
655b09ca 830 $wiki_entries = $DB->get_records_sql($sql, $params);
39fcb981 831 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
832 foreach ($wiki_entries as $wiki_entry) {
d0b95287 833 $key = 'view.php?id='.$id.($wiki_entry->groupid?"&groupid=".$wiki_entry->groupid:"").'&page='.$wiki_entry->pagename;
39fcb981 834 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
835 if ($currentid == $wiki_entry->id) {
836 $wikis['selected'] = $key;
837 }
838 }
839 }
840 }
841 else {
842 /// A user can see other teacher wikis if they are a teacher, a member of the same
843 /// group (for separate groups) or there are visible groups.
844 if ($groupmode == VISIBLEGROUPS) {
845 $viewall = true;
846 }
847 else if ($groupmode == SEPARATEGROUPS) {
848 $viewall = $mygroupid;
849 }
850 else {
851 $viewall = false;
852 }
853 if ($viewall !== false) {
27978302 854 if (!empty($CFG->enablegroupings) && !empty($wiki->groupingid)) {
855 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
655b09ca 856 .' FROM {wiki_entries} w '
857 .' INNER JOIN {groups} g ON g.id = w.groupid '
858 .' INNER JOIN {groupings_groups} gg ON g.id = gg.groupid '
859 .' WHERE w.wikiid = ? AND gg.groupingid = ?'
27978302 860 .' ORDER BY w.groupid';
655b09ca 861 $params = array($wiki->id, $wiki->groupingid);
27978302 862 } else {
863 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
655b09ca 864 .' FROM {wiki_entries} w, {groups} g '
865 .' WHERE w.wikiid = ? AND g.id = w.groupid '
27978302 866 .' ORDER BY w.groupid';
655b09ca 867 $params = array($wiki->id);
27978302 868 }
655b09ca 869 $wiki_entries = $DB->get_records_sql($sql, $params);
39fcb981 870 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
fa22fd5f 871
872
39fcb981 873 foreach ($wiki_entries as $wiki_entry) {
fa22fd5f 874 if (($viewall === true) or @in_array($wiki_entry->groupid, $viewall)/*$viewall == $wiki_entry->groupid*/) {
875 $key = 'view.php?id='.$id.($wiki_entry->groupid?"&groupid=".$wiki_entry->groupid:"").'&page='.$wiki_entry->pagename;
39fcb981 876 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
877 if ($currentid == $wiki_entry->id) {
878 $wikis['selected'] = $key;
879 }
880 }
881 }
882 }
883 }
884 break;
885 }
27978302 886
39fcb981 887 return $wikis;
888}
889
890function wiki_add_entry(&$wiki, &$course, $userid=0, $groupid=0) {
891/// Adds a new wiki entry of the specified type, unless already entered.
892/// No checking is done here. It is assumed that the caller has the correct
893/// privileges to add this entry.
894
655b09ca 895 global $USER, $DB;
39fcb981 896
897 /// If this wiki already has a wiki_type entry, return false.
898 if (wiki_get_entry($wiki, $course, $userid, $groupid) !== false) {
899 return false;
900 }
901
ee3cc995 902 $wiki_entry = new Object();
903
39fcb981 904 switch ($wiki->wtype) {
905
906 case 'student':
907 $wiki_entry->wikiid = $wiki->id;
908 $wiki_entry->userid = $userid ? $userid : $USER->id;
909 $wiki_entry->pagename = wiki_page_name($wiki);
910 $wiki_entry->timemodified = time();
911 break;
912
913 case 'group':
914 /// Get the groupmode. It's been added to the wiki object.
27978302 915 $groupmode = groups_get_activity_groupmode($wiki);
39fcb981 916
fa22fd5f 917 ///give the first groupid by default and try
918 $mygroups = mygroupid($course->id);
919
39fcb981 920 /// If there is a groupmode, get the group id.
921 if ($groupmode) {
fa22fd5f 922 $groupid = $groupid ? $groupid : $mygroups[0]/*mygroupid($course->id)*/;
39fcb981 923 }
924 /// If mode is 'nogroups', then groupid is zero.
925 else {
926 $groupid = 0;
927 }
928 $wiki_entry->wikiid = $wiki->id;
929 $wiki_entry->groupid = $groupid;
930 $wiki_entry->pagename = wiki_page_name($wiki);
931 $wiki_entry->timemodified = time();
f84d6a8d 932
39fcb981 933 break;
934
935 case 'teacher':
936 /// Get the groupmode. It's been added to the wiki object.
27978302 937 $groupmode = groups_get_activity_groupmode($wiki);
39fcb981 938
939 /// If there is a groupmode, get the user's group id.
940 if ($groupmode and $groupid == 0) {
fa22fd5f 941 $mygroupid = mygroupid($course->id);
942 $groupid = $mygroupid[0]/*mygroupid($course->id)*/;
39fcb981 943 }
944
945 $wiki_entry->wikiid = $wiki->id;
946 $wiki_entry->course = $wiki->course;
947 $wiki_entry->groupid = $groupid;
948 $wiki_entry->pagename = wiki_page_name($wiki);
949 $wiki_entry->timemodified = time();
950 break;
951 }
655b09ca 952 $wiki_entry->pagename = $wiki_entry->pagename;
e61d4b39 953
655b09ca 954 return $DB->insert_record("wiki_entries", $wiki_entry, true);
39fcb981 955}
956
957function wiki_can_add_entry(&$wiki, &$user, &$course, $userid=0, $groupid=0) {
958/// Returns true or false if the user can add a wiki entry for this wiki.
959
960 /// Get the groupmode. It's been added to the wiki object.
27978302 961 $groupmode = groups_get_activity_groupmode($wiki);
39fcb981 962 $mygroupid = mygroupid($course->id);
39fcb981 963
964 switch ($wiki->wtype) {
965
966 case 'student':
967/// A student can create their own wiki, if they are a member of that course.
968/// A user can create their own wiki at the site level.
969 if ($userid == 0) {
ee3cc995 970 return (wiki_is_student($wiki, $user->id) or wiki_is_student($wiki, $user->id));
39fcb981 971 }
972/// An editing teacher can create any student wiki, or
973/// a non-editing teacher, if not assigned to a group can create any student wiki, or if assigned to a group can
974/// create any student wiki in their group.
975 else {
ee3cc995 976 return ((($userid == $user->id) and wiki_is_student($wiki, $user->id)) or wiki_is_teacheredit($wiki) or
2c386f82 977 (wiki_is_teacher($wiki) and (!$groupmode or $mygroupid == 0 or (groups_is_member($mygroupid, $userid)))));
39fcb981 978 }
979 break;
980
981 case 'group':
982 /// If mode is 'nogroups', then all participants can add wikis.
b82fcee1 983 if (wiki_is_teacheredit($wiki, $user->id)) {
f84d6a8d 984 return true;
b82fcee1 985 }
f84d6a8d 986
39fcb981 987 if (!$groupmode) {
ee3cc995 988 return (wiki_is_student($wiki, $user->id) or wiki_is_teacher($wiki, $user->id));
39fcb981 989 }
990 /// If not requesting a group, must be a member of a group.
991 else if ($groupid == 0) {
8f0cd6ef 992 return ($mygroupid != 0);
39fcb981 993 }
994 /// If requesting a group, must be an editing teacher, a non-editing teacher with no assigned group,
fa22fd5f 995 /// or a non-editing teacher requesting their group. or a student in group, but wiki is empty.
39fcb981 996 else {
ee3cc995 997 return (wiki_is_teacheredit($wiki) or
998 (wiki_is_teacher($wiki) and ($mygroupid == 0 or @in_array($groupid, $mygroupid))) or
999 (wiki_is_student($wiki, $user->id) and @in_array($groupid, $mygroupid))
fa22fd5f 1000 );
39fcb981 1001 }
1002 break;
1003
1004 case 'teacher':
1005 /// If mode is 'nogroups', then all teachers can add wikis.
1006 if (!$groupmode) {
1d126f4f 1007 return wiki_is_teacher($wiki, $user->id);
39fcb981 1008 }
1009 /// If not requesting a group, must be a member of a group.
1010 else if ($groupid == 0) {
ee3cc995 1011 return ($mygroupid != 0 and wiki_is_teacher($wiki));
39fcb981 1012 }
8f0cd6ef 1013 /// If there is a group mode, non-editing teachers with an assigned group, can only create wikis
39fcb981 1014 /// in their group. Non-editing teachers with no assigned group and editing teachers can create any wiki.
1015 else {
ee3cc995 1016 return (wiki_is_teacheredit($wiki) or
1017 (wiki_is_teacher($wiki) and ($mygroupid == 0 or @in_array($groupid, $mygroupid))));
39fcb981 1018 }
1019 break;
1020 }
1021
1022 return false;
1023}
1024
1025function wiki_can_edit_entry(&$wiki_entry, &$wiki, &$user, &$course) {
1026/// Returns true or false if the user can edit this wiki entry.
1027
1028 $can_edit = false;
27978302 1029 $groupmode = groups_get_activity_groupmode($wiki);
39fcb981 1030 $mygroupid = mygroupid($course->id);
39fcb981 1031
8f0cd6ef 1032 /// Editing teacher's and admins can edit all wikis, non-editing teachers can edit wikis in their groups,
39fcb981 1033 /// or all wikis if group mode is 'no groups' or they don't belong to a group.
1d126f4f 1034 if (wiki_is_teacheredit($wiki, $user->id) or
ee3cc995 1035 ((!$groupmode or $mygroupid == 0) and wiki_is_teacher($wiki, $user->id))) {
39fcb981 1036 $can_edit = true;
1037 }
1038 else {
1039 switch ($wiki->wtype) {
1040
1041 /// Only a teacher or the owner of a student wiki can edit it.
1042 case 'student':
1043 $can_edit = (($user->id == $wiki_entry->userid) or
ee3cc995 1044 ($groupmode and wiki_is_teacher($wiki, $user->id) and
2c386f82 1045 groups_is_member($mygroupid, $wiki_entry->userid)));
39fcb981 1046 break;
1047
1048 case 'group':
1049 /// If there is a groupmode, determine the user's group status.
1050 if ($groupmode) {
1051 /// If the user is a member of the wiki group, they can edit the wiki.
2c386f82 1052 $can_edit = groups_is_member($wiki_entry->groupid, $user->id);
39fcb981 1053 }
1054 /// If mode is 'nogroups', then all participants can edit the wiki.
1055 else {
ee3cc995 1056 $can_edit = (wiki_is_student($wiki, $user->id) or wiki_is_teacher($wiki, $user->id));
39fcb981 1057 }
1058 break;
1059
1060 case 'teacher':
1061 /// If there is a groupmode, determine the user's group status.
1062 if ($groupmode) {
1063 /// If the user is a member of the wiki group, they can edit the wiki.
2c386f82 1064 $can_edit = (wiki_is_teacher($wiki, $user->id) and groups_is_member($wiki_entry->groupid, $user->id));
39fcb981 1065 }
1066 else {
1d126f4f 1067 $can_edit = wiki_is_teacher($wiki, $user->id);
39fcb981 1068 }
1069 break;
1070 }
1071 }
39fcb981 1072 return $can_edit;
1073}
1074
1075function wiki_user_can_access_student_wiki(&$wiki, $userid, &$course) {
1076 global $USER;
1077
1078 /// Get the groupmode. It's been added to the wiki object.
27978302 1079 $groupmode = groups_get_activity_groupmode($wiki);
39fcb981 1080 $usersgroup = mygroupid($course->id);
ee3cc995 1081 $isteacher = wiki_is_teacher($wiki, $USER->id);
39fcb981 1082
1083 /// If this user is allowed to access this wiki then return TRUE.
1084 /// *** THIS COULD BE A PROBLEM, IF STUDENTS COULD EVER BE PART OF MORE THAN ONE GROUP ***
1085 /// A user can access a student wiki, if:
1086 /// - it is their wiki,
1087 /// - group mode is VISIBLEGROUPS,
1088 /// - group mode is SEPARATEGROUPS, and the user is a member of the requested user's group,
1089 /// - they are an editing teacher or administrator,
1090 /// - they are a non-editing teacher not assigned to a specific group,
1091 /// - they are a non-editing teacher and group mode is NOGROUPS.
1092 /// - they are an administrator (mostly for site-level wikis).
1093 if (($userid and ($USER->id == $userid)) or ($groupmode == VISIBLEGROUPS) or
2c386f82 1094 (($groupmode == SEPARATEGROUPS) and groups_is_member($usersgroup, $userid)) or
ee3cc995 1095 (wiki_is_teacheredit($wiki, $USER->id)) or
1d126f4f 1096 (wiki_is_teacher($wiki, $USER->id) and (!$usersgroup or $groupmode == NOGROUPS))) {
39fcb981 1097 $can_access = true;
1098 }
1099 else {
1100 $can_access = false;
1101 }
1102 return $can_access;
1103}
1104
1105function wiki_user_can_access_group_wiki(&$wiki, $groupid, &$course) {
1106 global $USER;
1107
1108 /// Get the groupmode. It's been added to the wiki object.
27978302 1109 $groupmode = groups_get_activity_groupmode($wiki);
39fcb981 1110 $usersgroup = mygroupid($course->id);
ee3cc995 1111 $isteacher = wiki_is_teacher($wiki, $USER->id);
39fcb981 1112
1113 /// A user can access a group wiki, if:
1114 /// - group mode is NOGROUPS,
1115 /// - group mode is VISIBLEGROUPS,
1116 /// - group mode is SEPARATEGROUPS, and they are a member of the requested group,
1117 /// - they are an editing teacher or administrator,
1118 /// - they are a non-editing teacher not assigned to a specific group.
8f0cd6ef 1119 if (($groupmode == NOGROUPS) or ($groupmode == VISIBLEGROUPS) or
fa22fd5f 1120 (($groupmode == SEPARATEGROUPS) and @in_array($groupid, $usersgroup)/*($usersgroup == $groupid)*/) or
ee3cc995 1121 (wiki_is_teacheredit($wiki, $USER->id)) or
1122 (wiki_is_teacher($wiki, $USER->id) and !$usersgroup)) {
39fcb981 1123 $can_access = true;
1124 }
1125 else {
1126 $can_access = false;
1127 }
1128 return $can_access;
1129}
1130
1131function wiki_user_can_access_teacher_wiki(&$wiki, $groupid, &$course) {
1132 global $USER;
1133
1134 /// Get the groupmode. It's been added to the wiki object.
27978302 1135 $groupmode = groups_get_activity_groupmode($wiki);
39fcb981 1136
1137 /// A user can access a teacher wiki, if:
1138 /// - group mode is NOGROUPS,
1139 /// - group mode is VISIBLEGROUPS,
1140 /// - group mode is SEPARATEGROUPS, and they are a member of the requested group,
1141 /// - they are a teacher or administrator,
8f0cd6ef 1142 if (($groupmode == NOGROUPS) or ($groupmode == VISIBLEGROUPS) or
fa22fd5f 1143 (($groupmode == SEPARATEGROUPS) and (@in_array($groupid, mygroupid($course->id))/*mygroupid($course->id) == $groupid*/)) or
ee3cc995 1144 (wiki_is_teacher($wiki, $USER->id))){
39fcb981 1145 $can_access = true;
1146 }
1147 else {
1148 $can_access = false;
1149 }
1150 return $can_access;
1151}
1152
1153function wiki_get_owner(&$wiki_entry) {
655b09ca 1154 global $DB;
1155
39fcb981 1156 if ($wiki_entry->userid > 0) {
655b09ca 1157 $user = $DB->get_record('user', array('id'=>$wiki_entry->userid));
39fcb981 1158 $owner = fullname($user);
1159 }
1160 else if ($wiki_entry->groupid > 0) {
f3f7610c 1161 $owner = groups_get_group_name($wiki_entry->groupid); //TODO:check.
39fcb981 1162 }
1163 else if ($wiki_entry->course > 0) {
655b09ca 1164 $course = $DB->get_record('course', array('id'=>$wiki_entry->course));
39fcb981 1165 $owner = $course->shortname;
1166 }
1167 else {
a9de0242 1168 $owner = '- '.get_string("ownerunknown","wiki").' -';
39fcb981 1169 }
1170 return $owner;
1171}
1172
1173function wiki_print_search_form($cmid, $search="", $userid, $groupid, $return=false) {
1174 global $CFG;
1175 # TODO: Add Group and User !!!
b7dc2256 1176 $output = "<form id=\"search\" action=\"$CFG->wwwroot/mod/wiki/view.php\">";
beae1797 1177 $output .="<fieldset class='invisiblefieldset'>";
1178 $output .= "<span style='font-size:0.6em;'>";
a9ef4a63 1179 $output .= "<input value=\"".get_string("searchwiki", "wiki").":\" type=\"submit\" />";
1180 $output .= "<input name=\"id\" type=\"hidden\" value=\"$cmid\" />";
1181 $output = $output.($groupid?"<input name=\"groupid\" type=\"hidden\" value=\"$groupid\" />":"");
1182 $output = $output.($userid?"<input name=\"userid\" type=\"hidden\" value=\"$userid\" />":"");
4c2740fc 1183 $output .= "<input name=\"q\" type=\"text\" size=\"20\" value=\"".s($search)."\" />".' ';
beae1797 1184 $output .= "</span>";
a9ef4a63 1185 $output .= "<input name=\"page\" type=\"hidden\" value=\"SearchPages\" />";
beae1797 1186 $output .= "</fieldset>";
39fcb981 1187 $output .= "</form>";
1188
1189 if ($return) {
1190 return $output;
1191 }
1192 echo $output;
1193}
1194
1195function wiki_print_wikilinks_block($cmid, $binary=false, $return=false) {
087fef3e 1196/// Prints a link-list of special wiki-pages
39fcb981 1197 global $CFG, $ewiki_title;
1198
1199 $links=array();
1200
f80b0733 1201 $links["SiteMap"]=get_string("sitemap", "wiki");
39fcb981 1202 $links["PageIndex"]=get_string("pageindex", "wiki");
1203 $links["NewestPages"]=get_string("newestpages", "wiki");
1204 $links["MostVisitedPages"]=get_string("mostvisitedpages", "wiki");
1205 $links["MostOftenChangedPages"]=get_string("mostoftenchangedpages", "wiki");
1206 $links["UpdatedPages"]=get_string("updatedpages", "wiki");
1207 $links["OrphanedPages"]=get_string("orphanedpages", "wiki");
1208 $links["WantedPages"]=get_string("wantedpages", "wiki");
a9de0242 1209 $links["WikiExport"]=get_string("wikiexport", "wiki");
39fcb981 1210 if($binary) {
1211 $links["FileDownload"]=get_string("filedownload", "wiki");
1212 }
8f0cd6ef 1213 popup_form(EWIKI_SCRIPT, $links, "wikilinks", "", get_string("choosewikilinks", "wiki"), "", "", $return);
39fcb981 1214}
1215
087fef3e 1216function wiki_print_page_actions($cmid, $specialpages, $page, $action, $binary=false, $canedit=true) {
39fcb981 1217/// Displays actions which can be performed on the page
1218
1219 $page=array();
1220
1221 // Edit this Page
1222 if (in_array($action, array("edit", "links", "info", "attachments"))) {
8f0cd6ef 1223 $page["view/$page"]=get_string("viewpage","wiki");
39fcb981 1224 }
087fef3e 1225 if ($canedit && !in_array($page, $specialpages) && $action != "edit") {
1226 $page["edit/$page"]=get_string("editthispage","wiki");
39fcb981 1227 }
1228 if ($action != "links") {
087fef3e 1229 $page["links/$page"]=get_string("backlinks","wiki");
39fcb981 1230 }
087fef3e 1231 if ($canedit && !in_array($page, $specialpages) && $action!="info") {
1232 $page["info/$page"]=get_string("pageinfo","wiki");
39fcb981 1233 }
087fef3e 1234 if($canedit && $binary && !in_array($page, $specialpages) && $action != "attachments") {
1235 $page["attachments/$page"]=get_string("attachments","wiki");
39fcb981 1236 }
1237
8f0cd6ef 1238 popup_form(EWIKI_SCRIPT, $page, "wikiactions", "", get_string("action", "wiki"), "", "", false);
39fcb981 1239}
1240
087fef3e 1241function wiki_print_administration_actions($wiki, $cmid, $userid, $groupid, $page, $noeditor, $course) {
39fcb981 1242/// Displays actions which can be performed on the page
f84d6a8d 1243
1244 /// Create the URL
39fcb981 1245 $ewscript = 'admin.php?id='.$cmid;
839f2456 1246 if (isset($userid) && $userid!=0) $ewscript .= '&amp;userid='.$userid;
1247 if (isset($groupid) && $groupid!=0) $ewscript .= '&amp;groupid='.$groupid;
1248 if (isset($page)) $ewscript .= '&amp;page='.$page;
1249 $ewscript.="&amp;action=";
39fcb981 1250
c513f599 1251
1252 /// Build that action array according to wiki flags.
1253 $action = array();
ee3cc995 1254 $isteacher = wiki_is_teacher($wiki);
c513f599 1255
1256 if ($wiki->setpageflags or $isteacher) {
1257 $action['setpageflags'] = get_string('setpageflags', 'wiki');
1258 }
1259 if ($wiki->removepages or $isteacher) {
1260 $action['removepages'] = get_string('removepages', 'wiki');
1261 }
1262 if ($wiki->strippages or $isteacher) {
1263 $action['strippages'] = get_string('strippages', 'wiki');
1264 }
1265 if ($wiki->revertchanges or $isteacher) {
1266 $action['revertpages'] = get_string('revertpages', 'wiki');
1267 }
1268
39fcb981 1269 if($noeditor) {
1270 $action["checklinks"]=get_string("checklinks", "wiki");
1271 }
8f0cd6ef 1272 popup_form($ewscript, $action, "wikiadministration", "", get_string("chooseadministration", "wiki"), "", "", false);
39fcb981 1273}
1274
1275function wiki_admin_get_flagarray() {
1276 $ret = array(
1277 EWIKI_DB_F_TEXT => get_string("flagtxt","wiki"),
1278 EWIKI_DB_F_BINARY => get_string("flagbin","wiki"),
1279 EWIKI_DB_F_DISABLED => get_string("flagoff","wiki"),
1280 EWIKI_DB_F_HTML => get_string("flaghtm","wiki"),
1281 EWIKI_DB_F_READONLY => get_string("flagro","wiki"),
1282 EWIKI_DB_F_WRITEABLE => get_string("flagwr","wiki"),
1283 );
8f0cd6ef 1284
39fcb981 1285 return $ret;
1286}
1287
1288///////// Ewiki Administration. Mostly taken from the ewiki/tools folder and changed
1289function wiki_admin_setpageflags_list($pageflagstatus) {
8f0cd6ef 1290 $FD = wiki_admin_get_flagarray();
ee3cc995 1291 $table = new Object();
39fcb981 1292 $table->head = array(get_string("pagename","wiki"), get_string("flags","wiki"));
1293 if($pageflagstatus) {
1294 $table->head[]=get_string("status","wiki");
1295 }
8f0cd6ef 1296
39fcb981 1297 $result = ewiki_database("GETALL", array("version", "flags"));
1298 while ($row = $result->get()) {
1299 $id = $row["id"];
1300 $data = ewiki_database("GET", $row);
1301
1302 $cell_pagename="";
1303 $cell_flags="";
1304 if ($data["flags"] & EWIKI_DB_F_TEXT) {
b1503075 1305 $cell_pagename .= '<a href="' . EWIKI_SCRIPT . $id . '">';
39fcb981 1306 } else {
b1503075 1307 $cell_pagename .= '<a href="' . EWIKI_SCRIPT_BINARY . $id . '">';
39fcb981 1308 }
b1503075 1309 $cell_pagename .= s($id) . '</a> / '.get_string("version","wiki").": ".$row["version"];
8f0cd6ef 1310
39fcb981 1311 foreach ($FD as $n=>$str) {
b1503075 1312 $cell_flags .='<input type="checkbox" name="flags['. rawurlencode($id)
1313 . '][' . $n . ']" value="1" '
82056f67 1314 . (($data["flags"] & $n) ? "checked=\"checked\"" : "")
a9ef4a63 1315 . ' />'.$str. ' ';
39fcb981 1316 }
1317 if($pageflagstatus) {
1318 $table->data[]=array($cell_pagename, $cell_flags, $pageflagstatus[$id]);
1319 } else {
1320 $table->data[]=array($cell_pagename, $cell_flags);
1321 }
1322 }
1323 return $table;
1324}
1325
1326function wiki_admin_setpageflags($pageflags) {
1327 $FD = wiki_admin_get_flagarray();
8f0cd6ef 1328
39fcb981 1329 $status=array();
8f0cd6ef 1330 if($pageflags) {
39fcb981 1331 foreach($pageflags as $page=>$fa) {
1332
1333 $page = rawurldecode($page);
1334
1335 $flags = 0;
1336 $fstr = "";
1337 foreach($fa as $num=>$isset) {
1338 if ($isset) {
1339 $flags += $num;
1340 $fstr .= ($fstr?",":""). $FD[$num];
1341 }
1342 }
1343
1344 #$status[$page] .= "{$flags}=[{$fstr}]";
1345
1346 $data = ewiki_database("GET", array("id" => $page));
1347
1348 if ($data["flags"] != $flags) {
1349 $data["flags"] = $flags;
1350 $data["author"] = "ewiki-tools, " . ewiki_author();
1351 $data["version"]++;
1352 ewiki_database("WRITE", $data);
1353 $status[$page] = "<b>".get_string("flagsset","wiki")."</b> ".$status[$page];
1354 }
1355 }
1356 }
8f0cd6ef 1357 return $status;
39fcb981 1358}
1359
1360
1361function wiki_admin_remove_list($listall="") {
1362 /// Table header
ee3cc995 1363 $table = new Object();
39fcb981 1364 $table->head = array("&nbsp;", get_string("pagename","wiki"), get_string("errororreason","wiki"));
8f0cd6ef 1365
39fcb981 1366 /// Get all pages
1367 $result = ewiki_database("GETALL", array("version"));
1368 $selected = array();
1369
1370 /// User wants to see all pages
1371 if ($listall) {
1372 while ($row = $result->get()) {
a9ef4a63 1373 $selected[$row["id"]] = get_string("listall","wiki")."<br />";
39fcb981 1374 }
1375 }
8f0cd6ef 1376 while ($page = $result->get()) {
39fcb981 1377 $id = $page["id"];
1378 $page = ewiki_database("GET", array("id"=>$id));
1379 $flags = $page["flags"];
8f0cd6ef 1380 #print "$id ".strlen(trim(($page["content"])))."<br />";
39fcb981 1381
1382 if (!strlen(trim(($page["content"]))) && !($flags & EWIKI_DB_F_BINARY)) {
a9ef4a63 1383 @$selected[$id] .= get_string("emptypage","wiki")."<br />";
39fcb981 1384 }
8f0cd6ef 1385
39fcb981 1386 // Check for orphaned pages
1387 $result2 = ewiki_database("SEARCH", array("content" => $id));
1388 $orphanedpage=true;
1389 if ($result2 && $result2->count()) {
1390 while ($row = $result2->get()) {
1391 $checkcontent = ewiki_database("GET", array("id"=>$row["id"]));
1392 $checkcontent = strtolower($checkcontent["content"]);
8f0cd6ef 1393
39fcb981 1394 if(strpos($checkcontent, strtolower($id)) !== false) {
1395 $orphanedpage=false;
1396 }
1397
a9ef4a63 1398 #echo "rc({$row['id']})==>($id): $check2 <br />";
39fcb981 1399 }
1400 }
1401
8f0cd6ef 1402 /// Some more reasons for Deletion...
39fcb981 1403 if ($orphanedpage && $id!=EWIKI_PAGE_INDEX &&!($flags & EWIKI_DB_F_BINARY)) {
a9ef4a63 1404 @$selected[$id] .= get_string("orphanedpage","wiki")."<br />";
39fcb981 1405 }
1406
1407 if ($flags & EWIKI_DB_F_DISABLED) {
a9ef4a63 1408 @$selected[$id] .= get_string("disabledpage","wiki")."<br />";
39fcb981 1409 }
1410
1411 if (($flags & 3) == 3) {
a9ef4a63 1412 @$selected[$id] .= get_string("errorbinandtxt","wiki")."<br />";
39fcb981 1413 }
1414
1415 if (!($flags & 3)) {
a9ef4a63 1416 @$selected[$id] .= get_string("errornotype","wiki")."<br />";
39fcb981 1417 }
1418
1419 if ($flags & EWIKI_DB_F_HTML) {
a9ef4a63 1420 @$selected[$id] .= get_string("errorhtml","wiki")."<br />";
39fcb981 1421 }
1422
1423 if (($flags & EWIKI_DB_F_READONLY) && !($flags & EWIKI_DB_F_BINARY)) {
a9ef4a63 1424 @$selected[$id] .= get_string("readonly","wiki")."<br />";
39fcb981 1425 }
1426
1427 if (($flags & EWIKI_DB_F_READONLY) && ($flags & EWIKI_DB_F_WRITEABLE)) {
a9ef4a63 1428 @$selected[$id] .= get_string("errorroandwr","wiki")."<br />";
39fcb981 1429 }
1430
1431 if (strlen($page["content"]) >= 65536) {
a9ef4a63 1432 @$selected[$id] .= get_string("errorsize","wiki")."<br />";
39fcb981 1433 }
1434
1435 if (strpos($page["refs"], "\n".get_string("deletemewikiword","wiki")."\n")!==false) {
a9ef4a63 1436 @$selected[$id] .= get_string("deletemewikiwordfound","wiki",get_string("deletemewikiword","wiki"))."<br />";
39fcb981 1437 }
1438 }
8f0cd6ef 1439
1440 foreach ($selected as $id => $reason) {
b1503075 1441 $table_checkbox='<input type="checkbox" value="'.rawurlencode($id).'" name="pagestodelete[]" />';
39fcb981 1442
1443 #-- link & id
1444 if (strpos($id, EWIKI_IDF_INTERNAL) === false) {
b1503075 1445 $table_page='<a href="' . ewiki_script("", $id) . '">';
39fcb981 1446 } else {
b1503075 1447 $table_page='<a href="' . ewiki_script_binary("", $id) . '">';
39fcb981 1448 }
b1503075 1449 $table_page .= s($id) . '</a>';
39fcb981 1450
1451 #-- print reason
1452 $table_reason=$reason;
8f0cd6ef 1453
39fcb981 1454 $table->data[]=array($table_checkbox, $table_page, $table_reason);
8f0cd6ef 1455 }
1456
1457 return $table;
39fcb981 1458}
1459
1460/// This function actually removes the pages
1461function wiki_admin_remove($pagestodelete, $course, $wiki, $userid, $groupid) {
8f0cd6ef 1462 $ret="";
39fcb981 1463 foreach ($pagestodelete as $id) {
1464
1465 $id = rawurldecode($id);
1466
1467 $data = ewiki_database("GET", array("id"=>$id));
1468 for ($version=1; $version<=$data["version"]; $version++) {
1469 ewiki_database("DELETE", array("id"=>$id, "version"=>$version));
1470 if($data["flags"] & EWIKI_DB_F_BINARY) {
1471 $filepath=moodle_binary_get_path($id, $data["meta"], $course, $wiki, $userid, $groupid);
1472 @unlink("$filepath");
1473 }
1474 }
8f0cd6ef 1475
39fcb981 1476 }
1477 return $ret;
1478}
1479
1480function wiki_admin_strip_list($pagestostrip="",$version="",$err="") {
1481 /// Table header
ee3cc995 1482 $table = new Object();
39fcb981 1483 $table->head = array("&nbsp;", get_string("pagename","wiki"), get_string("deleteversions","wiki"));
8f0cd6ef 1484
39fcb981 1485 $vc=ewiki_database("COUNTVERSIONS", array());
1486 $result = ewiki_database("GETALL",array());
1487 $i=0;
1488 while ($row = $result->get()) {
1489 $id = $row["id"];
1490 if($vc[$id]>1) {
1491 $error="";
1492 if($err[$id]) {
1493 $error=" ".join(", ",$err[$id]);
8f0cd6ef 1494 }
39fcb981 1495 $checked="";
1496 if($pagestostrip=="" || $pagestostrip[$i]) {
82056f67 1497 $checked=" checked=\"checked\"";
39fcb981 1498 }
1499 if($version=="") {
1500 $versiondefault="1-".($row["version"]-1);
1501 } else {
1502 $versiondefault=$version[$i];
1503 }
8f0cd6ef 1504 $table->data[]=array('<input type="checkbox" value="'.rawurlencode($id).'" name="pagestostrip['.$i.']" '.$checked.' />',
2bc0b7a0 1505 '<A HREF="'.EWIKI_SCRIPT.$id.'">'.s($id).'</A> / '.get_string("version","wiki").": ".$row["version"],
a9ef4a63 1506 '<input name="version['.$i.']" value="'.$versiondefault.'" size="7" />'.$error);
8f0cd6ef 1507
39fcb981 1508 }
1509 $i++;
1510 }
1511 return $table;
1512}
1513
1514function wiki_admin_strip_versions($pagestostrip, $version, &$err) {
1515 $ret=array();
1516 foreach ($pagestostrip as $key => $id_ue) {
8f0cd6ef 1517
39fcb981 1518 $id = rawurldecode($id_ue);
1519 if (preg_match('/^(\d+)[-\s._:]+(\d+)$/', trim($version[$key]), $uu)) {
1520 $versA = $uu[1];
1521 $versZ = $uu[2];
8f0cd6ef 1522
39fcb981 1523 // Let the last Version in the database
d2481a47 1524 $checkdata = ewiki_database("GET", array("id" => $id));
39fcb981 1525 if($versZ>=$checkdata["version"]) {
8f0cd6ef 1526 $err[$id][] = get_string("versionrangetoobig","wiki");
39fcb981 1527 } else {
1528 if($versA<=$versZ) {
1529 for ($v=$versA; $v<=$versZ; $v++) {
1530 $ret[$id][]=$v;
1531 }
1532 } else {
1533 $err[$id][]=get_string("wrongversionrange","wiki",$version[$key]);
1534 }
1535 }
1536 }
1537 else {
1538 $err[$id][]=get_string("wrongversionrange","wiki",$version[$key]);
1539 }
1540 }
1541 return $ret;
1542}
8f0cd6ef 1543
39fcb981 1544function wiki_admin_strip($pagestostrip) {
1545 /// Purges old page-versions
1546 foreach($pagestostrip as $id => $versions) {
1547 foreach($versions as $version) {
1548 ewiki_database("DELETE", array("id"=>$id, "version"=>$version));
1549 }
1550 }
8f0cd6ef 1551}
39fcb981 1552
1553function wiki_admin_checklinks_list() {
1554 $ret=array();
1555 $result = ewiki_database("GETALL",array());
1556 while ($row = $result->get()) {
1557 if(!($row["flags"] & EWIKI_DB_F_BINARY)) {
2bc0b7a0 1558 $index=s($row["id"]);
39fcb981 1559 $ret[$index] = $row["id"];
1560 }
1561 }
1562 return $ret;
1563}
1564
1565function wiki_admin_checklinks($pagetocheck) {
1566 /// Checks http:// Links
1567 $ret="";
1568 if($pagetocheck) {
1569 $get = ewiki_database("GET", array("id" => $pagetocheck));
1570 $content = $get["content"];
8f0cd6ef 1571
a9de0242 1572 preg_match_all('_(http.?://[^\s"\'<>#,;]+[^\s"\'<>#,;.])_', $content, $links);
39fcb981 1573 $badlinks = array();
1574 if(!$links[1]) {
a9ef4a63 1575 $ret = get_string("nolinksfound","wiki")."<br /><br />";
39fcb981 1576 } else {
1577 foreach ($links[1] as $href) {
1578 #print "[ $href ]";
8f0cd6ef 1579 #$d = @implode("", @file($href));
39fcb981 1580 $d="";
1581 if($checkfd = @fopen($href, 'r')) {
1582 fclose($checkfd);
1583 $d="OK";
1584 }
1585 if (empty($d) || !strlen(trim($d)) || stristr("not found", $d) || stristr("error 404", $d)) {
a9ef4a63 1586 $ret.="[".get_string("linkdead","wiki")."] $href <br />\n";
39fcb981 1587 $badlinks[] = $href;
1588 } else {
a9ef4a63 1589 $ret.="[".get_string("linkok","wiki")."] $href <br />\n";
39fcb981 1590 }
8f0cd6ef 1591 }
39fcb981 1592 }
8f0cd6ef 1593
39fcb981 1594 /// Remove old Notices
ee3cc995 1595 $content = eregi_replace(' µµ__~\['.get_string("offline","wiki").'\]__µµ ','', $content);
39fcb981 1596
1597 #-- replace dead links
1598 foreach ($badlinks as $href) {
ee3cc995 1599 $content = preg_replace("\377^(.*)($href)\377m", '$1 µµ__~['.get_string("offline","wiki").']__µµ $2', $content);
39fcb981 1600 }
1601
1602 #-- compare against db content
1603 if ($content != $get["content"]) {
1604 $get["content"] = $content;
1605 $get["version"]++;
1606 $get["author"] = ewiki_author("ewiki_checklinks");
1607 $get["lastmodified"] = time();
1608
1609 ewiki_database("WRITE", $get);
1610 }
1611 }
1612 return $ret;
1613}
1614
1615function wiki_admin_revert($proceed, $authorfieldpattern, $changesfield, $howtooperate, $deleteversions) {
1616 $ret="";
1617 #-- params
1618 $m_time = $changesfield * 3600;
1619 $depth = $deleteversions - 1;
1620 $depth = ($depth>0?$depth:0);
8f0cd6ef 1621
39fcb981 1622 #-- walk through
1623 $result = ewiki_database("GETALL", array("id", "author", "lastmodified"));
1624 while ($row = $result->get()) {
1625 $id = $row["id"];
1626 #-- which versions to check
1627 $verZ = $row["version"];
1628 if ($howtooperate=="lastonly") {
1629 $verA = $verZ;
1630 }
1631 else {
1632 $verA = $verZ-$depth;
8f0cd6ef 1633 if ($verA <= 0) {
39fcb981 1634 $verA = 1;
1635 }
1636 }
1637
1638 for ($ver=$verA; $ver<=$verZ; $ver++) {
1639 #-- load current $ver database entry
1640 if ($verA != $verZ) {
1641 $row = ewiki_database("GET", array("id"=>$id, "version"=>$ver));
1642 }
1643
1644 #-- match
1645 if (stristr($row["author"], $authorfieldpattern) && ($row["lastmodified"] + $m_time > time())) {
1646 $ret .= "$id (".get_string("versionstodelete","wiki").": ";
1647 #-- delete multiple versions
1648 if ($howtooperate=="allsince") {
1649 while ($ver<=$verZ) {
1650 $ret .= " $ver";
1651 if ($proceed) {
1652 ewiki_database("DELETE", array("id"=>$id, "version"=>$ver));
1653 }
1654 $ver++;
1655 }
1656 }
1657 #-- or just the affected one
1658 else {
1659 $ret .= " $ver";
1660 if ($proceed) {
1661 ewiki_database("DELETE", $row);
1662 }
1663 }
a9ef4a63 1664 $ret .= ")<br />";
39fcb981 1665 break;
1666 }
1667 } #-- for($ver)
1668 } #-- while($row)
1669 return $ret;
1670}
f3221af9 1671
1672
1673function wiki_get_view_actions() {
1674 return array('view','view all');
1675}
1676
1677function wiki_get_post_actions() {
1678 return array('hack');
1679}
1680
1681
4db1861a 1682/**
1683 * Obtains an editing lock on a wiki page.
1684 * @param int $wikiid ID of wiki object.
1685 * @param string $pagename Name of page.
1686 * @return array Two-element array with a boolean true (if lock has been obtained)
1687 * or false (if lock was held by somebody else). If lock was held by someone else,
6022c4f5 1688 * the values of the wiki_locks entry are held in the second element; if lock was
1689 * held by current user then the the second element has a member ->id only.
4db1861a 1690 */
1691function wiki_obtain_lock($wikiid,$pagename) {
655b09ca 1692 global $USER, $DB;
f84d6a8d 1693
2ee60b49 1694 // Check for lock
4db1861a 1695 $alreadyownlock=false;
655b09ca 1696 if($lock=$DB->get_record('wiki_locks', array('pagename'=>$pagename,'wikiid'=>$wikiid))) {
4db1861a 1697 // Consider the page locked if the lock has been confirmed within WIKI_LOCK_PERSISTENCE seconds
1698 if($lock->lockedby==$USER->id) {
1699 // Cool, it's our lock, do nothing except remember it in session
1700 $lockid=$lock->id;
1701 $alreadyownlock=true;
1702 } else if(time()-$lock->lockedseen < WIKI_LOCK_PERSISTENCE) {
2ee60b49 1703 return array(false,$lock);
4db1861a 1704 } else {
1705 // Not locked any more. Get rid of the old lock record.
655b09ca 1706 if(!$DB->delete_records('wiki_locks', array('pagename'=>$pagename,'wikiid'=>$wikiid))) {
4d6ba0f0 1707 print_error('cannotdeletelockrecored', 'wiki');
4db1861a 1708 }
f84d6a8d 1709 }
4db1861a 1710 }
f84d6a8d 1711
4db1861a 1712 // Add lock
f84d6a8d 1713 if(!$alreadyownlock) {
2ee60b49 1714 // Lock page
1715 $newlock=new stdClass;
1716 $newlock->lockedby=$USER->id;
1717 $newlock->lockedsince=time();
1718 $newlock->lockedseen=$newlock->lockedsince;
1719 $newlock->wikiid=$wikiid;
1720 $newlock->pagename=$pagename;
655b09ca 1721 if(!$lockid=$DB->insert_record('wiki_locks',$newlock)) {
4d6ba0f0 1722 print_error('cannotinsertlockrecored', 'wiki');
2ee60b49 1723 }
4db1861a 1724 }
f84d6a8d 1725
4db1861a 1726 // Store lock information in session so we can clear it later
1727 if(!array_key_exists(SESSION_WIKI_LOCKS,$_SESSION)) {
2ee60b49 1728 $_SESSION[SESSION_WIKI_LOCKS]=array();
4db1861a 1729 }
2ee60b49 1730 $_SESSION[SESSION_WIKI_LOCKS][$wikiid.'_'.$pagename]=$lockid;
6022c4f5 1731 $lockdata=new StdClass;
1732 $lockdata->id=$lockid;
1733 return array(true,$lockdata);
4db1861a 1734}
1735
1736/**
1737 * If the user has an editing lock, releases it. Has no effect otherwise.
1738 * Note that it doesn't matter if this isn't called (as happens if their
1739 * browser crashes or something) since locks time out anyway. This is just
1740 * to avoid confusion of the 'what? it says I'm editing that page but I'm
1741 * not, I just saved it!' variety.
1742 * @param int $wikiid ID of wiki object.
1743 * @param string $pagename Name of page.
1744 */
f84d6a8d 1745function wiki_release_lock($wikiid,$pagename) {
655b09ca 1746 global $DB;
1747
4db1861a 1748 if(!array_key_exists(SESSION_WIKI_LOCKS,$_SESSION)) {
2ee60b49 1749 // No locks at all in session
1750 return;
4db1861a 1751 }
f84d6a8d 1752
4db1861a 1753 $key=$wikiid.'_'.$pagename;
f84d6a8d 1754
4db1861a 1755 if(array_key_exists($key,$_SESSION[SESSION_WIKI_LOCKS])) {
2ee60b49 1756 $lockid=$_SESSION[SESSION_WIKI_LOCKS][$key];
1757 unset($_SESSION[SESSION_WIKI_LOCKS][$key]);
655b09ca 1758 if (!$DB->delete_records('wiki_locks', array('id'=>$lockid))) {
4d6ba0f0 1759 print_error('cannotdeletelockrecored', 'wiki');
4db1861a 1760 }
f84d6a8d 1761 }
4db1861a 1762}
1763
f432bebf 1764/**
1765 * Returns all other caps used in module
1766 */
1767function wiki_get_extra_capabilities() {
1768 return array('moodle/site:accessallgroups', 'moodle/site:viewfullnames');
1769}
4db1861a 1770
42f103be 1771/**
1772 * @param string $feature FEATURE_xx constant for requested feature
1773 * @return mixed True if module supports feature, null if doesn't know
1774 */
1775function wiki_supports($feature) {
1776 switch($feature) {
1777 case FEATURE_GROUPS: return true;
1778 case FEATURE_GROUPINGS: return true;
1779 case FEATURE_GROUPMEMBERSONLY: return true;
1780 case FEATURE_MODEDIT_INTRO_EDITOR: return true;
1781 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
1782 case FEATURE_GRADE_HAS_GRADE: return false;
1783 case FEATURE_GRADE_OUTCOMES: return true;
1784
1785 default: return null;
1786 }
1787}