| Conditions | 34 |
| Paths | > 20000 |
| Total Lines | 204 |
| Code Lines | 131 |
| 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 |
||
| 40 | function get_course_data($from, $number_of_items, $column, $direction, $dataFunctions = [], $getCount = false) |
||
| 41 | { |
||
| 42 | $addTeacherColumn = api_get_configuration_value('add_teachers_in_course_list'); |
||
| 43 | $table = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 44 | $from = (int) $from; |
||
| 45 | $number_of_items = (int) $number_of_items; |
||
| 46 | $column = (int) $column; |
||
| 47 | |||
| 48 | if (!in_array(strtolower($direction), ['asc', 'desc'])) { |
||
| 49 | $direction = 'desc'; |
||
| 50 | } |
||
| 51 | |||
| 52 | $teachers = ''; |
||
| 53 | if ($addTeacherColumn) { |
||
| 54 | $teachers = " GROUP_CONCAT(cu.user_id SEPARATOR ',') as col7, "; |
||
| 55 | } |
||
| 56 | $select = "SELECT |
||
| 57 | code AS col0, |
||
| 58 | title AS col1, |
||
| 59 | code AS col2, |
||
| 60 | course_language AS col3, |
||
| 61 | category_code AS col4, |
||
| 62 | subscribe AS col5, |
||
| 63 | unsubscribe AS col6, |
||
| 64 | $teachers |
||
| 65 | visibility, |
||
| 66 | directory, |
||
| 67 | visual_code, |
||
| 68 | course.code, |
||
| 69 | course.id "; |
||
| 70 | |||
| 71 | if ($getCount) { |
||
| 72 | $select = 'SELECT COUNT(DISTINCT(course.id)) as count '; |
||
| 73 | } |
||
| 74 | |||
| 75 | $sql = "$select FROM $table course"; |
||
| 76 | |||
| 77 | if ((api_is_platform_admin() || api_is_session_admin()) && |
||
| 78 | api_is_multiple_url_enabled() && api_get_current_access_url_id() != -1 |
||
| 79 | ) { |
||
| 80 | $access_url_rel_course_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 81 | $sql .= " INNER JOIN $access_url_rel_course_table url_rel_course |
||
| 82 | ON (course.id = url_rel_course.c_id)"; |
||
| 83 | } |
||
| 84 | |||
| 85 | if ($addTeacherColumn) { |
||
| 86 | $tableCourseRelUser = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
||
| 87 | $sql .= " |
||
| 88 | LEFT JOIN $tableCourseRelUser cu |
||
| 89 | ON (course.id = cu.c_id AND cu.status = ".COURSEMANAGER.") |
||
| 90 | "; |
||
| 91 | } |
||
| 92 | |||
| 93 | if (isset($_GET['keyword'])) { |
||
| 94 | $keyword = Database::escape_string("%".trim($_GET['keyword'])."%"); |
||
| 95 | $sql .= " WHERE ( |
||
| 96 | title LIKE '".$keyword."' OR |
||
| 97 | code LIKE '".$keyword."' OR |
||
| 98 | visual_code LIKE '".$keyword."' |
||
| 99 | ) |
||
| 100 | "; |
||
| 101 | } elseif (isset($_GET['keyword_code'])) { |
||
| 102 | $keyword_code = Database::escape_string("%".$_GET['keyword_code']."%"); |
||
| 103 | $keyword_title = Database::escape_string("%".$_GET['keyword_title']."%"); |
||
| 104 | $keyword_category = isset($_GET['keyword_category']) |
||
| 105 | ? Database::escape_string("%".$_GET['keyword_category']."%") |
||
| 106 | : null; |
||
| 107 | $keyword_language = Database::escape_string("%".$_GET['keyword_language']."%"); |
||
| 108 | $keyword_visibility = Database::escape_string("%".$_GET['keyword_visibility']."%"); |
||
| 109 | $keyword_subscribe = Database::escape_string($_GET['keyword_subscribe']); |
||
| 110 | $keyword_unsubscribe = Database::escape_string($_GET['keyword_unsubscribe']); |
||
| 111 | |||
| 112 | $sql .= " WHERE |
||
| 113 | (code LIKE '".$keyword_code."' OR visual_code LIKE '".$keyword_code."') AND |
||
| 114 | title LIKE '".$keyword_title."' AND |
||
| 115 | course_language LIKE '".$keyword_language."' AND |
||
| 116 | visibility LIKE '".$keyword_visibility."' AND |
||
| 117 | subscribe LIKE '".$keyword_subscribe."' AND |
||
| 118 | unsubscribe LIKE '".$keyword_unsubscribe."'"; |
||
| 119 | |||
| 120 | if (!empty($keyword_category)) { |
||
| 121 | $sql .= " AND category_code LIKE '".$keyword_category."' "; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | // Adding the filter to see the user's only of the current access_url. |
||
| 126 | if ((api_is_platform_admin() || api_is_session_admin()) && |
||
| 127 | api_is_multiple_url_enabled() && api_get_current_access_url_id() != -1 |
||
| 128 | ) { |
||
| 129 | $sql .= " AND url_rel_course.access_url_id = ".api_get_current_access_url_id(); |
||
| 130 | } |
||
| 131 | |||
| 132 | if ($addTeacherColumn) { |
||
| 133 | $teachers = isset($_GET['course_teachers']) ? $_GET['course_teachers'] : []; |
||
| 134 | if (!empty($teachers)) { |
||
| 135 | $teachers = array_map('intval', $teachers); |
||
| 136 | $addNull = ''; |
||
| 137 | foreach ($teachers as $key => $teacherId) { |
||
| 138 | if (0 === $teacherId) { |
||
| 139 | $addNull = 'OR cu.user_id IS NULL '; |
||
| 140 | unset($key); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | $sql .= ' AND ( cu.user_id IN ("'.implode('", "', $teachers).'") '.$addNull.' ) '; |
||
| 144 | } |
||
| 145 | |||
| 146 | if (false === $getCount) { |
||
| 147 | $sql .= " GROUP BY course.id "; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | if ($getCount) { |
||
| 152 | $res = Database::query($sql); |
||
| 153 | $row = Database::fetch_array($res); |
||
| 154 | if ($row) { |
||
| 155 | return (int) $row['count']; |
||
| 156 | } |
||
| 157 | |||
| 158 | return 0; |
||
| 159 | } |
||
| 160 | |||
| 161 | $sql .= " ORDER BY col$column $direction "; |
||
| 162 | $sql .= " LIMIT $from, $number_of_items"; |
||
| 163 | |||
| 164 | $res = Database::query($sql); |
||
| 165 | $courses = []; |
||
| 166 | $languages = api_get_languages_to_array(); |
||
| 167 | $path = api_get_path(WEB_CODE_PATH); |
||
| 168 | $coursePath = api_get_path(WEB_COURSE_PATH); |
||
| 169 | |||
| 170 | while ($course = Database::fetch_array($res)) { |
||
| 171 | $courseId = $course['id']; |
||
| 172 | $courseCode = $course['code']; |
||
| 173 | |||
| 174 | // Place colour icons in front of courses. |
||
| 175 | $showVisualCode = $course['visual_code'] != $courseCode ? Display::label($course['visual_code'], 'info') : null; |
||
| 176 | $course[1] = get_course_visibility_icon($course['visibility']).PHP_EOL |
||
| 177 | .Display::url(Security::remove_XSS($course[1]), $coursePath.$course['directory'].'/index.php').PHP_EOL |
||
| 178 | .$showVisualCode; |
||
| 179 | $course[5] = $course[5] == SUBSCRIBE_ALLOWED ? get_lang('Yes') : get_lang('No'); |
||
| 180 | $course[6] = $course[6] == UNSUBSCRIBE_ALLOWED ? get_lang('Yes') : get_lang('No'); |
||
| 181 | $language = isset($languages[$course[3]]) ? $languages[$course[3]] : $course[3]; |
||
| 182 | |||
| 183 | $actions = []; |
||
| 184 | $actions[] = Display::url( |
||
| 185 | Display::return_icon('info2.png', get_lang('Info')), |
||
| 186 | "course_information.php?code=$courseCode" |
||
| 187 | ); |
||
| 188 | $actions[] = Display::url( |
||
| 189 | Display::return_icon('course_home.png', get_lang('CourseHomepage')), |
||
| 190 | $coursePath.$course['directory'].'/index.php' |
||
| 191 | ); |
||
| 192 | $actions[] = Display::url( |
||
| 193 | Display::return_icon('statistics.png', get_lang('Tracking')), |
||
| 194 | $path.'tracking/courseLog.php?'.api_get_cidreq_params($courseCode) |
||
| 195 | ); |
||
| 196 | $actions[] = Display::url( |
||
| 197 | Display::return_icon('edit.png', get_lang('Edit')), |
||
| 198 | $path.'admin/course_edit.php?id='.$courseId |
||
| 199 | ); |
||
| 200 | $actions[] = Display::url( |
||
| 201 | Display::return_icon('backup.png', get_lang('CreateBackup')), |
||
| 202 | $path.'coursecopy/create_backup.php?'.api_get_cidreq_params($courseCode) |
||
| 203 | ); |
||
| 204 | $actions[] = Display::url( |
||
| 205 | Display::return_icon('delete.png', get_lang('Delete')), |
||
| 206 | $path.'admin/course_list.php?' |
||
| 207 | .http_build_query([ |
||
| 208 | 'delete_course' => $courseCode, |
||
| 209 | 'sec_token' => Security::getTokenFromSession(), |
||
| 210 | ]), |
||
| 211 | [ |
||
| 212 | 'onclick' => "javascript: if (!confirm('" |
||
| 213 | .addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))."')) return false;", |
||
| 214 | ] |
||
| 215 | ); |
||
| 216 | $courseItem = [ |
||
| 217 | $course[0], |
||
| 218 | $course[1], |
||
| 219 | $course[2], |
||
| 220 | $language, |
||
| 221 | $course[4], |
||
| 222 | $course[5], |
||
| 223 | $course[6], |
||
| 224 | ]; |
||
| 225 | |||
| 226 | if ($addTeacherColumn) { |
||
| 227 | $teacherIdList = array_filter(explode(',', $course[7])); |
||
| 228 | $teacherList = []; |
||
| 229 | if (!empty($teacherIdList)) { |
||
| 230 | foreach ($teacherIdList as $teacherId) { |
||
| 231 | $userInfo = api_get_user_info($teacherId); |
||
| 232 | if ($userInfo) { |
||
| 233 | $teacherList[] = $userInfo['complete_name']; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 | $courseItem[] = implode(', ', $teacherList); |
||
| 238 | } |
||
| 239 | $courseItem[] = implode(PHP_EOL, $actions); |
||
| 240 | $courses[] = $courseItem; |
||
| 241 | } |
||
| 242 | |||
| 243 | return $courses; |
||
| 244 | } |
||
| 660 |
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.