@@ -78,25 +78,33 @@ export class AddonModQuizSyncProvider extends CoreCourseActivitySyncBaseProvider
7878 * @param quiz Quiz.
7979 * @param courseId Course ID.
8080 * @param warnings List of warnings generated by the sync.
81- * @param attemptId Last attempt ID.
82- * @param offlineAttempt Offline attempt synchronized, if any.
83- * @param onlineAttempt Online data for the offline attempt.
84- * @param removeAttempt Whether the offline data should be removed.
85- * @param updated Whether some data was sent to the site.
81+ * @param options Other options.
8682 * @return Promise resolved on success.
8783 */
88- protected finishSync ( siteId : string , quiz : any , courseId : number , warnings : string [ ] , attemptId ?: number , offlineAttempt ?: any ,
89- onlineAttempt ?: any , removeAttempt ?: boolean , updated ?: boolean ) : Promise < AddonModQuizSyncResult > {
84+ protected finishSync ( siteId : string , quiz : any , courseId : number , warnings : string [ ] , options ?: FinishSyncOptions )
85+ : Promise < AddonModQuizSyncResult > {
86+ options = options || { } ;
9087
9188 // Invalidate the data for the quiz and attempt.
92- return this . quizProvider . invalidateAllQuizData ( quiz . id , courseId , attemptId , siteId ) . catch ( ( ) => {
89+ return this . quizProvider . invalidateAllQuizData ( quiz . id , courseId , options . attemptId , siteId ) . catch ( ( ) => {
9390 // Ignore errors.
9491 } ) . then ( ( ) => {
95- if ( removeAttempt && attemptId ) {
96- return this . quizOfflineProvider . removeAttemptAndAnswers ( attemptId , siteId ) ;
92+ if ( options . removeAttempt && options . attemptId ) {
93+ const promises = [ ] ;
94+
95+ promises . push ( this . quizOfflineProvider . removeAttemptAndAnswers ( options . attemptId , siteId ) ) ;
96+
97+ if ( options . onlineQuestions ) {
98+ for ( const slot in options . onlineQuestions ) {
99+ promises . push ( this . questionDelegate . deleteOfflineData ( options . onlineQuestions [ slot ] ,
100+ AddonModQuizProvider . COMPONENT , quiz . coursemodule , siteId ) ) ;
101+ }
102+ }
103+
104+ return Promise . all ( promises ) ;
97105 }
98106 } ) . then ( ( ) => {
99- if ( updated ) {
107+ if ( options . updated ) {
100108 // Data has been sent. Update prefetched data.
101109 return this . courseProvider . getModuleBasicInfoByInstance ( quiz . id , 'quiz' , siteId ) . then ( ( module ) => {
102110 return this . prefetchAfterUpdateQuiz ( module , quiz , courseId , undefined , siteId ) ;
@@ -110,14 +118,14 @@ export class AddonModQuizSyncProvider extends CoreCourseActivitySyncBaseProvider
110118 } ) ;
111119 } ) . then ( ( ) => {
112120 // Check if online attempt was finished because of the sync.
113- if ( onlineAttempt && ! this . quizProvider . isAttemptFinished ( onlineAttempt . state ) ) {
121+ if ( options . onlineAttempt && ! this . quizProvider . isAttemptFinished ( options . onlineAttempt . state ) ) {
114122 // Attempt wasn't finished at start. Check if it's finished now.
115123 return this . quizProvider . getUserAttempts ( quiz . id , { cmId : quiz . coursemodule , siteId} ) . then ( ( attempts ) => {
116124 // Search the attempt.
117125 for ( const i in attempts ) {
118126 const attempt = attempts [ i ] ;
119127
120- if ( attempt . id == onlineAttempt . id ) {
128+ if ( attempt . id == options . onlineAttempt . id ) {
121129 return this . quizProvider . isAttemptFinished ( attempt . state ) ;
122130 }
123131 }
@@ -355,15 +363,25 @@ export class AddonModQuizSyncProvider extends CoreCourseActivitySyncBaseProvider
355363 // Attempt not found or it's finished in online. Discard it.
356364 warnings . push ( this . translate . instant ( 'addon.mod_quiz.warningattemptfinished' ) ) ;
357365
358- return this . finishSync ( siteId , quiz , courseId , warnings , offlineAttempt . id , offlineAttempt , onlineAttempt , true ) ;
366+ return this . finishSync ( siteId , quiz , courseId , warnings , {
367+ attemptId : offlineAttempt . id ,
368+ offlineAttempt,
369+ onlineAttempt,
370+ removeAttempt : true ,
371+ } ) ;
359372 }
360373
361374 // Get the data stored in offline.
362375 const answersList = await this . quizOfflineProvider . getAttemptAnswers ( offlineAttempt . id , siteId ) ;
363376
364377 if ( ! answersList . length ) {
365378 // No answers stored, finish.
366- return this . finishSync ( siteId , quiz , courseId , warnings , lastAttemptId , offlineAttempt , onlineAttempt , true ) ;
379+ return this . finishSync ( siteId , quiz , courseId , warnings , {
380+ attemptId : lastAttemptId ,
381+ offlineAttempt,
382+ onlineAttempt,
383+ removeAttempt : true ,
384+ } ) ;
367385 }
368386
369387 const offlineAnswers = this . questionProvider . convertAnswersArrayToObject ( answersList ) ;
@@ -376,17 +394,23 @@ export class AddonModQuizSyncProvider extends CoreCourseActivitySyncBaseProvider
376394 'core.settings.synchronization' , siteId ) ;
377395
378396 // Now get the online questions data.
379- const pages = this . quizProvider . getPagesFromLayoutAndQuestions ( onlineAttempt . layout , offlineQuestions ) ;
380-
381397 const onlineQuestions = await this . quizProvider . getAllQuestionsData ( quiz , onlineAttempt , preflightData , {
382- pages,
398+ pages : this . quizProvider . getPagesFromLayoutAndQuestions ( onlineAttempt . layout , offlineQuestions ) ,
383399 readingStrategy : CoreSitesReadingStrategy . OnlyNetwork ,
384400 siteId,
385401 } ) ;
386402
387403 // Validate questions, discarding the offline answers that can't be synchronized.
388404 const discardedData = await this . validateQuestions ( onlineAttempt . id , onlineQuestions , offlineQuestions , siteId ) ;
389405
406+ // Let questions prepare the data to send.
407+ await Promise . all ( Object . keys ( offlineQuestions ) . map ( async ( slot ) => {
408+ const onlineQuestion = onlineQuestions [ slot ] ;
409+
410+ await this . questionDelegate . prepareSyncData ( onlineQuestion , offlineQuestions [ slot ] . answers ,
411+ AddonModQuizProvider . COMPONENT , quiz . coursemodule , siteId ) ;
412+ } ) ) ;
413+
390414 // Get the answers to send.
391415 const answers = this . quizOfflineProvider . extractAnswersFromQuestions ( offlineQuestions ) ;
392416 const finish = offlineAttempt . finished && ! discardedData ;
@@ -410,7 +434,14 @@ export class AddonModQuizSyncProvider extends CoreCourseActivitySyncBaseProvider
410434 }
411435
412436 // Data sent. Finish the sync.
413- return this . finishSync ( siteId , quiz , courseId , warnings , lastAttemptId , offlineAttempt , onlineAttempt , true , true ) ;
437+ return this . finishSync ( siteId , quiz , courseId , warnings , {
438+ attemptId : lastAttemptId ,
439+ offlineAttempt,
440+ onlineAttempt,
441+ removeAttempt : true ,
442+ updated : true ,
443+ onlineQuestions,
444+ } ) ;
414445 }
415446
416447 /**
@@ -458,3 +489,15 @@ export class AddonModQuizSyncProvider extends CoreCourseActivitySyncBaseProvider
458489 } ) ;
459490 }
460491}
492+
493+ /**
494+ * Options to pass to finish sync.
495+ */
496+ type FinishSyncOptions = {
497+ attemptId ?: number ; // Last attempt ID.
498+ offlineAttempt ?: any ; // Offline attempt synchronized, if any.
499+ onlineAttempt ?: any ; // Online data for the offline attempt.
500+ removeAttempt ?: boolean ; // Whether the offline data should be removed.
501+ updated ?: boolean ; // Whether the offline data should be removed.
502+ onlineQuestions ?: any ; // Online questions indexed by slot.
503+ } ;
0 commit comments