From dbf0f679e0c0d19cb4aa30b3167eb03f5791874f Mon Sep 17 00:00:00 2001 From: Alex Yeung Date: Thu, 19 Dec 2024 10:23:52 +0000 Subject: [PATCH] CTP-4183 Fix JS issues for Moodle 4.5 --- .github/workflows/moodle-ci.yml | 2 +- coursework.js | 61 +- coursework_edit.js | 1413 +++++++++++++++---------------- view.php | 15 + 4 files changed, 731 insertions(+), 760 deletions(-) diff --git a/.github/workflows/moodle-ci.yml b/.github/workflows/moodle-ci.yml index 51ff875..eda1dc3 100644 --- a/.github/workflows/moodle-ci.yml +++ b/.github/workflows/moodle-ci.yml @@ -8,7 +8,7 @@ jobs: services: postgres: - image: postgres:13 + image: postgres:14 env: POSTGRES_USER: 'postgres' POSTGRES_HOST_AUTH_METHOD: 'trust' diff --git a/coursework.js b/coursework.js index fc543eb..e64055e 100644 --- a/coursework.js +++ b/coursework.js @@ -5,61 +5,18 @@ 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); + // Initialize DataTables + if(isMobileDevice() && $(window).width() < 768) { + is_responsive = true; + initDatatable(is_responsive); - $('.datatabletest').on('order.dt', function(e) { - $('.submissionrowmulti').removeClass("shown"); - }); - } - else { - initDatatable(is_responsive); - } + $('.datatabletest').on('order.dt', function(e) { + $('.submissionrowmulti').removeClass("shown"); }); - }); + } else { + initDatatable(is_responsive); + } /** * diff --git a/coursework_edit.js b/coursework_edit.js index a43000e..b0a4bd8 100644 --- a/coursework_edit.js +++ b/coursework_edit.js @@ -22,303 +22,360 @@ window.addEventListener('beforeunload', (event) => { } } }); -$(document).ready(function () { - function log_datatable_navigate(tr) { - var row_id = tr.attr('id'); - var tableid = tr.closest('table').attr('id'); - var key = 'datatable_navigate_' + window.location.href + tableid; - localStorage.setItem(key, row_id); - } - /* plagiarism flag */ - $('.datatabletest').on('click', '.new_plagiarism_flag', function (e) { - log_datatable_navigate($(this).closest('tr')); - }); +require(['jquery'], function($) { + $(document).ready(function() { + function log_datatable_navigate(tr) { + var row_id = tr.attr('id'); + var tableid = tr.closest('table').attr('id'); + var key = 'datatable_navigate_' + window.location.href + tableid; + localStorage.setItem(key, row_id); + } - $('.datatabletest').on('click', '.edit_plagiarism_flag', function (e) { - log_datatable_navigate($(this).closest('tr')); - }); + /* plagiarism flag */ + $('.datatabletest').on('click', '.new_plagiarism_flag', function(e) { + log_datatable_navigate($(this).closest('tr')); + }); - /* feedback */ - $('.datatabletest').on('click', '.new_final_feedback', function (e) { - log_datatable_navigate($(this).closest('tr')); - }); + $('.datatabletest').on('click', '.edit_plagiarism_flag', function(e) { + log_datatable_navigate($(this).closest('tr')); + }); - $('.datatabletest').on('click', '.edit_final_feedback', function (e) { - log_datatable_navigate($(this).closest('tr')); - }); + /* feedback */ + $('.datatabletest').on('click', '.new_final_feedback', function(e) { + log_datatable_navigate($(this).closest('tr')); + }); - $('.datatabletest').on('click', '.show_feedback', function (e) { - log_datatable_navigate($(this).closest('tr')); - }); + $('.datatabletest').on('click', '.edit_final_feedback', function(e) { + log_datatable_navigate($(this).closest('tr')); + }); - /* assessor feedback */ - $('.datatabletest').on('click', '.assessor_feedback_grade .new_feedback', function (e) { - log_datatable_navigate($(this).closest('td.assessors').closest('table.assessors').closest('tr').prev()); - }); + $('.datatabletest').on('click', '.show_feedback', function(e) { + log_datatable_navigate($(this).closest('tr')); + }); - $('.datatabletest').on('click', '.assessor_feedback_grade .show_feedback', function (e) { - log_datatable_navigate($(this).closest('td.assessors').closest('table.assessors').closest('tr').prev()); - }); + /* assessor feedback */ + $('.datatabletest').on('click', '.assessor_feedback_grade .new_feedback', function(e) { + log_datatable_navigate($(this).closest('td.assessors').closest('table.assessors').closest('tr').prev()); + }); - $('.datatabletest').on('click', '.assessor_feedback_grade .edit_feedback', function (e) { - log_datatable_navigate($(this).closest('td.assessors').closest('table.assessors').closest('tr').prev()); - }); + $('.datatabletest').on('click', '.assessor_feedback_grade .show_feedback', function(e) { + log_datatable_navigate($(this).closest('td.assessors').closest('table.assessors').closest('tr').prev()); + }); - /* deadline extension */ - $('.datatabletest').on('click', '.new_deadline_extension', function (e) { - log_datatable_navigate($(this).closest('tr')); - }); + $('.datatabletest').on('click', '.assessor_feedback_grade .edit_feedback', function(e) { + log_datatable_navigate($(this).closest('td.assessors').closest('table.assessors').closest('tr').prev()); + }); - $('.datatabletest').on('click', '.edit_deadline_extension', function (e) { - log_datatable_navigate($(this).closest('tr')); - }); + /* deadline extension */ + $('.datatabletest').on('click', '.new_deadline_extension', function(e) { + log_datatable_navigate($(this).closest('tr')); + }); - /* submission */ - $('.datatabletest').on('click', '.new_submission', function (e) { - log_datatable_navigate($(this).closest('tr')); - }); + $('.datatabletest').on('click', '.edit_deadline_extension', function(e) { + log_datatable_navigate($(this).closest('tr')); + }); - // Prepare Message - var datatables_lang_messages = JSON.parse($('#datatables_lang_messages').attr('data-lang')); + /* submission */ + $('.datatabletest').on('click', '.new_submission', function(e) { + log_datatable_navigate($(this).closest('tr')); + }); -/** - * Personal Deadline - */ - $('.datatabletest').on('click', '.edit_personal_deadline',function (e) { - e.preventDefault(); - var parent = $(this).closest('.personal_deadline_cell'); - parent.children('.show_personal_dealine').addClass('display-none'); - var change_form = parent.children('.show_edit_personal_dealine'); - var data_get = $(this).attr('data-get'); - var data_time = $(this).attr('data-time-iso-8601'); - if (change_form.html().length === 1) { - var form = ''; - form += '
'; - form += '
'; - $(change_form).html(form); - } - $(change_form).removeClass('display-none'); - }); + // Prepare Message + var datatables_lang_messages = JSON.parse($('#datatables_lang_messages').attr('data-lang')); + + /** + * Personal Deadline + */ + $('.datatabletest').on('click', '.edit_personal_deadline', function(e) { + e.preventDefault(); + var parent = $(this).closest('.personal_deadline_cell'); + parent.children('.show_personal_dealine').addClass('display-none'); + var change_form = parent.children('.show_edit_personal_dealine'); + var data_get = $(this).attr('data-get'); + var data_time = $(this).attr('data-time-iso-8601'); + if (change_form.html().length === 1) { + var form = ''; + form += '
'; + form += '
'; + $(change_form).html(form); + } + $(change_form).removeClass('display-none'); + }); - $('.datatabletest').on('click', '.cancel-personal-deadline', function (e) { - e.preventDefault(); - $(this).closest('.show_edit_personal_dealine').addClass('display-none'); - $(this).closest('.personal_deadline_cell').children('.show_personal_dealine').removeClass('display-none'); - }); + $('.datatabletest').on('click', '.cancel-personal-deadline', function(e) { + e.preventDefault(); + $(this).closest('.show_edit_personal_dealine').addClass('display-none'); + $(this).closest('.personal_deadline_cell').children('.show_personal_dealine').removeClass('display-none'); + }); - $('.datatabletest').on('click', '.approve-personal-deadline', function (e) { - e.preventDefault(); - var deadline = $(this); - var data_get = $(deadline).attr('data-get'); - var value = $(deadline).closest('.show_edit_personal_dealine').children('.input-personal-deadline').val(); - var input_date = new Date(value); - if (input_date <= Date.parse(now)) { - (new M.core.alert({ - message: datatables_lang_messages.alert_validate_deadline.replace(/\_/g, ' '), - title: datatables_lang_messages.notification_info, - })).show(); - return 0; - } + $('.datatabletest').on('click', '.approve-personal-deadline', function(e) { + e.preventDefault(); + var deadline = $(this); + var data_get = $(deadline).attr('data-get'); + var value = $(deadline).closest('.show_edit_personal_dealine').children('.input-personal-deadline').val(); + var input_date = new Date(value); + if (input_date <= Date.parse(now)) { + (new M.core.alert({ + message: datatables_lang_messages.alert_validate_deadline.replace(/\_/g, ' '), + title: datatables_lang_messages.notification_info, + })).show(); + return 0; + } - var url = datatables_lang_messages.url_root + "/mod/coursework/actions/personal_deadline.php"; - var param = JSON.parse(data_get); - param.personal_deadline_time = value; - - $.ajax({ - type: "POST", - url: url, - data: param, - beforeSend: function () { - $('html, body').css("cursor", "wait"); - $(self).prev('img').css('visibility', 'visible'); - }, - success: function (response) { - $('html, body').css("cursor", "auto"); - data_response = JSON.parse(response); - if (data_response.error == 1) { - (new M.core.alert({ - message: data_response.message, - title: datatables_lang_messages.notification_info, - })).show(); - } else { - var parent = $(deadline).closest('.personal_deadline_cell'); - $(parent).attr('data-order', data_response.timestamp); - var table = table_obj_list[Object.keys(table_obj_list)[0]]; - table.row('#' + $(parent).closest('tr').attr('id')).invalidate(); + var url = datatables_lang_messages.url_root + "/mod/coursework/actions/personal_deadline.php"; + var param = JSON.parse(data_get); + param.personal_deadline_time = value; - $(parent).children('.show_personal_dealine').children('.content_personal_deadline').html(data_response.time); - $(parent).children('.show_edit_personal_dealine').addClass('display-none'); - $(parent).children('.show_personal_dealine').removeClass('display-none'); - (new M.core.alert({ - message: datatables_lang_messages.alert_personaldeadline_save_successful.replace(/\_/g, ' '), - title: datatables_lang_messages.notification_info, - })).show(); + $.ajax({ + type: "POST", + url: url, + data: param, + beforeSend: function() { + $('html, body').css("cursor", "wait"); + $(self).prev('img').css('visibility', 'visible'); + }, + success: function(response) { + $('html, body').css("cursor", "auto"); + data_response = JSON.parse(response); + if (data_response.error == 1) { + (new M.core.alert({ + message: data_response.message, + title: datatables_lang_messages.notification_info, + })).show(); + } else { + var parent = $(deadline).closest('.personal_deadline_cell'); + $(parent).attr('data-order', data_response.timestamp); + var table = table_obj_list[Object.keys(table_obj_list)[0]]; + table.row('#' + $(parent).closest('tr').attr('id')).invalidate(); + + $(parent).children('.show_personal_dealine').children('.content_personal_deadline').html(data_response.time); + $(parent).children('.show_edit_personal_dealine').addClass('display-none'); + $(parent).children('.show_personal_dealine').removeClass('display-none'); + (new M.core.alert({ + message: datatables_lang_messages.alert_personaldeadline_save_successful.replace(/\_/g, ' '), + title: datatables_lang_messages.notification_info, + })).show(); + } + }, + error: function() { + $('html, body').css("cursor", "auto"); + }, + complete: function() { + $('html, body').css("cursor", "auto"); } - }, - error: function () { - $('html, body').css("cursor", "auto"); - }, - complete: function () { - $('html, body').css("cursor", "auto"); - } + }); + }); - }); + /*************************** + * Extensions + */ + + /** + * Add new extension + */ + $('.datatabletest').on('click', '.new_deadline_extension', function(e) { + e.preventDefault(); + var data_name = $(this).attr('data-name'); + var data_params = JSON.parse($(this).attr('data-params')); + var data_time = JSON.parse($(this).attr('data-time')); + var current_rowid = $(this).closest('tr').attr('id'); + extension_new_change_data_form(data_name, data_params, data_time, current_rowid); + $('#modal-ajax').modal('show'); + }); - /*************************** - * Extensions - */ - - /** - * Add new extension - */ - $('.datatabletest').on('click', '.new_deadline_extension', function (e) { - e.preventDefault(); - var data_name = $(this).attr('data-name'); - var data_params = JSON.parse($(this).attr('data-params')); - var data_time = JSON.parse($(this).attr('data-time')); - var current_rowid = $(this).closest('tr').attr('id'); - extension_new_change_data_form(data_name, data_params, data_time, current_rowid); - $('#modal-ajax').modal('show'); - }); + /** + * Edit extensions + */ + $('.datatabletest').on('click', '.edit_deadline_extension', function(e) { + + e.preventDefault(); + var data_name = $(this).attr('data-name'); + var data_params = JSON.parse($(this).attr('data-params')); + var data_time = JSON.parse($(this).attr('data-time')); + var current_rowid = $(this).closest('tr').attr('id'); + extension_edit_change_data_form(data_name, data_params, data_time, current_rowid); + $('#modal-ajax').modal('show'); + }); - /** - * Edit extensions - */ - $('.datatabletest').on('click', '.edit_deadline_extension', function (e) { - - e.preventDefault(); - var data_name = $(this).attr('data-name'); - var data_params = JSON.parse($(this).attr('data-params')); - var data_time = JSON.parse($(this).attr('data-time')); - var current_rowid = $(this).closest('tr').attr('id'); - extension_edit_change_data_form(data_name, data_params, data_time, current_rowid); - $('#modal-ajax').modal('show'); - }); + /** + * Submit save extension + */ + $('.modal-footer').on('click', '#extension-submit', function(e) { + e.preventDefault(); + var params = {}; + params.allocatabletype = $('#extension-allocatabletype').val(); + params.allocatableid = $('#extension-allocatableid').val(); + params.courseworkid = $('#extension-courseworkid').val(); + params.id = $('#extension-id').val(); + params.extended_deadline = $('#extension-extend-deadline').val(); + params.editor = $('#extension-time-content').html(); + params.text = $('#id_extra_information').val(); + params.submissionid = $('#extension-submissionid').val(); + params.pre_defined_reason = $('#extension-reason-select').val(); + params.requesttype = 'submit'; + current_rowid = $('#button-id').val(); + var url = datatables_lang_messages.url_root; + $.ajax({ + type: "POST", + url: url + "/mod/coursework/actions/ajax/deadline_extension/submit.php", + data: params, + beforeSend: function() { + $('html, body').css("cursor", "wait"); + $('.modal-footer').children('img').css('visibility', 'visible'); + }, + success: function(response) { + var data_response = JSON.parse(response); + $('html, body').css("cursor", "auto"); + $('.modal-footer').children('img').css('visibility', 'hidden'); + if (data_response.error == 1) { + (new M.core.alert({ + message: data_response.messages, + title: datatables_lang_messages.notification_info, + })).show(); + } else { + if (Object.keys(table_obj_list).length > 0) { + // Get the first datatable object. + var table = table_obj_list[Object.keys(table_obj_list)[0]]; + var current_row_data = table.row('#' + current_rowid).data(); + var submissiondateindex = table.column('.tableheaddate').index(); + var current_moderation_cell_data = data_response.content; + current_row_data[submissiondateindex] = current_moderation_cell_data; + var table_row = table.row('#' + current_rowid); + table_row.data(current_row_data); + var dom_row = $('#' + current_rowid); + dom_row.find('.time_submitted_cell').attr('data-order', current_moderation_cell_data['@data-order']); + dom_row.find('.edit_personal_deadline').remove(); + table_row.invalidate(); + $('#extension-id').val(data_response.data.id); + } - /** - * Submit save extension - */ - $('.modal-footer').on('click', '#extension-submit', function (e) { - e.preventDefault(); - var params = {}; - params.allocatabletype = $('#extension-allocatabletype').val(); - params.allocatableid = $('#extension-allocatableid').val(); - params.courseworkid = $('#extension-courseworkid').val(); - params.id = $('#extension-id').val(); - params.extended_deadline = $('#extension-extend-deadline').val(); - params.editor = $('#extension-time-content').html(); - params.text = $('#id_extra_information').val(); - params.submissionid = $('#extension-submissionid').val(); - params.pre_defined_reason = $('#extension-reason-select').val(); - params.requesttype = 'submit'; - current_rowid = $('#button-id').val(); - var url = datatables_lang_messages.url_root; - $.ajax({ - type: "POST", - url: url + "/mod/coursework/actions/ajax/deadline_extension/submit.php", - data: params, - beforeSend: function () { - $('html, body').css("cursor", "wait"); - $('.modal-footer').children('img').css('visibility', 'visible'); - }, - success: function (response) { - var data_response = JSON.parse(response); - $('html, body').css("cursor", "auto"); - $('.modal-footer').children('img').css('visibility', 'hidden'); - if (data_response.error == 1) { - (new M.core.alert({ - message: data_response.messages, - title: datatables_lang_messages.notification_info, - })).show(); - } else { - if (Object.keys(table_obj_list).length > 0) { - // Get the first datatable object. - var table = table_obj_list[Object.keys(table_obj_list)[0]]; - var current_row_data = table.row('#' + current_rowid).data(); - var submissiondateindex = table.column('.tableheaddate').index(); - var current_moderation_cell_data = data_response.content; - current_row_data[submissiondateindex] = current_moderation_cell_data; - var table_row = table.row('#' + current_rowid); - table_row.data(current_row_data); - var dom_row = $('#' + current_rowid); - dom_row.find('.time_submitted_cell').attr('data-order', current_moderation_cell_data['@data-order']); - dom_row.find('.edit_personal_deadline').remove(); - table_row.invalidate(); - $('#extension-id').val(data_response.data.id); + change__status_extension_submit_button(true); + save_extension_form_data(); + + (new M.core.alert({ + message: datatables_lang_messages.alert_extension_save_successful.replace(/\_/g, ' '), + title: datatables_lang_messages.notification_info, + })).show(); } + }, + error: function() { + $('html, body').css("cursor", "auto"); + }, + complete: function() { + $('html, body').css("cursor", "auto"); + } + }); + }); - change__status_extension_submit_button(true); - save_extension_form_data(); - (new M.core.alert({ - message: datatables_lang_messages.alert_extension_save_successful.replace(/\_/g, ' '), - title: datatables_lang_messages.notification_info, - })).show(); - } - }, - error: function () { - $('html, body').css("cursor", "auto"); - }, - complete: function () { - $('html, body').css("cursor", "auto"); + /** + * Function close button + */ + $('#modal-ajax').on('hide.bs.modal', function(e) { + var self = this; + if (is_data_extension_form_change()) { + var confirm = new M.core.confirm({ + title: datatables_lang_messages.notification_leave_form_title.replace(/\_/g, ' '), + question: datatables_lang_messages.notification_leave_form_message.replace(/\_/g, ' '), + yesLabel: datatables_lang_messages.notification_yes_label, + noLabel: datatables_lang_messages.notification_no_label, + }); + + confirm.on('complete-yes', function() { + save_extension_form_data(); + confirm.hide(); + confirm.destroy(); + $(self).modal('hide'); + }); + + confirm.on('complete-no', function() { + confirm.hide(); + confirm.destroy(); + return false; + }); + + confirm.show(); + return false; } + return true; }); - }); + /** + * Function next button + */ + $('.modal-footer').on('click', '#extension-next', function(e) { + e.preventDefault(); + + if (is_data_extension_form_change()) { + var confirm = new M.core.confirm({ + title: datatables_lang_messages.notification_leave_form_title.replace(/\_/g, ' '), + question: datatables_lang_messages.notification_leave_form_message.replace(/\_/g, ' '), + yesLabel: datatables_lang_messages.notification_yes_label, + noLabel: datatables_lang_messages.notification_no_label, + }); + + confirm.on('complete-yes', function() { + confirm.hide(); + confirm.destroy(); + if (Object.keys(table_obj_list).length > 0) { - /** - * Function close button - */ - $('#modal-ajax').on('hide.bs.modal', function (e) { - var self = this; - if(is_data_extension_form_change()) { - var confirm = new M.core.confirm({ - title: datatables_lang_messages.notification_leave_form_title.replace(/\_/g, ' '), - question: datatables_lang_messages.notification_leave_form_message.replace(/\_/g, ' '), - yesLabel: datatables_lang_messages.notification_yes_label, - noLabel: datatables_lang_messages.notification_no_label, - }); + var self = $(this); + var prev_rowid = $('#button-id').val(); - confirm.on('complete-yes',function() { - save_extension_form_data(); - confirm.hide(); - confirm.destroy(); - $(self).modal('hide'); - }); + // Get the first datatable object. + var table = table_obj_list[Object.keys(table_obj_list)[0]]; - confirm.on('complete-no',function() { - confirm.hide(); - confirm.destroy(); - return false; - }); + var prev_row_index = table.row('#' + prev_rowid).index(); + + + var current_row_index = prev_row_index + 1; + + if (table.row(current_row_index)) { + var current_row_data = table.row(current_row_index).data(); + if (current_row_data) { + var current_rowid = table.row(current_row_index).id(); + + var submissiondateindex = table.column('.tableheaddate').index(); + var current_cell_data = current_row_data[submissiondateindex]; + if (current_cell_data) { + var tmp_node = $('
').html(current_cell_data.display); + var submisiondate = $(tmp_node).find('.new_deadline_extension'); + if (submisiondate.length > 0) { + var data_params = JSON.parse(submisiondate.attr('data-params')); + var data_name = submisiondate.attr('data-name'); + var data_time = JSON.parse(submisiondate.attr('data-time')); + extension_new_change_data_form(data_name, data_params, data_time, current_rowid); + } else { + submisiondate = $(tmp_node).find('.edit_deadline_extension'); + var data_params = JSON.parse(submisiondate.attr('data-params')); + var data_name = submisiondate.attr('data-name'); + var data_time = JSON.parse(submisiondate.attr('data-time')); + extension_edit_change_data_form(data_name, data_params, data_time, current_rowid); + } + } + } else { + $('#extension-next').prop('disabled', true); + (new M.core.alert({ + message: datatables_lang_messages.alert_no_extension.replace(/\_/g, ' '), + title: datatables_lang_messages.notification_info, + })).show(); + } + } + } + }); - confirm.show(); - return false; - } - return true; - }); + confirm.on('complete-no', function() { + confirm.hide(); + confirm.destroy(); - /** - * Function next button - */ - $('.modal-footer').on('click', '#extension-next', function (e) { - e.preventDefault(); - - if (is_data_extension_form_change()) { - var confirm = new M.core.confirm({ - title: datatables_lang_messages.notification_leave_form_title.replace(/\_/g, ' '), - question: datatables_lang_messages.notification_leave_form_message.replace(/\_/g, ' '), - yesLabel: datatables_lang_messages.notification_yes_label, - noLabel: datatables_lang_messages.notification_no_label, - }); + }); - confirm.on('complete-yes', function () { - confirm.hide(); - confirm.destroy(); + confirm.show(); + } else { if (Object.keys(table_obj_list).length > 0) { var self = $(this); @@ -327,15 +384,16 @@ $(document).ready(function () { // Get the first datatable object. var table = table_obj_list[Object.keys(table_obj_list)[0]]; - var prev_row_index = table.row('#' + prev_rowid).index(); + var ordereddata = table.rows({order: 'applied', search: 'applied'}).data().toArray(); + var prev_row_index = ordereddata.findIndex(compare_row, prev_rowid); var current_row_index = prev_row_index + 1; if (table.row(current_row_index)) { - var current_row_data = table.row(current_row_index).data(); - if (current_row_data) { - var current_rowid = table.row(current_row_index).id(); + var current_row_data = ordereddata[current_row_index]; + if (typeof current_row_data != 'undefined') { + var current_rowid = current_row_data.DT_RowId; var submissiondateindex = table.column('.tableheaddate').index(); var current_cell_data = current_row_data[submissiondateindex]; @@ -355,94 +413,90 @@ $(document).ready(function () { extension_edit_change_data_form(data_name, data_params, data_time, current_rowid); } } - } - else { + } else { $('#extension-next').prop('disabled', true); (new M.core.alert({ - message: datatables_lang_messages.alert_no_extension.replace(/\_/g, ' '), + message: datatables_lang_messages.alert_no_mitigation.replace(/\_/g, ' '), title: datatables_lang_messages.notification_info, })).show(); } } } - }); - - confirm.on('complete-no', function () { - confirm.hide(); - confirm.destroy(); - - }); - - confirm.show(); - } else { - if (Object.keys(table_obj_list).length > 0) { - - var self = $(this); - var prev_rowid = $('#button-id').val(); + } - // Get the first datatable object. - var table = table_obj_list[Object.keys(table_obj_list)[0]]; - var ordereddata = table.rows( { order: 'applied', search: 'applied' } ).data().toArray(); - var prev_row_index = ordereddata.findIndex(compare_row, prev_rowid); + }); + /** + * Function back button + */ + $('.modal-footer').on('click', '#extension-back', function(e) { + e.preventDefault(); + if (is_data_extension_form_change()) { + var confirm = new M.core.confirm({ + title: datatables_lang_messages.notification_leave_form_title.replace(/\_/g, ' '), + question: datatables_lang_messages.notification_leave_form_message.replace(/\_/g, ' '), + yesLabel: datatables_lang_messages.notification_yes_label, + noLabel: datatables_lang_messages.notification_no_label, + }); + + confirm.on('complete-yes', function() { + confirm.hide(); + confirm.destroy(); + if (Object.keys(table_obj_list).length > 0) { - var current_row_index = prev_row_index + 1; + var self = $(this); + var prev_rowid = $('#button-id').val(); - if (table.row(current_row_index)) { - var current_row_data = ordereddata[current_row_index]; - if (typeof current_row_data != 'undefined') { - var current_rowid = current_row_data.DT_RowId; + // Get the first datatable object. + var table = table_obj_list[Object.keys(table_obj_list)[0]]; - var submissiondateindex = table.column('.tableheaddate').index(); - var current_cell_data = current_row_data[submissiondateindex]; - if (current_cell_data) { - var tmp_node = $('
').html(current_cell_data.display); - var submisiondate = $(tmp_node).find('.new_deadline_extension'); - if (submisiondate.length > 0) { - var data_params = JSON.parse(submisiondate.attr('data-params')); - var data_name = submisiondate.attr('data-name'); - var data_time = JSON.parse(submisiondate.attr('data-time')); - extension_new_change_data_form(data_name, data_params, data_time, current_rowid); + var prev_row_index = table.row('#' + prev_rowid).index(); + + + var current_row_index = prev_row_index - 1; + + if (table.row(current_row_index)) { + var current_row_data = table.row(current_row_index).data(); + if (current_row_data) { + var current_rowid = table.row(current_row_index).id(); + + var submissiondateindex = table.column('.tableheaddate').index(); + var current_cell_data = current_row_data[submissiondateindex]; + if (current_cell_data) { + var tmp_node = $('
').html(current_cell_data.display); + var submisiondate = $(tmp_node).find('.new_deadline_extension'); + if (submisiondate.length > 0) { + var data_params = JSON.parse(submisiondate.attr('data-params')); + var data_name = submisiondate.attr('data-name'); + var data_time = JSON.parse(submisiondate.attr('data-time')); + extension_new_change_data_form(data_name, data_params, data_time, current_rowid); + } else { + submisiondate = $(tmp_node).find('.edit_deadline_extension'); + var data_params = JSON.parse(submisiondate.attr('data-params')); + var data_name = submisiondate.attr('data-name'); + var data_time = JSON.parse(submisiondate.attr('data-time')); + extension_edit_change_data_form(data_name, data_params, data_time, current_rowid); + } + } } else { - submisiondate = $(tmp_node).find('.edit_deadline_extension'); - var data_params = JSON.parse(submisiondate.attr('data-params')); - var data_name = submisiondate.attr('data-name'); - var data_time = JSON.parse(submisiondate.attr('data-time')); - extension_edit_change_data_form(data_name, data_params, data_time, current_rowid); + $('#extension-back').prop('disabled', true); + (new M.core.alert({ + message: datatables_lang_messages.alert_no_mitigation.replace(/\_/g, ' '), + title: datatables_lang_messages.notification_info, + })).show(); } } } - else { - $('#extension-next').prop('disabled', true); - (new M.core.alert({ - message: datatables_lang_messages.alert_no_mitigation.replace(/\_/g, ' '), - title: datatables_lang_messages.notification_info, - })).show(); - } - } - } - } + }); + confirm.on('complete-no', function() { + confirm.hide(); + confirm.destroy(); + }); - }); - - /** - * Function back button - */ - $('.modal-footer').on('click', '#extension-back', function (e) { - e.preventDefault(); - if (is_data_extension_form_change()) { - var confirm = new M.core.confirm({ - title: datatables_lang_messages.notification_leave_form_title.replace(/\_/g, ' '), - question: datatables_lang_messages.notification_leave_form_message.replace(/\_/g, ' '), - yesLabel: datatables_lang_messages.notification_yes_label, - noLabel: datatables_lang_messages.notification_no_label, - }); - - confirm.on('complete-yes', function () { - confirm.hide(); - confirm.destroy(); + confirm.show(); + } else { if (Object.keys(table_obj_list).length > 0) { var self = $(this); @@ -451,15 +505,16 @@ $(document).ready(function () { // Get the first datatable object. var table = table_obj_list[Object.keys(table_obj_list)[0]]; - var prev_row_index = table.row('#' + prev_rowid).index(); + var ordereddata = table.rows({order: 'applied', search: 'applied'}).data().toArray(); + var prev_row_index = ordereddata.findIndex(compare_row, prev_rowid); var current_row_index = prev_row_index - 1; if (table.row(current_row_index)) { - var current_row_data = table.row(current_row_index).data(); - if (current_row_data) { - var current_rowid = table.row(current_row_index).id(); + var current_row_data = ordereddata[current_row_index]; + if (typeof current_row_data != 'undefined') { + var current_rowid = current_row_data.DT_RowId; var submissiondateindex = table.column('.tableheaddate').index(); var current_cell_data = current_row_data[submissiondateindex]; @@ -479,8 +534,7 @@ $(document).ready(function () { extension_edit_change_data_form(data_name, data_params, data_time, current_rowid); } } - } - else { + } else { $('#extension-back').prop('disabled', true); (new M.core.alert({ message: datatables_lang_messages.alert_no_mitigation.replace(/\_/g, ' '), @@ -489,427 +543,372 @@ $(document).ready(function () { } } } - }); - - confirm.on('complete-no', function () { - confirm.hide(); - confirm.destroy(); - }); - - confirm.show(); - } else { - if (Object.keys(table_obj_list).length > 0) { - - var self = $(this); - var prev_rowid = $('#button-id').val(); - - // Get the first datatable object. - var table = table_obj_list[Object.keys(table_obj_list)[0]]; - - var ordereddata = table.rows( { order: 'applied', search: 'applied' } ).data().toArray(); - var prev_row_index = ordereddata.findIndex(compare_row, prev_rowid); - - - var current_row_index = prev_row_index - 1; - - if (table.row(current_row_index)) { - var current_row_data = ordereddata[current_row_index]; - if (typeof current_row_data != 'undefined') { - var current_rowid = current_row_data.DT_RowId; - - var submissiondateindex = table.column('.tableheaddate').index(); - var current_cell_data = current_row_data[submissiondateindex]; - if (current_cell_data) { - var tmp_node = $('
').html(current_cell_data.display); - var submisiondate = $(tmp_node).find('.new_deadline_extension'); - if (submisiondate.length > 0) { - var data_params = JSON.parse(submisiondate.attr('data-params')); - var data_name = submisiondate.attr('data-name'); - var data_time = JSON.parse(submisiondate.attr('data-time')); - extension_new_change_data_form(data_name, data_params, data_time, current_rowid); - } else { - submisiondate = $(tmp_node).find('.edit_deadline_extension'); - var data_params = JSON.parse(submisiondate.attr('data-params')); - var data_name = submisiondate.attr('data-name'); - var data_time = JSON.parse(submisiondate.attr('data-time')); - extension_edit_change_data_form(data_name, data_params, data_time, current_rowid); - } - } - } - else { - $('#extension-back').prop('disabled', true); - (new M.core.alert({ - message: datatables_lang_messages.alert_no_mitigation.replace(/\_/g, ' '), - title: datatables_lang_messages.notification_info, - })).show(); - } - } } - } - }); - - function extension_edit_change_data_form(data_name, data_params, data_time, current_rowid) { - var title = 'Editing the extension for ' + data_name; - var time_content = 'Default deadline: ' + data_time.time_content; - $('#extension-modal-title').html(title); - $('#form-extension').find('input[type=hidden]').val(""); - $('#form-extension').find('textarea').val(""); - $('#button-id').val(current_rowid); - $('#extension-submissionid').val(data_params.submissionid); - $('#extension-name').val(data_name); - data_params.requesttype = 'edit'; - var url = datatables_lang_messages.url_root; - $.ajax({ - type: "GET", - url: url + "/mod/coursework/actions/ajax/deadline_extension/edit.php", - data: data_params, - beforeSend: function () { - change__status_extension_submit_button(true); - $('html, body').css("cursor", "wait"); - $('.modal-footer').children('img').css('visibility', 'visible'); - }, - success: function (response) { - var data_response = JSON.parse(response); - $('html, body').css("cursor", "auto"); - $('.modal-footer').children('img').css('visibility', 'hidden'); - if (data_response.error == 1) { - (new M.core.alert({ - message: data_response.message + ' .Please reload the page!', - title: datatables_lang_messages.notification_info, - })).show(); - } else { - var extension = data_response.data; - if (extension.time_content) { - $('#extension-time-content').html(extension.time_content); - } else { - $('#extension-time-content').html(time_content); - } - document.getElementById('extension-extend-deadline').value = data_response.data.time_iso_8601.slice(0,16); - $('#extension-reason-select').val(extension.pre_defined_reason); - $('#extension-allocatabletype').val(extension.allocatabletype); - $('#extension-allocatableid').val(extension.allocatableid); - $('#extension-courseworkid').val(extension.courseworkid); - $('#extension-id').val(extension.id); - - $('#id_extra_information').val(extension.text); - - $('#id_extra_information').prop('disabled', false); - $('#extension-extend-deadline').prop('disabled', false); - $('#extension-reason-select').prop('disabled', false); - save_extension_form_data(); - } - }, - error: function () { - $('html, body').css("cursor", "auto"); - change__status_extension_submit_button(false); - }, - complete: function () { - $('html, body').css("cursor", "auto"); - change__status_extension_submit_button(false); - } }); - } - - function extension_new_change_data_form(data_name, data_params, data_time, current_rowid) { - var title = 'New extension for ' + data_name; - $('#extension-modal-title').html(title); - $('#form-extension').find('input[type=hidden]').val(''); - $('#form-extension').find('textarea').val(''); - if(data_time.is_have_deadline == '1') { + function extension_edit_change_data_form(data_name, data_params, data_time, current_rowid) { + var title = 'Editing the extension for ' + data_name; + var time_content = 'Default deadline: ' + data_time.time_content; + $('#extension-modal-title').html(title); + $('#form-extension').find('input[type=hidden]').val(""); + $('#form-extension').find('textarea').val(""); + $('#button-id').val(current_rowid); + $('#extension-submissionid').val(data_params.submissionid); + $('#extension-name').val(data_name); + data_params.requesttype = 'edit'; var url = datatables_lang_messages.url_root; $.ajax({ type: "GET", - url: url + "/mod/coursework/actions/ajax/deadline_extension/new.php", + url: url + "/mod/coursework/actions/ajax/deadline_extension/edit.php", data: data_params, - beforeSend: function () { + beforeSend: function() { change__status_extension_submit_button(true); $('html, body').css("cursor", "wait"); $('.modal-footer').children('img').css('visibility', 'visible'); }, - success: function (response) { + success: function(response) { + var data_response = JSON.parse(response); $('html, body').css("cursor", "auto"); $('.modal-footer').children('img').css('visibility', 'hidden'); - var data_response = JSON.parse(response); - $('#extension-time-content').html(data_response.data.time_content); - document.getElementById('extension-extend-deadline').value = data_response.data.time_iso_8601.slice(0,16); - save_extension_form_data(); + if (data_response.error == 1) { + (new M.core.alert({ + message: data_response.message + ' .Please reload the page!', + title: datatables_lang_messages.notification_info, + })).show(); + } else { + var extension = data_response.data; + if (extension.time_content) { + $('#extension-time-content').html(extension.time_content); + } else { + $('#extension-time-content').html(time_content); + } + document.getElementById('extension-extend-deadline').value = data_response.data.time_iso_8601.slice(0, 16); + $('#extension-reason-select').val(extension.pre_defined_reason); + $('#extension-allocatabletype').val(extension.allocatabletype); + $('#extension-allocatableid').val(extension.allocatableid); + $('#extension-courseworkid').val(extension.courseworkid); + $('#extension-id').val(extension.id); + + $('#id_extra_information').val(extension.text); + + $('#id_extra_information').prop('disabled', false); + $('#extension-extend-deadline').prop('disabled', false); + $('#extension-reason-select').prop('disabled', false); + save_extension_form_data(); + } }, - error: function () { + error: function() { $('html, body').css("cursor", "auto"); + change__status_extension_submit_button(false); }, - complete: function () { + complete: function() { $('html, body').css("cursor", "auto"); + change__status_extension_submit_button(false); } }); - } else { - save_extension_form_data(); } - $('#extension-reason-select').val(''); - $('#extension-allocatabletype').val(data_params.allocatabletype); - $('#extension-allocatableid').val(data_params.allocatableid); - $('#extension-courseworkid').val(data_params.courseworkid); - $('#extension-submissionid').val(data_params.submissionid); - $('#extension-name').val(data_name); - $('#button-id').val(current_rowid); - - $('#id_extra_information').prop('disabled', false); - $('#extension-extend-deadline').prop('disabled', false); - $('#extension-reason-select').prop('disabled', false); - } + + function extension_new_change_data_form(data_name, data_params, data_time, current_rowid) { + var title = 'New extension for ' + data_name; + $('#extension-modal-title').html(title); + $('#form-extension').find('input[type=hidden]').val(''); + $('#form-extension').find('textarea').val(''); + + if (data_time.is_have_deadline == '1') { + var url = datatables_lang_messages.url_root; + $.ajax({ + type: "GET", + url: url + "/mod/coursework/actions/ajax/deadline_extension/new.php", + data: data_params, + beforeSend: function() { + change__status_extension_submit_button(true); + $('html, body').css("cursor", "wait"); + $('.modal-footer').children('img').css('visibility', 'visible'); + }, + success: function(response) { + $('html, body').css("cursor", "auto"); + $('.modal-footer').children('img').css('visibility', 'hidden'); + var data_response = JSON.parse(response); + $('#extension-time-content').html(data_response.data.time_content); + document.getElementById('extension-extend-deadline').value = data_response.data.time_iso_8601.slice(0, 16); + save_extension_form_data(); + }, + error: function() { + $('html, body').css("cursor", "auto"); + }, + complete: function() { + $('html, body').css("cursor", "auto"); + } + }); + } else { + save_extension_form_data(); + } + $('#extension-reason-select').val(''); + $('#extension-allocatabletype').val(data_params.allocatabletype); + $('#extension-allocatableid').val(data_params.allocatableid); + $('#extension-courseworkid').val(data_params.courseworkid); + $('#extension-submissionid').val(data_params.submissionid); + $('#extension-name').val(data_name); + $('#button-id').val(current_rowid); + + $('#id_extra_information').prop('disabled', false); + $('#extension-extend-deadline').prop('disabled', false); + $('#extension-reason-select').prop('disabled', false); + } - $("#form-extension :input").change(function () { - extension_form_change = true; - change__status_extension_submit_button(false); - }); + $("#form-extension :input").change(function() { + extension_form_change = true; + change__status_extension_submit_button(false); + }); - function change__status_extension_submit_button(status) { - $('#extension-submit').prop('disabled', status); - } + function change__status_extension_submit_button(status) { + $('#extension-submit').prop('disabled', status); + } - function save_extension_form_data() { - extension_form_change = false; - } + function save_extension_form_data() { + extension_form_change = false; + } - function is_data_extension_form_change() { - return extension_form_change; - } + function is_data_extension_form_change() { + return extension_form_change; + } - /** - * Feedback - */ - $('.datatabletest').on('click', '.new_final_feedback, .new_feedback, .edit_final_feedback, .edit_feedback, .show_feedback', function (e) { - e.preventDefault(); - var url = $(this).attr('href'); - $.ajax({ - type: "GET", - url: url+'&ajax=1' - }).done(function(response) { - response = $.parseJSON(response); - var modalbody = $('#modal-grading').find('.modal-body'); - // Careful as not all requests return a response.success value. Only if it's false, show error. - if ((response.success ?? true) === false && (response.message ?? null)) { - modalbody.html(response.message); - } else { - modalbody.html(response.formhtml); - var filemanager = modalbody.find('.filemanager'); - if (response.filemanageroptions && filemanager.length) { - var elementid = filemanager.attr('id'); - var clientid = elementid.substr(12); - if (clientid) { - response.filemanageroptions.client_id = clientid; - M.form_filemanager.init(Y, response.filemanageroptions); + /** + * Feedback + */ + $('.datatabletest').on('click', '.new_final_feedback, .new_feedback, .edit_final_feedback, .edit_feedback, .show_feedback', function(e) { + e.preventDefault(); + var url = $(this).attr('href'); + $.ajax({ + type: "GET", + url: url + '&ajax=1' + }).done(function(response) { + response = $.parseJSON(response); + var modalbody = $('#modal-grading').find('.modal-body'); + // Careful as not all requests return a response.success value. Only if it's false, show error. + if ((response.success ?? true) === false && (response.message ?? null)) { + modalbody.html(response.message); + } else { + modalbody.html(response.formhtml); + var filemanager = modalbody.find('.filemanager'); + if (response.filemanageroptions && filemanager.length) { + var elementid = filemanager.attr('id'); + var clientid = elementid.substr(12); + if (clientid) { + response.filemanageroptions.client_id = clientid; + M.form_filemanager.init(Y, response.filemanageroptions); + } } - } - if (response.editoroptions) { - require(['editor_tiny/editor'], (Tiny) => { - Tiny.setupForElementId({ - elementId: 'id_feedbackcomment', - options: JSON.parse(response.editoroptions), + if (response.editoroptions) { + require(['editor_tiny/editor'], (Tiny) => { + Tiny.setupForElementId({ + elementId: 'id_feedbackcomment', + options: JSON.parse(response.editoroptions), + }); }); - }); - } + } - if (response.commentoptions) { - M.util.js_pending('gradingform_guide/comment_chooser'); - require(['gradingform_guide/comment_chooser'], function(amd) { - $(".remark").each( function (i,ele) { - buttonele = $(ele).find(".commentchooser"); - textele = $(ele).find(".markingguideremark"); - buttonid = $(buttonele).attr("id"); - textid = $(textele).attr("id"); - amd.initialise(1, buttonid, textid, response.commentoptions); - M.util.js_complete('gradingform_guide/comment_chooser'); + if (response.commentoptions) { + M.util.js_pending('gradingform_guide/comment_chooser'); + require(['gradingform_guide/comment_chooser'], function(amd) { + $(".remark").each(function(i, ele) { + buttonele = $(ele).find(".commentchooser"); + textele = $(ele).find(".markingguideremark"); + buttonid = $(buttonele).attr("id"); + textid = $(textele).attr("id"); + amd.initialise(1, buttonid, textid, response.commentoptions); + M.util.js_complete('gradingform_guide/comment_chooser'); - }) + }) - }); + }); + } } - } + }); + var cell_td = $(this).closest('td'); + var cell_selector = get_td_cell_selector(cell_td); + var cell_type = cell_td.attr('data-class-name'); + show_loading_modal_grading(cell_selector, cell_type); }); - var cell_td = $(this).closest('td'); - var cell_selector = get_td_cell_selector(cell_td); - var cell_type = cell_td.attr('data-class-name'); - show_loading_modal_grading(cell_selector, cell_type); - }); - - /** - * - * @param td_cell - * @returns {string} - */ - function get_td_cell_selector(td_cell) { - var result = '.' + td_cell.attr('class').replaceAll(' ', '.'); - var tr_cell = td_cell.closest('tr'); - if (tr_cell.attr('id')) { - result = '#' + tr_cell.attr('id') + ' ' + result; - } else { - result = '.' + tr_cell.attr('class').replaceAll(' ', '.') + ' ' + result; - } - return result; - } - - /** - * - * @param cell_selector - * @param cell_type - */ - function show_loading_modal_grading(cell_selector, cell_type) { - // Set row id. - var modal = $('#modal-grading'); - modal.find('#cell_selector').val(cell_selector); - modal.find('#cell_type').val(cell_type); - modal.find('.modal-body').html(' loading'); - $('#modal-grading').modal('show'); - } - $('#modal-grading').on('click', '#id_submitfeedbackbutton, #id_submitbutton', function (e) { - e.preventDefault(); - var button = $(this); - button.prop('disabled', true); - var submitbutton = (button.attr('id') == 'id_submitbutton') ? 1 : 0; - var removefeedbackbutton = (button.attr('id') == 'id_removefeedbackbutton') ? 1 : 0; - var submitfeedbackbutton = (button.attr('id') == 'id_submitfeedbackbutton') ? 1 : 0; - var modal = $('#modal-grading'); - var url = '/mod/coursework/actions/feedbacks/create.php'; - var form_data = modal.find('form').serializeArray(); - for (var i = 0, length = form_data.length; i < length; i++) { - if (form_data[i].name == 'feedbackid' && !isNaN(parseInt(form_data[i].value)) && form_data[i].value != '0') { - url = '/mod/coursework/actions/feedbacks/update.php'; - break; - } - } - var cell_type = modal.find('#cell_type').val(); - update_feedback(form_data,url,cell_type,submitbutton,removefeedbackbutton,submitfeedbackbutton,0,button); - /* - form_data = form_data.concat({name: 'ajax', value: 1}, - {name: 'cell_type', value: cell_type}, - {name: 'submitbutton', value: submitbutton}, - {name: 'removefeedbackbutton', value: removefeedbackbutton}); - $.ajax({ - type: 'POST', - data: form_data, - url: url, - dataType: 'json' - }).done(function(response) { - console.log(response); - if (response.success) { - var cell_selector = modal.find('#cell_selector').val(); - $(cell_selector).html(response.html); - $('#modal-grading').modal('hide'); - if (submitbutton == 1) { - alert('Your data has been saved.'); - } else { - alert('The feedback has been removed.'); - } + /** + * + * @param td_cell + * @returns {string} + */ + function get_td_cell_selector(td_cell) { + var result = '.' + td_cell.attr('class').replaceAll(' ', '.'); + var tr_cell = td_cell.closest('tr'); + if (tr_cell.attr('id')) { + result = '#' + tr_cell.attr('id') + ' ' + result; } else { - alert('Sorry! There was an error with your request.'); + result = '.' + tr_cell.attr('class').replaceAll(' ', '.') + ' ' + result; } - }).always(function() { - me.prop('disabled', false); - });*/ - }); + return result; + } - $('#modal-grading').on('click', '#id_cancel', function (e) { - e.preventDefault(); - $('#modal-grading').modal('hide'); - }); + /** + * + * @param cell_selector + * @param cell_type + */ + function show_loading_modal_grading(cell_selector, cell_type) { + // Set row id. + var modal = $('#modal-grading'); + modal.find('#cell_selector').val(cell_selector); + modal.find('#cell_type').val(cell_type); + modal.find('.modal-body').html(' loading'); + $('#modal-grading').modal('show'); + } - $('#modal-grading').on('click', '#id_removefeedbackbutton', function (e) { - e.preventDefault(); - var button = $(this); - button.prop('disabled', true); - if (confirm('do you want to remove feedback')) { + $('#modal-grading').on('click', '#id_submitfeedbackbutton, #id_submitbutton', function(e) { + e.preventDefault(); + var button = $(this); + button.prop('disabled', true); var submitbutton = (button.attr('id') == 'id_submitbutton') ? 1 : 0; var removefeedbackbutton = (button.attr('id') == 'id_removefeedbackbutton') ? 1 : 0; var submitfeedbackbutton = (button.attr('id') == 'id_submitfeedbackbutton') ? 1 : 0; var modal = $('#modal-grading'); - var url = '/mod/coursework/actions/feedbacks/update.php'; + var url = '/mod/coursework/actions/feedbacks/create.php'; var form_data = modal.find('form').serializeArray(); - var cell_type = modal.find('#cell_type').val(); - update_feedback(form_data, url, cell_type, submitbutton, removefeedbackbutton, submitfeedbackbutton,1, button); - } - }); - - function update_feedback(form_data,url,celltype,submitbutton,removefeedbackbutton,submitfeedbackbutton,confirm,button) { - - form_data = form_data.concat({name: 'ajax', value: 1}, - {name: 'cell_type', value: celltype}, - {name: 'submitbutton', value: submitbutton}, - {name: 'submitfeedbackbutton', value: submitfeedbackbutton}, - {name: 'removefeedbackbutton', value: removefeedbackbutton}, - {name: 'confirm', value: confirm}); - - $.ajax({ - type: 'POST', - data: form_data, - url: url, - dataType: 'json' - }).done(function(response) { - if ((response.success ?? true) === false && (response.message ?? null)) { - // Could be an error like "Please provide a valid grade for each criterion". - $('#modal-grading').find('.modal-body').html( - '
' + response.message + '
' - ); - } else if (response.success) { - var cell_selector = $('#modal-grading').find('#cell_selector').val(); - $(cell_selector).html(response.html); - - if (typeof response.extrahtml !== 'undefined' && response.extrahtml != '') { - $(cell_selector).next('td').html(response.extrahtml); + for (var i = 0, length = form_data.length; i < length; i++) { + if (form_data[i].name == 'feedbackid' && !isNaN(parseInt(form_data[i].value)) && form_data[i].value != '0') { + url = '/mod/coursework/actions/feedbacks/update.php'; + break; } - if (typeof response.assessdate !== 'undefined' && response.assessdate != '') { - $(cell_selector).next('td').html(response.assessdate); + } + var cell_type = modal.find('#cell_type').val(); + update_feedback(form_data, url, cell_type, submitbutton, removefeedbackbutton, submitfeedbackbutton, 0, button); + /* + form_data = form_data.concat({name: 'ajax', value: 1}, + {name: 'cell_type', value: cell_type}, + {name: 'submitbutton', value: submitbutton}, + {name: 'removefeedbackbutton', value: removefeedbackbutton}); + $.ajax({ + type: 'POST', + data: form_data, + url: url, + dataType: 'json' + }).done(function(response) { + console.log(response); + if (response.success) { + var cell_selector = modal.find('#cell_selector').val(); + $(cell_selector).html(response.html); + $('#modal-grading').modal('hide'); + if (submitbutton == 1) { + alert('Your data has been saved.'); + } else { + alert('The feedback has been removed.'); + } + } else { + alert('Sorry! There was an error with your request.'); } + }).always(function() { + me.prop('disabled', false); + });*/ + }); - if (typeof response.assessorname !== 'undefined' && response.assessorname != '') { - $(cell_selector).prev('td').html(response.assessorname); - } + $('#modal-grading').on('click', '#id_cancel', function(e) { + e.preventDefault(); + $('#modal-grading').modal('hide'); + }); - if (typeof response.assessortwo !== 'undefined' && response.assessortwo != '') { - var tdcell = $(cell_selector).closest('tr').next().find('td')[1]; - $(tdcell).html(response.assessortwo); - } + $('#modal-grading').on('click', '#id_removefeedbackbutton', function(e) { + e.preventDefault(); + var button = $(this); + button.prop('disabled', true); + if (confirm('do you want to remove feedback')) { + var submitbutton = (button.attr('id') == 'id_submitbutton') ? 1 : 0; + var removefeedbackbutton = (button.attr('id') == 'id_removefeedbackbutton') ? 1 : 0; + var submitfeedbackbutton = (button.attr('id') == 'id_submitfeedbackbutton') ? 1 : 0; + var modal = $('#modal-grading'); + var url = '/mod/coursework/actions/feedbacks/update.php'; + var form_data = modal.find('form').serializeArray(); + var cell_type = modal.find('#cell_type').val(); + update_feedback(form_data, url, cell_type, submitbutton, removefeedbackbutton, submitfeedbackbutton, 1, button); + } + }); - if (typeof response.finalhtml !== 'undefined' && response.assessortwo != '') { - var tablerowid = 'allocatable_' + response.allocatableid; - var tdcell2 = $('#' + tablerowid).find('.multiple_agreed_grade_cell')[0]; - $(tdcell2).html(response.finalhtml); - } - var datatables_lang_messages_two = JSON.parse($('#datatables_lang_messages').attr('data-lang')); - $('#modal-grading').modal('hide') - if (submitbutton === 1) { - (new M.core.alert({ - message: datatables_lang_messages_two.alert_feedback_save_successful.replace(/\_/g, ' '), - title: datatables_lang_messages_two.notification_info, - })).show(); + function update_feedback(form_data, url, celltype, submitbutton, removefeedbackbutton, submitfeedbackbutton, confirm, button) { - } else if (submitfeedbackbutton === 1) { - (new M.core.alert({ - message: datatables_lang_messages_two.alert_feedback_draft_save_successful.replace(/\_/g, ' '), - title: datatables_lang_messages_two.notification_info, - })).show(); + form_data = form_data.concat({name: 'ajax', value: 1}, + {name: 'cell_type', value: celltype}, + {name: 'submitbutton', value: submitbutton}, + {name: 'submitfeedbackbutton', value: submitfeedbackbutton}, + {name: 'removefeedbackbutton', value: removefeedbackbutton}, + {name: 'confirm', value: confirm}); + + $.ajax({ + type: 'POST', + data: form_data, + url: url, + dataType: 'json' + }).done(function(response) { + if ((response.success ?? true) === false && (response.message ?? null)) { + // Could be an error like "Please provide a valid grade for each criterion". + $('#modal-grading').find('.modal-body').html( + '
' + response.message + '
' + ); + } else if (response.success) { + var cell_selector = $('#modal-grading').find('#cell_selector').val(); + $(cell_selector).html(response.html); + + if (typeof response.extrahtml !== 'undefined' && response.extrahtml != '') { + $(cell_selector).next('td').html(response.extrahtml); + } + if (typeof response.assessdate !== 'undefined' && response.assessdate != '') { + $(cell_selector).next('td').html(response.assessdate); + } + + if (typeof response.assessorname !== 'undefined' && response.assessorname != '') { + $(cell_selector).prev('td').html(response.assessorname); + } + + if (typeof response.assessortwo !== 'undefined' && response.assessortwo != '') { + var tdcell = $(cell_selector).closest('tr').next().find('td')[1]; + $(tdcell).html(response.assessortwo); + } + + if (typeof response.finalhtml !== 'undefined' && response.assessortwo != '') { + var tablerowid = 'allocatable_' + response.allocatableid; + var tdcell2 = $('#' + tablerowid).find('.multiple_agreed_grade_cell')[0]; + $(tdcell2).html(response.finalhtml); + } + var datatables_lang_messages_two = JSON.parse($('#datatables_lang_messages').attr('data-lang')); + $('#modal-grading').modal('hide') + if (submitbutton === 1) { + (new M.core.alert({ + message: datatables_lang_messages_two.alert_feedback_save_successful.replace(/\_/g, ' '), + title: datatables_lang_messages_two.notification_info, + })).show(); + + } else if (submitfeedbackbutton === 1) { + (new M.core.alert({ + message: datatables_lang_messages_two.alert_feedback_draft_save_successful.replace(/\_/g, ' '), + title: datatables_lang_messages_two.notification_info, + })).show(); + } else { + (new M.core.alert({ + message: datatables_lang_messages_two.alert_feedback_remove_successful.replace(/\_/g, ' '), + title: datatables_lang_messages_two.notification_info, + })).show(); + } } else { (new M.core.alert({ - message: datatables_lang_messages_two.alert_feedback_remove_successful.replace(/\_/g, ' '), + message: response.message, title: datatables_lang_messages_two.notification_info, })).show(); - } - } else { - (new M.core.alert({ - message: response.message, - title: datatables_lang_messages_two.notification_info, - })).show(); - error_wrapper_element.prepend('
' + response.message + '
'); + error_wrapper_element.prepend('
' + response.message + '
'); - } - }).always(function(){ - button.prop('disabled', false); - }); - } -}) + } + }).always(function() { + button.prop('disabled', false); + }); + } + }); +}); diff --git a/view.php b/view.php index 91f7a7e..ab302a9 100644 --- a/view.php +++ b/view.php @@ -526,6 +526,21 @@ $PAGE->requires->jquery(); $PAGE->requires->css(new moodle_url('/mod/coursework/datatables/css/datatables.bootstrap.min.css')); +// Add DataTables CSS files +$PAGE->requires->css(new moodle_url('/mod/coursework/datatables/css/buttons.datatables.min.css')); +$PAGE->requires->css(new moodle_url('/mod/coursework/datatables/css/datatables.bootstrap.min.css')); +$PAGE->requires->css(new moodle_url('/mod/coursework/datatables/css/responsive.bootstrap.min.css')); +$PAGE->requires->css(new moodle_url('/mod/coursework/datatables/css/searchpanes.datatables.min.css')); +$PAGE->requires->css(new moodle_url('/mod/coursework/datatables/css/select.bootstrap.min.css')); +$PAGE->requires->css(new moodle_url('/mod/coursework/datatables/css/select.dataTables.min.css')); + +// Add DataTables JS files +$PAGE->requires->js(new moodle_url('/mod/coursework/datatables/js/jquery.datatables.js'), true); +$PAGE->requires->js(new moodle_url('/mod/coursework/datatables/js/datatables.buttons.js'), true); +$PAGE->requires->js(new moodle_url('/mod/coursework/datatables/js/datatables.responsive.min.js'), true); +$PAGE->requires->js(new moodle_url('/mod/coursework/datatables/js/datatables.searchpanes.js'), true); +$PAGE->requires->js(new moodle_url('/mod/coursework/datatables/js/datatables.select.js'), true); + // Require JS files. // Note that jquery datatables files not included as called by coursework.js. if ($cangrade || $canviewstudents) {