Passed
Pull Request — master (#6127)
by Angel Fernando Quiroz
09:20
created

XApiEventSubscriber::onExerciseQuestionAnswered()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 20
rs 9.9
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
        $plugin = XApiPlugin::create();
85
86
        if (AbstractEvent::TYPE_POST === $event->getType()) {
87
            $plugin->addCourseToolForTinCan($event->getCourseInfo()['id']);
88
        }
89
    }
90
91
    /**
92
     * @throws ORMException
93
     * @throws OptimisticLockException
94
     * @throws NotSupported
95
     * @throws TransactionRequiredException
96
     */
97
    public function onExerciseQuestionAnswered(ExerciseQuestionAnsweredEvent $event): void
98
    {
99
        $em = Database::getManager();
100
        $attemptRepo = $em->getRepository(TrackEAttempt::class);
101
102
        $exe = $em->find(TrackEExercise::class, $event->getTrackingExeId());
103
        $question = $em->find(CQuizQuestion::class, $event->getQuestionId());
104
        $attempt = $attemptRepo->findOneBy(
105
            [
106
                'exeId' => $exe->getExeId(),
107
                'questionId' => $question->getId(),
108
            ]
109
        );
110
        $quiz = $em->find(CQuiz::class, $event->getExerciseId());
111
112
        $quizQuestionAnswered = new QuizQuestionAnswered($attempt, $question, $quiz);
113
114
        $statement = $quizQuestionAnswered->generate();
115
116
        $this->saveSharedStatement($statement);
117
    }
118
119
    /**
120
     * @throws OptimisticLockException
121
     * @throws ORMException
122
     * @throws TransactionRequiredException
123
     */
124
    public function onExerciseEnded(ExerciseEndedEvent $event): void
125
    {
126
        $em = Database::getManager();
127
128
        $exe = $em->find(TrackEExercise::class, $event->getTrackingExeId());
129
        $quiz = $em->find(CQuiz::class, $exe->getExeExoId());
130
131
        $quizCompleted = new QuizCompleted($exe, $quiz);
132
133
        $statement = $quizCompleted->generate();
134
135
        $this->saveSharedStatement($statement);
136
    }
137
138
    /**
139
     * @throws ORMException
140
     * @throws OptimisticLockException
141
     * @throws TransactionRequiredException
142
     */
143
    public function onLpItemViewed(LearningPathItemViewedEvent $event): void
144
    {
145
        $em = Database::getManager();
146
147
        $lpItemView = $em->find(CLpItemView::class, $event->getItemViewId());
148
        $lpItem = $em->find(CLpItem::class, $lpItemView->getLpItemId());
149
150
        if ('quiz' == $lpItem->getItemType()) {
151
            return;
152
        }
153
154
        $lpView = $em->find(CLpView::class, $lpItemView->getLpViewId());
155
156
        $lpItemViewed = new LearningPathItemViewed($lpItemView, $lpItem, $lpView);
157
158
        $this->saveSharedStatement(
159
            $lpItemViewed->generate()
160
        );
161
    }
162
163
    /**
164
     * @throws OptimisticLockException
165
     * @throws ORMException
166
     * @throws TransactionRequiredException
167
     */
168
    public function onLpEnded(LearningPathEndedEvent $event): void
169
    {
170
        $em = Database::getManager();
171
172
        $lpView = $em->find(CLpView::class, $event->getLpViewId());
173
        $lp = $em->find(CLp::class, $lpView->getLpId());
174
175
        $learningPathEnded = new LearningPathCompleted($lpView, $lp);
176
177
        $this->saveSharedStatement(
178
            $learningPathEnded->generate()
179
        );
180
    }
181
182
    /**
183
     * @throws OptimisticLockException
184
     * @throws ORMException
185
     */
186
    public function onPortfolioItemAdded(PortfolioItemAddedEvent $event): void
187
    {
188
        $item = $event->getPortfolio();
189
190
        if (!$item) {
191
            return;
192
        }
193
194
        $statement = (new PortfolioItemShared($item))->generate();
195
196
        $this->saveSharedStatement($statement);
197
    }
198
199
    /**
200
     * @throws OptimisticLockException
201
     * @throws ORMException
202
     */
203
    public function onPortfolioItemEdited(PortfolioItemEditedEvent $event): void
204
    {
205
        $item = $event->getPortfolio();
206
207
        if (!$item) {
208
            return;
209
        }
210
211
        $statement = (new PortfolioItemShared($item))->generate();
212
213
        $this->saveSharedStatement($statement);
214
    }
215
216
    /**
217
     * @throws OptimisticLockException
218
     * @throws ORMException
219
     */
220
    public function onPortfolioItemViewed(PortfolioItemViewedEvent $event): void
221
    {
222
        $item = $event->getPortfolio();
223
224
        if (!$item) {
225
            return;
226
        }
227
228
        $statement = (new PortfolioItemViewed($item))->generate();
229
230
        $this->saveSharedStatement($statement);
231
    }
232
233
    /**
234
     * @throws OptimisticLockException
235
     * @throws ORMException
236
     */
237
    public function onPortfolioItemCommented(PortfolioItemCommentedEvent $event): void
238
    {
239
        $comment = $event->getComment();
240
241
        if (!$comment) {
242
            return;
243
        }
244
245
        $portfolioItemCommented = new PortfolioItemCommented($comment);
246
247
        $statement = $portfolioItemCommented->generate();
248
249
        $this->saveSharedStatement($statement);
250
    }
251
252
    public function onPortfolioItemHighlighted(PortfolioItemHighlightedEvent $event): void
253
    {
254
        $item = $event->getPortfolio();
255
256
        if (!$item) {
257
            return;
258
        }
259
260
        $statement = (new PortfolioItemHighlighted($item))->generate();
261
262
        $this->saveSharedStatement($statement);
263
    }
264
265
    /**
266
     * @throws OptimisticLockException
267
     * @throws ORMException
268
     */
269
    public function onPortfolioItemDownloaded(PortfolioItemDownloadedEvent $event): void
270
    {
271
        $owner = $event->getOwner();
272
273
        if (!$owner) {
274
            return;
275
        }
276
277
        $statement = (new PortfolioDownloaded($owner))->generate();
278
279
        $this->saveSharedStatement($statement);
280
    }
281
282
    public function onPortfolioItemScored(PortfolioItemScoredEvent $event): void
283
    {
284
        $item = $event->getPortfolio();
285
286
        if (!$item) {
287
            return;
288
        }
289
290
        $statement = (new PortfolioItemScored($item))->generate();
291
292
        $this->saveSharedStatement($statement);
293
    }
294
295
    /**
296
     * @throws OptimisticLockException
297
     * @throws ORMException
298
     */
299
    public function onPortfolioCommentScored(PortfolioCommentScoredEvent $event): void
300
    {
301
        $comment = $event->getComment();
302
303
        if (!$comment) {
304
            return;
305
        }
306
307
        $statement = (new PortfolioCommentScored($comment))->generate();
308
309
        $this->saveSharedStatement($statement);
310
    }
311
312
    /**
313
     * @throws OptimisticLockException
314
     * @throws ORMException
315
     */
316
    public function onPortfolioCommentEdited(PortfolioCommentEditedEvent $event): void
317
    {
318
        $comment = $event->getComment();
319
320
        if (!$comment) {
321
            return;
322
        }
323
324
        $statement = (new PortfolioCommentEdited($comment))->generate();
325
326
        $this->saveSharedStatement($statement);
327
    }
328
}
329