|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 defined('MOODLE_INTERNAL') || die(); 00028 00032 class filter_data extends moodle_text_filter { 00033 00034 public function filter($text, array $options = array()) { 00035 global $CFG, $DB; 00036 00037 // Trivial-cache - keyed on $cachedcontextid 00038 static $cachedcontextid; 00039 static $contentlist; 00040 00041 static $nothingtodo; 00042 00043 // Try to get current course 00044 if (!$courseid = get_courseid_from_context($this->context)) { 00045 $courseid = 0; 00046 } 00047 00048 // Initialise/invalidate our trivial cache if dealing with a different context 00049 if (!isset($cachedcontextid) || $cachedcontextid !== $this->context->id) { 00050 $cachedcontextid = $this->context->id; 00051 $contentlist = array(); 00052 $nothingtodo = false; 00053 } 00054 00055 if ($nothingtodo === true) { 00056 return $text; 00057 } 00058 00059 // Create a list of all the resources to search for. It may be cached already. 00060 if (empty($contentlist)) { 00061 $coursestosearch = $courseid ? array($courseid) : array(); // Add courseid if found 00062 if (get_site()->id != $courseid) { // Add siteid if was not courseid 00063 $coursestosearch[] = get_site()->id; 00064 } 00065 // We look for text field contents only if have autolink enabled (param1) 00066 list ($coursesql, $params) = $DB->get_in_or_equal($coursestosearch); 00067 $sql = 'SELECT dc.id AS contentid, dr.id AS recordid, dc.content AS content, d.id AS dataid 00068 FROM {data} d 00069 JOIN {data_fields} df ON df.dataid = d.id 00070 JOIN {data_records} dr ON dr.dataid = d.id 00071 JOIN {data_content} dc ON dc.fieldid = df.id AND dc.recordid = dr.id 00072 WHERE d.course ' . $coursesql . ' 00073 AND df.type = \'text\' 00074 AND ' . $DB->sql_compare_text('df.param1', 1) . " = '1'"; 00075 00076 if (!$contents = $DB->get_records_sql($sql, $params)) { 00077 $nothingtodo = true; 00078 return $text; 00079 } 00080 00081 foreach ($contents as $key => $content) { 00082 // Trim empty or unlinkable concepts 00083 $currentcontent = trim(strip_tags($content->content)); 00084 if (empty($currentcontent)) { 00085 unset($contents[$key]); 00086 continue; 00087 } else { 00088 $contents[$key]->content = $currentcontent; 00089 } 00090 00091 // Rule out any small integers. See bug 1446 00092 $currentint = intval($currentcontent); 00093 if ($currentint && (strval($currentint) == $currentcontent) && $currentint < 1000) { 00094 unset($contents[$key]); 00095 } 00096 } 00097 00098 if (empty($contents)) { 00099 $nothingtodo = true; 00100 return $text; 00101 } 00102 00103 usort($contents, 'filter_data::sort_entries_by_length'); 00104 00105 foreach ($contents as $content) { 00106 $href_tag_begin = '<a class="data autolink dataid'.$content->dataid.'" title="'.$content->content.'" '. 00107 'href="'.$CFG->wwwroot.'/mod/data/view.php?d='.$content->dataid. 00108 '&rid='.$content->recordid.'">'; 00109 $contentlist[] = new filterobject($content->content, $href_tag_begin, '</a>', false, true); 00110 } 00111 00112 $contentlist = filter_remove_duplicates($contentlist); // Clean dupes 00113 } 00114 return filter_phrases($text, $contentlist); // Look for all these links in the text 00115 } 00116 00117 private static function sort_entries_by_length($content0, $content1) { 00118 $len0 = strlen($content0->content); 00119 $len1 = strlen($content1->content); 00120 00121 if ($len0 < $len1) { 00122 return 1; 00123 } else if ($len0 > $len1) { 00124 return -1; 00125 } else { 00126 return 0; 00127 } 00128 } 00129 }