3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This filter provides automatic linking to
20 * activities when its name (title) is found inside every Moodle text
23 * @subpackage activitynames
24 * @copyright 2004 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') ||
die();
31 * Activity name filtering
33 class filter_activitynames
extends moodle_text_filter
{
34 // Trivial-cache - keyed on $cachedcourseid
35 static $activitylist = null;
36 static $cachedcourseid;
38 function filter($text, array $options = array()) {
39 if (!$courseid = get_courseid_from_context($this->context
)) {
43 // Initialise/invalidate our trivial cache if dealing with a different course
44 if (!isset(self
::$cachedcourseid) || self
::$cachedcourseid !== (int)$courseid) {
45 self
::$activitylist = null;
47 self
::$cachedcourseid = (int)$courseid;
51 if (is_null(self
::$activitylist)) {
52 self
::$activitylist = array();
54 $modinfo = get_fast_modinfo($courseid);
55 if (!empty($modinfo->cms
)) {
56 self
::$activitylist = array(); /// We will store all the activities here
58 //Sort modinfo by name length
59 $sortedactivities = fullclone($modinfo->cms
);
60 usort($sortedactivities, 'filter_activitynames_comparemodulenamesbylength');
62 foreach ($sortedactivities as $cm) {
63 //Exclude labels, hidden activities and activities for group members only
64 if ($cm->visible
and empty($cm->groupmembersonly
) and $cm->has_view()) {
65 $title = s(trim(strip_tags($cm->name
)));
66 $currentname = trim($cm->name
);
67 $entitisedname = s($currentname);
68 /// Avoid empty or unlinkable activity names
70 $href_tag_begin = html_writer
::start_tag('a',
71 array('class' => 'autolink', 'title' => $title,
72 'href' => $cm->get_url()));
73 self
::$activitylist[$cm->id
] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
74 if ($currentname != $entitisedname) { /// If name has some entity (& " < >) add that filter too. MDL-17545
75 self
::$activitylist[$cm->id
.'-e'] = new filterobject($entitisedname, $href_tag_begin, '</a>', false, true);
83 $filterslist = array();
84 if (self
::$activitylist) {
85 $cmid = $this->context
->instanceid
;
86 if ($this->context
->contextlevel
== CONTEXT_MODULE
&& isset(self
::$activitylist[$cmid])) {
87 // remove filterobjects for the current module
88 $filterslist = array_values(array_diff_key(self
::$activitylist, array($cmid => 1, $cmid.'-e' => 1)));
90 $filterslist = array_values(self
::$activitylist);
95 return $text = filter_phrases($text, $filterslist);
104 //This function is used to order module names from longer to shorter
105 function filter_activitynames_comparemodulenamesbylength($a, $b) {
106 if (strlen($a->name
) == strlen($b->name
)) {
109 return (strlen($a->name
) < strlen($b->name
)) ?
1 : -1;