weekly release 2.3.9+
[moodle.git] / filter / activitynames / filter.php
CommitLineData
4317f92f 1<?php
35716b86
PS
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
19 * This filter provides automatic linking to
20 * activities when its name (title) is found inside every Moodle text
21 *
22 * @package filter
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
26 */
27
28defined('MOODLE_INTERNAL') || die();
29
30/**
31 * Activity name filtering
32 */
33class filter_activitynames extends moodle_text_filter {
9e3f34d1 34 // Trivial-cache - keyed on $cachedcourseid
35 static $activitylist = null;
36 static $cachedcourseid;
45a1ad99 37
dcfffe30 38 function filter($text, array $options = array()) {
9e3f34d1 39 global $CFG, $COURSE, $DB;
eb6eb58e 40
35716b86
PS
41 if (!$courseid = get_courseid_from_context($this->context)) {
42 return $text;
45a1ad99 43 }
44
9aa90807 45 // Initialise/invalidate our trivial cache if dealing with a different course
68a7c9a6
PS
46 if (!isset(self::$cachedcourseid) || self::$cachedcourseid !== (int)$courseid) {
47 self::$activitylist = null;
4317f92f 48 }
68a7c9a6 49 self::$cachedcourseid = (int)$courseid;
9aa90807 50
34d186eb 51 /// It may be cached
45a1ad99 52
68a7c9a6
PS
53 if (is_null(self::$activitylist)) {
54 self::$activitylist = array();
dd97c328 55
35716b86 56 if ($COURSE->id == $courseid) {
dd97c328 57 $course = $COURSE;
58 } else {
35716b86 59 $course = $DB->get_record("course", array("id"=>$courseid));
dd97c328 60 }
61
62 if (!isset($course->modinfo)) {
63 return $text;
64 }
07158c1e 65
fea43a7f 66 /// Casting $course->modinfo to string prevents one notice when the field is null
67 $modinfo = unserialize((string)$course->modinfo);
facc743a 68
34d186eb 69 if (!empty($modinfo)) {
07158c1e 70
68a7c9a6 71 self::$activitylist = array(); /// We will store all the activities here
34d186eb 72
73 //Sort modinfo by name length
35716b86 74 usort($modinfo, 'filter_activitynames_comparemodulenamesbylength');
34d186eb 75
76 foreach ($modinfo as $activity) {
4317f92f 77 //Exclude labels, hidden activities and activities for group members only
dd97c328 78 if ($activity->mod != "label" and $activity->visible and empty($activity->groupmembersonly)) {
9a9012dc
PS
79 $title = s(trim(strip_tags($activity->name)));
80 $currentname = trim($activity->name);
d533ff3a 81 $entitisedname = s($currentname);
32a50e28 82 /// Avoid empty or unlinkable activity names
83 if (!empty($title)) {
4aea3cc7 84 $href_tag_begin = "<a class=\"autolink\" title=\"$title\" href=\"$CFG->wwwroot/mod/$activity->mod/view.php?id=$activity->cm\">";
68a7c9a6 85 self::$activitylist[] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
d533ff3a 86 if ($currentname != $entitisedname) { /// If name has some entity (&amp; &quot; &lt; &gt;) add that filter too. MDL-17545
68a7c9a6 87 self::$activitylist[] = new filterobject($entitisedname, $href_tag_begin, '</a>', false, true);
32a50e28 88 }
34d186eb 89 }
45a1ad99 90 }
91 }
92 }
93 }
34d186eb 94
68a7c9a6
PS
95 if (self::$activitylist) {
96 return $text = filter_phrases ($text, self::$activitylist);
dd97c328 97 } else {
98 return $text;
99 }
45a1ad99 100 }
9e3f34d1 101}
5096a8ea 102
45a1ad99 103
3a558dd9 104
9e3f34d1 105//This function is used to order module names from longer to shorter
35716b86 106function filter_activitynames_comparemodulenamesbylength($a, $b) {
9e3f34d1 107 if (strlen($a->name) == strlen($b->name)) {
108 return 0;
3a558dd9 109 }
9e3f34d1 110 return (strlen($a->name) < strlen($b->name)) ? 1 : -1;
111}
4317f92f 112