From 88ac2e0c5c6a36d301a7d39feac6e1dfe493880c Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 30 Jul 2020 11:05:16 +0200 Subject: [PATCH] MOBILE-3449 assign: Don't block all assign sync when edit grade --- .../addon-mod-assign-submission.html | 2 +- .../components/submission/submission.ts | 41 +++++++++++++++---- src/addon/mod/assign/lang/en.json | 1 + src/addon/mod/assign/providers/assign-sync.ts | 24 +++++++++++ src/assets/lang/en.json | 1 + 5 files changed, 59 insertions(+), 10 deletions(-) diff --git a/src/addon/mod/assign/components/submission/addon-mod-assign-submission.html b/src/addon/mod/assign/components/submission/addon-mod-assign-submission.html index e4cb512e5..2164fd42d 100644 --- a/src/addon/mod/assign/components/submission/addon-mod-assign-submission.html +++ b/src/addon/mod/assign/components/submission/addon-mod-assign-submission.html @@ -20,7 +20,7 @@ - + diff --git a/src/addon/mod/assign/components/submission/submission.ts b/src/addon/mod/assign/components/submission/submission.ts index 6bc9c7282..c1af093df 100644 --- a/src/addon/mod/assign/components/submission/submission.ts +++ b/src/addon/mod/assign/components/submission/submission.ts @@ -35,7 +35,9 @@ import { } from '../../providers/assign'; import { AddonModAssignHelperProvider } from '../../providers/helper'; import { AddonModAssignOfflineProvider } from '../../providers/assign-offline'; +import { AddonModAssignSync } from '../../providers/assign-sync'; import { CoreTabsComponent } from '@components/tabs/tabs'; +import { CoreTabComponent } from '@components/tabs/tab'; import { CoreSplitViewComponent } from '@components/split-view/split-view'; import { AddonModAssignSubmissionPluginComponent } from '../submission-plugin/submission-plugin'; @@ -537,11 +539,6 @@ export class AddonModAssignSubmissionComponent implements OnInit, OnDestroy { // Make sure outcomes is an array. gradeInfo.outcomes = gradeInfo.outcomes || []; - if (!this.isDestroyed) { - // Block the assignment. - this.syncProvider.blockOperation(AddonModAssignProvider.COMPONENT, this.assign.id); - } - // Treat the grade info. return this.treatGradeInfo(); }).then(() => { @@ -952,15 +949,41 @@ export class AddonModAssignSubmissionComponent implements OnInit, OnDestroy { } } + /** + * Block or unblock the automatic sync of the user grade. + * + * @param block Whether to block or unblock. + */ + protected setGradeSyncBlocked(block?: boolean): void { + if (this.isDestroyed || !this.assign || !this.isGrading) { + return; + } + + const syncId = AddonModAssignSync.instance.getGradeSyncId(this.assign.id, this.submitId); + + if (block) { + this.syncProvider.blockOperation(AddonModAssignProvider.COMPONENT, syncId); + } else { + this.syncProvider.unblockOperation(AddonModAssignProvider.COMPONENT, syncId); + } + } + + /** + * A certain tab has been selected, either manually or automatically. + * + * @param tab The tab that was selected. + */ + tabSelected(tab: CoreTabComponent): void { + // Block sync when selecting grade tab, unblock when leaving it. + this.setGradeSyncBlocked(this.tabs.getIndex(tab) === 1); + } + /** * Component being destroyed. */ ngOnDestroy(): void { + this.setGradeSyncBlocked(false); this.isDestroyed = true; - - if (this.assign && this.isGrading) { - this.syncProvider.unblockOperation(AddonModAssignProvider.COMPONENT, this.assign.id); - } } } diff --git a/src/addon/mod/assign/lang/en.json b/src/addon/mod/assign/lang/en.json index fd076f605..5311cdf8a 100644 --- a/src/addon/mod/assign/lang/en.json +++ b/src/addon/mod/assign/lang/en.json @@ -89,6 +89,7 @@ "submitassignment": "Submit assignment", "submittedearly": "Assignment was submitted {{$a}} early", "submittedlate": "Assignment was submitted {{$a}} late", + "syncblockedusercomponent": "user grade", "timemodified": "Last modified", "timeremaining": "Time remaining", "ungroupedusers": "The setting 'Require group to make submission' is enabled and some users are either not a member of any group, or are a member of more than one group, so are unable to make submissions.", diff --git a/src/addon/mod/assign/providers/assign-sync.ts b/src/addon/mod/assign/providers/assign-sync.ts index 8b5bcfbb1..2aeaff695 100644 --- a/src/addon/mod/assign/providers/assign-sync.ts +++ b/src/addon/mod/assign/providers/assign-sync.ts @@ -31,6 +31,8 @@ import { AddonModAssignOfflineProvider } from './assign-offline'; import { AddonModAssignSubmissionDelegate } from './submission-delegate'; import { AddonModAssignFeedbackDelegate } from './feedback-delegate'; +import { makeSingleton } from '@singletons/core.singletons'; + /** * Data returned by an assign sync. */ @@ -79,6 +81,17 @@ export class AddonModAssignSyncProvider extends CoreSyncBaseProvider { this.componentTranslate = courseProvider.translateModuleName('assign'); } + /** + * Get the sync ID for a certain user grade. + * + * @param assignId Assign ID. + * @param userId User the grade belongs to. + * @return Sync ID. + */ + getGradeSyncId(assignId: number, userId: number): string { + return 'assignGrade#' + assignId + '#' + userId; + } + /** * Convenience function to get scale selected option. * @@ -373,6 +386,15 @@ export class AddonModAssignSyncProvider extends CoreSyncBaseProvider { const userId = offlineData.userid; let discardError; + const syncId = this.getGradeSyncId(assign.id, userId); + + // Check if this grade sync is blocked. + if (this.syncProvider.isBlocked(AddonModAssignProvider.COMPONENT, syncId, siteId)) { + this.logger.error(`Cannot sync grade for assign ${assign.id} and user ${userId} because it is blocked.`); + + return Promise.reject(new Error(this.translate.instant('core.errorsyncblocked', + {$a: this.translate.instant('addon.mod_assign.syncblockedusercomponent')}))); + } return this.assignProvider.getSubmissionStatus(assign.id, userId, undefined, false, true, true, siteId).then((status) => { const timemodified = status.feedback && (status.feedback.gradeddate || status.feedback.grade.timemodified); @@ -455,3 +477,5 @@ export class AddonModAssignSyncProvider extends CoreSyncBaseProvider { }); } } + +export class AddonModAssignSync extends makeSingleton(AddonModAssignSyncProvider) {} diff --git a/src/assets/lang/en.json b/src/assets/lang/en.json index ddd310e2a..971ff4451 100644 --- a/src/assets/lang/en.json +++ b/src/assets/lang/en.json @@ -408,6 +408,7 @@ "addon.mod_assign.submitassignment_help": "Once this assignment is submitted you will not be able to make any more changes.", "addon.mod_assign.submittedearly": "Assignment was submitted {{$a}} early", "addon.mod_assign.submittedlate": "Assignment was submitted {{$a}} late", + "addon.mod_assign.syncblockedusercomponent": "user grade", "addon.mod_assign.timemodified": "Last modified", "addon.mod_assign.timeremaining": "Time remaining", "addon.mod_assign.ungroupedusers": "The setting 'Require group to make submission' is enabled and some users are either not a member of any group, or are a member of more than one group, so are unable to make submissions.",