| Conditions | 31 |
| Paths | 3840 |
| Total Lines | 235 |
| Code Lines | 140 |
| 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 |
||
| 42 | public function getExercisesReporting( |
||
| 43 | $document_path, |
||
| 44 | $user_id = null, |
||
| 45 | $filter = 0, |
||
| 46 | $exercise_id = 0, |
||
| 47 | $hotpotato_name = null |
||
| 48 | ) { |
||
| 49 | $return = array(); |
||
| 50 | |||
| 51 | $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST); |
||
| 52 | $TBL_TABLE_LP_MAIN = Database::get_course_table(TABLE_LP_MAIN); |
||
| 53 | |||
| 54 | $TBL_USER = Database::get_main_table(TABLE_MAIN_USER); |
||
| 55 | $TBL_TRACK_EXERCISES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 56 | $TBL_TRACK_ATTEMPT_RECORDING = Database:: get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING); |
||
| 57 | |||
| 58 | $cid = api_get_course_id(); |
||
| 59 | $course_id = api_get_course_int_id(); |
||
| 60 | $user_id = intval($user_id); |
||
| 61 | $sessionId = api_get_session_id(); |
||
| 62 | $session_id_and = ' AND te.session_id = ' . $sessionId . ' '; |
||
| 63 | $exercise_id = intval($exercise_id); |
||
| 64 | |||
| 65 | if (!empty($exercise_id)) { |
||
| 66 | $session_id_and .= " AND exe_exo_id = $exercise_id "; |
||
| 67 | } |
||
| 68 | |||
| 69 | if (empty($user_id)) { |
||
| 70 | $user_id_and = null; |
||
| 71 | $sql = "SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").", |
||
| 72 | official_code, |
||
| 73 | ce.title as extitle, |
||
| 74 | te.exe_result as exresult , |
||
| 75 | te.exe_weighting as exweight, |
||
| 76 | te.exe_date as exdate, |
||
| 77 | te.exe_id as exid, |
||
| 78 | email as exemail, |
||
| 79 | te.start_date as exstart, |
||
| 80 | steps_counter as exstep, |
||
| 81 | exe_user_id as excruid, |
||
| 82 | te.exe_duration as duration, |
||
| 83 | te.orig_lp_id as orig_lp_id, |
||
| 84 | tlm.name as lp_name |
||
| 85 | FROM $TBL_EXERCISES AS ce |
||
| 86 | INNER JOIN $TBL_TRACK_EXERCISES AS te |
||
| 87 | ON (te.exe_exo_id = ce.id) |
||
| 88 | INNER JOIN $TBL_USER AS user |
||
| 89 | ON (user.user_id = exe_user_id) |
||
| 90 | LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm |
||
| 91 | ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id) |
||
| 92 | WHERE |
||
| 93 | ce.c_id = $course_id AND |
||
| 94 | te.status != 'incomplete' AND |
||
| 95 | te.c_id = ce.c_id $user_id_and $session_id_and AND |
||
| 96 | ce.active <>-1"; |
||
| 97 | } else { |
||
| 98 | $user_id_and = ' AND te.exe_user_id = ' . api_get_user_id() . ' '; |
||
| 99 | // get only this user's results |
||
| 100 | $sql="SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").", |
||
| 101 | official_code, |
||
| 102 | ce.title as extitle, |
||
| 103 | te.exe_result as exresult, |
||
| 104 | te.exe_weighting as exweight, |
||
| 105 | te.exe_date as exdate, |
||
| 106 | te.exe_id as exid, |
||
| 107 | email as exemail, |
||
| 108 | te.start_date as exstart, |
||
| 109 | steps_counter as exstep, |
||
| 110 | exe_user_id as excruid, |
||
| 111 | te.exe_duration as duration, |
||
| 112 | ce.results_disabled as exdisabled, |
||
| 113 | te.orig_lp_id as orig_lp_id, |
||
| 114 | tlm.name as lp_name |
||
| 115 | FROM $TBL_EXERCISES AS ce |
||
| 116 | INNER JOIN $TBL_TRACK_EXERCISES AS te |
||
| 117 | ON (te.exe_exo_id = ce.id) |
||
| 118 | INNER JOIN $TBL_USER AS user |
||
| 119 | ON (user.user_id = exe_user_id) |
||
| 120 | LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm |
||
| 121 | ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id) |
||
| 122 | WHERE |
||
| 123 | ce.c_id = $course_id AND |
||
| 124 | te.status != 'incomplete' AND |
||
| 125 | te.c_id = ce.c_id $user_id_and $session_id_and AND |
||
| 126 | ce.active <>-1 AND |
||
| 127 | ORDER BY userpart2, te.c_id ASC, ce.title ASC, te.exe_date DESC"; |
||
| 128 | } |
||
| 129 | |||
| 130 | $results = array(); |
||
| 131 | $resx = Database::query($sql); |
||
| 132 | $bestAttemptPerUser = array(); |
||
| 133 | while ($rowx = Database::fetch_array($resx, 'ASSOC')) { |
||
| 134 | if ($this->onlyBestAttempts) { |
||
| 135 | if (!isset($bestAttemptPerUser[$rowx['excruid']])) { |
||
| 136 | $bestAttemptPerUser[$rowx['excruid']] = $rowx; |
||
| 137 | } else { |
||
| 138 | if ($rowx['exresult'] > $bestAttemptPerUser[$rowx['excruid']]['exresult']) { |
||
| 139 | $bestAttemptPerUser[$rowx['excruid']] = $rowx; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } else { |
||
| 143 | $results[] = $rowx; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | if ($this->onlyBestAttempts) { |
||
| 148 | $results = $bestAttemptPerUser; |
||
| 149 | } |
||
| 150 | |||
| 151 | $filter_by_not_revised = false; |
||
| 152 | $filter_by_revised = false; |
||
| 153 | |||
| 154 | if ($filter) { |
||
| 155 | switch ($filter) { |
||
| 156 | case 1: |
||
| 157 | $filter_by_not_revised = true; |
||
| 158 | break; |
||
| 159 | case 2: |
||
| 160 | $filter_by_revised = true; |
||
| 161 | break; |
||
| 162 | default: |
||
| 163 | null; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | if (empty($sessionId)) { |
||
| 168 | $students = CourseManager::get_user_list_from_course_code($cid); |
||
| 169 | } else { |
||
| 170 | $students = CourseManager::get_user_list_from_course_code($cid, $sessionId); |
||
| 171 | } |
||
| 172 | $studentsUserIdList = array_keys($students); |
||
| 173 | |||
| 174 | // Print the results of tests |
||
| 175 | $userWithResults = array(); |
||
| 176 | if (is_array($results)) { |
||
| 177 | $i = 0; |
||
| 178 | foreach ($results as $result) { |
||
| 179 | $revised = false; |
||
| 180 | |||
| 181 | //revised or not |
||
| 182 | $sql_exe = "SELECT exe_id FROM $TBL_TRACK_ATTEMPT_RECORDING |
||
| 183 | WHERE author != '' AND exe_id = ".Database :: escape_string($result['exid'])." |
||
| 184 | LIMIT 1"; |
||
| 185 | $query = Database::query($sql_exe); |
||
| 186 | |||
| 187 | if (Database:: num_rows($query) > 0) { |
||
| 188 | $revised = true; |
||
| 189 | } |
||
| 190 | |||
| 191 | if ($filter_by_not_revised && $revised) { |
||
| 192 | continue; |
||
| 193 | } |
||
| 194 | |||
| 195 | if ($filter_by_revised && !$revised) { |
||
| 196 | continue; |
||
| 197 | } |
||
| 198 | |||
| 199 | $return[$i] = array(); |
||
| 200 | if (empty($user_id)) { |
||
| 201 | $return[$i]['official_code'] = $result['official_code']; |
||
| 202 | if (api_is_western_name_order()) { |
||
| 203 | $return[$i]['first_name'] = $results[$i]['userpart1']; |
||
| 204 | $return[$i]['last_name'] = $results[$i]['userpart2']; |
||
| 205 | } else { |
||
| 206 | $return[$i]['first_name'] = $results[$i]['userpart2']; |
||
| 207 | $return[$i]['last_name'] = $results[$i]['userpart1']; |
||
| 208 | } |
||
| 209 | $return[$i]['user_id'] = $results[$i]['excruid']; |
||
| 210 | $return[$i]['email'] = $results[$i]['exemail']; |
||
| 211 | } |
||
| 212 | $return[$i]['title'] = $result['extitle']; |
||
| 213 | $return[$i]['start_date'] = api_get_local_time($result['exstart']); |
||
| 214 | $return[$i]['end_date'] = api_get_local_time($result['exdate']); |
||
| 215 | $return[$i]['duration'] = $result['duration']; |
||
| 216 | $return[$i]['result'] = $result['exresult']; |
||
| 217 | $return[$i]['max'] = $result['exweight']; |
||
| 218 | $return[$i]['status'] = $revised ? get_lang('Validated') : get_lang('NotValidated'); |
||
| 219 | $return[$i]['lp_id'] = $result['orig_lp_id']; |
||
| 220 | $return[$i]['lp_name'] = $result['lp_name']; |
||
| 221 | |||
| 222 | if (in_array($result['excruid'], $studentsUserIdList)) { |
||
| 223 | $return[$i]['is_user_subscribed'] = get_lang('Yes'); |
||
| 224 | } else { |
||
| 225 | $return[$i]['is_user_subscribed'] = get_lang('No'); |
||
| 226 | } |
||
| 227 | |||
| 228 | $userWithResults[$result['excruid']] = 1; |
||
| 229 | $i++; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | if ($this->includeAllUsers) { |
||
| 234 | $latestId = count($return); |
||
| 235 | $userWithResults = array_keys($userWithResults); |
||
| 236 | |||
| 237 | if (!empty($students)) { |
||
| 238 | foreach ($students as $student) { |
||
|
|
|||
| 239 | if (!in_array($student['user_id'], $userWithResults)) { |
||
| 240 | $i = $latestId; |
||
| 241 | $isWestern = api_is_western_name_order(); |
||
| 242 | |||
| 243 | if (empty($user_id)) { |
||
| 244 | $return[$i]['official_code'] = $student['official_code']; |
||
| 245 | if ($isWestern) { |
||
| 246 | $return[$i]['first_name'] = $student['firstname']; |
||
| 247 | $return[$i]['last_name'] = $student['lastname']; |
||
| 248 | } else { |
||
| 249 | $return[$i]['first_name'] = $student['lastname']; |
||
| 250 | $return[$i]['last_name'] = $student['firstname']; |
||
| 251 | } |
||
| 252 | |||
| 253 | $return[$i]['user_id'] = $student['user_id']; |
||
| 254 | $return[$i]['email'] = $student['email']; |
||
| 255 | } |
||
| 256 | $return[$i]['title'] = null; |
||
| 257 | $return[$i]['start_date'] = null; |
||
| 258 | $return[$i]['end_date'] = null; |
||
| 259 | $return[$i]['duration'] = null; |
||
| 260 | $return[$i]['result'] = null; |
||
| 261 | $return[$i]['max'] = null; |
||
| 262 | $return[$i]['status'] = get_lang('NotAttempted'); |
||
| 263 | $return[$i]['lp_id'] = null; |
||
| 264 | $return[$i]['lp_name'] = null; |
||
| 265 | $return[$i]['is_user_subscribed'] = get_lang('Yes'); |
||
| 266 | |||
| 267 | $latestId++; |
||
| 268 | } |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | $this->results = $return; |
||
| 274 | |||
| 275 | return true; |
||
| 276 | } |
||
| 277 | |||
| 673 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.