| Total Complexity | 79 |
| Total Lines | 553 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ExerciseLink 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 ExerciseLink, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class ExerciseLink extends AbstractLink |
||
| 13 | { |
||
| 14 | private $course_info; |
||
|
|
|||
| 15 | private $exercise_table; |
||
| 16 | private $exercise_data = []; |
||
| 17 | private $is_hp; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @param int $hp |
||
| 21 | */ |
||
| 22 | public function __construct($hp = 0) |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Generate an array of all exercises available. |
||
| 34 | * |
||
| 35 | * @param bool $getOnlyHotPotatoes |
||
| 36 | * |
||
| 37 | * @return array 2-dimensional array - every element contains 2 subelements (id, name) |
||
| 38 | */ |
||
| 39 | public function get_all_links($getOnlyHotPotatoes = false) |
||
| 40 | { |
||
| 41 | $tableItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 42 | $exerciseTable = $this->get_exercise_table(); |
||
| 43 | $lpItemTable = Database::get_course_table(TABLE_LP_ITEM); |
||
| 44 | |||
| 45 | $documentPath = api_get_path(SYS_COURSE_PATH).$this->course_code.'/document'; |
||
| 46 | if (empty($this->course_code)) { |
||
| 47 | return []; |
||
| 48 | } |
||
| 49 | $sessionId = $this->get_session_id(); |
||
| 50 | if (empty($sessionId)) { |
||
| 51 | $session_condition = api_get_session_condition(0, true); |
||
| 52 | } else { |
||
| 53 | $session_condition = api_get_session_condition($sessionId, true, true); |
||
| 54 | } |
||
| 55 | |||
| 56 | // @todo |
||
| 57 | $uploadPath = null; |
||
| 58 | |||
| 59 | $sql = 'SELECT iid, title FROM '.$exerciseTable.' |
||
| 60 | WHERE c_id = '.$this->course_id.' AND active=1 '.$session_condition; |
||
| 61 | |||
| 62 | $sqlLp = "SELECT e.iid, e.title |
||
| 63 | FROM $exerciseTable e |
||
| 64 | INNER JOIN $lpItemTable i |
||
| 65 | ON (e.c_id = i.c_id AND e.id = i.path) |
||
| 66 | WHERE |
||
| 67 | e.c_id = $this->course_id AND |
||
| 68 | active = 0 AND |
||
| 69 | item_type = 'quiz' |
||
| 70 | $session_condition"; |
||
| 71 | |||
| 72 | $exerciseInLP = []; |
||
| 73 | $result = Database::query($sql); |
||
| 74 | $resultLp = Database::query($sqlLp); |
||
| 75 | $exerciseInLP = Database::store_result($resultLp); |
||
| 76 | |||
| 77 | |||
| 78 | $cats = []; |
||
| 79 | if (isset($result)) { |
||
| 80 | if (Database::num_rows($result) > 0) { |
||
| 81 | while ($data = Database::fetch_array($result)) { |
||
| 82 | $cats[] = [$data['iid'], $data['title']]; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | if (!empty($exerciseInLP)) { |
||
| 88 | foreach ($exerciseInLP as $exercise) { |
||
| 89 | $cats[] = [ |
||
| 90 | $exercise['iid'], |
||
| 91 | $exercise['title'].' ('.get_lang('Learning path').')', |
||
| 92 | ]; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | return $cats; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Has anyone done this exercise yet ? |
||
| 101 | */ |
||
| 102 | public function has_results() |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get the score of this exercise. Only the first attempts are taken into account. |
||
| 121 | * |
||
| 122 | * @param int $stud_id student id (default: all students who have results - |
||
| 123 | * then the average is returned) |
||
| 124 | * @param string $type |
||
| 125 | * |
||
| 126 | * @return array (score, max) if student is given |
||
| 127 | * array (sum of scores, number of scores) otherwise |
||
| 128 | * or null if no scores available |
||
| 129 | */ |
||
| 130 | public function calc_score($stud_id = null, $type = null) |
||
| 131 | { |
||
| 132 | $allowStats = api_get_configuration_value('allow_gradebook_stats'); |
||
| 133 | |||
| 134 | if ($allowStats) { |
||
| 135 | $link = $this->entity; |
||
| 136 | if (!empty($link)) { |
||
| 137 | $weight = $link->getScoreWeight(); |
||
| 138 | |||
| 139 | switch ($type) { |
||
| 140 | case 'best': |
||
| 141 | $bestResult = $link->getBestScore(); |
||
| 142 | $result = [$bestResult, $weight]; |
||
| 143 | |||
| 144 | return $result; |
||
| 145 | break; |
||
| 146 | case 'average': |
||
| 147 | $count = count($this->getStudentList()); |
||
| 148 | if (empty($count)) { |
||
| 149 | $result = [0, $weight]; |
||
| 150 | |||
| 151 | return $result; |
||
| 152 | } |
||
| 153 | $sumResult = array_sum($link->getUserScoreList()); |
||
| 154 | $result = [$sumResult / $count, $weight]; |
||
| 155 | |||
| 156 | return $result; |
||
| 157 | break; |
||
| 158 | case 'ranking': |
||
| 159 | return [null, null]; |
||
| 160 | break; |
||
| 161 | default: |
||
| 162 | if (!empty($stud_id)) { |
||
| 163 | $scoreList = $link->getUserScoreList(); |
||
| 164 | $result = [0, $weight]; |
||
| 165 | if (isset($scoreList[$stud_id])) { |
||
| 166 | $result = [$scoreList[$stud_id], $weight]; |
||
| 167 | } |
||
| 168 | |||
| 169 | return $result; |
||
| 170 | } else { |
||
| 171 | $studentCount = count($this->getStudentList()); |
||
| 172 | $sumResult = array_sum($link->getUserScoreList()); |
||
| 173 | $result = [$sumResult, $studentCount]; |
||
| 174 | } |
||
| 175 | |||
| 176 | return $result; |
||
| 177 | break; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | $tblStats = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 183 | $tblHp = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES); |
||
| 184 | $tblDoc = Database::get_course_table(TABLE_DOCUMENT); |
||
| 185 | |||
| 186 | /* the following query should be similar (in conditions) to the one used |
||
| 187 | in exercise/exercise.php, look for note-query-exe-results marker*/ |
||
| 188 | $sessionId = $this->get_session_id(); |
||
| 189 | $courseId = $this->getCourseId(); |
||
| 190 | $exerciseData = $this->get_exercise_data(); |
||
| 191 | |||
| 192 | $exerciseId = isset($exerciseData['id']) ? (int) $exerciseData['id'] : 0; |
||
| 193 | $stud_id = (int) $stud_id; |
||
| 194 | |||
| 195 | if (empty($exerciseId)) { |
||
| 196 | return null; |
||
| 197 | } |
||
| 198 | |||
| 199 | $key = 'exercise_link_id:'. |
||
| 200 | $this->get_id(). |
||
| 201 | 'exerciseId:'.$exerciseId.'student:'.$stud_id.'session:'.$sessionId.'courseId:'.$courseId.'type:'.$type; |
||
| 202 | |||
| 203 | $useCache = api_get_configuration_value('gradebook_use_apcu_cache'); |
||
| 204 | $cacheAvailable = api_get_configuration_value('apc') && $useCache; |
||
| 205 | $cacheDriver = null; |
||
| 206 | if ($cacheAvailable) { |
||
| 207 | $cacheDriver = new \Doctrine\Common\Cache\ApcuCache(); |
||
| 208 | if ($cacheDriver->contains($key)) { |
||
| 209 | return $cacheDriver->fetch($key); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | $exercise = new Exercise($courseId); |
||
| 214 | $exercise->read($exerciseId); |
||
| 215 | |||
| 216 | if (!$this->is_hp) { |
||
| 217 | if ($exercise->exercise_was_added_in_lp == false) { |
||
| 218 | $sql = "SELECT * FROM $tblStats |
||
| 219 | WHERE |
||
| 220 | exe_exo_id = $exerciseId AND |
||
| 221 | orig_lp_id = 0 AND |
||
| 222 | orig_lp_item_id = 0 AND |
||
| 223 | status <> 'incomplete' AND |
||
| 224 | session_id = $sessionId AND |
||
| 225 | c_id = $courseId |
||
| 226 | "; |
||
| 227 | } else { |
||
| 228 | $lpId = null; |
||
| 229 | if (!empty($exercise->lpList)) { |
||
| 230 | // Taking only the first LP |
||
| 231 | $lpId = current($exercise->lpList); |
||
| 232 | $lpId = $lpId['lp_id']; |
||
| 233 | } |
||
| 234 | |||
| 235 | $sql = "SELECT * |
||
| 236 | FROM $tblStats |
||
| 237 | WHERE |
||
| 238 | exe_exo_id = $exerciseId AND |
||
| 239 | orig_lp_id = $lpId AND |
||
| 240 | status <> 'incomplete' AND |
||
| 241 | session_id = $sessionId AND |
||
| 242 | c_id = $courseId "; |
||
| 243 | } |
||
| 244 | |||
| 245 | if (!empty($stud_id) && $type != 'ranking') { |
||
| 246 | $sql .= " AND exe_user_id = $stud_id "; |
||
| 247 | } |
||
| 248 | $sql .= ' ORDER BY exe_id DESC'; |
||
| 249 | } else { |
||
| 250 | $sql = "SELECT * FROM $tblHp hp |
||
| 251 | INNER JOIN $tblDoc doc |
||
| 252 | ON (hp.exe_name = doc.path AND doc.c_id = hp.c_id) |
||
| 253 | WHERE |
||
| 254 | hp.c_id = $courseId AND |
||
| 255 | doc.id = $exerciseId"; |
||
| 256 | |||
| 257 | if (!empty($stud_id)) { |
||
| 258 | $sql .= " AND hp.exe_user_id = $stud_id "; |
||
| 259 | } |
||
| 260 | } |
||
| 261 | |||
| 262 | $scores = Database::query($sql); |
||
| 263 | |||
| 264 | if (isset($stud_id) && empty($type)) { |
||
| 265 | // for 1 student |
||
| 266 | if ($data = Database::fetch_array($scores)) { |
||
| 267 | $attempts = Database::query($sql); |
||
| 268 | $counter = 0; |
||
| 269 | while ($attempt = Database::fetch_array($attempts)) { |
||
| 270 | $counter++; |
||
| 271 | } |
||
| 272 | $result = [$data['score'], $data['max_score'], $data['exe_date'], $counter]; |
||
| 273 | if ($cacheAvailable) { |
||
| 274 | $cacheDriver->save($key, $result); |
||
| 275 | } |
||
| 276 | |||
| 277 | return $result; |
||
| 278 | } else { |
||
| 279 | if ($cacheAvailable) { |
||
| 280 | $cacheDriver->save($key, null); |
||
| 281 | } |
||
| 282 | |||
| 283 | return null; |
||
| 284 | } |
||
| 285 | } else { |
||
| 286 | // all students -> get average |
||
| 287 | // normal way of getting the info |
||
| 288 | $students = []; // user list, needed to make sure we only |
||
| 289 | // take first attempts into account |
||
| 290 | $student_count = 0; |
||
| 291 | $sum = 0; |
||
| 292 | $bestResult = 0; |
||
| 293 | $weight = 0; |
||
| 294 | $sumResult = 0; |
||
| 295 | |||
| 296 | $studentList = $this->getStudentList(); |
||
| 297 | $studentIdList = []; |
||
| 298 | if (!empty($studentList)) { |
||
| 299 | $studentIdList = array_column($studentList, 'user_id'); |
||
| 300 | } |
||
| 301 | |||
| 302 | while ($data = Database::fetch_array($scores, 'ASSOC')) { |
||
| 303 | // Only take into account users in the current student list. |
||
| 304 | if (!empty($studentIdList)) { |
||
| 305 | if (!in_array($data['exe_user_id'], $studentIdList)) { |
||
| 306 | continue; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | if (!isset($students[$data['exe_user_id']])) { |
||
| 311 | if ($data['max_score'] != 0) { |
||
| 312 | $students[$data['exe_user_id']] = $data['score']; |
||
| 313 | $student_count++; |
||
| 314 | if ($data['score'] > $bestResult) { |
||
| 315 | $bestResult = $data['score']; |
||
| 316 | } |
||
| 317 | $sum += $data['score'] / $data['max_score']; |
||
| 318 | $sumResult += $data['score']; |
||
| 319 | $weight = $data['max_score']; |
||
| 320 | } |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | if ($student_count == 0) { |
||
| 325 | if ($cacheAvailable) { |
||
| 326 | $cacheDriver->save($key, null); |
||
| 327 | } |
||
| 328 | |||
| 329 | return null; |
||
| 330 | } else { |
||
| 331 | switch ($type) { |
||
| 332 | case 'best': |
||
| 333 | $result = [$bestResult, $weight]; |
||
| 334 | if ($cacheAvailable) { |
||
| 335 | $cacheDriver->save($key, $result); |
||
| 336 | } |
||
| 337 | |||
| 338 | return $result; |
||
| 339 | break; |
||
| 340 | case 'average': |
||
| 341 | $count = count($this->getStudentList()); |
||
| 342 | if (empty($count)) { |
||
| 343 | $result = [0, $weight]; |
||
| 344 | if ($cacheAvailable) { |
||
| 345 | $cacheDriver->save($key, $result); |
||
| 346 | } |
||
| 347 | |||
| 348 | return $result; |
||
| 349 | } |
||
| 350 | |||
| 351 | $result = [$sumResult / $count, $weight]; |
||
| 352 | |||
| 353 | if ($cacheAvailable) { |
||
| 354 | $cacheDriver->save($key, $result); |
||
| 355 | } |
||
| 356 | |||
| 357 | return $result; |
||
| 358 | break; |
||
| 359 | case 'ranking': |
||
| 360 | $ranking = AbstractLink::getCurrentUserRanking($stud_id, $students); |
||
| 361 | if ($cacheAvailable) { |
||
| 362 | $cacheDriver->save($key, $ranking); |
||
| 363 | } |
||
| 364 | |||
| 365 | return $ranking; |
||
| 366 | break; |
||
| 367 | default: |
||
| 368 | $result = [$sum, $student_count]; |
||
| 369 | if ($cacheAvailable) { |
||
| 370 | $cacheDriver->save($key, $result); |
||
| 371 | } |
||
| 372 | |||
| 373 | return $result; |
||
| 374 | break; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Get URL where to go to if the user clicks on the link. |
||
| 382 | * First we go to exercise_jump.php and then to the result page. |
||
| 383 | * Check this php file for more info. |
||
| 384 | */ |
||
| 385 | public function get_link() |
||
| 386 | { |
||
| 387 | $sessionId = $this->get_session_id(); |
||
| 388 | $data = $this->get_exercise_data(); |
||
| 389 | $exerciseId = $data['id']; |
||
| 390 | $path = isset($data['path']) ? $data['path'] : ''; |
||
| 391 | |||
| 392 | $url = api_get_path(WEB_CODE_PATH).'gradebook/exercise_jump.php?' |
||
| 393 | .http_build_query( |
||
| 394 | [ |
||
| 395 | 'path' => $path, |
||
| 396 | 'session_id' => $sessionId, |
||
| 397 | 'cidReq' => $this->get_course_code(), |
||
| 398 | 'gradebook' => 'view', |
||
| 399 | 'exerciseId' => $exerciseId, |
||
| 400 | 'type' => $this->get_type(), |
||
| 401 | ] |
||
| 402 | ); |
||
| 403 | |||
| 404 | return $url; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get name to display: same as exercise title. |
||
| 409 | */ |
||
| 410 | public function get_name() |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Get description to display: same as exercise description. |
||
| 419 | */ |
||
| 420 | public function get_description() |
||
| 421 | { |
||
| 422 | $data = $this->get_exercise_data(); |
||
| 423 | |||
| 424 | return isset($data['description']) ? $data['description'] : null; |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Check if this still links to an exercise. |
||
| 429 | */ |
||
| 430 | public function is_valid_link() |
||
| 431 | { |
||
| 432 | $exerciseData = $this->get_exercise_data(); |
||
| 433 | |||
| 434 | return !empty($exerciseData); |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public function get_type_name() |
||
| 441 | { |
||
| 442 | if ($this->is_hp == 1) { |
||
| 443 | return 'HotPotatoes'; |
||
| 444 | } |
||
| 445 | |||
| 446 | return get_lang('Tests'); |
||
| 447 | } |
||
| 448 | |||
| 449 | public function needs_name_and_description() |
||
| 450 | { |
||
| 451 | return false; |
||
| 452 | } |
||
| 453 | |||
| 454 | public function needs_max() |
||
| 455 | { |
||
| 456 | return false; |
||
| 457 | } |
||
| 458 | |||
| 459 | public function needs_results() |
||
| 462 | } |
||
| 463 | |||
| 464 | public function is_allowed_to_change_name() |
||
| 465 | { |
||
| 466 | return false; |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | public function get_icon_name() |
||
| 473 | { |
||
| 474 | return 'exercise'; |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param bool $hp |
||
| 479 | */ |
||
| 480 | public function setHp($hp) |
||
| 483 | } |
||
| 484 | |||
| 485 | public function getBestScore() |
||
| 486 | { |
||
| 487 | return $this->getStats('best'); |
||
| 488 | } |
||
| 489 | |||
| 490 | public function getStats($type) |
||
| 495 | } |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Lazy load function to get the database contents of this exercise. |
||
| 500 | */ |
||
| 501 | public function get_exercise_data() |
||
| 502 | { |
||
| 503 | $tableItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 504 | if ($this->is_hp == 1) { |
||
| 505 | $table = Database::get_course_table(TABLE_DOCUMENT); |
||
| 506 | } else { |
||
| 507 | $table = Database::get_course_table(TABLE_QUIZ_TEST); |
||
| 508 | } |
||
| 509 | |||
| 510 | $exerciseId = $this->get_ref_id(); |
||
| 511 | |||
| 512 | if (empty($this->exercise_data)) { |
||
| 513 | if ($this->is_hp == 1) { |
||
| 514 | $sql = "SELECT * FROM $table ex |
||
| 515 | INNER JOIN $tableItemProperty ip |
||
| 516 | ON (ip.ref = ex.id AND ip.c_id = ex.c_id) |
||
| 517 | WHERE |
||
| 518 | ip.c_id = $this->course_id AND |
||
| 519 | ex.c_id = $this->course_id AND |
||
| 520 | ip.ref = $exerciseId AND |
||
| 521 | ip.tool = '".TOOL_DOCUMENT."' AND |
||
| 522 | ex.path LIKE '%htm%' AND |
||
| 523 | ex.path LIKE '%HotPotatoes_files%' AND |
||
| 524 | ip.visibility = 1"; |
||
| 525 | $result = Database::query($sql); |
||
| 526 | $this->exercise_data = Database::fetch_array($result); |
||
| 527 | } else { |
||
| 528 | // Try with iid |
||
| 529 | $sql = 'SELECT * FROM '.$table.' |
||
| 530 | WHERE |
||
| 531 | c_id = '.$this->course_id.' AND |
||
| 532 | iid = '.$exerciseId; |
||
| 533 | $result = Database::query($sql); |
||
| 534 | $rows = Database::num_rows($result); |
||
| 535 | |||
| 536 | if (!empty($rows)) { |
||
| 537 | $this->exercise_data = Database::fetch_array($result); |
||
| 538 | } else { |
||
| 539 | // Try wit id |
||
| 540 | $sql = 'SELECT * FROM '.$table.' |
||
| 541 | WHERE |
||
| 542 | c_id = '.$this->course_id.' AND |
||
| 543 | id = '.$exerciseId; |
||
| 544 | $result = Database::query($sql); |
||
| 545 | $this->exercise_data = Database::fetch_array($result); |
||
| 546 | } |
||
| 547 | } |
||
| 548 | } |
||
| 549 | |||
| 550 | if (empty($this->exercise_data)) { |
||
| 551 | return false; |
||
| 552 | } |
||
| 553 | |||
| 554 | return $this->exercise_data; |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Lazy load function to get the database table of the exercise. |
||
| 559 | */ |
||
| 560 | private function get_exercise_table() |
||
| 565 | } |
||
| 566 | } |
||
| 567 |