|
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 00035 require_once($CFG->dirroot . '/mod/wiki/lib.php'); 00036 require_once($CFG->dirroot . '/mod/wiki/parser/parser.php'); 00037 require_once($CFG->libdir . '/filelib.php'); 00038 00039 define('WIKI_REFRESH_CACHE_TIME', 30); // @TODO: To be deleted. 00040 define('FORMAT_CREOLE', '37'); 00041 define('FORMAT_NWIKI', '38'); 00042 define('NO_VALID_RATE', '-999'); 00043 define('IMPROVEMENT', '+'); 00044 define('EQUAL', '='); 00045 define('WORST', '-'); 00046 00047 define('LOCK_TIMEOUT', 30); 00048 00053 function wiki_get_wiki($wikiid) { 00054 global $DB; 00055 00056 return $DB->get_record('wiki', array('id' => $wikiid)); 00057 } 00058 00063 function wiki_get_subwikis($wikiid) { 00064 global $DB; 00065 return $DB->get_records('wiki_subwikis', array('wikiid' => $wikiid)); 00066 } 00067 00074 function wiki_get_subwiki_by_group($wikiid, $groupid, $userid = 0) { 00075 global $DB; 00076 return $DB->get_record('wiki_subwikis', array('wikiid' => $wikiid, 'groupid' => $groupid, 'userid' => $userid)); 00077 } 00078 00084 function wiki_get_subwiki($subwikiid) { 00085 global $DB; 00086 return $DB->get_record('wiki_subwikis', array('id' => $subwikiid)); 00087 00088 } 00089 00096 function wiki_add_subwiki($wikiid, $groupid, $userid = 0) { 00097 global $DB; 00098 00099 $record = new StdClass(); 00100 $record->wikiid = $wikiid; 00101 $record->groupid = $groupid; 00102 $record->userid = $userid; 00103 00104 $insertid = $DB->insert_record('wiki_subwikis', $record); 00105 return $insertid; 00106 } 00107 00113 function wiki_get_wiki_from_pageid($pageid) { 00114 global $DB; 00115 00116 $sql = "SELECT w.* 00117 FROM {wiki} w, {wiki_subwikis} s, {wiki_pages} p 00118 WHERE p.id = ? AND 00119 p.subwikiid = s.id AND 00120 s.wikiid = w.id"; 00121 00122 return $DB->get_record_sql($sql, array($pageid)); 00123 } 00124 00130 function wiki_get_page($pageid) { 00131 global $DB; 00132 return $DB->get_record('wiki_pages', array('id' => $pageid)); 00133 } 00134 00140 function wiki_get_current_version($pageid) { 00141 global $DB; 00142 00143 // @TODO: Fix this query 00144 $sql = "SELECT * 00145 FROM {wiki_versions} 00146 WHERE pageid = ? 00147 ORDER BY version DESC"; 00148 return array_pop($DB->get_records_sql($sql, array($pageid), 0, 1)); 00149 00150 } 00151 00158 function wiki_get_last_version($pageid) { 00159 return wiki_get_current_version($pageid); 00160 } 00161 00167 function wiki_get_section_page($page, $section) { 00168 00169 $version = wiki_get_current_version($page->id); 00170 return wiki_parser_proxy::get_section($version->content, $version->contentformat, $section); 00171 } 00172 00179 function wiki_get_page_by_title($swid, $title) { 00180 global $DB; 00181 return $DB->get_record('wiki_pages', array('subwikiid' => $swid, 'title' => $title)); 00182 } 00183 00189 function wiki_get_version($versionid) { 00190 global $DB; 00191 return $DB->get_record('wiki_versions', array('id' => $versionid)); 00192 } 00193 00199 function wiki_get_first_page($subwikid, $module = null) { 00200 global $DB, $USER; 00201 00202 $sql = "SELECT p.* 00203 FROM {wiki} w, {wiki_subwikis} s, {wiki_pages} p 00204 WHERE s.id = ? AND 00205 s.wikiid = w.id AND 00206 w.firstpagetitle = p.title AND 00207 p.subwikiid = s.id"; 00208 return $DB->get_record_sql($sql, array($subwikid)); 00209 } 00210 00211 function wiki_save_section($wikipage, $sectiontitle, $sectioncontent, $userid) { 00212 00213 $wiki = wiki_get_wiki_from_pageid($wikipage->id); 00214 $cm = get_coursemodule_from_instance('wiki', $wiki->id); 00215 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00216 00217 if (has_capability('mod/wiki:editpage', $context)) { 00218 $version = wiki_get_current_version($wikipage->id); 00219 $content = wiki_parser_proxy::get_section($version->content, $version->contentformat, $sectiontitle, true); 00220 00221 $newcontent = $content[0] . $sectioncontent . $content[2]; 00222 00223 return wiki_save_page($wikipage, $newcontent, $userid); 00224 } else { 00225 return false; 00226 } 00227 } 00228 00235 function wiki_save_page($wikipage, $newcontent, $userid) { 00236 global $DB; 00237 00238 $wiki = wiki_get_wiki_from_pageid($wikipage->id); 00239 $cm = get_coursemodule_from_instance('wiki', $wiki->id); 00240 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00241 00242 if (has_capability('mod/wiki:editpage', $context)) { 00243 $version = wiki_get_current_version($wikipage->id); 00244 00245 $version->content = $newcontent; 00246 $version->userid = $userid; 00247 $version->version++; 00248 $version->timecreated = time(); 00249 $versionid = $DB->insert_record('wiki_versions', $version); 00250 00251 $wikipage->timemodified = $version->timecreated; 00252 $wikipage->userid = $userid; 00253 $return = wiki_refresh_cachedcontent($wikipage, $newcontent); 00254 00255 return $return; 00256 } else { 00257 return false; 00258 } 00259 } 00260 00261 function wiki_refresh_cachedcontent($page, $newcontent = null) { 00262 global $DB; 00263 00264 $version = wiki_get_current_version($page->id); 00265 if (empty($version)) { 00266 return null; 00267 } 00268 if (!isset($newcontent)) { 00269 $newcontent = $version->content; 00270 } 00271 00272 $options = array('swid' => $page->subwikiid, 'pageid' => $page->id); 00273 $parseroutput = wiki_parse_content($version->contentformat, $newcontent, $options); 00274 $page->cachedcontent = $parseroutput['toc'] . $parseroutput['parsed_text']; 00275 $page->timerendered = time(); 00276 $DB->update_record('wiki_pages', $page); 00277 00278 wiki_refresh_page_links($page, $parseroutput['link_count']); 00279 00280 return array('page' => $page, 'sections' => $parseroutput['repeated_sections'], 'version' => $version->version); 00281 } 00285 function wiki_restore_page($wikipage, $newcontent, $userid) { 00286 $return = wiki_save_page($wikipage, $newcontent, $userid); 00287 return $return['page']; 00288 } 00289 00290 function wiki_refresh_page_links($page, $links) { 00291 global $DB; 00292 00293 $DB->delete_records('wiki_links', array('frompageid' => $page->id)); 00294 foreach ($links as $linkname => $linkinfo) { 00295 00296 $newlink = new stdClass(); 00297 $newlink->subwikiid = $page->subwikiid; 00298 $newlink->frompageid = $page->id; 00299 00300 if ($linkinfo['new']) { 00301 $newlink->tomissingpage = $linkname; 00302 } else { 00303 $newlink->topageid = $linkinfo['pageid']; 00304 } 00305 00306 try { 00307 $DB->insert_record('wiki_links', $newlink); 00308 } catch (dml_exception $e) { 00309 debugging($e->getMessage()); 00310 } 00311 00312 } 00313 } 00314 00322 function wiki_create_page($swid, $title, $format, $userid) { 00323 global $DB, $PAGE; 00324 $subwiki = wiki_get_subwiki($swid); 00325 $cm = get_coursemodule_from_instance('wiki', $subwiki->wikiid); 00326 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00327 require_capability('mod/wiki:editpage', $context); 00328 // if page exists 00329 if ($page = wiki_get_page_by_title($swid, $title)) { 00330 return $page->id; 00331 } 00332 00333 // Creating a new empty version 00334 $version = new stdClass(); 00335 $version->content = ''; 00336 $version->contentformat = $format; 00337 $version->version = 0; 00338 $version->timecreated = time(); 00339 $version->userid = $userid; 00340 00341 $versionid = null; 00342 $versionid = $DB->insert_record('wiki_versions', $version); 00343 00344 // Createing a new empty page 00345 $page = new stdClass(); 00346 $page->subwikiid = $swid; 00347 $page->title = $title; 00348 $page->cachedcontent = ''; 00349 $page->timecreated = $version->timecreated; 00350 $page->timemodified = $version->timecreated; 00351 $page->timerendered = $version->timecreated; 00352 $page->userid = $userid; 00353 $page->pageviews = 0; 00354 $page->readonly = 0; 00355 00356 $pageid = $DB->insert_record('wiki_pages', $page); 00357 00358 // Setting the pageid 00359 $version->id = $versionid; 00360 $version->pageid = $pageid; 00361 $DB->update_record('wiki_versions', $version); 00362 00363 wiki_make_cache_expire($page->title); 00364 return $pageid; 00365 } 00366 00367 function wiki_make_cache_expire($pagename) { 00368 global $DB; 00369 00370 $sql = "UPDATE {wiki_pages} 00371 SET timerendered = 0 00372 WHERE id IN ( SELECT l.frompageid 00373 FROM {wiki_links} l 00374 WHERE l.tomissingpage = ? 00375 )"; 00376 $DB->execute ($sql, array($pagename)); 00377 } 00378 00384 function wiki_get_wiki_page_version($pageid, $version) { 00385 global $DB; 00386 return $DB->get_record('wiki_versions', array('pageid' => $pageid, 'version' => $version)); 00387 } 00388 00395 function wiki_get_wiki_page_versions($pageid, $limitfrom, $limitnum) { 00396 global $DB; 00397 return $DB->get_records('wiki_versions', array('pageid' => $pageid), 'version DESC', '*', $limitfrom, $limitnum); 00398 } 00399 00404 function wiki_count_wiki_page_versions($pageid) { 00405 global $DB; 00406 return $DB->count_records('wiki_versions', array('pageid' => $pageid)); 00407 } 00408 00413 function wiki_get_linked_to_pages($pageid) { 00414 global $DB; 00415 return $DB->get_records('wiki_links', array('frompageid' => $pageid)); 00416 } 00417 00422 function wiki_get_linked_from_pages($pageid) { 00423 global $DB; 00424 return $DB->get_records('wiki_links', array('topageid' => $pageid)); 00425 } 00426 00432 function wiki_get_contributions($swid, $userid) { 00433 global $DB; 00434 00435 $sql = "SELECT v.* 00436 FROM {wiki_versions} v, {wiki_pages} p 00437 WHERE p.subwikiid = ? AND 00438 v.pageid = p.id AND 00439 v.userid = ?"; 00440 00441 return $DB->get_records_sql($sql, array($swid, $userid)); 00442 } 00443 00448 function wiki_get_missing_or_empty_pages($swid) { 00449 global $DB; 00450 00451 $sql = "SELECT DISTINCT p.title, p.id, p.subwikiid 00452 FROM {wiki} w, {wiki_subwikis} s, {wiki_pages} p 00453 WHERE s.wikiid = w.id and 00454 s.id = ? and 00455 w.firstpagetitle != p.title and 00456 p.subwikiid = ? and 00457 1 = (SELECT count(*) 00458 FROM {wiki_versions} v 00459 WHERE v.pageid = p.id) 00460 UNION 00461 SELECT DISTINCT l.tomissingpage as title, 0 as id, l.subwikiid 00462 FROM {wiki_links} l 00463 WHERE l.subwikiid = ? and 00464 l.topageid = 0"; 00465 00466 return $DB->get_records_sql($sql, array($swid, $swid, $swid)); 00467 } 00468 00473 function wiki_get_page_list($swid) { 00474 global $DB; 00475 $records = $DB->get_records('wiki_pages', array('subwikiid' => $swid), 'title ASC'); 00476 return $records; 00477 } 00478 00484 function wiki_get_orphaned_pages($swid) { 00485 global $DB; 00486 00487 $sql = "SELECT p.id, p.title 00488 FROM {wiki_pages} p, {wiki} w , {wiki_subwikis} s 00489 WHERE p.subwikiid = ? 00490 AND s.id = ? 00491 AND w.id = s.wikiid 00492 AND p.title != w.firstpagetitle 00493 AND p.id NOT IN (SELECT topageid FROM {wiki_links} WHERE subwikiid = ?);"; 00494 00495 return $DB->get_records_sql($sql, array($swid, $swid, $swid)); 00496 } 00497 00503 function wiki_search_title($swid, $search) { 00504 global $DB; 00505 00506 return $DB->get_records_select('wiki_pages', "subwikiid = ? AND title LIKE ?", array($swid, '%'.$search.'%')); 00507 } 00508 00514 function wiki_search_content($swid, $search) { 00515 global $DB; 00516 00517 return $DB->get_records_select('wiki_pages', "subwikiid = ? AND cachedcontent LIKE ?", array($swid, '%'.$search.'%')); 00518 } 00519 00525 function wiki_search_all($swid, $search) { 00526 global $DB; 00527 00528 return $DB->get_records_select('wiki_pages', "subwikiid = ? AND (cachedcontent LIKE ? OR title LIKE ?)", array($swid, '%'.$search.'%', '%'.$search.'%')); 00529 } 00530 00534 function wiki_get_user_info($userid) { 00535 global $DB; 00536 return $DB->get_record('user', array('id' => $userid)); 00537 } 00538 00543 function wiki_increment_pageviews($page) { 00544 global $DB; 00545 00546 $page->pageviews++; 00547 $DB->update_record('wiki_pages', $page); 00548 } 00549 00550 //---------------------------------------------------------- 00551 //---------------------------------------------------------- 00552 00556 function wiki_get_formats() { 00557 return array('html', 'creole', 'nwiki'); 00558 } 00559 00574 function wiki_parse_content($markup, $pagecontent, $options = array()) { 00575 global $PAGE; 00576 00577 $subwiki = wiki_get_subwiki($options['swid']); 00578 $cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid); 00579 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00580 00581 $parser_options = array( 00582 'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link', 00583 'link_callback_args' => array('swid' => $options['swid']), 00584 'table_callback' => '/mod/wiki/locallib.php:wiki_parser_table', 00585 'real_path_callback' => '/mod/wiki/locallib.php:wiki_parser_real_path', 00586 'real_path_callback_args' => array( 00587 'context' => $context, 00588 'component' => 'mod_wiki', 00589 'filearea' => 'attachments', 00590 'subwikiid'=> $subwiki->id, 00591 'pageid' => $options['pageid'] 00592 ), 00593 'pageid' => $options['pageid'], 00594 'pretty_print' => (isset($options['pretty_print']) && $options['pretty_print']), 00595 'printable' => (isset($options['printable']) && $options['printable']) 00596 ); 00597 00598 return wiki_parser_proxy::parse($pagecontent, $markup, $parser_options); 00599 } 00600 00615 function wiki_parser_link($link, $options = null) { 00616 global $CFG; 00617 00618 if (is_object($link)) { 00619 $parsedlink = array('content' => $link->title, 'url' => $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $link->id, 'new' => false, 'link_info' => array('link' => $link->title, 'pageid' => $link->id, 'new' => false)); 00620 00621 $version = wiki_get_current_version($link->id); 00622 if ($version->version == 0) { 00623 $parsedlink['new'] = true; 00624 } 00625 return $parsedlink; 00626 } else { 00627 $swid = $options['swid']; 00628 00629 if ($page = wiki_get_page_by_title($swid, $link)) { 00630 $parsedlink = array('content' => $link, 'url' => $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $page->id, 'new' => false, 'link_info' => array('link' => $link, 'pageid' => $page->id, 'new' => false)); 00631 00632 $version = wiki_get_current_version($page->id); 00633 if ($version->version == 0) { 00634 $parsedlink['new'] = true; 00635 } 00636 00637 return $parsedlink; 00638 00639 } else { 00640 return array('content' => $link, 'url' => $CFG->wwwroot . '/mod/wiki/create.php?swid=' . $swid . '&title=' . urlencode($link) . '&action=new', 'new' => true, 'link_info' => array('link' => $link, 'new' => true, 'pageid' => 0)); 00641 } 00642 } 00643 } 00644 00652 function wiki_parser_table($table) { 00653 global $OUTPUT; 00654 00655 $htmltable = new html_table(); 00656 00657 $headers = $table[0]; 00658 $htmltable->head = array(); 00659 foreach ($headers as $h) { 00660 $htmltable->head[] = $h[1]; 00661 } 00662 00663 array_shift($table); 00664 $htmltable->data = array(); 00665 foreach ($table as $row) { 00666 $row_data = array(); 00667 foreach ($row as $r) { 00668 $row_data[] = $r[1]; 00669 } 00670 $htmltable->data[] = $row_data; 00671 } 00672 00673 return html_writer::table($htmltable); 00674 } 00675 00688 function wiki_parser_real_path($url, $context, $component, $filearea, $swid) { 00689 global $CFG; 00690 00691 if (preg_match("/^(?:http|ftp)s?\:\/\//", $url)) { 00692 return $url; 00693 } else { 00694 00695 $file = 'pluginfile.php'; 00696 if (!$CFG->slasharguments) { 00697 $file = $file . '?file='; 00698 } 00699 $baseurl = "$CFG->wwwroot/$file/{$context->id}/$component/$filearea/$swid/"; 00700 // it is a file in current file area 00701 return $baseurl . $url; 00702 } 00703 } 00704 00711 function wiki_parser_get_token($markup, $name) { 00712 00713 return wiki_parser_proxy::get_token($name, $markup); 00714 } 00715 00721 function wiki_user_can_view($subwiki) { 00722 global $USER; 00723 00724 $wiki = wiki_get_wiki($subwiki->wikiid); 00725 $cm = get_coursemodule_from_instance('wiki', $wiki->id); 00726 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00727 00728 // Working depending on activity groupmode 00729 switch (groups_get_activity_groupmode($cm)) { 00730 case NOGROUPS: 00731 00732 if ($wiki->wikimode == 'collaborative') { 00733 // Collaborative Mode: 00734 // There is one wiki for all the class. 00735 // 00736 // Only view capbility needed 00737 return has_capability('mod/wiki:viewpage', $context); 00738 } else if ($wiki->wikimode == 'individual') { 00739 // Individual Mode: 00740 // Each person owns a wiki. 00741 if ($subwiki->userid == $USER->id) { 00742 // Only the owner of the wiki can view it 00743 return has_capability('mod/wiki:viewpage', $context); 00744 } else { // User has special capabilities 00745 // User must have: 00746 // mod/wiki:viewpage capability 00747 // and 00748 // mod/wiki:managewiki capability 00749 $view = has_capability('mod/wiki:viewpage', $context); 00750 $manage = has_capability('mod/wiki:managewiki', $context); 00751 00752 return $view && $manage; 00753 } 00754 } else { 00755 //Error 00756 return false; 00757 } 00758 case SEPARATEGROUPS: 00759 // Collaborative and Individual Mode 00760 // 00761 // Collaborative Mode: 00762 // There is one wiki per group. 00763 // Individual Mode: 00764 // Each person owns a wiki. 00765 if ($wiki->wikimode == 'collaborative' || $wiki->wikimode == 'individual') { 00766 // Only members of subwiki group could view that wiki 00767 if ($subwiki->groupid == groups_get_activity_group($cm)) { 00768 // Only view capability needed 00769 return has_capability('mod/wiki:viewpage', $context); 00770 00771 } else { // User is not part of that group 00772 // User must have: 00773 // mod/wiki:managewiki capability 00774 // or 00775 // moodle/site:accessallgroups capability 00776 // and 00777 // mod/wiki:viewpage capability 00778 $view = has_capability('mod/wiki:viewpage', $context); 00779 $manage = has_capability('mod/wiki:managewiki', $context); 00780 $access = has_capability('moodle/site:accessallgroups', $context); 00781 return ($manage || $access) && $view; 00782 } 00783 } else { 00784 //Error 00785 return false; 00786 } 00787 case VISIBLEGROUPS: 00788 // Collaborative and Individual Mode 00789 // 00790 // Collaborative Mode: 00791 // There is one wiki per group. 00792 // Individual Mode: 00793 // Each person owns a wiki. 00794 if ($wiki->wikimode == 'collaborative' || $wiki->wikimode == 'individual') { 00795 // Everybody can read all wikis 00796 // 00797 // Only view capability needed 00798 return has_capability('mod/wiki:viewpage', $context); 00799 } else { 00800 //Error 00801 return false; 00802 } 00803 default: // Error 00804 return false; 00805 } 00806 } 00807 00813 function wiki_user_can_edit($subwiki) { 00814 global $USER; 00815 00816 $wiki = wiki_get_wiki($subwiki->wikiid); 00817 $cm = get_coursemodule_from_instance('wiki', $wiki->id); 00818 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 00819 00820 // Working depending on activity groupmode 00821 switch (groups_get_activity_groupmode($cm)) { 00822 case NOGROUPS: 00823 00824 if ($wiki->wikimode == 'collaborative') { 00825 // Collaborative Mode: 00826 // There is a wiki for all the class. 00827 // 00828 // Only edit capbility needed 00829 return has_capability('mod/wiki:editpage', $context); 00830 } else if ($wiki->wikimode == 'individual') { 00831 // Individual Mode 00832 // There is a wiki per user 00833 00834 // Only the owner of that wiki can edit it 00835 if ($subwiki->userid == $USER->id) { 00836 return has_capability('mod/wiki:editpage', $context); 00837 } else { // Current user is not the owner of that wiki. 00838 00839 // User must have: 00840 // mod/wiki:editpage capability 00841 // and 00842 // mod/wiki:managewiki capability 00843 $edit = has_capability('mod/wiki:editpage', $context); 00844 $manage = has_capability('mod/wiki:managewiki', $context); 00845 00846 return $edit && $manage; 00847 } 00848 } else { 00849 //Error 00850 return false; 00851 } 00852 case SEPARATEGROUPS: 00853 if ($wiki->wikimode == 'collaborative') { 00854 // Collaborative Mode: 00855 // There is one wiki per group. 00856 // 00857 // Only members of subwiki group could edit that wiki 00858 if ($subwiki->groupid == groups_get_activity_group($cm)) { 00859 // Only edit capability needed 00860 return has_capability('mod/wiki:editpage', $context); 00861 } else { // User is not part of that group 00862 // User must have: 00863 // mod/wiki:managewiki capability 00864 // and 00865 // moodle/site:accessallgroups capability 00866 // and 00867 // mod/wiki:editpage capability 00868 $manage = has_capability('mod/wiki:managewiki', $context); 00869 $access = has_capability('moodle/site:accessallgroups', $context); 00870 $edit = has_capability('mod/wiki:editpage', $context); 00871 return $manage && $access && $edit; 00872 } 00873 } else if ($wiki->wikimode == 'individual') { 00874 // Individual Mode: 00875 // Each person owns a wiki. 00876 // 00877 // Only the owner of that wiki can edit it 00878 if ($subwiki->userid == $USER->id) { 00879 return has_capability('mod/wiki:editpage', $context); 00880 } else { // Current user is not the owner of that wiki. 00881 // User must have: 00882 // mod/wiki:managewiki capability 00883 // and 00884 // moodle/site:accessallgroups capability 00885 // and 00886 // mod/wiki:editpage capability 00887 $manage = has_capability('mod/wiki:managewiki', $context); 00888 $access = has_capability('moodle/site:accessallgroups', $context); 00889 $edit = has_capability('mod/wiki:editpage', $context); 00890 return $manage && $access && $edit; 00891 } 00892 } else { 00893 //Error 00894 return false; 00895 } 00896 case VISIBLEGROUPS: 00897 if ($wiki->wikimode == 'collaborative') { 00898 // Collaborative Mode: 00899 // There is one wiki per group. 00900 // 00901 // Only members of subwiki group could edit that wiki 00902 if (groups_is_member($subwiki->groupid)) { 00903 // Only edit capability needed 00904 return has_capability('mod/wiki:editpage', $context); 00905 } else { // User is not part of that group 00906 // User must have: 00907 // mod/wiki:managewiki capability 00908 // and 00909 // mod/wiki:editpage capability 00910 $manage = has_capability('mod/wiki:managewiki', $context); 00911 $edit = has_capability('mod/wiki:editpage', $context); 00912 return $manage && $edit; 00913 } 00914 } else if ($wiki->wikimode == 'individual') { 00915 // Individual Mode: 00916 // Each person owns a wiki. 00917 // 00918 // Only the owner of that wiki can edit it 00919 if ($subwiki->userid == $USER->id) { 00920 return has_capability('mod/wiki:editpage', $context); 00921 } else { // Current user is not the owner of that wiki. 00922 // User must have: 00923 // mod/wiki:managewiki capability 00924 // and 00925 // mod/wiki:editpage capability 00926 $manage = has_capability('mod/wiki:managewiki', $context); 00927 $edit = has_capability('mod/wiki:editpage', $context); 00928 return $manage && $edit; 00929 } 00930 } else { 00931 //Error 00932 return false; 00933 } 00934 default: // Error 00935 return false; 00936 } 00937 } 00938 00939 //---------------- 00940 // Locks 00941 //---------------- 00942 00948 function wiki_is_page_section_locked($pageid, $userid, $section = null) { 00949 global $DB; 00950 00951 $sql = "pageid = ? AND lockedat > ? AND userid != ?"; 00952 $params = array($pageid, time(), $userid); 00953 00954 if (!empty($section)) { 00955 $sql .= " AND (sectionname = ? OR sectionname IS null)"; 00956 $params[] = $section; 00957 } 00958 00959 return $DB->record_exists_select('wiki_locks', $sql, $params); 00960 } 00961 00965 function wiki_set_lock($pageid, $userid, $section = null, $insert = false) { 00966 global $DB; 00967 00968 if (wiki_is_page_section_locked($pageid, $userid, $section)) { 00969 return false; 00970 } 00971 00972 $params = array('pageid' => $pageid, 'userid' => $userid, 'sectionname' => $section); 00973 00974 $lock = $DB->get_record('wiki_locks', $params); 00975 00976 if (!empty($lock)) { 00977 $DB->update_record('wiki_locks', array('id' => $lock->id, 'lockedat' => time() + LOCK_TIMEOUT)); 00978 } else if ($insert) { 00979 $DB->insert_record('wiki_locks', array('pageid' => $pageid, 'sectionname' => $section, 'userid' => $userid, 'lockedat' => time() + 30)); 00980 } 00981 00982 return true; 00983 } 00984 00988 function wiki_delete_locks($pageid, $userid = null, $section = null, $delete_from_db = true, $delete_section_and_page = false) { 00989 global $DB; 00990 00991 $params = array('pageid' => $pageid); 00992 00993 if (!empty($userid)) { 00994 $params['userid'] = $userid; 00995 } 00996 00997 if (!empty($section)) { 00998 $params['sectionname'] = $section; 00999 } 01000 01001 if ($delete_from_db) { 01002 $DB->delete_records('wiki_locks', $params); 01003 if ($delete_section_and_page && !empty($section)) { 01004 $params['sectionname'] = null; 01005 $DB->delete_records('wiki_locks', $params); 01006 } 01007 } else { 01008 $DB->set_field('wiki_locks', 'lockedat', time(), $params); 01009 } 01010 } 01011 01015 function wiki_delete_old_locks() { 01016 global $DB; 01017 01018 $DB->delete_records_select('wiki_locks', "lockedat < ?", array(time() - 3600)); 01019 } 01020 01030 function wiki_delete_links($linkid = null, $topageid = null, $frompageid = null, $subwikiid = null) { 01031 global $DB; 01032 $params = array(); 01033 01034 // if link id is givien then don't check for anything else 01035 if (!empty($linkid)) { 01036 $params['id'] = $linkid; 01037 } else { 01038 if (!empty($topageid)) { 01039 $params['topageid'] = $topageid; 01040 } 01041 if (!empty($frompageid)) { 01042 $params['frompageid'] = $frompageid; 01043 } 01044 if (!empty($subwikiid)) { 01045 $params['subwikiid'] = $subwikiid; 01046 } 01047 } 01048 01049 //Delete links if any params are passed, else nothing to delete. 01050 if (!empty($params)) { 01051 $DB->delete_records('wiki_links', $params); 01052 } 01053 } 01054 01061 function wiki_delete_synonym($subwikiid, $pageid = null) { 01062 global $DB; 01063 01064 $params = array('subwikiid' => $subwikiid); 01065 if (!is_null($pageid)) { 01066 $params['pageid'] = $pageid; 01067 } 01068 $DB->delete_records('wiki_synonyms', $params, IGNORE_MISSING); 01069 } 01070 01078 function wiki_delete_pages($context, $pageids = null, $subwikiid = null) { 01079 global $DB; 01080 01081 if (!empty($pageids) && is_int($pageids)) { 01082 $pageids = array($pageids); 01083 } else if (!empty($subwikiid)) { 01084 $pageids = wiki_get_page_list($subwikiid); 01085 } 01086 01087 //If there is no pageid then return as we can't delete anything. 01088 if (empty($pageids)) { 01089 return; 01090 } 01091 01093 foreach ($pageids as $pageid) { 01094 if (is_object($pageid)) { 01095 $pageid = $pageid->id; 01096 } 01097 01098 //Delete page comments 01099 $comments = wiki_get_comments($context->id, $pageid); 01100 foreach ($comments as $commentid => $commentvalue) { 01101 wiki_delete_comment($commentid, $context, $pageid); 01102 } 01103 01104 //Delete page tags 01105 $tags = tag_get_tags_array('wiki_pages', $pageid); 01106 foreach ($tags as $tagid => $tagvalue) { 01107 tag_delete_instance('wiki_pages', $pageid, $tagid); 01108 } 01109 01110 //Delete Synonym 01111 wiki_delete_synonym($subwikiid, $pageid); 01112 01113 //Delete all page versions 01114 wiki_delete_page_versions(array($pageid=>array(0))); 01115 01116 //Delete all page locks 01117 wiki_delete_locks($pageid); 01118 01119 //Delete all page links 01120 wiki_delete_links(null, $pageid); 01121 01122 //Delete page 01123 $params = array('id' => $pageid); 01124 $DB->delete_records('wiki_pages', $params); 01125 } 01126 } 01127 01134 function wiki_delete_page_versions($deleteversions) { 01135 global $DB; 01136 01138 foreach ($deleteversions as $id => $versions) { 01139 foreach ($versions as $version) { 01140 $params = array('pageid' => $id); 01141 //If version = 0, then remove all versions of this page, else remove 01142 //specified version 01143 if ($version != 0) { 01144 $params['version'] = $version; 01145 } 01146 $DB->delete_records('wiki_versions', $params, IGNORE_MISSING); 01147 } 01148 } 01149 } 01150 01151 function wiki_get_comment($commentid){ 01152 global $DB; 01153 return $DB->get_record('comments', array('id' => $commentid)); 01154 } 01155 01162 function wiki_get_comments($contextid, $pageid) { 01163 global $DB; 01164 01165 return $DB->get_records('comments', array('contextid' => $contextid, 'itemid' => $pageid, 'commentarea' => 'wiki_page')); 01166 } 01167 01176 function wiki_add_comment($context, $pageid, $content, $editor) { 01177 global $CFG; 01178 require_once($CFG->dirroot . '/comment/lib.php'); 01179 01180 list($context, $course, $cm) = get_context_info_array($context->id); 01181 $cmt = new stdclass(); 01182 $cmt->context = $context; 01183 $cmt->itemid = $pageid; 01184 $cmt->area = 'wiki_page'; 01185 $cmt->course = $course; 01186 $cmt->component = 'mod_wiki'; 01187 01188 $manager = new comment($cmt); 01189 01190 if ($editor == 'creole') { 01191 $manager->add($content, FORMAT_CREOLE); 01192 } else if ($editor == 'html') { 01193 $manager->add($content, FORMAT_HTML); 01194 } else if ($editor == 'nwiki') { 01195 $manager->add($content, FORMAT_NWIKI); 01196 } 01197 01198 } 01199 01207 function wiki_delete_comment($idcomment, $context, $pageid) { 01208 global $CFG; 01209 require_once($CFG->dirroot . '/comment/lib.php'); 01210 01211 list($context, $course, $cm) = get_context_info_array($context->id); 01212 $cmt = new stdClass(); 01213 $cmt->context = $context; 01214 $cmt->itemid = $pageid; 01215 $cmt->area = 'wiki_page'; 01216 $cmt->course = $course; 01217 $cmt->component = 'mod_wiki'; 01218 01219 $manager = new comment($cmt); 01220 $manager->delete($idcomment); 01221 01222 } 01223 01228 function wiki_delete_comments_wiki() { 01229 global $PAGE, $DB; 01230 01231 $cm = $PAGE->cm; 01232 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 01233 01234 $table = 'comments'; 01235 $select = 'contextid = ?'; 01236 01237 $DB->delete_records_select($table, $select, array($context->id)); 01238 01239 } 01240 01241 function wiki_add_progress($pageid, $oldversionid, $versionid, $progress) { 01242 global $DB; 01243 for ($v = $oldversionid + 1; $v <= $versionid; $v++) { 01244 $user = wiki_get_wiki_page_id($pageid, $v); 01245 01246 $DB->insert_record('wiki_progress', array('userid' => $user->userid, 'pageid' => $pageid, 'versionid' => $v, 'progress' => $progress)); 01247 } 01248 } 01249 01250 function wiki_get_wiki_page_id($pageid, $id) { 01251 global $DB; 01252 return $DB->get_record('wiki_versions', array('pageid' => $pageid, 'id' => $id)); 01253 } 01254 01255 function wiki_print_page_content($page, $context, $subwikiid) { 01256 global $OUTPUT, $CFG; 01257 01258 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) { 01259 $content = wiki_refresh_cachedcontent($page); 01260 $page = $content['page']; 01261 } 01262 01263 if (isset($content)) { 01264 $box = ''; 01265 foreach ($content['sections'] as $s) { 01266 $box .= '<p>' . get_string('repeatedsection', 'wiki', $s) . '</p>'; 01267 } 01268 01269 if (!empty($box)) { 01270 echo $OUTPUT->box($box); 01271 } 01272 } 01273 $html = file_rewrite_pluginfile_urls($page->cachedcontent, 'pluginfile.php', $context->id, 'mod_wiki', 'attachments', $subwikiid); 01274 $html = format_text($html, FORMAT_MOODLE, array('overflowdiv'=>true)); 01275 echo $OUTPUT->box($html); 01276 01277 if (!empty($CFG->usetags)) { 01278 $tags = tag_get_tags_array('wiki_pages', $page->id); 01279 echo $OUTPUT->container_start('wiki-tags'); 01280 echo '<span class="wiki-tags-title">'.get_string('tags').': </span>'; 01281 $links = array(); 01282 foreach ($tags as $tagid=>$tag) { 01283 $url = new moodle_url('/tag/index.php', array('tag'=>$tag)); 01284 $links[] = html_writer::link($url, $tag, array('title'=>get_string('tagtitle', 'wiki', $tag))); 01285 } 01286 echo join($links, ", "); 01287 echo $OUTPUT->container_end(); 01288 } 01289 01290 wiki_increment_pageviews($page); 01291 } 01292 01301 function wiki_trim_string($text, $limit = 25) { 01302 01303 if (textlib::strlen($text) > $limit) { 01304 $text = textlib::substr($text, 0, $limit) . '...'; 01305 } 01306 01307 return $text; 01308 } 01309 01317 function wiki_print_edit_form_default_fields($format, $pageid, $version = -1, $upload = false, $deleteuploads = array()) { 01318 global $CFG, $PAGE, $OUTPUT; 01319 01320 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />'; 01321 01322 if ($version >= 0) { 01323 echo '<input type="hidden" name="version" value="' . $version . '" />'; 01324 } 01325 01326 echo '<input type="hidden" name="format" value="' . $format . '"/>'; 01327 01328 //attachments 01329 require_once($CFG->dirroot . '/lib/form/filemanager.php'); 01330 01331 $filemanager = new MoodleQuickForm_filemanager('attachments', get_string('wikiattachments', 'wiki'), array('id' => 'attachments'), array('subdirs' => false, 'maxfiles' => 99, 'maxbytes' => $CFG->maxbytes)); 01332 01333 $value = file_get_submitted_draft_itemid('attachments'); 01334 if (!empty($value) && !$upload) { 01335 $filemanager->setValue($value); 01336 } 01337 01338 echo "<fieldset class=\"wiki-upload-section clearfix\"><legend class=\"ftoggler\">" . get_string("uploadtitle", 'wiki') . "</legend>"; 01339 01340 echo $OUTPUT->container_start('mdl-align wiki-form-center aaaaa'); 01341 print $filemanager->toHtml(); 01342 echo $OUTPUT->container_end(); 01343 01344 $cm = $PAGE->cm; 01345 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 01346 01347 echo $OUTPUT->container_start('mdl-align wiki-form-center wiki-upload-table'); 01348 wiki_print_upload_table($context, 'wiki_upload', $pageid, $deleteuploads); 01349 echo $OUTPUT->container_end(); 01350 01351 echo "</fieldset>"; 01352 01353 echo '<input class="wiki_button" type="submit" name="editoption" value="' . get_string('save', 'wiki') . '"/>'; 01354 echo '<input class="wiki_button" type="submit" name="editoption" value="' . get_string('upload', 'wiki') . '"/>'; 01355 echo '<input class="wiki_button" type="submit" name="editoption" value="' . get_string('preview') . '"/>'; 01356 echo '<input class="wiki_button" type="submit" name="editoption" value="' . get_string('cancel') . '" />'; 01357 } 01358 01366 function wiki_print_upload_table($context, $filearea, $fileitemid, $deleteuploads = array()) { 01367 global $CFG, $OUTPUT; 01368 01369 $htmltable = new html_table(); 01370 01371 $htmltable->head = array(get_string('deleteupload', 'wiki'), get_string('uploadname', 'wiki'), get_string('uploadactions', 'wiki')); 01372 01373 $fs = get_file_storage(); 01374 $files = $fs->get_area_files($context->id, 'mod_wiki', $filearea, $fileitemid); //TODO: this is weird (skodak) 01375 01376 foreach ($files as $file) { 01377 if (!$file->is_directory()) { 01378 $checkbox = '<input type="checkbox" name="deleteupload[]", value="' . $file->get_pathnamehash() . '"'; 01379 01380 if (in_array($file->get_pathnamehash(), $deleteuploads)) { 01381 $checkbox .= ' checked="checked"'; 01382 } 01383 01384 $checkbox .= " />"; 01385 01386 $htmltable->data[] = array($checkbox, '<a href="' . file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $context->id . '/wiki_upload/' . $fileitemid . '/' . $file->get_filename()) . '">' . $file->get_filename() . '</a>', ""); 01387 } 01388 } 01389 01390 print '<h3 class="upload-table-title">' . get_string('uploadfiletitle', 'wiki') . "</h3>"; 01391 print html_writer::table($htmltable); 01392 } 01393 01402 function wiki_build_tree($page, $node, &$keys) { 01403 $content = array(); 01404 static $icon; 01405 $icon = new pix_icon('f/odt', ''); 01406 $pages = wiki_get_linked_pages($page->id); 01407 foreach ($pages as $p) { 01408 $key = $page->id . ':' . $p->id; 01409 if (in_array($key, $keys)) { 01410 break; 01411 } 01412 array_push($keys, $key); 01413 $l = wiki_parser_link($p); 01414 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $p->id)); 01415 $nodeaux = $node->add($p->title, $link, null, null, null, $icon); 01416 if ($l['new']) { 01417 $nodeaux->add_class('wiki_newentry'); 01418 } 01419 wiki_build_tree($p, $nodeaux, $keys); 01420 } 01421 $content[] = $node; 01422 return $content; 01423 } 01424 01429 function wiki_get_linked_pages($pageid) { 01430 global $DB; 01431 01432 $sql = "SELECT p.id, p.title 01433 FROM {wiki_pages} p 01434 JOIN {wiki_links} l ON l.topageid = p.id 01435 WHERE l.frompageid = ? 01436 ORDER BY p.title ASC"; 01437 return $DB->get_records_sql($sql, array($pageid)); 01438 } 01439 01444 function wiki_get_updated_pages_by_subwiki($swid) { 01445 global $DB, $USER; 01446 01447 $sql = "SELECT * 01448 FROM {wiki_pages} 01449 WHERE subwikiid = ? AND timemodified > ? 01450 ORDER BY timemodified DESC"; 01451 return $DB->get_records_sql($sql, array($swid, $USER->lastlogin)); 01452 }