2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This filter provides automatic linking to database activity entries
19 * when found inside every Moodle text.
23 * @copyright 2006 Vy-Shane Sin Fat
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
30 * Database activity filtering
32 class filter_data
extends moodle_text_filter
{
34 public function filter($text, array $options = array()) {
37 // Trivial-cache - keyed on $cachedcontextid
38 static $cachedcontextid;
43 // Try to get current course.
44 $coursectx = $this->context
->get_course_context(false);
48 $courseid = $coursectx->instanceid
;
51 // Initialise/invalidate our trivial cache if dealing with a different context
52 if (!isset($cachedcontextid) ||
$cachedcontextid !== $this->context
->id
) {
53 $cachedcontextid = $this->context
->id
;
54 $contentlist = array();
58 if ($nothingtodo === true) {
62 // Create a list of all the resources to search for. It may be cached already.
63 if (empty($contentlist)) {
64 $coursestosearch = $courseid ?
array($courseid) : array(); // Add courseid if found
65 if (get_site()->id
!= $courseid) { // Add siteid if was not courseid
66 $coursestosearch[] = get_site()->id
;
68 // We look for text field contents only if have autolink enabled (param1)
69 list ($coursesql, $params) = $DB->get_in_or_equal($coursestosearch);
70 $sql = 'SELECT dc.id AS contentid, dr.id AS recordid, dc.content AS content, d.id AS dataid
72 JOIN {data_fields} df ON df.dataid = d.id
73 JOIN {data_records} dr ON dr.dataid = d.id
74 JOIN {data_content} dc ON dc.fieldid = df.id AND dc.recordid = dr.id
75 WHERE d.course ' . $coursesql . '
76 AND df.type = \'text\'
77 AND ' . $DB->sql_compare_text('df.param1', 1) . " = '1'";
79 if (!$contents = $DB->get_records_sql($sql, $params)) {
84 foreach ($contents as $key => $content) {
85 // Trim empty or unlinkable concepts
86 $currentcontent = trim(strip_tags($content->content
));
87 if (empty($currentcontent)) {
88 unset($contents[$key]);
91 $contents[$key]->content
= $currentcontent;
94 // Rule out any small integers. See bug 1446
95 $currentint = intval($currentcontent);
96 if ($currentint && (strval($currentint) == $currentcontent) && $currentint < 1000) {
97 unset($contents[$key]);
101 if (empty($contents)) {
106 usort($contents, 'filter_data::sort_entries_by_length');
108 foreach ($contents as $content) {
109 $href_tag_begin = '<a class="data autolink dataid'.$content->dataid
.'" title="'.$content->content
.'" '.
110 'href="'.$CFG->wwwroot
.'/mod/data/view.php?d='.$content->dataid
.
111 '&rid='.$content->recordid
.'">';
112 $contentlist[] = new filterobject($content->content
, $href_tag_begin, '</a>', false, true);
115 $contentlist = filter_remove_duplicates($contentlist); // Clean dupes
117 return filter_phrases($text, $contentlist); // Look for all these links in the text
120 private static function sort_entries_by_length($content0, $content1) {
121 $len0 = strlen($content0->content
);
122 $len1 = strlen($content1->content
);
126 } else if ($len0 > $len1) {