MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / resource / filter.php
blobf6080ef9067cf639cd3804f003ed38f818d6966b
1 <?php // $Id$
2 //This function provides automatic linking to
3 //resources when its name (title) is found inside every Moodle text
4 //Williams, Stronk7, Martin D
6 function resource_filter($courseid, $text) {
8 global $CFG;
10 // Trivial-cache - keyed on $cachedcourseid
11 static $nothingtodo;
12 static $resourcelist;
13 static $cachedcourseid;
15 // if we don't have a courseid, we can't run the query, so
16 if (empty($courseid)) {
17 return $text;
20 // Initialise/invalidate our trivial cache if dealing with a different course
21 if (!isset($cachedcourseid) || $cachedcourseid !== (int)$courseid) {
22 $resourcelist = array();
23 $nothingtodo = false;
25 $cachedcourseid = (int)$courseid;
27 if ($nothingtodo === true) {
28 return $text;
31 /// Create a list of all the resources to search for. It may be cached already.
33 if (empty($resourcelist)) {
35 /// The resources are sorted from long to short so longer ones can be linked first.
37 if (!$resources = get_records('resource', 'course', $courseid, 'CHAR_LENGTH(name) DESC', 'id,name')) {
38 $nothingtodo = true;
39 return $text;
42 $resourcelist = array();
44 foreach ($resources as $resource) {
45 $currentname = trim($resource->name);
46 $strippedname = strip_tags($currentname);
47 /// Avoid empty or unlinkable resource names
48 if (!empty($strippedname)) {
49 $resourcelist[] = new filterobject($currentname,
50 '<a class="resource autolink" title="'.$strippedname.'" href="'.
51 $CFG->wwwroot.'/mod/resource/view.php?r='.$resource->id.'" '.$CFG->frametarget.'>',
52 '</a>', false, true);
57 return filter_phrases($text, $resourcelist); // Look for all these links in the text