| Total Complexity | 73 |
| Total Lines | 735 |
| 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 |
||
| 11 | class ExerciseResult |
||
| 12 | { |
||
| 13 | public $includeAllUsers = false; |
||
| 14 | public $onlyBestAttempts = false; |
||
| 15 | private $results = []; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @param bool $includeAllUsers |
||
| 19 | */ |
||
| 20 | public function setIncludeAllUsers($includeAllUsers) |
||
| 21 | { |
||
| 22 | $this->includeAllUsers = $includeAllUsers; |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param bool $value |
||
| 27 | */ |
||
| 28 | public function setOnlyBestAttempts($value) |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Gets the results of all students (or just one student if access is limited). |
||
| 35 | * |
||
| 36 | * @param string $document_path The document path (for HotPotatoes retrieval) |
||
| 37 | * @param int $user_id User ID. Optional. If no user ID is provided, we take all the results. Defauts to null |
||
| 38 | * @param int $filter |
||
| 39 | * @param int $exercise_id |
||
| 40 | * |
||
| 41 | * @return bool |
||
| 42 | */ |
||
| 43 | public function getExercisesReporting( |
||
| 44 | $document_path, |
||
| 45 | $user_id = null, |
||
| 46 | $filter = 0, |
||
| 47 | $exercise_id = 0 |
||
| 48 | ) { |
||
| 49 | $return = []; |
||
| 50 | $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST); |
||
| 51 | $TBL_TABLE_LP_MAIN = Database::get_course_table(TABLE_LP_MAIN); |
||
| 52 | $TBL_USER = Database::get_main_table(TABLE_MAIN_USER); |
||
| 53 | $TBL_TRACK_EXERCISES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 54 | $TBL_TRACK_ATTEMPT_RECORDING = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING); |
||
| 55 | |||
| 56 | $cid = api_get_course_id(); |
||
| 57 | $course_id = api_get_course_int_id(); |
||
| 58 | $user_id = (int) $user_id; |
||
| 59 | $sessionId = api_get_session_id(); |
||
| 60 | $session_id_and = ' AND te.session_id = '.$sessionId.' '; |
||
| 61 | $exercise_id = (int) $exercise_id; |
||
| 62 | |||
| 63 | if (empty($sessionId) && |
||
| 64 | api_get_configuration_value('show_exercise_session_attempts_in_base_course') |
||
| 65 | ) { |
||
| 66 | $session_id_and = ''; |
||
| 67 | } |
||
| 68 | |||
| 69 | if (!empty($exercise_id)) { |
||
| 70 | $session_id_and .= " AND exe_exo_id = $exercise_id "; |
||
| 71 | } |
||
| 72 | |||
| 73 | if (empty($user_id)) { |
||
| 74 | $user_id_and = null; |
||
| 75 | $sql = "SELECT |
||
| 76 | firstname, |
||
| 77 | lastname, |
||
| 78 | official_code, |
||
| 79 | ce.title as extitle, |
||
| 80 | ce.pass_percentage as expasspercentage, |
||
| 81 | te.exe_result as exresult , |
||
| 82 | te.exe_weighting as exweight, |
||
| 83 | te.exe_date as exdate, |
||
| 84 | te.exe_id as exid, |
||
| 85 | email as exemail, |
||
| 86 | te.start_date as exstart, |
||
| 87 | steps_counter as exstep, |
||
| 88 | exe_user_id as excruid, |
||
| 89 | te.exe_duration as duration, |
||
| 90 | te.orig_lp_id as orig_lp_id, |
||
| 91 | tlm.name as lp_name, |
||
| 92 | user.username, |
||
| 93 | te.status as exstatus |
||
| 94 | FROM $TBL_EXERCISES AS ce |
||
| 95 | INNER JOIN $TBL_TRACK_EXERCISES AS te |
||
| 96 | ON (te.exe_exo_id = ce.iid) |
||
| 97 | INNER JOIN $TBL_USER AS user |
||
| 98 | ON (user.user_id = exe_user_id) |
||
| 99 | LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm |
||
| 100 | ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id) |
||
| 101 | WHERE |
||
| 102 | ce.c_id = $course_id AND |
||
| 103 | te.c_id = ce.c_id $user_id_and $session_id_and AND |
||
| 104 | ce.active <> -1"; |
||
| 105 | } else { |
||
| 106 | $user_id_and = ' AND te.exe_user_id = '.api_get_user_id().' '; |
||
| 107 | $orderBy = 'lastname'; |
||
| 108 | if (api_is_western_name_order()) { |
||
| 109 | $orderBy = 'firstname'; |
||
| 110 | } |
||
| 111 | // get only this user's results |
||
| 112 | $sql = "SELECT |
||
| 113 | firstname, |
||
| 114 | lastname, |
||
| 115 | official_code, |
||
| 116 | ce.title as extitle, |
||
| 117 | ce.pass_percentage as expasspercentage, |
||
| 118 | te.exe_result as exresult, |
||
| 119 | te.exe_weighting as exweight, |
||
| 120 | te.exe_date as exdate, |
||
| 121 | te.exe_id as exid, |
||
| 122 | email as exemail, |
||
| 123 | te.start_date as exstart, |
||
| 124 | steps_counter as exstep, |
||
| 125 | exe_user_id as excruid, |
||
| 126 | te.exe_duration as duration, |
||
| 127 | ce.results_disabled as exdisabled, |
||
| 128 | te.orig_lp_id as orig_lp_id, |
||
| 129 | tlm.name as lp_name, |
||
| 130 | user.username, |
||
| 131 | te.status as exstatus |
||
| 132 | FROM $TBL_EXERCISES AS ce |
||
| 133 | INNER JOIN $TBL_TRACK_EXERCISES AS te |
||
| 134 | ON (te.exe_exo_id = ce.iid) |
||
| 135 | INNER JOIN $TBL_USER AS user |
||
| 136 | ON (user.user_id = exe_user_id) |
||
| 137 | LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm |
||
| 138 | ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id) |
||
| 139 | WHERE |
||
| 140 | ce.c_id = $course_id AND |
||
| 141 | te.c_id = ce.c_id $user_id_and $session_id_and AND |
||
| 142 | ce.active <>-1 AND |
||
| 143 | ORDER BY $orderBy, te.c_id ASC, ce.title ASC, te.exe_date DESC"; |
||
| 144 | } |
||
| 145 | |||
| 146 | $results = []; |
||
| 147 | $resx = Database::query($sql); |
||
| 148 | $bestAttemptPerUser = []; |
||
| 149 | while ($rowx = Database::fetch_array($resx, 'ASSOC')) { |
||
| 150 | if ($this->onlyBestAttempts) { |
||
| 151 | if (!isset($bestAttemptPerUser[$rowx['excruid']])) { |
||
| 152 | $bestAttemptPerUser[$rowx['excruid']] = $rowx; |
||
| 153 | } else { |
||
| 154 | if ($rowx['exresult'] > $bestAttemptPerUser[$rowx['excruid']]['exresult']) { |
||
| 155 | $bestAttemptPerUser[$rowx['excruid']] = $rowx; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } else { |
||
| 159 | $results[] = $rowx; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | if ($this->onlyBestAttempts) { |
||
| 164 | // Remove userId indexes to avoid issues in output |
||
| 165 | foreach ($bestAttemptPerUser as $attempt) { |
||
| 166 | $results[] = $attempt; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | $filter_by_not_revised = false; |
||
| 171 | $filter_by_revised = false; |
||
| 172 | |||
| 173 | if ($filter) { |
||
| 174 | switch ($filter) { |
||
| 175 | case 1: |
||
| 176 | $filter_by_not_revised = true; |
||
| 177 | break; |
||
| 178 | case 2: |
||
| 179 | $filter_by_revised = true; |
||
| 180 | break; |
||
| 181 | default: |
||
| 182 | null; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | if (empty($sessionId)) { |
||
| 187 | $students = CourseManager::get_user_list_from_course_code($cid); |
||
| 188 | } else { |
||
| 189 | $students = CourseManager::get_user_list_from_course_code($cid, $sessionId); |
||
| 190 | } |
||
| 191 | $studentsUserIdList = array_keys($students); |
||
| 192 | |||
| 193 | // Print the results of tests |
||
| 194 | $userWithResults = []; |
||
| 195 | if (is_array($results)) { |
||
| 196 | $i = 0; |
||
| 197 | foreach ($results as $result) { |
||
| 198 | $revised = 0; |
||
| 199 | if ($result['exstatus'] === 'incomplete') { |
||
| 200 | $revised = -1; |
||
| 201 | } else { |
||
| 202 | //revised or not |
||
| 203 | $sql_exe = "SELECT exe_id |
||
| 204 | FROM $TBL_TRACK_ATTEMPT_RECORDING |
||
| 205 | WHERE |
||
| 206 | author != '' AND |
||
| 207 | exe_id = ".intval($result['exid'])." |
||
| 208 | LIMIT 1"; |
||
| 209 | $query = Database::query($sql_exe); |
||
| 210 | |||
| 211 | if (Database::num_rows($query) > 0) { |
||
| 212 | $revised = 1; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | if ($filter_by_not_revised && $revised === 1) { |
||
| 217 | continue; |
||
| 218 | } |
||
| 219 | |||
| 220 | if ($filter_by_revised && $revised < 1) { |
||
| 221 | continue; |
||
| 222 | } |
||
| 223 | |||
| 224 | $return[$i] = []; |
||
| 225 | if (empty($user_id)) { |
||
| 226 | $return[$i]['official_code'] = $result['official_code']; |
||
| 227 | $return[$i]['firstname'] = $results[$i]['firstname']; |
||
| 228 | $return[$i]['lastname'] = $results[$i]['lastname']; |
||
| 229 | $return[$i]['user_id'] = $results[$i]['excruid']; |
||
| 230 | $return[$i]['email'] = $results[$i]['exemail']; |
||
| 231 | $return[$i]['username'] = $results[$i]['username']; |
||
| 232 | } |
||
| 233 | $return[$i]['title'] = $result['extitle']; |
||
| 234 | $return[$i]['minimun'] = $result['expasspercentage'] |
||
| 235 | ? float_format($result['expasspercentage'] / 100 * $result['exweight']) |
||
| 236 | : 0; |
||
| 237 | $return[$i]['start_date'] = api_get_local_time($result['exstart']); |
||
| 238 | $return[$i]['end_date'] = api_get_local_time($result['exdate']); |
||
| 239 | $return[$i]['duration'] = $result['duration']; |
||
| 240 | $return[$i]['result'] = $result['exresult']; |
||
| 241 | $return[$i]['max'] = $result['exweight']; |
||
| 242 | // Revised: 1 = revised, 0 = not revised, -1 = not even finished by user |
||
| 243 | $return[$i]['status'] = $revised === 1 ? get_lang('Validated') : ($revised === 0 ? get_lang('NotValidated') : get_lang('Unclosed')); |
||
| 244 | $return[$i]['lp_id'] = $result['orig_lp_id']; |
||
| 245 | $return[$i]['lp_name'] = $result['lp_name']; |
||
| 246 | |||
| 247 | if (in_array($result['excruid'], $studentsUserIdList)) { |
||
| 248 | $return[$i]['is_user_subscribed'] = get_lang('Yes'); |
||
| 249 | } else { |
||
| 250 | $return[$i]['is_user_subscribed'] = get_lang('No'); |
||
| 251 | } |
||
| 252 | |||
| 253 | $userWithResults[$result['excruid']] = 1; |
||
| 254 | $i++; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | if ($this->includeAllUsers) { |
||
| 259 | $latestId = count($return); |
||
| 260 | $userWithResults = array_keys($userWithResults); |
||
| 261 | |||
| 262 | if (!empty($students)) { |
||
| 263 | foreach ($students as $student) { |
||
| 264 | if (!in_array($student['user_id'], $userWithResults)) { |
||
| 265 | $i = $latestId; |
||
| 266 | $isWestern = api_is_western_name_order(); |
||
| 267 | |||
| 268 | if (empty($user_id)) { |
||
| 269 | $return[$i]['official_code'] = $student['official_code']; |
||
| 270 | $return[$i]['firstname'] = $student['firstname']; |
||
| 271 | $return[$i]['lastname'] = $student['lastname']; |
||
| 272 | $return[$i]['user_id'] = $student['user_id']; |
||
| 273 | $return[$i]['email'] = $student['email']; |
||
| 274 | $return[$i]['username'] = $student['username']; |
||
| 275 | } |
||
| 276 | $return[$i]['title'] = null; |
||
| 277 | $return[$i]['minimun'] = null; |
||
| 278 | $return[$i]['start_date'] = null; |
||
| 279 | $return[$i]['end_date'] = null; |
||
| 280 | $return[$i]['duration'] = null; |
||
| 281 | $return[$i]['result'] = null; |
||
| 282 | $return[$i]['max'] = null; |
||
| 283 | $return[$i]['status'] = get_lang('NotAttempted'); |
||
| 284 | $return[$i]['lp_id'] = null; |
||
| 285 | $return[$i]['lp_name'] = null; |
||
| 286 | $return[$i]['is_user_subscribed'] = get_lang('Yes'); |
||
| 287 | |||
| 288 | $latestId++; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | $this->results = $return; |
||
| 295 | |||
| 296 | return true; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Exports the complete report as a CSV file. |
||
| 301 | * |
||
| 302 | * @param string $document_path Document path inside the document tool |
||
| 303 | * @param int $user_id Optional user ID |
||
| 304 | * @param bool $export_user_fields Whether to include user fields or not |
||
| 305 | * @param int $export_filter |
||
| 306 | * @param int $exercise_id |
||
| 307 | * |
||
| 308 | * @return bool False on error |
||
| 309 | */ |
||
| 310 | public function exportCompleteReportCSV( |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Exports the complete report as an XLS file. |
||
| 462 | * |
||
| 463 | * @param string $document_path |
||
| 464 | * @param null $user_id |
||
|
|
|||
| 465 | * @param bool $export_user_fields |
||
| 466 | * @param int $export_filter |
||
| 467 | * @param int $exercise_id |
||
| 468 | * @param null $hotpotato_name |
||
| 469 | * |
||
| 470 | * @return bool |
||
| 471 | */ |
||
| 472 | public function exportCompleteReportXLS( |
||
| 748 |