-
Notifications
You must be signed in to change notification settings - Fork 1
/
coursework.js
296 lines (275 loc) · 13 KB
/
coursework.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
var table_obj_list = [];
var id = 0;
var is_responsive = false;
var tableobject = 0;
$( document ).ready(function() {
var langmessage = JSON.parse($('#element_lang_messages').attr('data-lang'));
var base_url = window.location.origin + '/mod/coursework/datatables/js/';
require.config({
paths: {
'datatables.net': base_url + 'jquery.datatables',
'datatables.searchpanes': base_url + 'datatables.searchpanes',
'datatables.buttons': base_url + 'datatables.buttons',
'datatables.select': base_url + 'datatables.select',
'datatables.responsive': base_url + 'datatables.responsive.min',
}
});
require(['datatables.net'], function (DataTable) {
$.fn.DataTable = DataTable;
$.fn.DataTableSettings = DataTable.settings;
$.fn.dataTableExt = DataTable.ext;
DataTable.$ = $;
$.fn.DataTable = function ( opts ) {
return $(this).dataTable( opts ).api();
};
$.fn.dataTable.Api.register('row().show()', function() {
var page_info = this.table().page.info();
// Get row index.
var new_row_index = this.index();
// Row position.
var row_position = this.table()
.rows({ search: 'applied' })[0]
.indexOf(new_row_index);
// Already on right page ?
if ((row_position >= page_info.start && row_position < page_info.end) || row_position < 0) {
// Return row object.
return this;
}
// Find page number.
var page_to_display = Math.floor(row_position / this.table().page.len());
// Go to that page.
this.table().page(page_to_display);
// Return row object.
return this;
});
require(['datatables.searchpanes', 'datatables.select', 'datatables.buttons', 'datatables.responsive'], function() {
if(isMobileDevice() && $(window).width() < 768) {
is_responsive = true;
initDatatable(is_responsive);
$('.datatabletest').on('order.dt', function(e) {
$('.submissionrowmulti').removeClass("shown");
});
}
else {
initDatatable(is_responsive);
}
});
});
/**
*
* @param tableid
*/
function background_load_table(tableid) {
var tableelement = $('#' + tableid);
var wrapperelement = tableelement.parent('.dataTables_wrapper');
var paginationelement = wrapperelement.find('.dataTables_paginate');
tableobject = table_obj_list[tableid];
var submissionswrapper = tableelement.parent('.dataTables_wrapper');
// Hide buttons.
wrapperelement.find('.dataTables_paginate, .dataTables_info, .dataTables_length, .dataTables_filter').css('visibility', 'hidden');
wrapperelement.find('thead, .dt-button').each(function() {
var me = $(this);
me.css('pointer-events', 'none');
if (me.hasClass('dt-button')) {
me.find('span').html(' ' + me.find('span').html());
}
});
console.log(submissionswrapper);
$('<div id="datatable_top_loading_message" class="text-center submission-loading"><i class="fa fa-spinner fa-spin"></i> ' + langmessage.loadingpagination + '</div>').insertBefore(submissionswrapper);
$('<div class="text-center pagination-loading"><i class="fa fa-spinner fa-spin"></i> ' + langmessage.loadingpagination + '</div>').insertAfter(paginationelement);
$('<i class="fa fa-spinner fa-spin pagination-loading"></i>').insertBefore(wrapperelement.find('.dt-button > span'));
// Prepare params for ajax request.
var params = {
group: tableelement.attr('group'),
perpage: tableelement.attr('perpage'),
sortby: tableelement.attr('sortby'),
sorthow: tableelement.attr('sorthow'),
firstnamealpha: tableelement.attr('firstnamealpha'),
lastnamealpha: tableelement.attr('lastnamealpha'),
groupnamealpha: tableelement.attr('groupnamealpha'),
substatus: tableelement.attr('substatus'),
unallocated: tableelement.attr('unallocated'),
courseworkid: tableelement.attr('courseworkid')
};
$.ajax({
url: '/mod/coursework/actions/ajax/datatable/grading.php',
type: 'POST',
data: params
}).done(function(response) {
console.log('test remove 1');
$("#datatable_top_loading_message").remove();
tableobject.rows.add($(response)).draw(false);
wrapperelement.find('.submission-loading').remove();
}).fail(function() {}).always(function() {
// Show buttons.
wrapperelement.find('.pagination-loading').remove();
wrapperelement.find('.submission-loading').remove();
wrapperelement.find('thead, .dt-button').css('pointer-events', 'auto');
wrapperelement.find('.dataTables_paginate, .dataTables_info, .dataTables_length, .dataTables_filter').css('visibility', 'visible');
});
}
function initDatatable(is_responsive) {
$(".datatabletest").each(function () {
// Class that determines whether all data for the databale has been full loaded.
var fullloaded = $(this).hasClass('full-loaded');
table_obj_list[$(this).attr('id')] = $(this).DataTable( {
'order': [],
stateSave: true,
language: {
searchPanes: {
collapse: {0: $('#search_pane_button').val() || 'Filter', _:($('#search_pane_button').val() || 'Filter') + ' (%d)'}
}
},
buttons:[
],
dom: 'Blfrtip',
columnDefs:[
{
searchPanes:{show: false},
targets: ['studentname','addition-multiple-button'],
bSortable: false
},
{
searchPanes: {show: false},
targets: ['lastname_cell','firstname_cell','tableheadpersonaldeadline', 'tableheaddate', 'tableheadfilename', 'tableheadplagiarismalert', 'plagiarism', 'agreedgrade', 'feedbackandgrading', 'provisionalgrade', 'tableheadmoderationagreement']
},
{
searchPanes:{
show: true,
header: $('#search_pane_group').val() || 'Group',
},
targets: 'tableheadgroups',
},
{
searchPanes:{
show: true,
header: $('#search_pane_status').val() || 'Status',
getFullText: true,
},
targets: 'tableheadstatus',
},
{
searchPanes:{
show: true,
header: $('#search_pane_firstname').val() || 'First Name Initial',
},
targets: 'firstname_letter_cell',
},
{
searchPanes:{
show: true,
header: $('#search_pane_lastname').val() || 'Last Name Initial',
},
targets: 'lastname_letter_cell',
},
{ "visible": false, "targets": [ 'lastname_letter_cell','firstname_letter_cell', 'lastname_cell','firstname_cell'] }
],
select: {
style: 'multi',
selector: '.select-checkbox'
},
stateSaveParams: function (settings, data) {
data.columns = [];
}
});
if (!fullloaded) {
background_load_table($(this).attr('id'));
}
});
}
if(isMobileDevice() && $(window).width() < 768) {
// For small screens.
var table = $('.datatabletest tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest("tr");
var row_id = tr.attr('id').replace('allocatable_', '');
var table_id = 'assessorfeedbacktable_' + row_id;
if ($(tr).next('tr.row_assessors').length > 0) {
$(tr).next('tr.row_assessors').remove();
}
else {
// As originally written this code created a new table with duplicate IDs.
// See comment on CTP-3783 below for more info.
const oldTable = $('#' + table_id);
const newRow = $(
'<tr class = "submissionrowmultisub row_assessors"><td class="assessors" colspan = "11"></td></tr>'
);
oldTable.addClass('assessors_expanded').css('width', '95%').appendTo(newRow.find('td'));
}
$(tr).toggleClass('shown');
});
}
else {
// Add event listener for opening and closing details.
$('.datatabletest tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest("tr");
var table_key = $(this).closest('.datatabletest').attr('id');
var table = table_obj_list[table_key];
if (table) {
var row = table.row( tr );
var row_id = tr.attr('id').replace('allocatable_', '');
var table_id = 'assessorfeedbacktable_' + row_id;
const oldTable = $('#' + table_id);
if (oldTable.length) {
const subRow = $('#sub-row-' + tr.data('allocatable'));
if (subRow.length === 0) {
// Open this row - create as sub-row.
// CTP-3783 As originally written this code cloned the old table and added its HTML again to the new row.
// This meant that we had 2 x tables do duplicate IDs, with the old table hidden and new one visible.
// Then multiple behat tests failed when trying to click the hidden feedback button not visible one.
const newRow = $(
'<tr class = "submissionrowmultisub" id="sub-row-' + tr.data('allocatable')
+ '"><td class="assessors" colspan = "11"></td></tr>'
);
oldTable.addClass('assessors_expanded').css('width', '95%').appendTo(newRow.find('td'));
oldTable.show();
row.child(newRow).show();
tr.addClass('shown');
} else {
// Sub-row already exists.
if (subRow.css('display') === 'none') {
subRow.show();
tr.addClass('shown');
} else {
// This row is already open - close it.
tr.removeClass('shown');
subRow.hide();
}
}
} else {
// No need to move table - just open/close.
if (subRow.css('display') === 'none') {
subRow.show();
tr.addClass('shown');
} else {
// This row is already open - close it.
subRow.hide();
tr.removeClass('shown');
}
}
}
});
}
$('.datatabletest').on('click', '.splitter-firstname, .splitter-lastname, .splitter-email', function (event) {
event.preventDefault();
var node = $(event.target),
isAscending = node.hasClass('sorting_asc'),
currentsort = 'asc', sortby = 'desc';
if (!isAscending) {
currentsort = 'desc';
sortby = 'asc';
}
var table_id = $(this).closest('.datatabletest').attr('id');
table = table_obj_list[table_id];
var headerclass = $(this).hasClass('splitter-firstname') ? 'firstname_cell' : 'lastname_cell';
headerclass = $(this).hasClass('splitter-email') ? 'email_cell' : headerclass;
console.log(headerclass);
var sortColumn = table.column('.' + headerclass).index();
table.order([sortColumn, sortby]).draw();
node.addClass('sorting_' + sortby).removeClass('sorting sorting_' + currentsort);
node.parent().removeClass('sorting sorting_asc sorting_desc');
node.siblings().removeClass('sorting_asc sorting_desc').addClass('sorting');
});
});
function isMobileDevice() {
return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
}