<?php
namespace App\Controller;
use App\Entity\Task;
use App\Entity\ExamTemplate;
use App\Entity\Attribute;
use App\Entity\AttributeValue;
use App\Entity\City;
use App\Entity\County;
use App\Entity\School;
use App\Entity\Teacher;
use App\Entity\Voivodeship;
use App\Service\Helpers;
use DateTime;
use PHPUnit\TextUI\Help;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Doctrine\Persistence\ManagerRegistry;
/** @Route("/nauczyciel", name="teacher_") */
class TeacherController extends AbstractController
{
private $entityManager;
public function __construct(ManagerRegistry $entityManager)
{
$this->em = $entityManager;
}
/**
* @Route("/tokeny", name="token_reindex")
* @IsGranted("ROLE_ADMIN")
*/
public function tokenReindex(Request $request)
{
$criteria = array();
$teachers = $this->em->getRepository(Teacher::class)->findByCriteria($criteria);
foreach ($teachers as $teacher){
$token = null;
while($token==null){
$tokenTmp = $teacher->generateToken();
$c = array();
$c['token'] = $tokenTmp;
$c['single'] = true;
$teacherTmp = $this->em->getRepository(Teacher::class)->findByCriteria($c);
if($teacherTmp == null){
$token=$tokenTmp;
}
}
$teacher->setOption('token',$token);
$this->em->getManager()->persist($teacher);
$this->em->getManager()->flush();
}
}
/**
* @Route("/", name="index")
* @IsGranted("ROLE_ADMIN")
*/
public function index(Request $request)
{
$criteria = array();
$voivodeships = $this->em->getRepository(Voivodeship::class)->findBy(array(),array('name'=>'asc'));
$countys=array();
$cities=array();
$schools=array();
if($request->get('voivodeship')){
$voivodeship = $this->em->getRepository(Voivodeship::class)->find($request->get('voivodeship'));
$countys = $this->em->getRepository(County::class)->findBy(array("voivodeship"=>$voivodeship),array('name'=>'asc'));
if($request->get('county')){
$county = $this->em->getRepository(County::class)->find($request->get('county'));
$cities = $this->em->getRepository(City::class)->findBy(array("county"=>$county),array('name'=>'asc'));
if($request->get('city')){
$city = $this->em->getRepository(City::class)->find($request->get('city'));
$schools = $this->em->getRepository(School::class)->findBy(array("city"=>$city),array('name'=>'asc'));
}
}
}
$criteria['voivodeship']=$request->get('voivodeship');
$criteria['county']=$request->get('county');
$criteria['city']=$request->get('city');
$criteria['school']=$request->get('school');
$criteria['level']=$request->get('level');
$criteria['subject']=$request->get('subject');
$criteria['status']=$request->get('status');
$criteria['email']=$request->get('email');
$criteria['name']=$request->get('name');
$criteria['count']=true;
$count = $this->em->getRepository(Teacher::class)->findByCriteria($criteria);
$criteria['page']=$request->get('page',1);
unset($criteria['count']);
$teachers = $this->em->getRepository(Teacher::class)->findByCriteria($criteria);
$attribute = $this->em->getRepository(Attribute::class)->find(1);
$subjects = $this->em->getRepository(AttributeValue::class)->findBy(array("attribute"=>$attribute));
return $this->render('teacher/index.html.twig', [
"teachers"=>$teachers,
"voivodeships"=>$voivodeships,
"countys"=>$countys,
"cities"=>$cities,
"schools"=>$schools,
"subjects"=>$subjects,
"criteria"=>$criteria,
"count"=>$count
]);
}
/**
* @Route("/pobierz-nauczycieli", name="get_teachers")
* @IsGranted("ROLE_ADMIN")
*/
public function getTeachers(Request $request)
{
$criteria = $request->get('criteria');
$teachers = $this->em->getRepository(Teacher::class)->findByCriteria($criteria);
$attribute = $this->em->getRepository(Attribute::class)->find(1);
$subjects = $this->em->getRepository(AttributeValue::class)->findBy(array("attribute"=>$attribute));
return $this->render('teacher/getteachers.html.twig', [
"teachers"=>$teachers,
]);
}
/**
* @Route("/dodaj", name="add")
* @IsGranted("ROLE_ADMIN")
*/
public function add()
{
return $this->render('teacher/add.html.twig', [
]);
}
/**
* @Route("/aktywacja", name="activation")
*
*/
public function activation(Request $request)
{
$manager = $this->em->getManager();
$teacher = $this->getUser();
if($teacher->getOption('preregistered')){
$teacher->setRoles(["ROLE_TEACHER"]);
$teacher->setStatus(2);
$teacher->setOption("agreement_1",$request->get('agreement_1',0));
$teacher->setOption("agreement_2",$request->get('agreement_2',0));
$teacher->setOption("agreement_3",$request->get('agreement_3',0));
$teacher->setOption("agreement_4",$request->get('agreement_4',0));
$manager->persist($teacher);
$manager->flush();
// $message = (new Email())
// ->from('rejestracja@generatorklasowek.pl')
// ->to($teacher->getUsername())
// ->subject('Aktywacja konta w Generatorze Klasówek')
// ->html(
// $this->renderView(
// 'emails/activation.html.twig',
// ['teacher' => $teacher]
// ),
// 'text/html'
// )
//
// // you can remove the following code if you don't define a text version for your emails
// ->text(
// $this->renderView(
// 'emails/activation.txt.twig',
// ['teacher' => $teacher]
// ),
// 'text/plain'
// );
// $mailer->send($message);
$token = new UsernamePasswordToken(
$teacher,
$teacher->getPassword(),
'secured',
$teacher->getRoles()
);
$this->container->get('security.token_storage')->setToken($token);
}
return $this->redirectToRoute('home');
}
/**
* @Route("/edytuj/{id}", name="edit")
* @IsGranted("ROLE_ADMIN")
*/
public function edit()
{
return $this->render('teacher/edit.html.twig', [
]);
}
/**
* @Route("/profil", name="profile")
* @IsGranted({"ROLE_TEACHER"})
*/
public function profile(Request $request,UserPasswordEncoderInterface $passwordEncoder)
{
$teacher = $this->getUser();
if($request->isMethod('post')){
$manager = $this->em->getManager();
if($request->get('username')!=$teacher->getUserName()){
$history = $teacher->getOption('emails_history',array());
$history[date('Y-m-d H:i:s')]['old_email']=$teacher->getUserName();
$history[date('Y-m-d H:i:s')]['new_email']=$request->get('username');
$teacher->setOption('emails_history',$history);
}
$teacher->setUsername($request->get('username'));
$teacher->setFirstName($request->get('first_name'));
$teacher->setLastName($request->get('last_name'));
if($request->get('passwd')){
$teacher->setPassword($passwordEncoder->encodePassword($teacher,$request->get('passwd')));
}
$s = $request->get('school');
foreach($s as $key=>$value){
if(!is_array($value)){
$s[$key]=trim($value);
}
}
if($s['name']){
$school = new School();
$school->setVoivodeship($this->em->getRepository(Voivodeship::class)->find($request->get('voivodeship')));
$school->setCounty($this->em->getRepository(County::class)->find($request->get('county')));
$school->setCity($this->em->getRepository(City::class)->find($request->get('city')));
$school->setName($s['name']);
$school->setPostcode($s['postcode']);
$school->setStreet($s['street']);
$school->setPhone($s['phone']);
$school->setEmail($s['email']);
$school->setLevel($s['level']);
$school->setIsValid(false);
$manager->persist($school);
$teacher->setSchool($school);
}
elseif($request->get('school_id')){
$school = $this->em->getRepository(School::class)->find($request->get('school_id'));
$teacher->setSchool($school);
}
$options = $teacher->getOptions();
$optionsPost = $request->get('options');
$options['agreement_1']=isset($optionsPost['agreement_1'])?$optionsPost['agreement_1']:0;
$options['agreement_2']=isset($optionsPost['agreement_2'])?$optionsPost['agreement_2']:0;
$options['agreement_3']=isset($optionsPost['agreement_3'])?$optionsPost['agreement_3']:0;
$teacher->setOptions($options);
$manager->persist($teacher);
$manager->flush();
}
return $this->render('teacher/profile.html.twig', [
"teacher"=>$teacher
]);
}
/**
* @Route("/usun/{id}", name="remove")
* @IsGranted("ROLE_ADMIN")
*/
public function remove(Request $request)
{
$manager = $this->em->getManager();
$teacher = $this->em->getRepository(Teacher::class)->find($request->get('id'));
$teacher->setStatus(5);
$manager->persist($teacher);
$manager->flush();
return $this->redirectToRoute('teacher_index');
}
/**
* @Route("/zapisz", name="update")
* @IsGranted("ROLE_ADMIN")
*/
public function update(Request $request,UserPasswordEncoderInterface $passwordEncoder)
{
$manager = $this->em->getManager();
if($request->get('id')){
$teacher = $this->em->getRepository(Teacher::class)->find($request->get('id'));
}
else{
$passwd = Helpers::randomPassword();
$teacher = new Teacher();
$teacher->setExpireAt(new DateTime("+12 months"));
$teacher->setCreatedAt(new DateTime("now"));
$teacher->setPassword($passwordEncoder->encodePassword($teacher,$passwd));
}
$teacher->setFirstName($request->get('first_name'));
$teacher->setLastName($request->get('last_name'));
$teacher->setUsername($request->get('username'));
if($request->get('passwd')){
$teacher->setPassword($passwordEncoder->encodePassword($teacher,$request->get('passwd')));
}
if($request->get('status')==1 and $request->get('status')!=$teacher->getStatus()){
// niezweryfikowany
$teacher->setRoles(["ROLE_GUEST"]);
}
elseif($request->get('status')==2 and $request->get('status')!=$teacher->getStatus()){
// aktywowany
$teacher->setRoles(["ROLE_TEACHER"]);
// $message = (new Email())
// ->subject('Aktywacja konta w Generatorze Klasówek')
// ->from('rejestracja@generatorklasowek.pl')
// ->to($teacher->getUsername())
// ->html(
// $this->renderView(
// 'emails/activation.html.twig',
// ['teacher' => $teacher]
// ),
// 'text/html'
// )
//
// // you can remove the following code if you don't define a text version for your emails
// ->text(
// $this->renderView(
// 'emails/activation.txt.twig',
// ['teacher' => $teacher]
// ),
// 'text/plain'
// )
// ;
//
// $mailer->send($message);
}
elseif($request->get('status')==3 and $request->get('status')!=$teacher->getStatus()){
// zablokowany
$teacher->setRoles(["ROLE_GUEST"]);
}
elseif($request->get('status')==4){
// oczekujący
$teacher->setRoles(["ROLE_GUEST"]);
}
$teacher->setStatus($request->get('status'));
if($request->get("school_id") && !$request->get('school')['name']){
$school = $this->em->getRepository(School::class)->find($request->get('school_id'));
} else if($request->get("school_id") == 0) {
$school = $teacher->getSchool();
if($school->getIsValid() !== 1) {
$school->setIsValid(1);
$manager->persist($school);
}
}
else{
$s = $request->get('school');
$school = new School();
$school->setVoivodeship($this->em->getRepository(Voivodeship::class)->find($request->get('voivodeship')));
$school->setCounty($this->em->getRepository(County::class)->find($request->get('county')));
$school->setCity($this->em->getRepository(City::class)->find($request->get('city')));
$school->setName($s['name']);
$school->setPostcode($s['postcode']);
$school->setStreet($s['street']);
$school->setPhone($s['phone']);
$school->setEmail($s['email']);
$school->setLevel($s['level']);
$school->setIsValid(true);
$manager->persist($school);
}
$teacher->setSchool($school);
$post=$request->get('options');
$options=$teacher->getOptions();
foreach($post['subject'] as $key=>$value){
$options['subjects'][]=$key;
}
$options['note']=$post['note'];
$options['codes']=$post['codes'];
$teacher->setOptions($options);
$manager->persist($teacher);
$manager->flush();
return $this->redirectToRoute('teacher_index');
}
/**
* @Route("/przypomnij-haslo", name="remind_password")
*/
public function remindPassword(Request $request,UserPasswordEncoderInterface $passwordEncoder, MailerInterface $mailer){
$teacher=$this->em->getRepository(Teacher::class)->find($request->get('teacher_id'));
if($teacher instanceof Teacher){
$manager = $this->em->getManager();
$passwd = Helpers::randomPassword();
$teacher->setPassword($passwordEncoder->encodePassword($teacher,$passwd));
$manager->persist($teacher);
$manager->flush();
$message = (new Email())
->subject('Zmiana hasła w Generatorze Klasówek')
->from('rejestracja@generatorklasowek.pl')
->to($teacher->getUsername())
->html(
$this->renderView(
// templates/emails/registration.html.twig
'emails/remind.html.twig',
['teacher' => $teacher,'passwd'=> $passwd]
),
'text/html'
)
// you can remove the following code if you don't define a text version for your emails
->text(
$this->renderView(
// templates/emails/registration.txt.twig
'emails/remind.txt.twig',
['teacher' => $teacher,'passwd'=> $passwd]
),
'text/plain'
)
;
$mailer->send($message);
return $this->redirectToRoute('teacher_show',array('id'=>$teacher->getId()));
}
return $this->redirectToRoute('teacher_index');
}
public function form(Request $request, Helpers $helper)
{
$teacher = null;
$attribute = $this->em->getRepository(Attribute::class)->find(1);
$subjects = $this->em->getRepository(AttributeValue::class)->findBy(array("attribute"=>$attribute));
if($request->get('id')){
$teacher = $this->em->getRepository(Teacher::class)->find($request->get('id'));
}
$books = $helper->getBooks();
return $this->render('teacher/form.html.twig', [
"subjects"=>$subjects,
"teacher"=>$teacher,
"books"=>$books
]);
}
/**
* @Route("/pokaz/{id}", name="show")
* @IsGranted("ROLE_ADMIN")
*/
public function show(Request $request)
{
$teacher = $this->em->getRepository(Teacher::class)->find($request->get('id'));
$attribute = $this->em->getRepository(Attribute::class)->find(1);
$subjects = $this->em->getRepository(AttributeValue::class)->findBy(array("attribute"=>$attribute));
if($request->get('expire_at')){
$em = $this->em->getManager();
$teacher->setExpireAt(new DateTime($request->get('expire_at')));
$em->persist($teacher);
$em->flush();
}
return $this->render('teacher/show.html.twig', [
"subjects"=>$subjects,
"teacher"=>$teacher
]);
}
/**
* @Route("/hurtowe-przedluzanie-waznosci-kont", name="bulk")
* @IsGranted("ROLE_ADMIN")
*/
public function bulk(Request $request)
{
$teachers = $this->em->getRepository(Teacher::class)->findAll();
if($request->get('expire_at')){
$em = $this->em->getManager();
foreach($teachers as $teacher){
if($teacher->getStatus()==2){
$teacher->setExpireAt(new DateTime($request->get('expire_at')));
$em->persist($teacher);
}
}
$em->flush();
}
return $this->render('teacher/bulk.html.twig', [
]);
}
/**
* @Route("/wybierz-szkole", name="get_school")
*/
public function getSchool(Request $request)
{
$voivodeships = $this->em->getRepository(Voivodeship::class)->findBy(array(),array('name'=>'asc'));
$countys=array();
$cities=array();
$schools=array();
$school=null;
if($request->get('school_id')){
$school = $this->em->getRepository(School::class)->find($request->get('school_id'));
}
if($request->get('voivodeship',($school instanceof School)?$school->getVoivodeship()->getId():null)) {
$voivodeship = $this->em->getRepository(Voivodeship::class)->find($request->get('voivodeship',($school instanceof School)?$school->getVoivodeship()->getId():null));
$countys = $this->em->getRepository(County::class)->findBy(array("voivodeship" => $voivodeship), array('name' => 'asc'));
}
if($request->get('county',($school instanceof School)?$school->getCounty()->getId():null)) {
$county = $this->em->getRepository(County::class)->find($request->get('county',($school instanceof School)?$school->getCounty()->getId():null));
$cities = $this->em->getRepository(City::class)->findBy(array("county" => $county), array('name' => 'asc'));
}
if($request->get('city',($school instanceof School)?$school->getCity()->getId():null)) {
$city = $this->em->getRepository(City::class)->find($request->get('city',($school instanceof School)?$school->getCity()->getId():null));
$schools = $this->em->getRepository(School::class)->findBy(array("city" => $city), array('name' => 'asc'));
}
return $this->render('teacher/getschool.html.twig', [
"voivodeships"=>$voivodeships,
"countys"=>$countys,
"cities"=>$cities,
"schools"=>$schools,
"school"=>$school
]);
}
/**
* @Route("/logowanie", name="login")
*/
public function login(AuthenticationUtils $authenticationUtils){
$user = $this->getUser();
if($user) {
$auth_checker = $this->get('security.authorization_checker');
//$isRoleUser = $auth_checker->isGranted('ROLE_USER');
if($auth_checker->isGranted('ROLE_TEACHER') and $user->getStatus()==5){
return $this->redirectToRoute('logout');
}
else{
return $this->redirectToRoute('home');
}
}
return $this->render('/widget/login.html.twig',
[
'last_username' => $authenticationUtils->getLastUsername(),
'error' => $authenticationUtils->getLastAuthenticationError()
]);
}
/**
* @Route("/logout",name="logout")
*/
public function logout(){
}
/**
* @Route("/przypominanie-hasla", name="password_remind")
*
*/
public function passwordRemind(Request $request, MailerInterface $mailer){
$error = null;
if($request->getMethod()=='POST'){
$criteria =array();
$criteria['username']=trim($request->get('email'));
$teacher=$this->em->getRepository(Teacher::class)->findOneBy($criteria);
if($teacher instanceof Teacher) {
$hash = md5($teacher->getSalt() . $teacher->getUsername());
$message = (new Email())
->subject('Zmiana hasła w Generatorze Klasówek')
->from('rejestracja@generatorklasowek.pl')
->to(trim($teacher->getUsername()))
->html(
$this->renderView(
// templates/emails/registration.html.twig
'emails/changePassword.html.twig',
['teacher' => $teacher, 'hash' => $hash]
),
'text/html'
)
->text(
$this->renderView(
// templates/emails/registration.txt.twig
'emails/changePassword.txt.twig',
['teacher' => $teacher, 'hash' => $hash]
),
'text/plain'
);
$mailer->send($message);
}
}
return $this->render('/teacher/passwordRemind.html.twig', [
'error'=>$error
]);
}
/**
* @Route("/zmiana-hasla", name="password_change")
*
*/
public function passwordChange(Request $request, MailerInterface $mailer,UserPasswordEncoderInterface $passwordEncoder){
$error = null;
if($request->getMethod()=='POST'){
$teacher = $this->em->getRepository(Teacher::class)->find($request->get('id'));
$em = $this->em->getManager();
if(!$request->get('passwd')){
$error = 'Zdefiniuj nowe hasło';
}
else{
if($teacher instanceof Teacher and md5($teacher->getSalt().$teacher->getUsername())==$request->get('hash')){
$teacher->setPassword($passwordEncoder->encodePassword($teacher,$request->get('passwd')));
$em->persist($teacher);
$em->flush();
}
}
}
return $this->render('/teacher/passwordChange.html.twig', [
'error'=>$error
]);
}
/**
* @Route("/rejestracja", name="register")
*
*/
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder, MailerInterface $mailer){
if($request->isXmlHttpRequest()){
$manager = $this->em->getManager();
// początek weryfikacji
$errors = array();
$t = $request->get('teacher');
foreach($t as $key=>$value){
if(!is_array($value)){
$t[$key]=trim($value);
}
}
$s = $request->get('school');
foreach($s as $key=>$value){
if(!is_array($value)){
$s[$key]=trim($value);
}
}
$t['username'] = strtolower($t['username']);
if(!$t['first_name']) $errors['first_name']="Podaj imię";
if(!$t['last_name']) $errors['last_name']="Podaj nazwisko";
if(!$t['username']) $errors['username']="Podaj adres e-mail";
if(empty($t['subject'])) $errors['subject']="Wybierz przedmiot";
$teacher = $this->em->getRepository(Teacher::class)->findOneBy(array("username"=>$t['username']));
if($teacher instanceof Teacher){
$errors['username']="Podany adres e-mail został już użyty do rejestracji.";
}
if($s['name']){
if(!$s['name']) $errors['school_name']="Podaj nazwę szkoły";
if(!$s['postcode']) $errors['school_postcode']="Podaj kod pocztowy dla szkoły";
if(!$s['street']) $errors['school_street']="Podaj ulicę szkoły";
if(!$s['phone']) $errors['school_phone']="Podaj telefon do szkoły";
if(!$s['email']) $errors['school_email']="Podaj email do szkoły";
if(!$request->get('voivodeship')) $errors['voivodeship']="Wybierz wojeództwo w jakim znajduje się Twoja szkoła";
if(!$request->get('county')) $errors['county']="Wybierz powiat w jakim znajduje się Twoja szkoła";
if(!$request->get('city')) $errors['city']="Wybierz miejscowość w jakiej znajduje się Twoja szkoła";
}
else{
if(!$request->get('school_id')) $errors['school_id']="Wybierz szkołę lub dodaj nową";
}
if(!$request->get('agreement_4')) $errors['agreement_4']="Zapoznaj się z regulaminem i zaakceptuj jego warunki.";
if(!$errors){
if($s['name']){
$school = new School();
$school->setVoivodeship($this->em->getRepository(Voivodeship::class)->find($request->get('voivodeship')));
$school->setCounty($this->em->getRepository(County::class)->find($request->get('county')));
$school->setCity($this->em->getRepository(City::class)->find($request->get('city')));
$school->setName($s['name']);
$school->setPostcode($s['postcode']);
$school->setStreet($s['street']);
$school->setPhone($s['phone']);
$school->setEmail($s['email']);
$school->setLevel($s['level']);
$school->setIsValid(false);
$manager->persist($school);
}
else {
$school = $this->em->getRepository(School::class)->find($request->get("school_id"));
}
$passwd = Helpers::randomPassword();
$teacher = new Teacher();
$teacher->setExpireAt(new DateTime("+12 months"));
$teacher->setCreatedAt(new DateTime("now"));
$teacher->setPassword($passwordEncoder->encodePassword($teacher,$passwd));
$teacher->setStatus(1);
$teacher->setSchool($school);
$teacher->setFirstName($t['first_name']);
$teacher->setLastName($t['last_name']);
$teacher->setUsername($t['username']);
$teacher->setRoles(["ROLE_GUEST"]);
$options=array();
foreach($t['subject'] as $key=>$value){
$options['subjects'][]=$key;
}
$options['agreement_1'] = $request->get('agreement_1',0);
$options['agreement_2'] = $request->get('agreement_2',0);
$options['agreement_3'] = $request->get('agreement_3',0);
$teacher->setOptions($options);
$manager->persist($teacher);
$manager->flush();
//wysłanie e-mail z hasłem
$message = (new Email())
->from('rejestracja@generatorklasowek.pl')
->subject('Rejestracja w Generatorze Klasówek')
->to($t['username'])
->html(
$this->renderView(
// templates/emails/registration.html.twig
'emails/registration.html.twig',
['teacher' => $teacher,'passwd'=> $passwd]
),
'text/html'
)
// you can remove the following code if you don't define a text version for your emails
->text(
$this->renderView(
// templates/emails/registration.txt.twig
'emails/registration.txt.twig',
['teacher' => $teacher,'passwd'=> $passwd]
),
'text/plain'
)
;
$mailer->send($message);
return $response = new JsonResponse(['success'=>1]);
}
else{
return $response = new JsonResponse(['errors'=>$errors,'success'=>0]);
}
// koniec weryfikacji
}
$attribute = $this->em->getRepository(Attribute::class)->find(1);
$subjects = $this->em->getRepository(AttributeValue::class)->findBy(array("attribute"=>$attribute));
return $this->render('/teacher/register.html.twig',
[
"subjects"=>$subjects,
"attribute"=>$attribute
]);
}
/**
* @Route("/dziekujemy-za-rejestracje", name="thank_you")
*
*/
public function thankyou(){
return $this->render('/teacher/thankyou.html.twig',
[
]);
}
/**
* @Route("/import-kodow", name="code_import")
* @IsGranted("ROLE_ADMIN")
*/
public function codeimport(Request $request){
if($request->get('codes') or $request->files->get('file')){
$em = $this->em->getManager();
if($request->get('codes')){
$rows = explode("\n",$request->get('codes'));
$header = explode(";",trim($rows[0]));
}
elseif($request->files->get('file')){
$file = $request->files->get('file');
$content = file_get_contents($file->getPathname());
$rows = explode("\n",$content);
$header = explode(";",trim($rows[0]));
}
$i=0;
foreach($rows as $row){
if($i>0){
$data = explode(";",$row);
$email = strtolower($data[0]);
$teacher = $this->getDoctrine()->getRepository(Teacher::class)->findOneBy(array("username"=>$email));
if($teacher instanceof Teacher){
$codes = array();
foreach($data as $key=>$value){
$codes[$header[$key]]=$value;
}
$teacher->setOption('codes',$codes);
$em->persist($teacher);
}
}
$i++;
}
$em->flush();
}
return $this->render('/teacher/codeimport.html.twig',
[
]);
}
}