MDL-83641 cohort: allow 'includes' option be specified in form element.

This commit is contained in:
Paul Holden 2024-11-27 22:01:21 +01:00
parent a8b2acd35c
commit a11e396945
No known key found for this signature in database
GPG Key ID: A81A96D6045F6164
6 changed files with 23 additions and 6 deletions

View File

@ -0,0 +1,8 @@
issueNumber: MDL-83641
notes:
core_form:
- message: >-
The `cohort` form element now accepts new `includes` option, which is
passed to the corresponding external service to determine which cohorts
to return (self, parents, all)
type: changed

View File

@ -63,11 +63,9 @@ class assign_role_cohort extends moodleform {
$mform->addElement('select', 'roleid', get_string('selectrole', 'tool_cohortroles'), $options);
$mform->addRule('roleid', null, 'required');
$context = context_system::instance();
$options = array(
'multiple' => true,
'data-contextid' => $context->id,
'data-includes' => 'all'
'includes' => 'all',
);
$mform->addElement('cohort', 'cohortids', get_string('selectcohorts', 'tool_cohortroles'), $options);
$mform->addRule('cohortids', null, 'required');

View File

@ -6,6 +6,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 3.1
*/
define("core/form-cohort-selector",["core/ajax","jquery"],(function(ajax,$){return{processResults:function(selector,data){var results=[],i=0,excludelist=String($(selector).data("exclude")).split(",");for(i=0;i<data.cohorts.length;i++)-1===excludelist.indexOf(String(data.cohorts[i].id))&&results.push({value:data.cohorts[i].id,label:data.cohorts[i].name});return results},transport:function(selector,query,success,failure){var promises;void 0===query&&(query="");var calls=[{methodname:"core_cohort_search_cohorts",args:{query:query,includes:"parents",limitfrom:0,limitnum:100,context:{contextid:$(selector).data("contextid")}}}];promises=ajax.call(calls),$.when.apply($.when,promises).done((function(data){success(data)})).fail(failure)}}}));
define("core/form-cohort-selector",["core/ajax","jquery"],(function(ajax,$){return{processResults:function(selector,data){var results=[],i=0,excludelist=String($(selector).data("exclude")).split(",");for(i=0;i<data.cohorts.length;i++)-1===excludelist.indexOf(String(data.cohorts[i].id))&&results.push({value:data.cohorts[i].id,label:data.cohorts[i].name});return results},transport:function(selector,query,success,failure){var promises,el=$(selector);void 0===query&&(query="");var calls=[{methodname:"core_cohort_search_cohorts",args:{query:query,includes:el.data("includes"),limitfrom:0,limitnum:100,context:{contextid:el.data("contextid")}}}];promises=ajax.call(calls),$.when.apply($.when,promises).done((function(data){success(data)})).fail(failure)}}}));
//# sourceMappingURL=form-cohort-selector.min.js.map

View File

@ -1 +1 @@
{"version":3,"file":"form-cohort-selector.min.js","sources":["../src/form-cohort-selector.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Course selector adaptor for auto-complete form element.\n *\n * @module core/form-cohort-selector\n * @copyright 2016 Damyon Wiese <damyon@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * @since 3.1\n */\ndefine(['core/ajax', 'jquery'], function(ajax, $) {\n\n return {\n // Public variables and functions.\n processResults: function(selector, data) {\n // Mangle the results into an array of objects.\n var results = [];\n var i = 0;\n var excludelist = String($(selector).data('exclude')).split(',');\n\n for (i = 0; i < data.cohorts.length; i++) {\n if (excludelist.indexOf(String(data.cohorts[i].id)) === -1) {\n results.push({value: data.cohorts[i].id, label: data.cohorts[i].name});\n }\n }\n return results;\n },\n\n transport: function(selector, query, success, failure) {\n var el = $(selector);\n\n // Parse some data-attributes from the form element.\n\n // Build the query.\n var promises = null;\n\n if (typeof query === \"undefined\") {\n query = '';\n }\n var contextid = el.data('contextid');\n\n var searchargs = {\n query: query,\n includes: 'parents',\n limitfrom: 0,\n limitnum: 100,\n context: {contextid: contextid}\n };\n\n var calls = [{\n methodname: 'core_cohort_search_cohorts', args: searchargs\n }];\n\n // Go go go!\n promises = ajax.call(calls);\n $.when.apply($.when, promises).done(function(data) {\n success(data);\n }).fail(failure);\n }\n };\n});\n"],"names":["define","ajax","$","processResults","selector","data","results","i","excludelist","String","split","cohorts","length","indexOf","id","push","value","label","name","transport","query","success","failure","promises","calls","methodname","args","includes","limitfrom","limitnum","context","contextid","call","when","apply","done","fail"],"mappings":";;;;;;;;AAuBAA,mCAAO,CAAC,YAAa,WAAW,SAASC,KAAMC,SAEpC,CAEHC,eAAgB,SAASC,SAAUC,UAE3BC,QAAU,GACVC,EAAI,EACJC,YAAcC,OAAOP,EAAEE,UAAUC,KAAK,YAAYK,MAAM,SAEvDH,EAAI,EAAGA,EAAIF,KAAKM,QAAQC,OAAQL,KACwB,IAArDC,YAAYK,QAAQJ,OAAOJ,KAAKM,QAAQJ,GAAGO,MAC3CR,QAAQS,KAAK,CAACC,MAAOX,KAAKM,QAAQJ,GAAGO,GAAIG,MAAOZ,KAAKM,QAAQJ,GAAGW,cAGjEZ,SAGXa,UAAW,SAASf,SAAUgB,MAAOC,QAASC,aAMtCC,cAEiB,IAAVH,QACPA,MAAQ,QAYRI,MAAQ,CAAC,CACTC,WAAY,6BAA8BC,KAT7B,CACbN,MAAOA,MACPO,SAAU,UACVC,UAAW,EACXC,SAAU,IACVC,QAAS,CAACC,UAjBL7B,EAAEE,UAUQC,KAAK,iBAexBkB,SAAWtB,KAAK+B,KAAKR,OACrBtB,EAAE+B,KAAKC,MAAMhC,EAAE+B,KAAMV,UAAUY,MAAK,SAAS9B,MACzCgB,QAAQhB,SACT+B,KAAKd"}
{"version":3,"file":"form-cohort-selector.min.js","sources":["../src/form-cohort-selector.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Course selector adaptor for auto-complete form element.\n *\n * @module core/form-cohort-selector\n * @copyright 2016 Damyon Wiese <damyon@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * @since 3.1\n */\ndefine(['core/ajax', 'jquery'], function(ajax, $) {\n\n return {\n // Public variables and functions.\n processResults: function(selector, data) {\n // Mangle the results into an array of objects.\n var results = [];\n var i = 0;\n var excludelist = String($(selector).data('exclude')).split(',');\n\n for (i = 0; i < data.cohorts.length; i++) {\n if (excludelist.indexOf(String(data.cohorts[i].id)) === -1) {\n results.push({value: data.cohorts[i].id, label: data.cohorts[i].name});\n }\n }\n return results;\n },\n\n transport: function(selector, query, success, failure) {\n var el = $(selector);\n\n // Parse some data-attributes from the form element.\n\n // Build the query.\n var promises = null;\n\n if (typeof query === \"undefined\") {\n query = '';\n }\n\n var includes = el.data('includes');\n var contextid = el.data('contextid');\n\n var searchargs = {\n query: query,\n includes: includes,\n limitfrom: 0,\n limitnum: 100,\n context: {contextid: contextid}\n };\n\n var calls = [{\n methodname: 'core_cohort_search_cohorts', args: searchargs\n }];\n\n // Go go go!\n promises = ajax.call(calls);\n $.when.apply($.when, promises).done(function(data) {\n success(data);\n }).fail(failure);\n }\n };\n});\n"],"names":["define","ajax","$","processResults","selector","data","results","i","excludelist","String","split","cohorts","length","indexOf","id","push","value","label","name","transport","query","success","failure","promises","el","calls","methodname","args","includes","limitfrom","limitnum","context","contextid","call","when","apply","done","fail"],"mappings":";;;;;;;;AAuBAA,mCAAO,CAAC,YAAa,WAAW,SAASC,KAAMC,SAEpC,CAEHC,eAAgB,SAASC,SAAUC,UAE3BC,QAAU,GACVC,EAAI,EACJC,YAAcC,OAAOP,EAAEE,UAAUC,KAAK,YAAYK,MAAM,SAEvDH,EAAI,EAAGA,EAAIF,KAAKM,QAAQC,OAAQL,KACwB,IAArDC,YAAYK,QAAQJ,OAAOJ,KAAKM,QAAQJ,GAAGO,MAC3CR,QAAQS,KAAK,CAACC,MAAOX,KAAKM,QAAQJ,GAAGO,GAAIG,MAAOZ,KAAKM,QAAQJ,GAAGW,cAGjEZ,SAGXa,UAAW,SAASf,SAAUgB,MAAOC,QAASC,aAMtCC,SALAC,GAAKtB,EAAEE,eAOU,IAAVgB,QACPA,MAAQ,QAcRK,MAAQ,CAAC,CACTC,WAAY,6BAA8BC,KAT7B,CACbP,MAAOA,MACPQ,SALWJ,GAAGnB,KAAK,YAMnBwB,UAAW,EACXC,SAAU,IACVC,QAAS,CAACC,UAPER,GAAGnB,KAAK,iBAexBkB,SAAWtB,KAAKgC,KAAKR,OACrBvB,EAAEgC,KAAKC,MAAMjC,EAAEgC,KAAMX,UAAUa,MAAK,SAAS/B,MACzCgB,QAAQhB,SACTgC,KAAKf"}

View File

@ -50,11 +50,13 @@ define(['core/ajax', 'jquery'], function(ajax, $) {
if (typeof query === "undefined") {
query = '';
}
var includes = el.data('includes');
var contextid = el.data('contextid');
var searchargs = {
query: query,
includes: 'parents',
includes: includes,
limitfrom: 0,
limitnum: 100,
context: {contextid: contextid}

View File

@ -45,6 +45,11 @@ class MoodleQuickForm_cohort extends MoodleQuickForm_autocomplete {
*/
protected $exclude = array();
/**
* @var string $includes One of self/parents/all
*/
protected $includes = 'parents';
/**
* @var int $contextid The context id to fetch cohorts in.
*/
@ -86,6 +91,9 @@ class MoodleQuickForm_cohort extends MoodleQuickForm_autocomplete {
$this->exclude = array($this->exclude);
}
}
if (isset($options['includes'])) {
$this->includes = $options['includes'];
}
if (isset($options['requiredcapabilities'])) {
$this->requiredcapabilities = $options['requiredcapabilities'];
}
@ -93,6 +101,7 @@ class MoodleQuickForm_cohort extends MoodleQuickForm_autocomplete {
$validattributes = array(
'ajax' => 'core/form-cohort-selector',
'data-exclude' => implode(',', $this->exclude),
'data-includes' => $this->includes,
'data-contextid' => (int)$this->contextid
);
if ($this->multiple) {