<?php
namespace App\Controller;
use App\Service\ExamRegistrationsService;
use App\Service\ExamsService;
use App\Service\ExamVendorService;
use App\Service\ExamWebhookService;
use App\Service\ProctorService;
use App\Service\StudentsService;
use App\Service\TalService;
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;
class ExamController extends BaseController
{
/**
* @var ExamsService
*/
private $examsService;
/**
* @var ProctorService
*/
private $proctorService;
/**
* @var ExamVendorService
*/
private $examVendorService;
/**
* @var ExamWebhookService
*/
private $examWebhookService;
/**
* @var ExamRegistrationsService
*/
private $examRegistrationsService;
/**
* @var TalService
*/
private $talService;
private $logger;
public function __construct(
ExamsService $examsService,
ProctorService $proctorService,
ExamVendorService $examVendorService,
ExamWebhookService $examWebhookService,
ExamRegistrationsService $examRegistrationsService,
TalService $talService,
LoggerInterface $logger
) {
$this->examsService = $examsService;
$this->proctorService = $proctorService;
$this->examVendorService = $examVendorService;
$this->examWebhookService = $examWebhookService;
$this->examRegistrationsService = $examRegistrationsService;
$this->talService = $talService;
$this->logger = $logger;
}
// /**
// * @Route("/api/searchExams")
// */
// public function searchExams(Request $request){
// $content = $this->getContent($request);
// $searchTerm = $content->get('searchTerm');
// $studentId = parent::getStudentId($request);
// try{
// $response = $this->examsService->searchExams($searchTerm,$studentId);
// }
// catch (Exception $exception){
// throw new Exception($exception->getMessage());
// }
// return new Response(json_encode($response));
// }
/**
* @Route("/api/getAllActiveExams")
*/
public function getAllActiveExams(Request $request)
{
$studentId = parent::getStudentId($request);
$response = $this->examsService->getAllActiveExams($studentId);
return new Response(json_encode($response));
}
/**
* @Route("/api/getAllActiveExamsByVendorId")
**/
public function getAllActiveExamsByVendorId(Request $request) {
try {
$vendorId = $request->get('vendorId');
$exams = $this->examsService->getAllActiveExamsByVendorId($vendorId);
} catch (Exception $ex) {
return $this->json($ex->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->json($exams);
}
/**
* @Route("/api/getAllActiveExamsWithRegistrationDetails")
*/
public function getAllActiveExamsWithRegistrationDetails(Request $request)
{
$studentId = parent::getStudentId($request);
$response = $this->examsService->getAllActiveExamsWithRegistrationDetails($studentId);
return new Response(json_encode($response));
}
/**
* @Route("/api/getAllActiveExamsWithStudyMaterial")
*/
public function getAllActiveExamsWithStudyMaterial(Request $request)
{
$studentId = parent::getStudentId($request);
$response = $this->examsService->getAllActiveExamsWithStudyMaterial($studentId);
return new Response(json_encode($response));
}
/**
* @Route("/api/getAllExamsWithStudyMaterial")
*/
public function getAllExamsWithStudyMaterial(Request $request)
{
$response = $this->examsService->getAllExamsWithStudyMaterial();
return $this->json($response);
}
/**
* @Route("/api/getAllSeminaryTestingExams")
*/
public function getAllSeminaryTestingExams()
{
$response = $this->examsService->getAllSeminaryTestingExams();
return $this->json(array('exams' => $response));
}
/**
* @Route("/api/getAllExamsAccordingStudent")
*/
public function getAllExamsAccordingStudent(Request $request, StudentsService $studentsService)
{
$studentId = parent::getStudentId($request);
$student = $studentsService->find($studentId);
if (!$student) {
return new Response(json_encode('Student not found.'),500);
}
$examsWithProctorCodes = $this->examsService->getAllExamsAccordingStudent($studentId, $student->getCccId());
$memberHasntRegistered2years = $studentsService->memberDidntRegisterOver2Years($studentId);
return new Response(json_encode(['exams' => $examsWithProctorCodes,'memberHasntRegistered2years' => $memberHasntRegistered2years]));
}
/**
* @Route("/api/startExam")
*/
public function startExam(Request $request)
{
try {
$content = $this->getContent($request);
$studentId = $content->get('studentId');
$examId = ParameterUtil::getRequired($content->get('examId'));
$registrationId = ParameterUtil::getRequired($content->get('registrationId'));
if(!$studentId)
$studentId = $content->get('student_id');
$this->logger->info("in start exam");
$response = $this->examVendorService->getStartLink(
$studentId,
$examId,
$registrationId
);
$this->examRegistrationsService->setProctorUuid($registrationId);
$this->examRegistrationsService->setOnlineExamRedirect($studentId, $examId);
} catch (Exception $e) {
$this->logger->error('start exam error' . $e->getMessage());
return $this->json(array('error' => $e->getMessage()), 500);
}
return $this->json(array('url' => $response['startExamLink']));
}
/**
* @Route("/api/webhook/examStarted")
*/
public function examStarted(Request $request)
{
try {
$response = $this->examWebhookService->examStarted($request);
} catch (Exception $e) {
return $this->json(array('error' => $e->getMessage()));
}
return $this->json($response);
}
/**
* @Route("/api/webhook/examCompleted")
*/
public function examCompleted(Request $request)
{
try {
$response = $this->examWebhookService->examCompleted($request);
} catch (Exception $e) {
return $this->json(array('error' => $e->getMessage()));
}
return $this->json($response);
}
/**
* @Route("/api/admin/addUpdateExam")
*/
public function addUpdateExam(Request $request)
{
try {
$content = $this->getContent($request);
$examId = $content->get('examId');
$vendorId = $content->get('vendorId');
$active = $content->get('active');
$phoneSystemActive = $content->get('phoneSystemActive');
$examLevel= $content->get('examLevel');
$code = $content->get('code');
$subject = $content->get('subject')?:$content->get('secularTitle');
$secularNumber= $content->get('secularNumber');
$secularTitle = $content->get('secularTitle');
$duration = $content->get('duration');
$credits= (int)$content->get('credits');
$additionalCharge = $content->get('additionalCharge');
$tti_allow = $content->get('tti_allow');
$content_code = $content->get('content_code');
$group_code = $content->get('group_code');
$program_content_code = $content->get('program_content_code');
$program_started_content_code = $content->get('program_started_content_code');
$program_completed_content_code= $content->get('program_completed_content_code');
$alternate_course_version = $content->get('alternate_course_version');
$online_only = $content->get('online_only');
$paper_only = $content->get('paper_only');
$allowed_resources = $content->get('allowed_resources');
$assignments = $content->get('assignments');
$for_seminary_testing = $content->get('for_seminary_testing');
$talFee = $content->get('talFee');
$updatedExam= $this->examsService->addUpdateExam($examId,$vendorId,$active,$phoneSystemActive,$examLevel,$code,$subject,$secularNumber,$secularTitle,
$duration,$credits,$additionalCharge,$tti_allow,
$content_code,$group_code,$program_content_code,$program_started_content_code,
$program_completed_content_code, $allowed_resources,$assignments,$for_seminary_testing,
$alternate_course_version,$online_only,$paper_only,$talFee
);
//$updatedExam['examDuration'] = $updatedExam->getDuration();
$allExams = $this->examsService->getAllExamsWithStudyMaterial();
} catch (Exception $e) {
return $this->json(array('error' => $e->getMessage()),500);
}
return $this->json(['allExams'=>$allExams]);
}
}