Passed
Push — 1.11.x ( cd7ea3...4f89e2 )
by Julito
11:36
created

compare_data()   C

Complexity

Conditions 13
Paths 9

Size

Total Lines 47
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 34
nc 9
nop 1
dl 0
loc 47
rs 6.6166
c 0
b 0
f 0

How to fix   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
 * User move script (to move between courses and sessions).
7
 */
8
$cidReset = true;
9
require_once __DIR__.'/../inc/global.inc.php';
10
require_once api_get_path(SYS_CODE_PATH).'work/work.lib.php';
11
12
$this_section = SECTION_PLATFORM_ADMIN;
13
api_protect_admin_script();
14
15
$em = Database::getManager();
16
17
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
18
$debug = 0;
19
20
if (isset($_REQUEST['load_ajax'])) {
21
    //Checking the variable $_SESSION['combination'] that has all the
22
    // information of the selected course (instead of using a lots of
23
    // hidden variables ... )
24
    if (isset($_SESSION['combination']) && !empty($_SESSION['combination'])) {
25
        $combinations = $_SESSION['combination'];
26
        $combination_result = isset($combinations[$_REQUEST['unique_id']]) ? $combinations[$_REQUEST['unique_id']] : [];
27
        if (empty($combination_result)) {
28
            echo get_lang('ThereWasAnError');
29
        } else {
30
            $origin_course_code = $combination_result['course_code'];
31
            $origin_session_id = (int) $combination_result['session_id'];
32
            $new_session_id = (int) $_REQUEST['session_id'];
33
34
            if ($origin_session_id == $new_session_id) {
35
                echo get_lang('CantMoveToTheSameSession');
36
                exit;
37
            }
38
            $user_id = (int) $_REQUEST['user_id'];
39
            $new_course_list = SessionManager::get_course_list_by_session_id($new_session_id);
40
41
            $course_founded = false;
42
            foreach ($new_course_list as $course_item) {
43
                if ($origin_course_code == $course_item['code']) {
44
                    $course_founded = true;
45
                }
46
            }
47
48
            $result_message = [];
49
            $result_message_compare = [];
50
            $update_database = true;
51
            if (isset($_REQUEST['view_stat']) && $_REQUEST['view_stat'] == 1) {
52
                $update_database = false;
53
            }
54
55
            // Check if the same course exist in the session destination
56
            if ($course_founded) {
57
                $result = SessionManager::get_users_by_session($new_session_id);
58
                if (empty($result) || !in_array($user_id, array_keys($result))) {
59
                    if ($debug) {
60
                        echo 'User added to the session';
61
                    }
62
                    // Registering user to the new session
63
                    SessionManager::subscribeUsersToSession(
64
                        $new_session_id,
65
                        [$user_id],
66
                        false,
67
                        false
68
                    );
69
                }
70
71
                $course_info = api_get_course_info($origin_course_code);
72
                // Check if the user is registered in the session otherwise we will add it
73
                Tracking::processUserDataMove(
74
                    $user_id,
75
                    $course_info,
76
                    $origin_session_id,
77
                    $new_session_id,
78
                    $update_database,
79
                    $debug
80
                );
81
            } else {
82
                echo get_lang('CourseDoesNotExistInThisSession');
83
            }
84
        }
85
    } else {
86
        echo get_lang('ThereWasAnError');
87
    }
88
    exit;
89
}
90
$htmlHeadXtra[] = '<script>
91
   function moveto (unique_id, user_id) {
92
        var session_id = document.getElementById(unique_id).options[document.getElementById(unique_id).selectedIndex].value;
93
         $.ajax({
94
            contentType: "application/x-www-form-urlencoded",
95
            beforeSend: function(myObject) {
96
            $("div#reponse_"+unique_id).html("<img src=\'../inc/lib/javascript/indicator.gif\' />"); },
97
            type: "POST",
98
            url: "user_move_stats.php",
99
            data: "load_ajax=1"+"&unique_id="+unique_id+"&user_id="+user_id+"&session_id="+session_id,
100
            success: function(datos) {
101
             $("div#reponse_"+unique_id).html(datos);
102
            }
103
        });
104
    }
105
    function view_stat (unique_id, user_id) {
106
        var session_id = document.getElementById(unique_id).options[document.getElementById(unique_id).selectedIndex].value;
107
108
         $.ajax({
109
            contentType: "application/x-www-form-urlencoded",
110
            beforeSend: function(myObject) {
111
            $("div#reponse_"+unique_id).html("<img src=\'../inc/lib/javascript/indicator.gif\' />"); },
112
            type: "POST",
113
            url: "user_move_stats.php",
114
            data: "load_ajax=1&view_stat=1"+"&unique_id="+unique_id+"&user_id="+user_id+"&session_id="+session_id,
115
            success: function(datos) {
116
             $("div#reponse_"+unique_id).html(datos);
117
            }
118
        });
119
    }
120
    </script>';
121
122
function get_courses_list_by_user_id_based_in_exercises($user_id)
123
{
124
    $TABLETRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
125
    $user_id = (int) $user_id;
126
    $sql = "SELECT DISTINCT exe_user_id, c_id, session_id
127
            FROM $TABLETRACK_EXERCICES
128
            WHERE exe_user_id = $user_id
129
            ORDER by exe_user_id, c_id ASC";
130
131
    $res = Database::query($sql);
132
    $course_list = [];
133
    while ($row = Database::fetch_array($res, 'ASSOC')) {
134
        $course_list[] = $row;
135
    }
136
137
    return $course_list;
138
}
139
140
Display::addFlash(
141
    Display::return_message(
142
        get_lang('CompareUserResultsBetweenCoursesAndCoursesInASession'),
143
        'normal',
144
        false
145
    )
146
);
147
Display::display_header(get_lang('MoveUserStats'));
148
echo  '<div class="actions">';
149
echo '<a href="../admin/index.php">'.
150
    Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'), '', ICON_SIZE_MEDIUM).'</a>';
151
echo '</div>';
152
153
// Some pagination
154
$page = 1;
155
if (isset($_GET['page']) && !empty($_GET['page'])) {
156
    $page = intval($_GET['page']);
157
}
158
$default = 20;
159
$count = UserManager::get_number_of_users(null, api_get_current_access_url_id());
160
$nro_pages = round($count / $default) + 1;
161
$begin = $default * ($page - 1);
162
$end = $default * $page;
163
$navigation = "$begin - $end  / $count<br />";
164
165
if ($page > 1) {
166
    $navigation .= '<a href="'.api_get_self().'?page='.($page - 1).'">'.get_lang('Previous').'</a>';
167
} else {
168
    $navigation .= get_lang('Previous');
169
}
170
$navigation .= '&nbsp;';
171
$page++;
172
if ($page < $nro_pages) {
173
    $navigation .= '<a href="'.api_get_self().'?page='.$page.'">'.get_lang('Next').'</a>';
174
} else {
175
    $navigation .= get_lang('Next');
176
}
177
178
echo $navigation;
179
$user_list = UserManager::get_user_list([], [], $begin, $default);
180
$session_list = SessionManager::get_sessions_list([], ['name']);
181
$options = '';
182
$options .= '<option value="0">--'.get_lang('SelectASession').'--</option>';
183
foreach ($session_list as $session_data) {
184
    $my_session_list[$session_data['id']] = $session_data['name'];
185
    $options .= '<option value="'.$session_data['id'].'">'.$session_data['name'].'</option>';
186
}
187
188
$combinations = [];
189
190
if (!empty($user_list)) {
191
    foreach ($user_list as $user) {
192
        $user_id = $user['user_id'];
193
        $name = $user['firstname'].' '.$user['lastname'];
194
        $course_list_registered = CourseManager::get_courses_list_by_user_id(
195
            $user_id,
196
            true,
197
            false
198
        );
199
200
        $new_course_list = [];
201
        foreach ($course_list_registered as $course_reg) {
202
            if (empty($course_reg['session_id'])) {
203
                $course_reg['session_id'] = 0;
204
            }
205
            // Recover the code for historical reasons. If it can be proven
206
            // that the code can be safely replaced by c_id in the following
207
            // PHP code, feel free to do so
208
            $courseInfo = api_get_course_info_by_id($course_reg['real_id']);
209
            $course_reg['code'] = $courseInfo['code'];
210
            $new_course_list[] = $course_reg['code'].'_'.$course_reg['session_id'];
211
        }
212
213
        $course_list = get_courses_list_by_user_id_based_in_exercises($user_id);
214
215
        if (is_array($course_list) && !empty($course_list)) {
216
            foreach ($course_list as $my_course) {
217
                $courseInfo = api_get_course_info_by_id($my_course['c_id']);
218
                $my_course['real_id'] = $my_course['c_id'];
219
                $key = $courseInfo['code'].'_'.$my_course['session_id'];
220
221
                if (!in_array($key, $new_course_list)) {
222
                    $my_course['not_registered'] = 1;
223
                    $course_list_registered[] = $my_course;
224
                }
225
            }
226
        }
227
228
        foreach ($course_list_registered as &$course) {
229
            $courseInfo = api_get_course_info_by_id($course['real_id']);
230
            $course['name'] = $courseInfo['name'];
231
        }
232
233
        $course_list = $course_list_registered;
234
235
        echo '<div class="table-responsive">';
236
        echo '<table class="table table-hover table-striped data_table">';
237
        echo '<thead>';
238
        echo '<tr>';
239
        echo '<th style="text-align:left;" colspan="'.count($course_list).'">';
240
        echo "<h3>$name #$user_id </h3>  ";
241
        echo '</th>';
242
        echo '</tr>';
243
        echo '</thead>';
244
        echo '<tbody>';
245
246
        if (!empty($course_list)) {
247
            echo '<tr>';
248
            foreach ($course_list as $course) {
249
                echo '<td>';
250
                if (isset($course['id_session']) && !empty($course['id_session'])) {
251
                    echo '<b>'.get_lang('SessionName').'</b> '.$my_session_list[$course['id_session']].'<br />';
252
                }
253
                echo $course['name'];
254
                echo ' ('.$course['code'].') ';
255
                if (isset($course['not_registered']) && !empty($course['not_registered'])) {
256
                    echo ' <i>'.get_lang('UserNotRegistered').'</i>';
257
                }
258
                echo '</td>';
259
            }
260
            echo '</tr>';
261
            echo '<tr>';
262
263
            foreach ($course_list as $course) {
264
                $course_code = $course['code'];
265
                if (empty($course['id_session'])) {
266
                    $session_id = 0;
267
                } else {
268
                    $session_id = $course['id_session'];
269
                }
270
                echo '<td>';
271
                echo get_lang('MoveTo');
272
                echo '<br />';
273
                $unique_id = uniqid();
274
                $combinations[$unique_id] = ['course_code' => $course_code, 'session_id' => $session_id];
275
276
                echo '<select id="'.$unique_id.'" name="'.$unique_id.'" class="form-control">';
277
                echo $options;
278
                echo '</select>';
279
                echo '<br />';
280
                echo '<button type="submit" class="btn btn-success" onclick="view_stat(\''.$unique_id.'\', \''.$user_id.'\');"> '.get_lang('CompareStats').'</button>';
281
                echo '<button type="submit" class="btn btn-success" onclick="moveto(\''.$unique_id.'\', \''.$user_id.'\');"> '.get_lang('Move').'</button>';
282
                echo '<div id ="reponse_'.$unique_id.'"></div>';
283
                echo '</td>';
284
            }
285
            echo '</tr>';
286
        } else {
287
            echo '<td>';
288
            echo get_lang('NoCoursesForThisUser');
289
            echo '</td>';
290
        }
291
        echo '</tbody>';
292
        echo '</table>';
293
        echo '</div>';
294
    }
295
}
296
echo $navigation;
297
$_SESSION['combination'] = $combinations;
298