MDL-42382 admin: Add replace filters button

Co-authored-by: Luca Bösch <[email protected]>
Co-authored-by: Andrei Bautu <[email protected]>
This commit is contained in:
Noel De Martin
2020-11-17 10:21:40 +01:00
co-authored by Luca Bösch Andrei Bautu
parent 6ef4e66f03
commit c74fa727db
3 changed files with 29 additions and 13 deletions
+1
View 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.';
+17 -11
View 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]);
}
}
/**
+11 -2
View 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);
}
}