2 //This function provides automatic linking to
3 //wiki pages when its page title is found inside every Moodle text
4 //It's based in the glosssary filter by Williams Castillo
5 //Modifications by mchurch. Enjoy! :-)
7 require_once($CFG->dirroot
.'/mod/wiki/lib.php');
9 function wiki_filter($courseid, $text) {
13 // Trivial-cache - keyed on $cachedcourseid
16 static $cachedcourseid;
18 if (empty($courseid)) {
22 // Initialise/invalidate our trivial cache if dealing with a different course
23 if (!isset($cachedcourseid) ||
$cachedcourseid !== (int)$courseid) {
24 $wikipagelist = array();
27 $cachedcourseid = (int)$courseid;
29 if (!empty($nothingtodo)) { // We've been here in this page already
33 /// Create a list of all the wikis to search for. It may be cached already.
35 if (empty($wikipagelist)) {
37 /// Get all wikis for this course.
38 if (!$wikis = wiki_get_course_wikis($courseid)) {
43 $wikipagelist = array();
45 /// Walk through each wiki, and get entries.
46 foreach ($wikis as $wiki) {
47 if ($wiki_entries = wiki_get_entries($wiki)) {
49 /// Walk through each entry and get the pages.
50 foreach ($wiki_entries as $wiki_entry) {
51 if ($wiki_pages = get_records('wiki_pages', 'wiki', $wiki_entry->id
, 'pagename, version DESC')) {
52 /// Walk through each page and filter.
53 $wikientries = array();
54 foreach ($wiki_pages as $wiki_page) {
55 if (!in_array($wiki_page->pagename
, $wikientries)) {
56 $startlink = '<a class="wiki autolink" title="Wiki" href="'
57 .$CFG->wwwroot
.'/mod/wiki/view.php?wid='.$wiki->id
58 .'&userid='.$wiki_entry->userid
59 .'&groupid='.$wiki_entry->groupid
60 .'&page='.$wiki_page->pagename
.'">';
61 $wikipagelist[] = new filterobject($wiki_page->pagename
, $startlink, '</a>', false, true);
62 $wikientries[] = $wiki_page->pagename
;
71 return filter_phrases($text, $wikipagelist);