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