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 |
* AMD module to handle overriding activity completion status.
|
|
|
18 |
*
|
|
|
19 |
* @module report_progress/completion_override
|
|
|
20 |
* @copyright 2016 onwards Eiz Eddin Al Katrib <eiz@barasoft.co.uk>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
* @since 3.1
|
|
|
23 |
*/
|
|
|
24 |
define(['jquery', 'core/ajax', 'core/str', 'core/modal_save_cancel', 'core/modal_events', 'core/notification',
|
|
|
25 |
'core/custom_interaction_events', 'core/templates', 'core/pending'],
|
|
|
26 |
function($, Ajax, Str, ModalSaveCancel, ModalEvents, Notification, CustomEvents, Templates, Pending) {
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* @var {String} the full name of the current user.
|
|
|
30 |
* @private
|
|
|
31 |
*/
|
|
|
32 |
var userFullName;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* @var {JQuery} JQuery object containing the element (completion link) that was most recently activated.
|
|
|
36 |
* @private
|
|
|
37 |
*/
|
|
|
38 |
var triggerElement;
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Helper function to get the pix icon key based on the completion state.
|
|
|
42 |
* @method getIconDescriptorFromState
|
|
|
43 |
* @param {number} state The current completion state.
|
|
|
44 |
* @param {string} tracking The completion tracking type, either 'manual' or 'auto'.
|
|
|
45 |
* @return {string} the key for the respective icon.
|
|
|
46 |
* @private
|
|
|
47 |
*/
|
|
|
48 |
var getIconKeyFromState = function(state, tracking) {
|
|
|
49 |
return state > 0 ? 'i/completion-' + tracking + '-y-override' : 'i/completion-' + tracking + '-n-override';
|
|
|
50 |
};
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Handles the confirmation of an override change, calling the web service to update it.
|
|
|
54 |
* @method setOverride
|
|
|
55 |
* @param {Object} override the override data
|
|
|
56 |
* @private
|
|
|
57 |
*/
|
|
|
58 |
var setOverride = function(override) {
|
|
|
59 |
const pendingPromise = new Pending('report_progress/compeletion_override/setOverride');
|
|
|
60 |
// Generate a loading spinner while we're working.
|
|
|
61 |
Templates.render('core/loading', {}).then(function(html) {
|
|
|
62 |
// Append the loading spinner to the trigger element.
|
|
|
63 |
triggerElement.append(html);
|
|
|
64 |
|
|
|
65 |
// Update the completion status override.
|
|
|
66 |
return Ajax.call([{
|
|
|
67 |
methodname: 'core_completion_override_activity_completion_status',
|
|
|
68 |
args: override
|
|
|
69 |
}])[0];
|
|
|
70 |
}).then(function(results) {
|
|
|
71 |
var completionState = (results.state > 0) ? 1 : 0;
|
|
|
72 |
|
|
|
73 |
// Now, build the new title string, get the new icon, and update the DOM.
|
|
|
74 |
var tooltipKey = completionState ? 'completion-y-override' : 'completion-n-override';
|
|
|
75 |
Str.get_string(tooltipKey, 'completion', userFullName).then(function(stateString) {
|
|
|
76 |
var params = {
|
|
|
77 |
state: stateString,
|
|
|
78 |
date: '',
|
|
|
79 |
user: triggerElement.attr('data-userfullname'),
|
|
|
80 |
activity: triggerElement.attr('data-activityname')
|
|
|
81 |
};
|
|
|
82 |
return Str.get_string('progress-title', 'completion', params);
|
|
|
83 |
}).then(function(titleString) {
|
|
|
84 |
var completionTracking = triggerElement.attr('data-completiontracking');
|
|
|
85 |
return Templates.renderPix(getIconKeyFromState(completionState, completionTracking), 'core', titleString);
|
|
|
86 |
}).then(function(html) {
|
|
|
87 |
var oppositeState = completionState > 0 ? 0 : 1;
|
|
|
88 |
triggerElement.find('.loading-icon').remove();
|
|
|
89 |
triggerElement.data('changecompl', override.userid + '-' + override.cmid + '-' + oppositeState);
|
|
|
90 |
triggerElement.attr('data-changecompl', override.userid + '-' + override.cmid + '-' + oppositeState);
|
|
|
91 |
triggerElement.children("img").replaceWith(html);
|
|
|
92 |
return;
|
|
|
93 |
}).catch(Notification.exception);
|
|
|
94 |
|
|
|
95 |
return;
|
|
|
96 |
})
|
|
|
97 |
.then(() => {
|
|
|
98 |
pendingPromise.resolve();
|
|
|
99 |
return;
|
|
|
100 |
}).catch(Notification.exception);
|
|
|
101 |
};
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Handler for activation of a completion status button element.
|
|
|
105 |
* @method userConfirm
|
|
|
106 |
* @param {Event} e the CustomEvents event (CustomEvents.events.activate in this case)
|
|
|
107 |
* @param {Object} data an object containing the original event (click, keydown, etc.).
|
|
|
108 |
* @private
|
|
|
109 |
*/
|
|
|
110 |
var userConfirm = function(e, data) {
|
|
|
111 |
data.originalEvent.preventDefault();
|
|
|
112 |
data.originalEvent.stopPropagation();
|
|
|
113 |
e.preventDefault();
|
|
|
114 |
e.stopPropagation();
|
|
|
115 |
|
|
|
116 |
triggerElement = $(e.currentTarget);
|
|
|
117 |
var elemData = triggerElement.data('changecompl').split('-');
|
|
|
118 |
var override = {
|
|
|
119 |
userid: elemData[0],
|
|
|
120 |
cmid: elemData[1],
|
|
|
121 |
newstate: elemData[2]
|
|
|
122 |
};
|
|
|
123 |
var newStateStr = (override.newstate == 1) ? 'completion-y' : 'completion-n';
|
|
|
124 |
|
|
|
125 |
Str.get_strings([
|
|
|
126 |
{key: newStateStr, component: 'completion'}
|
|
|
127 |
]).then(function(strings) {
|
|
|
128 |
return Str.get_strings([
|
|
|
129 |
{key: 'confirm', component: 'moodle'},
|
|
|
130 |
{key: 'areyousureoverridecompletion', component: 'completion', param: strings[0]}
|
|
|
131 |
]);
|
|
|
132 |
}).then(function(strings) {
|
|
|
133 |
// Create a save/cancel modal.
|
|
|
134 |
return ModalSaveCancel.create({
|
|
|
135 |
title: strings[0],
|
|
|
136 |
body: strings[1],
|
|
|
137 |
show: true,
|
|
|
138 |
removeOnClose: true,
|
|
|
139 |
});
|
|
|
140 |
}).then(function(modal) {
|
|
|
141 |
// Now set up the handlers for the confirmation or cancellation of the modal, and show it.
|
|
|
142 |
|
|
|
143 |
// Confirmation only.
|
|
|
144 |
modal.getRoot().on(ModalEvents.save, function() {
|
|
|
145 |
setOverride(override);
|
|
|
146 |
});
|
|
|
147 |
|
|
|
148 |
return modal;
|
|
|
149 |
}).catch(Notification.exception);
|
|
|
150 |
};
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Init this module which allows activity completion state to be changed via ajax.
|
|
|
154 |
* @method init
|
|
|
155 |
* @param {string} fullName The current user's full name.
|
|
|
156 |
* @private
|
|
|
157 |
*/
|
|
|
158 |
var init = function(fullName) {
|
|
|
159 |
userFullName = fullName;
|
|
|
160 |
|
|
|
161 |
// Register the click, space and enter events as activators for the trigger element.
|
|
|
162 |
$('#completion-progress a.changecompl').each(function(index, element) {
|
|
|
163 |
CustomEvents.define(element, [CustomEvents.events.activate]);
|
|
|
164 |
});
|
|
|
165 |
|
|
|
166 |
// Set the handler on the parent element (the table), but filter so the callback is only called for <a> type children
|
|
|
167 |
// having the '.changecompl' class. The <a> element can then be accessed in the callback via e.currentTarget.
|
|
|
168 |
$('#completion-progress').on(CustomEvents.events.activate, "a.changecompl", function(e, data) {
|
|
|
169 |
userConfirm(e, data);
|
|
|
170 |
});
|
|
|
171 |
};
|
|
|
172 |
|
|
|
173 |
return /** @alias module:report_progress/completion_override */ {
|
|
|
174 |
init: init
|
|
|
175 |
};
|
|
|
176 |
});
|