MDL-29993 filters - new filter/data filter replacement for legacy mod/data/filter.
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Sun, 30 Oct 2011 00:33:11 +0000 (02:33 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Sun, 30 Oct 2011 00:33:11 +0000 (02:33 +0200)
filter/data/db/install.php [new file with mode: 0644]
filter/data/filter.php [new file with mode: 0644]
filter/data/lang/en/filter_data.php [new file with mode: 0644]
filter/data/version.php [new file with mode: 0644]
mod/data/filter.php [deleted file]
mod/data/version.php

diff --git a/filter/data/db/install.php b/filter/data/db/install.php
new file mode 100644 (file)
index 0000000..dd171b8
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Database activity filter post install hook
+ *
+ * @package    filter
+ * @subpackage data
+ * @copyright  2011 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+function xmldb_filter_data_install() {
+    global $DB;
+
+    // If the legacy mod/data filter is installed we need to:
+    //   1- Delete new filter (filter_active and filter_config) information, in order to
+    //   2- Usurpate the identity of the legacy filter by moving all its
+    //      information to filter/data
+    // If the legacy mod/data filter was not installed, no action is needed
+    if ($DB->record_exists('filter_active', array('filter' => 'mod/data'))) {
+        $DB->delete_records('filter_active', array('filter' => 'filter/data'));
+        $DB->set_field('filter_active', 'filter', 'filter/data', array('filter' => 'mod/data'));
+    }
+}
diff --git a/filter/data/filter.php b/filter/data/filter.php
new file mode 100644 (file)
index 0000000..60ca85f
--- /dev/null
@@ -0,0 +1,129 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * This filter provides automatic linking to database activity entries
+ * when found inside every Moodle text.
+ *
+ * @package    filter
+ * @subpackage data
+ * @copyright  2006 Vy-Shane Sin Fat
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Database activity filtering
+ */
+class filter_data extends moodle_text_filter {
+
+    public function filter($text, array $options = array()) {
+        global $CFG, $DB;
+
+        // Trivial-cache - keyed on $cachedcontextid
+        static $cachedcontextid;
+        static $contentlist;
+
+        static $nothingtodo;
+
+        // Try to get current course
+        if (!$courseid = get_courseid_from_context($this->context)) {
+            $courseid = 0;
+        }
+
+        // Initialise/invalidate our trivial cache if dealing with a different context
+        if (!isset($cachedcontextid) || $cachedcontextid !== $this->context->id) {
+            $cachedcontextid = $this->context->id;
+            $contentlist = array();
+            $nothingtodo = false;
+        }
+
+        if ($nothingtodo === true) {
+            return $text;
+        }
+
+        // Create a list of all the resources to search for. It may be cached already.
+        if (empty($contentlist)) {
+            $coursestosearch = $courseid ? array($courseid) : array(); // Add courseid if found
+            if (get_site()->id != $courseid) { // Add siteid if was not courseid
+                $coursestosearch[] = get_site()->id;
+            }
+            // We look for text field contents only if have autolink enabled (param1)
+            list ($coursesql, $params) = $DB->get_in_or_equal($coursestosearch);
+            $sql = 'SELECT dc.id AS contentid, dr.id AS recordid, dc.content AS content, d.id AS dataid
+                      FROM {data} d
+                      JOIN {data_fields} df ON df.dataid = d.id
+                      JOIN {data_records} dr ON dr.dataid = d.id
+                      JOIN {data_content} dc ON dc.fieldid = df.id AND dc.recordid = dr.id
+                     WHERE d.course ' . $coursesql . '
+                       AND df.type = \'text\'
+                       AND ' . $DB->sql_compare_text('df.param1', 1) . ' = 1';
+
+            if (!$contents = $DB->get_records_sql($sql, $params)) {
+                $nothingtodo = true;
+                return $text;
+            }
+
+            foreach ($contents as $key => $content) {
+                // Trim empty or unlinkable concepts
+                $currentcontent = trim(strip_tags($content->content));
+                if (empty($currentcontent)) {
+                    unset($contents[$key]);
+                    continue;
+                } else {
+                    $contents[$key]->content = $currentcontent;
+                }
+
+                // Rule out any small integers.  See bug 1446
+                $currentint = intval($currentcontent);
+                if ($currentint && (strval($currentint) == $currentcontent) && $currentint < 1000) {
+                    unset($contents[$key]);
+                }
+            }
+
+            if (empty($contents)) {
+                $nothingtodo = true;
+                return $text;
+            }
+
+            usort($contents, 'filter_data::sort_entries_by_length');
+
+            foreach ($contents as $content) {
+                $href_tag_begin = '<a class="data autolink dataid'.$content->dataid.'" title="'.$content->content.'" '.
+                                  'href="'.$CFG->wwwroot.'/mod/data/view.php?d='.$content->dataid.
+                                  '&amp;rid='.$content->recordid.'">';
+                $contentlist[] = new filterobject($content->content, $href_tag_begin, '</a>', false, true);
+            }
+
+            $contentlist = filter_remove_duplicates($contentlist); // Clean dupes
+        }
+        return filter_phrases($text, $contentlist);  // Look for all these links in the text
+    }
+
+    private static function sort_entries_by_length($content0, $content1) {
+        $len0 = strlen($content0->content);
+        $len1 = strlen($content1->content);
+
+        if ($len0 < $len1) {
+            return 1;
+        } else if ($len0 > $len1) {
+            return -1;
+        } else {
+            return 0;
+        }
+    }
+}
diff --git a/filter/data/lang/en/filter_data.php b/filter/data/lang/en/filter_data.php
new file mode 100644 (file)
index 0000000..a6d1f4d
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Strings for filter_data
+ *
+ * @package    filter
+ * @subpackage data
+ * @copyright  2011 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+$string['filtername'] = 'Database auto-linking';
diff --git a/filter/data/version.php b/filter/data/version.php
new file mode 100644 (file)
index 0000000..a6ec47b
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Data activity filter version information
+ *
+ * @package    filter
+ * @subpackage data
+ * @copyright  2011 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+$plugin->version  = 2011102800;
+$plugin->requires = 2011102700;  // Requires this Moodle version
+$plugin->component= 'filter_data';
+
+$plugin->dependencies = array('mod_data' => 2011102800);
diff --git a/mod/data/filter.php b/mod/data/filter.php
deleted file mode 100644 (file)
index 6a81b2b..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-<?php
-    //
-    // This function provides automatic linking to data contents of text
-    // fields where these fields have autolink enabled.
-    //
-    // Original code by Williams, Stronk7, Martin D.
-    // Modified for data module by Vy-Shane SF.
-
-    function data_filter($courseid, $text) {
-        global $CFG, $DB;
-
-        static $nothingtodo;
-        static $contentlist;
-
-        if (!empty($nothingtodo)) {   // We've been here in this page already
-            return $text;
-        }
-
-        // if we don't have a courseid, we can't run the query, so
-        if (empty($courseid)) {
-            return $text;
-        }
-
-        // Create a list of all the resources to search for. It may be cached already.
-        if (empty($contentlist)) {
-            // We look for text field contents only, and only if the field has
-            // autolink enabled (param1).
-            $sql = 'SELECT dc.id AS contentid, ' .
-                   'dr.id AS recordid, ' .
-                   'dc.content AS content, ' .
-                   'd.id AS dataid ' .
-                        'FROM {data} d, ' .
-                             '{data_fields} df, ' .
-                             '{data_records} dr, ' .
-                             '{data_content} dc ' .
-                            "WHERE (d.course = ? or d.course = '".SITEID."')" .
-                            'AND d.id = df.dataid ' .
-                            'AND df.id = dc.fieldid ' .
-                            'AND d.id = dr.dataid ' .
-                            'AND dr.id = dc.recordid ' .
-                            "AND df.type = 'text' " .
-                            "AND " . $DB->sql_compare_text('df.param1', 1) . " = '1'";
-
-            if (!$datacontents = $DB->get_records_sql($sql, array($courseid))) {
-                return $text;
-            }
-
-            $contentlist = array();
-
-            foreach ($datacontents as $datacontent) {
-                $currentcontent = trim($datacontent->content);
-                $strippedcontent = strip_tags($currentcontent);
-
-                if (!empty($strippedcontent)) {
-                    $contentlist[] = new filterobject(
-                                            $currentcontent,
-                                            '<a class="data autolink" title="'.
-                                            $strippedcontent.'" href="'.
-                                            $CFG->wwwroot.'/mod/data/view.php?d='. $datacontent->dataid .
-                                            '&amp;rid='. $datacontent->recordid .'">',
-                                            '</a>', false, true);
-                }
-            } // End foreach
-        }
-        return  filter_phrases($text, $contentlist);  // Look for all these links in the text
-    }
-
-
index deba450..241c818 100644 (file)
@@ -1,12 +1,30 @@
 <?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
-////////////////////////////////////////////////////////////////////////////////
-//  Code fragment to define the module version etc.
-//  This fragment is called by /admin/index.php
-////////////////////////////////////////////////////////////////////////////////
-
-$module->version  = 2011052300;
-$module->requires = 2011052300;  // Requires this Moodle version
-$module->cron     = 60;
+/**
+ * Data module version information
+ *
+ * @package    mod
+ * @subpackage data
+ * @copyright  2005 onwards Martin Dougiamas {@link http://moodle.com}
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
 
+defined('MOODLE_INTERNAL') || die();
 
+$module->version  = 2011102800;
+$module->requires = 2011102700;  // Requires this Moodle version
+$module->cron     = 60;          // Period for cron to check this module (secs)