1 |
efrain |
1 |
M.gradingform_rubriceditor = {'templates' : {}, 'eventhandler' : null, 'name' : null, 'Y' : null};
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* This function is called for each rubriceditor on page.
|
|
|
5 |
*/
|
|
|
6 |
M.gradingform_rubriceditor.init = function(Y, options) {
|
|
|
7 |
M.gradingform_rubriceditor.name = options.name
|
|
|
8 |
M.gradingform_rubriceditor.Y = Y
|
|
|
9 |
M.gradingform_rubriceditor.templates[options.name] = {
|
|
|
10 |
'criterion' : options.criteriontemplate,
|
|
|
11 |
'level' : options.leveltemplate
|
|
|
12 |
}
|
|
|
13 |
M.gradingform_rubriceditor.disablealleditors()
|
|
|
14 |
Y.on('click', M.gradingform_rubriceditor.clickanywhere, 'body', null)
|
|
|
15 |
YUI().use('event-touch', function (Y) {
|
|
|
16 |
Y.one('body').on('touchstart', M.gradingform_rubriceditor.clickanywhere);
|
|
|
17 |
Y.one('body').on('touchend', M.gradingform_rubriceditor.clickanywhere);
|
|
|
18 |
})
|
|
|
19 |
M.gradingform_rubriceditor.addhandlers()
|
|
|
20 |
};
|
|
|
21 |
|
|
|
22 |
// Adds handlers for clicking submit button. This function must be called each time JS adds new elements to html
|
|
|
23 |
M.gradingform_rubriceditor.addhandlers = function() {
|
|
|
24 |
var Y = M.gradingform_rubriceditor.Y
|
|
|
25 |
var name = M.gradingform_rubriceditor.name
|
|
|
26 |
if (M.gradingform_rubriceditor.eventhandler) M.gradingform_rubriceditor.eventhandler.detach()
|
|
|
27 |
M.gradingform_rubriceditor.eventhandler = Y.on('click', M.gradingform_rubriceditor.buttonclick, '#rubric-'+name+' input[type=submit]', null);
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
// switches all input text elements to non-edit mode
|
|
|
31 |
M.gradingform_rubriceditor.disablealleditors = function() {
|
|
|
32 |
var Y = M.gradingform_rubriceditor.Y
|
|
|
33 |
var name = M.gradingform_rubriceditor.name
|
|
|
34 |
Y.all('#rubric-'+name+' .level').each( function(node) {M.gradingform_rubriceditor.editmode(node, false)} );
|
|
|
35 |
Y.all('#rubric-'+name+' .description').each( function(node) {M.gradingform_rubriceditor.editmode(node, false)} );
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
// function invoked on each click on the page. If level and/or criterion description is clicked
|
|
|
39 |
// it switches this element to edit mode. If rubric button is clicked it does nothing so the 'buttonclick'
|
|
|
40 |
// function is invoked
|
|
|
41 |
M.gradingform_rubriceditor.clickanywhere = function(e) {
|
|
|
42 |
if (e.type == 'touchstart') return
|
|
|
43 |
var el = e.target
|
|
|
44 |
// if clicked on button - disablecurrenteditor, continue
|
|
|
45 |
if (el.get('tagName') == 'INPUT' && el.get('type') == 'submit') {
|
|
|
46 |
return
|
|
|
47 |
}
|
|
|
48 |
// else if clicked on level and this level is not enabled - enable it
|
|
|
49 |
// or if clicked on description and this description is not enabled - enable it
|
|
|
50 |
var focustb = false
|
|
|
51 |
while (el && !(el.hasClass('level') || el.hasClass('description'))) {
|
|
|
52 |
if (el.hasClass('score')) focustb = true
|
|
|
53 |
el = el.get('parentNode')
|
|
|
54 |
}
|
|
|
55 |
if (el) {
|
|
|
56 |
if (el.one('textarea').hasClass('hiddenelement')) {
|
|
|
57 |
M.gradingform_rubriceditor.disablealleditors()
|
|
|
58 |
M.gradingform_rubriceditor.editmode(el, true, focustb)
|
|
|
59 |
}
|
|
|
60 |
return
|
|
|
61 |
}
|
|
|
62 |
// else disablecurrenteditor
|
|
|
63 |
M.gradingform_rubriceditor.disablealleditors()
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
// switch the criterion description or level to edit mode or switch back
|
|
|
67 |
M.gradingform_rubriceditor.editmode = function(el, editmode, focustb) {
|
|
|
68 |
var ta = el.one('textarea')
|
|
|
69 |
if (!editmode && ta.hasClass('hiddenelement')) return;
|
|
|
70 |
if (editmode && !ta.hasClass('hiddenelement')) return;
|
|
|
71 |
var pseudotablink = '<span class="pseudotablink" tabindex="0"></span>',
|
|
|
72 |
taplain = ta.get('parentNode').one('.plainvalue'),
|
|
|
73 |
tbplain = null,
|
|
|
74 |
tb = el.one('.score input[type=text]')
|
|
|
75 |
// add 'plainvalue' next to textarea for description/definition and next to input text field for score (if applicable)
|
|
|
76 |
if (!taplain) {
|
|
|
77 |
ta.get('parentNode').append('<div class="plainvalue">'+pseudotablink+'<span class="textvalue"> </span></div>')
|
|
|
78 |
taplain = ta.get('parentNode').one('.plainvalue')
|
|
|
79 |
taplain.one('.pseudotablink').on('focus', M.gradingform_rubriceditor.clickanywhere)
|
|
|
80 |
if (tb) {
|
|
|
81 |
tb.get('parentNode').append('<span class="plainvalue">'+pseudotablink+'<span class="textvalue"> </span></span>')
|
|
|
82 |
tbplain = tb.get('parentNode').one('.plainvalue')
|
|
|
83 |
tbplain.one('.pseudotablink').on('focus', M.gradingform_rubriceditor.clickanywhere)
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
if (tb && !tbplain) tbplain = tb.get('parentNode').one('.plainvalue')
|
|
|
87 |
if (!editmode) {
|
|
|
88 |
// if we need to hide the input fields, copy their contents to plainvalue(s). If description/definition
|
|
|
89 |
// is empty, display the default text ('Click to edit ...') and add/remove 'empty' CSS class to element
|
|
|
90 |
var value = ta.get('value')
|
|
|
91 |
if (value.length) taplain.removeClass('empty')
|
|
|
92 |
else {
|
|
|
93 |
value = (el.hasClass('level')) ? M.util.get_string('levelempty', 'gradingform_rubric') : M.util.get_string('criterionempty', 'gradingform_rubric')
|
|
|
94 |
taplain.addClass('empty')
|
|
|
95 |
}
|
|
|
96 |
taplain.one('.textvalue').set('innerHTML', Y.Escape.html(value));
|
|
|
97 |
if (tb) tbplain.one('.textvalue').set('innerHTML', Y.Escape.html(tb.get('value')));
|
|
|
98 |
// hide/display textarea, textbox and plaintexts
|
|
|
99 |
taplain.removeClass('hiddenelement')
|
|
|
100 |
ta.addClass('hiddenelement')
|
|
|
101 |
if (tb) {
|
|
|
102 |
tbplain.removeClass('hiddenelement')
|
|
|
103 |
tb.addClass('hiddenelement')
|
|
|
104 |
}
|
|
|
105 |
} else {
|
|
|
106 |
// if we need to show the input fields, set the width/height for textarea so it fills the cell
|
|
|
107 |
try {
|
|
|
108 |
var width = parseFloat(ta.get('parentNode').getComputedStyle('width')),
|
|
|
109 |
height
|
|
|
110 |
if (el.hasClass('level')) height = parseFloat(el.getComputedStyle('height')) - parseFloat(el.one('.score').getComputedStyle('height'))
|
|
|
111 |
else height = parseFloat(ta.get('parentNode').getComputedStyle('height'))
|
|
|
112 |
ta.setStyle('width', Math.max(width-16,50)+'px')
|
|
|
113 |
ta.setStyle('height', Math.max(height,20)+'px')
|
|
|
114 |
}
|
|
|
115 |
catch (err) {
|
|
|
116 |
// this browser do not support 'computedStyle', leave the default size of the textbox
|
|
|
117 |
}
|
|
|
118 |
// hide/display textarea, textbox and plaintexts
|
|
|
119 |
taplain.addClass('hiddenelement')
|
|
|
120 |
ta.removeClass('hiddenelement')
|
|
|
121 |
if (tb) {
|
|
|
122 |
tbplain.addClass('hiddenelement')
|
|
|
123 |
tb.removeClass('hiddenelement')
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
// focus the proper input field in edit mode
|
|
|
127 |
if (editmode) { if (tb && focustb) tb.focus(); else ta.focus() }
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
// handler for clicking on submit buttons within rubriceditor element. Adds/deletes/rearranges criteria and/or levels on client side
|
|
|
131 |
M.gradingform_rubriceditor.buttonclick = function(e, confirmed) {
|
|
|
132 |
var Y = M.gradingform_rubriceditor.Y
|
|
|
133 |
var name = M.gradingform_rubriceditor.name
|
|
|
134 |
if (e.target.get('type') != 'submit') return;
|
|
|
135 |
M.gradingform_rubriceditor.disablealleditors()
|
|
|
136 |
var chunks = e.target.get('id').split('-'),
|
|
|
137 |
action = chunks[chunks.length-1]
|
|
|
138 |
if (chunks[0] != name || chunks[1] != 'criteria') return;
|
|
|
139 |
var elements_str
|
|
|
140 |
if (chunks.length>4 || action == 'addlevel') {
|
|
|
141 |
elements_str = '#rubric-'+name+' #'+name+'-criteria-'+chunks[2]+'-levels .level'
|
|
|
142 |
} else {
|
|
|
143 |
elements_str = '#rubric-'+name+' .criterion'
|
|
|
144 |
}
|
|
|
145 |
// prepare the id of the next inserted level or criterion
|
|
|
146 |
var newlevid = 0;
|
|
|
147 |
var newid = 0;
|
|
|
148 |
if (action == 'addcriterion' || action == 'addlevel' || action == 'duplicate' ) {
|
|
|
149 |
newid = M.gradingform_rubriceditor.calculatenewid('#rubric-'+name+' .criterion');
|
|
|
150 |
newlevid = M.gradingform_rubriceditor.calculatenewid('#rubric-'+name+' .level');
|
|
|
151 |
}
|
|
|
152 |
if (chunks.length == 3 && action == 'addcriterion') {
|
|
|
153 |
// ADD NEW CRITERION
|
|
|
154 |
var levelsscores = [0], levidx = 1
|
|
|
155 |
var parentel = Y.one('#'+name+'-criteria')
|
|
|
156 |
if (parentel.one('>tbody')) parentel = parentel.one('>tbody')
|
|
|
157 |
if (parentel.all('.criterion').size()) {
|
|
|
158 |
var lastcriterion = parentel.all('.criterion').item(parentel.all('.criterion').size()-1).all('.level')
|
|
|
159 |
for (levidx=0;levidx<lastcriterion.size();levidx++) levelsscores[levidx] = lastcriterion.item(levidx).one('.score input[type=text]').get('value')
|
|
|
160 |
}
|
|
|
161 |
for (levidx;levidx<3;levidx++) levelsscores[levidx] = parseFloat(levelsscores[levidx-1])+1
|
|
|
162 |
var levelsstr = '';
|
|
|
163 |
for (levidx=0;levidx<levelsscores.length;levidx++) {
|
|
|
164 |
levelsstr += M.gradingform_rubriceditor.templates[name].level.
|
|
|
165 |
replace(/\{LEVEL-id\}/g, 'NEWID'+(newlevid+levidx)).
|
|
|
166 |
replace(/\{LEVEL-score\}/g, levelsscores[levidx]).
|
|
|
167 |
replace(/\{LEVEL-index\}/g, levidx + 1);
|
|
|
168 |
}
|
|
|
169 |
var newcriterion = M.gradingform_rubriceditor.templates[name]['criterion'].replace(/\{LEVELS\}/, levelsstr)
|
|
|
170 |
parentel.append(newcriterion.replace(/\{CRITERION-id\}/g, 'NEWID'+newid).replace(/\{.+?\}/g, ''))
|
|
|
171 |
M.gradingform_rubriceditor.assignclasses('#rubric-'+name+' #'+name+'-criteria-NEWID'+newid+'-levels .level')
|
|
|
172 |
M.gradingform_rubriceditor.addhandlers();
|
|
|
173 |
M.gradingform_rubriceditor.disablealleditors()
|
|
|
174 |
M.gradingform_rubriceditor.assignclasses(elements_str)
|
|
|
175 |
M.gradingform_rubriceditor.editmode(
|
|
|
176 |
Y.one('#rubric-' + name + ' #' + name + '-criteria-NEWID' + newid + '-description-cell'), true
|
|
|
177 |
);
|
|
|
178 |
} else if (chunks.length == 5 && action == 'addlevel') {
|
|
|
179 |
// ADD NEW LEVEL
|
|
|
180 |
var newscore = 0;
|
|
|
181 |
parent = Y.one('#'+name+'-criteria-'+chunks[2]+'-levels')
|
|
|
182 |
var levelIndex = 1;
|
|
|
183 |
parent.all('.level').each(function (node) {
|
|
|
184 |
newscore = Math.max(newscore, parseFloat(node.one('.score input[type=text]').get('value')) + 1);
|
|
|
185 |
levelIndex++;
|
|
|
186 |
});
|
|
|
187 |
var newlevel = M.gradingform_rubriceditor.templates[name]['level'].
|
|
|
188 |
replace(/\{CRITERION-id\}/g, chunks[2]).replace(/\{LEVEL-id\}/g, 'NEWID'+newlevid).
|
|
|
189 |
replace(/\{LEVEL-score\}/g, newscore).
|
|
|
190 |
replace(/\{LEVEL-index\}/g, levelIndex).
|
|
|
191 |
replace(/\{.+?\}/g, '');
|
|
|
192 |
parent.append(newlevel)
|
|
|
193 |
M.gradingform_rubriceditor.addhandlers();
|
|
|
194 |
M.gradingform_rubriceditor.disablealleditors()
|
|
|
195 |
M.gradingform_rubriceditor.assignclasses(elements_str)
|
|
|
196 |
M.gradingform_rubriceditor.editmode(parent.all('.level').item(parent.all('.level').size()-1), true)
|
|
|
197 |
} else if (chunks.length == 4 && action == 'moveup') {
|
|
|
198 |
// MOVE CRITERION UP
|
|
|
199 |
el = Y.one('#'+name+'-criteria-'+chunks[2])
|
|
|
200 |
if (el.previous()) el.get('parentNode').insertBefore(el, el.previous())
|
|
|
201 |
M.gradingform_rubriceditor.assignclasses(elements_str)
|
|
|
202 |
} else if (chunks.length == 4 && action == 'movedown') {
|
|
|
203 |
// MOVE CRITERION DOWN
|
|
|
204 |
el = Y.one('#'+name+'-criteria-'+chunks[2])
|
|
|
205 |
if (el.next()) el.get('parentNode').insertBefore(el.next(), el)
|
|
|
206 |
M.gradingform_rubriceditor.assignclasses(elements_str)
|
|
|
207 |
} else if (chunks.length == 4 && action == 'delete') {
|
|
|
208 |
// DELETE CRITERION
|
|
|
209 |
if (confirmed) {
|
|
|
210 |
Y.one('#'+name+'-criteria-'+chunks[2]).remove()
|
|
|
211 |
M.gradingform_rubriceditor.assignclasses(elements_str)
|
|
|
212 |
} else {
|
|
|
213 |
M.util.js_pending('gradingform_rubriceditor:deleteConfirmation');
|
|
|
214 |
require(['core/notification', 'core/str'], function(Notification, Str) {
|
|
|
215 |
Notification.saveCancelPromise(
|
|
|
216 |
Str.get_string('confirmation', 'admin'),
|
|
|
217 |
Str.get_string('confirmdeletecriterion', 'gradingform_rubric'),
|
|
|
218 |
Str.get_string('yes', 'moodle')
|
|
|
219 |
).then(function() {
|
|
|
220 |
M.gradingform_rubriceditor.buttonclick.apply(this, [e, true]);
|
|
|
221 |
return;
|
|
|
222 |
}.bind(this)).catch(function() {
|
|
|
223 |
// User cancelled.
|
|
|
224 |
});
|
|
|
225 |
M.util.js_complete('gradingform_rubriceditor:deleteConfirmation');
|
|
|
226 |
}.bind(this));
|
|
|
227 |
}
|
|
|
228 |
} else if (chunks.length == 4 && action == 'duplicate') {
|
|
|
229 |
// Duplicate criterion.
|
|
|
230 |
var levelsdef = [], levelsscores = [0], levidx = null;
|
|
|
231 |
var parentel = Y.one('#'+name+'-criteria');
|
|
|
232 |
if (parentel.one('>tbody')) { parentel = parentel.one('>tbody'); }
|
|
|
233 |
|
|
|
234 |
var source = Y.one('#'+name+'-criteria-'+chunks[2]);
|
|
|
235 |
if (source.all('.level')) {
|
|
|
236 |
var lastcriterion = source.all('.level');
|
|
|
237 |
for (levidx = 0; levidx < lastcriterion.size(); levidx++) {
|
|
|
238 |
levelsdef[levidx] = lastcriterion.item(levidx).one('.definition .textvalue').get('innerHTML');
|
|
|
239 |
}
|
|
|
240 |
for (levidx = 0; levidx < lastcriterion.size(); levidx++) {
|
|
|
241 |
levelsscores[levidx] = lastcriterion.item(levidx).one('.score input[type=text]').get('value');
|
|
|
242 |
}
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
for (levidx; levidx < 3; levidx++) { levelsscores[levidx] = parseFloat(levelsscores[levidx-1]) + 1; }
|
|
|
246 |
var levelsstr = '';
|
|
|
247 |
for (levidx = 0; levidx < levelsscores.length; levidx++) {
|
|
|
248 |
levelsstr += M.gradingform_rubriceditor.templates[name].level
|
|
|
249 |
.replace(/\{LEVEL-id\}/g, 'NEWID'+(newlevid+levidx))
|
|
|
250 |
.replace(/\{LEVEL-score\}/g, levelsscores[levidx])
|
|
|
251 |
.replace(/\{LEVEL-definition\}/g, levelsdef[levidx]);
|
|
|
252 |
}
|
|
|
253 |
var description = source.one('.description .textvalue');
|
|
|
254 |
var newcriterion = M.gradingform_rubriceditor.templates[name].criterion
|
|
|
255 |
.replace(/\{LEVELS\}/, levelsstr)
|
|
|
256 |
.replace(/\{CRITERION-description\}/, description.get('innerHTML'));
|
|
|
257 |
parentel.append(newcriterion.replace(/\{CRITERION-id\}/g, 'NEWID'+newid).replace(/\{.+?\}/g, ''));
|
|
|
258 |
M.gradingform_rubriceditor.assignclasses('#rubric-'+name+' #'+name+'-criteria-NEWID'+newid+'-levels .level');
|
|
|
259 |
M.gradingform_rubriceditor.addhandlers();
|
|
|
260 |
M.gradingform_rubriceditor.disablealleditors();
|
|
|
261 |
M.gradingform_rubriceditor.assignclasses(elements_str);
|
|
|
262 |
M.gradingform_rubriceditor.editmode(Y.one('#rubric-'+name+' #'+name+'-criteria-NEWID'+newid+'-description-cell'),true);
|
|
|
263 |
} else if (chunks.length == 6 && action == 'delete') {
|
|
|
264 |
// DELETE LEVEL
|
|
|
265 |
if (confirmed) {
|
|
|
266 |
Y.one('#'+name+'-criteria-'+chunks[2]+'-'+chunks[3]+'-'+chunks[4]).remove()
|
|
|
267 |
M.gradingform_rubriceditor.assignclasses(elements_str)
|
|
|
268 |
} else {
|
|
|
269 |
M.util.js_pending('gradingform_rubriceditor:deleteLevelConfirmation');
|
|
|
270 |
require(['core/notification', 'core/str'], function(Notification, Str) {
|
|
|
271 |
Notification.saveCancelPromise(
|
|
|
272 |
Str.get_string('confirmation', 'admin'),
|
|
|
273 |
Str.get_string('confirmdeletelevel', 'gradingform_rubric'),
|
|
|
274 |
Str.get_string('yes', 'moodle')
|
|
|
275 |
).then(function() {
|
|
|
276 |
M.gradingform_rubriceditor.buttonclick.apply(this, [e, true]);
|
|
|
277 |
return;
|
|
|
278 |
}.bind(this)).catch(function() {
|
|
|
279 |
// User cancelled.
|
|
|
280 |
});
|
|
|
281 |
M.util.js_complete('gradingform_rubriceditor:deleteLevelConfirmation');
|
|
|
282 |
}.bind(this));
|
|
|
283 |
}
|
|
|
284 |
} else {
|
|
|
285 |
// unknown action
|
|
|
286 |
return;
|
|
|
287 |
}
|
|
|
288 |
e.preventDefault();
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
// properly set classes (first/last/odd/even), level width and/or criterion sortorder for elements Y.all(elements_str)
|
|
|
292 |
M.gradingform_rubriceditor.assignclasses = function (elements_str) {
|
|
|
293 |
var elements = M.gradingform_rubriceditor.Y.all(elements_str)
|
|
|
294 |
for (var i=0;i<elements.size();i++) {
|
|
|
295 |
elements.item(i).removeClass('first').removeClass('last').removeClass('even').removeClass('odd').
|
|
|
296 |
addClass(((i%2)?'odd':'even') + ((i==0)?' first':'') + ((i==elements.size()-1)?' last':''))
|
|
|
297 |
elements.item(i).all('input[type=hidden]').each(
|
|
|
298 |
function(node) {if (node.get('name').match(/sortorder/)) node.set('value', i)}
|
|
|
299 |
);
|
|
|
300 |
if (elements.item(i).hasClass('level')) elements.item(i).set('width', Math.round(100/elements.size())+'%')
|
|
|
301 |
}
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
// returns unique id for the next added element, it should not be equal to any of Y.all(elements_str) ids
|
|
|
305 |
M.gradingform_rubriceditor.calculatenewid = function (elements_str) {
|
|
|
306 |
var newid = 1
|
|
|
307 |
M.gradingform_rubriceditor.Y.all(elements_str).each( function(node) {
|
|
|
308 |
var idchunks = node.get('id').split('-'), id = idchunks.pop();
|
|
|
309 |
if (id.match(/^NEWID(\d+)$/)) newid = Math.max(newid, parseInt(id.substring(5))+1);
|
|
|
310 |
} );
|
|
|
311 |
return newid
|
|
|
312 |
}
|