MDL-68169 user: Add keyword filter
authorAndrew Nicols <andrew@nicols.co.uk>
Fri, 3 Apr 2020 12:41:43 +0000 (20:41 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Wed, 27 May 2020 02:49:44 +0000 (10:49 +0800)
Part of MDL-67743

lang/en/user.php
user/amd/build/local/participantsfilter/filter.min.js
user/amd/build/local/participantsfilter/filter.min.js.map
user/amd/build/local/participantsfilter/filtertypes/keyword.min.js [new file with mode: 0644]
user/amd/build/local/participantsfilter/filtertypes/keyword.min.js.map [new file with mode: 0644]
user/amd/src/local/participantsfilter/filter.js
user/amd/src/local/participantsfilter/filtertypes/keyword.js [new file with mode: 0644]
user/classes/output/participants_filter.php

index 0c768b1..1f832af 100644 (file)
@@ -28,6 +28,8 @@ $string['clearfilterrow'] = 'Remove filter row';
 $string['clearfilters'] = 'Clear filters';
 $string['countparticipantsfound'] = '{$a} participants found';
 $string['match'] = 'Match';
+$string['placeholdertypeorselect'] = 'Type or select...';
+$string['placeholdertype'] = 'Type...';
 $string['privacy:courserequestpath'] = 'Requested courses';
 $string['privacy:descriptionpath'] = 'Profile description';
 $string['privacy:devicespath'] = 'User devices';
@@ -131,8 +133,8 @@ $string['privacy:passwordresetpath'] = 'Password resets';
 $string['privacy:profileimagespath'] = 'Profile images';
 $string['privacy:privatefilespath'] = 'Private files';
 $string['privacy:sessionpath'] = 'Session data';
+$string['filterbykeyword'] = 'Keyword';
 $string['selectfiltertype'] = 'Select';
 $string['target:upcomingactivitiesdue'] = 'Upcoming activities due';
 $string['target:upcomingactivitiesdue_help'] = 'This target generates reminders for upcoming activities due.';
 $string['target:upcomingactivitiesdueinfo'] = 'All upcoming activities due insights are listed here. These students have received these insights directly.';
-$string['typeorselect'] = 'Type or select...';
index 740ef17..3ef1d57 100644 (file)
Binary files a/user/amd/build/local/participantsfilter/filter.min.js and b/user/amd/build/local/participantsfilter/filter.min.js differ
index 5670dd2..e694bbf 100644 (file)
Binary files a/user/amd/build/local/participantsfilter/filter.min.js.map and b/user/amd/build/local/participantsfilter/filter.min.js.map differ
diff --git a/user/amd/build/local/participantsfilter/filtertypes/keyword.min.js b/user/amd/build/local/participantsfilter/filtertypes/keyword.min.js
new file mode 100644 (file)
index 0000000..4e178f8
Binary files /dev/null and b/user/amd/build/local/participantsfilter/filtertypes/keyword.min.js differ
diff --git a/user/amd/build/local/participantsfilter/filtertypes/keyword.min.js.map b/user/amd/build/local/participantsfilter/filtertypes/keyword.min.js.map
new file mode 100644 (file)
index 0000000..0c000b7
Binary files /dev/null and b/user/amd/build/local/participantsfilter/filtertypes/keyword.min.js.map differ
index b3180ad..7895ca0 100644 (file)
@@ -59,6 +59,24 @@ export default class {
         // eslint-disable-line no-empty-function
     }
 
+    /**
+     * Get the placeholder to use when showing the value selector.
+     *
+     * @return {Promise} Resolving to a String
+     */
+    get placeholder() {
+        return getString('placeholdertypeorselect', 'core_user');
+    }
+
+    /**
+     * Whether to show suggestions in the autocomplete.
+     *
+     * @return {Boolean}
+     */
+    get showSuggestions() {
+        return true;
+    }
+
     /**
      * Add the value selector to the filter row.
      */
@@ -81,13 +99,13 @@ export default class {
             null,
 
             // The string to use as a placeholder.
-            await getString('typeorselect', 'core_user'),
+            await this.placeholder,
 
             // Disable case sensitivity on searches.
             false,
 
             // Show suggestions.
-            true,
+            this.showSuggestions,
 
             // Do not override the 'no suggestions' string.
             null,
diff --git a/user/amd/src/local/participantsfilter/filtertypes/keyword.js b/user/amd/src/local/participantsfilter/filtertypes/keyword.js
new file mode 100644 (file)
index 0000000..c7b7872
--- /dev/null
@@ -0,0 +1,58 @@
+// 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/>.
+
+/**
+ * Keyword filter.
+ *
+ * @module     core_user/local/participantsfilter/filtertypes/keyword
+ * @package    core_user
+ * @copyright  2020 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+import Filter from '../filter';
+import {get_string as getString} from 'core/str';
+
+export default class extends Filter {
+    constructor(filterType, filterSet) {
+        super(filterType, filterSet);
+    }
+
+    /**
+     * For keywords the final value is an Array of strings.
+     *
+     * @returns {Object}
+     */
+    get values() {
+        return this.rawValues;
+    }
+
+    /**
+     * Get the placeholder to use when showing the value selector.
+     *
+     * @return {Promise} Resolving to a String
+     */
+    get placeholder() {
+        return getString('placeholdertype', 'core_user');
+    }
+
+    /**
+     * Whether to show suggestions in the autocomplete.
+     *
+     * @return {Boolean}
+     */
+    get showSuggestions() {
+        return false;
+    }
+}
index c88ec04..c98fccb 100644 (file)
@@ -67,6 +67,8 @@ class participants_filter implements renderable, templatable {
     protected function get_filtertypes(): array {
         $filtertypes = [];
 
+        $filtertypes[] = $this->get_keyword_filter();
+
         if ($filtertype = $this->get_enrolmentstatus_filter()) {
             $filtertypes[] = $filtertype;
         }
@@ -319,6 +321,23 @@ class participants_filter implements renderable, templatable {
         );
     }
 
+    /**
+     * Get data for the keywords filter.
+     *
+     * @return stdClass|null
+     */
+    protected function get_keyword_filter(): ?stdClass {
+        return $this->get_filter_object(
+            'keywords',
+            get_string('filterbykeyword', 'core_user'),
+            true,
+            true,
+            'core_user/local/participantsfilter/filtertypes/keyword',
+            [],
+            true
+        );
+    }
+
     /**
      * Export the renderer data in a mustache template friendly format.
      *
@@ -344,6 +363,7 @@ class participants_filter implements renderable, templatable {
      * @param bool $multiple
      * @param string|null $filterclass
      * @param array $values
+     * @param bool $allowempty
      * @return stdClass|null
      */
     protected function get_filter_object(
@@ -352,9 +372,11 @@ class participants_filter implements renderable, templatable {
         bool $custom,
         bool $multiple,
         ?string $filterclass,
-        array $values
+        array $values,
+        bool $allowempty = false
     ): ?stdClass {
-        if (empty($values)) {
+
+        if (!$allowempty && empty($values)) {
             // Do not show empty filters.
             return null;
         }