Skip to content

Commit a6076d3

Browse files
committed
MOBILE-3893 assign: Check if submission is empty before saving
1 parent 34f9124 commit a6076d3

File tree

8 files changed

+104
-0
lines changed

8 files changed

+104
-0
lines changed

scripts/langindex.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@
432432
"addon.mod_assign.overdue": "assign",
433433
"addon.mod_assign.submission": "assign",
434434
"addon.mod_assign.submissioneditable": "assign",
435+
"addon.mod_assign.submissionempty": "assign",
435436
"addon.mod_assign.submissionnoteditable": "assign",
436437
"addon.mod_assign.submissionnotsupported": "local_moodlemobileapp",
437438
"addon.mod_assign.submissionslocked": "assign",

src/addons/mod/assign/lang.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"overdue": "Assignment is overdue by: {{$a}}",
8585
"submission": "Submission",
8686
"submissioneditable": "Student can edit this submission",
87+
"submissionempty": "Nothing was submitted",
8788
"submissionnoteditable": "Student cannot edit this submission",
8889
"submissionnotsupported": "This submission is not supported by the app and may not contain all the information.",
8990
"submissionslocked": "This assignment is not accepting submissions",

src/addons/mod/assign/pages/edit/edit.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ export class AddonModAssignEditPage implements OnInit, OnDestroy, CanLeave {
398398
throw Translate.instant('addon.mod_assign.acceptsubmissionstatement');
399399
}
400400

401+
if (AddonModAssignHelper.isSubmissionEmptyForEdit(this.assign!, this.userSubmission!, inputData)) {
402+
throw Translate.instant('addon.mod_assign.submissionempty');
403+
}
404+
401405
let modal = await CoreLoadings.show();
402406
let size = -1;
403407

src/addons/mod/assign/services/assign-helper.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,31 @@ export class AddonModAssignHelperProvider {
237237
return true;
238238
}
239239

240+
/**
241+
* Check whether the edited submission has no content.
242+
*
243+
* @param assign Assignment object.
244+
* @param submission Submission to inspect.
245+
* @param inputData Data entered in the submission form.
246+
* @returns Whether the submission is empty.
247+
*/
248+
isSubmissionEmptyForEdit(
249+
assign: AddonModAssignAssign,
250+
submission: AddonModAssignSubmission,
251+
inputData: CoreFormFields,
252+
): boolean {
253+
const anyNotEmpty = submission.plugins?.some((plugin) =>
254+
!AddonModAssignSubmissionDelegate.isPluginEmptyForEdit(assign, plugin, inputData));
255+
256+
// If any plugin is not empty, we consider that the submission is not empty either.
257+
if (anyNotEmpty) {
258+
return false;
259+
}
260+
261+
// If all the plugins were empty (or there were no plugins), we consider the submission to be empty.
262+
return true;
263+
}
264+
240265
/**
241266
* List the participants for a single assignment, with some summary info about their submissions.
242267
*

src/addons/mod/assign/services/handlers/default-submission.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ export class AddonModAssignDefaultSubmissionHandler implements AddonModAssignSub
5050
return true;
5151
}
5252

53+
/**
54+
* @inheritdoc
55+
*/
56+
isEmptyForEdit(
57+
assign: AddonModAssignAssign, // eslint-disable-line @typescript-eslint/no-unused-vars
58+
plugin: AddonModAssignPlugin, // eslint-disable-line @typescript-eslint/no-unused-vars
59+
inputData: CoreFormFields, // eslint-disable-line @typescript-eslint/no-unused-vars
60+
): boolean {
61+
return true;
62+
}
63+
5364
/**
5465
* @inheritdoc
5566
*/

src/addons/mod/assign/services/submission-delegate.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ export interface AddonModAssignSubmissionHandler extends CoreDelegateHandler {
6262
plugin: AddonModAssignPlugin,
6363
): boolean;
6464

65+
/**
66+
* Check if a plugin has no data in the edit form.
67+
*
68+
* @param assign The assignment.
69+
* @param plugin The plugin object.
70+
* @param inputData Data entered by the user for the submission.
71+
* @returns Whether the plugin is empty.
72+
*/
73+
isEmptyForEdit?(
74+
assign: AddonModAssignAssign,
75+
plugin: AddonModAssignPlugin,
76+
inputData: CoreFormFields,
77+
): boolean;
78+
6579
/**
6680
* Should clear temporary data for a cancelled submission.
6781
*
@@ -502,6 +516,22 @@ export class AddonModAssignSubmissionDelegateService extends CoreDelegate<AddonM
502516
return this.executeFunctionOnEnabled(plugin.type, 'isEmpty', [assign, plugin]);
503517
}
504518

519+
/**
520+
* Check if a plugin has no data in the edit form
521+
*
522+
* @param assign The assignment.
523+
* @param plugin The plugin object.
524+
* @param inputData Data entered in the submission form.
525+
* @returns Whether the plugin is empty.
526+
*/
527+
isPluginEmptyForEdit(
528+
assign: AddonModAssignAssign,
529+
plugin: AddonModAssignPlugin,
530+
inputData: CoreFormFields,
531+
): boolean | undefined {
532+
return this.executeFunctionOnEnabled(plugin.type, 'isEmptyForEdit', [assign, plugin, inputData]);
533+
}
534+
505535
/**
506536
* Prefetch any required data for a submission plugin.
507537
*

src/addons/mod/assign/submission/file/services/handler.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ export class AddonModAssignSubmissionFileHandlerService implements AddonModAssig
6161
return files.length === 0;
6262
}
6363

64+
/**
65+
* @inheritdoc
66+
*/
67+
isEmptyForEdit(assign: AddonModAssignAssign): boolean {
68+
const currentFiles = CoreFileSession.getFiles(ADDON_MOD_ASSIGN_COMPONENT, assign.id);
69+
70+
return currentFiles.length == 0;
71+
}
72+
6473
/**
6574
* @inheritdoc
6675
*/

src/addons/mod/assign/submission/onlinetext/services/handler.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,29 @@ export class AddonModAssignSubmissionOnlineTextHandlerService implements AddonMo
5858
return text.trim().length === 0;
5959
}
6060

61+
/**
62+
* @inheritdoc
63+
*/
64+
isEmptyForEdit(
65+
assign: AddonModAssignAssign,
66+
plugin: AddonModAssignPlugin,
67+
inputData: AddonModAssignSubmissionOnlineTextData,
68+
): boolean {
69+
const text = this.getTextToSubmit(plugin, inputData);
70+
71+
if (CoreText.countWords(text) > 0) {
72+
return false;
73+
}
74+
75+
// Check if the online text submission contains video, audio or image elements
76+
// that can be ignored and stripped by count_words().
77+
if (/<\s*((video|audio)[^>]*>(.*?)<\s*\/\s*(video|audio)>)|(img[^>]*>)/.test(text)) {
78+
return false;
79+
}
80+
81+
return true;
82+
}
83+
6184
/**
6285
* @inheritdoc
6386
*/

0 commit comments

Comments
 (0)