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 * Participants filter management.
19 * @module core_user/participants_filter
20 * @copyright 2021 Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 import CoreFilter from 'core/datafilter';
25 import * as DynamicTable from 'core_table/dynamic';
26 import Selectors from 'core/datafilter/selectors';
27 import Notification from 'core/notification';
28 import Pending from 'core/pending';
31 * Initialise the participants filter on the element with the given id.
33 * @param {String} filterRegionId The id for the filter element.
35 export const init = filterRegionId => {
37 const filterSet = document.getElementById(filterRegionId);
39 // Create and initialize filter.
40 const coreFilter = new CoreFilter(filterSet, function(filters, pendingPromise) {
41 DynamicTable.setFilters(
42 DynamicTable.getTableFromId(filterSet.dataset.tableRegion),
44 jointype: parseInt(filterSet.querySelector(Selectors.filterset.fields.join).value, 10),
49 pendingPromise.resolve();
53 .catch(Notification.exception);
58 * Set the current filter options based on a provided configuration.
60 * @param {Object} config
61 * @param {Number} config.jointype
62 * @param {Object} config.filters
65 const setFilterFromConfig = config => {
66 const filterConfig = Object.entries(config.filters);
68 if (!filterConfig.length) {
69 // There are no filters to set from.
70 return Promise.resolve();
73 // Set the main join type.
74 filterSet.querySelector(Selectors.filterset.fields.join).value = config.jointype;
76 const filterPromises = filterConfig.map(([filterType, filterData]) => {
77 if (filterType === 'courseid') {
78 // The courseid is a special case.
82 const filterValues = filterData.values;
84 if (!filterValues.length) {
85 // There are no values for this filter.
89 return coreFilter.addFilterRow()
90 .then(([filterRow]) => {
91 coreFilter.addFilter(filterRow, filterType, filterValues);
94 }).filter(promise => promise);
96 if (!filterPromises.length) {
97 return Promise.resolve();
100 return Promise.all(filterPromises)
102 return coreFilter.removeEmptyFilters();
105 coreFilter.updateFiltersOptions();
109 coreFilter.updateTableFromFilter();
114 // Initialize DynamicTable for showing result.
115 const tableRoot = DynamicTable.getTableFromId(filterSet.dataset.tableRegion);
116 const initialFilters = DynamicTable.getFilters(tableRoot);
117 if (initialFilters) {
118 const initialFilterPromise = new Pending('core/filter:setFilterFromConfig');
119 // Apply the initial filter configuration.
120 setFilterFromConfig(initialFilters)
121 .then(() => initialFilterPromise.resolve())