2003-10-16 06:00:47 +00:00
< ? PHP // $Id$
2003-09-29 17:45:42 +00:00
2003-10-13 14:11:17 +00:00
$textfilter_function = 'glossary_dynamic_link' ;
if ( function_exists ( $textfilter_function )) {
return ;
}
2003-10-16 22:05:00 +00:00
function glossary_dynamic_link ( $courseid , $text ) {
2003-09-29 17:45:42 +00:00
global $CFG ;
2003-10-16 22:05:00 +00:00
2003-10-20 16:42:49 +00:00
$GLOSSARY_CONCEPT_IS_ENTRY = 0 ;
$GLOSSARY_CONCEPT_IS_CATEGORY = 1 ;
2003-10-29 23:33:54 +00:00
$glossarieslist = get_records_select ( " glossary " , " usedynalink != 0 and (course = $courseid or globalglossary != 0) " , " globalglossary, id " );
2003-10-16 22:05:00 +00:00
if ( $glossarieslist ) {
$glossaries = " " ;
foreach ( $glossarieslist as $glossary ) {
$glossaries .= " $glossary->id , " ;
2003-09-30 04:01:34 +00:00
}
2003-10-16 22:05:00 +00:00
$glossaries = substr ( $glossaries , 0 , - 1 );
2003-10-21 20:45:53 +00:00
/// sorting by the lenght of the concept in order to assure that large concepts
/// could be linked first, if they exist in the text to parse
switch ( $CFG -> dbtype ) {
case " postgres7 " :
case " mysql " :
$ebylenght = " CHAR_LENGTH(concept) desc, " ;
$cbylenght = " CHAR_LENGTH(name) desc, " ;
break ;
default :
$ebylenght = " " ;
$cbylenght = " " ;
break ;
}
2003-10-29 23:33:54 +00:00
$entries = get_records_select ( " glossary_entries " , " glossaryid IN ( $glossaries ) AND usedynalink != 0 and approved != 0 and concept != '' " , " $ebylenght glossaryid " , " id,glossaryid,concept,casesensitive, $GLOSSARY_CONCEPT_IS_ENTRY category,fullmatch " );
2003-11-19 17:47:40 +00:00
$categories = get_records_select ( " glossary_categories " , " glossaryid IN ( $glossaries ) AND usedynalink != 0 " , " $cbylenght glossaryid " , " id,glossaryid,name concept, 1 casesensitive, $GLOSSARY_CONCEPT_IS_CATEGORY category, 1 fullmatch " );
2003-10-16 22:05:00 +00:00
if ( $entries and $categories ) {
$concepts = array_merge ( $entries , $categories );
2003-10-22 04:15:08 +00:00
usort ( $concepts , 'glossary_sort_entries_by_lenght' );
2003-10-16 22:05:00 +00:00
} elseif ( $categories ) {
$concepts = $categories ;
} elseif ( $entries ) {
$concepts = $entries ;
2003-09-30 04:01:34 +00:00
}
2003-10-21 20:45:53 +00:00
if ( isset ( $concepts ) ) {
2003-10-16 22:05:00 +00:00
$lastglossary = 0 ;
2003-10-17 15:35:18 +00:00
$lastcategory = 0 ;
2003-11-21 03:33:21 +00:00
$cm = '' ;
2003-10-16 22:05:00 +00:00
foreach ( $concepts as $concept ) {
if ( $concept -> category ) {
if ( $lastcategory != $concept -> id ) {
$category = get_record ( " glossary_categories " , " id " , $concept -> id );
$lastcategory = $concept -> id ;
2003-11-19 17:47:40 +00:00
if ( $cm -> instance != $category -> glossaryid ) {
$cm = get_coursemodule_from_instance ( " glossary " , $category -> glossaryid , $courseid );
}
2003-10-16 22:05:00 +00:00
}
2003-11-19 17:47:40 +00:00
2003-10-16 22:05:00 +00:00
$title = strip_tags ( " $glossary->name : " . get_string ( " category " , " glossary " ) . " $category->name " );
2003-11-15 15:55:47 +00:00
$href_tag_begin = " <a class= \" autolink \" title= \" $title\ " href = \ " $CFG->wwwroot /mod/glossary/view.php?id= $cm->id &mode=cat&hook= $concept->id\ " > " ;
2003-10-16 22:05:00 +00:00
} else {
2003-11-19 17:47:40 +00:00
if ( $lastglossary != $concept -> glossaryid ) {
$glossary = get_record ( " glossary " , " id " , $concept -> glossaryid );
$lastglossary = $glossary -> id ;
}
2003-11-14 17:18:15 +00:00
$concepttitle = urlencode ( $concept -> concept );
$title = strip_tags ( " $glossary->name : $concepttitle " );
$href_tag_begin = " <a target= \" entry \" class= \" autolink \" title= \" $title\ " href = \ " $CFG->wwwroot /mod/glossary/showentry.php?courseid= $courseid &concept= $concepttitle\ " " .
" onClick= \" return openpopup('/mod/glossary/showentry.php?courseid= $courseid\ &concept= $concepttitle ', 'entry', 'menubar=0,location=0,scrollbars,resizable,width=600,height=450', 0); \" > " ;
2003-10-16 22:05:00 +00:00
}
2003-11-20 23:37:33 +00:00
$replace = " \\ []' \" *() " ;
$currentconcept = glossary_addslashes ( $replace , $concept -> concept );
2003-11-04 15:04:35 +00:00
if ( $currentconcept = trim ( strip_tags ( $currentconcept )) ) {
if ( ! $concept -> category ) {
2003-11-19 17:47:40 +00:00
if ( $aliases = get_records ( " glossary_alias " , " entryid " , $concept -> id , " alias " ) ) {
2003-11-04 15:04:35 +00:00
foreach ( $aliases as $alias ) {
2003-11-20 23:37:33 +00:00
$currentalias = glossary_addslashes ( $replace , $alias -> alias );
2003-11-19 17:47:40 +00:00
$currentconcept .= " | " . trim ( $currentalias );
2003-11-04 15:04:35 +00:00
}
2003-11-03 02:26:44 +00:00
}
}
2003-11-04 15:04:35 +00:00
$text = glossary_link_concepts ( $text , $currentconcept , $href_tag_begin , " </a> " , $concept -> casesensitive , $concept -> fullmatch );
2003-11-03 02:26:44 +00:00
}
2003-09-29 17:45:42 +00:00
}
}
}
return $text ;
}
2003-10-16 22:05:00 +00:00
function glossary_link_concepts ( $text , $concept , $href_tag_begin , $href_tag_end = " </a> " , $casesensitive , $fullmatch ) {
2003-10-21 16:30:12 +00:00
$concept = str_replace ( " / " , " \ / " , $concept );
2003-10-15 17:27:51 +00:00
$list_of_words_cp = $concept ;
2003-09-29 17:45:42 +00:00
2003-10-16 22:05:00 +00:00
if ( $list_of_words_cp { 0 } == " | " ) {
$list_of_words_cp { 0 } = " " ;
}
if ( $list_of_words_cp { strlen ( $list_of_words_cp ) - 1 } == " | " ) {
$list_of_words_cp { strlen ( $list_of_words_cp ) - 1 } = " " ;
}
2003-11-19 17:47:40 +00:00
2003-10-16 22:05:00 +00:00
$list_of_words_cp = trim ( $list_of_words_cp );
if ( $fullmatch ) {
$invalidprefixs = " ([a-zA-Z0-9]) " ;
$invalidsufixs = " ([a-zA-Z0-9]) " ;
2003-10-15 17:27:51 +00:00
2003-10-16 22:05:00 +00:00
// getting ride of words or phrases that containg the pivot concept on it
$words = array ();
$regexp = '/' . $invalidprefixs . " ( " . $list_of_words_cp . " ) " . " | " . " ( " . $list_of_words_cp . " ) " . $invalidsufixs . '/is' ;
preg_match_all ( $regexp , $text , $list_of_words );
2003-10-21 13:27:48 +00:00
if ( $list_of_words ) {
foreach ( array_unique ( $list_of_words [ 0 ]) as $key => $value ) {
$words [ '<*' . $key . '*>' ] = $value ;
}
if ( $words ) {
$text = str_replace ( $words , array_keys ( $words ), $text );
}
2003-10-16 22:05:00 +00:00
}
}
// getting ride of "nolink" tags
$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 ( $excludes ) {
$text = str_replace ( $excludes , array_keys ( $excludes ), $text );
}
// getting ride of "A" tags
$links = array ();
2003-10-15 17:27:51 +00:00
preg_match_all ( '/<A (.+?)>(.+?)<\/A>/is' , $text , $list_of_links );
foreach ( array_unique ( $list_of_links [ 0 ]) as $key => $value ) {
2003-10-16 22:05:00 +00:00
$links [ '<@' . $key . '@>' ] = $value ;
2003-10-15 17:27:51 +00:00
}
2003-10-16 22:05:00 +00:00
if ( $links ) {
2003-10-15 22:01:12 +00:00
$text = str_replace ( $links , array_keys ( $links ), $text );
}
2003-10-16 22:05:00 +00:00
// getting ride of all other tags
2003-09-29 17:45:42 +00:00
$final = array ();
2003-10-29 23:33:54 +00:00
preg_match_all ( '/<(.+?)>/is' , $text , $list_of_tags );
2003-09-29 17:45:42 +00:00
2003-10-29 23:33:54 +00:00
foreach ( array_unique ( $list_of_tags [ 0 ]) as $key => $value ) {
2003-09-29 17:45:42 +00:00
$final [ '<|' . $key . '|>' ] = $value ;
}
2003-10-15 17:27:51 +00:00
2003-09-29 17:45:42 +00:00
$text = str_replace ( $final , array_keys ( $final ), $text );
2003-10-20 16:42:49 +00:00
$list_of_words_cp = " ( " . $list_of_words_cp . " ) " ;
2003-10-16 22:05:00 +00:00
if ( $casesensitive ) {
$text = ereg_replace ( " $list_of_words_cp " , " $href_tag_begin " . " \\ 1 " . " $href_tag_end " , $text );
} else {
$text = eregi_replace ( " $list_of_words_cp " , " $href_tag_begin " . " \\ 1 " . " $href_tag_end " , $text );
2003-09-29 17:45:42 +00:00
}
2003-11-19 17:47:40 +00:00
if ( $final ) {
$text = str_replace ( array_keys ( $final ), $final , $text );
}
2003-10-16 22:05:00 +00:00
if ( $links ) {
2003-10-15 22:01:12 +00:00
$text = str_replace ( array_keys ( $links ), $links , $text );
}
2003-10-16 22:05:00 +00:00
if ( $excludes ) {
$text = str_replace ( array_keys ( $excludes ), $excludes , $text );
}
2003-11-19 17:47:40 +00:00
if ( $fullmatch and isset ( $words ) ) {
if ( $words ) {
$text = str_replace ( array_keys ( $words ), $words , $text );
2003-10-20 16:42:49 +00:00
}
2003-10-16 22:05:00 +00:00
}
2003-11-14 17:18:15 +00:00
return $text ;
2003-09-29 17:45:42 +00:00
}
2003-09-30 04:01:34 +00:00
function glossary_sort_entries_by_lenght ( $entry0 , $entry1 ) {
if ( strlen ( trim ( $entry0 -> concept )) < strlen ( trim ( $entry1 -> concept )) ) {
return 1 ;
2003-10-22 04:15:08 +00:00
} elseif ( strlen ( trim ( $entry0 -> concept )) > strlen ( trim ( $entry1 -> concept )) ) {
return - 1 ;
2003-09-30 04:01:34 +00:00
} else {
return 0 ;
}
}
2003-11-20 23:37:33 +00:00
function glossary_addslashes ( $chars , $text ) {
if ( $chars ) {
for ( $i = 0 ; $i < strlen ( $chars ); $i ++ ) {
$text = str_replace ( $chars [ $i ], " \\ " . $chars [ $i ], $text );
}
}
return $text ;
}
2003-11-12 06:21:15 +00:00
?>