Skip to content

Commit 91a3aef

Browse files
committed
MOBILE-1543 quiz: Listen for status changes in quiz entry page
1 parent 41483c1 commit 91a3aef

File tree

1 file changed

+62
-22
lines changed

1 file changed

+62
-22
lines changed

www/addons/mod_quiz/controllers/index.js

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ angular.module('mm.addons.mod_quiz')
2323
*/
2424
.controller('mmaModQuizIndexCtrl', function($scope, $stateParams, $mmaModQuiz, $mmCourse, $ionicPlatform, $q, $translate,
2525
$mmaModQuizHelper, $ionicHistory, $ionicScrollDelegate, $mmEvents, mmaModQuizEventAttemptFinished, $state,
26-
$mmQuestionBehaviourDelegate, $mmaModQuizSync, $mmText, $mmUtil, mmaModQuizEventAutomSynced,
27-
$mmCoursePrefetchDelegate, mmCoreDownloaded) {
26+
$mmQuestionBehaviourDelegate, $mmaModQuizSync, $mmText, $mmUtil, mmaModQuizEventAutomSynced, $mmSite,
27+
$mmCoursePrefetchDelegate, mmCoreDownloaded, mmCoreDownloading, mmCoreEventPackageStatusChanged,
28+
mmaModQuizComponent) {
2829
var module = $stateParams.module || {},
2930
courseId = $stateParams.courseid,
3031
quiz,
@@ -37,7 +38,8 @@ angular.module('mm.addons.mod_quiz')
3738
attemptAccessInfo,
3839
moreAttempts,
3940
scrollView = $ionicScrollDelegate.$getByHandle('mmaModQuizIndexScroll'),
40-
autoReview;
41+
autoReview,
42+
currentStatus;
4143

4244
$scope.title = module.name;
4345
$scope.description = module.description;
@@ -126,6 +128,12 @@ angular.module('mm.addons.mod_quiz')
126128
return $mmaModQuiz.getUserAttempts(quiz.id).then(function(atts) {
127129
attempts = atts;
128130

131+
if ($mmaModQuiz.isQuizOffline(quiz)) {
132+
// Handle status. We don't add getStatus to promises because it should be fast.
133+
setStatusListener();
134+
getStatus().then(showStatus);
135+
}
136+
129137
return treatAttempts().then(function() {
130138
// Check if user can create/continue attempts.
131139
if (attempts.length) {
@@ -336,6 +344,38 @@ angular.module('mm.addons.mod_quiz')
336344
});
337345
}
338346

347+
// Get status of the quiz.
348+
function getStatus() {
349+
var revision = $mmaModQuiz.getQuizRevisionFromAttempts(attempts),
350+
timemodified = $mmaModQuiz.getQuizTimemodifiedFromAttempts(attempts);
351+
352+
return $mmCoursePrefetchDelegate.getModuleStatus(module, courseId, revision, timemodified);
353+
}
354+
355+
// Set a listener to monitor changes on this SCORM status to show a message to the user.
356+
function setStatusListener() {
357+
if (typeof statusObserver !== 'undefined') {
358+
return; // Already set.
359+
}
360+
361+
// Listen for changes on this module status to show a message to the user.
362+
statusObserver = $mmEvents.on(mmCoreEventPackageStatusChanged, function(data) {
363+
if (data.siteid === $mmSite.getId() && data.componentId === module.id &&
364+
data.component === mmaModQuizComponent) {
365+
showStatus(data.status);
366+
}
367+
});
368+
}
369+
370+
// Showing or hide a status message depending on the SCORM status.
371+
function showStatus(status) {
372+
currentStatus = status;
373+
374+
if (status == mmCoreDownloading) {
375+
$scope.downloading = true;
376+
}
377+
}
378+
339379
// Fetch the Quiz data.
340380
fetchQuizData().then(function() {
341381
$mmaModQuiz.logViewQuiz(quiz.id).then(function() {
@@ -369,28 +409,28 @@ angular.module('mm.addons.mod_quiz')
369409

370410
// Attempt the quiz.
371411
$scope.attemptQuiz = function() {
412+
if ($scope.downloading) {
413+
// Scope is being downloaded, abort.
414+
return;
415+
}
416+
372417
if ($mmaModQuiz.isQuizOffline(quiz)) {
373418
// Quiz supports offline, check if it needs to be downloaded.
374-
var revision = $mmaModQuiz.getQuizRevisionFromAttempts(attempts),
375-
timemodified = $mmaModQuiz.getQuizTimemodifiedFromAttempts(attempts),
376-
modal = $mmUtil.showModalLoading();
377-
378-
$mmCoursePrefetchDelegate.getModuleStatus(module, courseId, revision, timemodified).then(function(status) {
379-
if (status != mmCoreDownloaded) {
380-
// Prefetch the quiz.
381-
return $mmaModQuiz.prefetch(module, courseId).then(function() {
382-
// Success downloading, open quiz.
383-
openQuiz();
384-
}).catch(function() {
385-
$mmUtil.showErrorModal('mma.mod_quiz.errordownloading', true);
386-
});
387-
} else {
388-
// Already downloaded, open it.
419+
if (currentStatus != mmCoreDownloaded) {
420+
// Prefetch the quiz.
421+
$scope.downloading = true;
422+
return $mmaModQuiz.prefetch(module, courseId, true).then(function() {
423+
// Success downloading, open quiz.
389424
openQuiz();
390-
}
391-
}).finally(function() {
392-
modal.dismiss();
393-
});
425+
}).catch(function(error) {
426+
$mmaModQuizHelper.showError(error, 'mma.mod_quiz.errordownloading');
427+
}).finally(function() {
428+
$scope.downloading = false;
429+
});
430+
} else {
431+
// Already downloaded, open it.
432+
openQuiz();
433+
}
394434
} else {
395435
openQuiz();
396436
}

0 commit comments

Comments
 (0)