1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * Report builder filter management
19 * @module core_reportbuilder/filters
20 * @copyright 2021 Paul Holden <paulh@moodle.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 import {dispatchEvent} from 'core/event_dispatcher';
25 import {loadFragment} from 'core/fragment';
26 import Notification from 'core/notification';
27 import Pending from 'core/pending';
28 import {getString} from 'core/str';
29 import Templates from 'core/templates';
30 import {add as addToast} from 'core/toast';
31 import DynamicForm from 'core_form/dynamicform';
32 import * as reportEvents from 'core_reportbuilder/local/events';
33 import * as reportSelectors from 'core_reportbuilder/local/selectors';
34 import {resetFilters} from 'core_reportbuilder/local/repository/filters';
37 * Update filter button text to indicate applied filter count
39 * @param {Element} reportElement
40 * @param {Number} filterCount
42 const setFilterButtonCount = async(reportElement, filterCount) => {
43 const filterButtonLabel = reportElement.querySelector(reportSelectors.regions.filterButtonLabel);
45 if (filterCount > 0) {
46 filterButtonLabel.textContent = await getString('filtersappliedx', 'core_reportbuilder', filterCount);
48 filterButtonLabel.textContent = await getString('filters', 'moodle');
53 * Initialise module for given report
56 * @param {Number} reportId
57 * @param {Number} contextId
59 export const init = (reportId, contextId) => {
60 const reportElement = document.querySelector(reportSelectors.forReport(reportId));
61 const filterFormContainer = reportElement.querySelector(reportSelectors.regions.filtersForm);
63 // Ensure we only add our listeners once (can be called multiple times by mustache template).
64 if (filterFormContainer.dataset.initialized) {
67 filterFormContainer.dataset.initialized = true;
69 const filterForm = new DynamicForm(filterFormContainer, '\\core_reportbuilder\\form\\filter');
71 // Submit report filters.
72 filterForm.addEventListener(filterForm.events.FORM_SUBMITTED, event => {
73 event.preventDefault();
75 // After the form has been submitted, we should trigger report table reload.
76 dispatchEvent(reportEvents.tableReload, {}, reportElement);
77 setFilterButtonCount(reportElement, event.detail);
79 getString('filtersapplied', 'core_reportbuilder')
81 .catch(Notification.exception);
84 // Reset report filters.
85 filterForm.addEventListener(filterForm.events.NOSUBMIT_BUTTON_PRESSED, event => {
86 event.preventDefault();
88 const pendingPromise = new Pending('core_reportbuilder/filters:reset');
89 const reportParameters = reportElement.dataset.parameter;
91 resetFilters(reportId, reportParameters)
92 .then(() => getString('filtersreset', 'core_reportbuilder'))
94 .then(() => loadFragment('core_reportbuilder', 'filters_form', contextId, {
96 parameters: reportParameters,
99 Templates.replaceNodeContents(filterFormContainer, html, js);
101 dispatchEvent(reportEvents.tableReload, {}, reportElement);
102 setFilterButtonCount(reportElement, 0);
104 return pendingPromise.resolve();
106 .catch(Notification.exception);
109 // Modify "region-main" overflow for big filter forms.
110 document.querySelector('#region-main').style.overflowX = "visible";