[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/amd/src/ -> form-course-selector.js (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1