Passed
Pull Request — master (#6127)
by Angel Fernando Quiroz
07:52
created

onPortfolioItemEdited()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 27
nc 5
nop 1
dl 0
loc 48
rs 8.8657
c 1
b 0
f 0
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
    private ExternalNotificationConnectPlugin $plugin;
23
24
    public function __construct()
25
    {
26
        $this->plugin = ExternalNotificationConnectPlugin::create();
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    public static function getSubscribedEvents(): array
33
    {
34
        return [
35
            Events::PORTFOLIO_ITEM_ADDED => 'onPortfolioItemAdded',
36
            Events::PORTFOLIO_ITEM_EDITED => 'onPortfolioItemEdited',
37
            Events::PORTOFLIO_ITEM_DELETED => 'onPortfolioItemDeleted',
38
            Events::PORTFOLIO_ITEM_VISIBILITY_CHANGED => 'onPortfolioItemVisibility',
39
40
            Events::LP_CREATED => 'onLpCreated',
41
        ];
42
    }
43
44
    public function onPortfolioItemAdded(PortfolioItemAddedEvent $event): void
45
    {
46
        if (!$this->plugin->isEnabled(true)
47
            || 'true' !== $this->plugin->get(ExternalNotificationConnectPlugin::SETTING_NOTIFY_PORTFOLIO)
48
        ) {
49
            return;
50
        }
51
52
        $item = $event->getPortfolio();
53
54
        if (!$item) {
55
            return;
56
        }
57
58
        $url = Container::getRouter()
59
            ->generate(
60
                'legacy_main',
61
                [
62
                    'name' => 'portfolio/index.php?',
63
                    'action' => 'view',
64
                    'id' => $item->getId(),
65
                ]
66
            );
67
68
        try {
69
            $json = $this->doCreateRequest(
70
                [
71
                    'user_id' => api_get_user_entity()->getId(),
72
                    'course_code' => api_get_course_entity()->getCode(),
73
                    'content_id' => $item->getId(),
74
                    'content_type' => 'eportfolio',
75
                    'content_url' => $url.'&'.api_get_cidreq(),
76
                    'post_title' => $item->getTitle(),
77
                ]
78
            );
79
        } catch (Exception $e) {
80
            Display::addFlash(
81
                Display::return_message($e->getMessage(), 'error')
82
            );
83
84
            return;
85
        }
86
87
        if (empty($json)) {
88
            return;
89
        }
90
91
        error_log('ExtNotifConn: Portfolio item created: ID '.$json['data']['notification_id']);
92
    }
93
94
    public function onPortfolioItemEdited(PortfolioItemEditedEvent $event): void
95
    {
96
        if (!$this->plugin->isEnabled(true)
97
            || 'true' !== $this->plugin->get(ExternalNotificationConnectPlugin::SETTING_NOTIFY_PORTFOLIO)
98
        ) {
99
            return;
100
        }
101
102
        $item = $event->getPortfolio();
103
104
        if (!$item) {
105
            return;
106
        }
107
108
        $url = Container::getRouter()
109
            ->generate(
110
                'legacy_main',
111
                [
112
                    'name' => 'portfolio/index.php?',
113
                    'action' => 'view',
114
                    'id' => $item->getId(),
115
                ]
116
            );
117
118
        try {
119
            $json = $this->doEditRequest(
120
                [
121
                    'user_id' => api_get_user_entity()->getId(),
122
                    'course_code' => api_get_course_entity()->getCode(),
123
                    'content_id' => $item->getId(),
124
                    'content_type' => 'eportfolio',
125
                    'content_url' => $url.'&'.api_get_cidreq(),
126
                    'post_title' => $item->getTitle(),
127
                ]
128
            );
129
        } catch (Exception $e) {
130
            Display::addFlash(
131
                Display::return_message($e->getMessage(), 'error')
132
            );
133
134
            return;
135
        }
136
137
        if (empty($json)) {
138
            return;
139
        }
140
141
        error_log('ExtNotifConn: Portfolio item edited. Status'.((int)$json['status']));
142
    }
143
144
    public function onPortfolioItemDeleted(PortfolioItemDeletedEvent $event): void
145
    {
146
        if (!$this->plugin->isEnabled(true)
147
            || 'true' !== $this->plugin->get(ExternalNotificationConnectPlugin::SETTING_NOTIFY_PORTFOLIO)
148
        ) {
149
            return;
150
        }
151
152
        $item = $event->getPortfolio();
153
154
        if (!$item) {
155
            return;
156
        }
157
158
        if (AbstractEvent::TYPE_PRE === $event->getType()) {
159
            try {
160
                $json = $this->doDeleteRequest($item->getId(), 'eportfolio');
161
            } catch (Exception $e) {
162
                Display::addFlash(
163
                    Display::return_message($e->getMessage(), 'error')
164
                );
165
166
                return;
167
            }
168
169
            if (empty($json)) {
170
                return;
171
            }
172
173
            error_log('ExtNotifConn: Portfolio item deleted: Status '.((int)$json['status']));
174
        }
175
    }
176
177
    public function onPortfolioItemVisibility(PortfolioItemVisibilityChangedEvent $event): void
178
    {
179
        if (!$this->plugin->isEnabled(true)
180
            || 'true' !== $this->plugin->get(ExternalNotificationConnectPlugin::SETTING_NOTIFY_PORTFOLIO)
181
        ) {
182
            return;
183
        }
184
185
        $item = $event->getPortfolio();
186
187
        if (!$item) {
188
            return;
189
        }
190
191
        $recipients = $event->getRecipientIdList();
192
193
        try {
194
            $json = $this->doVisibilityRequest(
195
                [
196
                    'content_id' => $item->getId(),
197
                    'content_type' => 'eportfolio',
198
                    'visibility' => (int) $item->isVisible(),
199
                    'user_list' => $recipients,
200
                ]
201
            );
202
        } catch (Exception $e) {
203
            Display::addFlash(
204
                Display::return_message($e->getMessage(), 'error')
205
            );
206
207
            return;
208
        }
209
210
        if (empty($json)) {
211
            return;
212
        }
213
214
        error_log('ExtNotifConn: Portfolio item visibility: ID '.$json['data']['notification_id']);
215
    }
216
217
    public function onLpCreated(LearningPathCreatedEvent $event): void
218
    {
219
        if (!$this->plugin->isEnabled(true)
220
            || 'true' !== $this->plugin->get(ExternalNotificationConnectPlugin::SETTING_NOTIFY_LEARNPATH)
221
        ) {
222
            return;
223
        }
224
225
        $lp = $event->getLp();
226
227
        if (!$lp) {
228
            return;
229
        }
230
231
        $url = Container::getRouter()
232
            ->generate(
233
                'legacy_main',
234
                [
235
                    'name' => 'lp/lp_controller.php',
236
                    'action' => 'view',
237
                    'lp_id' => $lp->getIid(),
238
                    'isStudentView' => 'true',
239
                ]
240
            );
241
242
        try {
243
            $json = $this->doCreateRequest(
244
                [
245
                    'user_id' => api_get_user_entity()->getId(),
246
                    'course_code' => api_get_course_entity()->getCode(),
247
                    'content_id' => $lp->getIid(),
248
                    'content_type' => 'lp',
249
                    'content_url' => $url.'&'.api_get_cidreq(),
250
                    'post_title' => $lp->getTitle(),
251
                ]
252
            );
253
        } catch (Exception $e) {
254
            Display::addFlash(
255
                Display::return_message($e->getMessage(), 'error')
256
            );
257
258
            return;
259
        }
260
261
        if (empty($json)) {
262
            return;
263
        }
264
265
        error_log('ExtNotifConn: Learning path created: ID '.$json['data']['notification_id']);
266
    }
267
}
268