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