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 |
* Handle selection changes and actions on the competency tree.
|
|
|
18 |
*
|
|
|
19 |
* @module tool_lp/competencyactions
|
|
|
20 |
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
define(['jquery',
|
|
|
24 |
'core/url',
|
|
|
25 |
'core/templates',
|
|
|
26 |
'core/notification',
|
|
|
27 |
'core/str',
|
|
|
28 |
'core/ajax',
|
|
|
29 |
'tool_lp/dragdrop-reorder',
|
|
|
30 |
'tool_lp/tree',
|
|
|
31 |
'tool_lp/dialogue',
|
|
|
32 |
'tool_lp/menubar',
|
|
|
33 |
'tool_lp/competencypicker',
|
|
|
34 |
'tool_lp/competency_outcomes',
|
|
|
35 |
'tool_lp/competencyruleconfig',
|
|
|
36 |
'core/pending',
|
|
|
37 |
],
|
|
|
38 |
function(
|
|
|
39 |
$, url, templates, notification, str, ajax, dragdrop, Ariatree, Dialogue, menubar, Picker, Outcomes, RuleConfig, Pending
|
|
|
40 |
) {
|
|
|
41 |
|
|
|
42 |
// Private variables and functions.
|
|
|
43 |
/** @var {Object} treeModel - This is an object representing the nodes in the tree. */
|
|
|
44 |
var treeModel = null;
|
|
|
45 |
/** @var {Node} moveSource - The start of a drag operation */
|
|
|
46 |
var moveSource = null;
|
|
|
47 |
/** @var {Node} moveTarget - The end of a drag operation */
|
|
|
48 |
var moveTarget = null;
|
|
|
49 |
/** @var {Number} pageContextId The page context ID. */
|
|
|
50 |
var pageContextId;
|
|
|
51 |
/** @var {Object} Picker instance. */
|
|
|
52 |
var pickerInstance;
|
|
|
53 |
/** @var {Object} Rule config instance. */
|
|
|
54 |
var ruleConfigInstance;
|
|
|
55 |
/** @var {Object} The competency we're picking a relation to. */
|
|
|
56 |
var relatedTarget;
|
|
|
57 |
/** @var {Object} Taxonomy constants indexed per level. */
|
|
|
58 |
var taxonomiesConstants;
|
|
|
59 |
/** @var {Array} The rules modules. Values are object containing type, namd and amd. */
|
|
|
60 |
var rulesModules;
|
|
|
61 |
/** @var {Number} the selected competency ID. */
|
|
|
62 |
var selectedCompetencyId = null;
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Respond to choosing the "Add" menu item for the selected node in the tree.
|
|
|
66 |
* @method addHandler
|
|
|
67 |
*/
|
|
|
68 |
var addHandler = function() {
|
|
|
69 |
var parent = $('[data-region="competencyactions"]').data('competency');
|
|
|
70 |
|
|
|
71 |
var params = {
|
|
|
72 |
competencyframeworkid: treeModel.getCompetencyFrameworkId(),
|
|
|
73 |
pagecontextid: pageContextId
|
|
|
74 |
};
|
|
|
75 |
|
|
|
76 |
if (parent !== null) {
|
|
|
77 |
// We are adding at a sub node.
|
|
|
78 |
params.parentid = parent.id;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
var relocate = function() {
|
|
|
82 |
var queryparams = $.param(params);
|
|
|
83 |
window.location = url.relativeUrl('/admin/tool/lp/editcompetency.php?' + queryparams);
|
|
|
84 |
};
|
|
|
85 |
|
|
|
86 |
if (parent !== null && treeModel.hasRule(parent.id)) {
|
|
|
87 |
str.get_strings([
|
|
|
88 |
{key: 'confirm', component: 'moodle'},
|
|
|
89 |
{key: 'addingcompetencywillresetparentrule', component: 'tool_lp', param: parent.shortname},
|
|
|
90 |
{key: 'yes', component: 'core'},
|
|
|
91 |
{key: 'no', component: 'core'}
|
|
|
92 |
]).done(function(strings) {
|
|
|
93 |
notification.confirm(
|
|
|
94 |
strings[0],
|
|
|
95 |
strings[1],
|
|
|
96 |
strings[2],
|
|
|
97 |
strings[3],
|
|
|
98 |
relocate
|
|
|
99 |
);
|
|
|
100 |
}).fail(notification.exception);
|
|
|
101 |
} else {
|
|
|
102 |
relocate();
|
|
|
103 |
}
|
|
|
104 |
};
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* A source and destination has been chosen - so time to complete a move.
|
|
|
108 |
* @method doMove
|
|
|
109 |
*/
|
|
|
110 |
var doMove = function() {
|
|
|
111 |
var frameworkid = $('[data-region="filtercompetencies"]').data('frameworkid');
|
|
|
112 |
var requests = ajax.call([{
|
|
|
113 |
methodname: 'core_competency_set_parent_competency',
|
|
|
114 |
args: {competencyid: moveSource, parentid: moveTarget}
|
|
|
115 |
}, {
|
|
|
116 |
methodname: 'tool_lp_data_for_competencies_manage_page',
|
|
|
117 |
args: {competencyframeworkid: frameworkid,
|
|
|
118 |
search: $('[data-region="filtercompetencies"] input').val()}
|
|
|
119 |
}]);
|
|
|
120 |
requests[1].done(reloadPage).fail(notification.exception);
|
|
|
121 |
};
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Confirms a competency move.
|
|
|
125 |
*
|
|
|
126 |
* @method confirmMove
|
|
|
127 |
*/
|
|
|
128 |
var confirmMove = function() {
|
|
|
129 |
moveTarget = typeof moveTarget === "undefined" ? 0 : moveTarget;
|
|
|
130 |
if (moveTarget == moveSource) {
|
|
|
131 |
// No move to do.
|
|
|
132 |
return;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
var targetComp = treeModel.getCompetency(moveTarget) || {},
|
|
|
136 |
sourceComp = treeModel.getCompetency(moveSource) || {},
|
|
|
137 |
confirmMessage = 'movecompetencywillresetrules',
|
|
|
138 |
showConfirm = false;
|
|
|
139 |
|
|
|
140 |
// We shouldn't be moving the competency to the same parent.
|
|
|
141 |
if (sourceComp.parentid == moveTarget) {
|
|
|
142 |
return;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
// If we are moving to a child of self.
|
|
|
146 |
if (targetComp.path && targetComp.path.indexOf('/' + sourceComp.id + '/') >= 0) {
|
|
|
147 |
confirmMessage = 'movecompetencytochildofselfwillresetrules';
|
|
|
148 |
|
|
|
149 |
// Show a confirmation if self has rules, as they'll disappear.
|
|
|
150 |
showConfirm = showConfirm || treeModel.hasRule(sourceComp.id);
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
// Show a confirmation if the current parent, or the destination have rules.
|
|
|
154 |
showConfirm = showConfirm || (treeModel.hasRule(targetComp.id) || treeModel.hasRule(sourceComp.parentid));
|
|
|
155 |
|
|
|
156 |
// Show confirm, and/or do the things.
|
|
|
157 |
if (showConfirm) {
|
|
|
158 |
str.get_strings([
|
|
|
159 |
{key: 'confirm', component: 'moodle'},
|
|
|
160 |
{key: confirmMessage, component: 'tool_lp'},
|
|
|
161 |
{key: 'yes', component: 'moodle'},
|
|
|
162 |
{key: 'no', component: 'moodle'}
|
|
|
163 |
]).done(function(strings) {
|
|
|
164 |
notification.confirm(
|
|
|
165 |
strings[0], // Confirm.
|
|
|
166 |
strings[1], // Delete competency X?
|
|
|
167 |
strings[2], // Delete.
|
|
|
168 |
strings[3], // Cancel.
|
|
|
169 |
doMove
|
|
|
170 |
);
|
|
|
171 |
}).fail(notification.exception);
|
|
|
172 |
|
|
|
173 |
} else {
|
|
|
174 |
doMove();
|
|
|
175 |
}
|
|
|
176 |
};
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* A move competency popup was opened - initialise the aria tree in it.
|
|
|
180 |
* @method initMovePopup
|
|
|
181 |
* @param {dialogue} popup The tool_lp/dialogue that was created.
|
|
|
182 |
*/
|
|
|
183 |
var initMovePopup = function(popup) {
|
|
|
184 |
var body = $(popup.getContent());
|
|
|
185 |
var treeRoot = body.find('[data-enhance=movetree]');
|
|
|
186 |
var tree = new Ariatree(treeRoot, false);
|
|
|
187 |
tree.on('selectionchanged', function(evt, params) {
|
|
|
188 |
var target = params.selected;
|
|
|
189 |
moveTarget = $(target).data('id');
|
|
|
190 |
});
|
|
|
191 |
treeRoot.show();
|
|
|
192 |
|
|
|
193 |
body.on('click', '[data-action="move"]', function() {
|
|
|
194 |
popup.close();
|
|
|
195 |
confirmMove();
|
|
|
196 |
});
|
|
|
197 |
body.on('click', '[data-action="cancel"]', function() {
|
|
|
198 |
popup.close();
|
|
|
199 |
});
|
|
|
200 |
};
|
|
|
201 |
|
|
|
202 |
/**
|
|
|
203 |
* Turn a flat list of competencies into a tree structure (recursive).
|
|
|
204 |
* @method addCompetencyChildren
|
|
|
205 |
* @param {Object} parent The current parent node in the tree
|
|
|
206 |
* @param {Object[]} competencies The flat list of competencies
|
|
|
207 |
*/
|
|
|
208 |
var addCompetencyChildren = function(parent, competencies) {
|
|
|
209 |
var i;
|
|
|
210 |
|
|
|
211 |
for (i = 0; i < competencies.length; i++) {
|
|
|
212 |
if (competencies[i].parentid == parent.id) {
|
|
|
213 |
parent.haschildren = true;
|
|
|
214 |
competencies[i].children = [];
|
|
|
215 |
competencies[i].haschildren = false;
|
|
|
216 |
parent.children[parent.children.length] = competencies[i];
|
|
|
217 |
addCompetencyChildren(competencies[i], competencies);
|
|
|
218 |
}
|
|
|
219 |
}
|
|
|
220 |
};
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* A node was chosen and "Move" was selected from the menu. Open a popup to select the target.
|
|
|
224 |
* @param {Event} e
|
|
|
225 |
* @method moveHandler
|
|
|
226 |
*/
|
|
|
227 |
var moveHandler = function(e) {
|
|
|
228 |
e.preventDefault();
|
|
|
229 |
var competency = $('[data-region="competencyactions"]').data('competency');
|
|
|
230 |
|
|
|
231 |
// Remember what we are moving.
|
|
|
232 |
moveSource = competency.id;
|
|
|
233 |
|
|
|
234 |
// Load data for the template.
|
|
|
235 |
var requests = ajax.call([
|
|
|
236 |
{
|
|
|
237 |
methodname: 'core_competency_search_competencies',
|
|
|
238 |
args: {
|
|
|
239 |
competencyframeworkid: competency.competencyframeworkid,
|
|
|
240 |
searchtext: ''
|
|
|
241 |
}
|
|
|
242 |
}, {
|
|
|
243 |
methodname: 'core_competency_read_competency_framework',
|
|
|
244 |
args: {
|
|
|
245 |
id: competency.competencyframeworkid
|
|
|
246 |
}
|
|
|
247 |
}
|
|
|
248 |
]);
|
|
|
249 |
|
|
|
250 |
// When all data has arrived, continue.
|
|
|
251 |
$.when.apply(null, requests).done(function(competencies, framework) {
|
|
|
252 |
|
|
|
253 |
// Expand the list of competencies into a tree.
|
|
|
254 |
var i;
|
|
|
255 |
var competenciestree = [];
|
|
|
256 |
for (i = 0; i < competencies.length; i++) {
|
|
|
257 |
var onecompetency = competencies[i];
|
|
|
258 |
if (onecompetency.parentid == "0") {
|
|
|
259 |
onecompetency.children = [];
|
|
|
260 |
onecompetency.haschildren = 0;
|
|
|
261 |
competenciestree[competenciestree.length] = onecompetency;
|
|
|
262 |
addCompetencyChildren(onecompetency, competencies);
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
str.get_strings([
|
|
|
267 |
{key: 'movecompetency', component: 'tool_lp', param: competency.shortname},
|
|
|
268 |
{key: 'move', component: 'tool_lp'},
|
|
|
269 |
{key: 'cancel', component: 'moodle'}
|
|
|
270 |
]).done(function(strings) {
|
|
|
271 |
|
|
|
272 |
var context = {
|
|
|
273 |
framework: framework,
|
|
|
274 |
competencies: competenciestree
|
|
|
275 |
};
|
|
|
276 |
|
|
|
277 |
templates.render('tool_lp/competencies_move_tree', context)
|
|
|
278 |
.done(function(tree) {
|
|
|
279 |
new Dialogue(
|
|
|
280 |
strings[0], // Move competency x.
|
|
|
281 |
tree, // The move tree.
|
|
|
282 |
initMovePopup
|
|
|
283 |
);
|
|
|
284 |
|
|
|
285 |
}).fail(notification.exception);
|
|
|
286 |
|
|
|
287 |
}).fail(notification.exception);
|
|
|
288 |
|
|
|
289 |
}).fail(notification.exception);
|
|
|
290 |
|
|
|
291 |
};
|
|
|
292 |
|
|
|
293 |
/**
|
|
|
294 |
* Edit the selected competency.
|
|
|
295 |
* @method editHandler
|
|
|
296 |
*/
|
|
|
297 |
var editHandler = function() {
|
|
|
298 |
var competency = $('[data-region="competencyactions"]').data('competency');
|
|
|
299 |
|
|
|
300 |
var params = {
|
|
|
301 |
competencyframeworkid: treeModel.getCompetencyFrameworkId(),
|
|
|
302 |
id: competency.id,
|
|
|
303 |
parentid: competency.parentid,
|
|
|
304 |
pagecontextid: pageContextId
|
|
|
305 |
};
|
|
|
306 |
|
|
|
307 |
var queryparams = $.param(params);
|
|
|
308 |
window.location = url.relativeUrl('/admin/tool/lp/editcompetency.php?' + queryparams);
|
|
|
309 |
};
|
|
|
310 |
|
|
|
311 |
/**
|
|
|
312 |
* Re-render the page with the latest data.
|
|
|
313 |
* @param {Object} context
|
|
|
314 |
* @method reloadPage
|
|
|
315 |
*/
|
|
|
316 |
var reloadPage = function(context) {
|
|
|
317 |
templates.render('tool_lp/manage_competencies_page', context)
|
|
|
318 |
.done(function(newhtml, newjs) {
|
|
|
319 |
$('[data-region="managecompetencies"]').replaceWith(newhtml);
|
|
|
320 |
templates.runTemplateJS(newjs);
|
|
|
321 |
})
|
|
|
322 |
.fail(notification.exception);
|
|
|
323 |
};
|
|
|
324 |
|
|
|
325 |
/**
|
|
|
326 |
* Perform a search and render the page with the new search results.
|
|
|
327 |
* @param {Event} e
|
|
|
328 |
* @method updateSearchHandler
|
|
|
329 |
*/
|
|
|
330 |
var updateSearchHandler = function(e) {
|
|
|
331 |
e.preventDefault();
|
|
|
332 |
|
|
|
333 |
var frameworkid = $('[data-region="filtercompetencies"]').data('frameworkid');
|
|
|
334 |
|
|
|
335 |
var requests = ajax.call([{
|
|
|
336 |
methodname: 'tool_lp_data_for_competencies_manage_page',
|
|
|
337 |
args: {competencyframeworkid: frameworkid,
|
|
|
338 |
search: $('[data-region="filtercompetencies"] input').val()}
|
|
|
339 |
}]);
|
|
|
340 |
requests[0].done(reloadPage).fail(notification.exception);
|
|
|
341 |
};
|
|
|
342 |
|
|
|
343 |
/**
|
|
|
344 |
* Move a competency "up". This only affects the sort order within the same branch of the tree.
|
|
|
345 |
* @method moveUpHandler
|
|
|
346 |
*/
|
|
|
347 |
var moveUpHandler = function() {
|
|
|
348 |
// We are chaining ajax requests here.
|
|
|
349 |
var competency = $('[data-region="competencyactions"]').data('competency');
|
|
|
350 |
var requests = ajax.call([{
|
|
|
351 |
methodname: 'core_competency_move_up_competency',
|
|
|
352 |
args: {id: competency.id}
|
|
|
353 |
}, {
|
|
|
354 |
methodname: 'tool_lp_data_for_competencies_manage_page',
|
|
|
355 |
args: {competencyframeworkid: competency.competencyframeworkid,
|
|
|
356 |
search: $('[data-region="filtercompetencies"] input').val()}
|
|
|
357 |
}]);
|
|
|
358 |
requests[1].done(reloadPage).fail(notification.exception);
|
|
|
359 |
};
|
|
|
360 |
|
|
|
361 |
/**
|
|
|
362 |
* Move a competency "down". This only affects the sort order within the same branch of the tree.
|
|
|
363 |
* @method moveDownHandler
|
|
|
364 |
*/
|
|
|
365 |
var moveDownHandler = function() {
|
|
|
366 |
// We are chaining ajax requests here.
|
|
|
367 |
var competency = $('[data-region="competencyactions"]').data('competency');
|
|
|
368 |
var requests = ajax.call([{
|
|
|
369 |
methodname: 'core_competency_move_down_competency',
|
|
|
370 |
args: {id: competency.id}
|
|
|
371 |
}, {
|
|
|
372 |
methodname: 'tool_lp_data_for_competencies_manage_page',
|
|
|
373 |
args: {competencyframeworkid: competency.competencyframeworkid,
|
|
|
374 |
search: $('[data-region="filtercompetencies"] input').val()}
|
|
|
375 |
}]);
|
|
|
376 |
requests[1].done(reloadPage).fail(notification.exception);
|
|
|
377 |
};
|
|
|
378 |
|
|
|
379 |
/**
|
|
|
380 |
* Open a dialogue to show all the courses using the selected competency.
|
|
|
381 |
* @method seeCoursesHandler
|
|
|
382 |
*/
|
|
|
383 |
var seeCoursesHandler = function() {
|
|
|
384 |
var competency = $('[data-region="competencyactions"]').data('competency');
|
|
|
385 |
|
|
|
386 |
var requests = ajax.call([{
|
|
|
387 |
methodname: 'tool_lp_list_courses_using_competency',
|
|
|
388 |
args: {id: competency.id}
|
|
|
389 |
}]);
|
|
|
390 |
|
|
|
391 |
requests[0].done(function(courses) {
|
|
|
392 |
var context = {
|
|
|
393 |
courses: courses
|
|
|
394 |
};
|
|
|
395 |
templates.render('tool_lp/linked_courses_summary', context).done(function(html) {
|
|
|
396 |
str.get_string('linkedcourses', 'tool_lp').done(function(linkedcourses) {
|
|
|
397 |
new Dialogue(
|
|
|
398 |
linkedcourses, // Title.
|
|
|
399 |
html, // The linked courses.
|
|
|
400 |
initMovePopup
|
|
|
401 |
);
|
|
|
402 |
}).fail(notification.exception);
|
|
|
403 |
}).fail(notification.exception);
|
|
|
404 |
}).fail(notification.exception);
|
|
|
405 |
};
|
|
|
406 |
|
|
|
407 |
/**
|
|
|
408 |
* Open a competencies popup to relate competencies.
|
|
|
409 |
*
|
|
|
410 |
* @method relateCompetenciesHandler
|
|
|
411 |
*/
|
|
|
412 |
var relateCompetenciesHandler = function() {
|
|
|
413 |
relatedTarget = $('[data-region="competencyactions"]').data('competency');
|
|
|
414 |
|
|
|
415 |
if (!pickerInstance) {
|
|
|
416 |
pickerInstance = new Picker(pageContextId, relatedTarget.competencyframeworkid);
|
|
|
417 |
pickerInstance.on('save', function(e, data) {
|
|
|
418 |
var pendingPromise = new Pending();
|
|
|
419 |
var compIds = data.competencyIds;
|
|
|
420 |
|
|
|
421 |
var calls = [];
|
|
|
422 |
$.each(compIds, function(index, value) {
|
|
|
423 |
calls.push({
|
|
|
424 |
methodname: 'core_competency_add_related_competency',
|
|
|
425 |
args: {competencyid: value, relatedcompetencyid: relatedTarget.id}
|
|
|
426 |
});
|
|
|
427 |
});
|
|
|
428 |
|
|
|
429 |
calls.push({
|
|
|
430 |
methodname: 'tool_lp_data_for_related_competencies_section',
|
|
|
431 |
args: {competencyid: relatedTarget.id}
|
|
|
432 |
});
|
|
|
433 |
|
|
|
434 |
var promises = ajax.call(calls);
|
|
|
435 |
|
|
|
436 |
promises[calls.length - 1].then(function(context) {
|
|
|
437 |
return templates.render('tool_lp/related_competencies', context);
|
|
|
438 |
}).then(function(html, js) {
|
|
|
439 |
$('[data-region="relatedcompetencies"]').replaceWith(html);
|
|
|
440 |
templates.runTemplateJS(js);
|
|
|
441 |
updatedRelatedCompetencies();
|
|
|
442 |
return;
|
|
|
443 |
})
|
|
|
444 |
.then(pendingPromise.resolve)
|
|
|
445 |
.catch(notification.exception);
|
|
|
446 |
});
|
|
|
447 |
}
|
|
|
448 |
|
|
|
449 |
pickerInstance.setDisallowedCompetencyIDs([relatedTarget.id]);
|
|
|
450 |
pickerInstance.display();
|
|
|
451 |
};
|
|
|
452 |
|
|
|
453 |
var ruleConfigHandler = function(e) {
|
|
|
454 |
e.preventDefault();
|
|
|
455 |
relatedTarget = $('[data-region="competencyactions"]').data('competency');
|
|
|
456 |
ruleConfigInstance.setTargetCompetencyId(relatedTarget.id);
|
|
|
457 |
ruleConfigInstance.display();
|
|
|
458 |
};
|
|
|
459 |
|
|
|
460 |
var ruleConfigSaveHandler = function(e, config) {
|
|
|
461 |
var update = {
|
|
|
462 |
id: relatedTarget.id,
|
|
|
463 |
shortname: relatedTarget.shortname,
|
|
|
464 |
idnumber: relatedTarget.idnumber,
|
|
|
465 |
description: relatedTarget.description,
|
|
|
466 |
descriptionformat: relatedTarget.descriptionformat,
|
|
|
467 |
ruletype: config.ruletype,
|
|
|
468 |
ruleoutcome: config.ruleoutcome,
|
|
|
469 |
ruleconfig: config.ruleconfig
|
|
|
470 |
};
|
|
|
471 |
var promise = ajax.call([{
|
|
|
472 |
methodname: 'core_competency_update_competency',
|
|
|
473 |
args: {competency: update}
|
|
|
474 |
}]);
|
|
|
475 |
promise[0].then(function(result) {
|
|
|
476 |
if (result) {
|
|
|
477 |
relatedTarget.ruletype = config.ruletype;
|
|
|
478 |
relatedTarget.ruleoutcome = config.ruleoutcome;
|
|
|
479 |
relatedTarget.ruleconfig = config.ruleconfig;
|
|
|
480 |
renderCompetencySummary(relatedTarget);
|
|
|
481 |
}
|
|
|
482 |
return;
|
|
|
483 |
}).catch(notification.exception);
|
|
|
484 |
};
|
|
|
485 |
|
|
|
486 |
/**
|
|
|
487 |
* Delete a competency.
|
|
|
488 |
* @method doDelete
|
|
|
489 |
*/
|
|
|
490 |
var doDelete = function() {
|
|
|
491 |
// We are chaining ajax requests here.
|
|
|
492 |
var competency = $('[data-region="competencyactions"]').data('competency');
|
|
|
493 |
var requests = ajax.call([{
|
|
|
494 |
methodname: 'core_competency_delete_competency',
|
|
|
495 |
args: {id: competency.id}
|
|
|
496 |
}, {
|
|
|
497 |
methodname: 'tool_lp_data_for_competencies_manage_page',
|
|
|
498 |
args: {competencyframeworkid: competency.competencyframeworkid,
|
|
|
499 |
search: $('[data-region="filtercompetencies"] input').val()}
|
|
|
500 |
}]);
|
|
|
501 |
requests[0].done(function(success) {
|
|
|
502 |
if (success === false) {
|
|
|
503 |
str.get_strings([
|
|
|
504 |
{key: 'competencycannotbedeleted', component: 'tool_lp', param: competency.shortname},
|
|
|
505 |
{key: 'cancel', component: 'moodle'}
|
|
|
506 |
]).done(function(strings) {
|
|
|
507 |
notification.alert(
|
|
|
508 |
null,
|
|
|
509 |
strings[0]
|
|
|
510 |
);
|
|
|
511 |
}).fail(notification.exception);
|
|
|
512 |
}
|
|
|
513 |
}).fail(notification.exception);
|
|
|
514 |
requests[1].done(reloadPage).fail(notification.exception);
|
|
|
515 |
};
|
|
|
516 |
|
|
|
517 |
/**
|
|
|
518 |
* Show a confirm dialogue before deleting a competency.
|
|
|
519 |
* @method deleteCompetencyHandler
|
|
|
520 |
*/
|
|
|
521 |
var deleteCompetencyHandler = function() {
|
|
|
522 |
var competency = $('[data-region="competencyactions"]').data('competency'),
|
|
|
523 |
confirmMessage = 'deletecompetency';
|
|
|
524 |
|
|
|
525 |
if (treeModel.hasRule(competency.parentid)) {
|
|
|
526 |
confirmMessage = 'deletecompetencyparenthasrule';
|
|
|
527 |
}
|
|
|
528 |
|
|
|
529 |
str.get_strings([
|
|
|
530 |
{key: 'confirm', component: 'moodle'},
|
|
|
531 |
{key: confirmMessage, component: 'tool_lp', param: competency.shortname},
|
|
|
532 |
{key: 'delete', component: 'moodle'},
|
|
|
533 |
{key: 'cancel', component: 'moodle'}
|
|
|
534 |
]).done(function(strings) {
|
|
|
535 |
notification.confirm(
|
|
|
536 |
strings[0], // Confirm.
|
|
|
537 |
strings[1], // Delete competency X?
|
|
|
538 |
strings[2], // Delete.
|
|
|
539 |
strings[3], // Cancel.
|
|
|
540 |
doDelete
|
|
|
541 |
);
|
|
|
542 |
}).fail(notification.exception);
|
|
|
543 |
};
|
|
|
544 |
|
|
|
545 |
/**
|
|
|
546 |
* HTML5 implementation of drag/drop (there is an accesible alternative in the menus).
|
|
|
547 |
* @method dragStart
|
|
|
548 |
* @param {Event} e
|
|
|
549 |
*/
|
|
|
550 |
var dragStart = function(e) {
|
|
|
551 |
e.originalEvent.dataTransfer.setData('text', $(e.target).parent().data('id'));
|
|
|
552 |
};
|
|
|
553 |
|
|
|
554 |
/**
|
|
|
555 |
* HTML5 implementation of drag/drop (there is an accesible alternative in the menus).
|
|
|
556 |
* @method allowDrop
|
|
|
557 |
* @param {Event} e
|
|
|
558 |
*/
|
|
|
559 |
var allowDrop = function(e) {
|
|
|
560 |
e.originalEvent.dataTransfer.dropEffect = 'move';
|
|
|
561 |
e.preventDefault();
|
|
|
562 |
};
|
|
|
563 |
|
|
|
564 |
/**
|
|
|
565 |
* HTML5 implementation of drag/drop (there is an accesible alternative in the menus).
|
|
|
566 |
* @method dragEnter
|
|
|
567 |
* @param {Event} e
|
|
|
568 |
*/
|
|
|
569 |
var dragEnter = function(e) {
|
|
|
570 |
e.preventDefault();
|
|
|
571 |
$(this).addClass('currentdragtarget');
|
|
|
572 |
};
|
|
|
573 |
|
|
|
574 |
/**
|
|
|
575 |
* HTML5 implementation of drag/drop (there is an accesible alternative in the menus).
|
|
|
576 |
* @method dragLeave
|
|
|
577 |
* @param {Event} e
|
|
|
578 |
*/
|
|
|
579 |
var dragLeave = function(e) {
|
|
|
580 |
e.preventDefault();
|
|
|
581 |
$(this).removeClass('currentdragtarget');
|
|
|
582 |
};
|
|
|
583 |
|
|
|
584 |
/**
|
|
|
585 |
* HTML5 implementation of drag/drop (there is an accesible alternative in the menus).
|
|
|
586 |
* @method dropOver
|
|
|
587 |
* @param {Event} e
|
|
|
588 |
*/
|
|
|
589 |
var dropOver = function(e) {
|
|
|
590 |
e.preventDefault();
|
|
|
591 |
moveSource = e.originalEvent.dataTransfer.getData('text');
|
|
|
592 |
moveTarget = $(e.target).parent().data('id');
|
|
|
593 |
$(this).removeClass('currentdragtarget');
|
|
|
594 |
|
|
|
595 |
confirmMove();
|
|
|
596 |
};
|
|
|
597 |
|
|
|
598 |
/**
|
|
|
599 |
* Deletes a related competency without confirmation.
|
|
|
600 |
*
|
|
|
601 |
* @param {Event} e The event that triggered the action.
|
|
|
602 |
* @method deleteRelatedHandler
|
|
|
603 |
*/
|
|
|
604 |
var deleteRelatedHandler = function(e) {
|
|
|
605 |
e.preventDefault();
|
|
|
606 |
|
|
|
607 |
var relatedid = this.id.substr(11);
|
|
|
608 |
var competency = $('[data-region="competencyactions"]').data('competency');
|
|
|
609 |
var removeRelated = ajax.call([
|
|
|
610 |
{methodname: 'core_competency_remove_related_competency',
|
|
|
611 |
args: {relatedcompetencyid: relatedid, competencyid: competency.id}},
|
|
|
612 |
{methodname: 'tool_lp_data_for_related_competencies_section',
|
|
|
613 |
args: {competencyid: competency.id}}
|
|
|
614 |
]);
|
|
|
615 |
|
|
|
616 |
removeRelated[1].done(function(context) {
|
|
|
617 |
templates.render('tool_lp/related_competencies', context).done(function(html) {
|
|
|
618 |
$('[data-region="relatedcompetencies"]').replaceWith(html);
|
|
|
619 |
updatedRelatedCompetencies();
|
|
|
620 |
}).fail(notification.exception);
|
|
|
621 |
}).fail(notification.exception);
|
|
|
622 |
};
|
|
|
623 |
|
|
|
624 |
/**
|
|
|
625 |
* Updates the competencies list (with relations) and add listeners.
|
|
|
626 |
*
|
|
|
627 |
* @method updatedRelatedCompetencies
|
|
|
628 |
*/
|
|
|
629 |
var updatedRelatedCompetencies = function() {
|
|
|
630 |
|
|
|
631 |
// Listeners to newly loaded related competencies.
|
|
|
632 |
$('[data-action="deleterelation"]').on('click', deleteRelatedHandler);
|
|
|
633 |
|
|
|
634 |
};
|
|
|
635 |
|
|
|
636 |
/**
|
|
|
637 |
* Log the competency viewed event.
|
|
|
638 |
*
|
|
|
639 |
* @param {Object} competency The competency.
|
|
|
640 |
* @method triggerCompetencyViewedEvent
|
|
|
641 |
*/
|
|
|
642 |
var triggerCompetencyViewedEvent = function(competency) {
|
|
|
643 |
if (competency.id !== selectedCompetencyId) {
|
|
|
644 |
// Set the selected competency id.
|
|
|
645 |
selectedCompetencyId = competency.id;
|
|
|
646 |
ajax.call([{
|
|
|
647 |
methodname: 'core_competency_competency_viewed',
|
|
|
648 |
args: {id: competency.id}
|
|
|
649 |
}]);
|
|
|
650 |
}
|
|
|
651 |
};
|
|
|
652 |
|
|
|
653 |
/**
|
|
|
654 |
* Return the taxonomy constant for a level.
|
|
|
655 |
*
|
|
|
656 |
* @param {Number} level The level.
|
|
|
657 |
* @return {String}
|
|
|
658 |
* @function getTaxonomyAtLevel
|
|
|
659 |
*/
|
|
|
660 |
var getTaxonomyAtLevel = function(level) {
|
|
|
661 |
var constant = taxonomiesConstants[level];
|
|
|
662 |
if (!constant) {
|
|
|
663 |
constant = 'competency';
|
|
|
664 |
}
|
|
|
665 |
return constant;
|
|
|
666 |
};
|
|
|
667 |
|
|
|
668 |
/**
|
|
|
669 |
* Render the competency summary.
|
|
|
670 |
*
|
|
|
671 |
* @param {Object} competency The competency.
|
|
|
672 |
*/
|
|
|
673 |
var renderCompetencySummary = function(competency) {
|
|
|
674 |
var promise = $.Deferred().resolve().promise(),
|
|
|
675 |
context = {};
|
|
|
676 |
|
|
|
677 |
context.competency = competency;
|
|
|
678 |
context.showdeleterelatedaction = true;
|
|
|
679 |
context.showrelatedcompetencies = true;
|
|
|
680 |
context.showrule = false;
|
|
|
681 |
context.pluginbaseurl = url.relativeUrl('/admin/tool/lp');
|
|
|
682 |
|
|
|
683 |
if (competency.ruleoutcome != Outcomes.NONE) {
|
|
|
684 |
// Get the outcome and rule name.
|
|
|
685 |
promise = Outcomes.getString(competency.ruleoutcome).then(function(str) {
|
|
|
686 |
var name;
|
|
|
687 |
$.each(rulesModules, function(index, modInfo) {
|
|
|
688 |
if (modInfo.type == competency.ruletype) {
|
|
|
689 |
name = modInfo.name;
|
|
|
690 |
}
|
|
|
691 |
});
|
|
|
692 |
return [str, name];
|
|
|
693 |
});
|
|
|
694 |
}
|
|
|
695 |
|
|
|
696 |
promise.then(function(strs) {
|
|
|
697 |
if (typeof strs !== 'undefined') {
|
|
|
698 |
context.showrule = true;
|
|
|
699 |
context.rule = {
|
|
|
700 |
outcome: strs[0],
|
|
|
701 |
type: strs[1]
|
|
|
702 |
};
|
|
|
703 |
}
|
|
|
704 |
return context;
|
|
|
705 |
}).then(function(context) {
|
|
|
706 |
return templates.render('tool_lp/competency_summary', context);
|
|
|
707 |
}).then(function(html) {
|
|
|
708 |
$('[data-region="competencyinfo"]').html(html);
|
|
|
709 |
$('[data-action="deleterelation"]').on('click', deleteRelatedHandler);
|
|
|
710 |
return templates.render('tool_lp/loading', {});
|
|
|
711 |
}).then(function(html, js) {
|
|
|
712 |
templates.replaceNodeContents('[data-region="relatedcompetencies"]', html, js);
|
|
|
713 |
return ajax.call([{
|
|
|
714 |
methodname: 'tool_lp_data_for_related_competencies_section',
|
|
|
715 |
args: {competencyid: competency.id}
|
|
|
716 |
}])[0];
|
|
|
717 |
}).then(function(context) {
|
|
|
718 |
return templates.render('tool_lp/related_competencies', context);
|
|
|
719 |
}).then(function(html, js) {
|
|
|
720 |
$('[data-region="relatedcompetencies"]').replaceWith(html);
|
|
|
721 |
templates.runTemplateJS(js);
|
|
|
722 |
updatedRelatedCompetencies();
|
|
|
723 |
return;
|
|
|
724 |
}).catch(notification.exception);
|
|
|
725 |
};
|
|
|
726 |
|
|
|
727 |
/**
|
|
|
728 |
* Return the string "Add <taxonomy>".
|
|
|
729 |
*
|
|
|
730 |
* @param {Number} level The level.
|
|
|
731 |
* @return {String}
|
|
|
732 |
* @function strAddTaxonomy
|
|
|
733 |
*/
|
|
|
734 |
var strAddTaxonomy = function(level) {
|
|
|
735 |
return str.get_string('taxonomy_add_' + getTaxonomyAtLevel(level), 'tool_lp');
|
|
|
736 |
};
|
|
|
737 |
|
|
|
738 |
/**
|
|
|
739 |
* Return the string "Selected <taxonomy>".
|
|
|
740 |
*
|
|
|
741 |
* @param {Number} level The level.
|
|
|
742 |
* @return {String}
|
|
|
743 |
* @function strSelectedTaxonomy
|
|
|
744 |
*/
|
|
|
745 |
var strSelectedTaxonomy = function(level) {
|
|
|
746 |
return str.get_string('taxonomy_selected_' + getTaxonomyAtLevel(level), 'tool_lp');
|
|
|
747 |
};
|
|
|
748 |
|
|
|
749 |
/**
|
|
|
750 |
* Handler when a node in the aria tree is selected.
|
|
|
751 |
* @method selectionChanged
|
|
|
752 |
* @param {Event} evt The event that triggered the selection change.
|
|
|
753 |
* @param {Object} params The parameters for the event. Contains a list of selected nodes.
|
|
|
754 |
* @return {Boolean}
|
|
|
755 |
*/
|
|
|
756 |
var selectionChanged = function(evt, params) {
|
|
|
757 |
var node = params.selected,
|
|
|
758 |
id = $(node).data('id'),
|
|
|
759 |
btn = $('[data-region="competencyactions"] [data-action="add"]'),
|
|
|
760 |
actionMenu = $('[data-region="competencyactionsmenu"]'),
|
|
|
761 |
selectedTitle = $('[data-region="selected-competency"]'),
|
|
|
762 |
level = 0,
|
|
|
763 |
sublevel = 1;
|
|
|
764 |
|
|
|
765 |
menubar.closeAll();
|
|
|
766 |
|
|
|
767 |
if (typeof id === "undefined") {
|
|
|
768 |
// Assume this is the root of the tree.
|
|
|
769 |
// Here we are only getting the text from the top of the tree, to do it we clone the tree,
|
|
|
770 |
// remove all children and then call text on the result.
|
|
|
771 |
$('[data-region="competencyinfo"]').html(node.clone().children().remove().end().text());
|
|
|
772 |
$('[data-region="competencyactions"]').data('competency', null);
|
|
|
773 |
actionMenu.hide();
|
|
|
774 |
|
|
|
775 |
} else {
|
|
|
776 |
var competency = treeModel.getCompetency(id);
|
|
|
777 |
|
|
|
778 |
level = treeModel.getCompetencyLevel(id);
|
|
|
779 |
sublevel = level + 1;
|
|
|
780 |
|
|
|
781 |
actionMenu.show();
|
|
|
782 |
$('[data-region="competencyactions"]').data('competency', competency);
|
|
|
783 |
renderCompetencySummary(competency);
|
|
|
784 |
// Log Competency viewed event.
|
|
|
785 |
triggerCompetencyViewedEvent(competency);
|
|
|
786 |
}
|
|
|
787 |
strSelectedTaxonomy(level).then(function(str) {
|
|
|
788 |
selectedTitle.text(str);
|
|
|
789 |
return;
|
|
|
790 |
}).catch(notification.exception);
|
|
|
791 |
|
|
|
792 |
strAddTaxonomy(sublevel).then(function(str) {
|
|
|
793 |
btn.show()
|
|
|
794 |
.find('[data-region="term"]')
|
|
|
795 |
.text(str);
|
|
|
796 |
return;
|
|
|
797 |
}).catch(notification.exception);
|
|
|
798 |
|
|
|
799 |
// We handled this event so consume it.
|
|
|
800 |
evt.preventDefault();
|
|
|
801 |
return false;
|
|
|
802 |
};
|
|
|
803 |
|
|
|
804 |
/**
|
|
|
805 |
* Return the string "Selected <taxonomy>".
|
|
|
806 |
*
|
|
|
807 |
* @function parseTaxonomies
|
|
|
808 |
* @param {String} taxonomiesstr Comma separated list of taxonomies.
|
|
|
809 |
* @return {Array} of level => taxonomystr
|
|
|
810 |
*/
|
|
|
811 |
var parseTaxonomies = function(taxonomiesstr) {
|
|
|
812 |
var all = taxonomiesstr.split(',');
|
|
|
813 |
all.unshift("");
|
|
|
814 |
delete all[0];
|
|
|
815 |
|
|
|
816 |
// Note we don't need to fill holes, because other functions check for empty anyway.
|
|
|
817 |
return all;
|
|
|
818 |
};
|
|
|
819 |
|
|
|
820 |
return {
|
|
|
821 |
/**
|
|
|
822 |
* Initialise this page (attach event handlers etc).
|
|
|
823 |
*
|
|
|
824 |
* @method init
|
|
|
825 |
* @param {Object} model The tree model provides some useful functions for loading and searching competencies.
|
|
|
826 |
* @param {Number} pagectxid The page context ID.
|
|
|
827 |
* @param {Object} taxonomies Constants indexed by level.
|
|
|
828 |
* @param {Object} rulesMods The modules of the rules.
|
|
|
829 |
*/
|
|
|
830 |
init: function(model, pagectxid, taxonomies, rulesMods) {
|
|
|
831 |
treeModel = model;
|
|
|
832 |
pageContextId = pagectxid;
|
|
|
833 |
taxonomiesConstants = parseTaxonomies(taxonomies);
|
|
|
834 |
rulesModules = rulesMods;
|
|
|
835 |
|
|
|
836 |
$('[data-region="competencyactions"] [data-action="add"]').on('click', addHandler);
|
|
|
837 |
|
|
|
838 |
menubar.enhance('.competencyactionsmenu', {
|
|
|
839 |
'[data-action="edit"]': editHandler,
|
|
|
840 |
'[data-action="delete"]': deleteCompetencyHandler,
|
|
|
841 |
'[data-action="move"]': moveHandler,
|
|
|
842 |
'[data-action="moveup"]': moveUpHandler,
|
|
|
843 |
'[data-action="movedown"]': moveDownHandler,
|
|
|
844 |
'[data-action="linkedcourses"]': seeCoursesHandler,
|
|
|
845 |
'[data-action="relatedcompetencies"]': relateCompetenciesHandler.bind(this),
|
|
|
846 |
'[data-action="competencyrules"]': ruleConfigHandler.bind(this)
|
|
|
847 |
});
|
|
|
848 |
$('[data-region="competencyactionsmenu"]').hide();
|
|
|
849 |
$('[data-region="competencyactions"] [data-action="add"]').hide();
|
|
|
850 |
|
|
|
851 |
$('[data-region="filtercompetencies"]').on('submit', updateSearchHandler);
|
|
|
852 |
// Simple html5 drag drop because we already added an accessible alternative.
|
|
|
853 |
var top = $('[data-region="managecompetencies"] [data-enhance="tree"]');
|
|
|
854 |
top.on('dragstart', 'li>span', dragStart)
|
|
|
855 |
.on('dragover', 'li>span', allowDrop)
|
|
|
856 |
.on('dragenter', 'li>span', dragEnter)
|
|
|
857 |
.on('dragleave', 'li>span', dragLeave)
|
|
|
858 |
.on('drop', 'li>span', dropOver);
|
|
|
859 |
|
|
|
860 |
model.on('selectionchanged', selectionChanged);
|
|
|
861 |
|
|
|
862 |
// Prepare the configuration tool.
|
|
|
863 |
ruleConfigInstance = new RuleConfig(treeModel, rulesModules);
|
|
|
864 |
ruleConfigInstance.on('save', ruleConfigSaveHandler.bind(this));
|
|
|
865 |
}
|
|
|
866 |
};
|
|
|
867 |
});
|