Passed
Pull Request — master (#6127)
by Angel Fernando Quiroz
08:01
created

onLpCreated()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 25
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 43
rs 9.52
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
use Chamilo\CoreBundle\Framework\Container;
8
use Chamilo\CoreBundle\Event\AbstractEvent;
9
use Chamilo\CoreBundle\Event\Events;
10
use Chamilo\CoreBundle\Event\LearningPathCreatedEvent;
11
use Chamilo\CoreBundle\Event\PortfolioItemAddedEvent;
12
use Chamilo\CoreBundle\Event\PortfolioItemDeletedEvent;
13
use Chamilo\CoreBundle\Event\PortfolioItemEditedEvent;
14
use Chamilo\CoreBundle\Event\PortfolioItemVisibilityChangedEvent;
15
use Chamilo\PluginBundle\ExternalNotificationConnect\Traits\RequestTrait\RequestTrait;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
18
class ExternalNotificationConnectEventSubscriber implements EventSubscriberInterface
19
{
20
    use RequestTrait;
21
22
    /**
23
     * @inheritDoc
24
     */
25
    public static function getSubscribedEvents(): array
26
    {
27
        return [
28
            Events::PORTFOLIO_ITEM_ADDED => 'onPortfolioItemAdded',
29
            Events::PORTFOLIO_ITEM_EDITED => 'onPortfolioItemEdited',
30
            Events::PORTOFLIO_ITEM_DELETED => 'onPortfolioItemDeleted',
31
            Events::PORTFOLIO_ITEM_VISIBILITY_CHANGED => 'onPortfolioItemVisibility',
32
33
            Events::LP_CREATED => 'onLpCreated',
34
        ];
35
    }
36
37
    public function onPortfolioItemAdded(PortfolioItemAddedEvent $event): void
38
    {
39
        $item = $event->getPortfolio();
40
41
        if (!$item) {
42
            return;
43
        }
44
45
        $url = Container::getRouter()
46
            ->generate(
47
                'legacy_main',
48
                [
49
                    'name' => 'portfolio/index.php?',
50
                    'action' => 'view',
51
                    'id' => $item->getId(),
52
                ]
53
            );
54
55
        try {
56
            $json = $this->doCreateRequest(
57
                [
58
                    'user_id' => api_get_user_entity()->getId(),
59
                    'course_code' => api_get_course_entity()->getCode(),
60
                    'content_id' => $item->getId(),
61
                    'content_type' => 'eportfolio',
62
                    'content_url' => $url.'&'.api_get_cidreq(),
63
                    'post_title' => $item->getTitle(),
64
                ]
65
            );
66
        } catch (Exception $e) {
67
            Display::addFlash(
68
                Display::return_message($e->getMessage(), 'error')
69
            );
70
71
            return;
72
        }
73
74
        if (empty($json)) {
75
            return;
76
        }
77
78
        error_log('ExtNotifConn: Portfolio item created: ID '.$json['data']['notification_id']);
79
    }
80
81
    public function onPortfolioItemEdited(PortfolioItemEditedEvent $event): void
82
    {
83
        $item = $event->getPortfolio();
84
85
        if (!$item) {
86
            return;
87
        }
88
89
        $url = Container::getRouter()
90
            ->generate(
91
                'legacy_main',
92
                [
93
                    'name' => 'portfolio/index.php?',
94
                    'action' => 'view',
95
                    'id' => $item->getId(),
96
                ]
97
            );
98
99
        try {
100
            $json = $this->doEditRequest(
101
                [
102
                    'user_id' => api_get_user_entity()->getId(),
103
                    'course_code' => api_get_course_entity()->getCode(),
104
                    'content_id' => $item->getId(),
105
                    'content_type' => 'eportfolio',
106
                    'content_url' => $url.'&'.api_get_cidreq(),
107
                    'post_title' => $item->getTitle(),
108
                ]
109
            );
110
        } catch (Exception $e) {
111
            Display::addFlash(
112
                Display::return_message($e->getMessage(), 'error')
113
            );
114
115
            return;
116
        }
117
118
        if (empty($json)) {
119
            return;
120
        }
121
122
        error_log('ExtNotifConn: Portfolio item edited. Status'.((int)$json['status']));
123
    }
124
125
    public function onPortfolioItemDeleted(PortfolioItemDeletedEvent $event): void
126
    {
127
        $item = $event->getPortfolio();
128
129
        if (!$item) {
130
            return;
131
        }
132
133
        if (AbstractEvent::TYPE_PRE === $event->getType()) {
134
            try {
135
                $json = $this->doDeleteRequest($item->getId(), 'eportfolio');
136
            } catch (Exception $e) {
137
                Display::addFlash(
138
                    Display::return_message($e->getMessage(), 'error')
139
                );
140
141
                return;
142
            }
143
144
            if (empty($json)) {
145
                return;
146
            }
147
148
            error_log('ExtNotifConn: Portfolio item deleted: Status '.((int)$json['status']));
149
        }
150
    }
151
152
    public function onPortfolioItemVisibility(PortfolioItemVisibilityChangedEvent $event): void
153
    {
154
        $item = $event->getPortfolio();
155
156
        if (!$item) {
157
            return;
158
        }
159
160
        $recipients = $event->getRecipientIdList();
161
162
        try {
163
            $json = $this->doVisibilityRequest(
164
                [
165
                    'content_id' => $item->getId(),
166
                    'content_type' => 'eportfolio',
167
                    'visibility' => (int) $item->isVisible(),
168
                    'user_list' => $recipients,
169
                ]
170
            );
171
        } catch (Exception $e) {
172
            Display::addFlash(
173
                Display::return_message($e->getMessage(), 'error')
174
            );
175
176
            return;
177
        }
178
179
        if (empty($json)) {
180
            return;
181
        }
182
183
        error_log('ExtNotifConn: Portfolio item visibility: ID '.$json['data']['notification_id']);
184
    }
185
186
    public function onLpCreated(LearningPathCreatedEvent $event): void
187
    {
188
        $lp = $event->getLp();
189
190
        if (!$lp) {
191
            return;
192
        }
193
194
        $url = Container::getRouter()
195
            ->generate(
196
                'legacy_main',
197
                [
198
                    'name' => 'lp/lp_controller.php',
199
                    'action' => 'view',
200
                    'lp_id' => $lp->getIid(),
201
                    'isStudentView' => 'true',
202
                ]
203
            );
204
205
        try {
206
            $json = $this->doCreateRequest(
207
                [
208
                    'user_id' => api_get_user_entity()->getId(),
209
                    'course_code' => api_get_course_entity()->getCode(),
210
                    'content_id' => $lp->getIid(),
211
                    'content_type' => 'lp',
212
                    'content_url' => $url.'&'.api_get_cidreq(),
213
                    'post_title' => $lp->getTitle(),
214
                ]
215
            );
216
        } catch (Exception $e) {
217
            Display::addFlash(
218
                Display::return_message($e->getMessage(), 'error')
219
            );
220
221
            return;
222
        }
223
224
        if (empty($json)) {
225
            return;
226
        }
227
228
        error_log('ExtNotifConn: Learning path created: ID '.$json['data']['notification_id']);
229
    }
230
}
231