@@ -17,32 +17,7 @@ import { Injectable } from '@angular/core';
1717import { CoreQuestionBehaviourHandler } from '@core/question/providers/behaviour-delegate' ;
1818import { CoreQuestionDelegate } from '@core/question/providers/delegate' ;
1919import { CoreQuestionProvider , CoreQuestionState } from '@core/question/providers/question' ;
20-
21- /**
22- * Check if a response is complete.
23- *
24- * @param question The question.
25- * @param answers Object with the question answers (without prefix).
26- * @param component The component the question is related to.
27- * @param componentId Component ID.
28- * @return 1 if complete, 0 if not complete, -1 if cannot determine.
29- */
30- export type isCompleteResponseFunction = ( question : any , answers : any , component : string , componentId : string | number ) => number ;
31-
32- /**
33- * Check if two responses are the same.
34- *
35- * @param question Question.
36- * @param prevAnswers Object with the previous question answers.
37- * @param prevBasicAnswers Object with the previous basic" answers (without sequencecheck, certainty, ...).
38- * @param newAnswers Object with the new question answers.
39- * @param newBasicAnswers Object with the previous basic" answers (without sequencecheck, certainty, ...).
40- * @param component The component the question is related to.
41- * @param componentId Component ID.
42- * @return Whether they're the same.
43- */
44- export type isSameResponseFunction = ( question : any , prevAnswers : any , prevBasicAnswers : any , newAnswers : any ,
45- newBasicAnswers : any , component : string , componentId : string | number ) => boolean ;
20+ import { AddonQbehaviourDeferredFeedbackHandler } from '@addon/qbehaviour/deferredfeedback/providers/handler' ;
4621
4722/**
4823 * Handler to support manual graded question behaviour.
@@ -52,7 +27,9 @@ export class AddonQbehaviourManualGradedHandler implements CoreQuestionBehaviour
5227 name = 'AddonQbehaviourManualGraded' ;
5328 type = 'manualgraded' ;
5429
55- constructor ( private questionDelegate : CoreQuestionDelegate , private questionProvider : CoreQuestionProvider ) {
30+ constructor ( protected questionDelegate : CoreQuestionDelegate ,
31+ protected questionProvider : CoreQuestionProvider ,
32+ protected deferredFeedbackHandler : AddonQbehaviourDeferredFeedbackHandler ) {
5633 // Nothing to do.
5734 }
5835
@@ -68,84 +45,8 @@ export class AddonQbehaviourManualGradedHandler implements CoreQuestionBehaviour
6845 */
6946 determineNewState ( component : string , attemptId : number , question : any , componentId : string | number , siteId ?: string )
7047 : CoreQuestionState | Promise < CoreQuestionState > {
71- return this . determineNewStateManualGraded ( component , attemptId , question , componentId , siteId ) ;
72- }
73-
74- /**
75- * Determine a question new state based on its answer(s) for manual graded question behaviour.
76- *
77- * @param component Component the question belongs to.
78- * @param attemptId Attempt ID the question belongs to.
79- * @param question The question.
80- * @param componentId Component ID.
81- * @param siteId Site ID. If not defined, current site.
82- * @param isCompleteFn Function to override the default isCompleteResponse check.
83- * @param isSameFn Function to override the default isSameResponse check.
84- * @return Promise resolved with state.
85- */
86- async determineNewStateManualGraded ( component : string , attemptId : number , question : any , componentId : string | number ,
87- siteId ?: string , isCompleteFn ?: isCompleteResponseFunction , isSameFn ?: isSameResponseFunction )
88- : Promise < CoreQuestionState > {
89-
90- // Check if we have local data for the question.
91- let dbQuestion ;
92- try {
93- dbQuestion = await this . questionProvider . getQuestion ( component , attemptId , question . slot , siteId ) ;
94- } catch ( error ) {
95- // No entry found, use the original data.
96- dbQuestion = question ;
97- }
98-
99- const state = this . questionProvider . getState ( dbQuestion . state ) ;
100-
101- if ( state . finished || ! state . active ) {
102- // Question is finished, it cannot change.
103- return state ;
104- }
105-
106- const newBasicAnswers = this . questionProvider . getBasicAnswers ( question . answers ) ;
107-
108- if ( dbQuestion . state ) {
109- // Question already has a state stored. Check if answer has changed.
110- let prevAnswers = await this . questionProvider . getQuestionAnswers ( component , attemptId , question . slot , false , siteId ) ;
111-
112- prevAnswers = this . questionProvider . convertAnswersArrayToObject ( prevAnswers , true ) ;
113- const prevBasicAnswers = this . questionProvider . getBasicAnswers ( prevAnswers ) ;
114-
115- // If answers haven't changed the state is the same.
116- if ( isSameFn ) {
117- if ( isSameFn ( question , prevAnswers , prevBasicAnswers , question . answers , newBasicAnswers ,
118- component , componentId ) ) {
119- return state ;
120- }
121- } else {
122- if ( this . questionDelegate . isSameResponse ( question , prevBasicAnswers , newBasicAnswers , component , componentId ) ) {
123- return state ;
124- }
125- }
126- }
127-
128- // Check if the response is complete and calculate the new state.
129- let complete : number ;
130- let newState : string ;
131-
132- if ( isCompleteFn ) {
133- // Pass all the answers since some behaviours might need the extra data.
134- complete = isCompleteFn ( question , question . answers , component , componentId ) ;
135- } else {
136- // Only pass the basic answers since questions should be independent of extra data.
137- complete = this . questionDelegate . isCompleteResponse ( question , newBasicAnswers , component , componentId ) ;
138- }
139-
140- if ( complete < 0 ) {
141- newState = 'cannotdeterminestatus' ;
142- } else if ( complete > 0 ) {
143- newState = 'complete' ;
144- } else {
145- newState = 'todo' ;
146- }
147-
148- return this . questionProvider . getState ( newState ) ;
48+ // Same implementation as the deferred feedback. Use that function instead of replicating it.
49+ return this . deferredFeedbackHandler . determineNewStateDeferred ( component , attemptId , question , componentId , siteId ) ;
14950 }
15051
15152 /**
0 commit comments