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