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 on the competency tree.
|
|
|
18 |
*
|
|
|
19 |
* @module tool_lp/competencytree
|
|
|
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(['core/ajax', 'core/notification', 'core/templates', 'tool_lp/tree', 'tool_lp/competency_outcomes', 'jquery'],
|
|
|
24 |
function(ajax, notification, templates, Ariatree, CompOutcomes, $) {
|
|
|
25 |
|
|
|
26 |
// Private variables and functions.
|
|
|
27 |
/** @var {Object[]} competencies - Cached list of competencies */
|
|
|
28 |
var competencies = {};
|
|
|
29 |
|
|
|
30 |
/** @var {Number} competencyFrameworkId - The current framework id */
|
|
|
31 |
var competencyFrameworkId = 0;
|
|
|
32 |
|
|
|
33 |
/** @var {String} competencyFrameworkShortName - The current framework short name */
|
|
|
34 |
var competencyFrameworkShortName = '';
|
|
|
35 |
|
|
|
36 |
/** @var {String} treeSelector - The selector for the root of the tree. */
|
|
|
37 |
var treeSelector = '';
|
|
|
38 |
|
|
|
39 |
/** @var {String} currentNodeId - The data-id of the current node in the tree. */
|
|
|
40 |
var currentNodeId = '';
|
|
|
41 |
|
|
|
42 |
/** @var {Boolean} competencyFramworkCanManage - Can manage the competencies framework */
|
|
|
43 |
var competencyFramworkCanManage = false;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Build a tree from the flat list of competencies.
|
|
|
47 |
* @param {Object} parent The parent competency.
|
|
|
48 |
* @param {Array} all The list of all competencies.
|
|
|
49 |
*/
|
|
|
50 |
var addChildren = function(parent, all) {
|
|
|
51 |
var i = 0;
|
|
|
52 |
var current = false;
|
|
|
53 |
parent.haschildren = false;
|
|
|
54 |
parent.children = [];
|
|
|
55 |
for (i = 0; i < all.length; i++) {
|
|
|
56 |
current = all[i];
|
|
|
57 |
if (current.parentid == parent.id) {
|
|
|
58 |
parent.haschildren = true;
|
|
|
59 |
parent.children.push(current);
|
|
|
60 |
addChildren(current, all);
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
};
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Load the list of competencies via ajax. Competencies are filtered by the searchtext.
|
|
|
67 |
* @param {String} searchtext The text to filter on.
|
|
|
68 |
* @return {promise}
|
|
|
69 |
*/
|
|
|
70 |
var loadCompetencies = function(searchtext) {
|
|
|
71 |
var deferred = $.Deferred();
|
|
|
72 |
|
|
|
73 |
templates.render('tool_lp/loading', {}).done(function(loadinghtml, loadingjs) {
|
|
|
74 |
templates.replaceNodeContents($(treeSelector), loadinghtml, loadingjs);
|
|
|
75 |
|
|
|
76 |
var promises = ajax.call([{
|
|
|
77 |
methodname: 'core_competency_search_competencies',
|
|
|
78 |
args: {
|
|
|
79 |
searchtext: searchtext,
|
|
|
80 |
competencyframeworkid: competencyFrameworkId
|
|
|
81 |
}
|
|
|
82 |
}]);
|
|
|
83 |
promises[0].done(function(result) {
|
|
|
84 |
competencies = {};
|
|
|
85 |
var i = 0;
|
|
|
86 |
for (i = 0; i < result.length; i++) {
|
|
|
87 |
competencies[result[i].id] = result[i];
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
var children = [];
|
|
|
91 |
var competency = false;
|
|
|
92 |
for (i = 0; i < result.length; i++) {
|
|
|
93 |
competency = result[i];
|
|
|
94 |
if (parseInt(competency.parentid, 10) === 0) {
|
|
|
95 |
children.push(competency);
|
|
|
96 |
addChildren(competency, result);
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
var context = {
|
|
|
100 |
shortname: competencyFrameworkShortName,
|
|
|
101 |
canmanage: competencyFramworkCanManage,
|
|
|
102 |
competencies: children
|
|
|
103 |
};
|
|
|
104 |
templates.render('tool_lp/competencies_tree_root', context).done(function(html, js) {
|
|
|
105 |
templates.replaceNodeContents($(treeSelector), $(html).html(), js);
|
|
|
106 |
var tree = new Ariatree(treeSelector, false);
|
|
|
107 |
|
|
|
108 |
if (currentNodeId) {
|
|
|
109 |
var node = $(treeSelector).find('[data-id=' + currentNodeId + ']');
|
|
|
110 |
if (node.length) {
|
|
|
111 |
tree.selectItem(node);
|
|
|
112 |
tree.updateFocus(node);
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
deferred.resolve(competencies);
|
|
|
116 |
}).fail(deferred.reject);
|
|
|
117 |
}).fail(deferred.reject);
|
|
|
118 |
});
|
|
|
119 |
|
|
|
120 |
return deferred.promise();
|
|
|
121 |
};
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Whenever the current item in the tree is changed - remember the "id".
|
|
|
125 |
* @param {Event} evt
|
|
|
126 |
* @param {Object} params The parameters for the event (This is the selected node).
|
|
|
127 |
*/
|
|
|
128 |
var rememberCurrent = function(evt, params) {
|
|
|
129 |
var node = params.selected;
|
|
|
130 |
currentNodeId = node.attr('data-id');
|
|
|
131 |
};
|
|
|
132 |
|
|
|
133 |
return /** @alias module:tool_lp/competencytree */ {
|
|
|
134 |
// Public variables and functions.
|
|
|
135 |
/**
|
|
|
136 |
* Initialise the tree.
|
|
|
137 |
*
|
|
|
138 |
* @param {Number} id The competency framework id.
|
|
|
139 |
* @param {String} shortname The framework shortname
|
|
|
140 |
* @param {String} search The current search string
|
|
|
141 |
* @param {String} selector The selector for the tree div
|
|
|
142 |
* @param {Boolean} canmanage Can manage the competencies
|
|
|
143 |
* @param {Number} competencyid The id of the competency to show first
|
|
|
144 |
*/
|
|
|
145 |
init: function(id, shortname, search, selector, canmanage, competencyid) {
|
|
|
146 |
competencyFrameworkId = id;
|
|
|
147 |
competencyFrameworkShortName = shortname;
|
|
|
148 |
competencyFramworkCanManage = canmanage;
|
|
|
149 |
treeSelector = selector;
|
|
|
150 |
loadCompetencies(search).fail(notification.exception);
|
|
|
151 |
if (competencyid > 0) {
|
|
|
152 |
currentNodeId = competencyid;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
this.on('selectionchanged', rememberCurrent);
|
|
|
156 |
},
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
* Add an event handler for custom events emitted by the tree.
|
|
|
160 |
*
|
|
|
161 |
* @param {String} eventname The name of the event - only "selectionchanged" for now
|
|
|
162 |
* @param {Function} handler The handler for the event.
|
|
|
163 |
*/
|
|
|
164 |
on: function(eventname, handler) {
|
|
|
165 |
// We can't use the tree on function directly
|
|
|
166 |
// because the tree gets rebuilt whenever the search string changes,
|
|
|
167 |
// instead we attach the listner to the root node of the tree which never
|
|
|
168 |
// gets destroyed (same as "on()" code in the tree.js).
|
|
|
169 |
$(treeSelector).on(eventname, handler);
|
|
|
170 |
},
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Get the children of a competency.
|
|
|
174 |
*
|
|
|
175 |
* @param {Number} id The competency ID.
|
|
|
176 |
* @return {Array}
|
|
|
177 |
* @method getChildren
|
|
|
178 |
*/
|
|
|
179 |
getChildren: function(id) {
|
|
|
180 |
var children = [];
|
|
|
181 |
$.each(competencies, function(index, competency) {
|
|
|
182 |
if (competency.parentid == id) {
|
|
|
183 |
children.push(competency);
|
|
|
184 |
}
|
|
|
185 |
});
|
|
|
186 |
return children;
|
|
|
187 |
},
|
|
|
188 |
|
|
|
189 |
/**
|
|
|
190 |
* Get the competency framework id this model was initiliased with.
|
|
|
191 |
*
|
|
|
192 |
* @return {Number}
|
|
|
193 |
*/
|
|
|
194 |
getCompetencyFrameworkId: function() {
|
|
|
195 |
return competencyFrameworkId;
|
|
|
196 |
},
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* Get a competency by id
|
|
|
200 |
*
|
|
|
201 |
* @param {Number} id The competency id
|
|
|
202 |
* @return {Object}
|
|
|
203 |
*/
|
|
|
204 |
getCompetency: function(id) {
|
|
|
205 |
return competencies[id];
|
|
|
206 |
},
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
* Get the competency level.
|
|
|
210 |
*
|
|
|
211 |
* @param {Number} id The competency ID.
|
|
|
212 |
* @return {Number}
|
|
|
213 |
*/
|
|
|
214 |
getCompetencyLevel: function(id) {
|
|
|
215 |
var competency = this.getCompetency(id),
|
|
|
216 |
level = competency.path.replace(/^\/|\/$/g, '').split('/').length;
|
|
|
217 |
return level;
|
|
|
218 |
},
|
|
|
219 |
|
|
|
220 |
/**
|
|
|
221 |
* Whether a competency has children.
|
|
|
222 |
*
|
|
|
223 |
* @param {Number} id The competency ID.
|
|
|
224 |
* @return {Boolean}
|
|
|
225 |
* @method hasChildren
|
|
|
226 |
*/
|
|
|
227 |
hasChildren: function(id) {
|
|
|
228 |
return this.getChildren(id).length > 0;
|
|
|
229 |
},
|
|
|
230 |
|
|
|
231 |
/**
|
|
|
232 |
* Does the competency have a rule?
|
|
|
233 |
*
|
|
|
234 |
* @param {Number} id The competency ID.
|
|
|
235 |
* @return {Boolean}
|
|
|
236 |
*/
|
|
|
237 |
hasRule: function(id) {
|
|
|
238 |
var comp = this.getCompetency(id);
|
|
|
239 |
if (comp) {
|
|
|
240 |
return comp.ruleoutcome != CompOutcomes.OUTCOME_NONE
|
|
|
241 |
&& comp.ruletype;
|
|
|
242 |
}
|
|
|
243 |
return false;
|
|
|
244 |
},
|
|
|
245 |
|
|
|
246 |
/**
|
|
|
247 |
* Reload all the page competencies framework competencies.
|
|
|
248 |
* @method reloadCompetencies
|
|
|
249 |
* @return {Promise}
|
|
|
250 |
*/
|
|
|
251 |
reloadCompetencies: function() {
|
|
|
252 |
return loadCompetencies('').fail(notification.exception);
|
|
|
253 |
},
|
|
|
254 |
|
|
|
255 |
/**
|
|
|
256 |
* Get all competencies for this framework.
|
|
|
257 |
*
|
|
|
258 |
* @return {Object[]}
|
|
|
259 |
*/
|
|
|
260 |
listCompetencies: function() {
|
|
|
261 |
return competencies;
|
|
|
262 |
},
|
|
|
263 |
|
|
|
264 |
};
|
|
|
265 |
});
|