<?php
namespace App\Controller;
use App\Entity\Card;
use App\Service\LearningClass;
use App\Service\LibraryClass;
use App\Service\CardClass;
use App\Repository\CardRepository;
use App\Repository\CoreClassificationRepository;
use App\Repository\CoreLibraryPoolRepository;
use App\Repository\CoreLibraryRepository;
use App\Repository\PlanRepository;
use App\Repository\UserLibraryPoolRepository;
use App\Repository\UserLibraryRepository;
use App\Repository\UserLibrarySubscriptionRepository;
use App\Repository\UserPlanRepository;
use App\Repository\UserPropertiesRepository;
use App\Repository\VPoolcardsRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class LibraryController extends AbstractController
{
/**
* @Route("/library", name="app_library_overview", schemes={"https"})
*/
public function index(Request $request, UserLibraryRepository $rep, CoreLibraryRepository $repCoreLib, CoreLibraryPoolRepository $repCoreLibPool, UserLibraryRepository $repUserLib, UserPlanRepository $repUserPlan, PlanRepository $repPlan, CoreClassificationRepository $repCoreClassification)
{
$user = $this->get('security.token_storage')->getToken()->getUser();
$userId = $user->getId();
$libary = $rep->getUserLibrary($userId);
$planId = $repUserPlan->getUserPlanId($userId);
$maxPlanLibs = $repPlan->getQuantityLibrary($planId);
$cntUserLibs = $repUserLib->getUserLibraryCount($userId);
$corelibs = $repCoreLib->getCoreLibrary();
$choices = [];
foreach ($corelibs as $t) {
$choices[$t['name']] = $t['id'];
}
// Form new library
$libform = $this->createFormBuilder(null, array('attr' => array('class' => 'php-email-form', 'method' => 'post', 'action' => $this->generateUrl('app_library_overview'))))
->setAttribute('class', 'php-email-form')
->add('libname', TextType::class,[
'required' => true,
'label' => 'Bezeichnung',
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('libdesc', TextareaType::class,[
'required' => true,
'label' => 'Beschreibung',
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('corelib', ChoiceType::class,[
'choices' => $choices,
'label' => 'Themengebiet',
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('Speichern', SubmitType::class,[
])
->getForm();
// Form handleRequest new library
$libform->handleRequest($request);
if ($libform->isSubmitted() && $libform->isValid()) {
// Check, if library name for user exists
$a = $rep->checkLibraryNameExists($userId, $libform['libname']->getData());
if($a == 0) {
$service = new LibraryClass;
$em2 = $this->getDoctrine()->getManager();
$service->saveNewLibrary($user->getId(), $libform['corelib']->getData(), $libform['libname']->getData(), $libform['libdesc']->getData(), $em2, $repCoreLib, $repCoreLibPool, $repUserLib, $repCoreClassification);
}
return $this->redirect($this->generateUrl('app_library_overview'));
}
return $this->render('library/index.html.twig', [
'libary' => $libary,
'maxPlanLibs' => $maxPlanLibs,
'cntUserLibs' => $cntUserLibs,
'libform' => $libform->createView()
]);
}
/**
* @Route("/library/{libId}/pool", name="app_library_pool", schemes={"https"})
*/
public function library($libId, Request $request, UserLibraryPoolRepository $repPool, UserLibraryRepository $repLib, VPoolcardsRepository $view)
{
$user = $this->get('security.token_storage')->getToken()->getUser();
$libname = $repLib->getUserLibraryName($libId);
$pool = $repPool->getLibraryPools($libId);
$cardcount = $view->getCardCount();
$poolform = $this->createFormBuilder(null, array('attr' => array('class' => 'php-email-form', 'method' => 'post')))
->setAttribute('class', 'php-email-form')
->add('poolname', TextType::class,[
'required' => true,
'label' => 'Bezeichnung',
'attr' => array(
'class' => 'form-control text-name'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('pooldesc', TextareaType::class,[
'required' => true,
'label' => 'Beschreibung',
'attr' => array(
'rows' => '6',
'class' => 'form-control text-desc'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('mon', CheckboxType::class,[
'required' => false,
'label' => 'Mo',
'data' => false,
'attr' => array(
'class' => 'form-check-input text-mon'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('tue', CheckboxType::class,[
'required' => false,
'label' => 'Di',
'data' => false,
'attr' => array(
'class' => 'form-check-input text-tue'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('wed', CheckboxType::class,[
'required' => false,
'label' => 'Mi',
'data' => false,
'attr' => array(
'class' => 'form-check-input text-wed'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('thu', CheckboxType::class,[
'required' => false,
'label' => 'Do',
'attr' => array(
'class' => 'form-check-input text-thu'),
'data' => false,
'label_attr' => array(
'class' => 'form-label'),
])
->add('fri', CheckboxType::class,[
'required' => false,
'label' => 'Fr',
'data' => false,
'attr' => array(
'class' => 'form-check-input text-fri'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('sat', CheckboxType::class,[
'required' => false,
'label' => 'Sa',
'data' => false,
'attr' => array(
'class' => 'form-check-input text-sat'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('sun', CheckboxType::class,[
'required' => false,
'label' => 'So',
'data' => false,
'attr' => array(
'class' => 'form-check-input text-sun'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('Erstellen', SubmitType::class,[
])
->getForm();
$poolform->handleRequest($request);
if ($poolform->isSubmitted() && $poolform->isValid()) {
// Check, if library name for user exists
$a = $repPool->checkPoolNameExists($poolform['poolname']->getData(), $libId);
if($a == 0) {
$service = new LibraryClass;
$em2 = $this->getDoctrine()->getManager();
$service->saveNewPool($user->getId(), $libId, $poolform['poolname']->getData(), $poolform['pooldesc']->getData(), $poolform['mon']->getData(), $poolform['tue']->getData(), $poolform['wed']->getData(), $poolform['thu']->getData(), $poolform['fri']->getData(), $poolform['sat']->getData(), $poolform['sun']->getData(), $em2);
}
return $this->redirect($this->generateUrl('app_library_pool', array('libId' => $libId)));
}
return $this->render('library/library.html.twig', [
'poolform' => $poolform->createView(),
'pool' => $pool,
'libname' => $libname,
'cardcount' => $cardcount,
'libId' => $libId,
]);
}
/**
* @Route("/library/{libId}/pool/{poolId}", name="app_pool", schemes={"https"})
*/
public function pool($libId, $poolId, Request $request, UserLibraryPoolRepository $repPool, UserLibraryRepository $repLib, CardRepository $repCards, UserLibrarySubscriptionRepository $repSubs, UserPlanRepository $repUserPlan, PlanRepository $repPlan)
{
$user = $this->get('security.token_storage')->getToken()->getUser();
$userId = $user->getId();
$planId = $repUserPlan->getUserPlanId($userId);
$showsolution = 1;
$poolname = $repPool->getUserLibraryPoolName($poolId);
$pooltype = $repPool->getUserLibraryPoolType($poolId);
$libname = $repLib->getUserLibraryName($libId);
$cards = $repCards->getCards($poolId);
$cardscount = $repCards->getPoolCardsCount($poolId);
$maxPlanCards = $repPlan->getQuantityCard($planId);
$cntUserCards = $repCards->getUserCardsCount($userId);
$classifications = $repPool->getLibraryPoolClassifications($libId);
$choices = [];
foreach ($classifications as $t) {
$choices[$t['name']] = $t['id'];
}
if($pooltype != 0) {
$showsolution = 0;
}
$cardform = $this->createFormBuilder(null, array('attr' => array('class' => 'php-email-form', 'method' => 'post')))
->setAttribute('class', 'php-email-form')
->add('task', TextType::class,[
'required' => true,
'label' => 'Wort/Phrase',
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('solution', TextareaType::class,[
'required' => true,
'label' => 'Lösung',
'attr' => array(
'class' => 'form-control',
'rows' => '7'
),
'label_attr' => array(
'class' => 'form-label'),
])
->add('classification', ChoiceType::class,[
'choices' => $choices,
'label' => 'Einordnung',
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('Erstellen', SubmitType::class,[
])
->getForm();
$cardform->handleRequest($request);
if ($cardform->isSubmitted() && $cardform->isValid()) {
$userCard = new Card();
$userCard->setUserId($userId);
$userCard->setPoolId($poolId);
$userCard->setReferenceId(0);
$userCard->setReferenceVersion(0);
$userCard->setCopyId(0);
$userCard->setCopyVersion(0);
$userCard->setTask($cardform['task']->getData());
$userCard->setSolution($cardform['solution']->getData());
$userCard->setClassificationId($cardform['classification']->getData());
$userCard->setVersion(1);
$userCard->setCorrect(0);
$userCard->setCorrect2(0);
$userCard->setSkilled(0);
$userCard->setDelflag(0);
$userCard->setCreatedDate(new \DateTime());
$userCard->setModifyDate(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($userCard);
$em->flush();
/*
$service = new CardClass;
$service->initialCopyCard(2, 2, 1, $em, $repPool, $repCards);
*/
// Subscription durchlaufen
$subsData = $repSubs->getUserSubscriptions($userId, $libId);
foreach ($subsData as $d) {
$service = new CardClass;
$em2 = $this->getDoctrine()->getManager();
$service->copyCard($d['user_id'], $d['user_library_id'], $libId, $cardform['task']->getData(), $cardform['solution']->getData(), $cardform['classification']->getData(), $em2, $repLib, $repPool, $repCards);
}
return $this->redirect($this->generateUrl('app_pool', array('libId' => $libId, 'poolId' => $poolId)));
}
return $this->render('library/pool.html.twig', [
'cardform' => $cardform->createView(),
'libId' => $libId,
'poolId' => $poolId,
'poolname' => $poolname,
'pooltype' => $pooltype,
'libname' => $libname,
'cards' => $cards,
'showsolution' => $showsolution,
'cardscount' => $cardscount,
'action' => null,
'cardId' => null,
'maxPlanCards' => $maxPlanCards,
'cntUserCards' => $cntUserCards,
]);
}
/**
* @Route("/library/{libId}/pool/{poolId}/{action}", name="app_pool2", schemes={"https"})
*/
public function pool2($libId, $poolId, $action, UserLibraryPoolRepository $repPool, UserLibraryRepository $repLib, CardRepository $repCards, UserPropertiesRepository $repProps, EntityManagerInterface $em)
{
$user = $this->get('security.token_storage')->getToken()->getUser();
$userId = $user->getId();
$showsolution = 1;
$randomValue = "";
$poolname = $repPool->getUserLibraryPoolName($poolId);
$pooltype = $repPool->getUserLibraryPoolType($poolId);
$libname = $repLib->getUserLibraryName($libId);
$classifications = $repPool->getLibraryPoolClassifications($libId);
$choices = [];
foreach ($classifications as $t) {
$choices[$t['name']] = $t['id'];
}
if($pooltype != 0) {
$showsolution = 0;
}
if($action == "show") {
$showsolution = 1;
}
if($action == "hide") {
$showsolution = 0;
}
if($action == "refresh") {
$service = new LearningClass;
$em2 = $this->getDoctrine()->getManager();
$service->refreshPool($poolId, $libId, $userId, $em2, $repPool);
$action = "fill";
}
if($action == "fill") {
$fuserId = $user->getId();
$fpoolId = $repPool->getUserLibraryPoolIdByType($fuserId, $libId, 0);
$fpoolId1 = $repPool->getUserLibraryPoolIdByType($fuserId, $libId, 1);
$fcards = $repCards->getCardsId($fpoolId);
$fcardscount = $repCards->getPoolCardsCount($fpoolId1);
$value1 = $repProps->getPoolType1Value($fuserId);
$diff = $value1 - $fcardscount;
if($diff > 0) {
shuffle($fcards);
$randomValue = array_rand($fcards, $diff);
if($diff > 1) {
for($i=0; $i < $diff; $i++) {
$articleClass = 'App\Entity\Card';
$card = $em->getReference($articleClass, $fcards[$randomValue[$i]]);
$card->setPoolId($fpoolId1);
$card->setModifyDate(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($card);
$em->flush();
}
}
else {
$articleClass = 'App\Entity\Card';
$card = $em->getReference($articleClass, $fcards[$randomValue]);
$card->setPoolId($fpoolId1);
$card->setModifyDate(new \DateTime());
$em = $this->getDoctrine()->getManager();
$em->persist($card);
$em->flush();
}
return $this->redirect($this->generateUrl('app_pool', array('libId' => $libId, 'poolId' => $poolId)));
}
}
if($action == "learn") {
$service = new LearningClass;
$em = $this->getDoctrine()->getManager();
$service->resetPool($poolId, $em);
$cards = $repCards->getLearningCards($poolId);
}
if($action == "continue") {
$action = "learn";
}
$cardform = $this->createFormBuilder(null, array('attr' => array('class' => 'php-email-form', 'method' => 'post', 'action' => $this->generateUrl('app_library_overview'))))
->setAttribute('class', 'php-email-form')
->add('task', TextType::class,[
'required' => true,
'label' => 'Wort/Phrase',
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('solution', TextareaType::class,[
'required' => true,
'label' => 'Lösung',
'attr' => array(
'class' => 'form-control',
'rows' => '7'
),
'label_attr' => array(
'class' => 'form-label'),
])
->add('classification', ChoiceType::class,[
'choices' => $choices,
'label' => 'Einordnung',
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('Erstellen', SubmitType::class,[
])
->getForm();
$cards = $repCards->getCards($poolId);
shuffle($cards);
$cardscount = $repCards->getPoolCardsCount($poolId);
if($action == "fill" || $action == "show" || $action == "hide") {
return $this->render('library/pool.html.twig', [
'cardform' => $cardform->createView(),
'libId' => $libId,
'poolId' => $poolId,
'poolname' => $poolname,
'pooltype' => $pooltype,
'libname' => $libname,
'cards' => $cards,
'showsolution' => $showsolution,
'cardscount' => $cardscount,
'action' => $action,
'cardId' => null,
]);
}
if($action == "learn" || $action == "continue") {
return $this->render('library/learning.html.twig', [
'libId' => $libId,
'poolId' => $poolId,
'poolname' => $poolname,
'pooltype' => $pooltype,
'libname' => $libname,
'cards' => $cards,
'showsolution' => $showsolution,
'cardscount' => $cardscount,
'action' => $action,
'cardId' => null,
]);
}
}
/**
* @Route("/library/{libId}/pool/{poolId}/{action}/{cardId}", name="app_pool3", schemes={"https"})
*/
public function pool3($libId, $poolId, $action, $cardId, UserLibraryPoolRepository $repPool, UserLibraryRepository $repLib, CardRepository $repCards, UserPropertiesRepository $repProps, EntityManagerInterface $em, Request $request)
{
$showsolution = 1;
$cardform = null;
$poolname = $repPool->getUserLibraryPoolName($poolId);
$pooltype = $repPool->getUserLibraryPoolType($poolId);
$libname = $repLib->getUserLibraryName($libId);
$classifications = $repPool->getLibraryPoolClassifications($libId);
$choices = [];
foreach ($classifications as $t) {
$choices[$t['name']] = $t['id'];
}
$pool = $repPool->getLibraryPools($libId);
$choices2 = [];
foreach ($pool as $t) {
$choices2[$t['name']] = $t['id'];
}
if($pooltype != 0) {
$showsolution = 0;
}
if($action == "true") {
$cardClass = 'App\Entity\Card';
$card = $em->getReference($cardClass, $cardId);
$card->setCorrect(1);
$card->setCorrect2(1);
$em = $this->getDoctrine()->getManager();
$em->persist($card);
$em->flush();
return $this->redirect($this->generateUrl('app_pool2', array('libId' => $libId, 'poolId' => $poolId, 'action' => "continue")));
}
if($action == "false") {
$cardClass = 'App\Entity\Card';
$card = $em->getReference($cardClass, $cardId);
$card->setCorrect(2);
$card->setCorrect2(2);
$em = $this->getDoctrine()->getManager();
$em->persist($card);
$em->flush();
return $this->redirect($this->generateUrl('app_pool2', array('libId' => $libId, 'poolId' => $poolId, 'action' => "continue")));
}
$cardform = $this->createFormBuilder(null, array('attr' => array('class' => 'php-email-form', 'method' => 'post')))
->setAttribute('class', 'php-email-form')
->add('task', TextType::class,[
'required' => true,
'label' => 'Wort/Phrase',
'data' => $repCards->getTask($cardId),
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('solution', TextareaType::class,[
'required' => true,
'label' => 'Lösung',
'data' => $repCards->getSolution($cardId),
'attr' => array(
'class' => 'form-control',
'rows' => '7'
),
'label_attr' => array(
'class' => 'form-label'),
])
->add('classification', ChoiceType::class,[
'choices' => $choices,
'label' => 'Einordnung',
'data' => $repCards->getClassificationId($cardId),
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('pool', ChoiceType::class,[
'choices' => $choices2,
'label' => 'Pool',
'data' => $repCards->getPoolId($cardId),
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('Speichern', SubmitType::class,[
'label' => 'Ändern',
])
->getForm();
$cardform->handleRequest($request);
if ($cardform->isSubmitted() && $cardform->isValid()) {
$cardClass = 'App\Entity\Card';
$card = $em->getReference($cardClass, $cardId);
$version = $card->getVersion();
$version = $version + 1;
$card->setVersion($version);
$card->setTask($cardform['task']->getData());
$card->setSolution($cardform['solution']->getData());
$card->setClassificationId($cardform['classification']->getData());
$card->setPoolId($cardform['pool']->getData());
$em = $this->getDoctrine()->getManager();
$em->persist($card);
$em->flush();
return $this->redirect($this->generateUrl('app_pool', array('libId' => $libId, 'poolId' => $poolId)));
}
$delcardform = $this->createFormBuilder(null, array('attr' => array('class' => 'php-email-form', 'method' => 'post')))
->setAttribute('class', 'php-email-form')
->add('task', TextType::class,[
'required' => true,
'label' => 'Wort/Phrase',
'data' => $repCards->getTask($cardId),
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('solution', TextareaType::class,[
'required' => true,
'label' => 'Lösung',
'data' => $repCards->getSolution($cardId),
'attr' => array(
'class' => 'form-control',
'rows' => '7'
),
'label_attr' => array(
'class' => 'form-label'),
])
->add('classification', ChoiceType::class,[
'choices' => $choices,
'label' => 'Einordnung',
'data' => $repCards->getClassificationId($cardId),
'attr' => array(
'class' => 'form-control'),
'label_attr' => array(
'class' => 'form-label'),
])
->add('DelButton', SubmitType::class,[
'label' => 'Löschen',
])
->getForm();
$delcardform->handleRequest($request);
if ($delcardform->isSubmitted() && $delcardform->isValid()) {
$cardClass = 'App\Entity\Card';
$card = $em->getReference($cardClass, $cardId);
$card->setTask($delcardform['task']->getData());
$card->setSolution($delcardform['solution']->getData());
$card->setClassificationId($delcardform['classification']->getData());
$card->setDelflag("1");
$em = $this->getDoctrine()->getManager();
$em->persist($card);
$em->flush();
return $this->redirect($this->generateUrl('app_pool', array('libId' => $libId, 'poolId' => $poolId)));
}
$cards = $repCards->getCards($poolId);
$cardscount = $repCards->getPoolCardsCount($poolId);
if($action != "learnshow") {
return $this->render('library/pool2.html.twig', [
'cardform' => $cardform->createView(),
'delcardform' => $delcardform->createView(),
'libId' => $libId,
'poolId' => $poolId,
'poolname' => $poolname,
'pooltype' => $pooltype,
'libname' => $libname,
'cards' => $cards,
'showsolution' => $showsolution,
'cardscount' => $cardscount,
'action' => $action,
'cardId' => $cardId,
]);
} else {
return $this->render('library/learning.html.twig', [
'cardform' => null,
'delcardform' => null,
'libId' => $libId,
'poolId' => $poolId,
'poolname' => $poolname,
'pooltype' => $pooltype,
'libname' => $libname,
'cards' => $cards,
'showsolution' => $showsolution,
'cardscount' => $cardscount,
'action' => $action,
'cardId' => $cardId,
]);
}
}
}