3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This contains functions and classes that will be used by scripts in wiki module
22 * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
23 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
25 * @author Jordi Piguillem
27 * @author David Jimenez
29 * @author Daniel Serrano
30 * @author Kenneth Riba
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 defined('MOODLE_INTERNAL') ||
die();
37 require_once($CFG->dirroot
. '/mod/wiki/lib.php');
38 require_once($CFG->dirroot
. '/mod/wiki/parser/parser.php');
39 require_once($CFG->libdir
. '/filelib.php');
41 define('WIKI_REFRESH_CACHE_TIME', 30); // @TODO: To be deleted.
42 define('FORMAT_CREOLE', '37');
43 define('FORMAT_NWIKI', '38');
44 define('NO_VALID_RATE', '-999');
45 define('IMPROVEMENT', '+');
49 define('LOCK_TIMEOUT', 30);
53 * @param int $wikiid the instance id of wiki
55 function wiki_get_wiki($wikiid) {
58 return $DB->get_record('wiki', array('id' => $wikiid));
62 * Get sub wiki instances with same wiki id
65 function wiki_get_subwikis($wikiid) {
67 return $DB->get_records('wiki_subwikis', array('wikiid' => $wikiid));
71 * Get a sub wiki instance by wiki id and group id
76 function wiki_get_subwiki_by_group($wikiid, $groupid, $userid = 0) {
78 return $DB->get_record('wiki_subwikis', array('wikiid' => $wikiid, 'groupid' => $groupid, 'userid' => $userid));
82 * Get a sub wiki instace by instance id
83 * @param int $subwikiid
86 function wiki_get_subwiki($subwikiid) {
88 return $DB->get_record('wiki_subwikis', array('id' => $subwikiid));
93 * Add a new sub wiki instance
96 * @return int $insertid
98 function wiki_add_subwiki($wikiid, $groupid, $userid = 0) {
101 $record = new StdClass();
102 $record->wikiid
= $wikiid;
103 $record->groupid
= $groupid;
104 $record->userid
= $userid;
106 $insertid = $DB->insert_record('wiki_subwikis', $record);
111 * Get a wiki instance by pageid
115 function wiki_get_wiki_from_pageid($pageid) {
119 FROM {wiki} w, {wiki_subwikis} s, {wiki_pages} p
121 p.subwikiid = s.id AND
124 return $DB->get_record_sql($sql, array($pageid));
128 * Get a wiki page by pageid
132 function wiki_get_page($pageid) {
134 return $DB->get_record('wiki_pages', array('id' => $pageid));
138 * Get latest version of wiki page
142 function wiki_get_current_version($pageid) {
145 // @TODO: Fix this query
149 ORDER BY version DESC";
150 $records = $DB->get_records_sql($sql, array($pageid), 0, 1);
151 return array_pop($records);
156 * Alias of wiki_get_current_version
157 * @TODO, does the exactly same thing as wiki_get_current_version, should be removed
161 function wiki_get_last_version($pageid) {
162 return wiki_get_current_version($pageid);
168 * @param string $section
170 function wiki_get_section_page($page, $section) {
172 $version = wiki_get_current_version($page->id
);
173 return wiki_parser_proxy
::get_section($version->content
, $version->contentformat
, $section);
177 * Get a wiki page by page title
178 * @param int $swid, sub wiki id
179 * @param string $title
182 function wiki_get_page_by_title($swid, $title) {
184 return $DB->get_record('wiki_pages', array('subwikiid' => $swid, 'title' => $title));
188 * Get a version record by record id
189 * @param int $versionid, the version id
192 function wiki_get_version($versionid) {
194 return $DB->get_record('wiki_versions', array('id' => $versionid));
198 * Get first page of wiki instace
199 * @param int $subwikiid
200 * @param int $module, wiki instance object
202 function wiki_get_first_page($subwikid, $module = null) {
206 FROM {wiki} w, {wiki_subwikis} s, {wiki_pages} p
209 w.firstpagetitle = p.title AND
211 return $DB->get_record_sql($sql, array($subwikid));
214 function wiki_save_section($wikipage, $sectiontitle, $sectioncontent, $userid) {
216 $wiki = wiki_get_wiki_from_pageid($wikipage->id
);
217 $cm = get_coursemodule_from_instance('wiki', $wiki->id
);
218 $context = context_module
::instance($cm->id
);
220 if (has_capability('mod/wiki:editpage', $context)) {
221 $version = wiki_get_current_version($wikipage->id
);
222 $content = wiki_parser_proxy
::get_section($version->content
, $version->contentformat
, $sectiontitle, true);
224 $newcontent = $content[0] . $sectioncontent . $content[2];
226 return wiki_save_page($wikipage, $newcontent, $userid);
234 * @param object $wikipage
235 * @param string $newcontent
238 function wiki_save_page($wikipage, $newcontent, $userid) {
241 $wiki = wiki_get_wiki_from_pageid($wikipage->id
);
242 $cm = get_coursemodule_from_instance('wiki', $wiki->id
);
243 $context = context_module
::instance($cm->id
);
245 if (has_capability('mod/wiki:editpage', $context)) {
246 $version = wiki_get_current_version($wikipage->id
);
248 $version->content
= $newcontent;
249 $version->userid
= $userid;
251 $version->timecreated
= time();
252 $version->id
= $DB->insert_record('wiki_versions', $version);
254 $wikipage->timemodified
= $version->timecreated
;
255 $wikipage->userid
= $userid;
256 $return = wiki_refresh_cachedcontent($wikipage, $newcontent);
257 $event = \mod_wiki\event\page_updated
::create(
259 'context' => $context,
260 'objectid' => $wikipage->id
,
261 'relateduserid' => $userid,
263 'newcontent' => $newcontent
266 $event->add_record_snapshot('wiki', $wiki);
267 $event->add_record_snapshot('wiki_pages', $wikipage);
268 $event->add_record_snapshot('wiki_versions', $version);
276 function wiki_refresh_cachedcontent($page, $newcontent = null) {
279 $version = wiki_get_current_version($page->id
);
280 if (empty($version)) {
283 if (!isset($newcontent)) {
284 $newcontent = $version->content
;
287 $options = array('swid' => $page->subwikiid
, 'pageid' => $page->id
);
288 $parseroutput = wiki_parse_content($version->contentformat
, $newcontent, $options);
289 $page->cachedcontent
= $parseroutput['toc'] . $parseroutput['parsed_text'];
290 $page->timerendered
= time();
291 $DB->update_record('wiki_pages', $page);
293 wiki_refresh_page_links($page, $parseroutput['link_count']);
295 return array('page' => $page, 'sections' => $parseroutput['repeated_sections'], 'version' => $version->version
);
299 * Restore a page with specified version.
301 * @param stdClass $wikipage wiki page record
302 * @param stdClass $version wiki page version to restore
303 * @param context_module $context context of wiki module
304 * @return stdClass restored page
306 function wiki_restore_page($wikipage, $version, $context) {
307 $return = wiki_save_page($wikipage, $version->content
, $version->userid
);
308 $event = \mod_wiki\event\page_version_restored
::create(
310 'context' => $context,
311 'objectid' => $version->id
,
313 'pageid' => $wikipage->id
316 $event->add_record_snapshot('wiki_versions', $version);
318 return $return['page'];
321 function wiki_refresh_page_links($page, $links) {
324 $DB->delete_records('wiki_links', array('frompageid' => $page->id
));
325 foreach ($links as $linkname => $linkinfo) {
327 $newlink = new stdClass();
328 $newlink->subwikiid
= $page->subwikiid
;
329 $newlink->frompageid
= $page->id
;
331 if ($linkinfo['new']) {
332 $newlink->tomissingpage
= $linkname;
334 $newlink->topageid
= $linkinfo['pageid'];
338 $DB->insert_record('wiki_links', $newlink);
339 } catch (dml_exception
$e) {
340 debugging($e->getMessage());
347 * Create a new wiki page, if the page exists, return existing pageid
349 * @param string $title
350 * @param string $format
353 function wiki_create_page($swid, $title, $format, $userid) {
355 $subwiki = wiki_get_subwiki($swid);
356 $cm = get_coursemodule_from_instance('wiki', $subwiki->wikiid
);
357 $context = context_module
::instance($cm->id
);
358 require_capability('mod/wiki:editpage', $context);
360 if ($page = wiki_get_page_by_title($swid, $title)) {
364 // Creating a new empty version
365 $version = new stdClass();
366 $version->content
= '';
367 $version->contentformat
= $format;
368 $version->version
= 0;
369 $version->timecreated
= time();
370 $version->userid
= $userid;
373 $versionid = $DB->insert_record('wiki_versions', $version);
375 // Createing a new empty page
376 $page = new stdClass();
377 $page->subwikiid
= $swid;
378 $page->title
= $title;
379 $page->cachedcontent
= '';
380 $page->timecreated
= $version->timecreated
;
381 $page->timemodified
= $version->timecreated
;
382 $page->timerendered
= $version->timecreated
;
383 $page->userid
= $userid;
384 $page->pageviews
= 0;
387 $pageid = $DB->insert_record('wiki_pages', $page);
389 // Setting the pageid
390 $version->id
= $versionid;
391 $version->pageid
= $pageid;
392 $DB->update_record('wiki_versions', $version);
394 $event = \mod_wiki\event\page_created
::create(
396 'context' => $context,
397 'objectid' => $pageid
402 wiki_make_cache_expire($page->title
);
406 function wiki_make_cache_expire($pagename) {
409 $sql = "UPDATE {wiki_pages}
411 WHERE id IN ( SELECT l.frompageid
413 WHERE l.tomissingpage = ?
415 $DB->execute ($sql, array($pagename));
419 * Get a specific version of page
421 * @param int $version
423 function wiki_get_wiki_page_version($pageid, $version) {
425 return $DB->get_record('wiki_versions', array('pageid' => $pageid, 'version' => $version));
431 * @param int $limitfrom
432 * @param int $limitnum
434 function wiki_get_wiki_page_versions($pageid, $limitfrom, $limitnum) {
436 return $DB->get_records('wiki_versions', array('pageid' => $pageid), 'version DESC', '*', $limitfrom, $limitnum);
440 * Count the number of page version
443 function wiki_count_wiki_page_versions($pageid) {
445 return $DB->count_records('wiki_versions', array('pageid' => $pageid));
449 * Get linked from page
452 function wiki_get_linked_to_pages($pageid) {
454 return $DB->get_records('wiki_links', array('frompageid' => $pageid));
458 * Get linked from page
461 function wiki_get_linked_from_pages($pageid) {
463 return $DB->get_records('wiki_links', array('topageid' => $pageid));
467 * Get pages which user have been edited
471 function wiki_get_contributions($swid, $userid) {
475 FROM {wiki_versions} v, {wiki_pages} p
476 WHERE p.subwikiid = ? AND
480 return $DB->get_records_sql($sql, array($swid, $userid));
484 * Get missing or empty pages in wiki
485 * @param int $swid sub wiki id
487 function wiki_get_missing_or_empty_pages($swid) {
490 $sql = "SELECT DISTINCT p.title, p.id, p.subwikiid
491 FROM {wiki} w, {wiki_subwikis} s, {wiki_pages} p
492 WHERE s.wikiid = w.id and
494 w.firstpagetitle != p.title and
497 FROM {wiki_versions} v
498 WHERE v.pageid = p.id)
500 SELECT DISTINCT l.tomissingpage as title, 0 as id, l.subwikiid
502 WHERE l.subwikiid = ? and
505 return $DB->get_records_sql($sql, array($swid, $swid, $swid));
509 * Get pages list in wiki
510 * @param int $swid sub wiki id
511 * @param string $sort How to sort the pages. By default, title ASC.
514 function wiki_get_page_list($swid, $sort = 'title ASC') {
516 $records = $DB->get_records('wiki_pages', array('subwikiid' => $swid), $sort);
521 * Return a list of orphaned wikis for one specific subwiki
523 * @param int $swid sub wiki id
525 function wiki_get_orphaned_pages($swid) {
528 $sql = "SELECT p.id, p.title
529 FROM {wiki_pages} p, {wiki} w , {wiki_subwikis} s
530 WHERE p.subwikiid = ?
533 AND p.title != w.firstpagetitle
534 AND p.id NOT IN (SELECT topageid FROM {wiki_links} WHERE subwikiid = ?)";
536 return $DB->get_records_sql($sql, array($swid, $swid, $swid));
541 * @param int $swid sub wiki id
542 * @param string $search
544 function wiki_search_title($swid, $search) {
547 return $DB->get_records_select('wiki_pages', "subwikiid = ? AND title LIKE ?", array($swid, '%'.$search.'%'));
551 * Search wiki content
552 * @param int $swid sub wiki id
553 * @param string $search
555 function wiki_search_content($swid, $search) {
558 return $DB->get_records_select('wiki_pages', "subwikiid = ? AND cachedcontent LIKE ?", array($swid, '%'.$search.'%'));
562 * Search wiki title and content
563 * @param int $swid sub wiki id
564 * @param string $search
566 function wiki_search_all($swid, $search) {
569 return $DB->get_records_select('wiki_pages', "subwikiid = ? AND (cachedcontent LIKE ? OR title LIKE ?)", array($swid, '%'.$search.'%', '%'.$search.'%'));
575 function wiki_get_user_info($userid) {
577 return $DB->get_record('user', array('id' => $userid));
581 * Increase page view nubmer
582 * @param int $page, database record
584 function wiki_increment_pageviews($page) {
588 $DB->update_record('wiki_pages', $page);
591 //----------------------------------------------------------
592 //----------------------------------------------------------
595 * Text format supported by wiki module
597 function wiki_get_formats() {
598 return array('html', 'creole', 'nwiki');
602 * Parses a string with the wiki markup language in $markup.
604 * @return Array or false when something wrong has happened.
606 * Returned array contains the following fields:
607 * 'parsed_text' => String. Contains the parsed wiki content.
608 * 'unparsed_text' => String. Constains the original wiki content.
609 * 'link_count' => Array of array('destination' => ..., 'new' => "is new?"). Contains the internal wiki links found in the wiki content.
610 * 'deleted_sections' => the list of deleted sections.
613 * @author Josep Arús Pous
615 function wiki_parse_content($markup, $pagecontent, $options = array()) {
618 $subwiki = wiki_get_subwiki($options['swid']);
619 $cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid
);
620 $context = context_module
::instance($cm->id
);
622 $parser_options = array(
623 'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link',
624 'link_callback_args' => array('swid' => $options['swid']),
625 'table_callback' => '/mod/wiki/locallib.php:wiki_parser_table',
626 'real_path_callback' => '/mod/wiki/locallib.php:wiki_parser_real_path',
627 'real_path_callback_args' => array(
628 'context' => $context,
629 'component' => 'mod_wiki',
630 'filearea' => 'attachments',
631 'subwikiid'=> $subwiki->id
,
632 'pageid' => $options['pageid']
634 'pageid' => $options['pageid'],
635 'pretty_print' => (isset($options['pretty_print']) && $options['pretty_print']),
636 'printable' => (isset($options['printable']) && $options['printable'])
639 return wiki_parser_proxy
::parse($pagecontent, $markup, $parser_options);
643 * This function is the parser callback to parse wiki links.
645 * It returns the necesary information to print a link.
647 * NOTE: Empty pages and non-existent pages must be print in red color.
649 * !!!!!! IMPORTANT !!!!!!
650 * It is critical that you call format_string on the content before it is used.
652 * @param string|stdClass $link name of a page or page object.
653 * @param array $options
654 * @return array Array('content' => string, 'url' => string, 'new' => bool, 'link_info' => array)
656 * @TODO Doc return and options
658 function wiki_parser_link($link, $options = null) {
661 if (is_object($link)) {
662 $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));
664 $version = wiki_get_current_version($link->id
);
665 if ($version->version
== 0) {
666 $parsedlink['new'] = true;
670 $swid = $options['swid'];
672 if ($page = wiki_get_page_by_title($swid, $link)) {
673 $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));
675 $version = wiki_get_current_version($page->id
);
676 if ($version->version
== 0) {
677 $parsedlink['new'] = true;
683 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));
689 * Returns the table fully parsed (HTML)
691 * @return HTML for the table $table
692 * @author Josep Arús Pous
695 function wiki_parser_table($table) {
698 $htmltable = new html_table();
700 $headers = $table[0];
701 $htmltable->head
= array();
702 foreach ($headers as $h) {
703 $htmltable->head
[] = $h[1];
707 $htmltable->data
= array();
708 foreach ($table as $row) {
710 foreach ($row as $r) {
713 $htmltable->data
[] = $row_data;
716 return html_writer
::table($htmltable);
720 * Returns an absolute path link, unless there is no such link.
722 * @param string $url Link's URL or filename
723 * @param stdClass $context filearea params
724 * @param string $component The component the file is associated with
725 * @param string $filearea The filearea the file is stored in
726 * @param int $swid Sub wiki id
728 * @return string URL for files full path
731 function wiki_parser_real_path($url, $context, $component, $filearea, $swid) {
734 if (preg_match("/^(?:http|ftp)s?\:\/\//", $url)) {
738 $file = 'pluginfile.php';
739 if (!$CFG->slasharguments
) {
740 $file = $file . '?file=';
742 $baseurl = "$CFG->wwwroot/$file/{$context->id}/$component/$filearea/$swid/";
743 // it is a file in current file area
744 return $baseurl . $url;
749 * Returns the token used by a wiki language to represent a given tag or "object" (bold -> **)
751 * @return A string when it has only one token at the beginning (f. ex. lists). An array composed by 2 strings when it has 2 tokens, one at the beginning and one at the end (f. ex. italics). Returns false otherwise.
752 * @author Josep Arús Pous
754 function wiki_parser_get_token($markup, $name) {
756 return wiki_parser_proxy
::get_token($name, $markup);
760 * Checks if current user can view a subwiki
762 * @param stdClass $subwiki usually record from {wiki_subwikis}. Must contain fields 'wikiid', 'groupid', 'userid'.
763 * If it also contains fields 'course' and 'groupmode' from table {wiki} it will save extra DB query.
764 * @param stdClass $wiki optional wiki object if known
767 function wiki_user_can_view($subwiki, $wiki = null) {
770 if (empty($wiki) ||
$wiki->id
!= $subwiki->wikiid
) {
771 $wiki = wiki_get_wiki($subwiki->wikiid
);
773 $modinfo = get_fast_modinfo($wiki->course
);
774 if (!isset($modinfo->instances
['wiki'][$subwiki->wikiid
])) {
775 // Module does not exist.
778 $cm = $modinfo->instances
['wiki'][$subwiki->wikiid
];
779 if (!$cm->uservisible
) {
780 // The whole module is not visible to the current user.
783 $context = context_module
::instance($cm->id
);
785 // Working depending on activity groupmode
786 switch (groups_get_activity_groupmode($cm)) {
789 if ($wiki->wikimode
== 'collaborative') {
790 // Collaborative Mode:
791 // There is one wiki for all the class.
793 // Only view capbility needed
794 return has_capability('mod/wiki:viewpage', $context);
795 } else if ($wiki->wikimode
== 'individual') {
797 // Each person owns a wiki.
798 if ($subwiki->userid
== $USER->id
) {
799 // Only the owner of the wiki can view it
800 return has_capability('mod/wiki:viewpage', $context);
801 } else { // User has special capabilities
803 // mod/wiki:viewpage capability
805 // mod/wiki:managewiki capability
806 $view = has_capability('mod/wiki:viewpage', $context);
807 $manage = has_capability('mod/wiki:managewiki', $context);
809 return $view && $manage;
816 // Collaborative and Individual Mode
818 // Collaborative Mode:
819 // There is one wiki per group.
821 // Each person owns a wiki.
822 if ($wiki->wikimode
== 'collaborative' ||
$wiki->wikimode
== 'individual') {
823 // Only members of subwiki group could view that wiki
824 if (in_array($subwiki->groupid
, $modinfo->get_groups($cm->groupingid
))) {
825 // Only view capability needed
826 return has_capability('mod/wiki:viewpage', $context);
828 } else { // User is not part of that group
830 // mod/wiki:managewiki capability
832 // moodle/site:accessallgroups capability
834 // mod/wiki:viewpage capability
835 $view = has_capability('mod/wiki:viewpage', $context);
836 $manage = has_capability('mod/wiki:managewiki', $context);
837 $access = has_capability('moodle/site:accessallgroups', $context);
838 return ($manage ||
$access) && $view;
845 // Collaborative and Individual Mode
847 // Collaborative Mode:
848 // There is one wiki per group.
850 // Each person owns a wiki.
851 if ($wiki->wikimode
== 'collaborative' ||
$wiki->wikimode
== 'individual') {
852 // Everybody can read all wikis
854 // Only view capability needed
855 return has_capability('mod/wiki:viewpage', $context);
866 * Checks if current user can edit a subwiki
870 function wiki_user_can_edit($subwiki) {
873 $wiki = wiki_get_wiki($subwiki->wikiid
);
874 $cm = get_coursemodule_from_instance('wiki', $wiki->id
);
875 $context = context_module
::instance($cm->id
);
877 // Working depending on activity groupmode
878 switch (groups_get_activity_groupmode($cm)) {
881 if ($wiki->wikimode
== 'collaborative') {
882 // Collaborative Mode:
883 // There is a wiki for all the class.
885 // Only edit capbility needed
886 return has_capability('mod/wiki:editpage', $context);
887 } else if ($wiki->wikimode
== 'individual') {
889 // There is a wiki per user
891 // Only the owner of that wiki can edit it
892 if ($subwiki->userid
== $USER->id
) {
893 return has_capability('mod/wiki:editpage', $context);
894 } else { // Current user is not the owner of that wiki.
897 // mod/wiki:editpage capability
899 // mod/wiki:managewiki capability
900 $edit = has_capability('mod/wiki:editpage', $context);
901 $manage = has_capability('mod/wiki:managewiki', $context);
903 return $edit && $manage;
910 if ($wiki->wikimode
== 'collaborative') {
911 // Collaborative Mode:
912 // There is one wiki per group.
914 // Only members of subwiki group could edit that wiki
915 if (groups_is_member($subwiki->groupid
)) {
916 // Only edit capability needed
917 return has_capability('mod/wiki:editpage', $context);
918 } else { // User is not part of that group
920 // mod/wiki:managewiki capability
922 // moodle/site:accessallgroups capability
924 // mod/wiki:editpage capability
925 $manage = has_capability('mod/wiki:managewiki', $context);
926 $access = has_capability('moodle/site:accessallgroups', $context);
927 $edit = has_capability('mod/wiki:editpage', $context);
928 return $manage && $access && $edit;
930 } else if ($wiki->wikimode
== 'individual') {
932 // Each person owns a wiki.
934 // Only the owner of that wiki can edit it
935 if ($subwiki->userid
== $USER->id
) {
936 return has_capability('mod/wiki:editpage', $context);
937 } else { // Current user is not the owner of that wiki.
939 // mod/wiki:managewiki capability
941 // moodle/site:accessallgroups capability
943 // mod/wiki:editpage capability
944 $manage = has_capability('mod/wiki:managewiki', $context);
945 $access = has_capability('moodle/site:accessallgroups', $context);
946 $edit = has_capability('mod/wiki:editpage', $context);
947 return $manage && $access && $edit;
954 if ($wiki->wikimode
== 'collaborative') {
955 // Collaborative Mode:
956 // There is one wiki per group.
958 // Only members of subwiki group could edit that wiki
959 if (groups_is_member($subwiki->groupid
)) {
960 // Only edit capability needed
961 return has_capability('mod/wiki:editpage', $context);
962 } else { // User is not part of that group
964 // mod/wiki:managewiki capability
966 // mod/wiki:editpage capability
967 $manage = has_capability('mod/wiki:managewiki', $context);
968 $edit = has_capability('mod/wiki:editpage', $context);
969 return $manage && $edit;
971 } else if ($wiki->wikimode
== 'individual') {
973 // Each person owns a wiki.
975 // Only the owner of that wiki can edit it
976 if ($subwiki->userid
== $USER->id
) {
977 return has_capability('mod/wiki:editpage', $context);
978 } else { // Current user is not the owner of that wiki.
980 // mod/wiki:managewiki capability
982 // mod/wiki:editpage capability
983 $manage = has_capability('mod/wiki:managewiki', $context);
984 $edit = has_capability('mod/wiki:editpage', $context);
985 return $manage && $edit;
1001 * Checks if a page-section is locked.
1003 * @return true if the combination of section and page is locked, FALSE otherwise.
1005 function wiki_is_page_section_locked($pageid, $userid, $section = null) {
1008 $sql = "pageid = ? AND lockedat > ? AND userid != ?";
1009 $params = array($pageid, time(), $userid);
1011 if (!empty($section)) {
1012 $sql .= " AND (sectionname = ? OR sectionname IS null)";
1013 $params[] = $section;
1016 return $DB->record_exists_select('wiki_locks', $sql, $params);
1020 * Inserts or updates a wiki_locks record.
1022 function wiki_set_lock($pageid, $userid, $section = null, $insert = false) {
1025 if (wiki_is_page_section_locked($pageid, $userid, $section)) {
1029 $params = array('pageid' => $pageid, 'userid' => $userid, 'sectionname' => $section);
1031 $lock = $DB->get_record('wiki_locks', $params);
1033 if (!empty($lock)) {
1034 $DB->update_record('wiki_locks', array('id' => $lock->id
, 'lockedat' => time() + LOCK_TIMEOUT
));
1035 } else if ($insert) {
1036 $DB->insert_record('wiki_locks',
1037 array('pageid' => $pageid, 'sectionname' => $section, 'userid' => $userid, 'lockedat' => time() + LOCK_TIMEOUT
));
1044 * Deletes wiki_locks that are not in use. (F.Ex. after submitting the changes). If no userid is present, it deletes ALL the wiki_locks of a specific page.
1046 * @param int $pageid page id.
1047 * @param int $userid id of user for which lock is deleted.
1048 * @param string $section section to be deleted.
1049 * @param bool $delete_from_db deleted from db.
1050 * @param bool $delete_section_and_page delete section and page version.
1052 function wiki_delete_locks($pageid, $userid = null, $section = null, $delete_from_db = true, $delete_section_and_page = false) {
1055 $wiki = wiki_get_wiki_from_pageid($pageid);
1056 $cm = get_coursemodule_from_instance('wiki', $wiki->id
);
1057 $context = context_module
::instance($cm->id
);
1059 $params = array('pageid' => $pageid);
1061 if (!empty($userid)) {
1062 $params['userid'] = $userid;
1065 if (!empty($section)) {
1066 $params['sectionname'] = $section;
1069 if ($delete_from_db) {
1070 $DB->delete_records('wiki_locks', $params);
1071 if ($delete_section_and_page && !empty($section)) {
1072 $params['sectionname'] = null;
1073 $DB->delete_records('wiki_locks', $params);
1075 $event = \mod_wiki\event\page_locks_deleted
::create(
1077 'context' => $context,
1078 'objectid' => $pageid,
1079 'relateduserid' => $userid,
1081 'section' => $section
1084 // No need to add snapshot, as important data is section, userid and pageid, which is part of event.
1087 $DB->set_field('wiki_locks', 'lockedat', time(), $params);
1092 * Deletes wiki_locks that expired 1 hour ago.
1094 function wiki_delete_old_locks() {
1097 $DB->delete_records_select('wiki_locks', "lockedat < ?", array(time() - 3600));
1101 * Deletes wiki_links. It can be sepecific link or links attached in subwiki
1103 * @global mixed $DB database object
1104 * @param int $linkid id of the link to be deleted
1105 * @param int $topageid links to the specific page
1106 * @param int $frompageid links from specific page
1107 * @param int $subwikiid links to subwiki
1109 function wiki_delete_links($linkid = null, $topageid = null, $frompageid = null, $subwikiid = null) {
1113 // if link id is givien then don't check for anything else
1114 if (!empty($linkid)) {
1115 $params['id'] = $linkid;
1117 if (!empty($topageid)) {
1118 $params['topageid'] = $topageid;
1120 if (!empty($frompageid)) {
1121 $params['frompageid'] = $frompageid;
1123 if (!empty($subwikiid)) {
1124 $params['subwikiid'] = $subwikiid;
1128 //Delete links if any params are passed, else nothing to delete.
1129 if (!empty($params)) {
1130 $DB->delete_records('wiki_links', $params);
1135 * Delete wiki synonyms related to subwikiid or page
1137 * @param int $subwikiid id of sunbwiki
1138 * @param int $pageid id of page
1140 function wiki_delete_synonym($subwikiid, $pageid = null) {
1143 $params = array('subwikiid' => $subwikiid);
1144 if (!is_null($pageid)) {
1145 $params['pageid'] = $pageid;
1147 $DB->delete_records('wiki_synonyms', $params, IGNORE_MISSING
);
1151 * Delete pages and all related data
1153 * @param mixed $context context in which page needs to be deleted.
1154 * @param mixed $pageids id's of pages to be deleted
1155 * @param int $subwikiid id of the subwiki for which all pages should be deleted
1157 function wiki_delete_pages($context, $pageids = null, $subwikiid = null) {
1160 if (!empty($pageids) && is_int($pageids)) {
1161 $pageids = array($pageids);
1162 } else if (!empty($subwikiid)) {
1163 $pageids = wiki_get_page_list($subwikiid);
1166 //If there is no pageid then return as we can't delete anything.
1167 if (empty($pageids)) {
1171 /// Delete page and all it's relevent data
1172 foreach ($pageids as $pageid) {
1173 if (is_object($pageid)) {
1174 $pageid = $pageid->id
;
1177 //Delete page comments
1178 $comments = wiki_get_comments($context->id
, $pageid);
1179 foreach ($comments as $commentid => $commentvalue) {
1180 wiki_delete_comment($commentid, $context, $pageid);
1184 core_tag_tag
::remove_all_item_tags('mod_wiki', 'wiki_pages', $pageid);
1187 wiki_delete_synonym($subwikiid, $pageid);
1189 //Delete all page versions
1190 wiki_delete_page_versions(array($pageid=>array(0)), $context);
1192 //Delete all page locks
1193 wiki_delete_locks($pageid);
1195 //Delete all page links
1196 wiki_delete_links(null, $pageid);
1198 $params = array('id' => $pageid);
1200 // Get page before deleting.
1201 $page = $DB->get_record('wiki_pages', $params);
1204 $DB->delete_records('wiki_pages', $params);
1206 // Trigger page_deleted event.
1207 $event = \mod_wiki\event\page_deleted
::create(
1209 'context' => $context,
1210 'objectid' => $pageid,
1211 'other' => array('subwikiid' => $subwikiid)
1213 $event->add_record_snapshot('wiki_pages', $page);
1219 * Delete specificed versions of a page or versions created by users
1220 * if version is 0 then it will remove all versions of the page
1222 * @param array $deleteversions delete versions for a page
1223 * @param context_module $context module context
1225 function wiki_delete_page_versions($deleteversions, $context = null) {
1228 /// delete page-versions
1229 foreach ($deleteversions as $id => $versions) {
1230 $params = array('pageid' => $id);
1231 if (is_null($context)) {
1232 $wiki = wiki_get_wiki_from_pageid($id);
1233 $cm = get_coursemodule_from_instance('wiki', $wiki->id
);
1234 $context = context_module
::instance($cm->id
);
1236 // Delete all versions, if version specified is 0.
1237 if (in_array(0, $versions)) {
1238 $oldversions = $DB->get_records('wiki_versions', $params);
1239 $DB->delete_records('wiki_versions', $params, IGNORE_MISSING
);
1241 list($insql, $param) = $DB->get_in_or_equal($versions);
1242 $insql .= ' AND pageid = ?';
1243 array_push($param, $params['pageid']);
1244 $oldversions = $DB->get_records_select('wiki_versions', 'version ' . $insql, $param);
1245 $DB->delete_records_select('wiki_versions', 'version ' . $insql, $param);
1247 foreach ($oldversions as $version) {
1248 // Trigger page version deleted event.
1249 $event = \mod_wiki\event\page_version_deleted
::create(
1251 'context' => $context,
1252 'objectid' => $version->id
,
1257 $event->add_record_snapshot('wiki_versions', $version);
1263 function wiki_get_comment($commentid){
1265 return $DB->get_record('comments', array('id' => $commentid));
1269 * Returns all comments by context and pageid
1271 * @param int $contextid Current context id
1272 * @param int $pageid Current pageid
1274 function wiki_get_comments($contextid, $pageid) {
1277 return $DB->get_records('comments', array('contextid' => $contextid, 'itemid' => $pageid, 'commentarea' => 'wiki_page'), 'timecreated ASC');
1281 * Add comments ro database
1283 * @param object $context. Current context
1284 * @param int $pageid. Current pageid
1285 * @param string $content. Content of the comment
1286 * @param string editor. Version of editor we are using.
1288 function wiki_add_comment($context, $pageid, $content, $editor) {
1290 require_once($CFG->dirroot
. '/comment/lib.php');
1292 list($context, $course, $cm) = get_context_info_array($context->id
);
1293 $cmt = new stdclass();
1294 $cmt->context
= $context;
1295 $cmt->itemid
= $pageid;
1296 $cmt->area
= 'wiki_page';
1297 $cmt->course
= $course;
1298 $cmt->component
= 'mod_wiki';
1300 $manager = new comment($cmt);
1302 if ($editor == 'creole') {
1303 $manager->add($content, FORMAT_CREOLE
);
1304 } else if ($editor == 'html') {
1305 $manager->add($content, FORMAT_HTML
);
1306 } else if ($editor == 'nwiki') {
1307 $manager->add($content, FORMAT_NWIKI
);
1313 * Delete comments from database
1315 * @param $idcomment. Id of comment which will be deleted
1316 * @param $context. Current context
1317 * @param $pageid. Current pageid
1319 function wiki_delete_comment($idcomment, $context, $pageid) {
1321 require_once($CFG->dirroot
. '/comment/lib.php');
1323 list($context, $course, $cm) = get_context_info_array($context->id
);
1324 $cmt = new stdClass();
1325 $cmt->context
= $context;
1326 $cmt->itemid
= $pageid;
1327 $cmt->area
= 'wiki_page';
1328 $cmt->course
= $course;
1329 $cmt->component
= 'mod_wiki';
1331 $manager = new comment($cmt);
1332 $manager->delete($idcomment);
1337 * Delete al comments from wiki
1340 function wiki_delete_comments_wiki() {
1344 $context = context_module
::instance($cm->id
);
1346 $table = 'comments';
1347 $select = 'contextid = ?';
1349 $DB->delete_records_select($table, $select, array($context->id
));
1353 function wiki_add_progress($pageid, $oldversionid, $versionid, $progress) {
1355 for ($v = $oldversionid +
1; $v <= $versionid; $v++
) {
1356 $user = wiki_get_wiki_page_id($pageid, $v);
1358 $DB->insert_record('wiki_progress', array('userid' => $user->userid
, 'pageid' => $pageid, 'versionid' => $v, 'progress' => $progress));
1362 function wiki_get_wiki_page_id($pageid, $id) {
1364 return $DB->get_record('wiki_versions', array('pageid' => $pageid, 'id' => $id));
1367 function wiki_print_page_content($page, $context, $subwikiid) {
1368 global $OUTPUT, $CFG;
1370 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME
< time()) {
1371 $content = wiki_refresh_cachedcontent($page);
1372 $page = $content['page'];
1375 if (isset($content)) {
1377 foreach ($content['sections'] as $s) {
1378 $box .= '<p>' . get_string('repeatedsection', 'wiki', $s) . '</p>';
1382 echo $OUTPUT->box($box);
1385 $html = file_rewrite_pluginfile_urls($page->cachedcontent
, 'pluginfile.php', $context->id
, 'mod_wiki', 'attachments', $subwikiid);
1386 $html = format_text($html, FORMAT_HTML
, array('overflowdiv' => true, 'allowid' => true));
1387 echo $OUTPUT->box($html);
1389 echo $OUTPUT->tag_list(core_tag_tag
::get_item_tags('mod_wiki', 'wiki_pages', $page->id
),
1392 wiki_increment_pageviews($page);
1396 * This function trims any given text and returns it with some dots at the end
1398 * @param string $text
1399 * @param string $limit
1403 function wiki_trim_string($text, $limit = 25) {
1405 if (core_text
::strlen($text) > $limit) {
1406 $text = core_text
::substr($text, 0, $limit) . '...';
1413 * Prints default edit form fields and buttons
1415 * @param string $format Edit form format (html, creole...)
1416 * @param integer $version Version number. A negative number means no versioning.
1419 function wiki_print_edit_form_default_fields($format, $pageid, $version = -1, $upload = false, $deleteuploads = array()) {
1420 global $CFG, $PAGE, $OUTPUT;
1422 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
1424 if ($version >= 0) {
1425 echo '<input type="hidden" name="version" value="' . $version . '" />';
1428 echo '<input type="hidden" name="format" value="' . $format . '"/>';
1431 require_once($CFG->dirroot
. '/lib/form/filemanager.php');
1433 $filemanager = new MoodleQuickForm_filemanager('attachments', get_string('wikiattachments', 'wiki'), array('id' => 'attachments'), array('subdirs' => false, 'maxfiles' => 99, 'maxbytes' => $CFG->maxbytes
));
1435 $value = file_get_submitted_draft_itemid('attachments');
1436 if (!empty($value) && !$upload) {
1437 $filemanager->setValue($value);
1440 echo "<fieldset class=\"wiki-upload-section clearfix\"><legend class=\"ftoggler\">" . get_string("uploadtitle", 'wiki') . "</legend>";
1442 echo $OUTPUT->container_start('container');
1443 print $filemanager->toHtml();
1444 echo $OUTPUT->container_end();
1447 $context = context_module
::instance($cm->id
);
1449 echo $OUTPUT->container_start('container wiki-upload-table');
1450 wiki_print_upload_table($context, 'wiki_upload', $pageid, $deleteuploads);
1451 echo $OUTPUT->container_end();
1455 echo '<input class="wiki_button btn btn-secondary" type="submit" name="editoption" value="'
1456 . get_string('save', 'wiki') . '" />';
1457 echo '<input class="wiki_button btn btn-secondary" type="submit" name="editoption" value="'
1458 . get_string('upload', 'wiki') . '" />';
1459 echo '<input class="wiki_button btn btn-secondary" type="submit" name="editoption" value="' . get_string('preview') . '" />';
1460 echo '<input class="wiki_button btn btn-secondary" type="submit" name="editoption" value="' . get_string('cancel') . '" />';
1464 * Prints a table with the files attached to a wiki page
1465 * @param object $context
1466 * @param string $filearea
1467 * @param int $fileitemid
1468 * @param array deleteuploads
1470 function wiki_print_upload_table($context, $filearea, $fileitemid, $deleteuploads = array()) {
1471 global $CFG, $OUTPUT;
1473 $htmltable = new html_table();
1475 $htmltable->head
= array(get_string('deleteupload', 'wiki'), get_string('uploadname', 'wiki'), get_string('uploadactions', 'wiki'));
1477 $fs = get_file_storage();
1478 $files = $fs->get_area_files($context->id
, 'mod_wiki', $filearea, $fileitemid); //TODO: this is weird (skodak)
1480 foreach ($files as $file) {
1481 if (!$file->is_directory()) {
1482 $checkbox = '<input type="checkbox" name="deleteupload[]", value="' . $file->get_pathnamehash() . '"';
1484 if (in_array($file->get_pathnamehash(), $deleteuploads)) {
1485 $checkbox .= ' checked="checked"';
1490 $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>', "");
1494 print '<h3 class="upload-table-title">' . get_string('uploadfiletitle', 'wiki') . "</h3>";
1495 print html_writer
::table($htmltable);
1499 * Generate wiki's page tree
1501 * @param page_wiki $page. A wiki page object
1502 * @param navigation_node $node. Starting navigation_node
1503 * @param array $keys. An array to store keys
1504 * @return an array with all tree nodes
1506 function wiki_build_tree($page, $node, &$keys) {
1508 static $icon = null;
1509 if ($icon === null) {
1510 // Substitute the default navigation icon with empty image.
1511 $icon = new pix_icon('spacer', '');
1513 $pages = wiki_get_linked_pages($page->id
);
1514 foreach ($pages as $p) {
1515 $key = $page->id
. ':' . $p->id
;
1516 if (in_array($key, $keys)) {
1519 array_push($keys, $key);
1520 $l = wiki_parser_link($p);
1521 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $p->id
));
1522 // navigation_node::get_content will format the title for us
1523 $nodeaux = $node->add($p->title
, $link, null, null, null, $icon);
1525 $nodeaux->add_class('wiki_newentry');
1527 wiki_build_tree($p, $nodeaux, $keys);
1534 * Get linked pages from page
1535 * @param int $pageid
1537 function wiki_get_linked_pages($pageid) {
1540 $sql = "SELECT p.id, p.title
1542 JOIN {wiki_links} l ON l.topageid = p.id
1543 WHERE l.frompageid = ?
1544 ORDER BY p.title ASC";
1545 return $DB->get_records_sql($sql, array($pageid));
1549 * Get updated pages from wiki
1550 * @param int $pageid
1552 function wiki_get_updated_pages_by_subwiki($swid) {
1557 WHERE subwikiid = ? AND timemodified > ?
1558 ORDER BY timemodified DESC";
1559 return $DB->get_records_sql($sql, array($swid, $USER->lastlogin
));
1563 * Check if the user can create pages in a certain wiki.
1564 * @param context $context Wiki's context.
1565 * @param integer|stdClass $user A user id or object. By default (null) checks the permissions of the current user.
1566 * @return bool True if user can create pages, false otherwise.
1569 function wiki_can_create_pages($context, $user = null) {
1570 return has_capability('mod/wiki:createpage', $context, $user);
1574 * Get a sub wiki instance by wiki id, group id and user id.
1575 * If the wiki doesn't exist in DB it will return an isntance with id -1.
1577 * @param int $wikiid Wiki ID.
1578 * @param int $groupid Group ID.
1579 * @param int $userid User ID.
1580 * @return object Subwiki instance.
1583 function wiki_get_possible_subwiki_by_group($wikiid, $groupid, $userid = 0) {
1584 if (!$subwiki = wiki_get_subwiki_by_group($wikiid, $groupid, $userid)) {
1585 $subwiki = new stdClass();
1587 $subwiki->wikiid
= $wikiid;
1588 $subwiki->groupid
= $groupid;
1589 $subwiki->userid
= $userid;
1595 * Get all the possible subwikis visible to the user in a wiki.
1596 * It will return all the subwikis that can be created in a wiki, even if they don't exist in DB yet.
1598 * @param stdClass $wiki Wiki to get the subwikis from.
1599 * @param cm_info|stdClass $cm Optional. The course module object.
1600 * @param context_module $context Optional. Context of wiki module.
1601 * @return array List of subwikis.
1604 function wiki_get_visible_subwikis($wiki, $cm = null, $context = null) {
1607 $subwikis = array();
1609 if (empty($wiki) or !is_object($wiki)) {
1615 $cm = get_coursemodule_from_instance('wiki', $wiki->id
);
1617 if (empty($context)) {
1618 $context = context_module
::instance($cm->id
);
1621 if (!has_capability('mod/wiki:viewpage', $context)) {
1625 $manage = has_capability('mod/wiki:managewiki', $context);
1627 if (!$groupmode = groups_get_activity_groupmode($cm)) {
1629 if ($wiki->wikimode
== 'collaborative') {
1631 $subwikis[] = wiki_get_possible_subwiki_by_group($wiki->id
, 0, 0);
1632 } else if ($wiki->wikimode
== 'individual') {
1633 // There's 1 subwiki per user.
1635 // User can view all subwikis.
1636 $users = get_enrolled_users($context);
1637 foreach ($users as $user) {
1638 $subwikis[] = wiki_get_possible_subwiki_by_group($wiki->id
, 0, $user->id
);
1641 // User can only see his subwiki.
1642 $subwikis[] = wiki_get_possible_subwiki_by_group($wiki->id
, 0, $USER->id
);
1646 if ($wiki->wikimode
== 'collaborative') {
1647 // 1 subwiki per group.
1648 $aag = has_capability('moodle/site:accessallgroups', $context);
1649 if ($aag ||
$groupmode == VISIBLEGROUPS
) {
1650 // User can see all groups.
1651 $allowedgroups = groups_get_all_groups($cm->course
, 0, $cm->groupingid
);
1652 $allparticipants = new stdClass();
1653 $allparticipants->id
= 0;
1654 array_unshift($allowedgroups, $allparticipants); // Add all participants.
1656 // User can only see the groups he belongs to.
1657 $allowedgroups = groups_get_all_groups($cm->course
, $USER->id
, $cm->groupingid
);
1660 foreach ($allowedgroups as $group) {
1661 $subwikis[] = wiki_get_possible_subwiki_by_group($wiki->id
, $group->id
, 0);
1663 } else if ($wiki->wikimode
== 'individual') {
1664 // 1 subwiki per user and group.
1666 if ($manage ||
$groupmode == VISIBLEGROUPS
) {
1667 // User can view all subwikis.
1668 $users = get_enrolled_users($context);
1669 foreach ($users as $user) {
1670 // Get all the groups this user belongs to.
1671 $groups = groups_get_all_groups($cm->course
, $user->id
);
1672 if (!empty($groups)) {
1673 foreach ($groups as $group) {
1674 $subwikis[] = wiki_get_possible_subwiki_by_group($wiki->id
, $group->id
, $user->id
);
1677 // User doesn't belong to any group, add it to group 0.
1678 $subwikis[] = wiki_get_possible_subwiki_by_group($wiki->id
, 0, $user->id
);
1682 // The user can only see the subwikis of the groups he belongs.
1683 $allowedgroups = groups_get_all_groups($cm->course
, $USER->id
, $cm->groupingid
);
1684 foreach ($allowedgroups as $group) {
1685 $users = groups_get_members($group->id
);
1686 foreach ($users as $user) {
1687 $subwikis[] = wiki_get_possible_subwiki_by_group($wiki->id
, $group->id
, $user->id
);
1698 * Utility function for getting a subwiki by group and user, validating that the user can view it.
1699 * If the subwiki doesn't exists in DB yet it'll have id -1.
1701 * @param stdClass $wiki The wiki.
1702 * @param int $groupid Group ID. 0 means the subwiki doesn't use groups.
1703 * @param int $userid User ID. 0 means the subwiki doesn't use users.
1704 * @return stdClass Subwiki. If it doesn't exists in DB yet it'll have id -1. If the user can't view the
1705 * subwiki this function will return false.
1707 * @throws moodle_exception
1709 function wiki_get_subwiki_by_group_and_user_with_validation($wiki, $groupid, $userid) {
1712 // Get subwiki based on group and user.
1713 if (!$subwiki = wiki_get_subwiki_by_group($wiki->id
, $groupid, $userid)) {
1715 // The subwiki doesn't exist.
1716 // Validate if user is valid.
1718 $user = core_user
::get_user($userid, '*', MUST_EXIST
);
1719 core_user
::require_active_user($user);
1722 // Validate that groupid is valid.
1723 if ($groupid != 0 && !groups_group_exists($groupid)) {
1724 throw new moodle_exception('cannotfindgroup', 'error');
1727 // Valid data but subwiki not found. We'll simulate a subwiki object to check if the user would be able to see it
1728 // if it existed. If he's able to see it then we'll return an empty array because the subwiki has no pages.
1729 $subwiki = new stdClass();
1731 $subwiki->wikiid
= $wiki->id
;
1732 $subwiki->userid
= $userid;
1733 $subwiki->groupid
= $groupid;
1736 // Check that the user can view the subwiki. This function checks capabilities.
1737 if (!wiki_user_can_view($subwiki, $wiki)) {
1745 * Returns wiki pages tagged with a specified tag.
1747 * This is a callback used by the tag area mod_wiki/wiki_pages to search for wiki pages
1748 * tagged with a specific tag.
1750 * @param core_tag_tag $tag
1751 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
1752 * are displayed on the page and the per-page limit may be bigger
1753 * @param int $fromctx context id where the link was displayed, may be used by callbacks
1754 * to display items in the same context first
1755 * @param int $ctx context id where to search for records
1756 * @param bool $rec search in subcontexts as well
1757 * @param int $page 0-based number of page being displayed
1758 * @return \core_tag\output\tagindex
1760 function mod_wiki_get_tagged_pages($tag, $exclusivemode = false, $fromctx = 0, $ctx = 0, $rec = 1, $page = 0) {
1762 $perpage = $exclusivemode ?
20 : 5;
1764 // Build the SQL query.
1765 $ctxselect = context_helper
::get_preload_record_columns_sql('ctx');
1766 $query = "SELECT wp.id, wp.title, ws.userid, ws.wikiid, ws.id AS subwikiid, ws.groupid, w.wikimode,
1767 cm.id AS cmid, c.id AS courseid, c.shortname, c.fullname, $ctxselect
1768 FROM {wiki_pages} wp
1769 JOIN {wiki_subwikis} ws ON wp.subwikiid = ws.id
1770 JOIN {wiki} w ON w.id = ws.wikiid
1771 JOIN {modules} m ON m.name='wiki'
1772 JOIN {course_modules} cm ON cm.module = m.id AND cm.instance = w.id
1773 JOIN {tag_instance} tt ON wp.id = tt.itemid
1774 JOIN {course} c ON cm.course = c.id
1775 JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :coursemodulecontextlevel
1776 WHERE tt.itemtype = :itemtype AND tt.tagid = :tagid AND tt.component = :component
1777 AND cm.deletioninprogress = 0
1778 AND wp.id %ITEMFILTER% AND c.id %COURSEFILTER%";
1780 $params = array('itemtype' => 'wiki_pages', 'tagid' => $tag->id
, 'component' => 'mod_wiki',
1781 'coursemodulecontextlevel' => CONTEXT_MODULE
);
1784 $context = $ctx ? context
::instance_by_id($ctx) : context_system
::instance();
1785 $query .= $rec ?
' AND (ctx.id = :contextid OR ctx.path LIKE :path)' : ' AND ctx.id = :contextid';
1786 $params['contextid'] = $context->id
;
1787 $params['path'] = $context->path
.'/%';
1790 $query .= " ORDER BY ";
1792 // In order-clause specify that modules from inside "fromctx" context should be returned first.
1793 $fromcontext = context
::instance_by_id($fromctx);
1794 $query .= ' (CASE WHEN ctx.id = :fromcontextid OR ctx.path LIKE :frompath THEN 0 ELSE 1 END),';
1795 $params['fromcontextid'] = $fromcontext->id
;
1796 $params['frompath'] = $fromcontext->path
.'/%';
1798 $query .= ' c.sortorder, cm.id, wp.id';
1800 $totalpages = $page +
1;
1802 // Use core_tag_index_builder to build and filter the list of items.
1803 $builder = new core_tag_index_builder('mod_wiki', 'wiki_pages', $query, $params, $page * $perpage, $perpage +
1);
1804 while ($item = $builder->has_item_that_needs_access_check()) {
1805 context_helper
::preload_from_record($item);
1806 $courseid = $item->courseid
;
1807 if (!$builder->can_access_course($courseid)) {
1808 $builder->set_accessible($item, false);
1811 $modinfo = get_fast_modinfo($builder->get_course($courseid));
1812 // Set accessibility of this item and all other items in the same course.
1813 $builder->walk(function ($taggeditem) use ($courseid, $modinfo, $builder) {
1814 if ($taggeditem->courseid
== $courseid) {
1815 $accessible = false;
1816 if (($cm = $modinfo->get_cm($taggeditem->cmid
)) && $cm->uservisible
) {
1817 $subwiki = (object)array('id' => $taggeditem->subwikiid
, 'groupid' => $taggeditem->groupid
,
1818 'userid' => $taggeditem->userid
, 'wikiid' => $taggeditem->wikiid
);
1819 $wiki = (object)array('id' => $taggeditem->wikiid
, 'wikimode' => $taggeditem->wikimode
,
1820 'course' => $cm->course
);
1821 $accessible = wiki_user_can_view($subwiki, $wiki);
1823 $builder->set_accessible($taggeditem, $accessible);
1828 $items = $builder->get_items();
1829 if (count($items) > $perpage) {
1830 $totalpages = $page +
2; // We don't need exact page count, just indicate that the next page exists.
1834 // Build the display contents.
1836 $tagfeed = new core_tag\output\tagfeed
();
1837 foreach ($items as $item) {
1838 context_helper
::preload_from_record($item);
1839 $modinfo = get_fast_modinfo($item->courseid
);
1840 $cm = $modinfo->get_cm($item->cmid
);
1841 $pageurl = new moodle_url('/mod/wiki/view.php', array('pageid' => $item->id
));
1842 $pagename = format_string($item->title
, true, array('context' => context_module
::instance($item->cmid
)));
1843 $pagename = html_writer
::link($pageurl, $pagename);
1844 $courseurl = course_get_url($item->courseid
, $cm->sectionnum
);
1845 $cmname = html_writer
::link($cm->url
, $cm->get_formatted_name());
1846 $coursename = format_string($item->fullname
, true, array('context' => context_course
::instance($item->courseid
)));
1847 $coursename = html_writer
::link($courseurl, $coursename);
1848 $icon = html_writer
::link($pageurl, html_writer
::empty_tag('img', array('src' => $cm->get_icon_url())));
1849 $tagfeed->add($icon, $pagename, $cmname.'<br>'.$coursename);
1852 $content = $OUTPUT->render_from_template('core_tag/tagfeed',
1853 $tagfeed->export_for_template($OUTPUT));
1855 return new core_tag\output\tagindex
($tag, 'mod_wiki', 'wiki_pages', $content,
1856 $exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);