MDL-53380 tool_lp: Framework auto complete element

This commit is contained in:
Frederic Massart 2016-03-08 14:30:00 +08:00
parent 60a84422cf
commit 6e20a8de26
3 changed files with 217 additions and 0 deletions

View File

@ -0,0 +1 @@
define(["jquery","core/ajax","core/notification"],function(a,b,c){return{list:function(d,e){var f,g={context:{contextid:d}};return a.extend(g,"undefined"==typeof e?{}:e),f=b.call([{methodname:"tool_lp_list_competency_frameworks",args:g}])[0],f.fail(c.exception)},processResults:function(b,c){var d=[];return a.each(c,function(a,b){d.push({value:b.id,label:b.shortname+" "+b.idnumber})}),d},transport:function(b,c,d){var e=a(b),f=e.data("contextid"),g=e.data("onlyvisible");if(!f)throw new Error("The attribute data-contextid is required on "+b);this.list(f,{query:c,onlyvisible:g}).then(d)}}});

View File

@ -0,0 +1,96 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Frameworks datasource.
*
* This module is compatible with core/form-autocomplete.
*
* @package tool_lpmigrate
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery', 'core/ajax', 'core/notification'], function($, Ajax, Notification) {
return /** @alias module:tool_lpmigrate/frameworks_datasource */ {
/**
* List frameworks.
*
* @param {Number} contextId The context ID.
* @param {Object} options Additional parameters to pass to the external function.
* @return {Promise}
*/
list: function(contextId, options) {
var promise,
args = {
context: {
contextid: contextId
}
};
$.extend(args, typeof options === 'undefined' ? {} : options);
promise = Ajax.call([{
methodname: 'tool_lp_list_competency_frameworks',
args: args
}])[0];
return promise.fail(Notification.exception);
},
/**
* Process the results for auto complete elements.
*
* @param {String} selector The selector of the auto complete element.
* @param {Array} results An array or results.
* @return {Array} New array of results.
*/
processResults: function(selector, results) {
var options = [];
$.each(results, function(index, data) {
options.push({
value: data.id,
label: data.shortname + ' ' + data.idnumber
});
});
return options;
},
/**
* Source of data for Ajax element.
*
* @param {String} selector The selector of the auto complete element.
* @param {String} query The query string.
* @param {Function} callback A callback function receiving an array of results.
* @return {Void}
*/
transport: function(selector, query, callback) {
var el = $(selector),
contextId = el.data('contextid'),
onlyVisible = el.data('onlyvisible');
if (!contextId) {
throw new Error('The attribute data-contextid is required on ' + selector);
}
this.list(contextId, {
query: query,
onlyvisible: onlyVisible,
}).then(callback);
}
};
});

View File

@ -0,0 +1,120 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Framework selector field.
*
* @package tool_lp
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lp\form;
use coding_exception;
use MoodleQuickForm_autocomplete;
use \tool_lp\competency_framework;
global $CFG;
require_once($CFG->libdir . '/form/autocomplete.php');
/**
* Form field type for choosing a framework.
*
* @package tool_lp
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class framework_autocomplete extends MoodleQuickForm_autocomplete {
/** @var bool Only visible frameworks? */
protected $onlyvisible = false;
/**
* Constructor.
*
* @param string $elementName Element name
* @param mixed $elementLabel Label(s) for an element
* @param array $options Options to control the element's display
* Valid options are:
* - context context The context.
* - contextid int The context id.
* - multiple bool Whether or not the field accepts more than one values.
* - onlyvisible bool Whether or not only visible framework can be listed.
*/
public function __construct($elementName = null, $elementLabel = null, $options = array()) {
$contextid = null;
if (!empty($options['contextid'])) {
$contextid = $options['contextid'];
} else if (!empty($options['context'])) {
$contextid = $options['context']->id;
}
$this->onlyvisible = !empty($options['onlyvisible']);
$validattributes = array(
'ajax' => 'tool_lp/frameworks_datasource',
'data-contextid' => $contextid,
'data-onlyvisible' => $this->onlyvisible ? '1' : '0',
);
if (!empty($options['multiple'])) {
$validattributes['multiple'] = 'multiple';
}
parent::__construct($elementName, $elementLabel, array(), $validattributes);
}
/**
* Set the value of this element.
*
* @param mixed string|array $value The value to set.
* @return boolean
*/
public function setValue($value) {
global $DB;
$values = (array) $value;
$ids = array();
foreach ($values as $onevalue) {
if ((!$this->optionExists($onevalue)) &&
($onevalue !== '_qf__force_multiselect_submission')) {
array_push($ids, $onevalue);
}
}
if (empty($ids)) {
return $this->setSelected(array());
}
// Logic here is simulating API.
$toselect = array();
list($insql, $inparams) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED, 'param');
$frameworks = competency_framework::get_records_select("id $insql", $inparams, 'shortname');
foreach ($frameworks as $framework) {
if (!has_any_capability(array('tool/lp:competencyread', 'tool/lp:competencymanage'), $framework->get_context())) {
continue;
} else if ($this->onlyvisible && !$framework->get_visible()) {
continue;
}
$this->addOption($framework->get_shortname() . ' ' . $framework->get_idnumber(), $framework->get_id());
array_push($toselect, $framework->get_id());
}
return $this->setSelected($toselect);
}
}