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