Total Complexity | 72 |
Total Lines | 723 |
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 | te.exe_result as exresult , |
||
81 | te.exe_weighting as exweight, |
||
82 | te.exe_date as exdate, |
||
83 | te.exe_id as exid, |
||
84 | email as exemail, |
||
85 | te.start_date as exstart, |
||
86 | steps_counter as exstep, |
||
87 | exe_user_id as excruid, |
||
88 | te.exe_duration as duration, |
||
89 | te.orig_lp_id as orig_lp_id, |
||
90 | tlm.name as lp_name, |
||
91 | user.username, |
||
92 | te.status as exstatus |
||
93 | FROM $TBL_EXERCISES AS ce |
||
94 | INNER JOIN $TBL_TRACK_EXERCISES AS te |
||
95 | ON (te.exe_exo_id = ce.iid) |
||
96 | INNER JOIN $TBL_USER AS user |
||
97 | ON (user.user_id = exe_user_id) |
||
98 | LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm |
||
99 | ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id) |
||
100 | WHERE |
||
101 | ce.c_id = $course_id AND |
||
102 | te.c_id = ce.c_id $user_id_and $session_id_and AND |
||
103 | ce.active <> -1"; |
||
104 | } else { |
||
105 | $user_id_and = ' AND te.exe_user_id = '.api_get_user_id().' '; |
||
106 | $orderBy = 'lastname'; |
||
107 | if (api_is_western_name_order()) { |
||
108 | $orderBy = 'firstname'; |
||
109 | } |
||
110 | // get only this user's results |
||
111 | $sql = "SELECT |
||
112 | firstname, |
||
113 | lastname, |
||
114 | official_code, |
||
115 | ce.title as extitle, |
||
116 | te.exe_result as exresult, |
||
117 | te.exe_weighting as exweight, |
||
118 | te.exe_date as exdate, |
||
119 | te.exe_id as exid, |
||
120 | email as exemail, |
||
121 | te.start_date as exstart, |
||
122 | steps_counter as exstep, |
||
123 | exe_user_id as excruid, |
||
124 | te.exe_duration as duration, |
||
125 | ce.results_disabled as exdisabled, |
||
126 | te.orig_lp_id as orig_lp_id, |
||
127 | tlm.name as lp_name, |
||
128 | user.username, |
||
129 | te.status as exstatus |
||
130 | FROM $TBL_EXERCISES AS ce |
||
131 | INNER JOIN $TBL_TRACK_EXERCISES AS te |
||
132 | ON (te.exe_exo_id = ce.iid) |
||
133 | INNER JOIN $TBL_USER AS user |
||
134 | ON (user.user_id = exe_user_id) |
||
135 | LEFT JOIN $TBL_TABLE_LP_MAIN AS tlm |
||
136 | ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id) |
||
137 | WHERE |
||
138 | ce.c_id = $course_id AND |
||
139 | te.c_id = ce.c_id $user_id_and $session_id_and AND |
||
140 | ce.active <>-1 AND |
||
141 | ORDER BY $orderBy, te.c_id ASC, ce.title ASC, te.exe_date DESC"; |
||
142 | } |
||
143 | |||
144 | $results = []; |
||
145 | $resx = Database::query($sql); |
||
146 | $bestAttemptPerUser = []; |
||
147 | while ($rowx = Database::fetch_array($resx, 'ASSOC')) { |
||
148 | if ($this->onlyBestAttempts) { |
||
149 | if (!isset($bestAttemptPerUser[$rowx['excruid']])) { |
||
150 | $bestAttemptPerUser[$rowx['excruid']] = $rowx; |
||
151 | } else { |
||
152 | if ($rowx['exresult'] > $bestAttemptPerUser[$rowx['excruid']]['exresult']) { |
||
153 | $bestAttemptPerUser[$rowx['excruid']] = $rowx; |
||
154 | } |
||
155 | } |
||
156 | } else { |
||
157 | $results[] = $rowx; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | if ($this->onlyBestAttempts) { |
||
162 | // Remove userId indexes to avoid issues in output |
||
163 | foreach ($bestAttemptPerUser as $attempt) { |
||
164 | $results[] = $attempt; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | $filter_by_not_revised = false; |
||
169 | $filter_by_revised = false; |
||
170 | |||
171 | if ($filter) { |
||
172 | switch ($filter) { |
||
173 | case 1: |
||
174 | $filter_by_not_revised = true; |
||
175 | break; |
||
176 | case 2: |
||
177 | $filter_by_revised = true; |
||
178 | break; |
||
179 | default: |
||
180 | null; |
||
181 | } |
||
182 | } |
||
183 | |||
184 | if (empty($sessionId)) { |
||
185 | $students = CourseManager::get_user_list_from_course_code($cid); |
||
186 | } else { |
||
187 | $students = CourseManager::get_user_list_from_course_code($cid, $sessionId); |
||
188 | } |
||
189 | $studentsUserIdList = array_keys($students); |
||
190 | |||
191 | // Print the results of tests |
||
192 | $userWithResults = []; |
||
193 | if (is_array($results)) { |
||
194 | $i = 0; |
||
195 | foreach ($results as $result) { |
||
196 | $revised = 0; |
||
197 | if ($result['exstatus'] === 'incomplete') { |
||
198 | $revised = -1; |
||
199 | } else { |
||
200 | //revised or not |
||
201 | $sql_exe = "SELECT exe_id |
||
202 | FROM $TBL_TRACK_ATTEMPT_RECORDING |
||
203 | WHERE |
||
204 | author != '' AND |
||
205 | exe_id = ".intval($result['exid'])." |
||
206 | LIMIT 1"; |
||
207 | $query = Database::query($sql_exe); |
||
208 | |||
209 | if (Database:: num_rows($query) > 0) { |
||
210 | $revised = 1; |
||
211 | } |
||
212 | } |
||
213 | |||
214 | if ($filter_by_not_revised && $revised === 1) { |
||
215 | continue; |
||
216 | } |
||
217 | |||
218 | if ($filter_by_revised && $revised < 1) { |
||
219 | continue; |
||
220 | } |
||
221 | |||
222 | $return[$i] = []; |
||
223 | if (empty($user_id)) { |
||
224 | $return[$i]['official_code'] = $result['official_code']; |
||
225 | $return[$i]['firstname'] = $results[$i]['firstname']; |
||
226 | $return[$i]['lastname'] = $results[$i]['lastname']; |
||
227 | $return[$i]['user_id'] = $results[$i]['excruid']; |
||
228 | $return[$i]['email'] = $results[$i]['exemail']; |
||
229 | $return[$i]['username'] = $results[$i]['username']; |
||
230 | } |
||
231 | $return[$i]['title'] = $result['extitle']; |
||
232 | $return[$i]['start_date'] = api_get_local_time($result['exstart']); |
||
233 | $return[$i]['end_date'] = api_get_local_time($result['exdate']); |
||
234 | $return[$i]['duration'] = $result['duration']; |
||
235 | $return[$i]['result'] = $result['exresult']; |
||
236 | $return[$i]['max'] = $result['exweight']; |
||
237 | // Revised: 1 = revised, 0 = not revised, -1 = not even finished by user |
||
238 | $return[$i]['status'] = $revised === 1 ? get_lang('Validated') : ($revised === 0 ? get_lang('NotValidated') : get_lang('Unclosed')); |
||
239 | $return[$i]['lp_id'] = $result['orig_lp_id']; |
||
240 | $return[$i]['lp_name'] = $result['lp_name']; |
||
241 | |||
242 | if (in_array($result['excruid'], $studentsUserIdList)) { |
||
243 | $return[$i]['is_user_subscribed'] = get_lang('Yes'); |
||
244 | } else { |
||
245 | $return[$i]['is_user_subscribed'] = get_lang('No'); |
||
246 | } |
||
247 | |||
248 | $userWithResults[$result['excruid']] = 1; |
||
249 | $i++; |
||
250 | } |
||
251 | } |
||
252 | |||
253 | if ($this->includeAllUsers) { |
||
254 | $latestId = count($return); |
||
255 | $userWithResults = array_keys($userWithResults); |
||
256 | |||
257 | if (!empty($students)) { |
||
258 | foreach ($students as $student) { |
||
259 | if (!in_array($student['user_id'], $userWithResults)) { |
||
260 | $i = $latestId; |
||
261 | $isWestern = api_is_western_name_order(); |
||
262 | |||
263 | if (empty($user_id)) { |
||
264 | $return[$i]['official_code'] = $student['official_code']; |
||
265 | $return[$i]['firstname'] = $student['firstname']; |
||
266 | $return[$i]['lastname'] = $student['lastname']; |
||
267 | $return[$i]['user_id'] = $student['user_id']; |
||
268 | $return[$i]['email'] = $student['email']; |
||
269 | $return[$i]['username'] = $student['username']; |
||
270 | } |
||
271 | $return[$i]['title'] = null; |
||
272 | $return[$i]['start_date'] = null; |
||
273 | $return[$i]['end_date'] = null; |
||
274 | $return[$i]['duration'] = null; |
||
275 | $return[$i]['result'] = null; |
||
276 | $return[$i]['max'] = null; |
||
277 | $return[$i]['status'] = get_lang('NotAttempted'); |
||
278 | $return[$i]['lp_id'] = null; |
||
279 | $return[$i]['lp_name'] = null; |
||
280 | $return[$i]['is_user_subscribed'] = get_lang('Yes'); |
||
281 | |||
282 | $latestId++; |
||
283 | } |
||
284 | } |
||
285 | } |
||
286 | } |
||
287 | |||
288 | $this->results = $return; |
||
289 | |||
290 | return true; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Exports the complete report as a CSV file. |
||
295 | * |
||
296 | * @param string $document_path Document path inside the document tool |
||
297 | * @param int $user_id Optional user ID |
||
298 | * @param bool $export_user_fields Whether to include user fields or not |
||
299 | * @param int $export_filter |
||
300 | * @param int $exercise_id |
||
301 | * |
||
302 | * @return bool False on error |
||
303 | */ |
||
304 | public function exportCompleteReportCSV( |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * Exports the complete report as an XLS file. |
||
454 | * |
||
455 | * @param string $document_path |
||
456 | * @param null $user_id |
||
|
|||
457 | * @param bool $export_user_fields |
||
458 | * @param int $export_filter |
||
459 | * @param int $exercise_id |
||
460 | * @param null $hotpotato_name |
||
461 | * |
||
462 | * @return bool |
||
463 | */ |
||
464 | public function exportCompleteReportXLS( |
||
736 |