-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathExerciseListener.php
More file actions
executable file
·196 lines (160 loc) · 7.14 KB
/
ExerciseListener.php
File metadata and controls
executable file
·196 lines (160 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace UJM\ExoBundle\Listener;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Claroline\CoreBundle\Event\CopyResourceEvent;
use Claroline\CoreBundle\Event\CreateFormResourceEvent;
use Claroline\CoreBundle\Event\CreateResourceEvent;
use Claroline\CoreBundle\Event\DeleteResourceEvent;
use Claroline\CoreBundle\Event\DisplayToolEvent;
use Claroline\CoreBundle\Event\OpenResourceEvent;
use UJM\ExoBundle\Entity\Exercise;
use UJM\ExoBundle\Entity\ExerciseQuestion;
use UJM\ExoBundle\Entity\Subscription;
use UJM\ExoBundle\Form\ExerciseType;
class ExerciseListener extends ContainerAware
{
public function onCreateForm(CreateFormResourceEvent $event)
{
$exercise = new Exercise();
$exercise->setNbQuestion(0);
$exercise->setDuration(0);
$exercise->setMaxAttempts(0);
$exercise->setStartDate(new \Datetime());
$exercise->setEndDate(new \Datetime());
$exercise->setDateCorrection(new \Datetime());
$form = $this->container->get('form.factory')
->create(new ExerciseType(), $exercise);
$twig = $this->container->get('templating');
$content = $twig->render(
'UJMExoBundle:Exercise:new.html.twig',
array(
'form' => $form->createView(),
'resourceType' => 'ujm_exercise'
)
);
$event->setResponseContent($content);
}
public function onCreate(CreateResourceEvent $event)
{
$request = $this->container->get('request');
$form = $this->container
->get('form.factory')
->create(new ExerciseType, new Exercise());
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->container->get('doctrine.orm.entity_manager');
$user = $this->container->get('security.token_storage')->getToken()->getUser();
$exercise = $form->getData();
$exercise->setName($exercise->getTitle());
$exercise->setDateCreate(new \Datetime());
$exercise->setNbQuestionPage(1);
$exercise->setPublished(FALSE);
$subscription = new Subscription($user, $exercise);
$subscription->setAdmin(true);
$subscription->setCreator(true);
$em->persist($exercise);
$em->persist($subscription);
$event->setResources(array($exercise));
$event->stopPropagation();
return;
}
$content = $this->container->get('templating')->render(
'UJMExoBundle:Exercise:new.html.twig',
array(
'resourceType' => 'ujm_exercise',
'form' => $form->createView()
)
);
$event->setErrorFormContent($content);
$event->stopPropagation();
}
public function onOpen(OpenResourceEvent $event)
{
//Redirection to the controller.
$route = $this->container
->get('router')
->generate('ujm_exercise_open', array('exerciseId' => $event->getResource()->getId()));
$event->setResponse(new RedirectResponse($route));
$event->stopPropagation();
}
public function onDelete(DeleteResourceEvent $event)
{
$em = $this->container->get('claroline.persistence.object_manager');
$papers = $em->getRepository('UJMExoBundle:Paper')
->findOneBy(array(
'exercise' => $event->getResource()->getId()
)
);
$eqs = $em->getRepository('UJMExoBundle:ExerciseQuestion')
->findBy(array(
'exercise' => $event->getResource()->getId()
)
);
foreach ($eqs as $eq) {
$em->remove($eq);
}
$subscriptions = $em->getRepository('UJMExoBundle:Subscription')
->findBy(array(
'exercise' => $event->getResource()->getId()
)
);
foreach ($subscriptions as $subscription) {
$em->remove($subscription);
}
$em->flush();
$em->remove($event->getResource());
$event->stopPropagation();
}
public function onDisplayDesktop(DisplayToolEvent $event)
{
$subRequest = $this->container->get('request')->duplicate(array(), null, array("_controller" => 'UJMExoBundle:Question:index'));
$response = $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
$event->setContent($response->getContent());
}
public function onCopy(CopyResourceEvent $event)
{
$em = $this->container->get('claroline.persistence.object_manager');
$resource = $event->getResource();
$exerciseToCopy = $em->getRepository('UJMExoBundle:Exercise')->find($resource->getId());
$listQuestionsExoToCopy = $em->getRepository('UJMExoBundle:ExerciseQuestion')
->findBy(array('exercise' => $exerciseToCopy->getId()));
$newExercise = new Exercise();
$newExercise->setName($resource->getName());
$newExercise->setTitle($exerciseToCopy->getTitle());
$newExercise->setDescription($exerciseToCopy->getDescription());
$newExercise->setShuffle($exerciseToCopy->getShuffle());
$newExercise->setNbQuestion($exerciseToCopy->getNbQuestion());
$newExercise->setDateCreate($exerciseToCopy->getDateCreate());
$newExercise->setDuration($exerciseToCopy->getDuration());
$newExercise->setNbQuestionPage($exerciseToCopy->getNbQuestionPage());
$newExercise->setDoprint($exerciseToCopy->getDoprint());
$newExercise->setMaxAttempts($exerciseToCopy->getMaxAttempts());
$newExercise->setCorrectionMode($exerciseToCopy->getCorrectionMode());
$newExercise->setDateCorrection($exerciseToCopy->getDateCorrection());
$newExercise->setMarkMode($exerciseToCopy->getMarkMode());
$newExercise->setStartDate($exerciseToCopy->getStartDate());
$newExercise->setUseDateEnd($exerciseToCopy->getUseDateEnd());
$newExercise->setEndDate($exerciseToCopy->getEndDate());
$newExercise->setDispButtonInterrupt($exerciseToCopy->getDispButtonInterrupt());
$newExercise->setLockAttempt($exerciseToCopy->getLockAttempt());
$newExercise->setPublished($exerciseToCopy->getPublished());
$em->persist($newExercise);
$em->flush();
foreach ($listQuestionsExoToCopy as $eq) {
$questionToAdd = $em->getRepository('UJMExoBundle:Question')->find($eq->getQuestion());;
$exerciseQuestion = new ExerciseQuestion($newExercise, $questionToAdd);
$exerciseQuestion->setOrdre($eq->getOrdre());
$em->persist($exerciseQuestion);
}
$user = $this->container->get('security.token_storage')->getToken()->getUser();
$subscription = new Subscription($user, $newExercise);
$subscription->setAdmin(true);
$subscription->setCreator(true);
$em->persist($subscription);
$em->flush();
$event->setCopy($newExercise);
$event->stopPropagation();
}
}