MOBILE-3893 assign: Check if submission is empty before saving

main
Albert Gasset 2024-08-12 15:50:17 +02:00
parent 34f9124e9a
commit a6076d3f42
8 changed files with 104 additions and 0 deletions

View File

@ -432,6 +432,7 @@
"addon.mod_assign.overdue": "assign",
"addon.mod_assign.submission": "assign",
"addon.mod_assign.submissioneditable": "assign",
"addon.mod_assign.submissionempty": "assign",
"addon.mod_assign.submissionnoteditable": "assign",
"addon.mod_assign.submissionnotsupported": "local_moodlemobileapp",
"addon.mod_assign.submissionslocked": "assign",

View File

@ -84,6 +84,7 @@
"overdue": "Assignment is overdue by: {{$a}}",
"submission": "Submission",
"submissioneditable": "Student can edit this submission",
"submissionempty": "Nothing was submitted",
"submissionnoteditable": "Student cannot edit this submission",
"submissionnotsupported": "This submission is not supported by the app and may not contain all the information.",
"submissionslocked": "This assignment is not accepting submissions",

View File

@ -398,6 +398,10 @@ export class AddonModAssignEditPage implements OnInit, OnDestroy, CanLeave {
throw Translate.instant('addon.mod_assign.acceptsubmissionstatement');
}
if (AddonModAssignHelper.isSubmissionEmptyForEdit(this.assign!, this.userSubmission!, inputData)) {
throw Translate.instant('addon.mod_assign.submissionempty');
}
let modal = await CoreLoadings.show();
let size = -1;

View File

@ -237,6 +237,31 @@ export class AddonModAssignHelperProvider {
return true;
}
/**
* Check whether the edited submission has no content.
*
* @param assign Assignment object.
* @param submission Submission to inspect.
* @param inputData Data entered in the submission form.
* @returns Whether the submission is empty.
*/
isSubmissionEmptyForEdit(
assign: AddonModAssignAssign,
submission: AddonModAssignSubmission,
inputData: CoreFormFields,
): boolean {
const anyNotEmpty = submission.plugins?.some((plugin) =>
!AddonModAssignSubmissionDelegate.isPluginEmptyForEdit(assign, plugin, inputData));
// If any plugin is not empty, we consider that the submission is not empty either.
if (anyNotEmpty) {
return false;
}
// If all the plugins were empty (or there were no plugins), we consider the submission to be empty.
return true;
}
/**
* List the participants for a single assignment, with some summary info about their submissions.
*

View File

@ -50,6 +50,17 @@ export class AddonModAssignDefaultSubmissionHandler implements AddonModAssignSub
return true;
}
/**
* @inheritdoc
*/
isEmptyForEdit(
assign: AddonModAssignAssign, // eslint-disable-line @typescript-eslint/no-unused-vars
plugin: AddonModAssignPlugin, // eslint-disable-line @typescript-eslint/no-unused-vars
inputData: CoreFormFields, // eslint-disable-line @typescript-eslint/no-unused-vars
): boolean {
return true;
}
/**
* @inheritdoc
*/

View File

@ -62,6 +62,20 @@ export interface AddonModAssignSubmissionHandler extends CoreDelegateHandler {
plugin: AddonModAssignPlugin,
): boolean;
/**
* Check if a plugin has no data in the edit form.
*
* @param assign The assignment.
* @param plugin The plugin object.
* @param inputData Data entered by the user for the submission.
* @returns Whether the plugin is empty.
*/
isEmptyForEdit?(
assign: AddonModAssignAssign,
plugin: AddonModAssignPlugin,
inputData: CoreFormFields,
): boolean;
/**
* Should clear temporary data for a cancelled submission.
*
@ -502,6 +516,22 @@ export class AddonModAssignSubmissionDelegateService extends CoreDelegate<AddonM
return this.executeFunctionOnEnabled(plugin.type, 'isEmpty', [assign, plugin]);
}
/**
* Check if a plugin has no data in the edit form
*
* @param assign The assignment.
* @param plugin The plugin object.
* @param inputData Data entered in the submission form.
* @returns Whether the plugin is empty.
*/
isPluginEmptyForEdit(
assign: AddonModAssignAssign,
plugin: AddonModAssignPlugin,
inputData: CoreFormFields,
): boolean | undefined {
return this.executeFunctionOnEnabled(plugin.type, 'isEmptyForEdit', [assign, plugin, inputData]);
}
/**
* Prefetch any required data for a submission plugin.
*

View File

@ -61,6 +61,15 @@ export class AddonModAssignSubmissionFileHandlerService implements AddonModAssig
return files.length === 0;
}
/**
* @inheritdoc
*/
isEmptyForEdit(assign: AddonModAssignAssign): boolean {
const currentFiles = CoreFileSession.getFiles(ADDON_MOD_ASSIGN_COMPONENT, assign.id);
return currentFiles.length == 0;
}
/**
* @inheritdoc
*/

View File

@ -58,6 +58,29 @@ export class AddonModAssignSubmissionOnlineTextHandlerService implements AddonMo
return text.trim().length === 0;
}
/**
* @inheritdoc
*/
isEmptyForEdit(
assign: AddonModAssignAssign,
plugin: AddonModAssignPlugin,
inputData: AddonModAssignSubmissionOnlineTextData,
): boolean {
const text = this.getTextToSubmit(plugin, inputData);
if (CoreText.countWords(text) > 0) {
return false;
}
// Check if the online text submission contains video, audio or image elements
// that can be ignored and stripped by count_words().
if (/<\s*((video|audio)[^>]*>(.*?)<\s*\/\s*(video|audio)>)|(img[^>]*>)/.test(text)) {
return false;
}
return true;
}
/**
* @inheritdoc
*/