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