mod/assign/amd/src/participant_selector.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. * Custom auto-complete adapter to load users from the assignment list_participants webservice.
  17. *
  18. * @module mod_assign/participant_selector
  19. * @copyright 2015 Damyon Wiese <damyon@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define(['core/ajax', 'jquery', 'core/templates'], function(ajax, $, templates) {
  23. return /** @alias module:mod_assign/participants_selector */ {
  24. // Public variables and functions.
  25. /**
  26. * Process the results returned from transport (convert to value + label)
  27. *
  28. * @method processResults
  29. * @param {String} selector
  30. * @param {Array} data
  31. * @return {Array}
  32. */
  33. processResults: function(selector, data) {
  34. return data;
  35. },
  36. /**
  37. * Fetch results based on the current query. This also renders each result from a template before returning them.
  38. *
  39. * @method transport
  40. * @param {String} selector Selector for the original select element
  41. * @param {String} query Current search string
  42. * @param {Function} success Success handler
  43. * @param {Function} failure Failure handler
  44. */
  45. transport: function(selector, query, success, failure) {
  46. var assignmentid = $(selector).attr('data-assignmentid');
  47. var groupid = $(selector).attr('data-groupid');
  48. var filters = $('[data-region="configure-filters"] input[type="checkbox"]');
  49. var filterstrings = [];
  50. filters.each(function(index, element) {
  51. filterstrings[$(element).attr('name')] = $(element).prop('checked');
  52. });
  53. ajax.call([{
  54. methodname: 'mod_assign_list_participants',
  55. args: {
  56. assignid: assignmentid,
  57. groupid: groupid,
  58. filter: query,
  59. limit: 30,
  60. includeenrolments: false,
  61. tablesort: true
  62. }
  63. }])[0].then(function(results) {
  64. var promises = [];
  65. var identityfields = $('[data-showuseridentity]').data('showuseridentity').split(',');
  66. // We got the results, now we loop over them and render each one from a template.
  67. $.each(results, function(index, user) {
  68. var ctx = user,
  69. identity = [],
  70. show = true;
  71. if (filterstrings.filter_submitted && !user.submitted) {
  72. show = false;
  73. }
  74. if (filterstrings.filter_notsubmitted && user.submitted) {
  75. show = false;
  76. }
  77. if (filterstrings.filter_requiregrading && !user.requiregrading) {
  78. show = false;
  79. }
  80. if (filterstrings.filter_grantedextension && !user.grantedextension) {
  81. show = false;
  82. }
  83. if (show) {
  84. $.each(identityfields, function(i, k) {
  85. if (typeof user[k] !== 'undefined' && user[k] !== '') {
  86. ctx.hasidentity = true;
  87. identity.push(user[k]);
  88. }
  89. });
  90. ctx.identity = identity.join(', ');
  91. promises.push(templates.render('mod_assign/list_participant_user_summary', ctx).then(function(html) {
  92. return {value: user.id, label: html};
  93. }));
  94. }
  95. });
  96. // Do the dance for $.when()
  97. return $.when.apply($, promises);
  98. }).then(function() {
  99. var users = [];
  100. // Determine if we've been passed any arguments..
  101. if (arguments[0]) {
  102. // Undo the $.when() dance from arguments object into an array..
  103. users = Array.prototype.slice.call(arguments);
  104. }
  105. success(users);
  106. return;
  107. }).catch(failure);
  108. }
  109. };
  110. });