Passed
Pull Request — master (#6127)
by Angel Fernando Quiroz
40:13 queued 28:46
created

XApiEventSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 20
rs 9.7666
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
use Chamilo\CoreBundle\Entity\TrackEAttempt;
8
use Chamilo\CoreBundle\Entity\TrackEExercise;
9
use Chamilo\CoreBundle\Event\CourseCreatedEvent;
10
use Chamilo\CoreBundle\Event\ExerciseEndedEvent;
11
use Chamilo\CoreBundle\Event\ExerciseQuestionAnsweredEvent;
12
use Chamilo\CoreBundle\Event\AbstractEvent;
13
use Chamilo\CoreBundle\Event\Events;
14
use Chamilo\CoreBundle\Event\LearningPathEndedEvent;
15
use Chamilo\CoreBundle\Event\LearningPathItemViewedEvent;
16
use Chamilo\CoreBundle\Event\PortfolioCommentEditedEvent;
17
use Chamilo\CoreBundle\Event\PortfolioCommentScoredEvent;
18
use Chamilo\CoreBundle\Event\PortfolioItemAddedEvent;
19
use Chamilo\CoreBundle\Event\PortfolioItemCommentedEvent;
20
use Chamilo\CoreBundle\Event\PortfolioItemDownloadedEvent;
21
use Chamilo\CoreBundle\Event\PortfolioItemEditedEvent;
22
use Chamilo\CoreBundle\Event\PortfolioItemHighlightedEvent;
23
use Chamilo\CoreBundle\Event\PortfolioItemScoredEvent;
24
use Chamilo\CoreBundle\Event\PortfolioItemViewedEvent;
25
use Chamilo\CourseBundle\Entity\CLp;
26
use Chamilo\CourseBundle\Entity\CLpItem;
27
use Chamilo\CourseBundle\Entity\CLpItemView;
28
use Chamilo\CourseBundle\Entity\CLpView;
29
use Chamilo\CourseBundle\Entity\CQuiz;
30
use Chamilo\CourseBundle\Entity\CQuizQuestion;
31
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\LearningPathCompleted;
32
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\LearningPathItemViewed;
33
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\PortfolioCommentEdited;
34
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\PortfolioCommentScored;
35
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\PortfolioDownloaded;
36
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\PortfolioItemCommented;
37
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\PortfolioItemScored;
38
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\PortfolioItemShared;
39
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\PortfolioItemViewed;
40
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\QuizCompleted;
41
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\QuizQuestionAnswered;
42
use Doctrine\ORM\Exception\NotSupported;
43
use Doctrine\ORM\Exception\ORMException;
44
use Doctrine\ORM\OptimisticLockException;
45
use Doctrine\ORM\TransactionRequiredException;
46
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
47
48
class XApiEventSubscriber implements EventSubscriberInterface
49
{
50
    use XApiActivityTrait;
51
52
    protected XApiPlugin $plugin;
53
54
    public function __construct()
55
    {
56
        $this->plugin = XApiPlugin::create();
57
    }
58
59
    public static function getSubscribedEvents(): array
60
    {
61
        return [
62
            Events::COURSE_CREATED => 'onCreateCourse',
63
64
            Events::EXERCISE_QUESTION_ANSWERED => 'onExerciseQuestionAnswered',
65
            Events::EXERCISE_ENDED => 'onExerciseEnded',
66
67
            Events::LP_ITEM_VIEWED => 'onLpItemViewed',
68
            Events::LP_ENDED => 'onLpEnded',
69
70
            Events::PORTFOLIO_ITEM_ADDED => 'onPortfolioItemAdded',
71
            Events::PORTFOLIO_ITEM_EDITED => 'onPortfolioItemEdited',
72
            Events::PORTFOLIO_ITEM_VIEWED => 'onPortfolioItemViewed',
73
            Events::PORTFOLIO_ITEM_COMMENTED => 'onPortfolioItemCommented',
74
            Events::PORTFOLIO_ITEM_HIGHLIGHTED => 'onPortfolioItemHighlighted',
75
            Events::PORTFOLIO_DOWNLOADED => 'onPortfolioItemDownloaded',
76
            Events::PORTFOLIO_ITEM_SCORED => 'onPortfolioItemScored',
77
            Events::PORTFOLIO_COMMENT_SCORED => 'onPortfolioCommentScored',
78
            Events::PORTFOLIO_COMMENT_EDITED => 'onPortfolioCommentEdited',
79
        ];
80
    }
81
82
    public function onCreateCourse(CourseCreatedEvent $event): void
83
    {
84
        if (!$this->plugin->isEnabled(true)) {
85
            return;
86
        }
87
88
        $course = $event->getCourse();
89
90
        if (AbstractEvent::TYPE_POST === $event->getType() && $course) {
91
            $this->plugin->addCourseToolForTinCan($course->getId());
92
        }
93
    }
94
95
    /**
96
     * @throws ORMException
97
     * @throws OptimisticLockException
98
     * @throws NotSupported
99
     * @throws TransactionRequiredException
100
     */
101
    public function onExerciseQuestionAnswered(ExerciseQuestionAnsweredEvent $event): void
102
    {
103
        if (!$this->plugin->isEnabled(true)
104
            || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_QUIZ_QUESTION_ACTIVE)
105
        ) {
106
            return;
107
        }
108
109
        $em = Database::getManager();
110
        $attemptRepo = $em->getRepository(TrackEAttempt::class);
111
112
        $exe = $em->find(TrackEExercise::class, $event->getTrackingExeId());
113
        $question = $em->find(CQuizQuestion::class, $event->getQuestionId());
114
        $attempt = $attemptRepo->findOneBy(
115
            [
116
                'exeId' => $exe->getExeId(),
117
                'questionId' => $question->getId(),
118
            ]
119
        );
120
        $quiz = $em->find(CQuiz::class, $event->getExerciseId());
121
122
        $quizQuestionAnswered = new QuizQuestionAnswered($attempt, $question, $quiz);
123
124
        $statement = $quizQuestionAnswered->generate();
125
126
        $this->saveSharedStatement($statement);
127
    }
128
129
    /**
130
     * @throws OptimisticLockException
131
     * @throws ORMException
132
     * @throws TransactionRequiredException
133
     */
134
    public function onExerciseEnded(ExerciseEndedEvent $event): void
135
    {
136
        if (!$this->plugin->isEnabled(true)
137
            || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_QUIZ_ACTIVE)
138
        ) {
139
            return;
140
        }
141
142
        $em = Database::getManager();
143
144
        $exe = $em->find(TrackEExercise::class, $event->getTrackingExeId());
145
        $quiz = $em->find(CQuiz::class, $exe->getExeExoId());
146
147
        $quizCompleted = new QuizCompleted($exe, $quiz);
148
149
        $statement = $quizCompleted->generate();
150
151
        $this->saveSharedStatement($statement);
152
    }
153
154
    /**
155
     * @throws ORMException
156
     * @throws OptimisticLockException
157
     * @throws TransactionRequiredException
158
     */
159
    public function onLpItemViewed(LearningPathItemViewedEvent $event): void
160
    {
161
        if (!$this->plugin->isEnabled(true)
162
            || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_LP_ITEM_ACTIVE)
163
        ) {
164
            return;
165
        }
166
167
        $em = Database::getManager();
168
169
        $lpItemView = $em->find(CLpItemView::class, $event->getItemViewId());
170
        $lpItem = $em->find(CLpItem::class, $lpItemView->getLpItemId());
171
172
        if ('quiz' == $lpItem->getItemType()) {
173
            return;
174
        }
175
176
        $lpView = $em->find(CLpView::class, $lpItemView->getLpViewId());
177
178
        $lpItemViewed = new LearningPathItemViewed($lpItemView, $lpItem, $lpView);
179
180
        $this->saveSharedStatement(
181
            $lpItemViewed->generate()
182
        );
183
    }
184
185
    /**
186
     * @throws OptimisticLockException
187
     * @throws ORMException
188
     * @throws TransactionRequiredException
189
     */
190
    public function onLpEnded(LearningPathEndedEvent $event): void
191
    {
192
        if (!$this->plugin->isEnabled(true)
193
            || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_LP_ACTIVE)
194
        ) {
195
            return;
196
        }
197
198
        $em = Database::getManager();
199
200
        $lpView = $em->find(CLpView::class, $event->getLpViewId());
201
        $lp = $em->find(CLp::class, $lpView->getLpId());
202
203
        $learningPathEnded = new LearningPathCompleted($lpView, $lp);
204
205
        $this->saveSharedStatement(
206
            $learningPathEnded->generate()
207
        );
208
    }
209
210
    /**
211
     * @throws OptimisticLockException
212
     * @throws ORMException
213
     */
214
    public function onPortfolioItemAdded(PortfolioItemAddedEvent $event): void
215
    {
216
        if (!$this->plugin->isEnabled(true)
217
            || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_PORTFOLIO_ACTIVE)
218
        ) {
219
            return;
220
        }
221
222
        $item = $event->getPortfolio();
223
224
        if (!$item) {
225
            return;
226
        }
227
228
        $statement = (new PortfolioItemShared($item))->generate();
229
230
        $this->saveSharedStatement($statement);
231
    }
232
233
    /**
234
     * @throws OptimisticLockException
235
     * @throws ORMException
236
     */
237
    public function onPortfolioItemEdited(PortfolioItemEditedEvent $event): void
238
    {
239
        if (!$this->plugin->isEnabled(true)
240
            || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_PORTFOLIO_ACTIVE)
241
        ) {
242
            return;
243
        }
244
245
        $item = $event->getPortfolio();
246
247
        if (!$item) {
248
            return;
249
        }
250
251
        $statement = (new PortfolioItemShared($item))->generate();
252
253
        $this->saveSharedStatement($statement);
254
    }
255
256
    /**
257
     * @throws OptimisticLockException
258
     * @throws ORMException
259
     */
260
    public function onPortfolioItemViewed(PortfolioItemViewedEvent $event): void
261
    {
262
        if (!$this->plugin->isEnabled(true)
263
            || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_PORTFOLIO_ACTIVE)
264
        ) {
265
            return;
266
        }
267
268
        $item = $event->getPortfolio();
269
270
        if (!$item) {
271
            return;
272
        }
273
274
        $statement = (new PortfolioItemViewed($item))->generate();
275
276
        $this->saveSharedStatement($statement);
277
    }
278
279
    /**
280
     * @throws OptimisticLockException
281
     * @throws ORMException
282
     */
283
    public function onPortfolioItemCommented(PortfolioItemCommentedEvent $event): void
284
    {
285
        if (!$this->plugin->isEnabled(true)
286
            || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_PORTFOLIO_ACTIVE)
287
        ) {
288
            return;
289
        }
290
291
        $comment = $event->getComment();
292
293
        if (!$comment) {
294
            return;
295
        }
296
297
        $portfolioItemCommented = new PortfolioItemCommented($comment);
298
299
        $statement = $portfolioItemCommented->generate();
300
301
        $this->saveSharedStatement($statement);
302
    }
303
304
    public function onPortfolioItemHighlighted(PortfolioItemHighlightedEvent $event): void
305
    {
306
        if (!$this->plugin->isEnabled(true)
307
            || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_PORTFOLIO_ACTIVE)
308
        ) {
309
            return;
310
        }
311
312
        $item = $event->getPortfolio();
313
314
        if (!$item) {
315
            return;
316
        }
317
318
        $statement = (new PortfolioItemHighlighted($item))->generate();
319
320
        $this->saveSharedStatement($statement);
321
    }
322
323
    /**
324
     * @throws OptimisticLockException
325
     * @throws ORMException
326
     */
327
    public function onPortfolioItemDownloaded(PortfolioItemDownloadedEvent $event): void
328
    {
329
        if (!$this->plugin->isEnabled(true)
330
            || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_PORTFOLIO_ACTIVE)
331
        ) {
332
            return;
333
        }
334
335
        $owner = $event->getOwner();
336
337
        if (!$owner) {
338
            return;
339
        }
340
341
        $statement = (new PortfolioDownloaded($owner))->generate();
342
343
        $this->saveSharedStatement($statement);
344
    }
345
346
    public function onPortfolioItemScored(PortfolioItemScoredEvent $event): void
347
    {
348
        if (!$this->plugin->isEnabled(true)
349
            || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_PORTFOLIO_ACTIVE)
350
        ) {
351
            return;
352
        }
353
354
        $item = $event->getPortfolio();
355
356
        if (!$item) {
357
            return;
358
        }
359
360
        $statement = (new PortfolioItemScored($item))->generate();
361
362
        $this->saveSharedStatement($statement);
363
    }
364
365
    /**
366
     * @throws OptimisticLockException
367
     * @throws ORMException
368
     */
369
    public function onPortfolioCommentScored(PortfolioCommentScoredEvent $event): void
370
    {
371
        if (!$this->plugin->isEnabled(true)
372
            || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_PORTFOLIO_ACTIVE)
373
        ) {
374
            return;
375
        }
376
377
        $comment = $event->getComment();
378
379
        if (!$comment) {
380
            return;
381
        }
382
383
        $statement = (new PortfolioCommentScored($comment))->generate();
384
385
        $this->saveSharedStatement($statement);
386
    }
387
388
    /**
389
     * @throws OptimisticLockException
390
     * @throws ORMException
391
     */
392
    public function onPortfolioCommentEdited(PortfolioCommentEditedEvent $event): void
393
    {
394
        if (!$this->plugin->isEnabled(true)
395
            || 'true' !== $this->plugin->get(XApiPlugin::SETTING_LRS_PORTFOLIO_ACTIVE)
396
        ) {
397
            return;
398
        }
399
400
        $comment = $event->getComment();
401
402
        if (!$comment) {
403
            return;
404
        }
405
406
        $statement = (new PortfolioCommentEdited($comment))->generate();
407
408
        $this->saveSharedStatement($statement);
409
    }
410
}
411