<?php
namespace App\Controller;
use App\Service\EventService;
use App\Service\ExamRegistrationsService;
use App\Service\ExamRegistrationsTransactionsService;
use App\Service\ExamsService;
use App\Service\ExamVendorService;
use App\Service\PckPackagesService;
use App\Service\ProctorService;
use App\Service\PurchaseService;
use App\Service\SchoolInvoiceDetailsService;
use App\Service\SchoolPaymentsService;
use App\Service\TalService;
use App\Service\TestSessionsService;
use App\Util\ParameterUtil;
use Psr\Log\LoggerInterface;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\StreamedResponse;
class ExamRegistrationController extends BaseController
{
/**
* @var ExamRegistrationsService
*/
private $examRegistrationsService;
/**
* @var ExamsService
*/
private $examsService;
/**
* @var TalService
*/
private $talService;
/**
* @var ExamVendorService
*/
private $examVendorService;
/**
* @var EventService
*/
private $eventService;
/**
* @var PurchaseService
*/
private $purchaseService;
/**
* @var ProctorService
*/
private $proctorService;
/**
* @var TestSessionsService
*/
private $testSessionsService;
/**
* @var PckPackagesService
*/
private $pckPackagesService;
/**
* @var SchoolInvoiceDetailsService
*/
private SchoolInvoiceDetailsService $schoolInvoiceDetailsService;
private SchoolPaymentsService $schoolPaymentsService;
private ExamRegistrationsTransactionsService $examRegistrationsTransactionsService;
public function __construct(
ExamRegistrationsService $examRegistrationsService,
ExamsService $examsService,
TalService $talService,
ExamVendorService $examVendorService,
EventService $eventService,
PurchaseService $purchaseService,
ProctorService $proctorService,
TestSessionsService $testSessionsService,
PckPackagesService $pckPackagesService,
SchoolInvoiceDetailsService $schoolInvoiceDetailsService,
SchoolPaymentsService $schoolPaymentsService,
ExamRegistrationsTransactionsService $examRegistrationsTransactionsService
) {
$this->examRegistrationsService = $examRegistrationsService;
$this->examsService = $examsService;
$this->talService = $talService;
$this->examVendorService = $examVendorService;
$this->eventService = $eventService;
$this->purchaseService = $purchaseService;
$this->proctorService = $proctorService;
$this->testSessionsService = $testSessionsService;
$this->pckPackagesService = $pckPackagesService;
$this->schoolInvoiceDetailsService = $schoolInvoiceDetailsService;
$this->schoolPaymentsService = $schoolPaymentsService;
$this->examRegistrationsTransactionsService = $examRegistrationsTransactionsService;
}
/**
* @Route("/api/onlineExamRegistration")
*/
public function examRegistration(Request $request, LoggerInterface $logger)
{
try {
$content = $this->getContent($request);
$studentId = parent::getStudentId($request);
$response = $this->examRegistrationsService->examRegistration(ParameterUtil::getRequired($content->get('examId')), $studentId, null, !$this->isAdmin($logger));
} catch (Exception $ex) {
return $this->json($ex->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->json(($response));
}
/**
* @Route("/api/forcePackageUseOnlineExamRegistration")
*/
public function examRegistrationWithPackageId(Request $request, LoggerInterface $logger)
{
try {
$content = $this->getContent($request);
$studentId = ParameterUtil::getRequired($content->get('studentId'));
$packageId = ParameterUtil::getRequired($content->get('packageId'));
$response = $this->examRegistrationsService->examRegistration(ParameterUtil::getRequired($content->get('examId')), $studentId, null, !$this->isAdmin($logger),$packageId);
} catch (Exception $ex) {
return $this->json($ex->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->json(($response));
}
/**
* @Route("/api/cancelExamRegistration")
*/
public function cancelExamRegistration(Request $request, LoggerInterface $logger)
{
try {
$content = $this->getContent($request);
$adminCancellationReason = $content->get('adminCancelReason') ?: null;
$schoolId =$content->get('schoolId');
$isSchoolRegistration = $content->get('isSchoolRegistration') ? true : false;
$regId = ParameterUtil::getRequired($content->get('registrationId'));
$response = $this->examRegistrationsTransactionsService->cancelExamRegistration($regId,$isSchoolRegistration,$schoolId,$this->isAdmin($logger), $adminCancellationReason);
} catch (Exception $ex) {
return $this->json($ex->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
} catch (\Exception $e) {
return $this->json($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->json($response);
}
/**
* @Route("/api/changeRegistrationToOnline")
*/
public function changeRegistrationToOnline(Request $request)
{
try {
$content = $this->getContent($request);
$adminCancellationReason = $content->get('adminCancelReason') ?: null;
$regId = ParameterUtil::getRequired($content->get('registrationId'));
$registration = $this->examRegistrationsService->findById($regId);
if (!$registration) {
throw new Exception("Registration not found $regId.");
}
$cancelSessionResponse = $this->examRegistrationsService->cancelSession($regId, true, $adminCancellationReason);
$exam = $cancelSessionResponse['exam'];
if (!$exam) {
throw new Exception("Exam not found " . $cancelSessionResponse['exam']['id']);
}
if(!$exam['program_content_code']){
$response = $this->examVendorService->getStartLink(
$registration->getStudentid(),
$registration->getExamid(),
$regId
);
// $proctorUuid = $this->proctorService->addRPNowExamRegistration(
// $registration->getStudentid(),
// $registration->getExamid(),
// $regId
// );
$exam['onlineAccess'] = $response['onlineAccess'];
$exam['proctorUuid'] = null;
}
} catch (Exception $ex) {
return $this->json($ex->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->json(array('exam' => $exam));
}
/**
* @Route("/api/getExamRegistrations")
*/
public function getExamRegistrations(Request $request)
{
try {
$content = $this->getContent($request);
$response = $this->examRegistrationsService->getExamRegistrations(
ParameterUtil::getRequired($content->get('dateType'), 'dateType'),
ParameterUtil::getRequired($content->get('startDate'), 'startDate'),
ParameterUtil::getRequired($content->get('endDate'), 'endDate'),
$content->get('schoolId')
// $content->get('sessionTypeId'),
// $content->get('sessionTypeValueId'),
// $content->get('vendorId'),
// $content->get('examId'),
// $content->get('packageTypesId'),
// $content->get('schoolCohort'),
// $content->get('schoolId')
);
} catch (Exception $ex) {
return $this->json($ex->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->json($response);
}
/**
* @Route("/api/admin/makeVendorApprovedExamRetakeRegistration")
*/
public function makeVendorApprovedExamRetakeRegistration(Request $request, LoggerInterface $logger)
{
$content = $this->getContent($request);
$studentId= ParameterUtil::getRequired($content->get('student_id'));
$examId = ParameterUtil::getRequired($content->get('examId'));
$vendorApprovalReasonId = ParameterUtil::getRequired($content->get('vendorApprovalReasonId'));
try{
$response = $this->examRegistrationsTransactionsService->makeVendorApprovedExamRetakeRegistration($studentId,$examId,$vendorApprovalReasonId);
}
catch (\Exception $ex) {
return $this->json($ex->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->json($response);
}
/**
* @Route("/api/admin/changeExamToCourse")
*/
public function changeExamToCourse(Request $request, LoggerInterface $logger)
{
$content = $this->getContent($request);
$studentId= ParameterUtil::getRequired($content->get('student_id'));
$examId = ParameterUtil::getRequired($content->get('examId'));
try{
$response = $this->examRegistrationsTransactionsService->changeExamToCourse($studentId,$examId);
}
catch (\Exception $ex) {
return $this->json($ex->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->json($response);
}
/**
* @Route("/api/cancelSession")
*/
public function cancelSession(Request $request, LoggerInterface $logger)
{
try {
$content = $this->getContent($request);
$response = $this->examRegistrationsService->cancelSession(ParameterUtil::getRequired($content->get('registrationId')), $this->isAdmin($logger), $content->get('adminCancelReason'));
} catch (Exception $ex) {
return $this->json($ex->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
//the response is the sessionId that we're cancelling, we need to return it to update duration
return $this->json($response);
}
/**
* @Route("/api/cancelExamRegistrationWithFee")
*/
public function cancelExamRegistrationWithFee(Request $request)
{
try {
$content = $this->getContent($request);
$response = $this->examRegistrationsService->cancelExamRegistrationWithFee(
ParameterUtil::getRequired($content->get('registrationId')),
ParameterUtil::getRequired($content->get('ccLast4')),
ParameterUtil::getRequired($content->get('ccExo')),
ParameterUtil::getRequired($content->get('fee')),
ParameterUtil::getRequired($content->get('authResponse')),
ParameterUtil::getRequired($content->get('authTransId'))
);
} catch (Exception $ex) {
return $this->json(array('error' => $ex->getMessage()));
}
return $this->json(array('success' => $response));
}
/**
* @Route("/api/completeExamRegistration")
*/
public function completeExamRegistration(Request $request)
{
try {
$content = $this->getContent($request);
$response = $this->examRegistrationsService->completeExamRegistration(ParameterUtil::getRequired($content->get('registrationId')), 'from api');
} catch (Exception $ex) {
return $this->json(array('error' => $ex->getMessage()));
}
return $this->json(array('success' => $response));
}
/**
* @Route("/api/updateGradesForCoopersmithExams")
*/
public function updateGradesForCoopersmithExams()
{
try {
$response = $this->examRegistrationsService->updateGradesForCoopersmithExams();
} catch (Exception $ex) {
return $this->json(array('error' => $ex->getMessage()));
}
return $this->json(array('success' => $response));
}
/**
* @Route("/api/startExamProgram")
*/
public function startExamProgram(Request $request, LoggerInterface $logger)
{
try {
$content = $this->getContent($request);
// $studentId = ParameterUtil::getRequired($content->get('studentId'));
$studentId = parent::getStudentId($request);
$examId = ParameterUtil::getRequired($content->get('examId'));
$registrationId = ParameterUtil::getRequired($content->get('registrationId'));
$logger->info("in start program");
$response = $this->examVendorService->getStartLink(
$studentId,
$examId,
$registrationId,
true
);
} catch (Exception $e) {
$logger->error('start exam error' . $e->getMessage());
return $this->json(array('error' => $e->getMessage()), 500);
}
return $this->json(array('url' => $response['startExamLink']));
}
/**
* @Route("/api/cancelUnusedRegistrations")
*/
public function cancelUnusedExamRegistrations(Request $request) {
try {
$content = $this->getContent($request);
$studentId = ParameterUtil::getRequired($content->get("studentId"),'studentId');
$response = $this->examRegistrationsService->cancelUnusedExamRegistrations($studentId);
} catch (Exception $ex) {
return $this->json(array('error' => $ex->getMessage()));
}
return $this->json($response);
}
/**
* @Route("/api/scheduleSessionForRegistration")
*/
public function scheduleSessionForRegistration(Request $request, LoggerInterface $logger)
{
try {
$content = $this->getContent($request);
$studentId = parent::getStudentId($request);
$response = $this->examRegistrationsService->scheduleSessionForRegistration(ParameterUtil::getRequired($content->get('registrationId')), ParameterUtil::getRequired($content->get('sessionId')), $studentId,false,$this->isAdmin($logger));
$exam = $this->examsService->find($response['examId']);
if ($this->examsService->isTalExam($exam)) {
try {
$this->talService->addStudent($studentId);
} catch (Exception $e) {
$logger->error('Error adding student to TAL ' . $e->getMessage());
}
}
} catch (Exception $ex) {
return $this->json(array('error' => $ex->getMessage()));
}
return $this->json($response);
}
/**
* @Route("/api/setProctorReserved")
*/
public function setProctorReserved(Request $request)
{
try {
$content = $this->getContent($request);
$this->examRegistrationsService->setProctorReserved(ParameterUtil::getRequired($content->get('registrationId')));
} catch (Exception $ex) {
return $this->json(array('error' => $ex->getMessage()));
}
return $this->json(array('success'));
}
/**
* @Route("/api/cron/registerStudentsForExams", name="app_cron_register_students_for_exams")
*/
public function registerStudentsForExams(Request $request)
{
$auth = $request->headers->get('token-auth');
if ($auth == $_ENV['TTI_REQUEST_AUTH']) {
try {
$response = $this->examRegistrationsService->registerStudentsForExams();
} catch (Exception $ex) {
return new Response($ex->getMessage(), 500);
}
} else {
return new Response('false authentication', 401);
}
return new Response($response);
}
/**
* @Route("/api/cron/checkGradeForIncompleteExams")
*/
public function checkGradeForIncompleteExam(Request $request)
{
$auth = $request->headers->get('token-auth');
if ($auth == $_ENV['TTI_REQUEST_AUTH']) {
try {
$response = $this->examRegistrationsService->checkGradeForIncompleteExam();
} catch (Exception $ex) {
return new Response($ex->getMessage(), 500);
}
} else {
return new Response('false authentication', 401);
}
return new Response($response);
}
/**
* @Route("/api/cron/getGrades")
*/
public function getGrades(Request $request)
{
$auth = $request->headers->get('token-auth');
if ($auth == $_ENV['TTI_REQUEST_AUTH']) {
try {
$response = $this->examRegistrationsService->getGradesForTalCisExams();
} catch (Exception $ex) {
return new Response($ex->getMessage(), 500);
}
} else {
return new Response('false authentication', 401);
}
return new Response($response);
}
/**
* @Route("/api/reGradeRegistration")
*/
public function reGradeRegistration(Request $request){
$content = $this->getContent($request);
$secularNumber = ParameterUtil::getRequired($content->get('courseNumber'));
$tal_guid = ParameterUtil::getRequired($content->get('tal_guid'));
try {
$response = $this->examRegistrationsService->reGradeRegistration($secularNumber,$tal_guid);
} catch (Exception $ex) {
return new Response($ex->getMessage(), 500);
}
return new Response($response);
}
/**
* @Route("/api/cron/getCoopersmithGrades")
*/
public function getCoopersmithGrades(Request $request)
{
$auth = $request->headers->get('token-auth');
if ($auth == $_ENV['TTI_REQUEST_AUTH']) {
try {
$this->examRegistrationsService->getGradesForCoopersmithExams(false);
} catch (Exception $ex) {
return new Response($ex->getMessage(), 500);
}
} else {
return new Response('false authentication', 401);
}
return new Response('Success.');
}
/**
* @Route("/api/cron/getCoopersmithGradesAll")
*/
public function getCoopersmithGradesAll(Request $request)
{
$auth = $request->headers->get('token-auth');
if ($auth == $_ENV['TTI_REQUEST_AUTH']) {
$content = $this->getContent($request);
$startDate = $content->get('startDate');
$endDate = $content->get('endDate');
try {
$this->examRegistrationsService->
getGradesForCoopersmithExams(true, $startDate, $endDate);
} catch (Exception $ex) {
return new Response($ex->getMessage(), 500);
}
} else {
return new Response('false authentication', 401);
}
return new Response('Success');
}
/**
* @Route("/api/cron/updateCoopersmithGrades")
*/
public function updateCoopersmithGrades(Request $request)
{
$auth = $request->headers->get('token-auth');
if ($auth == $_ENV['TTI_REQUEST_AUTH']) {
try {
$response = $this->examRegistrationsService->updateGradesForCoopersmithExams();
} catch (Exception $ex) {
return new Response($ex->getMessage(), 500);
}
} else {
return new Response('false authentication', 401);
}
return new Response($response);
}
/**
* @Route("/api/cron/dailyReportOnCoopersmithExamFromWoodmontPackage")
*/
public function dailyReportOnCoopersmithExamFromWoodmontPackage(Request $request)
{
$auth = $request->headers->get('token-auth');
if ($auth == $_ENV['TTI_REQUEST_AUTH']) {
try {
$response = $this->examRegistrationsService->reportOnCoopersmithExamFromWoodmontPackage();
} catch (Exception $ex) {
return new Response($ex->getMessage(), 500);
}
} else {
return new Response('false authentication', 401);
}
return new Response($response);
}
/**
* @Route("/api/getGradesByDates")
*/
public function getGradesByDates(Request $request)
{
$auth = $request->headers->get('token-auth');
$content = $this->getContent($request);
$startDate = $content->get('startDate');
$endDate = $content->get('endDate');
if ($auth == $_ENV['TTI_REQUEST_AUTH']) {
try {
$response = $this->examRegistrationsService->getGradesForTalCisExamsByDates($startDate, $endDate);
} catch (Exception $ex) {
return new Response($ex->getMessage(), 500);
}
} else {
return new Response('false authentication', 401);
}
return new Response($response);
}
/**
* @Route("/api/getGradesByDatesForClosedBatchedRegistrations")
*/
public function getGradesByDatesForClosedBatchedRegistrations(Request $request)
{
$auth = $request->headers->get('token-auth');
$content = $this->getContent($request);
$startDate = $content->get('startDate');
$endDate = $content->get('endDate');
if ($auth == $_ENV['TTI_REQUEST_AUTH']) {
try {
$response = $this->examRegistrationsService->getGradesByDatesForClosedBatchedRegistrations($startDate, $endDate);
} catch (Exception $ex) {
return new Response($ex->getMessage(), 500);
}
} else {
return new Response('false authentication', 401);
}
return new Response($response);
}
//we updated registration with grades but without a specific exam taken date,
// here we go over those registrations and set the online exam started date
// to be TAL's examTakenDate - this way we'll have a date to show in the front end
/**
* @Route("/api/getExamTakenDateForAlreadyGradedRegistrations")
*/
public function getExamTakenDateForAlreadyGradedRegistrations(Request $request)
{
$auth = $request->headers->get('token-auth');
$content = $this->getContent($request);
if ($auth == $_ENV['TTI_REQUEST_AUTH']) {
try {
$response = $this->examRegistrationsService->getExamTakenDateForAlreadyGradedRegistrations();
} catch (Exception $ex) {
return new Response($ex->getMessage(), 500);
}
} else {
return new Response('false authentication', 401);
}
return new Response($response);
}
/**
* @Route("/api/getGradesForOldRegistrationsMarkedAs01")
*/
public function getGradesForOldRegistrationsMarkedAs01(Request $request)
{
$auth = $request->headers->get('token-auth');
$content = $this->getContent($request);
$limit = $content->get('limit');
if ($auth == $_ENV['TTI_REQUEST_AUTH']) {
try {
$response = $this->examRegistrationsService->getGradesForOldRegistrationsMarkedAs01($limit);
} catch (Exception $ex) {
return new Response($ex->getMessage(), 500);
}
} else {
return new Response('false authentication', 401);
}
return new Response($response);
}
/**
* @Route("/api/cleanupOldIncompleteRegistrations")
*/
public function cleanupOldIncompleteRegistrations(Request $request)
{
$auth = $request->headers->get('token-auth');
$content = $this->getContent($request);
if ($auth == $_ENV['TTI_REQUEST_AUTH']) {
try {
$registrationIds = $content->get('registrationIds');
$response = $this->examRegistrationsService->cleanupOldIncompleteRegistrations($registrationIds);
} catch (Exception $ex) {
return new Response($ex->getMessage(), 500);
}
} else {
return new Response('false authentication', 401);
}
return new Response($response);
}
/**
* @Route("/api/event/checkOnlineAccess")
*/
public function eventCheckOnlineAccess(Request $request, LoggerInterface $logger)
{
$regId = $request->query->get('regId');
$logger->info('got into checkOnlineAccess');
if (!$regId) {
throw new Exception("Missing required parameter.");
}
$this->eventService->checkOnlineAccess($regId);
}
/**
* @Route("/api/event/checkProgramCompleted")
*/
public function eventCheckProgramCompleted(Request $request, LoggerInterface $logger)
{
$regId = $request->query->get('regId');
$logger->info("into checkProgramCompletedEvent");
if (!$regId) {
throw new Exception("Missing required parameter.");
}
$this->eventService->checkOnlineProgramCompleted($regId);
}
/**
* @Route("/api/checkProgramCompleted")
*/
public function checkProgramCompleted(Request $request, LoggerInterface $logger)
{
$content = $this->getContent($request);
$regId = $content->get('regId');
$logger->info("into checkProgramCompletedEvent");
if (!$regId) {
throw new Exception("Missing required parameter.");
}
$registration = $this->examRegistrationsService->findById($regId);
$response = $registration->getOnlineProgramCompleted() ? $registration->getOnlineProgramCompleted()->format('Y-m-d') : '';
return new Response($response);
}
/**
* @Route("/api/checkOnlineAccess")
*/
public function checkOnlineAccess(Request $request, LoggerInterface $logger)
{
$content = $this->getContent($request);
$regId = $content->get('regId');
$logger->info("into checkProgramCompletedEvent");
if (!$regId) {
throw new Exception("Missing required parameter.");
}
$registration = $this->examRegistrationsService->findById($regId);
$response = $registration->getOnlineaccess() ? $registration->getOnlineaccess()->format('Y-m-d') : '';
return new Response($response);
}
/**
* @Route("/api/getExamRegistrationsForStudent")
*/
public function getExamRegistrationsForStudent(Request $request) {
$content = $this->getContent($request);
$studentId = $content->get('studentId');
if (!$studentId) {
throw new Exception("Missing studentId.");
}
$vendorId = $content->get('vendorId');
if (!$vendorId) {
throw new Exception("Missing vendorId.");
}
$registrations = $this->examRegistrationsService->getRegistrations($studentId, $vendorId);
return new Response($registrations);
}
}