Skip to content

Commit bbdccc1

Browse files
committed
Exercise: Refactor CategorizedExerciseResultStateProvider to use UserHelper and improve session/course handling - refs BT#19742
1 parent 0ed74d5 commit bbdccc1

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

src/CoreBundle/State/CategorizedExerciseResultStateProvider.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use ApiPlatform\State\ProviderInterface;
1111
use Chamilo\CoreBundle\ApiResource\CategorizedExerciseResult;
1212
use Chamilo\CoreBundle\Entity\TrackEExercise;
13+
use Chamilo\CoreBundle\Helpers\UserHelper;
1314
use Chamilo\CoreBundle\Security\Authorization\Voter\TrackEExerciseVoter;
1415
use Doctrine\ORM\EntityManagerInterface;
1516
use Event;
@@ -33,6 +34,7 @@ class CategorizedExerciseResultStateProvider implements ProviderInterface
3334
public function __construct(
3435
private readonly EntityManagerInterface $entityManager,
3536
private readonly AuthorizationCheckerInterface $security,
37+
private readonly UserHelper $userHelper,
3638
private readonly RequestStack $requestStack
3739
) {}
3840

@@ -51,10 +53,12 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
5153
throw new Exception('Not allowed');
5254
}
5355

56+
$course = $trackExercise->getCourse();
57+
5458
$sessionHandler = $this->requestStack->getCurrentRequest()->getSession();
55-
$sessionHandler->set('_course', api_get_course_info_by_id($trackExercise->getCourse()->getId()));
59+
$sessionHandler->set('_course', api_get_course_info_by_id($course->getId()));
5660

57-
$objExercise = new Exercise();
61+
$objExercise = new Exercise($course->getId());
5862
$objExercise->read($trackExercise->getQuiz()->getIid());
5963

6064
ob_start();
@@ -66,7 +70,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
6670

6771
ob_end_clean();
6872

69-
$stats = self::getStatsTableByAttempt($objExercise, $categoryList);
73+
$stats = self::getStatsTableByAttempt($objExercise, $trackExercise, $categoryList);
7074

7175
return new CategorizedExerciseResult($trackExercise, $stats);
7276
}
@@ -78,8 +82,8 @@ private function displayQuestionListByAttempt(
7882
Exercise $objExercise,
7983
TrackEExercise $exerciseTracking
8084
): array {
81-
$courseId = api_get_course_int_id();
82-
$sessionId = api_get_session_id();
85+
$courseId = $exerciseTracking->getCourse()->getId();
86+
$sessionId = (int) $exerciseTracking->getSession()?->getId();
8387

8488
$question_list = explode(',', $exerciseTracking->getDataTracking());
8589
$question_list = array_map('intval', $question_list);
@@ -106,8 +110,7 @@ private function displayQuestionListByAttempt(
106110
RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
107111
RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
108112
RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
109-
],
110-
true
113+
]
111114
)) {
112115
$show_results = true;
113116
}
@@ -118,8 +121,7 @@ private function displayQuestionListByAttempt(
118121
RESULT_DISABLE_SHOW_SCORE_ONLY,
119122
RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
120123
RESULT_DISABLE_RANKING,
121-
],
122-
true
124+
]
123125
)) {
124126
$show_only_score = true;
125127
}
@@ -141,16 +143,15 @@ private function displayQuestionListByAttempt(
141143
RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
142144
RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK,
143145
RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK,
144-
],
145-
true
146+
]
146147
)) {
147148
$show_only_score = true;
148149
$show_results = true;
149150
$numberAttempts = 0;
150151

151152
if ($objExercise->attempts > 0) {
152153
$attempts = Event::getExerciseResultsByUser(
153-
api_get_user_id(),
154+
$this->userHelper->getCurrent()->getId(),
154155
$objExercise->id,
155156
$courseId,
156157
$sessionId,
@@ -341,8 +342,11 @@ private function displayQuestionListByAttempt(
341342
return $category_list;
342343
}
343344

344-
private static function getStatsTableByAttempt(Exercise $exercise, array $category_list = []): array
345-
{
345+
private static function getStatsTableByAttempt(
346+
Exercise $exercise,
347+
TrackEExercise $exerciseTracking,
348+
array $category_list = []
349+
): array {
346350
if (empty($category_list)) {
347351
return [];
348352
}

0 commit comments

Comments
 (0)