Gradebook: Fixes and enhancements for certificate handling in documents and gradebook - #5074
parent
3985e3fc62
commit
c457ad2f77
@ -0,0 +1,72 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace Chamilo\CoreBundle\Controller; |
||||
|
||||
use Chamilo\CoreBundle\Entity\GradebookCategory; |
||||
use Chamilo\CourseBundle\Entity\CDocument; |
||||
use Doctrine\ORM\EntityManagerInterface; |
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
||||
use Symfony\Component\HttpFoundation\JsonResponse; |
||||
use Symfony\Component\HttpFoundation\Response; |
||||
use Symfony\Component\Routing\Annotation\Route; |
||||
|
||||
#[Route('/gradebook')] |
||||
class GradebookController extends AbstractController |
||||
{ |
||||
// Sets the default certificate for a gradebook category |
||||
#[Route('/set_default_certificate/{cid}/{certificateId}', name: 'chamilo_core_gradebook_set_default_certificate')] |
||||
public function setDefaultCertificate(int $cid, int $certificateId, EntityManagerInterface $entityManager): Response |
||||
{ |
||||
// Find the gradebook category by course ID |
||||
$gradebookCategory = $entityManager->getRepository(GradebookCategory::class)->findOneBy(['course' => $cid]); |
||||
|
||||
// Check if the category and certificate exist |
||||
if (!$gradebookCategory) { |
||||
return new Response('Gradebook category not found', Response::HTTP_NOT_FOUND); |
||||
} |
||||
|
||||
$certificate = $entityManager->getRepository(CDocument::class)->find($certificateId); |
||||
if (!$certificate) { |
||||
return new Response('Certificate not found', Response::HTTP_NOT_FOUND); |
||||
} |
||||
|
||||
// Set the certificate as default for the gradebook category |
||||
$gradebookCategory->setDocument($certificate); |
||||
$entityManager->flush(); |
||||
|
||||
// Return success response |
||||
return new JsonResponse([ |
||||
'message' => 'Default certificate set successfully', |
||||
'certificateId' => $certificate->getIid(), |
||||
'gradebookCategoryId' => $gradebookCategory->getId() |
||||
]); |
||||
} |
||||
|
||||
// Gets the default certificate for a gradebook category |
||||
#[Route('/default_certificate/{cid}', name: 'chamilo_core_gradebook_default_certificate')] |
||||
public function getDefaultCertificate(int $cid, EntityManagerInterface $entityManager): JsonResponse |
||||
{ |
||||
// Find the gradebook category by course ID |
||||
$gradebookCategory = $entityManager->getRepository(GradebookCategory::class)->findOneBy(['course' => $cid]); |
||||
|
||||
// Check if the gradebook category exists for the course |
||||
if (!$gradebookCategory) { |
||||
return new JsonResponse(['message' => 'Gradebook category not found for the course', 'certificateId' => null], Response::HTTP_NOT_FOUND); |
||||
} |
||||
|
||||
// Get the default certificate if it exists |
||||
$defaultCertificate = $gradebookCategory->getDocument(); |
||||
|
||||
if (!$defaultCertificate) { |
||||
return new JsonResponse(['message' => 'No default certificate set', 'certificateId' => null], Response::HTTP_OK); |
||||
} |
||||
|
||||
// Return success response with the default certificate ID |
||||
return new JsonResponse([ |
||||
'message' => 'Default certificate found', |
||||
'certificateId' => $defaultCertificate->getIid() |
||||
]); |
||||
} |
||||
} |
@ -0,0 +1,22 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Traits; |
||||
|
||||
use DocumentManager; |
||||
|
||||
trait GradebookControllerTrait |
||||
{ |
||||
private function getAllInfoToCertificate(int $userId, int $courseId, int $sessionId, bool $isPreview = false): array |
||||
{ |
||||
return DocumentManager::get_all_info_to_certificate( |
||||
$userId, |
||||
$courseId, |
||||
$sessionId, |
||||
$isPreview |
||||
); |
||||
} |
||||
} |
Loading…
Reference in new issue