| Total Complexity | 71 |
| Total Lines | 689 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ExerciseResult often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ExerciseResult, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class ExerciseResult |
||
| 11 | { |
||
| 12 | private $results = []; |
||
| 13 | public $includeAllUsers = false; |
||
| 14 | public $onlyBestAttempts = false; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @param bool $includeAllUsers |
||
| 18 | */ |
||
| 19 | public function setIncludeAllUsers($includeAllUsers) |
||
| 20 | { |
||
| 21 | $this->includeAllUsers = $includeAllUsers; |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param bool $value |
||
| 26 | */ |
||
| 27 | public function setOnlyBestAttempts($value) |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Gets the results of all students (or just one student if access is limited) |
||
| 34 | * |
||
| 35 | * @param string $document_path The document path (for HotPotatoes retrieval) |
||
| 36 | * @param int $user_id User ID. Optional. If no user ID is provided, we take all the results. Defauts to null |
||
| 37 | * @param int $filter |
||
| 38 | * @param int $exercise_id |
||
| 39 | * |
||
| 40 | * @return bool |
||
| 41 | */ |
||
| 42 | public function getExercisesReporting( |
||
| 43 | $document_path, |
||
|
|
|||
| 44 | $user_id = null, |
||
| 45 | $filter = 0, |
||
| 46 | $exercise_id = 0 |
||
| 47 | ) { |
||
| 48 | $return = []; |
||
| 49 | $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST); |
||
| 50 | $TBL_TABLE_LP_MAIN = Database::get_course_table(TABLE_LP_MAIN); |
||
| 51 | $TBL_USER = Database::get_main_table(TABLE_MAIN_USER); |
||
| 52 | $TBL_TRACK_EXERCISES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 53 | $TBL_TRACK_ATTEMPT_RECORDING = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING); |
||
| 54 | |||
| 55 | $cid = api_get_course_id(); |
||
| 56 | $course_id = api_get_course_int_id(); |
||
| 57 | $user_id = intval($user_id); |
||
| 58 | $sessionId = api_get_session_id(); |
||
| 59 | $session_id_and = ' AND te.session_id = '.$sessionId.' '; |
||
| 60 | $exercise_id = intval($exercise_id); |
||
| 61 | |||
| 62 | if (!empty($exercise_id)) { |
||
| 63 | $session_id_and .= " AND exe_exo_id = $exercise_id "; |
||
| 64 | } |
||
| 65 | |||
| 66 | if (empty($user_id)) { |
||
| 67 | $user_id_and = null; |
||
| 68 | $sql = "SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").", |
||
| 69 | official_code, |
||
| 70 | ce.title as extitle, |
||
| 71 | te.exe_result as exresult , |
||
| 72 | te.exe_weighting as exweight, |
||
| 73 | te.exe_date as exdate, |
||
| 74 | te.exe_id as exid, |
||
| 75 | email as exemail, |
||
| 76 | te.start_date as exstart, |
||
| 77 | steps_counter as exstep, |
||
| 78 | exe_user_id as excruid, |
||
| 79 | te.exe_duration as duration, |
||
| 80 | te.orig_lp_id as orig_lp_id, |
||
| 81 | tlm.name as lp_name |
||
| 82 | FROM $TBL_EXERCISES AS ce |
||
| 83 | INNER JOIN $TBL_TRACK_EXERCISES AS te |
||
| 84 | ON (te.exe_exo_id = ce.id) |
||
| 85 | INNER JOIN $TBL_USER AS user |
||
| 86 | ON (user.user_id = exe_user_id) |
||
| 87 | LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm |
||
| 88 | ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id) |
||
| 89 | WHERE |
||
| 90 | ce.c_id = $course_id AND |
||
| 91 | te.status != 'incomplete' AND |
||
| 92 | te.c_id = ce.c_id $user_id_and $session_id_and AND |
||
| 93 | ce.active <>-1"; |
||
| 94 | } else { |
||
| 95 | $user_id_and = ' AND te.exe_user_id = '.api_get_user_id().' '; |
||
| 96 | // get only this user's results |
||
| 97 | $sql = "SELECT ".(api_is_western_name_order() ? "firstname as userpart1, lastname userpart2" : "lastname as userpart1, firstname as userpart2").", |
||
| 98 | official_code, |
||
| 99 | ce.title as extitle, |
||
| 100 | te.exe_result as exresult, |
||
| 101 | te.exe_weighting as exweight, |
||
| 102 | te.exe_date as exdate, |
||
| 103 | te.exe_id as exid, |
||
| 104 | email as exemail, |
||
| 105 | te.start_date as exstart, |
||
| 106 | steps_counter as exstep, |
||
| 107 | exe_user_id as excruid, |
||
| 108 | te.exe_duration as duration, |
||
| 109 | ce.results_disabled as exdisabled, |
||
| 110 | te.orig_lp_id as orig_lp_id, |
||
| 111 | tlm.name as lp_name |
||
| 112 | FROM $TBL_EXERCISES AS ce |
||
| 113 | INNER JOIN $TBL_TRACK_EXERCISES AS te |
||
| 114 | ON (te.exe_exo_id = ce.id) |
||
| 115 | INNER JOIN $TBL_USER AS user |
||
| 116 | ON (user.user_id = exe_user_id) |
||
| 117 | LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm |
||
| 118 | ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id) |
||
| 119 | WHERE |
||
| 120 | ce.c_id = $course_id AND |
||
| 121 | te.status != 'incomplete' AND |
||
| 122 | te.c_id = ce.c_id $user_id_and $session_id_and AND |
||
| 123 | ce.active <>-1 AND |
||
| 124 | ORDER BY userpart2, te.c_id ASC, ce.title ASC, te.exe_date DESC"; |
||
| 125 | } |
||
| 126 | |||
| 127 | $results = []; |
||
| 128 | $resx = Database::query($sql); |
||
| 129 | $bestAttemptPerUser = []; |
||
| 130 | while ($rowx = Database::fetch_array($resx, 'ASSOC')) { |
||
| 131 | if ($this->onlyBestAttempts) { |
||
| 132 | if (!isset($bestAttemptPerUser[$rowx['excruid']])) { |
||
| 133 | $bestAttemptPerUser[$rowx['excruid']] = $rowx; |
||
| 134 | } else { |
||
| 135 | if ($rowx['exresult'] > $bestAttemptPerUser[$rowx['excruid']]['exresult']) { |
||
| 136 | $bestAttemptPerUser[$rowx['excruid']] = $rowx; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } else { |
||
| 140 | $results[] = $rowx; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | if ($this->onlyBestAttempts) { |
||
| 145 | // Remove userId indexes to avoid issues in output |
||
| 146 | foreach ($bestAttemptPerUser as $attempt) { |
||
| 147 | $results[] = $attempt; |
||
| 148 | } |
||
| 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 = []; |
||
| 176 | if (is_array($results)) { |
||
| 177 | $i = 0; |
||
| 178 | foreach ($results as $result) { |
||
| 179 | $revised = false; |
||
| 180 | //revised or not |
||
| 181 | $sql_exe = "SELECT exe_id |
||
| 182 | FROM $TBL_TRACK_ATTEMPT_RECORDING |
||
| 183 | WHERE |
||
| 184 | author != '' AND |
||
| 185 | exe_id = ".Database::escape_string($result['exid'])." |
||
| 186 | LIMIT 1"; |
||
| 187 | $query = Database::query($sql_exe); |
||
| 188 | |||
| 189 | if (Database:: num_rows($query) > 0) { |
||
| 190 | $revised = true; |
||
| 191 | } |
||
| 192 | |||
| 193 | if ($filter_by_not_revised && $revised) { |
||
| 194 | continue; |
||
| 195 | } |
||
| 196 | |||
| 197 | if ($filter_by_revised && !$revised) { |
||
| 198 | continue; |
||
| 199 | } |
||
| 200 | |||
| 201 | $return[$i] = []; |
||
| 202 | if (empty($user_id)) { |
||
| 203 | $return[$i]['official_code'] = $result['official_code']; |
||
| 204 | if (api_is_western_name_order()) { |
||
| 205 | $return[$i]['first_name'] = $results[$i]['userpart1']; |
||
| 206 | $return[$i]['last_name'] = $results[$i]['userpart2']; |
||
| 207 | } else { |
||
| 208 | $return[$i]['first_name'] = $results[$i]['userpart2']; |
||
| 209 | $return[$i]['last_name'] = $results[$i]['userpart1']; |
||
| 210 | } |
||
| 211 | $return[$i]['user_id'] = $results[$i]['excruid']; |
||
| 212 | $return[$i]['email'] = $results[$i]['exemail']; |
||
| 213 | } |
||
| 214 | $return[$i]['title'] = $result['extitle']; |
||
| 215 | $return[$i]['start_date'] = api_get_local_time($result['exstart']); |
||
| 216 | $return[$i]['end_date'] = api_get_local_time($result['exdate']); |
||
| 217 | $return[$i]['duration'] = $result['duration']; |
||
| 218 | $return[$i]['result'] = $result['exresult']; |
||
| 219 | $return[$i]['max'] = $result['exweight']; |
||
| 220 | $return[$i]['status'] = $revised ? get_lang('Validated') : get_lang('NotValidated'); |
||
| 221 | $return[$i]['lp_id'] = $result['orig_lp_id']; |
||
| 222 | $return[$i]['lp_name'] = $result['lp_name']; |
||
| 223 | |||
| 224 | if (in_array($result['excruid'], $studentsUserIdList)) { |
||
| 225 | $return[$i]['is_user_subscribed'] = get_lang('Yes'); |
||
| 226 | } else { |
||
| 227 | $return[$i]['is_user_subscribed'] = get_lang('No'); |
||
| 228 | } |
||
| 229 | |||
| 230 | $userWithResults[$result['excruid']] = 1; |
||
| 231 | $i++; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | if ($this->includeAllUsers) { |
||
| 236 | $latestId = count($return); |
||
| 237 | $userWithResults = array_keys($userWithResults); |
||
| 238 | |||
| 239 | if (!empty($students)) { |
||
| 240 | foreach ($students as $student) { |
||
| 241 | if (!in_array($student['user_id'], $userWithResults)) { |
||
| 242 | $i = $latestId; |
||
| 243 | $isWestern = api_is_western_name_order(); |
||
| 244 | |||
| 245 | if (empty($user_id)) { |
||
| 246 | $return[$i]['official_code'] = $student['official_code']; |
||
| 247 | if ($isWestern) { |
||
| 248 | $return[$i]['first_name'] = $student['firstname']; |
||
| 249 | $return[$i]['last_name'] = $student['lastname']; |
||
| 250 | } else { |
||
| 251 | $return[$i]['first_name'] = $student['lastname']; |
||
| 252 | $return[$i]['last_name'] = $student['firstname']; |
||
| 253 | } |
||
| 254 | |||
| 255 | $return[$i]['user_id'] = $student['user_id']; |
||
| 256 | $return[$i]['email'] = $student['email']; |
||
| 257 | } |
||
| 258 | $return[$i]['title'] = null; |
||
| 259 | $return[$i]['start_date'] = null; |
||
| 260 | $return[$i]['end_date'] = null; |
||
| 261 | $return[$i]['duration'] = null; |
||
| 262 | $return[$i]['result'] = null; |
||
| 263 | $return[$i]['max'] = null; |
||
| 264 | $return[$i]['status'] = get_lang('NotAttempted'); |
||
| 265 | $return[$i]['lp_id'] = null; |
||
| 266 | $return[$i]['lp_name'] = null; |
||
| 267 | $return[$i]['is_user_subscribed'] = get_lang('Yes'); |
||
| 268 | |||
| 269 | $latestId++; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | $this->results = $return; |
||
| 276 | |||
| 277 | return true; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Exports the complete report as a CSV file |
||
| 282 | * @param string $document_path Document path inside the document tool |
||
| 283 | * @param integer $user_id Optional user ID |
||
| 284 | * @param boolean $export_user_fields Whether to include user fields or not |
||
| 285 | * @param int $export_filter |
||
| 286 | * @param int $exercise_id |
||
| 287 | * |
||
| 288 | * @return boolean False on error |
||
| 289 | */ |
||
| 290 | public function exportCompleteReportCSV( |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Exports the complete report as an XLS file |
||
| 434 | * |
||
| 435 | * @param string $document_path |
||
| 436 | * @param null $user_id |
||
| 437 | * @param bool $export_user_fields |
||
| 438 | * @param int $export_filter |
||
| 439 | * @param int $exercise_id |
||
| 440 | * @param null $hotpotato_name |
||
| 441 | * @return bool |
||
| 442 | */ |
||
| 443 | public function exportCompleteReportXLS( |
||
| 701 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.