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 |
* User competency workflow.
|
|
|
18 |
*
|
|
|
19 |
* @module tool_lp/user_competency_workflow
|
|
|
20 |
* @copyright 2015 Frédéric Massart - FMCorz.net
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
define(['jquery',
|
|
|
24 |
'core/templates',
|
|
|
25 |
'core/ajax',
|
|
|
26 |
'core/notification',
|
|
|
27 |
'core/str',
|
|
|
28 |
'tool_lp/menubar',
|
|
|
29 |
'tool_lp/event_base'],
|
|
|
30 |
function($, Templates, Ajax, Notification, Str, Menubar, EventBase) {
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* UserCompetencyWorkflow class.
|
|
|
34 |
*/
|
|
|
35 |
var UserCompetencyWorkflow = function() {
|
|
|
36 |
EventBase.prototype.constructor.apply(this, []);
|
|
|
37 |
};
|
|
|
38 |
UserCompetencyWorkflow.prototype = Object.create(EventBase.prototype);
|
|
|
39 |
|
|
|
40 |
/** @property {String} The selector to find the user competency data. */
|
|
|
41 |
UserCompetencyWorkflow.prototype._nodeSelector = '[data-node="user-competency"]';
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Cancel a review request and refresh the view.
|
|
|
45 |
*
|
|
|
46 |
* @param {Object} data The user competency data.
|
|
|
47 |
* @method _cancelReviewRequest
|
|
|
48 |
*/
|
|
|
49 |
UserCompetencyWorkflow.prototype._cancelReviewRequest = function(data) {
|
|
|
50 |
var call = {
|
|
|
51 |
methodname: 'core_competency_user_competency_cancel_review_request',
|
|
|
52 |
args: {
|
|
|
53 |
userid: data.userid,
|
|
|
54 |
competencyid: data.competencyid
|
|
|
55 |
}
|
|
|
56 |
};
|
|
|
57 |
|
|
|
58 |
Ajax.call([call])[0].then(function() {
|
|
|
59 |
this._trigger('review-request-cancelled', data);
|
|
|
60 |
this._trigger('status-changed', data);
|
|
|
61 |
}.bind(this)).catch(function() {
|
|
|
62 |
this._trigger('error-occured', data);
|
|
|
63 |
}.bind(this));
|
|
|
64 |
};
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Cancel a review request an refresh the view.
|
|
|
68 |
*
|
|
|
69 |
* @param {Object} data The user competency data.
|
|
|
70 |
* @method cancelReviewRequest
|
|
|
71 |
*/
|
|
|
72 |
UserCompetencyWorkflow.prototype.cancelReviewRequest = function(data) {
|
|
|
73 |
this._cancelReviewRequest(data);
|
|
|
74 |
};
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Cancel a review request handler.
|
|
|
78 |
*
|
|
|
79 |
* @param {Event} e The event.
|
|
|
80 |
* @method _cancelReviewRequestHandler
|
|
|
81 |
*/
|
|
|
82 |
UserCompetencyWorkflow.prototype._cancelReviewRequestHandler = function(e) {
|
|
|
83 |
e.preventDefault();
|
|
|
84 |
var data = this._findUserCompetencyData($(e.target));
|
|
|
85 |
this.cancelReviewRequest(data);
|
|
|
86 |
};
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Request a review and refresh the view.
|
|
|
90 |
*
|
|
|
91 |
* @param {Object} data The user competency data.
|
|
|
92 |
* @method _requestReview
|
|
|
93 |
*/
|
|
|
94 |
UserCompetencyWorkflow.prototype._requestReview = function(data) {
|
|
|
95 |
var call = {
|
|
|
96 |
methodname: 'core_competency_user_competency_request_review',
|
|
|
97 |
args: {
|
|
|
98 |
userid: data.userid,
|
|
|
99 |
competencyid: data.competencyid
|
|
|
100 |
}
|
|
|
101 |
};
|
|
|
102 |
|
|
|
103 |
Ajax.call([call])[0].then(function() {
|
|
|
104 |
this._trigger('review-requested', data);
|
|
|
105 |
this._trigger('status-changed', data);
|
|
|
106 |
}.bind(this)).catch(function() {
|
|
|
107 |
this._trigger('error-occured', data);
|
|
|
108 |
}.bind(this));
|
|
|
109 |
};
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Request a review.
|
|
|
113 |
*
|
|
|
114 |
* @param {Object} data The user competency data.
|
|
|
115 |
* @method requestReview
|
|
|
116 |
*/
|
|
|
117 |
UserCompetencyWorkflow.prototype.requestReview = function(data) {
|
|
|
118 |
this._requestReview(data);
|
|
|
119 |
};
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Request a review handler.
|
|
|
123 |
*
|
|
|
124 |
* @param {Event} e The event.
|
|
|
125 |
* @method _requestReviewHandler
|
|
|
126 |
*/
|
|
|
127 |
UserCompetencyWorkflow.prototype._requestReviewHandler = function(e) {
|
|
|
128 |
e.preventDefault();
|
|
|
129 |
var data = this._findUserCompetencyData($(e.target));
|
|
|
130 |
this.requestReview(data);
|
|
|
131 |
};
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* Start a review and refresh the view.
|
|
|
135 |
*
|
|
|
136 |
* @param {Object} data The user competency data.
|
|
|
137 |
* @method _startReview
|
|
|
138 |
*/
|
|
|
139 |
UserCompetencyWorkflow.prototype._startReview = function(data) {
|
|
|
140 |
var call = {
|
|
|
141 |
methodname: 'core_competency_user_competency_start_review',
|
|
|
142 |
args: {
|
|
|
143 |
userid: data.userid,
|
|
|
144 |
competencyid: data.competencyid
|
|
|
145 |
}
|
|
|
146 |
};
|
|
|
147 |
Ajax.call([call])[0].then(function() {
|
|
|
148 |
this._trigger('review-started', data);
|
|
|
149 |
this._trigger('status-changed', data);
|
|
|
150 |
}.bind(this)).catch(function() {
|
|
|
151 |
this._trigger('error-occured', data);
|
|
|
152 |
}.bind(this));
|
|
|
153 |
};
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Start a review.
|
|
|
157 |
*
|
|
|
158 |
* @param {Object} data The user competency data.
|
|
|
159 |
* @method startReview
|
|
|
160 |
*/
|
|
|
161 |
UserCompetencyWorkflow.prototype.startReview = function(data) {
|
|
|
162 |
this._startReview(data);
|
|
|
163 |
};
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* Start a review handler.
|
|
|
167 |
*
|
|
|
168 |
* @param {Event} e The event.
|
|
|
169 |
* @method _startReviewHandler
|
|
|
170 |
*/
|
|
|
171 |
UserCompetencyWorkflow.prototype._startReviewHandler = function(e) {
|
|
|
172 |
e.preventDefault();
|
|
|
173 |
var data = this._findUserCompetencyData($(e.target));
|
|
|
174 |
this.startReview(data);
|
|
|
175 |
};
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* Stop a review and refresh the view.
|
|
|
179 |
*
|
|
|
180 |
* @param {Object} data The user competency data.
|
|
|
181 |
* @method _stopReview
|
|
|
182 |
*/
|
|
|
183 |
UserCompetencyWorkflow.prototype._stopReview = function(data) {
|
|
|
184 |
var call = {
|
|
|
185 |
methodname: 'core_competency_user_competency_stop_review',
|
|
|
186 |
args: {
|
|
|
187 |
userid: data.userid,
|
|
|
188 |
competencyid: data.competencyid
|
|
|
189 |
}
|
|
|
190 |
};
|
|
|
191 |
|
|
|
192 |
Ajax.call([call])[0].then(function() {
|
|
|
193 |
this._trigger('review-stopped', data);
|
|
|
194 |
this._trigger('status-changed', data);
|
|
|
195 |
}.bind(this)).catch(function() {
|
|
|
196 |
this._trigger('error-occured', data);
|
|
|
197 |
}.bind(this));
|
|
|
198 |
};
|
|
|
199 |
|
|
|
200 |
/**
|
|
|
201 |
* Stop a review.
|
|
|
202 |
*
|
|
|
203 |
* @param {Object} data The user competency data.
|
|
|
204 |
* @method stopReview
|
|
|
205 |
*/
|
|
|
206 |
UserCompetencyWorkflow.prototype.stopReview = function(data) {
|
|
|
207 |
this._stopReview(data);
|
|
|
208 |
};
|
|
|
209 |
|
|
|
210 |
/**
|
|
|
211 |
* Stop a review handler.
|
|
|
212 |
*
|
|
|
213 |
* @param {Event} e The event.
|
|
|
214 |
* @method _stopReviewHandler
|
|
|
215 |
*/
|
|
|
216 |
UserCompetencyWorkflow.prototype._stopReviewHandler = function(e) {
|
|
|
217 |
e.preventDefault();
|
|
|
218 |
var data = this._findUserCompetencyData($(e.target));
|
|
|
219 |
this.stopReview(data);
|
|
|
220 |
};
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* Enhance a menu bar.
|
|
|
224 |
*
|
|
|
225 |
* @param {String} selector Menubar selector.
|
|
|
226 |
*/
|
|
|
227 |
UserCompetencyWorkflow.prototype.enhanceMenubar = function(selector) {
|
|
|
228 |
Menubar.enhance(selector, {
|
|
|
229 |
'[data-action="request-review"]': this._requestReviewHandler.bind(this),
|
|
|
230 |
'[data-action="cancel-review-request"]': this._cancelReviewRequestHandler.bind(this),
|
|
|
231 |
});
|
|
|
232 |
};
|
|
|
233 |
|
|
|
234 |
/**
|
|
|
235 |
* Find the user competency data from a node.
|
|
|
236 |
*
|
|
|
237 |
* @param {Node} node The node to search from.
|
|
|
238 |
* @return {Object} User competency data.
|
|
|
239 |
*/
|
|
|
240 |
UserCompetencyWorkflow.prototype._findUserCompetencyData = function(node) {
|
|
|
241 |
var parent = node.parents(this._nodeSelector),
|
|
|
242 |
data;
|
|
|
243 |
|
|
|
244 |
if (parent.length != 1) {
|
|
|
245 |
throw new Error('The evidence node was not located.');
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
data = parent.data();
|
|
|
249 |
if (typeof data === 'undefined' || typeof data.userid === 'undefined' || typeof data.competencyid === 'undefined') {
|
|
|
250 |
throw new Error('User competency data could not be found.');
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
return data;
|
|
|
254 |
};
|
|
|
255 |
|
|
|
256 |
/**
|
|
|
257 |
* Enhance a menu bar.
|
|
|
258 |
*
|
|
|
259 |
* @param {String} selector Menubar selector.
|
|
|
260 |
*/
|
|
|
261 |
UserCompetencyWorkflow.prototype.enhanceMenubar = function(selector) {
|
|
|
262 |
Menubar.enhance(selector, {
|
|
|
263 |
'[data-action="request-review"]': this._requestReviewHandler.bind(this),
|
|
|
264 |
'[data-action="cancel-review-request"]': this._cancelReviewRequestHandler.bind(this),
|
|
|
265 |
'[data-action="start-review"]': this._startReviewHandler.bind(this),
|
|
|
266 |
'[data-action="stop-review"]': this._stopReviewHandler.bind(this),
|
|
|
267 |
});
|
|
|
268 |
};
|
|
|
269 |
|
|
|
270 |
/**
|
|
|
271 |
* Register the events in the region.
|
|
|
272 |
*
|
|
|
273 |
* @param {String} selector The base selector to search nodes in and attach events.
|
|
|
274 |
*/
|
|
|
275 |
UserCompetencyWorkflow.prototype.registerEvents = function(selector) {
|
|
|
276 |
var wrapper = $(selector);
|
|
|
277 |
|
|
|
278 |
wrapper.find('[data-action="request-review"]').click(this._requestReviewHandler.bind(this));
|
|
|
279 |
wrapper.find('[data-action="cancel-review-request"]').click(this._cancelReviewRequestHandler.bind(this));
|
|
|
280 |
wrapper.find('[data-action="start-review"]').click(this._startReviewHandler.bind(this));
|
|
|
281 |
wrapper.find('[data-action="stop-review"]').click(this._stopReviewHandler.bind(this));
|
|
|
282 |
};
|
|
|
283 |
|
|
|
284 |
return /** @alias module:tool_lp/user_competency_actions */ UserCompetencyWorkflow;
|
|
|
285 |
});
|