<?php
namespace App\Controller;
use App\Entity\Course;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Subscription;
use App\Entity\User;
use App\Entity\Question;
use App\Entity\CodeStep;
/**
* @Route("/dashboard")
*/
class FrontDashboardController extends AbstractController
{
/**
* @Route("/", name="dashboard_front")
*/
public function index(): Response
{
if(in_array('ROLE_ADMIN', $this->getUser()->getRoles())){
return $this->redirectToRoute('admin');
}
$lastSubscription = null;
$md5UserID = null;
$isMinor = false;
$lastSubscriptions = $this->getDoctrine()->getRepository(Subscription::class)->findBy(array('user'=>$this->getUser()), array('id'=>'DESC'), 1, 0);
if($lastSubscriptions && $lastSubscriptions[0]->getPack()->getCategory() != 'code'){
if($lastSubscriptions[0]->getExamDate()) {
$examDate = $lastSubscriptions[0]->getExamDate();
} else {
$examDate = $lastSubscriptions[0]->getExam()->getDate();
}
if(strtotime($examDate->format('Y-m-d')) > strtotime(date('Y-m-d'))){
$lastSubscription = $lastSubscriptions[0];
if($this->getUser()->getDatebirth()->getTimestamp() > strtotime("-18 years")) $isMinor = true;
$md5UserID = md5($this->getUser()->getId());
}
}
return $this->render('front/dashboard/index.html.twig', [
'controller_name' => 'FrontDashboardController',
'lastSubscription' => $lastSubscription,
'isMinor' => $isMinor,
'md5UserID' => $md5UserID
]);
}
/**
* @Route("/profile", name="dashboard_profile")
*/
public function profile(): Response
{
$isMinor = false;
if($this->getUser()->getDatebirth() && $this->getUser()->getDatebirth()->getTimestamp() > strtotime("-18 years")) $isMinor = true;
$userData = array();
if($this->getUser()->getData()){
$userData = $this->getUser()->getData();
}
return $this->render('front/dashboard/profile.html.twig', [
'controller_name' => 'FrontDashboardController',
'isMinor' => $isMinor,
'userData' => $userData
]);
}
/**
* @Route("/profile-edit", name="dashboard_edit_profile")
*/
public function edit_profile(): Response
{
$isMinor = false;
if($this->getUser()->getDatebirth() && $this->getUser()->getDatebirth()->getTimestamp() > strtotime("-18 years")) $isMinor = true;
$userData = array();
if($this->getUser()->getData()){
$userData = $this->getUser()->getData();
}
return $this->render('front/dashboard/profile-edit.html.twig', [
'controller_name' => 'FrontDashboardController',
'isMinor' => $isMinor,
'userData' => $userData
]);
}
/**
* @Route("/subscriptions", name="dashboard_subscriptions")
*/
public function subscriptions(): Response
{
if(in_array('ROLE_PARTNER', $this->getUser()->getRoles())){
return $this->redirectToRoute('dashboard_parrainage');
}
$showSubscription = isset($_GET['sID']) ? (int)$_GET['sID'] : null;
$subscriptions = $this->getDoctrine()->getRepository(Subscription::class)->findBy(array('user'=>$this->getUser()));
return $this->render('front/dashboard/subscriptions.html.twig', [
'controller_name' => 'FrontDashboardController',
'subscriptions' => $subscriptions,
'showSubscription' => $showSubscription
]);
}
/**
* @Route("/parrainage", name="dashboard_parrainage")
*/
public function parrainage(): Response
{
if(!in_array('ROLE_PARTNER', $this->getUser()->getRoles())){
return $this->redirectToRoute('dashboard_subscriptions');
}
$users = $this->getDoctrine()->getRepository(User::class)->findBy(array('partner'=>$this->getUser()));
return $this->render('front/dashboard/parrainage.html.twig', [
'controller_name' => 'FrontDashboardController',
'users' => $users
]);
}
/**
* @Route("/code", name="dashboard_code")
*/
public function code(): Response
{
// if(!$this->getUser()->canCode()) throw $this->createNotFoundException('', new NoConfigurationException());
if(!$this->getUser()->canCode()) return $this->redirectToRoute('dashboard_front');
$canCode = $this->getUser()->canCode();
$courseID = 9;
$codeStep = $this->getDoctrine()->getRepository(CodeStep::class)->findOneBy(array('subscription'=>$canCode->getId()));
$getCodeStatsExam = $this->getCodeResults($codeStep, false);
$getCodeStatsTraining = $this->getCodeResults($codeStep, true);
// var_dump($getCodeStatsExam['codeResults']);
// die;
return $this->render('front/dashboard/code.html.twig', [
'controller_name' => 'FrontDashboardController',
'subbscription' => $canCode,
'getCodeStatsExam' => $getCodeStatsExam,
'getCodeStatsTraining' => $getCodeStatsTraining
]);
}
private function getCodeResults($codeStep, $isTraining = false){
if($codeStep){
$codeStepAnswers = !$isTraining ? $codeStep->getAnswers() : $codeStep->getTrainingAnswers();
$codeStepAnswers = $codeStepAnswers ? json_decode($codeStepAnswers) : null;
} else {
$codeStepAnswers = null;
}
$lastQuestion = null;
$totalAnswered = 0;
$activeCourses = $this->getDoctrine()->getRepository(Course::class)->findBy(array('active'=>1));
$questions = $this->getDoctrine()->getRepository(Question::class)->findBy(array('course'=>$activeCourses, 'active'=>1));
$userGoodAnswers = array();
foreach($questions as $question){
$goodQuestionAnswers = $question->getAnswers();
$goodQuestionArrayAnswers = array();
if($goodQuestionAnswers){
foreach($goodQuestionAnswers as $goodQuestionAnswerPos => $goodQuestionAnswer){
if($goodQuestionAnswer->getCorrect() == 1){
$goodQuestionArrayAnswers[] = $goodQuestionAnswerPos;
}
}
if(isset($codeStepAnswers->{$question->getId()})) {
if($goodQuestionArrayAnswers == $codeStepAnswers->{$question->getId()}) $userGoodAnswers[] = $question->getId();
$lastQuestion = $question;
$totalAnswered++;
}
}
}
return array(
'userGoodAnswers' => $userGoodAnswers,
'total' => sizeof($questions),
'totalAnswered' => $totalAnswered,
'lastQuestion' => $lastQuestion
);
}
/**
* @Route("/code/entrainement", name="dashboard_code_training")
*/
public function code_training(): Response
{
if(!$this->getUser()->canCode()) return $this->redirectToRoute('dashboard_front');
$courses = $this->getDoctrine()->getRepository(Course::class)->findBy(array('active'=>true));
return $this->render('front/dashboard/code-training.html.twig', [
'controller_name' => 'FrontDashboardController',
'courses' => $courses
]);
}
/**
* @Route("/code/entrainement/{courseID}", name="code_training_course")
*/
public function code_training_course($courseID): Response
{
if(!$this->getUser()->canCode()) return $this->redirectToRoute('dashboard_front');
$course = $this->getDoctrine()->getRepository(Course::class)->findOneBy(array('id'=>(int)$courseID));
return $this->render('front/dashboard/code-training-course.html.twig', [
'controller_name' => 'FrontDashboardController',
'course' => $course
]);
}
/**
* @Route("/code/examen", name="dashboard_code_examen")
*/
public function code_examen(): Response
{
if(!$this->getUser()->canCode()) return $this->redirectToRoute('dashboard_front');
return $this->render('front/dashboard/code-examen.html.twig', [
'controller_name' => 'FrontDashboardController'
]);
}
/**
* @Route("/code/galerie", name="dashboard_code_gallery")
*/
public function code_gallery(): Response
{
if(!$this->getUser()->canCode()) return $this->redirectToRoute('dashboard_front');
$em = $this->getDoctrine()->getManager();
$query = $em->getRepository('App:Question')
->createQueryBuilder('q')
->leftJoin("App\Entity\Course", "c", "WITH", "c.id = q.course and c.active = 1 and q.active = 1")
->where("c.active = 1 and q.active = 1")
->addOrderBy('c.position', 'ASC')
->addOrderBy('q.position', 'ASC');
$questions = $query->getQuery()->getResult();
return $this->render('front/dashboard/code-gallery.html.twig', [
'controller_name' => 'FrontDashboardController',
'questions' => $questions
]);
}
/**
* @Route("/autoeval", name="auto_eval")
*/
public function auto_eval(): Response
{
$lastSubscription = null;
$lastSubscriptions = $this->getDoctrine()->getRepository(Subscription::class)->findBy(array('user'=>$this->getUser()), array('id'=>'DESC'), 1, 0);
if($lastSubscriptions && $lastSubscriptions[0]->getPack()->getCategory() != 'code'){
if(strtotime($lastSubscriptions[0]->getExam()->getDate()->format('Y-m-d')) > strtotime(date('Y-m-d'))){
$lastSubscription = $lastSubscriptions[0];
}
}
return $this->render('front/dashboard/auto-eval.html.twig', [
'controller_name' => 'FrontDashboardController',
'lastSubscription' => $lastSubscription
]);
}
/**
* @Route("/signcontract", name="sign_contract")
*/
public function sign_contract(): Response
{
$userData = $this->getUser()->getData();
if(isset($userData)) {
if(isset($userData['signedContract'])) {
return $this->redirectToRoute('dashboard_front');
}
}
$lastSubscription = null;
$lastSubscriptions = $this->getDoctrine()->getRepository(Subscription::class)->findBy(array('user'=>$this->getUser(), 'status'=>1), array('id'=>'DESC'), 1, 0);
if($lastSubscriptions && $lastSubscriptions[0]->getPack()->getCategory() != 'code'){
if(strtotime($lastSubscriptions[0]->getExam()->getDate()->format('Y-m-d')) > strtotime(date('Y-m-d'))){
$lastSubscription = $lastSubscriptions[0];
require __DIR__ . '/../../vendor/autoload.php';
$pdf = new \TCPDF();
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->setListIndentWidth(6);
$pdf->setImageScale(1.53);
$pdf->SetFont('dejavusans', '', 10, '', false);
// $pdf->SetMargins(10, 5, 10, true);
// $pdf->SetAutoPageBreak(true, 0);
$pdf->AddPage();
$html = $this->renderView('front/dashboard/pdf-contract.html.twig', [
'lastSubscription' => $lastSubscription
]);
if(isset($_GET['echo'])){
var_dump($html);
die;
}
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->AddPage();
$html = $this->renderView('front/dashboard/annexe_1.html.twig', [
'lastSubscription' => $lastSubscription
]);
$pdf->writeHTML($html, true, false, true, false, '');
if($this->getParameter('kernel.environment') == 'prod'){
$uploadDir = 'www';
} else {
$uploadDir = 'public_html';
}
$pdfUrl = '/assets/contracts/contrat_'.md5($this->getUser()->getId()).'.pdf';
$pdfFile = __DIR__ .'/../../'. $uploadDir .$pdfUrl;
$pdf->Output($pdfFile, 'F');
}
}
return $this->render('front/dashboard/sign-contract.html.twig', [
'controller_name' => 'FrontDashboardController',
'lastSubscription' => $lastSubscription,
'pdfUrl' => $pdfUrl
]);
}
}