Conditions | 42 |
Paths | > 20000 |
Total Lines | 265 |
Code Lines | 156 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
77 | private function displayQuestionListByAttempt( |
||
78 | Exercise $objExercise, |
||
79 | TrackEExercise $exerciseTracking |
||
80 | ): array { |
||
81 | $courseId = api_get_course_int_id(); |
||
82 | $sessionId = api_get_session_id(); |
||
83 | |||
84 | $question_list = explode(',', $exerciseTracking->getDataTracking()); |
||
85 | $question_list = array_map('intval', $question_list); |
||
86 | |||
87 | if ($objExercise->getResultAccess()) { |
||
88 | $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id( |
||
89 | $exerciseTracking->getExeId() |
||
90 | ); |
||
91 | |||
92 | if (false === $objExercise->hasResultsAccess($exercise_stat_info)) { |
||
93 | throw new Exception(get_lang('YouPassedTheLimitOfXMinutesToSeeTheResults'), $objExercise->getResultsAccess()); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | $total_score = $total_weight = 0; |
||
98 | |||
99 | // Hide results |
||
100 | $show_results = false; |
||
101 | $show_only_score = false; |
||
102 | |||
103 | if (\in_array( |
||
104 | $objExercise->results_disabled, |
||
105 | [ |
||
106 | RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER, |
||
107 | RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS, |
||
108 | RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING, |
||
109 | ], |
||
110 | true |
||
111 | )) { |
||
112 | $show_results = true; |
||
113 | } |
||
114 | |||
115 | if (\in_array( |
||
116 | $objExercise->results_disabled, |
||
117 | [ |
||
118 | RESULT_DISABLE_SHOW_SCORE_ONLY, |
||
119 | RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES, |
||
120 | RESULT_DISABLE_RANKING, |
||
121 | ], |
||
122 | true |
||
123 | )) { |
||
124 | $show_only_score = true; |
||
125 | } |
||
126 | |||
127 | // Not display expected answer, but score, and feedback |
||
128 | if (RESULT_DISABLE_SHOW_SCORE_ONLY === $objExercise->results_disabled |
||
129 | && EXERCISE_FEEDBACK_TYPE_END === $objExercise->getFeedbackType() |
||
130 | ) { |
||
131 | $show_results = true; |
||
132 | $show_only_score = false; |
||
133 | } |
||
134 | |||
135 | $showTotalScoreAndUserChoicesInLastAttempt = true; |
||
136 | $showTotalScore = true; |
||
137 | |||
138 | if (\in_array( |
||
139 | $objExercise->results_disabled, |
||
140 | [ |
||
141 | RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT, |
||
142 | RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK, |
||
143 | RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK, |
||
144 | ], |
||
145 | true |
||
146 | )) { |
||
147 | $show_only_score = true; |
||
148 | $show_results = true; |
||
149 | $numberAttempts = 0; |
||
150 | |||
151 | if ($objExercise->attempts > 0) { |
||
152 | $attempts = Event::getExerciseResultsByUser( |
||
|
|||
153 | api_get_user_id(), |
||
154 | $objExercise->id, |
||
155 | $courseId, |
||
156 | $sessionId, |
||
157 | $exerciseTracking->getOrigLpId(), |
||
158 | $exerciseTracking->getOrigLpItemId(), |
||
159 | 'desc' |
||
160 | ); |
||
161 | |||
162 | if ($attempts) { |
||
163 | $numberAttempts = \count($attempts); |
||
164 | } |
||
165 | |||
166 | $showTotalScore = false; |
||
167 | |||
168 | if (RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT === $objExercise->results_disabled) { |
||
169 | $showTotalScore = true; |
||
170 | } |
||
171 | |||
172 | $showTotalScoreAndUserChoicesInLastAttempt = false; |
||
173 | |||
174 | if ($numberAttempts >= $objExercise->attempts) { |
||
175 | $showTotalScore = true; |
||
176 | $show_only_score = false; |
||
177 | $showTotalScoreAndUserChoicesInLastAttempt = true; |
||
178 | } |
||
179 | |||
180 | if (RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK === $objExercise->results_disabled) { |
||
181 | $showTotalScore = true; |
||
182 | $show_only_score = false; |
||
183 | $showTotalScoreAndUserChoicesInLastAttempt = false; |
||
184 | |||
185 | if ($numberAttempts >= $objExercise->attempts) { |
||
186 | $showTotalScoreAndUserChoicesInLastAttempt = true; |
||
187 | } |
||
188 | |||
189 | // Check if the current attempt is the last. |
||
190 | if (!empty($attempts)) { |
||
191 | $showTotalScoreAndUserChoicesInLastAttempt = false; |
||
192 | $position = 1; |
||
193 | |||
194 | foreach ($attempts as $attempt) { |
||
195 | if ($exerciseTracking->getExeId() === $attempt['exe_id']) { |
||
196 | break; |
||
197 | } |
||
198 | |||
199 | $position++; |
||
200 | } |
||
201 | |||
202 | if ($position === $objExercise->attempts) { |
||
203 | $showTotalScoreAndUserChoicesInLastAttempt = true; |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | |||
209 | if (RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK === |
||
210 | $objExercise->results_disabled |
||
211 | ) { |
||
212 | $show_only_score = false; |
||
213 | $showTotalScore = false; |
||
214 | if ($numberAttempts >= $objExercise->attempts) { |
||
215 | $showTotalScore = true; |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | |||
220 | $category_list = [ |
||
221 | 'none' => [ |
||
222 | 'score' => 0, |
||
223 | 'total' => 0, |
||
224 | ], |
||
225 | ]; |
||
226 | $exerciseResultCoordinates = []; |
||
227 | |||
228 | $result = []; |
||
229 | // Loop over all question to show results for each of them, one by one |
||
230 | foreach ($question_list as $questionId) { |
||
231 | // Creates a temporary Question object |
||
232 | $objQuestionTmp = Question::read($questionId, $objExercise->course); |
||
233 | |||
234 | // We're inside *one* question. Go through each possible answer for this question |
||
235 | $result = $objExercise->manage_answer( |
||
236 | $exerciseTracking->getExeId(), |
||
237 | $questionId, |
||
238 | null, |
||
239 | 'exercise_result', |
||
240 | $exerciseResultCoordinates, |
||
241 | false, |
||
242 | true, |
||
243 | $show_results, |
||
244 | $objExercise->selectPropagateNeg(), |
||
245 | [], |
||
246 | $showTotalScoreAndUserChoicesInLastAttempt |
||
247 | ); |
||
248 | |||
249 | if (false === $result) { |
||
250 | continue; |
||
251 | } |
||
252 | |||
253 | $total_score += $result['score']; |
||
254 | $total_weight += $result['weight']; |
||
255 | |||
256 | $my_total_score = $result['score']; |
||
257 | $my_total_weight = $result['weight']; |
||
258 | $scorePassed = ExerciseLib::scorePassed($my_total_score, $my_total_weight); |
||
259 | |||
260 | // Category report |
||
261 | $category_was_added_for_this_test = false; |
||
262 | if (!empty($objQuestionTmp->category)) { |
||
263 | if (!isset($category_list[$objQuestionTmp->category])) { |
||
264 | $category_list[$objQuestionTmp->category] = [ |
||
265 | 'score' => 0, |
||
266 | 'total' => 0, |
||
267 | 'total_questions' => 0, |
||
268 | 'passed' => 0, |
||
269 | 'wrong' => 0, |
||
270 | 'no_answer' => 0, |
||
271 | ]; |
||
272 | } |
||
273 | |||
274 | $category_list[$objQuestionTmp->category]['score'] += $my_total_score; |
||
275 | $category_list[$objQuestionTmp->category]['total'] += $my_total_weight; |
||
276 | |||
277 | if ($scorePassed) { |
||
278 | // Only count passed if score is not empty |
||
279 | if (!empty($my_total_score)) { |
||
280 | $category_list[$objQuestionTmp->category]['passed']++; |
||
281 | } |
||
282 | } elseif ($result['user_answered']) { |
||
283 | $category_list[$objQuestionTmp->category]['wrong']++; |
||
284 | } else { |
||
285 | $category_list[$objQuestionTmp->category]['no_answer']++; |
||
286 | } |
||
287 | |||
288 | $category_list[$objQuestionTmp->category]['total_questions']++; |
||
289 | $category_was_added_for_this_test = true; |
||
290 | } |
||
291 | |||
292 | if (!empty($objQuestionTmp->category_list)) { |
||
293 | foreach ($objQuestionTmp->category_list as $category_id) { |
||
294 | $category_list[$category_id]['score'] += $my_total_score; |
||
295 | $category_list[$category_id]['total'] += $my_total_weight; |
||
296 | $category_was_added_for_this_test = true; |
||
297 | } |
||
298 | } |
||
299 | |||
300 | // No category for this question! |
||
301 | if (!$category_was_added_for_this_test) { |
||
302 | $category_list['none']['score'] += $my_total_score; |
||
303 | $category_list['none']['total'] += $my_total_weight; |
||
304 | } |
||
305 | } |
||
306 | |||
307 | if (($show_results || $show_only_score) && $showTotalScore) { |
||
308 | if ($result |
||
309 | && isset($result['answer_type']) |
||
310 | && MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY !== $result['answer_type'] |
||
311 | ) { |
||
312 | $pluginEvaluation = QuestionOptionsEvaluationPlugin::create(); |
||
313 | if ('true' === $pluginEvaluation->get(QuestionOptionsEvaluationPlugin::SETTING_ENABLE)) { |
||
314 | $formula = $pluginEvaluation->getFormulaForExercise($objExercise->getId()); |
||
315 | |||
316 | if (!empty($formula)) { |
||
317 | $total_score = $pluginEvaluation->getResultWithFormula( |
||
318 | $exerciseTracking->getExeId(), |
||
319 | $formula |
||
320 | ); |
||
321 | $total_weight = $pluginEvaluation->getMaxScore(); |
||
322 | } |
||
323 | } |
||
324 | } |
||
325 | } |
||
326 | |||
327 | if ($this->isAllowedToSeeResults()) { |
||
328 | $show_results = true; |
||
329 | } |
||
330 | |||
331 | if (!$show_results && !$show_only_score && RESULT_DISABLE_RADAR !== $objExercise->results_disabled) { |
||
332 | throw new AccessDeniedException(); |
||
333 | } |
||
334 | |||
335 | // Adding total |
||
336 | $category_list['total'] = [ |
||
337 | 'score' => $total_score, |
||
338 | 'total' => $total_weight, |
||
339 | ]; |
||
340 | |||
341 | return $category_list; |
||
342 | } |
||
447 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.