MDL-22142 backup - P3: add all the remaining dependencies
[moodle.git] / mod / wiki / lib.php
CommitLineData
e5cc530b 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
39fcb981 17
e5cc530b 18/**
19 * Library of functions and constants for module wiki
20 * (replace wiki with the name of your module and delete this line)
14a960ac 21 *
e5cc530b 22 * @package mod-wiki
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
39fcb981 26
e5cc530b 27/** @global array $WIKI_TYPES */
17da2e6f 28global $WIKI_TYPES;
713e7568 29$WIKI_TYPES = array ('teacher' => get_string('defaultcourseteacher'),
39fcb981 30 'group' => get_string('groups',"wiki"),
713e7568 31 'student' => get_string('defaultcoursestudent') );
4a9df373 32define("EWIKI_ESCAPE_AT", 0); # For the algebraic filter
39fcb981 33
22e846cd 34// How long locks stay around without being confirmed (seconds)
4db1861a 35define("WIKI_LOCK_PERSISTENCE",120);
22e846cd 36
37// How often to confirm that you still want a lock
4db1861a 38define("WIKI_LOCK_RECONFIRM",60);
22e846cd 39
4db1861a 40// Session variable used to store wiki locks
41define('SESSION_WIKI_LOCKS','wikilocks');
22e846cd 42
ee3cc995 43/*** Moodle 1.7 compatibility functions *****
ee3cc995 44 ********************************************/
e5cc530b 45
46/**
47 * @todo add some $cm caching if needed
48 * @param object $wiki
49 * @return object
50 */
ee3cc995 51function wiki_context($wiki) {
52 //TODO: add some $cm caching if needed
53 if (is_object($wiki)) {
54 $wiki = $wiki->id;
55 }
56 if (! $cm = get_coursemodule_from_instance('wiki', $wiki)) {
4d6ba0f0 57 print_error('invalidcoursemodule');
ee3cc995 58 }
59
60 return get_context_instance(CONTEXT_MODULE, $cm->id);
61}
62
e5cc530b 63/**
64 * @param object $wiki
65 * @param int $userid
66 * @return bool
67 */
ee3cc995 68function wiki_is_teacher($wiki, $userid=NULL) {
69 return has_capability('mod/wiki:manage', wiki_context($wiki), $userid);
70}
e5cc530b 71/**
72 * @param object $wiki
73 * @param int $userid
74 * @return bool
75 */
ee3cc995 76function wiki_is_teacheredit($wiki, $userid=NULL) {
33e7885e 77 return has_capability('mod/wiki:manage', wiki_context($wiki), $userid)
78 and has_capability('moodle/site:accessallgroups', wiki_context($wiki), $userid);
ee3cc995 79}
e5cc530b 80/**
81 * @param object $wiki
82 * @param int $userid
83 * @return bool
84 */
ee3cc995 85function wiki_is_student($wiki, $userid=NULL) {
86 return has_capability('mod/wiki:participate', wiki_context($wiki), $userid);
87}
e5cc530b 88/**
89 * @param object $wiki
90 * @param string $groups
91 * @param string $sort
92 * @param string $fields
93 * @return array
94 */
4da8ff66 95function wiki_get_students($wiki, $groups='', $sort='u.lastaccess', $fields='u.*') {
96 return $users = get_users_by_capability(wiki_context($wiki), 'mod/wiki:participate', $fields, $sort, '', '', $groups);
ee3cc995 97}
98
99/* end of compatibility functions */
100
e5cc530b 101/**
102 * Given an object containing all the necessary data,
103 * (defined by the form in mod_form.php) this function
104 * will create a new instance and return the id number
105 * of the new instance.
106 *
107 * @global object
108 * @param object $wiki
109 * @return bool|int
110 */
39fcb981 111function wiki_add_instance($wiki) {
c18269c7 112 global $DB;
14a960ac 113
39fcb981 114 $wiki->timemodified = time();
115
116 # May have to add extra stuff in here #
117
526efb55 118 /// Determine the pagename for this wiki and save.
119 $wiki->pagename = wiki_page_name($wiki);
c513f599 120
c18269c7 121 return $DB->insert_record("wiki", $wiki);
39fcb981 122}
123
e5cc530b 124/**
125 * Given an object containing all the necessary data,
126 * (defined by the form in mod_form.php) this function
127 * will update an existing instance with new data.
128 *
129 * @global object
130 * @param object @wiki
131 * @return bool
132 */
39fcb981 133function wiki_update_instance($wiki) {
c18269c7 134 global $DB;
39fcb981 135
526efb55 136 /// Determine the pagename for this wiki.
137 $wiki->pagename = wiki_page_name($wiki);
39fcb981 138
139 $wiki->timemodified = time();
140 $wiki->id = $wiki->instance;
c18269c7 141 return $DB->update_record("wiki", $wiki);
39fcb981 142}
143
e5cc530b 144/**
145 * Delete all Directories recursively
146 *
147 * @param string $basedir
148 */
39fcb981 149function wiki_rmdir($basedir) {
150 $handle = @opendir($basedir);
151 if($handle) {
152 while (false!==($folder = readdir($handle))) {
ee3cc995 153 if($folder != "." && $folder != ".." && $folder != "CVS") {
39fcb981 154 wiki_rmdir("$basedir/$folder"); // recursive
8f0cd6ef 155 }
39fcb981 156 }
157 closedir($handle);
158 }
159 @rmdir($basedir);
160}
161
e5cc530b 162/**
163 * Given an ID of an instance of this module,
164 * this function will permanently delete the instance
165 * and any data that depends on it.
166 *
167 * @global stdObject
168 * @global object
169 * @param int $id
170 * @return bool
171 */
39fcb981 172function wiki_delete_instance($id) {
56da74f3 173 global $CFG, $DB, $OUTPUT;
8f0cd6ef 174
c18269c7 175 if (! $wiki = $DB->get_record("wiki", array("id"=>$id))) {
39fcb981 176 return false;
177 }
178
179 $result = true;
180
181 #Delete Files
182### Should probably check regardless of this setting in case its been changed...
183 if($wiki->ewikiacceptbinary) {
184 if ($basedir = $CFG->dataroot."/".$wiki->course."/".$CFG->moddata."/wiki/$id") {
185 if ($files = get_directory_list($basedir)) {
186 foreach ($files as $file) {
187 #if ($file != $exception) {
188 unlink("$basedir/$file");
56da74f3 189 echo $OUTPUT->notification("Existing file '$file' has been deleted!");
39fcb981 190 #}
191 }
192 }
193 #if (!$exception) { // Delete directory as well, if empty
8f0cd6ef 194 wiki_rmdir("$basedir");
39fcb981 195 #}
8f0cd6ef 196 }
39fcb981 197 }
8f0cd6ef 198
39fcb981 199 # Delete any dependent records here #
c18269c7 200 if(!$DB->delete_records("wiki_locks", array("wikiid"=>$wiki->id))) {
22e846cd 201 $result = false;
202 }
203
c18269c7 204 if (! $DB->delete_records("wiki", array("id"=>$wiki->id))) {
39fcb981 205 $result = false;
206 }
207
208 /// Delete all wiki_entries and wiki_pages.
209 if (($wiki_entries = wiki_get_entries($wiki)) !== false) {
210 foreach ($wiki_entries as $wiki_entry) {
c18269c7 211 if (! $DB->delete_records("wiki_pages", array("wiki"=>$wiki_entry->id))) {
39fcb981 212 $result = false;
213 }
c18269c7 214 if (! $DB->delete_records("wiki_entries", array("id"=>$wiki_entry->id))) {
39fcb981 215 $result = false;
216 }
217 }
218 }
8f0cd6ef 219
39fcb981 220 return $result;
221}
222
e5cc530b 223/**
224 * Return a small object with summary information about what a
225 * user has done with a given particular instance of this module
226 * Used for user activity reports.
227 * $return->time = the time they did it
228 * $return->info = a short text description
229 *
230 * @param object $course
231 * @param object $user
232 * @param object $mod
233 * @param object $wiki
234 * @return null
235 */
39fcb981 236function wiki_user_outline($course, $user, $mod, $wiki) {
39fcb981 237
763692fd 238 $return = NULL;
39fcb981 239 return $return;
240}
241
e5cc530b 242/**
243 * Print a detailed representation of what a user has done with
244 * a given particular instance of this module, for user activity reports.
245 *
246 * @param object $course
247 * @param object $user
248 * @param object $mod
249 * @param object $wiki
250 * @return bool true
251 */
39fcb981 252function wiki_user_complete($course, $user, $mod, $wiki) {
39fcb981 253 return true;
254}
255
e5cc530b 256/**
257 * Given a course and a time, this module should find recent activity
258 * that has occurred in wiki activities and print it out.
259 * Return true if there was output, or false is there was none.
260 *
261 * @global stdClass
262 * @global object
263 * @param object $course
264 * @param bool $isteacher
265 * @param int $timestart
266 * @return bool
267 */
39fcb981 268function wiki_print_recent_activity($course, $isteacher, $timestart) {
9ab0a4fa 269 global $CFG, $DB, $OUTPUT;
14a960ac 270
655b09ca 271 $sql = "SELECT l.*, cm.instance
14a960ac
PS
272 FROM {log} l JOIN {course_modules} cm ON l.cmid = cm.id
273 WHERE l.time > ? AND l.course = ?
655b09ca 274 AND l.module = 'wiki' AND action LIKE 'edit%'
275 ORDER BY l.time ASC";
14a960ac 276
655b09ca 277 if (!$logs = $DB->get_records_sql($sql, array($timestart, $course->id))){
1fb5d4b0 278 return false;
279 }
280
dd97c328 281 $modinfo = get_fast_modinfo($course);
282 $wikis = array();
283
1fb5d4b0 284 foreach ($logs as $log) {
dd97c328 285 $cm = $modinfo->instances['wiki'][$log->instance];
286 if (!$cm->uservisible) {
287 continue;
1fb5d4b0 288 }
dd97c328 289
b09f731c 290 /// Process log->url and rebuild it here to properly clean the pagename - MDL-15896
291 $extractedpage = preg_replace('/^.*&page=/', '', $log->url);
292 $log->url = preg_replace('/page=.*$/', 'page='.urlencode($extractedpage), $log->url);
293
dd97c328 294 $wikis[$log->info] = wiki_log_info($log);
295 $wikis[$log->info]->pagename = $log->info;
296 $wikis[$log->info]->time = $log->time;
297 $wikis[$log->info]->url = str_replace('&', '&amp;', $log->url);
1fb5d4b0 298 }
39fcb981 299
dd97c328 300 if (!$wikis) {
301 return false;
302 }
9ab0a4fa 303 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki').':', 3);
dd97c328 304 foreach ($wikis as $wiki) {
305 print_recent_activity_note($wiki->time, $wiki, $wiki->pagename,
306 $CFG->wwwroot.'/mod/wiki/'.$wiki->url);
1fb5d4b0 307 }
dd97c328 308
309 return false;
1fb5d4b0 310}
311
e5cc530b 312/**
313 * @global stdClass
314 * @global object
315 * @param string $log
316 * @return array
317 */
1fb5d4b0 318function wiki_log_info($log) {
655b09ca 319 global $CFG, $DB;
320 return $DB->get_record_sql("SELECT u.firstname, u.lastname
321 FROM {user} u
322 WHERE u.id = ?", array($log->userid));
39fcb981 323}
324
e5cc530b 325/**
326 * Function to be run periodically according to the moodle cron
327 * This function searches for things that need to be done, such
328 * as sending out mail, toggling flags etc ...
329 *
330 * @global object
331 * @return bool
332 */
39fcb981 333function wiki_cron () {
655b09ca 334 global $DB;
39fcb981 335
22e846cd 336 // Delete expired locks
655b09ca 337 $result = $DB->delete_records_select('wiki_locks','lockedseen < '.(time()-WIKI_LOCK_PERSISTENCE));
39fcb981 338
22e846cd 339 return $result;
39fcb981 340}
341
e5cc530b 342/**
343 * Returns the users with data in one wiki
344 * (users with records in wiki_pages and wiki_entries)
345 *
346 * @global stdClass
347 * @global object
348 * @param int $wikiid
349 * @return array
350 */
39fcb981 351function wiki_get_participants($wikiid) {
e5cc530b 352
655b09ca 353 global $CFG, $DB;
472a28ca 354
355 //Get users from wiki_pages
655b09ca 356 $st_pages = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
357 FROM {user} u, {wiki_entries} e, {wiki_pages} p
358 WHERE e.wikiid = ? and
359 p.wiki = e.id and
360 u.id = p.userid", array($wikiid));
472a28ca 361
362 //Get users from wiki_entries
655b09ca 363 $st_entries = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
364 FROM {user} u, {wiki_entries} e
365 WHERE e.wikiid = ? and
366 u.id = e.userid", array($wikiid));
472a28ca 367
368 //Add entries to pages
369 if ($st_entries) {
370 foreach ($st_entries as $st_entry) {
371 $st_pages[$st_entry->id] = $st_entry;
372 }
373 }
374
375 return $st_pages;
39fcb981 376}
377
472a28ca 378
39fcb981 379//////////////////////////////////////////////////////////////////////////////////////
8f0cd6ef 380/// Any other wiki functions go here. Each of them must have a name that
39fcb981 381/// starts with wiki_
382
e5cc530b 383/**
384 * Return the passed in string in Wiki name format.
385 * Remove any leading and trailing whitespace, capitalize all the words
386 * and then remove any internal whitespace.
387 *
388 * @param string $wikinane
389 * @return string
390 */
39fcb981 391function wiki_wiki_name($wikiname) {
39fcb981 392
393 if (wiki_is_wiki_name($wikiname)) {
394 return $wikiname;
395 }
396 else {
397 /// Create uppercase words and remove whitespace.
398 $wikiname = preg_replace("/(\w+)\s/", "$1", ucwords(trim($wikiname)));
399
400 /// Check again - there may only be one word.
401 if (wiki_is_wiki_name($wikiname)) {
402 return $wikiname;
403 }
404 /// If there is only one word, append default wiki name to it.
405 else {
406 return $wikiname.get_string('wikidefaultpagename', 'wiki');
407 }
408 }
409}
410
e5cc530b 411/**
412 * Check for correct wikiname syntax and return true or false.
413 *
414 * @param string $wikiname
415 * @return bool
416 */
39fcb981 417function wiki_is_wiki_name($wikiname) {
39fcb981 418
8f0cd6ef 419 /// If there are spaces between the words, incorrect format.
39fcb981 420 if (preg_match_all('/\w+/', $wikiname, $out) > 1) {
421 return false;
422 }
423 /// If there isn't more than one group of uppercase letters separated by
424 /// lowercase letters or '_', incorrect format.
425 else if (preg_match_all('/[A-Z]+[a-z_]+/', $wikiname, $out) > 1) {
426 return true;
427 }
8f0cd6ef 428 else {
39fcb981 429 return false;
430 }
431}
432
e5cc530b 433/**
434 * Determines the wiki's page name and returns it.
435 * @param object $wiki
436 * @return string
437 */
39fcb981 438function wiki_page_name(&$wiki) {
39fcb981 439 if (!empty($wiki->initialcontent)) {
440 $ppos = strrpos($wiki->initialcontent, '/');
441 if ($ppos === false) {
442 $pagename = $wiki->initialcontent;
443 }
444 else {
445 $pagename = substr($wiki->initialcontent, $ppos+1);
446 }
447 }
448 else if (!empty($wiki->pagename)) {
449 $pagename = $wiki->pagename;
450 }
451 else {
526efb55 452 $pagename = $wiki->name;
39fcb981 453 }
454 return $pagename;
455}
456
e5cc530b 457/**
458 * @global stdClass
459 * @param object $wiki
460 * @return string
461 */
39fcb981 462function wiki_content_dir(&$wiki) {
463/// Determines the wiki's default content directory (if there is one).
464 global $CFG;
465
466 if (!empty($wiki->initialcontent)) {
467 $ppos = strrpos($wiki->initialcontent, '/');
468 if ($ppos === false) {
469 $subdir = '';
470 }
471 else {
472 $subdir = substr($wiki->initialcontent, 0, $ppos+1);
473 }
474 $contentdir = $CFG->dataroot.'/'.$wiki->course.'/'.$subdir;
475 }
476 else {
477 $contentdir = false;
478 }
479 return $contentdir;
480}
481
e5cc530b 482/**
483 * Returns all wikis for the specified course and optionally of the specified type.
484 *
485 * @global object
486 * @param int $courseid
487 * @param string $wtype
488 * @return array
489 */
c513f599 490function wiki_get_course_wikis($courseid, $wtype='*') {
655b09ca 491 global $DB;
c513f599 492
655b09ca 493 $select = 'course = ?';
494 $params = array($courseid);
c513f599 495 if ($wtype != '*') {
655b09ca 496 $select .= ' AND wtype = ?';
497 $params[] = $wtype;
c513f599 498 }
655b09ca 499 return $DB->get_records_select('wiki', $select, $params, 'id');
c513f599 500}
501
e5cc530b 502/**
503 * Returns true if wiki already has wiki entries; otherwise false.
504 * @global object
505 * @param object $wiki
506 * @return bool
507 */
39fcb981 508function wiki_has_entries(&$wiki) {
655b09ca 509 global $DB;
39fcb981 510
655b09ca 511 return $DB->record_exists('wiki_entries', array('wikiid'=>$wiki->id));
39fcb981 512}
513
e5cc530b 514/**
515 * Returns an array with all wiki entries indexed by entry id; false if there are none.
516 * If the optional $byindex is specified, returns the entries indexed by that field.
517 * Valid values for $byindex are 'student', 'group'.
518 *
519 * @global stdClass
520 * @global object
521 * @param object $wiki
522 * @param string $byindex
523 */
39fcb981 524function wiki_get_entries(&$wiki, $byindex=NULL) {
655b09ca 525 global $CFG, $DB;
14a960ac 526
39fcb981 527 if ($byindex == 'student') {
655b09ca 528 return $DB->get_records('wiki_entries', array('wikiid'=>$wiki->id), '',
39fcb981 529 'userid,id,wikiid,course,groupid,pagename,timemodified');
530 }
531 else if ($byindex == 'group') {
655b09ca 532 return $DB->get_records('wiki_entries', array('wikiid'=>$wiki->id), '',
39fcb981 533 'groupid,id,wikiid,course,userid,pagename,timemodified');
534 }
535 else {
655b09ca 536 return $DB->get_records('wiki_entries', array('wikiid'=>$wiki->id));
39fcb981 537 }
538}
539
e5cc530b 540/**
541 * Returns the wiki entry according to the wiki type.
542 *
543 * Optionally, will return wiki entry for $userid student wiki, or
544 * $groupid group or teacher wiki.
545 * Creates one if it needs to and it can.
546 *
547 * @global object
548 * @param object $wiki By reference
549 * @param object $course By reference
550 * @param int $userid
551 * @param int $groupid
552 * @return object
553 */
c513f599 554function wiki_get_default_entry(&$wiki, &$course, $userid=0, $groupid=0) {
c513f599 555 global $USER;
b82fcee1 556 /// If there is a groupmode, get the user's group id.
27978302 557 $groupmode = groups_get_activity_groupmode($wiki);
b82fcee1 558 // if groups mode is in use and no group supplied, use the first one found
559 if ($groupmode && !$groupid) {
87a65403 560 $mygroupids = groups_get_all_groups($course->id, $USER->id);
561 if ($mygroupids) {
562 $mygroupids = array_keys($mygroupids);
563 }
564 if($mygroupids && count($mygroupids)>0) {
b82fcee1 565 // Use first group. They ought to be able to change later
566 $groupid=$mygroupids[0];
567 } else {
568 // Whatever groups are in the course, pick one
2c386f82 569 $coursegroups = groups_get_all_groups($course->id);
b82fcee1 570 if(!$coursegroups || count($coursegroups)==0) {
4d6ba0f0 571 print_error('groupmodeerror', 'wiki');
f84d6a8d 572 }
b82fcee1 573 $unkeyed=array_values($coursegroups); // Make sure first item is index 0
574 $groupid=$unkeyed[0]->id;
575 }
576 }
577
c513f599 578 /// If the wiki entry doesn't exist, can this user create it?
579 if (($wiki_entry = wiki_get_entry($wiki, $course, $userid, $groupid)) === false) {
c513f599 580 if (wiki_can_add_entry($wiki, $USER, $course, $userid, $groupid)) {
581 wiki_add_entry($wiki, $course, $userid, $groupid);
582 if (($wiki_entry = wiki_get_entry($wiki, $course, $userid, $groupid)) === false) {
4d6ba0f0 583 print_error('cannotaddentry', 'wiki');
c513f599 584 }
585 }
586 }
fa22fd5f 587 //print_object($wiki_entry);
c513f599 588 return $wiki_entry;
589}
590
e5cc530b 591/**
592 * Returns the wiki entry according to the wiki type.
593 *
594 * Optionally, will return wiki entry for $userid student wiki, or
595 * $groupid group or teacher wiki.
596 * @global object
597 * @param object $wiki By reference
598 * @param object $course By reference
599 * @param int $userid
600 * @param int $groupid
601 * @return object
602 */
39fcb981 603function wiki_get_entry(&$wiki, &$course, $userid=0, $groupid=0) {
39fcb981 604 global $USER;
605
606 switch ($wiki->wtype) {
607 case 'student':
608 /// If a specific user was requested, return it, if allowed.
609 if ($userid and wiki_user_can_access_student_wiki($wiki, $userid, $course)) {
610 $wentry = wiki_get_student_entry($wiki, $userid);
611 }
612
613 /// If there is no entry for this user, check if this user is a teacher.
614 else if (!$wentry = wiki_get_student_entry($wiki, $USER->id)) {
ee3cc995 615/* if (wiki_is_teacher($wiki, $USER->id)) {
39fcb981 616 /// If this user is a teacher, return the first entry.
617 if ($wentries = wiki_get_entries($wiki)) {
618 $wentry = current($wentries);
619 }
620 }*/
621 }
622 break;
623
624 case 'group':
625 /// If there is a groupmode, get the user's group id.
27978302 626 $groupmode = groups_get_activity_groupmode($wiki);
745c1968 627 if($groupmode) {
628 if(!$groupid) {
87a65403 629 $mygroupids = groups_get_all_groups($course->id, $USER->id);
630 if ($mygroupids) {
631 $mygroupids = array_keys($mygroupids);
632 }
633 if($mygroupids && count($mygroupids)>0) {
745c1968 634 // Use first group. They ought to be able to change later
635 $groupid=$mygroupids[0];
636 } else {
637 // Whatever groups are in the course, pick one
2c386f82 638 $coursegroups = groups_get_all_groups($course->id);
745c1968 639 if(!$coursegroups || count($coursegroups)==0) {
4d6ba0f0 640 print_error('groupmodeerror', 'wiki');
f84d6a8d 641 }
745c1968 642 $unkeyed=array_values($coursegroups); // Make sure first item is index 0
643 $groupid=$unkeyed[0]->id;
644 }
645 }
f84d6a8d 646
745c1968 647 //echo "groupid is in wiki_get_entry ".$groupid."<br />";
648 /// If a specific group was requested, return it, if allowed.
649 if ($groupid and wiki_user_can_access_group_wiki($wiki, $groupid, $course)) {
650 $wentry = wiki_get_group_entry($wiki, $groupid);
651 } else {
4d6ba0f0 652 print_error('cannotaccessgroup', 'wiki');
39fcb981 653 }
654 }
655 /// If mode is 'nogroups', then groupid is zero.
656 else {
657 $wentry = wiki_get_group_entry($wiki, 0);
658 }
659 break;
660
661 case 'teacher':
662 /// If there is a groupmode, get the user's group id.
87a65403 663
664 if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
665 $groupmode = $cm->groupmode;
666 } else {
667 $groupmode = $course->groupmode;
668 }
669
670 if ($groupmode) {
671 $mygroupids = groups_get_all_groups($course->id, $USER->id);
672 if ($mygroupids) {
673 $mygroupids = array_keys($mygroupids);
674 }
fa22fd5f 675 $groupid = $groupid ? $groupid : $mygroupids[0]/*mygroupid($course->id)*/;
39fcb981 676 }
677
678 /// If a specific group was requested, return it, if allowed.
679 if (wiki_user_can_access_teacher_wiki($wiki, $groupid, $course)) {
680 $wentry = wiki_get_teacher_entry($wiki, $groupid);
681 }
682 break;
683 }
684 return $wentry;
685}
686
e5cc530b 687/**
688 * Returns the wiki entry for the wiki teacher type.
689 *
690 * @global object
691 * @param object $wiki
692 * @param int $groupid
693 * @return object
694 */
39fcb981 695function wiki_get_teacher_entry(&$wiki, $groupid=0) {
655b09ca 696 global $DB;
655b09ca 697 return $DB->get_record('wiki_entries', array('wikiid'=>$wiki->id, 'course'=>$wiki->course, 'groupid'=>$groupid));
39fcb981 698}
699
e5cc530b 700/**
701 * Returns the wiki entry for the given group.
702 *
703 * @global object
704 * @param object $wiki
705 * @param int $groupid
706 * @return object
707 */
39fcb981 708function wiki_get_group_entry(&$wiki, $groupid=null) {
655b09ca 709 global $DB;
655b09ca 710 return $DB->get_record('wiki_entries', array('wikiid'=>$wiki->id, 'groupid'=>$groupid));
39fcb981 711}
712
e5cc530b 713/**
714 * Returns the wiki entry for the given student.
715 *
716 * @global object
717 * @global object
718 * @param object $wiki
719 * @param int $userid
720 * @return object
721 */
39fcb981 722function wiki_get_student_entry(&$wiki, $userid=null) {
655b09ca 723 global $USER, $DB;
39fcb981 724
725 if (is_null($userid)) {
726 $userid = $USER->id;
727 }
655b09ca 728 return $DB->get_record('wiki_entries', array('wikiid'=>$wiki->id, 'userid'=>$userid));
39fcb981 729}
730
e5cc530b 731/**
732 * Returns a list of other wikis to display, depending on the type, group and user.
733 * Returns the key containing the currently selected entry as well.
734 *
735 * @global stdClass
736 * @global int
737 * @global object
738 * @param object $wiki
739 * @param object $user
740 * @param object $course
741 * @param int $currentid
742 * @return array
743 */
39fcb981 744function wiki_get_other_wikis(&$wiki, &$user, &$course, $currentid=0) {
87a65403 745 global $CFG, $id, $DB, $USER;
39fcb981 746
747 $wikis = false;
748
27978302 749 $groupmode = groups_get_activity_groupmode($wiki);
87a65403 750 $mygroupid = groups_get_all_groups($course->id, $USER->id);
751 if ($mygroupid) {
752 $mygroupid = array_keys($mygroupid);
753 }
ee3cc995 754 $isteacher = wiki_is_teacher($wiki, $user->id);
755 $isteacheredit = wiki_is_teacheredit($wiki, $user->id);
39fcb981 756
27978302 757 $groupingid = null;
758 $cm = new stdClass;
759 $cm->id = $wiki->cmid;
760 $cm->groupmode = $wiki->groupmode;
761 $cm->groupingid = $wiki->groupingid;
762 $cm->groupmembersonly = $wiki->groupmembersonly;
98da6021 763 if (!empty($cm->groupingid)) {
27978302 764 $groupingid = $wiki->groupingid;
765 }
14a960ac
PS
766
767
39fcb981 768 switch ($wiki->wtype) {
769
770 case 'student':
771 /// Get all the existing entries for this wiki.
772 $wiki_entries = wiki_get_entries($wiki, 'student');
14a960ac 773
98da6021 774 if (!empty($wiki->groupingid)) {
655b09ca 775 $sql = "SELECT gm.userid FROM {groups_members} gm " .
776 "INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid " .
777 "WHERE gg.groupingid = ? ";
14a960ac 778
655b09ca 779 $groupingmembers = $DB->get_records_sql($sql, array($wiki->groupingid));
27978302 780 }
14a960ac 781
222ac91b 782 if ($isteacher and (SITEID != $course->id)) {
39fcb981 783
784 /// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all student
785 /// wikis, regardless of creation.
222ac91b 786 if ((SITEID != $course->id) and ($isteacheredit or ($groupmode == NOGROUPS))) {
4f0c2d00 787 if ($students = get_users_by_capability(get_context_instance(CONTEXT_COURSE, $course->id), 'moodle/course:participate', '', '', '', '', '', '', false)) {
39fcb981 788 /// Default pagename is dependent on the wiki settings.
789 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
790
791 foreach ($students as $student) {
98da6021 792 if (!empty($wiki->groupingid) && empty($groupingmembers[$student->id])) {
27978302 793 continue;
794 }
39fcb981 795 /// If this student already has an entry, use its pagename.
796 if ($wiki_entries[$student->id]) {
797 $pagename = $wiki_entries[$student->id]->pagename;
798 }
799 else {
800 $pagename = $defpagename;
801 }
802
d0b95287 803 $key = 'view.php?id='.$id.'&userid='.$student->id.'&page='.$pagename;
39fcb981 804 $wikis[$key] = fullname($student).':'.$pagename;
805 }
806 }
807 }
808 else if ($groupmode == SEPARATEGROUPS) {
fa22fd5f 809
ee3cc995 810 if ($students = wiki_get_students($wiki, $mygroupid)) {
39fcb981 811 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
812 foreach ($students as $student) {
98da6021 813 if (!empty($wiki->groupingid) && empty($groupingmembers[$student->id])) {
27978302 814 continue;
815 }
39fcb981 816 /// If this student already has an entry, use its pagename.
817 if ($wiki_entries[$student->id]) {
818 $pagename = $wiki_entries[$student->id]->pagename;
819 }
820 else {
821 $pagename = $defpagename;
822 }
823
d0b95287 824 $key = 'view.php?id='.$id.'&userid='.$student->id.'&page='.$pagename;
39fcb981 825 $wikis[$key] = fullname($student).':'.$pagename;
826 }
827 }
828 }
829 else if ($groupmode == VISIBLEGROUPS) {
8f0cd6ef 830 /// Get all students in your group.
ee3cc995 831 if ($students = wiki_get_students($wiki, $mygroupid)) {
39fcb981 832 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
833 foreach ($students as $student) {
98da6021 834 if (!empty($wiki->groupingid) && empty($groupingmembers[$student->id])) {
27978302 835 continue;
836 }
39fcb981 837 /// If this student already has an entry, use its pagename.
838 if ($wiki_entries[$student->id]) {
839 $pagename = $wiki_entries[$student->id]->pagename;
840 }
841 else {
842 $pagename = $defpagename;
843 }
d0b95287 844 $key = 'view.php?id='.$id.'&userid='.$student->id.'&page='.$pagename;
39fcb981 845 $wikis[$key] = fullname($student).':'.$pagename;
846 }
847 }
848 /// Get all student wikis created, regardless of group.
98da6021 849 if (!empty($wiki->groupingid)) {
27978302 850 $sql = 'SELECT w.id, w.userid, w.pagename, u.firstname, u.lastname '
655b09ca 851 .' FROM {wiki_entries} w '
852 .' INNER JOIN {user} u ON w.userid = u.id '
853 .' INNER JOIN {groups_members} gm ON gm.userid = u.id '
854 .' INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid '
855 .' WHERE w.wikiid = ? AND gg.groupingid = ?'
27978302 856 .' ORDER BY w.id';
655b09ca 857 $params = array($wiki->id, $wiki->groupingid);
27978302 858 } else {
859 $sql = 'SELECT w.id, w.userid, w.pagename, u.firstname, u.lastname '
655b09ca 860 .' FROM {wiki_entries} w, {user} u '
861 .' WHERE w.wikiid = ? AND u.id = w.userid '
27978302 862 .' ORDER BY w.id';
655b09ca 863 $params = array($wiki->id);
27978302 864 }
655b09ca 865 $wiki_entries = $DB->get_records_sql($sql, $params);
39fcb981 866 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
867 foreach ($wiki_entries as $wiki_entry) {
d0b95287 868 $key = 'view.php?id='.$id.'&userid='.$wiki_entry->userid.'&page='.$wiki_entry->pagename;
39fcb981 869 $wikis[$key] = fullname($wiki_entry).':'.$wiki_entry->pagename;
870 if ($currentid == $wiki_entry->id) {
871 $wikis['selected'] = $key;
872 }
873 }
8f0cd6ef 874 }
39fcb981 875 }
876 else {
877 /// A user can see other student wikis if they are a member of the same
878 /// group (for separate groups) or there are visible groups, or if this is
879 /// a site-level wiki, and they are an administrator.
1d126f4f 880 if (($groupmode == VISIBLEGROUPS) or wiki_is_teacheredit($wiki)) {
39fcb981 881 $viewall = true;
882 }
883 else if ($groupmode == SEPARATEGROUPS) {
87a65403 884 $viewall = groups_get_all_groups($course->id, $USER->id);
885 if ($viewall) {
886 $viewall = array_keys($viewall);
887 }
39fcb981 888 }
889 else {
890 $viewall = false;
891 }
892
893 if ($viewall !== false) {
98da6021 894 if (!empty($wiki->groupingid)) {
27978302 895 $sql = 'SELECT w.id, w.userid, w.pagename, u.firstname, u.lastname '
655b09ca 896 .' FROM {wiki_entries} w '
897 .' INNER JOIN {user} u ON w.userid = u.id '
898 .' INNER JOIN {groups_members} gm ON gm.userid = u.id '
899 .' INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid '
900 .' WHERE w.wikiid = ? AND gg.groupingid = ?'
27978302 901 .' ORDER BY w.id';
655b09ca 902 $params = array($wiki->id, $wiki->groupingid);
27978302 903 } else {
904 $sql = 'SELECT w.id, w.userid, w.pagename, u.firstname, u.lastname '
655b09ca 905 .' FROM {wiki_entries} w, {user} u '
906 .' WHERE w.wikiid = ? AND u.id = w.userid '
27978302 907 .' ORDER BY w.id';
655b09ca 908 $params = array($wiki->id);
27978302 909 }
655b09ca 910 $wiki_entries = $DB->get_records_sql($sql, $params);
39fcb981 911 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
912 foreach ($wiki_entries as $wiki_entry) {
98da6021 913 if (!empty($wiki->groupingid) && empty($groupingmembers[$wiki_entry->userid])) {
27978302 914 continue;
915 }
14a960ac 916
2c386f82 917 if (($viewall === true) or groups_is_member($viewall, $wiki_entry->userid)) {
d0b95287 918 $key = 'view.php?id='.$id.'&userid='.$wiki_entry->userid.'&page='.$wiki_entry->pagename;
39fcb981 919 $wikis[$key] = fullname($wiki_entry).':'.$wiki_entry->pagename;
920 if ($currentid == $wiki_entry->id) {
921 $wikis['selected'] = $key;
922 }
923 }
924 }
925 }
926 }
927 break;
928
929 case 'group':
930 /// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all group
931 /// wikis, regardless of creation.
f84d6a8d 932
fa22fd5f 933 /// If user is a member of multiple groups, need to show current group etc?
39fcb981 934
935 /// Get all the existing entries for this wiki.
936 $wiki_entries = wiki_get_entries($wiki, 'group');
14a960ac 937
39fcb981 938 if ($groupmode and ($isteacheredit or ($isteacher and !$mygroupid))) {
27978302 939 if ($groups = groups_get_all_groups($course->id, null, $groupingid)) {
39fcb981 940 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
941 foreach ($groups as $group) {
942
943 /// If this group already has an entry, use its pagename.
944 if (isset($wiki_entries[$group->id])) {
945 $pagename = $wiki_entries[$group->id]->pagename;
946 }
947 else {
948 $pagename = $defpagename;
949 }
950
d0b95287 951 $key = 'view.php?id='.$id.($group->id?"&groupid=".$group->id:"").'&page='.$pagename;
39fcb981 952 $wikis[$key] = $group->name.':'.$pagename;
953 }
954 }
955 }
fa22fd5f 956 //if a studnet with multiple groups in SPG
957 else if ($groupmode == SEPARATEGROUPS){
27978302 958 if ($groups = groups_get_all_groups($course->id, $user->id, $groupingid)){
fa22fd5f 959
960 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
961 foreach ($groups as $group) {
962 /// If this group already has an entry, use its pagename.
963 if (isset($wiki_entries[$group->id])) {
964 $pagename = $wiki_entries[$group->id]->pagename;
965 }
966 else {
967 $pagename = $defpagename;
968 }
969 $key = 'view.php?id='.$id.($group->id?"&groupid=".$group->id:"").'&page='.$pagename;
970 $wikis[$key] = $group->name.':'.$pagename;
971 }
972
973 }
974
975 }
39fcb981 976 /// A user can see other group wikis if there are visible groups.
977 else if ($groupmode == VISIBLEGROUPS) {
98da6021 978 if (!empty($wiki->groupingid)) {
27978302 979 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
655b09ca 980 .' FROM {wiki_entries} w '
981 .' INNER JOIN {groups} g ON g.id = w.groupid '
982 .' INNER JOIN {groupings_groups} gg ON g.id = gg.groupid '
983 .' WHERE w.wikiid = ? AND gg.groupingid = ?'
27978302 984 .' ORDER BY w.groupid';
655b09ca 985 $params = array($wiki->id, $wiki->groupingid);
27978302 986 } else {
987 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
655b09ca 988 .' FROM {wiki_entries} w, {groups} g '
989 .' WHERE w.wikiid = ? AND g.id = w.groupid '
27978302 990 .' ORDER BY w.groupid';
655b09ca 991 $params = array($wiki->id);
27978302 992 }
655b09ca 993 $wiki_entries = $DB->get_records_sql($sql, $params);
39fcb981 994 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
995 foreach ($wiki_entries as $wiki_entry) {
d0b95287 996 $key = 'view.php?id='.$id.($wiki_entry->groupid?"&groupid=".$wiki_entry->groupid:"").'&page='.$wiki_entry->pagename;
39fcb981 997 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
998 if ($currentid == $wiki_entry->id) {
999 $wikis['selected'] = $key;
1000 }
1001 }
1002 }
1003 break;
1004
1005 case 'teacher':
1006 if ($isteacher) {
1007 /// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all
1008 /// teacher wikis, regardless of creation.
1009 if ($groupmode and ($isteacheredit or ($isteacher and !$mygroupid))) {
27978302 1010 if ($groups = groups_get_all_groups($course->id, null, $groupingid)) {
39fcb981 1011 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
39fcb981 1012 foreach ($groups as $group) {
1013 /// If this group already has an entry, use its pagename.
1014 if ($wiki_entries[$group->id]) {
1015 $pagename = $wiki_entries[$group->id]->pagename;
1016 }
1017 else {
1018 $pagename = $defpagename;
1019 }
1020
d0b95287 1021 $key = 'view.php?id='.$id.($group->id?"&groupid=".$group->id:"").'&page='.$pagename;
39fcb981 1022 $wikis[$key] = $group->name.':'.$pagename;
1023 }
1024 }
1025 }
1026 /// A teacher can see all other group teacher wikis.
1027 else if ($groupmode) {
98da6021 1028 if (!empty($wiki->groupingid)) {
27978302 1029 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
655b09ca 1030 .' FROM {wiki_entries} w '
1031 .' INNER JOIN {groups} g ON g.id = w.groupid '
1032 .' INNER JOIN {groupings_groups} gg ON g.id = gg.groupid '
1033 .' WHERE w.wikiid = ? AND gg.groupingid = ?'
27978302 1034 .' ORDER BY w.groupid';
655b09ca 1035 $params = array($wiki->id, $wiki->groupingid);
27978302 1036 } else {
39fcb981 1037 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
655b09ca 1038 .' FROM {wiki_entries} w, {groups} g '
1039 .' WHERE w.wikiid = ? AND g.id = w.groupid '
39fcb981 1040 .' ORDER BY w.groupid';
655b09ca 1041 $params = array($wiki->id);
27978302 1042 }
655b09ca 1043 $wiki_entries = $DB->get_records_sql($sql, $params);
39fcb981 1044 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
1045 foreach ($wiki_entries as $wiki_entry) {
d0b95287 1046 $key = 'view.php?id='.$id.($wiki_entry->groupid?"&groupid=".$wiki_entry->groupid:"").'&page='.$wiki_entry->pagename;
39fcb981 1047 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
1048 if ($currentid == $wiki_entry->id) {
1049 $wikis['selected'] = $key;
1050 }
1051 }
1052 }
1053 }
1054 else {
1055 /// A user can see other teacher wikis if they are a teacher, a member of the same
1056 /// group (for separate groups) or there are visible groups.
1057 if ($groupmode == VISIBLEGROUPS) {
1058 $viewall = true;
1059 }
1060 else if ($groupmode == SEPARATEGROUPS) {
1061 $viewall = $mygroupid;
1062 }
1063 else {
1064 $viewall = false;
1065 }
1066 if ($viewall !== false) {
98da6021 1067 if (!empty($wiki->groupingid)) {
27978302 1068 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
655b09ca 1069 .' FROM {wiki_entries} w '
1070 .' INNER JOIN {groups} g ON g.id = w.groupid '
1071 .' INNER JOIN {groupings_groups} gg ON g.id = gg.groupid '
1072 .' WHERE w.wikiid = ? AND gg.groupingid = ?'
27978302 1073 .' ORDER BY w.groupid';
655b09ca 1074 $params = array($wiki->id, $wiki->groupingid);
27978302 1075 } else {
1076 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
655b09ca 1077 .' FROM {wiki_entries} w, {groups} g '
1078 .' WHERE w.wikiid = ? AND g.id = w.groupid '
27978302 1079 .' ORDER BY w.groupid';
655b09ca 1080 $params = array($wiki->id);
27978302 1081 }
655b09ca 1082 $wiki_entries = $DB->get_records_sql($sql, $params);
39fcb981 1083 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
fa22fd5f 1084
1085
39fcb981 1086 foreach ($wiki_entries as $wiki_entry) {
fa22fd5f 1087 if (($viewall === true) or @in_array($wiki_entry->groupid, $viewall)/*$viewall == $wiki_entry->groupid*/) {
1088 $key = 'view.php?id='.$id.($wiki_entry->groupid?"&groupid=".$wiki_entry->groupid:"").'&page='.$wiki_entry->pagename;
39fcb981 1089 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
1090 if ($currentid == $wiki_entry->id) {
1091 $wikis['selected'] = $key;
1092 }
1093 }
1094 }
1095 }
1096 }
1097 break;
1098 }
14a960ac 1099
39fcb981 1100 return $wikis;
1101}
1102
e5cc530b 1103/**
1104 * Adds a new wiki entry of the specified type, unless already entered.
1105 *
1106 * No checking is done here. It is assumed that the caller has the correct
1107 * privileges to add this entry.
1108 *
1109 * @global object
1110 * @global object
1111 * @param object $wiki
1112 * @param object $course
1113 * @param int $userid
1114 * @param int $groupid
1115 * @return bool
1116 */
39fcb981 1117function wiki_add_entry(&$wiki, &$course, $userid=0, $groupid=0) {
655b09ca 1118 global $USER, $DB;
39fcb981 1119
1120 /// If this wiki already has a wiki_type entry, return false.
1121 if (wiki_get_entry($wiki, $course, $userid, $groupid) !== false) {
1122 return false;
1123 }
1124
ee3cc995 1125 $wiki_entry = new Object();
1126
39fcb981 1127 switch ($wiki->wtype) {
1128
1129 case 'student':
1130 $wiki_entry->wikiid = $wiki->id;
1131 $wiki_entry->userid = $userid ? $userid : $USER->id;
1132 $wiki_entry->pagename = wiki_page_name($wiki);
1133 $wiki_entry->timemodified = time();
1134 break;
1135
1136 case 'group':
1137 /// Get the groupmode. It's been added to the wiki object.
27978302 1138 $groupmode = groups_get_activity_groupmode($wiki);
39fcb981 1139
fa22fd5f 1140 ///give the first groupid by default and try
87a65403 1141 $mygroups = groups_get_all_groups($course->id, $USER->id);
1142 if ($mygroups) {
1143 $mygroups = array_keys($mygroups);
1144 }
fa22fd5f 1145
39fcb981 1146 /// If there is a groupmode, get the group id.
1147 if ($groupmode) {
fa22fd5f 1148 $groupid = $groupid ? $groupid : $mygroups[0]/*mygroupid($course->id)*/;
39fcb981 1149 }
1150 /// If mode is 'nogroups', then groupid is zero.
1151 else {
1152 $groupid = 0;
1153 }
1154 $wiki_entry->wikiid = $wiki->id;
1155 $wiki_entry->groupid = $groupid;
1156 $wiki_entry->pagename = wiki_page_name($wiki);
1157 $wiki_entry->timemodified = time();
f84d6a8d 1158
39fcb981 1159 break;
1160
1161 case 'teacher':
1162 /// Get the groupmode. It's been added to the wiki object.
27978302 1163 $groupmode = groups_get_activity_groupmode($wiki);
39fcb981 1164
1165 /// If there is a groupmode, get the user's group id.
1166 if ($groupmode and $groupid == 0) {
87a65403 1167 $mygroupid = groups_get_all_groups($course->id, $USER->id);
1168 if ($mygroupid) {
1169 $mygroupid = array_keys($mygroupid);
1170 }
fa22fd5f 1171 $groupid = $mygroupid[0]/*mygroupid($course->id)*/;
39fcb981 1172 }
1173
1174 $wiki_entry->wikiid = $wiki->id;
1175 $wiki_entry->course = $wiki->course;
1176 $wiki_entry->groupid = $groupid;
1177 $wiki_entry->pagename = wiki_page_name($wiki);
1178 $wiki_entry->timemodified = time();
1179 break;
1180 }
655b09ca 1181 $wiki_entry->pagename = $wiki_entry->pagename;
e61d4b39 1182
655b09ca 1183 return $DB->insert_record("wiki_entries", $wiki_entry, true);
39fcb981 1184}
1185
e5cc530b 1186/**
1187 * Returns true or false if the user can add a wiki entry for this wiki.
1188 *
1189 * @param object $wiki By reference
1190 * @param object $user By reference
1191 * @param object $course By reference
1192 * @param int $userid
1193 * @param int $groupid
1194 * @return bool
1195 */
39fcb981 1196function wiki_can_add_entry(&$wiki, &$user, &$course, $userid=0, $groupid=0) {
87a65403 1197 global $USER;
39fcb981 1198 /// Get the groupmode. It's been added to the wiki object.
27978302 1199 $groupmode = groups_get_activity_groupmode($wiki);
87a65403 1200 $mygroupid = groups_get_all_groups($course->id, $USER->id);
1201 if ($mygroupid) {
1202 $mygroupid = array_keys($mygroupid);
1203 }
39fcb981 1204
1205 switch ($wiki->wtype) {
1206
1207 case 'student':
1208/// A student can create their own wiki, if they are a member of that course.
1209/// A user can create their own wiki at the site level.
1210 if ($userid == 0) {
ee3cc995 1211 return (wiki_is_student($wiki, $user->id) or wiki_is_student($wiki, $user->id));
39fcb981 1212 }
1213/// An editing teacher can create any student wiki, or
1214/// a non-editing teacher, if not assigned to a group can create any student wiki, or if assigned to a group can
1215/// create any student wiki in their group.
1216 else {
ee3cc995 1217 return ((($userid == $user->id) and wiki_is_student($wiki, $user->id)) or wiki_is_teacheredit($wiki) or
2c386f82 1218 (wiki_is_teacher($wiki) and (!$groupmode or $mygroupid == 0 or (groups_is_member($mygroupid, $userid)))));
39fcb981 1219 }
1220 break;
1221
1222 case 'group':
1223 /// If mode is 'nogroups', then all participants can add wikis.
b82fcee1 1224 if (wiki_is_teacheredit($wiki, $user->id)) {
f84d6a8d 1225 return true;
b82fcee1 1226 }
f84d6a8d 1227
39fcb981 1228 if (!$groupmode) {
ee3cc995 1229 return (wiki_is_student($wiki, $user->id) or wiki_is_teacher($wiki, $user->id));
39fcb981 1230 }
1231 /// If not requesting a group, must be a member of a group.
1232 else if ($groupid == 0) {
8f0cd6ef 1233 return ($mygroupid != 0);
39fcb981 1234 }
1235 /// If requesting a group, must be an editing teacher, a non-editing teacher with no assigned group,
fa22fd5f 1236 /// or a non-editing teacher requesting their group. or a student in group, but wiki is empty.
39fcb981 1237 else {
ee3cc995 1238 return (wiki_is_teacheredit($wiki) or
1239 (wiki_is_teacher($wiki) and ($mygroupid == 0 or @in_array($groupid, $mygroupid))) or
1240 (wiki_is_student($wiki, $user->id) and @in_array($groupid, $mygroupid))
fa22fd5f 1241 );
39fcb981 1242 }
1243 break;
1244
1245 case 'teacher':
1246 /// If mode is 'nogroups', then all teachers can add wikis.
1247 if (!$groupmode) {
1d126f4f 1248 return wiki_is_teacher($wiki, $user->id);
39fcb981 1249 }
1250 /// If not requesting a group, must be a member of a group.
1251 else if ($groupid == 0) {
ee3cc995 1252 return ($mygroupid != 0 and wiki_is_teacher($wiki));
39fcb981 1253 }
eb712e42
EL
1254 /// If requesting a group, must be an editing teacher, a non-editing teacher with no assigned group,
1255 /// or a non-editing teacher requesting their group. or a student in group, but wiki is empty.
39fcb981 1256 else {
ee3cc995 1257 return (wiki_is_teacheredit($wiki) or
eb712e42
EL
1258 (wiki_is_teacher($wiki) and ($mygroupid == 0 or @in_array($groupid, $mygroupid))) or
1259 (wiki_is_student($wiki, $user->id) and @in_array($groupid, $mygroupid))
1260 );
39fcb981 1261 }
1262 break;
1263 }
1264
1265 return false;
1266}
1267
e5cc530b 1268/**
1269 * Returns true or false if the user can edit this wiki entry.
1270 *
1271 * @param object $wiki_entry by reference
1272 * @param object $wiki by reference
1273 * @param object $user by reference
1274 * @param object $course by reference
1275 * @return bool
1276 */
39fcb981 1277function wiki_can_edit_entry(&$wiki_entry, &$wiki, &$user, &$course) {
39fcb981 1278
1279 $can_edit = false;
27978302 1280 $groupmode = groups_get_activity_groupmode($wiki);
87a65403 1281 $mygroupid = groups_get_all_groups($course->id, $USER->id);
1282 if ($mygroupid) {
1283 $mygroupid = array_keys($mygroupid);
1284 }
39fcb981 1285
8f0cd6ef 1286 /// Editing teacher's and admins can edit all wikis, non-editing teachers can edit wikis in their groups,
39fcb981 1287 /// or all wikis if group mode is 'no groups' or they don't belong to a group.
1d126f4f 1288 if (wiki_is_teacheredit($wiki, $user->id) or
ee3cc995 1289 ((!$groupmode or $mygroupid == 0) and wiki_is_teacher($wiki, $user->id))) {
39fcb981 1290 $can_edit = true;
1291 }
1292 else {
1293 switch ($wiki->wtype) {
1294
1295 /// Only a teacher or the owner of a student wiki can edit it.
1296 case 'student':
1297 $can_edit = (($user->id == $wiki_entry->userid) or
ee3cc995 1298 ($groupmode and wiki_is_teacher($wiki, $user->id) and
2c386f82 1299 groups_is_member($mygroupid, $wiki_entry->userid)));
39fcb981 1300 break;
1301
1302 case 'group':
1303 /// If there is a groupmode, determine the user's group status.
1304 if ($groupmode) {
1305 /// If the user is a member of the wiki group, they can edit the wiki.
2c386f82 1306 $can_edit = groups_is_member($wiki_entry->groupid, $user->id);
39fcb981 1307 }
1308 /// If mode is 'nogroups', then all participants can edit the wiki.
1309 else {
ee3cc995 1310 $can_edit = (wiki_is_student($wiki, $user->id) or wiki_is_teacher($wiki, $user->id));
39fcb981 1311 }
1312 break;
1313
1314 case 'teacher':
1315 /// If there is a groupmode, determine the user's group status.
1316 if ($groupmode) {
1317 /// If the user is a member of the wiki group, they can edit the wiki.
2c386f82 1318 $can_edit = (wiki_is_teacher($wiki, $user->id) and groups_is_member($wiki_entry->groupid, $user->id));
39fcb981 1319 }
1320 else {
1d126f4f 1321 $can_edit = wiki_is_teacher($wiki, $user->id);
39fcb981 1322 }
1323 break;
1324 }
1325 }
39fcb981 1326 return $can_edit;
1327}
1328
e5cc530b 1329/**
1330 * @global object
1331 * @param object $wiki by reference
1332 * @param int $userid
1333 * @param object $course
1334 * @return bool
1335 */
39fcb981 1336function wiki_user_can_access_student_wiki(&$wiki, $userid, &$course) {
1337 global $USER;
1338
1339 /// Get the groupmode. It's been added to the wiki object.
27978302 1340 $groupmode = groups_get_activity_groupmode($wiki);
87a65403 1341 $usersgroup = groups_get_all_groups($course->id, $USER->id);
1342 if ($usersgroup) {
1343 $usersgroup = array_keys($usersgroup);
1344 }
ee3cc995 1345 $isteacher = wiki_is_teacher($wiki, $USER->id);
39fcb981 1346
1347 /// If this user is allowed to access this wiki then return TRUE.
1348 /// *** THIS COULD BE A PROBLEM, IF STUDENTS COULD EVER BE PART OF MORE THAN ONE GROUP ***
1349 /// A user can access a student wiki, if:
1350 /// - it is their wiki,
1351 /// - group mode is VISIBLEGROUPS,
1352 /// - group mode is SEPARATEGROUPS, and the user is a member of the requested user's group,
1353 /// - they are an editing teacher or administrator,
1354 /// - they are a non-editing teacher not assigned to a specific group,
1355 /// - they are a non-editing teacher and group mode is NOGROUPS.
1356 /// - they are an administrator (mostly for site-level wikis).
1357 if (($userid and ($USER->id == $userid)) or ($groupmode == VISIBLEGROUPS) or
2c386f82 1358 (($groupmode == SEPARATEGROUPS) and groups_is_member($usersgroup, $userid)) or
ee3cc995 1359 (wiki_is_teacheredit($wiki, $USER->id)) or
1d126f4f 1360 (wiki_is_teacher($wiki, $USER->id) and (!$usersgroup or $groupmode == NOGROUPS))) {
39fcb981 1361 $can_access = true;
1362 }
1363 else {
1364 $can_access = false;
1365 }
1366 return $can_access;
1367}
1368
e5cc530b 1369/**
1370 * @global object
1371 * @param object $wiki by reference
1372 * @param int $groupid
1373 * @param object $course
1374 * @return bool
1375 */
39fcb981 1376function wiki_user_can_access_group_wiki(&$wiki, $groupid, &$course) {
1377 global $USER;
1378
1379 /// Get the groupmode. It's been added to the wiki object.
27978302 1380 $groupmode = groups_get_activity_groupmode($wiki);
87a65403 1381 $usersgroup = groups_get_all_groups($course->id, $USER->id);
1382 if ($usersgroup) {
1383 $usersgroup = array_keys($usersgroup);
1384 }
ee3cc995 1385 $isteacher = wiki_is_teacher($wiki, $USER->id);
39fcb981 1386
1387 /// A user can access a group wiki, if:
1388 /// - group mode is NOGROUPS,
1389 /// - group mode is VISIBLEGROUPS,
1390 /// - group mode is SEPARATEGROUPS, and they are a member of the requested group,
1391 /// - they are an editing teacher or administrator,
1392 /// - they are a non-editing teacher not assigned to a specific group.
8f0cd6ef 1393 if (($groupmode == NOGROUPS) or ($groupmode == VISIBLEGROUPS) or
fa22fd5f 1394 (($groupmode == SEPARATEGROUPS) and @in_array($groupid, $usersgroup)/*($usersgroup == $groupid)*/) or
ee3cc995 1395 (wiki_is_teacheredit($wiki, $USER->id)) or
1396 (wiki_is_teacher($wiki, $USER->id) and !$usersgroup)) {
39fcb981 1397 $can_access = true;
1398 }
1399 else {
1400 $can_access = false;
1401 }
1402 return $can_access;
1403}
1404
e5cc530b 1405/**
1406 * @global object
1407 * @param object $wiki by reference
1408 * @param int $groupid
1409 * @param object $course by reference
1410 * @return bool
1411 */
39fcb981 1412function wiki_user_can_access_teacher_wiki(&$wiki, $groupid, &$course) {
1413 global $USER;
1414
1415 /// Get the groupmode. It's been added to the wiki object.
27978302 1416 $groupmode = groups_get_activity_groupmode($wiki);
39fcb981 1417
1418 /// A user can access a teacher wiki, if:
1419 /// - group mode is NOGROUPS,
1420 /// - group mode is VISIBLEGROUPS,
1421 /// - group mode is SEPARATEGROUPS, and they are a member of the requested group,
1422 /// - they are a teacher or administrator,
87a65403 1423
1424 $mygroupids = groups_get_all_groups($course->id, $USER->id);
1425 if ($mygroupids) {
1426 $mygroupids = array_keys($mygroupids);
1427 }
1428
8f0cd6ef 1429 if (($groupmode == NOGROUPS) or ($groupmode == VISIBLEGROUPS) or
87a65403 1430 (($groupmode == SEPARATEGROUPS) and (@in_array($groupid, $mygroupids)/*mygroupid($course->id) == $groupid*/)) or
ee3cc995 1431 (wiki_is_teacher($wiki, $USER->id))){
39fcb981 1432 $can_access = true;
1433 }
1434 else {
1435 $can_access = false;
1436 }
1437 return $can_access;
1438}
1439
e5cc530b 1440/**
1441 * @global object
1442 * @param object $wiki_entry by reference
1443 * @return string
1444 */
39fcb981 1445function wiki_get_owner(&$wiki_entry) {
655b09ca 1446 global $DB;
1447
39fcb981 1448 if ($wiki_entry->userid > 0) {
655b09ca 1449 $user = $DB->get_record('user', array('id'=>$wiki_entry->userid));
39fcb981 1450 $owner = fullname($user);
1451 }
1452 else if ($wiki_entry->groupid > 0) {
f3f7610c 1453 $owner = groups_get_group_name($wiki_entry->groupid); //TODO:check.
39fcb981 1454 }
1455 else if ($wiki_entry->course > 0) {
655b09ca 1456 $course = $DB->get_record('course', array('id'=>$wiki_entry->course));
39fcb981 1457 $owner = $course->shortname;
1458 }
1459 else {
a9de0242 1460 $owner = '- '.get_string("ownerunknown","wiki").' -';
39fcb981 1461 }
1462 return $owner;
1463}
1464
e5cc530b 1465/**
1466 * @todo Add Group and User !!!
1467 *
1468 * @global stdClass
1469 * @param int $cmid
1470 * @param string $search
1471 * @param int $userid
1472 * @param int $groupid
1473 * @param bool $return
1474 * @return string
1475 */
39fcb981 1476function wiki_print_search_form($cmid, $search="", $userid, $groupid, $return=false) {
1477 global $CFG;
1478 # TODO: Add Group and User !!!
b7dc2256 1479 $output = "<form id=\"search\" action=\"$CFG->wwwroot/mod/wiki/view.php\">";
beae1797 1480 $output .="<fieldset class='invisiblefieldset'>";
1481 $output .= "<span style='font-size:0.6em;'>";
a9ef4a63 1482 $output .= "<input value=\"".get_string("searchwiki", "wiki").":\" type=\"submit\" />";
1483 $output .= "<input name=\"id\" type=\"hidden\" value=\"$cmid\" />";
1484 $output = $output.($groupid?"<input name=\"groupid\" type=\"hidden\" value=\"$groupid\" />":"");
1485 $output = $output.($userid?"<input name=\"userid\" type=\"hidden\" value=\"$userid\" />":"");
4c2740fc 1486 $output .= "<input name=\"q\" type=\"text\" size=\"20\" value=\"".s($search)."\" />".' ';
beae1797 1487 $output .= "</span>";
a9ef4a63 1488 $output .= "<input name=\"page\" type=\"hidden\" value=\"SearchPages\" />";
beae1797 1489 $output .= "</fieldset>";
39fcb981 1490 $output .= "</form>";
1491
1492 if ($return) {
1493 return $output;
1494 }
1495 echo $output;
1496}
1497
e5cc530b 1498/**
1499 * Prints a link-list of special wiki-pages
1500 *
1501 * @global object
1502 * @global string
1503 * @param int $cmid
1504 * @param bool $binary
1505 * @param bool $return
1506 */
39fcb981 1507function wiki_print_wikilinks_block($cmid, $binary=false, $return=false) {
928c9b4a 1508 global $CFG, $ewiki_title, $OUTPUT;
39fcb981 1509
1510 $links=array();
1511
f80b0733 1512 $links["SiteMap"]=get_string("sitemap", "wiki");
39fcb981 1513 $links["PageIndex"]=get_string("pageindex", "wiki");
1514 $links["NewestPages"]=get_string("newestpages", "wiki");
1515 $links["MostVisitedPages"]=get_string("mostvisitedpages", "wiki");
1516 $links["MostOftenChangedPages"]=get_string("mostoftenchangedpages", "wiki");
1517 $links["UpdatedPages"]=get_string("updatedpages", "wiki");
1518 $links["OrphanedPages"]=get_string("orphanedpages", "wiki");
1519 $links["WantedPages"]=get_string("wantedpages", "wiki");
a9de0242 1520 $links["WikiExport"]=get_string("wikiexport", "wiki");
39fcb981 1521 if($binary) {
1522 $links["FileDownload"]=get_string("filedownload", "wiki");
1523 }
14a960ac 1524
928c9b4a 1525 $name = null;
1526 if (preg_match('/([a-zA-Z0-9\-_]*)=$/', EWIKI_SCRIPT, $matches)) {
1527 $name = $matches[1];
1528 }
1529
f8dab966 1530 echo $OUTPUT->single_select(new moodle_url(EWIKI_SCRIPT), $name, $links, null, array(''=>get_string("choosewikilinks", "wiki")), 'wikilinks');
39fcb981 1531}
1532
e5cc530b 1533/**
1534 * Displays actions which can be performed on the page
1535 * @param int $cmit
1536 * @param array $specialpages
1537 * @param array $page
1538 * @param string $action
1539 * @param bool $binary
1540 * @param bool $canedit
1541 */
087fef3e 1542function wiki_print_page_actions($cmid, $specialpages, $page, $action, $binary=false, $canedit=true) {
39fcb981 1543
1544 $page=array();
1545
1546 // Edit this Page
1547 if (in_array($action, array("edit", "links", "info", "attachments"))) {
8f0cd6ef 1548 $page["view/$page"]=get_string("viewpage","wiki");
39fcb981 1549 }
087fef3e 1550 if ($canedit && !in_array($page, $specialpages) && $action != "edit") {
1551 $page["edit/$page"]=get_string("editthispage","wiki");
39fcb981 1552 }
1553 if ($action != "links") {
087fef3e 1554 $page["links/$page"]=get_string("backlinks","wiki");
39fcb981 1555 }
087fef3e 1556 if ($canedit && !in_array($page, $specialpages) && $action!="info") {
1557 $page["info/$page"]=get_string("pageinfo","wiki");
39fcb981 1558 }
087fef3e 1559 if($canedit && $binary && !in_array($page, $specialpages) && $action != "attachments") {
1560 $page["attachments/$page"]=get_string("attachments","wiki");
39fcb981 1561 }
14a960ac 1562
928c9b4a 1563 $name = null;
1564 if (preg_match('/([a-zA-Z0-9\-_]*)=$/', EWIKI_SCRIPT, $matches)) {
1565 $name = $matches[1];
1566 }
39fcb981 1567
f8dab966 1568 echo $OUTPUT->single_select(new moodle_url(EWIKI_SCRIPT), $name, $page, null, array(''=>get_string("action", "wiki")), 'wikiactions');
39fcb981 1569}
1570
e5cc530b 1571/**
1572 * Displays actions which can be performed on the page
1573 *
1574 * @param object $wiki
1575 * @param int $cmid
1576 * @param int $userid
1577 * @param int $groupid
1578 * @param object $page
1579 * @param bool $noeditor
1580 * @param object $course
1581 */
087fef3e 1582function wiki_print_administration_actions($wiki, $cmid, $userid, $groupid, $page, $noeditor, $course) {
928c9b4a 1583 global $OUTPUT;
f84d6a8d 1584 /// Create the URL
39fcb981 1585 $ewscript = 'admin.php?id='.$cmid;
928c9b4a 1586 if (isset($userid) && $userid!=0) $ewscript .= '&userid='.$userid;
1587 if (isset($groupid) && $groupid!=0) $ewscript .= '&groupid='.$groupid;
1588 if (isset($page)) $ewscript .= '&page='.$page;
39fcb981 1589
c513f599 1590
1591 /// Build that action array according to wiki flags.
1592 $action = array();
ee3cc995 1593 $isteacher = wiki_is_teacher($wiki);
c513f599 1594
1595 if ($wiki->setpageflags or $isteacher) {
1596 $action['setpageflags'] = get_string('setpageflags', 'wiki');
1597 }
1598 if ($wiki->removepages or $isteacher) {
1599 $action['removepages'] = get_string('removepages', 'wiki');
1600 }
1601 if ($wiki->strippages or $isteacher) {
1602 $action['strippages'] = get_string('strippages', 'wiki');
1603 }
1604 if ($wiki->revertchanges or $isteacher) {
1605 $action['revertpages'] = get_string('revertpages', 'wiki');
1606 }
1607
39fcb981 1608 if($noeditor) {
1609 $action["checklinks"]=get_string("checklinks", "wiki");
1610 }
f8dab966 1611 echo $OUTPUT->single_select(new moodle_url($ewscript), 'action', $action, null, array(''=>get_string("chooseadministration", "wiki")), 'wikiadministration');
39fcb981 1612}
1613
e5cc530b 1614/**
1615 * @uses EWIKI_DB_F_TEXT
1616 * @uses EWIKI_DB_F_BINARY
1617 * @uses EWIKI_DB_F_DISABLED
1618 * @uses EWIKI_DB_F_HTML
1619 * @uses EWIKI_DB_F_READONLY
1620 * @uses EWIKI_DB_F_WRITEABLE
1621 * @return array
1622 */
39fcb981 1623function wiki_admin_get_flagarray() {
1624 $ret = array(
1625 EWIKI_DB_F_TEXT => get_string("flagtxt","wiki"),
1626 EWIKI_DB_F_BINARY => get_string("flagbin","wiki"),
1627 EWIKI_DB_F_DISABLED => get_string("flagoff","wiki"),
1628 EWIKI_DB_F_HTML => get_string("flaghtm","wiki"),
1629 EWIKI_DB_F_READONLY => get_string("flagro","wiki"),
1630 EWIKI_DB_F_WRITEABLE => get_string("flagwr","wiki"),
1631 );
8f0cd6ef 1632
39fcb981 1633 return $ret;
1634}
1635
1636///////// Ewiki Administration. Mostly taken from the ewiki/tools folder and changed
e5cc530b 1637
1638/**
1639 * @param bool $pageflagstatus
1640 * @return object
1641 */
39fcb981 1642function wiki_admin_setpageflags_list($pageflagstatus) {
8f0cd6ef 1643 $FD = wiki_admin_get_flagarray();
7ce73e92 1644 $table = new html_table();
39fcb981 1645 $table->head = array(get_string("pagename","wiki"), get_string("flags","wiki"));
1646 if($pageflagstatus) {
1647 $table->head[]=get_string("status","wiki");
1648 }
8f0cd6ef 1649
39fcb981 1650 $result = ewiki_database("GETALL", array("version", "flags"));
1651 while ($row = $result->get()) {
1652 $id = $row["id"];
1653 $data = ewiki_database("GET", $row);
1654
1655 $cell_pagename="";
1656 $cell_flags="";
1657 if ($data["flags"] & EWIKI_DB_F_TEXT) {
b1503075 1658 $cell_pagename .= '<a href="' . EWIKI_SCRIPT . $id . '">';
39fcb981 1659 } else {
b1503075 1660 $cell_pagename .= '<a href="' . EWIKI_SCRIPT_BINARY . $id . '">';
39fcb981 1661 }
b1503075 1662 $cell_pagename .= s($id) . '</a> / '.get_string("version","wiki").": ".$row["version"];
8f0cd6ef 1663
39fcb981 1664 foreach ($FD as $n=>$str) {
b1503075 1665 $cell_flags .='<input type="checkbox" name="flags['. rawurlencode($id)
1666 . '][' . $n . ']" value="1" '
82056f67 1667 . (($data["flags"] & $n) ? "checked=\"checked\"" : "")
a9ef4a63 1668 . ' />'.$str. ' ';
39fcb981 1669 }
1670 if($pageflagstatus) {
1671 $table->data[]=array($cell_pagename, $cell_flags, $pageflagstatus[$id]);
1672 } else {
1673 $table->data[]=array($cell_pagename, $cell_flags);
1674 }
1675 }
1676 return $table;
1677}
1678
e5cc530b 1679/**
1680 * @param array $pageflags
1681 * @return array Status array
1682 */
39fcb981 1683function wiki_admin_setpageflags($pageflags) {
1684 $FD = wiki_admin_get_flagarray();
8f0cd6ef 1685
39fcb981 1686 $status=array();
8f0cd6ef 1687 if($pageflags) {
39fcb981 1688 foreach($pageflags as $page=>$fa) {
1689
1690 $page = rawurldecode($page);
1691
1692 $flags = 0;
1693 $fstr = "";
1694 foreach($fa as $num=>$isset) {
1695 if ($isset) {
1696 $flags += $num;
1697 $fstr .= ($fstr?",":""). $FD[$num];
1698 }
1699 }
1700
1701 #$status[$page] .= "{$flags}=[{$fstr}]";
1702
1703 $data = ewiki_database("GET", array("id" => $page));
1704
1705 if ($data["flags"] != $flags) {
1706 $data["flags"] = $flags;
1707 $data["author"] = "ewiki-tools, " . ewiki_author();
1708 $data["version"]++;
1709 ewiki_database("WRITE", $data);
1710 $status[$page] = "<b>".get_string("flagsset","wiki")."</b> ".$status[$page];
1711 }
1712 }
1713 }
8f0cd6ef 1714 return $status;
39fcb981 1715}
1716
e5cc530b 1717/**
1718 * @param bool $listall
1719 * @return object
1720 */
39fcb981 1721function wiki_admin_remove_list($listall="") {
1722 /// Table header
7ce73e92 1723 $table = new html_table();
39fcb981 1724 $table->head = array("&nbsp;", get_string("pagename","wiki"), get_string("errororreason","wiki"));
8f0cd6ef 1725
39fcb981 1726 /// Get all pages
1727 $result = ewiki_database("GETALL", array("version"));
1728 $selected = array();
1729
1730 /// User wants to see all pages
1731 if ($listall) {
1732 while ($row = $result->get()) {
a9ef4a63 1733 $selected[$row["id"]] = get_string("listall","wiki")."<br />";
39fcb981 1734 }
1735 }
8f0cd6ef 1736 while ($page = $result->get()) {
39fcb981 1737 $id = $page["id"];
1738 $page = ewiki_database("GET", array("id"=>$id));
1739 $flags = $page["flags"];
8f0cd6ef 1740 #print "$id ".strlen(trim(($page["content"])))."<br />";
39fcb981 1741
1742 if (!strlen(trim(($page["content"]))) && !($flags & EWIKI_DB_F_BINARY)) {
a9ef4a63 1743 @$selected[$id] .= get_string("emptypage","wiki")."<br />";
39fcb981 1744 }
8f0cd6ef 1745
39fcb981 1746 // Check for orphaned pages
1747 $result2 = ewiki_database("SEARCH", array("content" => $id));
1748 $orphanedpage=true;
1749 if ($result2 && $result2->count()) {
1750 while ($row = $result2->get()) {
1751 $checkcontent = ewiki_database("GET", array("id"=>$row["id"]));
1752 $checkcontent = strtolower($checkcontent["content"]);
8f0cd6ef 1753
39fcb981 1754 if(strpos($checkcontent, strtolower($id)) !== false) {
1755 $orphanedpage=false;
1756 }
1757
a9ef4a63 1758 #echo "rc({$row['id']})==>($id): $check2 <br />";
39fcb981 1759 }
1760 }
1761
8f0cd6ef 1762 /// Some more reasons for Deletion...
39fcb981 1763 if ($orphanedpage && $id!=EWIKI_PAGE_INDEX &&!($flags & EWIKI_DB_F_BINARY)) {
a9ef4a63 1764 @$selected[$id] .= get_string("orphanedpage","wiki")."<br />";
39fcb981 1765 }
1766
1767 if ($flags & EWIKI_DB_F_DISABLED) {
a9ef4a63 1768 @$selected[$id] .= get_string("disabledpage","wiki")."<br />";
39fcb981 1769 }
1770
1771 if (($flags & 3) == 3) {
a9ef4a63 1772 @$selected[$id] .= get_string("errorbinandtxt","wiki")."<br />";
39fcb981 1773 }
1774
1775 if (!($flags & 3)) {
a9ef4a63 1776 @$selected[$id] .= get_string("errornotype","wiki")."<br />";
39fcb981 1777 }
1778
1779 if ($flags & EWIKI_DB_F_HTML) {
a9ef4a63 1780 @$selected[$id] .= get_string("errorhtml","wiki")."<br />";
39fcb981 1781 }
1782
1783 if (($flags & EWIKI_DB_F_READONLY) && !($flags & EWIKI_DB_F_BINARY)) {
a9ef4a63 1784 @$selected[$id] .= get_string("readonly","wiki")."<br />";
39fcb981 1785 }
1786
1787 if (($flags & EWIKI_DB_F_READONLY) && ($flags & EWIKI_DB_F_WRITEABLE)) {
a9ef4a63 1788 @$selected[$id] .= get_string("errorroandwr","wiki")."<br />";
39fcb981 1789 }
1790
1791 if (strlen($page["content"]) >= 65536) {
a9ef4a63 1792 @$selected[$id] .= get_string("errorsize","wiki")."<br />";
39fcb981 1793 }
1794
1795 if (strpos($page["refs"], "\n".get_string("deletemewikiword","wiki")."\n")!==false) {
a9ef4a63 1796 @$selected[$id] .= get_string("deletemewikiwordfound","wiki",get_string("deletemewikiword","wiki"))."<br />";
39fcb981 1797 }
1798 }
8f0cd6ef 1799
1800 foreach ($selected as $id => $reason) {
b1503075 1801 $table_checkbox='<input type="checkbox" value="'.rawurlencode($id).'" name="pagestodelete[]" />';
39fcb981 1802
1803 #-- link & id
1804 if (strpos($id, EWIKI_IDF_INTERNAL) === false) {
b1503075 1805 $table_page='<a href="' . ewiki_script("", $id) . '">';
39fcb981 1806 } else {
b1503075 1807 $table_page='<a href="' . ewiki_script_binary("", $id) . '">';
39fcb981 1808 }
b1503075 1809 $table_page .= s($id) . '</a>';
39fcb981 1810
1811 #-- print reason
1812 $table_reason=$reason;
8f0cd6ef 1813
39fcb981 1814 $table->data[]=array($table_checkbox, $table_page, $table_reason);
8f0cd6ef 1815 }
1816
1817 return $table;
39fcb981 1818}
1819
e5cc530b 1820/**
1821 * This function actually removes the pages
1822 * @param array $pagestodelete
1823 * @param object $course
1824 * @param object $wiki
1825 * @param int $userid
1826 * @param int $groupid
1827 * @return string
1828 */
39fcb981 1829function wiki_admin_remove($pagestodelete, $course, $wiki, $userid, $groupid) {
8f0cd6ef 1830 $ret="";
39fcb981 1831 foreach ($pagestodelete as $id) {
1832
1833 $id = rawurldecode($id);
1834
1835 $data = ewiki_database("GET", array("id"=>$id));
1836 for ($version=1; $version<=$data["version"]; $version++) {
1837 ewiki_database("DELETE", array("id"=>$id, "version"=>$version));
1838 if($data["flags"] & EWIKI_DB_F_BINARY) {
1839 $filepath=moodle_binary_get_path($id, $data["meta"], $course, $wiki, $userid, $groupid);
1840 @unlink("$filepath");
1841 }
1842 }
8f0cd6ef 1843
39fcb981 1844 }
1845 return $ret;
1846}
1847
e5cc530b 1848/**
1849 * @param array $pagestostrip
1850 * @param string $version
1851 * @param string $err
1852 * @return object
1853 */
39fcb981 1854function wiki_admin_strip_list($pagestostrip="",$version="",$err="") {
1855 /// Table header
7ce73e92 1856 $table = new html_table();
39fcb981 1857 $table->head = array("&nbsp;", get_string("pagename","wiki"), get_string("deleteversions","wiki"));
8f0cd6ef 1858
39fcb981 1859 $vc=ewiki_database("COUNTVERSIONS", array());
1860 $result = ewiki_database("GETALL",array());
1861 $i=0;
1862 while ($row = $result->get()) {
1863 $id = $row["id"];
1864 if($vc[$id]>1) {
1865 $error="";
1866 if($err[$id]) {
1867 $error=" ".join(", ",$err[$id]);
8f0cd6ef 1868 }
39fcb981 1869 $checked="";
1870 if($pagestostrip=="" || $pagestostrip[$i]) {
82056f67 1871 $checked=" checked=\"checked\"";
39fcb981 1872 }
1873 if($version=="") {
1874 $versiondefault="1-".($row["version"]-1);
1875 } else {
1876 $versiondefault=$version[$i];
1877 }
8f0cd6ef 1878 $table->data[]=array('<input type="checkbox" value="'.rawurlencode($id).'" name="pagestostrip['.$i.']" '.$checked.' />',
2bc0b7a0 1879 '<A HREF="'.EWIKI_SCRIPT.$id.'">'.s($id).'</A> / '.get_string("version","wiki").": ".$row["version"],
a9ef4a63 1880 '<input name="version['.$i.']" value="'.$versiondefault.'" size="7" />'.$error);
8f0cd6ef 1881
39fcb981 1882 }
1883 $i++;
1884 }
1885 return $table;
1886}
1887
e5cc530b 1888/**
1889 * @param array $pagestostrip
1890 * @param string $version
1891 * @param string $err
1892 * @return array
1893 */
39fcb981 1894function wiki_admin_strip_versions($pagestostrip, $version, &$err) {
1895 $ret=array();
1896 foreach ($pagestostrip as $key => $id_ue) {
8f0cd6ef 1897
39fcb981 1898 $id = rawurldecode($id_ue);
1899 if (preg_match('/^(\d+)[-\s._:]+(\d+)$/', trim($version[$key]), $uu)) {
1900 $versA = $uu[1];
1901 $versZ = $uu[2];
8f0cd6ef 1902
39fcb981 1903 // Let the last Version in the database
d2481a47 1904 $checkdata = ewiki_database("GET", array("id" => $id));
39fcb981 1905 if($versZ>=$checkdata["version"]) {
8f0cd6ef 1906 $err[$id][] = get_string("versionrangetoobig","wiki");
39fcb981 1907 } else {
1908 if($versA<=$versZ) {
1909 for ($v=$versA; $v<=$versZ; $v++) {
1910 $ret[$id][]=$v;
1911 }
1912 } else {
1913 $err[$id][]=get_string("wrongversionrange","wiki",$version[$key]);
1914 }
1915 }
1916 }
1917 else {
1918 $err[$id][]=get_string("wrongversionrange","wiki",$version[$key]);
1919 }
1920 }
1921 return $ret;
1922}
8f0cd6ef 1923
e5cc530b 1924/**
1925 * @param array $pagestostrip
1926 */
39fcb981 1927function wiki_admin_strip($pagestostrip) {
1928 /// Purges old page-versions
1929 foreach($pagestostrip as $id => $versions) {
1930 foreach($versions as $version) {
1931 ewiki_database("DELETE", array("id"=>$id, "version"=>$version));
1932 }
1933 }
8f0cd6ef 1934}
39fcb981 1935
e5cc530b 1936/**
1937 * @return array
1938 */
39fcb981 1939function wiki_admin_checklinks_list() {
1940 $ret=array();
1941 $result = ewiki_database("GETALL",array());
1942 while ($row = $result->get()) {
1943 if(!($row["flags"] & EWIKI_DB_F_BINARY)) {
2bc0b7a0 1944 $index=s($row["id"]);
39fcb981 1945 $ret[$index] = $row["id"];
1946 }
1947 }
1948 return $ret;
1949}
1950
e5cc530b 1951/**
1952 * Checks http:// Links
1953 * @return string
1954 */
39fcb981 1955function wiki_admin_checklinks($pagetocheck) {
39fcb981 1956 $ret="";
1957 if($pagetocheck) {
1958 $get = ewiki_database("GET", array("id" => $pagetocheck));
1959 $content = $get["content"];
8f0cd6ef 1960
a9de0242 1961 preg_match_all('_(http.?://[^\s"\'<>#,;]+[^\s"\'<>#,;.])_', $content, $links);
39fcb981 1962 $badlinks = array();
1963 if(!$links[1]) {
a9ef4a63 1964 $ret = get_string("nolinksfound","wiki")."<br /><br />";
39fcb981 1965 } else {
1966 foreach ($links[1] as $href) {
1967 #print "[ $href ]";
8f0cd6ef 1968 #$d = @implode("", @file($href));
39fcb981 1969 $d="";
1970 if($checkfd = @fopen($href, 'r')) {
1971 fclose($checkfd);
1972 $d="OK";
1973 }
1974 if (empty($d) || !strlen(trim($d)) || stristr("not found", $d) || stristr("error 404", $d)) {
a9ef4a63 1975 $ret.="[".get_string("linkdead","wiki")."] $href <br />\n";
39fcb981 1976 $badlinks[] = $href;
1977 } else {
a9ef4a63 1978 $ret.="[".get_string("linkok","wiki")."] $href <br />\n";
39fcb981 1979 }
8f0cd6ef 1980 }
39fcb981 1981 }
8f0cd6ef 1982
39fcb981 1983 /// Remove old Notices
6dbcacee 1984 $content = preg_replace('/ µµ__~\['.get_string("offline","wiki").'\]__µµ /i','', $content);
39fcb981 1985
1986 #-- replace dead links
1987 foreach ($badlinks as $href) {
ee3cc995 1988 $content = preg_replace("\377^(.*)($href)\377m", '$1 µµ__~['.get_string("offline","wiki").']__µµ $2', $content);
39fcb981 1989 }
1990
1991 #-- compare against db content
1992 if ($content != $get["content"]) {
1993 $get["content"] = $content;
1994 $get["version"]++;
1995 $get["author"] = ewiki_author("ewiki_checklinks");
1996 $get["lastmodified"] = time();
1997
1998 ewiki_database("WRITE", $get);
1999 }
2000 }
2001 return $ret;
2002}
2003
e5cc530b 2004/**
2005 * @param bool $proceed
2006 * @param string $authorfieldpattern
2007 * @param int $changesfield
2008 * @param string $howtooperate
2009 * @param int $deleteversions
2010 * @return string
2011 */
39fcb981 2012function wiki_admin_revert($proceed, $authorfieldpattern, $changesfield, $howtooperate, $deleteversions) {
2013 $ret="";
2014 #-- params
2015 $m_time = $changesfield * 3600;
2016 $depth = $deleteversions - 1;
2017 $depth = ($depth>0?$depth:0);
8f0cd6ef 2018
39fcb981 2019 #-- walk through
2020 $result = ewiki_database("GETALL", array("id", "author", "lastmodified"));
2021 while ($row = $result->get()) {
2022 $id = $row["id"];
2023 #-- which versions to check
2024 $verZ = $row["version"];
2025 if ($howtooperate=="lastonly") {
2026 $verA = $verZ;
2027 }
2028 else {
2029 $verA = $verZ-$depth;
8f0cd6ef 2030 if ($verA <= 0) {
39fcb981 2031 $verA = 1;
2032 }
2033 }
2034
2035 for ($ver=$verA; $ver<=$verZ; $ver++) {
2036 #-- load current $ver database entry
2037 if ($verA != $verZ) {
2038 $row = ewiki_database("GET", array("id"=>$id, "version"=>$ver));
2039 }
2040
2041 #-- match
2042 if (stristr($row["author"], $authorfieldpattern) && ($row["lastmodified"] + $m_time > time())) {
2043 $ret .= "$id (".get_string("versionstodelete","wiki").": ";
2044 #-- delete multiple versions
2045 if ($howtooperate=="allsince") {
2046 while ($ver<=$verZ) {
2047 $ret .= " $ver";
2048 if ($proceed) {
2049 ewiki_database("DELETE", array("id"=>$id, "version"=>$ver));
2050 }
2051 $ver++;
2052 }
2053 }
2054 #-- or just the affected one
2055 else {
2056 $ret .= " $ver";
2057 if ($proceed) {
2058 ewiki_database("DELETE", $row);
2059 }
2060 }
a9ef4a63 2061 $ret .= ")<br />";
39fcb981 2062 break;
2063 }
2064 } #-- for($ver)
2065 } #-- while($row)
2066 return $ret;
2067}
f3221af9 2068
2069
e5cc530b 2070/**
2071 * @return array
2072 */
f3221af9 2073function wiki_get_view_actions() {
2074 return array('view','view all');
2075}
2076
e5cc530b 2077/**
2078 * @return array
2079 */
f3221af9 2080function wiki_get_post_actions() {
2081 return array('hack');
2082}
2083
2084
4db1861a 2085/**
2086 * Obtains an editing lock on a wiki page.
e5cc530b 2087 *
2088 * @global object
2089 * @global object
4db1861a 2090 * @param int $wikiid ID of wiki object.
2091 * @param string $pagename Name of page.
2092 * @return array Two-element array with a boolean true (if lock has been obtained)
2093 * or false (if lock was held by somebody else). If lock was held by someone else,
6022c4f5 2094 * the values of the wiki_locks entry are held in the second element; if lock was
2095 * held by current user then the the second element has a member ->id only.
4db1861a 2096 */
2097function wiki_obtain_lock($wikiid,$pagename) {
655b09ca 2098 global $USER, $DB;
f84d6a8d 2099
2ee60b49 2100 // Check for lock
4db1861a 2101 $alreadyownlock=false;
655b09ca 2102 if($lock=$DB->get_record('wiki_locks', array('pagename'=>$pagename,'wikiid'=>$wikiid))) {
4db1861a 2103 // Consider the page locked if the lock has been confirmed within WIKI_LOCK_PERSISTENCE seconds
2104 if($lock->lockedby==$USER->id) {
2105 // Cool, it's our lock, do nothing except remember it in session
2106 $lockid=$lock->id;
2107 $alreadyownlock=true;
2108 } else if(time()-$lock->lockedseen < WIKI_LOCK_PERSISTENCE) {
2ee60b49 2109 return array(false,$lock);
4db1861a 2110 } else {
2111 // Not locked any more. Get rid of the old lock record.
bb4b6010 2112 $DB->delete_records('wiki_locks', array('pagename'=>$pagename,'wikiid'=>$wikiid));
f84d6a8d 2113 }
4db1861a 2114 }
f84d6a8d 2115
4db1861a 2116 // Add lock
f84d6a8d 2117 if(!$alreadyownlock) {
2ee60b49 2118 // Lock page
2119 $newlock=new stdClass;
2120 $newlock->lockedby=$USER->id;
2121 $newlock->lockedsince=time();
2122 $newlock->lockedseen=$newlock->lockedsince;
2123 $newlock->wikiid=$wikiid;
2124 $newlock->pagename=$pagename;
bb4b6010 2125 $lockid = $DB->insert_record('wiki_locks',$newlock);
4db1861a 2126 }
f84d6a8d 2127
4db1861a 2128 // Store lock information in session so we can clear it later
2129 if(!array_key_exists(SESSION_WIKI_LOCKS,$_SESSION)) {
2ee60b49 2130 $_SESSION[SESSION_WIKI_LOCKS]=array();
4db1861a 2131 }
2ee60b49 2132 $_SESSION[SESSION_WIKI_LOCKS][$wikiid.'_'.$pagename]=$lockid;
6022c4f5 2133 $lockdata=new StdClass;
2134 $lockdata->id=$lockid;
2135 return array(true,$lockdata);
4db1861a 2136}
2137
2138/**
2139 * If the user has an editing lock, releases it. Has no effect otherwise.
2140 * Note that it doesn't matter if this isn't called (as happens if their
2141 * browser crashes or something) since locks time out anyway. This is just
2142 * to avoid confusion of the 'what? it says I'm editing that page but I'm
2143 * not, I just saved it!' variety.
e5cc530b 2144 *
2145 * @global object
4db1861a 2146 * @param int $wikiid ID of wiki object.
2147 * @param string $pagename Name of page.
2148 */
f84d6a8d 2149function wiki_release_lock($wikiid,$pagename) {
655b09ca 2150 global $DB;
2151
4db1861a 2152 if(!array_key_exists(SESSION_WIKI_LOCKS,$_SESSION)) {
2ee60b49 2153 // No locks at all in session
2154 return;
4db1861a 2155 }
f84d6a8d 2156
4db1861a 2157 $key=$wikiid.'_'.$pagename;
f84d6a8d 2158
4db1861a 2159 if(array_key_exists($key,$_SESSION[SESSION_WIKI_LOCKS])) {
2ee60b49 2160 $lockid=$_SESSION[SESSION_WIKI_LOCKS][$key];
2161 unset($_SESSION[SESSION_WIKI_LOCKS][$key]);
bb4b6010 2162 $DB->delete_records('wiki_locks', array('id'=>$lockid));
f84d6a8d 2163 }
4db1861a 2164}
2165
f432bebf 2166/**
2167 * Returns all other caps used in module
e5cc530b 2168 *
2169 * @return array
f432bebf 2170 */
2171function wiki_get_extra_capabilities() {
2172 return array('moodle/site:accessallgroups', 'moodle/site:viewfullnames');
2173}
4db1861a 2174
42f103be 2175/**
e5cc530b 2176 * @uses FEATURE_GROUPS
2177 * @uses FEATURE_GROUPINGS
2178 * @uses FEATURE_GROUPMEMBERSONLY
2179 * @uses FEATURE_MOD_INTRO
2180 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
2181 * @uses FEATURE_GRADE_HAS_GRADE
2182 * @uses FEATURE_GRADE_OUTCOMES
42f103be 2183 * @param string $feature FEATURE_xx constant for requested feature
2184 * @return mixed True if module supports feature, null if doesn't know
2185 */
2186function wiki_supports($feature) {
2187 switch($feature) {
2188 case FEATURE_GROUPS: return true;
2189 case FEATURE_GROUPINGS: return true;
2190 case FEATURE_GROUPMEMBERSONLY: return true;
dc5c2bd9 2191 case FEATURE_MOD_INTRO: return true;
42f103be 2192 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
2193 case FEATURE_GRADE_HAS_GRADE: return false;
2194 case FEATURE_GRADE_OUTCOMES: return true;
2195
2196 default: return null;
2197 }
17da2e6f 2198}