Skip to content

Commit 2177aeb

Browse files
committed
MOBILE-1576 quiz: Show automatic sync warnings when viewing quiz
1 parent 11342c3 commit 2177aeb

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

www/addons/mod_quiz/controllers/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ angular.module('mm.addons.mod_quiz')
5555
$scope.title = quiz.name || $scope.title;
5656
$scope.description = quiz.intro || $scope.description;
5757

58+
// Try to get warnings from automatic sync.
59+
return $mmaModQuizSync.getQuizSyncWarnings(quiz.id).then(function(warnings) {
60+
if (warnings && warnings.length) {
61+
// Show warnings and delete them so they aren't shown again.
62+
$mmUtil.showErrorModal($mmText.buildMessage(warnings));
63+
return $mmaModQuizSync.setQuizSyncWarnings(quiz.id, []);
64+
}
65+
});
66+
}).then(function() {
5867
// Try to sync the quiz.
5968
return syncQuiz(!refresh, false).catch(function() {
6069
// Ignore errors, keep getting data even if sync fails.

www/addons/mod_quiz/services/quiz_sync.js

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@
1515
angular.module('mm.addons.mod_quiz')
1616

1717
.constant('mmaModQuizSynchronizationStore', 'mod_quiz_sync')
18+
.constant('mmaModQuizSynchronizationWarningsStore', 'mod_quiz_sync_warnings')
1819

19-
.config(function($mmSitesFactoryProvider, mmaModQuizSynchronizationStore) {
20+
.config(function($mmSitesFactoryProvider, mmaModQuizSynchronizationStore, mmaModQuizSynchronizationWarningsStore) {
2021
var stores = [
2122
{
2223
name: mmaModQuizSynchronizationStore,
2324
keyPath: 'quizid',
2425
indexes: []
26+
},
27+
{
28+
name: mmaModQuizSynchronizationWarningsStore,
29+
keyPath: 'quizid',
30+
indexes: []
2531
}
2632
];
2733
$mmSitesFactoryProvider.registerStores(stores);
@@ -36,7 +42,7 @@ angular.module('mm.addons.mod_quiz')
3642
*/
3743
.factory('$mmaModQuizSync', function($log, $mmaModQuiz, $mmSite, $mmSitesManager, $q, $mmaModQuizOffline, $mmQuestion,
3844
$mmQuestionDelegate, $mmApp, $mmConfig, $mmEvents, $translate, mmaModQuizSynchronizationStore, mmaModQuizSyncTime,
39-
mmaModQuizEventAutomSynced, mmCoreSettingsSyncOnlyOnWifi) {
45+
mmaModQuizEventAutomSynced, mmCoreSettingsSyncOnlyOnWifi, mmaModQuizSynchronizationWarningsStore) {
4046

4147
$log = $log.getInstance('$mmaModQuizSync');
4248

@@ -64,6 +70,27 @@ angular.module('mm.addons.mod_quiz')
6470
});
6571
};
6672

73+
/**
74+
* Get the synchronization warnings of a quiz.
75+
*
76+
* @module mm.addons.mod_quiz
77+
* @ngdoc method
78+
* @name $mmaModQuizSync#getQuizSyncTime
79+
* @param {Number} quizId Quiz ID.
80+
* @param {String} [siteId] Site ID. If not defined, current site.
81+
* @return {Promise} Promise resolved with the time.
82+
*/
83+
self.getQuizSyncWarnings = function(quizId, siteId) {
84+
siteId = siteId || $mmSite.getId();
85+
return $mmSitesManager.getSiteDb(siteId).then(function(db) {
86+
return db.get(mmaModQuizSynchronizationWarningsStore, quizId).then(function(entry) {
87+
return entry.warnings;
88+
}).catch(function() {
89+
return [];
90+
});
91+
});
92+
};
93+
6794
/**
6895
* Check if a quiz has data to synchronize.
6996
*
@@ -83,15 +110,15 @@ angular.module('mm.addons.mod_quiz')
83110
};
84111

85112
/**
86-
* Get the synchronization time of a quiz. Returns 0 if no time stored.
113+
* Set the synchronization time for a quiz.
87114
*
88115
* @module mm.addons.mod_quiz
89116
* @ngdoc method
90117
* @name $mmaModQuizSync#setQuizSyncTime
91118
* @param {Number} quizId Quiz ID.
92119
* @param {String} [siteId] Site ID. If not defined, current site.
93120
* @param {Number} [time] Time to set. If not defined, current time.
94-
* @return {Promise} Promise resolved with the time.
121+
* @return {Promise} Promise resolved when done.
95122
*/
96123
self.setQuizSyncTime = function(quizId, siteId, time) {
97124
siteId = siteId || $mmSite.getId();
@@ -103,6 +130,27 @@ angular.module('mm.addons.mod_quiz')
103130
});
104131
};
105132

133+
/**
134+
* Set the synchronization warnings for a quiz.
135+
*
136+
* @module mm.addons.mod_quiz
137+
* @ngdoc method
138+
* @name $mmaModQuizSync#setQuizSyncWarnings
139+
* @param {Number} quizId Quiz ID.
140+
* @param {String[]} warnings Warnings to set.
141+
* @param {String} [siteId] Site ID. If not defined, current site.
142+
* @return {Promise} Promise resolved when done.
143+
*/
144+
self.setQuizSyncWarnings = function(quizId, warnings, siteId) {
145+
siteId = siteId || $mmSite.getId();
146+
return $mmSitesManager.getSiteDb(siteId).then(function(db) {
147+
if (typeof warnings == 'undefined') {
148+
warnings = [];
149+
}
150+
return db.insert(mmaModQuizSynchronizationWarningsStore, {quizid: quizId, warnings: warnings});
151+
});
152+
};
153+
106154
/**
107155
* Try to synchronize all quizzes from current site that need it and haven't been synchronized in a while.
108156
*
@@ -161,6 +209,13 @@ angular.module('mm.addons.mod_quiz')
161209
if (!$mmaModQuiz.isQuizBeingPlayed(quiz.id, siteId)) {
162210
promises.push($mmaModQuiz.getQuizById(quiz.courseid, quiz.id, siteId).then(function(quiz) {
163211
return self.syncQuizIfNeeded(quiz, siteId).then(function(warnings) {
212+
if (warnings && warnings.length) {
213+
// Store the warnings to show them when the user opens the quiz.
214+
return self.setQuizSyncWarnings(quiz.id, warnings, siteId).then(function() {
215+
return warnings;
216+
});
217+
}
218+
}).then(function(warnings) {
164219
if (typeof warnings != 'undefined') {
165220
// We tried to sync. Send event.
166221
$mmEvents.trigger(mmaModQuizEventAutomSynced, {

0 commit comments

Comments
 (0)