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