Skip to content

Commit 25d4208

Browse files
committed
Merge pull request #497 from dpalou/MOBILE-1600
Mobile 1600
2 parents f43d75e + 67912b8 commit 25d4208

File tree

2 files changed

+62
-12
lines changed

2 files changed

+62
-12
lines changed

www/addons/mod/quiz/controllers/index.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ angular.module('mm.addons.mod_quiz')
165165
// User just finished an attempt in offline and it seems it's been synced, since it's finished in online.
166166
// Go to the review of this attempt if the user hasn't left this view.
167167
if (!$scope.$$destroyed && $state.current.name == 'site.mod_quiz') {
168-
goToAutoReview();
168+
promises.push(goToAutoReview());
169169
}
170170
autoReview = undefined;
171171
}
@@ -332,7 +332,16 @@ angular.module('mm.addons.mod_quiz')
332332

333333
// Go to review an attempt that has just been finished.
334334
function goToAutoReview() {
335-
$state.go('site.mod_quiz-review', {courseid: courseId, quizid: quiz.id, attemptid: autoReview.attemptId});
335+
// Verify that user can see the review.
336+
var attemptId = autoReview.attemptId;
337+
if (quizAccessInfo.canreviewmyattempts) {
338+
return $mmaModQuiz.getAttemptReview(attemptId, -1).then(function() {
339+
return $state.go('site.mod_quiz-review', {courseid: courseId, quizid: quiz.id, attemptid: attemptId});
340+
}).catch(function() {
341+
// Ignore errors.
342+
});
343+
}
344+
return $q.when();
336345
}
337346

338347
// Open a quiz to attempt it.
@@ -450,18 +459,23 @@ angular.module('mm.addons.mod_quiz')
450459
return;
451460
}
452461

453-
var forwardView = $ionicHistory.forwardView();
462+
var forwardView = $ionicHistory.forwardView(),
463+
promise;
454464
if (forwardView && forwardView.stateName === 'site.mod_quiz-player') {
455465
if (autoReview && autoReview.synced) {
456-
goToAutoReview();
466+
promise = goToAutoReview();
457467
autoReview = undefined;
468+
} else {
469+
promise = $q.when();
458470
}
459471

460472
// Refresh data.
461473
$scope.quizLoaded = false;
462474
scrollView.scrollTop();
463-
refreshData().finally(function() {
464-
$scope.quizLoaded = true;
475+
promise.then(function() {
476+
refreshData().finally(function() {
477+
$scope.quizLoaded = true;
478+
});
465479
});
466480
} else {
467481
autoReview = undefined;

www/addons/mod/quiz/services/quiz.js

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)