<?php
namespace App\Controller;
use App\Service\SchoolInvoiceDetailsService;
use App\Service\SchoolPaymentsService;
use App\Service\SchoolService;
use App\Service\StudentsService;
use App\Service\TestSessionsService;
use App\Service\UsersService;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use App\Util\ParameterUtil;
use App\Service\QueryLogService;
use Symfony\Component\HttpFoundation\Response;
class SchoolController extends BaseController
{
/**
* @var SchoolService
*/
private $schoolService;
/**
* @var TestSessionsService
*/
private $testSessionsService;
/**
* @var StudentsService
*/
private $studentsService;
/**
* @var SchoolPaymentsService
*/
private SchoolPaymentsService $schoolPaymentsService;
private SchoolInvoiceDetailsService $schoolInvoiceDetailsService;
private UsersService $usersService;
private QueryLogService $queryLogService;
public function __construct(
SchoolService $schoolService,
TestSessionsService $testSessionsService,
StudentsService $studentsService,
SchoolPaymentsService $schoolPaymentsService,
SchoolInvoiceDetailsService $schoolInvoiceDetailsService,
UsersService $usersService,
QueryLogService $queryLogService
) {
$this->schoolService = $schoolService;
$this->testSessionsService = $testSessionsService;
$this->studentsService = $studentsService;
$this->schoolPaymentsService = $schoolPaymentsService;
$this->schoolInvoiceDetailsService = $schoolInvoiceDetailsService;
$this->usersService = $usersService;
$this->queryLogService = $queryLogService;
}
/**
* @Route("/api/getActiveSchools")
*/
public function getActiveSchools()
{
return $this->json($this->schoolService->getActiveSchools());
}
/**
* @Route("/api/getSchoolCohorts")
*/
public function getSchoolCohorts(Request $request)
{
$schoolId = $request->query->get('schoolId');
$schoolCohorts = $this->schoolService->getSchoolCohorts($schoolId);
return $this->json($schoolCohorts ? $schoolCohorts[0] : null);
}
/**
* @Route("/api/getSchools")
*/
public function getSchools()
{
$isSchoolAdmin = $this->isSchoolAdmin();
if($isSchoolAdmin){
$schoolId = $this->getUser()->getSchoolId();
$schools = $this->schoolService->getSchoolForSchoolSession($schoolId);
}
else{
$schools = $this->schoolService->getSchools();
}
foreach ($schools as $school) {
$schoolId = $school['id'];
$response[$schoolId]['schoolInfo'] = $school;
// $response[$schoolId]['students'] = $this->studentsService->getStudentsAccordingSchool($schoolId);
$response[$schoolId]['schoolSessions'] = $this->testSessionsService->getSchoolSessions($schoolId);
$response[$schoolId]['studentSchoolSession'] = $this->testSessionsService->getStudentsAndExamsBySessionPerSchool($schoolId);
$response[$schoolId]['schoolPaymentBalance'] = $this->schoolPaymentsService->getCurrentBalance($schoolId);
$response[$schoolId]['schoolPayments'] = $this->schoolPaymentsService->findAllBySchool($schoolId);
$response[$schoolId]['accountReport'] = $this->schoolInvoiceDetailsService->getAccountReport($schoolId);
$response[$schoolId]['schoolAdminUserEmail'] = $this->usersService->getSchoolAdminEmail($schoolId)?:$this->getUser()->getEmail();
$response[$schoolId]['schoolInfo']['logs'] = $this->queryLogService->findAllSchoolsLogs($schoolId);
}
return $this->json($response);
}
/**
* @Route("/api/getSchoolInfo")
*/
public function getSchoolInfo(Security $security)
{
$isSchoolAdmin = $this->isSchoolAdmin();
if($isSchoolAdmin){
$schoolId = $this->getUser()->getSchoolId();
$schools = $this->schoolService->getSchoolForSchoolSession($schoolId);
$response['schoolDetails'] = $schools[0];
$response['accountReport'] = $this->schoolInvoiceDetailsService->getAccountReport($schoolId);
$response['schoolBalance'] = $this->schoolPaymentsService->getCurrentBalance($schoolId);
$response['schoolAdminUserEmail'] = $security->getUser()->getEmail();
$response['schoolInfo']['logs'] = $this->queryLogService->findAllSchoolsLogs($schoolId);
// $schools = $this->schoolService->getSchoolForSchoolSession($isSchoolAdmin);
return $this->json($response);
}
else{
$schools = $this->schoolService->getSchools();
}
return $this->json($schools);
}
/**
* @Route("/api/getSchoolDetails")
*/
public function getSchoolDetails(Request $request)
{
$schoolId = $request->query->get('schoolId');
$response['schoolStudents'] = $this->studentsService->getStudentsAccordingSchool($schoolId);
$response['schoolSessions'] = $this->testSessionsService->getSchoolSessions($schoolId);
$response['schoolSessionStudents'] = $this->testSessionsService->getStudentsAndExamsBySessionPerSchool($schoolId);
$response['currentSchoolDetails'] =$this->schoolService->getSchoolForSchoolSession($schoolId);
$response['schoolPaymentBalance'] = $this->schoolPaymentsService->getCurrentBalance($schoolId);
$response['schoolInfo']['logs'] = $this->queryLogService->findAllSchoolsLogs($schoolId);
return $this->json($response);
}
/**
* @Route("/api/getAllSchoolStudents")
*/
public function getAllSchoolStudents(Request $request)
{
try {
$schoolId = $request->query->get('schoolId');
$response = $this->studentsService->getAllSchoolStudents($schoolId);
} catch (Exception $ex) {
return $this->json(array('error' => $ex->getMessage()));
}
return $this->json($response);
}
/**
* @Route("/api/admin/addEditSchool")
*/
public function addEditSchool(Request $request)
{
//$active, $address, $chargeProctorFee, $city, $comments, $contact, $country,$email, $gender, $member_pricePerCredit, $multiple_active_cohorts, $name, $nonmember_pricePerCredit, $phone, $standardOrCustomPricePerCredit, $state, $students_are_members, $zip
$content = $this->getContent($request);
$school = $this->schoolService->AddEditSchool(
$content->get('active'),
$content->get('address'),
$content->get('chargeProctorFee'),
$content->get('city'),
$content->get('comments'),
$content->get('contact'),
$content->get('country'),
ParameterUtil::getRequired($content->get('email')),
ParameterUtil::getRequired($content->get('gender')),
$content->get('member_pricePerCredit'),
$content->get('multiple_active_cohorts'),
ParameterUtil::getRequired($content->get('name')),
$content->get('nonmember_pricePerCredit'),
$content->get('phone'),
$content->get('standardOrCustomPricePerCredit'),
$content->get('state'),
$content->get('students_are_members'),
$content->get('zip'),
$content->get('schoolId')
);
$schoolId = $school[0]['id'];
$response['schoolInfo'] = $school[0];
$response['schoolSessions'] = $this->testSessionsService->getSchoolSessions($schoolId);
$response['studentSchoolSession'] = $this->testSessionsService->getStudentsAndExamsBySessionPerSchool($schoolId);
$response['schoolPaymentBalance'] = $this->schoolPaymentsService->getCurrentBalance($schoolId);
$response['schoolPayments'] = $this->schoolPaymentsService->findAllBySchool($schoolId);
$response['accountReport'] = $this->schoolInvoiceDetailsService->getAccountReport($schoolId);
$response['schoolAdminUserEmail'] = $this->usersService->getSchoolAdminEmail($schoolId)?:$this->getUser()->getEmail();
$response['schoolInfo']['logs'] = $this->queryLogService->findAllSchoolsLogs($schoolId);
return $this->json($response);
}
/**
* @Route("/api/admin/deleteSchool")
*/
public function deleteSchool(Request $request)
{
$content = $this->getContent($request);
try{
$response = $this->schoolService->deleteSchool( ParameterUtil::getRequired($content->get('schoolId')));
}
catch (\Exception $e){
return new Response($e->getMessage(), 500);
}
return $this->json($response);
}
}