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

HookAdvancedSubscription::hookNotificationContent()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 4
nop 1
dl 0
loc 28
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
/**
6
 * Hook Observer for Advanced subscription plugin.
7
 *
8
 * @author Daniel Alejandro Barreto Alva <[email protected]>
9
 */
10
require_once __DIR__.'/../config.php';
11
12
class HookAdvancedSubscription {
13
    /**
14
     * @param $params
15
     *
16
     * @return soap_fault|null
17
     */
18
    public static function WSSessionListInCategory($params)
19
    {
20
        global $debug;
21
22
        if ($debug) {
23
            error_log(__FUNCTION__);
24
            error_log('Params '.print_r($params, 1));
0 ignored issues
show
Bug introduced by
Are you sure print_r($params, 1) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
            error_log('Params './** @scrutinizer ignore-type */ print_r($params, 1));
Loading history...
25
            if (!WSHelperVerifyKey($params)) {
0 ignored issues
show
Bug introduced by
The function WSHelperVerifyKey was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
            if (!/** @scrutinizer ignore-call */ WSHelperVerifyKey($params)) {
Loading history...
26
                error_log(return_error(WS_ERROR_SECRET_KEY));
0 ignored issues
show
Bug introduced by
The function return_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
                error_log(/** @scrutinizer ignore-call */ return_error(WS_ERROR_SECRET_KEY));
Loading history...
Bug introduced by
The constant WS_ERROR_SECRET_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
27
            }
28
        }
29
        // Check if category ID is set
30
        if (!empty($params['id']) && empty($params['name'])) {
31
            $sessionCategoryId = $params['id'];
32
        } elseif (!empty($params['name'])) {
33
            // Check if category name is set
34
            $sessionCategoryId = SessionManager::getSessionCategoryIdByName($params['name']);
35
            if (is_array($sessionCategoryId)) {
36
                $sessionCategoryId = current($sessionCategoryId);
37
            }
38
        } else {
39
            // Return soap fault Not valid input params
40
41
            return return_error(WS_ERROR_INVALID_INPUT);
0 ignored issues
show
Bug introduced by
The constant WS_ERROR_INVALID_INPUT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
42
        }
43
44
        // Get the session brief List by category
45
        $fields = [
46
            'id',
47
            'short_description',
48
            'mode',
49
            'human_text_duration',
50
            'vacancies',
51
            'schedule',
52
        ];
53
        $datePub = new DateTime();
54
55
        return SessionManager::getShortSessionListAndExtraByCategory(
56
            $sessionCategoryId,
57
            $params['target'],
58
            $fields,
59
            $datePub
60
        );
61
    }
62
63
    /**
64
     * @param $params
65
     *
66
     * @return soap_fault|null
67
     */
68
    public static function WSSessionGetDetailsByUser($params)
69
    {
70
        global $debug;
71
72
        if ($debug) {
73
            error_log('WSUserSubscribedInCourse');
74
            error_log('Params '.print_r($params, 1));
0 ignored issues
show
Bug introduced by
Are you sure print_r($params, 1) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
            error_log('Params './** @scrutinizer ignore-type */ print_r($params, 1));
Loading history...
75
        }
76
        if (!WSHelperVerifyKey($params)) {
0 ignored issues
show
Bug introduced by
The function WSHelperVerifyKey was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        if (!/** @scrutinizer ignore-call */ WSHelperVerifyKey($params)) {
Loading history...
77
            return return_error(WS_ERROR_SECRET_KEY);
0 ignored issues
show
Bug introduced by
The constant WS_ERROR_SECRET_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The function return_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            return /** @scrutinizer ignore-call */ return_error(WS_ERROR_SECRET_KEY);
Loading history...
78
        }
79
        // Check params
80
        if (is_array($params) && !empty($params['session_id']) && !empty($params['user_id'])) {
81
            $userId = UserManager::get_user_id_from_original_id($params['user_id'], $params['user_field']);
82
            $sessionId = (int) $params['session_id'];
83
            // Check if user exists
84
            if (UserManager::is_user_id_valid($userId) &&
85
                SessionManager::isValidId($sessionId)
86
            ) {
87
                // Check if student is already subscribed
88
                $plugin = AdvancedSubscriptionPlugin::create();
89
                $isOpen = $plugin->isSessionOpen($sessionId);
90
                $status = $plugin->getQueueStatus($userId, $sessionId);
91
                $vacancy = $plugin->getVacancy($sessionId);
92
                $data = $plugin->getSessionDetails($sessionId);
93
                $isUserInTargetGroup = $plugin->isUserInTargetGroup($userId, $sessionId);
94
                if (!empty($data) && is_array($data)) {
95
                    $data['status'] = $status;
96
                    // Vacancy and queue status cases:
97
                    if ($isOpen) {
98
                        // Go to Course session
99
                        $data['action_url'] = $plugin->getOpenSessionUrl($userId, $params);
100
                        if (SessionManager::isUserSubscribedAsStudent($sessionId, $userId)) {
101
                            $data['status'] = 10;
102
                        }
103
                    } else {
104
                        if (!$isUserInTargetGroup) {
105
                            $data['status'] = -2;
106
                        } else {
107
                            try {
108
                                $isAllowed = $plugin->isAllowedToDoRequest($userId, $params);
109
                                $data['message'] = $plugin->getStatusMessage($status, $isAllowed);
110
                            } catch (\Exception $e) {
111
                                $data['message'] = $e->getMessage();
112
                            }
113
                            $params['action'] = 'subscribe';
114
                            $params['sessionId'] = (int) $sessionId;
115
                            $params['currentUserId'] = 0; // No needed
116
                            $params['studentUserId'] = (int) $userId;
117
                            $params['queueId'] = 0; // No needed
118
                            $params['newStatus'] = ADVANCED_SUBSCRIPTION_QUEUE_STATUS_START;
119
                            if ($vacancy > 0) {
120
                                // Check conditions
121
                                if (ADVANCED_SUBSCRIPTION_QUEUE_STATUS_NO_QUEUE == $status) {
122
                                    // No in Queue, require queue subscription url action
123
                                    $data['action_url'] = $plugin->getTermsUrl($params);
124
                                } elseif (ADVANCED_SUBSCRIPTION_QUEUE_STATUS_ADMIN_APPROVED == $status) {
125
                                    // send url action
126
                                    $data['action_url'] = $plugin->getSessionUrl($sessionId);
127
                                } // Else: In queue, output status message, no more info.
128
                            } else {
129
                                if (ADVANCED_SUBSCRIPTION_QUEUE_STATUS_ADMIN_APPROVED == $status) {
130
                                    $data['action_url'] = $plugin->getSessionUrl($sessionId);
131
                                } elseif (ADVANCED_SUBSCRIPTION_QUEUE_STATUS_NO_QUEUE == $status) {
132
                                    // in Queue or not, cannot be subscribed to session
133
                                    $data['action_url'] = $plugin->getTermsUrl($params);
134
                                } // Else: In queue, output status message, no more info.
135
                            }
136
                        }
137
                    }
138
                    $result = $data;
139
                } else {
140
                    // Return soap fault No result was found
141
                    $result = return_error(WS_ERROR_NOT_FOUND_RESULT);
0 ignored issues
show
Bug introduced by
The constant WS_ERROR_NOT_FOUND_RESULT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
142
                }
143
            } else {
144
                // Return soap fault No result was found
145
                $result = return_error(WS_ERROR_NOT_FOUND_RESULT);
146
            }
147
        } else {
148
            // Return soap fault Not valid input params
149
            $result = return_error(WS_ERROR_INVALID_INPUT);
0 ignored issues
show
Bug introduced by
The constant WS_ERROR_INVALID_INPUT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
150
        }
151
152
        return $result;
153
    }
154
155
    /**
156
     * Get a list of sessions (id, coach_id, name, courses_num, users_num, classes_num,
157
     * access_start_date, access_end_date, access_days_before_num, session_admin_id, visibility,
158
     * session_category_id, promotion_id,
159
     * validated_user_num, waiting_user_num,
160
     * extra, course) the validated_usernum and waiting_user_num are
161
     * used when have the plugin for advance incsription enables.
162
     * The extra data (field_name, field_value)
163
     * The course data (course_id, course_code, course_title,
164
     * coach_username, coach_firstname, coach_lastname).
165
     *
166
     * @param array $params List of parameters (id, category_name, access_url_id, secret_key)
167
     *
168
     * @return array|soap_fault Sessions list (id=>[title=>'title',url='http://...',date_start=>'...',date_end=>''])
169
     */
170
    public static function WSListSessionsDetailsByCategory($params)
171
    {
172
        global $debug;
173
174
        if ($debug) {
175
            error_log('WSListSessionsDetailsByCategory');
176
            error_log('Params '.print_r($params, 1));
0 ignored issues
show
Bug introduced by
Are you sure print_r($params, 1) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

176
            error_log('Params './** @scrutinizer ignore-type */ print_r($params, 1));
Loading history...
177
        }
178
        $secretKey = $params['secret_key'];
179
180
        // Check if secret key is valid
181
        if (!WSHelperVerifyKey($secretKey)) {
0 ignored issues
show
Bug introduced by
The function WSHelperVerifyKey was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

181
        if (!/** @scrutinizer ignore-call */ WSHelperVerifyKey($secretKey)) {
Loading history...
182
            return return_error(WS_ERROR_SECRET_KEY);
0 ignored issues
show
Bug introduced by
The constant WS_ERROR_SECRET_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The function return_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
            return /** @scrutinizer ignore-call */ return_error(WS_ERROR_SECRET_KEY);
Loading history...
183
        }
184
185
        // Check if category ID is set
186
        if (!empty($params['id']) && empty($params['category_name'])) {
187
            $sessionCategoryId = $params['id'];
188
        } elseif (!empty($params['category_name'])) {
189
            // Check if category name is set
190
            $sessionCategoryId = SessionManager::getSessionCategoryIdByName($params['category_name']);
191
            if (is_array($sessionCategoryId)) {
192
                $sessionCategoryId = current($sessionCategoryId);
193
            }
194
        } else {
195
            // Return soap fault Not valid input params
196
197
            return return_error(WS_ERROR_INVALID_INPUT);
0 ignored issues
show
Bug introduced by
The constant WS_ERROR_INVALID_INPUT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
198
        }
199
200
        // Get the session List by category
201
        $sessionList = SessionManager::getSessionListAndExtraByCategoryId($sessionCategoryId);
202
203
        if (empty($sessionList)) {
204
            // If not found any session, return error
205
206
            return return_error(WS_ERROR_NOT_FOUND_RESULT);
0 ignored issues
show
Bug introduced by
The constant WS_ERROR_NOT_FOUND_RESULT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
207
        }
208
209
        // Get validated and waiting queue users count for each session
210
        $plugin = AdvancedSubscriptionPlugin::create();
211
        foreach ($sessionList as &$session) {
212
            // Add validated and queue users count
213
            $session['validated_user_num'] = $plugin->countQueueByParams(
214
                [
215
                    'sessions' => [$session['id']],
216
                    'status' => [ADVANCED_SUBSCRIPTION_QUEUE_STATUS_ADMIN_APPROVED],
217
                ]
218
            );
219
            $session['waiting_user_num'] = $plugin->countQueueByParams(
220
                [
221
                    'sessions' => [$session['id']],
222
                    'status' => [
223
                        ADVANCED_SUBSCRIPTION_QUEUE_STATUS_START,
224
                        ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_APPROVED,
225
                    ],
226
                ]
227
            );
228
        }
229
230
        return $sessionList;
231
    }
232
}
233