Passed
Push — ofaj ( 0f9380...ade756 )
by
unknown
12:01 queued 12s
created

MySpace::displayResumeLpByItem()   F

Complexity

Conditions 41
Paths 1200

Size

Total Lines 382
Code Lines 269

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 41
eloc 269
nc 1200
nop 3
dl 0
loc 382
rs 0
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
use ChamiloSession as Session;
5
use CpChart\Cache as pCache;
6
use CpChart\Data as pData;
7
use CpChart\Image as pImage;
8
9
/**
10
 * Class MySpace.
11
 */
12
class MySpace
13
{
14
    /**
15
     * Get admin actions.
16
     *
17
     * @return string
18
     */
19
    public static function getAdminActions()
20
    {
21
        $actions = [
22
            [
23
                'url' => api_get_path(WEB_CODE_PATH).'mySpace/admin_view.php?display=coaches',
24
                'content' => get_lang('DisplayCoaches'),
25
            ],
26
            [
27
                'url' => api_get_path(WEB_CODE_PATH).'mySpace/admin_view.php?display=user',
28
                'content' => get_lang('DisplayUserOverview'),
29
            ],
30
            [
31
                'url' => api_get_path(WEB_CODE_PATH).'mySpace/admin_view.php?display=session',
32
                'content' => get_lang('DisplaySessionOverview'),
33
            ],
34
            [
35
                'url' => api_get_path(WEB_CODE_PATH).'mySpace/admin_view.php?display=course',
36
                'content' => get_lang('DisplayCourseOverview'),
37
            ],
38
            [
39
                'url' => api_get_path(WEB_CODE_PATH).'tracking/question_course_report.php?view=admin',
40
                'content' => get_lang('LPQuestionListResults'),
41
            ],
42
            [
43
                'url' => api_get_path(WEB_CODE_PATH).'tracking/course_session_report.php?view=admin',
44
                'content' => get_lang('LPExerciseResultsBySession'),
45
            ],
46
            [
47
                'url' => api_get_path(WEB_CODE_PATH).'mySpace/admin_view.php?display=accessoverview',
48
                'content' => get_lang('DisplayAccessOverview').' ('.get_lang('Beta').')',
49
            ],
50
            [
51
                'url' => api_get_path(WEB_CODE_PATH).'mySpace/exercise_category_report.php',
52
                'content' => get_lang('ExerciseCategoryAllSessionsReport'),
53
            ],
54
            [
55
                'url' => api_get_path(WEB_CODE_PATH).'mySpace/survey_report.php',
56
                'content' => get_lang('SurveyReport'),
57
            ],
58
            [
59
                'url' => api_get_path(WEB_CODE_PATH).'mySpace/tc_report.php',
60
                'content' => get_lang('TCReport'),
61
            ],
62
            [
63
                'url' => api_get_path(WEB_CODE_PATH).'mySpace/ti_report.php',
64
                'content' => get_lang('TIReport'),
65
            ],
66
        ];
67
68
        return Display::actions($actions, null);
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public static function getTopMenu()
75
    {
76
        $menuItems = [];
77
        $menuItems[] = Display::url(
78
            Display::return_icon(
79
                'statistics.png',
80
                get_lang('MyStats'),
81
                '',
82
                ICON_SIZE_MEDIUM
83
            ),
84
            api_get_path(WEB_CODE_PATH)."auth/my_progress.php"
85
        );
86
        $menuItems[] = Display::url(
87
            Display::return_icon(
88
                'teacher.png',
89
                get_lang('TeacherInterface'),
90
                [],
91
                32
92
            ),
93
            api_get_path(WEB_CODE_PATH).'mySpace/?view=teacher'
94
        );
95
        $menuItems[] = Display::url(
96
            Display::return_icon(
97
                'star_na.png',
98
                get_lang('AdminInterface'),
99
                [],
100
                32
101
            ),
102
            '#'
103
        );
104
        $menuItems[] = Display::url(
105
            Display::return_icon('quiz.png', get_lang('ExamTracking'), [], 32),
106
            api_get_path(WEB_CODE_PATH).'tracking/exams.php'
107
        );
108
        $menu = '';
109
        foreach ($menuItems as $item) {
110
            $menu .= $item;
111
        }
112
        $menu .= '<br />';
113
114
        return $menu;
115
    }
116
117
    /**
118
     * This function serves exporting data in CSV format.
119
     *
120
     * @param array  $header    the header labels
121
     * @param array  $data      the data array
122
     * @param string $file_name the name of the file which contains exported data
123
     *
124
     * @return string mixed             Returns a message (string) if an error occurred
125
     */
126
    public function export_csv($header, $data, $file_name = 'export.csv')
127
    {
128
        $archive_path = api_get_path(SYS_ARCHIVE_PATH);
129
        $archive_url = api_get_path(WEB_CODE_PATH).'course_info/download.php?archive_path=&archive=';
130
        $message = '';
131
        if (!$open = fopen($archive_path.$file_name, 'w+')) {
132
            $message = get_lang('noOpen');
133
        } else {
134
            $info = '';
135
136
            foreach ($header as $value) {
137
                $info .= $value.';';
138
            }
139
            $info .= "\r\n";
140
141
            foreach ($data as $row) {
142
                foreach ($row as $value) {
143
                    $info .= $value.';';
144
                }
145
                $info .= "\r\n";
146
            }
147
148
            fwrite($open, $info);
149
            fclose($open);
150
            @chmod($file_name, api_get_permissions_for_new_files());
151
152
            header("Location:".$archive_url.$file_name);
153
            exit;
154
        }
155
156
        return $message;
157
    }
158
159
    /**
160
     * Gets the connections to a course as an array of login and logout time.
161
     *
162
     * @param int   $userId     User id
163
     * @param array $courseInfo
164
     * @param int   $sessionId  Session id (optional, default = 0)
165
     *
166
     * @return array Connections
167
     */
168
    public static function get_connections_to_course(
169
        $userId,
170
        $courseInfo,
171
        $sessionId = 0
172
    ) {
173
        $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
174
175
        // protect data
176
        $userId = (int) $userId;
177
        $courseId = (int) $courseInfo['real_id'];
178
        $sessionId = (int) $sessionId;
179
        $sessionCondition = api_get_session_condition($sessionId);
180
181
        $sql = 'SELECT login_course_date, logout_course_date
182
                FROM '.$table.'
183
                WHERE
184
                    user_id = '.$userId.' AND
185
                    c_id = '.$courseId.'
186
                    '.$sessionCondition.'
187
                ORDER BY login_course_date ASC';
188
        $rs = Database::query($sql);
189
        $connections = [];
190
191
        while ($row = Database::fetch_array($rs)) {
192
            $connections[] = [
193
                'login' => $row['login_course_date'],
194
                'logout' => $row['logout_course_date'],
195
            ];
196
        }
197
198
        return $connections;
199
    }
200
201
    /**
202
     * @param $user_id
203
     * @param $course_list
204
     * @param int $session_id
205
     *
206
     * @return array|bool
207
     */
208
    public static function get_connections_from_course_list(
209
        $user_id,
210
        $course_list,
211
        $session_id = 0
212
    ) {
213
        // Database table definitions
214
        $tbl_track_course = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
215
        if (empty($course_list)) {
216
            return false;
217
        }
218
219
        // protect data
220
        $user_id = (int) $user_id;
221
        $session_id = (int) $session_id;
222
        $new_course_list = [];
223
        foreach ($course_list as $course_item) {
224
            $courseInfo = api_get_course_info($course_item['code']);
225
            if ($courseInfo) {
226
                $courseId = $courseInfo['real_id'];
227
                $new_course_list[] = '"'.$courseId.'"';
228
            }
229
        }
230
        $course_list = implode(', ', $new_course_list);
231
232
        if (empty($course_list)) {
233
            return false;
234
        }
235
        $sql = 'SELECT login_course_date, logout_course_date, c_id
236
                FROM '.$tbl_track_course.'
237
                WHERE
238
                    user_id = '.$user_id.' AND
239
                    c_id IN ('.$course_list.') AND
240
                    session_id = '.$session_id.'
241
                ORDER BY login_course_date ASC';
242
        $rs = Database::query($sql);
243
        $connections = [];
244
245
        while ($row = Database::fetch_array($rs)) {
246
            $timestamp_login_date = api_strtotime($row['login_course_date'], 'UTC');
247
            $timestamp_logout_date = api_strtotime($row['logout_course_date'], 'UTC');
248
            $connections[] = [
249
                'login' => $timestamp_login_date,
250
                'logout' => $timestamp_logout_date,
251
                'c_id' => $row['c_id'],
252
            ];
253
        }
254
255
        return $connections;
256
    }
257
258
    /**
259
     * Creates a small table in the last column of the table with the user overview.
260
     *
261
     * @param int $user_id the id of the user
262
     *
263
     * @return array List course
264
     */
265
    public static function returnCourseTracking($user_id)
266
    {
267
        $user_id = (int) $user_id;
268
269
        if (empty($user_id)) {
270
            return [];
271
        }
272
273
        $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
274
        // getting all the courses of the user
275
        $sql = "SELECT * FROM $tbl_course_user
276
                WHERE
277
                    user_id = $user_id AND
278
                    relation_type <> ".COURSE_RELATION_TYPE_RRHH;
279
        $result = Database::query($sql);
280
281
        $list = [];
282
283
        while ($row = Database::fetch_array($result)) {
284
            $courseInfo = api_get_course_info_by_id($row['c_id']);
285
            $courseId = $courseInfo['real_id'];
286
            $courseCode = $courseInfo['code'];
287
288
            if (empty($courseInfo)) {
289
                continue;
290
            }
291
292
            $avg_score = Tracking::get_avg_student_score($user_id, $courseCode);
293
            if (is_numeric($avg_score)) {
294
                $avg_score = round($avg_score, 2);
295
            } else {
296
                $avg_score = '-';
297
            }
298
299
            // Student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
300
            $exercisesResults = self::exercises_results($user_id, $courseCode);
301
302
            $resultToString = '';
303
            if (!is_null($exercisesResults['percentage'])) {
304
                $resultToString = $exercisesResults['score_obtained'].'/'.$exercisesResults['score_possible'].' ( '.$exercisesResults['percentage'].'% )';
305
            }
306
307
            $item = [
308
                'code' => $courseInfo['code'],
309
                'real_id' => $courseInfo['real_id'],
310
                'title' => $courseInfo['title'],
311
                'category' => $courseInfo['categoryName'],
312
                'image_small' => $courseInfo['course_image'],
313
                'image_large' => $courseInfo['course_image_large'],
314
                'time_spent' => api_time_to_hms(Tracking::get_time_spent_on_the_course($user_id, $courseId)),
315
                'student_progress' => round(Tracking::get_avg_student_progress($user_id, $courseCode)),
316
                'student_score' => $avg_score,
317
                'student_message' => Tracking::count_student_messages($user_id, $courseCode),
318
                'student_assignments' => Tracking::count_student_assignments($user_id, $courseCode),
319
                'student_exercises' => $resultToString,
320
                'questions_answered' => $exercisesResults['questions_answered'],
321
                'last_connection' => Tracking::get_last_connection_date_on_the_course($user_id, $courseInfo),
322
            ];
323
            $list[] = $item;
324
        }
325
326
        return $list;
327
    }
328
329
    /**
330
     * Creates a small table in the last column of the table with the user overview.
331
     *
332
     * @param int   $user_id    the id of the user
333
     * @param array $url_params additional url parameters
334
     * @param array $row        the row information (the other columns)
335
     *
336
     * @return string html code
337
     */
338
    public static function course_info_tracking_filter($user_id, $url_params, $row)
339
    {
340
        // the table header
341
        $return = '<table class="data_table" style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
342
        // database table definition
343
        $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
344
345
        // getting all the courses of the user
346
        $sql = "SELECT * FROM $tbl_course_user
347
                WHERE
348
                    user_id = '".intval($user_id)."' AND
349
                    relation_type<>".COURSE_RELATION_TYPE_RRHH." ";
350
        $result = Database::query($sql);
351
        while ($row = Database::fetch_array($result)) {
352
            $courseInfo = api_get_course_info_by_id($row['c_id']);
353
            if (empty($courseInfo)) {
354
                continue;
355
            }
356
357
            $courseCode = $courseInfo['code'];
358
            $courseId = $courseInfo['real_id'];
359
360
            $return .= '<tr>';
361
            // course code
362
            $return .= '    <td width="157px" >'.cut($courseCode, 20, true).'</td>';
363
            // time spent in the course
364
            $return .= '<td><div>'.api_time_to_hms(Tracking::get_time_spent_on_the_course($user_id, $courseId)).'</div></td>';
365
            // student progress in course
366
            $return .= '<td><div>'.round(Tracking::get_avg_student_progress($user_id, $courseCode), 2).'</div></td>';
367
            // student score
368
            $avg_score = Tracking::get_avg_student_score($user_id, $courseCode);
369
            if (is_numeric($avg_score)) {
370
                $avg_score = round($avg_score, 2);
371
            } else {
372
                $avg_score = '-';
373
            }
374
375
            $return .= '    <td><div>'.$avg_score.'</div></td>';
376
            // student tes score
377
            //$return .= '  <td><div style="width:40px">'.round(Tracking::get_avg_student_exercise_score ($user_id, $courseCode),2).'%</div></td>';
378
            // student messages
379
            $return .= '    <td><div>'.Tracking::count_student_messages($user_id, $courseCode).'</div></td>';
380
            // student assignments
381
            $return .= '    <td><div>'.Tracking::count_student_assignments($user_id, $courseCode).'</div></td>';
382
            // student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
383
            $exercises_results = self::exercises_results($user_id, $courseCode);
384
            $return .= '    <td width="105px"><div>'.(is_null($exercises_results['percentage']) ? '' : $exercises_results['score_obtained'].'/'.$exercises_results['score_possible'].' ( '.$exercises_results['percentage'].'% )').'</div></td>';
385
            $return .= '    <td><div>'.$exercises_results['questions_answered'].'</div></td>';
386
            $return .= '    <td><div>'.Tracking::get_last_connection_date_on_the_course($user_id, $courseInfo).'</div></td>';
387
            $return .= '<tr>';
388
        }
389
        $return .= '</table>';
390
391
        return $return;
392
    }
393
394
    /**
395
     * Display a sortable table that contains an overview off all the
396
     * reporting progress of all users and all courses the user is subscribed to.
397
     *
398
     * @author Patrick Cool <[email protected]>, Ghent University, Belgium
399
     *          Alex Aragon <[email protected]>, BeezNest, Perú
400
     *
401
     * @version Chamilo 1.11.8
402
     *
403
     * @since April 2019
404
     */
405
    public static function returnTrackingUserOverviewFilter($user_id)
406
    {
407
        $tpl = new Template('', false, false, false, false, false, false);
408
        $userInfo = api_get_user_info($user_id);
409
410
        $avatar = UserManager::getUserPicture($user_id, USER_IMAGE_SIZE_SMALL);
411
        $user = [
412
            'id' => $user_id,
413
            'code_user' => $userInfo['official_code'],
414
            'complete_name' => $userInfo['complete_name'],
415
            'username' => $userInfo['username'],
416
            'course' => self::returnCourseTracking($user_id),
417
            'avatar' => $avatar,
418
        ];
419
420
        $tpl->assign('item', $user);
421
        $templateName = $tpl->get_template('my_space/partials/tracking_user_overview.tpl');
422
        $content = $tpl->fetch($templateName);
423
424
        return $content;
425
    }
426
427
    /**
428
     * Display a sortable table that contains an overview off all the
429
     * reporting progress of all users and all courses the user is subscribed to.
430
     *
431
     * @author Patrick Cool <[email protected]>, Ghent University, Belgium
432
     *         Alex Aragon <[email protected]>, BeezNest, Perú
433
     *
434
     * @version Chamilo 1.11.8
435
     *
436
     * @since October 2008, Update April 2019
437
     */
438
    public static function display_tracking_user_overview()
439
    {
440
        self::display_user_overview_export_options();
441
442
        $params = ['view' => 'admin', 'display' => 'user'];
443
        $table = new SortableTable(
444
            'tracking_user_overview',
445
            ['MySpace', 'get_number_of_users_tracking_overview'],
446
            ['MySpace', 'get_user_data_tracking_overview'],
447
            0,
448
            20,
449
            'ASC',
450
            null, [
451
                'class' => 'table table-transparent',
452
            ]
453
        );
454
        $table->additional_parameters = $params;
455
456
        $table->set_column_filter(0, ['MySpace', 'returnTrackingUserOverviewFilter']);
457
        $tableContent = $table->return_table();
458
        $tpl = new Template('', false, false, false, false, false, false);
459
        $tpl->assign('table', $tableContent);
460
        $templateName = $tpl->get_template('my_space/user_summary.tpl');
461
        $tpl->display($templateName);
462
    }
463
464
    /**
465
     * @param $export_csv
466
     */
467
    public static function display_tracking_coach_overview($export_csv)
468
    {
469
        if ($export_csv) {
470
            $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
471
        } else {
472
            $is_western_name_order = api_is_western_name_order();
473
        }
474
        $sort_by_first_name = api_sort_by_first_name();
475
        if (isset($_GET['tracking_list_coaches_column'])) {
476
            $tracking_column = (int) $_GET['tracking_list_coaches_column'];
477
        } else {
478
            $tracking_column = ($is_western_name_order xor $sort_by_first_name) ? 1 : 0;
479
        }
480
481
        $tracking_direction = (isset($_GET['tracking_list_coaches_direction']) && in_array(strtoupper($_GET['tracking_list_coaches_direction']), ['ASC', 'DESC', 'ASCENDING', 'DESCENDING', '0', '1'])) ? $_GET['tracking_list_coaches_direction'] : 'DESC';
482
        // Prepare array for column order - when impossible, use some of user names.
483
        if ($is_western_name_order) {
484
            $order = [
485
                0 => 'firstname',
486
                1 => 'lastname',
487
                2 => $sort_by_first_name ? 'firstname' : 'lastname',
488
                3 => 'login_date',
489
                4 => $sort_by_first_name ? 'firstname' : 'lastname',
490
                5 => $sort_by_first_name ? 'firstname' : 'lastname',
491
            ];
492
        } else {
493
            $order = [
494
                0 => 'lastname',
495
                1 => 'firstname',
496
                2 => $sort_by_first_name ? 'firstname' : 'lastname',
497
                3 => 'login_date',
498
                4 => $sort_by_first_name ? 'firstname' : 'lastname',
499
                5 => $sort_by_first_name ? 'firstname' : 'lastname',
500
            ];
501
        }
502
        $table = new SortableTable(
503
            'tracking_list_coaches_myspace',
504
            ['MySpace', 'count_coaches'],
505
            null,
506
            ($is_western_name_order xor $sort_by_first_name) ? 1 : 0
507
        );
508
        $parameters['view'] = 'admin';
509
        $table->set_additional_parameters($parameters);
510
        if ($is_western_name_order) {
511
            $table->set_header(0, get_lang('FirstName'), true);
512
            $table->set_header(1, get_lang('LastName'), true);
513
        } else {
514
            $table->set_header(0, get_lang('LastName'), true);
515
            $table->set_header(1, get_lang('FirstName'), true);
516
        }
517
        $table->set_header(2, get_lang('TimeSpentOnThePlatform'), false);
518
        $table->set_header(3, get_lang('LastConnexion'), false);
519
        $table->set_header(4, get_lang('NbStudents'), false);
520
        $table->set_header(5, get_lang('CountCours'), false);
521
        $table->set_header(6, get_lang('NumberOfSessions'), false);
522
        $table->set_header(7, get_lang('Sessions'), false);
523
524
        if ($is_western_name_order) {
525
            $csv_header[] = [
526
                get_lang('FirstName'),
527
                get_lang('LastName'),
528
                get_lang('TimeSpentOnThePlatform'),
529
                get_lang('LastConnexion'),
530
                get_lang('NbStudents'),
531
                get_lang('CountCours'),
532
                get_lang('NumberOfSessions'),
533
            ];
534
        } else {
535
            $csv_header[] = [
536
                get_lang('LastName'),
537
                get_lang('FirstName'),
538
                get_lang('TimeSpentOnThePlatform'),
539
                get_lang('LastConnexion'),
540
                get_lang('NbStudents'),
541
                get_lang('CountCours'),
542
                get_lang('NumberOfSessions'),
543
            ];
544
        }
545
546
        $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
547
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
548
        $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
549
        $tbl_sessions = Database::get_main_table(TABLE_MAIN_SESSION);
550
551
        $sqlCoachs = "SELECT DISTINCT
552
                        scu.user_id as id_coach,
553
                        u.id as user_id,
554
                        lastname,
555
                        firstname,
556
                        MAX(login_date) as login_date
557
                        FROM $tbl_user u, $tbl_session_course_user scu, $tbl_track_login
558
                        WHERE
559
                            scu.user_id = u.id AND scu.status=2 AND login_user_id=u.id
560
                        GROUP BY user_id ";
561
562
        if (api_is_multiple_url_enabled()) {
563
            $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
564
            $access_url_id = api_get_current_access_url_id();
565
            if ($access_url_id != -1) {
566
                $sqlCoachs = "SELECT DISTINCT
567
                                    scu.user_id as id_coach,
568
                                    u.id as user_id,
569
                                    lastname,
570
                                    firstname,
571
                                    MAX(login_date) as login_date
572
                                FROM $tbl_user u,
573
                                $tbl_session_course_user scu,
574
                                $tbl_track_login ,
575
                                $tbl_session_rel_access_url session_rel_url
576
                                WHERE
577
                                    scu.user_id = u.id AND
578
                                    scu.status = 2 AND
579
                                    login_user_id = u.id AND
580
                                    access_url_id = $access_url_id AND
581
                                    session_rel_url.session_id = scu.session_id
582
                                GROUP BY u.id";
583
            }
584
        }
585
        if (!empty($order[$tracking_column])) {
586
            $sqlCoachs .= " ORDER BY `".$order[$tracking_column]."` ".$tracking_direction;
587
        }
588
589
        $result_coaches = Database::query($sqlCoachs);
590
        $global_coaches = [];
591
        while ($coach = Database::fetch_array($result_coaches)) {
592
            $global_coaches[$coach['user_id']] = $coach;
593
        }
594
595
        $sql_session_coach = "SELECT session.id_coach, u.id as user_id, lastname, firstname, MAX(login_date) as login_date
596
                                FROM $tbl_user u , $tbl_sessions as session, $tbl_track_login
597
                                WHERE id_coach = u.id AND login_user_id = u.id
598
                                GROUP BY u.id
599
                                ORDER BY login_date $tracking_direction";
600
601
        if (api_is_multiple_url_enabled()) {
602
            $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
603
            $access_url_id = api_get_current_access_url_id();
604
            if ($access_url_id != -1) {
605
                $sql_session_coach = "SELECT session.id_coach, u.id as user_id, lastname, firstname, MAX(login_date) as login_date
606
					FROM $tbl_user u , $tbl_sessions as session, $tbl_track_login , $tbl_session_rel_access_url as session_rel_url
607
					WHERE
608
					    id_coach = u.id AND
609
					    login_user_id = u.id  AND
610
					    access_url_id = $access_url_id AND
611
					    session_rel_url.session_id = session.id
612
					GROUP BY  u.id
613
					ORDER BY login_date $tracking_direction";
614
            }
615
        }
616
617
        $result_sessions_coach = Database::query($sql_session_coach);
618
        //$total_no_coaches += Database::num_rows($result_sessions_coach);
619
        while ($coach = Database::fetch_array($result_sessions_coach)) {
620
            $global_coaches[$coach['user_id']] = $coach;
621
        }
622
623
        $all_datas = [];
624
        foreach ($global_coaches as $id_coach => $coaches) {
625
            $time_on_platform = api_time_to_hms(
626
                Tracking::get_time_spent_on_the_platform($coaches['user_id'])
627
            );
628
            $last_connection = Tracking::get_last_connection_date(
629
                $coaches['user_id']
630
            );
631
            $nb_students = count(
632
                Tracking::get_student_followed_by_coach($coaches['user_id'])
633
            );
634
            $nb_courses = count(
635
                Tracking::get_courses_followed_by_coach($coaches['user_id'])
636
            );
637
            $nb_sessions = count(
638
                Tracking::get_sessions_coached_by_user($coaches['user_id'])
639
            );
640
641
            $table_row = [];
642
            if ($is_western_name_order) {
643
                $table_row[] = $coaches['firstname'];
644
                $table_row[] = $coaches['lastname'];
645
            } else {
646
                $table_row[] = $coaches['lastname'];
647
                $table_row[] = $coaches['firstname'];
648
            }
649
            $table_row[] = $time_on_platform;
650
            $table_row[] = $last_connection;
651
            $table_row[] = $nb_students;
652
            $table_row[] = $nb_courses;
653
            $table_row[] = $nb_sessions;
654
            $table_row[] = '<a href="session.php?id_coach='.$coaches['user_id'].'">
655
                '.Display::return_icon('2rightarrow.png', get_lang('Details')).'
656
            </a>';
657
            $all_datas[] = $table_row;
658
659
            if ($is_western_name_order) {
660
                $csv_content[] = [
661
                    api_html_entity_decode($coaches['firstname'], ENT_QUOTES),
662
                    api_html_entity_decode($coaches['lastname'], ENT_QUOTES),
663
                    $time_on_platform,
664
                    $last_connection,
665
                    $nb_students,
666
                    $nb_courses,
667
                    $nb_sessions,
668
                ];
669
            } else {
670
                $csv_content[] = [
671
                    api_html_entity_decode($coaches['lastname'], ENT_QUOTES),
672
                    api_html_entity_decode($coaches['firstname'], ENT_QUOTES),
673
                    $time_on_platform,
674
                    $last_connection,
675
                    $nb_students,
676
                    $nb_courses,
677
                    $nb_sessions,
678
                ];
679
            }
680
        }
681
682
        if ($tracking_column != 3) {
683
            if ($tracking_direction == 'DESC') {
684
                usort($all_datas, ['MySpace', 'rsort_users']);
685
            } else {
686
                usort($all_datas, ['MySpace', 'sort_users']);
687
            }
688
        }
689
690
        if ($export_csv && $tracking_column != 3) {
691
            usort($csv_content, 'sort_users');
692
        }
693
        if ($export_csv) {
694
            $csv_content = array_merge($csv_header, $csv_content);
695
        }
696
697
        foreach ($all_datas as $row) {
698
            $table->addRow($row, 'align="right"');
699
        }
700
        $table->display();
701
    }
702
703
    /**
704
     * @return mixed
705
     */
706
    public static function count_coaches()
707
    {
708
        global $total_no_coaches;
709
710
        return $total_no_coaches;
711
    }
712
713
    public static function sort_users($a, $b)
714
    {
715
        $tracking = Session::read('tracking_column');
716
717
        return api_strcmp(
718
            trim(api_strtolower($a[$tracking])),
719
            trim(api_strtolower($b[$tracking]))
720
        );
721
    }
722
723
    public static function rsort_users($a, $b)
724
    {
725
        $tracking = Session::read('tracking_column');
726
727
        return api_strcmp(
728
            trim(api_strtolower($b[$tracking])),
729
            trim(api_strtolower($a[$tracking]))
730
        );
731
    }
732
733
    /**
734
     * Display a sortable table that contains an overview off all the progress of the user in a session.
735
     *
736
     * @deprecated ?
737
     *
738
     * @author César Perales <[email protected]>, Beeznest Team
739
     */
740
    public static function display_tracking_lp_progress_overview(
741
        $sessionId = '',
742
        $courseId = '',
743
        $date_from,
744
        $date_to
745
    ) {
746
        $course = api_get_course_info_by_id($courseId);
747
        /**
748
         * Column name
749
         * The order is important you need to check the $column variable in the model.ajax.php file.
750
         */
751
        $columns = [
752
            get_lang('Username'),
753
            get_lang('FirstName'),
754
            get_lang('LastName'),
755
        ];
756
        //add lessons of course
757
        $lessons = LearnpathList::get_course_lessons($course['code'], $sessionId);
758
759
        //create columns array
760
        foreach ($lessons as $lesson_id => $lesson) {
761
            $columns[] = $lesson['name'];
762
        }
763
764
        $columns[] = get_lang('Total');
765
766
        /**
767
         * Column config.
768
         */
769
        $column_model = [
770
            [
771
                'name' => 'username',
772
                'index' => 'username',
773
                'align' => 'left',
774
                'search' => 'true',
775
                'wrap_cell' => "true",
776
            ],
777
            [
778
                'name' => 'firstname',
779
                'index' => 'firstname',
780
                'align' => 'left',
781
                'search' => 'true',
782
            ],
783
            [
784
                'name' => 'lastname',
785
                'index' => 'lastname',
786
                'align' => 'left',
787
                'search' => 'true',
788
            ],
789
        ];
790
791
        // Get dinamic column names
792
        foreach ($lessons as $lesson_id => $lesson) {
793
            $column_model[] = [
794
                'name' => $lesson['id'],
795
                'index' => $lesson['id'],
796
                'align' => 'left',
797
                'search' => 'true',
798
            ];
799
        }
800
801
        $column_model[] = [
802
            'name' => 'total',
803
            'index' => 'total',
804
            'align' => 'left',
805
            'search' => 'true',
806
        ];
807
808
        $action_links = '';
809
        // jqgrid will use this URL to do the selects
810
        $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_session_lp_progress&session_id='.$sessionId.'&course_id='.$courseId.'&date_to='.$date_to.'&date_from='.$date_from;
811
812
        // Table Id
813
        $tableId = 'lpProgress';
814
815
        // Autowidth
816
        $extra_params['autowidth'] = 'true';
817
818
        // height auto
819
        $extra_params['height'] = 'auto';
820
821
        $table = Display::grid_js(
822
            $tableId,
823
            $url,
824
            $columns,
825
            $column_model,
826
            $extra_params,
827
            [],
828
            $action_links,
829
            true
830
        );
831
832
        $return = '<script>$(function() {'.$table.
833
            'jQuery("#'.$tableId.'").jqGrid("navGrid","#'.$tableId.'_pager",{view:false, edit:false, add:false, del:false, search:false, excel:true});
834
                jQuery("#'.$tableId.'").jqGrid("navButtonAdd","#'.$tableId.'_pager",{
835
                       caption:"",
836
                       title:"'.get_lang('ExportExcel').'",
837
                       onClickButton : function () {
838
                           jQuery("#'.$tableId.'").jqGrid("excelExport",{"url":"'.$url.'&export_format=xls"});
839
                       }
840
                });
841
            });</script>';
842
        $return .= Display::grid_html($tableId);
843
844
        return $return;
845
    }
846
847
    /**
848
     * Display a sortable table that contains an overview off all the progress of the user in a session.
849
     *
850
     * @param int $sessionId  The session ID
851
     * @param int $courseId   The course ID
852
     * @param int $exerciseId The quiz ID
853
     * @param     $date_from
854
     * @param     $date_to
855
     *
856
     * @return string HTML array of results formatted for gridJS
857
     *
858
     * @deprecated ?
859
     *
860
     * @author César Perales <[email protected]>, Beeznest Team
861
     */
862
    public static function display_tracking_exercise_progress_overview(
863
        $sessionId = 0,
864
        $courseId = 0,
865
        $exerciseId = 0,
866
        $date_from = null,
867
        $date_to = null
868
    ) {
869
        $date_from = Security::remove_XSS($date_from);
870
        $date_to = Security::remove_XSS($date_to);
871
        /**
872
         * Column names
873
         * The column order is important. Check $column variable in the main/inc/ajax/model.ajax.php file.
874
         */
875
        $columns = [
876
            get_lang('Session'),
877
            get_lang('ExerciseId'),
878
            get_lang('ExerciseName'),
879
            get_lang('Username'),
880
            get_lang('LastName'),
881
            get_lang('FirstName'),
882
            get_lang('Time'),
883
            get_lang('QuestionId'),
884
            get_lang('QuestionTitle'),
885
            get_lang('WorkDescription'),
886
            get_lang('Answer'),
887
            get_lang('Correct'),
888
        ];
889
890
        /**
891
         * Column config.
892
         */
893
        $column_model = [
894
            ['name' => 'session', 'index' => 'session', 'align' => 'left', 'search' => 'true', 'wrap_cell' => "true"],
895
            ['name' => 'exercise_id', 'index' => 'exercise_id', 'align' => 'left', 'search' => 'true'],
896
            ['name' => 'quiz_title', 'index' => 'quiz_title', 'align' => 'left', 'search' => 'true'],
897
            ['name' => 'username', 'index' => 'username', 'align' => 'left', 'search' => 'true'],
898
            ['name' => 'lastname', 'index' => 'lastname', 'align' => 'left', 'search' => 'true'],
899
            ['name' => 'firstname', 'index' => 'firstname', 'align' => 'left', 'search' => 'true'],
900
            ['name' => 'time', 'index' => 'time', 'align' => 'left', 'search' => 'true', 'wrap_cell' => "true"],
901
            ['name' => 'question_id', 'index' => 'question_id', 'align' => 'left', 'search' => 'true'],
902
            ['name' => 'question', 'index' => 'question', 'align' => 'left', 'search' => 'true', 'wrap_cell' => "true"],
903
            ['name' => 'description', 'index' => 'description', 'align' => 'left', 'width' => '550', 'search' => 'true', 'wrap_cell' => "true"],
904
            ['name' => 'answer', 'index' => 'answer', 'align' => 'left', 'search' => 'true', 'wrap_cell' => "true"],
905
            ['name' => 'correct', 'index' => 'correct', 'align' => 'left', 'search' => 'true', 'wrap_cell' => "true"],
906
        ];
907
        //get dynamic column names
908
909
        // jqgrid will use this URL to do the selects
910
        $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_exercise_progress&session_id='.$sessionId.'&course_id='.$courseId.'&exercise_id='.$exerciseId.'&date_to='.$date_to.'&date_from='.$date_from;
911
912
        // Autowidth
913
        $extra_params['autowidth'] = 'true';
914
915
        // height auto
916
        $extra_params['height'] = 'auto';
917
918
        $tableId = 'exerciseProgressOverview';
919
        $table = Display::grid_js(
920
            $tableId,
921
            $url,
922
            $columns,
923
            $column_model,
924
            $extra_params,
925
            [],
926
            '',
927
            true
928
        );
929
930
        $return = '<script>$(function() {'.$table.
931
            'jQuery("#'.$tableId.'").jqGrid("navGrid","#'.$tableId.'_pager",{view:false, edit:false, add:false, del:false, search:false, excel:true});
932
                jQuery("#'.$tableId.'").jqGrid("navButtonAdd","#'.$tableId.'_pager",{
933
                       caption:"",
934
                       title:"'.get_lang('ExportExcel').'",
935
                       onClickButton : function () {
936
                           jQuery("#'.$tableId.'").jqGrid("excelExport",{"url":"'.$url.'&export_format=xls"});
937
                       }
938
                });
939
            });</script>';
940
        $return .= Display::grid_html($tableId);
941
942
        return $return;
943
    }
944
945
    /**
946
     * Displays a form with all the additionally defined user fields of the profile
947
     * and give you the opportunity to include these in the CSV export.
948
     *
949
     * @author Patrick Cool <[email protected]>, Ghent University, Belgium
950
     *
951
     * @version 1.8.6
952
     *
953
     * @since November 2008
954
     */
955
    public static function display_user_overview_export_options()
956
    {
957
        $message = '';
958
        $defaults = [];
959
        // include the user manager and formvalidator library
960
        if (isset($_GET['export']) && 'options' == $_GET['export']) {
961
            // get all the defined extra fields
962
            $extrafields = UserManager::get_extra_fields(
963
                0,
964
                50,
965
                5,
966
                'ASC',
967
                false,
968
                1
969
            );
970
971
            // creating the form with all the defined extra fields
972
            $form = new FormValidator(
973
                'exportextrafields',
974
                'post',
975
                api_get_self()."?view=".Security::remove_XSS($_GET['view']).'&display='.Security::remove_XSS($_GET['display']).'&export='.Security::remove_XSS($_GET['export'])
976
            );
977
978
            if (is_array($extrafields) && count($extrafields) > 0) {
979
                foreach ($extrafields as $key => $extra) {
980
                    $form->addElement('checkbox', 'extra_export_field'.$extra[0], '', $extra[3]);
981
                }
982
                $form->addButtonSave(get_lang('Ok'), 'submit');
983
984
                // setting the default values for the form that contains all the extra fields
985
                $exportFields = Session::read('additional_export_fields');
986
                if (is_array($exportFields)) {
987
                    foreach ($exportFields as $key => $value) {
988
                        $defaults['extra_export_field'.$value] = 1;
989
                    }
990
                }
991
                $form->setDefaults($defaults);
992
            } else {
993
                $form->addElement('html', Display::return_message(get_lang('ThereAreNotExtrafieldsAvailable'), 'warning'));
994
            }
995
996
            if ($form->validate()) {
997
                // exporting the form values
998
                $values = $form->exportValues();
999
1000
                // re-initialising the session that contains the additional fields that need to be exported
1001
                Session::write('additional_export_fields', []);
1002
1003
                // adding the fields that are checked to the session
1004
                $message = '';
1005
                $additionalExportFields = [];
1006
                foreach ($values as $field_ids => $value) {
1007
                    if ($value == 1 && strstr($field_ids, 'extra_export_field')) {
1008
                        $additionalExportFields[] = str_replace('extra_export_field', '', $field_ids);
1009
                    }
1010
                }
1011
                Session::write('additional_export_fields', $additionalExportFields);
1012
1013
                // adding the fields that will be also exported to a message string
1014
                $additionalExportFields = Session::read('additional_export_fields');
1015
                if (is_array($additionalExportFields)) {
1016
                    foreach ($additionalExportFields as $key => $extra_field_export) {
1017
                        $message .= '<li>'.$extrafields[$extra_field_export][3].'</li>';
1018
                    }
1019
                }
1020
1021
                // Displaying a feedback message
1022
                if (!empty($additionalExportFields)) {
1023
                    echo Display::return_message(
1024
                        get_lang('FollowingFieldsWillAlsoBeExported').': <br /><ul>'.$message.'</ul>',
1025
                        'confirm',
1026
                        false
1027
                    );
1028
                } else {
1029
                    echo Display::return_message(
1030
                        get_lang('NoAdditionalFieldsWillBeExported'),
1031
                        'confirm',
1032
                        false
1033
                    );
1034
                }
1035
            } else {
1036
                $form->display();
1037
            }
1038
        } else {
1039
            $additionalExportFields = Session::read('additional_export_fields');
1040
            if (!empty($additionalExportFields)) {
1041
                // get all the defined extra fields
1042
                $extrafields = UserManager::get_extra_fields(0, 50, 5, 'ASC');
1043
1044
                foreach ($additionalExportFields as $key => $extra_field_export) {
1045
                    $message .= '<li>'.$extrafields[$extra_field_export][3].'</li>';
1046
                }
1047
1048
                echo Display::return_message(
1049
                    get_lang('FollowingFieldsWillAlsoBeExported').': <br /><ul>'.$message.'</ul>',
1050
                    'normal',
1051
                    false
1052
                );
1053
            }
1054
        }
1055
    }
1056
1057
    /**
1058
     * Export to cvs a list of users who were enrolled in the lessons.
1059
     * It is necessary that in the extra field, a company is defined.
1060
     *
1061
     * @param string|null $startDate
1062
     * @param string|null $endDate
1063
     *
1064
     * @return array
1065
     */
1066
    public static function exportCompanyResumeCsv($startDate, $endDate)
1067
    {
1068
        $companys = self::getCompanyLearnpathSubscription($startDate, $endDate);
1069
        $csv_content = [];
1070
        // Printing table
1071
        $total = 0;
1072
        $displayText = get_lang('Company');
1073
        // the first line of the csv file with the column headers
1074
        $csv_row = [];
1075
        $csv_row[] = $displayText;
1076
1077
        $csv_row[] = get_lang('CountOfSubscribedUsers');
1078
        $csv_content[] = $csv_row;
1079
1080
        foreach ($companys as $entity => $student) {
1081
            $csv_row = [];
1082
            // user official code
1083
            $csv_row[] = $entity;
1084
            $csv_row[] = count($student);
1085
            $total += count($student);
1086
            $csv_content[] = $csv_row;
1087
        }
1088
1089
        $csv_row = [];
1090
        // user official code
1091
        $csv_row[] = get_lang('GeneralTotal');
1092
        $csv_row[] = $total;
1093
        $csv_content[] = $csv_row;
1094
        Export::arrayToCsv($csv_content, 'reporting_company_resume');
1095
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
1096
    }
1097
1098
    /**
1099
     * Generates a structure to show the links or names for the authors by lesson report.
1100
     *
1101
     * @param array $students
1102
     * @param array $studentRegistered
1103
     * @param       $lpCourseCode
1104
     */
1105
    public static function getStudentDataToReportByLp($students = [], $studentRegistered = [], $lpCourseCode)
1106
    {
1107
        $data = [];
1108
        $totalStudents = 0;
1109
        $data['csv'] = '';
1110
        $data['html'] = '';
1111
        $icon = Display::return_icon('statistics.png', get_lang('Stats'));
1112
        foreach ($students as $student) {
1113
            $lpSessionId = isset($student['session_id']) ? (int) $student['session_id'] : 0;
1114
            $studentId = (int) $student['id'];
1115
            if (!isset($studentRegistered[$studentId][$lpSessionId])) {
1116
                $url = api_get_path(WEB_CODE_PATH)."mySpace/myStudents.php?details=true&student=$studentId";
1117
                if (0 != $lpSessionId) {
1118
                    $url .= "&id_session=$lpSessionId";
1119
                }
1120
                $url .= "&course=$lpCourseCode";
1121
                $reportLink = Display::url(
1122
                    $icon,
1123
                    $url
1124
                );
1125
                $studentName = $student['complete_name']."(".$student['company'].")";
1126
                $studentRegistered[$studentId][$lpSessionId] = $student;
1127
                $data['csv'] .= $studentName.' / ';
1128
                $data['html'] .= "$reportLink <strong>$studentName</strong><br>";
1129
                $totalStudents++;
1130
            }
1131
        }
1132
        $data['student_registered'] = $studentRegistered;
1133
        $data['total_students'] = $totalStudents;
1134
1135
        return $data;
1136
    }
1137
1138
    /**
1139
     * * Generates a structure to show the names for the authors by lesson report by item.
1140
     *
1141
     * @param array  $students
1142
     * @param array  $studentProcessed
1143
     * @param string $typeReport
1144
     * @param false  $csv
1145
     */
1146
    public static function getStudentDataToReportByLpItem($students = [], $studentProcessed = [], $typeReport = '', $csv = false)
1147
    {
1148
        $totalStudent = count($students);
1149
        $sessionIcon = Display::return_icon(
1150
            'admin_star.png',
1151
            get_lang('StudentInSessionCourse'),
1152
            [],
1153
            ICON_SIZE_MEDIUM
1154
        );
1155
        $classIcon = Display::return_icon(
1156
            'group_summary.png',
1157
            get_lang('UsersInsideClass'),
1158
            '',
1159
            ICON_SIZE_MEDIUM
1160
        );
1161
        /* use 'for' to performance */
1162
        for ($i = 0; $i < $totalStudent; $i++) {
1163
            $student = $students[$i];
1164
            $studentId = $student['id'];
1165
            $lpItemIdStudent = $student['lp_item_id'];
1166
            $sessionId = isset($student['session_id']) ? (int) $student['session_id'] : 0;
1167
            $studentName = $student['complete_name'];
1168
            $studentCompany = $student['company'];
1169
            $studentName = "$studentName($studentCompany)";
1170
            $type = isset($student['type']) ? $student['type'] : null;
1171
            $icon = null;
1172
            if (0 != $sessionId) {
1173
                $icon = $sessionIcon;
1174
            }
1175
            if ('class' == $typeReport) {
1176
                $icon = $classIcon;
1177
            }
1178
            $studentString = "$icon $studentName";
1179
            if (0 != $sessionId) {
1180
                $studentString = "<strong>$studentString</strong>";
1181
            }
1182
            if ($csv == false) {
1183
                $studentProcessed[$lpItemIdStudent][$type][$studentId] = $studentString.'<br>';
1184
            } else {
1185
                $studentProcessed[$lpItemIdStudent][$type][$studentId] = "$studentName / ";
1186
            }
1187
        }
1188
1189
        return $studentProcessed;
1190
    }
1191
1192
    /**
1193
     * Displays a list as a table of users who were enrolled in the lessons.
1194
     * It is necessary that in the extra field, a company is defined.
1195
     *
1196
     * @param string|null $startDate
1197
     * @param string|null $endDate
1198
     */
1199
    public static function displayResumeCompany(
1200
        $startDate = null,
1201
        $endDate = null
1202
    ) {
1203
        $companys = self::getCompanyLearnpathSubscription($startDate, $endDate);
1204
        $tableHtml = '';
1205
        // Printing table
1206
        $total = 0;
1207
        $table = "<div class='table-responsive'><table class='table table-hover table-striped table-bordered data_table'>";
1208
1209
        $displayText = get_lang('Company');
1210
        $table .= "<thead><tr><th class='th-header'>$displayText</th><th class='th-header'> ".get_lang('CountOfSubscribedUsers')." </th></tr></thead><tbody>";
1211
1212
        foreach ($companys as $entity => $student) {
1213
            $table .= "<tr><td>$entity</td><td>".count($student)."</td></tr>";
1214
            $total += count($student);
1215
        }
1216
        $table .= "<tr><td>".get_lang('GeneralTotal')."</td><td>$total</td></tr>";
1217
        $table .= '</tbody></table></div>';
1218
1219
        if (!empty($startDate) or !empty($endDate)) {
1220
            $tableHtml = $table;
1221
        }
1222
1223
        $form = new FormValidator('searchDate', 'get');
1224
        $form->addHidden('display', 'company');
1225
        $today = new DateTime();
1226
        if (empty($startDate)) {
1227
            $startDate = api_get_local_time($today->modify('first day of this month')->format('Y-m-d'));
1228
        }
1229
        if (empty($endDate)) {
1230
            $endDate = api_get_local_time($today->modify('last day of this month')->format('Y-m-d'));
1231
        }
1232
        $form->addDatePicker(
1233
            'startDate',
1234
            get_lang('DateStart'),
1235
            [
1236
                'value' => $startDate,
1237
            ]);
1238
        $form->addDatePicker(
1239
            'endDate',
1240
            get_lang('DateEnd'),
1241
            [
1242
                'value' => $endDate,
1243
            ]);
1244
        $form->addButtonSearch(get_lang('Search'));
1245
        if (count($companys) != 0) {
1246
            //$form->addButtonSave(get_lang('Ok'), 'export');
1247
            $form
1248
                ->addButton(
1249
                    'export_csv',
1250
                    get_lang('ExportAsCSV'),
1251
                    'check',
1252
                    'primary',
1253
                    null,
1254
                    null,
1255
                    [
1256
                    ]
1257
                );
1258
        }
1259
1260
        $tableContent = $form->returnForm();
1261
        $tableContent .= $tableHtml;
1262
        // $tableContent .= $table->return_table();
1263
1264
        $tpl = new Template('', false, false, false, false, false, false);
1265
        $tpl->assign('table', $tableContent);
1266
        $templateName = $tpl->get_template('my_space/course_summary.tpl');
1267
        $tpl->display($templateName);
1268
    }
1269
1270
    /**
1271
     *  Displays a list as a table of teachers who are set authors by a extra_field authors.
1272
     *
1273
     * @param string|null $startDate
1274
     * @param string|null $endDate
1275
     * @param bool        $csv
1276
     */
1277
    public static function displayResumeLP(
1278
        $startDate = null,
1279
        $endDate = null,
1280
        $csv = false
1281
    ) {
1282
        $tblExtraField = Database::get_main_table(TABLE_EXTRA_FIELD);
1283
        $tblCourse = Database::get_main_table(TABLE_MAIN_COURSE);
1284
        $tblExtraFieldValue = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
1285
        $tblLpItem = Database::get_course_table(TABLE_LP_ITEM);
1286
        $tblLp = Database::get_course_table(TABLE_LP_MAIN);
1287
        $tblAccessUrlCourse = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
1288
        $accessUrlFilter = '';
1289
        if (api_is_multiple_url_enabled()) {
1290
            $urlId = api_get_current_access_url_id();
1291
            $accessUrlFilter = " INNER JOIN $tblAccessUrlCourse aurc
1292
                      ON (c.id = aurc.c_id AND aurc.access_url_id = $urlId)";
1293
        }
1294
        $query = "
1295
        SELECT DISTINCT
1296
            lp.name,
1297
            lpi.title,
1298
            lp.id as lp_id,
1299
            lpi.id AS lp_item_id,
1300
            REPLACE (efv.value, ';', ',') AS users_id,
1301
            c.title AS course_title,
1302
            c.code AS course_code
1303
        FROM $tblExtraFieldValue AS efv
1304
        INNER JOIN $tblExtraField AS ef
1305
        ON (
1306
            efv.field_id = ef.id AND
1307
            ef.variable = 'authorlpitem' AND
1308
            efv.value != ''
1309
            )
1310
        INNER JOIN $tblLpItem AS lpi
1311
        ON (efv.item_id = lpi.iid)
1312
        INNER JOIN $tblLp AS lp
1313
        ON (lpi.lp_id = lp.iid AND lpi.c_id = lp.c_id)
1314
        INNER JOIN $tblCourse AS c
1315
        ON (lp.c_id = c.id)
1316
        $accessUrlFilter";
1317
        $queryResult = Database::query($query);
1318
        $dataTeachers = Database::store_result($queryResult, 'ASSOC');
1319
        $totalData = count($dataTeachers);
1320
        $lpItems = [];
1321
        $teachers = [];
1322
        $users = [];
1323
        $learningPaths = [];
1324
        $csvContent = [];
1325
        $htmlData = '';
1326
        /* use 'for' to performance */
1327
        for ($i = 0; $i < $totalData; $i++) {
1328
            $row = $dataTeachers[$i];
1329
            $lpId = $row['lp_id'];
1330
            $lpItems[] = $lpId;
1331
            $authorData = $row['users_id'];
1332
            $learningPaths[$lpId] = $row;
1333
            if (strpos($authorData, ",") === false) {
1334
                if (!isset($users[$authorData])) {
1335
                    $users[$authorData] = api_get_user_info($authorData);
1336
                }
1337
                $teachers[$authorData][$lpId] = $users[$authorData];
1338
                $learningPaths[$lpId]['teachers'][$authorData] = $users[$authorData];
1339
            } else {
1340
                $items = explode(',', $authorData);
1341
                $totalItems = count($items);
1342
                for ($j = 0; $j < $totalItems; $j++) {
1343
                    $authorData = $items[$j];
1344
                    if (!isset($users[$authorData])) {
1345
                        $users[$authorData] = api_get_user_info($authorData);
1346
                    }
1347
                    $teachers[$authorData][$lpId] = $users[$authorData];
1348
                    $learningPaths[$lpId]['teachers'][$authorData] = $users[$authorData];
1349
                }
1350
            }
1351
        }
1352
        $lpItems = array_unique($lpItems);
1353
        $whereInLp = implode(',', $lpItems);
1354
        if (count($lpItems) != 0) {
1355
            $registeredUsers = self::getCompanyLearnpathSubscription(
1356
                $startDate,
1357
                $endDate,
1358
                $whereInLp
1359
            );
1360
            foreach ($registeredUsers as $students) {
1361
                $totalStudents = count($students);
1362
                /* use 'for' to performance */
1363
                for ($i = 0; $i < $totalStudents; $i++) {
1364
                    $user = $students[$i];
1365
                    $lpId = $user['lp_item'];
1366
                    $studentId = $user['id'];
1367
                    $learningPaths[$lpId]['courseStudent'][$studentId] = $user;
1368
                }
1369
            }
1370
            $registeredUsersBySession = self::getSessionAddUserCourseFromTrackDefault(
1371
                $startDate,
1372
                $endDate,
1373
                $whereInLp
1374
            );
1375
            foreach ($registeredUsersBySession as $lpId => $student) {
1376
                $totalStudents = count($student);
1377
                /* use 'for' to performance */
1378
                for ($i = 0; $i < $totalStudents; $i++) {
1379
                    $user = $student[$i];
1380
                    $lpId = $user['lp'];
1381
                    $studentId = $user['id'];
1382
                    $learningPaths[$lpId]['sessionStudent'][$studentId] = $user;
1383
                    $learningPaths[$lpId]['sessionStudent'][$studentId]['session_id'] = $user;
1384
                }
1385
            }
1386
            $registeredUsersGroup = self::getCompanyLearnpathSubscription(
1387
                $startDate,
1388
                $endDate,
1389
                $whereInLp,
1390
                true
1391
            );
1392
            foreach ($registeredUsersGroup as $student) {
1393
                $totalStudents = count($student);
1394
                /* use 'for' to performance */
1395
                for ($i = 0; $i < $totalStudents; $i++) {
1396
                    $user = $student[$i];
1397
                    $lpId = $user['lp_item'];
1398
                    $studentId = $user['id'];
1399
                    $learningPaths[$lpId]['courseStudentGroup'][$studentId] = $user;
1400
                }
1401
            }
1402
1403
            $index = 0;
1404
            $iconAdd = Display::return_icon('add.png', get_lang('ShowOrHide'), '', ICON_SIZE_SMALL);
1405
            $iconRemove = Display::return_icon('error.png', get_lang('ShowOrHide'), '', ICON_SIZE_SMALL);
1406
            $htmlData = "<div class='table-responsive'>
1407
            <table class='table table-hover table-striped table-bordered data_table'>
1408
            <thead>
1409
                <tr>
1410
                    <th class='th-header'>".get_lang('Author')."</th>
1411
                    <th class='th-header'>".get_lang('LearningPathList')."</th>
1412
                    <th class='th-header'>".get_lang('CountOfSubscribedUsers')."</th>
1413
                    <th class='th-header'>".get_lang('StudentList')."</th>
1414
                </tr>
1415
            </thead>
1416
                <tbody>";
1417
            $lastTeacher = '';
1418
            /* csv */
1419
            $csv_row = [];
1420
            $csv_row[] = get_lang('Author');
1421
            $csv_row[] = get_lang('LearningPathList');
1422
            $csv_row[] = get_lang('CountOfSubscribedUsers');
1423
            $csv_row[] = get_lang('StudentList');
1424
            $csvContent[] = $csv_row;
1425
            $studentsName = '';
1426
            /* csv */
1427
            foreach ($teachers as $authorLId => $teacher) {
1428
                $totalStudents = 0;
1429
                foreach ($teacher as $lpId => $teacherData) {
1430
                    $lpSessionId = 0;
1431
                    $lpData = $learningPaths[$lpId];
1432
                    $printTeacherName = ($lastTeacher != $teacherData['complete_name']) ? $teacherData['complete_name'] : '';
1433
                    $htmlData .= "<tr><td>$printTeacherName</td>";
1434
                    $hiddenField = 'student_show_'.$index;
1435
                    $hiddenFieldLink = 'student_show_'.$index.'_';
1436
                    $lpCourseCode = $lpData['course_code'];
1437
                    $lpName = $lpData['name'];
1438
                    $courseStudent = isset($lpData['courseStudent']) ? $lpData['courseStudent'] : [];
1439
                    $courseStudentGroup = isset($lpData['courseStudentGroup']) ? $lpData['courseStudentGroup'] : [];
1440
                    $sessionStudent = isset($lpData['sessionStudent']) ? $lpData['sessionStudent'] : [];
1441
                    $htmlData .= "<td>$lpName</td><td>".count($courseStudent)." ( ".count($sessionStudent)." )</td><td>";
1442
                    $csv_row = [];
1443
                    $csv_row[] = $printTeacherName;
1444
                    $csv_row[] = $lpName;
1445
                    $csv_row[] = count($courseStudent).' ( '.count($sessionStudent)." )";
1446
                    if (!empty($courseStudent)
1447
                        || !empty($courseStudentGroup)
1448
                        || !empty($sessionStudent)
1449
                    ) {
1450
                        $htmlData .= "<a href='#!' id='$hiddenFieldLink' onclick='showHideStudent(\"$hiddenField\")'>
1451
                        <div class='icon_add'>$iconAdd</div>
1452
                        <div class='icon_remove hidden'>$iconRemove</div>
1453
                        </a>
1454
                        <div id='$hiddenField' class='hidden'>";
1455
                        $studentRegistered = [];
1456
1457
                        $tempArray = self::getStudentDataToReportByLp($courseStudent, $studentRegistered, $lpCourseCode);
1458
                        $studentsName .= $tempArray['csv'];
1459
                        $htmlData .= $tempArray['html'];
1460
                        $studentRegistered = $tempArray['student_registered'];
1461
                        $totalStudents += $tempArray['total_students'];
1462
1463
                        $tempArray = self::getStudentDataToReportByLp($sessionStudent, $studentRegistered, $lpCourseCode);
1464
                        $studentsName .= $tempArray['csv'];
1465
                        $htmlData .= $tempArray['html'];
1466
                        $studentRegistered = $tempArray['student_registered'];
1467
                        $totalStudents += $tempArray['total_students'];
1468
1469
                        $tempArray = self::getStudentDataToReportByLp($courseStudentGroup, $studentRegistered, $lpCourseCode);
1470
                        $studentsName .= $tempArray['csv'];
1471
                        $htmlData .= $tempArray['html'];
1472
                        $studentRegistered = $tempArray['student_registered'];
1473
                        $totalStudents += $tempArray['total_students'];
1474
1475
                        $htmlData .= "</div>";
1476
                    }
1477
                    $htmlData .= "</td></tr>";
1478
                    $index++;
1479
                    $csv_row[] = trim($studentsName, ' / ');
1480
                    $studentsName = '';
1481
                    $csvContent[] = $csv_row;
1482
                    $lastTeacher = $teacherData['complete_name'];
1483
                }
1484
                $htmlData .= "<tr>
1485
                <td></td>
1486
                <td><strong>".get_lang('LearnpathsTotal')." ".count($teacher)." </strong></td>
1487
                <td><strong>$totalStudents</strong></td>
1488
                <td></td>
1489
                </tr>";
1490
            }
1491
            $htmlData .= "</tbody>
1492
            </table>
1493
            </div>";
1494
        }
1495
        if (false == $csv) {
1496
            $form = new FormValidator('searchDate', 'get');
1497
            $form->addHidden('display', 'learningPath');
1498
            $today = new DateTime();
1499
            if (empty($startDate)) {
1500
                $startDate = $today->modify('first day of this month')->format('Y-m-d');
1501
            }
1502
            if (empty($endDate)) {
1503
                $endDate = $today->modify('last day of this month')->format('Y-m-d');
1504
            }
1505
            $form->addDatePicker(
1506
                'startDate',
1507
                get_lang('DateStart'),
1508
                [
1509
                    'value' => $startDate,
1510
                ]);
1511
            $form->addDatePicker(
1512
                'endDate',
1513
                get_lang('DateEnd'),
1514
                [
1515
                    'value' => $endDate,
1516
                ]);
1517
            $form->addButtonSearch(get_lang('Search'));
1518
            if (0 != count($csvContent)) {
1519
                $form
1520
                    ->addButton(
1521
                        'export_csv',
1522
                        get_lang('ExportAsCSV'),
1523
                        'check',
1524
                        'primary',
1525
                        null,
1526
                        null,
1527
                        [
1528
                        ]
1529
                    );
1530
            }
1531
            $tableContent = $form->returnForm();
1532
            if (!empty($startDate) || !empty($endDate)) {
1533
                $tableContent .= $htmlData;
1534
            }
1535
            $tpl = new Template('', false, false, false, false, false, false);
1536
            $tpl->assign('table', $tableContent);
1537
            $templateName = $tpl->get_template('my_space/course_summary.tpl');
1538
            $tpl->display($templateName);
1539
        } else {
1540
            if (count($csvContent) != 0) {
1541
                Export::arrayToCsv($csvContent, 'reporting_lp_by_authors');
1542
            }
1543
        }
1544
    }
1545
1546
    /**
1547
     *  Displays a list as a table of teachers who are set authors of lp's item by a extra_field authors.
1548
     */
1549
    public static function displayResumeLpByItem(string $startDate = null, string $endDate = null, bool $csv = false)
1550
    {
1551
        $tableHtml = '';
1552
        $table = '';
1553
        $tblExtraField = Database::get_main_table(TABLE_EXTRA_FIELD);
1554
        $tblExtraFieldValue = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
1555
        $tblLpItem = Database::get_course_table(TABLE_LP_ITEM);
1556
        $tblLp = Database::get_course_table(TABLE_LP_MAIN);
1557
        $tblAccessUrlCourse = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
1558
        $accessUrlFilter = '';
1559
        if (api_is_multiple_url_enabled()) {
1560
            $urlId = api_get_current_access_url_id();
1561
            $accessUrlFilter = " INNER JOIN $tblAccessUrlCourse aurc
1562
                      ON (lp.c_id = aurc.c_id AND aurc.access_url_id = $urlId)";
1563
        }
1564
        $index = 0;
1565
        $cLpItems = [];
1566
        $cLpItemsAuthor = [];
1567
        $authorArray = [];
1568
        $studentArray = [];
1569
        $whereInLp = [];
1570
        $dataSet = [];
1571
        /** Get lp items only with authors */
1572
        $sql = " SELECT
1573
                efv.item_id AS lp_item_id,
1574
                efv.value AS author
1575
            FROM $tblExtraFieldValue AS efv
1576
            INNER JOIN $tblExtraField AS ef
1577
            ON (
1578
                ef.variable = 'authorlpitem' AND
1579
                efv.field_id = ef.id AND
1580
                efv.value != ''
1581
            )
1582
            ORDER BY efv.item_id ";
1583
        $queryResult = Database::query($sql);
1584
        $data = Database::store_result($queryResult, 'ASSOC');
1585
        $totalData = count($data);
1586
        /* use 'for' to performance */
1587
        for ($i = 0; $i < $totalData; $i++) {
1588
            $cLpItemsAuthor[$data[$i]['lp_item_id']] = $data[$i]['author'];
1589
        }
1590
        /** Get lp items only with price */
1591
        $sql = " SELECT
1592
               lp.iid AS lp_id,
1593
               lp.name AS lp_name,
1594
               efv.item_id AS lp_item_id,
1595
               lpi.title AS title,
1596
               efv.value AS price
1597
            FROM $tblExtraFieldValue AS efv
1598
            INNER JOIN $tblExtraField AS ef
1599
            ON (
1600
                ef.variable = 'price' AND
1601
                efv.field_id = ef.id AND
1602
                efv.value > 0
1603
            )
1604
            INNER JOIN $tblLpItem AS lpi
1605
            ON (lpi.iid = efv.item_id)
1606
            INNER JOIN $tblLp AS lp
1607
            ON (lpi.lp_id = lp.iid AND lpi.c_id = lp.c_id)
1608
            $accessUrlFilter";
1609
        $queryResult = Database::query($sql);
1610
        $data = Database::store_result($queryResult, 'ASSOC');
1611
        $totalData = count($data);
1612
        /* use 'for' to performance */
1613
        for ($i = 0; $i < $totalData; $i++) {
1614
            $item = $data[$i];
1615
            $lpItemId = (int) $item['lp_item_id'];
1616
            $whereInLp[] = $item['lp_id'];
1617
            $author = isset($cLpItemsAuthor[$lpItemId]) ? $cLpItemsAuthor[$lpItemId] : null;
1618
            $item['author'] = $author;
1619
            if (!empty($author)) {
1620
                $cLpItems[count($cLpItems)] = $item;
1621
            }
1622
        }
1623
        $totalLpItems = count($cLpItems);
1624
        $tableNoData = "<div class='table-responsive'>
1625
                <table class='table table-hover table-striped table-bordered data_table'>
1626
                <thead>
1627
                    <tr>
1628
                    <th class='th-header'>".get_lang('NoDataAvailable').'</th>
1629
                </tr>
1630
                </thead>
1631
                </tbody>
1632
                </tbody>
1633
                </table>
1634
                </div>';
1635
        if (0 == $totalLpItems) {
1636
            $tableHtml = $tableNoData;
1637
        } elseif (0 == count($whereInLp)) {
1638
            $tableHtml = $tableNoData;
1639
        } else {
1640
            $whereInLp = array_unique($whereInLp);
1641
            $whereInLp = implode(',', $whereInLp);
1642
            $registeredUsersBySession = self::getSessionAddUserCourseFromTrackDefault(
1643
                $startDate,
1644
                $endDate,
1645
                $whereInLp
1646
            );
1647
            $registeredUsersInCourse = self::getUserSubscribedInCourseByDateAndLp($startDate, $endDate, $whereInLp);
1648
            $registeredUsersInLp = self::getCompanyLearnpathSubscription(
1649
                $startDate,
1650
                $endDate,
1651
                $whereInLp
1652
            );
1653
            $registeredGroupsInLp = self::getCompanyLearnpathSubscription(
1654
                $startDate,
1655
                $endDate,
1656
                $whereInLp,
1657
                true
1658
            );
1659
            /* use 'for' to performance */
1660
            for ($i = 0; $i < $totalLpItems; $i++) {
1661
                $lpItem = $cLpItems[$i];
1662
                $lpItemId = $lpItem['lp_item_id'];
1663
                $author = str_replace(';', ',', $lpItem['author']);
1664
                $tempArrayAuthor = explode(',', $author);
1665
                $byCourse = $registeredUsersInLp[$lpItemId] ?? [];
1666
                $byCourseGroups = $registeredGroupsInLp[$lpItemId] ?? [];
1667
                $bySession = $registeredUsersBySession[$lpItemId] ?? [];
1668
                $byUserInCourse = $registeredUsersInCourse[$lpItemId] ?? [];
1669
                if (is_array($tempArrayAuthor)) {
1670
                    $totalAuthors = count($tempArrayAuthor);
1671
                    for ($j = 0; $j < $totalAuthors; $j++) {
1672
                        if (!isset($authorArray[$tempArrayAuthor[$j]])) {
1673
                            $authorArray[$tempArrayAuthor[$j]] = api_get_user_info($tempArrayAuthor[$j]);
1674
                        }
1675
                        $dataSet[$tempArrayAuthor[$j]][$lpItemId] = [
1676
                            'course' => $byCourse,
1677
                            'courseGroups' => $byCourseGroups,
1678
                            'session' => $bySession,
1679
                            'lp_item' => $lpItem,
1680
                            'course_user' => $byUserInCourse,
1681
                        ];
1682
                    }
1683
                } else {
1684
                    if (!isset($authorArray[$author])) {
1685
                        $authorArray[$author] = api_get_user_info($author);
1686
                    }
1687
                    $dataSet[$author][$lpItemId] = [
1688
                        'course' => $byCourse,
1689
                        'courseGroups' => $byCourseGroups,
1690
                        'session' => $bySession,
1691
                        'lp_item' => $lpItem,
1692
                        'course_user' => $byUserInCourse,
1693
                    ];
1694
                }
1695
            }
1696
        }
1697
        if ($csv == false) {
1698
            if (empty($tableHtml)) {
1699
                $table .= "<div class='table-responsive'>
1700
                    <table class='table table-hover table-striped table-bordered data_table'>
1701
                    <thead>
1702
                    <tr>
1703
                    <th class='th-header'>".get_lang('Author')."</th>
1704
                    <th class='th-header'>".get_lang('ContentList')."</th>
1705
                    <th class='th-header'>".get_lang('Tariff')."</th>
1706
                    <th class='th-header'>".get_lang('CountOfSubscribedUsers')."</th>
1707
                    <th class='th-header'>".get_lang('ToInvoice')."</th>
1708
                    <th class='th-header'>".get_lang('StudentList')."</th>
1709
                    </tr>
1710
                    </thead>
1711
                    <tbody>";
1712
                //Icon Constant
1713
                $iconAdd = Display::return_icon('add.png', get_lang('ShowOrHide'), '', ICON_SIZE_SMALL);
1714
                $iconRemove = Display::return_icon('error.png', get_lang('ShowOrHide'), '', ICON_SIZE_SMALL);
1715
1716
                $lastAuthor = '';
1717
                $total = 0;
1718
                foreach ($dataSet as $authorId => $lpItems) {
1719
                    $authorTemp = $authorArray[$authorId];
1720
                    $totalSudent = 0;
1721
                    foreach ($lpItems as $lpItem) {
1722
                        $totalStudents = 0;
1723
                        $itemLp = $lpItem['lp_item'];
1724
                        $title = $itemLp['title'];
1725
                        $price = $itemLp['price'];
1726
                        $byCourse = $lpItem['course'];
1727
                        $byCourseGroups = $lpItem['courseGroups'];
1728
                        $bySession = $lpItem['session'];
1729
                        $byUserInCourse = $lpItem['course_user'];
1730
                        $hide = "class='author_$authorId hidden' ";
1731
                        $tableTemp = '';
1732
                        if ($lastAuthor != $authorTemp) {
1733
                            $table .= "<tr><td>".$authorTemp['complete_name']."</td>";
1734
                        } else {
1735
                            $table .= "<tr $hide ><td></td>";
1736
                        }
1737
                        $table .= "<td>$title</td><td>$price</td>";
1738
                        $studentRegister = count($byCourse);
1739
                        $studentGroupsRegister = count($byCourseGroups);
1740
                        $studentRegisterBySession = count($bySession);
1741
                        $usersInCourseCount = count($byUserInCourse);
1742
1743
                        $hiddenField = 'student_show_'.$index;
1744
                        $hiddenFieldLink = 'student_show_'.$index.'_';
1745
                        if (0 != $studentRegister ||
1746
                            0 != $studentRegisterBySession ||
1747
                            0 != $studentGroupsRegister ||
1748
                            0 != $usersInCourseCount
1749
                        ) {
1750
                            $tableTemp .= "<td>
1751
                                <a href='#!' id='$hiddenFieldLink' onclick='showHideStudent(\"$hiddenField\")'>
1752
                                <div class='icon_add'>$iconAdd</div>
1753
                                <div class='icon_remove hidden'>$iconRemove</div>
1754
                                </a>
1755
                                <div id='$hiddenField' class='hidden'>";
1756
                            $studentProcessed = [];
1757
                            /* Student by course*/
1758
                            $studentProcessed = self::getStudentDataToReportByLpItem($byCourse, $studentProcessed);
1759
                            /* Student by Class*/
1760
                            $studentProcessed = self::getStudentDataToReportByLpItem($byCourseGroups, $studentProcessed, 'class');
1761
                            /* Student by sessions*/
1762
                            $studentProcessed = self::getStudentDataToReportByLpItem($bySession, $studentProcessed);
1763
                            // Students in course*/
1764
                            $studentProcessed = self::getStudentDataToReportByLpItem($byUserInCourse, $studentProcessed);
1765
                            $index++;
1766
                            foreach ($studentProcessed as $lpItemId => $item) {
1767
                                foreach ($item as $type => $student) {
1768
                                    foreach ($student as $userId => $text) {
1769
                                        if ('LearnpathSubscription' == $type) {
1770
                                            $tableTemp .= $text;
1771
                                            $totalStudents++;
1772
                                        } else {
1773
                                            if (!isset($studentProcessed[$lpItemId]['LearnpathSubscription'])) {
1774
                                                $tableTemp .= $text;
1775
                                                $totalStudents++;
1776
                                            }
1777
                                        }
1778
                                    }
1779
                                }
1780
                            }
1781
                            $tableTemp .= "</div></td>";
1782
                        } else {
1783
                            $tableTemp .= "<td></td>";
1784
                        }
1785
                        $table .= "<td>$totalStudents</td>";
1786
                        $invoicing = ($totalStudents * $price);
1787
                        $table .= "<td>$invoicing</td>";
1788
                        $total += $invoicing;
1789
                        $totalSudent += $totalStudents;
1790
                        $table .= $tableTemp."</tr>";
1791
                        $lastAuthor = $authorTemp;
1792
                    }
1793
                    $hiddenFieldLink = 'student__show_'.$index.'_';
1794
                    $index++;
1795
                    $table .= "<tr>
1796
                    <th class='th-header'></th>
1797
                    <th class='th-header'>
1798
                            <a href='#!' id='$hiddenFieldLink' onclick='ShowMoreAuthor(\"$authorId\")'>
1799
                                <div class='icon_add_author_$authorId'>$iconAdd</div>
1800
                                <div class='icon_remove_author_$authorId hidden'>$iconRemove</div>
1801
                            </a>
1802
                        </th>
1803
                    <th class='th-header'></th>
1804
                    <th class='th-header'>$totalSudent</th>
1805
                    <th class='th-header'>$total</th>
1806
                    <th class='th-header'></tr>";
1807
                    $total = 0;
1808
                }
1809
                $table .= "</tbody></table></div>";
1810
                $tableHtml = $table;
1811
            }
1812
1813
            $form = new FormValidator('searchDate', 'get');
1814
            $form->addHidden('display', 'learningPathByItem');
1815
            $today = new DateTime();
1816
            if (empty($startDate)) {
1817
                $startDate = $today->modify('first day of this month')->format('Y-m-d');
1818
            }
1819
            if (empty($endDate)) {
1820
                $endDate = $today->modify('last day of this month')->format('Y-m-d');
1821
            }
1822
            $form->addDatePicker(
1823
                'startDate',
1824
                get_lang('DateStart'),
1825
                [
1826
                    'value' => $startDate,
1827
                ]
1828
            );
1829
            $form->addDatePicker(
1830
                'endDate',
1831
                get_lang('DateEnd'),
1832
                [
1833
                    'value' => $endDate,
1834
                ]
1835
            );
1836
            $form->addButtonSearch(get_lang('Search'));
1837
1838
            if (count($dataSet) != 0) {
1839
                $form->addButton(
1840
                    'export_csv',
1841
                    get_lang('ExportAsCSV'),
1842
                    'check',
1843
                    'primary',
1844
                    null,
1845
                    null,
1846
                    [
1847
                    ]
1848
                );
1849
            }
1850
            $tableContent = $form->returnForm();
1851
            $tableContent .= $tableHtml;
1852
            $tpl = new Template('', false, false, false, false, false, false);
1853
            $tpl->assign('table', $tableContent);
1854
            $templateName = $tpl->get_template('my_space/course_summary.tpl');
1855
            $tpl->display($templateName);
1856
        } else {
1857
            $csv_content = [];
1858
            $csv_row = [];
1859
            $csv_row[] = get_lang('Author');
1860
            $csv_row[] = get_lang('ContentList');
1861
            $csv_row[] = get_lang('Tariff');
1862
            $csv_row[] = get_lang('CountOfSubscribedUsers');
1863
            $csv_row[] = get_lang('ToInvoice');
1864
            $csv_row[] = get_lang('StudentList');
1865
            $csv_content[] = $csv_row;
1866
            $total = 0;
1867
            foreach ($dataSet as $authorId => $lpItems) {
1868
                $authorTemp = $authorArray[$authorId];
1869
                $totalSudent = 0;
1870
                foreach ($lpItems as $lpItem) {
1871
                    $totalStudents = 0;
1872
                    $itemLp = $lpItem['lp_item'];
1873
                    $itemLpId = $itemLp['lp_item_id'];
1874
                    $title = $itemLp['title'];
1875
                    $price = $itemLp['price'];
1876
                    $byCourse = $lpItem['course'];
1877
                    $bySession = $lpItem['session'];
1878
                    $byCourseGroups = $lpItem['courseGroups'];
1879
                    $byUserInCourse = $lpItem['course_user'];
1880
1881
                    $csv_row = [];
1882
                    $csv_row[] = $authorTemp['complete_name'];
1883
                    $csv_row[] = $title;
1884
                    $csv_row[] = $price;
1885
1886
                    $studentRegister = count($byCourse);
1887
                    $studentRegisterBySession = count($bySession);
1888
                    $studentGroupsRegister = count($byCourseGroups);
1889
                    $usersInCourseCount = count($byUserInCourse);
1890
1891
                    $studentsName = '';
1892
                    if (0 != $studentRegister ||
1893
                        0 != $studentRegisterBySession ||
1894
                        0 != $studentGroupsRegister ||
1895
                        0 != $usersInCourseCount
1896
                    ) {
1897
                        $studentProcessed = [];
1898
                        /* Student by course*/
1899
                        $studentProcessed = self::getStudentDataToReportByLpItem($byCourse, $studentProcessed, '', true);
1900
                        /* Student by Class*/
1901
                        $studentProcessed = self::getStudentDataToReportByLpItem($byCourseGroups, $studentProcessed, 'class', true);
1902
                        /* Student by sessions*/
1903
                        $studentProcessed = self::getStudentDataToReportByLpItem($bySession, $studentProcessed, '', true);
1904
                        // Students in course*/
1905
                        $studentProcessed = self::getStudentDataToReportByLpItem($byUserInCourse, $studentProcessed, '', true);
1906
1907
                        $index++;
1908
                        foreach ($studentProcessed as $lpItemId => $item) {
1909
                            foreach ($item as $type => $student) {
1910
                                foreach ($student as $userId => $text) {
1911
                                    if ('LearnpathSubscription' == $type) {
1912
                                        $studentsName .= $text;
1913
                                        $totalStudents++;
1914
                                    } else {
1915
                                        if (!isset($studentProcessed[$lpItemId]['LearnpathSubscription'])) {
1916
                                            $studentsName .= $text;
1917
                                            $totalStudents++;
1918
                                        }
1919
                                    }
1920
                                }
1921
                            }
1922
                        }
1923
                    }
1924
                    $csv_row[] = $totalStudents;
1925
                    $csv_row[] = $price * $totalStudents;
1926
                    $csv_row[] = trim($studentsName, " / ");
1927
                    $csv_content[] = $csv_row;
1928
                }
1929
            }
1930
            Export::arrayToCsv($csv_content, 'reporting_lp_by_authors');
1931
        }
1932
    }
1933
1934
    public static function getSessionAddUserCourseFromTrackDefault(
1935
        $startDate = null,
1936
        $endDate = null,
1937
        $whereInLp = null
1938
    ) {
1939
        $whereInLp = Database::escape_string($whereInLp);
1940
        $data = [];
1941
        $tblTrackDefault = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
1942
        $tblSessionRelCourseUser = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
1943
        $tblLp = Database::get_course_table(TABLE_LP_MAIN);
1944
        $tblLpItem = Database::get_course_table(TABLE_LP_ITEM);
1945
        $tblUser = Database::get_main_table(TABLE_MAIN_USER);
1946
        $tblAccessUrlUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
1947
        $accessUrlFilter = '';
1948
        if (api_is_multiple_url_enabled()) {
1949
            $urlId = api_get_current_access_url_id();
1950
            $accessUrlFilter = " INNER JOIN $tblAccessUrlUser auru
1951
                      ON (u.id = auru.user_id AND auru.access_url_id = $urlId)";
1952
        }
1953
1954
        if (!empty($startDate)) {
1955
            $startDate = new DateTime($startDate);
1956
        } else {
1957
            $startDate = new DateTime();
1958
        }
1959
        if (!empty($endDate)) {
1960
            $endDate = new DateTime($endDate);
1961
        } else {
1962
            $endDate = new DateTime();
1963
        }
1964
        if (!empty($startDate) and !empty($endDate)) {
1965
            if ($startDate > $endDate) {
1966
                $dateTemp = $endDate;
1967
                $endDate = $startDate;
1968
                $startDate = $dateTemp;
1969
                unset($dateTemp);
1970
            }
1971
        }
1972
        $startDate = api_get_utc_datetime($startDate->setTime(0, 0, 0)->format('Y-m-d H:i:s'));
1973
        $endDate = api_get_utc_datetime($endDate->setTime(0, 0, 0)->format('Y-m-d H:i:s'));
1974
        $extra = '';
1975
        if (!empty($whereInLp)) {
1976
            $extra = " AND lpi.lp_id in ($whereInLp) ";
1977
        }
1978
1979
        $sql = "SELECT DISTINCT
1980
            lp.iid AS lp,
1981
            lpi.iid AS lp_item,
1982
            lpi.iid AS lp_item_id,
1983
            td.default_value AS id,
1984
            srcu.session_id AS session_id,
1985
            u.username AS username,
1986
            td.default_date AS default_date,
1987
            td.default_event_type AS type,
1988
            u.firstname as firstname,
1989
            u.lastname as lastname
1990
        FROM $tblTrackDefault AS td
1991
        INNER JOIN $tblSessionRelCourseUser AS srcu
1992
        ON (td.default_value = srcu.user_id AND td.c_id = srcu.c_id)
1993
        INNER JOIN $tblLp AS lp
1994
        ON (lp.c_id = srcu.c_id)
1995
        INNER JOIN $tblLpItem AS lpi
1996
        ON (
1997
            lpi.c_id = srcu.c_id AND
1998
            lp.id = lpi.lp_id AND
1999
            lpi.c_id = lp.c_id
2000
        )
2001
        INNER JOIN $tblUser AS u
2002
        ON (u.id = srcu.user_id)
2003
        $accessUrlFilter
2004
        WHERE
2005
            td.default_event_type = 'session_add_user_course' AND
2006
            td.default_date >= '$startDate' AND
2007
            td.default_date <= '$endDate'
2008
            $extra
2009
        ORDER BY td.default_value ";
2010
        $queryResult = Database::query($sql);
2011
        $dataTrack = Database::store_result($queryResult, 'ASSOC');
2012
        foreach ($dataTrack as $item) {
2013
            $item['complete_name'] = api_get_person_name($item['firstname'], $item['lastname']);
2014
            $item['company'] = self::getCompanyOfUser($item['id']);
2015
            $data[$item['lp_item_id']][] = $item;
2016
        }
2017
2018
        return $data;
2019
    }
2020
2021
    public static function getUserSubscribedInCourseByDateAndLp(
2022
        $startDate = null,
2023
        $endDate = null,
2024
        $whereInLp = null
2025
    ): array {
2026
        $whereInLp = Database::escape_string($whereInLp);
2027
        $tblTrackDefault = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
2028
        $tblCourseRelUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
2029
        $tblLp = Database::get_course_table(TABLE_LP_MAIN);
2030
        $tblLpItem = Database::get_course_table(TABLE_LP_ITEM);
2031
        $tblUser = Database::get_main_table(TABLE_MAIN_USER);
2032
        $tblAccessUrlUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
2033
        $accessUrlFilter = '';
2034
2035
        if (api_is_multiple_url_enabled()) {
2036
            $urlId = api_get_current_access_url_id();
2037
            $accessUrlFilter = " INNER JOIN $tblAccessUrlUser auru
2038
                ON (u.id = auru.user_id AND auru.access_url_id = $urlId)";
2039
        }
2040
2041
        $startDate = !empty($startDate) ? new DateTime($startDate) : new DateTime();
2042
        $endDate = !empty($endDate) ? new DateTime($endDate) : new DateTime();
2043
2044
        $startDate = api_get_utc_datetime($startDate->setTime(0, 0)->format('Y-m-d H:i:s'));
2045
        $endDate = api_get_utc_datetime($endDate->setTime(0, 0)->format('Y-m-d H:i:s'));
2046
2047
        $extra = '';
2048
2049
        if (!empty($whereInLp)) {
2050
            $extra = " AND lpi.lp_id in ($whereInLp) ";
2051
        }
2052
2053
        $sql = "SELECT DISTINCT
2054
                lp.iid AS lp,
2055
                lpi.iid AS lp_item,
2056
                lpi.iid AS lp_item_id,
2057
                u.id AS id,
2058
                u.username AS username,
2059
                td.default_date AS default_date,
2060
                td.default_event_type AS type,
2061
                u.firstname as firstname,
2062
                u.lastname as lastname
2063
            FROM $tblTrackDefault AS td
2064
            INNER JOIN $tblCourseRelUser AS cru ON td.c_id = cru.c_id
2065
            INNER JOIN $tblLp AS lp ON lp.c_id = cru.c_id
2066
            INNER JOIN $tblLpItem AS lpi
2067
                ON (lpi.c_id = cru.c_id AND lp.id = lpi.lp_id AND lpi.c_id = lp.c_id)
2068
            INNER JOIN $tblUser AS u ON u.id = cru.user_id
2069
            $accessUrlFilter
2070
            WHERE
2071
                td.default_event_type = '".LOG_SUBSCRIBE_USER_TO_COURSE."'
2072
                AND td.default_date >= '$startDate'
2073
                AND td.default_date <= '$endDate'
2074
                AND td.default_value LIKE CONCAT('%s:2:\\\\\\\\\\\"id\\\\\\\\\";i:', cru.user_id, ';%')
2075
                $extra
2076
            ORDER BY u.id";
2077
2078
        $result = Database::query($sql);
2079
2080
        $data = [];
2081
2082
        while ($item = Database::fetch_assoc($result)) {
2083
            $item['complete_name'] = api_get_person_name($item['firstname'], $item['lastname']);
2084
            $item['company'] = self::getCompanyOfUser($item['id']);
2085
2086
            $data[$item['lp_item_id']][] = $item;
2087
        }
2088
2089
        return $data;
2090
    }
2091
2092
    /**
2093
     * Display a sortable table that contains an overview of all the reporting progress of all courses.
2094
     */
2095
    public static function display_tracking_course_overview()
2096
    {
2097
        $params = ['view' => 'admin', 'display' => 'courseoverview'];
2098
        $table = new SortableTable(
2099
            'tracking_session_overview',
2100
            ['MySpace', 'get_total_number_courses'],
2101
            ['MySpace', 'get_course_data_tracking_overview'],
2102
            1,
2103
            20,
2104
            'ASC',
2105
            null, [
2106
                'class' => 'table table-transparent',
2107
            ]
2108
        );
2109
        $table->additional_parameters = $params;
2110
        $table->set_column_filter(0, ['MySpace', 'course_tracking_filter']);
2111
        $tableContent = $table->return_table();
2112
2113
        $tpl = new Template('', false, false, false, false, false, false);
2114
        $tpl->assign('table', $tableContent);
2115
        $templateName = $tpl->get_template('my_space/course_summary.tpl');
2116
        $tpl->display($templateName);
2117
    }
2118
2119
    /**
2120
     * Get the total number of courses.
2121
     *
2122
     * @return int Total number of courses
2123
     */
2124
    public static function get_total_number_courses()
2125
    {
2126
        return CourseManager::count_courses(api_get_current_access_url_id());
2127
    }
2128
2129
    /**
2130
     * Get data for the courses.
2131
     *
2132
     * @param int    $from        Inferior limit
2133
     * @param int    $numberItems Number of items to select
2134
     * @param string $column      Column to order on
2135
     * @param string $direction   Order direction
2136
     *
2137
     * @return array Results
2138
     */
2139
    public static function get_course_data_tracking_overview(
2140
        $from,
2141
        $numberItems,
2142
        $column,
2143
        $direction
2144
    ) {
2145
        switch ($column) {
2146
            default:
2147
            case 1:
2148
                $column = 'title';
2149
                break;
2150
        }
2151
2152
        $courses = CourseManager::get_courses_list(
2153
            $from,
2154
            $numberItems,
2155
            $column,
2156
            $direction,
2157
             -1,
2158
            '',
2159
            api_get_current_access_url_id()
2160
        );
2161
2162
        $list = [];
2163
        foreach ($courses as $course) {
2164
            $list[] = [
2165
                '0' => $course['code'],
2166
                'col0' => $course['code'],
2167
            ];
2168
        }
2169
2170
        return $list;
2171
    }
2172
2173
    /**
2174
     * Fills in course reporting data.
2175
     *
2176
     * @param int course code
2177
     * @param array $url_params additional url parameters
2178
     * @param array $row        the row information (the other columns)
2179
     *
2180
     * @return string html code
2181
     */
2182
    public static function course_tracking_filter($course_code, $url_params, $row)
2183
    {
2184
        $course_code = $row[0];
2185
        $courseInfo = api_get_course_info($course_code);
2186
        $courseId = $courseInfo['real_id'];
2187
2188
        $tpl = new Template('', false, false, false, false, false, false);
2189
        $data = null;
2190
2191
        // database table definition
2192
        $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
2193
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
2194
2195
        // getting all the courses of the user
2196
        $sql = "SELECT *
2197
                FROM $tbl_user AS u
2198
                INNER JOIN $tbl_course_rel_user AS cu
2199
                ON cu.user_id = u.user_id
2200
                WHERE cu.c_id = '".$courseId."'";
2201
        $result = Database::query($sql);
2202
        $time_spent = 0;
2203
        $progress = 0;
2204
        $nb_progress_lp = 0;
2205
        $score = 0;
2206
        $nb_score_lp = 0;
2207
        $nb_messages = 0;
2208
        $nb_assignments = 0;
2209
        $last_login_date = false;
2210
        $total_score_obtained = 0;
2211
        $total_score_possible = 0;
2212
        $total_questions_answered = 0;
2213
        while ($row = Database::fetch_object($result)) {
2214
            // get time spent in the course and session
2215
            $time_spent += Tracking::get_time_spent_on_the_course(
2216
                $row->user_id,
2217
                $courseInfo['real_id']
2218
            );
2219
            $progress_tmp = Tracking::get_avg_student_progress(
2220
                $row->user_id,
2221
                $course_code,
2222
                [],
2223
                null,
2224
                true
2225
            );
2226
            if ($progress_tmp) {
2227
                $progress += $progress_tmp[0];
2228
                $nb_progress_lp += $progress_tmp[1];
2229
            }
2230
            $score_tmp = Tracking::get_avg_student_score(
2231
                $row->user_id,
2232
                $course_code,
2233
                [],
2234
                null,
2235
                true
2236
            );
2237
            if (is_array($score_tmp)) {
2238
                $score += $score_tmp[0];
2239
                $nb_score_lp += $score_tmp[1];
2240
            }
2241
            $nb_messages += Tracking::count_student_messages(
2242
                $row->user_id,
2243
                $course_code
2244
            );
2245
            $nb_assignments += Tracking::count_student_assignments(
2246
                $row->user_id,
2247
                $course_code
2248
            );
2249
            $last_login_date_tmp = Tracking::get_last_connection_date_on_the_course(
2250
                $row->user_id,
2251
                $courseInfo,
2252
                null,
2253
                false
2254
            );
2255
            if ($last_login_date_tmp != false &&
2256
                $last_login_date == false
2257
            ) { // TODO: To be cleaned
2258
                $last_login_date = $last_login_date_tmp;
2259
            } elseif ($last_login_date_tmp != false && $last_login_date != false) {
2260
                // TODO: Repeated previous condition. To be cleaned.
2261
                // Find the max and assign it to first_login_date
2262
                if (strtotime($last_login_date_tmp) > strtotime($last_login_date)) {
2263
                    $last_login_date = $last_login_date_tmp;
2264
                }
2265
            }
2266
2267
            $exercise_results_tmp = self::exercises_results($row->user_id, $course_code);
2268
            $total_score_obtained += $exercise_results_tmp['score_obtained'];
2269
            $total_score_possible += $exercise_results_tmp['score_possible'];
2270
            $total_questions_answered += $exercise_results_tmp['questions_answered'];
2271
        }
2272
        if ($nb_progress_lp > 0) {
2273
            $avg_progress = round($progress / $nb_progress_lp, 2);
2274
        } else {
2275
            $avg_progress = 0;
2276
        }
2277
        if ($nb_score_lp > 0) {
2278
            $avg_score = round($score / $nb_score_lp, 2);
2279
        } else {
2280
            $avg_score = '-';
2281
        }
2282
        if ($last_login_date) {
2283
            $last_login_date = api_convert_and_format_date(
2284
                $last_login_date,
2285
                DATE_FORMAT_SHORT,
2286
                date_default_timezone_get()
2287
            );
2288
        } else {
2289
            $last_login_date = '-';
2290
        }
2291
        if ($total_score_possible > 0) {
2292
            $total_score_percentage = round($total_score_obtained / $total_score_possible * 100, 2);
2293
        } else {
2294
            $total_score_percentage = 0;
2295
        }
2296
        if ($total_score_percentage > 0) {
2297
            $total_score = $total_score_obtained.'/'.$total_score_possible.' ('.$total_score_percentage.' %)';
2298
        } else {
2299
            $total_score = '-';
2300
        }
2301
2302
        $data = [
2303
            'course_code' => $course_code,
2304
            'id' => $courseId,
2305
            'image' => $courseInfo['course_image_large'],
2306
            'image_small' => $courseInfo['course_image'],
2307
            'title' => $courseInfo['title'],
2308
            'url' => $courseInfo['course_public_url'],
2309
            'category' => $courseInfo['categoryName'],
2310
            'time_spent' => api_time_to_hms($time_spent),
2311
            'avg_progress' => $avg_progress,
2312
            'avg_score' => $avg_score,
2313
            'number_message' => $nb_messages,
2314
            'number_assignments' => $nb_assignments,
2315
            'total_score' => $total_score,
2316
            'questions_answered' => $total_questions_answered,
2317
            'last_login' => $last_login_date,
2318
        ];
2319
2320
        $tpl->assign('data', $data);
2321
        $layout = $tpl->get_template('my_space/partials/tracking_course_overview.tpl');
2322
        $content = $tpl->fetch($layout);
2323
2324
        return $content;
2325
    }
2326
2327
    /**
2328
     * This function exports the table that we see in display_tracking_course_overview().
2329
     */
2330
    public static function export_tracking_course_overview()
2331
    {
2332
        // database table definition
2333
        $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
2334
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
2335
2336
        // the values of the sortable table
2337
        if ($_GET['tracking_course_overview_page_nr']) {
2338
            $from = $_GET['tracking_course_overview_page_nr'];
2339
        } else {
2340
            $from = 0;
2341
        }
2342
        if ($_GET['tracking_course_overview_column']) {
2343
            $orderby = $_GET['tracking_course_overview_column'];
2344
        } else {
2345
            $orderby = 0;
2346
        }
2347
2348
        if ($_GET['tracking_course_overview_direction']) {
2349
            $direction = $_GET['tracking_course_overview_direction'];
2350
        } else {
2351
            $direction = 'ASC';
2352
        }
2353
2354
        $course_data = self::get_course_data_tracking_overview(
2355
            $from,
2356
            1000,
2357
            $orderby,
2358
            $direction
2359
        );
2360
2361
        $csv_content = [];
2362
2363
        // the first line of the csv file with the column headers
2364
        $csv_row = [];
2365
        $csv_row[] = get_lang('Course');
2366
        $csv_row[] = get_lang('AvgTimeSpentInTheCourse');
2367
        $csv_row[] = get_lang('AvgStudentsProgress');
2368
        $csv_row[] = get_lang('AvgCourseScore');
2369
        $csv_row[] = get_lang('TotalNumberOfMessages');
2370
        $csv_row[] = get_lang('TotalNumberOfAssignments');
2371
        $csv_row[] = get_lang('TotalExercisesScoreObtained');
2372
        $csv_row[] = get_lang('TotalExercisesScorePossible');
2373
        $csv_row[] = get_lang('TotalExercisesAnswered');
2374
        $csv_row[] = get_lang('TotalExercisesScorePercentage');
2375
        $csv_row[] = get_lang('LatestLogin');
2376
        $csv_content[] = $csv_row;
2377
2378
        // the other lines (the data)
2379
        foreach ($course_data as $key => $course) {
2380
            $course_code = $course[0];
2381
            $courseInfo = api_get_course_info($course_code);
2382
            $course_title = $courseInfo['title'];
2383
            $courseId = $courseInfo['real_id'];
2384
2385
            $csv_row = [];
2386
            $csv_row[] = $course_title;
2387
2388
            // getting all the courses of the session
2389
            $sql = "SELECT *
2390
                    FROM $tbl_user AS u
2391
                    INNER JOIN $tbl_course_rel_user AS cu
2392
                    ON cu.user_id = u.user_id
2393
                    WHERE cu.c_id = '".$courseId."'";
2394
            $result = Database::query($sql);
2395
            $time_spent = 0;
2396
            $progress = 0;
2397
            $nb_progress_lp = 0;
2398
            $score = 0;
2399
            $nb_score_lp = 0;
2400
            $nb_messages = 0;
2401
            $nb_assignments = 0;
2402
            $last_login_date = false;
2403
            $total_score_obtained = 0;
2404
            $total_score_possible = 0;
2405
            $total_questions_answered = 0;
2406
            while ($row = Database::fetch_object($result)) {
2407
                // get time spent in the course and session
2408
                $time_spent += Tracking::get_time_spent_on_the_course(
2409
                    $row->user_id,
2410
                    $courseId
2411
                );
2412
                $progress_tmp = Tracking::get_avg_student_progress(
2413
                    $row->user_id,
2414
                    $course_code,
2415
                    [],
2416
                    null,
2417
                    true
2418
                );
2419
                $progress += $progress_tmp[0];
2420
                $nb_progress_lp += $progress_tmp[1];
2421
                $score_tmp = Tracking::get_avg_student_score(
2422
                    $row->user_id,
2423
                    $course_code,
2424
                    [],
2425
                    null,
2426
                    true
2427
                );
2428
                if (is_array($score_tmp)) {
2429
                    $score += $score_tmp[0];
2430
                    $nb_score_lp += $score_tmp[1];
2431
                }
2432
                $nb_messages += Tracking::count_student_messages(
2433
                    $row->user_id,
2434
                    $course_code
2435
                );
2436
                $nb_assignments += Tracking::count_student_assignments(
2437
                    $row->user_id,
2438
                    $course_code
2439
                );
2440
2441
                $last_login_date_tmp = Tracking::get_last_connection_date_on_the_course(
2442
                    $row->user_id,
2443
                    $courseInfo,
2444
                    null,
2445
                    false
2446
                );
2447
                if ($last_login_date_tmp != false && $last_login_date == false) {
2448
                    // TODO: To be cleaned.
2449
                    $last_login_date = $last_login_date_tmp;
2450
                } elseif ($last_login_date_tmp != false && $last_login_date == false) {
2451
                    // TODO: Repeated previous condition. To be cleaned.
2452
                    // Find the max and assign it to first_login_date
2453
                    if (strtotime($last_login_date_tmp) > strtotime($last_login_date)) {
2454
                        $last_login_date = $last_login_date_tmp;
2455
                    }
2456
                }
2457
2458
                $exercise_results_tmp = self::exercises_results($row->user_id, $course_code);
2459
                $total_score_obtained += $exercise_results_tmp['score_obtained'];
2460
                $total_score_possible += $exercise_results_tmp['score_possible'];
2461
                $total_questions_answered += $exercise_results_tmp['questions_answered'];
2462
            }
2463
            if ($nb_progress_lp > 0) {
2464
                $avg_progress = round($progress / $nb_progress_lp, 2);
2465
            } else {
2466
                $avg_progress = 0;
2467
            }
2468
            if ($nb_score_lp > 0) {
2469
                $avg_score = round($score / $nb_score_lp, 2);
2470
            } else {
2471
                $avg_score = '-';
2472
            }
2473
            if ($last_login_date) {
2474
                $last_login_date = api_convert_and_format_date(
2475
                    $last_login_date,
2476
                    DATE_FORMAT_SHORT,
2477
                    date_default_timezone_get()
2478
                );
2479
            } else {
2480
                $last_login_date = '-';
2481
            }
2482
            if ($total_score_possible > 0) {
2483
                $total_score_percentage = round($total_score_obtained / $total_score_possible * 100, 2);
2484
            } else {
2485
                $total_score_percentage = 0;
2486
            }
2487
            // time spent in the course
2488
            $csv_row[] = api_time_to_hms($time_spent);
2489
            // student progress in course
2490
            $csv_row[] = $avg_progress;
2491
            // student score
2492
            $csv_row[] = $avg_score;
2493
            // student messages
2494
            $csv_row[] = $nb_messages;
2495
            // student assignments
2496
            $csv_row[] = $nb_assignments;
2497
            // student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
2498
            $csv_row[] = $total_score_obtained;
2499
            $csv_row[] = $total_score_possible;
2500
            $csv_row[] = $total_questions_answered;
2501
            $csv_row[] = $total_score_percentage;
2502
            // last connection
2503
            $csv_row[] = $last_login_date;
2504
            $csv_content[] = $csv_row;
2505
        }
2506
        Export::arrayToCsv($csv_content, 'reporting_course_overview');
2507
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
2508
    }
2509
2510
    /**
2511
     * Display a sortable table that contains an overview of all the reporting
2512
     * progress of all sessions and all courses the user is subscribed to.
2513
     *
2514
     * @author Guillaume Viguier <[email protected]>
2515
     */
2516
    public static function display_tracking_session_overview()
2517
    {
2518
        $head = '<table style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
2519
        $head .= '<tr>';
2520
        $head .= '<th width="155px" style="border-left:0;border-bottom:0"><span>'.get_lang('Course').'</span></th>';
2521
        $head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgTimeSpentInTheCourse'), 6, true).'</span></th>';
2522
        $head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgStudentsProgress'), 6, true).'</span></th>';
2523
        $head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('AvgCourseScore'), 6, true).'</span></th>';
2524
        $head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfMessages'), 6, true).'</span></th>';
2525
        $head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalNumberOfAssignments'), 6, true).'</span></th>';
2526
        $head .= '<th width="105px" style="border-bottom:0"><span>'.get_lang('TotalExercisesScoreObtained').'</span></th>';
2527
        $head .= '<th style="padding:0;border-bottom:0"><span>'.cut(get_lang('TotalExercisesAnswered'), 6, true).'</span></th>';
2528
        $head .= '<th style="padding:0;border-bottom:0;border-right:0;"><span>'.get_lang('LatestLogin').'</span></th>';
2529
        $head .= '</tr></table>';
2530
2531
        $params = ['view' => 'admin', 'display' => 'sessionoverview'];
2532
        $table = new SortableTable(
2533
            'tracking_session_overview',
2534
            ['MySpace', 'get_total_number_sessions'],
2535
            ['MySpace', 'get_session_data_tracking_overview'],
2536
            1
2537
        );
2538
        $table->additional_parameters = $params;
2539
2540
        $table->set_header(0, '', false, null, ['style' => 'display: none']);
2541
        $table->set_header(
2542
            1,
2543
            get_lang('Session'),
2544
            true,
2545
            ['style' => 'font-size:8pt'],
2546
            ['style' => 'font-size:8pt']
2547
        );
2548
        $table->set_header(
2549
            2,
2550
            $head,
2551
            false,
2552
            ['style' => 'width:90%;border:0;padding:0;font-size:7.5pt;'],
2553
            ['style' => 'width:90%;padding:0;font-size:7.5pt;']
2554
        );
2555
        $table->set_column_filter(2, ['MySpace', 'session_tracking_filter']);
2556
        $table->display();
2557
    }
2558
2559
    /**
2560
     * Get the total number of sessions.
2561
     *
2562
     * @return int Total number of sessions
2563
     */
2564
    public static function get_total_number_sessions()
2565
    {
2566
        return SessionManager::count_sessions(api_get_current_access_url_id());
2567
    }
2568
2569
    /**
2570
     * Get data for the sessions.
2571
     *
2572
     * @param int    $from        Inferior limit
2573
     * @param int    $numberItems Number of items to select
2574
     * @param string $column      Column to order on
2575
     * @param string $direction   Order direction
2576
     *
2577
     * @return array Results
2578
     */
2579
    public static function get_session_data_tracking_overview(
2580
        $from,
2581
        $numberItems,
2582
        $column,
2583
        $direction
2584
    ) {
2585
        $from = (int) $from;
2586
        $numberItems = (int) $numberItems;
2587
        $direction = Database::escape_string($direction);
2588
        $columnName = 'name';
2589
        if ($column === 1) {
2590
            $columnName = 'id';
2591
        }
2592
2593
        $options = [
2594
            'order' => " $columnName $direction",
2595
            'limit' => " $from,$numberItems",
2596
        ];
2597
        $sessions = SessionManager::get_sessions_admin($options);
2598
        $list = [];
2599
        foreach ($sessions as $session) {
2600
            $list[] = [
2601
                '0' => $session['id'],
2602
                'col0' => $session['id'],
2603
                '1' => strip_tags($session['name']),
2604
                'col1' => strip_tags($session['name']),
2605
            ];
2606
        }
2607
2608
        return $list;
2609
    }
2610
2611
    /**
2612
     * Fills in session reporting data.
2613
     *
2614
     * @param int   $session_id the id of the user
2615
     * @param array $url_params additonal url parameters
2616
     * @param array $row        the row information (the other columns)
2617
     *
2618
     * @return string html code
2619
     */
2620
    public static function session_tracking_filter($session_id, $url_params, $row)
2621
    {
2622
        $session_id = $row[0];
2623
        // the table header
2624
        $return = '<table class="table table-hover table-striped data_table" style="width: 100%;border:0;padding:0;border-collapse:collapse;table-layout: fixed">';
2625
2626
        // database table definition
2627
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
2628
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
2629
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2630
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
2631
2632
        // getting all the courses of the user
2633
        $sql = "SELECT * FROM $tbl_course AS c
2634
                INNER JOIN $tbl_session_rel_course AS sc
2635
                ON sc.c_id = c.id
2636
                WHERE sc.session_id = '".$session_id."'";
2637
        $result = Database::query($sql);
2638
        while ($row = Database::fetch_object($result)) {
2639
            $courseId = $row->c_id;
2640
            $courseInfo = api_get_course_info_by_id($courseId);
2641
            $return .= '<tr>';
2642
            // course code
2643
            $return .= '    <td width="157px" >'.$row->title.'</td>';
2644
            // get the users in the course
2645
            $sql = "SELECT u.user_id
2646
                    FROM $tbl_user AS u
2647
                    INNER JOIN $tbl_session_rel_course_rel_user AS scu
2648
                    ON u.user_id = scu.user_id
2649
                    WHERE scu.session_id = '".$session_id."' AND scu.c_id = '".$courseId."'";
2650
            $result_users = Database::query($sql);
2651
            $time_spent = 0;
2652
            $progress = 0;
2653
            $nb_progress_lp = 0;
2654
            $score = 0;
2655
            $nb_score_lp = 0;
2656
            $nb_messages = 0;
2657
            $nb_assignments = 0;
2658
            $last_login_date = false;
2659
            $total_score_obtained = 0;
2660
            $total_score_possible = 0;
2661
            $total_questions_answered = 0;
2662
            while ($row_user = Database::fetch_object($result_users)) {
2663
                // get time spent in the course and session
2664
                $time_spent += Tracking::get_time_spent_on_the_course($row_user->user_id, $courseId, $session_id);
2665
                $progress_tmp = Tracking::get_avg_student_progress($row_user->user_id, $row->code, [], $session_id, true);
2666
                $progress += $progress_tmp[0];
2667
                $nb_progress_lp += $progress_tmp[1];
2668
                $score_tmp = Tracking::get_avg_student_score($row_user->user_id, $row->code, [], $session_id, true);
2669
                if (is_array($score_tmp)) {
2670
                    $score += $score_tmp[0];
2671
                    $nb_score_lp += $score_tmp[1];
2672
                }
2673
                $nb_messages += Tracking::count_student_messages($row_user->user_id, $row->code, $session_id);
2674
                $nb_assignments += Tracking::count_student_assignments($row_user->user_id, $row->code, $session_id);
2675
                $last_login_date_tmp = Tracking::get_last_connection_date_on_the_course(
2676
                    $row_user->user_id,
2677
                    $courseInfo,
2678
                    $session_id,
2679
                    false
2680
                );
2681
                if ($last_login_date_tmp != false && $last_login_date == false) {
2682
                    // TODO: To be cleaned.
2683
                    $last_login_date = $last_login_date_tmp;
2684
                } elseif ($last_login_date_tmp != false && $last_login_date != false) {
2685
                    // TODO: Repeated previous condition! To be cleaned.
2686
                    // Find the max and assign it to first_login_date
2687
                    if (strtotime($last_login_date_tmp) > strtotime($last_login_date)) {
2688
                        $last_login_date = $last_login_date_tmp;
2689
                    }
2690
                }
2691
2692
                $exercise_results_tmp = self::exercises_results($row_user->user_id, $row->code, $session_id);
2693
                $total_score_obtained += $exercise_results_tmp['score_obtained'];
2694
                $total_score_possible += $exercise_results_tmp['score_possible'];
2695
                $total_questions_answered += $exercise_results_tmp['questions_answered'];
2696
            }
2697
            if ($nb_progress_lp > 0) {
2698
                $avg_progress = round($progress / $nb_progress_lp, 2);
2699
            } else {
2700
                $avg_progress = 0;
2701
            }
2702
            if ($nb_score_lp > 0) {
2703
                $avg_score = round($score / $nb_score_lp, 2);
2704
            } else {
2705
                $avg_score = '-';
2706
            }
2707
            if ($last_login_date) {
2708
                $last_login_date = api_convert_and_format_date(
2709
                    $last_login_date,
2710
                    DATE_FORMAT_SHORT,
2711
                    date_default_timezone_get()
2712
                );
2713
            } else {
2714
                $last_login_date = '-';
2715
            }
2716
            if ($total_score_possible > 0) {
2717
                $total_score_percentage = round($total_score_obtained / $total_score_possible * 100, 2);
2718
            } else {
2719
                $total_score_percentage = 0;
2720
            }
2721
            if ($total_score_percentage > 0) {
2722
                $total_score = $total_score_obtained.'/'.$total_score_possible.' ('.$total_score_percentage.' %)';
2723
            } else {
2724
                $total_score = '-';
2725
            }
2726
            // time spent in the course
2727
            $return .= '    <td><div>'.api_time_to_hms($time_spent).'</div></td>';
2728
            // student progress in course
2729
            $return .= '    <td><div>'.$avg_progress.'</div></td>';
2730
            // student score
2731
            $return .= '    <td><div>'.$avg_score.'</div></td>';
2732
            // student messages
2733
            $return .= '    <td><div>'.$nb_messages.'</div></td>';
2734
            // student assignments
2735
            $return .= '    <td><div>'.$nb_assignments.'</div></td>';
2736
            // student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
2737
            $return .= '<td width="105px;">'.$total_score.'</td>';
2738
            $return .= '<td>'.$total_questions_answered.'</td>';
2739
            // last connection
2740
            $return .= '    <td><div>'.$last_login_date.'</div></td>';
2741
            $return .= '<tr>';
2742
        }
2743
        $return .= '</table>';
2744
2745
        return $return;
2746
    }
2747
2748
    /**
2749
     * This function exports the table that we see in display_tracking_session_overview().
2750
     */
2751
    public static function export_tracking_session_overview()
2752
    {
2753
        // database table definition
2754
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
2755
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
2756
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2757
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
2758
2759
        // the values of the sortable table
2760
        $from = 0;
2761
        if ($_GET['tracking_session_overview_page_nr']) {
2762
            $from = $_GET['tracking_session_overview_page_nr'];
2763
        }
2764
2765
        $orderby = 0;
2766
        if ($_GET['tracking_session_overview_column']) {
2767
            $orderby = $_GET['tracking_session_overview_column'];
2768
        }
2769
2770
        $direction = 'ASC';
2771
        if ($_GET['tracking_session_overview_direction']) {
2772
            $direction = $_GET['tracking_session_overview_direction'];
2773
        }
2774
2775
        $session_data = self::get_session_data_tracking_overview($from, 1000, $orderby, $direction);
2776
2777
        $csv_content = [];
2778
2779
        // the first line of the csv file with the column headers
2780
        $csv_row = [];
2781
        $csv_row[] = get_lang('Session');
2782
        $csv_row[] = get_lang('Course');
2783
        $csv_row[] = get_lang('AvgTimeSpentInTheCourse');
2784
        $csv_row[] = get_lang('AvgStudentsProgress');
2785
        $csv_row[] = get_lang('AvgCourseScore');
2786
        $csv_row[] = get_lang('TotalNumberOfMessages');
2787
        $csv_row[] = get_lang('TotalNumberOfAssignments');
2788
        $csv_row[] = get_lang('TotalExercisesScoreObtained');
2789
        $csv_row[] = get_lang('TotalExercisesScorePossible');
2790
        $csv_row[] = get_lang('TotalExercisesAnswered');
2791
        $csv_row[] = get_lang('TotalExercisesScorePercentage');
2792
        $csv_row[] = get_lang('LatestLogin');
2793
        $csv_content[] = $csv_row;
2794
2795
        // the other lines (the data)
2796
        foreach ($session_data as $key => $session) {
2797
            $session_id = $session[0];
2798
            $session_title = $session[1];
2799
2800
            // getting all the courses of the session
2801
            $sql = "SELECT * FROM $tbl_course AS c
2802
                    INNER JOIN $tbl_session_rel_course AS sc
2803
                    ON sc.c_id = c.id
2804
                    WHERE sc.session_id = '".$session_id."';";
2805
            $result = Database::query($sql);
2806
            while ($row = Database::fetch_object($result)) {
2807
                $courseId = $row->c_id;
2808
                $courseInfo = api_get_course_info_by_id($courseId);
2809
                $csv_row = [];
2810
                $csv_row[] = $session_title;
2811
                $csv_row[] = $row->title;
2812
                // get the users in the course
2813
                $sql = "SELECT scu.user_id
2814
                        FROM $tbl_user AS u
2815
                        INNER JOIN $tbl_session_rel_course_rel_user AS scu
2816
                        ON u.user_id = scu.user_id
2817
                        WHERE scu.session_id = '".$session_id."' AND scu.c_id = '".$courseId."'";
2818
                $result_users = Database::query($sql);
2819
                $time_spent = 0;
2820
                $progress = 0;
2821
                $nb_progress_lp = 0;
2822
                $score = 0;
2823
                $nb_score_lp = 0;
2824
                $nb_messages = 0;
2825
                $nb_assignments = 0;
2826
                $last_login_date = false;
2827
                $total_score_obtained = 0;
2828
                $total_score_possible = 0;
2829
                $total_questions_answered = 0;
2830
                while ($row_user = Database::fetch_object($result_users)) {
2831
                    // get time spent in the course and session
2832
                    $time_spent += Tracking::get_time_spent_on_the_course($row_user->user_id, $courseId, $session_id);
2833
                    $progress_tmp = Tracking::get_avg_student_progress(
2834
                        $row_user->user_id,
2835
                        $row->code,
2836
                        [],
2837
                        $session_id,
2838
                        true
2839
                    );
2840
                    $progress += $progress_tmp[0];
2841
                    $nb_progress_lp += $progress_tmp[1];
2842
                    $score_tmp = Tracking::get_avg_student_score(
2843
                        $row_user->user_id,
2844
                        $row->code,
2845
                        [],
2846
                        $session_id,
2847
                        true
2848
                    );
2849
                    if (is_array($score_tmp)) {
2850
                        $score += $score_tmp[0];
2851
                        $nb_score_lp += $score_tmp[1];
2852
                    }
2853
                    $nb_messages += Tracking::count_student_messages(
2854
                        $row_user->user_id,
2855
                        $row->code,
2856
                        $session_id
2857
                    );
2858
2859
                    $nb_assignments += Tracking::count_student_assignments(
2860
                        $row_user->user_id,
2861
                        $row->code,
2862
                        $session_id
2863
                    );
2864
2865
                    $last_login_date_tmp = Tracking:: get_last_connection_date_on_the_course(
2866
                        $row_user->user_id,
2867
                        $courseInfo,
2868
                        $session_id,
2869
                        false
2870
                    );
2871
                    if ($last_login_date_tmp != false && $last_login_date == false) {
2872
                        // TODO: To be cleaned.
2873
                        $last_login_date = $last_login_date_tmp;
2874
                    } elseif ($last_login_date_tmp != false && $last_login_date == false) {
2875
                        // TODO: Repeated previous condition. To be cleaned.
2876
                        // Find the max and assign it to first_login_date
2877
                        if (strtotime($last_login_date_tmp) > strtotime($last_login_date)) {
2878
                            $last_login_date = $last_login_date_tmp;
2879
                        }
2880
                    }
2881
2882
                    $exercise_results_tmp = self::exercises_results($row_user->user_id, $row->code, $session_id);
2883
                    $total_score_obtained += $exercise_results_tmp['score_obtained'];
2884
                    $total_score_possible += $exercise_results_tmp['score_possible'];
2885
                    $total_questions_answered += $exercise_results_tmp['questions_answered'];
2886
                }
2887
                if ($nb_progress_lp > 0) {
2888
                    $avg_progress = round($progress / $nb_progress_lp, 2);
2889
                } else {
2890
                    $avg_progress = 0;
2891
                }
2892
                if ($nb_score_lp > 0) {
2893
                    $avg_score = round($score / $nb_score_lp, 2);
2894
                } else {
2895
                    $avg_score = '-';
2896
                }
2897
                if ($last_login_date) {
2898
                    $last_login_date = api_convert_and_format_date(
2899
                        $last_login_date,
2900
                        DATE_FORMAT_SHORT,
2901
                        date_default_timezone_get()
2902
                    );
2903
                } else {
2904
                    $last_login_date = '-';
2905
                }
2906
                if ($total_score_possible > 0) {
2907
                    $total_score_percentage = round($total_score_obtained / $total_score_possible * 100, 2);
2908
                } else {
2909
                    $total_score_percentage = 0;
2910
                }
2911
                if ($total_score_percentage > 0) {
2912
                    $total_score = $total_score_obtained.'/'.$total_score_possible.' ('.$total_score_percentage.' %)';
2913
                } else {
2914
                    $total_score = '-';
2915
                }
2916
                // time spent in the course
2917
                $csv_row[] = api_time_to_hms($time_spent);
2918
                // student progress in course
2919
                $csv_row[] = $avg_progress;
2920
                // student score
2921
                $csv_row[] = $avg_score;
2922
                // student messages
2923
                $csv_row[] = $nb_messages;
2924
                // student assignments
2925
                $csv_row[] = $nb_assignments;
2926
                // student exercises results (obtained score, maximum score, number of exercises answered, score percentage)
2927
                $csv_row[] = $total_score_obtained;
2928
                $csv_row[] = $total_score_possible;
2929
                $csv_row[] = $total_questions_answered;
2930
                $csv_row[] = $total_score_percentage;
2931
                // last connection
2932
                $csv_row[] = $last_login_date;
2933
                $csv_content[] = $csv_row;
2934
            }
2935
        }
2936
        Export::arrayToCsv($csv_content, 'reporting_session_overview');
2937
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
2938
    }
2939
2940
    /**
2941
     * Get general information about the exercise performance of the user
2942
     * the total obtained score (all the score on all the questions)
2943
     * the maximum score that could be obtained
2944
     * the number of questions answered
2945
     * the success percentage.
2946
     *
2947
     * @param int    $user_id     the id of the user
2948
     * @param string $course_code the course code
2949
     * @param int    $session_id
2950
     *
2951
     * @return array
2952
     *
2953
     * @author Patrick Cool <[email protected]>, Ghent University, Belgium
2954
     *
2955
     * @version Dokeos 1.8.6
2956
     *
2957
     * @since November 2008
2958
     */
2959
    public static function exercises_results($user_id, $course_code, $session_id = 0)
2960
    {
2961
        $user_id = (int) $user_id;
2962
        $courseId = api_get_course_int_id($course_code);
2963
        $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
2964
2965
        $sql = "SELECT exe_result, exe_weighting
2966
                FROM $table
2967
                WHERE
2968
                    c_id = $courseId AND
2969
                    exe_user_id = $user_id";
2970
2971
        $session_id = (int) $session_id;
2972
        if (!empty($session_id)) {
2973
            $sql .= " AND session_id = '".$session_id."' ";
2974
        }
2975
        $result = Database::query($sql);
2976
        $score_obtained = 0;
2977
        $score_possible = 0;
2978
        $questions_answered = 0;
2979
        while ($row = Database::fetch_array($result)) {
2980
            $score_obtained += $row['exe_result'];
2981
            $score_possible += $row['exe_weighting'];
2982
            $questions_answered++;
2983
        }
2984
2985
        $percentage = null;
2986
        if ($score_possible != 0) {
2987
            $percentage = round(($score_obtained / $score_possible * 100), 2);
2988
        }
2989
2990
        return [
2991
            'score_obtained' => $score_obtained,
2992
            'score_possible' => $score_possible,
2993
            'questions_answered' => $questions_answered,
2994
            'percentage' => $percentage,
2995
        ];
2996
    }
2997
2998
    /**
2999
     * This function exports the table that we see in display_tracking_user_overview().
3000
     *
3001
     * @author Patrick Cool <[email protected]>, Ghent University, Belgium
3002
     *
3003
     * @version Dokeos 1.8.6
3004
     *
3005
     * @since October 2008
3006
     */
3007
    public static function export_tracking_user_overview()
3008
    {
3009
        // database table definitions
3010
        $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
3011
        $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
3012
3013
        // the values of the sortable table
3014
        if ($_GET['tracking_user_overview_page_nr']) {
3015
            $from = $_GET['tracking_user_overview_page_nr'];
3016
        } else {
3017
            $from = 0;
3018
        }
3019
        if ($_GET['tracking_user_overview_column']) {
3020
            $orderby = $_GET['tracking_user_overview_column'];
3021
        } else {
3022
            $orderby = 0;
3023
        }
3024
        if ($is_western_name_order != api_is_western_name_order() && ($orderby == 1 || $orderby == 2)) {
3025
            // Swapping the sorting column if name order for export is different than the common name order.
3026
            $orderby = 3 - $orderby;
3027
        }
3028
        if ($_GET['tracking_user_overview_direction']) {
3029
            $direction = $_GET['tracking_user_overview_direction'];
3030
        } else {
3031
            $direction = 'ASC';
3032
        }
3033
3034
        $user_data = self::get_user_data_tracking_overview(
3035
            $from,
3036
            1000,
3037
            $orderby,
3038
            $direction
3039
        );
3040
3041
        // the first line of the csv file with the column headers
3042
        $csv_row = [];
3043
        $csv_row[] = get_lang('OfficialCode');
3044
        if ($is_western_name_order) {
3045
            $csv_row[] = get_lang('FirstName');
3046
            $csv_row[] = get_lang('LastName');
3047
        } else {
3048
            $csv_row[] = get_lang('LastName');
3049
            $csv_row[] = get_lang('FirstName');
3050
        }
3051
        $csv_row[] = get_lang('LoginName');
3052
        $csv_row[] = get_lang('CourseCode');
3053
3054
        // the additional user defined fields (only those that were selected to be exported)
3055
        $fields = UserManager::get_extra_fields(0, 50, 5, 'ASC');
3056
3057
        $additionalExportFields = Session::read('additional_export_fields');
3058
3059
        if (is_array($additionalExportFields)) {
3060
            foreach ($additionalExportFields as $key => $extra_field_export) {
3061
                $csv_row[] = $fields[$extra_field_export][3];
3062
                $field_names_to_be_exported[] = 'extra_'.$fields[$extra_field_export][1];
3063
            }
3064
        }
3065
        $csv_row[] = get_lang('AvgTimeSpentInTheCourse', '');
3066
        $csv_row[] = get_lang('AvgStudentsProgress', '');
3067
        $csv_row[] = get_lang('AvgCourseScore', '');
3068
        $csv_row[] = get_lang('AvgExercisesScore', '');
3069
        $csv_row[] = get_lang('AvgMessages', '');
3070
        $csv_row[] = get_lang('AvgAssignments', '');
3071
        $csv_row[] = get_lang('TotalExercisesScoreObtained', '');
3072
        $csv_row[] = get_lang('TotalExercisesScorePossible', '');
3073
        $csv_row[] = get_lang('TotalExercisesAnswered', '');
3074
        $csv_row[] = get_lang('TotalExercisesScorePercentage', '');
3075
        $csv_row[] = get_lang('FirstLogin', '');
3076
        $csv_row[] = get_lang('LatestLogin', '');
3077
        $csv_content[] = $csv_row;
3078
3079
        // the other lines (the data)
3080
        foreach ($user_data as $key => $user) {
3081
            // getting all the courses of the user
3082
            $sql = "SELECT * FROM $tbl_course_user
3083
                    WHERE user_id = '".intval($user[4])."' AND relation_type<>".COURSE_RELATION_TYPE_RRHH." ";
3084
            $result = Database::query($sql);
3085
            while ($row = Database::fetch_row($result)) {
3086
                $courseInfo = api_get_course_info($row['course_code']);
3087
                $courseId = $courseInfo['real_id'];
3088
3089
                $csv_row = [];
3090
                // user official code
3091
                $csv_row[] = $user[0];
3092
                // user first|last name
3093
                $csv_row[] = $user[1];
3094
                // user last|first name
3095
                $csv_row[] = $user[2];
3096
                // user login name
3097
                $csv_row[] = $user[3];
3098
                // course code
3099
                $csv_row[] = $row[0];
3100
                // the additional defined user fields
3101
                $extra_fields = self::get_user_overview_export_extra_fields($user[4]);
3102
3103
                if (is_array($field_names_to_be_exported)) {
3104
                    foreach ($field_names_to_be_exported as $key => $extra_field_export) {
3105
                        $csv_row[] = $extra_fields[$extra_field_export];
3106
                    }
3107
                }
3108
                // time spent in the course
3109
                $csv_row[] = api_time_to_hms(Tracking::get_time_spent_on_the_course($user[4], $courseId));
3110
                // student progress in course
3111
                $csv_row[] = round(Tracking::get_avg_student_progress($user[4], $row[0]), 2);
3112
                // student score
3113
                $csv_row[] = round(Tracking::get_avg_student_score($user[4], $row[0]), 2);
3114
                // student tes score
3115
                $csv_row[] = round(Tracking::get_avg_student_exercise_score($user[4], $row[0]), 2);
3116
                // student messages
3117
                $csv_row[] = Tracking::count_student_messages($user[4], $row[0]);
3118
                // student assignments
3119
                $csv_row[] = Tracking::count_student_assignments($user[4], $row[0]);
3120
                // student exercises results
3121
                $exercises_results = self::exercises_results($user[4], $row[0]);
3122
                $csv_row[] = $exercises_results['score_obtained'];
3123
                $csv_row[] = $exercises_results['score_possible'];
3124
                $csv_row[] = $exercises_results['questions_answered'];
3125
                $csv_row[] = $exercises_results['percentage'];
3126
                // first connection
3127
                $csv_row[] = Tracking::get_first_connection_date_on_the_course($user[4], $courseId);
3128
                // last connection
3129
                $csv_row[] = strip_tags(Tracking::get_last_connection_date_on_the_course($user[4], $courseInfo));
3130
3131
                $csv_content[] = $csv_row;
3132
            }
3133
        }
3134
        Export::arrayToCsv($csv_content, 'reporting_user_overview');
3135
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
3136
    }
3137
3138
    /**
3139
     * Get data for courses list in sortable with pagination.
3140
     *
3141
     * @return array
3142
     */
3143
    public static function get_course_data($from, $number_of_items, $column, $direction)
3144
    {
3145
        global $courses, $csv_content, $charset, $session_id;
3146
3147
        // definition database tables
3148
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
3149
        $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
3150
        $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
3151
3152
        $course_data = [];
3153
        $courses_code = array_keys($courses);
3154
3155
        foreach ($courses_code as &$code) {
3156
            $code = "'$code'";
3157
        }
3158
3159
        // get all courses with limit
3160
        $sql = "SELECT course.code as col1, course.title as col2
3161
                FROM $tbl_course course
3162
                WHERE course.code IN (".implode(',', $courses_code).")";
3163
3164
        if (!in_array($direction, ['ASC', 'DESC'])) {
3165
            $direction = 'ASC';
3166
        }
3167
3168
        $column = (int) $column;
3169
        $from = (int) $from;
3170
        $number_of_items = (int) $number_of_items;
3171
        $sql .= " ORDER BY col$column $direction ";
3172
        $sql .= " LIMIT $from,$number_of_items";
3173
3174
        $res = Database::query($sql);
3175
        while ($row_course = Database::fetch_row($res)) {
3176
            $course_code = $row_course[0];
3177
            $courseInfo = api_get_course_info($course_code);
3178
            $courseId = $courseInfo['real_id'];
3179
            $avg_assignments_in_course = $avg_messages_in_course = $nb_students_in_course = $avg_progress_in_course = $avg_score_in_course = $avg_time_spent_in_course = $avg_score_in_exercise = 0;
3180
3181
            // students directly subscribed to the course
3182
            if (empty($session_id)) {
3183
                $sql = "SELECT user_id
3184
                        FROM $tbl_course_user as course_rel_user
3185
                        WHERE
3186
                            course_rel_user.status='5' AND
3187
                            course_rel_user.c_id = '$courseId'";
3188
            } else {
3189
                $sql = "SELECT user_id FROM $tbl_session_course_user srcu
3190
                        WHERE
3191
                            c_id = '$courseId' AND
3192
                            session_id = '$session_id' AND
3193
                            status<>2";
3194
            }
3195
            $rs = Database::query($sql);
3196
            $users = [];
3197
            while ($row = Database::fetch_array($rs)) {
3198
                $users[] = $row['user_id'];
3199
            }
3200
3201
            if (count($users) > 0) {
3202
                $nb_students_in_course = count($users);
3203
                $avg_assignments_in_course = Tracking::count_student_assignments($users, $course_code, $session_id);
3204
                $avg_messages_in_course = Tracking::count_student_messages($users, $course_code, $session_id);
3205
                $avg_progress_in_course = Tracking::get_avg_student_progress($users, $course_code, [], $session_id);
3206
                $avg_score_in_course = Tracking::get_avg_student_score($users, $course_code, [], $session_id);
3207
                $avg_score_in_exercise = Tracking::get_avg_student_exercise_score($users, $course_code, 0, $session_id);
3208
                $avg_time_spent_in_course = Tracking::get_time_spent_on_the_course(
3209
                    $users,
3210
                    $courseInfo['real_id'],
3211
                    $session_id
3212
                );
3213
3214
                $avg_progress_in_course = round($avg_progress_in_course / $nb_students_in_course, 2);
3215
                if (is_numeric($avg_score_in_course)) {
3216
                    $avg_score_in_course = round($avg_score_in_course / $nb_students_in_course, 2);
3217
                }
3218
                $avg_time_spent_in_course = api_time_to_hms($avg_time_spent_in_course / $nb_students_in_course);
3219
            } else {
3220
                $avg_time_spent_in_course = null;
3221
                $avg_progress_in_course = null;
3222
                $avg_score_in_course = null;
3223
                $avg_score_in_exercise = null;
3224
                $avg_messages_in_course = null;
3225
                $avg_assignments_in_course = null;
3226
            }
3227
            $table_row = [];
3228
            $table_row[] = $row_course[1];
3229
            $table_row[] = $nb_students_in_course;
3230
            $table_row[] = $avg_time_spent_in_course;
3231
            $table_row[] = is_null($avg_progress_in_course) ? '' : $avg_progress_in_course.'%';
3232
            $table_row[] = is_null($avg_score_in_course) ? '' : $avg_score_in_course.'%';
3233
            $table_row[] = is_null($avg_score_in_exercise) ? '' : $avg_score_in_exercise.'%';
3234
            $table_row[] = $avg_messages_in_course;
3235
            $table_row[] = $avg_assignments_in_course;
3236
3237
            //set the "from" value to know if I access the Reporting by the chamilo tab or the course link
3238
            $table_row[] = '<center><a href="../../tracking/courseLog.php?cidReq='.$course_code.'&from=myspace&id_session='.$session_id.'">
3239
                             '.Display::return_icon('2rightarrow.png', get_lang('Details')).'
3240
                             </a>
3241
                            </center>';
3242
            $scoreInCourse = null;
3243
            if (null !== $avg_score_in_course) {
3244
                if (is_numeric($avg_score_in_course)) {
3245
                    $scoreInCourse = $avg_score_in_course.'%';
3246
                } else {
3247
                    $scoreInCourse = $avg_score_in_course;
3248
                }
3249
            }
3250
            $csv_content[] = [
3251
                api_html_entity_decode($row_course[1], ENT_QUOTES, $charset),
3252
                $nb_students_in_course,
3253
                $avg_time_spent_in_course,
3254
                is_null($avg_progress_in_course) ? null : $avg_progress_in_course.'%',
3255
                $scoreInCourse,
3256
                is_null($avg_score_in_exercise) ? null : $avg_score_in_exercise.'%',
3257
                $avg_messages_in_course,
3258
                $avg_assignments_in_course,
3259
            ];
3260
            $course_data[] = $table_row;
3261
        }
3262
3263
        return $course_data;
3264
    }
3265
3266
    /**
3267
     * Get the number of users of the platform.
3268
     *
3269
     * @return int
3270
     */
3271
    public static function get_number_of_users_tracking_overview()
3272
    {
3273
        return UserManager::get_number_of_users(0, api_get_current_access_url_id());
3274
    }
3275
3276
    /**
3277
     * Get all the data for the sortable table of the reporting progress of
3278
     * all users and all the courses the user is subscribed to.
3279
     *
3280
     * @param int    $from
3281
     * @param int    $numberItems
3282
     * @param int    $column
3283
     * @param string $direction
3284
     *
3285
     * @return array
3286
     */
3287
    public static function get_user_data_tracking_overview($from, $numberItems, $column, $direction)
3288
    {
3289
        $isWestern = api_is_western_name_order();
3290
3291
        switch ($column) {
3292
            case '0':
3293
                $column = 'official_code';
3294
                break;
3295
            case '1':
3296
                $column = $isWestern ? 'firstname' : 'lastname';
3297
                break;
3298
            case '2':
3299
                $column = $isWestern ? 'lastname' : 'firstname';
3300
                break;
3301
            case '3':
3302
                $column = 'username';
3303
                break;
3304
            case '4':
3305
                $column = 'username';
3306
                break;
3307
        }
3308
3309
        $order = [
3310
            " `$column` $direction",
3311
        ];
3312
        $userList = UserManager::get_user_list([], $order, $from, $numberItems);
3313
        $return = [];
3314
        foreach ($userList as $user) {
3315
            $firstPosition = $user['lastname'];
3316
            $secondPosition = $user['firstname'];
3317
            if ($isWestern) {
3318
                $firstPosition = $user['firstname'];
3319
                $secondPosition = $user['lastname'];
3320
            }
3321
            $return[] = [
3322
                '0' => $user['official_code'],
3323
                'col0' => $user['official_code'],
3324
                '1' => $firstPosition,
3325
                'col1' => $firstPosition,
3326
                '2' => $secondPosition,
3327
                'col2' => $secondPosition,
3328
                '3' => $user['username'],
3329
                'col3' => $user['username'],
3330
                '4' => $user['user_id'],
3331
                'col4' => $user['user_id'],
3332
            ];
3333
        }
3334
3335
        return $return;
3336
    }
3337
3338
    /**
3339
     * Get all information that the user with user_id = $user_data has
3340
     * entered in the additionally defined profile fields.
3341
     *
3342
     * @param int $user_id the id of the user
3343
     *
3344
     * @return array
3345
     *
3346
     * @author Patrick Cool <[email protected]>, Ghent University, Belgium
3347
     *
3348
     * @version Dokeos 1.8.6
3349
     *
3350
     * @since November 2008
3351
     */
3352
    public static function get_user_overview_export_extra_fields($user_id)
3353
    {
3354
        // include the user manager
3355
        $data = UserManager::get_extra_user_data($user_id, true);
3356
3357
        return $data;
3358
    }
3359
3360
    /**
3361
     * Checks if a username exist in the DB otherwise it create a "double"
3362
     * i.e. if we look into for jmontoya but the user's name already exist we create the user jmontoya2
3363
     * the return array will be array(username=>'jmontoya', sufix='2').
3364
     *
3365
     * @param string firstname
3366
     * @param string lastname
3367
     * @param string username
3368
     *
3369
     * @return array with the username, the sufix
3370
     *
3371
     * @author Julio Montoya
3372
     */
3373
    public static function make_username($firstname, $lastname, $username, $language = null, $encoding = null)
3374
    {
3375
        // if username exist
3376
        if (!UserManager::is_username_available($username) || empty($username)) {
3377
            $i = 0;
3378
            while (1) {
3379
                if ($i == 0) {
3380
                    $sufix = '';
3381
                } else {
3382
                    $sufix = $i;
3383
                }
3384
                $desired_username = UserManager::create_username(
3385
                    $firstname,
3386
                    $lastname
3387
                );
3388
                if (UserManager::is_username_available($desired_username.$sufix)) {
3389
                    break;
3390
                } else {
3391
                    $i++;
3392
                }
3393
            }
3394
            $username_array = ['username' => $desired_username, 'sufix' => $sufix];
3395
3396
            return $username_array;
3397
        } else {
3398
            $username_array = ['username' => $username, 'sufix' => ''];
3399
3400
            return $username_array;
3401
        }
3402
    }
3403
3404
    /**
3405
     * Checks if there are repeted users in a given array.
3406
     *
3407
     * @param array $usernames  list of the usernames in the uploaded file
3408
     * @param array $user_array $user_array['username'] and $user_array['sufix']
3409
     *                          where suffix is the number part in a login i.e -> jmontoya2
3410
     *
3411
     * @return array with the $usernames array and the $user_array array
3412
     *
3413
     * @author Julio Montoya
3414
     */
3415
    public static function check_user_in_array($usernames, $user_array)
3416
    {
3417
        $user_list = array_keys($usernames);
3418
        $username = $user_array['username'].$user_array['sufix'];
3419
3420
        if (in_array($username, $user_list)) {
3421
            $user_array['sufix'] += $usernames[$username];
3422
            $usernames[$username]++;
3423
        } else {
3424
            $usernames[$username] = 1;
3425
        }
3426
        $result_array = [$usernames, $user_array];
3427
3428
        return $result_array;
3429
    }
3430
3431
    /**
3432
     * Checks whether a username has been already subscribed in a session.
3433
     *
3434
     * @param string $username    a given username
3435
     * @param array  $course_list the array with the course list id
3436
     * @param int    $id_session  the session id
3437
     *
3438
     * @return int 0 if the user is not subscribed otherwise it returns the user_id of the given username
3439
     *
3440
     * @author Julio Montoya
3441
     */
3442
    public static function user_available_in_session($username, $course_list, $id_session)
3443
    {
3444
        $table_user = Database::get_main_table(TABLE_MAIN_USER);
3445
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
3446
        $id_session = (int) $id_session;
3447
        $username = Database::escape_string($username);
3448
        foreach ($course_list as $courseId) {
3449
            $courseId = (int) $courseId;
3450
            $sql = " SELECT u.user_id FROM $tbl_session_rel_course_rel_user rel
3451
                     INNER JOIN $table_user u
3452
                     ON (rel.user_id = u.user_id)
3453
                     WHERE
3454
                        rel.session_id='$id_session' AND
3455
                        u.status='5' AND
3456
                        u.username ='$username' AND
3457
                        rel.c_id='$courseId'";
3458
            $rs = Database::query($sql);
3459
            if (Database::num_rows($rs) > 0) {
3460
                return Database::result($rs, 0, 0);
3461
            }
3462
        }
3463
3464
        return 0;
3465
    }
3466
3467
    /**
3468
     * This function checks whether some users in the uploaded file
3469
     * repeated and creates unique usernames if necesary.
3470
     * A case: Within the file there is an user repeted twice (Julio Montoya / Julio Montoya)
3471
     * and the username fields are empty.
3472
     * Then, this function would create unique usernames based on the first and the last name.
3473
     * Two users wiould be created - jmontoya and jmontoya2.
3474
     * Of course, if in the database there is a user with the name jmontoya,
3475
     * the newly created two users registered would be jmontoya2 and jmontoya3.
3476
     *
3477
     * @param $users list of users
3478
     *
3479
     * @return array
3480
     *
3481
     * @author Julio Montoya Armas
3482
     */
3483
    public static function check_all_usernames($users, $course_list, $id_session)
3484
    {
3485
        $table_user = Database::get_main_table(TABLE_MAIN_USER);
3486
        $usernames = [];
3487
        $new_users = [];
3488
        foreach ($users as $index => $user) {
3489
            $desired_username = [];
3490
            if (empty($user['UserName'])) {
3491
                $desired_username = self::make_username($user['FirstName'], $user['LastName'], '');
3492
                $pre_username = $desired_username['username'].$desired_username['sufix'];
3493
                $user['UserName'] = $pre_username;
3494
                $user['create'] = '1';
3495
            } else {
3496
                if (UserManager::is_username_available($user['UserName'])) {
3497
                    $desired_username = self::make_username($user['FirstName'], $user['LastName'], $user['UserName']);
3498
                    $user['UserName'] = $desired_username['username'].$desired_username['sufix'];
3499
                    $user['create'] = '1';
3500
                } else {
3501
                    $is_session_avail = self::user_available_in_session($user['UserName'], $course_list, $id_session);
3502
                    if ($is_session_avail == 0) {
3503
                        $user_name = $user['UserName'];
3504
                        $sql_select = "SELECT user_id FROM $table_user WHERE username ='$user_name' ";
3505
                        $rs = Database::query($sql_select);
3506
                        $user['create'] = Database::result($rs, 0, 0);
3507
                    } else {
3508
                        $user['create'] = $is_session_avail;
3509
                    }
3510
                }
3511
            }
3512
            // Usernames is the current list of users in the file.
3513
            $result_array = self::check_user_in_array($usernames, $desired_username);
3514
            $usernames = $result_array[0];
3515
            $desired_username = $result_array[1];
3516
            $user['UserName'] = $desired_username['username'].$desired_username['sufix'];
3517
            $new_users[] = $user;
3518
        }
3519
3520
        return $new_users;
3521
    }
3522
3523
    /**
3524
     * This functions checks whether there are users that are already
3525
     * registered in the DB by different creator than the current coach.
3526
     *
3527
     * @param array $users
3528
     *
3529
     * @return array
3530
     *
3531
     * @author Julio Montoya Armas
3532
     */
3533
    public static function get_user_creator($users)
3534
    {
3535
        $errors = [];
3536
        $table_user = Database::get_main_table(TABLE_MAIN_USER);
3537
        foreach ($users as $index => $user) {
3538
            $username = Database::escape_string($user['UserName']);
3539
            $sql = "SELECT creator_id FROM $table_user WHERE username='$username' ";
3540
3541
            $rs = Database::query($sql);
3542
            $creator_id = Database::result($rs, 0, 0);
3543
            // check if we are the creators or not
3544
            if ($creator_id != '') {
3545
                if ($creator_id != api_get_user_id()) {
3546
                    $user['error'] = get_lang('UserAlreadyRegisteredByOtherCreator');
3547
                    $errors[] = $user;
3548
                }
3549
            }
3550
        }
3551
3552
        return $errors;
3553
    }
3554
3555
    /**
3556
     * Validates imported data.
3557
     *
3558
     * @param array $users list of users
3559
     */
3560
    public static function validate_data($users, $id_session = null)
3561
    {
3562
        $errors = [];
3563
        $new_users = [];
3564
        foreach ($users as $index => $user) {
3565
            // 1. Check whether mandatory fields are set.
3566
            $mandatory_fields = ['LastName', 'FirstName'];
3567
            if (api_get_setting('registration', 'email') == 'true') {
3568
                $mandatory_fields[] = 'Email';
3569
            }
3570
3571
            foreach ($mandatory_fields as $key => $field) {
3572
                if (!isset($user[$field]) || strlen($user[$field]) == 0) {
3573
                    $user['error'] = get_lang($field.'Mandatory');
3574
                    $errors[] = $user;
3575
                }
3576
            }
3577
            // 2. Check whether the username is too long.
3578
            if (UserManager::is_username_too_long($user['UserName'])) {
3579
                $user['error'] = get_lang('UserNameTooLong');
3580
                $errors[] = $user;
3581
            }
3582
3583
            $user['UserName'] = trim($user['UserName']);
3584
3585
            if (empty($user['UserName'])) {
3586
                $user['UserName'] = UserManager::create_username($user['FirstName'], $user['LastName']);
3587
            }
3588
            $new_users[] = $user;
3589
        }
3590
        $results = ['errors' => $errors, 'users' => $new_users];
3591
3592
        return $results;
3593
    }
3594
3595
    /**
3596
     * Adds missing user-information (which isn't required, like password, etc).
3597
     */
3598
    public static function complete_missing_data($user)
3599
    {
3600
        // 1. Generate a password if it is necessary.
3601
        if (!isset($user['Password']) || strlen($user['Password']) == 0) {
3602
            $user['Password'] = api_generate_password();
3603
        }
3604
3605
        return $user;
3606
    }
3607
3608
    /**
3609
     * Saves imported data.
3610
     */
3611
    public static function save_data($users, $course_list, $id_session)
3612
    {
3613
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
3614
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
3615
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
3616
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
3617
3618
        $id_session = (int) $id_session;
3619
        $sendMail = $_POST['sendMail'] ? 1 : 0;
3620
3621
        // Adding users to the platform.
3622
        $new_users = [];
3623
        foreach ($users as $index => $user) {
3624
            $user = self::complete_missing_data($user);
3625
            // coach only will registered users
3626
            $default_status = STUDENT;
3627
            if ($user['create'] == COURSEMANAGER) {
3628
                $user['id'] = UserManager:: create_user(
3629
                    $user['FirstName'],
3630
                    $user['LastName'],
3631
                    $default_status,
3632
                    $user['Email'],
3633
                    $user['UserName'],
3634
                    $user['Password'],
3635
                    $user['OfficialCode'],
3636
                    api_get_setting('PlatformLanguage'),
3637
                    $user['PhoneNumber'],
3638
                    ''
3639
                );
3640
                $user['added_at_platform'] = 1;
3641
            } else {
3642
                $user['id'] = $user['create'];
3643
                $user['added_at_platform'] = 0;
3644
            }
3645
            $new_users[] = $user;
3646
        }
3647
        // Update user list.
3648
        $users = $new_users;
3649
3650
        // Inserting users.
3651
        foreach ($course_list as $enreg_course) {
3652
            $nbr_users = 0;
3653
            $new_users = [];
3654
            $enreg_course = Database::escape_string($enreg_course);
3655
            foreach ($users as $index => $user) {
3656
                $userid = (int) $user['id'];
3657
                $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(session_id, c_id, user_id)
3658
                        VALUES('$id_session','$enreg_course','$userid')";
3659
                $result = Database::query($sql);
3660
                if (Database::affected_rows($result)) {
3661
                    $nbr_users++;
3662
                }
3663
                $new_users[] = $user;
3664
            }
3665
3666
            //update the nbr_users field
3667
            $sql_select = "SELECT COUNT(user_id) as nbUsers FROM $tbl_session_rel_course_rel_user
3668
                           WHERE session_id='$id_session' AND c_id='$enreg_course'";
3669
            $rs = Database::query($sql_select);
3670
            list($nbr_users) = Database::fetch_array($rs);
3671
            $sql_update = "UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users
3672
                           WHERE session_id='$id_session' AND c_id='$enreg_course'";
3673
            Database::query($sql_update);
3674
3675
            $sql_update = "UPDATE $tbl_session SET nbr_users= '$nbr_users' WHERE id='$id_session'";
3676
            Database::query($sql_update);
3677
        }
3678
3679
        $new_users = [];
3680
        foreach ($users as $index => $user) {
3681
            $userid = $user['id'];
3682
            $sql_insert = "INSERT IGNORE INTO $tbl_session_rel_user(session_id, user_id, registered_at)
3683
                           VALUES ('$id_session','$userid', '".api_get_utc_datetime()."')";
3684
            Database::query($sql_insert);
3685
            $user['added_at_session'] = 1;
3686
            $new_users[] = $user;
3687
        }
3688
3689
        $users = $new_users;
3690
        $registered_users = get_lang('FileImported').'<br /> Import file results : <br />';
3691
        // Sending emails.
3692
        $addedto = '';
3693
        if ($sendMail) {
3694
            foreach ($users as $index => $user) {
3695
                $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName');
3696
                $emailbody = get_lang('Dear').' '.
3697
                    api_get_person_name($user['FirstName'], $user['LastName']).",\n\n".
3698
                    get_lang('YouAreReg')." ".api_get_setting('siteName')." ".get_lang('WithTheFollowingSettings')."\n\n".
3699
                    get_lang('Username')." : $user[UserName]\n".
3700
                    get_lang('Pass')." : $user[Password]\n\n".
3701
                    get_lang('Address')." ".api_get_setting('siteName')." ".get_lang('Is')." : ".api_get_path(WEB_PATH)." \n\n".
3702
                    get_lang('Problem')."\n\n".
3703
                    get_lang('SignatureFormula').",\n\n".
3704
                    api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n".
3705
                    get_lang('Manager')." ".api_get_setting('siteName')."\nT. ".
3706
                    api_get_setting('administratorTelephone')."\n".get_lang('Email')." : ".api_get_setting('emailAdministrator');
3707
3708
                api_mail_html(
3709
                    api_get_person_name($user['FirstName'], $user['LastName'], null, PERSON_NAME_EMAIL_ADDRESS),
3710
                    $user['Email'],
3711
                    $emailsubject,
3712
                    $emailbody
3713
                );
3714
                $userInfo = api_get_user_info($user['id']);
3715
3716
                if (($user['added_at_platform'] == 1 && $user['added_at_session'] == 1) || $user['added_at_session'] == 1) {
3717
                    if ($user['added_at_platform'] == 1) {
3718
                        $addedto = get_lang('UserCreatedPlatform');
3719
                    } else {
3720
                        $addedto = '          ';
3721
                    }
3722
3723
                    if ($user['added_at_session'] == 1) {
3724
                        $addedto .= get_lang('UserInSession');
3725
                    }
3726
                } else {
3727
                    $addedto = get_lang('UserNotAdded');
3728
                }
3729
3730
                $registered_users .= UserManager::getUserProfileLink($userInfo).' - '.$addedto.'<br />';
3731
            }
3732
        } else {
3733
            foreach ($users as $index => $user) {
3734
                $userInfo = api_get_user_info($user['id']);
3735
                if (($user['added_at_platform'] == 1 && $user['added_at_session'] == 1) || $user['added_at_session'] == 1) {
3736
                    if ($user['added_at_platform'] == 1) {
3737
                        $addedto = get_lang('UserCreatedPlatform');
3738
                    } else {
3739
                        $addedto = '          ';
3740
                    }
3741
3742
                    if ($user['added_at_session'] == 1) {
3743
                        $addedto .= ' '.get_lang('UserInSession');
3744
                    }
3745
                } else {
3746
                    $addedto = get_lang('UserNotAdded');
3747
                }
3748
                $registered_users .= "<a href=\"../user/userInfo.php?uInfo=".$user['id']."\">".
3749
                    Security::remove_XSS($userInfo['complete_user_name'])."</a> - ".$addedto.'<br />';
3750
            }
3751
        }
3752
        Display::addFlash(Display::return_message($registered_users, 'normal', false));
3753
        header('Location: course.php?id_session='.$id_session);
3754
        exit;
3755
    }
3756
3757
    /**
3758
     * Reads CSV-file.
3759
     *
3760
     * @param string $file Path to the CSV-file
3761
     *
3762
     * @return array All userinformation read from the file
3763
     */
3764
    public function parse_csv_data($file)
3765
    {
3766
        $users = Import::csvToArray($file);
3767
        foreach ($users as $index => $user) {
3768
            if (isset($user['Courses'])) {
3769
                $user['Courses'] = explode('|', trim($user['Courses']));
3770
            }
3771
            $users[$index] = $user;
3772
        }
3773
3774
        return $users;
3775
    }
3776
3777
    /**
3778
     * Reads XML-file.
3779
     *
3780
     * @param string $file Path to the XML-file
3781
     *
3782
     * @return array All userinformation read from the file
3783
     */
3784
    public static function parse_xml_data($file)
3785
    {
3786
        $crawler = Import::xml($file);
3787
        $crawler = $crawler->filter('Contacts > Contact ');
3788
        $array = [];
3789
        foreach ($crawler as $domElement) {
3790
            $row = [];
3791
            foreach ($domElement->childNodes as $node) {
3792
                if ($node->nodeName != '#text') {
3793
                    $row[$node->nodeName] = $node->nodeValue;
3794
                }
3795
            }
3796
            if (!empty($row)) {
3797
                $array[] = $row;
3798
            }
3799
        }
3800
3801
        return $array;
3802
    }
3803
3804
    /**
3805
     * @param int $courseId
3806
     * @param int $sessionId
3807
     * @param int $studentId
3808
     */
3809
    public static function displayTrackingAccessOverView($courseId, $sessionId, $studentId)
3810
    {
3811
        $courseId = (int) $courseId;
3812
        $sessionId = (int) $sessionId;
3813
        $studentId = (int) $studentId;
3814
3815
        $courseList = [];
3816
        $sessionList = [];
3817
        $studentList = [];
3818
3819
        if (!empty($courseId)) {
3820
            $course = api_get_course_entity($courseId);
3821
            if ($course) {
3822
                $courseList[$course->getId()] = $course->getTitle();
3823
            }
3824
        }
3825
3826
        if (!empty($sessionId)) {
3827
            $session = api_get_session_entity($sessionId);
3828
            if ($session) {
3829
                $sessionList[$session->getId()] = $session->getName();
3830
            }
3831
        }
3832
3833
        if (!empty($studentId)) {
3834
            $student = api_get_user_entity($studentId);
3835
            if ($student) {
3836
                $studentList[$student->getId()] = UserManager::formatUserFullName($student);
3837
            }
3838
        }
3839
3840
        $form = new FormValidator('access_overview', 'GET');
3841
        $form->addElement(
3842
            'select_ajax',
3843
            'course_id',
3844
            get_lang('SearchCourse'),
3845
            $courseList,
3846
            [
3847
                'url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?'.http_build_query([
3848
                    'a' => 'search_course_by_session_all',
3849
                    'session_id' => $sessionId,
3850
                    'course_id' => $courseId,
3851
                ]),
3852
            ]
3853
        );
3854
3855
        $form->addElement(
3856
            'select_ajax',
3857
            'session_id',
3858
            get_lang('SearchSession'),
3859
            $sessionList,
3860
            [
3861
                'url_function' => "
3862
                    function () {
3863
                        var params = $.param({
3864
                            a: 'search_session_by_course',
3865
                            course_id: $('#access_overview_course_id').val() || 0
3866
                        });
3867
3868
                        return '".api_get_path(WEB_AJAX_PATH)."session.ajax.php?' + params;
3869
                    }
3870
                ",
3871
            ]
3872
        );
3873
3874
        $form->addSelect(
3875
            'profile',
3876
            get_lang('Profile'),
3877
            [
3878
                '' => get_lang('Select'),
3879
                STUDENT => get_lang('Student'),
3880
                COURSEMANAGER => get_lang('CourseManager'),
3881
                DRH => get_lang('Drh'),
3882
            ],
3883
            ['id' => 'profile']
3884
        );
3885
3886
        $form->addElement(
3887
            'select_ajax',
3888
            'student_id',
3889
            get_lang('SearchUsers'),
3890
            $studentList,
3891
            [
3892
                'placeholder' => get_lang('All'),
3893
                'url_function' => "
3894
                    function () {
3895
                        var params = $.param({
3896
                            a: 'search_user_by_course',
3897
                            session_id: $('#access_overview_session_id').val(),
3898
                            course_id: $('#access_overview_course_id').val()
3899
                        });
3900
3901
                        return '".api_get_path(WEB_AJAX_PATH)."course.ajax.php?' + params;
3902
                    }
3903
                ",
3904
            ]
3905
        );
3906
        $form->addDateRangePicker(
3907
            'date',
3908
            get_lang('DateRange'),
3909
            true,
3910
            [
3911
                'id' => 'date_range',
3912
                'format' => 'YYYY-MM-DD',
3913
                'timePicker' => 'false',
3914
                'validate_format' => 'Y-m-d',
3915
            ]
3916
        );
3917
        $form->addHidden('display', 'accessoverview');
3918
        $form->addRule('course_id', get_lang('Required'), 'required');
3919
        $form->addRule('profile', get_lang('Required'), 'required');
3920
        $form->addButton('submit', get_lang('Generate'), 'gear', 'primary');
3921
3922
        $table = null;
3923
3924
        if ($form->validate()) {
3925
            $table = new SortableTable(
3926
                'tracking_access_overview',
3927
                ['MySpace', 'getNumberOfTrackAccessOverview'],
3928
                ['MySpace', 'getUserDataAccessTrackingOverview'],
3929
                0
3930
            );
3931
            $table->set_header(0, get_lang('LoginDate'), true);
3932
            $table->set_header(1, get_lang('Username'), true);
3933
            if (api_is_western_name_order()) {
3934
                $table->set_header(2, get_lang('FirstName'), true);
3935
                $table->set_header(3, get_lang('LastName'), true);
3936
            } else {
3937
                $table->set_header(2, get_lang('LastName'), true);
3938
                $table->set_header(3, get_lang('FirstName'), true);
3939
            }
3940
            $table->set_header(4, get_lang('Clicks'), false);
3941
            $table->set_header(5, get_lang('IP'), false);
3942
            $table->set_header(6, get_lang('TimeLoggedIn'), false);
3943
        }
3944
3945
        $template = new Template(
3946
            null,
3947
            false,
3948
            false,
3949
            false,
3950
            false,
3951
            false,
3952
            false
3953
        );
3954
        $template->assign('form', $form->returnForm());
3955
        $template->assign('table', $table ? $table->return_table() : null);
3956
3957
        echo $template->fetch(
3958
            $template->get_template('my_space/accessoverview.tpl')
3959
        );
3960
    }
3961
3962
    /**
3963
     * @return int
3964
     */
3965
    public static function getNumberOfTrackAccessOverview()
3966
    {
3967
        $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
3968
        $sql = "SELECT COUNT(course_access_id) count FROM $table";
3969
        $result = Database::query($sql);
3970
        $row = Database::fetch_assoc($result);
3971
3972
        return $row['count'];
3973
    }
3974
3975
    /**
3976
     * @param $from
3977
     * @param $numberItems
3978
     * @param $column
3979
     * @param $orderDirection
3980
     *
3981
     * @return array
3982
     */
3983
    public static function getUserDataAccessTrackingOverview(
3984
        $from,
3985
        $numberItems,
3986
        $column,
3987
        $orderDirection
3988
    ) {
3989
        $from = (int) $from;
3990
        $numberItems = (int) $numberItems;
3991
        $column = (int) $column;
3992
        $orderDirection = Database::escape_string($orderDirection);
3993
        $orderDirection = !in_array(strtolower(trim($orderDirection)), ['asc', 'desc']) ? 'asc' : $orderDirection;
3994
3995
        $user = Database::get_main_table(TABLE_MAIN_USER);
3996
        $course = Database::get_main_table(TABLE_MAIN_COURSE);
3997
        $track_e_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
3998
        $track_e_course_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
3999
4000
        global $export_csv;
4001
        $is_western_name_order = api_is_western_name_order();
4002
        if ($export_csv) {
4003
            $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
4004
        }
4005
4006
        //TODO add course name
4007
        $sql = "SELECT
4008
                a.login_course_date as col0,
4009
                u.username as col1,
4010
                ".(
4011
                    $is_western_name_order ? "
4012
                        u.firstname AS col2,
4013
                        u.lastname AS col3,
4014
                    " : "
4015
                        u.lastname AS col2,
4016
                        u.firstname AS col3,
4017
                "
4018
        )."
4019
                a.logout_course_date,
4020
                c.title,
4021
                c.code,
4022
                u.user_id
4023
            FROM $track_e_course_access a
4024
            INNER JOIN $user u ON a.user_id = u.user_id
4025
            INNER JOIN $course c ON a.c_id = c.id
4026
            WHERE 1=1 ";
4027
4028
        if (isset($_GET['course_id']) && !empty($_GET['course_id'])) {
4029
            $courseId = (int) $_GET['course_id'];
4030
            $sql .= " AND c.id = ".$courseId;
4031
        }
4032
4033
        if (isset($_GET['session_id']) && !empty($_GET['session_id'])) {
4034
            $sessionId = (int) $_GET['session_id'];
4035
            $sql .= " AND a.session_id = ".$sessionId;
4036
        }
4037
4038
        if (isset($_GET['student_id']) && !empty($_GET['student_id'])) {
4039
            $userId = (int) $_GET['student_id'];
4040
            $sql .= " AND u.user_id = ".$userId;
4041
        }
4042
4043
        if (isset($_GET['date']) && !empty($_GET['date'])) {
4044
            $dates = DateRangePicker::parseDateRange($_GET['date']);
4045
            if (isset($dates['start']) && !empty($dates['start'])) {
4046
                $dates['start'] = Database::escape_string($dates['start']);
4047
                $sql .= " AND login_course_date >= '".$dates['start']."'";
4048
            }
4049
            if (isset($dates['end']) && !empty($dates['end'])) {
4050
                $dates['end'] = Database::escape_string($dates['end']);
4051
                $sql .= " AND logout_course_date <= '".$dates['end']."'";
4052
            }
4053
        }
4054
4055
        $sql .= " ORDER BY col$column $orderDirection ";
4056
        $sql .= " LIMIT $from,$numberItems";
4057
4058
        $result = Database::query($sql);
4059
4060
        $data = [];
4061
        while ($user = Database::fetch_assoc($result)) {
4062
            $data[] = $user;
4063
        }
4064
4065
        $return = [];
4066
        //TODO: Dont use numeric index
4067
        foreach ($data as $key => $info) {
4068
            $start_date = $info['col0'];
4069
            $end_date = $info['logout_course_date'];
4070
4071
            $return[$info['user_id']] = [
4072
                $start_date,
4073
                $info['col1'],
4074
                $info['col2'],
4075
                $info['col3'],
4076
                $info['user_id'],
4077
                'ip',
4078
                //TODO is not correct/precise, it counts the time not logged between two loggins
4079
                gmdate("H:i:s", strtotime($end_date) - strtotime($start_date)),
4080
            ];
4081
        }
4082
4083
        foreach ($return as $key => $info) {
4084
            $ipResult = Database::select(
4085
                'user_ip',
4086
                $track_e_login,
4087
                ['where' => [
4088
                    '? BETWEEN login_date AND logout_date' => $info[0],
4089
                ]],
4090
                'first'
4091
            );
4092
4093
            $return[$key][5] = $ipResult['user_ip'];
4094
        }
4095
4096
        return $return;
4097
    }
4098
4099
    /**
4100
     * Gets the connections to a course as an array of login and logout time.
4101
     *
4102
     * @param int    $user_id
4103
     * @param array  $course_info
4104
     * @param int    $sessionId
4105
     * @param string $start_date
4106
     * @param string $end_date
4107
     *
4108
     * @author  Jorge Frisancho Jibaja
4109
     * @author  Julio Montoya <[email protected]> fixing the function
4110
     *
4111
     * @version OCT-22- 2010
4112
     *
4113
     * @return array
4114
     */
4115
    public static function get_connections_to_course_by_date(
4116
        $user_id,
4117
        $course_info,
4118
        $sessionId,
4119
        $start_date,
4120
        $end_date
4121
    ) {
4122
        $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
4123
        $user_id = (int) $user_id;
4124
        $connections = [];
4125
        if (!empty($course_info)) {
4126
            $courseId = (int) $course_info['real_id'];
4127
            $end_date = add_day_to($end_date);
4128
4129
            $start_date = Database::escape_string($start_date);
4130
            $end_date = Database::escape_string($end_date);
4131
            $sessionCondition = api_get_session_condition($sessionId);
4132
            $sql = "SELECT
4133
                        login_course_date,
4134
                        logout_course_date,
4135
                        TIMESTAMPDIFF(SECOND, login_course_date, logout_course_date) duration
4136
                    FROM $table
4137
                    WHERE
4138
                        user_id = $user_id AND
4139
                        c_id = $courseId AND
4140
                        login_course_date BETWEEN '$start_date' AND '$end_date' AND
4141
                        logout_course_date BETWEEN '$start_date' AND '$end_date'
4142
                        $sessionCondition
4143
                    ORDER BY login_course_date ASC";
4144
            $rs = Database::query($sql);
4145
4146
            while ($row = Database::fetch_array($rs)) {
4147
                $connections[] = [
4148
                    'login' => $row['login_course_date'],
4149
                    'logout' => $row['logout_course_date'],
4150
                    'duration' => $row['duration'],
4151
                ];
4152
            }
4153
        }
4154
4155
        return $connections;
4156
    }
4157
}
4158
4159
/**
4160
 * @param $user_id
4161
 * @param array $course_info
4162
 * @param int   $sessionId
4163
 * @param null  $start_date
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $start_date is correct as it would always require null to be passed?
Loading history...
4164
 * @param null  $end_date
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $end_date is correct as it would always require null to be passed?
Loading history...
4165
 *
4166
 * @return array
4167
 */
4168
function get_stats($user_id, $course_info, $sessionId, $start_date = null, $end_date = null)
4169
{
4170
    $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
4171
    $result = [];
4172
    if (!empty($course_info)) {
4173
        $stringStartDate = '';
4174
        $stringEndDate = '';
4175
        if ($start_date != null && $end_date != null) {
4176
            $end_date = add_day_to($end_date);
4177
4178
            $start_date = Database::escape_string($start_date);
4179
            $end_date = Database::escape_string($end_date);
4180
4181
            $stringStartDate = "AND login_course_date BETWEEN '$start_date' AND '$end_date'";
4182
            $stringEndDate = "AND logout_course_date BETWEEN '$start_date' AND '$end_date'";
4183
        }
4184
        $user_id = (int) $user_id;
4185
        $courseId = (int) $course_info['real_id'];
4186
        $sessionCondition = api_get_session_condition($sessionId);
4187
        $sql = "SELECT
4188
                SEC_TO_TIME(AVG(time_to_sec(timediff(logout_course_date,login_course_date)))) as avrg,
4189
                SEC_TO_TIME(SUM(time_to_sec(timediff(logout_course_date,login_course_date)))) as total,
4190
                count(user_id) as times
4191
                FROM $table
4192
                WHERE
4193
                    user_id = $user_id AND
4194
                    c_id = $courseId $stringStartDate $stringEndDate
4195
                    $sessionCondition
4196
                ORDER BY login_course_date ASC";
4197
4198
        $rs = Database::query($sql);
4199
        if ($row = Database::fetch_array($rs)) {
4200
            $foo_avg = $row['avrg'];
4201
            $foo_total = $row['total'];
4202
            $foo_times = $row['times'];
4203
            $result = [
4204
                'avg' => $foo_avg,
4205
                'total' => $foo_total,
4206
                'times' => $foo_times,
4207
            ];
4208
        }
4209
    }
4210
4211
    return $result;
4212
}
4213
4214
function add_day_to($end_date)
4215
{
4216
    $foo_date = strtotime($end_date);
4217
    $foo_date = strtotime(' +1 day', $foo_date);
4218
    $foo_date = date('Y-m-d', $foo_date);
4219
4220
    return $foo_date;
4221
}
4222
4223
/**
4224
 * Converte an array to a table in html.
4225
 *
4226
 * @param array $result
4227
 *
4228
 * @author Jorge Frisancho Jibaja
4229
 *
4230
 * @version OCT-22- 2010
4231
 *
4232
 * @return string
4233
 */
4234
function convert_to_string($result)
4235
{
4236
    $html = '<table class="table">';
4237
    if (!empty($result)) {
4238
        foreach ($result as $key => $data) {
4239
            $html .= '<tr><td>';
4240
            $html .= api_get_local_time($data['login']);
4241
            $html .= '</td>';
4242
            $html .= '<td>';
4243
4244
            $html .= api_time_to_hms(api_strtotime($data['logout']) - api_strtotime($data['login']));
4245
            $html .= '</tr></td>';
4246
        }
4247
    }
4248
    $html .= '</table>';
4249
4250
    return $html;
4251
}
4252
4253
/**
4254
 * This function draw the graphic to be displayed on the user view as an image.
4255
 *
4256
 * @param array  $sql_result
4257
 * @param string $start_date
4258
 * @param string $end_date
4259
 * @param string $type
4260
 *
4261
 * @author Jorge Frisancho Jibaja
4262
 *
4263
 * @version OCT-22- 2010
4264
 *
4265
 * @return string
4266
 */
4267
function grapher($sql_result, $start_date, $end_date, $type = '')
4268
{
4269
    if (empty($start_date)) {
4270
        $start_date = '';
4271
    }
4272
    if (empty($end_date)) {
4273
        $end_date = '';
4274
    }
4275
    if ($type == '') {
4276
        $type = 'day';
4277
    }
4278
    $main_year = $main_month_year = $main_day = [];
4279
4280
    $period = new DatePeriod(
4281
        new DateTime($start_date),
4282
        new DateInterval('P1D'),
4283
        new DateTime($end_date)
4284
    );
4285
4286
    foreach ($period as $date) {
4287
        $main_day[$date->format('d-m-Y')] = 0;
4288
    }
4289
4290
    $period = new DatePeriod(
4291
        new DateTime($start_date),
4292
        new DateInterval('P1M'),
4293
        new DateTime($end_date)
4294
    );
4295
4296
    foreach ($period as $date) {
4297
        $main_month_year[$date->format('m-Y')] = 0;
4298
    }
4299
4300
    $i = 0;
4301
    if (is_array($sql_result) && count($sql_result) > 0) {
4302
        foreach ($sql_result as $key => $data) {
4303
            $login = api_strtotime($data['login']);
4304
            $logout = api_strtotime($data['logout']);
4305
            //creating the main array
4306
            if (isset($main_month_year[date('m-Y', $login)])) {
4307
                $main_month_year[date('m-Y', $login)] += float_format(($logout - $login) / 60, 0);
4308
            }
4309
            if (isset($main_day[date('d-m-Y', $login)])) {
4310
                $main_day[date('d-m-Y', $login)] += float_format(($logout - $login) / 60, 0);
4311
            }
4312
            if ($i > 500) {
4313
                break;
4314
            }
4315
            $i++;
4316
        }
4317
        switch ($type) {
4318
            case 'day':
4319
                $main_date = $main_day;
4320
                break;
4321
            case 'month':
4322
                $main_date = $main_month_year;
4323
                break;
4324
            case 'year':
4325
                $main_date = $main_year;
4326
                break;
4327
        }
4328
4329
        $labels = array_keys($main_date);
4330
        if (count($main_date) == 1) {
4331
            $labels = $labels[0];
4332
            $main_date = $main_date[$labels];
4333
        }
4334
4335
        /* Create and populate the pData object */
4336
        $myData = new pData();
4337
        $myData->addPoints($main_date, 'Serie1');
4338
        if (count($main_date) != 1) {
4339
            $myData->addPoints($labels, 'Labels');
4340
            $myData->setSerieDescription('Labels', 'Months');
4341
            $myData->setAbscissa('Labels');
4342
        }
4343
        $myData->setSerieWeight('Serie1', 1);
4344
        $myData->setSerieDescription('Serie1', get_lang('MyResults'));
4345
        $myData->setAxisName(0, get_lang('Minutes'));
4346
        $myData->loadPalette(api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color', true);
4347
4348
        // Cache definition
4349
        $cachePath = api_get_path(SYS_ARCHIVE_PATH);
4350
        $myCache = new pCache(['CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)]);
4351
        $chartHash = $myCache->getHash($myData);
4352
4353
        if ($myCache->isInCache($chartHash)) {
4354
            //if we already created the img
4355
            $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
4356
            $myCache->saveFromCache($chartHash, $imgPath);
4357
            $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
4358
        } else {
4359
            /* Define width, height and angle */
4360
            $mainWidth = 760;
4361
            $mainHeight = 230;
4362
            $angle = 50;
4363
4364
            /* Create the pChart object */
4365
            $myPicture = new pImage($mainWidth, $mainHeight, $myData);
4366
4367
            /* Turn of Antialiasing */
4368
            $myPicture->Antialias = false;
4369
            /* Draw the background */
4370
            $settings = ["R" => 255, "G" => 255, "B" => 255];
4371
            $myPicture->drawFilledRectangle(0, 0, $mainWidth, $mainHeight, $settings);
4372
4373
            /* Add a border to the picture */
4374
            $myPicture->drawRectangle(
4375
                0,
4376
                0,
4377
                $mainWidth - 1,
4378
                $mainHeight - 1,
4379
                ["R" => 0, "G" => 0, "B" => 0]
4380
            );
4381
4382
            /* Set the default font */
4383
            $myPicture->setFontProperties(
4384
                [
4385
                    "FontName" => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',
4386
                    "FontSize" => 10, ]
4387
            );
4388
            /* Write the chart title */
4389
            $myPicture->drawText(
4390
                $mainWidth / 2,
4391
                30,
4392
                get_lang('TimeSpentInTheCourse'),
4393
                [
4394
                    "FontSize" => 12,
4395
                    "Align" => TEXT_ALIGN_BOTTOMMIDDLE,
4396
                ]
4397
            );
4398
4399
            /* Set the default font */
4400
            $myPicture->setFontProperties(
4401
                [
4402
                    "FontName" => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',
4403
                    "FontSize" => 8,
4404
                ]
4405
            );
4406
4407
            /* Define the chart area */
4408
            $myPicture->setGraphArea(50, 40, $mainWidth - 40, $mainHeight - 80);
4409
4410
            /* Draw the scale */
4411
            $scaleSettings = [
4412
                'XMargin' => 10,
4413
                'YMargin' => 10,
4414
                'Floating' => true,
4415
                'GridR' => 200,
4416
                'GridG' => 200,
4417
                'GridB' => 200,
4418
                'DrawSubTicks' => true,
4419
                'CycleBackground' => true,
4420
                'LabelRotation' => $angle,
4421
                'Mode' => SCALE_MODE_ADDALL_START0,
4422
            ];
4423
            $myPicture->drawScale($scaleSettings);
4424
4425
            /* Turn on Antialiasing */
4426
            $myPicture->Antialias = true;
4427
4428
            /* Enable shadow computing */
4429
            $myPicture->setShadow(
4430
                true,
4431
                [
4432
                    "X" => 1,
4433
                    "Y" => 1,
4434
                    "R" => 0,
4435
                    "G" => 0,
4436
                    "B" => 0,
4437
                    "Alpha" => 10,
4438
                ]
4439
            );
4440
4441
            /* Draw the line chart */
4442
            $myPicture->setFontProperties(
4443
                [
4444
                    "FontName" => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',
4445
                    "FontSize" => 10,
4446
                ]
4447
            );
4448
            $myPicture->drawSplineChart();
4449
            $myPicture->drawPlotChart(
4450
                [
4451
                    "DisplayValues" => true,
4452
                    "PlotBorder" => true,
4453
                    "BorderSize" => 1,
4454
                    "Surrounding" => -60,
4455
                    "BorderAlpha" => 80,
4456
                ]
4457
            );
4458
4459
            /* Do NOT Write the chart legend */
4460
4461
            /* Write and save into cache */
4462
            $myCache->writeToCache($chartHash, $myPicture);
4463
            $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
4464
            $myCache->saveFromCache($chartHash, $imgPath);
4465
            $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
4466
        }
4467
        $html = '<img src="'.$imgPath.'">';
4468
4469
        return $html;
4470
    } else {
4471
        $foo_img = api_convert_encoding(
4472
            '<div id="messages" class="warning-message">'.get_lang('GraphicNotAvailable').'</div>',
4473
            'UTF-8'
4474
        );
4475
4476
        return $foo_img;
4477
    }
4478
}
4479