@@ -32,7 +32,7 @@ import {
32
32
} from '../lesson' ;
33
33
import { AddonModLessonSync , AddonModLessonSyncResult } from '../lesson-sync' ;
34
34
import { ADDON_MOD_LESSON_COMPONENT , AddonModLessonJumpTo , AddonModLessonPageSubtype } from '../../constants' ;
35
- import { CorePrompts } from '@services/overlays/prompts ' ;
35
+ import { AddonModLessonHelper } from '../lesson-helper ' ;
36
36
37
37
/**
38
38
* Handler to prefetch lessons.
@@ -60,7 +60,7 @@ export class AddonModLessonPrefetchHandlerService extends CoreCourseActivityPref
60
60
let lesson = await AddonModLesson . getLesson ( courseId , module . id , { siteId } ) ;
61
61
62
62
// Get the lesson password if it's needed.
63
- const passwordData = await this . getLessonPassword ( lesson . id , {
63
+ const passwordData = await AddonModLessonHelper . getLessonPassword ( lesson . id , {
64
64
readingStrategy : CoreSitesReadingStrategy . ONLY_NETWORK ,
65
65
askPassword : single ,
66
66
siteId,
@@ -88,64 +88,6 @@ export class AddonModLessonPrefetchHandlerService extends CoreCourseActivityPref
88
88
return result ;
89
89
}
90
90
91
- /**
92
- * Get the lesson password if needed. If not stored, it can ask the user to enter it.
93
- *
94
- * @param lessonId Lesson ID.
95
- * @param options Other options.
96
- * @returns Promise resolved when done.
97
- */
98
- async getLessonPassword (
99
- lessonId : number ,
100
- options : AddonModLessonGetPasswordOptions = { } ,
101
- ) : Promise < AddonModLessonGetPasswordResult > {
102
-
103
- options . siteId = options . siteId || CoreSites . getCurrentSiteId ( ) ;
104
-
105
- // Get access information to check if password is needed.
106
- const accessInfo = await AddonModLesson . getAccessInformation ( lessonId , options ) ;
107
-
108
- if ( ! accessInfo . preventaccessreasons . length ) {
109
- // Password not needed.
110
- return { accessInfo } ;
111
- }
112
-
113
- const passwordNeeded = accessInfo . preventaccessreasons . length == 1 &&
114
- AddonModLesson . isPasswordProtected ( accessInfo ) ;
115
-
116
- if ( ! passwordNeeded ) {
117
- // Lesson cannot be played, reject.
118
- throw new CoreError ( accessInfo . preventaccessreasons [ 0 ] . message ) ;
119
- }
120
-
121
- // The lesson requires a password. Check if there is one in DB.
122
- let password = await CorePromiseUtils . ignoreErrors ( AddonModLesson . getStoredPassword ( lessonId ) ) ;
123
-
124
- if ( password ) {
125
- try {
126
- return await this . validatePassword ( lessonId , accessInfo , password , options ) ;
127
- } catch {
128
- // Error validating it.
129
- }
130
- }
131
-
132
- // Ask for the password if allowed.
133
- if ( ! options . askPassword ) {
134
- // Cannot ask for password, reject.
135
- throw new CoreError ( accessInfo . preventaccessreasons [ 0 ] . message ) ;
136
- }
137
-
138
- // Create and show the modal.
139
- const response = await CorePrompts . promptPassword ( {
140
- title : 'addon.mod_lesson.enterpassword' ,
141
- placeholder : 'core.login.password' ,
142
- submit : 'addon.mod_lesson.continue' ,
143
- } ) ;
144
- password = response . password ;
145
-
146
- return this . validatePassword ( lessonId , accessInfo , password , options ) ;
147
- }
148
-
149
91
/**
150
92
* Invalidate the prefetched content.
151
93
*
@@ -241,7 +183,7 @@ export class AddonModLessonPrefetchHandlerService extends CoreCourseActivityPref
241
183
let lesson = await AddonModLesson . getLesson ( courseId , module . id , commonOptions ) ;
242
184
243
185
// Get the lesson password if it's needed.
244
- const passwordData = await this . getLessonPassword ( lesson . id , {
186
+ const passwordData = await AddonModLessonHelper . getLessonPassword ( lesson . id , {
245
187
readingStrategy : CoreSitesReadingStrategy . ONLY_NETWORK ,
246
188
askPassword : single ,
247
189
siteId,
@@ -477,39 +419,6 @@ export class AddonModLessonPrefetchHandlerService extends CoreCourseActivityPref
477
419
} ) ) ;
478
420
}
479
421
480
- /**
481
- * Validate the password.
482
- *
483
- * @param lessonId Lesson ID.
484
- * @param accessInfo Lesson access info.
485
- * @param password Password to check.
486
- * @param options Other options.
487
- * @returns Promise resolved when done.
488
- */
489
- protected async validatePassword (
490
- lessonId : number ,
491
- accessInfo : AddonModLessonGetAccessInformationWSResponse ,
492
- password : string ,
493
- options : CoreCourseCommonModWSOptions = { } ,
494
- ) : Promise < AddonModLessonGetPasswordResult > {
495
-
496
- options . siteId = options . siteId || CoreSites . getCurrentSiteId ( ) ;
497
-
498
- const lesson = await AddonModLesson . getLessonWithPassword ( lessonId , {
499
- password,
500
- ...options , // Include all options.
501
- } ) ;
502
-
503
- // Password is ok, store it and return the data.
504
- await AddonModLesson . storePassword ( lesson . id , password , options . siteId ) ;
505
-
506
- return {
507
- password,
508
- lesson,
509
- accessInfo,
510
- } ;
511
- }
512
-
513
422
/**
514
423
* Sync a module.
515
424
*
@@ -525,19 +434,3 @@ export class AddonModLessonPrefetchHandlerService extends CoreCourseActivityPref
525
434
}
526
435
527
436
export const AddonModLessonPrefetchHandler = makeSingleton ( AddonModLessonPrefetchHandlerService ) ;
528
-
529
- /**
530
- * Options to pass to get lesson password.
531
- */
532
- export type AddonModLessonGetPasswordOptions = CoreCourseCommonModWSOptions & {
533
- askPassword ?: boolean ; // True if we should ask for password if needed, false otherwise.
534
- } ;
535
-
536
- /**
537
- * Result of getLessonPassword.
538
- */
539
- export type AddonModLessonGetPasswordResult = {
540
- password ?: string ;
541
- lesson ?: AddonModLessonLessonWSData ;
542
- accessInfo : AddonModLessonGetAccessInformationWSResponse ;
543
- } ;
0 commit comments