| Conditions | 23 |
| Paths | 18576 |
| Total Lines | 238 |
| Code Lines | 152 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 513 | function processStudentList( |
||
| 514 | $filter_score, |
||
| 515 | $global, |
||
| 516 | Cquiz $exercise, |
||
| 517 | $courseInfo, |
||
| 518 | $sessionId, |
||
| 519 | $newSessionList |
||
| 520 | ) { |
||
| 521 | $exerciseStatsTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES); |
||
| 522 | $courseId = $courseInfo['real_id']; |
||
| 523 | |||
| 524 | if (empty($sessionId)) { |
||
| 525 | $students = CourseManager::get_student_list_from_course_code( |
||
| 526 | $courseInfo['code'], |
||
| 527 | false, |
||
| 528 | 0, |
||
| 529 | null, |
||
| 530 | null, |
||
| 531 | false |
||
| 532 | ); |
||
| 533 | } else { |
||
| 534 | $students = CourseManager::get_student_list_from_course_code( |
||
| 535 | $courseInfo['code'], |
||
| 536 | true, |
||
| 537 | $sessionId, |
||
| 538 | null, |
||
| 539 | null, |
||
| 540 | false |
||
| 541 | ); |
||
| 542 | } |
||
| 543 | |||
| 544 | $html = ''; |
||
| 545 | $totalStudents = count($students); |
||
| 546 | |||
| 547 | if ($global) { |
||
| 548 | // One row per course + exam (+ session). |
||
| 549 | $html .= '<tr>'; |
||
| 550 | $html .= '<td>'.Security::remove_XSS($courseInfo['title']).'</td>'; |
||
| 551 | $html .= '<td>'; |
||
| 552 | |||
| 553 | $html .= Security::remove_XSS($exercise->getTitle()); |
||
| 554 | |||
| 555 | if (!empty($sessionId)) { |
||
| 556 | $sessionName = $newSessionList[$sessionId] ?? null; |
||
| 557 | if (!empty($sessionName)) { |
||
| 558 | $html .= ' '.Display::getMdiIcon( |
||
| 559 | ObjectIcon::STAR, |
||
| 560 | 'ch-tool-icon', |
||
| 561 | null, |
||
| 562 | ICON_SIZE_SMALL, |
||
| 563 | get_lang('Session') |
||
| 564 | ).' ('.$sessionName.')'; |
||
| 565 | } |
||
| 566 | } |
||
| 567 | |||
| 568 | $html .= '</td>'; |
||
| 569 | } else { |
||
| 570 | // Course context: first column is the exam title with rowspan. |
||
| 571 | $html .= '<tr>'; |
||
| 572 | $html .= '<td rowspan="'.$totalStudents.'">'; |
||
| 573 | $html .= Security::remove_XSS($exercise->getTitle()); |
||
| 574 | $html .= '</td>'; |
||
| 575 | } |
||
| 576 | |||
| 577 | $globalRow = [ |
||
| 578 | $courseInfo['title'], |
||
| 579 | $exercise->getTitle(), |
||
| 580 | ]; |
||
| 581 | |||
| 582 | $totalWithParameterScore = 0; |
||
| 583 | $taken = 0; |
||
| 584 | $export_array_global = []; |
||
| 585 | $studentResult = []; |
||
| 586 | $export_array = []; |
||
| 587 | |||
| 588 | $exerciseId = $exercise->getIid(); |
||
| 589 | $sessionCondition = api_get_session_condition($sessionId); |
||
| 590 | |||
| 591 | foreach ($students as $student) { |
||
| 592 | $studentId = isset($student['user_id']) ? (int) $student['user_id'] : (int) $student['id_user']; |
||
| 593 | |||
| 594 | $sql = "SELECT COUNT(exe_id) AS count |
||
| 595 | FROM $exerciseStatsTable |
||
| 596 | WHERE |
||
| 597 | c_id = $courseId AND |
||
| 598 | exe_exo_id = $exerciseId AND |
||
| 599 | exe_user_id = $studentId |
||
| 600 | $sessionCondition"; |
||
| 601 | $result = Database::query($sql); |
||
| 602 | $attempts = Database::fetch_array($result); |
||
| 603 | |||
| 604 | $sql = "SELECT exe_id, score, max_score |
||
| 605 | FROM $exerciseStatsTable |
||
| 606 | WHERE |
||
| 607 | exe_user_id = $studentId AND |
||
| 608 | c_id = $courseId AND |
||
| 609 | exe_exo_id = $exerciseId AND |
||
| 610 | session_id = $sessionId |
||
| 611 | ORDER BY score DESC |
||
| 612 | LIMIT 1"; |
||
| 613 | $result = Database::query($sql); |
||
| 614 | |||
| 615 | $score = 0; |
||
| 616 | $weighting = 0; |
||
| 617 | |||
| 618 | while ($scoreInfo = Database::fetch_array($result)) { |
||
| 619 | $score += $scoreInfo['score']; |
||
| 620 | $weighting += $scoreInfo['max_score']; |
||
| 621 | } |
||
| 622 | |||
| 623 | $percentageScore = 0; |
||
| 624 | |||
| 625 | // Protect against zero or "0" weighting values. |
||
| 626 | if (!empty($weighting) && (float) $weighting !== 0.0) { |
||
| 627 | $percentageScore = round(($score * 100) / (float) $weighting); |
||
| 628 | } |
||
| 629 | |||
| 630 | if ($attempts['count'] > 0) { |
||
| 631 | $taken++; |
||
| 632 | } |
||
| 633 | |||
| 634 | if ($percentageScore >= $filter_score) { |
||
| 635 | $totalWithParameterScore++; |
||
| 636 | } |
||
| 637 | |||
| 638 | $tempArray = []; |
||
| 639 | |||
| 640 | if (!$global) { |
||
| 641 | $userInfo = api_get_user_info($studentId); |
||
| 642 | |||
| 643 | $userRow = '<td>'.$userInfo['complete_name'].'</td>'; |
||
| 644 | $userRow .= '<td>'.$userInfo['username'].'</td>'; |
||
| 645 | |||
| 646 | if (!empty($attempts['count'])) { |
||
| 647 | $userRow .= '<td>'.$percentageScore.'</td>'; |
||
| 648 | $tempArray[] = $percentageScore; |
||
| 649 | |||
| 650 | if ($percentageScore >= $filter_score) { |
||
| 651 | $userRow .= '<td style="background-color:#DFFFA8">'; |
||
| 652 | $userRow .= get_lang('Pass').'</td>'; |
||
| 653 | $tempArray[] = get_lang('Pass'); |
||
| 654 | } else { |
||
| 655 | $userRow .= '<td style="background-color:#FC9A9E">'; |
||
| 656 | $userRow .= get_lang('Fail').'</td>'; |
||
| 657 | $tempArray[] = get_lang('Fail'); |
||
| 658 | } |
||
| 659 | |||
| 660 | $userRow .= '<td>'.$attempts['count'].'</td>'; |
||
| 661 | $tempArray[] = $attempts['count']; |
||
| 662 | } else { |
||
| 663 | $userRow .= '<td>-</td>'; |
||
| 664 | $tempArray[] = '-'; |
||
| 665 | |||
| 666 | $userRow .= '<td style="background-color:#FCE89A">'; |
||
| 667 | $userRow .= get_lang('No attempts').'</td>'; |
||
| 668 | $tempArray[] = get_lang('No attempts'); |
||
| 669 | |||
| 670 | $userRow .= '<td>0</td>'; |
||
| 671 | $tempArray[] = 0; |
||
| 672 | } |
||
| 673 | |||
| 674 | $userRow .= '</tr>'; |
||
| 675 | |||
| 676 | $studentResult[$studentId] = [ |
||
| 677 | 'html' => $userRow, |
||
| 678 | 'score' => $score, |
||
| 679 | 'array' => $tempArray, |
||
| 680 | 'user' => $userInfo['complete_name'], |
||
| 681 | 'username' => $userInfo['username'], |
||
| 682 | ]; |
||
| 683 | } |
||
| 684 | } |
||
| 685 | |||
| 686 | $row_not_global['exercise'] = $exercise->getTitle(); |
||
|
|
|||
| 687 | |||
| 688 | if (!$global) { |
||
| 689 | if (!empty($studentResult)) { |
||
| 690 | $studentResultEmpty = []; |
||
| 691 | $studentResultContent = []; |
||
| 692 | |||
| 693 | foreach ($studentResult as $row) { |
||
| 694 | if ('-' === $row['score']) { |
||
| 695 | $studentResultEmpty[] = $row; |
||
| 696 | } else { |
||
| 697 | $studentResultContent[] = $row; |
||
| 698 | } |
||
| 699 | } |
||
| 700 | |||
| 701 | // Sort only users with an actual score. |
||
| 702 | usort($studentResultContent, 'sort_user'); |
||
| 703 | $studentResult = array_merge($studentResultContent, $studentResultEmpty); |
||
| 704 | |||
| 705 | foreach ($studentResult as $row) { |
||
| 706 | $html .= $row['html']; |
||
| 707 | $row_not_global['results'][] = $row['array']; |
||
| 708 | $row_not_global['users'][] = $row['user']; |
||
| 709 | $row_not_global['usernames'][] = $row['username']; |
||
| 710 | } |
||
| 711 | |||
| 712 | $export_array[] = $row_not_global; |
||
| 713 | } |
||
| 714 | } |
||
| 715 | |||
| 716 | if ($global) { |
||
| 717 | // Exam taken. |
||
| 718 | $html .= '<td>'.$taken.'</td>'; |
||
| 719 | $globalRow[] = $taken; |
||
| 720 | |||
| 721 | // Exam not taken. |
||
| 722 | $notTaken = $totalStudents - $taken; |
||
| 723 | $html .= '<td>'.$notTaken.'</td>'; |
||
| 724 | $globalRow[] = $notTaken; |
||
| 725 | |||
| 726 | // Exam passed (>= filter score). |
||
| 727 | if (!empty($totalWithParameterScore)) { |
||
| 728 | $html .= '<td style="background-color:#DFFFA8">'.$totalWithParameterScore.'</td>'; |
||
| 729 | } else { |
||
| 730 | $html .= '<td style="background-color:#FCE89A">'.$totalWithParameterScore.'</td>'; |
||
| 731 | } |
||
| 732 | $globalRow[] = $totalWithParameterScore; |
||
| 733 | |||
| 734 | // Exam failed. |
||
| 735 | $fail = $taken - $totalWithParameterScore; |
||
| 736 | $html .= '<td>'.$fail.'</td>'; |
||
| 737 | $globalRow[] = $fail; |
||
| 738 | |||
| 739 | // Total learners. |
||
| 740 | $html .= '<td>'.$totalStudents.'</td>'; |
||
| 741 | $globalRow[] = $totalStudents; |
||
| 742 | |||
| 743 | $html .= '</tr>'; |
||
| 744 | $export_array_global[] = $globalRow; |
||
| 745 | } |
||
| 746 | |||
| 747 | return [ |
||
| 748 | 'html' => $html, |
||
| 749 | 'export_array_global' => $global ? $export_array_global : $export_array, |
||
| 750 | 'total_students' => $totalStudents, |
||
| 751 | ]; |
||
| 753 |