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

ResubscriptionEventSubscriber   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
c 1
b 0
f 0
dl 0
loc 91
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 4 1
B onResubscribe() 0 79 8
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
use Chamilo\CoreBundle\Event\AbstractEvent;
8
use Chamilo\CoreBundle\Event\Events;
9
use Chamilo\CoreBundle\Event\SessionResubscriptionEvent;
10
use Database;
11
use Display;
12
use Exception;
13
use Resubscription;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
16
class ResubscriptionEventSubscriber implements EventSubscriberInterface
17
{
18
    public static function getSubscribedEvents(): array
19
    {
20
        return [
21
            Events::SESSION_RESUBSCRIPTION => 'onResubscribe',
22
        ];
23
    }
24
25
    /**
26
     * @throws \Exception
27
     */
28
    public function onResubscribe(SessionResubscriptionEvent $event): void
29
    {
30
        if (AbstractEvent::TYPE_PRE === $event->getType()) {
31
            $resubscriptionLimit = Resubscription::create()->get('resubscription_limit');
32
33
            // Initialize variables as a calendar year by default
34
            $limitDateFormat = 'Y-01-01';
35
            $limitDate = gmdate($limitDateFormat);
36
            $resubscriptionOffset = "1 year";
37
38
            // No need to use a 'switch' with only two options so an 'if' is enough.
39
            // However, this could change if the number of options increases
40
            if ($resubscriptionLimit === 'natural_year') {
41
                $limitDateFormat = 'Y-m-d';
42
                $limitDate = gmdate($limitDateFormat);
43
                $limitDate = gmdate($limitDateFormat, strtotime("$limitDate -$resubscriptionOffset"));
44
            }
45
46
            $join = " INNER JOIN ".Database::get_main_table(TABLE_MAIN_SESSION)." s ON s.id = su.session_id";
47
48
            // User sessions and courses
49
            $userSessions = Database::select(
50
                'su.session_id, s.access_end_date',
51
                Database::get_main_table(TABLE_MAIN_SESSION_USER).' su '.$join,
52
                [
53
                    'where' => [
54
                        'su.user_id = ? AND s.access_end_date >= ?' => [
55
                            api_get_user_id(),
56
                            $limitDate,
57
                        ],
58
                    ],
59
                    'order' => 'access_end_date DESC',
60
                ]
61
            );
62
            $userSessionCourses = [];
63
            foreach ($userSessions as $userSession) {
64
                $userSessionCourseResult = Database::select(
65
                    'c_id',
66
                    Database::get_main_table(TABLE_MAIN_SESSION_COURSE),
67
                    [
68
                        'where' => [
69
                            'session_id = ?' => [
70
                                $userSession['session_id'],
71
                            ],
72
                        ],
73
                    ]
74
                );
75
                foreach ($userSessionCourseResult as $userSessionCourse) {
76
                    if (!isset($userSessionCourses[$userSessionCourse['c_id']])) {
77
                        $userSessionCourses[$userSessionCourse['c_id']] = $userSession['access_end_date'];
78
                    }
79
                }
80
            }
81
82
            // Current session and courses
83
            $currentSessionCourseResult = Database::select(
84
                'c_id',
85
                Database::get_main_table(TABLE_MAIN_SESSION_COURSE),
86
                [
87
                    'where' => [
88
                        'session_id = ?' => [
89
                            $event->getSessionId(),
90
                        ],
91
                    ],
92
                ]
93
            );
94
95
            // Check if current course code matches with one of the users
96
            foreach ($currentSessionCourseResult as $currentSessionCourse) {
97
                if (isset($userSessionCourses[$currentSessionCourse['c_id']])) {
98
                    $endDate = $userSessionCourses[$currentSessionCourse['c_id']];
99
                    $resubscriptionDate = gmdate($limitDateFormat, strtotime($endDate." +$resubscriptionOffset"));
100
                    $icon = Display::getMdiIcon(ObjectIcon::USER, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Learner'));
101
                    $canResubscribeFrom = sprintf(
102
                        get_plugin_lang('CanResubscribeFromX', 'resubscription'),
103
                        $resubscriptionDate
104
                    );
105
106
                    throw new Exception(Display::label($icon.' '.$canResubscribeFrom, "info"));
107
                }
108
            }
109
        }
110
    }
111
}