| Conditions | 29 |
| Paths | 14592 |
| Total Lines | 262 |
| Code Lines | 164 |
| 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 |
||
| 46 | function get_course_data( |
||
| 47 | int $from, |
||
| 48 | int $number_of_items, |
||
| 49 | int $column, |
||
| 50 | string $direction, |
||
| 51 | array $dataFunctions = [], |
||
| 52 | bool $getCount = false |
||
| 53 | ): int|array { |
||
| 54 | $course_table = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 55 | |||
| 56 | if (!in_array(strtolower($direction), ['asc', 'desc'])) { |
||
| 57 | $direction = 'desc'; |
||
| 58 | } |
||
| 59 | |||
| 60 | $tblCourseCategory = Database::get_main_table(TABLE_MAIN_CATEGORY); |
||
| 61 | $tblCourseRelCategory = Database::get_main_table(TABLE_MAIN_COURSE_REL_CATEGORY); |
||
| 62 | |||
| 63 | $select = 'SELECT |
||
| 64 | course.code AS col0, |
||
| 65 | course.title AS col1, |
||
| 66 | course.code AS col2, |
||
| 67 | course_language AS col3, |
||
| 68 | subscribe AS col5, |
||
| 69 | unsubscribe AS col6, |
||
| 70 | course.code AS col7, |
||
| 71 | course.visibility AS col8, |
||
| 72 | directory as col9, |
||
| 73 | visual_code, |
||
| 74 | directory, |
||
| 75 | course.id'; |
||
| 76 | |||
| 77 | if ($getCount) { |
||
| 78 | $select = 'SELECT COUNT(DISTINCT(course.id)) as count '; |
||
| 79 | } |
||
| 80 | |||
| 81 | $sql = "$select FROM $course_table course "; |
||
| 82 | |||
| 83 | if (!empty($_GET['keyword_category'])) { |
||
| 84 | $sql .= "INNER JOIN $tblCourseRelCategory course_rel_category ON course.id = course_rel_category.course_id |
||
| 85 | INNER JOIN $tblCourseCategory category ON course_rel_category.course_category_id = category.id "; |
||
| 86 | } |
||
| 87 | |||
| 88 | if ((api_is_platform_admin() || api_is_session_admin()) |
||
| 89 | && api_is_multiple_url_enabled() && -1 != api_get_current_access_url_id() |
||
|
|
|||
| 90 | ) { |
||
| 91 | $access_url_rel_course_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 92 | $sql .= " INNER JOIN $access_url_rel_course_table url_rel_course |
||
| 93 | ON (course.id = url_rel_course.c_id)"; |
||
| 94 | } |
||
| 95 | |||
| 96 | if (!empty($_GET['session_id'])) { |
||
| 97 | $session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 98 | $session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 99 | |||
| 100 | $sql .= " INNER JOIN $session_rel_course r ON course.id = r.c_id |
||
| 101 | INNER JOIN $session s ON r.session_id = s.id "; |
||
| 102 | } |
||
| 103 | |||
| 104 | if (isset($_GET['keyword'])) { |
||
| 105 | $keyword = Database::escape_string('%'.trim($_GET['keyword']).'%'); |
||
| 106 | $sql .= " WHERE ( |
||
| 107 | course.title LIKE '".$keyword."' OR |
||
| 108 | course.code LIKE '".$keyword."' OR |
||
| 109 | visual_code LIKE '".$keyword."' |
||
| 110 | ) |
||
| 111 | "; |
||
| 112 | } elseif (isset($_GET['keyword_code'])) { |
||
| 113 | $keyword_code = Database::escape_string('%'.$_GET['keyword_code'].'%'); |
||
| 114 | $keyword_title = Database::escape_string('%'.$_GET['keyword_title'].'%'); |
||
| 115 | $keyword_category = isset($_GET['keyword_category']) |
||
| 116 | ? Database::escape_string($_GET['keyword_category']) |
||
| 117 | : null; |
||
| 118 | $keyword_language = Database::escape_string('%'.$_GET['keyword_language'].'%'); |
||
| 119 | $keyword_visibility = Database::escape_string('%'.$_GET['keyword_visibility'].'%'); |
||
| 120 | $keyword_subscribe = Database::escape_string($_GET['keyword_subscribe']); |
||
| 121 | $keyword_unsubscribe = Database::escape_string($_GET['keyword_unsubscribe']); |
||
| 122 | |||
| 123 | $sql .= " WHERE |
||
| 124 | (course.code LIKE '".$keyword_code."' OR visual_code LIKE '".$keyword_code."') AND |
||
| 125 | course.title LIKE '".$keyword_title."' AND |
||
| 126 | course_language LIKE '".$keyword_language."' AND |
||
| 127 | visibility LIKE '".$keyword_visibility."' AND |
||
| 128 | subscribe LIKE '".$keyword_subscribe."' AND |
||
| 129 | unsubscribe LIKE '".$keyword_unsubscribe."'"; |
||
| 130 | |||
| 131 | if (!empty($keyword_category)) { |
||
| 132 | $sql .= ' AND category.id = '.$keyword_category.' '; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | // Adding the filter to see the user's only of the current access_url. |
||
| 137 | if ((api_is_platform_admin() || api_is_session_admin()) |
||
| 138 | && api_is_multiple_url_enabled() && -1 != api_get_current_access_url_id() |
||
| 139 | ) { |
||
| 140 | $sql .= ' AND url_rel_course.access_url_id='.api_get_current_access_url_id(); |
||
| 141 | } |
||
| 142 | |||
| 143 | if (!empty($_GET['session_id'])) { |
||
| 144 | $sessionId = (int) $_GET['session_id']; |
||
| 145 | $sql .= ' AND s.id = '.$sessionId.' '; |
||
| 146 | } |
||
| 147 | |||
| 148 | if ($getCount) { |
||
| 149 | $res = Database::query($sql); |
||
| 150 | $row = Database::fetch_array($res); |
||
| 151 | if ($row) { |
||
| 152 | return (int) $row['count']; |
||
| 153 | } |
||
| 154 | |||
| 155 | return 0; |
||
| 156 | } |
||
| 157 | |||
| 158 | $sql .= ' GROUP BY course.code'; |
||
| 159 | $sql .= " ORDER BY col$column $direction "; |
||
| 160 | $sql .= " LIMIT $from, $number_of_items"; |
||
| 161 | |||
| 162 | $res = Database::query($sql); |
||
| 163 | $courses = []; |
||
| 164 | $languages = api_get_languages(); |
||
| 165 | |||
| 166 | $path = api_get_path(WEB_CODE_PATH); |
||
| 167 | |||
| 168 | while ($course = Database::fetch_array($res)) { |
||
| 169 | $courseInfo = api_get_course_info_by_id($course['id']); |
||
| 170 | |||
| 171 | // get categories |
||
| 172 | $sqlCategoriesByCourseId = "SELECT category.title FROM $tblCourseCategory category |
||
| 173 | INNER JOIN $tblCourseRelCategory course_rel_category ON category.id = course_rel_category.course_category_id |
||
| 174 | WHERE course_rel_category.course_id = ".$course['id']; |
||
| 175 | $resultCategories = Database::query($sqlCategoriesByCourseId); |
||
| 176 | $categories = []; |
||
| 177 | |||
| 178 | while ($category = Database::fetch_array($resultCategories)) { |
||
| 179 | $categories[] = $category['title']; |
||
| 180 | } |
||
| 181 | |||
| 182 | // Place colour icons in front of courses. |
||
| 183 | $show_visual_code = $course['visual_code'] != $course['col2'] ? Display::label($course['visual_code'], 'info') : null; |
||
| 184 | $course['col1'] = get_course_visibility_icon($courseInfo['visibility']).\PHP_EOL |
||
| 185 | .Display::url(Security::remove_XSS($course['col1']), $courseInfo['course_public_url']).\PHP_EOL |
||
| 186 | .$show_visual_code; |
||
| 187 | $course['col5'] = SUBSCRIBE_ALLOWED == $course['col5'] ? get_lang('Yes') : get_lang('No'); |
||
| 188 | $course['col6'] = UNSUBSCRIBE_ALLOWED == $course['col6'] ? get_lang('Yes') : get_lang('No'); |
||
| 189 | |||
| 190 | $courseId = $course['id']; |
||
| 191 | |||
| 192 | $actions = []; |
||
| 193 | $actions[] = Display::url( |
||
| 194 | Display::getMdiIcon( |
||
| 195 | ActionIcon::INFORMATION, |
||
| 196 | 'ch-tool-icon', |
||
| 197 | null, |
||
| 198 | ICON_SIZE_SMALL, |
||
| 199 | get_lang('Information') |
||
| 200 | ), |
||
| 201 | "course_information.php?id=$courseId" |
||
| 202 | ); |
||
| 203 | $actions[] = Display::url( |
||
| 204 | Display::getMdiIcon( |
||
| 205 | ToolIcon::COURSE_HOME, |
||
| 206 | 'ch-tool-icon', |
||
| 207 | null, |
||
| 208 | ICON_SIZE_SMALL, |
||
| 209 | get_lang('Course home') |
||
| 210 | ), |
||
| 211 | $courseInfo['course_public_url'] |
||
| 212 | ); |
||
| 213 | $actions[] = Display::url( |
||
| 214 | Display::getMdiIcon( |
||
| 215 | ToolIcon::TRACKING, |
||
| 216 | 'ch-tool-icon', |
||
| 217 | null, |
||
| 218 | ICON_SIZE_SMALL, |
||
| 219 | get_lang('Reporting') |
||
| 220 | ), |
||
| 221 | $path.'tracking/courseLog.php?'.api_get_cidreq_params($courseId) |
||
| 222 | ); |
||
| 223 | $actions[] = Display::url( |
||
| 224 | Display::getMdiIcon( |
||
| 225 | ActionIcon::EDIT, |
||
| 226 | 'ch-tool-icon', |
||
| 227 | null, |
||
| 228 | ICON_SIZE_SMALL, |
||
| 229 | get_lang('Edit') |
||
| 230 | ), |
||
| 231 | $path.'admin/course_edit.php?id='.$courseId |
||
| 232 | ); |
||
| 233 | $actions[] = Display::url( |
||
| 234 | Display::getMdiIcon( |
||
| 235 | ActionIcon::TAKE_BACKUP, |
||
| 236 | 'ch-tool-icon', |
||
| 237 | null, |
||
| 238 | ICON_SIZE_SMALL, |
||
| 239 | get_lang('Create a backup') |
||
| 240 | ), |
||
| 241 | $path.'course_copy/create_backup.php?'.api_get_cidreq_params($courseId) |
||
| 242 | ); |
||
| 243 | |||
| 244 | // Single course delete: ask if exclusive documents should also be removed. |
||
| 245 | $actions[] = Display::url( |
||
| 246 | Display::getMdiIcon( |
||
| 247 | ActionIcon::DELETE, |
||
| 248 | 'ch-tool-icon', |
||
| 249 | null, |
||
| 250 | ICON_SIZE_SMALL, |
||
| 251 | get_lang('Delete') |
||
| 252 | ), |
||
| 253 | $path.'admin/course_list.php?' |
||
| 254 | .http_build_query([ |
||
| 255 | 'delete_course' => $course['col0'], |
||
| 256 | // Default: keep documents; JS will toggle this param to 1 if admin agrees. |
||
| 257 | 'delete_docs' => 0, |
||
| 258 | 'sec_token' => Security::getTokenFromSession(), |
||
| 259 | ]), |
||
| 260 | [ |
||
| 261 | 'onclick' => 'return confirmDeleteCourseWithDocs(this);', |
||
| 262 | ] |
||
| 263 | ); |
||
| 264 | |||
| 265 | $em = Database::getManager(); |
||
| 266 | /** @var CatalogueCourseRelAccessUrlRelUsergroupRepository $repo */ |
||
| 267 | $repo = $em->getRepository(CatalogueCourseRelAccessUrlRelUsergroup::class); |
||
| 268 | $record = $repo->findOneBy([ |
||
| 269 | 'course' => $courseId, |
||
| 270 | 'accessUrl' => api_get_current_access_url_id(), |
||
| 271 | 'usergroup' => null, |
||
| 272 | ]); |
||
| 273 | |||
| 274 | $isInCatalogue = null !== $record; |
||
| 275 | $catalogueUrl = api_get_self().'?toggle_catalogue='.$course['id'].'&sec_token='.Security::getTokenFromSession(); |
||
| 276 | |||
| 277 | $actions[] = Display::url( |
||
| 278 | Display::getMdiIcon( |
||
| 279 | $isInCatalogue ? StateIcon::CATALOGUE_OFF : StateIcon::CATALOGUE_ON, |
||
| 280 | 'ch-tool-icon', |
||
| 281 | null, |
||
| 282 | ICON_SIZE_SMALL, |
||
| 283 | $isInCatalogue ? get_lang('Remove from catalogue') : get_lang('Add to catalogue'), |
||
| 284 | [ |
||
| 285 | 'class' => $isInCatalogue ? 'text-warning' : 'text-muted', |
||
| 286 | ] |
||
| 287 | ), |
||
| 288 | $catalogueUrl, |
||
| 289 | [ |
||
| 290 | 'title' => $isInCatalogue ? get_lang('Remove from catalogue') : get_lang('Add to catalogue'), |
||
| 291 | ] |
||
| 292 | ); |
||
| 293 | |||
| 294 | $courseItem = [ |
||
| 295 | $course['col0'], |
||
| 296 | $course['col1'], |
||
| 297 | $course['col2'], |
||
| 298 | $languages[$course['col3']] ?? $course['col3'], |
||
| 299 | implode(', ', $categories), |
||
| 300 | $course['col5'], |
||
| 301 | $course['col6'], |
||
| 302 | implode(\PHP_EOL, $actions), |
||
| 303 | ]; |
||
| 304 | $courses[] = $courseItem; |
||
| 305 | } |
||
| 306 | |||
| 307 | return $courses; |
||
| 308 | } |
||
| 752 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.