Passed
Push — 1.11.x ( 51db5d...aa7c5a )
by Julito
13:36
created

get_users()   C

Complexity

Conditions 12
Paths 100

Size

Total Lines 117
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 78
c 1
b 0
f 0
nc 100
nop 4
dl 0
loc 117
rs 6.0533

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
/**
6
 * Report on students subscribed to courses I am teaching.
7
 */
8
9
require_once __DIR__.'/../inc/global.inc.php';
10
require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
11
12
api_protect_course_script();
13
14
$allowToTrack = api_is_platform_admin() || api_is_allowed_to_edit();
15
16
if (!$allowToTrack) {
17
    api_not_allowed(true);
18
}
19
$consideredWorkingTime = api_get_configuration_value('considered_working_time');
20
21
if (false === $consideredWorkingTime) {
22
    api_not_allowed(true);
23
}
24
25
$courseCode = api_get_course_id();
26
$sessionId = api_get_session_id();
27
28
$nameTools = get_lang('Students');
29
30
$this_section = SECTION_TRACKING;
31
$webCodePath = api_get_path(WEB_CODE_PATH);
32
$interbreadcrumb[] = [
33
    'url' => api_is_student_boss() ? '#' : 'index.php',
34
    'name' => get_lang('MySpace'),
35
];
36
37
function get_count_users()
38
{
39
    $courseCode = api_get_course_id();
40
    $sessionId = api_get_session_id();
41
42
    return CourseManager::get_user_list_from_course_code(
43
        $courseCode,
44
        $sessionId,
45
        null,
46
        null,
47
        null,
48
        true
49
    );
50
}
51
52
function get_users($from, $number_of_items, $column, $direction)
53
{
54
    $consideredWorkingTime = api_get_configuration_value('considered_working_time');
55
56
    $courseId = api_get_course_int_id();
57
    $courseCode = api_get_course_id();
58
    $sessionId = api_get_session_id();
59
    $webCodePath = api_get_path(WEB_CODE_PATH);
60
61
    $lastConnectionDate = null;
62
    $is_western_name_order = api_is_western_name_order();
63
    $limit = null;
64
    $from = (int) $from;
65
    $number_of_items = (int) $number_of_items;
66
    $limit = 'LIMIT '.$from.','.$number_of_items;
67
68
    $students = CourseManager::get_user_list_from_course_code(
69
        $courseCode,
70
        $sessionId,
71
        $limit,
72
        null,
73
        null,
74
        false
75
    );
76
    $url = $webCodePath.'mySpace/myStudents.php';
77
78
    $workList = getWorkListTeacher(0, 100, null, null, null);
79
80
    $totalCourseWorkTime = 0;
81
    $workTimeList = [];
82
    foreach ($workList as $work) {
83
        $fieldValue = new ExtraFieldValue('work');
84
        $resultExtra = $fieldValue->getAllValuesForAnItem(
85
            $work['id'],
86
            true
87
        );
88
89
        foreach ($resultExtra as $field) {
90
            $field = $field['value'];
91
            if ($consideredWorkingTime == $field->getField()->getVariable()) {
92
                $time = $field->getValue();
93
                $parsed = date_parse($time);
94
                $workTimeList[$work['id']] = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];
95
96
                break;
97
            }
98
        }
99
    }
100
101
    $all_datas = [];
102
    foreach ($students as $studentData) {
103
        $studentId = $studentData['user_id'];
104
        $studentData = api_get_user_info($studentId);
105
        $urlDetails = $url."?student=$studentId";
106
        $row = [];
107
        if ($is_western_name_order) {
108
            $first = Display::url($studentData['firstname'], $urlDetails);
109
            $last = Display::url($studentData['lastname'], $urlDetails);
110
        } else {
111
            $first = Display::url($studentData['lastname'], $urlDetails);
112
            $last = Display::url($studentData['firstname'], $urlDetails);
113
        }
114
115
        $row[] = $first;
116
        $row[] = $last;
117
118
        $timeInSeconds = Tracking::get_time_spent_on_the_course(
119
            $studentId,
120
            $courseId,
121
            $sessionId
122
        );
123
124
        $row[] = api_time_to_hms($timeInSeconds);
125
126
        $userWorkTime = 0;
127
        foreach ($workList as $work) {
128
            $userWorks = get_work_user_list(
129
                0,
130
                100,
131
                null,
132
                null,
133
                $work['id'],
134
                null,
135
                $studentId,
136
                false,
137
                $courseId,
138
                $sessionId
139
            );
140
141
            if ($userWorks) {
142
                foreach ($userWorks as $work) {
143
                    $userWorkTime += $workTimeList[$work['parent_id']];
144
                }
145
            }
146
        }
147
148
        $row[] = api_time_to_hms($userWorkTime);
149
        $status = '';
150
        if ($userWorkTime && $timeInSeconds) {
151
            if ($userWorkTime > $timeInSeconds) {
152
                $status = Display::label('TimeToFix', 'warning');
153
            } else {
154
                $status = Display::label('Ok', 'success');
155
            }
156
        }
157
158
        $row[] = $status;
159
        /*$detailsLink = Display::url(
160
            Display::return_icon('2rightarrow.png', get_lang('Details').' '.$studentData['username']),
161
            $urlDetails,
162
            ['id' => 'details_'.$studentData['username']]
163
        );
164
        $row[] = $detailsLink;*/
165
        $all_datas[] = $row;
166
    }
167
168
    return $all_datas;
169
}
170
171
$is_western_name_order = api_is_western_name_order();
172
173
$sort_by_first_name = api_sort_by_first_name();
174
$actionsLeft = '';
175
$toolbar = Display::toolbarAction('toolbar-student', [$actionsLeft]);
176
177
$itemPerPage = 10;
178
$perPage = api_get_configuration_value('my_space_users_items_per_page');
179
if ($perPage) {
180
    $itemPerPage = (int) $perPage;
181
}
182
183
$table = new SortableTable(
184
    'tracking_work_student',
185
    'get_count_users',
186
    'get_users',
187
    ($is_western_name_order xor $sort_by_first_name) ? 1 : 0,
188
    $itemPerPage
189
);
190
191
$parameters =['cidReq' => $courseCode, 'id_session' => $sessionId];
192
$table->set_additional_parameters($parameters);
193
194
if ($is_western_name_order) {
195
    $table->set_header(0, get_lang('FirstName'), false);
196
    $table->set_header(1, get_lang('LastName'), false);
197
} else {
198
    $table->set_header(0, get_lang('LastName'), false);
199
    $table->set_header(1, get_lang('FirstName'), false);
200
}
201
202
$table->set_header(2, get_lang('TimeSpentInTheCourse'), false);
203
$table->set_header(3, get_lang('TimeSpentInWork'), false);
204
$table->set_header(4, get_lang('Status'), false);
205
206
Display::display_header($nameTools);
207
echo $toolbar;
208
echo Display::page_subheader($nameTools);
209
$table->display();
210
211
Display::display_footer();
212