Passed
Pull Request — 1.11.x (#4505)
by Angel Fernando Quiroz
16:24 queued 06:00
created

ExternalNotificationConnectPortfolioItemAddedHookObserver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 24
c 1
b 0
f 0
dl 0
loc 41
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A hookItemAdded() 0 37 4
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Entity\Portfolio;
6
use Chamilo\PluginBundle\ExternalNotificationConnect\Traits\RequestTrait\RequestTrait;
7
8
class ExternalNotificationConnectPortfolioItemAddedHookObserver extends ExternalNotificationConnectHookObserver implements HookPortfolioItemAddedObserverInterface
9
{
10
    use RequestTrait;
11
12
    public function hookItemAdded(HookPortfolioItemAddedEventInterface $hookEvent)
13
    {
14
        /** @var Portfolio $item */
15
        $item = $hookEvent->getEventData()['portfolio'];
16
        $userId = api_get_user_id();
17
        $courseCode = api_get_course_id();
18
19
        $cidreq = api_get_cidreq();
20
21
        $url = api_get_path(WEB_CODE_PATH).'portfolio/index.php?';
22
        $url .= ($cidreq ? $cidreq.'&' : '');
23
        $url .= http_build_query(['action' => 'view', 'id' => $item->getId()]);
24
25
        try {
26
            $json = $this->doCreateRequest(
27
                [
28
                    'user_id' => $userId,
29
                    'course_code' => $courseCode,
30
                    'content_id' => $item->getId(),
31
                    'content_type' => 'eportfolio',
32
                    'content_url' => $url,
33
                    'post_title' => $item->getTitle(),
34
                ]
35
            );
36
        } catch (Exception $e) {
37
            Display::addFlash(
38
                Display::return_message($e->getMessage(), 'error')
39
            );
40
41
            return;
42
        }
43
44
        if (empty($json)) {
45
            return;
46
        }
47
48
        error_log('ExtNotifConn: Portfolio item created: ID '.$json['data']['notification_id']);
49
    }
50
}
51