admin/tool/lp/amd/src/form-user-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. * User selector module.
  17. *
  18. * @module tool_lp/form-user-selector
  19. * @copyright 2015 Frédéric Massart - FMCorz.net
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define(['jquery', 'core/ajax', 'core/templates'], function($, Ajax, Templates) {
  23. return /** @alias module:tool_lp/form-user-selector */ {
  24. processResults: function(selector, results) {
  25. var users = [];
  26. $.each(results, function(index, user) {
  27. users.push({
  28. value: user.id,
  29. label: user._label
  30. });
  31. });
  32. return users;
  33. },
  34. transport: function(selector, query, success, failure) {
  35. var promise;
  36. var capability = $(selector).data('capability');
  37. if (typeof capability === "undefined") {
  38. capability = '';
  39. }
  40. promise = Ajax.call([{
  41. methodname: 'tool_lp_search_users',
  42. args: {
  43. query: query,
  44. capability: capability
  45. }
  46. }]);
  47. promise[0].then(function(results) {
  48. var promises = [],
  49. i = 0;
  50. // Render the label.
  51. $.each(results.users, function(index, user) {
  52. var ctx = user,
  53. identity = [];
  54. $.each(['idnumber', 'email', 'phone1', 'phone2', 'department', 'institution'], function(i, k) {
  55. if (typeof user[k] !== 'undefined' && user[k] !== '') {
  56. ctx.hasidentity = true;
  57. identity.push(user[k]);
  58. }
  59. });
  60. ctx.identity = identity.join(', ');
  61. promises.push(Templates.render('tool_lp/form-user-selector-suggestion', ctx));
  62. });
  63. // Apply the label to the results.
  64. return $.when.apply($.when, promises).then(function() {
  65. var args = arguments;
  66. $.each(results.users, function(index, user) {
  67. user._label = args[i];
  68. i++;
  69. });
  70. success(results.users);
  71. return;
  72. });
  73. }).catch(failure);
  74. }
  75. };
  76. });