Thanks, Eloy! I've got it in weblib instead
[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;
146
8f0cd6ef 147 return false; // True if anything was printed, otherwise false
39fcb981 148}
149
150function wiki_cron () {
151/// Function to be run periodically according to the moodle cron
8f0cd6ef 152/// This function searches for things that need to be done, such
153/// as sending out mail, toggling flags etc ...
39fcb981 154
155 global $CFG;
156
157 return true;
158}
159
160function wiki_grades($wikiid) {
8f0cd6ef 161/// Must return an array of grades for a given instance of this module,
39fcb981 162/// indexed by user. It also returns a maximum allowed grade.
163
4a9df373 164 return NULL;
39fcb981 165}
166
167function wiki_get_participants($wikiid) {
472a28ca 168//Returns the users with data in one wiki
169//(users with records in wiki_pages and wiki_entries)
39fcb981 170
472a28ca 171 global $CFG;
172
173 //Get users from wiki_pages
174 $st_pages = get_records_sql("SELECT DISTINCT u.id, u.id
175 FROM {$CFG->prefix}user u,
176 {$CFG->prefix}wiki_entries e,
177 {$CFG->prefix}wiki_pages p
178 WHERE e.wikiid = '$wikiid' and
179 p.wiki = e.id and
180 u.id = p.userid");
181
182 //Get users from wiki_entries
183 $st_entries = get_records_sql("SELECT DISTINCT u.id, u.id
184 FROM {$CFG->prefix}user u,
185 {$CFG->prefix}wiki_entries e
186 WHERE e.wikiid = '$wikiid' and
187 u.id = e.userid");
188
189 //Add entries to pages
190 if ($st_entries) {
191 foreach ($st_entries as $st_entry) {
192 $st_pages[$st_entry->id] = $st_entry;
193 }
194 }
195
196 return $st_pages;
39fcb981 197}
198
472a28ca 199
39fcb981 200//////////////////////////////////////////////////////////////////////////////////////
8f0cd6ef 201/// Any other wiki functions go here. Each of them must have a name that
39fcb981 202/// starts with wiki_
203
204function wiki_wiki_name($wikiname) {
205/// Return the passed in string in Wiki name format.
206/// Remove any leading and trailing whitespace, capitalize all the words
207/// and then remove any internal whitespace.
208
209 if (wiki_is_wiki_name($wikiname)) {
210 return $wikiname;
211 }
212 else {
213 /// Create uppercase words and remove whitespace.
214 $wikiname = preg_replace("/(\w+)\s/", "$1", ucwords(trim($wikiname)));
215
216 /// Check again - there may only be one word.
217 if (wiki_is_wiki_name($wikiname)) {
218 return $wikiname;
219 }
220 /// If there is only one word, append default wiki name to it.
221 else {
222 return $wikiname.get_string('wikidefaultpagename', 'wiki');
223 }
224 }
225}
226
227function wiki_is_wiki_name($wikiname) {
228/// Check for correct wikiname syntax and return true or false.
229
8f0cd6ef 230 /// If there are spaces between the words, incorrect format.
39fcb981 231 if (preg_match_all('/\w+/', $wikiname, $out) > 1) {
232 return false;
233 }
234 /// If there isn't more than one group of uppercase letters separated by
235 /// lowercase letters or '_', incorrect format.
236 else if (preg_match_all('/[A-Z]+[a-z_]+/', $wikiname, $out) > 1) {
237 return true;
238 }
8f0cd6ef 239 else {
39fcb981 240 return false;
241 }
242}
243
244function wiki_page_name(&$wiki) {
245/// Determines the wiki's page name and returns it.
246 if (!empty($wiki->initialcontent)) {
247 $ppos = strrpos($wiki->initialcontent, '/');
248 if ($ppos === false) {
249 $pagename = $wiki->initialcontent;
250 }
251 else {
252 $pagename = substr($wiki->initialcontent, $ppos+1);
253 }
254 }
255 else if (!empty($wiki->pagename)) {
256 $pagename = $wiki->pagename;
257 }
258 else {
526efb55 259 $pagename = $wiki->name;
39fcb981 260 }
261 return $pagename;
262}
263
264function wiki_content_dir(&$wiki) {
265/// Determines the wiki's default content directory (if there is one).
266 global $CFG;
267
268 if (!empty($wiki->initialcontent)) {
269 $ppos = strrpos($wiki->initialcontent, '/');
270 if ($ppos === false) {
271 $subdir = '';
272 }
273 else {
274 $subdir = substr($wiki->initialcontent, 0, $ppos+1);
275 }
276 $contentdir = $CFG->dataroot.'/'.$wiki->course.'/'.$subdir;
277 }
278 else {
279 $contentdir = false;
280 }
281 return $contentdir;
282}
283
c513f599 284function wiki_get_course_wikis($courseid, $wtype='*') {
285/// Returns all wikis for the specified course and optionally of the specified type.
286
287 $select = 'course = '.$courseid;
288 if ($wtype != '*') {
289 $select .= ' AND wtype = \''.$wtype.'\'';
290 }
291 return get_records_select('wiki', $select, 'id');
292}
293
39fcb981 294function wiki_has_entries(&$wiki) {
295/// Returns true if wiki already has wiki entries; otherwise false.
296
297 return record_exists('wiki_entries', 'wikiid', $wiki->id);
298}
299
300function wiki_get_entries(&$wiki, $byindex=NULL) {
301/// Returns an array with all wiki entries indexed by entry id; false if there are none.
302/// If the optional $byindex is specified, returns the entries indexed by that field.
303/// Valid values for $byindex are 'student', 'group'.
304
305 if ($byindex == 'student') {
8f0cd6ef 306 return get_records('wiki_entries', 'wikiid', $wiki->id, '',
39fcb981 307 'userid,id,wikiid,course,groupid,pagename,timemodified');
308 }
309 else if ($byindex == 'group') {
8f0cd6ef 310 return get_records('wiki_entries', 'wikiid', $wiki->id, '',
39fcb981 311 'groupid,id,wikiid,course,userid,pagename,timemodified');
312 }
313 else {
314 return get_records('wiki_entries', 'wikiid', $wiki->id);
315 }
316}
317
c513f599 318function wiki_get_default_entry(&$wiki, &$course, $userid=0, $groupid=0) {
319/// Returns the wiki entry according to the wiki type.
320/// Optionally, will return wiki entry for $userid student wiki, or
321/// $groupid group or teacher wiki.
322/// Creates one if it needs to and it can.
323
324 global $USER;
325
326 /// If the wiki entry doesn't exist, can this user create it?
327 if (($wiki_entry = wiki_get_entry($wiki, $course, $userid, $groupid)) === false) {
328
329 if (wiki_can_add_entry($wiki, $USER, $course, $userid, $groupid)) {
330 wiki_add_entry($wiki, $course, $userid, $groupid);
331 if (($wiki_entry = wiki_get_entry($wiki, $course, $userid, $groupid)) === false) {
332 error("Could not add wiki entry.");
333 }
334 }
335 }
336 return $wiki_entry;
337}
338
39fcb981 339function wiki_get_entry(&$wiki, &$course, $userid=0, $groupid=0) {
340/// Returns the wiki entry according to the wiki type.
341/// Optionally, will return wiki entry for $userid student wiki, or
342/// $groupid group or teacher wiki.
343 global $USER;
344
345 switch ($wiki->wtype) {
346 case 'student':
347 /// If a specific user was requested, return it, if allowed.
348 if ($userid and wiki_user_can_access_student_wiki($wiki, $userid, $course)) {
349 $wentry = wiki_get_student_entry($wiki, $userid);
350 }
351
352 /// If there is no entry for this user, check if this user is a teacher.
353 else if (!$wentry = wiki_get_student_entry($wiki, $USER->id)) {
354/* if (isteacher($course->id, $USER->id)) {
355 /// If this user is a teacher, return the first entry.
356 if ($wentries = wiki_get_entries($wiki)) {
357 $wentry = current($wentries);
358 }
359 }*/
360 }
361 break;
362
363 case 'group':
364 /// If there is a groupmode, get the user's group id.
365 $groupmode = groupmode($course, $wiki);
366
367 /// If a specific group was requested, return it, if allowed.
368 if ($groupid and wiki_user_can_access_group_wiki($wiki, $groupid, $course)) {
369 $wentry = wiki_get_group_entry($wiki, $groupid);
370 }
371 else if ($groupmode) {
372 /// If there is no entry for this user, check if this user is a teacher.
373 if (!$wentry = wiki_get_group_entry($wiki, mygroupid($course->id))) {
374/* if (isteacher($course->id, $USER->id)) {
375 /// If this user is a teacher, return the first entry.
376 if ($wentries = wiki_get_entries($wiki)) {
377 $wentry = current($wentries);
378 }
379 } */
380 }
381 }
382 /// If mode is 'nogroups', then groupid is zero.
383 else {
384 $wentry = wiki_get_group_entry($wiki, 0);
385 }
386 break;
387
388 case 'teacher':
389 /// If there is a groupmode, get the user's group id.
390 if (groupmode($course, $wiki)) {
391 $groupid = $groupid ? $groupid : mygroupid($course->id);
392 }
393
394 /// If a specific group was requested, return it, if allowed.
395 if (wiki_user_can_access_teacher_wiki($wiki, $groupid, $course)) {
396 $wentry = wiki_get_teacher_entry($wiki, $groupid);
397 }
398 break;
399 }
400 return $wentry;
401}
402
403function wiki_get_teacher_entry(&$wiki, $groupid=0) {
404/// Returns the wiki entry for the wiki teacher type.
405 return get_record('wiki_entries', 'wikiid', $wiki->id, 'course', $wiki->course, 'groupid', $groupid);
406}
407
408function wiki_get_group_entry(&$wiki, $groupid=null) {
409/// Returns the wiki entry for the given group.
410 return get_record('wiki_entries', 'wikiid', $wiki->id, 'groupid', $groupid);
411}
412
413function wiki_get_student_entry(&$wiki, $userid=null) {
414/// Returns the wiki entry for the given student.
415 global $USER;
416
417 if (is_null($userid)) {
418 $userid = $USER->id;
419 }
420 return get_record('wiki_entries', 'wikiid', $wiki->id, 'userid', $userid);
421}
422
423function wiki_get_other_wikis(&$wiki, &$user, &$course, $currentid=0) {
424 /// Returns a list of other wikis to display, depending on the type, group and user.
425 /// Returns the key containing the currently selected entry as well.
426
d0b95287 427 global $CFG, $id;
39fcb981 428
429 $wikis = false;
430
431 $groupmode = groupmode($course, $wiki);
432 $mygroupid = mygroupid($course->id);
433 $isteacher = isteacher($course->id, $user->id);
434 $isteacheredit = isteacheredit($course->id, $user->id);
39fcb981 435
436 switch ($wiki->wtype) {
437
438 case 'student':
439 /// Get all the existing entries for this wiki.
440 $wiki_entries = wiki_get_entries($wiki, 'student');
222ac91b 441 if ($isteacher and (SITEID != $course->id)) {
39fcb981 442
443 /// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all student
444 /// wikis, regardless of creation.
222ac91b 445 if ((SITEID != $course->id) and ($isteacheredit or ($groupmode == NOGROUPS))) {
39fcb981 446
447 if ($students = get_course_students($course->id)) {
448 /// Default pagename is dependent on the wiki settings.
449 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
450
451 foreach ($students as $student) {
452
453 /// If this student already has an entry, use its pagename.
454 if ($wiki_entries[$student->id]) {
455 $pagename = $wiki_entries[$student->id]->pagename;
456 }
457 else {
458 $pagename = $defpagename;
459 }
460
d0b95287 461 $key = 'view.php?id='.$id.'&userid='.$student->id.'&page='.$pagename;
39fcb981 462 $wikis[$key] = fullname($student).':'.$pagename;
463 }
464 }
465 }
466 else if ($groupmode == SEPARATEGROUPS) {
467 if ($students = get_group_students($mygroupid)) {
468 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
469 foreach ($students as $student) {
470
471 /// If this student already has an entry, use its pagename.
472 if ($wiki_entries[$student->id]) {
473 $pagename = $wiki_entries[$student->id]->pagename;
474 }
475 else {
476 $pagename = $defpagename;
477 }
478
d0b95287 479 $key = 'view.php?id='.$id.'&userid='.$student->id.'&page='.$pagename;
39fcb981 480 $wikis[$key] = fullname($student).':'.$pagename;
481 }
482 }
483 }
484 else if ($groupmode == VISIBLEGROUPS) {
8f0cd6ef 485 /// Get all students in your group.
39fcb981 486 if ($students = get_group_students($mygroupid)) {
487 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
488 foreach ($students as $student) {
489 /// If this student already has an entry, use its pagename.
490 if ($wiki_entries[$student->id]) {
491 $pagename = $wiki_entries[$student->id]->pagename;
492 }
493 else {
494 $pagename = $defpagename;
495 }
d0b95287 496 $key = 'view.php?id='.$id.'&userid='.$student->id.'&page='.$pagename;
39fcb981 497 $wikis[$key] = fullname($student).':'.$pagename;
498 }
499 }
500 /// Get all student wikis created, regardless of group.
501 $sql = 'SELECT w.id, w.userid, w.pagename, u.firstname, u.lastname '
502 .' FROM '.$CFG->prefix.'wiki_entries w, '.$CFG->prefix.'user u '
503 .' WHERE w.wikiid = '.$wiki->id.' AND u.id = w.userid '
504 .' ORDER BY w.id';
505 $wiki_entries = get_records_sql($sql);
506 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
507 foreach ($wiki_entries as $wiki_entry) {
d0b95287 508 $key = 'view.php?id='.$id.'&userid='.$wiki_entry->userid.'&page='.$wiki_entry->pagename;
39fcb981 509 $wikis[$key] = fullname($wiki_entry).':'.$wiki_entry->pagename;
510 if ($currentid == $wiki_entry->id) {
511 $wikis['selected'] = $key;
512 }
513 }
8f0cd6ef 514 }
39fcb981 515 }
516 else {
517 /// A user can see other student wikis if they are a member of the same
518 /// group (for separate groups) or there are visible groups, or if this is
519 /// a site-level wiki, and they are an administrator.
520 if (($groupmode == VISIBLEGROUPS) or
222ac91b 521 ((SITEID == $course->id) and isadmin())) {
39fcb981 522 $viewall = true;
523 }
524 else if ($groupmode == SEPARATEGROUPS) {
525 $viewall = mygroupid($course->id);
526 }
527 else {
528 $viewall = false;
529 }
530
531 if ($viewall !== false) {
532 $sql = 'SELECT w.id, w.userid, w.pagename, u.firstname, u.lastname '
533 .' FROM '.$CFG->prefix.'wiki_entries w, '.$CFG->prefix.'user u '
534 .' WHERE w.wikiid = '.$wiki->id.' AND u.id = w.userid '
535 .' ORDER BY w.id';
536 $wiki_entries = get_records_sql($sql);
537 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
538 foreach ($wiki_entries as $wiki_entry) {
539 if (($viewall === true) or ismember($viewall, $wiki_entry->userid)) {
d0b95287 540 $key = 'view.php?id='.$id.'&userid='.$wiki_entry->userid.'&page='.$wiki_entry->pagename;
39fcb981 541 $wikis[$key] = fullname($wiki_entry).':'.$wiki_entry->pagename;
542 if ($currentid == $wiki_entry->id) {
543 $wikis['selected'] = $key;
544 }
545 }
546 }
547 }
548 }
549 break;
550
551 case 'group':
552 /// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all group
553 /// wikis, regardless of creation.
554
555 /// Get all the existing entries for this wiki.
556 $wiki_entries = wiki_get_entries($wiki, 'group');
557
558 if ($groupmode and ($isteacheredit or ($isteacher and !$mygroupid))) {
559 if ($groups = get_groups($course->id)) {
560 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
561 foreach ($groups as $group) {
562
563 /// If this group already has an entry, use its pagename.
564 if (isset($wiki_entries[$group->id])) {
565 $pagename = $wiki_entries[$group->id]->pagename;
566 }
567 else {
568 $pagename = $defpagename;
569 }
570
d0b95287 571 $key = 'view.php?id='.$id.($group->id?"&groupid=".$group->id:"").'&page='.$pagename;
39fcb981 572 $wikis[$key] = $group->name.':'.$pagename;
573 }
574 }
575 }
576 /// A user can see other group wikis if there are visible groups.
577 else if ($groupmode == VISIBLEGROUPS) {
578 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
579 .' FROM '.$CFG->prefix.'wiki_entries w, '.$CFG->prefix.'groups g '
580 .' WHERE w.wikiid = '.$wiki->id.' AND g.id = w.groupid '
581 .' ORDER BY w.groupid';
582 $wiki_entries = get_records_sql($sql);
583 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
584 foreach ($wiki_entries as $wiki_entry) {
d0b95287 585 $key = 'view.php?id='.$id.($wiki_entry->groupid?"&groupid=".$wiki_entry->groupid:"").'&page='.$wiki_entry->pagename;
39fcb981 586 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
587 if ($currentid == $wiki_entry->id) {
588 $wikis['selected'] = $key;
589 }
590 }
591 }
592 break;
593
594 case 'teacher':
595 if ($isteacher) {
596 /// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all
597 /// teacher wikis, regardless of creation.
598 if ($groupmode and ($isteacheredit or ($isteacher and !$mygroupid))) {
599 if ($groups = get_groups($course->id)) {
600 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
8f0cd6ef 601
39fcb981 602 foreach ($groups as $group) {
603 /// If this group already has an entry, use its pagename.
604 if ($wiki_entries[$group->id]) {
605 $pagename = $wiki_entries[$group->id]->pagename;
606 }
607 else {
608 $pagename = $defpagename;
609 }
610
d0b95287 611 $key = 'view.php?id='.$id.($group->id?"&groupid=".$group->id:"").'&page='.$pagename;
39fcb981 612 $wikis[$key] = $group->name.':'.$pagename;
613 }
614 }
615 }
616 /// A teacher can see all other group teacher wikis.
617 else if ($groupmode) {
618 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
619 .' FROM '.$CFG->prefix.'wiki_entries w, '.$CFG->prefix.'groups g '
620 .' WHERE w.wikiid = '.$wiki->id.' AND g.id = w.groupid '
621 .' ORDER BY w.groupid';
622 $wiki_entries = get_records_sql($sql);
623 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
624 foreach ($wiki_entries as $wiki_entry) {
d0b95287 625 $key = 'view.php?id='.$id.($wiki_entry->groupid?"&groupid=".$wiki_entry->groupid:"").'&page='.$wiki_entry->pagename;
39fcb981 626 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
627 if ($currentid == $wiki_entry->id) {
628 $wikis['selected'] = $key;
629 }
630 }
631 }
632 }
633 else {
634 /// A user can see other teacher wikis if they are a teacher, a member of the same
635 /// group (for separate groups) or there are visible groups.
636 if ($groupmode == VISIBLEGROUPS) {
637 $viewall = true;
638 }
639 else if ($groupmode == SEPARATEGROUPS) {
640 $viewall = $mygroupid;
641 }
642 else {
643 $viewall = false;
644 }
645 if ($viewall !== false) {
646 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
647 .' FROM '.$CFG->prefix.'wiki_entries w, '.$CFG->prefix.'groups g '
648 .' WHERE w.wikiid = '.$wiki->id.' AND g.id = w.groupid '
649 .' ORDER BY w.groupid';
650 $wiki_entries = get_records_sql($sql);
651 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
652 foreach ($wiki_entries as $wiki_entry) {
653 if (($viewall === true) or $viewall == $wiki_entry->groupid) {
d0b95287 654 $key = 'view.php??id='.$id.($wiki_entry->groupid?"&groupid=".$wiki_entry->groupid:"").'&page='.$wiki_entry->pagename;
39fcb981 655 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
656 if ($currentid == $wiki_entry->id) {
657 $wikis['selected'] = $key;
658 }
659 }
660 }
661 }
662 }
663 break;
664 }
8f0cd6ef 665
39fcb981 666 return $wikis;
667}
668
669function wiki_add_entry(&$wiki, &$course, $userid=0, $groupid=0) {
670/// Adds a new wiki entry of the specified type, unless already entered.
671/// No checking is done here. It is assumed that the caller has the correct
672/// privileges to add this entry.
673
674 global $USER;
675
676 /// If this wiki already has a wiki_type entry, return false.
677 if (wiki_get_entry($wiki, $course, $userid, $groupid) !== false) {
678 return false;
679 }
680
681 switch ($wiki->wtype) {
682
683 case 'student':
684 $wiki_entry->wikiid = $wiki->id;
685 $wiki_entry->userid = $userid ? $userid : $USER->id;
686 $wiki_entry->pagename = wiki_page_name($wiki);
687 $wiki_entry->timemodified = time();
688 break;
689
690 case 'group':
691 /// Get the groupmode. It's been added to the wiki object.
692 $groupmode = groupmode($course, $wiki);
693
694 /// If there is a groupmode, get the group id.
695 if ($groupmode) {
696 $groupid = $groupid ? $groupid : mygroupid($course->id);
697 }
698 /// If mode is 'nogroups', then groupid is zero.
699 else {
700 $groupid = 0;
701 }
702 $wiki_entry->wikiid = $wiki->id;
703 $wiki_entry->groupid = $groupid;
704 $wiki_entry->pagename = wiki_page_name($wiki);
705 $wiki_entry->timemodified = time();
706 break;
707
708 case 'teacher':
709 /// Get the groupmode. It's been added to the wiki object.
710 $groupmode = groupmode($course, $wiki);
711
712 /// If there is a groupmode, get the user's group id.
713 if ($groupmode and $groupid == 0) {
714 $groupid = mygroupid($course->id);
715 }
716
717 $wiki_entry->wikiid = $wiki->id;
718 $wiki_entry->course = $wiki->course;
719 $wiki_entry->groupid = $groupid;
720 $wiki_entry->pagename = wiki_page_name($wiki);
721 $wiki_entry->timemodified = time();
722 break;
723 }
e61d4b39 724 $wiki_entry->pagename = addslashes($wiki_entry->pagename);
725
39fcb981 726 return insert_record("wiki_entries", $wiki_entry, true);
727}
728
729function wiki_can_add_entry(&$wiki, &$user, &$course, $userid=0, $groupid=0) {
730/// Returns true or false if the user can add a wiki entry for this wiki.
731
732 /// Get the groupmode. It's been added to the wiki object.
733 $groupmode = groupmode($course, $wiki);
734 $mygroupid = mygroupid($course->id);
39fcb981 735
736 switch ($wiki->wtype) {
737
738 case 'student':
739/// A student can create their own wiki, if they are a member of that course.
740/// A user can create their own wiki at the site level.
741 if ($userid == 0) {
742 return (isstudent($course->id, $user->id) or
222ac91b 743 ((SITEID == $course->id) and !empty($user) and !isguest()));
39fcb981 744 }
745/// An editing teacher can create any student wiki, or
746/// a non-editing teacher, if not assigned to a group can create any student wiki, or if assigned to a group can
747/// create any student wiki in their group.
748 else {
749 return ((($userid == $user->id) and isstudent($course->id, $user->id)) or isteacheredit($course->id) or
750 (isteacher($course->id) and (!$groupmode or $mygroupid == 0 or (ismember($mygroupid, $userid)))));
751 }
752 break;
753
754 case 'group':
755 /// If mode is 'nogroups', then all participants can add wikis.
756 if (!$groupmode) {
757 return (isstudent($course->id, $user->id) or isteacher($course->id, $user->id) or
222ac91b 758 ((SITEID == $course->id) and !empty($user) and !isguest()));
39fcb981 759 }
760 /// If not requesting a group, must be a member of a group.
761 else if ($groupid == 0) {
8f0cd6ef 762 return ($mygroupid != 0);
39fcb981 763 }
764 /// If requesting a group, must be an editing teacher, a non-editing teacher with no assigned group,
765 /// or a non-editing teacher requesting their group.
766 else {
767 return (isteacheredit($course->id) or
768 (isteacher($course->id) and ($mygroupid == 0 or $mygroupid == $groupid)));
769 }
770 break;
771
772 case 'teacher':
773 /// If mode is 'nogroups', then all teachers can add wikis.
774 if (!$groupmode) {
222ac91b 775 return (isteacher($course->id, $user->id) or ((SITEID == $course->id) and isadmin()));
39fcb981 776 }
777 /// If not requesting a group, must be a member of a group.
778 else if ($groupid == 0) {
8f0cd6ef 779 return ($mygroupid != 0 and isteacher($course->id));
39fcb981 780 }
8f0cd6ef 781 /// If there is a group mode, non-editing teachers with an assigned group, can only create wikis
39fcb981 782 /// in their group. Non-editing teachers with no assigned group and editing teachers can create any wiki.
783 else {
8f0cd6ef 784 return (isteacheredit($course->id) or
39fcb981 785 (isteacher($course->id) and ($mygroupid == 0 or $mygroupid == $groupid)));
786 }
787 break;
788 }
789
790 return false;
791}
792
793function wiki_can_edit_entry(&$wiki_entry, &$wiki, &$user, &$course) {
794/// Returns true or false if the user can edit this wiki entry.
795
796 $can_edit = false;
797 $groupmode = groupmode($course, $wiki);
798 $mygroupid = mygroupid($course->id);
39fcb981 799
8f0cd6ef 800 /// Editing teacher's and admins can edit all wikis, non-editing teachers can edit wikis in their groups,
39fcb981 801 /// or all wikis if group mode is 'no groups' or they don't belong to a group.
eb474b29 802 if (isadmin($user->id) or isteacheredit($course->id, $user->id) or
39fcb981 803 ((!$groupmode or $mygroupid == 0) and isteacher($course->id, $user->id))) {
804 $can_edit = true;
805 }
806 else {
807 switch ($wiki->wtype) {
808
809 /// Only a teacher or the owner of a student wiki can edit it.
810 case 'student':
811 $can_edit = (($user->id == $wiki_entry->userid) or
812 ($groupmode and isteacher($course->id, $user->id) and
813 ismember($mygroupid, $wiki_entry->userid)));
814 break;
815
816 case 'group':
817 /// If there is a groupmode, determine the user's group status.
818 if ($groupmode) {
819 /// If the user is a member of the wiki group, they can edit the wiki.
820 $can_edit = ismember($wiki_entry->groupid, $user->id);
821 }
822 /// If mode is 'nogroups', then all participants can edit the wiki.
823 else {
824 $can_edit = (isstudent($course->id, $user->id) or isteacher($course->id, $user->id) or
222ac91b 825 ((SITEID == $course->id) and !empty($user) and !isguest()));
39fcb981 826 }
827 break;
828
829 case 'teacher':
830 /// If there is a groupmode, determine the user's group status.
831 if ($groupmode) {
832 /// If the user is a member of the wiki group, they can edit the wiki.
833 $can_edit = (isteacher($course->id, $user->id) and ismember($wiki_entry->groupid, $user->id));
834 }
835 else {
222ac91b 836 $can_edit = (isteacher($course->id, $user->id) or ((SITEID == $course->id) and isadmin()));
39fcb981 837 }
838 break;
839 }
840 }
841
842 return $can_edit;
843}
844
845function wiki_user_can_access_student_wiki(&$wiki, $userid, &$course) {
846 global $USER;
847
848 /// Get the groupmode. It's been added to the wiki object.
849 $groupmode = groupmode($course, $wiki);
850 $usersgroup = mygroupid($course->id);
851 $isteacher = isteacher($course->id, $USER->id);
852
853 /// If this user is allowed to access this wiki then return TRUE.
854 /// *** THIS COULD BE A PROBLEM, IF STUDENTS COULD EVER BE PART OF MORE THAN ONE GROUP ***
855 /// A user can access a student wiki, if:
856 /// - it is their wiki,
857 /// - group mode is VISIBLEGROUPS,
858 /// - group mode is SEPARATEGROUPS, and the user is a member of the requested user's group,
859 /// - they are an editing teacher or administrator,
860 /// - they are a non-editing teacher not assigned to a specific group,
861 /// - they are a non-editing teacher and group mode is NOGROUPS.
862 /// - they are an administrator (mostly for site-level wikis).
863 if (($userid and ($USER->id == $userid)) or ($groupmode == VISIBLEGROUPS) or
8f0cd6ef 864 (($groupmode == SEPARATEGROUPS) and ismember($usersgroup, $userid)) or
39fcb981 865 (isteacheredit($course->id, $USER->id)) or
866 (isteacher($course->id, $USER->id) and (!$usersgroup or ($groupmode == NOGROUPS))) or
867 (isadmin())) {
868 $can_access = true;
869 }
870 else {
871 $can_access = false;
872 }
873 return $can_access;
874}
875
876function wiki_user_can_access_group_wiki(&$wiki, $groupid, &$course) {
877 global $USER;
878
879 /// Get the groupmode. It's been added to the wiki object.
880 $groupmode = groupmode($course, $wiki);
881 $usersgroup = mygroupid($course->id);
882 $isteacher = isteacher($course->id, $USER->id);
883
884 /// A user can access a group wiki, if:
885 /// - group mode is NOGROUPS,
886 /// - group mode is VISIBLEGROUPS,
887 /// - group mode is SEPARATEGROUPS, and they are a member of the requested group,
888 /// - they are an editing teacher or administrator,
889 /// - they are a non-editing teacher not assigned to a specific group.
8f0cd6ef 890 if (($groupmode == NOGROUPS) or ($groupmode == VISIBLEGROUPS) or
891 (($groupmode == SEPARATEGROUPS) and ($usersgroup == $groupid)) or
39fcb981 892 (isteacheredit($course->id, $USER->id)) or
893 (isteacher($course->id, $USER->id) and !$usersgroup)) {
894 $can_access = true;
895 }
896 else {
897 $can_access = false;
898 }
899 return $can_access;
900}
901
902function wiki_user_can_access_teacher_wiki(&$wiki, $groupid, &$course) {
903 global $USER;
904
905 /// Get the groupmode. It's been added to the wiki object.
906 $groupmode = groupmode($course, $wiki);
907
908 /// A user can access a teacher wiki, if:
909 /// - group mode is NOGROUPS,
910 /// - group mode is VISIBLEGROUPS,
911 /// - group mode is SEPARATEGROUPS, and they are a member of the requested group,
912 /// - they are a teacher or administrator,
8f0cd6ef 913 if (($groupmode == NOGROUPS) or ($groupmode == VISIBLEGROUPS) or
39fcb981 914 (($groupmode == SEPARATEGROUPS) and (mygroupid($course->id) == $groupid)) or
915 (isteacher($course->id, $USER->id))){
916 $can_access = true;
917 }
918 else {
919 $can_access = false;
920 }
921 return $can_access;
922}
923
924function wiki_get_owner(&$wiki_entry) {
925 if ($wiki_entry->userid > 0) {
926 $user = get_record('user', 'id', $wiki_entry->userid);
927 $owner = fullname($user);
928 }
929 else if ($wiki_entry->groupid > 0) {
930 $group = get_record('groups', 'id', $wiki_entry->groupid);
931 $owner = $group->name;
932 }
933 else if ($wiki_entry->course > 0) {
934 $course = get_record('course', 'id', $wiki_entry->course);
935 $owner = $course->shortname;
936 }
937 else {
a9de0242 938 $owner = '- '.get_string("ownerunknown","wiki").' -';
39fcb981 939 }
940 return $owner;
941}
942
943function wiki_print_search_form($cmid, $search="", $userid, $groupid, $return=false) {
944 global $CFG;
945 # TODO: Add Group and User !!!
946 $output = "<form name=\"search\" action=\"$CFG->wwwroot/mod/wiki/view.php\">";
af9fcd08 947 $output .= "<font size=\"-1\">";
a9ef4a63 948 $output .= "<input value=\"".get_string("searchwiki", "wiki").":\" type=\"submit\" />";
949 $output .= "<input name=\"id\" type=\"hidden\" value=\"$cmid\" />";
950 $output = $output.($groupid?"<input name=\"groupid\" type=\"hidden\" value=\"$groupid\" />":"");
951 $output = $output.($userid?"<input name=\"userid\" type=\"hidden\" value=\"$userid\" />":"");
952 $output .= "<input name=\"q\" type=\"text\" size=\"20\" value=\"$search\" />".' ';
39fcb981 953 $output .= "</font>";
a9ef4a63 954 $output .= "<input name=\"page\" type=\"hidden\" value=\"SearchPages\" />";
39fcb981 955 $output .= "</form>";
956
957 if ($return) {
958 return $output;
959 }
960 echo $output;
961}
962
963function wiki_print_wikilinks_block($cmid, $binary=false, $return=false) {
087fef3e 964/// Prints a link-list of special wiki-pages
39fcb981 965 global $CFG, $ewiki_title;
966
967 $links=array();
968
f80b0733 969 $links["SiteMap"]=get_string("sitemap", "wiki");
39fcb981 970 $links["PageIndex"]=get_string("pageindex", "wiki");
971 $links["NewestPages"]=get_string("newestpages", "wiki");
972 $links["MostVisitedPages"]=get_string("mostvisitedpages", "wiki");
973 $links["MostOftenChangedPages"]=get_string("mostoftenchangedpages", "wiki");
974 $links["UpdatedPages"]=get_string("updatedpages", "wiki");
975 $links["OrphanedPages"]=get_string("orphanedpages", "wiki");
976 $links["WantedPages"]=get_string("wantedpages", "wiki");
a9de0242 977 $links["WikiExport"]=get_string("wikiexport", "wiki");
39fcb981 978 if($binary) {
979 $links["FileDownload"]=get_string("filedownload", "wiki");
980 }
8f0cd6ef 981 popup_form(EWIKI_SCRIPT, $links, "wikilinks", "", get_string("choosewikilinks", "wiki"), "", "", $return);
39fcb981 982}
983
087fef3e 984function wiki_print_page_actions($cmid, $specialpages, $page, $action, $binary=false, $canedit=true) {
39fcb981 985/// Displays actions which can be performed on the page
986
987 $page=array();
988
989 // Edit this Page
990 if (in_array($action, array("edit", "links", "info", "attachments"))) {
8f0cd6ef 991 $page["view/$page"]=get_string("viewpage","wiki");
39fcb981 992 }
087fef3e 993 if ($canedit && !in_array($page, $specialpages) && $action != "edit") {
994 $page["edit/$page"]=get_string("editthispage","wiki");
39fcb981 995 }
996 if ($action != "links") {
087fef3e 997 $page["links/$page"]=get_string("backlinks","wiki");
39fcb981 998 }
087fef3e 999 if ($canedit && !in_array($page, $specialpages) && $action!="info") {
1000 $page["info/$page"]=get_string("pageinfo","wiki");
39fcb981 1001 }
087fef3e 1002 if($canedit && $binary && !in_array($page, $specialpages) && $action != "attachments") {
1003 $page["attachments/$page"]=get_string("attachments","wiki");
39fcb981 1004 }
1005
8f0cd6ef 1006 popup_form(EWIKI_SCRIPT, $page, "wikiactions", "", get_string("action", "wiki"), "", "", false);
39fcb981 1007}
1008
087fef3e 1009function wiki_print_administration_actions($wiki, $cmid, $userid, $groupid, $page, $noeditor, $course) {
39fcb981 1010/// Displays actions which can be performed on the page
d0b95287 1011
1012 /// Create the URL
39fcb981 1013 $ewscript = 'admin.php?id='.$cmid;
839f2456 1014 if (isset($userid) && $userid!=0) $ewscript .= '&amp;userid='.$userid;
1015 if (isset($groupid) && $groupid!=0) $ewscript .= '&amp;groupid='.$groupid;
1016 if (isset($page)) $ewscript .= '&amp;page='.$page;
1017 $ewscript.="&amp;action=";
39fcb981 1018
c513f599 1019
1020 /// Build that action array according to wiki flags.
1021 $action = array();
1022 $isteacher = isteacher($course->id);
1023
1024 if ($wiki->setpageflags or $isteacher) {
1025 $action['setpageflags'] = get_string('setpageflags', 'wiki');
1026 }
1027 if ($wiki->removepages or $isteacher) {
1028 $action['removepages'] = get_string('removepages', 'wiki');
1029 }
1030 if ($wiki->strippages or $isteacher) {
1031 $action['strippages'] = get_string('strippages', 'wiki');
1032 }
1033 if ($wiki->revertchanges or $isteacher) {
1034 $action['revertpages'] = get_string('revertpages', 'wiki');
1035 }
1036
39fcb981 1037 if($noeditor) {
1038 $action["checklinks"]=get_string("checklinks", "wiki");
1039 }
8f0cd6ef 1040 popup_form($ewscript, $action, "wikiadministration", "", get_string("chooseadministration", "wiki"), "", "", false);
39fcb981 1041}
1042
1043function wiki_admin_get_flagarray() {
1044 $ret = array(
1045 EWIKI_DB_F_TEXT => get_string("flagtxt","wiki"),
1046 EWIKI_DB_F_BINARY => get_string("flagbin","wiki"),
1047 EWIKI_DB_F_DISABLED => get_string("flagoff","wiki"),
1048 EWIKI_DB_F_HTML => get_string("flaghtm","wiki"),
1049 EWIKI_DB_F_READONLY => get_string("flagro","wiki"),
1050 EWIKI_DB_F_WRITEABLE => get_string("flagwr","wiki"),
1051 );
8f0cd6ef 1052
39fcb981 1053 return $ret;
1054}
1055
1056///////// Ewiki Administration. Mostly taken from the ewiki/tools folder and changed
1057function wiki_admin_setpageflags_list($pageflagstatus) {
8f0cd6ef 1058 $FD = wiki_admin_get_flagarray();
39fcb981 1059 $table->head = array(get_string("pagename","wiki"), get_string("flags","wiki"));
1060 if($pageflagstatus) {
1061 $table->head[]=get_string("status","wiki");
1062 }
8f0cd6ef 1063
39fcb981 1064 $result = ewiki_database("GETALL", array("version", "flags"));
1065 while ($row = $result->get()) {
1066 $id = $row["id"];
1067 $data = ewiki_database("GET", $row);
1068
1069 $cell_pagename="";
1070 $cell_flags="";
1071 if ($data["flags"] & EWIKI_DB_F_TEXT) {
1072 $cell_pagename .= '<A HREF="' . EWIKI_SCRIPT . $id . '">';
1073 } else {
1074 $cell_pagename .= '<A HREF="' . EWIKI_SCRIPT_BINARY . $id . '">';
1075 }
1076 $cell_pagename .= htmlentities($id) . '</A> / '.get_string("version","wiki").": ".$row["version"];
8f0cd6ef 1077
39fcb981 1078 foreach ($FD as $n=>$str) {
1079 $cell_flags .='<INPUT TYPE="checkbox" NAME="flags['. rawurlencode($id)
1080 . '][' . $n . ']" VALUE="1" '
a9ef4a63 1081 . (($data["flags"] & $n) ? "CHECKED=\"checked\"" : "")
1082 . ' />'.$str. ' ';
39fcb981 1083 }
1084 if($pageflagstatus) {
1085 $table->data[]=array($cell_pagename, $cell_flags, $pageflagstatus[$id]);
1086 } else {
1087 $table->data[]=array($cell_pagename, $cell_flags);
1088 }
1089 }
1090 return $table;
1091}
1092
1093function wiki_admin_setpageflags($pageflags) {
1094 $FD = wiki_admin_get_flagarray();
8f0cd6ef 1095
39fcb981 1096 $status=array();
8f0cd6ef 1097 if($pageflags) {
39fcb981 1098 foreach($pageflags as $page=>$fa) {
1099
1100 $page = rawurldecode($page);
1101
1102 $flags = 0;
1103 $fstr = "";
1104 foreach($fa as $num=>$isset) {
1105 if ($isset) {
1106 $flags += $num;
1107 $fstr .= ($fstr?",":""). $FD[$num];
1108 }
1109 }
1110
1111 #$status[$page] .= "{$flags}=[{$fstr}]";
1112
1113 $data = ewiki_database("GET", array("id" => $page));
1114
1115 if ($data["flags"] != $flags) {
1116 $data["flags"] = $flags;
1117 $data["author"] = "ewiki-tools, " . ewiki_author();
1118 $data["version"]++;
1119 ewiki_database("WRITE", $data);
1120 $status[$page] = "<b>".get_string("flagsset","wiki")."</b> ".$status[$page];
1121 }
1122 }
1123 }
8f0cd6ef 1124 return $status;
39fcb981 1125}
1126
1127
1128function wiki_admin_remove_list($listall="") {
1129 /// Table header
1130 $table->head = array("&nbsp;", get_string("pagename","wiki"), get_string("errororreason","wiki"));
8f0cd6ef 1131
39fcb981 1132 /// Get all pages
1133 $result = ewiki_database("GETALL", array("version"));
1134 $selected = array();
1135
1136 /// User wants to see all pages
1137 if ($listall) {
1138 while ($row = $result->get()) {
a9ef4a63 1139 $selected[$row["id"]] = get_string("listall","wiki")."<br />";
39fcb981 1140 }
1141 }
8f0cd6ef 1142 while ($page = $result->get()) {
39fcb981 1143 $id = $page["id"];
1144 $page = ewiki_database("GET", array("id"=>$id));
1145 $flags = $page["flags"];
8f0cd6ef 1146 #print "$id ".strlen(trim(($page["content"])))."<br />";
39fcb981 1147
1148 if (!strlen(trim(($page["content"]))) && !($flags & EWIKI_DB_F_BINARY)) {
a9ef4a63 1149 @$selected[$id] .= get_string("emptypage","wiki")."<br />";
39fcb981 1150 }
8f0cd6ef 1151
39fcb981 1152 // Check for orphaned pages
1153 $result2 = ewiki_database("SEARCH", array("content" => $id));
1154 $orphanedpage=true;
1155 if ($result2 && $result2->count()) {
1156 while ($row = $result2->get()) {
1157 $checkcontent = ewiki_database("GET", array("id"=>$row["id"]));
1158 $checkcontent = strtolower($checkcontent["content"]);
8f0cd6ef 1159
39fcb981 1160 if(strpos($checkcontent, strtolower($id)) !== false) {
1161 $orphanedpage=false;
1162 }
1163
a9ef4a63 1164 #echo "rc({$row['id']})==>($id): $check2 <br />";
39fcb981 1165 }
1166 }
1167
8f0cd6ef 1168 /// Some more reasons for Deletion...
39fcb981 1169 if ($orphanedpage && $id!=EWIKI_PAGE_INDEX &&!($flags & EWIKI_DB_F_BINARY)) {
a9ef4a63 1170 @$selected[$id] .= get_string("orphanedpage","wiki")."<br />";
39fcb981 1171 }
1172
1173 if ($flags & EWIKI_DB_F_DISABLED) {
a9ef4a63 1174 @$selected[$id] .= get_string("disabledpage","wiki")."<br />";
39fcb981 1175 }
1176
1177 if (($flags & 3) == 3) {
a9ef4a63 1178 @$selected[$id] .= get_string("errorbinandtxt","wiki")."<br />";
39fcb981 1179 }
1180
1181 if (!($flags & 3)) {
a9ef4a63 1182 @$selected[$id] .= get_string("errornotype","wiki")."<br />";
39fcb981 1183 }
1184
1185 if ($flags & EWIKI_DB_F_HTML) {
a9ef4a63 1186 @$selected[$id] .= get_string("errorhtml","wiki")."<br />";
39fcb981 1187 }
1188
1189 if (($flags & EWIKI_DB_F_READONLY) && !($flags & EWIKI_DB_F_BINARY)) {
a9ef4a63 1190 @$selected[$id] .= get_string("readonly","wiki")."<br />";
39fcb981 1191 }
1192
1193 if (($flags & EWIKI_DB_F_READONLY) && ($flags & EWIKI_DB_F_WRITEABLE)) {
a9ef4a63 1194 @$selected[$id] .= get_string("errorroandwr","wiki")."<br />";
39fcb981 1195 }
1196
1197 if (strlen($page["content"]) >= 65536) {
a9ef4a63 1198 @$selected[$id] .= get_string("errorsize","wiki")."<br />";
39fcb981 1199 }
1200
1201 if (strpos($page["refs"], "\n".get_string("deletemewikiword","wiki")."\n")!==false) {
a9ef4a63 1202 @$selected[$id] .= get_string("deletemewikiwordfound","wiki",get_string("deletemewikiword","wiki"))."<br />";
39fcb981 1203 }
1204 }
8f0cd6ef 1205
1206 foreach ($selected as $id => $reason) {
a9ef4a63 1207 $table_checkbox='<INPUT TYPE="checkbox" VALUE="'.rawurlencode($id).'" NAME="pagestodelete[]" />';
39fcb981 1208
1209 #-- link & id
1210 if (strpos($id, EWIKI_IDF_INTERNAL) === false) {
1211 $table_page='<A HREF="' . ewiki_script("", $id) . '">';
1212 } else {
1213 $table_page='<A HREF="' . ewiki_script_binary("", $id) . '">';
1214 }
1215 $table_page .= htmlentities($id) . '</A>';
1216
1217 #-- print reason
1218 $table_reason=$reason;
8f0cd6ef 1219
39fcb981 1220 $table->data[]=array($table_checkbox, $table_page, $table_reason);
8f0cd6ef 1221 }
1222
1223 return $table;
39fcb981 1224}
1225
1226/// This function actually removes the pages
1227function wiki_admin_remove($pagestodelete, $course, $wiki, $userid, $groupid) {
8f0cd6ef 1228 $ret="";
39fcb981 1229 foreach ($pagestodelete as $id) {
1230
1231 $id = rawurldecode($id);
1232
1233 $data = ewiki_database("GET", array("id"=>$id));
1234 for ($version=1; $version<=$data["version"]; $version++) {
1235 ewiki_database("DELETE", array("id"=>$id, "version"=>$version));
1236 if($data["flags"] & EWIKI_DB_F_BINARY) {
1237 $filepath=moodle_binary_get_path($id, $data["meta"], $course, $wiki, $userid, $groupid);
1238 @unlink("$filepath");
1239 }
1240 }
8f0cd6ef 1241
39fcb981 1242 }
1243 return $ret;
1244}
1245
1246function wiki_admin_strip_list($pagestostrip="",$version="",$err="") {
1247 /// Table header
1248 $table->head = array("&nbsp;", get_string("pagename","wiki"), get_string("deleteversions","wiki"));
8f0cd6ef 1249
39fcb981 1250 $vc=ewiki_database("COUNTVERSIONS", array());
1251 $result = ewiki_database("GETALL",array());
1252 $i=0;
1253 while ($row = $result->get()) {
1254 $id = $row["id"];
1255 if($vc[$id]>1) {
1256 $error="";
1257 if($err[$id]) {
1258 $error=" ".join(", ",$err[$id]);
8f0cd6ef 1259 }
39fcb981 1260 $checked="";
1261 if($pagestostrip=="" || $pagestostrip[$i]) {
1262 $checked=" CHECKED";
1263 }
1264 if($version=="") {
1265 $versiondefault="1-".($row["version"]-1);
1266 } else {
1267 $versiondefault=$version[$i];
1268 }
8f0cd6ef 1269 $table->data[]=array('<input type="checkbox" value="'.rawurlencode($id).'" name="pagestostrip['.$i.']" '.$checked.' />',
39fcb981 1270 '<A HREF="'.EWIKI_SCRIPT.$id.'">'.htmlentities($id).'</A> / '.get_string("version","wiki").": ".$row["version"],
a9ef4a63 1271 '<input name="version['.$i.']" value="'.$versiondefault.'" size="7" />'.$error);
8f0cd6ef 1272
39fcb981 1273 }
1274 $i++;
1275 }
1276 return $table;
1277}
1278
1279function wiki_admin_strip_versions($pagestostrip, $version, &$err) {
1280 $ret=array();
1281 foreach ($pagestostrip as $key => $id_ue) {
8f0cd6ef 1282
39fcb981 1283 $id = rawurldecode($id_ue);
1284 if (preg_match('/^(\d+)[-\s._:]+(\d+)$/', trim($version[$key]), $uu)) {
1285 $versA = $uu[1];
1286 $versZ = $uu[2];
8f0cd6ef 1287
39fcb981 1288 // Let the last Version in the database
d2481a47 1289 $checkdata = ewiki_database("GET", array("id" => $id));
39fcb981 1290 if($versZ>=$checkdata["version"]) {
8f0cd6ef 1291 $err[$id][] = get_string("versionrangetoobig","wiki");
39fcb981 1292 } else {
1293 if($versA<=$versZ) {
1294 for ($v=$versA; $v<=$versZ; $v++) {
1295 $ret[$id][]=$v;
1296 }
1297 } else {
1298 $err[$id][]=get_string("wrongversionrange","wiki",$version[$key]);
1299 }
1300 }
1301 }
1302 else {
1303 $err[$id][]=get_string("wrongversionrange","wiki",$version[$key]);
1304 }
1305 }
1306 return $ret;
1307}
8f0cd6ef 1308
39fcb981 1309function wiki_admin_strip($pagestostrip) {
1310 /// Purges old page-versions
1311 foreach($pagestostrip as $id => $versions) {
1312 foreach($versions as $version) {
1313 ewiki_database("DELETE", array("id"=>$id, "version"=>$version));
1314 }
1315 }
8f0cd6ef 1316}
39fcb981 1317
1318function wiki_admin_checklinks_list() {
1319 $ret=array();
1320 $result = ewiki_database("GETALL",array());
1321 while ($row = $result->get()) {
1322 if(!($row["flags"] & EWIKI_DB_F_BINARY)) {
1323 $index=htmlentities($row["id"]);
1324 $ret[$index] = $row["id"];
1325 }
1326 }
1327 return $ret;
1328}
1329
1330function wiki_admin_checklinks($pagetocheck) {
1331 /// Checks http:// Links
1332 $ret="";
1333 if($pagetocheck) {
1334 $get = ewiki_database("GET", array("id" => $pagetocheck));
1335 $content = $get["content"];
8f0cd6ef 1336
a9de0242 1337 preg_match_all('_(http.?://[^\s"\'<>#,;]+[^\s"\'<>#,;.])_', $content, $links);
39fcb981 1338 $badlinks = array();
1339 if(!$links[1]) {
a9ef4a63 1340 $ret = get_string("nolinksfound","wiki")."<br /><br />";
39fcb981 1341 } else {
1342 foreach ($links[1] as $href) {
1343 #print "[ $href ]";
8f0cd6ef 1344 #$d = @implode("", @file($href));
39fcb981 1345 $d="";
1346 if($checkfd = @fopen($href, 'r')) {
1347 fclose($checkfd);
1348 $d="OK";
1349 }
1350 if (empty($d) || !strlen(trim($d)) || stristr("not found", $d) || stristr("error 404", $d)) {
a9ef4a63 1351 $ret.="[".get_string("linkdead","wiki")."] $href <br />\n";
39fcb981 1352 $badlinks[] = $href;
1353 } else {
a9ef4a63 1354 $ret.="[".get_string("linkok","wiki")."] $href <br />\n";
39fcb981 1355 }
8f0cd6ef 1356 }
39fcb981 1357 }
8f0cd6ef 1358
39fcb981 1359 /// Remove old Notices
1360