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