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 |
* Javascript controller for the "Grading" panel at the right of the page.
|
|
|
18 |
*
|
|
|
19 |
* @module mod_assign/grading_panel
|
|
|
20 |
* @copyright 2016 Damyon Wiese <damyon@moodle.com>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
* @since 3.1
|
|
|
23 |
*/
|
|
|
24 |
define([
|
|
|
25 |
'jquery',
|
|
|
26 |
'core/yui',
|
|
|
27 |
'core/notification',
|
|
|
28 |
'core/templates',
|
|
|
29 |
'core/fragment',
|
|
|
30 |
'core/ajax',
|
|
|
31 |
'core/str',
|
|
|
32 |
'mod_assign/grading_form_change_checker',
|
|
|
33 |
'mod_assign/grading_events',
|
|
|
34 |
'core_form/events',
|
|
|
35 |
'core/toast',
|
|
|
36 |
'core_form/changechecker',
|
|
|
37 |
], function(
|
|
|
38 |
$,
|
|
|
39 |
Y,
|
|
|
40 |
notification,
|
|
|
41 |
templates,
|
|
|
42 |
fragment,
|
|
|
43 |
ajax,
|
|
|
44 |
str,
|
|
|
45 |
checker,
|
|
|
46 |
GradingEvents,
|
|
|
47 |
FormEvents,
|
|
|
48 |
Toast,
|
|
|
49 |
FormChangeChecker
|
|
|
50 |
) {
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* GradingPanel class.
|
|
|
54 |
*
|
|
|
55 |
* @class mod_assign/grading_panel
|
|
|
56 |
* @param {String} selector The selector for the page region containing the user navigation.
|
|
|
57 |
*/
|
|
|
58 |
var GradingPanel = function(selector) {
|
|
|
59 |
this._regionSelector = selector;
|
|
|
60 |
this._region = $(selector);
|
|
|
61 |
this._userCache = [];
|
|
|
62 |
|
|
|
63 |
this.registerEventListeners();
|
|
|
64 |
};
|
|
|
65 |
|
|
|
66 |
/** @property {String} Selector for the page region containing the user navigation. */
|
|
|
67 |
GradingPanel.prototype._regionSelector = null;
|
|
|
68 |
|
|
|
69 |
/** @property {Integer} Remember the last user id to prevent unnessecary reloads. */
|
|
|
70 |
GradingPanel.prototype._lastUserId = 0;
|
|
|
71 |
|
|
|
72 |
/** @property {Integer} Remember the last attempt number to prevent unnessecary reloads. */
|
|
|
73 |
GradingPanel.prototype._lastAttemptNumber = -1;
|
|
|
74 |
|
|
|
75 |
/** @property {JQuery} JQuery node for the page region containing the user navigation. */
|
|
|
76 |
GradingPanel.prototype._region = null;
|
|
|
77 |
|
|
|
78 |
/** @property {Integer} The id of the next user in the grading list */
|
|
|
79 |
GradingPanel.prototype.nextUserId = null;
|
|
|
80 |
|
|
|
81 |
/** @property {Boolean} Next user exists in the grading list */
|
|
|
82 |
GradingPanel.prototype.nextUser = false;
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Fade the dom node out, update it, and fade it back.
|
|
|
86 |
*
|
|
|
87 |
* @private
|
|
|
88 |
* @method _niceReplaceNodeContents
|
|
|
89 |
* @param {JQuery} node
|
|
|
90 |
* @param {String} html
|
|
|
91 |
* @param {String} js
|
|
|
92 |
* @return {Deferred} promise resolved when the animations are complete.
|
|
|
93 |
*/
|
|
|
94 |
GradingPanel.prototype._niceReplaceNodeContents = function(node, html, js) {
|
|
|
95 |
var promise = $.Deferred();
|
|
|
96 |
|
|
|
97 |
node.fadeOut("fast", function() {
|
|
|
98 |
templates.replaceNodeContents(node, html, js);
|
|
|
99 |
node.fadeIn("fast", function() {
|
|
|
100 |
promise.resolve();
|
|
|
101 |
});
|
|
|
102 |
});
|
|
|
103 |
|
|
|
104 |
return promise.promise();
|
|
|
105 |
};
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Make sure all form fields have the latest saved state.
|
|
|
109 |
* @private
|
|
|
110 |
* @method _saveFormState
|
|
|
111 |
*/
|
|
|
112 |
GradingPanel.prototype._saveFormState = function() {
|
|
|
113 |
// Copy data from notify students checkbox which was moved out of the form.
|
|
|
114 |
var checked = $('[data-region="grading-actions-form"] [name="sendstudentnotifications"]').prop("checked");
|
|
|
115 |
$('.gradeform [name="sendstudentnotifications"]').val(checked);
|
|
|
116 |
};
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Make form submit via ajax.
|
|
|
120 |
*
|
|
|
121 |
* @private
|
|
|
122 |
* @param {Object} event
|
|
|
123 |
* @param {Integer} nextUserId
|
|
|
124 |
* @param {Boolean} nextUser optional. Load next user in the grading list.
|
|
|
125 |
* @method _submitForm
|
|
|
126 |
* @fires event:formSubmittedByJavascript
|
|
|
127 |
*/
|
|
|
128 |
GradingPanel.prototype._submitForm = function(event, nextUserId, nextUser) {
|
|
|
129 |
// If the form has data in comment-area, then we need to save that comment
|
|
|
130 |
var commentAreaElement = document.querySelector('.comment-area');
|
|
|
131 |
if (commentAreaElement) {
|
|
|
132 |
var commentTextAreaElement = commentAreaElement.querySelector('.db > textarea');
|
|
|
133 |
if (commentTextAreaElement.value !== '') {
|
|
|
134 |
var commentActionPostElement = commentAreaElement.querySelector('.fd a[id^="comment-action-post-"]');
|
|
|
135 |
commentActionPostElement.click();
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
// The form was submitted - send it via ajax instead.
|
|
|
140 |
var form = $(this._region.find('form.gradeform'));
|
|
|
141 |
|
|
|
142 |
$('[data-region="overlay"]').show();
|
|
|
143 |
|
|
|
144 |
// Mark the form as submitted in the change checker.
|
|
|
145 |
FormChangeChecker.markFormSubmitted(form[0]);
|
|
|
146 |
|
|
|
147 |
// We call this, so other modules can update the form with the latest state.
|
|
|
148 |
form.trigger('save-form-state');
|
|
|
149 |
|
|
|
150 |
// Tell all form fields we are about to submit the form.
|
|
|
151 |
FormEvents.notifyFormSubmittedByJavascript(form[0]);
|
|
|
152 |
|
|
|
153 |
// Now we get all the current values from the form.
|
|
|
154 |
var data = form.serialize();
|
|
|
155 |
var assignmentid = this._region.attr('data-assignmentid');
|
|
|
156 |
|
|
|
157 |
// Now we can continue...
|
|
|
158 |
ajax.call([{
|
|
|
159 |
methodname: 'mod_assign_submit_grading_form',
|
|
|
160 |
args: {assignmentid: assignmentid, userid: this._lastUserId, jsonformdata: JSON.stringify(data)},
|
|
|
161 |
done: this._handleFormSubmissionResponse.bind(this, data, nextUserId, nextUser),
|
|
|
162 |
fail: notification.exception
|
|
|
163 |
}]);
|
|
|
164 |
};
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Handle form submission response.
|
|
|
168 |
*
|
|
|
169 |
* @private
|
|
|
170 |
* @method _handleFormSubmissionResponse
|
|
|
171 |
* @param {Array} formdata - submitted values
|
|
|
172 |
* @param {Number} [nextUserId] The id of the user to load after the form is saved
|
|
|
173 |
* @param {Boolean} [nextUser] - Whether to switch to next user in the grading list.
|
|
|
174 |
* @param {Array} response List of errors.
|
|
|
175 |
*/
|
|
|
176 |
GradingPanel.prototype._handleFormSubmissionResponse = function(formdata, nextUserId, nextUser, response) {
|
|
|
177 |
if (typeof nextUserId === "undefined") {
|
|
|
178 |
nextUserId = this._lastUserId;
|
|
|
179 |
}
|
|
|
180 |
if (response.length) {
|
|
|
181 |
// There was an error saving the grade. Re-render the form using the submitted data so we can show
|
|
|
182 |
// validation errors.
|
|
|
183 |
$(document).trigger('reset', [this._lastUserId, formdata]);
|
|
|
184 |
} else {
|
|
|
185 |
str.get_string('gradechangessaveddetail', 'mod_assign')
|
|
|
186 |
.then(function(str) {
|
|
|
187 |
Toast.add(str);
|
|
|
188 |
return str;
|
|
|
189 |
})
|
|
|
190 |
.catch(notification.exception);
|
|
|
191 |
|
|
|
192 |
// Reset the form state.
|
|
|
193 |
var form = $(this._region.find('form.gradeform'));
|
|
|
194 |
FormChangeChecker.resetFormDirtyState(form[0]);
|
|
|
195 |
|
|
|
196 |
if (nextUserId == this._lastUserId) {
|
|
|
197 |
$(document).trigger('reset', nextUserId);
|
|
|
198 |
} else if (nextUser) {
|
|
|
199 |
$(document).trigger('done-saving-show-next', true);
|
|
|
200 |
} else {
|
|
|
201 |
$(document).trigger('user-changed', nextUserId);
|
|
|
202 |
}
|
|
|
203 |
}
|
|
|
204 |
$('[data-region="overlay"]').hide();
|
|
|
205 |
};
|
|
|
206 |
|
|
|
207 |
/**
|
|
|
208 |
* Refresh form with default values.
|
|
|
209 |
*
|
|
|
210 |
* @private
|
|
|
211 |
* @method _resetForm
|
|
|
212 |
* @param {Event} e
|
|
|
213 |
* @param {Integer} userid
|
|
|
214 |
* @param {Array} formdata
|
|
|
215 |
*/
|
|
|
216 |
GradingPanel.prototype._resetForm = function(e, userid, formdata) {
|
|
|
217 |
// The form was cancelled - refresh with default values.
|
|
|
218 |
var event = $.Event("custom");
|
|
|
219 |
if (typeof userid == "undefined") {
|
|
|
220 |
userid = this._lastUserId;
|
|
|
221 |
}
|
|
|
222 |
this._lastUserId = 0;
|
|
|
223 |
this._refreshGradingPanel(event, userid, formdata);
|
|
|
224 |
};
|
|
|
225 |
|
|
|
226 |
/**
|
|
|
227 |
* Open a picker to choose an older attempt.
|
|
|
228 |
*
|
|
|
229 |
* @private
|
|
|
230 |
* @param {Object} e
|
|
|
231 |
* @method _chooseAttempt
|
|
|
232 |
*/
|
|
|
233 |
GradingPanel.prototype._chooseAttempt = function(e) {
|
|
|
234 |
// Show a dialog.
|
|
|
235 |
|
|
|
236 |
// The form is in the element pointed to by data-submissions.
|
|
|
237 |
var link = $(e.target);
|
|
|
238 |
var submissionsId = link.data('submissions');
|
|
|
239 |
var submissionsform = $(document.getElementById(submissionsId));
|
|
|
240 |
var formcopy = submissionsform.clone();
|
|
|
241 |
var formhtml = formcopy.wrap($('<form/>')).html();
|
|
|
242 |
|
|
|
243 |
str.get_strings([
|
|
|
244 |
{key: 'viewadifferentattempt', component: 'mod_assign'},
|
|
|
245 |
{key: 'view', component: 'core'},
|
|
|
246 |
{key: 'cancel', component: 'core'},
|
|
|
247 |
]).done(function(strs) {
|
|
|
248 |
notification.confirm(strs[0], formhtml, strs[1], strs[2], function() {
|
|
|
249 |
var attemptnumber = $("input:radio[name='select-attemptnumber']:checked").val();
|
|
|
250 |
|
|
|
251 |
this._refreshGradingPanel(null, this._lastUserId, '', attemptnumber);
|
|
|
252 |
}.bind(this));
|
|
|
253 |
}.bind(this)).fail(notification.exception);
|
|
|
254 |
};
|
|
|
255 |
|
|
|
256 |
/**
|
|
|
257 |
* Add popout buttons
|
|
|
258 |
*
|
|
|
259 |
* @private
|
|
|
260 |
* @method _addPopoutButtons
|
|
|
261 |
* @param {JQuery} selector The region selector to add popout buttons to.
|
|
|
262 |
*/
|
|
|
263 |
GradingPanel.prototype._addPopoutButtons = function(selector) {
|
|
|
264 |
var region = $(selector);
|
|
|
265 |
|
|
|
266 |
templates.render('mod_assign/popout_button', {}).done(function(html) {
|
|
|
267 |
var parents = region.find('[data-fieldtype="filemanager"],[data-fieldtype="editor"],[data-fieldtype="grading"]')
|
|
|
268 |
.closest('.fitem');
|
|
|
269 |
parents.addClass('has-popout').find('label').parent().append(html);
|
|
|
270 |
|
|
|
271 |
region.on('click', '[data-region="popout-button"]', this._togglePopout.bind(this));
|
|
|
272 |
}.bind(this)).fail(notification.exception);
|
|
|
273 |
};
|
|
|
274 |
|
|
|
275 |
/**
|
|
|
276 |
* Make a div "popout" or "popback".
|
|
|
277 |
*
|
|
|
278 |
* @private
|
|
|
279 |
* @method _togglePopout
|
|
|
280 |
* @param {Event} event
|
|
|
281 |
*/
|
|
|
282 |
GradingPanel.prototype._togglePopout = function(event) {
|
|
|
283 |
event.preventDefault();
|
|
|
284 |
var container = $(event.target).closest('.fitem');
|
|
|
285 |
if (container.hasClass('popout')) {
|
|
|
286 |
$('.popout').removeClass('popout');
|
|
|
287 |
} else {
|
|
|
288 |
$('.popout').removeClass('popout');
|
|
|
289 |
container.addClass('popout');
|
|
|
290 |
container.addClass('moodle-has-zindex');
|
|
|
291 |
}
|
|
|
292 |
};
|
|
|
293 |
|
|
|
294 |
/**
|
|
|
295 |
* Get the user context - re-render the template in the page.
|
|
|
296 |
*
|
|
|
297 |
* @private
|
|
|
298 |
* @method _refreshGradingPanel
|
|
|
299 |
* @param {Event} event
|
|
|
300 |
* @param {Number} userid
|
|
|
301 |
* @param {String} submissiondata serialised submission data.
|
|
|
302 |
* @param {Integer} attemptnumber
|
|
|
303 |
*/
|
|
|
304 |
GradingPanel.prototype._refreshGradingPanel = function(event, userid, submissiondata, attemptnumber) {
|
|
|
305 |
var contextid = this._region.attr('data-contextid');
|
|
|
306 |
if (typeof submissiondata === 'undefined') {
|
|
|
307 |
submissiondata = '';
|
|
|
308 |
}
|
|
|
309 |
if (typeof attemptnumber === 'undefined') {
|
|
|
310 |
attemptnumber = -1;
|
|
|
311 |
}
|
|
|
312 |
// Skip reloading if it is the same user.
|
|
|
313 |
if (this._lastUserId == userid && this._lastAttemptNumber == attemptnumber && submissiondata === '') {
|
|
|
314 |
return;
|
|
|
315 |
}
|
|
|
316 |
this._lastUserId = userid;
|
|
|
317 |
this._lastAttemptNumber = attemptnumber;
|
|
|
318 |
$(document).trigger('start-loading-user');
|
|
|
319 |
// Tell behat to back off too.
|
|
|
320 |
window.M.util.js_pending('mod-assign-loading-user');
|
|
|
321 |
// First insert the loading template.
|
|
|
322 |
templates.render('mod_assign/loading', {}).done(function(html, js) {
|
|
|
323 |
// Update the page.
|
|
|
324 |
this._niceReplaceNodeContents(this._region, html, js).done(function() {
|
|
|
325 |
if (userid > 0) {
|
|
|
326 |
this._region.show();
|
|
|
327 |
// Reload the grading form "fragment" for this user.
|
|
|
328 |
var params = {userid: userid, attemptnumber: attemptnumber, jsonformdata: JSON.stringify(submissiondata)};
|
|
|
329 |
fragment.loadFragment('mod_assign', 'gradingpanel', contextid, params).done(function(html, js) {
|
|
|
330 |
this._niceReplaceNodeContents(this._region, html, js)
|
|
|
331 |
.done(function() {
|
|
|
332 |
checker.saveFormState('[data-region="grade-panel"] .gradeform');
|
|
|
333 |
$(document).on('editor-content-restored', function() {
|
|
|
334 |
// If the editor has some content that has been restored
|
|
|
335 |
// then save the form state again for comparison.
|
|
|
336 |
checker.saveFormState('[data-region="grade-panel"] .gradeform');
|
|
|
337 |
});
|
|
|
338 |
$('[data-region="attempt-chooser"]').on('click', this._chooseAttempt.bind(this));
|
|
|
339 |
this._addPopoutButtons('[data-region="grade-panel"] .gradeform');
|
|
|
340 |
$(document).trigger('finish-loading-user');
|
|
|
341 |
// Tell behat we are friends again.
|
|
|
342 |
window.M.util.js_complete('mod-assign-loading-user');
|
|
|
343 |
}.bind(this))
|
|
|
344 |
.fail(notification.exception);
|
|
|
345 |
}.bind(this)).fail(notification.exception);
|
|
|
346 |
$('[data-region="review-panel"]').show();
|
|
|
347 |
} else {
|
|
|
348 |
this._region.hide();
|
|
|
349 |
$('[data-region="review-panel"]').hide();
|
|
|
350 |
$(document).trigger('finish-loading-user');
|
|
|
351 |
// Tell behat we are friends again.
|
|
|
352 |
window.M.util.js_complete('mod-assign-loading-user');
|
|
|
353 |
}
|
|
|
354 |
}.bind(this));
|
|
|
355 |
}.bind(this)).fail(notification.exception);
|
|
|
356 |
};
|
|
|
357 |
|
|
|
358 |
/**
|
|
|
359 |
* Get next user data and store it in global variables
|
|
|
360 |
*
|
|
|
361 |
* @private
|
|
|
362 |
* @method _getNextUser
|
|
|
363 |
* @param {Event} event
|
|
|
364 |
* @param {Object} data Next user's data
|
|
|
365 |
*/
|
|
|
366 |
GradingPanel.prototype._getNextUser = function(event, data) {
|
|
|
367 |
this.nextUserId = data.nextUserId;
|
|
|
368 |
this.nextUser = data.nextUser;
|
|
|
369 |
};
|
|
|
370 |
|
|
|
371 |
/**
|
|
|
372 |
* Handle the save-and-show-next event
|
|
|
373 |
*
|
|
|
374 |
* @private
|
|
|
375 |
* @method _handleSaveAndShowNext
|
|
|
376 |
*/
|
|
|
377 |
GradingPanel.prototype._handleSaveAndShowNext = function() {
|
|
|
378 |
this._submitForm(null, this.nextUserId, this.nextUser);
|
|
|
379 |
};
|
|
|
380 |
|
|
|
381 |
/**
|
|
|
382 |
* Get the grade panel element.
|
|
|
383 |
*
|
|
|
384 |
* @method getPanelElement
|
|
|
385 |
* @return {jQuery}
|
|
|
386 |
*/
|
|
|
387 |
GradingPanel.prototype.getPanelElement = function() {
|
|
|
388 |
return $('[data-region="grade-panel"]');
|
|
|
389 |
};
|
|
|
390 |
|
|
|
391 |
/**
|
|
|
392 |
* Hide the grade panel.
|
|
|
393 |
*
|
|
|
394 |
* @method collapsePanel
|
|
|
395 |
*/
|
|
|
396 |
GradingPanel.prototype.collapsePanel = function() {
|
|
|
397 |
this.getPanelElement().addClass('collapsed');
|
|
|
398 |
};
|
|
|
399 |
|
|
|
400 |
/**
|
|
|
401 |
* Show the grade panel.
|
|
|
402 |
*
|
|
|
403 |
* @method expandPanel
|
|
|
404 |
*/
|
|
|
405 |
GradingPanel.prototype.expandPanel = function() {
|
|
|
406 |
this.getPanelElement().removeClass('collapsed');
|
|
|
407 |
};
|
|
|
408 |
|
|
|
409 |
/**
|
|
|
410 |
* Register event listeners for the grade panel.
|
|
|
411 |
*
|
|
|
412 |
* @method registerEventListeners
|
|
|
413 |
*/
|
|
|
414 |
GradingPanel.prototype.registerEventListeners = function() {
|
|
|
415 |
var docElement = $(document);
|
|
|
416 |
var region = $(this._region);
|
|
|
417 |
// Add an event listener to prevent form submission when pressing enter key.
|
|
|
418 |
region.on('submit', 'form', function(e) {
|
|
|
419 |
e.preventDefault();
|
|
|
420 |
});
|
|
|
421 |
|
|
|
422 |
docElement.on('next-user', this._getNextUser.bind(this));
|
|
|
423 |
docElement.on('user-changed', this._refreshGradingPanel.bind(this));
|
|
|
424 |
docElement.on('save-changes', this._submitForm.bind(this));
|
|
|
425 |
docElement.on('save-and-show-next', this._handleSaveAndShowNext.bind(this));
|
|
|
426 |
docElement.on('reset', this._resetForm.bind(this));
|
|
|
427 |
|
|
|
428 |
docElement.on('save-form-state', this._saveFormState.bind(this));
|
|
|
429 |
|
|
|
430 |
docElement.on(GradingEvents.COLLAPSE_GRADE_PANEL, function() {
|
|
|
431 |
this.collapsePanel();
|
|
|
432 |
}.bind(this));
|
|
|
433 |
|
|
|
434 |
// We should expand if the review panel is collapsed.
|
|
|
435 |
docElement.on(GradingEvents.COLLAPSE_REVIEW_PANEL, function() {
|
|
|
436 |
this.expandPanel();
|
|
|
437 |
}.bind(this));
|
|
|
438 |
|
|
|
439 |
docElement.on(GradingEvents.EXPAND_GRADE_PANEL, function() {
|
|
|
440 |
this.expandPanel();
|
|
|
441 |
}.bind(this));
|
|
|
442 |
};
|
|
|
443 |
|
|
|
444 |
return GradingPanel;
|
|
|
445 |
});
|