@@ -16,6 +16,7 @@ import { Component, Optional, Injector } from '@angular/core';
1616import { Content } from 'ionic-angular' ;
1717
1818import { CoreApp } from '@providers/app' ;
19+ import { CoreEvents } from '@providers/events' ;
1920import { CoreFilepool } from '@providers/filepool' ;
2021import { CoreWSExternalFile } from '@providers/ws' ;
2122import { CoreDomUtils } from '@providers/utils/dom' ;
@@ -24,6 +25,7 @@ import { CoreH5P } from '@core/h5p/providers/h5p';
2425import { CoreH5PDisplayOptions } from '@core/h5p/classes/core' ;
2526import { CoreH5PHelper } from '@core/h5p/classes/helper' ;
2627import { CoreConstants } from '@core/constants' ;
28+ import { CoreSite } from '@classes/site' ;
2729
2830import {
2931 AddonModH5PActivity , AddonModH5PActivityProvider , AddonModH5PActivityData , AddonModH5PActivityAccessInfo
@@ -50,13 +52,22 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv
5052 percentage : string ; // Download/unzip percentage.
5153 progressMessage : string ; // Message about download/unzip.
5254 playing : boolean ; // Whether the package is being played.
55+ displayOptions : CoreH5PDisplayOptions ; // Display options for the package.
56+ onlinePlayerUrl : string ; // URL to play the package in online.
57+ fileUrl : string ; // The fileUrl to use to play the package.
58+ state : string ; // State of the file.
59+ siteCanDownload : boolean ;
5360
5461 protected fetchContentDefaultError = 'addon.mod_h5pactivity.errorgetactivity' ;
55- protected displayOptions : CoreH5PDisplayOptions ;
62+ protected site : CoreSite ;
63+ protected observer ;
5664
5765 constructor ( injector : Injector ,
5866 @Optional ( ) protected content : Content ) {
5967 super ( injector , content ) ;
68+
69+ this . site = this . sitesProvider . getCurrentSite ( ) ;
70+ this . siteCanDownload = this . site . canDownloadFiles ( ) && ! CoreH5P . instance . isOfflineDisabledInSite ( ) ;
6071 }
6172
6273 /**
@@ -87,14 +98,25 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv
8798 try {
8899 this . h5pActivity = await AddonModH5PActivity . instance . getH5PActivity ( this . courseId , this . module . id ) ;
89100
101+ this . dataRetrieved . emit ( this . h5pActivity ) ;
90102 this . description = this . h5pActivity . intro ;
91103 this . displayOptions = CoreH5PHelper . decodeDisplayOptions ( this . h5pActivity . displayoptions ) ;
92- this . dataRetrieved . emit ( this . h5pActivity ) ;
104+
105+ if ( this . h5pActivity . package && this . h5pActivity . package [ 0 ] ) {
106+ // The online player should use the original file, not the trusted one.
107+ this . onlinePlayerUrl = CoreH5P . instance . h5pPlayer . calculateOnlinePlayerUrl (
108+ this . site . getURL ( ) , this . h5pActivity . package [ 0 ] . fileurl , this . displayOptions ) ;
109+ }
93110
94111 await Promise . all ( [
95112 this . fetchAccessInfo ( ) ,
96113 this . fetchDeployedFileData ( ) ,
97114 ] ) ;
115+
116+ if ( ! this . siteCanDownload || this . state == CoreConstants . DOWNLOADED ) {
117+ // Cannot download the file or already downloaded, play the package directly.
118+ this . play ( ) ;
119+ }
98120 } finally {
99121 this . fillContextMenu ( refresh ) ;
100122 }
@@ -115,6 +137,11 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv
115137 * @return Promise resolved when done.
116138 */
117139 protected async fetchDeployedFileData ( ) : Promise < void > {
140+ if ( ! this . siteCanDownload ) {
141+ // Cannot download the file, no need to fetch the file data.
142+ return ;
143+ }
144+
118145 if ( this . h5pActivity . deployedfile ) {
119146 // File already deployed and still valid, use this one.
120147 this . deployedFile = this . h5pActivity . deployedfile ;
@@ -128,19 +155,30 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv
128155 this . deployedFile = await CoreH5P . instance . getTrustedH5PFile ( this . h5pActivity . package [ 0 ] . fileurl , this . displayOptions ) ;
129156 }
130157
131- await this . calculateFileStatus ( ) ;
158+ this . fileUrl = this . deployedFile . fileurl ;
159+
160+ // Listen for changes in the state.
161+ const eventName = await CoreFilepool . instance . getFileEventNameByUrl ( this . siteId , this . deployedFile . fileurl ) ;
162+
163+ if ( ! this . observer ) {
164+ this . observer = CoreEvents . instance . on ( eventName , ( ) => {
165+ this . calculateFileState ( ) ;
166+ } ) ;
167+ }
168+
169+ await this . calculateFileState ( ) ;
132170 }
133171
134172 /**
135- * Calculate the status of the deployed file.
173+ * Calculate the state of the deployed file.
136174 *
137175 * @return Promise resolved when done.
138176 */
139- protected async calculateFileStatus ( ) : Promise < void > {
140- const state = await CoreFilepool . instance . getFileStateByUrl ( this . siteId , this . deployedFile . fileurl ,
177+ protected async calculateFileState ( ) : Promise < void > {
178+ this . state = await CoreFilepool . instance . getFileStateByUrl ( this . siteId , this . deployedFile . fileurl ,
141179 this . deployedFile . timemodified ) ;
142180
143- this . showFileState ( state ) ;
181+ this . showFileState ( ) ;
144182 }
145183
146184 /**
@@ -154,18 +192,16 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv
154192
155193 /**
156194 * Displays some data based on the state of the main file.
157- *
158- * @param state The state of the file.
159195 */
160- protected showFileState ( state : string ) : void {
196+ protected showFileState ( ) : void {
161197
162- if ( state == CoreConstants . OUTDATED ) {
198+ if ( this . state == CoreConstants . OUTDATED ) {
163199 this . stateMessage = 'addon.mod_h5pactivity.filestateoutdated' ;
164200 this . needsDownload = true ;
165- } else if ( state == CoreConstants . NOT_DOWNLOADED ) {
201+ } else if ( this . state == CoreConstants . NOT_DOWNLOADED ) {
166202 this . stateMessage = 'addon.mod_h5pactivity.filestatenotdownloaded' ;
167203 this . needsDownload = true ;
168- } else if ( state == CoreConstants . DOWNLOADING ) {
204+ } else if ( this . state == CoreConstants . DOWNLOADING ) {
169205 this . stateMessage = '' ;
170206
171207 if ( ! this . downloading ) {
@@ -264,7 +300,12 @@ export class AddonModH5PActivityIndexComponent extends CoreCourseModuleMainActiv
264300 */
265301 play ( ) : void {
266302 this . playing = true ;
303+ }
267304
268- // @TODO
305+ /**
306+ * Component destroyed.
307+ */
308+ ngOnDestroy ( ) : void {
309+ this . observer && this . observer . off ( ) ;
269310 }
270311}
0 commit comments