<?php
namespace App\Controller;
/**
* Created by PhpStorm.
* User: C
* Date: 12/24/2018
* Time: 1:18 PM
*/
use App\Entity\Company;
use App\Entity\Ebook;
use App\Entity\GridThreeSixtySections;
use App\Entity\GridThreeSixtySurvey;
use App\Entity\GridThreeSixtySurveyResponses;
use App\Entity\Group;
use App\Entity\Pages;
use App\Entity\Question;
use App\Entity\Student;
use App\Entity\Survey;
use App\Entity\SurveySent;
use App\Entity\Team;
use App\Entity\Trainer;
use App\Entity\Users;
use PhpParser\Node\Stmt\Else_;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class FrontEndController extends DefaultController
{
public function home(Request $request)
{
if($this->isGranted('ROLE_SUPER_ADMIN')) {
$entityManager = $this->getDoctrine()->getManager();
$conn = $entityManager->getConnection();
$countBook =1;
$sql = 'select count(id) as count from student ';
$stmt = $conn->prepare($sql);
$stmt->execute();
$countstudent = $stmt->fetchall();
$sql = 'select count(id) as count from trainer ';
$stmt = $conn->prepare($sql);
$stmt->execute();
$countTrainers = $stmt->fetchall();
$sql = 'select count(id) as count from group_class ';
$stmt = $conn->prepare($sql);
$stmt->execute();
$countClasses = $stmt->fetchall();
return $this->render('admin/home.html.twig',array(
'countBook'=>$countBook,
'countStudent'=>$countstudent[0]['count'],
'countClasses'=>$countClasses[0]['count'],
'countTrainers'=>$countTrainers[0]['count'],
));
}
else{
return $this->redirectToRoute('user_home');
}
}
public function ebookList(Request $request)
{
$books = $this->getDoctrine()
->getRepository(Ebook::class)
->findAll();
return $this->render('admin/ebook-list.html.twig',array('books'=>$books));
}
public function ebookPreview(Request $request)
{
$id =$request->request->get('id');
$book = $this->getDoctrine()
->getRepository(Ebook::class)
->find($id);
$pages = $this->getDoctrine()
->getRepository(Pages::class)
->findBy(array('book'=>$book),array('pageNumber'=>'ASC'));
return $this->render('admin/ebook-preview.html.twig',array('pages'=>$pages));
}
public function classList(Request $request)
{
$groups = $this->getDoctrine()
->getRepository(Group::class)
->findAll();
return $this->render('admin/class-list.html.twig',array('groups'=>$groups));
}
public function activeClassList(Request $request)
{
if($this->isGranted('ROLE_SUPER_ADMIN')) {
$groups = $this->getDoctrine()
->getRepository(Group::class)
->findAll();
}else{
$user= $this->getUser();
$trainer = $this->getDoctrine()
->getRepository(Trainer::class)
->findOneBy( array('id'=>$user));
$groups =$trainer->getGroup();
}
return $this->render('admin/active-class-list.html.twig',array('groups'=>$groups ));
}
public function activeClassView(Request $request){
$id =$request->request->get('id');
$groups = $this->getDoctrine()
->getRepository(Group::class)
->find($id);
if($request->request->has('bookId')){
$bookId =$request->request->get('bookId');
$book = $this->getDoctrine()
->getRepository(Pages::class)
->find($bookId);
$pages = $this->getDoctrine()
->getRepository(Pages::class)
->findBy(array('book'=>$book),array('pageNumber'=>'ASC'));
}
else{
$book = $groups->getBook()[0];
$pages = $this->getDoctrine()
->getRepository(Pages::class)
->findBy(array('book'=>$book),array('pageNumber'=>'ASC'));
}
if($request->request->has('student')) {
$studentId= $request->request->get('student');
}else{
$studentId = $groups->getStudent()[0];
$studentId= $studentId->getId()->getId();
}
return $this->render('admin/active-class-View.html.twig',array('groups'=>$groups ,'book'=>$book,'pages'=>$pages,'studentId'=>$studentId));
}
public function GridThreeSixtySurveyList($type,Request $request)
{
$gridThreeSixtySurveys = $this->getDoctrine()
->getRepository(GridThreeSixtySurvey::class)
->findBy(array(
'type'=>$type
));
return $this->render('admin/grid-three-sixty-survey-list.html.twig',array('gridThreeSixtySurveys'=>$gridThreeSixtySurveys,'type'=>$type));
}
public function trainerList(Request $request)
{$trainers = $this->getDoctrine()
->getRepository(Trainer::class)
->findAll();
return $this->render('admin/trainer-list.html.twig',array('trainers'=>$trainers));
}
public function StudentList(Request $request)
{$students = $this->getDoctrine()
->getRepository(Student::class)
->findAll();
return $this->render('company/student-list.html.twig',array('students'=>$students));
}
public function trainerCreate(Request $request)
{
$id =$request->request->get('id');
if($id!=0){
$user = $this->getDoctrine()
->getRepository(Users::class)
->find($id);
$trainer = $this->getDoctrine()
->getRepository(Trainer::class)
->findOneBy(array('id'=>$user));
}
else{
$trainer = null;
}
return $this->render('admin/trainer-create.html.twig',array('trainer'=>$trainer));
}
public function studentCreate(Request $request)
{
$id =$request->request->get('id');
if($id!=0){
$user = $this->getDoctrine()
->getRepository(Users::class)
->find($id);
$student = $this->getDoctrine()
->getRepository(Student::class)
->findOneBy(array('id'=>$user));
}
else{
$student = null;
}
$companies = $this->getDoctrine()
->getRepository(Company::class)
->findAll();
$teams = $this->getDoctrine()
->getRepository(Team::class)
->findAll();
$students = $this->getDoctrine()
->getRepository(Student::class)
->findAll();
return $this->render('company/student-create.html.twig',array('student'=>$student,
'teams'=>$teams,
'companies'=>$companies,'students'=>$students));
}
public function studentCreateBulk(Request $request)
{
$id =$request->request->get('id');
if($id!=0){
$user = $this->getDoctrine()
->getRepository(Users::class)
->find($id);
$student = $this->getDoctrine()
->getRepository(Student::class)
->findOneBy(array('id'=>$user));
}
else{
$student = null;
}
$companies = $this->getDoctrine()
->getRepository(Company::class)
->findAll();
$teams = $this->getDoctrine()
->getRepository(Team::class)
->findAll();
return $this->render('company/student-create-bulk.html.twig',array('student'=>$student,
'teams'=>$teams,
'companies'=>$companies));
}
public function CompanyList(Request $request)
{$companies = $this->getDoctrine()
->getRepository(Company::class)
->findAll();
return $this->render('company/company-list.html.twig',array('companies'=>$companies));
}
public function CompanyCreate(Request $request)
{
$id =$request->request->get('id');
if($id!=0){
$company = $this->getDoctrine()
->getRepository(Company::class)
->find($id);
}
else{
$company = null;
}
return $this->render('company/company-create.html.twig',array('company'=>$company));
}
public function TeamList(Request $request)
{
$teams = $this->getDoctrine()
->getRepository(Team::class)
->findAll();
return $this->render('company/team-list.html.twig',array('teams'=>$teams));
}
public function TeamCreate(Request $request)
{
$companies = $this->getDoctrine()
->getRepository(Company::class)
->findAll();
$id =$request->request->get('id');
if($id!=0){
$team = $this->getDoctrine()
->getRepository(Team::class)
->find($id);
}
else{
$team = null;
}
return $this->render('company/team-create.html.twig',array('team'=>$team,'companies'=>$companies));
}
public function classCreate(Request $request)
{
$books = $this->getDoctrine()
->getRepository(Ebook::class)
->findAll();
$trainers = $this->getDoctrine()
->getRepository(Trainer::class)
->findAll();
$id =$request->request->get('id');
if($id!=0){
$group = $this->getDoctrine()
->getRepository(Group::class)
->find($id);
}
else{
$group = null;
}
$companies = $this->getDoctrine()
->getRepository(Company::class)
->findAll();
$teams = $this->getDoctrine()
->getRepository(Team::class)
->findAll();
$students = $this->getDoctrine()
->getRepository(Student::class)
->findAll();
return $this->render('admin/class-create.html.twig',array('group'=>$group,
'books'=>$books,
'trainers'=>$trainers,'companies'=>$companies,'teams'=>$teams,'students'=>$students));
}
public function GridThreeSixtySurveyCreate(Request $request)
{
$id =$request->request->get('id');
$type =$request->request->get('type');
if($id!=0){
$gridThreeSixtySurvey = $this->getDoctrine()
->getRepository( GridThreeSixtySurvey::class)
->find($id);
$entityManager = $this->getDoctrine()->getManager();
$conn = $entityManager->getConnection();
$sql = "SELECT GROUP_CONCAT(grid_three_sixty_survey_response.student_id) AS students ,TYPE as surveyType ,matter_student_id
FROM grid_three_sixty_survey_response WHERE grid_three_sixty_survey_response.survey_id = ".$id." GROUP BY
grid_three_sixty_survey_response.matter_student_id,grid_three_sixty_survey_response.`type` ";
$stmt = $conn->prepare($sql);
$stmt->execute();
$gridThreeSixtySurveyResonses = $stmt->fetchall();
$gridThreeSixtyRow = array();
$matterId = 0;
foreach ($gridThreeSixtySurveyResonses as $row){
if($row['matter_student_id']!=$matterId){
if(isset($newRow) ){
$gridThreeSixtyRow[]= $newRow;
}
$matterId =$row['matter_student_id'];
$newRow = array();
}
$newRow[$row['surveyType']] = $row['students'];
}
$gridThreeSixtyRow[]= $newRow;
}
else{
$gridThreeSixtySurvey = new GridThreeSixtySurvey();
$gridThreeSixtyRow =array();
}
$gridThreeSixtySurvey->setType($type);
$companies = $this->getDoctrine()
->getRepository(Company::class)
->findAll();
$teams = $this->getDoctrine()
->getRepository(Team::class)
->findAll();
$students = $this->getDoctrine()
->getRepository(Student::class)
->findAll();
return $this->render('admin/grid-three-sixty-survey-create.html.twig',array('gridThreeSixtySurvey'=>$gridThreeSixtySurvey,
'companies'=>$companies,'teams'=>$teams,'students'=>$students,'error_list'=>null,'gridThreeSixtyRow'=>$gridThreeSixtyRow));
}
public function surveyCreate(Request $request)
{
$id =$request->request->get('id');
if($id!=0){
$survey = $this->getDoctrine()
->getRepository(Survey::class)
->find($id);
}
else{
$survey = null;
}
$companies = $this->getDoctrine()
->getRepository(Company::class)
->findAll();
$teams = $this->getDoctrine()
->getRepository(Team::class)
->findAll();
$students = $this->getDoctrine()
->getRepository(Student::class)
->findAll();
return $this->render('admin/survey-create.html.twig',array('survey'=>$survey,
'type'=>$request->request->get('type'), 'companies'=>$companies,'teams'=>$teams,'students'=>$students,'error_list'=>null));
}
public function QuestionsList($type,Request $request){
$questions = $this->getDoctrine()
->getRepository(Question::class)
->findBy (array('type'=>$type));
return $this->render('admin/question-list.html.twig',array('questions'=>$questions,'type'=>$type));
}
public function SurveyList($type,Request $request){
$surveys = $this->getDoctrine()
->getRepository(Survey::class)
->findBy(array(
'type'=>$type
));
return $this->render('admin/survey-list.html.twig',array('surveys'=>$surveys,'type'=>$type));
}
public function GridThreeSixtyList($type,Request $request){
$sections = $this->getDoctrine()
->getRepository(GridThreeSixtySections::class)
->findBy(
array('type'=>$type)
);
return $this->render('admin/grid-three-sixty-list.html.twig',array('sections'=>$sections));
}
public function GridThreeSixtyEditPage(Request $request){
$id =$request->request->get('id');
$section = $this->getDoctrine()
->getRepository(GridThreeSixtySections::class)
->find($id);
return $this->render('admin/grid-three-sixty-create.html.twig',array('section'=>$section));
}
public function QuestionEditPage(Request $request){
$id =$request->request->get('id');
$question = $this->getDoctrine()
->getRepository(Question::class)
->find($id);
return $this->render('admin/question-create.html.twig',array('question'=>$question));
}
public function QuestionEdit(Request $request){
$id =$request->request->get('id','');
$question = $this->getDoctrine()
->getRepository(Question::class)
->find($id);
return $this->render('admin/question-create.html.twig',array('question'=>$question));
}
//UserPages
public function userHome(Request $request){
$entityManager = $this->getDoctrine()->getManager();
$conn = $entityManager->getConnection();
$sql = 'select DISTINCT(ebook.id)
,ebook.name
from ebook,book_group,student_group
where
ebook.id = book_group.book_id
and book_group.group_id = student_group.group_id
and student_group.student_id = :student_id ';
$stmt = $conn->prepare($sql);
$stmt->execute(['student_id'=>$this->getUser()->getId()]);
$books = $stmt->fetchall();
$sql = '
select grid_three_sixty_survey.name ,
grid_three_sixty_survey.id ,
grid_three_sixty_survey.end_date
from grid_three_sixty_survey,grid_three_sixty_survey_response
where grid_three_sixty_survey.id = grid_three_sixty_survey_response.survey_id
and grid_three_sixty_survey_response.student_id = :student_id
group BY grid_three_sixty_survey.name , grid_three_sixty_survey.id ,grid_three_sixty_survey.end_date
';
$stmt = $conn->prepare($sql);
$stmt->execute(['student_id'=>$this->getUser()->getId()]);
$gridSurveys = $stmt->fetchall();
$date = new \DateTime();
$dateFormated = date_format($date, 'Y-m-d');
$sql = 'select survey.name ,survey.id ,survey.end_date from survey,student_survey
where survey.id = student_survey.survey_id
and student_survey.student_id = :student_id
and Date(survey.end_date) >= :dateFormated ';
$stmt = $conn->prepare($sql);
$stmt->execute(['student_id'=>$this->getUser()->getId(),
'dateFormated'=>$dateFormated]);
$surveys = $stmt->fetchall();
return $this->render('user/home.html.twig',
array(
'books'=>$books,
'gridSurveys'=>$gridSurveys,
'surveys'=>$surveys
));
}
public function userEbookPreview(Request $request)
{
$id =$request->request->get('id');
$book = $this->getDoctrine()
->getRepository(Ebook::class)
->find($id);
$pages = $this->getDoctrine()
->getRepository(Pages::class)
->findBy(array('book'=>$book),array('pageNumber'=>'ASC'));
return $this->render('user/ebook-preview.html.twig',array('pages'=>$pages,'book'=>$book));
}
public function userGridSuerveyPage(Request $request)
{
$id =$request->request->get('id');
$user = $this->getUser();
$entityManager = $this->getDoctrine()->getManager();
$conn = $entityManager->getConnection();
$sql = 'select grid_three_sixty_survey_response.id,student.first_name,grid_three_sixty_survey_response.status from grid_three_sixty_survey_response , student
where grid_three_sixty_survey_response.student_id = :student_id
and grid_three_sixty_survey_response.survey_id=:survey_id
and student.id = grid_three_sixty_survey_response.matter_student_id
';
$stmt = $conn->prepare($sql);
$stmt->execute(['student_id'=>$this->getUser()->getId(),
'survey_id'=>$id]);
$gridSurveys = $stmt->fetchall();
if($gridSurveys==null)
$this->redirectToRoute('user_home');
$student = $entityManager->getRepository(Student::class)
->findOneBy(array('id'=>$user));
$survey = $entityManager->getRepository(GridThreeSixtySurvey::class)->find($id);
$sections = $this->getDoctrine()
->getRepository(GridThreeSixtySections::class)
->findby(
array('type'=>$survey->getType())
);
return $this->render('user/grid-three-sixty-survey.html.twig',array(
'sections'=>$sections,'gridSurveys'=>$gridSurveys));
}
public function userServeyPage(Request $request){
$entityManager = $this->getDoctrine()->getManager();
$id =$request->request->get('id');
$user = $this->getUser();
if ($user != null) {
$survey = $entityManager->getRepository(SurveySent::class)
->findOneBy(
array('userid' => $user->getId(),
'status' => 0,
'surveyid'=>$id
)
);
if ($survey != null) {
$SurveyObject = $entityManager->getRepository(Survey::class)
->find($id);
$currentdate = new \DateTime();
// get user from token
//student survey
//betjib el user b aya
$question = $entityManager->getRepository(Question::class)
->findBy(array(
'type'=>$SurveyObject->getType()
));
$student = $entityManager->getRepository(Student::class)
->findOneBy(array(
'id'=>$user
));
if ($SurveyObject->getEndDate() > $currentdate and $SurveyObject->getStartDate() < $currentdate) {
if($SurveyObject->getType()==3){
return $this->render('user/surveyArabic.html.twig', array('questions' => $question, 'survey' => $survey, 'SurveyObject' => $SurveyObject));
}
return $this->render('user/survey.html.twig', array('questions' => $question, 'survey' => $survey, 'SurveyObject' => $SurveyObject));
} else {
return $this->redirectToRoute('home');
}
}
}
return $this->redirectToRoute('home');
}
public function GridThreeSixtyReportList(Request $request){
$entityManager = $this->getDoctrine()->getManager();
$conn = $entityManager->getConnection();
$id= $request->request->get('id');
$sql = 'SELECT count(`grid_three_sixty_survey_response`.id) as nb_survey,`grid_three_sixty_survey_response`.matter_student_id,survey_id,users.username,student.first_name FROM `grid_three_sixty_survey_response`,`student`,`users`
WHERE `student`.`id` = `grid_three_sixty_survey_response`.`matter_student_id` and `student`.`id` = `users`.`id`
and `grid_three_sixty_survey_response`.survey_id = :survey_id
group by matter_student_id ';
$stmt = $conn->prepare($sql);
$stmt->execute([
'survey_id'=>$id]);
$UserReportList = $stmt->fetchall();
return $this->render('admin/grid-three-sixty-survey-report-list.html.twig',array('userReportList'=>$UserReportList));
}
}