Cache array of filterobjects per request.
[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
17 $course = get_record("course","id",$courseid);
18 $modinfo = unserialize($course->modinfo);
19
20 if (!empty($modinfo)) {
07158c1e 21
eb6eb58e 22 $activitylist = array(); /// We will store all the activities here
facc743a 23
07158c1e 24 //Sort modinfo by name length
25 usort($modinfo,'comparemodulenamesbylength');
26
45a1ad99 27 foreach ($modinfo as $activity) {
341d554f 28 //Exclude labels and hidden items
29 if ($activity->mod != "label" && $activity->visible) {
30 $title = strip_tags(urldecode($activity->name));
19f671d3 31 $title = str_replace('"', "'", $title);
341d554f 32 $href_tag_begin = "<a class=\"autolink\" title=\"$title\" href=\"$CFG->wwwroot/mod/$activity->mod/view.php?id=$activity->cm\">";
33 $currentname = urldecode($activity->name);
34 if ($currentname = trim($currentname)) {
eb6eb58e 35 $activitylist[] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
45a1ad99 36 }
37 }
38 }
eb6eb58e 39 $text = filter_phrases ($text, $activitylist);
45a1ad99 40 }
41 return $text;
42 }
5096a8ea 43
45a1ad99 44
3a558dd9 45
46 //This function is used to order module names from longer to shorter
00a3a66a 47 function comparemodulenamesbylength($a, $b) {
3a558dd9 48 if (strlen($a->name) == strlen($b->name)) {
49 return 0;
50 }
51 return (strlen($a->name) < strlen($b->name)) ? 1 : -1;
52 }
45a1ad99 53?>