2 //This function provides automatic linking to
3 //activities when its name (title) is found inside every Moodle text
4 //It's based in the glosssary filter by Williams Castillo
5 //Modifications by stronk7.
7 function activitynames_filter($courseid, $text) {
11 // Trivial-cache - keyed on $cachedcourseid
13 static $cachedcourseid;
15 if (empty($courseid)) {
19 // Initialise/invalidate our trivial cache if dealing with a different course
20 if (!isset($cachedcourseid) ||
$cachedcourseid !== (int)$courseid) {
21 $activitylist = array();
23 $cachedcourseid = (int)$courseid;
27 if (empty($activitylist)) {
29 $course = get_record("course","id",$courseid);
30 /// Casting $course->modinfo to string prevents one notice when the field is null
31 $modinfo = unserialize((string)$course->modinfo
);
33 if (!empty($modinfo)) {
35 $activitylist = array(); /// We will store all the activities here
37 //Sort modinfo by name length
38 usort($modinfo,'comparemodulenamesbylength');
40 foreach ($modinfo as $activity) {
41 //Exclude labels and hidden items
42 if ($activity->mod
!= "label" && $activity->visible
) {
43 $title = trim(strip_tags(urldecode($activity->name
)));
44 /// Avoid empty or unlinkable activity names
46 $title = str_replace('"', "'", $title);
47 $href_tag_begin = "<a class=\"autolink\" title=\"$title\" href=\"$CFG->wwwroot/mod/$activity->mod/view.php?id=$activity->cm\" $CFG->frametarget>";
48 $currentname = urldecode($activity->name
);
49 if ($currentname = trim($currentname)) {
50 $activitylist[] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
58 return $text = filter_phrases ($text, $activitylist);
63 //This function is used to order module names from longer to shorter
64 function comparemodulenamesbylength($a, $b) {
65 if (strlen($a->name
) == strlen($b->name
)) {
68 return (strlen($a->name
) < strlen($b->name
)) ?
1 : -1;