Skip to content

Commit

Permalink
MBS-9448: redirect for resetting filters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Wagner committed Nov 20, 2024
1 parent d40757e commit 28db252
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
48 changes: 48 additions & 0 deletions classes/form/rights_config_filter_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,52 @@ public function definition() {
$elementarray[] = $mform->createElement('submit', 'resetfilter', get_string('resetfilter', 'local_ai_manager'));
$mform->addGroup($elementarray, 'elementarray', get_string('filterheading', 'local_ai_manager'), [' '], false);
}

/**
* Store filterids and rolefilterids in session.
*
* @param array $filterids
* @param array $rolefilterids
* @return void
*/
public function store_filter(array $filterids, array $rolefilterids) {
global $SESSION;

// Ensure attribute exists for following lines.
if (!isset($SESSION->local_ai_manager_filterids)) {
$SESSION->local_ai_manager_filterids = [];
}

if (!isset($SESSION->local_ai_manager_rolefilterids)) {
$SESSION->local_ai_manager_rolefilterids = [];
}

if ($SESSION->local_ai_manager_filterids !== $filterids) {
$SESSION->local_ai_manager_filterids = $filterids;
}
if ($SESSION->local_ai_manager_rolefilterids !== $rolefilterids) {
$SESSION->local_ai_manager_rolefilterids = $rolefilterids;
}
}

/**
* Get filters from session.
*
* @return array
*/
public function get_stored_filters() : array {
global $SESSION;

// Ensure attribute exists for following lines.
if (!isset($SESSION->local_ai_manager_filterids)) {
$SESSION->local_ai_manager_filterids = [];
}

if (!isset($SESSION->local_ai_manager_rolefilterids)) {
$SESSION->local_ai_manager_rolefilterids = [];
}

return [$SESSION->local_ai_manager_filterids, $SESSION->local_ai_manager_rolefilterids];
}

}
35 changes: 18 additions & 17 deletions rights_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@

redirect($PAGE->url, get_string('userstatusupdated', 'local_ai_manager'));
} else {
echo $OUTPUT->header();
$tenantnavbar = new tenantnavbar('rights_config.php');
echo $OUTPUT->render($tenantnavbar);

echo $OUTPUT->heading(get_string('rightsconfig', 'local_ai_manager'), 2, 'text-center');

// Render and handle external filter provided through a hook.
$usertablefilter = new \local_ai_manager\hook\usertable_filter($tenant);
Expand All @@ -79,11 +74,12 @@
$filterform =
new \local_ai_manager\form\rights_config_filter_form(null,
['filteroptions' => $usertablefilter->get_filter_options()]);
$filterform->set_data(['rolefilterids' => [1,2,3]]);

if (!empty($filterform->get_data())) {

if (!empty($filterform->get_data()->resetfilter)) {
$filterids = [];
$rolefilterids = [];
$filterform->store_filter([], []);
redirect($PAGE->url);
} else {
$filterids = !empty($filterform->get_data()->filterids) ? $filterform->get_data()->filterids : [];
$rolefilterids = !empty($filterform->get_data()->rolefilterids) ? $filterform->get_data()->rolefilterids : [];
Expand All @@ -93,20 +89,25 @@
// phpcs:enable moodle.Commenting.TodoComment.MissingInfoInline
} else {
// Filter form has not submitted, so use the filter ids stored in the session.
$filterids = !empty($SESSION->local_ai_manager_filterids) ? $SESSION->local_ai_manager_filterids : [];
$rolefilterids = !empty($SESSION->local_ai_manager_rolefilterids) ? $SESSION->local_ai_manager_rolefilterids : [];
[$filterids, $rolefilterids] = $filterform->get_stored_filters();
}

// Store filterdata in session.
$filterform->store_filter($filterids, $rolefilterids);

// Set default data (if not set already by request.
$filterform->set_data(['filterids' => $filterids, 'rolefilterids' => $rolefilterids]);

// Update session if necessary.
if ($SESSION->local_ai_manager_filterids !== $filterids) {
$SESSION->local_ai_manager_filterids = $filterids;
}
if ($SESSION->local_ai_manager_rolefilterids !== $rolefilterids) {
$SESSION->local_ai_manager_rolefilterids = $rolefilterids;
}
echo $OUTPUT->header();

$tenantnavbar = new tenantnavbar('rights_config.php');
echo $OUTPUT->render($tenantnavbar);
echo $OUTPUT->heading(get_string('rightsconfig', 'local_ai_manager'), 2, 'text-center');

// Render filter form.
$filterform->display();

// Render rights table.
$uniqid = 'rights-config-table-' . uniqid();
$rightstable =
new \local_ai_manager\local\rights_config_table($uniqid, $tenant, $PAGE->url, $filterids, $rolefilterids);
Expand Down

0 comments on commit 28db252

Please sign in to comment.