user/amd/src/participants_filter.js

  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.
  12. //
  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/>.
  15. /**
  16. * Participants filter management.
  17. *
  18. * @module core_user/participants_filter
  19. * @copyright 2021 Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import CoreFilter from 'core/datafilter';
  23. import * as DynamicTable from 'core_table/dynamic';
  24. import Selectors from 'core/datafilter/selectors';
  25. import Notification from 'core/notification';
  26. import Pending from 'core/pending';
  27. /**
  28. * Initialise the participants filter on the element with the given id.
  29. *
  30. * @param {String} filterRegionId The id for the filter element.
  31. */
  32. export const init = filterRegionId => {
  33. const filterSet = document.getElementById(filterRegionId);
  34. // Create and initialize filter.
  35. const coreFilter = new CoreFilter(filterSet, function(filters, pendingPromise) {
  36. DynamicTable.setFilters(
  37. DynamicTable.getTableFromId(filterSet.dataset.tableRegion),
  38. {
  39. jointype: parseInt(filterSet.querySelector(Selectors.filterset.fields.join).value, 10),
  40. filters,
  41. }
  42. )
  43. .then(result => {
  44. pendingPromise.resolve();
  45. return result;
  46. })
  47. .catch(Notification.exception);
  48. });
  49. coreFilter.init();
  50. /**
  51. * Set the current filter options based on a provided configuration.
  52. *
  53. * @param {Object} config
  54. * @param {Number} config.jointype
  55. * @param {Object} config.filters
  56. * @returns {Promise}
  57. */
  58. const setFilterFromConfig = config => {
  59. const filterConfig = Object.entries(config.filters);
  60. if (!filterConfig.length) {
  61. // There are no filters to set from.
  62. return Promise.resolve();
  63. }
  64. // Set the main join type.
  65. filterSet.querySelector(Selectors.filterset.fields.join).value = config.jointype;
  66. const filterPromises = filterConfig.map(([filterType, filterData]) => {
  67. if (filterType === 'courseid') {
  68. // The courseid is a special case.
  69. return false;
  70. }
  71. const filterValues = filterData.values;
  72. if (!filterValues.length) {
  73. // There are no values for this filter.
  74. // Skip it.
  75. return false;
  76. }
  77. return coreFilter.addFilterRow()
  78. .then(([filterRow]) => {
  79. coreFilter.addFilter(filterRow, filterType, filterValues);
  80. return;
  81. });
  82. }).filter(promise => promise);
  83. if (!filterPromises.length) {
  84. return Promise.resolve();
  85. }
  86. return Promise.all(filterPromises)
  87. .then(() => {
  88. return coreFilter.removeEmptyFilters();
  89. })
  90. .then(() => {
  91. coreFilter.updateFiltersOptions();
  92. return;
  93. })
  94. .then(() => {
  95. coreFilter.updateTableFromFilter();
  96. return;
  97. });
  98. };
  99. // Initialize DynamicTable for showing result.
  100. const tableRoot = DynamicTable.getTableFromId(filterSet.dataset.tableRegion);
  101. const initialFilters = DynamicTable.getFilters(tableRoot);
  102. if (initialFilters) {
  103. const initialFilterPromise = new Pending('core/filter:setFilterFromConfig');
  104. // Apply the initial filter configuration.
  105. setFilterFromConfig(initialFilters)
  106. .then(() => initialFilterPromise.resolve())
  107. .catch();
  108. }
  109. };