grade/report/user/amd/src/user.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. * Allow the user to search for learners within the user report.
  17. *
  18. * @module gradereport_user/user
  19. * @copyright 2023 Mathew May <mathew.solutions>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import UserSearch from 'core_user/comboboxsearch/user';
  23. import {renderForPromise, replaceNodeContents} from 'core/templates';
  24. import * as Repository from 'core_grades/searchwidget/repository';
  25. export default class User extends UserSearch {
  26. /**
  27. * Construct the class.
  28. *
  29. * @param {string} baseUrl The base URL for the page.
  30. */
  31. constructor(baseUrl) {
  32. super();
  33. this.baseUrl = baseUrl;
  34. }
  35. static init(baseUrl) {
  36. return new User(baseUrl);
  37. }
  38. /**
  39. * Build the content then replace the node.
  40. */
  41. async renderDropdown() {
  42. const {html, js} = await renderForPromise('core_user/comboboxsearch/resultset', {
  43. users: this.getMatchedResults().slice(0, 5),
  44. hasresults: this.getMatchedResults().length > 0,
  45. instance: this.instance,
  46. matches: this.getDatasetSize(),
  47. searchterm: this.getSearchTerm(),
  48. selectall: this.selectAllResultsLink(),
  49. });
  50. replaceNodeContents(this.getHTMLElements().searchDropdown, html, js);
  51. // Remove aria-activedescendant when the available options change.
  52. this.searchInput.removeAttribute('aria-activedescendant');
  53. }
  54. /**
  55. * Build up the view all link.
  56. *
  57. * @returns {string|*}
  58. */
  59. selectAllResultsLink() {
  60. const url = new URL(this.baseUrl);
  61. url.searchParams.set('userid', 0);
  62. url.searchParams.set('searchvalue', this.getSearchTerm());
  63. return url.toString();
  64. }
  65. /**
  66. * Build up the link that is dedicated to a particular result.
  67. *
  68. * @param {Number} userID The ID of the user selected.
  69. * @returns {string|*}
  70. */
  71. selectOneLink(userID) {
  72. const url = new URL(this.baseUrl);
  73. url.searchParams.set('userid', userID);
  74. url.searchParams.set('searchvalue', this.getSearchTerm());
  75. return url.toString();
  76. }
  77. /**
  78. * Get the data we will be searching against in this component.
  79. *
  80. * @returns {Promise<*>}
  81. */
  82. fetchDataset() {
  83. // Small typing checks as sometimes groups don't exist therefore the element returns a empty string.
  84. const gts = typeof (this.groupID) === "string" && this.groupID === '' ? 0 : this.groupID;
  85. return Repository.userFetch(this.courseID, gts).then((r) => r.users);
  86. }
  87. }