<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
class DefaultController extends Controller
{
/**
* Match all /app* URL and serve them to ReactJS frontend app.
*
* @Route("/app/")
* @Route("/app/{routing}", name="app_frontend", requirements={"routing"=".+"})
* @param int $routing
* @return \Symfony\Component\HttpFoundation\Response
*/
public function appAction($routing = 1)
{
return $this->render('default/index.html.twig');
}
/**
* Default homepage (index)
* @Route("/", name="index")
* @param int $id
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function indexAction($id = 1)
{
// yay! Use this to see if the user is logged in
if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
return $this->redirectToRoute('app_frontend');
} else {
return $this->redirect('/app/login');
}
}
/**
* Get statistics data for selected year.
* This data are displayed in BClam Dashboard.
* #### Output ####
* ```
* {
* "subjects": 32,
* "schools": 28,
* "courses" : 669,
* "courseVariants" : 687
* }
* ```
*
* @ApiDoc(
* description="Get statistics for selected year",
* section="Statistics",
* requirements={
* {
* "name"="year",
* "dataType"="integer",
* "requirement"="\d+",
* "description"="year ID to get stats for"
* }
* },
* statusCodes={
* 200="data returned",
* }
* )
* @Route("/api/stats", name="stats")
* @Method({"GET"})
* @param Request $request
* @param ManagerRegistry $doctrine
* @return JsonResponse
*/
public function getStats(Request $request, ManagerRegistry $doctrine)
{
// get query data from http://localhost:8000/api/stats?year=
$year_id = $request->query->get('year') ?: 0;
$belaEm = $doctrine->getManager('bela');
$prequelEm = $doctrine->getManager('prequel');
$querySubject = $belaEm->createQuery('SELECT COUNT(s.subjectId) FROM BMD\BeLABundle\Entity\Subjects s WHERE s.validFlag=1');
$data['subjects'] = (int) $querySubject->getSingleScalarResult();
$querySchool = $prequelEm->createQuery('SELECT COUNT(s.id) FROM AppBundle\Entity\SchoolsSettings ss JOIN ss.school s WHERE ss.year=:year_id AND ss.validFlag=1');
$querySchool->setParameter('year_id', $year_id);
$data['schools'] = (int) $querySchool->getSingleScalarResult();
$queryCourses = $prequelEm->createQuery('SELECT COUNT(c.id) FROM AppBundle\Entity\Course c WHERE c.yearDefinitionId=:year_id');
$queryCourses->setParameter('year_id', $year_id);
$data['courses'] = (int) $queryCourses->getSingleScalarResult();
$queryCVC = $prequelEm->createQuery('SELECT COUNT(cv.id) FROM AppBundle\Entity\CourseVariants cv JOIN cv.course c WHERE c.yearDefinitionId=:year_id');
$queryCVC->setParameter('year_id', $year_id);
$data['courseVariants'] = (int) $queryCVC->getSingleScalarResult();
return new JsonResponse($data, 200);
}
/**
* Get url of your BeLA, this URL is used for example for password reset
* link or for Home Groups links in School Catalog view.
* #### Output ####
* ```
* {
* "url": "https://www.belasm.com"
* }
* ```
*
* @ApiDoc(
* description="Get url of your BeLA",
* section="BeLA",
* statusCodes={
* 200="data returned",
* }
* )
* @Route("/api/belaurl", name="get_bela_url")
* @Method({"GET"})
*/
public function getBeLAUrl()
{
return $this->json(['url' => $this->getParameter('bela_url')]);
}
}