|
1
|
|
|
<?php |
|
2
|
|
|
namespace AppBundle\Services; |
|
3
|
|
|
|
|
4
|
|
|
use AppBundle\Entity\Comment; |
|
5
|
|
|
use AppBundle\Entity\ModuleUser; |
|
6
|
|
|
use AppBundle\Entity\PassModule; |
|
7
|
|
|
use AppBundle\Form\AnswerForPassType; |
|
8
|
|
|
use AppBundle\Form\CommentType; |
|
9
|
|
|
use AppBundle\Traits\GenerateOutput; |
|
10
|
|
|
use Faker\Provider\cs_CZ\DateTime; |
|
11
|
|
|
use Knp\Component\Pager\PaginatorInterface; |
|
12
|
|
|
use Proxies\__CG__\AppBundle\Entity\Module; |
|
13
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
|
14
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
15
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface; |
|
19
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
20
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
21
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
22
|
|
|
|
|
23
|
|
|
class PassManager |
|
24
|
|
|
{ |
|
25
|
|
|
use GenerateOutput; |
|
26
|
|
|
|
|
27
|
|
|
protected $doctrine; |
|
28
|
|
|
protected $formFactory; |
|
29
|
|
|
protected $router; |
|
30
|
|
|
protected $tokenStorage; |
|
31
|
|
|
protected $deactivation; |
|
32
|
|
|
|
|
33
|
2 |
|
public function __construct(RegistryInterface $doctrine, |
|
34
|
|
|
FormFactoryInterface $formFactory, |
|
35
|
|
|
TokenStorageInterface $tokenStorage, |
|
36
|
|
|
DeactivationPassModule $deactivationPassModule |
|
37
|
|
|
) |
|
38
|
|
|
{ |
|
39
|
2 |
|
$this->doctrine = $doctrine; |
|
40
|
2 |
|
$this->formFactory = $formFactory; |
|
41
|
2 |
|
$this->tokenStorage = $tokenStorage; |
|
42
|
2 |
|
$this->deactivation = $deactivationPassModule; |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function identPass($idModule) |
|
46
|
|
|
{ |
|
47
|
1 |
|
$user = $this->getUser(); |
|
48
|
|
|
|
|
49
|
1 |
|
$moduleUser = $this->doctrine->getRepository('AppBundle:ModuleUser') |
|
50
|
1 |
|
->findOneBy([ |
|
51
|
1 |
|
'user' => $user->getId(), |
|
52
|
|
|
'module' => $idModule |
|
53
|
1 |
|
]); |
|
54
|
|
|
|
|
55
|
1 |
|
if (null === $moduleUser) { |
|
56
|
|
|
return $this->generateOutput('error', 403, 'You do not have access to this module'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
if ($moduleUser->getAttempts() == $moduleUser->getModule()->getAttempts()) |
|
60
|
1 |
|
return $this->generateOutput('error', 403, 'Your attempts are exhausted'); |
|
61
|
|
|
|
|
62
|
1 |
|
$lastPass = $moduleUser->getPassModules()->last(); |
|
63
|
|
|
|
|
64
|
1 |
|
if (0 == $moduleUser->getCountPassModules() || !$lastPass->getIsActive()) { |
|
65
|
|
|
$newPassModule = $this->createPassModule($moduleUser); |
|
66
|
|
|
return $this->generateOutput('redirect_to_pass', 301, $newPassModule->getId()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
$time_residue = $this->checkDatePass($lastPass); |
|
70
|
|
|
|
|
71
|
1 |
|
if (!$time_residue) |
|
72
|
1 |
|
$this->identPass($idModule); |
|
73
|
|
|
|
|
74
|
1 |
|
return $this->generateOutput('redirect_to_pass', 301, $lastPass->getId()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
2 |
|
public function checkDatePass(PassModule $pass) |
|
79
|
|
|
{ |
|
80
|
2 |
|
$nowDate = new \DateTime(); |
|
81
|
2 |
|
$dateEstimate = $pass->getTimeStart() |
|
82
|
2 |
|
->modify("+{$pass->getTimePeriod()} minutes"); |
|
83
|
|
|
|
|
84
|
2 |
|
if($nowDate > $dateEstimate){ |
|
85
|
|
|
$this->deactivation->deactivation($pass); |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
2 |
|
$interval = date_diff($nowDate, $dateEstimate, true); |
|
89
|
|
|
|
|
90
|
2 |
|
return $interval->format('%I:%S'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function createPassModule(ModuleUser $moduleUser) |
|
94
|
|
|
{ |
|
95
|
|
|
$passModule = $this->createPassModuleInstance(); |
|
96
|
|
|
$passModule->setModuleUser($moduleUser); |
|
97
|
|
|
$passModule->setTimePeriod($moduleUser->getModule()->getTime()); |
|
98
|
|
|
|
|
99
|
|
|
$this->doctrine->getEntityManager()->persist($passModule); |
|
100
|
|
|
$this->doctrine->getEntityManager()->flush(); |
|
101
|
|
|
return $passModule; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function createPassModuleInstance() |
|
105
|
|
|
{ |
|
106
|
|
|
return new PassModule(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
2 |
|
protected function getUser() |
|
110
|
|
|
{ |
|
111
|
2 |
|
if (null === $token = $this->tokenStorage->getToken()) { |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
2 |
|
if (!is_object($user = $token->getUser())) { |
|
116
|
|
|
// e.g. anonymous authentication |
|
117
|
|
|
return; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
2 |
|
return $user; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
public function passModule($idPassModule) |
|
124
|
|
|
{ |
|
125
|
1 |
|
$user = $this->getUser(); |
|
126
|
|
|
|
|
127
|
1 |
|
$passModule = $this->doctrine->getRepository('AppBundle:PassModule') |
|
128
|
1 |
|
->getModuleByIdAndUser($idPassModule, $user->getId()); |
|
129
|
|
|
|
|
130
|
1 |
|
if (null === $passModule) { |
|
131
|
|
|
return $this->generateOutput('error', 403, 'You do not have access to this pass'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
if (null === $passModule->getCurrentQuestion()) { |
|
135
|
|
|
$firstQuestionForPass = $this->doctrine->getRepository('AppBundle:Question') |
|
136
|
|
|
->getFirstQuestionForPass($passModule->getId()); |
|
137
|
|
|
|
|
138
|
|
|
if(null === $firstQuestionForPass) |
|
139
|
|
|
return $this->generateOutput('error', 500, 'This module does not have any questions ;('); |
|
140
|
|
|
|
|
141
|
|
|
$passModule->setCurrentQuestion($firstQuestionForPass); |
|
142
|
|
|
$this->doctrine->getEntityManager()->flush(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
if(!($passModule->getIsActive())){ |
|
146
|
|
|
return $this->generateOutput('error', 403, 'This pass is not active ;('); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
1 |
|
$time_residue = $this->checkDatePass($passModule); |
|
150
|
|
|
|
|
151
|
1 |
|
if (!$time_residue) |
|
152
|
1 |
|
return $this->generateOutput('error', 403, 'This pass is overdue ;('); |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
1 |
|
$currentQuestion = $passModule->getCurrentQuestion(); |
|
156
|
1 |
|
$form = $this->formFactory->create(AnswerForPassType::class, null, [ |
|
157
|
1 |
|
'method' => Request::METHOD_POST, |
|
158
|
1 |
|
'idQuestion' => $currentQuestion->getId(), |
|
159
|
1 |
|
'idPassModule' => $idPassModule, |
|
160
|
1 |
|
'answers' => $currentQuestion->getAnswers()->toArray() |
|
161
|
1 |
|
]); |
|
162
|
1 |
|
$form->add('submit', SubmitType::class, ['label' => 'Save', 'attr' => [ 'class' => 'btn btn-primary' ]]); |
|
163
|
|
|
|
|
164
|
1 |
|
$currentNumberQuestion = $passModule->getCountPassedQuestions()+1; |
|
165
|
|
|
|
|
166
|
1 |
|
return $this->generateOutput('ok', 200, [ |
|
167
|
1 |
|
$form, |
|
168
|
1 |
|
$currentQuestion, |
|
169
|
1 |
|
$time_residue, |
|
170
|
1 |
|
$passModule->getModuleUser()->getModule()->getCountQuestions(), |
|
171
|
|
|
$currentNumberQuestion |
|
172
|
1 |
|
]); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
} |