MDL-72507 Quiz: Quiz auto-save does not detect uploaded files

This commit is contained in:
Thong Bui
2021-10-19 16:10:15 +07:00
parent 214adb7984
commit 3c19cbbb18
8 changed files with 63 additions and 11 deletions
+1 -1
View File
@@ -1,2 +1,2 @@
define ("core_form/events",["exports","core/str","core/event_dispatcher","jquery","core/yui"],function(a,b,c,d,f){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.types=a.triggerUploadCompleted=a.triggerUploadStarted=a.notifyUploadCompleted=a.notifyUploadStarted=a.notifyFieldValidationFailure=a.notifyFormSubmittedByJavascript=a.notifyFormError=a.eventTypes=void 0;d=g(d);f=g(f);function g(a){return a&&a.__esModule?a:{default:a}}var h;(0,b.get_string)("changesmadereallygoaway","moodle").then(function(a){h=a;return a}).catch();var i=function(a){if(a){a.returnValue=h}},j={formError:"core_form/error",formSubmittedByJavascript:"core_form/submittedByJavascript",formFieldValidationFailed:"core_form/fieldValidationFailed",uploadStarted:"core_form/uploadStarted",uploadCompleted:"core_form/uploadCompleted"};a.eventTypes=j;a.notifyFormError=function notifyFormError(a){return(0,c.dispatchEvent)(j.formError,{},a)};a.notifyFormSubmittedByJavascript=function notifyFormSubmittedByJavascript(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:!1,d=2<arguments.length&&arguments[2]!==void 0?arguments[2]:!1;if(b){window.skipClientValidation=!0}var e=(0,c.dispatchEvent)(j.formSubmittedByJavascript,{skipValidation:b,fallbackHandled:d},a);if(b){window.skipClientValidation=!1}return e};a.notifyFieldValidationFailure=function notifyFieldValidationFailure(a,b){return(0,c.dispatchEvent)(j.formFieldValidationFailed,{message:b},a,{cancelable:!0})};var k=function(a){window.addEventListener("beforeunload",i);return(0,c.dispatchEvent)(j.uploadStarted,{},document.getElementById(a),{bubbles:!0,cancellable:!1})};a.notifyUploadStarted=k;var l=function(a){window.removeEventListener("beforeunload",i);return(0,c.dispatchEvent)(j.uploadCompleted,{},document.getElementById(a),{bubbles:!0,cancellable:!1})};a.notifyUploadCompleted=l;a.triggerUploadStarted=k;a.triggerUploadCompleted=l;a.types={uploadStarted:"core_form/uploadStarted",uploadCompleted:"core_form/uploadCompleted"};var m=!1;if(!m){f.default.use("event","moodle-core-event",function(){document.addEventListener(j.formError,function(a){var b=f.default.one(a.target),c=f.default.one(a.target.closest("form"));f.default.Global.fire(M.core.globalEvents.FORM_ERROR,{formid:c.generateID(),elementid:b.generateID()})});document.addEventListener(j.formSubmittedByJavascript,function(a){if(a.detail.fallbackHandled){return}if(a.skipValidation){window.skipClientValidation=!0}var b=f.default.one(a.target);b.fire(M.core.event.FORM_SUBMIT_AJAX,{currentTarget:b,fallbackHandled:!0});if(a.skipValidation){window.skipClientValidation=!1}})});document.addEventListener(j.formFieldValidationFailed,function(a){var b=d.default.Event("core_form-field-validation");(0,d.default)(a.target).trigger(b,a.detail.message)});m=!0}});
define ("core_form/events",["exports","core/str","core/event_dispatcher","jquery","core/yui"],function(a,b,c,d,f){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.notifyUploadChanged=a.types=a.triggerUploadCompleted=a.triggerUploadStarted=a.notifyUploadCompleted=a.notifyUploadStarted=a.notifyFieldValidationFailure=a.notifyFormSubmittedByJavascript=a.notifyFormError=a.eventTypes=void 0;d=g(d);f=g(f);function g(a){return a&&a.__esModule?a:{default:a}}var h;(0,b.get_string)("changesmadereallygoaway","moodle").then(function(a){h=a;return a}).catch();var i=function(a){if(a){a.returnValue=h}},j={formError:"core_form/error",formSubmittedByJavascript:"core_form/submittedByJavascript",formFieldValidationFailed:"core_form/fieldValidationFailed",uploadStarted:"core_form/uploadStarted",uploadCompleted:"core_form/uploadCompleted",uploadChanged:"core_form/uploadChanged"};a.eventTypes=j;a.notifyFormError=function notifyFormError(a){return(0,c.dispatchEvent)(j.formError,{},a)};a.notifyFormSubmittedByJavascript=function notifyFormSubmittedByJavascript(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:!1,d=2<arguments.length&&arguments[2]!==void 0?arguments[2]:!1;if(b){window.skipClientValidation=!0}var e=(0,c.dispatchEvent)(j.formSubmittedByJavascript,{skipValidation:b,fallbackHandled:d},a);if(b){window.skipClientValidation=!1}return e};a.notifyFieldValidationFailure=function notifyFieldValidationFailure(a,b){return(0,c.dispatchEvent)(j.formFieldValidationFailed,{message:b},a,{cancelable:!0})};var k=function(a){window.addEventListener("beforeunload",i);return(0,c.dispatchEvent)(j.uploadStarted,{},document.getElementById(a),{bubbles:!0,cancellable:!1})};a.notifyUploadStarted=k;var l=function(a){window.removeEventListener("beforeunload",i);return(0,c.dispatchEvent)(j.uploadCompleted,{},document.getElementById(a),{bubbles:!0,cancellable:!1})};a.notifyUploadCompleted=l;a.triggerUploadStarted=k;a.triggerUploadCompleted=l;a.types={uploadStarted:"core_form/uploadStarted",uploadCompleted:"core_form/uploadCompleted"};var m=!1;if(!m){f.default.use("event","moodle-core-event",function(){document.addEventListener(j.formError,function(a){var b=f.default.one(a.target),c=f.default.one(a.target.closest("form"));f.default.Global.fire(M.core.globalEvents.FORM_ERROR,{formid:c.generateID(),elementid:b.generateID()})});document.addEventListener(j.formSubmittedByJavascript,function(a){if(a.detail.fallbackHandled){return}if(a.skipValidation){window.skipClientValidation=!0}var b=f.default.one(a.target);b.fire(M.core.event.FORM_SUBMIT_AJAX,{currentTarget:b,fallbackHandled:!0});if(a.skipValidation){window.skipClientValidation=!1}})});document.addEventListener(j.formFieldValidationFailed,function(a){var b=d.default.Event("core_form-field-validation");(0,d.default)(a.target).trigger(b,a.detail.message)});m=!0}a.notifyUploadChanged=function notifyUploadChanged(a){return(0,c.dispatchEvent)(j.uploadChanged,{},document.getElementById(a),{bubbles:!0,cancellable:!1})}});
//# sourceMappingURL=events.min.js.map
File diff suppressed because one or more lines are too long
+36 -8
View File
@@ -53,9 +53,10 @@ const changesMadeCheck = e => {
* Events for `core_form`.
*
* @constant
* @property {String} formError See {@link event:formError}
* @property {String} formFieldValidationFailed See {@link event:formFieldValidationFailed}
* @property {String} formSubmittedByJavascript See {@link event:formSubmittedByJavascript}
* @property {String} formError See {@link event:core_form/error}
* @property {String} formFieldValidationFailed See {@link event:core_form/fieldValidationFailed}
* @property {String} formSubmittedByJavascript See {@link event:core_form/submittedByJavascript}
* @property {String} uploadChanged See {@link event:core_form/uploadChanged}
*/
export const eventTypes = {
/**
@@ -70,7 +71,7 @@ export const eventTypes = {
/**
* An event triggered when an mform is about to be submitted via javascript.
*
* @event formSubmittedByJavascript
* @event core_form/submittedByJavascript
* @type {CustomEvent}
* @property {HTMLElement} target The form that was submitted
* @property {object} detail
@@ -82,7 +83,7 @@ export const eventTypes = {
/**
* An event triggered upon form field validation failure.
*
* @event formFieldValidationFailed
* @event core_form/fieldValidationFailed
* @type {CustomEvent}
* @property {HTMLElement} target The field that failed validation
* @property {object} detail
@@ -93,7 +94,7 @@ export const eventTypes = {
/**
* An event triggered when an upload is started
*
* @event uploadStarted
* @event core_form/uploadStarted
* @type {CustomEvent}
* @property {HTMLElement} target The location where the upload began
*/
@@ -102,11 +103,20 @@ export const eventTypes = {
/**
* An event triggered when an upload completes
*
* @event uploadCompleted
* @event core_form/uploadCompleted
* @type {CustomEvent}
* @property {HTMLElement} target The location where the upload completd
* @property {HTMLElement} target The location where the upload completed
*/
uploadCompleted: 'core_form/uploadCompleted',
/**
* An event triggered when a file upload field has been changed.
*
* @event core_form/uploadChanged
* @type {CustomEvent}
* @property {HTMLElement} target The form field which was changed
*/
uploadChanged: 'core_form/uploadChanged',
};
// These are only imported for legacy.
@@ -312,3 +322,21 @@ if (!legacyEventsRegistered) {
legacyEventsRegistered = true;
}
/**
* Trigger an event to notify the file upload field has been changed.
*
* @method
* @param {string} elementId The element which was changed
* @returns {CustomEvent}
* @fires uploadChanged
*/
export const notifyUploadChanged = elementId => dispatchEvent(
eventTypes.uploadChanged,
{},
document.getElementById(elementId),
{
bubbles: true,
cancellable: false,
}
);
+12
View File
@@ -209,6 +209,10 @@ M.form_filemanager.init = function(Y, options) {
this.check_buttons();
this.refresh(this.currentpath);
M.form_filemanager.formChangeChecker.markFormChangedFromNode(this.filemanager.getDOMNode());
require(['core_form/events'], function(FormEvent) {
FormEvent.notifyUploadChanged(this.filemanager.get('id'));
}.bind(this));
},
check_buttons: function() {
if (this.filecount>0) {
@@ -466,6 +470,10 @@ M.form_filemanager.init = function(Y, options) {
args.scope.refresh(obj[0], {action: 'delete'});
}
M.form_filemanager.formChangeChecker.markFormChangedFromNode(this.scope.filemanager.getDOMNode());
require(['core_form/events'], function(FormEvent) {
FormEvent.notifyUploadChanged(this.scope.filemanager.get('id'));
}.bind(this));
}
});
};
@@ -1001,6 +1009,10 @@ M.form_filemanager.init = function(Y, options) {
args.scope.filecount--;
args.scope.refresh(obj.filepath, {action: 'delete'});
M.form_filemanager.formChangeChecker.markFormChangedFromNode(this.scope.filemanager.getDOMNode());
require(['core_form/events'], function(FormEvent) {
FormEvent.notifyUploadChanged(this.scope.filemanager.get('id'));
}.bind(this));
}
});
};
@@ -204,6 +204,10 @@ M.mod_quiz.autosave = {
this.form.delegate('change', this.value_changed, this.SELECTORS.CHANGE_ELEMENTS, this);
this.form.on('submit', this.stop_autosaving, this);
require(['core_form/events'], function(FormEvent) {
window.addEventListener(FormEvent.eventTypes.uploadChanged, this.value_changed.bind(this));
}.bind(this));
this.init_tinymce(this.TINYMCE_DETECTION_REPEATS);
this.save_hidden_field_values();
@@ -1 +1 @@
YUI.add("moodle-mod_quiz-autosave",function(s,e){M.mod_quiz=M.mod_quiz||{},M.mod_quiz.autosave={TINYMCE_DETECTION_DELAY:500,TINYMCE_DETECTION_REPEATS:20,WATCH_HIDDEN_DELAY:1e3,FAILURES_BEFORE_NOTIFY:1,FIRST_SUCCESSFUL_SAVE:-1,SELECTORS:{QUIZ_FORM:"#responseform",VALUE_CHANGE_ELEMENTS:'input, textarea, [contenteditable="true"]',CHANGE_ELEMENTS:"input, select",HIDDEN_INPUTS:"input[type=hidden]",CONNECTION_ERROR:"#connection-error",CONNECTION_OK:"#connection-ok"},AUTOSAVE_HANDLER:M.cfg.wwwroot+"/mod/quiz/autosave.ajax.php",delay:12e4,form:null,dirty:!1,delay_timer:null,save_transaction:null,savefailures:0,editor_change_handler:null,hidden_field_values:{},init:function(e){this.form=s.one(this.SELECTORS.QUIZ_FORM),this.form&&(this.delay=1e3*e,this.form.delegate("valuechange",this.value_changed,this.SELECTORS.VALUE_CHANGE_ELEMENTS,this),this.form.delegate("change",this.value_changed,this.SELECTORS.CHANGE_ELEMENTS,this),this.form.on("submit",this.stop_autosaving,this),this.init_tinymce(this.TINYMCE_DETECTION_REPEATS),this.save_hidden_field_values(),this.watch_hidden_fields())},save_hidden_field_values:function(){this.form.all(this.SELECTORS.HIDDEN_INPUTS).each(function(e){var t=e.get("name");t&&(this.hidden_field_values[t]=e.get("value"))},this)},watch_hidden_fields:function(){this.detect_hidden_field_changes(),s.later(this.WATCH_HIDDEN_DELAY,this,this.watch_hidden_fields)},detect_hidden_field_changes:function(){this.form.all(this.SELECTORS.HIDDEN_INPUTS).each(function(e){var t=e.get("name"),i=e.get("value");t&&(t in this.hidden_field_values&&i===this.hidden_field_values[t]||(this.hidden_field_values[t]=i,this.value_changed({target:e})))},this)},init_tinymce:function(e){"undefined"!=typeof window.tinyMCE?(this.editor_change_handler=s.bind(this.editor_changed,this),window.tinyMCE.onAddEditor.add(s.bind(this.init_tinymce_editor,this))):0<e&&s.later(this.TINYMCE_DETECTION_DELAY,this,this.init_tinymce,[e-1])},init_tinymce_editor:function(e,t){t.onChange.add(this.editor_change_handler),t.onRedo.add(this.editor_change_handler),t.onUndo.add(this.editor_change_handler),t.onKeyDown.add(this.editor_change_handler)},value_changed:function(e){var t=e.target.getAttribute("name");"thispage"===t||"scrollpos"===t||t&&t.match(/_:flagged$/)||(t=t||"#"+e.target.getAttribute("id"),this.start_save_timer_if_necessary())},editor_changed:function(e){this.start_save_timer_if_necessary()},start_save_timer_if_necessary:function(){this.dirty=!0,this.delay_timer||this.save_transaction||this.start_save_timer()},start_save_timer:function(){this.cancel_delay(),this.delay_timer=s.later(this.delay,this,this.save_changes)},cancel_delay:function(){this.delay_timer&&!0!==this.delay_timer&&this.delay_timer.cancel(),this.delay_timer=null},save_changes:function(){if(this.cancel_delay(),this.dirty=!1,this.is_time_nearly_over())this.stop_autosaving();else{"undefined"!=typeof window.tinyMCE&&window.tinyMCE.triggerSave();var e=this.form.all("input[type=submit], button[type=submit]");e.setAttribute("type","button"),this.save_transaction=s.io(this.AUTOSAVE_HANDLER,{method:"POST",form:{id:this.form},on:{success:this.save_done,failure:this.save_failed},context:this}),e.setAttribute("type","submit")}},save_done:function(e,t){var i=JSON.parse(t.responseText);"OK"===i.status?("undefined"!=typeof i.timeleft&&M.mod_quiz.timer.updateEndTime(i.timeleft),this.save_transaction=null,this.dirty&&this.start_save_timer(),0<this.savefailures?(s.one(this.SELECTORS.CONNECTION_ERROR).hide(),s.one(this.SELECTORS.CONNECTION_OK).show(),this.savefailures=this.FIRST_SUCCESSFUL_SAVE):this.savefailures===this.FIRST_SUCCESSFUL_SAVE&&(s.one(this.SELECTORS.CONNECTION_OK).hide(),this.savefailures=0)):this.save_failed(e,t)},save_failed:function(){this.save_transaction=null,this.start_save_timer(),this.savefailures=Math.max(1,this.savefailures+1),this.savefailures===this.FAILURES_BEFORE_NOTIFY&&(s.one(this.SELECTORS.CONNECTION_ERROR).show(),s.one(this.SELECTORS.CONNECTION_OK).hide())},is_time_nearly_over:function(){return M.mod_quiz.timer&&M.mod_quiz.timer.endtime&&(new Date).getTime()+2*this.delay>M.mod_quiz.timer.endtime},stop_autosaving:function(){this.cancel_delay(),this.delay_timer=!0,this.save_transaction&&this.save_transaction.abort()}}},"@VERSION@",{requires:["base","node","event","event-valuechange","node-event-delegate","io-form"]});
YUI.add("moodle-mod_quiz-autosave",function(s,e){M.mod_quiz=M.mod_quiz||{},M.mod_quiz.autosave={TINYMCE_DETECTION_DELAY:500,TINYMCE_DETECTION_REPEATS:20,WATCH_HIDDEN_DELAY:1e3,FAILURES_BEFORE_NOTIFY:1,FIRST_SUCCESSFUL_SAVE:-1,SELECTORS:{QUIZ_FORM:"#responseform",VALUE_CHANGE_ELEMENTS:'input, textarea, [contenteditable="true"]',CHANGE_ELEMENTS:"input, select",HIDDEN_INPUTS:"input[type=hidden]",CONNECTION_ERROR:"#connection-error",CONNECTION_OK:"#connection-ok"},AUTOSAVE_HANDLER:M.cfg.wwwroot+"/mod/quiz/autosave.ajax.php",delay:12e4,form:null,dirty:!1,delay_timer:null,save_transaction:null,savefailures:0,editor_change_handler:null,hidden_field_values:{},init:function(e){this.form=s.one(this.SELECTORS.QUIZ_FORM),this.form&&(this.delay=1e3*e,this.form.delegate("valuechange",this.value_changed,this.SELECTORS.VALUE_CHANGE_ELEMENTS,this),this.form.delegate("change",this.value_changed,this.SELECTORS.CHANGE_ELEMENTS,this),this.form.on("submit",this.stop_autosaving,this),require(["core_form/events"],function(e){window.addEventListener(e.eventTypes.uploadChanged,this.value_changed.bind(this))}.bind(this)),this.init_tinymce(this.TINYMCE_DETECTION_REPEATS),this.save_hidden_field_values(),this.watch_hidden_fields())},save_hidden_field_values:function(){this.form.all(this.SELECTORS.HIDDEN_INPUTS).each(function(e){var t=e.get("name");t&&(this.hidden_field_values[t]=e.get("value"))},this)},watch_hidden_fields:function(){this.detect_hidden_field_changes(),s.later(this.WATCH_HIDDEN_DELAY,this,this.watch_hidden_fields)},detect_hidden_field_changes:function(){this.form.all(this.SELECTORS.HIDDEN_INPUTS).each(function(e){var t=e.get("name"),i=e.get("value");t&&(t in this.hidden_field_values&&i===this.hidden_field_values[t]||(this.hidden_field_values[t]=i,this.value_changed({target:e})))},this)},init_tinymce:function(e){"undefined"!=typeof window.tinyMCE?(this.editor_change_handler=s.bind(this.editor_changed,this),window.tinyMCE.onAddEditor.add(s.bind(this.init_tinymce_editor,this))):0<e&&s.later(this.TINYMCE_DETECTION_DELAY,this,this.init_tinymce,[e-1])},init_tinymce_editor:function(e,t){t.onChange.add(this.editor_change_handler),t.onRedo.add(this.editor_change_handler),t.onUndo.add(this.editor_change_handler),t.onKeyDown.add(this.editor_change_handler)},value_changed:function(e){var t=e.target.getAttribute("name");"thispage"===t||"scrollpos"===t||t&&t.match(/_:flagged$/)||(t=t||"#"+e.target.getAttribute("id"),this.start_save_timer_if_necessary())},editor_changed:function(e){this.start_save_timer_if_necessary()},start_save_timer_if_necessary:function(){this.dirty=!0,this.delay_timer||this.save_transaction||this.start_save_timer()},start_save_timer:function(){this.cancel_delay(),this.delay_timer=s.later(this.delay,this,this.save_changes)},cancel_delay:function(){this.delay_timer&&!0!==this.delay_timer&&this.delay_timer.cancel(),this.delay_timer=null},save_changes:function(){if(this.cancel_delay(),this.dirty=!1,this.is_time_nearly_over())this.stop_autosaving();else{"undefined"!=typeof window.tinyMCE&&window.tinyMCE.triggerSave();var e=this.form.all("input[type=submit], button[type=submit]");e.setAttribute("type","button"),this.save_transaction=s.io(this.AUTOSAVE_HANDLER,{method:"POST",form:{id:this.form},on:{success:this.save_done,failure:this.save_failed},context:this}),e.setAttribute("type","submit")}},save_done:function(e,t){var i=JSON.parse(t.responseText);"OK"===i.status?("undefined"!=typeof i.timeleft&&M.mod_quiz.timer.updateEndTime(i.timeleft),this.save_transaction=null,this.dirty&&this.start_save_timer(),0<this.savefailures?(s.one(this.SELECTORS.CONNECTION_ERROR).hide(),s.one(this.SELECTORS.CONNECTION_OK).show(),this.savefailures=this.FIRST_SUCCESSFUL_SAVE):this.savefailures===this.FIRST_SUCCESSFUL_SAVE&&(s.one(this.SELECTORS.CONNECTION_OK).hide(),this.savefailures=0)):this.save_failed(e,t)},save_failed:function(){this.save_transaction=null,this.start_save_timer(),this.savefailures=Math.max(1,this.savefailures+1),this.savefailures===this.FAILURES_BEFORE_NOTIFY&&(s.one(this.SELECTORS.CONNECTION_ERROR).show(),s.one(this.SELECTORS.CONNECTION_OK).hide())},is_time_nearly_over:function(){return M.mod_quiz.timer&&M.mod_quiz.timer.endtime&&(new Date).getTime()+2*this.delay>M.mod_quiz.timer.endtime},stop_autosaving:function(){this.cancel_delay(),this.delay_timer=!0,this.save_transaction&&this.save_transaction.abort()}}},"@VERSION@",{requires:["base","node","event","event-valuechange","node-event-delegate","io-form"]});
@@ -203,6 +203,10 @@ M.mod_quiz.autosave = {
this.form.delegate('change', this.value_changed, this.SELECTORS.CHANGE_ELEMENTS, this);
this.form.on('submit', this.stop_autosaving, this);
require(['core_form/events'], function(FormEvent) {
window.addEventListener(FormEvent.eventTypes.uploadChanged, this.value_changed.bind(this));
}.bind(this));
this.init_tinymce(this.TINYMCE_DETECTION_REPEATS);
this.save_hidden_field_values();
+4
View File
@@ -202,6 +202,10 @@ M.mod_quiz.autosave = {
this.form.delegate('change', this.value_changed, this.SELECTORS.CHANGE_ELEMENTS, this);
this.form.on('submit', this.stop_autosaving, this);
require(['core_form/events'], function(FormEvent) {
window.addEventListener(FormEvent.eventTypes.uploadChanged, this.value_changed.bind(this));
}.bind(this));
this.init_tinymce(this.TINYMCE_DETECTION_REPEATS);
this.save_hidden_field_values();