@@ -30,7 +30,8 @@ angular.module('mm.addons.mod_quiz')
3030
3131 var self = { } ,
3232 blockedQuizzes = { } ,
33- $mmaModQuizSync ; // We'll inject it using $injector to prevent circular dependencies.
33+ $mmaModQuizSync , // We'll inject it using $injector to prevent circular dependencies.
34+ downloadPromises = { } ; // Store download promises to prevent duplicate requests.
3435
3536 // Constants.
3637
@@ -2073,31 +2074,49 @@ angular.module('mm.addons.mod_quiz')
20732074 startAttempt ,
20742075 quiz ,
20752076 quizAccessInfo ,
2077+ attemptAccessInfo ,
20762078 preflightData = { } ,
2077- scope ;
2079+ scope ,
2080+ prefetchPromise ,
2081+ deleted = false ;
2082+
2083+ if ( downloadPromises [ siteId ] && downloadPromises [ siteId ] [ module . id ] ) {
2084+ // There's already a download ongoing for this package, return the promise.
2085+ return downloadPromises [ siteId ] [ module . id ] ;
2086+ } else if ( ! downloadPromises [ siteId ] ) {
2087+ downloadPromises [ siteId ] = { } ;
2088+ }
20782089
20792090 // Mark package as downloading.
2080- return $mmFilepool . storePackageStatus ( siteId , mmaModQuizComponent , module . id , mmCoreDownloading ) . then ( function ( ) {
2091+ prefetchPromise = $mmFilepool . storePackageStatus ( siteId , mmaModQuizComponent , module . id , mmCoreDownloading ) . then ( function ( ) {
20812092 // Get quiz.
20822093 return self . getQuiz ( courseId , module . id , siteId ) . then ( function ( q ) {
20832094 quiz = q ;
20842095 } ) ;
20852096 } ) . then ( function ( ) {
20862097 var promises = [ ] ;
20872098
2088- // Get user attempts and data not related with attempts .
2099+ // Get some quiz data.
20892100 promises . push ( self . getQuizAccessInformation ( quiz . id , false , true , siteId ) . then ( function ( info ) {
20902101 quizAccessInfo = info ;
20912102 } ) ) ;
20922103 promises . push ( self . getQuizRequiredQtypes ( quiz . id , true , siteId ) ) ;
20932104 promises . push ( self . getUserAttempts ( quiz . id , 'all' , true , false , true , siteId ) . then ( function ( atts ) {
20942105 attempts = atts ;
20952106 } ) ) ;
2107+ promises . push ( self . getAttemptAccessInformation ( quiz . id , 0 , false , true , siteId ) . then ( function ( info ) {
2108+ attemptAccessInfo = info ;
2109+ } ) ) ;
20962110
20972111 return $q . all ( promises ) ;
20982112 } ) . then ( function ( ) {
20992113 var attempt = attempts [ attempts . length - 1 ] ;
21002114 if ( ! attempt || self . isAttemptFinished ( attempt . state ) ) {
2115+ // Check if the user can attempt the quiz.
2116+ if ( attemptAccessInfo . preventnewattemptreasons . length ) {
2117+ return $q . reject ( $mmText . buildMessage ( attemptAccessInfo . preventnewattemptreasons ) ) ;
2118+ }
2119+
21012120 startAttempt = true ;
21022121 attempt = undefined ;
21032122 }
@@ -2161,7 +2180,14 @@ angular.module('mm.addons.mod_quiz')
21612180 if ( scope ) {
21622181 scope . $destroy ( ) ;
21632182 }
2183+ deleted = true ;
2184+ delete downloadPromises [ siteId ] [ module . id ] ;
21642185 } ) ;
2186+
2187+ if ( ! deleted ) {
2188+ downloadPromises [ siteId ] [ module . id ] = prefetchPromise ;
2189+ }
2190+ return prefetchPromise ;
21652191 } ;
21662192
21672193 /**
@@ -2192,7 +2218,13 @@ angular.module('mm.addons.mod_quiz')
21922218 var rules = quizAccessInfo . activerulenames ;
21932219 return $mmaModQuizAccessRulesDelegate . getFixedPreflightData ( rules , quiz , attempt , preflightData , true , siteId )
21942220 . then ( function ( ) {
2195- // Don't return anything.
2221+
2222+ if ( ! attempt ) {
2223+ // We need to create a new attempt.
2224+ return self . startAttempt ( quiz . id , preflightData ) . then ( function ( ) {
2225+ // Don't return anything.
2226+ } ) ;
2227+ }
21962228 } ) ;
21972229 }
21982230 } ;
@@ -2264,7 +2296,9 @@ angular.module('mm.addons.mod_quiz')
22642296 }
22652297
22662298 angular . forEach ( pages , function ( page ) {
2267- promises . push ( self . getAttemptReview ( attempt . id , page , true , siteId ) ) ;
2299+ promises . push ( self . getAttemptReview ( attempt . id , page , true , siteId ) . catch ( function ( ) {
2300+ // Ignore failures, maybe the user can't review the attempt.
2301+ } ) ) ;
22682302 } ) ;
22692303 // All questions in same page.
22702304 promises . push ( self . getAttemptReview ( attempt . id , - 1 , true , siteId ) . then ( function ( data ) {
@@ -2274,6 +2308,8 @@ angular.module('mm.addons.mod_quiz')
22742308 questionPromises . push ( $mmQuestionHelper . prefetchQuestionFiles ( question , siteId ) ) ;
22752309 } ) ;
22762310 return $q . all ( questionPromises ) ;
2311+ } , function ( ) {
2312+ // Ignore failures, maybe the user can't review the attempt.
22772313 } ) ) ;
22782314 } else {
22792315
0 commit comments