merged fix for MDL-8934, assign role context statements incorrect
[moodle.git] / filter / activitynames / filter.php
CommitLineData
3a558dd9 1<?php // $Id$
45a1ad99 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
2ed07cc7 5 //Modifications by stronk7.
45a1ad99 6
45a1ad99 7 function activitynames_filter($courseid, $text) {
8
9 global $CFG;
10
eb6eb58e 11 static $activitylist;
12
45a1ad99 13 if (empty($courseid)) {
222ac91b 14 $courseid = SITEID;
45a1ad99 15 }
16
34d186eb 17 /// It may be cached
45a1ad99 18
34d186eb 19 if (empty($activitylist)) {
07158c1e 20
34d186eb 21 $course = get_record("course","id",$courseid);
fea43a7f 22 /// Casting $course->modinfo to string prevents one notice when the field is null
23 $modinfo = unserialize((string)$course->modinfo);
facc743a 24
34d186eb 25 if (!empty($modinfo)) {
07158c1e 26
34d186eb 27 $activitylist = array(); /// We will store all the activities here
28
29 //Sort modinfo by name length
30 usort($modinfo,'comparemodulenamesbylength');
31
32 foreach ($modinfo as $activity) {
33 //Exclude labels and hidden items
34 if ($activity->mod != "label" && $activity->visible) {
32a50e28 35 $title = trim(strip_tags(urldecode($activity->name)));
36 /// Avoid empty or unlinkable activity names
37 if (!empty($title)) {
38 $title = str_replace('"', "'", $title);
fa738731 39 $href_tag_begin = "<a class=\"autolink\" title=\"$title\" href=\"$CFG->wwwroot/mod/$activity->mod/view.php?id=$activity->cm\" $CFG->frametarget>";
32a50e28 40 $currentname = urldecode($activity->name);
41 if ($currentname = trim($currentname)) {
42 $activitylist[] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
43 }
34d186eb 44 }
45a1ad99 45 }
46 }
47 }
48 }
34d186eb 49
50 return $text = filter_phrases ($text, $activitylist);
45a1ad99 51 }
5096a8ea 52
45a1ad99 53
3a558dd9 54
55 //This function is used to order module names from longer to shorter
00a3a66a 56 function comparemodulenamesbylength($a, $b) {
3a558dd9 57 if (strlen($a->name) == strlen($b->name)) {
58 return 0;
59 }
60 return (strlen($a->name) < strlen($b->name)) ? 1 : -1;
61 }
45a1ad99 62?>