1313// limitations under the License.
1414
1515angular . module ( 'mm.addons.mod_quiz' )
16+ . constant ( 'mmaModQuizAccessPasswordStore' , 'mod_quiz_access_password' )
17+
18+ . config ( function ( $mmSitesFactoryProvider , mmaModQuizAccessPasswordStore ) {
19+ var stores = [
20+ {
21+ name : mmaModQuizAccessPasswordStore ,
22+ keyPath : 'id' ,
23+ indexes : [ ]
24+ }
25+ ] ;
26+ $mmSitesFactoryProvider . registerStores ( stores ) ;
27+ } )
1628
1729/**
1830 * Handler for password quiz access rule.
@@ -21,7 +33,7 @@ angular.module('mm.addons.mod_quiz')
2133 * @ngdoc service
2234 * @name $mmaQuizAccessPasswordHandler
2335 */
24- . factory ( '$mmaQuizAccessPasswordHandler' , function ( ) {
36+ . factory ( '$mmaQuizAccessPasswordHandler' , function ( $mmSitesManager , $mmSite , $q , mmaModQuizAccessPasswordStore ) {
2537
2638 var self = { } ;
2739
@@ -34,6 +46,80 @@ angular.module('mm.addons.mod_quiz')
3446 delete data . quizpassword ;
3547 } ;
3648
49+ /**
50+ * Get a password stored in DB.
51+ *
52+ * @param {Number } quizId Quiz ID.
53+ * @param {String } [siteId] Site ID. If not defined, current site.
54+ * @return {Promise } Promise resolved with password on success, rejected otherwise.
55+ */
56+ function getPasswordEntry ( quizId , siteId ) {
57+ siteId = siteId || $mmSite . getId ( ) ;
58+
59+ return $mmSitesManager . getSite ( siteId ) . then ( function ( site ) {
60+ return site . getDb ( ) . get ( mmaModQuizAccessPasswordStore , quizId ) ;
61+ } ) ;
62+ }
63+
64+ /**
65+ * Remove a password from DB.
66+ *
67+ * @param {Number } quizId Quiz ID.
68+ * @param {String } [siteId] Site ID. If not defined, current site.
69+ * @return {Promise } Promise resolved on success, rejected otherwise.
70+ */
71+ function removePassword ( quizId , siteId ) {
72+ siteId = siteId || $mmSite . getId ( ) ;
73+
74+ return $mmSitesManager . getSite ( siteId ) . then ( function ( site ) {
75+ return site . getDb ( ) . remove ( mmaModQuizAccessPasswordStore , quizId ) ;
76+ } ) ;
77+ }
78+
79+ /**
80+ * Store a password in DB.
81+ *
82+ * @param {Number } quizId Quiz ID.
83+ * @param {String } password Password.
84+ * @param {String } [siteId] Site ID. If not defined, current site.
85+ * @return {Promise } Promise resolved on success, rejected otherwise.
86+ */
87+ function storePassword ( quizId , password , siteId ) {
88+ siteId = siteId || $mmSite . getId ( ) ;
89+
90+ return $mmSitesManager . getSite ( siteId ) . then ( function ( site ) {
91+ var entry = {
92+ id : quizId ,
93+ password : password ,
94+ timemodified : new Date ( ) . getTime ( )
95+ } ;
96+
97+ return site . getDb ( ) . insert ( mmaModQuizAccessPasswordStore , entry ) ;
98+ } ) ;
99+ }
100+
101+ /**
102+ * Get fixed preflight data (data that doesn't require user interaction).
103+ *
104+ * @param {Object } quiz Quiz.
105+ * @param {Object } attempt Attempt.
106+ * @param {Object } preflightData Object where to store the preflight data.
107+ * @param {Boolean } prefetch True if prefetching, false if attempting the quiz.
108+ * @param {String } [siteId] Site ID. If not defined, current site.
109+ * @return {Promise } Promise resolved when preflight data has been added.
110+ */
111+ self . getFixedPreflightData = function ( quiz , attempt , preflightData , prefetch , siteId ) {
112+ if ( quiz && quiz . id && typeof preflightData . quizpassword == 'undefined' ) {
113+ return getPasswordEntry ( quiz . id , siteId ) . then ( function ( entry ) {
114+ preflightData . quizpassword = entry . password ;
115+ } ) . catch ( function ( ) {
116+ // Don't reject.
117+ } ) ;
118+ }
119+
120+ return $q . when ( ) ;
121+ } ;
122+
37123 /**
38124 * Whether or not the rule is enabled for the site.
39125 *
@@ -46,13 +132,20 @@ angular.module('mm.addons.mod_quiz')
46132 /**
47133 * Check if a preflight check is required.
48134 *
135+ * @param {Object } quiz Quiz.
49136 * @param {Object } [attempt] Attempt to continue. Not defined if starting a new attempt.
50137 * @param {Boolean } prefetch True if prefetching, false if attempting the quiz.
51- * @return {Boolean } True if preflight check required.
138+ * @param {String } [siteId] Site ID. If not defined, current site.
139+ * @return {Promise } Promise resolved with a boolean: true if preflight check required, false otherwise.
52140 */
53- self . isPreflightCheckRequired = function ( attempt , prefetch ) {
54- // If the password rule is active in a quiz we always require to input the password.
55- return true ;
141+ self . isPreflightCheckRequired = function ( quiz , attempt , prefetch , siteId ) {
142+ // Check if we have a password stored.
143+ return getPasswordEntry ( quiz . id , siteId ) . then ( function ( ) {
144+ return false ;
145+ } ) . catch ( function ( ) {
146+ // Not stored.
147+ return true ;
148+ } ) ;
56149 } ;
57150
58151 /**
@@ -64,6 +157,42 @@ angular.module('mm.addons.mod_quiz')
64157 return 'mma-quiz-access-password-preflight' ;
65158 } ;
66159
160+ /**
161+ * The preflight check has passed. This is a chance to record that fact in some way.
162+ *
163+ * @param {Object } quiz Quiz.
164+ * @param {Object } attempt Attempt.
165+ * @param {Object } preflightData Object where to store the preflight data.
166+ * @param {Boolean } prefetch True if prefetching, false if attempting the quiz.
167+ * @param {String } [siteId] Site ID. If not defined, current site.
168+ * @return {Promise } Promise resolved when done.
169+ */
170+ self . notifyPreflightCheckPassed = function ( quiz , attempt , preflightData , prefetch , siteId ) {
171+ if ( quiz && quiz . id && typeof preflightData . quizpassword != 'undefined' ) {
172+ return storePassword ( quiz . id , preflightData . quizpassword , siteId ) ;
173+ }
174+
175+ return $q . when ( ) ;
176+ } ;
177+
178+ /**
179+ * The preflight check has failed.
180+ *
181+ * @param {Object } quiz Quiz.
182+ * @param {Object } attempt Attempt.
183+ * @param {Object } preflightData Object where to store the preflight data.
184+ * @param {Boolean } prefetch True if prefetching, false if attempting the quiz.
185+ * @param {String } [siteId] Site ID. If not defined, current site.
186+ * @return {Promise } Promise resolved when done.
187+ */
188+ self . notifyPreflightCheckFailed = function ( quiz , attempt , preflightData , prefetch , siteId ) {
189+ if ( quiz && quiz . id ) {
190+ return removePassword ( quiz . id , siteId ) ;
191+ }
192+
193+ return $q . when ( ) ;
194+ } ;
195+
67196 return self ;
68197} )
69198
0 commit comments