| Conditions | 2 |
| Paths | 2 |
| Total Lines | 343 |
| Code Lines | 197 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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)); |
||
| 75 | } |
||
| 76 | if (!WSHelperVerifyKey($params)) { |
||
| 77 | return return_error(WS_ERROR_SECRET_KEY); |
||
| 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); |
||
| 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); |
||
| 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)); |
||
| 177 | } |
||
| 178 | $secretKey = $params['secret_key']; |
||
| 179 | |||
| 180 | // Check if secret key is valid |
||
| 181 | if (!WSHelperVerifyKey($secretKey)) { |
||
| 182 | return return_error(WS_ERROR_SECRET_KEY); |
||
| 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); |
||
| 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); |
||
| 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 |