forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.php
112 lines (91 loc) · 4.13 KB
/
filter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This filter provides automatic linking to
* activities when its name (title) is found inside every Moodle text
*
* @package filter
* @subpackage activitynames
* @copyright 2004 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Activity name filtering
*/
class filter_activitynames extends moodle_text_filter {
// Trivial-cache - keyed on $cachedcourseid
static $activitylist = null;
static $cachedcourseid;
function filter($text, array $options = array()) {
global $CFG, $COURSE, $DB;
if (!$courseid = get_courseid_from_context($this->context)) {
return $text;
}
// Initialise/invalidate our trivial cache if dealing with a different course
if (!isset($this->cachedcourseid) || $this->cachedcourseid !== (int)$courseid) {
$this->activitylist = null;
}
$this->cachedcourseid = (int)$courseid;
/// It may be cached
if (is_null($this->activitylist)) {
$this->activitylist = array();
if ($COURSE->id == $courseid) {
$course = $COURSE;
} else {
$course = $DB->get_record("course", array("id"=>$courseid));
}
if (!isset($course->modinfo)) {
return $text;
}
/// Casting $course->modinfo to string prevents one notice when the field is null
$modinfo = unserialize((string)$course->modinfo);
if (!empty($modinfo)) {
$this->activitylist = array(); /// We will store all the activities here
//Sort modinfo by name length
usort($modinfo, 'filter_activitynames_comparemodulenamesbylength');
foreach ($modinfo as $activity) {
//Exclude labels, hidden activities and activities for group members only
if ($activity->mod != "label" and $activity->visible and empty($activity->groupmembersonly)) {
$title = s(trim(strip_tags($activity->name)));
$currentname = trim($activity->name);
$entitisedname = s($currentname);
/// Avoid empty or unlinkable activity names
if (!empty($title)) {
$href_tag_begin = "<a class=\"autolink\" title=\"$title\" href=\"$CFG->wwwroot/mod/$activity->mod/view.php?id=$activity->cm\">";
$this->activitylist[] = new filterobject($currentname, $href_tag_begin, '</a>', false, true);
if ($currentname != $entitisedname) { /// If name has some entity (& " < >) add that filter too. MDL-17545
$this->activitylist[] = new filterobject($entitisedname, $href_tag_begin, '</a>', false, true);
}
}
}
}
}
}
if ($this->activitylist) {
return $text = filter_phrases ($text, $this->activitylist);
} else {
return $text;
}
}
}
//This function is used to order module names from longer to shorter
function filter_activitynames_comparemodulenamesbylength($a, $b) {
if (strlen($a->name) == strlen($b->name)) {
return 0;
}
return (strlen($a->name) < strlen($b->name)) ? 1 : -1;
}