Completed
Push — master ( 9b8b24...6e1754 )
by Julito
58:58
created

get_users()   F

Complexity

Conditions 20
Paths 12672

Size

Total Lines 118
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 90
nc 12672
nop 4
dl 0
loc 118
rs 2
c 0
b 0
f 0

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
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Student report
6
 * @package chamilo.reporting
7
 */
8
9
$cidReset = true;
10
11
require_once __DIR__.'/../inc/global.inc.php';
12
13
$export_csv = isset($_GET['export']) && $_GET['export'] == 'csv' ? true : false;
14
$keyword = isset($_GET['keyword']) ? Security::remove_XSS($_GET['keyword']) : null;
15
$active = isset($_GET['active']) ? intval($_GET['active']) : 1;
16
$sleepingDays = isset($_GET['sleeping_days']) ? intval($_GET['sleeping_days']) : null;
17
$status = isset($_GET['status']) ? Security::remove_XSS($_GET['status']) : null;
18
19
api_block_anonymous_users();
20
21
$this_section = SECTION_TRACKING;
22
23
$interbreadcrumb[] = array("url" => "index.php", "name" => get_lang('MySpace'));
24
25
if (isset($_GET["user_id"]) && $_GET["user_id"] != "" && !isset($_GET["type"])) {
26
    $interbreadcrumb[] = array("url" => "teachers.php", "name" => get_lang('Teachers'));
27
}
28
29
if (isset($_GET["user_id"]) && $_GET["user_id"] != "" && isset($_GET["type"]) && $_GET["type"] == "coach") {
30
    $interbreadcrumb[] = array("url" => "coaches.php", "name" => get_lang('Tutors'));
31
}
32
33
function get_count_users()
34
{
35
    $sleepingDays = isset($_GET['sleeping_days']) ? intval($_GET['sleeping_days']) : null;
36
    $active = isset($_GET['active']) ? $_GET['active'] : 1;
37
    $keyword = isset($_GET['keyword']) ? Security::remove_XSS($_GET['keyword']) : null;
38
    $status = isset($_GET['status']) ? Security::remove_XSS($_GET['status']) : null;
39
40
    $lastConnectionDate = null;
41
    if (!empty($sleepingDays)) {
42
        $lastConnectionDate = api_get_utc_datetime(strtotime($sleepingDays.' days ago'));
43
    }
44
    return SessionManager::getCountUserTracking(
45
        $keyword,
46
        $active,
47
        $lastConnectionDate,
48
        null,
49
        null,
50
        $status
51
    );
52
}
53
54
function get_users($from, $limit, $column, $direction)
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

54
function get_users($from, $limit, /** @scrutinizer ignore-unused */ $column, $direction)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
{
56
    $active = isset($_GET['active']) ? $_GET['active'] : 1;
57
    $keyword = isset($_GET['keyword']) ? Security::remove_XSS($_GET['keyword']) : null;
58
    $sleepingDays = isset($_GET['sleeping_days']) ? intval($_GET['sleeping_days']) : null;
59
    $status = isset($_GET['status']) ? Security::remove_XSS($_GET['status']) : null;
60
    $sessionId = isset($_GET['id_session']) ? (int) $_GET['id_session'] : 0;
61
62
    $lastConnectionDate = null;
63
    if (!empty($sleepingDays)) {
64
        $lastConnectionDate = api_get_utc_datetime(strtotime($sleepingDays.' days ago'));
65
    }
66
67
    $is_western_name_order = api_is_western_name_order();
68
    $coach_id = api_get_user_id();
69
    $column = 'u.user_id';
70
    $drhLoaded = false;
71
    if (api_is_drh()) {
72
        if (api_drh_can_access_all_session_content()) {
73
            $students = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
74
                'drh_all',
75
                api_get_user_id(),
76
                false,
77
                $from,
78
                $limit,
79
                '',
80
                $direction,
81
                $keyword,
82
                $active,
83
                $lastConnectionDate,
84
                null,
85
                null,
86
                $status
87
            );
88
            $drhLoaded = true;
89
        }
90
    }
91
92
    if ($drhLoaded === false) {
93
        $students = UserManager::getUsersFollowedByUser(
94
            api_get_user_id(),
95
            $status,
96
            false,
97
            false,
98
            false,
99
            $from,
100
            $limit,
101
            '',
102
            $direction,
103
            $active,
104
            $lastConnectionDate,
105
            COURSEMANAGER,
106
            $keyword
107
        );
108
    }
109
110
    $all_datas = array();
111
112
    foreach ($students as $student_data) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $students does not seem to be defined for all execution paths leading up to this point.
Loading history...
113
        $student_id = $student_data['user_id'];
114
        if (isset($_GET['id_session'])) {
115
            $courses = Tracking :: get_course_list_in_session_from_student($student_id, $_GET['id_session']);
116
        }
117
118
        $avg_time_spent = $avg_student_score = $avg_student_progress = 0;
119
        $nb_courses_student = 0;
120
        if (!empty($courses)) {
121
            foreach ($courses as $course_code) {
122
                $courseInfo = api_get_course_info($course_code);
123
                $courseId = $courseInfo['real_id'];
124
125
                if (CourseManager::is_user_subscribed_in_course($student_id, $course_code, true)) {
126
                    $avg_time_spent += Tracking::get_time_spent_on_the_course($student_id, $courseId, $_GET['id_session']);
127
                    $my_average = Tracking::get_avg_student_score($student_id, $course_code);
128
                    if (is_numeric($my_average)) {
129
                        $avg_student_score += $my_average;
130
                    }
131
                    $avg_student_progress += Tracking::get_avg_student_progress($student_id, $course_code);
132
                    $nb_courses_student++;
133
                }
134
            }
135
        }
136
137
        if ($nb_courses_student > 0) {
138
            $avg_time_spent = $avg_time_spent / $nb_courses_student;
139
            $avg_student_score = $avg_student_score / $nb_courses_student;
140
            $avg_student_progress = $avg_student_progress / $nb_courses_student;
141
        } else {
142
            $avg_time_spent = null;
143
            $avg_student_score = null;
144
            $avg_student_progress = null;
145
        }
146
147
        $row = array();
148
        if ($is_western_name_order) {
149
            $row[] = $student_data['firstname'];
150
            $row[] = $student_data['lastname'];
151
        } else {
152
            $row[] = $student_data['lastname'];
153
            $row[] = $student_data['firstname'];
154
        }
155
        $string_date = Tracking::get_last_connection_date($student_id, true);
156
        $first_date = Tracking::get_first_connection_date($student_id);
157
        $row[] = $first_date;
158
        $row[] = $string_date;
159
160
        if (isset($_GET['id_coach']) && intval($_GET['id_coach']) != 0) {
161
            $detailsLink = '<a href="myStudents.php?student='.$student_id.'&id_coach='.$coach_id.'&id_session='.$sessionId.'">
162
				            '.Display::return_icon('2rightarrow.png', get_lang('Details')).'</a>';
163
        } else {
164
            $detailsLink = '<a href="myStudents.php?student='.$student_id.'">
165
				            '.Display::return_icon('2rightarrow.png', get_lang('Details')).'</a>';
166
        }
167
        $row[] = $detailsLink;
168
        $all_datas[] = $row;
169
    }
170
171
    return $all_datas;
172
}
173
174
if ($export_csv) {
175
    $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
176
} else {
177
    $is_western_name_order = api_is_western_name_order();
178
}
179
180
$sort_by_first_name = api_sort_by_first_name();
181
$actionsLeft = '';
182
183
if (api_is_drh()) {
184
    $menu_items = array(
185
        Display::url(Display::return_icon('stats.png', get_lang('MyStats'), '', ICON_SIZE_MEDIUM), api_get_path(WEB_CODE_PATH)."auth/my_progress.php"),
186
        Display::url(Display::return_icon('user_na.png', get_lang('Students'), array(), ICON_SIZE_MEDIUM), '#'),
187
        Display::url(Display::return_icon('teacher.png', get_lang('Trainers'), array(), ICON_SIZE_MEDIUM), 'teachers.php'),
188
        Display::url(Display::return_icon('course.png', get_lang('Courses'), array(), ICON_SIZE_MEDIUM), 'course.php'),
189
        Display::url(Display::return_icon('session.png', get_lang('Sessions'), array(), ICON_SIZE_MEDIUM), 'session.php')
190
    );
191
192
    $nb_menu_items = count($menu_items);
193
    if ($nb_menu_items > 1) {
194
        foreach ($menu_items as $key => $item) {
195
            $actionsLeft .= $item;
196
        }
197
    }
198
}
199
200
$actionsRight = Display::url(
201
    Display::return_icon('printer.png', get_lang('Print'), array(), ICON_SIZE_MEDIUM),
202
    'javascript: void(0);',
203
    array('onclick'=>'javascript: window.print();')
204
);
205
$actionsRight .= Display::url(
206
    Display::return_icon('export_csv.png', get_lang('ExportAsCSV'), array(), ICON_SIZE_MEDIUM),
207
    api_get_self().'?export=csv&keyword='.$keyword
208
);
209
$toolbar = Display::toolbarAction('toolbar-user', [$actionsLeft, $actionsRight]);
210
211
$table = new SortableTable(
212
    'tracking_student',
213
    'get_count_users',
214
    'get_users',
215
    ($is_western_name_order xor $sort_by_first_name) ? 1 : 0,
216
    10
217
);
218
219
$params = array(
220
    'keyword' => $keyword,
221
    'active' => $active,
222
    'sleeping_days' => $sleepingDays,
223
    'status' => $status
224
);
225
$table->set_additional_parameters($params);
226
227
if ($is_western_name_order) {
228
    $table->set_header(0, get_lang('FirstName'), false);
229
    $table->set_header(1, get_lang('LastName'), false);
230
} else {
231
    $table->set_header(0, get_lang('LastName'), false);
232
    $table->set_header(1, get_lang('FirstName'), false);
233
}
234
235
$table->set_header(2, get_lang('FirstLogin'), false);
236
$table->set_header(3, get_lang('LastConnexion'), false);
237
$table->set_header(4, get_lang('Details'), false);
238
239
if ($export_csv) {
240
    if ($is_western_name_order) {
241
        $csv_header[] = array(
242
            get_lang('FirstName'),
243
            get_lang('LastName'),
244
            get_lang('FirstLogin'),
245
            get_lang('LastConnexion')
246
        );
247
    } else {
248
        $csv_header[] = array(
249
            get_lang('LastName'),
250
            get_lang('FirstName'),
251
            get_lang('FirstLogin'),
252
            get_lang('LastConnexion')
253
        );
254
    }
255
}
256
257
$form = new FormValidator('search_user', 'get', api_get_path(WEB_CODE_PATH).'mySpace/users.php');
258
259
$form->addElement(
260
    'select',
261
    'status',
262
    get_lang('Status'),
263
    array(
264
        '' => '',
265
        STUDENT => get_lang('Student'),
266
        COURSEMANAGER => get_lang('Teacher'),
267
        DRH => get_lang('DRH')
268
    )
269
);
270
$form = Tracking::setUserSearchForm($form);
271
$form->setDefaults($params);
272
273
// send the csv file if asked
274
$content = $table->get_table_data();
275
276
if ($export_csv) {
277
    foreach ($content as &$row) {
278
        unset($row[4]);
279
    }
280
    $csv_content = array_merge($csv_header, $content);
281
    ob_end_clean();
282
    Export :: arrayToCsv($csv_content, 'reporting_student_list');
283
    exit;
284
} else {
285
    Display::display_header(get_lang('Users'));
286
    echo $toolbar;
287
    $page_title = get_lang('Users');
288
    echo Display::page_subheader($page_title);
289
    if (isset($active)) {
290
        if ($active) {
291
            $activeLabel = get_lang('ActiveUsers');
292
        } else {
293
            $activeLabel = get_lang('InactiveUsers');
294
        }
295
        echo Display::page_subheader2($activeLabel);
296
    }
297
    $form->display();
298
    $table->display();
299
}
300
301
Display :: display_footer();
302