$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.';
// 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) {
}
$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;
}
}
}
- // 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]);
+ }
}
/**
* Form definition.
*/
public function definition() {
+ global $SESSION;
+
$mform =& $this->_form;
$fields = $this->_customdata['fields'];
$extraparams = $this->_customdata['extraparams'];
}
}
- // 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);
}
}