@@ -49,74 +49,101 @@ func RetrieveQuestions(c *fiber.Ctx) (err error) {
4949 }
5050
5151 var (
52- questions = questionConfig .Questions
53- requiredQuestions = questionConfig .RequiredQuestions
54- optionalQuestions = questionConfig .OptionalQuestions
55- number = questionConfig .Spec .NumberOfQuestions
56- inOrder = questionConfig .Spec .InOrder
52+ // questions = questionConfig.Questions
53+ // number = questionConfig.Spec.NumberOfQuestions
54+ requiredQuestions = questionConfig .RequiredQuestions
55+ optionalQuestions = questionConfig .OptionalQuestions
56+ campusQuestions = questionConfig .CampusQuestions
57+ numberOfOptionalQuestions = questionConfig .Spec .NumberOfOptionalQuestions
58+ numberOfCampusQuestions = questionConfig .Spec .NumberOfCampusQuestions
59+ inOrder = questionConfig .Spec .InOrder
5760 )
5861
62+ number := len (requiredQuestions )
63+ if numberOfOptionalQuestions == - 1 {
64+ number += len (optionalQuestions )
65+ } else if numberOfOptionalQuestions >= 0 {
66+ number += numberOfOptionalQuestions
67+ } else {
68+ return common .InternalServerError ("[retrieve questions]: number of optional questions invalid" )
69+ }
70+ if numberOfCampusQuestions == - 1 {
71+ number += len (campusQuestions )
72+ } else if numberOfCampusQuestions >= 0 {
73+ number += numberOfCampusQuestions
74+ } else {
75+ return common .InternalServerError ("[retrieve questions]: number of campus questions invalid" )
76+ }
77+
5978 var questionsResponse = QuestionConfig {
6079 Version : version ,
6180 Spec : QuestionSpec {
62- NumberOfQuestions : number ,
63- InOrder : inOrder ,
81+ NumberOfOptionalQuestions : numberOfOptionalQuestions ,
82+ NumberOfCampusQuestions : numberOfCampusQuestions ,
83+ NumberOfQuestions : number ,
84+ InOrder : inOrder ,
6485 },
6586 }
6687
67- if number == 0 {
68- // send all questions
69- questionsResponse .Questions = make ([]Question , len (questions ))
70- copy (questionsResponse .Questions , questions )
88+ questionsResponse .Questions = make ([]Question , number )
89+ tmpQuestions := make ([]* Question , 0 , number )
7190
72- } else if number == - 1 {
73- // send all required questions
74- questionsResponse . Questions = make ([] Question , len ( requiredQuestions ))
91+ if number == 0 {
92+ return common . InternalServerError ( "[retrieve questions]: number of questions too small" )
93+ }
7594
76- for i , question := range requiredQuestions {
77- questionsResponse .Questions [i ] = * question
78- }
79- } else {
80- // send all required questions and part of random optional questions according to number
81- // be sure that number == len(requiredQuestions) + len(chosenOptionalQuestions)
82- // if number < len(requiredQuestions), return error
83- questionsResponse .Questions = make ([]Question , number )
84- optionalQuestionsNumber := number - len (requiredQuestions )
85- if optionalQuestionsNumber < 0 {
86- return common .InternalServerError ("[retrieve questions]: number of questions too small" )
87- }
95+ copy (tmpQuestions , requiredQuestions )
8896
89- for i , question := range requiredQuestions {
90- questionsResponse .Questions [i ] = * question
91- }
97+ // for i, question := range requiredQuestions {
98+ // tmpQuestions[i] = question
99+ // // questionsResponse.Questions[i] = *question
100+ // }
92101
102+ // questionConfig.Questions = append(questionConfig.Questions, optionalQuestions...)
103+ if numberOfOptionalQuestions == - 1 {
104+ // send all opntional questions
105+ tmpQuestions = append (tmpQuestions , optionalQuestions ... )
106+ } else if numberOfOptionalQuestions > 0 {
93107 // shuffle optional questions
94- if optionalQuestionsNumber > 0 {
95- chosenOptionalQuestions := make ([]* Question , len (optionalQuestions ))
96- copy (chosenOptionalQuestions , optionalQuestions )
97- rand .Shuffle (len (chosenOptionalQuestions ), func (i , j int ) {
98- chosenOptionalQuestions [i ], chosenOptionalQuestions [j ] = chosenOptionalQuestions [j ], chosenOptionalQuestions [i ]
99- })
100-
101- for i , question := range chosenOptionalQuestions {
102- questionsResponse .Questions [i + len (requiredQuestions )] = * question
103- if i == optionalQuestionsNumber - 1 {
104- break
105- }
106- }
107- }
108+ chosenOptionalQuestions := make ([]* Question , len (optionalQuestions ))
109+ copy (chosenOptionalQuestions , optionalQuestions )
110+ rand .Shuffle (len (chosenOptionalQuestions ), func (i , j int ) {
111+ chosenOptionalQuestions [i ], chosenOptionalQuestions [j ] = chosenOptionalQuestions [j ], chosenOptionalQuestions [i ]
112+ })
113+
114+ tmpQuestions = append (tmpQuestions , chosenOptionalQuestions [:numberOfOptionalQuestions ]... )
115+ }
116+
117+ if numberOfCampusQuestions == - 1 {
118+ // send all campus questions
119+ tmpQuestions = append (tmpQuestions , campusQuestions ... )
120+ } else if numberOfCampusQuestions > 0 {
121+ // shuffle campus questions
122+ chosenCampusQuestions := make ([]* Question , len (campusQuestions ))
123+ copy (chosenCampusQuestions , campusQuestions )
124+ rand .Shuffle (len (chosenCampusQuestions ), func (i , j int ) {
125+ chosenCampusQuestions [i ], chosenCampusQuestions [j ] = chosenCampusQuestions [j ], chosenCampusQuestions [i ]
126+ })
127+
128+ tmpQuestions = append (tmpQuestions , chosenCampusQuestions [:numberOfCampusQuestions ]... )
108129 }
109130
131+
132+
110133 if ! inOrder {
111- rand .Shuffle (len (questionsResponse . Questions ), func (i , j int ) {
112- questionsResponse . Questions [i ], questionsResponse . Questions [j ] = questionsResponse . Questions [j ], questionsResponse . Questions [i ]
134+ rand .Shuffle (len (tmpQuestions ), func (i , j int ) {
135+ tmpQuestions [i ], tmpQuestions [j ] = tmpQuestions [j ], tmpQuestions [i ]
113136 })
114137 } else {
115- sort .Slice (questionsResponse . Questions , func (i , j int ) bool {
116- return questionsResponse . Questions [i ].ID < questionsResponse . Questions [j ].ID
138+ sort .Slice (tmpQuestions , func (i , j int ) bool {
139+ return tmpQuestions [i ].ID < tmpQuestions [j ].ID
117140 })
118141 }
119142
143+ for i , question := range tmpQuestions {
144+ questionsResponse .Questions [i ] = * question
145+ }
146+
120147 // shuffle options
121148 for i := range questionsResponse .Questions {
122149 options := questionsResponse .Questions [i ].Options // copy slice pointer only
@@ -127,8 +154,12 @@ func RetrieveQuestions(c *fiber.Ctx) (err error) {
127154
128155 // clear analysis and answer
129156 for i := range questionsResponse .Questions {
157+ if questionsResponse .Questions [i ].Group == "campus" {
158+ questionsResponse .Questions [i ].Group = "optional"
159+ }
130160 questionsResponse .Questions [i ].Analysis = ""
131161 questionsResponse .Questions [i ].Answer = nil
162+ questionsResponse .Questions [i ].Option = questionsResponse .Questions [i ].Options
132163 }
133164
134165 return c .JSON (questionsResponse )
@@ -183,9 +214,10 @@ func AnswerQuestions(c *fiber.Ctx) (err error) {
183214 var (
184215 questions = questionConfig .Questions
185216 requiredQuestions = questionConfig .RequiredQuestions
186- number = questionConfig .Spec .NumberOfQuestions
187217 )
188218
219+ number := len (requiredQuestions ) + questionConfig .Spec .NumberOfOptionalQuestions + questionConfig .Spec .NumberOfCampusQuestions
220+
189221 // get all submitted question number and required question number
190222 submittedQuestionNumber := len (body .Answers )
191223 submittedRequiredQuestionNumber := 0
@@ -253,6 +285,9 @@ func AnswerQuestions(c *fiber.Ctx) (err error) {
253285 }
254286
255287 accessToken , refreshToken , err := user .CreateJWTToken ()
288+ if err != nil {
289+ return
290+ }
256291
257292 return c .JSON (SubmitResponse {
258293 Correct : true ,
0 commit comments