-
Notifications
You must be signed in to change notification settings - Fork 5
/
module.js
executable file
·1200 lines (1173 loc) · 42.9 KB
/
module.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Grader report namespace
*/
M.gradereport_grader = {
/**
* @param {Array} reports An array of instantiated report objects
*/
reports : [],
/**
* @namespace M.gradereport_grader
* @param {Object} reports A collection of classes used by the grader report module
*/
classes : {},
/**
* @param {Object} tooltip Null or a tooltip object
*/
tooltip : null,
/**
* Instantiates a new grader report
*
* @function
* @param {YUI} Y
* @param {String} id The id attribute of the reports table
* @param {Object} cfg A configuration object
* @param {Array} An array of items in the report
* @param {Array} An array of users on the report
* @param {Array} An array of feedback objects
*/
init_report : function(Y, id, cfg, items, users, feedback) {
this.tooltip = this.tooltip || {
overlay : null, // Y.Overlay instance
/**
* Attaches the tooltip event to the provided cell
*
* @function M.gradereport_grader.tooltip.attach
* @this M.gradereport_grader
* @param {Y.Node} td The cell to attach the tooltip event to
*/
attach : function(td, report) {
td.on('mouseenter', this.show, this, report);
},
/**
* Shows the tooltip: Callback from @see M.gradereport_grader.tooltip#attach
*
* @function M.gradereport_grader.tooltip.show
* @this {M.gradereport_grader.tooltip}
* @param {Event} e
* @param {M.gradereport_grader.classes.report} report
*/
show : function(e, report) {
e.halt();
var properties = report.get_cell_info(e.target);
if (!properties) {
return;
}
var content = '<div class="graderreportoverlay">';
content += '<div class="fullname">'+properties.username+'</div><div class="itemname">'+properties.itemname+'</div>';
if (properties.feedback) {
content += '<div class="feedback">'+properties.feedback+'</div>';
}
content += '</div>';
properties.cell.on('mouseleave', this.hide, this, properties.cell);
properties.cell.addClass('tooltipactive');
this.overlay = this.overlay || (function(){
var overlay = new Y.Overlay({
bodyContent : 'Loading',
visible : false,
zIndex : 2
});
overlay.render(report.table.ancestor('div'));
return overlay;
})();
this.overlay.set('xy', [e.target.getX()+(e.target.get('offsetWidth')/2),e.target.getY()+e.target.get('offsetHeight')-5]);
this.overlay.set("bodyContent", content);
this.overlay.show();
this.overlay.get('boundingBox').setStyle('visibility', 'visible');
},
/**
* Hides the tooltip
*
* @function M.gradereport_grader.tooltip.hide
* @this {M.gradereport_grader.tooltip}
* @param {Event} e
* @param {Y.Node} cell
*/
hide : function(e, cell) {
cell.removeClass('tooltipactive');
this.overlay.hide();
this.overlay.get('boundingBox').setStyle('visibility', 'hidden');
}
};
// Create the actual report
this.reports[id] = new this.classes.report(Y, id, cfg, items, users, feedback);
}
};
/**
* Initialises the JavaScript for the gradebook grader report
*
* @class report
* @constructor
* @this {M.gradereport_grader}
* @param {YUI} Y
* @param {int} id The id of the table to attach the report to
* @param {Object} cfg Configuration variables
* @param {Array} items An array containing grade items
* @param {Array} users An array containing user information
* @param {Array} feedback An array containing feedback information
*/
M.gradereport_grader.classes.report = function(Y, id, cfg, items, users, feedback) {
this.Y = Y;
this.isediting = (cfg.isediting);
this.ajaxenabled = (cfg.ajaxenabled);
this.items = items;
this.users = users;
this.feedback = feedback;
this.table = Y.one('#user-grades');
// Alias this so that we can use the correct scope in the coming
// node iteration
this.table.all('tr').each(function(tr){
// Check it is a user row
if (tr.getAttribute('id').match(/^(fixed_)?user_(\d+)$/)) {
// Highlight rows
tr.all('th.cell').on('click', this.table_highlight_row, this, tr);
// Display tooltips
tr.all('td.cell').each(function(cell){
M.gradereport_grader.tooltip.attach(cell, this);
}, this);
}
}, this);
// If the fixed table exists then map those rows to highlight the
// grades table rows
var fixed = this.Y.one(id);
if (fixed) {
fixed.all('tr').each(function(tr) {
if (tr.getAttribute('id').match(/^fixed_user_(\d+)$/)) {
tr.all('th.cell').on('click', this.table_highlight_row, this, this.Y.one(tr.getAttribute('id').replace(/^fixed_/, '#')));
}
}, this);
}
// Highlight columns
this.table.all('.highlightable').each(function(cell){
cell.on('click', this.table_highlight_column, this, cell);
cell.removeClass('highlightable');
}, this);
// If ajax is enabled then initialise the ajax component
if (this.ajaxenabled) {
this.ajax = new M.gradereport_grader.classes.ajax(this, cfg);
}
};
/**
* Extend the report class with the following methods and properties
*/
M.gradereport_grader.classes.report.prototype.table = null; // YUI Node for the reports main table
M.gradereport_grader.classes.report.prototype.items = []; // Array containing grade items
M.gradereport_grader.classes.report.prototype.users = []; // Array containing user information
M.gradereport_grader.classes.report.prototype.feedback = []; // Array containing feedback items
M.gradereport_grader.classes.report.prototype.ajaxenabled = false; // True is AJAX is enabled for the report
M.gradereport_grader.classes.report.prototype.ajax = null; // An instance of the ajax class or null
/**
* Highlights a row in the report
*
* @function
* @param {Event} e
* @param {Y.Node} tr The table row to highlight
*/
M.gradereport_grader.classes.report.prototype.table_highlight_row = function (e, tr) {
tr.all('.cell').toggleClass('hmarked');
};
/**
* Highlights a cell in the table
*
* @function
* @param {Event} e
* @param {Y.Node} cell
*/
M.gradereport_grader.classes.report.prototype.table_highlight_column = function(e, cell) {
var column = 0;
while (cell = cell.previous('.cell')) {
column += parseFloat(cell.getAttribute('colspan')) || 1;
}
this.table.all('.c'+column).toggleClass('vmarked');
};
/**
* Builds an object containing information at the relevant cell given either
* the cell to get information for or an array containing userid and itemid
*
* @function
* @this {M.gradereport_grader}
* @param {Y.Node|Array} arg Either a YUI Node instance or an array containing
* the userid and itemid to reference
* @return {Object}
*/
M.gradereport_grader.classes.report.prototype.get_cell_info = function(arg) {
var userid= null;
var itemid = null;
var feedback = null;
var cell = null;
var i = null;
if (arg instanceof this.Y.Node) {
if (arg.get('nodeName').toUpperCase() !== 'TD') {
arg = arg.ancestor('td.cell');
}
var regexp = /^u(\d+)i(\d+)$/;
var parts = regexp.exec(arg.getAttribute('id'));
userid = parts[1];
itemid = parts[2];
cell = arg;
} else {
userid = arg[0];
itemid = arg[1];
cell = this.Y.one('#u'+userid+'i'+itemid);
}
if (!cell) {
return null;
}
for (i in this.feedback) {
if (this.feedback[i] && this.feedback[i].user == userid && this.feedback[i].item == itemid) {
feedback = this.feedback[i].content;
break;
}
}
return {
userid : userid,
username : this.users[userid],
itemid : itemid,
itemname : this.items[itemid].name,
itemtype : this.items[itemid].type,
itemscale : this.items[itemid].scale,
itemdp : this.items[itemid].decimals,
feedback : feedback,
cell : cell
};
};
/**
* Updates or creates the feedback JS structure for the given user/item
*
* @function
* @this {M.gradereport_grader}
* @param {Int} userid
* @param {Int} itemid
* @param {String} newfeedback
* @return {Bool}
*/
M.gradereport_grader.classes.report.prototype.update_feedback = function(userid, itemid, newfeedback) {
for (var i in this.feedback) {
if (this.feedback[i].user == userid && this.feedback[i].item == itemid) {
this.feedback[i].content = newfeedback;
return true;
}
}
this.feedback.push({user:userid,item:itemid,content:newfeedback});
return true;
};
/**
* Initialises the AJAX component of this report
* @class ajax
* @constructor
* @this {M.gradereport_grader.ajax}
* @param {M.gradereport_grader.classes.report} report
* @param {Object} cfg
*/
M.gradereport_grader.classes.ajax = function(report, cfg) {
this.report = report;
this.courseid = cfg.courseid || null;
this.feedbacktrunclength = cfg.feedbacktrunclength || null;
this.studentsperpage = cfg.studentsperpage || null;
this.showquickfeedback = cfg.showquickfeedback || false;
this.scales = cfg.scales || null;
this.existingfields = [];
if (!M.gradereport_grader.isediting) {
report.table.all('.cell.grade').on('makeditable|click', this.make_editable, this);
} else {
for (var userid in report.users) {
if (!this.existingfields[userid]) {
this.existingfields[userid] = [];
}
for (var itemid in report.items) {
this.existingfields[userid][itemid] = new this.existingfield(this, userid, itemid);
}
}
}
};
/**
* Extend the ajax class with the following methods and properties
*/
M.gradereport_grader.classes.ajax.prototype.report = null; // A reference to the report class this object will use
M.gradereport_grader.classes.ajax.prototype.courseid = null; // The id for the course being viewed
M.gradereport_grader.classes.ajax.prototype.feedbacktrunclength = null; // The length to truncate feedback to
M.gradereport_grader.classes.ajax.prototype.studentsperpage = null; // The number of students shown per page
M.gradereport_grader.classes.ajax.prototype.showquickfeedback = null; // True if feedback editing should be shown
M.gradereport_grader.classes.ajax.prototype.current = null; // The field being currently editing
M.gradereport_grader.classes.ajax.prototype.pendingsubmissions = []; // Array containing pending IO transactions
M.gradereport_grader.classes.ajax.prototype.scales = []; // An array of scales used in this report
/**
* Makes a cell editable
* @function
* @this {M.gradereport_grader.classes.ajax}
*/
M.gradereport_grader.classes.ajax.prototype.make_editable = function(e) {
var node = e;
if (e.halt) {
e.halt();
node = e.target;
}
if (node.get('nodeName').toUpperCase() !== 'TD') {
node = node.ancestor('td');
}
this.report.Y.detach('click', this.make_editable, node);
if (this.current) {
// Current is already set!
this.process_editable_field(node);
return;
}
// Sort out the field type
var fieldtype = 'text';
if (node.hasClass('grade_type_scale')) {
fieldtype = 'scale';
}
// Create the appropriate field widget
switch (fieldtype) {
case 'scale':
this.current = new M.gradereport_grader.classes.scalefield(this.report, node);
break;
case 'text':
default:
this.current = new M.gradereport_grader.classes.textfield(this.report, node);
break;
}
this.current.replace().attach_key_events();
};
/**
* Callback function for the user pressing the enter key on an editable field
*
* @function
* @this {M.gradereport_grader.classes.ajax}
* @param {Event} e
*/
M.gradereport_grader.classes.ajax.prototype.keypress_enter = function(e) {
this.process_editable_field(null);
};
/**
* Callback function for the user pressing Tab or Shift+Tab
*
* @function
* @this {M.gradereport_grader.classes.ajax}
* @param {Event} e
* @param {Bool} ignoreshift If true and shift is pressed then don't exec
*/
M.gradereport_grader.classes.ajax.prototype.keypress_tab = function(e, ignoreshift) {
var next = null;
if (e.shiftKey) {
if (ignoreshift) {
return;
}
next = this.get_above_cell();
} else {
next = this.get_below_cell();
}
this.process_editable_field(next);
};
/**
* Callback function for the user pressing an CTRL + an arrow key
*
* @function
* @this {M.gradereport_grader.classes.ajax}
*/
M.gradereport_grader.classes.ajax.prototype.keypress_arrows = function(e) {
e.preventDefault();
var next = null;
switch (e.keyCode) {
case 37: // Left
next = this.get_prev_cell();
break;
case 38: // Up
next = this.get_above_cell();
break;
case 39: // Right
next = this.get_next_cell();
break;
case 40: // Down
next = this.get_below_cell();
break;
}
this.process_editable_field(next);
};
/**
* Processes an editable field an does what ever is required to update it
*
* @function
* @this {M.gradereport_grader.classes.ajax}
* @param {Y.Node|null} next The next node to make editable (chaining)
*/
M.gradereport_grader.classes.ajax.prototype.process_editable_field = function(next) {
if (this.current.has_changed()) {
var properties = this.report.get_cell_info(this.current.node);
var values = this.current.commit();
this.current.revert();
this.submit(properties, values);
} else {
this.current.revert();
}
this.current = null;
if (next) {
this.make_editable(next, null);
}
};
/**
* Gets the next cell that is editable (right)
* @function
* @this {M.gradereport_grader.classes.ajax}
* @param {Y.Node} cell
* @return {Y.Node}
*/
M.gradereport_grader.classes.ajax.prototype.get_next_cell = function(cell) {
var n = cell || this.current.node;
var next = n.next('td');
var tr = null;
if (!next && (tr = n.ancestor('tr').next('tr'))) {
next = tr.all('.grade').item(0);
}
if (!next) {
next = this.current.node;
}
return next;
};
/**
* Gets the previous cell that is editable (left)
* @function
* @this {M.gradereport_grader.classes.ajax}
* @param {Y.Node} cell
* @return {Y.Node}
*/
M.gradereport_grader.classes.ajax.prototype.get_prev_cell = function(cell) {
var n = cell || this.current.node;
var next = n.previous('.grade');
var tr = null;
if (!next && (tr = n.ancestor('tr').previous('tr'))) {
var cells = tr.all('.grade');
next = cells.item(cells.size()-1);
}
if (!next) {
next = this.current.node;
}
return next;
};
/**
* Gets the cell above if it is editable (up)
* @function
* @this {M.gradereport_grader.classes.ajax}
* @param {Y.Node} cell
* @return {Y.Node}
*/
M.gradereport_grader.classes.ajax.prototype.get_above_cell = function(cell) {
var n = cell || this.current.node;
var tr = n.ancestor('tr').previous('tr');
var next = null;
if (tr) {
var column = 0;
var ntemp = n;
while (ntemp = ntemp.previous('td.cell')) {
column++;
}
next = tr.all('td.cell').item(column);
}
if (!next) {
next = this.current.node;
}
return next;
};
/**
* Gets the cell below if it is editable (down)
* @function
* @this {M.gradereport_grader.classes.ajax}
* @param {Y.Node} cell
* @return {Y.Node}
*/
M.gradereport_grader.classes.ajax.prototype.get_below_cell = function(cell) {
var n = cell || this.current.node;
var tr = n.ancestor('tr').next('tr');
var next = null;
if (tr && !tr.hasClass('avg')) {
var column = 0;
var ntemp = n;
while (ntemp = ntemp.previous('td.cell')) {
column++;
}
next = tr.all('td.cell').item(column);
}
if (!next) {
next = this.current.node;
}
return next;
};
/**
* Submits changes for update
*
* @function
* @this {M.gradereport_grader.classes.ajax}
* @param {Object} properties Properties of the cell being edited
* @param {Object} values Object containing old + new values
*/
M.gradereport_grader.classes.ajax.prototype.submit = function(properties, values) {
// Stop the IO queue so we can add to it
this.report.Y.io.queue.stop();
// If the grade has changed add an IO transaction to update it to the queue
if (values.grade !== values.oldgrade) {
this.pendingsubmissions.push({transaction:this.report.Y.io.queue(M.cfg.wwwroot+'/grade/report/grader/ajax_callbacks.php', {
method : 'POST',
data : 'id='+this.courseid+'&userid='+properties.userid+'&itemid='+properties.itemid+'&action=update&newvalue='+values.grade+'&type='+properties.itemtype+'&sesskey='+M.cfg.sesskey,
on : {
complete : this.submission_outcome
},
context : this,
arguments : {
properties : properties,
values : values,
type : 'grade'
}
}),complete:false,outcome:null});
}
// If feedback is editable and has changed add to the IO queue for it
if (values.editablefeedback && values.feedback !== values.oldfeedback) {
this.pendingsubmissions.push({transaction:this.report.Y.io.queue(M.cfg.wwwroot+'/grade/report/grader/ajax_callbacks.php', {
method : 'POST',
data : 'id='+this.courseid+'&userid='+properties.userid+'&itemid='+properties.itemid+'&action=update&newvalue='+values.feedback+'&type=feedback&sesskey='+M.cfg.sesskey,
on : {
complete : this.submission_outcome
},
context : this,
arguments : {
properties : properties,
values : values,
type : 'feedback'
}
}),complete:false,outcome:null});
}
// Process the IO queue
this.report.Y.io.queue.start();
};
/**
* Callback function for IO transaction completions
*
* Uses a synchronous queue to ensure we maintain some sort of order
*
* @function
* @this {M.gradereport_grader.classes.ajax}
* @param {Int} tid Transaction ID
* @param {Object} outcome
* @param {Mixed} args
*/
M.gradereport_grader.classes.ajax.prototype.submission_outcome = function(tid, outcome, args) {
// Parse the response as JSON
try {
outcome = this.report.Y.JSON.parse(outcome.responseText);
} catch(e) {
var message = M.str.gradereport_grader.ajaxfailedupdate;
message.replace(/\[1\]/, args.type);
message.replace(/\[2\]/, M.gradereport_grader.users[args.properties.userid]);
this.display_submission_error(message, args.properties.cell);
return;
}
// Quick reference for the grader report
var i = null;
// Check the outcome
if (outcome.result == 'success') {
// Iterate through each row in the result object
for (i in outcome.row) {
if (outcome.row[i] && outcome.row[i].userid && outcome.row[i].itemid) {
// alias it, we use it quite a bit
var r = outcome.row[i];
// Get the cell referred to by this result object
var info = this.report.get_cell_info([r.userid, r.itemid]);
if (!info) {
continue;
}
// Calculate the final grade for the cell
var finalgrade = '';
if (r.scale) {
finalgrade = (r.finalgrade)?this.scales[r.scale][parseFloat(r.finalgrade)-1]:'-';
} else {
finalgrade = (r.finalgrade)?parseFloat(r.finalgrade).toFixed(info.itemdp):'-';
}
if (this.report.isediting) {
if (args.properties.itemtype == 'scale') {
info.cell.one('#grade_'+r.userid+'_'+r.itemid).all('options').each(function(option){
if (option.get('value') == finalgrade) {
option.setAttribute('selected', 'selected');
} else {
option.removeAttribute('selected');
}
});
} else {
info.cell.one('#grade_'+r.userid+'_'+r.itemid).set('value', finalgrade);
}
} else {
// If there is no currently editing field or if this cell is not being currently edited
if (!this.current || info.cell.get('id') != this.current.node.get('id')) {
// Update the value
info.cell.one('.gradevalue').set('innerHTML',finalgrade);
} else if (this.current && info.cell.get('id') == this.current.node.get('id')) {
// If we are here the grade value of the cell currently being edited has changed !!!!!!!!!
// If the user has not actually changed the old value yet we will automatically correct it
// otherwise we will prompt the user to choose to use their value or the new value!
if (!this.current.has_changed() || confirm(M.str.gradereport_grader.ajaxfieldchanged)) {
this.current.set_grade(finalgrade);
this.current.grade.set('value', finalgrade);
}
}
}
}
}
// Flag the changed cell as overridden by ajax
args.properties.cell.addClass('ajaxoverridden');
} else {
var p = args.properties;
if (args.type == 'grade') {
var oldgrade = args.values.oldgrade;
p.cell.one('.gradevalue').set('innerHTML',oldgrade);
} else if (args.type == 'feedback') {
this.report.update_feedback(p.userid, p.itemid, args.values.oldfeedback);
}
this.display_submission_error(outcome.message, p.cell);
}
// Check if all IO transactions in the queue are complete yet
var allcomplete = true;
for (i in this.pendingsubmissions) {
if (this.pendingsubmissions[i]) {
if (this.pendingsubmissions[i].transaction.id == tid) {
this.pendingsubmissions[i].complete = true;
this.pendingsubmissions[i].outcome = outcome;
this.report.Y.io.queue.remove(this.pendingsubmissions[i].transaction);
}
if (!this.pendingsubmissions[i].complete) {
allcomplete = false;
}
}
}
if (allcomplete) {
this.pendingsubmissions = [];
}
};
/**
* Displays a submission error within a overlay on the cell that failed update
*
* @function
* @this {M.gradereport_grader.classes.ajax}
* @param {String} message
* @param {Y.Node} cell
*/
M.gradereport_grader.classes.ajax.prototype.display_submission_error = function(message, cell) {
var erroroverlay = new this.report.Y.Overlay({
headerContent : '<div><strong class="error">'+M.str.gradereport_grader.ajaxerror+'</strong> <em>'+M.str.gradereport_grader.ajaxclicktoclose+'</em></div>',
bodyContent : message,
visible : false,
zIndex : 3
});
erroroverlay.set('xy', [cell.getX()+10,cell.getY()+10]);
erroroverlay.render(this.report.table.ancestor('div'));
erroroverlay.show();
erroroverlay.get('boundingBox').on('click', function(){
this.get('boundingBox').setStyle('visibility', 'hidden');
this.hide();
this.destroy();
}, erroroverlay);
erroroverlay.get('boundingBox').setStyle('visibility', 'visible');
};
/**
* A class for existing fields
* This class is used only when the user is in editing mode
*
* @class existingfield
* @constructor
* @param {M.gradereport_grader.classes.report} report
* @param {Int} userid
* @param {Int} itemid
*/
M.gradereport_grader.classes.existingfield = function(report, userid, itemid) {
this.report = report;
this.userid = userid;
this.itemid = itemid;
this.editfeedback = M.gradereport_grader.ajax.showquickfeedback;
this.grade = report.Y.one('#grade_'+userid+'_'+itemid);
this.oldgrade = report.Y.one('#oldgrade_'+userid+'_'+itemid);
// On blur save any changes in the grade field
this.grade.on('blur', this.submit, this);
// Check if feedback is enabled
if (this.editfeedback) {
// Get the feedback fields
this.feedback = report.Y.one('#feedback_'+userid+'_'+itemid);
this.oldfeedback = report.Y.one('#oldfeedback_'+userid+'_'+itemid);
// On blur save any changes in the feedback field
this.feedback.on('blur', this.submit, this);
// Override the default tab movements when moving between cells
this.keyevents.push(report.Y.on('key', this.keypress_tab, this.grade, 'press:9+shift', this)); // Handle Shift+Tab
this.keyevents.push(report.Y.on('key', this.keypress_tab, this.feedback, 'press:9', this, true)); // Handle Tab
this.keyevents.push(report.Y.on('key', this.keypress_enter, this.feedback, 'press:13', this)); // Handle the Enter key being pressed
this.keyevents.push(report.Y.on('key', this.keypress_arrows, this.feedback, 'press:37,38,39,40+ctrl', this)); // Handle CTRL + arrow keys
// Override the default tab movements for fields in the same cell
this.keyevents.push(report.Y.on('key', function(e){e.preventDefault();this.grade.focus();}, this.feedback, 'press:9+shift', this));
this.keyevents.push(report.Y.on('key', function(e){if (e.shiftKey) {return;}e.preventDefault();this.feedback.focus();}, this.grade, 'press:9', this));
} else {
this.keyevents.push(report.Y.on('key', this.keypress_tab, this.grade, 'press:9', this)); // Handle Tab and Shift+Tab
}
this.keyevents.push(report.Y.on('key', this.keypress_enter, this.grade, 'press:13', this)); // Handle the Enter key being pressed
this.keyevents.push(report.Y.on('key', this.keypress_arrows, this.grade, 'press:37,38,39,40+ctrl', this)); // Handle CTRL + arrow keys
};
/**
* Attach the required properties and methods to the existing field class
* via prototyping
*/
M.gradereport_grader.classes.existingfield.prototype.userid = null;
M.gradereport_grader.classes.existingfield.prototype.itemid = null;
M.gradereport_grader.classes.existingfield.prototype.editfeedback = false;
M.gradereport_grader.classes.existingfield.prototype.grade = null;
M.gradereport_grader.classes.existingfield.prototype.oldgrade = null;
M.gradereport_grader.classes.existingfield.prototype.keyevents = [];
/**
* Handles saving of changed on keypress
*
* @function
* @this {M.gradereport_grader.classes.existingfield}
* @param {Event} e
*/
M.gradereport_grader.classes.existingfield.prototype.keypress_enter = function(e) {
this.submit();
};
/**
* Handles setting the correct focus if the user presses tab
*
* @function
* @this {M.gradereport_grader.classes.existingfield}
* @param {Event} e
* @param {Bool} ignoreshift
*/
M.gradereport_grader.classes.existingfield.prototype.keypress_tab = function(e, ignoreshift) {
e.preventDefault();
var next = null;
if (e.shiftKey) {
if (ignoreshift) {
return;
}
next = this.report.ajax.get_above_cell(this.grade.ancestor('td'));
} else {
next = this.report.ajax.get_below_cell(this.grade.ancestor('td'));
}
this.move_focus(next);
};
/**
* Handles setting the correct focus when the user presses CTRL+arrow keys
*
* @function
* @this {M.gradereport_grader.classes.existingfield}
* @param {Event} e
*/
M.gradereport_grader.classes.existingfield.prototype.keypress_arrows = function(e) {
var next = null;
switch (e.keyCode) {
case 37: // Left
next = this.report.ajax.get_prev_cell(this.grade.ancestor('td'));
break;
case 38: // Up
next = this.report.ajax.get_above_cell(this.grade.ancestor('td'));
break;
case 39: // Right
next = this.report.ajax.get_next_cell(this.grade.ancestor('td'));
break;
case 40: // Down
next = this.report.ajax.get_below_cell(this.grade.ancestor('td'));
break;
}
this.move_focus(next);
};
/**
* Move the focus to the node
* @function
* @this {M.gradereport_grader.classes.existingfield}
* @param {Y.Node} node
*/
M.gradereport_grader.classes.existingfield.prototype.move_focus = function(node) {
if (node) {
var properties = this.report.get_cell_info(node);
switch(properties.itemtype) {
case 'scale':
properties.cell.one('select.select').focus();
break;
case 'value':
default:
properties.cell.one('input.text').focus();
break;
}
}
};
/**
* Checks if the values for the field have changed
*
* @function
* @this {M.gradereport_grader.classes.existingfield}
* @return {Bool}
*/
M.gradereport_grader.classes.existingfield.prototype.has_changed = function() {
if (this.editfeedback) {
return (this.grade.get('value') !== this.oldgrade.get('value') || this.feedback.get('value') !== this.oldfeedback.get('value'));
}
return (this.grade.get('value') !== this.oldgrade.get('value'));
};
/**
* Submits any changes and then updates the fields accordingly
*
* @function
* @this {M.gradereport_grader.classes.existingfield}
*/
M.gradereport_grader.classes.existingfield.prototype.submit = function() {
if (!this.has_changed()) {
return;
}
var properties = this.report.get_cell_info([this.userid,this.itemid]);
var values = (function(f){
var feedback, oldfeedback = null;
if (f.editfeedback) {
feedback = f.feedback.get('value');
oldfeedback = f.oldfeedback.get('value');
}
return {
editablefeedback : f.editfeedback,
grade : f.grade.get('value'),
oldgrade : f.oldgrade.get('value'),
feedback : feedback,
oldfeedback : oldfeedback
};
})(this);
if (values.editablefeedback && values.feedback != values.oldfeedback) {
this.report.update_feedback(this.userid, this.itemid, values.feedback);
}
this.report.ajax.submit(properties, values);
};
/**
* Textfield class
* This classes gets used in conjunction with the report running with AJAX enabled
* and is used to manage a cell that has a grade requiring a textfield for input
*
* @class textfield
* @constructor
* @this {M.gradereport_grader.classes.textfield}
* @param {M.gradereport_grader.classes.report} report
* @param {Y.Node} node
*/
M.gradereport_grader.classes.textfield = function(report, node) {
this.report = report;
this.node = node;
this.gradespan = node.one('.gradevalue');
this.inputdiv = this.report.Y.Node.create('<div></div>');
this.editfeedback = this.report.ajax.showquickfeedback;
this.grade = this.report.Y.Node.create('<input type="text" class="text" value="" />');
this.gradetype = 'value';
this.inputdiv.append(this.grade);
if (this.report.ajax.showquickfeedback) {
this.feedback = this.report.Y.Node.create('<input type="text" class="quickfeedback" value="" />');
this.inputdiv.append(this.feedback);
}
};
/**
* Extend the textfield class with the following methods and properties
*/
M.gradereport_grader.classes.textfield.prototype.keyevents = [];
M.gradereport_grader.classes.textfield.prototype.editable = false;
M.gradereport_grader.classes.textfield.prototype.gradetype = null;
M.gradereport_grader.classes.textfield.prototype.grade = null;
M.gradereport_grader.classes.textfield.prototype.report = null;
M.gradereport_grader.classes.textfield.prototype.node = null;
M.gradereport_grader.classes.textfield.prototype.gradespam = null;
M.gradereport_grader.classes.textfield.prototype.inputdiv = null;
M.gradereport_grader.classes.textfield.prototype.editfeedback = false;
/**
* Replaces the cell contents with the controls to enable editing
*
* @function
* @this {M.gradereport_grader.classes.textfield}
* @return {M.gradereport_grader.classes.textfield}
*/
M.gradereport_grader.classes.textfield.prototype.replace = function() {
this.set_grade(this.get_grade());
if (this.editfeedback) {
this.set_feedback(this.get_feedback());
}
this.node.replaceChild(this.inputdiv, this.gradespan);
this.grade.focus();
this.editable = true;
return this;
};
/**
* Commits the changes within a cell and returns a result object of new + old values
* @function
* @this {M.gradereport_grader.classes.textfield}
* @return {Object}
*/
M.gradereport_grader.classes.textfield.prototype.commit = function() {
// Produce an anonymous result object contianing all values
var result = (function(field){
field.editable = false;
var oldgrade = field.get_grade();
if (oldgrade == '-') {
oldgrade = '';
}
var feedback = null;
var oldfeedback = null;
if (field.editfeedback) {
oldfeedback = field.get_feedback();
}
field.editable = true;
if (field.editfeedback) {
feedback = field.get_feedback();
}
return {
gradetype : field.gradetype,
editablefeedback : field.editfeedback,
grade : field.get_grade(),
oldgrade : oldgrade,
feedback : feedback,
oldfeedback : oldfeedback
};
})(this);
// Set the changes in stone
this.set_grade(result.grade);
if (this.editfeedback) {
this.set_feedback(result.feedback);
}
// Return the result object
return result;
};
/**
* Reverts a cell back to its static contents
* @function
* @this {M.gradereport_grader.classes.textfield}
*/
M.gradereport_grader.classes.textfield.prototype.revert = function() {
this.node.replaceChild(this.gradespan, this.inputdiv);
for (var i in this.keyevents) {
if (this.keyevents[i]) {
this.keyevents[i].detach();
}
}
this.keyevents = [];
this.node.on('makeditable|click', this.report.ajax.make_editable, this.report.ajax);
};
/**
* Gets the grade for current cell
*
* @function
* @this {M.gradereport_grader.classes.textfield}
* @return {Mixed}
*/
M.gradereport_grader.classes.textfield.prototype.get_grade = function() {
if (this.editable) {
return this.grade.get('value');
}
return this.gradespan.get('innerHTML');
};
/**
* Sets the grade for the current cell
* @function
* @this {M.gradereport_grader.classes.textfield}
* @param {Mixed} value
*/
M.gradereport_grader.classes.textfield.prototype.set_grade = function(value) {
if (!this.editable) {
if (value == '-') {
value = '';
}
this.grade.set('value', value);
} else {
if (value == '') {
value = '-';
}
this.gradespan.set('innerHTML', value);
}