| Conditions | 27 |
| Paths | > 20000 |
| Total Lines | 245 |
| Code Lines | 165 |
| 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 |
||
| 483 | function processStudentList($filter_score, $global, $exercise, $courseInfo, $sessionId, $newSessionList) |
||
| 484 | { |
||
| 485 | if ((isset($exercise['iid']) && empty($exercise['iid'])) || |
||
| 486 | !isset($exercise['iid']) |
||
| 487 | ) { |
||
| 488 | return [ |
||
| 489 | 'html' => '', |
||
| 490 | 'export_array_global' => [], |
||
| 491 | 'total_students' => 0, |
||
| 492 | ]; |
||
| 493 | } |
||
| 494 | |||
| 495 | $exerciseStatsTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 496 | $courseId = api_get_course_int_id($courseInfo['code']); |
||
| 497 | |||
| 498 | if (empty($sessionId)) { |
||
| 499 | $students = CourseManager::get_student_list_from_course_code( |
||
| 500 | $courseInfo['code'], |
||
| 501 | false, |
||
| 502 | 0, |
||
| 503 | null, |
||
| 504 | null, |
||
| 505 | false |
||
| 506 | ); |
||
| 507 | } else { |
||
| 508 | $students = CourseManager::get_student_list_from_course_code( |
||
| 509 | $courseInfo['code'], |
||
| 510 | true, |
||
| 511 | $sessionId, |
||
| 512 | null, |
||
| 513 | null, |
||
| 514 | false |
||
| 515 | ); |
||
| 516 | } |
||
| 517 | |||
| 518 | $html = null; |
||
| 519 | $totalStudents = count($students); |
||
| 520 | |||
| 521 | if (!$global) { |
||
| 522 | $html .= "<tr>"; |
||
| 523 | } |
||
| 524 | |||
| 525 | if (!$global) { |
||
| 526 | $html .= '<td rowspan="'.$totalStudents.'">'; |
||
| 527 | } else { |
||
| 528 | $html .= '<td>'; |
||
| 529 | } |
||
| 530 | |||
| 531 | $html .= $exercise['title']; |
||
| 532 | |||
| 533 | if ($global && !empty($sessionId)) { |
||
| 534 | $sessionName = isset($newSessionList[$sessionId]) ? $newSessionList[$sessionId] : null; |
||
| 535 | $html .= Display::return_icon('star.png', get_lang('Session')).' ('.$sessionName.')'; |
||
| 536 | } |
||
| 537 | |||
| 538 | $html .= '</td>'; |
||
| 539 | |||
| 540 | $globalRow = [ |
||
| 541 | $courseInfo['title'], |
||
| 542 | $exercise['title'], |
||
| 543 | ]; |
||
| 544 | |||
| 545 | $total_with_parameter_score = 0; |
||
| 546 | $taken = 0; |
||
| 547 | $export_array_global = []; |
||
| 548 | $studentResult = []; |
||
| 549 | $export_array = []; |
||
| 550 | |||
| 551 | foreach ($students as $student) { |
||
| 552 | $studentId = isset($student['user_id']) ? $student['user_id'] : $student['id_user']; |
||
| 553 | $sql = "SELECT COUNT(ex.exe_id) as count |
||
| 554 | FROM $exerciseStatsTable AS ex |
||
| 555 | WHERE |
||
| 556 | ex.c_id = $courseId AND |
||
| 557 | ex.exe_exo_id = ".$exercise['iid']." AND |
||
| 558 | exe_user_id= $studentId AND |
||
| 559 | session_id = $sessionId |
||
| 560 | "; |
||
| 561 | $result = Database::query($sql); |
||
| 562 | $attempts = Database::fetch_array($result); |
||
| 563 | |||
| 564 | $sql = "SELECT exe_id, exe_result, exe_weighting |
||
| 565 | FROM $exerciseStatsTable |
||
| 566 | WHERE |
||
| 567 | exe_user_id = $studentId AND |
||
| 568 | c_id = $courseId AND |
||
| 569 | exe_exo_id = ".$exercise['iid']." AND |
||
| 570 | session_id = $sessionId |
||
| 571 | ORDER BY exe_result DESC |
||
| 572 | LIMIT 1"; |
||
| 573 | $result = Database::query($sql); |
||
| 574 | $score = 0; |
||
| 575 | $weighting = 0; |
||
| 576 | while ($scoreInfo = Database::fetch_array($result)) { |
||
| 577 | $score = $score + $scoreInfo['exe_result']; |
||
| 578 | $weighting = $weighting + $scoreInfo['exe_weighting']; |
||
| 579 | } |
||
| 580 | |||
| 581 | $percentageScore = 0; |
||
| 582 | |||
| 583 | if ($weighting != 0) { |
||
| 584 | $percentageScore = round(($score * 100) / $weighting); |
||
| 585 | } |
||
| 586 | |||
| 587 | if ($attempts['count'] > 0) { |
||
| 588 | $taken++; |
||
| 589 | } |
||
| 590 | |||
| 591 | if ($percentageScore >= $filter_score) { |
||
| 592 | $total_with_parameter_score++; |
||
| 593 | } |
||
| 594 | |||
| 595 | $tempArray = []; |
||
| 596 | |||
| 597 | if (!$global) { |
||
| 598 | $userInfo = api_get_user_info($studentId); |
||
| 599 | |||
| 600 | // User |
||
| 601 | $userRow = '<td>'; |
||
| 602 | $userRow .= $userInfo['complete_name']; |
||
| 603 | $userRow .= '</td>'; |
||
| 604 | $userRow .= '<td>'.$userInfo['username'].'</td>'; |
||
| 605 | |||
| 606 | // Best result |
||
| 607 | |||
| 608 | if (!empty($attempts['count'])) { |
||
| 609 | $userRow .= '<td>'; |
||
| 610 | $userRow .= $percentageScore; |
||
| 611 | $tempArray[] = $percentageScore; |
||
| 612 | $userRow .= '</td>'; |
||
| 613 | |||
| 614 | if ($percentageScore >= $filter_score) { |
||
| 615 | $userRow .= '<td style="background-color:#DFFFA8">'; |
||
| 616 | $userRow .= get_lang('PassExam').'</td>'; |
||
| 617 | $tempArray[] = get_lang('PassExam'); |
||
| 618 | } else { |
||
| 619 | $userRow .= '<td style="background-color:#FC9A9E" >'; |
||
| 620 | $userRow .= get_lang('ExamFail').'</td>'; |
||
| 621 | $tempArray[] = get_lang('ExamFail'); |
||
| 622 | } |
||
| 623 | |||
| 624 | $userRow .= '<td>'; |
||
| 625 | $userRow .= $attempts['count']; |
||
| 626 | $tempArray[] = $attempts['count']; |
||
| 627 | $userRow .= '</td>'; |
||
| 628 | } else { |
||
| 629 | $score = '-'; |
||
| 630 | $userRow .= '<td>'; |
||
| 631 | $userRow .= '-'; |
||
| 632 | $tempArray[] = '-'; |
||
| 633 | $userRow .= '</td>'; |
||
| 634 | |||
| 635 | $userRow .= '<td style="background-color:#FCE89A">'; |
||
| 636 | $userRow .= get_lang('NoAttempt'); |
||
| 637 | $tempArray[] = get_lang('NoAttempt'); |
||
| 638 | $userRow .= '</td>'; |
||
| 639 | $userRow .= '<td>'; |
||
| 640 | $userRow .= 0; |
||
| 641 | $tempArray[] = 0; |
||
| 642 | $userRow .= '</td>'; |
||
| 643 | } |
||
| 644 | $userRow .= '</tr>'; |
||
| 645 | |||
| 646 | $studentResult[$studentId] = [ |
||
| 647 | 'html' => $userRow, |
||
| 648 | 'score' => $score, |
||
| 649 | 'array' => $tempArray, |
||
| 650 | 'user' => $userInfo['complete_name'], |
||
| 651 | 'username' => $userInfo['username'], |
||
| 652 | ]; |
||
| 653 | } |
||
| 654 | } |
||
| 655 | |||
| 656 | $row_not_global['exercise'] = $exercise['title']; |
||
| 657 | |||
| 658 | if (!$global) { |
||
| 659 | if (!empty($studentResult)) { |
||
| 660 | $studentResultEmpty = $studentResultContent = []; |
||
| 661 | foreach ($studentResult as $row) { |
||
| 662 | if ($row['score'] == '-') { |
||
| 663 | $studentResultEmpty[] = $row; |
||
| 664 | } else { |
||
| 665 | $studentResultContent[] = $row; |
||
| 666 | } |
||
| 667 | } |
||
| 668 | |||
| 669 | // Sort only users with content |
||
| 670 | usort($studentResultContent, 'sort_user'); |
||
| 671 | $studentResult = array_merge($studentResultContent, $studentResultEmpty); |
||
| 672 | |||
| 673 | foreach ($studentResult as $row) { |
||
| 674 | $html .= $row['html']; |
||
| 675 | $row_not_global['results'][] = $row['array']; |
||
| 676 | $row_not_global['users'][] = $row['user']; |
||
| 677 | $row_not_global['usernames'][] = $row['username']; |
||
| 678 | } |
||
| 679 | $export_array[] = $row_not_global; |
||
| 680 | } |
||
| 681 | } |
||
| 682 | |||
| 683 | if ($global) { |
||
| 684 | // Exam taken |
||
| 685 | $html .= '<td>'; |
||
| 686 | $html .= $taken; |
||
| 687 | $globalRow[] = $taken; |
||
| 688 | $html .= '</td>'; |
||
| 689 | |||
| 690 | // Exam NOT taken |
||
| 691 | $html .= '<td>'; |
||
| 692 | $html .= $not_taken = $totalStudents - $taken; |
||
| 693 | $globalRow[] = $not_taken; |
||
| 694 | $html .= '</td>'; |
||
| 695 | |||
| 696 | // Exam pass |
||
| 697 | if (!empty($total_with_parameter_score)) { |
||
| 698 | $html .= '<td style="background-color:#DFFFA8" >'; |
||
| 699 | } else { |
||
| 700 | $html .= '<td style="background-color:#FCE89A" >'; |
||
| 701 | } |
||
| 702 | |||
| 703 | $html .= $total_with_parameter_score; |
||
| 704 | $globalRow[] = $total_with_parameter_score; |
||
| 705 | $html .= '</td>'; |
||
| 706 | |||
| 707 | // Exam fail |
||
| 708 | $html .= '<td>'; |
||
| 709 | |||
| 710 | $html .= $fail = $taken - $total_with_parameter_score; |
||
| 711 | $globalRow[] = $fail; |
||
| 712 | $html .= '</td>'; |
||
| 713 | |||
| 714 | $html .= '<td>'; |
||
| 715 | $html .= $totalStudents; |
||
| 716 | $globalRow[] = $totalStudents; |
||
| 717 | |||
| 718 | $html .= '</td>'; |
||
| 719 | |||
| 720 | $html .= '</tr>'; |
||
| 721 | $export_array_global[] = $globalRow; |
||
| 722 | } |
||
| 723 | |||
| 724 | return [ |
||
| 725 | 'html' => $html, |
||
| 726 | 'export_array_global' => $global ? $export_array_global : $export_array, |
||
| 727 | 'total_students' => $totalStudents, |
||
| 728 | ]; |
||
| 731 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.