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