1 |
efrain |
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 |
* Badge select competency actions
|
|
|
18 |
*
|
|
|
19 |
* @module tool_lp/form_competency_element
|
|
|
20 |
* @copyright 2019 Damyon Wiese <damyon@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
define(['jquery', 'tool_lp/competencypicker', 'core/ajax', 'core/notification', 'core/templates'],
|
|
|
24 |
function($, Picker, Ajax, Notification, Templates) {
|
|
|
25 |
|
|
|
26 |
var pickerInstance = null;
|
|
|
27 |
|
|
|
28 |
var pageContextId = 1;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Re-render the list of selected competencies.
|
|
|
32 |
*
|
|
|
33 |
* @method renderCompetencies
|
|
|
34 |
* @return {boolean}
|
|
|
35 |
*/
|
|
|
36 |
var renderCompetencies = function() {
|
|
|
37 |
var currentCompetencies = $('[data-action="competencies"]').val();
|
|
|
38 |
var requests = [];
|
|
|
39 |
var i = 0;
|
|
|
40 |
|
|
|
41 |
if (currentCompetencies != '') {
|
|
|
42 |
currentCompetencies = currentCompetencies.split(',');
|
|
|
43 |
for (i = 0; i < currentCompetencies.length; i++) {
|
|
|
44 |
requests[requests.length] = {
|
|
|
45 |
methodname: 'core_competency_read_competency',
|
|
|
46 |
args: {id: currentCompetencies[i]}
|
|
|
47 |
};
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
$.when.apply($, Ajax.call(requests, false)).then(function() {
|
|
|
52 |
var i = 0,
|
|
|
53 |
competencies = [];
|
|
|
54 |
|
|
|
55 |
for (i = 0; i < arguments.length; i++) {
|
|
|
56 |
competencies[i] = arguments[i];
|
|
|
57 |
}
|
|
|
58 |
var context = {
|
|
|
59 |
competencies: competencies
|
|
|
60 |
};
|
|
|
61 |
|
|
|
62 |
return Templates.render('tool_lp/form_competency_list', context);
|
|
|
63 |
}).then(function(html, js) {
|
|
|
64 |
Templates.replaceNode($('[data-region="competencies"]'), html, js);
|
|
|
65 |
return true;
|
|
|
66 |
}).fail(Notification.exception);
|
|
|
67 |
|
|
|
68 |
return true;
|
|
|
69 |
};
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Deselect a competency
|
|
|
73 |
*
|
|
|
74 |
* @method unpickCompetenciesHandler
|
|
|
75 |
* @param {Event} e
|
|
|
76 |
* @return {boolean}
|
|
|
77 |
*/
|
|
|
78 |
var unpickCompetenciesHandler = function(e) {
|
|
|
79 |
var currentCompetencies = $('[data-action="competencies"]').val().split(','),
|
|
|
80 |
newCompetencies = [],
|
|
|
81 |
i,
|
|
|
82 |
toRemove = $(e.currentTarget).data('id');
|
|
|
83 |
|
|
|
84 |
for (i = 0; i < currentCompetencies.length; i++) {
|
|
|
85 |
if (currentCompetencies[i] != toRemove) {
|
|
|
86 |
newCompetencies[newCompetencies.length] = currentCompetencies[i];
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
$('[data-action="competencies"]').val(newCompetencies.join(','));
|
|
|
91 |
|
|
|
92 |
return renderCompetencies();
|
|
|
93 |
};
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Open a competencies popup to relate competencies.
|
|
|
97 |
*
|
|
|
98 |
* @method pickCompetenciesHandler
|
|
|
99 |
*/
|
|
|
100 |
var pickCompetenciesHandler = function() {
|
|
|
101 |
var currentCompetencies = $('[data-action="competencies"]').val().split(',');
|
|
|
102 |
|
|
|
103 |
if (!pickerInstance) {
|
|
|
104 |
pickerInstance = new Picker(pageContextId, false, 'parents', true);
|
|
|
105 |
pickerInstance.on('save', function(e, data) {
|
|
|
106 |
var before = $('[data-action="competencies"]').val();
|
|
|
107 |
var compIds = data.competencyIds;
|
|
|
108 |
if (before != '') {
|
|
|
109 |
compIds = compIds.concat(before.split(','));
|
|
|
110 |
}
|
|
|
111 |
var value = compIds.join(',');
|
|
|
112 |
|
|
|
113 |
$('[data-action="competencies"]').val(value);
|
|
|
114 |
|
|
|
115 |
return renderCompetencies();
|
|
|
116 |
});
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
pickerInstance.setDisallowedCompetencyIDs(currentCompetencies);
|
|
|
120 |
pickerInstance.display();
|
|
|
121 |
};
|
|
|
122 |
|
|
|
123 |
return /** @alias module:tool_lp/form_competency_element */ {
|
|
|
124 |
/**
|
|
|
125 |
* Listen for clicks on the competency picker and push the changes to the form element.
|
|
|
126 |
*
|
|
|
127 |
* @method init
|
|
|
128 |
* @param {Integer} contextId
|
|
|
129 |
*/
|
|
|
130 |
init: function(contextId) {
|
|
|
131 |
pageContextId = contextId;
|
|
|
132 |
renderCompetencies();
|
|
|
133 |
$('[data-action="select-competencies"]').on('click', pickCompetenciesHandler);
|
|
|
134 |
$('body').on('click', '[data-action="deselect-competency"]', unpickCompetenciesHandler);
|
|
|
135 |
}
|
|
|
136 |
};
|
|
|
137 |
});
|