MDL-15777 more fixes for portfolio to use file api
[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) {
219f652b 8 global $CFG, $COURSE, $DB;
45a1ad99 9
9aa90807 10 // Trivial-cache - keyed on $cachedcourseid
dd97c328 11 static $activitylist = null;
6a42a9b4 12 static $cachedcourseid;
eb6eb58e 13
45a1ad99 14 if (empty($courseid)) {
222ac91b 15 $courseid = SITEID;
45a1ad99 16 }
17
9aa90807 18 // Initialise/invalidate our trivial cache if dealing with a different course
19 if (!isset($cachedcourseid) || $cachedcourseid !== (int)$courseid) {
dd97c328 20 $activitylist = null;
9aa90807 21 }
22 $cachedcourseid = (int)$courseid;
23
34d186eb 24 /// It may be cached
45a1ad99 25
dd97c328 26 if (is_null($activitylist)) {
27 $activitylist = array();
28
29 if ($COURSE->id == $courseid) {
30 $course = $COURSE;
31 } else {
219f652b 32 $course = $DB->get_record("course", array("id"=>$courseid));
dd97c328 33 }
34
35 if (!isset($course->modinfo)) {
36 return $text;
37 }
07158c1e 38
fea43a7f 39 /// Casting $course->modinfo to string prevents one notice when the field is null
40 $modinfo = unserialize((string)$course->modinfo);
facc743a 41
34d186eb 42 if (!empty($modinfo)) {
07158c1e 43
34d186eb 44 $activitylist = array(); /// We will store all the activities here
45
46 //Sort modinfo by name length
dd97c328 47 usort($modinfo, 'comparemodulenamesbylength');
34d186eb 48
49 foreach ($modinfo as $activity) {
dd97c328 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))));
32a50e28 53 /// Avoid empty or unlinkable activity names
54 if (!empty($title)) {
fa738731 55 $href_tag_begin = "<a class=\"autolink\" title=\"$title\" href=\"$CFG->wwwroot/mod/$activity->mod/view.php?id=$activity->cm\" $CFG->frametarget>";
32a50e28 56 $currentname = urldecode($activity->name);
57 if ($currentname = trim($currentname)) {
58 $activitylist[] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
59 }
34d186eb 60 }
45a1ad99 61 }
62 }
63 }
64 }
34d186eb 65
dd97c328 66 if ($activitylist) {
67 return $text = filter_phrases ($text, $activitylist);
68 } else {
69 return $text;
70 }
45a1ad99 71 }
5096a8ea 72
45a1ad99 73
3a558dd9 74
75 //This function is used to order module names from longer to shorter
00a3a66a 76 function comparemodulenamesbylength($a, $b) {
3a558dd9 77 if (strlen($a->name) == strlen($b->name)) {
78 return 0;
79 }
80 return (strlen($a->name) < strlen($b->name)) ? 1 : -1;
81 }
45a1ad99 82?>