@@ -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 *
0 commit comments