|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00046 function wiki_add_instance($wiki) { 00047 global $DB; 00048 00049 $wiki->timemodified = time(); 00050 # May have to add extra stuff in here # 00051 if (empty($wiki->forceformat)) { 00052 $wiki->forceformat = 0; 00053 } 00054 return $DB->insert_record('wiki', $wiki); 00055 } 00056 00065 function wiki_update_instance($wiki) { 00066 global $DB; 00067 00068 $wiki->timemodified = time(); 00069 $wiki->id = $wiki->instance; 00070 if (empty($wiki->forceformat)) { 00071 $wiki->forceformat = 0; 00072 } 00073 00074 # May have to add extra stuff in here # 00075 00076 return $DB->update_record('wiki', $wiki); 00077 } 00078 00087 function wiki_delete_instance($id) { 00088 global $DB; 00089 00090 if (!$wiki = $DB->get_record('wiki', array('id' => $id))) { 00091 return false; 00092 } 00093 00094 $result = true; 00095 00096 # Get subwiki information # 00097 $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id)); 00098 00099 foreach ($subwikis as $subwiki) { 00100 # Get existing links, and delete them # 00101 if (!$DB->delete_records('wiki_links', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) { 00102 $result = false; 00103 } 00104 00105 # Get existing pages # 00106 if ($pages = $DB->get_records('wiki_pages', array('subwikiid' => $subwiki->id))) { 00107 foreach ($pages as $page) { 00108 # Get locks, and delete them # 00109 if (!$DB->delete_records('wiki_locks', array('pageid' => $page->id), IGNORE_MISSING)) { 00110 $result = false; 00111 } 00112 00113 # Get versions, and delete them # 00114 if (!$DB->delete_records('wiki_versions', array('pageid' => $page->id), IGNORE_MISSING)) { 00115 $result = false; 00116 } 00117 } 00118 00119 # Delete pages # 00120 if (!$DB->delete_records('wiki_pages', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) { 00121 $result = false; 00122 } 00123 } 00124 00125 # Get existing synonyms, and delete them # 00126 if (!$DB->delete_records('wiki_synonyms', array('subwikiid' => $subwiki->id), IGNORE_MISSING)) { 00127 $result = false; 00128 } 00129 00130 # Delete any subwikis # 00131 if (!$DB->delete_records('wiki_subwikis', array('id' => $subwiki->id), IGNORE_MISSING)) { 00132 $result = false; 00133 } 00134 } 00135 00136 # Delete any dependent records here # 00137 if (!$DB->delete_records('wiki', array('id' => $wiki->id))) { 00138 $result = false; 00139 } 00140 00141 return $result; 00142 } 00143 00144 function wiki_reset_userdata($data) { 00145 global $CFG,$DB; 00146 require_once($CFG->dirroot . '/mod/wiki/pagelib.php'); 00147 require_once($CFG->dirroot . '/tag/lib.php'); 00148 00149 $componentstr = get_string('modulenameplural', 'wiki'); 00150 $status = array(); 00151 00152 //get the wiki(s) in this course. 00153 if (!$wikis = $DB->get_records('wiki', array('course' => $data->courseid))) { 00154 return false; 00155 } 00156 $errors = false; 00157 foreach ($wikis as $wiki) { 00158 00159 // remove all comments 00160 if (!empty($data->reset_wiki_comments)) { 00161 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id)) { 00162 continue; 00163 } 00164 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00165 $DB->delete_records_select('comments', "contextid = ? AND commentarea='wiki_page'", array($context->id)); 00166 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallcomments'), 'error'=>false); 00167 } 00168 00169 if (!empty($data->reset_wiki_tags)) { 00170 # Get subwiki information # 00171 $subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id)); 00172 00173 foreach ($subwikis as $subwiki) { 00174 if ($pages = $DB->get_records('wiki_pages', array('subwikiid' => $subwiki->id))) { 00175 foreach ($pages as $page) { 00176 $tags = tag_get_tags_array('wiki_pages', $page->id); 00177 foreach ($tags as $tagid => $tagname) { 00178 // Delete the related tag_instances related to the wiki page. 00179 $errors = tag_delete_instance('wiki_pages', $page->id, $tagid); 00180 $status[] = array('component' => $componentstr, 'item' => get_string('tagsdeleted', 'wiki'), 'error' => $errors); 00181 } 00182 } 00183 } 00184 } 00185 } 00186 } 00187 return $status; 00188 } 00189 00190 00191 function wiki_reset_course_form_definition(&$mform) { 00192 $mform->addElement('header', 'wikiheader', get_string('modulenameplural', 'wiki')); 00193 $mform->addElement('advcheckbox', 'reset_wiki_tags', get_string('removeallwikitags', 'wiki')); 00194 $mform->addElement('advcheckbox', 'reset_wiki_comments', get_string('deleteallcomments')); 00195 } 00196 00207 function wiki_user_outline($course, $user, $mod, $wiki) { 00208 $return = NULL; 00209 return $return; 00210 } 00211 00219 function wiki_user_complete($course, $user, $mod, $wiki) { 00220 return true; 00221 } 00222 00237 function wiki_supports($feature) { 00238 switch ($feature) { 00239 case FEATURE_GROUPS: 00240 return true; 00241 case FEATURE_GROUPINGS: 00242 return true; 00243 case FEATURE_GROUPMEMBERSONLY: 00244 return true; 00245 case FEATURE_MOD_INTRO: 00246 return true; 00247 case FEATURE_COMPLETION_TRACKS_VIEWS: 00248 return true; 00249 case FEATURE_GRADE_HAS_GRADE: 00250 return false; 00251 case FEATURE_GRADE_OUTCOMES: 00252 return false; 00253 case FEATURE_RATE: 00254 return false; 00255 case FEATURE_BACKUP_MOODLE2: 00256 return true; 00257 case FEATURE_SHOW_DESCRIPTION: 00258 return true; 00259 00260 default: 00261 return null; 00262 } 00263 } 00264 00279 function wiki_print_recent_activity($course, $viewfullnames, $timestart) { 00280 global $CFG, $DB, $OUTPUT; 00281 00282 $sql = "SELECT p.*, w.id as wikiid, sw.groupid 00283 FROM {wiki_pages} p 00284 JOIN {wiki_subwikis} sw ON sw.id = p.subwikiid 00285 JOIN {wiki} w ON w.id = sw.wikiid 00286 WHERE p.timemodified > ? AND w.course = ? 00287 ORDER BY p.timemodified ASC"; 00288 if (!$pages = $DB->get_records_sql($sql, array($timestart, $course->id))) { 00289 return false; 00290 } 00291 $modinfo =& get_fast_modinfo($course); 00292 00293 $wikis = array(); 00294 00295 $modinfo = get_fast_modinfo($course); 00296 00297 foreach ($pages as $page) { 00298 if (!isset($modinfo->instances['wiki'][$page->wikiid])) { 00299 // not visible 00300 continue; 00301 } 00302 $cm = $modinfo->instances['wiki'][$page->wikiid]; 00303 if (!$cm->uservisible) { 00304 continue; 00305 } 00306 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00307 00308 if (!has_capability('mod/wiki:viewpage', $context)) { 00309 continue; 00310 } 00311 00312 $groupmode = groups_get_activity_groupmode($cm, $course); 00313 00314 if ($groupmode) { 00315 if ($groupmode == SEPARATEGROUPS and !has_capability('mod/wiki:managewiki', $context)) { 00316 // separate mode 00317 if (isguestuser()) { 00318 // shortcut 00319 continue; 00320 } 00321 00322 if (is_null($modinfo->groups)) { 00323 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo 00324 } 00325 00326 if (!in_array($page->groupid, $modinfo->groups[0])) { 00327 continue; 00328 } 00329 } 00330 } 00331 $wikis[] = $page; 00332 } 00333 unset($pages); 00334 00335 if (!$wikis) { 00336 return false; 00337 } 00338 echo $OUTPUT->heading(get_string("updatedwikipages", 'wiki') . ':', 3); 00339 foreach ($wikis as $wiki) { 00340 $cm = $modinfo->instances['wiki'][$wiki->wikiid]; 00341 $link = $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $wiki->id; 00342 print_recent_activity_note($wiki->timemodified, $wiki, $cm->name, $link, false, $viewfullnames); 00343 } 00344 00345 return true; // True if anything was printed, otherwise false 00346 } 00356 function wiki_cron() { 00357 global $CFG; 00358 00359 return true; 00360 } 00361 00375 function wiki_grades($wikiid) { 00376 return null; 00377 } 00378 00390 function wiki_get_participants($wikiid) { 00391 return false; 00392 } 00393 00404 function wiki_scale_used($wikiid, $scaleid) { 00405 $return = false; 00406 00407 //$rec = get_record("wiki","id","$wikiid","scale","-$scaleid"); 00408 // 00409 //if (!empty($rec) && !empty($scaleid)) { 00410 // $return = true; 00411 //} 00412 00413 return $return; 00414 } 00415 00424 function wiki_scale_used_anywhere($scaleid) { 00425 global $DB; 00426 00427 //if ($scaleid and $DB->record_exists('wiki', array('grade' => -$scaleid))) { 00428 // return true; 00429 //} else { 00430 // return false; 00431 //} 00432 00433 return false; 00434 } 00435 00441 function wiki_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) { 00442 global $CFG; 00443 00444 if ($context->contextlevel != CONTEXT_MODULE) { 00445 return false; 00446 } 00447 00448 require_login($course, true, $cm); 00449 00450 require_once($CFG->dirroot . "/mod/wiki/locallib.php"); 00451 00452 if ($filearea == 'attachments') { 00453 $swid = (int) array_shift($args); 00454 00455 if (!$subwiki = wiki_get_subwiki($swid)) { 00456 return false; 00457 } 00458 00459 require_capability('mod/wiki:viewpage', $context); 00460 00461 $relativepath = implode('/', $args); 00462 00463 $fullpath = "/$context->id/mod_wiki/attachments/$swid/$relativepath"; 00464 00465 $fs = get_file_storage(); 00466 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) { 00467 return false; 00468 } 00469 00470 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400; 00471 00472 send_stored_file($file, $lifetime, 0); 00473 } 00474 } 00475 00476 function wiki_search_form($cm, $search = '') { 00477 global $CFG, $OUTPUT; 00478 00479 $output = '<div class="wikisearch">'; 00480 $output .= '<form method="post" action="' . $CFG->wwwroot . '/mod/wiki/search.php" style="display:inline">'; 00481 $output .= '<fieldset class="invisiblefieldset">'; 00482 $output .= '<input name="searchstring" type="text" size="18" value="' . s($search, true) . '" alt="search" />'; 00483 $output .= '<input name="courseid" type="hidden" value="' . $cm->course . '" />'; 00484 $output .= '<input name="cmid" type="hidden" value="' . $cm->id . '" />'; 00485 $output .= '<input name="searchwikicontent" type="hidden" value="1" />'; 00486 $output .= ' <input value="' . get_string('searchwikis', 'wiki') . '" type="submit" />'; 00487 $output .= '</fieldset>'; 00488 $output .= '</form>'; 00489 $output .= '</div>'; 00490 00491 return $output; 00492 } 00493 function wiki_extend_navigation(navigation_node $navref, $course, $module, $cm) { 00494 global $CFG, $PAGE, $USER; 00495 00496 require_once($CFG->dirroot . '/mod/wiki/locallib.php'); 00497 00498 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00499 $url = $PAGE->url; 00500 $userid = 0; 00501 if ($module->wikimode == 'individual') { 00502 $userid = $USER->id; 00503 } 00504 00505 if (!$wiki = wiki_get_wiki($cm->instance)) { 00506 return false; 00507 } 00508 00509 if (!$gid = groups_get_activity_group($cm)) { 00510 $gid = 0; 00511 } 00512 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $gid, $userid)) { 00513 return null; 00514 } else { 00515 $swid = $subwiki->id; 00516 } 00517 00518 $pageid = $url->param('pageid'); 00519 $cmid = $url->param('id'); 00520 if (empty($pageid) && !empty($cmid)) { 00521 // wiki main page 00522 $page = wiki_get_page_by_title($swid, $wiki->firstpagetitle); 00523 $pageid = $page->id; 00524 } 00525 00526 if (has_capability('mod/wiki:createpage', $context)) { 00527 $link = new moodle_url('/mod/wiki/create.php', array('action' => 'new', 'swid' => $swid)); 00528 $node = $navref->add(get_string('newpage', 'wiki'), $link, navigation_node::TYPE_SETTING); 00529 } 00530 00531 if (is_numeric($pageid)) { 00532 00533 if (has_capability('mod/wiki:viewpage', $context)) { 00534 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $pageid)); 00535 $node = $navref->add(get_string('view', 'wiki'), $link, navigation_node::TYPE_SETTING); 00536 } 00537 00538 if (has_capability('mod/wiki:editpage', $context)) { 00539 $link = new moodle_url('/mod/wiki/edit.php', array('pageid' => $pageid)); 00540 $node = $navref->add(get_string('edit', 'wiki'), $link, navigation_node::TYPE_SETTING); 00541 } 00542 00543 if (has_capability('mod/wiki:viewcomment', $context)) { 00544 $link = new moodle_url('/mod/wiki/comments.php', array('pageid' => $pageid)); 00545 $node = $navref->add(get_string('comments', 'wiki'), $link, navigation_node::TYPE_SETTING); 00546 } 00547 00548 if (has_capability('mod/wiki:viewpage', $context)) { 00549 $link = new moodle_url('/mod/wiki/history.php', array('pageid' => $pageid)); 00550 $node = $navref->add(get_string('history', 'wiki'), $link, navigation_node::TYPE_SETTING); 00551 } 00552 00553 if (has_capability('mod/wiki:viewpage', $context)) { 00554 $link = new moodle_url('/mod/wiki/map.php', array('pageid' => $pageid)); 00555 $node = $navref->add(get_string('map', 'wiki'), $link, navigation_node::TYPE_SETTING); 00556 } 00557 00558 if (has_capability('mod/wiki:viewpage', $context)) { 00559 $link = new moodle_url('/mod/wiki/files.php', array('pageid' => $pageid)); 00560 $node = $navref->add(get_string('files', 'wiki'), $link, navigation_node::TYPE_SETTING); 00561 } 00562 00563 if (has_capability('mod/wiki:managewiki', $context)) { 00564 $link = new moodle_url('/mod/wiki/admin.php', array('pageid' => $pageid)); 00565 $node = $navref->add(get_string('admin', 'wiki'), $link, navigation_node::TYPE_SETTING); 00566 } 00567 } 00568 } 00574 function wiki_get_extra_capabilities() { 00575 return array('moodle/comment:view', 'moodle/comment:post', 'moodle/comment:delete'); 00576 } 00577 00595 function wiki_comment_permissions($comment_param) { 00596 return array('post'=>true, 'view'=>true); 00597 } 00598 00611 function wiki_comment_validate($comment_param) { 00612 global $DB, $CFG; 00613 require_once($CFG->dirroot . '/mod/wiki/locallib.php'); 00614 // validate comment area 00615 if ($comment_param->commentarea != 'wiki_page') { 00616 throw new comment_exception('invalidcommentarea'); 00617 } 00618 // validate itemid 00619 if (!$record = $DB->get_record('wiki_pages', array('id'=>$comment_param->itemid))) { 00620 throw new comment_exception('invalidcommentitemid'); 00621 } 00622 if (!$subwiki = wiki_get_subwiki($record->subwikiid)) { 00623 throw new comment_exception('invalidsubwikiid'); 00624 } 00625 if (!$wiki = wiki_get_wiki_from_pageid($comment_param->itemid)) { 00626 throw new comment_exception('invalidid', 'data'); 00627 } 00628 if (!$course = $DB->get_record('course', array('id'=>$wiki->course))) { 00629 throw new comment_exception('coursemisconf'); 00630 } 00631 if (!$cm = get_coursemodule_from_instance('wiki', $wiki->id, $course->id)) { 00632 throw new comment_exception('invalidcoursemodule'); 00633 } 00634 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00635 // group access 00636 if ($subwiki->groupid) { 00637 $groupmode = groups_get_activity_groupmode($cm, $course); 00638 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) { 00639 if (!groups_is_member($subwiki->groupid)) { 00640 throw new comment_exception('notmemberofgroup'); 00641 } 00642 } 00643 } 00644 // validate context id 00645 if ($context->id != $comment_param->context->id) { 00646 throw new comment_exception('invalidcontext'); 00647 } 00648 // validation for comment deletion 00649 if (!empty($comment_param->commentid)) { 00650 if ($comment = $DB->get_record('comments', array('id'=>$comment_param->commentid))) { 00651 if ($comment->commentarea != 'wiki_page') { 00652 throw new comment_exception('invalidcommentarea'); 00653 } 00654 if ($comment->contextid != $context->id) { 00655 throw new comment_exception('invalidcontext'); 00656 } 00657 if ($comment->itemid != $comment_param->itemid) { 00658 throw new comment_exception('invalidcommentitemid'); 00659 } 00660 } else { 00661 throw new comment_exception('invalidcommentid'); 00662 } 00663 } 00664 return true; 00665 } 00666 00673 function wiki_page_type_list($pagetype, $parentcontext, $currentcontext) { 00674 $module_pagetype = array( 00675 'mod-wiki-*'=>get_string('page-mod-wiki-x', 'wiki'), 00676 'mod-wiki-view'=>get_string('page-mod-wiki-view', 'wiki'), 00677 'mod-wiki-comments'=>get_string('page-mod-wiki-comments', 'wiki'), 00678 'mod-wiki-history'=>get_string('page-mod-wiki-history', 'wiki'), 00679 'mod-wiki-map'=>get_string('page-mod-wiki-map', 'wiki') 00680 ); 00681 return $module_pagetype; 00682 }