1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chamilo\CoreBundle\Controller; |
6
|
|
|
|
7
|
|
|
use Chamilo\CoreBundle\Entity\GradebookCategory; |
8
|
|
|
use Chamilo\CoreBundle\Repository\GradeBookCategoryRepository; |
9
|
|
|
use Chamilo\CourseBundle\Entity\CDocument; |
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
15
|
|
|
use Symfony\Component\Routing\Attribute\Route; |
16
|
|
|
|
17
|
|
|
#[Route('/gradebook')] |
18
|
|
|
class GradebookController extends AbstractController |
19
|
|
|
{ |
20
|
|
|
public function __construct( |
21
|
|
|
private readonly GradeBookCategoryRepository $gradeBookCategoryRepository, |
22
|
|
|
) {} |
23
|
|
|
|
24
|
|
|
#[Route('/categories', name: 'chamilo_core_gradebook_categories', methods: ['GET'])] |
25
|
|
|
public function getCategories(Request $request): JsonResponse |
26
|
|
|
{ |
27
|
|
|
// Extract parameters from the query string |
28
|
|
|
$courseId = (int) $request->query->get('courseId'); |
29
|
|
|
$sessionId = $request->query->get('sessionId') ? (int) $request->query->get('sessionId') : null; |
30
|
|
|
|
31
|
|
|
if (!$courseId) { |
32
|
|
|
return new JsonResponse(['error' => 'courseId parameter is required'], Response::HTTP_BAD_REQUEST); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Ensure the default category exists |
36
|
|
|
$this->gradeBookCategoryRepository->createDefaultCategory($courseId, $sessionId); |
37
|
|
|
|
38
|
|
|
// Fetch categories using the repository |
39
|
|
|
$categories = $this->gradeBookCategoryRepository->getCategoriesForCourse($courseId, $sessionId); |
40
|
|
|
|
41
|
|
|
// Format the response |
42
|
|
|
$formatted = array_map(fn($category) => [ |
43
|
|
|
'id' => $category->getId(), |
44
|
|
|
'title' => $category->getTitle(), |
45
|
|
|
'parentId' => $category->getParent()?->getId(), |
46
|
|
|
], $categories); |
47
|
|
|
|
48
|
|
|
return new JsonResponse($formatted); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Sets the default certificate for a gradebook category |
52
|
|
|
#[Route('/set_default_certificate/{cid}/{certificateId}', name: 'chamilo_core_gradebook_set_default_certificate')] |
53
|
|
|
public function setDefaultCertificate(int $cid, int $certificateId, EntityManagerInterface $entityManager): Response |
54
|
|
|
{ |
55
|
|
|
// Find the gradebook category by course ID |
56
|
|
|
$gradebookCategory = $entityManager->getRepository(GradebookCategory::class)->findOneBy(['course' => $cid]); |
57
|
|
|
|
58
|
|
|
// Check if the category and certificate exist |
59
|
|
|
if (!$gradebookCategory) { |
60
|
|
|
return new Response('Gradebook category not found', Response::HTTP_NOT_FOUND); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$certificate = $entityManager->getRepository(CDocument::class)->find($certificateId); |
64
|
|
|
if (!$certificate) { |
65
|
|
|
return new Response('Certificate not found', Response::HTTP_NOT_FOUND); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Set the certificate as default for the gradebook category |
69
|
|
|
$gradebookCategory->setDocument($certificate); |
70
|
|
|
$entityManager->flush(); |
71
|
|
|
|
72
|
|
|
// Return success response |
73
|
|
|
return new JsonResponse([ |
74
|
|
|
'message' => 'Default certificate set successfully', |
75
|
|
|
'certificateId' => $certificate->getIid(), |
76
|
|
|
'gradebookCategoryId' => $gradebookCategory->getId(), |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Gets the default certificate for a gradebook category |
81
|
|
|
#[Route('/default_certificate/{cid}', name: 'chamilo_core_gradebook_default_certificate')] |
82
|
|
|
public function getDefaultCertificate(int $cid, EntityManagerInterface $entityManager): JsonResponse |
83
|
|
|
{ |
84
|
|
|
// Find the gradebook category by course ID |
85
|
|
|
$gradebookCategory = $entityManager->getRepository(GradebookCategory::class)->findOneBy(['course' => $cid]); |
86
|
|
|
|
87
|
|
|
// Check if the gradebook category exists for the course |
88
|
|
|
if (!$gradebookCategory) { |
89
|
|
|
return new JsonResponse(['message' => 'Gradebook category not found for the course', 'certificateId' => null], Response::HTTP_NOT_FOUND); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Get the default certificate if it exists |
93
|
|
|
$defaultCertificate = $gradebookCategory->getDocument(); |
94
|
|
|
|
95
|
|
|
if (!$defaultCertificate) { |
96
|
|
|
return new JsonResponse(['message' => 'No default certificate set', 'certificateId' => null], Response::HTTP_OK); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Return success response with the default certificate ID |
100
|
|
|
return new JsonResponse([ |
101
|
|
|
'message' => 'Default certificate found', |
102
|
|
|
'certificateId' => $defaultCertificate->getIid(), |
103
|
|
|
]); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|