MDL-15777 more fixes for portfolio to use file api
[moodle.git] / filter / activitynames / filter.php
1 <?php // $Id$
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) {
8         global $CFG, $COURSE, $DB;
10         // Trivial-cache - keyed on $cachedcourseid
11         static $activitylist = null;
12         static $cachedcourseid;
14         if (empty($courseid)) {
15             $courseid = SITEID;
16         }
18         // Initialise/invalidate our trivial cache if dealing with a different course
19         if (!isset($cachedcourseid) || $cachedcourseid !== (int)$courseid) {
20             $activitylist = null;
21         } 
22         $cachedcourseid = (int)$courseid;
24         /// It may be cached
26         if (is_null($activitylist)) {
27             $activitylist = array();
29             if ($COURSE->id == $courseid) {
30                 $course = $COURSE;
31             } else {
32                 $course = $DB->get_record("course", array("id"=>$courseid));
33             }
35             if (!isset($course->modinfo)) {
36                 return $text;
37             }
39         /// Casting $course->modinfo to string prevents one notice when the field is null
40             $modinfo = unserialize((string)$course->modinfo);
42             if (!empty($modinfo)) {
44                 $activitylist = array();      /// We will store all the activities here
46                 //Sort modinfo by name length
47                 usort($modinfo, 'comparemodulenamesbylength');
49                 foreach ($modinfo as $activity) {
50                     //Exclude labels, hidden activities and activities for group members only 
51                     if ($activity->mod != "label" and $activity->visible and empty($activity->groupmembersonly)) {
52                         $title = s(trim(strip_tags(urldecode($activity->name))));
53                         /// Avoid empty or unlinkable activity names
54                         if (!empty($title)) {
55                             $href_tag_begin = "<a class=\"autolink\" title=\"$title\" href=\"$CFG->wwwroot/mod/$activity->mod/view.php?id=$activity->cm\" $CFG->frametarget>";
56                             $currentname = urldecode($activity->name);
57                             if ($currentname = trim($currentname)) {
58                                 $activitylist[] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
59                             }
60                         }
61                     }
62                 }
63             }
64         }
66         if ($activitylist) {
67             return $text = filter_phrases ($text, $activitylist);
68         } else {
69             return $text;
70         }
71     }
75     //This function is used to order module names from longer to shorter
76     function comparemodulenamesbylength($a, $b)  {
77         if (strlen($a->name) == strlen($b->name)) {
78             return 0;
79         }
80         return (strlen($a->name) < strlen($b->name)) ? 1 : -1;
81     }
82 ?>