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 * Custom auto-complete adapter to load users from the assignment list_participants webservice.
19 * @module mod_assign/participants_selector
20 * @copyright 2015 Damyon Wiese <damyon@moodle.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 define(['core/ajax', 'jquery', 'core/templates'], function(ajax, $, templates) {
26 return /** @alias module:mod_assign/participants_selector */ {
28 // Public variables and functions.
30 * Process the results returned from transport (convert to value + label)
32 * @method processResults
33 * @param {String} selector
37 processResults: function(selector, data) {
42 * Fetch results based on the current query. This also renders each result from a template before returning them.
45 * @param {String} selector Selector for the original select element
46 * @param {String} query Current search string
47 * @param {Function} success Success handler
48 * @param {Function} failure Failure handler
50 transport: function(selector, query, success, failure) {
51 var assignmentid = $(selector).attr('data-assignmentid');
52 var groupid = $(selector).attr('data-groupid');
53 var filters = $('[data-region="configure-filters"] input[type="checkbox"]');
54 var filterstrings = [];
56 filters.each(function(index, element) {
57 filterstrings[$(element).attr('name')] = $(element).prop('checked');
61 methodname: 'mod_assign_list_participants',
62 args: {assignid: assignmentid, groupid: groupid, filter: query, limit: 30, includeenrolments: false}
63 }])[0].then(function(results) {
65 var identityfields = $('[data-showuseridentity]').data('showuseridentity').split(',');
67 // We got the results, now we loop over them and render each one from a template.
68 $.each(results, function(index, user) {
73 if (filterstrings.filter_submitted && !user.submitted) {
76 if (filterstrings.filter_notsubmitted && user.submitted) {
79 if (filterstrings.filter_requiregrading && !user.requiregrading) {
83 $.each(identityfields, function(i, k) {
84 if (typeof user[k] !== 'undefined' && user[k] !== '') {
85 ctx.hasidentity = true;
86 identity.push(user[k]);
89 ctx.identity = identity.join(', ');
90 promises.push(templates.render('mod_assign/list_participant_user_summary', ctx).then(function(html) {
91 return {value: user.id, label: html};
95 // Do the dance for $.when()
96 return $.when.apply($, promises);
98 // Undo the $.when() dance from arguments object into an array..
99 var users = Array.prototype.slice.call(arguments);