mirrored from git://git.moodle.org/moodle.git
/
filter.php
127 lines (107 loc) · 4.63 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?PHP // $Id$
//This function provides automatic linking to
//activities when its name (title) is found inside every Moodle text
//It's based in the glosssary filter by Williams Castillo
//Modifications by stronk7.
function activitynames_filter($courseid, $text) {
global $CFG;
if (empty($courseid)) {
if ($site = get_site()) {
$courseid = $site->id;
}
}
$course = get_record("course","id",$courseid);
$modinfo = unserialize($course->modinfo);
//Sort modinfo by name length
usort($modinfo,'comparemodulenamesbylength');
if (!empty($modinfo)) {
$cm = '';
foreach ($modinfo as $activity) {
//Exclude labels and hidden items
if ($activity->mod != "label" && $activity->visible) {
$title = strip_tags(urldecode($activity->name));
$title = str_replace('"', "'", $title);
$href_tag_begin = "<a class=\"autolink\" title=\"$title\" href=\"$CFG->wwwroot/mod/$activity->mod/view.php?id=$activity->cm\">";
$currentname = urldecode($activity->name);
if ($currentname = trim($currentname)) {
//Avoid integers < 1000 to be linked. See bug 1441.
$intcurrent = intval($currentname);
if (!(!empty($intcurrent) && strval($intcurrent) == $currentname && $intcurrent < 1000)) {
$text = activity_link_names($text,$currentname,$href_tag_begin, "</a>");
}
}
}
}
}
return $text;
}
function activity_link_names($text,$name,$href_tag_begin,$href_tag_end = "</a>") {
$list_of_words_cp = strip_tags($name);
$list_of_words_cp = trim($list_of_words_cp,'|');
$list_of_words_cp = trim($list_of_words_cp);
$list_of_words_cp = preg_quote($list_of_words_cp,'/');
$invalidprefixs = "([a-zA-Z0-9])";
$invalidsufixs = "([a-zA-Z0-9])";
//Avoid seaching in the string if it's inside invalidprefixs and invalidsufixs
$words = array();
$regexp = '/'.$invalidprefixs.'('.$list_of_words_cp.')|('.$list_of_words_cp.')'.$invalidsufixs.'/is';
preg_match_all($regexp,$text,$list_of_words);
if ($list_of_words) {
foreach (array_unique($list_of_words[0]) as $key=>$value) {
$words['<*'.$key.'*>'] = $value;
}
if (!empty($words)) {
$text = str_replace($words,array_keys($words),$text);
}
}
//Now avoid searching inside the <nolink>tag
$excludes = array();
preg_match_all('/<nolink>(.+?)<\/nolink>/is',$text,$list_of_excludes);
foreach (array_unique($list_of_excludes[0]) as $key=>$value) {
$excludes['<+'.$key.'+>'] = $value;
}
if (!empty($excludes)) {
$text = str_replace($excludes,array_keys($excludes),$text);
}
//Now avoid searching inside links
$links = array();
preg_match_all('/<A[\s](.+?)>(.+?)<\/A>/is',$text,$list_of_links);
foreach (array_unique($list_of_links[0]) as $key=>$value) {
$links['<@'.$key.'@>'] = $value;
}
if (!empty($links)) {
$text = str_replace($links,array_keys($links),$text);
}
//Now avoid searching inside every tag
$final = array();
preg_match_all('/<(.+?)>/is',$text,$list_of_tags);
foreach (array_unique($list_of_tags[0]) as $key=>$value) {
$final['<|'.$key.'|>'] = $value;
}
if (!empty($final)) {
$text = str_replace($final,array_keys($final),$text);
}
$text = preg_replace('/('.$list_of_words_cp.')/is', $href_tag_begin.'$1'.$href_tag_end,$text);
//Now rebuild excluded areas
if (!empty($final)) {
$text = str_replace(array_keys($final),$final,$text);
}
if (!empty($links)) {
$text = str_replace(array_keys($links),$links,$text);
}
if (!empty($excludes)) {
$text = str_replace(array_keys($excludes),$excludes,$text);
}
if (!empty($words)) {
$text = str_replace(array_keys($words),$words,$text);
}
return $text;
}
//This function is used to order module names from longer to shorter
function comparemodulenamesbylength($a, $b) {
if (strlen($a->name) == strlen($b->name)) {
return 0;
}
return (strlen($a->name) < strlen($b->name)) ? 1 : -1;
}
?>