New faster filters!

This commit is contained in:
moodler 2005-03-04 10:13:42 +00:00
parent facc743aa2
commit 3e88d524ad
2 changed files with 35 additions and 138 deletions

View File

@ -1,147 +1,30 @@
<?php // $Id$
//This function provides automatic linking to
//resources when its name (title) is found inside every Moodle text
//It's based in the glosssary filter by Williams Castillo
//Modifications by stronk7. Enjoy! :-)
//Williams, Stronk7, Martin D
function resource_filter($courseid, $text) {
global $CFG;
if (empty($courseid)) {
$courseid = SITEID;
}
/// The resources are sorted from long to short so longer ones can be linked first.
switch ($CFG->dbtype) {
case 'postgres7':
$as = 'as';
break;
case 'mysql':
$as = '';
break;
}
if ($resources = get_records('resource', 'course', $courseid, 'CHAR_LENGTH(name) DESC', 'id,name')) {
///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
$links = array();
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 = "<a class=\"autolink\" title=\"$title\" href=\"$CFG->wwwroot/mod/resource/view.php?id=$cm->id\">";
$currentname = $resource->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 = resource_link_names($text,$currentname,$href_tag_begin, "</a>");
}
}
$currentname = trim($resource->name);
$links[] = new filterobject($currentname,
'<a class="autolink" title="'.strip_tags($currentname).'" href="'.
$CFG->wwwroot.'/mod/resource/view.php?r='.$resource->id.'">',
'</a>', false, true);
}
$text = filter_phrases($text, $links); // Look for all these links in the text
}
return $text;
}
function resource_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);
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 the <span class="nolink">tag
// This style doesn't break in editor. See bug #2428
$nolinkspan = array();
preg_match_all('/<span class=\"nolink\">(.+?)<\/span>/is',$text,$list_of_span);
foreach (array_unique($list_of_span[0]) as $key=>$value) {
$nolinkspan['<%'.$key.'%>'] = $value;
}
if (!empty($nolinkspan)) {
$text = str_replace($nolinkspan,array_keys($nolinkspan),$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( $nolinkspan)) {
$text = str_replace(array_keys($nolinkspan),$nolinkspan,$text);
}
if (!empty($words)) {
$text = str_replace(array_keys($words),$words,$text);
}
return $text;
}
?>

View File

@ -3,22 +3,36 @@
require_once("../../config.php");
require_once("lib.php");
require_variable($id); // Course Module ID
$id = optional_param('id', 0, PARAM_INT); // Course Module ID
$r = optional_param('r', 0, PARAM_INT); // Resource
if (!empty($CFG->forcelogin)) {
require_login();
}
if (! $cm = get_record("course_modules", "id", $id)) {
error("Course Module ID was incorrect");
if ($r) { // Two ways to specify the resource
if (! $resource = get_record('resource', 'id', $r)) {
error('Resource ID was incorrect');
}
if (! $cm = get_coursemodule_from_instance('resource', $resource->id, $resource->course)) {
error('Course Module ID was incorrect');
}
} else if ($id) {
if (! $cm = get_record('course_modules', 'id', $id)) {
error('Course Module ID was incorrect');
}
if (! $resource = get_record('resource', 'id', $cm->instance)) {
error('Resource ID was incorrect');
}
} else {
error('No valid parameters!!');
}
if (! $resource = get_record("resource", "id", $cm->instance)) {
error("Resource ID was incorrect");
}
require ("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
$resourceclass = "resource_$resource->type";
require ($CFG->dirroot.'/mod/resource/type/'.$resource->type.'/resource.class.php');
$resourceclass = 'resource_'.$resource->type;
$resourceinstance = new $resourceclass($cm->id);
$resourceinstance->display();