lib/amd/src/form-course-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. * Course selector adaptor for auto-complete form element.
  17. *
  18. * @module core/form-course-selector
  19. * @copyright 2016 Damyon Wiese <damyon@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @since 3.1
  22. */
  23. define(['core/ajax', 'jquery'], function(ajax, $) {
  24. return {
  25. // Public variables and functions.
  26. processResults: function(selector, data) {
  27. // Mangle the results into an array of objects.
  28. var results = [];
  29. var i = 0;
  30. var excludelist = String($(selector).data('exclude')).split(',');
  31. for (i = 0; i < data.courses.length; i++) {
  32. if (excludelist.indexOf(String(data.courses[i].id)) === -1) {
  33. results.push({value: data.courses[i].id, label: data.courses[i].displayname});
  34. }
  35. }
  36. return results;
  37. },
  38. transport: function(selector, query, success, failure) {
  39. var el = $(selector);
  40. // Parse some data-attributes from the form element.
  41. var requiredcapabilities = el.data('requiredcapabilities');
  42. if (requiredcapabilities.trim() !== "") {
  43. requiredcapabilities = requiredcapabilities.split(',');
  44. } else {
  45. requiredcapabilities = [];
  46. }
  47. var limittoenrolled = el.data('limittoenrolled');
  48. var includefrontpage = el.data('includefrontpage');
  49. var onlywithcompletion = el.data('onlywithcompletion');
  50. // Build the query.
  51. var promises = null;
  52. if (typeof query === "undefined") {
  53. query = '';
  54. }
  55. var searchargs = {
  56. criterianame: 'search',
  57. criteriavalue: query,
  58. page: 0,
  59. perpage: 100,
  60. requiredcapabilities: requiredcapabilities,
  61. limittoenrolled: limittoenrolled,
  62. onlywithcompletion: onlywithcompletion
  63. };
  64. var calls = [{
  65. methodname: 'core_course_search_courses', args: searchargs
  66. }];
  67. if (includefrontpage) {
  68. calls.push({
  69. methodname: 'core_course_get_courses',
  70. args: {
  71. options: {
  72. ids: [includefrontpage]
  73. }
  74. }
  75. });
  76. }
  77. // Go go go!
  78. promises = ajax.call(calls);
  79. $.when.apply($.when, promises).done(function(data, site) {
  80. if (site && site.length == 1) {
  81. var frontpage = site.pop();
  82. var matches = query === ''
  83. || frontpage.fullname.toUpperCase().indexOf(query.toUpperCase()) > -1
  84. || frontpage.shortname.toUpperCase().indexOf(query.toUpperCase()) > -1;
  85. if (matches) {
  86. data.courses.splice(0, 0, frontpage);
  87. }
  88. }
  89. success(data);
  90. }).fail(failure);
  91. }
  92. };
  93. });