MDL-12627 - when visiting the gradebook for the first time, we don't sort
[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
9aa90807 11 // Trivial-cache - keyed on $cachedcourseid
eb6eb58e 12 static $activitylist;
6a42a9b4 13 static $cachedcourseid;
eb6eb58e 14
45a1ad99 15 if (empty($courseid)) {
222ac91b 16 $courseid = SITEID;
45a1ad99 17 }
18
9aa90807 19 // Initialise/invalidate our trivial cache if dealing with a different course
20 if (!isset($cachedcourseid) || $cachedcourseid !== (int)$courseid) {
21 $activitylist = array();
22 }
23 $cachedcourseid = (int)$courseid;
24
34d186eb 25 /// It may be cached
45a1ad99 26
34d186eb 27 if (empty($activitylist)) {
07158c1e 28
34d186eb 29 $course = get_record("course","id",$courseid);
fea43a7f 30 /// Casting $course->modinfo to string prevents one notice when the field is null
31 $modinfo = unserialize((string)$course->modinfo);
facc743a 32
34d186eb 33 if (!empty($modinfo)) {
07158c1e 34
34d186eb 35 $activitylist = array(); /// We will store all the activities here
36
37 //Sort modinfo by name length
38 usort($modinfo,'comparemodulenamesbylength');
39
40 foreach ($modinfo as $activity) {
41 //Exclude labels and hidden items
42 if ($activity->mod != "label" && $activity->visible) {
32a50e28 43 $title = trim(strip_tags(urldecode($activity->name)));
44 /// Avoid empty or unlinkable activity names
45 if (!empty($title)) {
46 $title = str_replace('"', "'", $title);
fa738731 47 $href_tag_begin = "<a class=\"autolink\" title=\"$title\" href=\"$CFG->wwwroot/mod/$activity->mod/view.php?id=$activity->cm\" $CFG->frametarget>";
32a50e28 48 $currentname = urldecode($activity->name);
49 if ($currentname = trim($currentname)) {
50 $activitylist[] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
51 }
34d186eb 52 }
45a1ad99 53 }
54 }
55 }
56 }
34d186eb 57
58 return $text = filter_phrases ($text, $activitylist);
45a1ad99 59 }
5096a8ea 60
45a1ad99 61
3a558dd9 62
63 //This function is used to order module names from longer to shorter
00a3a66a 64 function comparemodulenamesbylength($a, $b) {
3a558dd9 65 if (strlen($a->name) == strlen($b->name)) {
66 return 0;
67 }
68 return (strlen($a->name) < strlen($b->name)) ? 1 : -1;
69 }
45a1ad99 70?>