<?php
namespace HomeBundle\Controller;
use CoreBundle\Entity\Branche;
use CoreBundle\Entity\NewsFeed;
use CoreBundle\Entity\NoteTest;
use CoreBundle\Entity\NoteTestGroup;
use CoreBundle\Entity\Periode;
use CoreBundle\Entity\Post;
use CoreBundle\Entity\Secteur;
use CoreBundle\Entity\Tutor;
use CoreBundle\Provider\DefaultValuesProvider;
use Doctrine\ORM\EntityManagerInterface;
use HomeBundle\Service\BoxImageByUser;
use HomeBundle\Service\SchoolLifeBoxByUser;
use QuestionnaireBundle\Service\QcmForStudent;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use CoreBundle\Entity\BoxText;
use Symfony\Component\HttpFoundation\JsonResponse;
use CoreBundle\Entity\BoxImage;
use CoreBundle\Entity\Student;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use CoreBundle\S3;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
/**
* @Route("/", name="app_home")
*/
public function indexAction(
EntityManagerInterface $em,
DefaultValuesProvider $defaultValuesProvider,
BoxImageByUser $boxImageByUser,
SchoolLifeBoxByUser $schoolLifeBoxByUser,
QcmForStudent $qcmForStudent,
) {
$user = $this->getUser();
$periode = $defaultValuesProvider->getPeriode();
// Emploi du temps
$boxImages = $boxImageByUser->get();
// school life
$boxes = $schoolLifeBoxByUser->get();
$forumUnreads = $em->getRepository(NewsFeed::class)
->getUnread($user, 'POST', '-15days');
$qcmOnlineUnreads = $em->getRepository(NewsFeed::class)
->getUnread($user, 'QCM', '-15days');
$documentUnreads = $em->getRepository(NewsFeed::class)
->getUnread($user, 'FILE', '-15days');
$edtUnreads = $em->getRepository(NewsFeed::class)
->getUnread($user, 'EDT', '-15days');
// qcm
$onlineQcms = [];
if ($user instanceof Student) {
$qcmForStudent->setSort('date', 'desc');
$onlineQcms = $qcmForStudent->getOnlines();
}
if ($this->getUser()->getAdmin()) {
$branches = $em->getRepository(Branche::class)
->findBy([], ['name' => 'asc']);
} elseif ($this->getUser() instanceof Student) {
$branches = [$this->getUser()->getBranche()];
} else {
$branches = $em->getRepository(Branche::class)
->getByTutor($this->getUser());
}
$lastPosts = $em->getRepository(Post::class)
->getUserLastPosts($periode, $this->getUser());
$openedTestGroups = [];
if ($this->getUser() instanceof Tutor) {
if ($this->getUser()->getAdmin()) {
$openedTestGroups = $em->getRepository(NoteTestGroup::class)
->getOpensByAdmin($this->getUser(), $periode);
} else {
$openedTestGroups = $em->getRepository(NoteTestGroup::class)
->getOpensByTutor($this->getUser(), $periode);
}
}
return $this->render('@HomeBundle/Default/index.html.twig', [
'branches' => $branches,
'boxImages' => $boxImages,
'boxes' => $boxes,
'lastPosts' => $lastPosts,
'openedTestGroups' => $openedTestGroups,
'onlineQcms' => $onlineQcms,
'forumUnreads' => $forumUnreads,
'qcmOnlineUnreads' => $qcmOnlineUnreads,
'documentUnreads' => $documentUnreads,
'edtUnreads' => $edtUnreads,
]);
}
/**
* @Route("/text/{box_image_id}/{id}",
* name="app_home_text",
* requirements={"box_image_id": "\d+", "id": "\d+"},
* defaults={"id": null},
* options={"expose" : true})
* @ParamConverter("boxImage", options={"mapping"={"box_image_id": "id"}})
* @IsGranted("ROLE_ADMIN")
*/
public function text(
EntityManagerInterface $em,
Request $request,
BoxImage $boxImage,
BoxText $boxText = null
) {
if (null === $boxText) {
$boxText = new BoxText();
$boxText->setBoxImage($boxImage);
}
$boxText->setContent($request->request->get('content'));
$em->persist($boxText);
$em->flush();
$formatter = new \IntlDateFormatter('fr', \IntlDateFormatter::FULL, \IntlDateFormatter::SHORT);
return new JsonResponse([
'code' => 'success',
'id' => $boxText->getId(),
'date' => $formatter->format(new \DateTime())
]);
}
/**
* @Route("/delete-text-box/{id}", name="app_home_delete_text_box", options={"expose" : true})
* @IsGranted("ROLE_ADMIN")
*/
public function delTextBox(EntityManagerInterface $em, BoxText $boxText)
{
$em->remove($boxText);
$em->flush();
return new JsonResponse(['code' => "success"]);
}
}