MDL-42382 admin: Add replace filters button
authorNoel De Martin <noel@moodle.com>
Mon, 16 Nov 2020 12:52:07 +0000 (13:52 +0100)
committerNoel De Martin <noel@moodle.com>
Tue, 17 Nov 2020 09:21:40 +0000 (10:21 +0100)
Co-authored-by: Luca Bösch <luca.boesch@bfh.ch>
Co-authored-by: Andrei Bautu <abautu@gmail.com>
lang/en/filters.php
user/filters/lib.php
user/filters/user_filter_forms.php

index f178887..e7fec91 100644 (file)
@@ -80,6 +80,7 @@ $string['profilelabel'] = '{$a->label}: {$a->profile} {$a->operator} {$a->value}
 $string['profilelabelnovalue'] = '{$a->label}: {$a->profile} {$a->operator}';
 $string['removeall'] = 'Remove all filters';
 $string['removeselected'] = 'Remove selected';
+$string['replacefilters'] = 'Replace filters';
 $string['selectlabel'] = '{$a->label} {$a->operator} {$a->value}';
 $string['startswith'] = 'starts with';
 $string['tablenosave'] = 'Changes in table above are saved automatically.';
index a6fd73d..0bb6c89 100644 (file)
@@ -101,6 +101,12 @@ class user_filtering {
         // Fist the new filter form.
         $this->_addform = new user_add_filter_form($baseurl, array('fields' => $this->_fields, 'extraparams' => $extraparams));
         if ($adddata = $this->_addform->get_data()) {
+            // Clear previous filters.
+            if (!empty($adddata->replacefilters)) {
+                $SESSION->user_filtering = [];
+            }
+
+            // Add new filters.
             foreach ($this->_fields as $fname => $field) {
                 $data = $field->check_data($adddata);
                 if ($data === false) {
@@ -111,19 +117,16 @@ class user_filtering {
                 }
                 $SESSION->user_filtering[$fname][] = $data;
             }
-            // Clear the form.
-            $_POST = array();
-            $this->_addform = new user_add_filter_form($baseurl, array('fields' => $this->_fields, 'extraparams' => $extraparams));
         }
 
         // Now the active filters.
         $this->_activeform = new user_active_filter_form($baseurl, array('fields' => $this->_fields, 'extraparams' => $extraparams));
-        if ($adddata = $this->_activeform->get_data()) {
-            if (!empty($adddata->removeall)) {
+        if ($activedata = $this->_activeform->get_data()) {
+            if (!empty($activedata->removeall)) {
                 $SESSION->user_filtering = array();
 
-            } else if (!empty($adddata->removeselected) and !empty($adddata->filter)) {
-                foreach ($adddata->filter as $fname => $instances) {
+            } else if (!empty($activedata->removeselected) and !empty($activedata->filter)) {
+                foreach ($activedata->filter as $fname => $instances) {
                     foreach ($instances as $i => $val) {
                         if (empty($val)) {
                             continue;
@@ -135,11 +138,14 @@ class user_filtering {
                     }
                 }
             }
-            // Clear+reload the form.
-            $_POST = array();
-            $this->_activeform = new user_active_filter_form($baseurl, array('fields' => $this->_fields, 'extraparams' => $extraparams));
         }
-        // Now the active filters.
+
+        // Rebuild the forms if filters data was processed.
+        if ($adddata || $activedata) {
+            $_POST = []; // Reset submitted data.
+            $this->_addform = new user_add_filter_form($baseurl, ['fields' => $this->_fields, 'extraparams' => $extraparams]);
+            $this->_activeform = new user_active_filter_form($baseurl, ['fields' => $this->_fields, 'extraparams' => $extraparams]);
+        }
     }
 
     /**
index c084135..b72ca1c 100644 (file)
@@ -36,6 +36,8 @@ class user_add_filter_form extends moodleform {
      * Form definition.
      */
     public function definition() {
+        global $SESSION;
+
         $mform       =& $this->_form;
         $fields      = $this->_customdata['fields'];
         $extraparams = $this->_customdata['extraparams'];
@@ -54,8 +56,15 @@ class user_add_filter_form extends moodleform {
             }
         }
 
-        // Add button.
-        $mform->addElement('submit', 'addfilter', get_string('addfilter', 'filters'));
+        // Add buttons.
+        $replacefiltersbutton = $mform->createElement('submit', 'replacefilters', get_string('replacefilters', 'filters'));
+        $addfilterbutton = $mform->createElement('submit', 'addfilter', get_string('addfilter', 'filters'));
+        $buttons = array_filter([
+            empty($SESSION->user_filtering) ? null : $replacefiltersbutton,
+            $addfilterbutton,
+        ]);
+
+        $mform->addGroup($buttons);
     }
 }