From 45a1ad99a2814478e24a9cc2becffb1f973cf28b Mon Sep 17 00:00:00 2001 From: stronk7 Date: Mon, 24 May 2004 17:30:56 +0000 Subject: [PATCH] Allow every activity name in texts to be linked to the original activity. Bug 1454. (http://moodle.org/bugs/bug.php?op=show&bugid=1454) --- filter/activitynames/filter.php | 117 ++++++++++++++++++++++++++++++++ lang/en/activitynames.php | 6 ++ 2 files changed, 123 insertions(+) create mode 100644 filter/activitynames/filter.php create mode 100644 lang/en/activitynames.php diff --git a/filter/activitynames/filter.php b/filter/activitynames/filter.php new file mode 100644 index 0000000000..c65ab95716 --- /dev/null +++ b/filter/activitynames/filter.php @@ -0,0 +1,117 @@ +id; + } + } + + $course = get_record("course","id",$courseid); + $modinfo = unserialize($course->modinfo); + + if (!empty($modinfo)) { + $cm = ''; + foreach ($modinfo as $activity) { +//echo $activity->name."-".urldecode($activity->name)."
"; + $title = strip_tags(urldecode($activity->name)); + $href_tag_begin = "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, ""); + } + } + } + } + return $text; + } + + function activity_link_names($text,$name,$href_tag_begin,$href_tag_end = "") { + + $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); + + 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 tag + $excludes = array(); + preg_match_all('/(.+?)<\/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>/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; + } +?> diff --git a/lang/en/activitynames.php b/lang/en/activitynames.php new file mode 100644 index 0000000000..557390f6ce --- /dev/null +++ b/lang/en/activitynames.php @@ -0,0 +1,6 @@ + + -- 2.39.5