Merged from MOODLE_14_STABLE: Added addslashes back in to username field after splitt...
[moodle.git] / filter / activitynames / filter.php
CommitLineData
45a1ad99 1<?PHP // $Id$
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) {
8
9 global $CFG;
10
11 if (empty($courseid)) {
222ac91b 12 $courseid = SITEID;
45a1ad99 13 }
14
15 $course = get_record("course","id",$courseid);
16 $modinfo = unserialize($course->modinfo);
17
18 if (!empty($modinfo)) {
19 $cm = '';
20 foreach ($modinfo as $activity) {
341d554f 21 //Exclude labels and hidden items
22 if ($activity->mod != "label" && $activity->visible) {
23 $title = strip_tags(urldecode($activity->name));
19f671d3 24 $title = str_replace('"', "'", $title);
341d554f 25 $href_tag_begin = "<a class=\"autolink\" title=\"$title\" href=\"$CFG->wwwroot/mod/$activity->mod/view.php?id=$activity->cm\">";
26 $currentname = urldecode($activity->name);
27 if ($currentname = trim($currentname)) {
28 //Avoid integers < 1000 to be linked. See bug 1441.
29 $intcurrent = intval($currentname);
30 if (!(!empty($intcurrent) && strval($intcurrent) == $currentname && $intcurrent < 1000)) {
31 $text = activity_link_names($text,$currentname,$href_tag_begin, "</a>");
32 }
45a1ad99 33 }
34 }
35 }
36 }
37 return $text;
38 }
39
40 function activity_link_names($text,$name,$href_tag_begin,$href_tag_end = "</a>") {
41
42 $list_of_words_cp = strip_tags($name);
43
44 $list_of_words_cp = trim($list_of_words_cp,'|');
45
46 $list_of_words_cp = trim($list_of_words_cp);
47
48 $list_of_words_cp = preg_quote($list_of_words_cp,'/');
49
50 $invalidprefixs = "([a-zA-Z0-9])";
51 $invalidsufixs = "([a-zA-Z0-9])";
52
53 //Avoid seaching in the string if it's inside invalidprefixs and invalidsufixs
54 $words = array();
55 $regexp = '/'.$invalidprefixs.'('.$list_of_words_cp.')|('.$list_of_words_cp.')'.$invalidsufixs.'/is';
56 preg_match_all($regexp,$text,$list_of_words);
57
6bda9403 58 if ($list_of_words) {
59 foreach (array_unique($list_of_words[0]) as $key=>$value) {
60 $words['<*'.$key.'*>'] = $value;
61 }
62 if (!empty($words)) {
63 $text = str_replace($words,array_keys($words),$text);
64 }
45a1ad99 65 }
66
67 //Now avoid searching inside the <nolink>tag
68 $excludes = array();
69 preg_match_all('/<nolink>(.+?)<\/nolink>/is',$text,$list_of_excludes);
70 foreach (array_unique($list_of_excludes[0]) as $key=>$value) {
71 $excludes['<+'.$key.'+>'] = $value;
72 }
73 if (!empty($excludes)) {
74 $text = str_replace($excludes,array_keys($excludes),$text);
75 }
76
77 //Now avoid searching inside links
78 $links = array();
2ed07cc7 79 preg_match_all('/<A[\s](.+?)>(.+?)<\/A>/is',$text,$list_of_links);
45a1ad99 80 foreach (array_unique($list_of_links[0]) as $key=>$value) {
81 $links['<@'.$key.'@>'] = $value;
82 }
83 if (!empty($links)) {
84 $text = str_replace($links,array_keys($links),$text);
85 }
86
87 //Now avoid searching inside every tag
88 $final = array();
89 preg_match_all('/<(.+?)>/is',$text,$list_of_tags);
90 foreach (array_unique($list_of_tags[0]) as $key=>$value) {
91 $final['<|'.$key.'|>'] = $value;
92 }
93 if (!empty($final)) {
94 $text = str_replace($final,array_keys($final),$text);
95 }
96
97 $text = preg_replace('/('.$list_of_words_cp.')/is', $href_tag_begin.'$1'.$href_tag_end,$text);
98
99 //Now rebuild excluded areas
100 if (!empty($final)) {
101 $text = str_replace(array_keys($final),$final,$text);
102 }
103 if (!empty($links)) {
104 $text = str_replace(array_keys($links),$links,$text);
105 }
106 if (!empty($excludes)) {
107 $text = str_replace(array_keys($excludes),$excludes,$text);
108 }
109 if (!empty($words)) {
110 $text = str_replace(array_keys($words),$words,$text);
111 }
112 return $text;
113 }
114?>