@@ -25,12 +25,14 @@ import { CoreH5P } from '@core/h5p/providers/h5p';
2525import { CoreH5PDisplayOptions } from '@core/h5p/classes/core' ;
2626import { CoreH5PHelper } from '@core/h5p/classes/helper' ;
2727import { CoreXAPI } from '@core/xapi/providers/xapi' ;
28+ import { CoreXAPIOffline } from '@core/xapi/providers/offline' ;
2829import { CoreConstants } from '@core/constants' ;
2930import { CoreSite } from '@classes/site' ;
3031
3132import {
3233 AddonModH5PActivity , AddonModH5PActivityProvider , AddonModH5PActivityData , AddonModH5PActivityAccessInfo
3334} from '../../providers/h5pactivity' ;
35+ import { AddonModH5PActivitySyncProvider , AddonModH5PActivitySync } from '../../providers/sync' ;
3436
3537/**
3638 * Component that displays an H5P activity entry page.
@@ -59,8 +61,11 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv
5961 state : string ; // State of the file.
6062 siteCanDownload : boolean ;
6163 trackComponent : string ; // Component for tracking.
64+ hasOffline : boolean ;
65+ isOpeningPage : boolean ;
6266
6367 protected fetchContentDefaultError = 'addon.mod_h5pactivity.errorgetactivity' ;
68+ protected syncEventName = AddonModH5PActivitySyncProvider . AUTO_SYNCED ;
6469 protected site : CoreSite ;
6570 protected observer ;
6671 protected messageListenerFunction : ( event : MessageEvent ) => Promise < void > ;
@@ -103,13 +108,18 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv
103108 */
104109 protected async fetchContent ( refresh : boolean = false , sync : boolean = false , showErrors : boolean = false ) : Promise < void > {
105110 try {
106- this . h5pActivity = await AddonModH5PActivity . instance . getH5PActivity ( this . courseId , this . module . id ) ;
111+ this . h5pActivity = await AddonModH5PActivity . instance . getH5PActivity ( this . courseId , this . module . id , false , this . siteId ) ;
107112
108113 this . dataRetrieved . emit ( this . h5pActivity ) ;
109114 this . description = this . h5pActivity . intro ;
110115 this . displayOptions = CoreH5PHelper . decodeDisplayOptions ( this . h5pActivity . displayoptions ) ;
111116
117+ if ( sync ) {
118+ await this . syncActivity ( showErrors ) ;
119+ }
120+
112121 await Promise . all ( [
122+ this . checkHasOffline ( ) ,
113123 this . fetchAccessInfo ( ) ,
114124 this . fetchDeployedFileData ( ) ,
115125 ] ) ;
@@ -136,13 +146,22 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv
136146 }
137147 }
138148
149+ /**
150+ * Fetch the access info and store it in the right variables.
151+ *
152+ * @return Promise resolved when done.
153+ */
154+ protected async checkHasOffline ( ) : Promise < void > {
155+ this . hasOffline = await CoreXAPIOffline . instance . contextHasStatements ( this . h5pActivity . context , this . siteId ) ;
156+ }
157+
139158 /**
140159 * Fetch the access info and store it in the right variables.
141160 *
142161 * @return Promise resolved when done.
143162 */
144163 protected async fetchAccessInfo ( ) : Promise < void > {
145- this . accessInfo = await AddonModH5PActivity . instance . getAccessInformation ( this . h5pActivity . id ) ;
164+ this . accessInfo = await AddonModH5PActivity . instance . getAccessInformation ( this . h5pActivity . id , false , this . siteId ) ;
146165 }
147166
148167 /**
@@ -331,8 +350,17 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv
331350 /**
332351 * Go to view user events.
333352 */
334- viewMyAttempts ( ) : void {
335- this . navCtrl . push ( 'AddonModH5PActivityUserAttemptsPage' , { courseId : this . courseId , h5pActivityId : this . h5pActivity . id } ) ;
353+ async viewMyAttempts ( ) : Promise < void > {
354+ this . isOpeningPage = true ;
355+
356+ try {
357+ await this . navCtrl . push ( 'AddonModH5PActivityUserAttemptsPage' , {
358+ courseId : this . courseId ,
359+ h5pActivityId : this . h5pActivity . id ,
360+ } ) ;
361+ } finally {
362+ this . isOpeningPage = false ;
363+ }
336364 }
337365
338366 /**
@@ -342,12 +370,31 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv
342370 * @return Promise resolved when done.
343371 */
344372 protected async onIframeMessage ( event : MessageEvent ) : Promise < void > {
345- if ( ! event . data || ! CoreXAPI . instance . canPostStatementInSite ( this . site ) || ! this . isCurrentXAPIPost ( event . data ) ) {
373+ if ( ! event . data || ! CoreXAPI . instance . canPostStatementsInSite ( this . site ) || ! this . isCurrentXAPIPost ( event . data ) ) {
346374 return ;
347375 }
348376
349377 try {
350- await CoreXAPI . instance . postStatement ( event . data . component , JSON . stringify ( event . data . statements ) ) ;
378+ const options = {
379+ offline : this . hasOffline ,
380+ courseId : this . courseId ,
381+ extra : this . h5pActivity . name ,
382+ siteId : this . site . getId ( ) ,
383+ } ;
384+
385+ const sent = await CoreXAPI . instance . postStatements ( this . h5pActivity . context , event . data . component ,
386+ JSON . stringify ( event . data . statements ) , options ) ;
387+
388+ this . hasOffline = ! sent ;
389+
390+ if ( sent ) {
391+ try {
392+ // Invalidate attempts.
393+ await AddonModH5PActivity . instance . invalidateUserAttempts ( this . h5pActivity . id , undefined , this . siteId ) ;
394+ } catch ( error ) {
395+ // Ignore errors.
396+ }
397+ }
351398 } catch ( error ) {
352399 CoreDomUtils . instance . showErrorModalDefault ( error , 'Error sending tracking data.' ) ;
353400 }
@@ -380,6 +427,39 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv
380427 return match && match [ 1 ] == this . h5pActivity . context ;
381428 }
382429
430+ /**
431+ * Performs the sync of the activity.
432+ *
433+ * @return Promise resolved when done.
434+ */
435+ protected sync ( ) : Promise < any > {
436+ return AddonModH5PActivitySync . instance . syncActivity ( this . h5pActivity . context , this . site . getId ( ) ) ;
437+ }
438+
439+ /**
440+ * An autosync event has been received.
441+ *
442+ * @param syncEventData Data receiven on sync observer.
443+ */
444+ protected autoSyncEventReceived ( syncEventData : any ) : void {
445+ this . checkHasOffline ( ) ;
446+ }
447+
448+ /**
449+ * Go to blog posts.
450+ *
451+ * @param event Event.
452+ */
453+ async gotoBlog ( event : any ) : Promise < void > {
454+ this . isOpeningPage = true ;
455+
456+ try {
457+ await super . gotoBlog ( event ) ;
458+ } finally {
459+ this . isOpeningPage = false ;
460+ }
461+ }
462+
383463 /**
384464 * Component destroyed.
385465 */
0 commit comments