| Conditions | 24 |
| Paths | 1080 |
| Total Lines | 131 |
| Code Lines | 70 |
| 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 |
||
| 121 | private function findUserSessions(User $user) |
||
| 122 | { |
||
| 123 | $allowOrder = api_get_configuration_value('session_list_order'); |
||
| 124 | $showAllSessions = api_get_configuration_value('show_all_sessions_on_my_course_page') === true; |
||
| 125 | $orderBySettings = api_get_configuration_value('my_courses_session_order'); |
||
| 126 | |||
| 127 | $position = ''; |
||
| 128 | |||
| 129 | if ($allowOrder) { |
||
| 130 | $position = ', s.position AS position '; |
||
| 131 | } |
||
| 132 | |||
| 133 | $now = api_get_utc_datetime(null, false, true); |
||
| 134 | |||
| 135 | $dql = "SELECT DISTINCT |
||
| 136 | s.id, |
||
| 137 | s.accessEndDate AS access_end_date, |
||
| 138 | s.duration, |
||
| 139 | CASE WHEN s.accessEndDate IS NULL THEN 1 ELSE 0 END HIDDEN _isFieldNull |
||
| 140 | $position |
||
| 141 | FROM ChamiloCoreBundle:Session AS s |
||
| 142 | LEFT JOIN ChamiloCoreBundle:SessionRelCourseRelUser AS scu WITH scu.session = s |
||
| 143 | INNER JOIN ChamiloCoreBundle:AccessUrlRelSession AS url WITH url.session = s.id |
||
| 144 | LEFT JOIN ChamiloCoreBundle:SessionCategory AS sc WITH s.category = sc |
||
| 145 | WHERE (scu.user = :user OR s.generalCoach = :user) AND url.url = :url"; |
||
| 146 | |||
| 147 | $order = "ORDER BY sc.name, s.name"; |
||
| 148 | |||
| 149 | if ($showAllSessions) { |
||
| 150 | $order = "ORDER BY s.accessStartDate"; |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($allowOrder) { |
||
| 154 | $order = "ORDER BY s.position"; |
||
| 155 | } |
||
| 156 | |||
| 157 | if (!empty($orderBySettings) && isset($orderBySettings['field']) && isset($orderBySettings['order'])) { |
||
| 158 | $field = $orderBySettings['field']; |
||
| 159 | $orderSetting = $orderBySettings['order']; |
||
| 160 | |||
| 161 | switch ($field) { |
||
| 162 | case 'start_date': |
||
| 163 | $order = "ORDER BY s.accessStartDate $orderSetting"; |
||
| 164 | break; |
||
| 165 | case 'end_date': |
||
| 166 | $order = " ORDER BY s.accessEndDate $orderSetting "; |
||
| 167 | if ($orderSetting == 'asc') { |
||
| 168 | // Put null values at the end |
||
| 169 | // https://stackoverflow.com/questions/12652034/how-can-i-order-by-null-in-dql |
||
| 170 | $order = "ORDER BY _isFieldNull asc, s.accessEndDate asc"; |
||
| 171 | } |
||
| 172 | break; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | $results = []; |
||
| 177 | $rows = $this->em |
||
| 178 | ->createQuery("$dql $order") |
||
| 179 | ->setParameters( |
||
| 180 | [ |
||
| 181 | 'user' => $user->getId(), |
||
| 182 | 'url' => api_get_current_access_url_id(), |
||
| 183 | ] |
||
| 184 | ) |
||
| 185 | ->getResult(); |
||
| 186 | |||
| 187 | foreach ($rows as $row) { |
||
| 188 | $coachList = \SessionManager::getCoachesBySession($row['id']); |
||
| 189 | $courseList = \UserManager::get_courses_list_by_session( |
||
| 190 | $user->getId(), |
||
| 191 | $row['id'] |
||
| 192 | ); |
||
| 193 | $daysLeft = \SessionManager::getDayLeftInSession( |
||
| 194 | ['id' => $row['id'], 'duration' => $row['duration']], |
||
| 195 | $user->getId() |
||
| 196 | ); |
||
| 197 | $isGeneralCoach = \SessionManager::user_is_general_coach($user->getId(), $row['id']); |
||
| 198 | $isCoachOfCourse = in_array($user->getId(), $coachList); |
||
| 199 | |||
| 200 | if (!$isGeneralCoach && !$isCoachOfCourse) { |
||
| 201 | // Teachers can access the session depending in the access_coach date |
||
| 202 | if ($row['duration']) { |
||
| 203 | if ($daysLeft <= 0) { |
||
| 204 | continue; |
||
| 205 | } |
||
| 206 | } else { |
||
| 207 | if (isset($row['access_end_date']) && !empty($row['access_end_date'])) { |
||
| 208 | if ($row['access_end_date'] <= $now) { |
||
| 209 | continue; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | $visibility = api_get_session_visibility($row['id'], null, false); |
||
| 216 | |||
| 217 | if ($visibility != SESSION_VISIBLE) { |
||
| 218 | // Course Coach session visibility. |
||
| 219 | $blockedCourseCount = 0; |
||
| 220 | $closedVisibilityList = [COURSE_VISIBILITY_CLOSED, COURSE_VISIBILITY_HIDDEN]; |
||
| 221 | $sessionCourseVisibility = SESSION_INVISIBLE; |
||
| 222 | |||
| 223 | foreach ($courseList as $course) { |
||
| 224 | // Checking session visibility |
||
| 225 | $sessionCourseVisibility = api_get_session_visibility( |
||
| 226 | $row['id'], |
||
| 227 | $course['real_id'], |
||
| 228 | false |
||
| 229 | ); |
||
| 230 | |||
| 231 | $courseIsVisible = !in_array($course['visibility'], $closedVisibilityList); |
||
| 232 | |||
| 233 | if ($courseIsVisible === false || $sessionCourseVisibility == SESSION_INVISIBLE) { |
||
| 234 | $blockedCourseCount++; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | // If all courses are blocked then no show in the list. |
||
| 239 | if ($blockedCourseCount !== count($courseList)) { |
||
| 240 | $visibility = $sessionCourseVisibility; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | if ($visibility == SESSION_INVISIBLE) { |
||
| 245 | continue; |
||
| 246 | } |
||
| 247 | |||
| 248 | $results[] = $row['id']; |
||
| 249 | } |
||
| 250 | |||
| 251 | return $results; |
||
| 252 | } |
||
| 254 |