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