Conditions | 66 |
Total Lines | 473 |
Code Lines | 305 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
537 | public static function getUserData( |
||
538 | $from, |
||
539 | $numberOfItems, |
||
540 | $column, |
||
541 | $direction, |
||
542 | array $conditions = [], |
||
543 | bool $exerciseToCheckConfig = true, |
||
544 | bool $displaySessionInfo = false, |
||
545 | ?string $courseCode = null, |
||
546 | ?int $sessionId = null, |
||
547 | bool $exportCsv = false, |
||
548 | array $userIds = [] |
||
549 | ) { |
||
550 | $includeInvitedUsers = $conditions['include_invited_users'] ?? false; |
||
551 | $getCount = $conditions['get_count'] ?? false; |
||
552 | |||
553 | $csvContent = []; |
||
554 | $tblUser = Database::get_main_table(TABLE_MAIN_USER); |
||
555 | $tblUrlRelUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER); |
||
556 | $accessUrlId = api_get_current_access_url_id(); |
||
557 | |||
558 | if (!empty($userIds) && is_array($userIds)) { |
||
559 | $userIds = array_map('intval', $userIds); |
||
560 | $conditionUser = " WHERE user.id IN (".implode(',', $userIds).") "; |
||
561 | } else { |
||
562 | $conditionUser = " WHERE user.id = " . (int) $userIds; |
||
563 | } |
||
564 | |||
565 | if (!empty($_GET['user_keyword'])) { |
||
566 | $keyword = trim(Database::escape_string($_GET['user_keyword'])); |
||
567 | $conditionUser .= " AND ( |
||
568 | user.firstname LIKE '%".$keyword."%' OR |
||
569 | user.lastname LIKE '%".$keyword."%' OR |
||
570 | user.username LIKE '%".$keyword."%' OR |
||
571 | user.email LIKE '%".$keyword."%' |
||
572 | ) "; |
||
573 | } |
||
574 | |||
575 | $urlTable = ''; |
||
576 | $urlCondition = ''; |
||
577 | if (api_is_multiple_url_enabled()) { |
||
578 | $urlTable = " INNER JOIN $tblUrlRelUser as url_users ON (user.id = url_users.user_id)"; |
||
579 | $urlCondition = " AND access_url_id = '$accessUrlId'"; |
||
580 | } |
||
581 | |||
582 | $invitedUsersCondition = ''; |
||
583 | if (!$includeInvitedUsers) { |
||
584 | $invitedUsersCondition = " AND user.status != ".INVITEE; |
||
585 | } |
||
586 | |||
587 | $select = ' |
||
588 | SELECT user.id as user_id, |
||
589 | user.official_code as col0, |
||
590 | user.lastname as col1, |
||
591 | user.firstname as col2, |
||
592 | user.username as col3, |
||
593 | user.email as col4'; |
||
594 | |||
595 | if ($getCount) { |
||
596 | $select = ' SELECT COUNT(distinct(user.id)) as count '; |
||
597 | } |
||
598 | |||
599 | $sqlInjectJoins = ''; |
||
600 | $where = 'AND 1 = 1 '; |
||
601 | $sqlInjectWhere = ''; |
||
602 | if (!empty($conditions)) { |
||
603 | if (isset($conditions['inject_joins'])) { |
||
604 | $sqlInjectJoins = $conditions['inject_joins']; |
||
605 | } |
||
606 | if (isset($conditions['where'])) { |
||
607 | $where = $conditions['where']; |
||
608 | } |
||
609 | if (isset($conditions['inject_where'])) { |
||
610 | $sqlInjectWhere = $conditions['inject_where']; |
||
611 | } |
||
612 | $injectExtraFields = !empty($conditions['inject_extra_fields']) ? $conditions['inject_extra_fields'] : 1; |
||
613 | $injectExtraFields = rtrim($injectExtraFields, ', '); |
||
614 | if (false === $getCount) { |
||
615 | $select .= " , $injectExtraFields"; |
||
616 | } |
||
617 | } |
||
618 | |||
619 | $sql = "$select |
||
620 | FROM $tblUser as user |
||
621 | $urlTable |
||
622 | $sqlInjectJoins |
||
623 | $conditionUser |
||
624 | $urlCondition |
||
625 | $invitedUsersCondition |
||
626 | $where |
||
627 | $sqlInjectWhere |
||
628 | "; |
||
629 | |||
630 | if (!in_array($direction, ['ASC', 'DESC'])) { |
||
631 | $direction = 'ASC'; |
||
632 | } |
||
633 | |||
634 | $column = $column <= 2 ? (int) $column : 0; |
||
635 | $from = (int) $from; |
||
636 | $numberOfItems = (int) $numberOfItems; |
||
637 | |||
638 | if ($getCount) { |
||
639 | $res = Database::query($sql); |
||
640 | $row = Database::fetch_array($res); |
||
641 | |||
642 | return $row['count']; |
||
643 | } |
||
644 | |||
645 | $sortByFirstName = api_sort_by_first_name(); |
||
646 | |||
647 | if ($sortByFirstName) { |
||
648 | if ($column == 1) { |
||
649 | $column = 2; |
||
650 | } elseif ($column == 2) { |
||
651 | $column = 1; |
||
652 | } |
||
653 | } |
||
654 | |||
655 | $sql .= " ORDER BY col$column $direction "; |
||
656 | $sql .= " LIMIT $from, $numberOfItems"; |
||
657 | |||
658 | $res = Database::query($sql); |
||
659 | $users = []; |
||
660 | |||
661 | $courseInfo = api_get_course_info($courseCode); |
||
662 | $courseId = $courseInfo['real_id']; |
||
663 | |||
664 | $totalSurveys = 0; |
||
665 | $totalExercises = ExerciseLib::get_all_exercises( |
||
666 | $courseInfo, |
||
667 | $sessionId |
||
668 | ); |
||
669 | |||
670 | if (empty($sessionId)) { |
||
671 | $surveyUserList = []; |
||
672 | $surveyList = SurveyManager::get_surveys($courseCode); |
||
673 | if ($surveyList) { |
||
674 | $totalSurveys = count($surveyList); |
||
675 | foreach ($surveyList as $survey) { |
||
676 | $userList = SurveyManager::get_people_who_filled_survey( |
||
677 | $survey['survey_id'], |
||
678 | false, |
||
679 | $courseId |
||
680 | ); |
||
681 | |||
682 | foreach ($userList as $user_id) { |
||
683 | isset($surveyUserList[$user_id]) ? $surveyUserList[$user_id]++ : $surveyUserList[$user_id] = 1; |
||
684 | } |
||
685 | } |
||
686 | } |
||
687 | } |
||
688 | |||
689 | $urlBase = api_get_path(WEB_CODE_PATH).'my_space/myStudents.php?details=true&cid='.$courseId. |
||
690 | '&course='.$courseCode.'&origin=tracking_course&sid='.$sessionId; |
||
691 | |||
692 | Session::write('user_id_list', []); |
||
693 | $userIdList = []; |
||
694 | |||
695 | if ($exerciseToCheckConfig) { |
||
696 | $addExerciseOption = api_get_setting('exercise.add_exercise_best_attempt_in_report', true); |
||
697 | $exerciseResultsToCheck = []; |
||
698 | if (!empty($addExerciseOption) && isset($addExerciseOption['courses']) && |
||
699 | isset($addExerciseOption['courses'][$courseCode]) |
||
700 | ) { |
||
701 | foreach ($addExerciseOption['courses'][$courseCode] as $exerciseId) { |
||
702 | $exercise = new Exercise(); |
||
703 | $exercise->read($exerciseId); |
||
704 | if ($exercise->iid) { |
||
705 | $exerciseResultsToCheck[] = $exercise; |
||
706 | } |
||
707 | } |
||
708 | } |
||
709 | } |
||
710 | |||
711 | $lpShowMaxProgress = 'true' === api_get_setting('lp.lp_show_max_progress_instead_of_average'); |
||
712 | if ('true' === api_get_setting('lp.lp_show_max_progress_or_average_enable_course_level_redefinition')) { |
||
713 | $lpShowProgressCourseSetting = api_get_course_setting('lp_show_max_or_average_progress', $courseInfo, true); |
||
714 | if (in_array($lpShowProgressCourseSetting, ['max', 'average'])) { |
||
715 | $lpShowMaxProgress = ('max' === $lpShowProgressCourseSetting); |
||
716 | } |
||
717 | } |
||
718 | |||
719 | while ($user = Database::fetch_array($res, 'ASSOC')) { |
||
720 | $userIdList[] = $user['user_id']; |
||
721 | $user['official_code'] = $user['col0']; |
||
722 | $user['username'] = $user['col3']; |
||
723 | $user['time'] = api_time_to_hms( |
||
724 | Tracking::get_time_spent_on_the_course( |
||
725 | $user['user_id'], |
||
726 | $courseId, |
||
727 | $sessionId |
||
728 | ) |
||
729 | ); |
||
730 | |||
731 | $avgStudentScore = Tracking::get_avg_student_score( |
||
732 | $user['user_id'], |
||
733 | api_get_course_entity($courseId), |
||
734 | [], |
||
735 | api_get_session_entity($sessionId) |
||
736 | ); |
||
737 | |||
738 | $averageBestScore = Tracking::get_avg_student_score( |
||
739 | $user['user_id'], |
||
740 | api_get_course_entity($courseId), |
||
741 | [], |
||
742 | api_get_session_entity($sessionId), |
||
743 | false, |
||
744 | false, |
||
745 | true |
||
746 | ); |
||
747 | |||
748 | $avgStudentProgress = Tracking::get_avg_student_progress( |
||
749 | $user['user_id'], |
||
750 | api_get_course_entity($courseId), |
||
751 | [], |
||
752 | api_get_session_entity($sessionId) |
||
753 | ); |
||
754 | |||
755 | if (empty($avgStudentProgress)) { |
||
756 | $avgStudentProgress = 0; |
||
757 | } |
||
758 | $user['average_progress'] = $avgStudentProgress.'%'; |
||
759 | |||
760 | $totalUserExercise = Tracking::get_exercise_student_progress( |
||
761 | $totalExercises, |
||
762 | $user['user_id'], |
||
763 | $courseId, |
||
764 | $sessionId |
||
765 | ); |
||
766 | |||
767 | $user['exercise_progress'] = $totalUserExercise; |
||
768 | |||
769 | $totalUserExercise = Tracking::get_exercise_student_average_best_attempt( |
||
770 | $totalExercises, |
||
771 | $user['user_id'], |
||
772 | $courseId, |
||
773 | $sessionId |
||
774 | ); |
||
775 | |||
776 | $user['exercise_average_best_attempt'] = $totalUserExercise; |
||
777 | |||
778 | if (is_numeric($avgStudentScore)) { |
||
779 | $user['student_score'] = $avgStudentScore.'%'; |
||
780 | } else { |
||
781 | $user['student_score'] = $avgStudentScore; |
||
782 | } |
||
783 | |||
784 | if (is_numeric($averageBestScore)) { |
||
785 | $user['student_score_best'] = $averageBestScore.'%'; |
||
786 | } else { |
||
787 | $user['student_score_best'] = $averageBestScore; |
||
788 | } |
||
789 | |||
790 | $exerciseResults = []; |
||
791 | if (!empty($exerciseResultsToCheck)) { |
||
792 | foreach ($exerciseResultsToCheck as $exercise) { |
||
793 | $bestExerciseResult = Event::get_best_attempt_exercise_results_per_user( |
||
794 | $user['user_id'], |
||
795 | $exercise->iid, |
||
796 | $courseId, |
||
797 | $sessionId, |
||
798 | false |
||
799 | ); |
||
800 | |||
801 | $best = null; |
||
802 | if ($bestExerciseResult) { |
||
803 | $best = $bestExerciseResult['exe_result'] / $bestExerciseResult['exe_weighting']; |
||
804 | $best = round($best, 2) * 100; |
||
805 | $best .= '%'; |
||
806 | } |
||
807 | $exerciseResults['exercise_'.$exercise->iid] = $best; |
||
808 | } |
||
809 | } |
||
810 | |||
811 | $user['first_connection'] = Tracking::get_first_connection_date_on_the_course( |
||
812 | $user['user_id'], |
||
813 | $courseId, |
||
814 | $sessionId, |
||
815 | !$exportCsv |
||
816 | ); |
||
817 | |||
818 | $user['last_connection'] = Tracking::get_last_connection_date_on_the_course( |
||
819 | $user['user_id'], |
||
820 | $courseInfo, |
||
821 | $sessionId, |
||
822 | !$exportCsv |
||
823 | ); |
||
824 | |||
825 | |||
826 | $user['count_assignments'] = Tracking::countStudentPublications( |
||
827 | $courseId, |
||
828 | $sessionId |
||
829 | ); |
||
830 | |||
831 | $user['count_messages'] = Tracking::countStudentMessages( |
||
832 | $courseId, |
||
833 | $sessionId |
||
834 | ); |
||
835 | |||
836 | |||
837 | $user['lp_finalization_date'] = Tracking::getCourseLpFinalizationDate( |
||
838 | $user['user_id'], |
||
839 | $courseId, |
||
840 | $sessionId, |
||
841 | !$exportCsv |
||
842 | ); |
||
843 | |||
844 | $user['quiz_finalization_date'] = Tracking::getCourseQuizLastFinalizationDate( |
||
845 | $user['user_id'], |
||
846 | $courseId, |
||
847 | $sessionId, |
||
848 | !$exportCsv |
||
849 | ); |
||
850 | |||
851 | if ($exportCsv) { |
||
852 | if (!empty($user['first_connection'])) { |
||
853 | $user['first_connection'] = api_get_local_time($user['first_connection']); |
||
854 | } else { |
||
855 | $user['first_connection'] = '-'; |
||
856 | } |
||
857 | if (!empty($user['last_connection'])) { |
||
858 | $user['last_connection'] = api_get_local_time($user['last_connection']); |
||
859 | } else { |
||
860 | $user['last_connection'] = '-'; |
||
861 | } |
||
862 | if (!empty($user['lp_finalization_date'])) { |
||
863 | $user['lp_finalization_date'] = api_get_local_time($user['lp_finalization_date']); |
||
864 | } else { |
||
865 | $user['lp_finalization_date'] = '-'; |
||
866 | } |
||
867 | if (!empty($user['quiz_finalization_date'])) { |
||
868 | $user['quiz_finalization_date'] = api_get_local_time($user['quiz_finalization_date']); |
||
869 | } else { |
||
870 | $user['quiz_finalization_date'] = '-'; |
||
871 | } |
||
872 | } |
||
873 | |||
874 | if (empty($sessionId)) { |
||
875 | $user['survey'] = ($surveyUserList[$user['user_id']] ?? 0).' / '.$totalSurveys; |
||
876 | } |
||
877 | |||
878 | $url = $urlBase.'&student='.$user['user_id']; |
||
879 | |||
880 | $user['link'] = '<a href="'.$url.'"> |
||
881 | '.Display::return_icon('2rightarrow.png', get_lang('Details')).' |
||
882 | </a>'; |
||
883 | |||
884 | // store columns in array $users |
||
885 | $userRow = []; |
||
886 | if ($displaySessionInfo && !empty($sessionId)) { |
||
887 | $sessionInfo = api_get_session_info($sessionId); |
||
888 | $userRow['session_name'] = $sessionInfo['name']; |
||
889 | $userRow['session_startdate'] = $sessionInfo['access_start_date']; |
||
890 | $userRow['session_enddate'] = $sessionInfo['access_end_date']; |
||
891 | $userRow['course_name'] = $courseInfo['name']; |
||
892 | } |
||
893 | $userRow['official_code'] = $user['official_code']; |
||
894 | if ($sortByFirstName) { |
||
895 | $userRow['firstname'] = $user['col2']; |
||
896 | $userRow['lastname'] = $user['col1']; |
||
897 | } else { |
||
898 | $userRow['lastname'] = $user['col1']; |
||
899 | $userRow['firstname'] = $user['col2']; |
||
900 | } |
||
901 | $userRow['username'] = $user['username']; |
||
902 | $userRow['time'] = $user['time']; |
||
903 | $userRow['average_progress'] = $user['average_progress']; |
||
904 | $userRow['exercise_progress'] = $user['exercise_progress']; |
||
905 | $userRow['exercise_average_best_attempt'] = $user['exercise_average_best_attempt']; |
||
906 | $userRow['student_score'] = $user['student_score']; |
||
907 | $userRow['student_score_best'] = $user['student_score_best']; |
||
908 | if (!empty($exerciseResults)) { |
||
909 | foreach ($exerciseResults as $exerciseId => $bestResult) { |
||
910 | $userRow[$exerciseId] = $bestResult; |
||
911 | } |
||
912 | } |
||
913 | |||
914 | $userRow['count_assignments'] = $user['count_assignments']; |
||
915 | $userRow['count_messages'] = $user['count_messages']; |
||
916 | |||
917 | $userGroupManager = new UserGroupModel(); |
||
918 | if ($exportCsv) { |
||
919 | $userRow['classes'] = implode( |
||
920 | ',', |
||
921 | $userGroupManager->getNameListByUser($user['user_id'], UserGroupModel::NORMAL_CLASS) |
||
922 | ); |
||
923 | } else { |
||
924 | $userRow['classes'] = $userGroupManager->getLabelsFromNameList( |
||
925 | $user['user_id'], |
||
926 | UserGroupModel::NORMAL_CLASS |
||
927 | ); |
||
928 | } |
||
929 | |||
930 | if (empty($sessionId)) { |
||
931 | $userRow['survey'] = $user['survey']; |
||
932 | } else { |
||
933 | $userSession = SessionManager::getUserSession($user['user_id'], $sessionId); |
||
934 | $userRow['registered_at'] = ''; |
||
935 | if ($userSession) { |
||
936 | $userRow['registered_at'] = api_get_local_time($userSession['registered_at']); |
||
937 | } |
||
938 | } |
||
939 | |||
940 | $userRow['first_connection'] = $user['first_connection']; |
||
941 | $userRow['last_connection'] = $user['last_connection']; |
||
942 | |||
943 | $userRow['lp_finalization_date'] = $user['lp_finalization_date']; |
||
944 | $userRow['quiz_finalization_date'] = $user['quiz_finalization_date']; |
||
945 | |||
946 | // we need to display an additional profile field |
||
947 | if (isset($_GET['additional_profile_field'])) { |
||
948 | $data = Session::read('additional_user_profile_info'); |
||
949 | |||
950 | $extraFieldInfo = Session::read('extra_field_info'); |
||
951 | foreach ($_GET['additional_profile_field'] as $fieldId) { |
||
952 | if (isset($data[$fieldId]) && isset($data[$fieldId][$user['user_id']])) { |
||
953 | if (is_array($data[$fieldId][$user['user_id']])) { |
||
954 | $userRow[$extraFieldInfo[$fieldId]['variable']] = implode( |
||
955 | ', ', |
||
956 | $data[$fieldId][$user['user_id']] |
||
957 | ); |
||
958 | } else { |
||
959 | $userRow[$extraFieldInfo[$fieldId]['variable']] = $data[$fieldId][$user['user_id']]; |
||
960 | } |
||
961 | } else { |
||
962 | $userRow[$extraFieldInfo[$fieldId]['variable']] = ''; |
||
963 | } |
||
964 | } |
||
965 | } |
||
966 | |||
967 | $data = Session::read('default_additional_user_profile_info'); |
||
968 | $defaultExtraFieldInfo = Session::read('default_extra_field_info'); |
||
969 | if (isset($defaultExtraFieldInfo) && isset($data)) { |
||
970 | foreach ($data as $key => $val) { |
||
971 | if (isset($val[$user['user_id']])) { |
||
972 | if (is_array($val[$user['user_id']])) { |
||
973 | $userRow[$defaultExtraFieldInfo[$key]['variable']] = implode( |
||
974 | ', ', |
||
975 | $val[$user['user_id']] |
||
976 | ); |
||
977 | } else { |
||
978 | $userRow[$defaultExtraFieldInfo[$key]['variable']] = $val[$user['user_id']]; |
||
979 | } |
||
980 | } else { |
||
981 | $userRow[$defaultExtraFieldInfo[$key]['variable']] = ''; |
||
982 | } |
||
983 | } |
||
984 | } |
||
985 | |||
986 | if (api_get_setting('show_email_addresses') === 'true') { |
||
987 | $userRow['email'] = $user['col4']; |
||
988 | } |
||
989 | |||
990 | $userRow['link'] = $user['link']; |
||
991 | |||
992 | if ($exportCsv) { |
||
993 | unset($userRow['link']); |
||
994 | $csvContent[] = $userRow; |
||
995 | } |
||
996 | $users[] = array_values($userRow); |
||
997 | } |
||
998 | |||
999 | if ($exportCsv) { |
||
1000 | Session::write('csv_content', $csvContent); |
||
1001 | } |
||
1002 | |||
1003 | Session::erase('additional_user_profile_info'); |
||
1004 | Session::erase('extra_field_info'); |
||
1005 | Session::erase('default_additional_user_profile_info'); |
||
1006 | Session::erase('default_extra_field_info'); |
||
1007 | Session::write('user_id_list', $userIdList); |
||
1008 | |||
1009 | return $users; |
||
1010 | } |
||
1319 |