[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/lp/amd/src/ -> frameworks_datasource.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   * Frameworks datasource.
  18   *
  19   * This module is compatible with core/form-autocomplete.
  20   *
  21   * @package    tool_lpmigrate
  22   * @copyright  2016 Frédéric Massart - FMCorz.net
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  define(['jquery', 'core/ajax', 'core/notification'], function($, Ajax, Notification) {
  27  
  28      return /** @alias module:tool_lpmigrate/frameworks_datasource */ {
  29  
  30          /**
  31           * List frameworks.
  32           *
  33           * @param {Number} contextId The context ID.
  34           * @param {Object} options Additional parameters to pass to the external function.
  35           * @return {Promise}
  36           */
  37          list: function(contextId, options) {
  38              var promise,
  39                  args = {
  40                      context: {
  41                          contextid: contextId
  42                      }
  43                  };
  44  
  45              $.extend(args, typeof options === 'undefined' ? {} : options);
  46              promise = Ajax.call([{
  47                  methodname: 'core_competency_list_competency_frameworks',
  48                  args: args
  49              }])[0];
  50  
  51              return promise.fail(Notification.exception);
  52          },
  53  
  54          /**
  55           * Process the results for auto complete elements.
  56           *
  57           * @param {String} selector The selector of the auto complete element.
  58           * @param {Array} results An array or results.
  59           * @return {Array} New array of results.
  60           */
  61          processResults: function(selector, results) {
  62              var options = [];
  63              $.each(results, function(index, data) {
  64                  options.push({
  65                      value: data.id,
  66                      label: data.shortname + ' ' + data.idnumber
  67                  });
  68              });
  69              return options;
  70          },
  71  
  72          /**
  73           * Source of data for Ajax element.
  74           *
  75           * @param {String} selector The selector of the auto complete element.
  76           * @param {String} query The query string.
  77           * @param {Function} callback A callback function receiving an array of results.
  78           */
  79          transport: function(selector, query, callback) {
  80              var el = $(selector),
  81                  contextId = el.data('contextid'),
  82                  onlyVisible = el.data('onlyvisible');
  83  
  84              if (!contextId) {
  85                  throw new Error('The attribute data-contextid is required on ' + selector);
  86              }
  87  
  88              this.list(contextId, {
  89                  query: query,
  90                  onlyvisible: onlyVisible,
  91              }).then(callback);
  92          }
  93      };
  94  
  95  });


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