Merge branch 'MDL-80633-main' of https://github.com/laurentdavid/moodle
[moodle.git] / user / amd / src / participants_filter.js
blob6255c4723eef9f5c86e9ef9abbc4c0e08fc79e3d
1 // This file is part of Moodle - http://moodle.org/
2 //
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.
7 //
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/>.
16 /**
17  * Participants filter management.
18  *
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
22  */
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';
30 /**
31  * Initialise the participants filter on the element with the given id.
32  *
33  * @param {String} filterRegionId The id for the filter element.
34  */
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),
43             {
44                 jointype: parseInt(filterSet.querySelector(Selectors.filterset.fields.join).value, 10),
45                 filters,
46             }
47         )
48             .then(result => {
49                 pendingPromise.resolve();
51                 return result;
52             })
53             .catch(Notification.exception);
54     });
55     coreFilter.init();
57     /**
58      * Set the current filter options based on a provided configuration.
59      *
60      * @param {Object} config
61      * @param {Number} config.jointype
62      * @param {Object} config.filters
63      * @returns {Promise}
64      */
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();
71         }
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.
79                 return false;
80             }
82             const filterValues = filterData.values;
84             if (!filterValues.length) {
85                 // There are no values for this filter.
86                 // Skip it.
87                 return false;
88             }
89             return coreFilter.addFilterRow()
90                 .then(([filterRow]) => {
91                     coreFilter.addFilter(filterRow, filterType, filterValues);
92                     return;
93                 });
94         }).filter(promise => promise);
96         if (!filterPromises.length) {
97             return Promise.resolve();
98         }
100         return Promise.all(filterPromises)
101             .then(() => {
102                 return coreFilter.removeEmptyFilters();
103             })
104             .then(() => {
105                 coreFilter.updateFiltersOptions();
106                 return;
107             })
108             .then(() => {
109                 coreFilter.updateTableFromFilter();
110                 return;
111             });
112     };
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())
122             .catch();
123     }