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