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