From: moodler
Date: Sun, 16 May 2004 06:41:39 +0000 (+0000)
Subject: Resource auto-linking filter! Developed by Eloy - thanks! (see bug 1389)
X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=d5aacf3e04b12aea73ff4c7b79af886992120143;p=moodle.git
Resource auto-linking filter! Developed by Eloy - thanks! (see bug 1389)
---
diff --git a/lang/en/resource.php b/lang/en/resource.php
index 6aca30f581..b4da2c73df 100644
--- a/lang/en/resource.php
+++ b/lang/en/resource.php
@@ -28,6 +28,7 @@ $string['fetchclienterror'] = 'An error was found with your web client while try
$string['fetcherror'] = 'An error was found while trying to retrieve the web page.';
$string['fetchservererror'] = 'An error was found with the remote server while trying to retrieve the web page (possibly a program error).
';
$string['filename'] = 'File name';
+$string['filtername'] = "Resource Auto-linking";
$string['fulltext'] = 'Full text';
$string['htmlfragment'] = 'HTML fragment';
$string['maindirectory'] = 'Main files directory';
diff --git a/mod/resource/filter.php b/mod/resource/filter.php
new file mode 100644
index 0000000000..de41f488d7
--- /dev/null
+++ b/mod/resource/filter.php
@@ -0,0 +1,136 @@
+id;
+ }
+ }
+
+ switch ($CFG->dbtype) {
+ case 'postgres7':
+ $as = 'as';
+ break;
+ case 'mysql':
+ $as = '';
+ break;
+ }
+
+ ///sorting by the lenght of the title in order to assure that large resources
+ ///could be linked first, if they exist in the text to parse
+
+ switch ($CFG->dbtype) {
+ case "postgres7":
+ case "mysql":
+ $rbylenght = "CHAR_LENGTH(name) desc";
+ break;
+ default:
+ $rbylenght = "";
+ break;
+ }
+
+ $resources = get_records_select("resource", "course = $courseid", "$rbylenght");
+
+ if (!empty($resources)) {
+ $cm = '';
+ foreach ($resources as $resource) {
+ if (!$cm = get_coursemodule_from_instance("resource", $resource->id, $courseid)) {
+ $cm->id = 1;
+ }
+ $title = strip_tags($resource->name);
+ $href_tag_begin = "wwwroot/mod/resource/view.php?id=$cm->id\">";
+ $currentname = $resource->name;
+ if ($currentname = trim($currentname)) {
+ $text = resource_link_names($text,$currentname,$href_tag_begin, "");
+ }
+ }
+ }
+ return $text;
+ }
+
+ function resource_link_names($text,$name,$href_tag_begin,$href_tag_end = "") {
+
+ $list_of_words_cp = $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;
+ }
+?>