Merge branch 'master_MDL-43222' of git://github.com/danmarsden/moodle
[moodle.git] / filter / data / filter.php
blobe703d92a959366b13f9f46ed90555315b4915608
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * This filter provides automatic linking to database activity entries
19 * when found inside every Moodle text.
21 * @package filter
22 * @subpackage data
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();
29 /**
30 * Database activity filtering
32 class filter_data extends moodle_text_filter {
34 public function filter($text, array $options = array()) {
35 global $CFG, $DB;
37 // Trivial-cache - keyed on $cachedcontextid
38 static $cachedcontextid;
39 static $contentlist;
41 static $nothingtodo;
43 // Try to get current course.
44 $coursectx = $this->context->get_course_context(false);
45 if (!$coursectx) {
46 $courseid = 0;
47 } else {
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();
55 $nothingtodo = false;
58 if ($nothingtodo === true) {
59 return $text;
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
71 FROM {data} d
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)) {
80 $nothingtodo = true;
81 return $text;
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]);
89 continue;
90 } else {
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)) {
102 $nothingtodo = true;
103 return $text;
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 '&amp;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);
124 if ($len0 < $len1) {
125 return 1;
126 } else if ($len0 > $len1) {
127 return -1;
128 } else {
129 return 0;