Skip to content

Commit 11342c3

Browse files
committed
MOBILE-1576 quiz: Download quiz when starting it
1 parent 40abc7a commit 11342c3

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

www/addons/mod_quiz/controllers/index.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ 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) {
26+
$mmQuestionBehaviourDelegate, $mmaModQuizSync, $mmText, $mmUtil, mmaModQuizEventAutomSynced,
27+
$mmCoursePrefetchDelegate, mmCoreDownloaded) {
2728
var module = $stateParams.module || {},
2829
courseId = $stateParams.courseid,
2930
quiz,
@@ -313,6 +314,14 @@ angular.module('mm.addons.mod_quiz')
313314
$state.go('site.mod_quiz-review', {courseid: courseId, quizid: quiz.id, attemptid: autoReview.attemptId});
314315
}
315316

317+
// Open a quiz to attempt it.
318+
function openQuiz() {
319+
$state.go('site.mod_quiz-player', {
320+
courseid: courseId,
321+
quizid: quiz.id,
322+
moduleurl: module.url
323+
});
324+
}
316325

317326
// Fetch the Quiz data.
318327
fetchQuizData().then(function() {
@@ -345,6 +354,35 @@ angular.module('mm.addons.mod_quiz')
345354
});
346355
};
347356

357+
// Attempt the quiz.
358+
$scope.attemptQuiz = function() {
359+
if ($mmaModQuiz.isQuizOffline(quiz)) {
360+
// Quiz supports offline, check if it needs to be downloaded.
361+
var revision = $mmaModQuiz.getQuizRevisionFromAttempts(attempts),
362+
timemodified = $mmaModQuiz.getQuizTimemodifiedFromAttempts(attempts),
363+
modal = $mmUtil.showModalLoading();
364+
365+
$mmCoursePrefetchDelegate.getModuleStatus(module, courseId, revision, timemodified).then(function(status) {
366+
if (status != mmCoreDownloaded) {
367+
// Prefetch the quiz.
368+
return $mmaModQuiz.prefetch(module, courseId).then(function() {
369+
// Success downloading, open quiz.
370+
openQuiz();
371+
}).catch(function() {
372+
$mmUtil.showErrorModal('mma.mod_quiz.errordownloading', true);
373+
});
374+
} else {
375+
// Already downloaded, open it.
376+
openQuiz();
377+
}
378+
}).finally(function() {
379+
modal.dismiss();
380+
});
381+
} else {
382+
openQuiz();
383+
}
384+
};
385+
348386
// Update data when we come back from the player since the attempt status could have changed.
349387
// We want to skip the first $ionicView.enter event because it's when the view is created.
350388
var skip = true;

www/addons/mod_quiz/lang/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"confirmstartheader": "Timed quiz",
1414
"connectionerror": "Network connection lost. (Autosave failed).\n\nMake a note of any responses entered on this page in the last few minutes, then try to re-connect.\n\nOnce connection has been re-established, your responses should be saved and this message will disappear.",
1515
"continueattemptquiz": "Continue the last attempt",
16+
"errordownloading": "Error downloading required data.",
1617
"errorgetattempt": "Error getting attempt data.",
1718
"errorgetquestions": "Error getting questions.",
1819
"errorgetquiz": "Error getting quiz data.",

www/addons/mod_quiz/templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ <h2>{{ 'mma.mod_quiz.summaryofattempts' | translate }}</h2>
9292
<button class="button button-block" ng-click="sync()">{{ 'mm.settings.synchronizenow' | translate }}</button>
9393
</div>
9494
<div class="item" ng-if="buttonText">
95-
<a class="button button-block" ui-sref="site.mod_quiz-player({courseid: courseId, quizid: quiz.id, moduleurl: moduleUrl})">{{ buttonText | translate }}</a>
95+
<a class="button button-block" ng-click="attemptQuiz()">{{ buttonText | translate }}</a>
9696
</div>
9797
<div class="item" ng-if="!buttonText && ((unsupportedQuestions && unsupportedQuestions.length) || (unsupportedRules && unsupportedRules.length) || behaviourSupported === false)">
9898
<a class="button button-block" ng-href="{{moduleUrl}}" mm-link>{{ 'mm.core.openinbrowser' | translate }}</a>

0 commit comments

Comments
 (0)