| 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 |
* Competency picker from user plans.
|
|
|
18 |
*
|
|
|
19 |
* To handle 'save' events use: picker.on('save').
|
|
|
20 |
*
|
|
|
21 |
* This will receive a object with either a single 'competencyId', or an array in 'competencyIds'
|
|
|
22 |
* depending on the value of multiSelect.
|
|
|
23 |
*
|
|
|
24 |
* @module tool_lp/competencypicker_user_plans
|
|
|
25 |
* @copyright 2015 Frédéric Massart - FMCorz.net
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
|
|
|
29 |
define(['jquery',
|
|
|
30 |
'core/notification',
|
|
|
31 |
'core/ajax',
|
|
|
32 |
'core/templates',
|
|
|
33 |
'core/str',
|
|
|
34 |
'tool_lp/tree',
|
|
|
35 |
'tool_lp/competencypicker'
|
|
|
36 |
],
|
|
|
37 |
function($, Notification, Ajax, Templates, Str, Tree, PickerBase) {
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Competency picker in plan class.
|
|
|
41 |
*
|
|
|
42 |
* @class tool_lp/competencypicker_user_plans
|
|
|
43 |
* @param {Number} userId
|
|
|
44 |
* @param {Number|false} singlePlan The ID of the plan when limited to one.
|
|
|
45 |
* @param {Boolean} multiSelect Support multi-select in the tree.
|
|
|
46 |
*/
|
|
|
47 |
var Picker = function(userId, singlePlan, multiSelect) {
|
|
|
48 |
PickerBase.prototype.constructor.apply(this, [1, false, 'self', multiSelect]);
|
|
|
49 |
this._userId = userId;
|
|
|
50 |
this._plans = [];
|
|
|
51 |
|
|
|
52 |
if (singlePlan) {
|
|
|
53 |
this._planId = singlePlan;
|
|
|
54 |
this._singlePlan = true;
|
|
|
55 |
}
|
|
|
56 |
};
|
|
|
57 |
Picker.prototype = Object.create(PickerBase.prototype);
|
|
|
58 |
|
|
|
59 |
/** @property {Array} The list of plans fetched. */
|
|
|
60 |
Picker.prototype._plans = null;
|
|
|
61 |
/** @property {Number} The current plan ID. */
|
|
|
62 |
Picker.prototype._planId = null;
|
|
|
63 |
/** @property {Boolean} Whether we can browse plans or not. */
|
|
|
64 |
Picker.prototype._singlePlan = false;
|
|
|
65 |
/** @property {Number} The user the plans belongs to. */
|
|
|
66 |
Picker.prototype._userId = null;
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Hook to executed after the view is rendered.
|
|
|
70 |
*
|
|
|
71 |
* @method _afterRender
|
|
|
72 |
*/
|
|
|
73 |
Picker.prototype._afterRender = function() {
|
|
|
74 |
var self = this;
|
|
|
75 |
PickerBase.prototype._afterRender.apply(self, arguments);
|
|
|
76 |
|
|
|
77 |
// Add listener for framework change.
|
|
|
78 |
if (!self._singlePlan) {
|
|
|
79 |
self._find('[data-action="chooseplan"]').change(function(e) {
|
|
|
80 |
self._planId = $(e.target).val();
|
|
|
81 |
self._loadCompetencies().then(self._refresh.bind(self))
|
|
|
82 |
.catch(Notification.exception);
|
|
|
83 |
});
|
|
|
84 |
}
|
|
|
85 |
};
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Fetch the competencies.
|
|
|
89 |
*
|
|
|
90 |
* @param {Number} planId The planId.
|
|
|
91 |
* @param {String} searchText Limit the competencies to those matching the text.
|
|
|
92 |
* @method _fetchCompetencies
|
|
|
93 |
* @return {Promise} The promise object.
|
|
|
94 |
*/
|
|
|
95 |
Picker.prototype._fetchCompetencies = function(planId, searchText) {
|
|
|
96 |
var self = this;
|
|
|
97 |
|
|
|
98 |
return Ajax.call([
|
|
|
99 |
{methodname: 'core_competency_list_plan_competencies', args: {
|
|
|
100 |
id: planId
|
|
|
101 |
}}
|
|
|
102 |
])[0].done(function(competencies) {
|
|
|
103 |
|
|
|
104 |
// Expand the list of competencies into a fake tree.
|
|
|
105 |
var i, comp;
|
|
|
106 |
var tree = [];
|
|
|
107 |
for (i = 0; i < competencies.length; i++) {
|
|
|
108 |
comp = competencies[i].competency;
|
|
|
109 |
if (comp.shortname.toLowerCase().indexOf(searchText.toLowerCase()) < 0) {
|
|
|
110 |
continue;
|
|
|
111 |
}
|
|
|
112 |
comp.children = [];
|
|
|
113 |
comp.haschildren = 0;
|
|
|
114 |
tree.push(comp);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
self._competencies = tree;
|
|
|
118 |
|
|
|
119 |
}).fail(Notification.exception);
|
|
|
120 |
};
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Convenience method to get a plan object.
|
|
|
124 |
*
|
|
|
125 |
* @param {Number} id The plan ID.
|
|
|
126 |
* @return {Object|undefined} The plan.
|
|
|
127 |
* @method _getPlan
|
|
|
128 |
*/
|
|
|
129 |
Picker.prototype._getPlan = function(id) {
|
|
|
130 |
var plan;
|
|
|
131 |
$.each(this._plans, function(i, f) {
|
|
|
132 |
if (f.id == id) {
|
|
|
133 |
plan = f;
|
|
|
134 |
return;
|
|
|
135 |
}
|
|
|
136 |
});
|
|
|
137 |
return plan;
|
|
|
138 |
};
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Load the competencies.
|
|
|
142 |
*
|
|
|
143 |
* @method _loadCompetencies
|
|
|
144 |
* @return {Promise}
|
|
|
145 |
*/
|
|
|
146 |
Picker.prototype._loadCompetencies = function() {
|
|
|
147 |
return this._fetchCompetencies(this._planId, this._searchText);
|
|
|
148 |
};
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Load the plans.
|
|
|
152 |
*
|
|
|
153 |
* @method _loadPlans
|
|
|
154 |
* @return {Promise}
|
|
|
155 |
*/
|
|
|
156 |
Picker.prototype._loadPlans = function() {
|
|
|
157 |
var promise,
|
|
|
158 |
self = this;
|
|
|
159 |
|
|
|
160 |
// Quit early because we already have the data.
|
|
|
161 |
if (self._plans.length > 0) {
|
|
|
162 |
return $.when();
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
if (self._singlePlan) {
|
|
|
166 |
promise = Ajax.call([
|
|
|
167 |
{methodname: 'core_competency_read_plan', args: {
|
|
|
168 |
id: this._planId
|
|
|
169 |
}}
|
|
|
170 |
])[0].then(function(plan) {
|
|
|
171 |
return [plan];
|
|
|
172 |
});
|
|
|
173 |
} else {
|
|
|
174 |
promise = Ajax.call([
|
|
|
175 |
{methodname: 'core_competency_list_user_plans', args: {
|
|
|
176 |
userid: self._userId
|
|
|
177 |
}}
|
|
|
178 |
])[0];
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
return promise.done(function(plans) {
|
|
|
182 |
self._plans = plans;
|
|
|
183 |
}).fail(Notification.exception);
|
|
|
184 |
};
|
|
|
185 |
|
|
|
186 |
/**
|
|
|
187 |
* Hook to executed before render.
|
|
|
188 |
*
|
|
|
189 |
* @method _preRender
|
|
|
190 |
* @return {Promise}
|
|
|
191 |
*/
|
|
|
192 |
Picker.prototype._preRender = function() {
|
|
|
193 |
var self = this;
|
|
|
194 |
return self._loadPlans().then(function() {
|
|
|
195 |
if (!self._planId && self._plans.length > 0) {
|
|
|
196 |
self._planId = self._plans[0].id;
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
// We could not set a framework ID, that probably means there are no frameworks accessible.
|
|
|
200 |
if (!self._planId) {
|
|
|
201 |
self._plans = [];
|
|
|
202 |
return $.when();
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
return self._loadCompetencies();
|
|
|
206 |
});
|
|
|
207 |
};
|
|
|
208 |
|
|
|
209 |
/**
|
|
|
210 |
* Render the dialogue.
|
|
|
211 |
*
|
|
|
212 |
* @method _render
|
|
|
213 |
* @return {Promise}
|
|
|
214 |
*/
|
|
|
215 |
Picker.prototype._render = function() {
|
|
|
216 |
var self = this;
|
|
|
217 |
return self._preRender().then(function() {
|
|
|
218 |
|
|
|
219 |
if (!self._singlePlan) {
|
|
|
220 |
$.each(self._plans, function(i, plan) {
|
|
|
221 |
if (plan.id == self._planId) {
|
|
|
222 |
plan.selected = true;
|
|
|
223 |
} else {
|
|
|
224 |
plan.selected = false;
|
|
|
225 |
}
|
|
|
226 |
});
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
var context = {
|
|
|
230 |
competencies: self._competencies,
|
|
|
231 |
plan: self._getPlan(self._planId),
|
|
|
232 |
plans: self._plans,
|
|
|
233 |
search: self._searchText,
|
|
|
234 |
singlePlan: self._singlePlan,
|
|
|
235 |
};
|
|
|
236 |
|
|
|
237 |
return Templates.render('tool_lp/competency_picker_user_plans', context);
|
|
|
238 |
});
|
|
|
239 |
};
|
|
|
240 |
|
|
|
241 |
return Picker;
|
|
|
242 |
});
|