Skip to content

Commit 121f897

Browse files
committed
Replace UserRelCourseVoteHelper with UserRelCourseVoteRepository for course rating controller #2860
1 parent ee10344 commit 121f897

File tree

3 files changed

+74
-50
lines changed

3 files changed

+74
-50
lines changed

src/CoreBundle/Controller/CatalogueController.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
use Chamilo\CoreBundle\Helpers\UserHelper;
2020
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
2121
use Chamilo\CoreBundle\Repository\SessionRepository;
22+
use Chamilo\CoreBundle\Repository\UserRelCourseVoteRepository;
2223
use Chamilo\CoreBundle\Settings\SettingsManager;
2324
use Chamilo\CoreBundle\Helpers\TrackingStatsHelper;
24-
use Chamilo\CoreBundle\Helpers\UserRelCourseVoteHelper;
2525
use DateTime;
2626
use Doctrine\ORM\EntityManagerInterface;
2727
use ExtraField;
@@ -42,20 +42,17 @@ public function __construct(
4242
private readonly CourseRepository $courseRepository,
4343
private readonly TrackingStatsHelper $trackingStatsHelper,
4444
private readonly SessionRepository $sessionRepository,
45-
private readonly UserRelCourseVoteHelper $userRelCourseVoteHelper
45+
private readonly UserRelCourseVoteRepository $courseVoteRepository,
4646
) {}
4747

4848
#[Route('/api/courses/{id}/rating', name: 'api_course_rating', methods: ['GET'])]
4949
public function courseRating(Course $course, Request $request): JsonResponse
5050
{
5151
$sessionId = $request->query->getInt('session', 0);
5252
$session = $sessionId > 0 ? $this->sessionRepository->find($sessionId) : null;
53-
$res = $this->userRelCourseVoteHelper->getCourseRating($course, $session);
53+
$rating = $this->courseVoteRepository->getCouseRating($course, $session);
5454

55-
return $this->json([
56-
'average' => $res['avg'],
57-
'count' => $res['count'],
58-
]);
55+
return $this->json($rating);
5956
}
6057
#[Route('/api/courses/{id}/visits', name: 'api_course_visits', methods: ['GET'])]
6158
public function courseVisits(Course $course, Request $request): JsonResponse

src/CoreBundle/Helpers/UserRelCourseVoteHelper.php

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/* For licensing terms, see /license.txt */
4+
5+
declare(strict_types=1);
6+
7+
namespace Chamilo\CoreBundle\Repository;
8+
9+
use Chamilo\CoreBundle\Entity\Course;
10+
use Chamilo\CoreBundle\Entity\Session;
11+
use Chamilo\CoreBundle\Entity\UserRelCourseVote;
12+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
13+
use Doctrine\DBAL\ParameterType;
14+
use Doctrine\ORM\NonUniqueResultException;
15+
use Doctrine\ORM\NoResultException;
16+
use Doctrine\Persistence\ManagerRegistry;
17+
18+
/**
19+
* @extends ServiceEntityRepository<UserRelCourseVote>
20+
*/
21+
class UserRelCourseVoteRepository extends ServiceEntityRepository
22+
{
23+
public function __construct(ManagerRegistry $registry)
24+
{
25+
parent::__construct($registry, UserRelCourseVote::class);
26+
}
27+
28+
/**
29+
* Retrieves the average vote and the count of votes for a specific course.
30+
*
31+
* @return array The first element of the array is the average vote (rounded to 2 decimal places),
32+
* and the second element is the count of votes.
33+
*/
34+
public function getCouseRating(Course $course, ?Session $session = null): array
35+
{
36+
$qb = $this->createQueryBuilder('v');
37+
38+
$qb
39+
->select([
40+
$qb->expr()->avg('v.vote'),
41+
$qb->expr()->count('v.id')
42+
])
43+
->where($qb->expr()->eq('v.course', ':course'))
44+
->setParameter('course', $course->getId(), ParameterType::INTEGER)
45+
;
46+
47+
if ($session !== null) {
48+
$qb
49+
->andWhere($qb->expr()->eq('v.session', ':session'))
50+
->setParameter('session', $session->getId(), ParameterType::INTEGER)
51+
;
52+
} else {
53+
$qb->andWhere($qb->expr()->isNull('v.session'));
54+
}
55+
56+
try {
57+
$result = $qb
58+
->setMaxResults(1)
59+
->getQuery()
60+
->getSingleResult();
61+
} catch (NoResultException|NonUniqueResultException) {
62+
$result = [1 => 0, 2 => 0];
63+
}
64+
65+
return [
66+
'average' => round((float) $result[1], 2),
67+
'count' => $result[2]
68+
];
69+
}
70+
}

0 commit comments

Comments
 (0)