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

hookCreated()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 43
rs 9.52
cc 4
nc 6
nop 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\PluginBundle\ExternalNotificationConnect\Traits\RequestTrait\RequestTrait;
6
7
class ExternalNotificationConnectLearningPathCreatedHookObserver extends ExternalNotificationConnectHookObserver implements HookLearningPathCreatedObserverInterface
8
{
9
    use RequestTrait;
10
11
    public function hookCreated(HookLearningPathCreatedEventInterface $hookEvent)
12
    {
13
        /** @var learnpath $lp */
14
        $lp = $hookEvent->getEventData()['lp'];
15
        $userId = api_get_user_id();
16
        $courseCode = api_get_course_id();
17
18
        $cidreq = api_get_cidreq();
19
20
        $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?';
21
        $url .= ($cidreq ? $cidreq.'&' : '');
22
        $url .= http_build_query(
23
            [
24
                'action' => 'view',
25
                'lp_id' => $lp->lp_id,
26
                'isStudentView' => 'true',
27
            ]
28
        );
29
30
        try {
31
            $json = $this->doCreateRequest(
32
                [
33
                    'user_id' => $userId,
34
                    'course_code' => $courseCode,
35
                    'content_id' => $lp->get_id(),
36
                    'content_type' => 'lp',
37
                    'content_url' => $url,
38
                    'post_title' => $lp->get_name(),
39
                ]
40
            );
41
        } catch (Exception $e) {
42
            Display::addFlash(
43
                Display::return_message($e->getMessage(), 'error')
44
            );
45
46
            return;
47
        }
48
49
        if (empty($json)) {
50
            return;
51
        }
52
53
        error_log('ExtNotifConn: Learning path created: ID '.$json['data']['notification_id']);
54
    }
55
}
56