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) {
10 // Trivial-cache - keyed on $cachedcourseid
13 static $cachedcourseid;
15 // if we don't have a courseid, we can't run the query, so
16 if (empty($courseid)) {
20 // Initialise/invalidate our trivial cache if dealing with a different course
21 if (!isset($cachedcourseid) ||
$cachedcourseid !== (int)$courseid) {
22 $resourcelist = array();
25 $cachedcourseid = (int)$courseid;
27 if ($nothingtodo === true) {
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')) {
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
.'>',
57 return filter_phrases($text, $resourcelist); // Look for all these links in the text