Skip to content

Commit b24e3e4

Browse files
committed
MOBILE-1581 quiz: Fix offline status calculation errors
1 parent 24b3e51 commit b24e3e4

File tree

4 files changed

+103
-3
lines changed

4 files changed

+103
-3
lines changed

www/addons/qbehaviour/deferredfeedback/handlers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ angular.module('mm.addons.qbehaviour_deferredfeedback')
7474

7575
// Answers have changed.
7676
if (typeof isComplete == 'function') {
77-
complete = isComplete(question, newBasicAnswers);
77+
// Pass all the answers since some behaviours might need the extra data.
78+
complete = isComplete(question, question.answers);
7879
} else {
80+
// Only pass the basic answers since questions should be independent of extra data.
7981
complete = $mmQuestion.isCompleteResponse(question, newBasicAnswers);
8082
}
8183

www/addons/qbehaviour/informationitem/handlers.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,29 @@ angular.module('mm.addons.qbehaviour_informationitem')
2121
* @ngdoc service
2222
* @name $mmaQbehaviourInformationItemHandler
2323
*/
24-
.factory('$mmaQbehaviourInformationItemHandler', function($mmQuestionHelper) {
24+
.factory('$mmaQbehaviourInformationItemHandler', function($mmQuestionHelper, $mmQuestion) {
2525

2626
var self = {};
2727

28+
/**
29+
* Determine a question state based on its answer(s).
30+
*
31+
* @param {String} component Component the question belongs to.
32+
* @param {Number} attemptId Attempt ID the question belongs to.
33+
* @param {Object} question The question.
34+
* @param {String} [siteId] Site ID. If not defined, current site.
35+
* @param {Function} [isComplete] To override the default isCompleteResponse check. Optional.
36+
* @param {Function} [isSame] To override the default isSameResponse check. Optional.
37+
* @return {Promise} Promise resolved with the state or false if cannot determine state.
38+
*/
39+
self.determineQuestionState = function(component, attemptId, question, siteId, isComplete, isSame) {
40+
if (question.answers['-seen']) {
41+
return $mmQuestion.getState('complete');
42+
}
43+
44+
return false;
45+
};
46+
2847
/**
2948
* Whether or not the module is enabled for the site.
3049
*

www/addons/qbehaviour/manualgraded/handlers.js

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,78 @@ angular.module('mm.addons.qbehaviour_manualgraded')
2121
* @ngdoc service
2222
* @name $mmaQbehaviourManualGradedHandler
2323
*/
24-
.factory('$mmaQbehaviourManualGradedHandler', function() {
24+
.factory('$mmaQbehaviourManualGradedHandler', function($mmQuestion) {
2525

2626
var self = {};
2727

28+
/**
29+
* Determine a question state based on its answer(s).
30+
*
31+
* @param {String} component Component the question belongs to.
32+
* @param {Number} attemptId Attempt ID the question belongs to.
33+
* @param {Object} question The question.
34+
* @param {String} [siteId] Site ID. If not defined, current site.
35+
* @param {Function} [isComplete] To override the default isCompleteResponse check. Optional.
36+
* @param {Function} [isSame] To override the default isSameResponse check. Optional.
37+
* @return {Promise} Promise resolved with the state or false if cannot determine state.
38+
*/
39+
self.determineQuestionState = function(component, attemptId, question, siteId, isComplete, isSame) {
40+
// Check if we have local data for the question.
41+
return $mmQuestion.getQuestion(component, attemptId, question.slot, siteId).catch(function() {
42+
// No entry found, use the original data.
43+
return question;
44+
}).then(function(dbQuestion) {
45+
var state = $mmQuestion.getState(dbQuestion.state);
46+
47+
if (state.finished || !state.active) {
48+
// Question is finished.
49+
return false;
50+
}
51+
52+
// We need to check if the answers have changed. Retrieve current stored answers.
53+
return $mmQuestion.getQuestionAnswers(component, attemptId, question.slot, false, siteId);
54+
}).then(function(prevAnswers) {
55+
var complete,
56+
newState,
57+
prevBasicAnswers,
58+
newBasicAnswers = $mmQuestion.getBasicAnswers(question.answers);
59+
60+
prevAnswers = $mmQuestion.convertAnswersArrayToObject(prevAnswers);
61+
prevBasicAnswers = $mmQuestion.getBasicAnswers(prevAnswers);
62+
63+
// Check if answers haven't changed.
64+
if (typeof isSame == 'function') {
65+
if (isSame(question, prevAnswers, prevBasicAnswers, question.answers, newBasicAnswers)) {
66+
return false;
67+
}
68+
} else {
69+
if ($mmQuestion.isSameResponse(question, prevBasicAnswers, newBasicAnswers)) {
70+
return false;
71+
}
72+
}
73+
74+
// Answers have changed.
75+
if (typeof isComplete == 'function') {
76+
// Pass all the answers since some behaviours might need the extra data.
77+
complete = isComplete(question, question.answers);
78+
} else {
79+
// Only pass the basic answers since questions should be independent of extra data.
80+
complete = $mmQuestion.isCompleteResponse(question, newBasicAnswers);
81+
}
82+
83+
if (complete == -1) {
84+
newState = 'unknown';
85+
} else if (complete) {
86+
newState = 'complete';
87+
} else {
88+
newState = 'todo';
89+
}
90+
91+
return $mmQuestion.getState(newState);
92+
93+
});
94+
};
95+
2896
/**
2997
* Whether or not the module is enabled for the site.
3098
*

www/addons/qtype/essay/handlers.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ angular.module('mm.addons.qtype_essay')
2525

2626
var self = {};
2727

28+
/**
29+
* Get the behaviour for this question.
30+
*
31+
* @param {Object} question Question to get the directive for.
32+
* @param {String} behaviour Default behaviour.
33+
* @return {String} Behaviour name.
34+
*/
35+
self.getBehaviour = function(question, behaviour) {
36+
return 'manualgraded';
37+
};
38+
2839
/**
2940
* Check if a response is complete.
3041
*

0 commit comments

Comments
 (0)