Completed
Push — master ( f41061...196bfc )
by Julito
09:03
created

SessionManager::getCountUsersInCourseSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 22
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Entity\Course;
5
use Chamilo\CoreBundle\Entity\ExtraField;
6
use Chamilo\CoreBundle\Entity\SequenceResource;
7
use Chamilo\CoreBundle\Entity\Session;
8
use Chamilo\CoreBundle\Entity\SessionRelCourse;
9
use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser;
10
use Chamilo\CoreBundle\Entity\SessionRelUser;
11
use Chamilo\CoreBundle\Repository\SequenceRepository;
12
use Chamilo\UserBundle\Entity\User;
13
use ExtraField as ExtraFieldModel;
14
use Monolog\Logger;
15
16
/**
17
 * Class SessionManager.
18
 *
19
 * This is the session library for Chamilo
20
 * (as in courses>session, not as in PHP session)
21
 * All main sessions functions should be placed here.
22
 * This class provides methods for sessions management.
23
 * Include/require it in your code to use its features.
24
 *
25
 * @package chamilo.library
26
 */
27
class SessionManager
28
{
29
    // See BT#4871
30
    public const SESSION_CHANGE_USER_REASON_SCHEDULE = 1;
31
    public const SESSION_CHANGE_USER_REASON_CLASSROOM = 2;
32
    public const SESSION_CHANGE_USER_REASON_LOCATION = 3;
33
    public const SESSION_CHANGE_USER_REASON_ENROLLMENT_ANNULATION = 4;
34
    public const DEFAULT_VISIBILITY = 4;  //SESSION_AVAILABLE
35
36
    public static $_debug = false;
37
38
    /**
39
     * Constructor.
40
     */
41
    public function __construct()
42
    {
43
    }
44
45
    /**
46
     * Fetches a session from the database.
47
     *
48
     * @param int $id Session Id
49
     *
50
     * @return array Session details
51
     */
52
    public static function fetch($id)
53
    {
54
        $em = Database::getManager();
55
56
        if (empty($id)) {
57
            return [];
58
        }
59
60
        /** @var Session $session */
61
        $session = $em->find('ChamiloCoreBundle:Session', $id);
62
63
        if (!$session) {
0 ignored issues
show
introduced by
$session is of type Chamilo\CoreBundle\Entity\Session, thus it always evaluated to true.
Loading history...
64
            return [];
65
        }
66
67
        $result = [
68
            'id' => $session->getId(),
69
            'id_coach' => $session->getGeneralCoach() ? $session->getGeneralCoach()->getId() : null,
70
            'session_category_id' => $session->getCategory() ? $session->getCategory()->getId() : null,
71
            'name' => $session->getName(),
72
            'description' => $session->getDescription(),
73
            'show_description' => $session->getShowDescription(),
74
            'duration' => $session->getDuration(),
75
            'nbr_courses' => $session->getNbrCourses(),
76
            'nbr_users' => $session->getNbrUsers(),
77
            'nbr_classes' => $session->getNbrClasses(),
78
            'session_admin_id' => $session->getSessionAdminId(),
79
            'visibility' => $session->getVisibility(),
80
            'promotion_id' => $session->getPromotionId(),
81
            'display_start_date' => $session->getDisplayStartDate()
82
                ? $session->getDisplayStartDate()->format('Y-m-d H:i:s')
83
                : null,
84
            'display_end_date' => $session->getDisplayEndDate()
85
                ? $session->getDisplayEndDate()->format('Y-m-d H:i:s')
86
                : null,
87
            'access_start_date' => $session->getAccessStartDate()
88
                ? $session->getAccessStartDate()->format('Y-m-d H:i:s')
89
                : null,
90
            'access_end_date' => $session->getAccessEndDate()
91
                ? $session->getAccessEndDate()->format('Y-m-d H:i:s')
92
                : null,
93
            'coach_access_start_date' => $session->getCoachAccessStartDate()
94
                ? $session->getCoachAccessStartDate()->format('Y-m-d H:i:s')
95
                : null,
96
            'coach_access_end_date' => $session->getCoachAccessEndDate()
97
                ? $session->getCoachAccessEndDate()->format('Y-m-d H:i:s')
98
                : null,
99
            'send_subscription_notification' => $session->getSendSubscriptionNotification(),
100
        ];
101
102
        // Converted to local values
103
        $variables = [
104
            'display_start_date',
105
            'display_end_date',
106
            'access_start_date',
107
            'access_end_date',
108
            'coach_access_start_date',
109
            'coach_access_end_date',
110
        ];
111
112
        foreach ($variables as $value) {
113
            $result[$value.'_to_local_time'] = null;
114
            if (!empty($result[$value])) {
115
                $result[$value.'_to_local_time'] = api_get_local_time($result[$value]);
116
            }
117
        }
118
119
        return $result;
120
    }
121
122
    /**
123
     * Create a session.
124
     *
125
     * @author Carlos Vargas <[email protected]>, from existing code
126
     *
127
     * @param string   $name
128
     * @param string   $startDate                    (YYYY-MM-DD hh:mm:ss)
129
     * @param string   $endDate                      (YYYY-MM-DD hh:mm:ss)
130
     * @param string   $displayStartDate             (YYYY-MM-DD hh:mm:ss)
131
     * @param string   $displayEndDate               (YYYY-MM-DD hh:mm:ss)
132
     * @param string   $coachStartDate               (YYYY-MM-DD hh:mm:ss)
133
     * @param string   $coachEndDate                 (YYYY-MM-DD hh:mm:ss)
134
     * @param mixed    $coachId                      If int, this is the session coach id,
135
     *                                               if string, the coach ID will be looked for from the user table
136
     * @param int      $sessionCategoryId            ID of the session category in which this session is registered
137
     * @param int      $visibility                   Visibility after end date (0 = read-only, 1 = invisible, 2 = accessible)
138
     * @param bool     $fixSessionNameIfExists
139
     * @param string   $duration
140
     * @param string   $description                  Optional. The session description
141
     * @param int      $showDescription              Optional. Whether show the session description
142
     * @param array    $extraFields
143
     * @param int      $sessionAdminId               Optional. If this sessions was created by a session admin, assign it to him
144
     * @param bool     $sendSubscriptionNotification Optional.
145
     *                                               Whether send a mail notification to users being subscribed
146
     * @param int|null $accessUrlId                  Optional.
147
     *
148
     * @return mixed Session ID on success, error message otherwise
149
     *
150
     * @todo use an array to replace all this parameters or use the model.lib.php ...
151
     *
152
     * @return mixed Session ID on success, error message otherwise
153
     * */
154
    public static function create_session(
155
        $name,
156
        $startDate,
157
        $endDate,
158
        $displayStartDate,
159
        $displayEndDate,
160
        $coachStartDate,
161
        $coachEndDate,
162
        $coachId,
163
        $sessionCategoryId,
164
        $visibility = 1,
165
        $fixSessionNameIfExists = false,
166
        $duration = null,
167
        $description = null,
168
        $showDescription = 0,
169
        $extraFields = [],
170
        $sessionAdminId = 0,
171
        $sendSubscriptionNotification = false,
172
        $accessUrlId = null
173
    ) {
174
        global $_configuration;
175
176
        // Check portal limits
177
        $accessUrlId = api_is_multiple_url_enabled()
178
            ? (empty($accessUrlId) ? api_get_current_access_url_id() : (int) $accessUrlId)
179
            : 1;
180
181
        if (is_array($_configuration[$accessUrlId]) &&
182
            isset($_configuration[$accessUrlId]['hosting_limit_sessions']) &&
183
            $_configuration[$accessUrlId]['hosting_limit_sessions'] > 0
184
        ) {
185
            $num = self::count_sessions();
186
            if ($num >= $_configuration[$accessUrlId]['hosting_limit_sessions']) {
187
                api_warn_hosting_contact('hosting_limit_sessions');
188
189
                return get_lang('PortalSessionsLimitReached');
190
            }
191
        }
192
193
        $name = Database::escape_string(trim($name));
194
        $sessionCategoryId = (int) $sessionCategoryId;
195
        $visibility = (int) $visibility;
196
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
197
198
        $startDate = Database::escape_string($startDate);
199
        $endDate = Database::escape_string($endDate);
200
201
        if (empty($name)) {
202
            $msg = get_lang('SessionNameIsRequired');
203
204
            return $msg;
205
        } elseif (!empty($startDate) && !api_is_valid_date($startDate, 'Y-m-d H:i') &&
206
            !api_is_valid_date($startDate, 'Y-m-d H:i:s')
207
        ) {
208
            $msg = get_lang('InvalidStartDate');
209
210
            return $msg;
211
        } elseif (!empty($endDate) && !api_is_valid_date($endDate, 'Y-m-d H:i') &&
212
            !api_is_valid_date($endDate, 'Y-m-d H:i:s')
213
        ) {
214
            $msg = get_lang('InvalidEndDate');
215
216
            return $msg;
217
        } elseif (!empty($startDate) && !empty($endDate) && $startDate >= $endDate) {
218
            $msg = get_lang('StartDateShouldBeBeforeEndDate');
219
220
            return $msg;
221
        } else {
222
            $ready_to_create = false;
223
            if ($fixSessionNameIfExists) {
224
                $name = self::generateNextSessionName($name);
225
                if ($name) {
226
                    $ready_to_create = true;
227
                } else {
228
                    $msg = get_lang('SessionNameAlreadyExists');
229
230
                    return $msg;
231
                }
232
            } else {
233
                $rs = Database::query("SELECT 1 FROM $tbl_session WHERE name='".$name."'");
234
                if (Database::num_rows($rs)) {
235
                    $msg = get_lang('SessionNameAlreadyExists');
236
237
                    return $msg;
238
                }
239
                $ready_to_create = true;
240
            }
241
242
            if ($ready_to_create) {
243
                $sessionAdminId = !empty($sessionAdminId) ? $sessionAdminId : api_get_user_id();
244
                $values = [
245
                    'name' => $name,
246
                    'id_coach' => $coachId,
247
                    'session_admin_id' => $sessionAdminId,
248
                    'visibility' => $visibility,
249
                    'description' => $description,
250
                    'show_description' => $showDescription,
251
                    'send_subscription_notification' => (int) $sendSubscriptionNotification,
252
                ];
253
254
                if (!empty($startDate)) {
255
                    $values['access_start_date'] = api_get_utc_datetime($startDate);
256
                }
257
258
                if (!empty($endDate)) {
259
                    $values['access_end_date'] = api_get_utc_datetime($endDate);
260
                }
261
262
                if (!empty($displayStartDate)) {
263
                    $values['display_start_date'] = api_get_utc_datetime($displayStartDate);
264
                }
265
266
                if (!empty($displayEndDate)) {
267
                    $values['display_end_date'] = api_get_utc_datetime($displayEndDate);
268
                }
269
270
                if (!empty($coachStartDate)) {
271
                    $values['coach_access_start_date'] = api_get_utc_datetime($coachStartDate);
272
                }
273
                if (!empty($coachEndDate)) {
274
                    $values['coach_access_end_date'] = api_get_utc_datetime($coachEndDate);
275
                }
276
277
                if (!empty($sessionCategoryId)) {
278
                    $values['session_category_id'] = $sessionCategoryId;
279
                }
280
281
                $values['position'] = 0;
282
                $session_id = Database::insert($tbl_session, $values);
283
                $duration = (int) $duration;
284
285
                if (!empty($duration)) {
286
                    $sql = "UPDATE $tbl_session SET
287
                        access_start_date = NULL,
288
                        access_end_date = NULL,
289
                        display_start_date = NULL,
290
                        display_end_date = NULL,
291
                        coach_access_start_date = NULL,
292
                        coach_access_end_date = NULL,
293
                        duration = $duration
294
                    WHERE id = $session_id";
295
                    Database::query($sql);
296
                } else {
297
                    $sql = "UPDATE $tbl_session
298
                        SET duration = 0
299
                        WHERE id = $session_id";
300
                    Database::query($sql);
301
                }
302
303
                if (!empty($session_id)) {
304
                    $extraFields['item_id'] = $session_id;
305
                    $sessionFieldValue = new ExtraFieldValue('session');
306
                    $sessionFieldValue->saveFieldValues($extraFields);
307
308
                    /*
309
                      Sends a message to the user_id = 1
310
311
                      $user_info = api_get_user_info(1);
312
                      $complete_name = $user_info['firstname'].' '.$user_info['lastname'];
313
                      $subject = api_get_setting('siteName').' - '.get_lang('ANewSessionWasCreated');
314
                      $message = get_lang('ANewSessionWasCreated')." <br /> ".get_lang('NameOfTheSession').' : '.$name;
315
                      api_mail_html($complete_name, $user_info['email'], $subject, $message);
316
                     *
317
                     */
318
                    // Adding to the correct URL
319
                    UrlManager::add_session_to_url($session_id, $accessUrlId);
320
321
                    // add event to system log
322
                    $user_id = api_get_user_id();
323
                    Event::addEvent(
324
                        LOG_SESSION_CREATE,
325
                        LOG_SESSION_ID,
326
                        $session_id,
327
                        api_get_utc_datetime(),
328
                        $user_id
329
                    );
330
                }
331
332
                return $session_id;
333
            }
334
        }
335
    }
336
337
    /**
338
     * @param string $name
339
     *
340
     * @return bool
341
     */
342
    public static function sessionNameExists($name)
343
    {
344
        $name = Database::escape_string($name);
345
        $sql = "SELECT COUNT(*) as count FROM ".Database::get_main_table(TABLE_MAIN_SESSION)."
346
                WHERE name = '$name'";
347
        $result = Database::fetch_array(Database::query($sql));
348
349
        return $result['count'] > 0;
350
    }
351
352
    /**
353
     * @param string $where_condition
354
     *
355
     * @return mixed
356
     */
357
    public static function get_count_admin($where_condition = '')
358
    {
359
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
360
        $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
361
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
362
        $table_access_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
363
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
364
365
        $where = 'WHERE 1=1 ';
366
        $user_id = api_get_user_id();
367
        $extraJoin = '';
368
369
        if (api_is_session_admin() &&
370
            api_get_setting('allow_session_admins_to_manage_all_sessions') == 'false'
371
        ) {
372
            $where .= " AND (
373
                            s.session_admin_id = $user_id  OR
374
                            sru.user_id = '$user_id' AND
375
                            sru.relation_type = '".SESSION_RELATION_TYPE_RRHH."'
376
                            )
377
                      ";
378
379
            $extraJoin = " INNER JOIN $tbl_session_rel_user sru
380
                           ON sru.session_id = s.id ";
381
        }
382
383
        $today = api_get_utc_datetime();
384
        $today = api_strtotime($today, 'UTC');
385
        $today = date('Y-m-d', $today);
386
387
        if (!empty($where_condition)) {
388
            $where_condition = str_replace("(  session_active = ':'  )", '1=1', $where_condition);
389
390
            $where_condition = str_replace('category_name', 'sc.name', $where_condition);
391
            $where_condition = str_replace(
392
                ["AND session_active = '1'  )", " AND (  session_active = '1'  )"],
393
                [') GROUP BY s.name HAVING session_active = 1 ', " GROUP BY s.name HAVING session_active = 1 "],
394
                $where_condition
395
            );
396
            $where_condition = str_replace(
397
                ["AND session_active = '0'  )", " AND (  session_active = '0'  )"],
398
                [') GROUP BY s.name HAVING session_active = 0 ', " GROUP BY s.name HAVING session_active = '0' "],
399
                $where_condition
400
            );
401
        } else {
402
            $where_condition = " AND 1 = 1";
403
        }
404
405
        $courseCondition = null;
406
        if (strpos($where_condition, 'c.id')) {
407
            $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
408
            $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE);
409
            $courseCondition = " INNER JOIN $table course_rel_session
410
                                 ON (s.id = course_rel_session.session_id)
411
                                 INNER JOIN $tableCourse c
412
                                 ON (course_rel_session.c_id = c.id)
413
                                ";
414
        }
415
416
        $sql = "SELECT COUNT(id) as total_rows FROM (
417
                SELECT DISTINCT
418
                 IF (
419
					(s.access_start_date <= '$today' AND '$today' <= s.access_end_date) OR
420
                    (s.access_start_date IS NULL AND s.access_end_date  = IS NULL ) OR
421
					(s.access_start_date <= '$today' AND s.access_end_date IS NULL) OR
422
					('$today' <= s.access_end_date AND s.access_start_date IS NULL)
423
				, 1, 0) as session_active,
424
                s.id
425
                FROM $tbl_session s
426
                LEFT JOIN $tbl_session_category sc
427
                ON s.session_category_id = sc.id
428
                INNER JOIN $tbl_user u
429
                ON s.id_coach = u.user_id
430
                $courseCondition
431
                $extraJoin
432
                $where $where_condition ) as session_table";
433
434
        if (api_is_multiple_url_enabled()) {
435
            $access_url_id = api_get_current_access_url_id();
436
            if ($access_url_id != -1) {
437
                $where .= " AND ar.access_url_id = $access_url_id ";
438
439
                $sql = "SELECT count(id) as total_rows FROM (
440
                SELECT DISTINCT
441
                  IF (
442
					(s.access_start_date <= '$today' AND '$today' <= s.access_end_date) OR
443
                    (s.access_start_date IS NULL AND s.access_end_date IS NULL) OR
444
					(s.access_start_date <= '$today' AND s.access_end_date IS NULL) OR
445
					('$today' <= s.access_end_date AND s.access_start_date IS NULL)
446
				, 1, 0)
447
				as session_active,
448
				s.id
449
                FROM $tbl_session s
450
                    LEFT JOIN  $tbl_session_category sc
451
                    ON s.session_category_id = sc.id
452
                    INNER JOIN $tbl_user u ON s.id_coach = u.user_id
453
                    INNER JOIN $table_access_url_rel_session ar
454
                    ON ar.session_id = s.id
455
                    $courseCondition
456
                    $extraJoin
457
                $where $where_condition) as session_table";
458
            }
459
        }
460
461
        $result_rows = Database::query($sql);
462
        $row = Database::fetch_array($result_rows);
463
        $num = $row['total_rows'];
464
465
        return $num;
466
    }
467
468
    /**
469
     * Get session list for a session admin or platform admin.
470
     *
471
     * @param int   $userId   User Id for the session admin.
472
     * @param array $options  Optional. Order and limit keys.
473
     * @param bool  $getCount Optional. Whether to get all the results or only the count.
474
     * @param array $columns  Optional. Columns from jqGrid.
475
     *
476
     * @return array
477
     */
478
    public static function getSessionsForAdmin(
479
        $userId,
480
        $options = [],
481
        $getCount = false,
482
        $columns = []
483
    ) {
484
        $tblSession = Database::get_main_table(TABLE_MAIN_SESSION);
485
        $sessionCategoryTable = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
486
487
        $where = 'WHERE 1 = 1 ';
488
489
        $userId = (int) $userId;
490
491
        if (!api_is_platform_admin()) {
492
            if (api_is_session_admin() &&
493
                api_get_setting('allow_session_admins_to_manage_all_sessions') == 'false'
494
            ) {
495
                $where .= " AND s.session_admin_id = $userId ";
496
            }
497
        }
498
499
        if (!api_is_platform_admin() &&
500
            api_is_teacher() &&
501
            api_get_setting('allow_teachers_to_create_sessions') == 'true'
502
        ) {
503
            $where .= " AND s.id_coach = $userId ";
504
        }
505
506
        $extraFieldModel = new ExtraFieldModel('session');
507
        $conditions = $extraFieldModel->parseConditions($options);
508
        $sqlInjectJoins = $conditions['inject_joins'];
509
        $where .= $conditions['where'];
510
        $sqlInjectWhere = $conditions['inject_where'];
511
        $inject_extra_fields = $conditions['inject_extra_fields'];
512
        $order = $conditions['order'];
513
        $limit = $conditions['limit'];
514
515
        $isMakingOrder = false;
516
        $showCountUsers = false;
517
518
        if ($getCount == true) {
519
            $select = " SELECT count(DISTINCT s.id) as total_rows";
520
        } else {
521
            if (!empty($columns['column_model'])) {
522
                foreach ($columns['column_model'] as $column) {
523
                    if ($column['name'] == 'users') {
524
                        $showCountUsers = true;
525
                    }
526
                }
527
            }
528
529
            $select =
530
                "SELECT DISTINCT 
531
                     s.name,
532
                     s.display_start_date, 
533
                     s.display_end_date, 
534
                     access_start_date, 
535
                     access_end_date, 
536
                     s.visibility, 
537
                     s.session_category_id, 
538
                     $inject_extra_fields 
539
                     s.id 
540
             ";
541
542
            if ($showCountUsers) {
543
                $select .= ', count(su.user_id) users';
544
            }
545
            if (isset($options['order'])) {
546
                $isMakingOrder = strpos($options['order'], 'category_name') === 0;
547
            }
548
        }
549
550
        $isFilteringSessionCategory = strpos($where, 'category_name') !== false;
551
        $isFilteringSessionCategoryWithName = strpos($where, 'sc.name') !== false;
552
553
        if ($isMakingOrder || $isFilteringSessionCategory || $isFilteringSessionCategoryWithName) {
554
            $sqlInjectJoins .= " LEFT JOIN $sessionCategoryTable sc ON s.session_category_id = sc.id ";
555
556
            if ($isFilteringSessionCategory) {
557
                $where = str_replace('category_name', 'sc.name', $where);
558
            }
559
560
            if ($isMakingOrder) {
561
                $order = str_replace('category_name', 'sc.name', $order);
562
            }
563
        }
564
565
        if ($showCountUsers) {
566
            $tblSessionRelUser = Database::get_main_table(TABLE_MAIN_SESSION_USER);
567
            $sqlInjectJoins .= " LEFT JOIN $tblSessionRelUser su ON (su.session_id = s.id)";
568
        }
569
570
        $query = "$select FROM $tblSession s $sqlInjectJoins $where $sqlInjectWhere";
571
572
        if (api_is_multiple_url_enabled()) {
573
            $tblAccessUrlRelSession = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
574
            $accessUrlId = api_get_current_access_url_id();
575
576
            if ($accessUrlId != -1) {
577
                $where .= " AND ar.access_url_id = $accessUrlId ";
578
                $query = "$select
579
                    FROM $tblSession s $sqlInjectJoins
580
                    INNER JOIN $tblAccessUrlRelSession ar
581
                    ON (ar.session_id = s.id) $where";
582
            }
583
        }
584
585
        if ($showCountUsers) {
586
            $query .= ' GROUP by s.id';
587
        }
588
589
        $allowOrder = api_get_configuration_value('session_list_order');
590
591
        if ($allowOrder) {
592
            $order = ' ORDER BY position ASC';
593
        }
594
595
        $query .= $order;
596
        $query .= $limit;
597
        $result = Database::query($query);
598
599
        $sessions = Database::store_result($result, 'ASSOC');
600
601
        if ($getCount) {
602
            return $sessions[0]['total_rows'];
603
        }
604
605
        return $sessions;
606
    }
607
608
    /**
609
     * Gets the admin session list callback of the session/session_list.php page.
610
     *
611
     * @param array $options           order and limit keys
612
     * @param bool  $getCount          Whether to get all the results or only the count
613
     * @param array $columns
614
     * @param array $extraFieldsToLoad
615
     *
616
     * @return mixed Integer for number of rows, or array of results
617
     * @assert ([],true) !== false
618
     */
619
    public static function formatSessionsAdminForGrid(
620
        $options = [],
621
        $getCount = false,
622
        $columns = [],
623
        $extraFieldsToLoad = []
624
    ) {
625
        $showCountUsers = false;
626
627
        if (!$getCount && !empty($columns['column_model'])) {
628
            foreach ($columns['column_model'] as $column) {
629
                if ($column['name'] == 'users') {
630
                    $showCountUsers = true;
631
                }
632
            }
633
        }
634
635
        $userId = api_get_user_id();
636
637
        $sessions = self::getSessionsForAdmin($userId, $options, $getCount, $columns);
638
639
        if ($getCount) {
640
            return (int) $sessions;
641
        }
642
643
        $formattedSessions = [];
644
645
        $categories = self::get_all_session_category();
646
        $orderedCategories = [];
647
        if (!empty($categories)) {
648
            foreach ($categories as $category) {
649
                $orderedCategories[$category['id']] = $category['name'];
650
            }
651
        }
652
653
        $activeIcon = Display::return_icon('accept.png', get_lang('Active'));
654
        $inactiveIcon = Display::return_icon('error.png', get_lang('Inactive'));
655
656
        foreach ($sessions as $session) {
657
            if ($showCountUsers) {
658
                $session['users'] = self::get_users_by_session($session['id'], 0, true);
659
            }
660
            $url = api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session='.$session['id'];
661
            if (api_is_drh() || $extraFieldsToLoad) {
662
                $url = api_get_path(WEB_PATH).'session/'.$session['id'].'/about/';
663
            }
664
665
            $session['name'] = Display::url($session['name'], $url);
666
667
            if (!empty($extraFieldsToLoad)) {
668
                foreach ($extraFieldsToLoad as $field) {
669
                    $extraFieldValue = new ExtraFieldValue('session');
670
                    $fieldData = $extraFieldValue->getAllValuesByItemAndField(
671
                        $session['id'],
672
                        $field['id']
673
                    );
674
                    $fieldDataArray = [];
675
                    $fieldDataToString = '';
676
                    if (!empty($fieldData)) {
677
                        foreach ($fieldData as $data) {
678
                            $fieldDataArray[] = $data['value'];
679
                        }
680
                        $fieldDataToString = implode(', ', $fieldDataArray);
681
                    }
682
                    $session[$field['variable']] = $fieldDataToString;
683
                }
684
            }
685
686
            if (isset($session['session_active']) && $session['session_active'] == 1) {
687
                $session['session_active'] = $activeIcon;
688
            } else {
689
                $session['session_active'] = $inactiveIcon;
690
            }
691
692
            $session = self::convert_dates_to_local($session, true);
693
694
            switch ($session['visibility']) {
695
                case SESSION_VISIBLE_READ_ONLY: //1
696
                    $session['visibility'] = get_lang('ReadOnly');
697
                    break;
698
                case SESSION_VISIBLE:           //2
699
                case SESSION_AVAILABLE:         //4
700
                    $session['visibility'] = get_lang('Visible');
701
                    break;
702
                case SESSION_INVISIBLE:         //3
703
                    $session['visibility'] = api_ucfirst(get_lang('Invisible'));
704
                    break;
705
            }
706
707
            // Cleaning double selects.
708
            foreach ($session as $key => &$value) {
709
                if (isset($optionsByDouble[$key]) || isset($optionsByDouble[$key.'_second'])) {
710
                    $options = explode('::', $value);
711
                }
712
                $original_key = $key;
713
                if (strpos($key, '_second') !== false) {
714
                    $key = str_replace('_second', '', $key);
715
                }
716
717
                if (isset($optionsByDouble[$key]) &&
718
                    isset($options[0]) &&
719
                    isset($optionsByDouble[$key][$options[0]])
720
                ) {
721
                    if (strpos($original_key, '_second') === false) {
722
                        $value = $optionsByDouble[$key][$options[0]]['option_display_text'];
723
                    } else {
724
                        $value = $optionsByDouble[$key][$options[1]]['option_display_text'];
725
                    }
726
                }
727
            }
728
729
            $categoryName = isset($orderedCategories[$session['session_category_id']])
730
                ? $orderedCategories[$session['session_category_id']]
731
                : '';
732
            $session['category_name'] = $categoryName;
733
            $formattedSessions[] = $session;
734
        }
735
736
        return $formattedSessions;
737
    }
738
739
    /**
740
     *  Get total of records for progress of learning paths in the given session.
741
     *
742
     *  @param int session id
743
     *
744
     *  @return int
745
     */
746
    public static function get_count_session_lp_progress($sessionId = 0)
747
    {
748
        $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
749
        $tbl_lp_view = Database::get_course_table(TABLE_LP_VIEW);
750
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
751
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
752
753
        $sessionId = intval($sessionId);
754
755
        $sql = "SELECT  count(*) as total_rows
756
                FROM $tbl_lp_view v
757
                INNER JOIN $tbl_lp l ON l.id = v.lp_id
758
                INNER JOIN $tbl_user u ON u.user_id = v.user_id
759
                INNER JOIN $tbl_course c
760
                WHERE v.session_id = ".$sessionId;
761
        $result_rows = Database::query($sql);
762
        $row = Database::fetch_array($result_rows);
763
        $num = $row['total_rows'];
764
765
        return $num;
766
    }
767
768
    /**
769
     * Gets the progress of learning paths in the given session.
770
     *
771
     * @param int    $sessionId
772
     * @param int    $courseId
773
     * @param string $date_from
774
     * @param string $date_to
775
     * @param array options order and limit keys
776
     *
777
     * @return array table with user name, lp name, progress
778
     */
779
    public static function get_session_lp_progress(
780
        $sessionId = 0,
781
        $courseId = 0,
782
        $date_from,
783
        $date_to,
784
        $options
785
    ) {
786
        //escaping vars
787
        $sessionId = $sessionId == 'T' ? 'T' : intval($sessionId);
788
        $courseId = intval($courseId);
789
        $date_from = Database::escape_string($date_from);
790
        $date_to = Database::escape_string($date_to);
791
792
        //tables
793
        $session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
794
        $user = Database::get_main_table(TABLE_MAIN_USER);
795
        $tbl_course_lp_view = Database::get_course_table(TABLE_LP_VIEW);
796
797
        $course = api_get_course_info_by_id($courseId);
798
799
        //getting all the students of the course
800
        //we are not using this because it only returns user ids
801
        /* if (empty($sessionId)
802
          {
803
          // Registered students in a course outside session.
804
          $users = CourseManager::get_student_list_from_course_code($course_code);
805
          } else {
806
          // Registered students in session.
807
          $users = CourseManager::get_student_list_from_course_code($course_code, true, $sessionId);
808
          } */
809
810
        $sessionCond = 'and session_id = %s';
811
        if ($sessionId == 'T') {
812
            $sessionCond = '';
813
        }
814
815
        $where = " WHERE c_id = '%s' AND s.status <> 2 $sessionCond";
816
817
        $limit = null;
818
        if (!empty($options['limit'])) {
819
            $limit = " LIMIT ".$options['limit'];
820
        }
821
822
        if (!empty($options['where'])) {
823
            $where .= ' '.$options['where'];
824
        }
825
826
        $order = null;
827
        if (!empty($options['order'])) {
828
            $order = " ORDER BY ".$options['order'];
829
        }
830
831
        $sql = "SELECT u.user_id, u.lastname, u.firstname, u.username, u.email, s.c_id
832
                FROM $session_course_user s
833
                INNER JOIN $user u ON u.user_id = s.user_id
834
                $where
835
                $order
836
                $limit";
837
838
        $sql_query = sprintf($sql, Database::escape_string($course['real_id']), $sessionId);
839
840
        $rs = Database::query($sql_query);
841
        while ($user = Database::fetch_array($rs)) {
842
            $users[$user['user_id']] = $user;
843
        }
844
845
        // Get lessons
846
        $lessons = LearnpathList::get_course_lessons($course['code'], $sessionId);
847
848
        $table = [];
849
        foreach ($users as $user) {
850
            $data = [
851
                'lastname' => $user[1],
852
                'firstname' => $user[2],
853
                'username' => $user[3],
854
            ];
855
856
            $sessionCond = 'AND v.session_id = %d';
857
            if ($sessionId == 'T') {
858
                $sessionCond = "";
859
            }
860
861
            //Get lessons progress by user
862
            $sql = "SELECT v.lp_id as id, v.progress
863
                    FROM  $tbl_course_lp_view v
864
                    WHERE v.c_id = %d
865
                    AND v.user_id = %d
866
            $sessionCond";
867
868
            $sql_query = sprintf(
869
                $sql,
870
                intval($courseId),
871
                intval($user['user_id']),
872
                $sessionId
873
            );
874
875
            $result = Database::query($sql_query);
876
877
            $user_lessons = [];
878
            while ($row = Database::fetch_array($result)) {
879
                $user_lessons[$row['id']] = $row;
880
            }
881
882
            //Match course lessons with user progress
883
            $progress = 0;
884
            $count = 0;
885
            foreach ($lessons as $lesson) {
886
                $data[$lesson['id']] = (!empty($user_lessons[$lesson['id']]['progress'])) ? $user_lessons[$lesson['id']]['progress'] : 0;
887
                $progress += $data[$lesson['id']];
888
                $data[$lesson['id']] = $data[$lesson['id']].'%';
889
                $count++;
890
            }
891
            if ($count == 0) {
892
                $data['total'] = 0;
893
            } else {
894
                $data['total'] = round($progress / $count, 2).'%';
895
            }
896
            $table[] = $data;
897
        }
898
899
        return $table;
900
    }
901
902
    /**
903
     * Gets the survey answers.
904
     *
905
     * @param int $sessionId
906
     * @param int $courseId
907
     * @param int $surveyId
908
     * @param array options order and limit keys
909
     *
910
     * @todo fix the query
911
     *
912
     * @return array table with user name, lp name, progress
913
     */
914
    public static function get_survey_overview(
915
        $sessionId = 0,
916
        $courseId = 0,
917
        $surveyId = 0,
918
        $date_from,
919
        $date_to,
920
        $options
921
    ) {
922
        //escaping vars
923
        $sessionId = intval($sessionId);
924
        $courseId = intval($courseId);
925
        $surveyId = intval($surveyId);
926
927
        //tables
928
        $session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
929
        $user = Database::get_main_table(TABLE_MAIN_USER);
930
        $c_survey = Database::get_course_table(TABLE_SURVEY);
931
        $c_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER);
932
        $c_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
933
        $c_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
934
935
        $course = api_get_course_info_by_id($courseId);
936
937
        $where = " WHERE c_id = '%s' AND s.status <> 2 AND session_id = %s";
938
939
        $limit = null;
940
        if (!empty($options['limit'])) {
941
            $limit = " LIMIT ".$options['limit'];
942
        }
943
944
        if (!empty($options['where'])) {
945
            $where .= ' '.$options['where'];
946
        }
947
948
        $order = null;
949
        if (!empty($options['order'])) {
950
            $order = " ORDER BY ".$options['order'];
951
        }
952
953
        $sql = "SELECT u.user_id, u.lastname, u.firstname, u.username, u.email, s.c_id
954
                FROM $session_course_user s
955
                INNER JOIN $user u ON u.user_id = s.user_id
956
                $where $order $limit";
957
958
        $sql_query = sprintf($sql, intval($course['real_id']), $sessionId);
959
        $rs = Database::query($sql_query);
960
        while ($user = Database::fetch_array($rs)) {
961
            $users[$user['user_id']] = $user;
962
        }
963
964
        //Get survey questions
965
        $questions = SurveyManager::get_questions($surveyId, $courseId);
966
967
        //Survey is anonymous?
968
        $result = Database::query(sprintf("SELECT anonymous FROM $c_survey WHERE survey_id = %d", $surveyId));
969
        $row = Database::fetch_array($result);
970
        $anonymous = ($row['anonymous'] == 1) ? true : false;
971
972
        $table = [];
973
        foreach ($users as $user) {
974
            $data = [
975
                'lastname' => ($anonymous ? '***' : $user[1]),
976
                'firstname' => ($anonymous ? '***' : $user[2]),
977
                'username' => ($anonymous ? '***' : $user[3]),
978
            ];
979
980
            //Get questions by user
981
            $sql = "SELECT sa.question_id, sa.option_id, sqo.option_text, sq.type
982
                    FROM $c_survey_answer sa
983
                    INNER JOIN $c_survey_question sq
984
                    ON sq.question_id = sa.question_id
985
                    LEFT JOIN $c_survey_question_option sqo
986
                    ON
987
                      sqo.c_id = sa.c_id AND
988
                      sqo.question_id = sq.question_id AND
989
                      sqo.question_option_id = sa.option_id AND
990
                      sqo.survey_id = sq.survey_id
991
                    WHERE
992
                      sa.survey_id = %d AND
993
                      sa.c_id = %d AND
994
                      sa.user = %d
995
            "; //. $where_survey;
996
            $sql_query = sprintf($sql, $surveyId, $courseId, $user['user_id']);
997
998
            $result = Database::query($sql_query);
999
1000
            $user_questions = [];
1001
            while ($row = Database::fetch_array($result)) {
1002
                $user_questions[$row['question_id']] = $row;
1003
            }
1004
1005
            //Match course lessons with user progress
1006
            foreach ($questions as $question_id => $question) {
1007
                $option_text = 'option_text';
1008
                if ($user_questions[$question_id]['type'] == 'open') {
1009
                    $option_text = 'option_id';
1010
                }
1011
                $data[$question_id] = $user_questions[$question_id][$option_text];
1012
            }
1013
1014
            $table[] = $data;
1015
        }
1016
1017
        return $table;
1018
    }
1019
1020
    /**
1021
     * Gets the progress of the given session.
1022
     *
1023
     * @param int $sessionId
1024
     * @param int $courseId
1025
     * @param array options order and limit keys
1026
     *
1027
     * @return array table with user name, lp name, progress
1028
     */
1029
    public static function get_session_progress(
1030
        $sessionId,
1031
        $courseId,
1032
        $date_from,
1033
        $date_to,
1034
        $options
1035
    ) {
1036
        $sessionId = intval($sessionId);
1037
1038
        $getAllSessions = false;
1039
        if (empty($sessionId)) {
1040
            $sessionId = 0;
1041
            $getAllSessions = true;
1042
        }
1043
1044
        //tables
1045
        $session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
1046
        $user = Database::get_main_table(TABLE_MAIN_USER);
1047
        $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
1048
        $workTableAssignment = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
1049
        $tbl_course_lp = Database::get_course_table(TABLE_LP_MAIN);
1050
        $wiki = Database::get_course_table(TABLE_WIKI);
1051
        $table_stats_default = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
1052
        $table_stats_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
1053
1054
        $course = api_get_course_info_by_id($courseId);
1055
        $where = " WHERE c_id = '%s' AND s.status <> 2 ";
1056
1057
        $limit = null;
1058
        if (!empty($options['limit'])) {
1059
            $limit = " LIMIT ".$options['limit'];
1060
        }
1061
1062
        if (!empty($options['where'])) {
1063
            $where .= ' '.$options['where'];
1064
        }
1065
1066
        $order = null;
1067
        if (!empty($options['order'])) {
1068
            $order = " ORDER BY ".$options['order'];
1069
        }
1070
1071
        //TODO, fix create report without session
1072
        $queryVariables = [$course['real_id']];
1073
        if (!empty($sessionId)) {
1074
            $where .= ' AND session_id = %s';
1075
            $queryVariables[] = $sessionId;
1076
            $sql = "SELECT
1077
                        u.user_id, u.lastname, u.firstname, u.username,
1078
                        u.email, s.c_id, s.session_id
1079
                    FROM $session_course_user s
1080
                    INNER JOIN $user u
1081
                    ON u.user_id = s.user_id
1082
                    $where $order $limit";
1083
        } else {
1084
            $sql = "SELECT
1085
                        u.user_id, u.lastname, u.firstname, u.username,
1086
                        u.email, s.c_id, s.session_id
1087
                    FROM $session_course_user s
1088
                    INNER JOIN $user u ON u.user_id = s.user_id
1089
                    $where $order $limit";
1090
        }
1091
1092
        $sql_query = vsprintf($sql, $queryVariables);
1093
        $rs = Database::query($sql_query);
1094
        while ($user = Database::fetch_array($rs)) {
1095
            $users[$user['user_id']] = $user;
1096
        }
1097
1098
        /**
1099
         *  Lessons.
1100
         */
1101
        $sql = "SELECT * FROM $tbl_course_lp WHERE c_id = %s "; //AND session_id = %s
1102
        $sql_query = sprintf($sql, $course['real_id']);
1103
        $result = Database::query($sql_query);
1104
        $arrLesson = [[]];
1105
        while ($row = Database::fetch_array($result)) {
1106
            if (empty($arrLesson[$row['session_id']]['lessons_total'])) {
1107
                $arrLesson[$row['session_id']]['lessons_total'] = 1;
1108
            } else {
1109
                $arrLesson[$row['session_id']]['lessons_total']++;
1110
            }
1111
        }
1112
1113
        /**
1114
         *  Exercises.
1115
         */
1116
        $exercises = ExerciseLib::get_all_exercises(
1117
            $course,
1118
            $sessionId,
1119
            false,
1120
            '',
1121
            $getAllSessions
1122
        );
1123
        $exercises_total = count($exercises);
1124
1125
        /**
1126
         *  Assignments.
1127
         */
1128
        //total
1129
        $params = [$course['real_id']];
1130
        if ($getAllSessions) {
1131
            $sql = "SELECT count(w.id) as count
1132
                    FROM $workTable w
1133
                    LEFT JOIN $workTableAssignment a
1134
                    ON (a.publication_id = w.id AND a.c_id = w.c_id)
1135
                    WHERE 
1136
                        w.c_id = %s AND 
1137
                        parent_id = 0 AND 
1138
                        active IN (1, 0)";
1139
        } else {
1140
            $sql = "SELECT count(w.id) as count
1141
                    FROM $workTable w
1142
                    LEFT JOIN $workTableAssignment a
1143
                    ON (a.publication_id = w.id AND a.c_id = w.c_id)
1144
                    WHERE 
1145
                        w.c_id = %s AND 
1146
                        parent_id = 0 AND 
1147
                        active IN (1, 0)";
1148
1149
            if (empty($sessionId)) {
1150
                $sql .= ' AND w.session_id = NULL ';
1151
            } else {
1152
                $sql .= ' AND w.session_id = %s ';
1153
                $params[] = $sessionId;
1154
            }
1155
        }
1156
1157
        $sql_query = vsprintf($sql, $params);
1158
        $result = Database::query($sql_query);
1159
        $row = Database::fetch_array($result);
1160
        $assignments_total = $row['count'];
1161
1162
        /**
1163
         * Wiki.
1164
         */
1165
        if ($getAllSessions) {
1166
            $sql = "SELECT count(distinct page_id)  as count FROM $wiki
1167
                    WHERE c_id = %s";
1168
        } else {
1169
            $sql = "SELECT count(distinct page_id)  as count FROM $wiki
1170
                    WHERE c_id = %s and session_id = %s";
1171
        }
1172
        $sql_query = sprintf($sql, $course['real_id'], $sessionId);
1173
        $result = Database::query($sql_query);
1174
        $row = Database::fetch_array($result);
1175
        $wiki_total = $row['count'];
1176
1177
        /**
1178
         * Surveys.
1179
         */
1180
        $survey_user_list = [];
1181
        $survey_list = SurveyManager::get_surveys($course['code'], $sessionId);
1182
1183
        $surveys_total = count($survey_list);
1184
        foreach ($survey_list as $survey) {
1185
            $user_list = SurveyManager::get_people_who_filled_survey(
1186
                $survey['survey_id'],
1187
                false,
1188
                $course['real_id']
1189
            );
1190
            foreach ($user_list as $user_id) {
1191
                isset($survey_user_list[$user_id]) ? $survey_user_list[$user_id]++ : $survey_user_list[$user_id] = 1;
1192
            }
1193
        }
1194
1195
        /**
1196
         * Forums.
1197
         */
1198
        $forums_total = CourseManager::getCountForum(
1199
            $course['real_id'],
1200
            $sessionId,
1201
            $getAllSessions
1202
        );
1203
1204
        //process table info
1205
        foreach ($users as $user) {
1206
            //Course description
1207
            $sql = "SELECT count(*) as count
1208
                    FROM $table_stats_access
1209
                    WHERE access_tool = 'course_description'
1210
                    AND c_id = '%s'
1211
                    AND access_session_id = %s
1212
                    AND access_user_id = %s ";
1213
            $sql_query = sprintf($sql, $course['real_id'], $user['id_session'], $user['user_id']);
1214
1215
            $result = Database::query($sql_query);
1216
            $row = Database::fetch_array($result);
1217
            $course_description_progress = ($row['count'] > 0) ? 100 : 0;
1218
1219
            if (!empty($arrLesson[$user['id_session']]['lessons_total'])) {
1220
                $lessons_total = $arrLesson[$user['id_session']]['lessons_total'];
1221
            } else {
1222
                $lessons_total = !empty($arrLesson[0]['lessons_total']) ? $arrLesson[0]['lessons_total'] : 0;
1223
            }
1224
1225
            //Lessons
1226
            //TODO: Lessons done and left is calculated by progress per item in lesson, maybe we should calculate it only per completed lesson?
1227
            $lessons_progress = Tracking::get_avg_student_progress(
1228
                $user['user_id'],
1229
                $course['code'],
1230
                [],
1231
                $user['id_session']
1232
            );
1233
            $lessons_done = ($lessons_progress * $lessons_total) / 100;
1234
            $lessons_left = $lessons_total - $lessons_done;
1235
1236
            // Exercises
1237
            $exercises_progress = str_replace(
1238
                '%',
1239
                '',
1240
                Tracking::get_exercise_student_progress(
1241
                    $exercises,
1242
                    $user['user_id'],
1243
                    $course['real_id'],
1244
                    $user['id_session']
1245
                )
1246
            );
1247
            $exercises_done = round(($exercises_progress * $exercises_total) / 100);
1248
            $exercises_left = $exercises_total - $exercises_done;
1249
1250
            //Assignments
1251
            $assignments_done = Tracking::count_student_assignments($user['user_id'], $course['code'], $user['id_session']);
1252
            $assignments_left = $assignments_total - $assignments_done;
1253
            if (!empty($assignments_total)) {
1254
                $assignments_progress = round((($assignments_done * 100) / $assignments_total), 2);
1255
            } else {
1256
                $assignments_progress = 0;
1257
            }
1258
1259
            // Wiki
1260
            // total revisions per user
1261
            $sql = "SELECT count(*) as count
1262
                    FROM $wiki
1263
                    WHERE c_id = %s and session_id = %s and user_id = %s";
1264
            $sql_query = sprintf($sql, $course['real_id'], $user['id_session'], $user['user_id']);
1265
            $result = Database::query($sql_query);
1266
            $row = Database::fetch_array($result);
1267
            $wiki_revisions = $row['count'];
1268
            //count visited wiki pages
1269
            $sql = "SELECT count(distinct default_value) as count
1270
                    FROM $table_stats_default
1271
                    WHERE
1272
                        default_user_id = %s AND
1273
                        default_event_type = 'wiki_page_view' AND
1274
                        default_value_type = 'wiki_page_id' AND
1275
                        c_id = %s
1276
                    ";
1277
            $sql_query = sprintf($sql, $user['user_id'], $course['real_id']);
1278
            $result = Database::query($sql_query);
1279
            $row = Database::fetch_array($result);
1280
1281
            $wiki_read = $row['count'];
1282
            $wiki_unread = $wiki_total - $wiki_read;
1283
            if (!empty($wiki_total)) {
1284
                $wiki_progress = round((($wiki_read * 100) / $wiki_total), 2);
1285
            } else {
1286
                $wiki_progress = 0;
1287
            }
1288
1289
            //Surveys
1290
            $surveys_done = (isset($survey_user_list[$user['user_id']]) ? $survey_user_list[$user['user_id']] : 0);
1291
            $surveys_left = $surveys_total - $surveys_done;
1292
            if (!empty($surveys_total)) {
1293
                $surveys_progress = round((($surveys_done * 100) / $surveys_total), 2);
1294
            } else {
1295
                $surveys_progress = 0;
1296
            }
1297
1298
            //Forums
1299
            $forums_done = CourseManager::getCountForumPerUser(
1300
                $user['user_id'],
1301
                $course['real_id'],
1302
                $user['id_session']
1303
            );
1304
            $forums_left = $forums_total - $forums_done;
1305
            if (!empty($forums_total)) {
1306
                $forums_progress = round((($forums_done * 100) / $forums_total), 2);
1307
            } else {
1308
                $forums_progress = 0;
1309
            }
1310
1311
            // Overall Total
1312
            $overall_total = ($course_description_progress + $exercises_progress + $forums_progress + $assignments_progress + $wiki_progress + $surveys_progress) / 6;
1313
1314
            $link = '<a href="'.api_get_path(WEB_CODE_PATH).'mySpace/myStudents.php?student='.$user[0].'&details=true&course='.$course['code'].'&id_session='.$user['id_session'].'"> %s </a>';
1315
            $linkForum = '<a href="'.api_get_path(WEB_CODE_PATH).'forum/index.php?cidReq='.$course['code'].'&id_session='.$user['id_session'].'"> %s </a>';
1316
            $linkWork = '<a href="'.api_get_path(WEB_CODE_PATH).'work/work.php?cidReq='.$course['code'].'&id_session='.$user['id_session'].'"> %s </a>';
1317
            $linkWiki = '<a href="'.api_get_path(WEB_CODE_PATH).'wiki/index.php?cidReq='.$course['code'].'&session_id='.$user['id_session'].'&action=statistics"> %s </a>';
1318
            $linkSurvey = '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_list.php?cidReq='.$course['code'].'&id_session='.$user['id_session'].'"> %s </a>';
1319
1320
            $table[] = [
1321
                'lastname' => $user[1],
1322
                'firstname' => $user[2],
1323
                'username' => $user[3],
1324
                //'profile'   => '',
1325
                'total' => round($overall_total, 2).'%',
1326
                'courses' => sprintf($link, $course_description_progress.'%'),
1327
                'lessons' => sprintf($link, $lessons_progress.'%'),
1328
                'exercises' => sprintf($link, $exercises_progress.'%'),
1329
                'forums' => sprintf($link, $forums_progress.'%'),
1330
                'homeworks' => sprintf($link, $assignments_progress.'%'),
1331
                'wikis' => sprintf($link, $wiki_progress.'%'),
1332
                'surveys' => sprintf($link, $surveys_progress.'%'),
1333
                //course description
1334
                'course_description_progress' => $course_description_progress.'%',
1335
                //lessons
1336
                'lessons_total' => sprintf($link, $lessons_total),
1337
                'lessons_done' => sprintf($link, $lessons_done),
1338
                'lessons_left' => sprintf($link, $lessons_left),
1339
                'lessons_progress' => sprintf($link, $lessons_progress.'%'),
1340
                //exercises
1341
                'exercises_total' => sprintf($link, $exercises_total),
1342
                'exercises_done' => sprintf($link, $exercises_done),
1343
                'exercises_left' => sprintf($link, $exercises_left),
1344
                'exercises_progress' => sprintf($link, $exercises_progress.'%'),
1345
                //forums
1346
                'forums_total' => sprintf($linkForum, $forums_total),
1347
                'forums_done' => sprintf($linkForum, $forums_done),
1348
                'forums_left' => sprintf($linkForum, $forums_left),
1349
                'forums_progress' => sprintf($linkForum, $forums_progress.'%'),
1350
                //assignments
1351
                'assignments_total' => sprintf($linkWork, $assignments_total),
1352
                'assignments_done' => sprintf($linkWork, $assignments_done),
1353
                'assignments_left' => sprintf($linkWork, $assignments_left),
1354
                'assignments_progress' => sprintf($linkWork, $assignments_progress.'%'),
1355
                //wiki
1356
                'wiki_total' => sprintf($linkWiki, $wiki_total),
1357
                'wiki_revisions' => sprintf($linkWiki, $wiki_revisions),
1358
                'wiki_read' => sprintf($linkWiki, $wiki_read),
1359
                'wiki_unread' => sprintf($linkWiki, $wiki_unread),
1360
                'wiki_progress' => sprintf($linkWiki, $wiki_progress.'%'),
1361
                //survey
1362
                'surveys_total' => sprintf($linkSurvey, $surveys_total),
1363
                'surveys_done' => sprintf($linkSurvey, $surveys_done),
1364
                'surveys_left' => sprintf($linkSurvey, $surveys_left),
1365
                'surveys_progress' => sprintf($linkSurvey, $surveys_progress.'%'),
1366
            ];
1367
        }
1368
1369
        return $table;
1370
    }
1371
1372
    /**
1373
     * Get the ip, total of clicks, login date and time logged in for all user, in one session.
1374
     *
1375
     * @todo track_e_course_access table should have ip so we dont have to look for it in track_e_login
1376
     *
1377
     * @author César Perales <[email protected]>, Beeznest Team
1378
     *
1379
     * @version 1.9.6
1380
     */
1381
    public static function get_user_data_access_tracking_overview(
1382
        $sessionId,
1383
        $courseId,
1384
        $studentId = 0,
1385
        $profile = '',
1386
        $date_from = '',
1387
        $date_to = '',
1388
        $options
1389
    ) {
1390
        //escaping variables
1391
        $sessionId = intval($sessionId);
1392
        $courseId = intval($courseId);
1393
        $studentId = intval($studentId);
1394
        $profile = intval($profile);
1395
        $date_from = Database::escape_string($date_from);
1396
        $date_to = Database::escape_string($date_to);
1397
1398
        // database table definition
1399
        $user = Database::get_main_table(TABLE_MAIN_USER);
1400
        $course = Database::get_main_table(TABLE_MAIN_COURSE);
1401
        $track_e_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
1402
        $track_e_course_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
1403
        $sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
1404
1405
        global $export_csv;
1406
        if ($export_csv) {
1407
            $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
1408
        } else {
1409
            $is_western_name_order = api_is_western_name_order();
1410
        }
1411
1412
        $where = null;
1413
        if (isset($sessionId) && !empty($sessionId)) {
1414
            $where = sprintf(" WHERE a.session_id = %d", $sessionId);
1415
        }
1416
        if (isset($courseId) && !empty($courseId)) {
1417
            $where .= sprintf(" AND c.id = %d", $courseId);
1418
        }
1419
        if (isset($studentId) && !empty($studentId)) {
1420
            $where .= sprintf(" AND u.user_id = %d", $studentId);
1421
        }
1422
        if (isset($profile) && !empty($profile)) {
1423
            $where .= sprintf(" AND u.status = %d", $profile);
1424
        }
1425
        if (!empty($date_to) && !empty($date_from)) {
1426
            $where .= sprintf(
1427
                " AND a.login_course_date >= '%s 00:00:00'
1428
                 AND a.login_course_date <= '%s 23:59:59'",
1429
                $date_from,
1430
                $date_to
1431
            );
1432
        }
1433
1434
        $limit = null;
1435
        if (!empty($options['limit'])) {
1436
            $limit = " LIMIT ".$options['limit'];
1437
        }
1438
1439
        if (!empty($options['where'])) {
1440
            $where .= ' '.$options['where'];
1441
        }
1442
1443
        $order = null;
1444
        if (!empty($options['order'])) {
1445
            $order = " ORDER BY ".$options['order'];
1446
        }
1447
1448
        //TODO add course name
1449
        $sql = "SELECT
1450
                a.login_course_date ,
1451
                u.username ,
1452
                ".($is_western_name_order ? "
1453
                    u.firstname,
1454
                    u.lastname,
1455
                    " : "
1456
                    u.lastname,
1457
                    u.firstname,
1458
                ")."
1459
                a.logout_course_date,
1460
                a.counter,
1461
                c.title,
1462
                c.code,
1463
                u.user_id,
1464
                a.session_id
1465
            FROM $track_e_course_access a
1466
            INNER JOIN $user u ON a.user_id = u.user_id
1467
            INNER JOIN $course c ON a.c_id = c.id
1468
            $where $order $limit";
1469
        $result = Database::query(sprintf($sql, $sessionId, $courseId));
1470
1471
        $data = [];
1472
        while ($user = Database::fetch_assoc($result)) {
1473
            $data[] = $user;
1474
        }
1475
1476
        foreach ($data as $key => $info) {
1477
            $sql = "SELECT
1478
                    name
1479
                    FROM $sessionTable
1480
                    WHERE
1481
                    id = {$info['session_id']}";
1482
            $result = Database::query($sql);
1483
            $session = Database::fetch_assoc($result);
1484
1485
            // building array to display
1486
            $return[] = [
1487
                'user_id' => $info['user_id'],
1488
                'logindate' => $info['login_course_date'],
1489
                'username' => $info['username'],
1490
                'firstname' => $info['firstname'],
1491
                'lastname' => $info['lastname'],
1492
                'clicks' => $info['counter'], //+ $clicks[$info['user_id']],
1493
                'ip' => '',
1494
                'timeLoggedIn' => gmdate("H:i:s", strtotime($info['logout_course_date']) - strtotime($info['login_course_date'])),
1495
                'session' => $session['name'],
1496
            ];
1497
        }
1498
1499
        foreach ($return as $key => $info) {
1500
            //Search for ip, we do less querys if we iterate the final array
1501
            $sql = sprintf(
1502
                "SELECT user_ip FROM $track_e_login WHERE login_user_id = %d AND login_date < '%s' ORDER BY login_date DESC LIMIT 1",
1503
                $info['user_id'],
1504
                $info['logindate']
1505
            ); //TODO add select by user too
1506
            $result = Database::query($sql);
1507
            $ip = Database::fetch_assoc($result);
1508
            //if no ip founded, we search the closest higher ip
1509
            if (empty($ip['user_ip'])) {
1510
                $sql = sprintf(
1511
                    "SELECT user_ip FROM $track_e_login WHERE login_user_id = %d AND login_date > '%s'  ORDER BY login_date ASC LIMIT 1",
1512
                    $info['user_id'],
1513
                    $info['logindate']
1514
                ); //TODO add select by user too
1515
                $result = Database::query($sql);
1516
                $ip = Database::fetch_assoc($result);
1517
            }
1518
            //add ip to final array
1519
            $return[$key]['ip'] = $ip['user_ip'];
1520
        }
1521
1522
        return $return;
1523
    }
1524
1525
    /**
1526
     * Creates a new course code based in given code.
1527
     *
1528
     * @param string $session_name
1529
     *                             <code>
1530
     *                             $wanted_code = 'curse' if there are in the DB codes like curse1 curse2 the function will return: course3
1531
     *                             if the course code doest not exist in the DB the same course code will be returned
1532
     *                             </code>
1533
     *
1534
     * @return string wanted unused code
1535
     */
1536
    public static function generateNextSessionName($session_name)
1537
    {
1538
        $session_name_ok = !self::sessionNameExists($session_name);
1539
        if (!$session_name_ok) {
1540
            $table = Database::get_main_table(TABLE_MAIN_SESSION);
1541
            $session_name = Database::escape_string($session_name);
1542
            $sql = "SELECT count(*) as count FROM $table
1543
                    WHERE name LIKE '$session_name%'";
1544
            $result = Database::query($sql);
1545
            if (Database::num_rows($result) > 0) {
1546
                $row = Database::fetch_array($result);
1547
                $count = $row['count'] + 1;
1548
                $session_name = $session_name.'_'.$count;
1549
                $result = self::sessionNameExists($session_name);
1550
                if (!$result) {
1551
                    return $session_name;
1552
                }
1553
            }
1554
1555
            return false;
1556
        }
1557
1558
        return $session_name;
1559
    }
1560
1561
    /**
1562
     * Edit a session.
1563
     *
1564
     * @author Carlos Vargas from existing code
1565
     *
1566
     * @param int    $id                           Session primary key
1567
     * @param string $name
1568
     * @param string $startDate
1569
     * @param string $endDate
1570
     * @param string $displayStartDate
1571
     * @param string $displayEndDate
1572
     * @param string $coachStartDate
1573
     * @param string $coachEndDate
1574
     * @param int    $coachId
1575
     * @param int    $sessionCategoryId
1576
     * @param int    $visibility
1577
     * @param string $description
1578
     * @param int    $showDescription
1579
     * @param int    $duration
1580
     * @param array  $extraFields
1581
     * @param int    $sessionAdminId
1582
     * @param bool   $sendSubscriptionNotification Optional.
1583
     *                                             Whether send a mail notification to users being subscribed
1584
     *
1585
     * @return mixed
1586
     */
1587
    public static function edit_session(
1588
        $id,
1589
        $name,
1590
        $startDate,
1591
        $endDate,
1592
        $displayStartDate,
1593
        $displayEndDate,
1594
        $coachStartDate,
1595
        $coachEndDate,
1596
        $coachId,
1597
        $sessionCategoryId,
1598
        $visibility,
1599
        $description = null,
1600
        $showDescription = 0,
1601
        $duration = null,
1602
        $extraFields = [],
1603
        $sessionAdminId = 0,
1604
        $sendSubscriptionNotification = false
1605
    ) {
1606
        $coachId = (int) $coachId;
1607
        $sessionCategoryId = (int) $sessionCategoryId;
1608
        $visibility = (int) $visibility;
1609
1610
        $em = Database::getManager();
1611
1612
        if (empty($name)) {
1613
            Display::addFlash(
1614
                Display::return_message(get_lang('SessionNameIsRequired'), 'warning')
1615
            );
1616
1617
            return false;
1618
        } elseif (empty($coachId)) {
1619
            Display::addFlash(
1620
                Display::return_message(get_lang('CoachIsRequired'), 'warning')
1621
            );
1622
1623
            return false;
1624
        } elseif (!empty($startDate) &&
1625
            !api_is_valid_date($startDate, 'Y-m-d H:i') &&
1626
            !api_is_valid_date($startDate, 'Y-m-d H:i:s')
1627
        ) {
1628
            Display::addFlash(
1629
                Display::return_message(get_lang('InvalidStartDate'), 'warning')
1630
            );
1631
1632
            return false;
1633
        } elseif (!empty($endDate) &&
1634
            !api_is_valid_date($endDate, 'Y-m-d H:i') &&
1635
            !api_is_valid_date($endDate, 'Y-m-d H:i:s')
1636
        ) {
1637
            Display::addFlash(
1638
                Display::return_message(get_lang('InvalidEndDate'), 'warning')
1639
            );
1640
1641
            return false;
1642
        } elseif (!empty($startDate) && !empty($endDate) && $startDate >= $endDate) {
1643
            Display::addFlash(
1644
                Display::return_message(get_lang('StartDateShouldBeBeforeEndDate'), 'warning')
1645
            );
1646
1647
            return false;
1648
        } else {
1649
            $sessionInfo = self::get_session_by_name($name);
1650
            $exists = false;
1651
1652
            if (!empty($sessionInfo)) {
1653
                if ($sessionInfo['id'] != $id) {
1654
                    $exists = true;
1655
                }
1656
            }
1657
1658
            if ($exists) {
1659
                Display::addFlash(
1660
                    Display::return_message(get_lang('SessionNameAlreadyExists'), 'warning')
1661
                );
1662
1663
                return false;
1664
            } else {
1665
                /** @var Session $sessionEntity */
1666
                $sessionEntity = api_get_session_entity($id);
1667
                $sessionEntity
1668
                    ->setName($name)
1669
                    ->setDuration($duration)
1670
                    ->setDescription($description)
1671
                    ->setShowDescription($showDescription)
1672
                    ->setVisibility($visibility)
1673
                    ->setSendSubscriptionNotification($sendSubscriptionNotification)
1674
                    ->setAccessStartDate(null)
1675
                    ->setAccessStartDate(null)
1676
                    ->setDisplayStartDate(null)
1677
                    ->setDisplayEndDate(null)
1678
                    ->setCoachAccessStartDate(null)
1679
                    ->setCoachAccessEndDate(null)
1680
                    ->setGeneralCoach(api_get_user_entity($coachId))
1681
                ;
1682
1683
                if (!empty($sessionAdminId)) {
1684
                    $sessionEntity->setSessionAdminId($sessionAdminId);
1685
                }
1686
1687
                if (!empty($startDate)) {
1688
                    $sessionEntity->setAccessStartDate(api_get_utc_datetime($startDate, true, true));
1689
                }
1690
1691
                if (!empty($endDate)) {
1692
                    $sessionEntity->setAccessEndDate(api_get_utc_datetime($endDate, true, true));
1693
                }
1694
1695
                if (!empty($displayStartDate)) {
1696
                    $sessionEntity->setDisplayStartDate(api_get_utc_datetime($displayStartDate, true, true));
1697
                }
1698
1699
                if (!empty($displayEndDate)) {
1700
                    $sessionEntity->setDisplayEndDate(api_get_utc_datetime($displayEndDate, true, true));
1701
                }
1702
1703
                if (!empty($coachStartDate)) {
1704
                    $sessionEntity->setCoachAccessStartDate(api_get_utc_datetime($coachStartDate, true, true));
1705
                }
1706
1707
                if (!empty($coachEndDate)) {
1708
                    $sessionEntity->setCoachAccessEndDate(api_get_utc_datetime($coachEndDate, true, true));
1709
                }
1710
1711
                if (!empty($sessionCategoryId)) {
1712
                    $category = $em->getRepository('ChamiloCoreBundle:SessionCategory')->find($sessionCategoryId);
1713
                    $sessionEntity->setCategory($category);
1714
                } else {
1715
                    $sessionEntity->setCategory(null);
1716
                }
1717
1718
                $em->merge($sessionEntity);
1719
                $em->flush();
1720
1721
                if (!empty($extraFields)) {
1722
                    $extraFields['item_id'] = $id;
1723
                    $sessionFieldValue = new ExtraFieldValue('session');
1724
                    $sessionFieldValue->saveFieldValues($extraFields);
1725
                }
1726
1727
                return $id;
1728
            }
1729
        }
1730
    }
1731
1732
    /**
1733
     * Delete session.
1734
     *
1735
     * @author Carlos Vargas  from existing code
1736
     *
1737
     * @param array $id_checked an array to delete sessions
1738
     * @param bool  $from_ws    optional, true if the function is called
1739
     *                          by a webservice, false otherwise
1740
     *
1741
     * @return bool
1742
     * */
1743
    public static function delete($id_checked, $from_ws = false)
1744
    {
1745
        $sessionId = null;
1746
        if (is_array($id_checked)) {
1747
            foreach ($id_checked as $sessionId) {
1748
                self::delete($sessionId);
1749
            }
1750
        } else {
1751
            $sessionId = (int) $id_checked;
1752
        }
1753
1754
        if (empty($sessionId)) {
1755
            return false;
1756
        }
1757
1758
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
1759
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
1760
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
1761
        $tbl_url_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
1762
        $tbl_item_properties = Database::get_course_table(TABLE_ITEM_PROPERTY);
1763
        $tbl_student_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
1764
        $tbl_student_publication_assignment = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
1765
        $userGroupSessionTable = Database::get_main_table(TABLE_USERGROUP_REL_SESSION);
1766
        $trackCourseAccess = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
1767
        $trackAccess = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
1768
1769
        $ticket = Database::get_main_table(TABLE_TICKET_TICKET);
1770
        $em = Database::getManager();
1771
        $userId = api_get_user_id();
1772
1773
        /** @var SequenceRepository $repo */
1774
        $repo = Database::getManager()->getRepository('ChamiloCoreBundle:SequenceResource');
1775
        $sequenceResource = $repo->findRequirementForResource(
1776
            $sessionId,
1777
            SequenceResource::SESSION_TYPE
1778
        );
1779
1780
        if ($sequenceResource) {
1781
            Display::addFlash(
1782
                Display::return_message(
1783
                    get_lang('ThereIsASequenceResourceLinkedToThisSessionYouNeedToDeleteItFirst'),
1784
                    'error'
1785
                )
1786
            );
1787
1788
            return false;
1789
        }
1790
1791
        if (self::allowed($sessionId) && !$from_ws) {
1792
            $qb = $em
1793
                ->createQuery('
1794
                    SELECT s.sessionAdminId FROM ChamiloCoreBundle:Session s
1795
                    WHERE s.id = ?1
1796
                ')
1797
                ->setParameter(1, $sessionId);
1798
1799
            $res = $qb->getSingleScalarResult();
1800
1801
            if ($res != $userId && !api_is_platform_admin()) {
1802
                api_not_allowed(true);
1803
            }
1804
        }
1805
1806
        // Delete documents inside a session
1807
        $courses = self::getCoursesInSession($sessionId);
1808
        foreach ($courses as $courseId) {
1809
            $courseInfo = api_get_course_info_by_id($courseId);
1810
            DocumentManager::deleteDocumentsFromSession($courseInfo, $sessionId);
1811
            $works = Database::select(
1812
                '*',
1813
                $tbl_student_publication,
1814
                [
1815
                    'where' => ['session_id = ? AND c_id = ?' => [$sessionId, $courseId]],
1816
                ]
1817
            );
1818
1819
            $currentCourseRepositorySys = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/';
1820
            foreach ($works as $index => $work) {
1821
                if ($work['filetype'] = 'folder') {
1822
                    Database::query("DELETE FROM $tbl_student_publication_assignment WHERE publication_id = $index");
1823
                }
1824
                my_delete($currentCourseRepositorySys.'/'.$work['url']);
1825
            }
1826
        }
1827
1828
        $sessionEntity = api_get_session_entity($sessionId);
1829
        $sessionName = $sessionEntity->getName();
1830
        $em->remove($sessionEntity);
1831
        $em->flush();
1832
1833
        // Class
1834
        $sql = "DELETE FROM $userGroupSessionTable
1835
                WHERE session_id = $sessionId";
1836
        Database::query($sql);
1837
1838
        Database::query("DELETE FROM $tbl_student_publication WHERE session_id = $sessionId");
1839
        Database::query("DELETE FROM $tbl_session_rel_course WHERE session_id = $sessionId");
1840
        Database::query("DELETE FROM $tbl_session_rel_course_rel_user WHERE session_id = $sessionId");
1841
        Database::query("DELETE FROM $tbl_session_rel_user WHERE session_id = $sessionId");
1842
        Database::query("DELETE FROM $tbl_item_properties WHERE session_id = $sessionId");
1843
        Database::query("DELETE FROM $tbl_url_session WHERE session_id = $sessionId");
1844
        Database::query("DELETE FROM $trackCourseAccess WHERE session_id = $sessionId");
1845
        Database::query("DELETE FROM $trackAccess WHERE access_session_id = $sessionId");
1846
        $sql = "UPDATE $ticket SET session_id = NULL WHERE session_id = $sessionId";
1847
        Database::query($sql);
1848
1849
        $extraFieldValue = new ExtraFieldValue('session');
1850
        $extraFieldValue->deleteValuesByItem($sessionId);
1851
1852
        $repo->deleteResource(
1853
            $sessionId,
1854
            SequenceResource::SESSION_TYPE
1855
        );
1856
1857
        // Add event to system log
1858
        Event::addEvent(
1859
            LOG_SESSION_DELETE,
1860
            LOG_SESSION_ID,
1861
            $sessionName.' - id:'.$sessionId,
1862
            api_get_utc_datetime(),
1863
            $userId
1864
        );
1865
1866
        return true;
1867
    }
1868
1869
    /**
1870
     * @param int $id promotion id
1871
     *
1872
     * @return bool
1873
     */
1874
    public static function clear_session_ref_promotion($id)
1875
    {
1876
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
1877
        $id = intval($id);
1878
        $sql = "UPDATE $tbl_session 
1879
                SET promotion_id = 0
1880
                WHERE promotion_id = $id";
1881
        if (Database::query($sql)) {
1882
            return true;
1883
        } else {
1884
            return false;
1885
        }
1886
    }
1887
1888
    /**
1889
     * Subscribes students to the given session and optionally (default)
1890
     * unsubscribes previous users.
1891
     *
1892
     * @author Carlos Vargas from existing code
1893
     * @author Julio Montoya. Cleaning code.
1894
     *
1895
     * @param int   $sessionId
1896
     * @param array $userList
1897
     * @param int   $session_visibility
1898
     * @param bool  $empty_users
1899
     * @param bool  $registerUsersToAllCourses
1900
     *
1901
     * @return bool
1902
     */
1903
    public static function subscribeUsersToSession(
1904
        $sessionId,
1905
        $userList,
1906
        $session_visibility = SESSION_VISIBLE_READ_ONLY,
1907
        $empty_users = true,
1908
        $registerUsersToAllCourses = true
1909
    ) {
1910
        if ($sessionId != strval(intval($sessionId))) {
1911
            return false;
1912
        }
1913
1914
        foreach ($userList as $intUser) {
1915
            if ($intUser != strval(intval($intUser))) {
1916
                return false;
1917
            }
1918
        }
1919
1920
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
1921
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
1922
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
1923
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
1924
1925
        $session = api_get_session_entity($sessionId);
1926
1927
        // from function parameter
1928
        if (empty($session_visibility)) {
1929
            $session_visibility = $session->getVisibility();
1930
            //default status loaded if empty
1931
            // by default readonly 1
1932
            if (empty($session_visibility)) {
1933
                $session_visibility = SESSION_VISIBLE_READ_ONLY;
1934
            }
1935
        } else {
1936
            if (!in_array($session_visibility, [SESSION_VISIBLE_READ_ONLY, SESSION_VISIBLE, SESSION_INVISIBLE])) {
1937
                $session_visibility = SESSION_VISIBLE_READ_ONLY;
1938
            }
1939
        }
1940
1941
        $sql = "SELECT user_id FROM $tbl_session_rel_course_rel_user
1942
                WHERE session_id = $sessionId AND status = 0";
1943
        $result = Database::query($sql);
1944
        $existingUsers = [];
1945
        while ($row = Database::fetch_array($result)) {
1946
            $existingUsers[] = $row['user_id'];
1947
        }
1948
1949
        $sql = "SELECT c_id FROM $tbl_session_rel_course
1950
                WHERE session_id = $sessionId";
1951
        $result = Database::query($sql);
1952
        $course_list = [];
1953
        while ($row = Database::fetch_array($result)) {
1954
            $course_list[] = $row['c_id'];
1955
        }
1956
1957
        if ($session->getSendSubscriptionNotification() &&
1958
            is_array($userList)
1959
        ) {
1960
            // Sending emails only
1961
            foreach ($userList as $user_id) {
1962
                if (in_array($user_id, $existingUsers)) {
1963
                    continue;
1964
                }
1965
1966
                $tplSubject = new Template(
1967
                    null,
1968
                    false,
1969
                    false,
1970
                    false,
1971
                    false,
1972
                    false
1973
                );
1974
                $layoutSubject = $tplSubject->get_template(
1975
                    'mail/subject_subscription_to_session_confirmation.tpl'
1976
                );
1977
                $subject = $tplSubject->fetch($layoutSubject);
1978
                $user_info = api_get_user_info($user_id);
1979
1980
                $tplContent = new Template(
1981
                    null,
1982
                    false,
1983
                    false,
1984
                    false,
1985
                    false,
1986
                    false
1987
                );
1988
                // Variables for default template
1989
                $tplContent->assign('complete_name', stripslashes($user_info['complete_name']));
1990
                $tplContent->assign('session_name', $session->getName());
1991
                $tplContent->assign(
1992
                    'session_coach',
1993
                    UserManager::formatUserFullName($session->getGeneralCoach())
1994
                );
1995
                $layoutContent = $tplContent->get_template(
1996
                    'mail/content_subscription_to_session_confirmation.tpl'
1997
                );
1998
                $content = $tplContent->fetch($layoutContent);
1999
2000
                api_mail_html(
2001
                    $user_info['complete_name'],
2002
                    $user_info['mail'],
2003
                    $subject,
2004
                    $content,
2005
                    api_get_person_name(
2006
                        api_get_setting('administratorName'),
2007
                        api_get_setting('administratorSurname')
2008
                    ),
2009
                    api_get_setting('emailAdministrator')
2010
                );
2011
            }
2012
        }
2013
2014
        if ($registerUsersToAllCourses) {
2015
            foreach ($course_list as $courseId) {
2016
                // for each course in the session
2017
                $nbr_users = 0;
2018
                $courseId = (int) $courseId;
2019
2020
                $sql = "SELECT DISTINCT user_id
2021
                        FROM $tbl_session_rel_course_rel_user
2022
                        WHERE
2023
                            session_id = $sessionId AND
2024
                            c_id = $courseId AND
2025
                            status = 0
2026
                        ";
2027
                $result = Database::query($sql);
2028
                $existingUsers = [];
2029
                while ($row = Database::fetch_array($result)) {
2030
                    $existingUsers[] = $row['user_id'];
2031
                }
2032
2033
                // Delete existing users
2034
                if ($empty_users) {
2035
                    foreach ($existingUsers as $existing_user) {
2036
                        if (!in_array($existing_user, $userList)) {
2037
                            self::unSubscribeUserFromCourseSession($existing_user, $courseId, $sessionId);
2038
                        }
2039
                    }
2040
                }
2041
2042
                // Replace with this new function
2043
                // insert new users into session_rel_course_rel_user and ignore if they already exist
2044
                foreach ($userList as $enreg_user) {
2045
                    if (!in_array($enreg_user, $existingUsers)) {
2046
                        $status = self::get_user_status_in_course_session(
2047
                            $enreg_user,
2048
                            $courseId,
2049
                            $sessionId
2050
                        );
2051
2052
                        // Avoid duplicate entries.
2053
                        if ($status === false || ($status !== false && $status != 0)) {
2054
                            $enreg_user = (int) $enreg_user;
2055
                            $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user (session_id, c_id, user_id, visibility, status)
2056
                                    VALUES($sessionId, $courseId, $enreg_user, $session_visibility, 0)";
2057
                            $result = Database::query($sql);
2058
                            if (Database::affected_rows($result)) {
2059
                                $nbr_users++;
2060
                            }
2061
2062
                            Event::addEvent(
2063
                                LOG_SESSION_ADD_USER_COURSE,
2064
                                LOG_USER_ID,
2065
                                $enreg_user,
2066
                                api_get_utc_datetime(),
2067
                                api_get_user_id(),
2068
                                $courseId,
2069
                                $sessionId
2070
                            );
2071
                        }
2072
                    }
2073
                }
2074
2075
                // Count users in this session-course relation
2076
                $sql = "SELECT COUNT(user_id) as nbUsers
2077
                        FROM $tbl_session_rel_course_rel_user
2078
                        WHERE session_id = $sessionId AND c_id = $courseId AND status<>2";
2079
                $rs = Database::query($sql);
2080
                list($nbr_users) = Database::fetch_array($rs);
2081
                // update the session-course relation to add the users total
2082
                $sql = "UPDATE $tbl_session_rel_course SET nbr_users = $nbr_users
2083
                        WHERE session_id = $sessionId AND c_id = $courseId";
2084
                Database::query($sql);
2085
            }
2086
        }
2087
2088
        // Delete users from the session
2089
        if ($empty_users === true) {
2090
            $sql = "DELETE FROM $tbl_session_rel_user
2091
                    WHERE 
2092
                      session_id = $sessionId AND 
2093
                      relation_type <> ".SESSION_RELATION_TYPE_RRHH;
2094
            // Don't reset session_rel_user.registered_at of users that will be registered later anyways.
2095
            if (!empty($userList)) {
2096
                $avoidDeleteThisUsers = " AND user_id NOT IN ('".implode("','", $userList)."')";
2097
                $sql .= $avoidDeleteThisUsers;
2098
            }
2099
            Event::addEvent(
2100
                LOG_SESSION_DELETE_USER,
2101
                LOG_USER_ID,
2102
                'all',
2103
                api_get_utc_datetime(),
2104
                api_get_user_id(),
2105
                null,
2106
                $sessionId
2107
            );
2108
            Database::query($sql);
2109
        }
2110
2111
        // Insert missing users into session
2112
        foreach ($userList as $enreg_user) {
2113
            $isUserSubscribed = self::isUserSubscribedAsStudent($sessionId, $enreg_user);
2114
            if ($isUserSubscribed === false) {
2115
                $enreg_user = (int) $enreg_user;
2116
                $sql = "INSERT IGNORE INTO $tbl_session_rel_user (relation_type, session_id, user_id, registered_at)
2117
                        VALUES (0, $sessionId, $enreg_user, '".api_get_utc_datetime()."')";
2118
                Database::query($sql);
2119
                Event::addEvent(
2120
                    LOG_SESSION_ADD_USER,
2121
                    LOG_USER_ID,
2122
                    $enreg_user,
2123
                    api_get_utc_datetime(),
2124
                    api_get_user_id(),
2125
                    null,
2126
                    $sessionId
2127
                );
2128
            }
2129
        }
2130
2131
        // update number of users in the session
2132
        $sql = "UPDATE $tbl_session 
2133
                SET nbr_users = (SELECT count(user_id) FROM $tbl_session_rel_user WHERE session_id = $sessionId)
2134
                WHERE id = $sessionId";
2135
        Database::query($sql);
2136
    }
2137
2138
    /**
2139
     * Returns user list of the current users subscribed in the course-session.
2140
     *
2141
     * @param int   $sessionId
2142
     * @param array $courseInfo
2143
     * @param int   $status
2144
     *
2145
     * @return array
2146
     */
2147
    public static function getUsersByCourseSession(
2148
        $sessionId,
2149
        $courseInfo,
2150
        $status = null
2151
    ) {
2152
        $sessionId = intval($sessionId);
2153
        $courseId = $courseInfo['real_id'];
2154
2155
        if (empty($sessionId) || empty($courseId)) {
2156
            return [];
2157
        }
2158
2159
        $statusCondition = null;
2160
        if (isset($status) && !is_null($status)) {
2161
            $status = intval($status);
2162
            $statusCondition = " AND status = $status";
2163
        }
2164
2165
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2166
2167
        $sql = "SELECT DISTINCT user_id
2168
                FROM $table
2169
                WHERE
2170
                    session_id = $sessionId AND
2171
                    c_id = $courseId
2172
                    $statusCondition
2173
                ";
2174
2175
        $result = Database::query($sql);
2176
        $existingUsers = [];
2177
        while ($row = Database::fetch_array($result)) {
2178
            $existingUsers[] = $row['user_id'];
2179
        }
2180
2181
        return $existingUsers;
2182
    }
2183
2184
    /**
2185
     * Returns user list of the current users subscribed in the course-session.
2186
     *
2187
     * @param array $sessionList
2188
     * @param array $courseList
2189
     * @param int   $status
2190
     * @param int   $start
2191
     * @param int   $limit
2192
     *
2193
     * @return array
2194
     */
2195
    public static function getUsersByCourseAndSessionList(
2196
        $sessionList,
2197
        $courseList,
2198
        $status = null,
2199
        $start = null,
2200
        $limit = null
2201
    ) {
2202
        if (empty($sessionList) || empty($courseList)) {
2203
            return [];
2204
        }
2205
        $sessionListToString = implode("','", $sessionList);
2206
        $courseListToString = implode("','", $courseList);
2207
2208
        $statusCondition = null;
2209
        if (isset($status) && !is_null($status)) {
2210
            $status = intval($status);
2211
            $statusCondition = " AND status = $status";
2212
        }
2213
2214
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2215
2216
        $sql = "SELECT DISTINCT user_id
2217
                FROM $table
2218
                WHERE
2219
                    session_id IN ('$sessionListToString') AND
2220
                    c_id IN ('$courseListToString')
2221
                    $statusCondition
2222
                ";
2223
        if (!is_null($start) && !is_null($limit)) {
2224
            $start = (int) $start;
2225
            $limit = (int) $limit;
2226
            $sql .= "LIMIT $start, $limit";
2227
        }
2228
        $result = Database::query($sql);
2229
        $existingUsers = [];
2230
        while ($row = Database::fetch_array($result)) {
2231
            $existingUsers[] = $row['user_id'];
2232
        }
2233
2234
        return $existingUsers;
2235
    }
2236
2237
    /**
2238
     * Remove a list of users from a course-session.
2239
     *
2240
     * @param array $userList
2241
     * @param int   $sessionId
2242
     * @param array $courseInfo
2243
     * @param int   $status
2244
     * @param bool  $updateTotal
2245
     *
2246
     * @return bool
2247
     */
2248
    public static function removeUsersFromCourseSession(
2249
        $userList,
2250
        $sessionId,
2251
        $courseInfo,
2252
        $status = null,
2253
        $updateTotal = true
2254
    ) {
2255
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2256
        $tableSessionCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
2257
        $sessionId = intval($sessionId);
2258
2259
        if (empty($sessionId) || empty($userList) || empty($courseInfo)) {
2260
            return false;
2261
        }
2262
2263
        is_array($courseInfo) ? $courseId = $courseInfo['real_id'] : $courseId = $courseInfo;
2264
2265
        $statusCondition = null;
2266
        if (isset($status) && !is_null($status)) {
2267
            $status = (int) $status;
2268
            $statusCondition = " AND status = $status";
2269
        }
2270
2271
        foreach ($userList as $userId) {
2272
            $userId = (int) $userId;
2273
            $sql = "DELETE FROM $table
2274
                    WHERE
2275
                        session_id = $sessionId AND
2276
                        c_id = $courseId AND
2277
                        user_id = $userId
2278
                        $statusCondition
2279
                    ";
2280
            Database::query($sql);
2281
2282
            Event::addEvent(
2283
                LOG_SESSION_DELETE_USER_COURSE,
2284
                LOG_USER_ID,
2285
                $userId,
2286
                api_get_utc_datetime(),
2287
                api_get_user_id(),
2288
                $courseId,
2289
                $sessionId
2290
            );
2291
        }
2292
2293
        if ($updateTotal) {
2294
            // Count users in this session-course relation
2295
            $sql = "SELECT COUNT(user_id) as nbUsers
2296
                    FROM $table
2297
                    WHERE
2298
                        session_id = $sessionId AND
2299
                        c_id = $courseId AND
2300
                        status <> 2";
2301
            $result = Database::query($sql);
2302
            list($userCount) = Database::fetch_array($result);
2303
2304
            // update the session-course relation to add the users total
2305
            $sql = "UPDATE $tableSessionCourse
2306
                    SET nbr_users = $userCount
2307
                    WHERE
2308
                        session_id = $sessionId AND
2309
                        c_id = $courseId";
2310
            Database::query($sql);
2311
        }
2312
    }
2313
2314
    /**
2315
     * Subscribe a user to an specific course inside a session.
2316
     *
2317
     * @param array  $user_list
2318
     * @param int    $session_id
2319
     * @param string $course_code
2320
     * @param int    $session_visibility
2321
     * @param bool   $removeUsersNotInList
2322
     *
2323
     * @return bool
2324
     */
2325
    public static function subscribe_users_to_session_course(
2326
        $user_list,
2327
        $session_id,
2328
        $course_code,
2329
        $session_visibility = SESSION_VISIBLE_READ_ONLY,
2330
        $removeUsersNotInList = false
2331
    ) {
2332
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
2333
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
2334
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2335
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
2336
2337
        if (empty($session_id) || empty($course_code)) {
2338
            return false;
2339
        }
2340
2341
        $session_id = (int) $session_id;
2342
        $session_visibility = (int) $session_visibility;
2343
        $course_code = Database::escape_string($course_code);
2344
        $courseInfo = api_get_course_info($course_code);
2345
        $courseId = $courseInfo['real_id'];
2346
2347
        if ($removeUsersNotInList) {
2348
            $currentUsers = self::getUsersByCourseSession($session_id, $courseInfo, 0);
2349
2350
            if (!empty($user_list)) {
2351
                $userToDelete = array_diff($currentUsers, $user_list);
2352
            } else {
2353
                $userToDelete = $currentUsers;
2354
            }
2355
2356
            if (!empty($userToDelete)) {
2357
                self::removeUsersFromCourseSession(
2358
                    $userToDelete,
2359
                    $session_id,
2360
                    $courseInfo,
2361
                    0,
2362
                    true
2363
                );
2364
            }
2365
        }
2366
2367
        $nbr_users = 0;
2368
        foreach ($user_list as $enreg_user) {
2369
            $enreg_user = (int) $enreg_user;
2370
            // Checking if user exists in session - course - user table.
2371
            $sql = "SELECT count(user_id) as count
2372
                    FROM $tbl_session_rel_course_rel_user
2373
                    WHERE
2374
                        session_id = $session_id AND
2375
                        c_id = $courseId and
2376
                        user_id = $enreg_user ";
2377
            $result = Database::query($sql);
2378
            $count = 0;
2379
2380
            if (Database::num_rows($result) > 0) {
2381
                $row = Database::fetch_array($result, 'ASSOC');
2382
                $count = $row['count'];
2383
            }
2384
2385
            if ($count == 0) {
2386
                $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user (session_id, c_id, user_id, visibility)
2387
                        VALUES ($session_id, $courseId, $enreg_user, $session_visibility)";
2388
                $result = Database::query($sql);
2389
                if (Database::affected_rows($result)) {
2390
                    $nbr_users++;
2391
                }
2392
            }
2393
2394
            // Checking if user exists in session - user table.
2395
            $sql = "SELECT count(user_id) as count
2396
                    FROM $tbl_session_rel_user
2397
                    WHERE session_id = $session_id AND user_id = $enreg_user ";
2398
            $result = Database::query($sql);
2399
            $count = 0;
2400
2401
            if (Database::num_rows($result) > 0) {
2402
                $row = Database::fetch_array($result, 'ASSOC');
2403
                $count = $row['count'];
2404
            }
2405
2406
            if (empty($count)) {
2407
                // If user is not registered to a session then add it.
2408
                $sql = "INSERT IGNORE INTO $tbl_session_rel_user (session_id, user_id, registered_at)
2409
                        VALUES ($session_id, $enreg_user, '".api_get_utc_datetime()."')";
2410
                Database::query($sql);
2411
2412
                $sql = "UPDATE $tbl_session SET nbr_users = nbr_users + 1
2413
                        WHERE id = $session_id ";
2414
                Database::query($sql);
2415
            }
2416
        }
2417
2418
        // count users in this session-course relation
2419
        $sql = "SELECT COUNT(user_id) as nbUsers
2420
                FROM $tbl_session_rel_course_rel_user
2421
                WHERE session_id = $session_id AND c_id = $courseId AND status <> 2";
2422
        $rs = Database::query($sql);
2423
        list($nbr_users) = Database::fetch_array($rs);
2424
        // update the session-course relation to add the users total
2425
        $sql = "UPDATE $tbl_session_rel_course
2426
                SET nbr_users = $nbr_users
2427
                WHERE session_id = $session_id AND c_id = $courseId";
2428
        Database::query($sql);
2429
    }
2430
2431
    /**
2432
     * Unsubscribe user from session.
2433
     *
2434
     * @param int Session id
2435
     * @param int User id
2436
     *
2437
     * @return bool True in case of success, false in case of error
2438
     */
2439
    public static function unsubscribe_user_from_session($session_id, $user_id)
2440
    {
2441
        $session_id = (int) $session_id;
2442
        $user_id = (int) $user_id;
2443
2444
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2445
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
2446
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
2447
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
2448
2449
        $sql = "DELETE FROM $tbl_session_rel_user
2450
                WHERE
2451
                    session_id = $session_id AND
2452
                    user_id = $user_id AND
2453
                    relation_type <> ".SESSION_RELATION_TYPE_RRHH;
2454
        $result = Database::query($sql);
2455
        $return = Database::affected_rows($result);
2456
2457
        // Update number of users
2458
        $sql = "UPDATE $tbl_session
2459
                SET nbr_users = nbr_users - $return
2460
                WHERE id = $session_id ";
2461
        Database::query($sql);
2462
2463
        Event::addEvent(
2464
            LOG_SESSION_DELETE_USER,
2465
            LOG_USER_ID,
2466
            $user_id,
2467
            api_get_utc_datetime(),
2468
            api_get_user_id(),
2469
            null,
2470
            $session_id
2471
        );
2472
2473
        // Get the list of courses related to this session
2474
        $course_list = self::get_course_list_by_session_id($session_id);
2475
        if (!empty($course_list)) {
2476
            foreach ($course_list as $course) {
2477
                self::unSubscribeUserFromCourseSession($user_id, $course['id'], $session_id);
2478
            }
2479
        }
2480
2481
        return true;
2482
    }
2483
2484
    /**
2485
     * @param int $user_id
2486
     * @param int $courseId
2487
     * @param int $session_id
2488
     */
2489
    public static function unSubscribeUserFromCourseSession($user_id, $courseId, $session_id)
2490
    {
2491
        $user_id = (int) $user_id;
2492
        $courseId = (int) $courseId;
2493
        $session_id = (int) $session_id;
2494
2495
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2496
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
2497
2498
        // Delete user from course
2499
        $sql = "DELETE FROM $tbl_session_rel_course_rel_user
2500
                WHERE session_id = $session_id AND c_id = $courseId AND user_id = $user_id";
2501
        $result = Database::query($sql);
2502
2503
        if (Database::affected_rows($result)) {
2504
            // Update number of users in this relation
2505
            $sql = "UPDATE $tbl_session_rel_course SET 
2506
                    nbr_users = nbr_users - 1
2507
                    WHERE session_id = $session_id AND c_id = $courseId";
2508
            Database::query($sql);
2509
        }
2510
2511
        Event::addEvent(
2512
            LOG_SESSION_DELETE_USER_COURSE,
2513
            LOG_USER_ID,
2514
            $user_id,
2515
            api_get_utc_datetime(),
2516
            api_get_user_id(),
2517
            $courseId,
2518
            $session_id
2519
        );
2520
    }
2521
2522
    /**
2523
     * Subscribes courses to the given session and optionally (default)
2524
     * unsubscribe previous users.
2525
     *
2526
     * @author Carlos Vargas from existing code
2527
     *
2528
     * @param int   $sessionId
2529
     * @param array $courseList                     List of courses int ids
2530
     * @param bool  $removeExistingCoursesWithUsers Whether to unsubscribe
2531
     *                                              existing courses and users (true, default) or not (false)
2532
     * @param bool  $copyEvaluation                 from base course to session course
2533
     *
2534
     * @throws Exception
2535
     *
2536
     * @return bool False on failure, true otherwise
2537
     * */
2538
    public static function add_courses_to_session(
2539
        $sessionId,
2540
        $courseList,
2541
        $removeExistingCoursesWithUsers = true,
2542
        $copyEvaluation = false
2543
    ) {
2544
        $sessionId = intval($sessionId);
2545
2546
        if (empty($sessionId) || empty($courseList)) {
2547
            return false;
2548
        }
2549
2550
        $em = Database::getManager();
2551
2552
        /** @var Session $session */
2553
        $session = $em->find('ChamiloCoreBundle:Session', $sessionId);
2554
2555
        if (!$session) {
2556
            return false;
2557
        }
2558
        $sessionVisibility = $session->getVisibility();
2559
2560
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2561
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
2562
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
2563
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
2564
2565
        // Get list of courses subscribed to this session
2566
        $sql = "SELECT c_id
2567
                FROM $tbl_session_rel_course
2568
                WHERE session_id = $sessionId";
2569
        $rs = Database::query($sql);
2570
        $existingCourses = Database::store_result($rs);
2571
        $nbr_courses = count($existingCourses);
2572
2573
        // Get list of users subscribed to this session
2574
        $sql = "SELECT user_id
2575
                FROM $tbl_session_rel_user
2576
                WHERE
2577
                    session_id = $sessionId AND
2578
                    relation_type<>".SESSION_RELATION_TYPE_RRHH;
2579
        $result = Database::query($sql);
2580
        $user_list = Database::store_result($result);
2581
2582
        // Remove existing courses from the session.
2583
        if ($removeExistingCoursesWithUsers === true && !empty($existingCourses)) {
2584
            foreach ($existingCourses as $existingCourse) {
2585
                if (!in_array($existingCourse['c_id'], $courseList)) {
2586
                    $sql = "DELETE FROM $tbl_session_rel_course
2587
                            WHERE
2588
                                c_id = ".$existingCourse['c_id']." AND
2589
                                session_id = $sessionId";
2590
                    Database::query($sql);
2591
2592
                    $sql = "DELETE FROM $tbl_session_rel_course_rel_user
2593
                            WHERE
2594
                                c_id = ".$existingCourse['c_id']." AND
2595
                                session_id = $sessionId";
2596
                    Database::query($sql);
2597
2598
                    Event::addEvent(
2599
                        LOG_SESSION_DELETE_COURSE,
2600
                        LOG_COURSE_ID,
2601
                        $existingCourse['c_id'],
2602
                        api_get_utc_datetime(),
2603
                        api_get_user_id(),
2604
                        $existingCourse['c_id'],
2605
                        $sessionId
2606
                    );
2607
2608
                    CourseManager::remove_course_ranking(
2609
                        $existingCourse['c_id'],
2610
                        $sessionId
2611
                    );
2612
                    $nbr_courses--;
2613
                }
2614
            }
2615
        }
2616
2617
        // Pass through the courses list we want to add to the session
2618
        foreach ($courseList as $courseId) {
2619
            $courseInfo = api_get_course_info_by_id($courseId);
2620
2621
            // If course doesn't exists continue!
2622
            if (empty($courseInfo)) {
2623
                continue;
2624
            }
2625
2626
            $exists = false;
2627
            // check if the course we want to add is already subscribed
2628
            foreach ($existingCourses as $existingCourse) {
2629
                if ($courseId == $existingCourse['c_id']) {
2630
                    $exists = true;
2631
                }
2632
            }
2633
2634
            if (!$exists) {
2635
                // Copy gradebook categories and links (from base course)
2636
                // to the new course session
2637
                if ($copyEvaluation) {
2638
                    $cats = Category::load(null, null, $courseInfo['code']);
2639
                    if (!empty($cats)) {
2640
                        $sessionCategory = Category:: load(
2641
                            null,
2642
                            null,
2643
                            $courseInfo['code'],
2644
                            null,
2645
                            null,
2646
                            $sessionId,
2647
                            false
2648
                        );
2649
2650
                        // @todo remove commented code
2651
                        if (empty($sessionCategory)) {
2652
                            // There is no category for this course+session, so create one
2653
                            $cat = new Category();
2654
                            $sessionName = $session->getName();
2655
                            $cat->set_name($courseInfo['code'].' - '.get_lang('Session').' '.$sessionName);
2656
                            $cat->set_session_id($sessionId);
2657
                            $cat->set_course_code($courseInfo['code']);
2658
                            $cat->set_description(null);
2659
                            //$cat->set_user_id($stud_id);
2660
                            $cat->set_parent_id(0);
2661
                            $cat->set_weight(100);
2662
                            $cat->set_visible(0);
2663
                            $cat->set_certificate_min_score(75);
2664
                            $cat->add();
2665
                            $sessionGradeBookCategoryId = $cat->get_id();
2666
                        } else {
2667
                            if (!empty($sessionCategory[0])) {
2668
                                $sessionGradeBookCategoryId = $sessionCategory[0]->get_id();
2669
                            }
2670
                        }
2671
2672
                        $categoryIdList = [];
2673
                        /** @var Category $cat */
2674
                        foreach ($cats as $cat) {
2675
                            $categoryIdList[$cat->get_id()] = $cat->get_id();
2676
                        }
2677
2678
                        $newCategoryIdList = [];
2679
                        foreach ($cats as $cat) {
2680
                            $links = $cat->get_links(
2681
                                null,
2682
                                false,
2683
                                $courseInfo['code'],
2684
                                0
2685
                            );
2686
2687
                            //$cat->set_session_id($sessionId);
2688
                            //$oldCategoryId = $cat->get_id();
2689
                            //$newId = $cat->add();
2690
                            //$newCategoryIdList[$oldCategoryId] = $newId;
2691
                            //$parentId = $cat->get_parent_id();
2692
2693
                            /*if (!empty($parentId)) {
2694
                                $newParentId = $newCategoryIdList[$parentId];
2695
                                $cat->set_parent_id($newParentId);
2696
                                $cat->save();
2697
                            }*/
2698
2699
                            if (!empty($links)) {
2700
                                /** @var AbstractLink $link */
2701
                                foreach ($links as $link) {
2702
                                    //$newCategoryId = $newCategoryIdList[$link->get_category_id()];
2703
                                    $link->set_category_id(
2704
                                        $sessionGradeBookCategoryId
2705
                                    );
2706
                                    $link->add();
2707
                                }
2708
                            }
2709
2710
                            $evaluationList = $cat->get_evaluations(
2711
                                null,
2712
                                false,
2713
                                $courseInfo['code'],
2714
                                0
2715
                            );
2716
2717
                            if (!empty($evaluationList)) {
2718
                                /** @var Evaluation $evaluation */
2719
                                foreach ($evaluationList as $evaluation) {
2720
                                    //$evaluationId = $newCategoryIdList[$evaluation->get_category_id()];
2721
                                    $evaluation->set_category_id(
2722
                                        $sessionGradeBookCategoryId
2723
                                    );
2724
                                    $evaluation->add();
2725
                                }
2726
                            }
2727
                        }
2728
2729
                        // Create
2730
                        DocumentManager::generateDefaultCertificate(
2731
                            $courseInfo,
2732
                            true,
2733
                            $sessionId
2734
                        );
2735
                    }
2736
                }
2737
2738
                // If the course isn't subscribed yet
2739
                $sql = "INSERT INTO $tbl_session_rel_course (session_id, c_id, nbr_users, position)
2740
                        VALUES ($sessionId, $courseId, 0, 0)";
2741
                Database::query($sql);
2742
2743
                Event::addEvent(
2744
                    LOG_SESSION_ADD_COURSE,
2745
                    LOG_COURSE_ID,
2746
                    $courseId,
2747
                    api_get_utc_datetime(),
2748
                    api_get_user_id(),
2749
                    $courseId,
2750
                    $sessionId
2751
                );
2752
2753
                // We add the current course in the existing courses array,
2754
                // to avoid adding another time the current course
2755
                $existingCourses[] = ['c_id' => $courseId];
2756
                $nbr_courses++;
2757
2758
                // subscribe all the users from the session to this course inside the session
2759
                $nbr_users = 0;
2760
                foreach ($user_list as $enreg_user) {
2761
                    $enreg_user_id = intval($enreg_user['user_id']);
2762
                    $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user (session_id, c_id, user_id, visibility)
2763
                            VALUES ($sessionId, $courseId, $enreg_user_id, $sessionVisibility)";
2764
                    $result = Database::query($sql);
2765
2766
                    Event::addEvent(
2767
                        LOG_SESSION_ADD_USER_COURSE,
2768
                        LOG_USER_ID,
2769
                        $enreg_user_id,
2770
                        api_get_utc_datetime(),
2771
                        api_get_user_id(),
2772
                        $courseId,
2773
                        $sessionId
2774
                    );
2775
2776
                    if (Database::affected_rows($result)) {
2777
                        $nbr_users++;
2778
                    }
2779
                }
2780
                $sql = "UPDATE $tbl_session_rel_course
2781
                        SET nbr_users = $nbr_users
2782
                        WHERE session_id = $sessionId AND c_id = $courseId";
2783
                Database::query($sql);
2784
            }
2785
        }
2786
2787
        $sql = "UPDATE $tbl_session
2788
                SET nbr_courses = $nbr_courses
2789
                WHERE id = $sessionId";
2790
        Database::query($sql);
2791
2792
        return true;
2793
    }
2794
2795
    /**
2796
     * Unsubscribe course from a session.
2797
     *
2798
     * @param int $session_id
2799
     * @param int $course_id
2800
     *
2801
     * @return bool True in case of success, false otherwise
2802
     */
2803
    public static function unsubscribe_course_from_session($session_id, $course_id)
2804
    {
2805
        $session_id = (int) $session_id;
2806
        $course_id = (int) $course_id;
2807
2808
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
2809
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2810
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
2811
2812
        // Get course code
2813
        $course_code = CourseManager::get_course_code_from_course_id($course_id);
2814
        $course_id = intval($course_id);
2815
2816
        if (empty($course_code)) {
2817
            return false;
2818
        }
2819
2820
        // Unsubscribe course
2821
        $sql = "DELETE FROM $tbl_session_rel_course
2822
                WHERE c_id = $course_id AND session_id = $session_id";
2823
        $result = Database::query($sql);
2824
        $nb_affected = Database::affected_rows($result);
2825
2826
        $sql = "DELETE FROM $tbl_session_rel_course_rel_user
2827
                WHERE c_id = $course_id AND session_id = $session_id";
2828
        Database::query($sql);
2829
2830
        Event::addEvent(
2831
            LOG_SESSION_DELETE_COURSE,
2832
            LOG_COURSE_ID,
2833
            $course_id,
2834
            api_get_utc_datetime(),
2835
            api_get_user_id(),
2836
            $course_id,
2837
            $session_id
2838
        );
2839
2840
        if ($nb_affected > 0) {
2841
            // Update number of courses in the session
2842
            $sql = "UPDATE $tbl_session SET nbr_courses= nbr_courses - $nb_affected
2843
                    WHERE id = $session_id";
2844
            Database::query($sql);
2845
2846
            return true;
2847
        } else {
2848
            return false;
2849
        }
2850
    }
2851
2852
    /**
2853
     * Creates a new extra field for a given session.
2854
     *
2855
     * @param string $variable    Field's internal variable name
2856
     * @param int    $fieldType   Field's type
2857
     * @param string $displayText Field's language var name
2858
     * @param string $default     Field's default value
2859
     *
2860
     * @return int new extra field id
2861
     */
2862
    public static function create_session_extra_field(
2863
        $variable,
2864
        $fieldType,
2865
        $displayText,
2866
        $default = ''
2867
    ) {
2868
        $extraField = new ExtraFieldModel('session');
2869
        $params = [
2870
            'variable' => $variable,
2871
            'field_type' => $fieldType,
2872
            'display_text' => $displayText,
2873
            'default_value' => $default,
2874
        ];
2875
2876
        return $extraField->save($params);
2877
    }
2878
2879
    /**
2880
     * Update an extra field value for a given session.
2881
     *
2882
     * @param int    $sessionId Session ID
2883
     * @param string $variable  Field variable name
2884
     * @param string $value     Optional. Default field value
2885
     *
2886
     * @return bool|int An integer when register a new extra field. And boolean when update the extrafield
2887
     */
2888
    public static function update_session_extra_field_value($sessionId, $variable, $value = '')
2889
    {
2890
        $extraFieldValue = new ExtraFieldValue('session');
2891
        $params = [
2892
            'item_id' => $sessionId,
2893
            'variable' => $variable,
2894
            'value' => $value,
2895
        ];
2896
2897
        return $extraFieldValue->save($params);
2898
    }
2899
2900
    /**
2901
     * Checks the relationship between a session and a course.
2902
     *
2903
     * @param int $session_id
2904
     * @param int $courseId
2905
     *
2906
     * @return bool returns TRUE if the session and the course are related, FALSE otherwise
2907
     * */
2908
    public static function relation_session_course_exist($session_id, $courseId)
2909
    {
2910
        $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
2911
        $return_value = false;
2912
        $sql = "SELECT c_id FROM $tbl_session_course
2913
                WHERE
2914
                  session_id = ".intval($session_id)." AND
2915
                  c_id = ".intval($courseId);
2916
        $result = Database::query($sql);
2917
        $num = Database::num_rows($result);
2918
        if ($num > 0) {
2919
            $return_value = true;
2920
        }
2921
2922
        return $return_value;
2923
    }
2924
2925
    /**
2926
     * Get the session information by name.
2927
     *
2928
     * @param string $name
2929
     *
2930
     * @return mixed false if the session does not exist, array if the session exist
2931
     */
2932
    public static function get_session_by_name($name)
2933
    {
2934
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
2935
        $name = Database::escape_string(trim($name));
2936
        if (empty($name)) {
2937
            return false;
2938
        }
2939
2940
        $sql = 'SELECT *
2941
		        FROM '.$tbl_session.'
2942
		        WHERE name = "'.$name.'"';
2943
        $result = Database::query($sql);
2944
        $num = Database::num_rows($result);
2945
        if ($num > 0) {
2946
            return Database::fetch_array($result);
2947
        } else {
2948
            return false;
2949
        }
2950
    }
2951
2952
    /**
2953
     * @param int $sessionId
2954
     * @param int $name
2955
     *
2956
     * @return bool
2957
     */
2958
    public static function sessionNameExistBesidesMySession($sessionId, $name)
2959
    {
2960
        $table = Database::get_main_table(TABLE_MAIN_SESSION);
2961
        $name = Database::escape_string(trim($name));
2962
        if (empty($name)) {
2963
            return false;
2964
        }
2965
2966
        $sql = "SELECT *
2967
		        FROM $table
2968
		        WHERE name = '$name' AND id <> $sessionId ";
2969
        $result = Database::query($sql);
2970
        $num = Database::num_rows($result);
2971
        if ($num > 0) {
2972
            return true;
2973
        }
2974
2975
        return false;
2976
    }
2977
2978
    /**
2979
     * Create a session category.
2980
     *
2981
     * @author Jhon Hinojosa <[email protected]>, from existing code
2982
     *
2983
     * @param string        name
2984
     * @param int        year_start
2985
     * @param int        month_start
2986
     * @param int        day_start
2987
     * @param int        year_end
2988
     * @param int        month_end
2989
     * @param int        day_end
2990
     *
2991
     * @return int session ID
2992
     * */
2993
    public static function create_category_session(
2994
        $sname,
2995
        $syear_start,
2996
        $smonth_start,
2997
        $sday_start,
2998
        $syear_end,
2999
        $smonth_end,
3000
        $sday_end
3001
    ) {
3002
        $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
3003
        $name = trim($sname);
3004
        $year_start = intval($syear_start);
3005
        $month_start = intval($smonth_start);
3006
        $day_start = intval($sday_start);
3007
        $year_end = intval($syear_end);
3008
        $month_end = intval($smonth_end);
3009
        $day_end = intval($sday_end);
3010
3011
        $date_start = "$year_start-".(($month_start < 10) ? "0$month_start" : $month_start)."-".(($day_start < 10) ? "0$day_start" : $day_start);
3012
        $date_end = "$year_end-".(($month_end < 10) ? "0$month_end" : $month_end)."-".(($day_end < 10) ? "0$day_end" : $day_end);
3013
3014
        if (empty($name)) {
3015
            $msg = get_lang('SessionCategoryNameIsRequired');
3016
3017
            return $msg;
3018
        } elseif (!$month_start || !$day_start || !$year_start || !checkdate($month_start, $day_start, $year_start)) {
3019
            $msg = get_lang('InvalidStartDate');
3020
3021
            return $msg;
3022
        } elseif (!$month_end && !$day_end && !$year_end) {
3023
            $date_end = '';
3024
        } elseif (!$month_end || !$day_end || !$year_end || !checkdate($month_end, $day_end, $year_end)) {
3025
            $msg = get_lang('InvalidEndDate');
3026
3027
            return $msg;
3028
        } elseif ($date_start >= $date_end) {
3029
            $msg = get_lang('StartDateShouldBeBeforeEndDate');
3030
3031
            return $msg;
3032
        }
3033
3034
        $access_url_id = api_get_current_access_url_id();
3035
        $params = [
3036
            'name' => $name,
3037
            'date_start' => $date_start,
3038
            'access_url_id' => $access_url_id,
3039
        ];
3040
3041
        if (!empty($date_end)) {
3042
            $params['date_end'] = $date_end;
3043
        }
3044
3045
        $id = Database::insert($tbl_session_category, $params);
3046
3047
        // Add event to system log
3048
        $user_id = api_get_user_id();
3049
        Event::addEvent(
3050
            LOG_SESSION_CATEGORY_CREATE,
3051
            LOG_SESSION_CATEGORY_ID,
3052
            $id,
3053
            api_get_utc_datetime(),
3054
            $user_id
3055
        );
3056
3057
        return $id;
3058
    }
3059
3060
    /**
3061
     * Edit a sessions category.
3062
     *
3063
     * @author Jhon Hinojosa <[email protected]>,from existing code
3064
     *
3065
     * @param int        id
3066
     * @param string        name
3067
     * @param int        year_start
3068
     * @param int        month_start
3069
     * @param int        day_start
3070
     * @param int        year_end
3071
     * @param int        month_end
3072
     * @param int        day_end
3073
     *
3074
     * @return bool
3075
     *              The parameter id is a primary key
3076
     * */
3077
    public static function edit_category_session(
3078
        $id,
3079
        $sname,
3080
        $syear_start,
3081
        $smonth_start,
3082
        $sday_start,
3083
        $syear_end,
3084
        $smonth_end,
3085
        $sday_end
3086
    ) {
3087
        $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
3088
        $name = trim($sname);
3089
        $year_start = intval($syear_start);
3090
        $month_start = intval($smonth_start);
3091
        $day_start = intval($sday_start);
3092
        $year_end = intval($syear_end);
3093
        $month_end = intval($smonth_end);
3094
        $day_end = intval($sday_end);
3095
        $id = intval($id);
3096
        $date_start = "$year_start-".(($month_start < 10) ? "0$month_start" : $month_start)."-".(($day_start < 10) ? "0$day_start" : $day_start);
3097
        $date_end = "$year_end-".(($month_end < 10) ? "0$month_end" : $month_end)."-".(($day_end < 10) ? "0$day_end" : $day_end);
3098
3099
        if (empty($name)) {
3100
            $msg = get_lang('SessionCategoryNameIsRequired');
3101
3102
            return $msg;
3103
        } elseif (!$month_start || !$day_start || !$year_start || !checkdate($month_start, $day_start, $year_start)) {
3104
            $msg = get_lang('InvalidStartDate');
3105
3106
            return $msg;
3107
        } elseif (!$month_end && !$day_end && !$year_end) {
3108
            $date_end = null;
3109
        } elseif (!$month_end || !$day_end || !$year_end || !checkdate($month_end, $day_end, $year_end)) {
3110
            $msg = get_lang('InvalidEndDate');
3111
3112
            return $msg;
3113
        } elseif ($date_start >= $date_end) {
3114
            $msg = get_lang('StartDateShouldBeBeforeEndDate');
3115
3116
            return $msg;
3117
        }
3118
        if ($date_end != null) {
3119
            $sql = "UPDATE $tbl_session_category
3120
                    SET
3121
                        name = '".Database::escape_string($name)."',
3122
                        date_start = '$date_start' ,
3123
                        date_end = '$date_end'
3124
                    WHERE id= $id";
3125
        } else {
3126
            $sql = "UPDATE $tbl_session_category SET
3127
                        name = '".Database::escape_string($name)."',
3128
                        date_start = '$date_start',
3129
                        date_end = NULL
3130
                    WHERE id= $id";
3131
        }
3132
        $result = Database::query($sql);
3133
3134
        return $result ? true : false;
3135
    }
3136
3137
    /**
3138
     * Delete sessions categories.
3139
     *
3140
     * @author Jhon Hinojosa <[email protected]>, from existing code
3141
     *
3142
     * @param    array    id_checked
3143
     * @param    bool    include delete session
3144
     * @param    bool    optional, true if the function is called by a webservice, false otherwise
3145
     *
3146
     * @return bool Nothing, or false on error
3147
     *              The parameters is a array to delete sessions
3148
     * */
3149
    public static function delete_session_category($id_checked, $delete_session = false, $from_ws = false)
3150
    {
3151
        $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
3152
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
3153
        if (is_array($id_checked)) {
3154
            $id_checked = Database::escape_string(implode(',', $id_checked));
3155
        } else {
3156
            $id_checked = intval($id_checked);
3157
        }
3158
3159
        //Setting session_category_id to 0
3160
        $sql = "UPDATE $tbl_session SET session_category_id = NULL
3161
                WHERE session_category_id IN (".$id_checked.")";
3162
        Database::query($sql);
3163
3164
        $sql = "SELECT id FROM $tbl_session WHERE session_category_id IN (".$id_checked.")";
3165
        $result = Database::query($sql);
3166
        while ($rows = Database::fetch_array($result)) {
3167
            $session_id = $rows['id'];
3168
            if ($delete_session) {
3169
                if ($from_ws) {
3170
                    self::delete($session_id, true);
3171
                } else {
3172
                    self::delete($session_id);
3173
                }
3174
            }
3175
        }
3176
        $sql = "DELETE FROM $tbl_session_category WHERE id IN (".$id_checked.")";
3177
        Database::query($sql);
3178
3179
        // Add event to system log
3180
        $user_id = api_get_user_id();
3181
        Event::addEvent(
3182
            LOG_SESSION_CATEGORY_DELETE,
3183
            LOG_SESSION_CATEGORY_ID,
3184
            $id_checked,
3185
            api_get_utc_datetime(),
3186
            $user_id
3187
        );
3188
3189
        return true;
3190
    }
3191
3192
    /**
3193
     * Get a list of sessions of which the given conditions match with an = 'cond'.
3194
     *
3195
     * @param array $conditions          a list of condition example :
3196
     *                                   array('status' => STUDENT) or
3197
     *                                   array('s.name' => array('operator' => 'LIKE', value = '%$needle%'))
3198
     * @param array $order_by            a list of fields on which sort
3199
     * @param int   $urlId
3200
     * @param array $onlyThisSessionList
3201
     *
3202
     * @return array an array with all sessions of the platform
3203
     *
3204
     * @todo   optional course code parameter, optional sorting parameters...
3205
     */
3206
    public static function get_sessions_list(
3207
        $conditions = [],
3208
        $order_by = [],
3209
        $from = null,
3210
        $to = null,
3211
        $urlId = 0,
3212
        $onlyThisSessionList = []
3213
    ) {
3214
        $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
3215
        $session_category_table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
3216
        $user_table = Database::get_main_table(TABLE_MAIN_USER);
3217
        $table_access_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
3218
        $session_course_table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
3219
        $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
3220
        $urlId = empty($urlId) ? api_get_current_access_url_id() : (int) $urlId;
3221
        $return_array = [];
3222
3223
        $sql_query = " SELECT
3224
                    DISTINCT(s.id),
3225
                    s.name,
3226
                    s.nbr_courses,
3227
                    s.access_start_date,
3228
                    s.access_end_date,
3229
                    u.firstname,
3230
                    u.lastname,
3231
                    sc.name as category_name,
3232
                    s.promotion_id
3233
				FROM $session_table s
3234
				INNER JOIN $user_table u ON s.id_coach = u.user_id
3235
				INNER JOIN $table_access_url_rel_session ar ON ar.session_id = s.id
3236
				LEFT JOIN  $session_category_table sc ON s.session_category_id = sc.id
3237
				LEFT JOIN $session_course_table sco ON (sco.session_id = s.id)
3238
				INNER JOIN $course_table c ON sco.c_id = c.id
3239
				WHERE ar.access_url_id = $urlId ";
3240
3241
        $availableFields = [
3242
            's.id',
3243
            's.name',
3244
            'c.id',
3245
        ];
3246
3247
        $availableOperator = [
3248
            'like',
3249
            '>=',
3250
            '<=',
3251
            '=',
3252
        ];
3253
3254
        if (count($conditions) > 0) {
3255
            foreach ($conditions as $field => $options) {
3256
                $operator = strtolower($options['operator']);
3257
                $value = Database::escape_string($options['value']);
3258
                $sql_query .= ' AND ';
3259
                if (in_array($field, $availableFields) && in_array($operator, $availableOperator)) {
3260
                    $sql_query .= $field." $operator '".$value."'";
3261
                }
3262
            }
3263
        }
3264
3265
        if (!empty($onlyThisSessionList)) {
3266
            $onlyThisSessionList = array_map('intval', $onlyThisSessionList);
3267
            $onlyThisSessionList = implode("','", $onlyThisSessionList);
3268
            $sql_query .= " AND s.id IN ('$onlyThisSessionList') ";
3269
        }
3270
3271
        $orderAvailableList = ['name'];
3272
        if (count($order_by) > 0) {
3273
            $order = null;
3274
            $direction = null;
3275
            if (isset($order_by[0]) && in_array($order_by[0], $orderAvailableList)) {
3276
                $order = $order_by[0];
3277
            }
3278
            if (isset($order_by[1]) && in_array(strtolower($order_by[1]), ['desc', 'asc'])) {
3279
                $direction = $order_by[1];
3280
            }
3281
3282
            if (!empty($order)) {
3283
                $sql_query .= " ORDER BY $order $direction ";
3284
            }
3285
        }
3286
3287
        if (!is_null($from) && !is_null($to)) {
3288
            $to = (int) $to;
3289
            $from = (int) $from;
3290
            $sql_query .= "LIMIT $from, $to";
3291
        }
3292
3293
        $sql_result = Database::query($sql_query);
3294
        if (Database::num_rows($sql_result) > 0) {
3295
            while ($result = Database::fetch_array($sql_result)) {
3296
                $return_array[$result['id']] = $result;
3297
            }
3298
        }
3299
3300
        return $return_array;
3301
    }
3302
3303
    /**
3304
     * Get the session category information by id.
3305
     *
3306
     * @param string session category ID
3307
     *
3308
     * @return mixed false if the session category does not exist, array if the session category exists
3309
     */
3310
    public static function get_session_category($id)
3311
    {
3312
        $table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
3313
        $id = intval($id);
3314
        $sql = "SELECT id, name, date_start, date_end
3315
                FROM $table
3316
                WHERE id= $id";
3317
        $result = Database::query($sql);
3318
        $num = Database::num_rows($result);
3319
        if ($num > 0) {
3320
            return Database::fetch_array($result);
3321
        } else {
3322
            return false;
3323
        }
3324
    }
3325
3326
    /**
3327
     * Get the session image.
3328
     *
3329
     * @return image path
3330
     */
3331
    public static function getSessionImage($id)
3332
    {
3333
        $extraFieldValuesTable = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
3334
        $sql = "SELECT value  FROM extra_field_values WHERE field_id = 16 AND item_id = ".intval($id);
3335
        $result = Database::query($sql);
3336
        if (Database::num_rows($result) > 0) {
3337
            while ($row = Database::fetch_array($result, 'ASSOC')) {
3338
                $sessionImage = $row['value'];
3339
                $sessionImage = api_get_path(WEB_UPLOAD_PATH).$sessionImage;
3340
            }
3341
3342
            return $sessionImage;
3343
        } else {
3344
            $sessionImage = api_get_path(WEB_PUBLIC_PATH)."img/session_default.png";
3345
3346
            return $sessionImage;
3347
        }
3348
    }
3349
3350
    /**
3351
     * Get Hot Sessions (limit 8).
3352
     *
3353
     * @return array with sessions
3354
     */
3355
    public static function getHotSessions()
3356
    {
3357
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
3358
        $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
3359
        $tbl_users = Database::get_main_table(TABLE_MAIN_USER);
3360
        $tbl_extra_fields = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
3361
        $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
3362
        $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
3363
3364
        $extraField = new \ExtraField('session');
3365
        $field = $extraField->get_handler_field_info_by_field_variable('image');
3366
3367
        $sql = "SELECT 
3368
                s.id,
3369
                s.name,
3370
                s.id_coach,
3371
                u.firstname,
3372
                u.lastname,
3373
                s.session_category_id,
3374
                c.name as category_name,
3375
                s.description,
3376
                (SELECT COUNT(*) FROM $tbl_session_user WHERE session_id = s.id) as users,
3377
				(SELECT COUNT(*) FROM $tbl_lp WHERE session_id = s.id) as lessons ";
3378
        if ($field !== false) {
3379
            $fieldId = $field['id'];
3380
            $sql .= ",(SELECT value FROM $tbl_extra_fields WHERE field_id = $fieldId AND item_id = s.id) as image ";
3381
        }
3382
        $sql .= " FROM $tbl_session s
3383
                LEFT JOIN $tbl_session_category c
3384
                    ON s.session_category_id = c.id
3385
                INNER JOIN $tbl_users u
3386
                    ON s.id_coach = u.id
3387
                ORDER BY 9 DESC
3388
                LIMIT 8";
3389
        $result = Database::query($sql);
3390
3391
        if (Database::num_rows($result) > 0) {
3392
            $plugin = BuyCoursesPlugin::create();
3393
            $checker = $plugin->isEnabled();
3394
            $sessions = [];
3395
            while ($row = Database::fetch_array($result, 'ASSOC')) {
3396
                if (!isset($row['image'])) {
3397
                    $row['image'] = '';
3398
                }
3399
                $row['on_sale'] = '';
3400
                if ($checker) {
3401
                    $row['on_sale'] = $plugin->getItemByProduct(
3402
                        $row['id'],
3403
                        BuyCoursesPlugin::PRODUCT_TYPE_SESSION
3404
                    );
3405
                }
3406
                $sessions[] = $row;
3407
            }
3408
3409
            return $sessions;
3410
        } else {
3411
            return false;
3412
        }
3413
    }
3414
3415
    /**
3416
     * Get all session categories (filter by access_url_id).
3417
     *
3418
     * @return mixed false if the session category does not exist, array if the session category exists
3419
     */
3420
    public static function get_all_session_category()
3421
    {
3422
        $table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
3423
        $id = api_get_current_access_url_id();
3424
        $sql = 'SELECT * FROM '.$table.'
3425
                WHERE access_url_id = '.$id.'
3426
                ORDER BY name ASC';
3427
        $result = Database::query($sql);
3428
        if (Database::num_rows($result) > 0) {
3429
            $data = Database::store_result($result, 'ASSOC');
3430
3431
            return $data;
3432
        } else {
3433
            return false;
3434
        }
3435
    }
3436
3437
    /**
3438
     * Assign a coach to course in session with status = 2.
3439
     *
3440
     * @param int  $userId
3441
     * @param int  $sessionId
3442
     * @param int  $courseId
3443
     * @param bool $noCoach   optional, if is true the user don't be a coach now,
3444
     *                        otherwise it'll assign a coach
3445
     *
3446
     * @return bool true if there are affected rows, otherwise false
3447
     */
3448
    public static function set_coach_to_course_session(
3449
        $userId,
3450
        $sessionId = 0,
3451
        $courseId = 0,
3452
        $noCoach = false
3453
    ) {
3454
        // Definition of variables
3455
        $userId = (int) $userId;
3456
3457
        $sessionId = !empty($sessionId) ? (int) $sessionId : api_get_session_id();
3458
        $courseId = !empty($courseId) ? (int) $courseId : api_get_course_id();
3459
3460
        if (empty($sessionId) || empty($courseId) || empty($userId)) {
3461
            return false;
3462
        }
3463
3464
        // Table definition
3465
        $tblSessionRelCourseRelUser = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
3466
        $tblSessionRelUser = Database::get_main_table(TABLE_MAIN_SESSION_USER);
3467
        $tblUser = Database::get_main_table(TABLE_MAIN_USER);
3468
3469
        // check if user is a teacher
3470
        $sql = "SELECT * FROM $tblUser
3471
                WHERE status = 1 AND user_id = $userId";
3472
3473
        $rsCheckUser = Database::query($sql);
3474
3475
        if (Database::num_rows($rsCheckUser) <= 0) {
3476
            return false;
3477
        }
3478
3479
        if ($noCoach) {
3480
            // check if user_id exists in session_rel_user (if the user is
3481
            // subscribed to the session in any manner)
3482
            $sql = "SELECT user_id FROM $tblSessionRelUser
3483
                    WHERE
3484
                        session_id = $sessionId AND
3485
                        user_id = $userId";
3486
            $res = Database::query($sql);
3487
3488
            if (Database::num_rows($res) > 0) {
3489
                // The user is already subscribed to the session. Change the
3490
                // record so the user is NOT a coach for this course anymore
3491
                // and then exit
3492
                $sql = "UPDATE $tblSessionRelCourseRelUser
3493
                        SET status = 0
3494
                        WHERE
3495
                            session_id = $sessionId AND
3496
                            c_id = $courseId AND
3497
                            user_id = $userId ";
3498
                $result = Database::query($sql);
3499
3500
                return Database::affected_rows($result) > 0;
3501
            }
3502
3503
            // The user is not subscribed to the session, so make sure
3504
            // he isn't subscribed to a course in this session either
3505
            // and then exit
3506
            $sql = "DELETE FROM $tblSessionRelCourseRelUser
3507
                    WHERE
3508
                        session_id = $sessionId AND
3509
                        c_id = $courseId AND
3510
                        user_id = $userId ";
3511
            $result = Database::query($sql);
3512
3513
            return Database::affected_rows($result) > 0;
3514
        }
3515
3516
        // Assign user as a coach to course
3517
        // First check if the user is registered to the course
3518
        $sql = "SELECT user_id FROM $tblSessionRelCourseRelUser
3519
                WHERE
3520
                    session_id = $sessionId AND
3521
                    c_id = $courseId AND
3522
                    user_id = $userId";
3523
        $rs_check = Database::query($sql);
3524
3525
        // Then update or insert.
3526
        if (Database::num_rows($rs_check) > 0) {
3527
            $sql = "UPDATE $tblSessionRelCourseRelUser SET status = 2
3528
                    WHERE
3529
                        session_id = $sessionId AND
3530
                        c_id = $courseId AND
3531
                        user_id = $userId ";
3532
            $result = Database::query($sql);
3533
3534
            return Database::affected_rows($result) > 0;
3535
        }
3536
3537
        $sql = "INSERT INTO $tblSessionRelCourseRelUser(session_id, c_id, user_id, status, visibility)
3538
                VALUES($sessionId, $courseId, $userId, 2, 1)";
3539
        $result = Database::query($sql);
3540
3541
        return Database::affected_rows($result) > 0;
3542
    }
3543
3544
    /**
3545
     * @param int $sessionId
3546
     *
3547
     * @return bool
3548
     */
3549
    public static function removeAllDrhFromSession($sessionId)
3550
    {
3551
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
3552
        $sessionId = (int) $sessionId;
3553
3554
        if (empty($sessionId)) {
3555
            return false;
3556
        }
3557
3558
        $sql = "DELETE FROM $tbl_session_rel_user
3559
                WHERE
3560
                    session_id = $sessionId AND                            
3561
                    relation_type =".SESSION_RELATION_TYPE_RRHH;
3562
        Database::query($sql);
3563
3564
        return true;
3565
    }
3566
3567
    /**
3568
     * Subscribes sessions to human resource manager (Dashboard feature).
3569
     *
3570
     * @param array $userInfo               Human Resource Manager info
3571
     * @param array $sessions_list          Sessions id
3572
     * @param bool  $sendEmail
3573
     * @param bool  $removeSessionsFromUser
3574
     *
3575
     * @return int
3576
     * */
3577
    public static function subscribeSessionsToDrh(
3578
        $userInfo,
3579
        $sessions_list,
3580
        $sendEmail = false,
3581
        $removeSessionsFromUser = true
3582
    ) {
3583
        // Database Table Definitions
3584
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
3585
        $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
3586
3587
        if (empty($userInfo)) {
3588
            return 0;
3589
        }
3590
3591
        $userId = $userInfo['user_id'];
3592
3593
        // Only subscribe DRH users.
3594
        $rolesAllowed = [
3595
            DRH,
3596
            SESSIONADMIN,
3597
            PLATFORM_ADMIN,
3598
            COURSE_TUTOR,
3599
        ];
3600
        $isAdmin = api_is_platform_admin_by_id($userInfo['user_id']);
3601
        if (!$isAdmin && !in_array($userInfo['status'], $rolesAllowed)) {
3602
            return 0;
3603
        }
3604
3605
        $affected_rows = 0;
3606
        // Deleting assigned sessions to hrm_id.
3607
        if ($removeSessionsFromUser) {
3608
            if (api_is_multiple_url_enabled()) {
3609
                $sql = "SELECT s.session_id
3610
                        FROM $tbl_session_rel_user s
3611
                        INNER JOIN $tbl_session_rel_access_url a 
3612
                        ON (a.session_id = s.session_id)
3613
                        WHERE
3614
                            s.user_id = $userId AND
3615
                            relation_type = ".SESSION_RELATION_TYPE_RRHH." AND
3616
                            access_url_id = ".api_get_current_access_url_id();
3617
            } else {
3618
                $sql = "SELECT s.session_id 
3619
                        FROM $tbl_session_rel_user s
3620
                        WHERE user_id = $userId AND relation_type=".SESSION_RELATION_TYPE_RRHH;
3621
            }
3622
            $result = Database::query($sql);
3623
3624
            if (Database::num_rows($result) > 0) {
3625
                while ($row = Database::fetch_array($result)) {
3626
                    $sql = "DELETE FROM $tbl_session_rel_user
3627
                            WHERE
3628
                                session_id = {$row['session_id']} AND
3629
                                user_id = $userId AND
3630
                                relation_type =".SESSION_RELATION_TYPE_RRHH;
3631
                    Database::query($sql);
3632
                }
3633
            }
3634
        }
3635
3636
        // Inserting new sessions list.
3637
        if (!empty($sessions_list) && is_array($sessions_list)) {
3638
            foreach ($sessions_list as $session_id) {
3639
                $session_id = intval($session_id);
3640
                $sql = "SELECT session_id
3641
                        FROM $tbl_session_rel_user
3642
                        WHERE
3643
                            session_id = $session_id AND
3644
                            user_id = $userId AND
3645
                            relation_type = '".SESSION_RELATION_TYPE_RRHH."'";
3646
                $result = Database::query($sql);
3647
                if (Database::num_rows($result) == 0) {
3648
                    $sql = "INSERT IGNORE INTO $tbl_session_rel_user (session_id, user_id, relation_type, registered_at)
3649
                            VALUES (
3650
                                $session_id,
3651
                                $userId,
3652
                                '".SESSION_RELATION_TYPE_RRHH."',
3653
                                '".api_get_utc_datetime()."'
3654
                            )";
3655
                    Database::query($sql);
3656
                    $affected_rows++;
3657
                }
3658
            }
3659
        }
3660
3661
        return $affected_rows;
3662
    }
3663
3664
    /**
3665
     * @param int $sessionId
3666
     *
3667
     * @return array
3668
     */
3669
    public static function getDrhUsersInSession($sessionId)
3670
    {
3671
        return self::get_users_by_session($sessionId, SESSION_RELATION_TYPE_RRHH);
3672
    }
3673
3674
    /**
3675
     * @param int $userId
3676
     * @param int $sessionId
3677
     *
3678
     * @return array
3679
     */
3680
    public static function getSessionFollowedByDrh($userId, $sessionId)
3681
    {
3682
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
3683
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
3684
        $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
3685
3686
        $userId = intval($userId);
3687
        $sessionId = intval($sessionId);
3688
3689
        $select = " SELECT * ";
3690
        if (api_is_multiple_url_enabled()) {
3691
            $sql = " $select FROM $tbl_session s
3692
                    INNER JOIN $tbl_session_rel_user sru ON (sru.session_id = s.id)
3693
                    LEFT JOIN $tbl_session_rel_access_url a ON (s.id = a.session_id)
3694
                    WHERE
3695
                        sru.user_id = '$userId' AND
3696
                        sru.session_id = '$sessionId' AND
3697
                        sru.relation_type = '".SESSION_RELATION_TYPE_RRHH."' AND
3698
                        access_url_id = ".api_get_current_access_url_id()."
3699
                        ";
3700
        } else {
3701
            $sql = "$select FROM $tbl_session s
3702
                     INNER JOIN $tbl_session_rel_user sru
3703
                     ON
3704
                        sru.session_id = s.id AND
3705
                        sru.user_id = '$userId' AND
3706
                        sru.session_id = '$sessionId' AND
3707
                        sru.relation_type = '".SESSION_RELATION_TYPE_RRHH."'
3708
                    ";
3709
        }
3710
3711
        $result = Database::query($sql);
3712
        if (Database::num_rows($result)) {
3713
            $row = Database::fetch_array($result, 'ASSOC');
3714
            $row['course_list'] = self::get_course_list_by_session_id($sessionId);
3715
3716
            return $row;
3717
        }
3718
3719
        return [];
3720
    }
3721
3722
    /**
3723
     * Get sessions followed by human resources manager.
3724
     *
3725
     * @param int    $userId
3726
     * @param int    $start
3727
     * @param int    $limit
3728
     * @param bool   $getCount
3729
     * @param bool   $getOnlySessionId
3730
     * @param bool   $getSql
3731
     * @param string $orderCondition
3732
     * @param string $keyword
3733
     * @param string $description
3734
     *
3735
     * @return array sessions
3736
     */
3737
    public static function get_sessions_followed_by_drh(
3738
        $userId,
3739
        $start = null,
3740
        $limit = null,
3741
        $getCount = false,
3742
        $getOnlySessionId = false,
3743
        $getSql = false,
3744
        $orderCondition = null,
3745
        $keyword = '',
3746
        $description = ''
3747
    ) {
3748
        return self::getSessionsFollowedByUser(
3749
            $userId,
3750
            DRH,
3751
            $start,
3752
            $limit,
3753
            $getCount,
3754
            $getOnlySessionId,
3755
            $getSql,
3756
            $orderCondition,
3757
            $keyword,
3758
            $description
3759
        );
3760
    }
3761
3762
    /**
3763
     * Get sessions followed by human resources manager.
3764
     *
3765
     * @param int    $userId
3766
     * @param int    $status           DRH Optional
3767
     * @param int    $start
3768
     * @param int    $limit
3769
     * @param bool   $getCount
3770
     * @param bool   $getOnlySessionId
3771
     * @param bool   $getSql
3772
     * @param string $orderCondition
3773
     * @param string $keyword
3774
     * @param string $description
3775
     *
3776
     * @return array sessions
3777
     */
3778
    public static function getSessionsFollowedByUser(
3779
        $userId,
3780
        $status = null,
3781
        $start = null,
3782
        $limit = null,
3783
        $getCount = false,
3784
        $getOnlySessionId = false,
3785
        $getSql = false,
3786
        $orderCondition = null,
3787
        $keyword = '',
3788
        $description = ''
3789
    ) {
3790
        // Database Table Definitions
3791
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
3792
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
3793
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
3794
        $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
3795
3796
        $userId = (int) $userId;
3797
3798
        $select = " SELECT DISTINCT * ";
3799
        if ($getCount) {
3800
            $select = " SELECT count(DISTINCT(s.id)) as count ";
3801
        }
3802
3803
        if ($getOnlySessionId) {
3804
            $select = " SELECT DISTINCT(s.id) ";
3805
        }
3806
3807
        $limitCondition = null;
3808
        if (!is_null($start) && !is_null($limit)) {
3809
            $limitCondition = " LIMIT ".intval($start).", ".intval($limit);
3810
        }
3811
3812
        if (empty($orderCondition)) {
3813
            $orderCondition = " ORDER BY s.name ";
3814
        }
3815
3816
        $whereConditions = null;
3817
        $sessionCourseConditions = null;
3818
        $sessionConditions = null;
3819
        $sessionQuery = '';
3820
        $courseSessionQuery = null;
3821
3822
        switch ($status) {
3823
            case DRH:
3824
                $sessionQuery = "SELECT sru.session_id
3825
                                 FROM
3826
                                 $tbl_session_rel_user sru
3827
                                 WHERE
3828
                                    sru.relation_type = '".SESSION_RELATION_TYPE_RRHH."' AND
3829
                                    sru.user_id = $userId";
3830
                break;
3831
            case COURSEMANAGER:
3832
                $courseSessionQuery = "
3833
                    SELECT scu.session_id as id
3834
                    FROM $tbl_session_rel_course_rel_user scu
3835
                    WHERE (scu.status = 2 AND scu.user_id = $userId)";
3836
3837
                $whereConditions = " OR (s.id_coach = $userId) ";
3838
                break;
3839
            default:
3840
                $sessionQuery = "SELECT sru.session_id
3841
                                 FROM
3842
                                 $tbl_session_rel_user sru
3843
                                 WHERE
3844
                                    sru.user_id = $userId";
3845
                break;
3846
        }
3847
3848
        $keywordCondition = '';
3849
        if (!empty($keyword)) {
3850
            $keyword = Database::escape_string($keyword);
3851
            $keywordCondition = " AND (s.name LIKE '%$keyword%' ) ";
3852
3853
            if (!empty($description)) {
3854
                $description = Database::escape_string($description);
3855
                $keywordCondition = " AND (s.name LIKE '%$keyword%' OR s.description LIKE '%$description%' ) ";
3856
            }
3857
        }
3858
3859
        $whereConditions .= $keywordCondition;
3860
        $subQuery = $sessionQuery.$courseSessionQuery;
3861
3862
        $sql = " $select FROM $tbl_session s
3863
                INNER JOIN $tbl_session_rel_access_url a 
3864
                ON (s.id = a.session_id)
3865
                WHERE
3866
                    access_url_id = ".api_get_current_access_url_id()." AND
3867
                    s.id IN (
3868
                        $subQuery
3869
                    )
3870
                    $whereConditions
3871
                    $orderCondition
3872
                    $limitCondition";
3873
3874
        if ($getSql) {
3875
            return $sql;
3876
        }
3877
        $result = Database::query($sql);
3878
3879
        if ($getCount) {
3880
            $row = Database::fetch_array($result);
3881
3882
            return $row['count'];
3883
        }
3884
3885
        $sessions = [];
3886
        if (Database::num_rows($result) > 0) {
3887
            $sysUploadPath = api_get_path(SYS_UPLOAD_PATH).'sessions/';
3888
            $webUploadPath = api_get_path(WEB_UPLOAD_PATH).'sessions/';
3889
            $imgPath = Display::return_icon(
3890
                'session_default_small.png',
3891
                null,
3892
                [],
3893
                ICON_SIZE_SMALL,
3894
                false,
3895
                true
3896
            );
3897
3898
            while ($row = Database::fetch_array($result)) {
3899
                if ($getOnlySessionId) {
3900
                    $sessions[$row['id']] = $row;
3901
                    continue;
3902
                }
3903
                $imageFilename = \ExtraField::FIELD_TYPE_FILE_IMAGE.'_'.$row['id'].'.png';
3904
                $row['image'] = is_file($sysUploadPath.$imageFilename) ? $webUploadPath.$imageFilename : $imgPath;
3905
3906
                if ($row['display_start_date'] == '0000-00-00 00:00:00' || $row['display_start_date'] == '0000-00-00') {
3907
                    $row['display_start_date'] = null;
3908
                }
3909
3910
                if ($row['display_end_date'] == '0000-00-00 00:00:00' || $row['display_end_date'] == '0000-00-00') {
3911
                    $row['display_end_date'] = null;
3912
                }
3913
3914
                if ($row['access_start_date'] == '0000-00-00 00:00:00' || $row['access_start_date'] == '0000-00-00') {
3915
                    $row['access_start_date'] = null;
3916
                }
3917
3918
                if ($row['access_end_date'] == '0000-00-00 00:00:00' || $row['access_end_date'] == '0000-00-00') {
3919
                    $row['access_end_date'] = null;
3920
                }
3921
3922
                if ($row['coach_access_start_date'] == '0000-00-00 00:00:00' ||
3923
                    $row['coach_access_start_date'] == '0000-00-00'
3924
                ) {
3925
                    $row['coach_access_start_date'] = null;
3926
                }
3927
3928
                if ($row['coach_access_end_date'] == '0000-00-00 00:00:00' ||
3929
                    $row['coach_access_end_date'] == '0000-00-00'
3930
                ) {
3931
                    $row['coach_access_end_date'] = null;
3932
                }
3933
3934
                $sessions[$row['id']] = $row;
3935
            }
3936
        }
3937
3938
        return $sessions;
3939
    }
3940
3941
    /**
3942
     * Gets the list (or the count) of courses by session filtered by access_url.
3943
     *
3944
     * @param int    $session_id  The session id
3945
     * @param string $course_name The course code
3946
     * @param string $orderBy     Field to order the data
3947
     * @param bool   $getCount    Optional. Count the session courses
3948
     *
3949
     * @return array|int List of courses. Whether $getCount is true, return the count
3950
     */
3951
    public static function get_course_list_by_session_id(
3952
        $session_id,
3953
        $course_name = '',
3954
        $orderBy = null,
3955
        $getCount = false
3956
    ) {
3957
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
3958
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
3959
        $session_id = (int) $session_id;
3960
        $sqlSelect = "*, c.id, c.id as real_id";
3961
3962
        if ($getCount) {
3963
            $sqlSelect = "COUNT(1) as count";
3964
        }
3965
3966
        // select the courses
3967
        $sql = "SELECT $sqlSelect
3968
                FROM $tbl_course c
3969
                INNER JOIN $tbl_session_rel_course src
3970
                ON (c.id = src.c_id)
3971
		        WHERE src.session_id = '$session_id' ";
3972
3973
        if (!empty($course_name)) {
3974
            $course_name = Database::escape_string($course_name);
3975
            $sql .= " AND c.title LIKE '%$course_name%' ";
3976
        }
3977
3978
        if (!empty($orderBy)) {
3979
            $orderBy = Database::escape_string($orderBy);
3980
            $orderBy = " ORDER BY $orderBy";
3981
        } else {
3982
            if (self::orderCourseIsEnabled()) {
3983
                $orderBy .= " ORDER BY position ";
3984
            } else {
3985
                $orderBy .= " ORDER BY title ";
3986
            }
3987
        }
3988
3989
        $sql .= Database::escape_string($orderBy);
3990
        $result = Database::query($sql);
3991
        $num_rows = Database::num_rows($result);
3992
        $courses = [];
3993
        if ($num_rows > 0) {
3994
            if ($getCount) {
3995
                $count = Database::fetch_assoc($result);
3996
3997
                return (int) $count['count'];
3998
            }
3999
4000
            while ($row = Database::fetch_array($result, 'ASSOC')) {
4001
                $courses[$row['real_id']] = $row;
4002
            }
4003
        }
4004
4005
        return $courses;
4006
    }
4007
4008
    /**
4009
     * Gets the list of courses by session filtered by access_url.
4010
     *
4011
     * @param $userId
4012
     * @param $sessionId
4013
     * @param null   $from
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $from is correct as it would always require null to be passed?
Loading history...
4014
     * @param null   $limit
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $limit is correct as it would always require null to be passed?
Loading history...
4015
     * @param null   $column
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $column is correct as it would always require null to be passed?
Loading history...
4016
     * @param null   $direction
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $direction is correct as it would always require null to be passed?
Loading history...
4017
     * @param bool   $getCount
4018
     * @param string $keyword
4019
     *
4020
     * @return array
4021
     */
4022
    public static function getAllCoursesFollowedByUser(
4023
        $userId,
4024
        $sessionId,
4025
        $from = null,
4026
        $limit = null,
4027
        $column = null,
4028
        $direction = null,
4029
        $getCount = false,
4030
        $keyword = ''
4031
    ) {
4032
        if (empty($sessionId)) {
4033
            $sessionsSQL = self::get_sessions_followed_by_drh(
4034
                $userId,
4035
                null,
4036
                null,
4037
                null,
4038
                true,
4039
                true
4040
            );
4041
        } else {
4042
            $sessionsSQL = intval($sessionId);
4043
        }
4044
4045
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
4046
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
4047
4048
        if ($getCount) {
4049
            $select = "SELECT COUNT(DISTINCT(c.code)) as count ";
4050
        } else {
4051
            $select = "SELECT DISTINCT c.* ";
4052
        }
4053
4054
        $keywordCondition = null;
4055
        if (!empty($keyword)) {
4056
            $keyword = Database::escape_string($keyword);
4057
            $keywordCondition = " AND (c.code LIKE '%$keyword%' OR c.title LIKE '%$keyword%' ) ";
4058
        }
4059
4060
        // Select the courses
4061
        $sql = "$select
4062
                FROM $tbl_course c
4063
                INNER JOIN $tbl_session_rel_course src
4064
                ON c.id = src.c_id
4065
		        WHERE
4066
		            src.session_id IN ($sessionsSQL)
4067
		            $keywordCondition
4068
		        ";
4069
        if ($getCount) {
4070
            $result = Database::query($sql);
4071
            $row = Database::fetch_array($result, 'ASSOC');
4072
4073
            return $row['count'];
4074
        }
4075
4076
        if (isset($from) && isset($limit)) {
4077
            $from = intval($from);
4078
            $limit = intval($limit);
4079
            $sql .= " LIMIT $from, $limit";
4080
        }
4081
4082
        $result = Database::query($sql);
4083
        $num_rows = Database::num_rows($result);
4084
        $courses = [];
4085
4086
        if ($num_rows > 0) {
4087
            while ($row = Database::fetch_array($result, 'ASSOC')) {
4088
                $courses[$row['id']] = $row;
4089
            }
4090
        }
4091
4092
        return $courses;
4093
    }
4094
4095
    /**
4096
     * Gets the list of courses by session filtered by access_url.
4097
     *
4098
     * @param int    $session_id
4099
     * @param string $course_name
4100
     *
4101
     * @return array list of courses
4102
     */
4103
    public static function get_course_list_by_session_id_like($session_id, $course_name = '')
4104
    {
4105
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
4106
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
4107
4108
        $session_id = intval($session_id);
4109
        $course_name = Database::escape_string($course_name);
4110
4111
        // select the courses
4112
        $sql = "SELECT c.id, c.title FROM $tbl_course c
4113
                INNER JOIN $tbl_session_rel_course src
4114
                ON c.id = src.c_id
4115
		        WHERE ";
4116
4117
        if (!empty($session_id)) {
4118
            $sql .= "src.session_id LIKE '$session_id' AND ";
4119
        }
4120
4121
        if (!empty($course_name)) {
4122
            $sql .= "UPPER(c.title) LIKE UPPER('%$course_name%') ";
4123
        }
4124
4125
        $sql .= "ORDER BY title;";
4126
        $result = Database::query($sql);
4127
        $num_rows = Database::num_rows($result);
4128
        $courses = [];
4129
        if ($num_rows > 0) {
4130
            while ($row = Database::fetch_array($result, 'ASSOC')) {
4131
                $courses[$row['id']] = $row;
4132
            }
4133
        }
4134
4135
        return $courses;
4136
    }
4137
4138
    /**
4139
     * Gets the count of courses by session filtered by access_url.
4140
     *
4141
     * @param int session id
4142
     * @param string $keyword
4143
     *
4144
     * @return array list of courses
4145
     */
4146
    public static function getCourseCountBySessionId($session_id, $keyword = '')
4147
    {
4148
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
4149
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
4150
        $session_id = intval($session_id);
4151
4152
        // select the courses
4153
        $sql = "SELECT COUNT(c.code) count
4154
                FROM $tbl_course c
4155
                INNER JOIN $tbl_session_rel_course src
4156
                ON c.id = src.c_id
4157
		        WHERE src.session_id = '$session_id' ";
4158
4159
        $keywordCondition = null;
4160
        if (!empty($keyword)) {
4161
            $keyword = Database::escape_string($keyword);
4162
            $keywordCondition = " AND (c.code LIKE '%$keyword%' OR c.title LIKE '%$keyword%' ) ";
4163
        }
4164
        $sql .= $keywordCondition;
4165
4166
        $result = Database::query($sql);
4167
        $num_rows = Database::num_rows($result);
4168
        if ($num_rows > 0) {
4169
            $row = Database::fetch_array($result, 'ASSOC');
4170
4171
            return $row['count'];
4172
        }
4173
4174
        return null;
4175
    }
4176
4177
    /**
4178
     * Get the session id based on the original id and field name in the extra fields.
4179
     * Returns 0 if session was not found.
4180
     *
4181
     * @param string $value    Original session id
4182
     * @param string $variable Original field name
4183
     *
4184
     * @return int Session id
4185
     */
4186
    public static function getSessionIdFromOriginalId($value, $variable)
4187
    {
4188
        $extraFieldValue = new ExtraFieldValue('session');
4189
        $result = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
4190
            $variable,
4191
            $value
4192
        );
4193
4194
        if (!empty($result)) {
4195
            return $result['item_id'];
4196
        }
4197
4198
        return 0;
4199
    }
4200
4201
    /**
4202
     * Get users by session.
4203
     *
4204
     * @param int  $id       session id
4205
     * @param int  $status   filter by status coach = 2
4206
     * @param bool $getCount Optional. Allow get the number of rows from the result
4207
     * @param int  $urlId
4208
     *
4209
     * @return array|int A list with an user list. If $getCount is true then return a the count of registers
4210
     */
4211
    public static function get_users_by_session(
4212
        $id,
4213
        $status = null,
4214
        $getCount = false,
4215
        $urlId = 0
4216
    ) {
4217
        if (empty($id)) {
4218
            return [];
4219
        }
4220
        $id = (int) $id;
4221
        $urlId = empty($urlId) ? api_get_current_access_url_id() : (int) $urlId;
4222
4223
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
4224
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
4225
        $table_access_url_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
4226
4227
        $selectedField = '
4228
            u.user_id, u.lastname, u.firstname, u.username, su.relation_type, au.access_url_id,
4229
            su.moved_to, su.moved_status, su.moved_at, su.registered_at
4230
        ';
4231
4232
        if ($getCount) {
4233
            $selectedField = 'count(1) AS count';
4234
        }
4235
4236
        $sql = "SELECT $selectedField
4237
                FROM $tbl_user u
4238
                INNER JOIN $tbl_session_rel_user su
4239
                ON u.user_id = su.user_id AND
4240
                su.session_id = $id
4241
                LEFT OUTER JOIN $table_access_url_user au
4242
                ON (au.user_id = u.user_id)
4243
                ";
4244
4245
        if (is_numeric($status)) {
4246
            $status = (int) $status;
4247
            $sql .= " WHERE su.relation_type = $status AND (au.access_url_id = $urlId OR au.access_url_id is null)";
4248
        } else {
4249
            $sql .= " WHERE (au.access_url_id = $urlId OR au.access_url_id is null )";
4250
        }
4251
4252
        $sql .= ' ORDER BY su.relation_type, ';
4253
        $sql .= api_sort_by_first_name() ? ' u.firstname, u.lastname' : '  u.lastname, u.firstname';
4254
4255
        $result = Database::query($sql);
4256
        if ($getCount) {
4257
            $count = Database::fetch_assoc($result);
4258
4259
            return $count['count'];
4260
        }
4261
4262
        $return = [];
4263
        while ($row = Database::fetch_array($result, 'ASSOC')) {
4264
            $return[] = $row;
4265
        }
4266
4267
        return $return;
4268
    }
4269
4270
    /**
4271
     * The general coach (field: session.id_coach).
4272
     *
4273
     * @param int  $user_id         user id
4274
     * @param bool $asPlatformAdmin The user is platform admin, return everything
4275
     *
4276
     * @return array
4277
     */
4278
    public static function get_sessions_by_general_coach($user_id, $asPlatformAdmin = false)
4279
    {
4280
        $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
4281
        $user_id = intval($user_id);
4282
4283
        // Session where we are general coach
4284
        $sql = "SELECT DISTINCT *
4285
                FROM $session_table";
4286
4287
        if (!$asPlatformAdmin) {
4288
            $sql .= " WHERE id_coach = $user_id";
4289
        }
4290
4291
        if (api_is_multiple_url_enabled()) {
4292
            $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
4293
            $access_url_id = api_get_current_access_url_id();
4294
4295
            $sqlCoach = '';
4296
            if (!$asPlatformAdmin) {
4297
                $sqlCoach = " id_coach = $user_id AND ";
4298
            }
4299
4300
            if ($access_url_id != -1) {
4301
                $sql = 'SELECT DISTINCT session.*
4302
                    FROM '.$session_table.' session INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
4303
                    ON (session.id = session_rel_url.session_id)
4304
                    WHERE '.$sqlCoach.' access_url_id = '.$access_url_id;
4305
            }
4306
        }
4307
        $sql .= ' ORDER by name';
4308
        $result = Database::query($sql);
4309
4310
        return Database::store_result($result, 'ASSOC');
4311
    }
4312
4313
    /**
4314
     * @param int $user_id
4315
     *
4316
     * @return array
4317
     *
4318
     * @deprecated use get_sessions_by_general_coach()
4319
     */
4320
    public static function get_sessions_by_coach($user_id)
4321
    {
4322
        $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
4323
4324
        return Database::select(
4325
            '*',
4326
            $session_table,
4327
            ['where' => ['id_coach = ?' => $user_id]]
4328
        );
4329
    }
4330
4331
    /**
4332
     * @param int $user_id
4333
     * @param int $courseId
4334
     * @param int $session_id
4335
     *
4336
     * @return array|bool
4337
     */
4338
    public static function get_user_status_in_course_session($user_id, $courseId, $session_id)
4339
    {
4340
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
4341
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
4342
        $sql = "SELECT session_rcru.status
4343
                FROM $table session_rcru 
4344
                INNER JOIN $tbl_user user
4345
                ON (session_rcru.user_id = user.user_id)
4346
                WHERE                    
4347
                    session_rcru.session_id = '".intval($session_id)."' AND
4348
                    session_rcru.c_id ='".intval($courseId)."' AND
4349
                    user.user_id = ".intval($user_id);
4350
4351
        $result = Database::query($sql);
4352
        $status = false;
4353
        if (Database::num_rows($result)) {
4354
            $status = Database::fetch_row($result);
4355
            $status = $status['0'];
4356
        }
4357
4358
        return $status;
4359
    }
4360
4361
    /**
4362
     * Gets user status within a session.
4363
     *
4364
     * @param int $userId
4365
     * @param int $sessionId
4366
     *
4367
     * @return SessionRelUser
4368
     */
4369
    public static function getUserStatusInSession($userId, $sessionId)
4370
    {
4371
        $em = Database::getManager();
4372
        $subscriptions = $em
4373
            ->getRepository('ChamiloCoreBundle:SessionRelUser')
4374
            ->findBy(['session' => $sessionId, 'user' => $userId]);
4375
4376
        /** @var SessionRelUser $subscription */
4377
        $subscription = current($subscriptions);
4378
4379
        return $subscription;
4380
    }
4381
4382
    /**
4383
     * @param int $id
4384
     *
4385
     * @return array
4386
     */
4387
    public static function get_all_sessions_by_promotion($id)
4388
    {
4389
        $table = Database::get_main_table(TABLE_MAIN_SESSION);
4390
4391
        return Database::select(
4392
            '*',
4393
            $table,
4394
            ['where' => ['promotion_id = ?' => $id]]
4395
        );
4396
    }
4397
4398
    /**
4399
     * @param int   $promotion_id
4400
     * @param array $list
4401
     */
4402
    public static function subscribe_sessions_to_promotion($promotion_id, $list)
4403
    {
4404
        $table = Database::get_main_table(TABLE_MAIN_SESSION);
4405
        $params = [];
4406
        $params['promotion_id'] = 0;
4407
        Database::update(
4408
            $table,
4409
            $params,
4410
            ['promotion_id = ?' => $promotion_id]
4411
        );
4412
4413
        $params['promotion_id'] = $promotion_id;
4414
        if (!empty($list)) {
4415
            foreach ($list as $session_id) {
4416
                $session_id = intval($session_id);
4417
                Database::update($table, $params, ['id = ?' => $session_id]);
4418
            }
4419
        }
4420
    }
4421
4422
    /**
4423
     * Updates a session status.
4424
     *
4425
     * @param int session id
4426
     * @param int status
4427
     */
4428
    public static function set_session_status($session_id, $status)
4429
    {
4430
        $t = Database::get_main_table(TABLE_MAIN_SESSION);
4431
        $params['visibility'] = $status;
4432
        Database::update($t, $params, ['id = ?' => $session_id]);
4433
    }
4434
4435
    /**
4436
     * Copies a session with the same data to a new session.
4437
     * The new copy is not assigned to the same promotion. @see subscribe_sessions_to_promotions() for that.
4438
     *
4439
     * @param   int     Session ID
4440
     * @param   bool    Whether to copy the relationship with courses
4441
     * @param   bool    Whether to copy the relationship with users
4442
     * @param   bool    New courses will be created
4443
     * @param   bool    Whether to set exercises and learning paths in the new session to invisible by default
4444
     *
4445
     * @return int The new session ID on success, 0 otherwise
4446
     *
4447
     * @todo make sure the extra session fields are copied too
4448
     */
4449
    public static function copy(
4450
        $id,
4451
        $copy_courses = true,
4452
        $copy_users = true,
4453
        $create_new_courses = false,
4454
        $set_exercises_lp_invisible = false
4455
    ) {
4456
        $id = intval($id);
4457
        $s = self::fetch($id);
4458
        // Check all dates before copying
4459
        // Get timestamp for now in UTC - see http://php.net/manual/es/function.time.php#117251
4460
        $now = time() - date('Z');
4461
        // Timestamp in one month
4462
        $inOneMonth = $now + (30 * 24 * 3600);
4463
        $inOneMonth = api_get_local_time($inOneMonth);
4464
        if (api_strtotime($s['access_start_date']) < $now) {
4465
            $s['access_start_date'] = api_get_local_time($now);
4466
        }
4467
        if (api_strtotime($s['display_start_date']) < $now) {
4468
            $s['display_start_date'] = api_get_local_time($now);
4469
        }
4470
        if (api_strtotime($s['coach_access_start_date']) < $now) {
4471
            $s['coach_access_start_date'] = api_get_local_time($now);
4472
        }
4473
        if (api_strtotime($s['access_end_date']) < $now) {
4474
            $s['access_end_date'] = $inOneMonth;
4475
        }
4476
        if (api_strtotime($s['display_end_date']) < $now) {
4477
            $s['display_end_date'] = $inOneMonth;
4478
        }
4479
        if (api_strtotime($s['coach_access_end_date']) < $now) {
4480
            $s['coach_access_end_date'] = $inOneMonth;
4481
        }
4482
        // Now try to create the session
4483
        $sid = self::create_session(
4484
            $s['name'].' '.get_lang('CopyLabelSuffix'),
4485
            $s['access_start_date'],
4486
            $s['access_end_date'],
4487
            $s['display_start_date'],
4488
            $s['display_end_date'],
4489
            $s['coach_access_start_date'],
4490
            $s['coach_access_end_date'],
4491
            (int) $s['id_coach'],
4492
            $s['session_category_id'],
4493
            (int) $s['visibility'],
4494
            true
4495
        );
4496
4497
        if (!is_numeric($sid) || empty($sid)) {
4498
            return false;
4499
        }
4500
4501
        if ($copy_courses) {
4502
            // Register courses from the original session to the new session
4503
            $courses = self::get_course_list_by_session_id($id);
4504
4505
            $short_courses = $new_short_courses = [];
4506
            if (is_array($courses) && count($courses) > 0) {
4507
                foreach ($courses as $course) {
4508
                    $short_courses[] = $course;
4509
                }
4510
            }
4511
4512
            $courses = null;
4513
            // We will copy the current courses of the session to new courses
4514
            if (!empty($short_courses)) {
4515
                if ($create_new_courses) {
4516
                    api_set_more_memory_and_time_limits();
4517
                    $params = [];
4518
                    $params['skip_lp_dates'] = true;
4519
4520
                    foreach ($short_courses as $course_data) {
4521
                        $course_info = CourseManager::copy_course_simple(
4522
                            $course_data['title'].' '.get_lang(
4523
                                'CopyLabelSuffix'
4524
                            ),
4525
                            $course_data['course_code'],
4526
                            $id,
4527
                            $sid,
4528
                            $params
4529
                        );
4530
4531
                        if ($course_info) {
4532
                            //By default new elements are invisible
4533
                            if ($set_exercises_lp_invisible) {
4534
                                $list = new LearnpathList('', $course_info['code'], $sid);
4535
                                $flat_list = $list->get_flat_list();
4536
                                if (!empty($flat_list)) {
4537
                                    foreach ($flat_list as $lp_id => $data) {
4538
                                        api_item_property_update(
4539
                                            $course_info,
4540
                                            TOOL_LEARNPATH,
4541
                                            $lp_id,
4542
                                            'invisible',
4543
                                            api_get_user_id(),
4544
                                            0,
4545
                                            0,
4546
                                            0,
4547
                                            0,
4548
                                            $sid
4549
                                        );
4550
                                    }
4551
                                }
4552
                                $quiz_table = Database::get_course_table(TABLE_QUIZ_TEST);
4553
                                $course_id = $course_info['real_id'];
4554
                                //@todo check this query
4555
                                $sql = "UPDATE $quiz_table SET active = 0
4556
                                        WHERE c_id = $course_id AND session_id = $sid";
4557
                                Database::query($sql);
4558
                            }
4559
                            $new_short_courses[] = $course_info['real_id'];
4560
                        }
4561
                    }
4562
                } else {
4563
                    foreach ($short_courses as $course_data) {
4564
                        $new_short_courses[] = $course_data['id'];
4565
                    }
4566
                }
4567
4568
                $short_courses = $new_short_courses;
4569
                self::add_courses_to_session($sid, $short_courses, true);
4570
                $short_courses = null;
4571
            }
4572
        }
4573
        if ($copy_users) {
4574
            // Register users from the original session to the new session
4575
            $users = self::get_users_by_session($id);
4576
            $short_users = [];
4577
            if (is_array($users) && count($users) > 0) {
4578
                foreach ($users as $user) {
4579
                    $short_users[] = $user['user_id'];
4580
                }
4581
            }
4582
            $users = null;
4583
            //Subscribing in read only mode
4584
            self::subscribeUsersToSession(
4585
                $sid,
4586
                $short_users,
4587
                SESSION_VISIBLE_READ_ONLY,
4588
                true
4589
            );
4590
            $short_users = null;
4591
        }
4592
4593
        return $sid;
4594
    }
4595
4596
    /**
4597
     * @param int $user_id
4598
     * @param int $session_id
4599
     *
4600
     * @return bool
4601
     */
4602
    public static function user_is_general_coach($user_id, $session_id)
4603
    {
4604
        $session_id = (int) $session_id;
4605
        $user_id = (int) $user_id;
4606
        $table = Database::get_main_table(TABLE_MAIN_SESSION);
4607
        $sql = "SELECT DISTINCT id
4608
	         	FROM $table
4609
	         	WHERE session.id_coach = '".$user_id."' AND id = '$session_id'";
4610
        $result = Database::query($sql);
4611
        if ($result && Database::num_rows($result)) {
4612
            return true;
4613
        }
4614
4615
        return false;
4616
    }
4617
4618
    /**
4619
     * Get the number of sessions.
4620
     *
4621
     * @param  int ID of the URL we want to filter on (optional)
4622
     *
4623
     * @return int Number of sessions
4624
     */
4625
    public static function count_sessions($access_url_id = null)
4626
    {
4627
        $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
4628
        $access_url_rel_session_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
4629
        $sql = "SELECT count(s.id) FROM $session_table s";
4630
        if (!empty($access_url_id) && $access_url_id == intval($access_url_id)) {
4631
            $sql .= ", $access_url_rel_session_table u ".
4632
                " WHERE s.id = u.session_id AND u.access_url_id = $access_url_id";
4633
        }
4634
        $res = Database::query($sql);
4635
        $row = Database::fetch_row($res);
4636
4637
        return $row[0];
4638
    }
4639
4640
    /**
4641
     * Return a COUNT from Session table.
4642
     *
4643
     * @param string $date in Y-m-d format
4644
     *
4645
     * @return int
4646
     */
4647
    public static function countSessionsByEndDate($date = null)
4648
    {
4649
        $count = 0;
4650
        $sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
4651
        $url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
4652
        $date = Database::escape_string($date);
4653
        $urlId = api_get_current_access_url_id();
4654
        $dateFilter = '';
4655
        if (!empty($date)) {
4656
            $dateFilter = <<<SQL
4657
                AND ('$date' BETWEEN s.access_start_date AND s.access_end_date)
4658
                OR (s.access_end_date IS NULL)
4659
                OR (s.access_start_date IS NULL AND
4660
                s.access_end_date IS NOT NULL AND s.access_end_date > '$date')
4661
SQL;
4662
        }
4663
        $sql = "SELECT COUNT(*) 
4664
                FROM $sessionTable s
4665
                INNER JOIN $url u
4666
                ON (s.id = u.session_id)
4667
                WHERE u.access_url_id = $urlId $dateFilter";
4668
        $res = Database::query($sql);
4669
        if ($res !== false && Database::num_rows($res) > 0) {
4670
            $count = current(Database::fetch_row($res));
4671
        }
4672
4673
        return $count;
4674
    }
4675
4676
    /**
4677
     * @param int  $id
4678
     * @param bool $checkSession
4679
     *
4680
     * @return bool
4681
     */
4682
    public static function cantEditSession($id, $checkSession = true)
4683
    {
4684
        if (!self::allowToManageSessions()) {
4685
            return false;
4686
        }
4687
4688
        if (api_is_platform_admin() && self::allowed($id)) {
4689
            return true;
4690
        }
4691
4692
        if ($checkSession) {
4693
            if (self::allowed($id)) {
4694
                return true;
4695
            }
4696
4697
            return false;
4698
        }
4699
4700
        return true;
4701
    }
4702
4703
    /**
4704
     * Protect a session to be edited.
4705
     *
4706
     * @param int  $id
4707
     * @param bool $checkSession
4708
     *
4709
     * @return mixed | bool true if pass the check, api_not_allowed otherwise
4710
     */
4711
    public static function protectSession($id, $checkSession = true)
4712
    {
4713
        if (!self::cantEditSession($id, $checkSession)) {
4714
            api_not_allowed(true);
4715
        }
4716
    }
4717
4718
    /**
4719
     * @return bool
4720
     */
4721
    public static function allowToManageSessions()
4722
    {
4723
        if (self::allowManageAllSessions()) {
4724
            return true;
4725
        }
4726
4727
        $setting = api_get_setting('allow_teachers_to_create_sessions');
4728
4729
        if (api_is_teacher() && $setting == 'true') {
4730
            return true;
4731
        }
4732
4733
        return false;
4734
    }
4735
4736
    /**
4737
     * @return bool
4738
     */
4739
    public static function allowOnlyMySessions()
4740
    {
4741
        if (self::allowToManageSessions() &&
4742
            !api_is_platform_admin() &&
4743
            api_is_teacher()
4744
        ) {
4745
            return true;
4746
        }
4747
4748
        return false;
4749
    }
4750
4751
    /**
4752
     * @return bool
4753
     */
4754
    public static function allowManageAllSessions()
4755
    {
4756
        if (api_is_platform_admin() || api_is_session_admin()) {
4757
            return true;
4758
        }
4759
4760
        return false;
4761
    }
4762
4763
    /**
4764
     * @param $id
4765
     *
4766
     * @return bool
4767
     */
4768
    public static function protect_teacher_session_edit($id)
4769
    {
4770
        if (!api_is_coach($id) && !api_is_platform_admin()) {
4771
            api_not_allowed(true);
4772
        } else {
4773
            return true;
4774
        }
4775
    }
4776
4777
    /**
4778
     * @param int $courseId
4779
     *
4780
     * @return array
4781
     */
4782
    public static function get_session_by_course($courseId)
4783
    {
4784
        $table_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
4785
        $table_session = Database::get_main_table(TABLE_MAIN_SESSION);
4786
        $url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
4787
        $courseId = intval($courseId);
4788
        $urlId = api_get_current_access_url_id();
4789
4790
        if (empty($courseId)) {
4791
            return [];
4792
        }
4793
4794
        $sql = "SELECT name, s.id
4795
                FROM $table_session_course sc
4796
                INNER JOIN $table_session s 
4797
                ON (sc.session_id = s.id)
4798
                INNER JOIN $url u
4799
                ON (u.session_id = s.id)
4800
                WHERE 
4801
                    u.access_url_id = $urlId AND 
4802
                    sc.c_id = '$courseId' ";
4803
        $result = Database::query($sql);
4804
4805
        return Database::store_result($result);
4806
    }
4807
4808
    /**
4809
     * @param int  $user_id
4810
     * @param bool $ignoreVisibilityForAdmins
4811
     * @param bool $ignoreTimeLimit
4812
     *
4813
     * @return array
4814
     */
4815
    public static function get_sessions_by_user(
4816
        $user_id,
4817
        $ignoreVisibilityForAdmins = false,
4818
        $ignoreTimeLimit = false
4819
    ) {
4820
        $sessionCategories = UserManager::get_sessions_by_category(
4821
            $user_id,
4822
            false,
4823
            $ignoreVisibilityForAdmins,
4824
            $ignoreTimeLimit
4825
        );
4826
4827
        $sessionArray = [];
4828
        if (!empty($sessionCategories)) {
4829
            foreach ($sessionCategories as $category) {
4830
                if (isset($category['sessions'])) {
4831
                    foreach ($category['sessions'] as $session) {
4832
                        $sessionArray[] = $session;
4833
                    }
4834
                }
4835
            }
4836
        }
4837
4838
        return $sessionArray;
4839
    }
4840
4841
    /**
4842
     * @param string $file
4843
     * @param bool   $updateSession                                   true: if the session exists it will be updated.
4844
     *                                                                false: if session exists a new session will be created adding a counter session1, session2, etc
4845
     * @param int    $defaultUserId
4846
     * @param Logger $logger
4847
     * @param array  $extraFields                                     convert a file row to an extra field. Example in CSV file there's a SessionID
4848
     *                                                                then it will converted to extra_external_session_id if you set: array('SessionId' => 'extra_external_session_id')
4849
     * @param string $extraFieldId
4850
     * @param int    $daysCoachAccessBeforeBeginning
4851
     * @param int    $daysCoachAccessAfterBeginning
4852
     * @param int    $sessionVisibility
4853
     * @param array  $fieldsToAvoidUpdate
4854
     * @param bool   $deleteUsersNotInList
4855
     * @param bool   $updateCourseCoaches
4856
     * @param bool   $sessionWithCoursesModifier
4857
     * @param bool   $addOriginalCourseTeachersAsCourseSessionCoaches
4858
     * @param bool   $removeAllTeachersFromCourse
4859
     * @param int    $showDescription
4860
     * @param array  $teacherBackupList
4861
     * @param array  $groupBackup
4862
     *
4863
     * @return array
4864
     */
4865
    public static function importCSV(
4866
        $file,
4867
        $updateSession,
4868
        $defaultUserId = null,
4869
        $logger = null,
4870
        $extraFields = [],
4871
        $extraFieldId = null,
4872
        $daysCoachAccessBeforeBeginning = null,
4873
        $daysCoachAccessAfterBeginning = null,
4874
        $sessionVisibility = 1,
4875
        $fieldsToAvoidUpdate = [],
4876
        $deleteUsersNotInList = false,
4877
        $updateCourseCoaches = false,
4878
        $sessionWithCoursesModifier = false,
4879
        $addOriginalCourseTeachersAsCourseSessionCoaches = true,
4880
        $removeAllTeachersFromCourse = true,
4881
        $showDescription = null,
4882
        &$teacherBackupList = [],
4883
        &$groupBackup = []
4884
    ) {
4885
        $content = file($file);
4886
        $error_message = null;
4887
        $session_counter = 0;
4888
        $defaultUserId = empty($defaultUserId) ? api_get_user_id() : (int) $defaultUserId;
4889
4890
        $eol = PHP_EOL;
4891
        if (PHP_SAPI != 'cli') {
4892
            $eol = '<br />';
4893
        }
4894
4895
        $debug = false;
4896
        if (isset($logger)) {
4897
            $debug = true;
4898
        }
4899
4900
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
4901
        $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
4902
        $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
4903
        $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
4904
        $sessions = [];
4905
        if (!api_strstr($content[0], ';')) {
4906
            $error_message = get_lang('NotCSV');
4907
        } else {
4908
            $tag_names = [];
4909
            foreach ($content as $key => $enreg) {
4910
                $enreg = explode(';', trim($enreg));
4911
                if ($key) {
4912
                    foreach ($tag_names as $tag_key => $tag_name) {
4913
                        if (isset($enreg[$tag_key])) {
4914
                            $sessions[$key - 1][$tag_name] = $enreg[$tag_key];
4915
                        }
4916
                    }
4917
                } else {
4918
                    foreach ($enreg as $tag_name) {
4919
                        $tag_names[] = api_preg_replace('/[^a-zA-Z0-9_\-]/', '', $tag_name);
4920
                    }
4921
                    if (!in_array('SessionName', $tag_names) ||
4922
                        !in_array('DateStart', $tag_names) ||
4923
                        !in_array('DateEnd', $tag_names)
4924
                    ) {
4925
                        $error_message = get_lang('NoNeededData');
4926
                        break;
4927
                    }
4928
                }
4929
            }
4930
4931
            $sessionList = [];
4932
            $report = [];
4933
4934
            // Looping the sessions.
4935
            foreach ($sessions as $enreg) {
4936
                $user_counter = 0;
4937
                $course_counter = 0;
4938
4939
                if (isset($extraFields) && !empty($extraFields)) {
4940
                    foreach ($extraFields as $original => $to) {
4941
                        $enreg[$to] = isset($enreg[$original]) ? $enreg[$original] : null;
4942
                    }
4943
                }
4944
4945
                $session_name = $enreg['SessionName'];
4946
4947
                if ($debug) {
4948
                    $logger->addInfo('---------------------------------------');
4949
                    $logger->addInfo("Sessions - Start process of session: $session_name");
4950
                    $logger->addInfo('---------------------------------------');
4951
                }
4952
4953
                // Default visibility
4954
                $visibilityAfterExpirationPerSession = $sessionVisibility;
4955
4956
                if (isset($enreg['VisibilityAfterExpiration'])) {
4957
                    $visibility = $enreg['VisibilityAfterExpiration'];
4958
                    switch ($visibility) {
4959
                        case 'read_only':
4960
                            $visibilityAfterExpirationPerSession = SESSION_VISIBLE_READ_ONLY;
4961
                            break;
4962
                        case 'accessible':
4963
                            $visibilityAfterExpirationPerSession = SESSION_VISIBLE;
4964
                            break;
4965
                        case 'not_accessible':
4966
                            $visibilityAfterExpirationPerSession = SESSION_INVISIBLE;
4967
                            break;
4968
                    }
4969
                }
4970
4971
                if (empty($session_name)) {
4972
                    continue;
4973
                }
4974
4975
                $displayAccessStartDate = isset($enreg['DisplayStartDate']) ? $enreg['DisplayStartDate'] : $enreg['DateStart'];
4976
                $displayAccessEndDate = isset($enreg['DisplayEndDate']) ? $enreg['DisplayEndDate'] : $enreg['DateEnd'];
4977
                $coachAccessStartDate = isset($enreg['CoachStartDate']) ? $enreg['CoachStartDate'] : $enreg['DateStart'];
4978
                $coachAccessEndDate = isset($enreg['CoachEndDate']) ? $enreg['CoachEndDate'] : $enreg['DateEnd'];
4979
                // We assume the dates are already in UTC
4980
                $dateStart = explode('/', $enreg['DateStart']);
4981
                $dateEnd = explode('/', $enreg['DateEnd']);
4982
                $dateStart = $dateStart[0].'-'.$dateStart[1].'-'.$dateStart[2].' 00:00:00';
4983
                $dateEnd = $dateEnd[0].'-'.$dateEnd[1].'-'.$dateEnd[2].' 23:59:59';
4984
                $displayAccessStartDate = explode('/', $displayAccessStartDate);
4985
                $displayAccessStartDate = implode('-', $displayAccessStartDate).' 00:00:00';
4986
                $displayAccessEndDate = explode('/', $displayAccessEndDate);
4987
                $displayAccessEndDate = implode('-', $displayAccessEndDate).' 23:59:59';
4988
                $coachAccessStartDate = explode('/', $coachAccessStartDate);
4989
                $coachAccessStartDate = implode('-', $coachAccessStartDate).' 00:00:00';
4990
                $coachAccessEndDate = explode('/', $coachAccessEndDate);
4991
                $coachAccessEndDate = implode('-', $coachAccessEndDate).' 23:59:59';
4992
                $session_category_id = isset($enreg['SessionCategory']) ? $enreg['SessionCategory'] : null;
4993
                $sessionDescription = isset($enreg['SessionDescription']) ? $enreg['SessionDescription'] : null;
4994
                $classes = isset($enreg['Classes']) ? explode('|', $enreg['Classes']) : [];
4995
                $extraParams = [];
4996
                if (!is_null($showDescription)) {
4997
                    $extraParams['show_description'] = intval($showDescription);
4998
                }
4999
5000
                $coachBefore = '';
5001
                $coachAfter = '';
5002
                if (!empty($daysCoachAccessBeforeBeginning) && !empty($daysCoachAccessAfterBeginning)) {
5003
                    $date = new \DateTime($dateStart);
5004
                    $interval = new DateInterval('P'.$daysCoachAccessBeforeBeginning.'D');
5005
                    $date->sub($interval);
5006
                    $coachBefore = $date->format('Y-m-d h:i');
5007
                    $coachAccessStartDate = $coachBefore;
5008
                    $coachBefore = api_get_utc_datetime($coachBefore);
5009
5010
                    $date = new \DateTime($dateEnd);
5011
                    $interval = new DateInterval('P'.$daysCoachAccessAfterBeginning.'D');
5012
                    $date->add($interval);
5013
                    $coachAfter = $date->format('Y-m-d h:i');
5014
                    $coachAccessEndDate = $coachAfter;
5015
                    $coachAfter = api_get_utc_datetime($coachAfter);
5016
                }
5017
5018
                $dateStart = api_get_utc_datetime($dateStart);
5019
                $dateEnd = api_get_utc_datetime($dateEnd);
5020
                $displayAccessStartDate = api_get_utc_datetime($displayAccessStartDate);
5021
                $displayAccessEndDate = api_get_utc_datetime($displayAccessEndDate);
5022
                $coachAccessStartDate = api_get_utc_datetime($coachAccessStartDate);
5023
                $coachAccessEndDate = api_get_utc_datetime($coachAccessEndDate);
5024
5025
                if (!empty($sessionDescription)) {
5026
                    $extraParams['description'] = $sessionDescription;
5027
                }
5028
5029
                if (!empty($session_category_id)) {
5030
                    $extraParams['session_category_id'] = $session_category_id;
5031
                }
5032
5033
                // Searching a general coach.
5034
                if (!empty($enreg['Coach'])) {
5035
                    $coach_id = UserManager::get_user_id_from_username($enreg['Coach']);
5036
                    if ($coach_id === false) {
5037
                        // If the coach-user does not exist - I'm the coach.
5038
                        $coach_id = $defaultUserId;
5039
                    }
5040
                } else {
5041
                    $coach_id = $defaultUserId;
5042
                }
5043
5044
                $users = explode('|', $enreg['Users']);
5045
                $courses = explode('|', $enreg['Courses']);
5046
5047
                $deleteOnlyCourseCoaches = false;
5048
                if (count($courses) == 1) {
5049
                    if ($logger) {
5050
                        $logger->addInfo('Only one course delete old coach list');
5051
                    }
5052
                    $deleteOnlyCourseCoaches = true;
5053
                }
5054
5055
                if (!$updateSession) {
5056
                    // Create a session.
5057
                    $unique_name = false;
5058
                    $i = 0;
5059
                    // Change session name, verify that session doesn't exist.
5060
                    $suffix = null;
5061
                    while (!$unique_name) {
5062
                        if ($i > 1) {
5063
                            $suffix = ' - '.$i;
5064
                        }
5065
                        $sql = 'SELECT 1 FROM '.$tbl_session.'
5066
                                WHERE name="'.Database::escape_string($session_name).$suffix.'"';
5067
                        $rs = Database::query($sql);
5068
                        if (Database::result($rs, 0, 0)) {
5069
                            $i++;
5070
                        } else {
5071
                            $unique_name = true;
5072
                            $session_name .= $suffix;
5073
                        }
5074
                    }
5075
5076
                    $sessionParams = [
5077
                        'name' => $session_name,
5078
                        'id_coach' => $coach_id,
5079
                        'access_start_date' => $dateStart,
5080
                        'access_end_date' => $dateEnd,
5081
                        'display_start_date' => $displayAccessStartDate,
5082
                        'display_end_date' => $displayAccessEndDate,
5083
                        'coach_access_start_date' => $coachAccessStartDate,
5084
                        'coach_access_end_date' => $coachAccessEndDate,
5085
                        'visibility' => $visibilityAfterExpirationPerSession,
5086
                        'session_admin_id' => $defaultUserId,
5087
                    ];
5088
5089
                    if (!empty($extraParams)) {
5090
                        $sessionParams = array_merge($sessionParams, $extraParams);
5091
                    }
5092
                    // Creating the session.
5093
                    $session_id = Database::insert($tbl_session, $sessionParams);
5094
                    if ($debug) {
5095
                        if ($session_id) {
5096
                            foreach ($enreg as $key => $value) {
5097
                                if (substr($key, 0, 6) == 'extra_') { //an extra field
5098
                                    self::update_session_extra_field_value($session_id, substr($key, 6), $value);
5099
                                }
5100
                            }
5101
                            $logger->addInfo("Session created: #$session_id - $session_name");
5102
                        } else {
5103
                            $message = "Sessions - Session NOT created: $session_name";
5104
                            $logger->addError($message);
5105
                            $report[] = $message;
5106
                        }
5107
                    }
5108
                    $session_counter++;
5109
                } else {
5110
                    $sessionId = null;
5111
                    if (isset($extraFields) && !empty($extraFields) && !empty($enreg['extra_'.$extraFieldId])) {
5112
                        $sessionId = self::getSessionIdFromOriginalId($enreg['extra_'.$extraFieldId], $extraFieldId);
5113
                        if (empty($sessionId)) {
5114
                            $my_session_result = false;
5115
                        } else {
5116
                            $my_session_result = true;
5117
                        }
5118
                    } else {
5119
                        $my_session_result = self::get_session_by_name($enreg['SessionName']);
5120
                    }
5121
5122
                    if ($my_session_result === false) {
5123
                        // One more check
5124
                        $sessionExistsWithName = self::get_session_by_name($session_name);
5125
                        if ($sessionExistsWithName) {
5126
                            if ($debug) {
5127
                                $message = "Skip Session - Trying to update a session, but name already exists: $session_name";
5128
                                $logger->addError($message);
5129
                                $report[] = $message;
5130
                            }
5131
                            continue;
5132
                        }
5133
5134
                        $sessionParams = [
5135
                            'name' => $session_name,
5136
                            'id_coach' => $coach_id,
5137
                            'access_start_date' => $dateStart,
5138
                            'access_end_date' => $dateEnd,
5139
                            'display_start_date' => $displayAccessStartDate,
5140
                            'display_end_date' => $displayAccessEndDate,
5141
                            'coach_access_start_date' => $coachAccessStartDate,
5142
                            'coach_access_end_date' => $coachAccessEndDate,
5143
                            'visibility' => $visibilityAfterExpirationPerSession,
5144
                            'session_admin_id' => $defaultUserId,
5145
                        ];
5146
5147
                        if (!empty($extraParams)) {
5148
                            $sessionParams = array_merge($sessionParams, $extraParams);
5149
                        }
5150
                        Database::insert($tbl_session, $sessionParams);
5151
5152
                        // We get the last insert id.
5153
                        $my_session_result = self::get_session_by_name($session_name);
5154
                        $session_id = $my_session_result['id'];
5155
5156
                        if ($session_id) {
5157
                            foreach ($enreg as $key => $value) {
5158
                                if (substr($key, 0, 6) == 'extra_') { //an extra field
5159
                                    self::update_session_extra_field_value($session_id, substr($key, 6), $value);
5160
                                }
5161
                            }
5162
                            if ($debug) {
5163
                                $logger->addInfo("Sessions - #$session_id created: $session_name");
5164
                            }
5165
5166
                            // Delete session-user relation only for students
5167
                            $sql = "DELETE FROM $tbl_session_user
5168
                                    WHERE session_id = '$session_id' AND relation_type <> ".SESSION_RELATION_TYPE_RRHH;
5169
                            Database::query($sql);
5170
5171
                            $sql = "DELETE FROM $tbl_session_course WHERE session_id = '$session_id'";
5172
                            Database::query($sql);
5173
5174
                            // Delete session-course-user relationships students and coaches.
5175
                            if ($updateCourseCoaches) {
5176
                                $sql = "DELETE FROM $tbl_session_course_user
5177
                                        WHERE session_id = '$session_id' AND status in ('0', '2')";
5178
                                Database::query($sql);
5179
                            } else {
5180
                                // Delete session-course-user relation ships *only* for students.
5181
                                $sql = "DELETE FROM $tbl_session_course_user
5182
                                        WHERE session_id = '$session_id' AND status <> 2";
5183
                                Database::query($sql);
5184
                            }
5185
                            if ($deleteOnlyCourseCoaches) {
5186
                                $sql = "DELETE FROM $tbl_session_course_user
5187
                                        WHERE session_id = '$session_id' AND status in ('2')";
5188
                                Database::query($sql);
5189
                            }
5190
                        }
5191
                    } else {
5192
                        // Updating the session.
5193
                        $params = [
5194
                            'id_coach' => $coach_id,
5195
                            'access_start_date' => $dateStart,
5196
                            'access_end_date' => $dateEnd,
5197
                            'display_start_date' => $displayAccessStartDate,
5198
                            'display_end_date' => $displayAccessEndDate,
5199
                            'coach_access_start_date' => $coachAccessStartDate,
5200
                            'coach_access_end_date' => $coachAccessEndDate,
5201
                            'visibility' => $visibilityAfterExpirationPerSession,
5202
                            'session_category_id' => $session_category_id,
5203
                        ];
5204
5205
                        if (!empty($sessionDescription)) {
5206
                            $params['description'] = $sessionDescription;
5207
                        }
5208
5209
                        if (!empty($fieldsToAvoidUpdate)) {
5210
                            foreach ($fieldsToAvoidUpdate as $field) {
5211
                                unset($params[$field]);
5212
                            }
5213
                        }
5214
5215
                        if (isset($sessionId) && !empty($sessionId)) {
5216
                            $session_id = $sessionId;
5217
                            if (!empty($enreg['SessionName'])) {
5218
                                $sessionExistsWithName = self::get_session_by_name($session_name);
5219
                                if ($sessionExistsWithName === false) {
5220
                                    $sessionName = Database::escape_string($enreg['SessionName']);
5221
                                    $sql = "UPDATE $tbl_session SET name = '$sessionName' WHERE id = $session_id";
5222
                                    Database::query($sql);
5223
                                    $logger->addInfo(
5224
                                        "Session #$session_id name IS updated with: '$session_name' External id: ".$enreg['extra_'.$extraFieldId]
5225
                                    );
5226
                                } else {
5227
                                    $sessionExistsBesidesMe = self::sessionNameExistBesidesMySession(
5228
                                        $session_id,
5229
                                        $session_name
5230
                                    );
5231
                                    if ($sessionExistsBesidesMe === true) {
5232
                                        if ($debug) {
5233
                                            $message = "Skip Session. Error when update session Session #$session_id Name: '$session_name'. Other session has the same name. External id: ".$enreg['extra_'.$extraFieldId];
5234
                                            $logger->addError($message);
5235
                                            $report[] = $message;
5236
                                        }
5237
                                        continue;
5238
                                    } else {
5239
                                        if ($debug) {
5240
                                            $logger->addInfo(
5241
                                                "Session #$session_id name is not updated because it didn't change (but update of other session values will continue) Name: '$session_name' External id: ".$enreg['extra_'.$extraFieldId]
5242
                                            );
5243
                                        }
5244
                                    }
5245
                                }
5246
                            }
5247
                        } else {
5248
                            $my_session_result = self::get_session_by_name($session_name);
5249
                            $session_id = $my_session_result['id'];
5250
                        }
5251
5252
                        if ($debug) {
5253
                            $logger->addInfo("Session #$session_id to be updated: '$session_name'");
5254
                        }
5255
5256
                        if ($session_id) {
5257
                            $sessionInfo = api_get_session_info($session_id);
5258
                            $params['show_description'] = isset($sessionInfo['show_description']) ? $sessionInfo['show_description'] : intval($showDescription);
5259
5260
                            if (!empty($daysCoachAccessBeforeBeginning) && !empty($daysCoachAccessAfterBeginning)) {
5261
                                if (empty($sessionInfo['nb_days_access_before_beginning']) ||
5262
                                    (!empty($sessionInfo['nb_days_access_before_beginning']) &&
5263
                                        $sessionInfo['nb_days_access_before_beginning'] < $daysCoachAccessBeforeBeginning)
5264
                                ) {
5265
                                    $params['coach_access_start_date'] = $coachBefore;
5266
                                }
5267
5268
                                if (empty($sessionInfo['nb_days_access_after_end']) ||
5269
                                    (!empty($sessionInfo['nb_days_access_after_end']) &&
5270
                                        $sessionInfo['nb_days_access_after_end'] < $daysCoachAccessAfterBeginning)
5271
                                ) {
5272
                                    $params['coach_access_end_date'] = $coachAfter;
5273
                                }
5274
                            }
5275
5276
                            Database::update($tbl_session, $params, ['id = ?' => $session_id]);
5277
                            foreach ($enreg as $key => $value) {
5278
                                if (substr($key, 0, 6) == 'extra_') { //an extra field
5279
                                    self::update_session_extra_field_value($session_id, substr($key, 6), $value);
5280
                                }
5281
                            }
5282
5283
                            if ($debug) {
5284
                                $logger->addInfo("Session updated #$session_id");
5285
                            }
5286
5287
                            // Delete session-user relation only for students
5288
                            $sql = "DELETE FROM $tbl_session_user
5289
                                    WHERE session_id = '$session_id' AND relation_type <> ".SESSION_RELATION_TYPE_RRHH;
5290
                            Database::query($sql);
5291
5292
                            $sql = "DELETE FROM $tbl_session_course WHERE session_id = '$session_id'";
5293
                            Database::query($sql);
5294
5295
                            // Delete session-course-user relationships students and coaches.
5296
                            if ($updateCourseCoaches) {
5297
                                $sql = "DELETE FROM $tbl_session_course_user
5298
                                        WHERE session_id = '$session_id' AND status in ('0', '2')";
5299
                                Database::query($sql);
5300
                            } else {
5301
                                // Delete session-course-user relation ships *only* for students.
5302
                                $sql = "DELETE FROM $tbl_session_course_user
5303
                                        WHERE session_id = '$session_id' AND status <> 2";
5304
                                Database::query($sql);
5305
                            }
5306
5307
                            if ($deleteOnlyCourseCoaches) {
5308
                                $sql = "DELETE FROM $tbl_session_course_user
5309
                                        WHERE session_id = '$session_id' AND status in ('2')";
5310
                                Database::query($sql);
5311
                            }
5312
                        } else {
5313
                            if ($debug) {
5314
                                $logger->addError(
5315
                                    "Sessions - Session not found"
5316
                                );
5317
                            }
5318
                        }
5319
                    }
5320
                    $session_counter++;
5321
                }
5322
5323
                $sessionList[] = $session_id;
5324
5325
                // Adding the relationship "Session - User" for students
5326
                $userList = [];
5327
                if (is_array($users)) {
5328
                    foreach ($users as $user) {
5329
                        $user_id = UserManager::get_user_id_from_username($user);
5330
                        if ($user_id !== false) {
5331
                            $userList[] = $user_id;
5332
                            // Insert new users.
5333
                            $sql = "INSERT IGNORE INTO $tbl_session_user SET
5334
                                    user_id = '$user_id',
5335
                                    session_id = '$session_id',
5336
                                    registered_at = '".api_get_utc_datetime()."'";
5337
                            Database::query($sql);
5338
                            if ($debug) {
5339
                                $logger->addInfo("Adding User #$user_id ($user) to session #$session_id");
5340
                            }
5341
                            $user_counter++;
5342
                        }
5343
                    }
5344
                }
5345
5346
                if ($deleteUsersNotInList) {
5347
                    // Getting user in DB in order to compare to the new list.
5348
                    $usersListInDatabase = self::get_users_by_session($session_id, 0);
5349
                    if (!empty($usersListInDatabase)) {
5350
                        if (empty($userList)) {
5351
                            foreach ($usersListInDatabase as $userInfo) {
5352
                                self::unsubscribe_user_from_session($session_id, $userInfo['user_id']);
5353
                            }
5354
                        } else {
5355
                            foreach ($usersListInDatabase as $userInfo) {
5356
                                if (!in_array($userInfo['user_id'], $userList)) {
5357
                                    self::unsubscribe_user_from_session($session_id, $userInfo['user_id']);
5358
                                }
5359
                            }
5360
                        }
5361
                    }
5362
                }
5363
5364
                // See BT#6449
5365
                $onlyAddFirstCoachOrTeacher = false;
5366
                if ($sessionWithCoursesModifier) {
5367
                    if (count($courses) >= 2) {
5368
                        // Only first teacher in course session;
5369
                        $onlyAddFirstCoachOrTeacher = true;
5370
                        // Remove all teachers from course.
5371
                        $removeAllTeachersFromCourse = false;
5372
                    }
5373
                }
5374
5375
                foreach ($courses as $course) {
5376
                    $courseArray = bracketsToArray($course);
5377
                    $course_code = $courseArray[0];
5378
5379
                    if (CourseManager::course_exists($course_code)) {
5380
                        $courseInfo = api_get_course_info($course_code);
5381
                        $courseId = $courseInfo['real_id'];
5382
5383
                        // Adding the course to a session.
5384
                        $sql = "INSERT IGNORE INTO $tbl_session_course
5385
                                SET c_id = '$courseId', session_id='$session_id'";
5386
                        Database::query($sql);
5387
5388
                        self::installCourse($session_id, $courseInfo['real_id']);
5389
5390
                        if ($debug) {
5391
                            $logger->addInfo("Adding course '$course_code' to session #$session_id");
5392
                        }
5393
5394
                        $course_counter++;
5395
                        $course_coaches = isset($courseArray[1]) ? $courseArray[1] : null;
5396
                        $course_users = isset($courseArray[2]) ? $courseArray[2] : null;
5397
                        $course_users = explode(',', $course_users);
5398
                        $course_coaches = explode(',', $course_coaches);
5399
5400
                        // Checking if the flag is set TeachersWillBeAddedAsCoachInAllCourseSessions (course_edit.php)
5401
                        $addTeachersToSession = true;
5402
5403
                        if (array_key_exists('add_teachers_to_sessions_courses', $courseInfo)) {
5404
                            $addTeachersToSession = $courseInfo['add_teachers_to_sessions_courses'];
5405
                        }
5406
5407
                        // If any user provided for a course, use the users array.
5408
                        if (empty($course_users)) {
5409
                            if (!empty($userList)) {
5410
                                self::subscribe_users_to_session_course(
5411
                                    $userList,
5412
                                    $session_id,
5413
                                    $course_code
5414
                                );
5415
                                if ($debug) {
5416
                                    $msg = "Adding student list ".implode(', #', $userList)." to course: '$course_code' and session #$session_id";
5417
                                    $logger->addInfo($msg);
5418
                                }
5419
                            }
5420
                        }
5421
5422
                        // Adding coaches to session course user.
5423
                        if (!empty($course_coaches)) {
5424
                            $savedCoaches = [];
5425
                            // only edit if add_teachers_to_sessions_courses is set.
5426
                            if ($addTeachersToSession) {
5427
                                if ($addOriginalCourseTeachersAsCourseSessionCoaches) {
5428
                                    // Adding course teachers as course session teachers.
5429
                                    $alreadyAddedTeachers = CourseManager::get_teacher_list_from_course_code(
5430
                                        $course_code
5431
                                    );
5432
5433
                                    if (!empty($alreadyAddedTeachers)) {
5434
                                        $teachersToAdd = [];
5435
                                        foreach ($alreadyAddedTeachers as $user) {
5436
                                            $teachersToAdd[] = $user['username'];
5437
                                        }
5438
                                        $course_coaches = array_merge(
5439
                                            $course_coaches,
5440
                                            $teachersToAdd
5441
                                        );
5442
                                    }
5443
                                }
5444
5445
                                foreach ($course_coaches as $course_coach) {
5446
                                    $coach_id = UserManager::get_user_id_from_username($course_coach);
5447
                                    if ($coach_id !== false) {
5448
                                        // Just insert new coaches
5449
                                        self::updateCoaches(
5450
                                            $session_id,
5451
                                            $courseId,
5452
                                            [$coach_id],
5453
                                            false
5454
                                        );
5455
5456
                                        if ($debug) {
5457
                                            $logger->addInfo("Adding course coach: user #$coach_id ($course_coach) to course: '$course_code' and session #$session_id");
5458
                                        }
5459
                                        $savedCoaches[] = $coach_id;
5460
                                    } else {
5461
                                        $error_message .= get_lang('UserDoesNotExist').' : '.$course_coach.$eol;
5462
                                    }
5463
                                }
5464
                            }
5465
5466
                            // Custom courses/session coaches
5467
                            $teacherToAdd = null;
5468
                            // Only one coach is added.
5469
                            if ($onlyAddFirstCoachOrTeacher == true) {
5470
                                if ($debug) {
5471
                                    $logger->addInfo("onlyAddFirstCoachOrTeacher : true");
5472
                                }
5473
5474
                                foreach ($course_coaches as $course_coach) {
5475
                                    $coach_id = UserManager::get_user_id_from_username($course_coach);
5476
                                    if ($coach_id !== false) {
5477
                                        $teacherToAdd = $coach_id;
5478
                                        break;
5479
                                    }
5480
                                }
5481
5482
                                // Un subscribe everyone that's not in the list.
5483
                                $teacherList = CourseManager::get_teacher_list_from_course_code($course_code);
5484
                                if (!empty($teacherList)) {
5485
                                    foreach ($teacherList as $teacher) {
5486
                                        if ($teacherToAdd != $teacher['user_id']) {
5487
                                            $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
5488
                                                    WHERE
5489
                                                        user_id = ".$teacher['user_id']." AND
5490
                                                        c_id = '".$courseId."'
5491
                                                    ";
5492
5493
                                            $result = Database::query($sql);
5494
                                            $rows = Database::num_rows($result);
5495
                                            if ($rows > 0) {
5496
                                                $userCourseData = Database::fetch_array($result, 'ASSOC');
5497
                                                if (!empty($userCourseData)) {
5498
                                                    $teacherBackupList[$teacher['user_id']][$course_code] = $userCourseData;
5499
                                                }
5500
                                            }
5501
5502
                                            $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_USER)."
5503
                                                    WHERE
5504
                                                        user_id = ".$teacher['user_id']." AND
5505
                                                        c_id = '".$courseInfo['real_id']."'
5506
                                                    ";
5507
5508
                                            $result = Database::query($sql);
5509
                                            while ($groupData = Database::fetch_array($result, 'ASSOC')) {
5510
                                                $groupBackup['user'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData;
5511
                                            }
5512
5513
                                            $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)."
5514
                                                    WHERE
5515
                                                        user_id = ".$teacher['user_id']." AND
5516
                                                        c_id = '".$courseInfo['real_id']."'
5517
                                                    ";
5518
5519
                                            $result = Database::query($sql);
5520
                                            while ($groupData = Database::fetch_array($result, 'ASSOC')) {
5521
                                                $groupBackup['tutor'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData;
5522
                                            }
5523
5524
                                            CourseManager::unsubscribe_user(
5525
                                                $teacher['user_id'],
5526
                                                $course_code
5527
                                            );
5528
5529
                                            if ($debug) {
5530
                                                $logger->addInfo("Delete user #".$teacher['user_id']." from base course: $course_code");
5531
                                            }
5532
                                        }
5533
                                    }
5534
                                }
5535
5536
                                if (!empty($teacherToAdd)) {
5537
                                    self::updateCoaches(
5538
                                        $session_id,
5539
                                        $courseId,
5540
                                        [$teacherToAdd],
5541
                                        true
5542
                                    );
5543
5544
                                    if ($debug) {
5545
                                        $logger->addInfo("Add coach #$teacherToAdd to course $courseId and session $session_id");
5546
                                    }
5547
5548
                                    $userCourseCategory = '';
5549
                                    if (isset($teacherBackupList[$teacherToAdd]) &&
5550
                                        isset($teacherBackupList[$teacherToAdd][$course_code])
5551
                                    ) {
5552
                                        $courseUserData = $teacherBackupList[$teacherToAdd][$course_code];
5553
                                        $userCourseCategory = $courseUserData['user_course_cat'];
5554
                                    }
5555
5556
                                    CourseManager::subscribeUser(
5557
                                        $teacherToAdd,
5558
                                        $course_code,
5559
                                        COURSEMANAGER,
5560
                                        0,
5561
                                        $userCourseCategory
5562
                                    );
5563
5564
                                    if ($debug) {
5565
                                        $logger->addInfo("Subscribe user #$teacherToAdd as teacher in course $course_code with user userCourseCategory $userCourseCategory");
5566
                                    }
5567
5568
                                    if (isset($groupBackup['user'][$teacherToAdd]) &&
5569
                                        isset($groupBackup['user'][$teacherToAdd][$course_code]) &&
5570
                                        !empty($groupBackup['user'][$teacherToAdd][$course_code])
5571
                                    ) {
5572
                                        foreach ($groupBackup['user'][$teacherToAdd][$course_code] as $data) {
5573
                                            $groupInfo = GroupManager::get_group_properties($data['group_id']);
5574
                                            GroupManager::subscribe_users(
5575
                                                $teacherToAdd,
5576
                                                $groupInfo,
5577
                                                $data['c_id']
5578
                                            );
5579
                                        }
5580
                                    }
5581
5582
                                    if (isset($groupBackup['tutor'][$teacherToAdd]) &&
5583
                                        isset($groupBackup['tutor'][$teacherToAdd][$course_code]) &&
5584
                                        !empty($groupBackup['tutor'][$teacherToAdd][$course_code])
5585
                                    ) {
5586
                                        foreach ($groupBackup['tutor'][$teacherToAdd][$course_code] as $data) {
5587
                                            $groupInfo = GroupManager::get_group_properties($data['group_id']);
5588
                                            GroupManager::subscribe_tutors(
5589
                                                $teacherToAdd,
5590
                                                $groupInfo,
5591
                                                $data['c_id']
5592
                                            );
5593
                                        }
5594
                                    }
5595
                                }
5596
                            }
5597
5598
                            // See BT#6449#note-195
5599
                            // All coaches are added.
5600
                            if ($removeAllTeachersFromCourse) {
5601
                                if ($debug) {
5602
                                    $logger->addInfo("removeAllTeachersFromCourse true");
5603
                                }
5604
                                $teacherToAdd = null;
5605
                                foreach ($course_coaches as $course_coach) {
5606
                                    $coach_id = UserManager::get_user_id_from_username(
5607
                                        $course_coach
5608
                                    );
5609
                                    if ($coach_id !== false) {
5610
                                        $teacherToAdd[] = $coach_id;
5611
                                    }
5612
                                }
5613
5614
                                if (!empty($teacherToAdd)) {
5615
                                    // Deleting all course teachers and adding the only coach as teacher.
5616
                                    $teacherList = CourseManager::get_teacher_list_from_course_code($course_code);
5617
5618
                                    if (!empty($teacherList)) {
5619
                                        foreach ($teacherList as $teacher) {
5620
                                            if (!in_array($teacher['user_id'], $teacherToAdd)) {
5621
                                                $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
5622
                                                        WHERE
5623
                                                            user_id = ".$teacher['user_id']." AND
5624
                                                            c_id = '".$courseId."'
5625
                                                        ";
5626
5627
                                                $result = Database::query($sql);
5628
                                                $rows = Database::num_rows($result);
5629
                                                if ($rows > 0) {
5630
                                                    $userCourseData = Database::fetch_array($result, 'ASSOC');
5631
                                                    if (!empty($userCourseData)) {
5632
                                                        $teacherBackupList[$teacher['user_id']][$course_code] = $userCourseData;
5633
                                                    }
5634
                                                }
5635
5636
                                                $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_USER)."
5637
                                                        WHERE
5638
                                                            user_id = ".$teacher['user_id']." AND
5639
                                                            c_id = '".$courseInfo['real_id']."'
5640
                                                        ";
5641
5642
                                                $result = Database::query($sql);
5643
                                                while ($groupData = Database::fetch_array($result, 'ASSOC')) {
5644
                                                    $groupBackup['user'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData;
5645
                                                }
5646
5647
                                                $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)."
5648
                                                        WHERE
5649
                                                            user_id = ".$teacher['user_id']." AND
5650
                                                            c_id = '".$courseInfo['real_id']."'
5651
                                                        ";
5652
5653
                                                $result = Database::query($sql);
5654
                                                while ($groupData = Database::fetch_array($result, 'ASSOC')) {
5655
                                                    $groupBackup['tutor'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData;
5656
                                                }
5657
5658
                                                CourseManager::unsubscribe_user(
5659
                                                    $teacher['user_id'],
5660
                                                    $course_code
5661
                                                );
5662
5663
                                                if ($debug) {
5664
                                                    $logger->addInfo("Delete user #".$teacher['user_id']." from base course: $course_code");
5665
                                                }
5666
                                            }
5667
                                        }
5668
                                    }
5669
5670
                                    foreach ($teacherToAdd as $teacherId) {
5671
                                        $userCourseCategory = '';
5672
                                        if (isset($teacherBackupList[$teacherId]) &&
5673
                                            isset($teacherBackupList[$teacherId][$course_code])
5674
                                        ) {
5675
                                            $courseUserData = $teacherBackupList[$teacherId][$course_code];
5676
                                            $userCourseCategory = $courseUserData['user_course_cat'];
5677
                                        }
5678
5679
                                        CourseManager::subscribeUser(
5680
                                            $teacherId,
5681
                                            $course_code,
5682
                                            COURSEMANAGER,
5683
                                            0,
5684
                                            $userCourseCategory
5685
                                        );
5686
5687
                                        if ($debug) {
5688
                                            $logger->addInfo("Add user as teacher #".$teacherId." in base course: $course_code with userCourseCategory: $userCourseCategory");
5689
                                        }
5690
5691
                                        if (isset($groupBackup['user'][$teacherId]) &&
5692
                                            isset($groupBackup['user'][$teacherId][$course_code]) &&
5693
                                            !empty($groupBackup['user'][$teacherId][$course_code])
5694
                                        ) {
5695
                                            foreach ($groupBackup['user'][$teacherId][$course_code] as $data) {
5696
                                                $groupInfo = GroupManager::get_group_properties($data['group_id']);
5697
                                                GroupManager::subscribe_users(
5698
                                                    $teacherId,
5699
                                                    $groupInfo,
5700
                                                    $data['c_id']
5701
                                                );
5702
                                            }
5703
                                        }
5704
5705
                                        if (isset($groupBackup['tutor'][$teacherId]) &&
5706
                                            isset($groupBackup['tutor'][$teacherId][$course_code]) &&
5707
                                            !empty($groupBackup['tutor'][$teacherId][$course_code])
5708
                                        ) {
5709
                                            foreach ($groupBackup['tutor'][$teacherId][$course_code] as $data) {
5710
                                                $groupInfo = GroupManager::get_group_properties($data['group_id']);
5711
                                                GroupManager::subscribe_tutors(
5712
                                                    $teacherId,
5713
                                                    $groupInfo,
5714
                                                    $data['c_id']
5715
                                                );
5716
                                            }
5717
                                        }
5718
                                    }
5719
                                }
5720
                            }
5721
5722
                            // Continue default behaviour.
5723
                            if ($onlyAddFirstCoachOrTeacher == false) {
5724
                                // Checking one more time see BT#6449#note-149
5725
                                $coaches = self::getCoachesByCourseSession($session_id, $courseId);
5726
                                // Update coaches if only there's 1 course see BT#6449#note-189
5727
                                if (empty($coaches) || count($courses) == 1) {
5728
                                    foreach ($course_coaches as $course_coach) {
5729
                                        $course_coach = trim($course_coach);
5730
                                        $coach_id = UserManager::get_user_id_from_username($course_coach);
5731
                                        if ($coach_id !== false) {
5732
                                            // Just insert new coaches
5733
                                            self::updateCoaches(
5734
                                                $session_id,
5735
                                                $courseId,
5736
                                                [$coach_id],
5737
                                                false
5738
                                            );
5739
5740
                                            if ($debug) {
5741
                                                $logger->addInfo("Sessions - Adding course coach: user #$coach_id ($course_coach) to course: '$course_code' and session #$session_id");
5742
                                            }
5743
                                            $savedCoaches[] = $coach_id;
5744
                                        } else {
5745
                                            $error_message .= get_lang('UserDoesNotExist').' : '.$course_coach.$eol;
5746
                                        }
5747
                                    }
5748
                                }
5749
                            }
5750
                        }
5751
5752
                        // Adding Students, updating relationship "Session - Course - User".
5753
                        $course_users = array_filter($course_users);
5754
                        if (!empty($course_users)) {
5755
                            foreach ($course_users as $user) {
5756
                                $user_id = UserManager::get_user_id_from_username($user);
5757
5758
                                if ($user_id !== false) {
5759
                                    self::subscribe_users_to_session_course(
5760
                                        [$user_id],
5761
                                        $session_id,
5762
                                        $course_code
5763
                                    );
5764
                                    if ($debug) {
5765
                                        $logger->addInfo("Adding student: user #$user_id ($user) to course: '$course_code' and session #$session_id");
5766
                                    }
5767
                                } else {
5768
                                    $error_message .= get_lang('UserDoesNotExist').': '.$user.$eol;
5769
                                }
5770
                            }
5771
                        }
5772
                        $inserted_in_course[$course_code] = $courseInfo['title'];
5773
                    }
5774
                }
5775
                $access_url_id = api_get_current_access_url_id();
5776
                UrlManager::add_session_to_url($session_id, $access_url_id);
5777
                $sql = "UPDATE $tbl_session SET nbr_users = '$user_counter', nbr_courses = '$course_counter' 
5778
                        WHERE id = '$session_id'";
5779
                Database::query($sql);
5780
5781
                self::addClassesByName($session_id, $classes, false);
5782
5783
                if ($debug) {
5784
                    $logger->addInfo("End process session #$session_id -------------------- ");
5785
                }
5786
            }
5787
5788
            if (!empty($report)) {
5789
                if ($debug) {
5790
                    $logger->addInfo("--Summary--");
5791
                    foreach ($report as $line) {
5792
                        $logger->addInfo($line);
5793
                    }
5794
                }
5795
            }
5796
        }
5797
5798
        return [
5799
            'error_message' => $error_message,
5800
            'session_counter' => $session_counter,
5801
            'session_list' => $sessionList,
5802
        ];
5803
    }
5804
5805
    /**
5806
     * @param int $sessionId
5807
     * @param int $courseId
5808
     *
5809
     * @return array
5810
     */
5811
    public static function getCoachesByCourseSession($sessionId, $courseId)
5812
    {
5813
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
5814
        $sessionId = intval($sessionId);
5815
        $courseId = intval($courseId);
5816
5817
        $sql = "SELECT user_id FROM $table
5818
                WHERE
5819
                    session_id = '$sessionId' AND
5820
                    c_id = '$courseId' AND
5821
                    status = 2";
5822
        $result = Database::query($sql);
5823
5824
        $coaches = [];
5825
        if (Database::num_rows($result) > 0) {
5826
            while ($row = Database::fetch_array($result)) {
5827
                $coaches[] = $row['user_id'];
5828
            }
5829
        }
5830
5831
        return $coaches;
5832
    }
5833
5834
    /**
5835
     * @param int $sessionId
5836
     * @param int $courseId
5837
     *
5838
     * @return string
5839
     */
5840
    public static function getCoachesByCourseSessionToString(
5841
        $sessionId,
5842
        $courseId
5843
    ) {
5844
        $coaches = self::getCoachesByCourseSession($sessionId, $courseId);
5845
        $list = [];
5846
        if (!empty($coaches)) {
5847
            foreach ($coaches as $coachId) {
5848
                $userInfo = api_get_user_info($coachId);
5849
                if ($userInfo) {
5850
                    $list[] = $userInfo['complete_name'];
5851
                }
5852
            }
5853
        }
5854
5855
        return array_to_string($list, CourseManager::USER_SEPARATOR);
5856
    }
5857
5858
    /**
5859
     * Get all coaches added in the session - course relationship.
5860
     *
5861
     * @param int $sessionId
5862
     *
5863
     * @return array
5864
     */
5865
    public static function getCoachesBySession($sessionId)
5866
    {
5867
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
5868
        $sessionId = intval($sessionId);
5869
5870
        $sql = "SELECT DISTINCT user_id
5871
                FROM $table
5872
                WHERE session_id = '$sessionId' AND status = 2";
5873
        $result = Database::query($sql);
5874
5875
        $coaches = [];
5876
        if (Database::num_rows($result) > 0) {
5877
            while ($row = Database::fetch_array($result)) {
5878
                $coaches[] = $row['user_id'];
5879
            }
5880
        }
5881
5882
        return $coaches;
5883
    }
5884
5885
    /**
5886
     * @param int $userId
5887
     *
5888
     * @return array
5889
     */
5890
    public static function getAllCoursesFromAllSessionFromDrh($userId)
5891
    {
5892
        $sessions = self::get_sessions_followed_by_drh($userId);
5893
        $coursesFromSession = [];
5894
        if (!empty($sessions)) {
5895
            foreach ($sessions as $session) {
5896
                $courseList = self::get_course_list_by_session_id($session['id']);
5897
                foreach ($courseList as $course) {
5898
                    $coursesFromSession[] = $course['code'];
5899
                }
5900
            }
5901
        }
5902
5903
        return $coursesFromSession;
5904
    }
5905
5906
    /**
5907
     * getAllCoursesFromAllSessions.
5908
     *
5909
     * @return array
5910
     */
5911
    public static function getAllCoursesFromAllSessions()
5912
    {
5913
        $sessions = self::get_sessions_list();
5914
        $coursesFromSession = [];
5915
        if (!empty($sessions)) {
5916
            foreach ($sessions as $session) {
5917
                $courseList = self::get_course_list_by_session_id($session['id']);
5918
                foreach ($courseList as $course) {
5919
                    $coursesFromSession[$course['code'].':'.$session['id']] = $course['visual_code'].' - '.$course['title'].' ('.$session['name'].')';
5920
                }
5921
            }
5922
        }
5923
5924
        return $coursesFromSession;
5925
    }
5926
5927
    /**
5928
     * @param string $status
5929
     * @param int    $userId
5930
     * @param bool   $getCount
5931
     * @param int    $from
5932
     * @param int    $numberItems
5933
     * @param int    $column
5934
     * @param string $direction
5935
     * @param string $keyword
5936
     * @param string $active
5937
     * @param string $lastConnectionDate
5938
     * @param array  $sessionIdList
5939
     * @param array  $studentIdList
5940
     * @param int    $filterByStatus
5941
     *
5942
     * @return array|int
5943
     */
5944
    public static function getAllUsersFromCoursesFromAllSessionFromStatus(
5945
        $status,
5946
        $userId,
5947
        $getCount = false,
5948
        $from = null,
5949
        $numberItems = null,
5950
        $column = 1,
5951
        $direction = 'asc',
5952
        $keyword = null,
5953
        $active = null,
5954
        $lastConnectionDate = null,
5955
        $sessionIdList = [],
5956
        $studentIdList = [],
5957
        $filterByStatus = null
5958
    ) {
5959
        $filterByStatus = intval($filterByStatus);
5960
5961
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
5962
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
5963
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
5964
        $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
5965
        $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
5966
        $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
5967
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
5968
        $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
5969
5970
        $direction = in_array(strtolower($direction), ['asc', 'desc']) ? $direction : 'asc';
5971
        $column = Database::escape_string($column);
5972
        $userId = intval($userId);
5973
5974
        $limitCondition = '';
5975
        if (isset($from) && isset($numberItems)) {
5976
            $from = intval($from);
5977
            $numberItems = intval($numberItems);
5978
            $limitCondition = "LIMIT $from, $numberItems";
5979
        }
5980
5981
        $urlId = api_get_current_access_url_id();
5982
5983
        $sessionConditions = '';
5984
        $courseConditions = '';
5985
        $userConditions = '';
5986
5987
        if (isset($active)) {
5988
            $active = intval($active);
5989
            $userConditions .= " AND active = $active";
5990
        }
5991
5992
        $courseList = CourseManager::get_courses_followed_by_drh($userId, DRH);
5993
        if (!empty($courseList)) {
5994
            $courseIdList = array_column($courseList, 'id');
5995
            $courseConditions = ' AND c.id IN ("'.implode('","', $courseIdList).'")';
5996
        }
5997
5998
        $userConditionsFromDrh = '';
5999
6000
        // Classic DRH
6001
        if (empty($studentIdList)) {
6002
            $studentListSql = UserManager::get_users_followed_by_drh(
6003
                $userId,
6004
                $filterByStatus,
6005
                true,
6006
                false
6007
            );
6008
            if (!empty($studentListSql)) {
6009
                $studentIdList = array_keys($studentListSql);
6010
                $studentListSql = "'".implode("','", $studentIdList)."'";
6011
            }
6012
        } else {
6013
            $studentIdList = array_map('intval', $studentIdList);
6014
            $studentListSql = "'".implode("','", $studentIdList)."'";
6015
        }
6016
        if (!empty($studentListSql)) {
6017
            $userConditionsFromDrh = " AND u.user_id IN (".$studentListSql.") ";
6018
        }
6019
6020
        switch ($status) {
6021
            case 'drh':
6022
                break;
6023
            case 'drh_all':
6024
                // Show all by DRH
6025
                if (empty($sessionIdList)) {
6026
                    $sessionsListSql = self::get_sessions_followed_by_drh(
6027
                        $userId,
6028
                        null,
6029
                        null,
6030
                        false,
6031
                        true,
6032
                        true
6033
                    );
6034
                } else {
6035
                    $sessionIdList = array_map('intval', $sessionIdList);
6036
                    $sessionsListSql = "'".implode("','", $sessionIdList)."'";
6037
                }
6038
                if (!empty($sessionsListSql)) {
6039
                    $sessionConditions = " AND s.id IN (".$sessionsListSql.") ";
6040
                }
6041
                break;
6042
            case 'session_admin':
6043
                $sessionConditions = " AND s.id_coach = $userId ";
6044
                $userConditionsFromDrh = '';
6045
                break;
6046
            case 'admin':
6047
                break;
6048
            case 'teacher':
6049
                $sessionConditions = " AND s.id_coach = $userId ";
6050
                $userConditionsFromDrh = '';
6051
                break;
6052
        }
6053
6054
        $select = "SELECT DISTINCT u.* ";
6055
        $masterSelect = "SELECT DISTINCT * FROM ";
6056
6057
        if ($getCount) {
6058
            $select = "SELECT DISTINCT u.user_id ";
6059
            $masterSelect = "SELECT COUNT(DISTINCT(user_id)) as count FROM ";
6060
        }
6061
6062
        if (!empty($filterByStatus)) {
6063
            $userConditions .= " AND u.status = ".$filterByStatus;
6064
        }
6065
6066
        if (!empty($lastConnectionDate)) {
6067
            $lastConnectionDate = Database::escape_string($lastConnectionDate);
6068
            $userConditions .= " AND u.last_login <= '$lastConnectionDate' ";
6069
        }
6070
6071
        if (!empty($keyword)) {
6072
            $keyword = Database::escape_string($keyword);
6073
            $userConditions .= " AND (
6074
                u.username LIKE '%$keyword%' OR
6075
                u.firstname LIKE '%$keyword%' OR
6076
                u.lastname LIKE '%$keyword%' OR
6077
                u.official_code LIKE '%$keyword%' OR
6078
                u.email LIKE '%$keyword%'
6079
            )";
6080
        }
6081
6082
        $where = " WHERE
6083
                   access_url_id = $urlId
6084
                   $userConditions
6085
        ";
6086
6087
        $userUnion = '';
6088
        if (!empty($userConditionsFromDrh)) {
6089
            $userUnion = "
6090
            UNION (
6091
                $select                    
6092
                FROM $tbl_user u
6093
                INNER JOIN $tbl_user_rel_access_url url ON (url.user_id = u.id)
6094
                $where
6095
                $userConditionsFromDrh
6096
            )";
6097
        }
6098
6099
        $sql = "$masterSelect (
6100
                ($select
6101
                FROM $tbl_session s
6102
                    INNER JOIN $tbl_session_rel_course_rel_user su ON (s.id = su.session_id)
6103
                    INNER JOIN $tbl_user u ON (u.user_id = su.user_id)
6104
                    INNER JOIN $tbl_session_rel_access_url url ON (url.session_id = s.id)
6105
                    $where
6106
                    $sessionConditions
6107
                    $userConditionsFromDrh
6108
                ) UNION (
6109
                    $select
6110
                    FROM $tbl_course c
6111
                    INNER JOIN $tbl_course_user cu ON (cu.c_id = c.id)
6112
                    INNER JOIN $tbl_user u ON (u.user_id = cu.user_id)
6113
                    INNER JOIN $tbl_course_rel_access_url url ON (url.c_id = c.id)
6114
                    $where
6115
                    $courseConditions
6116
                    $userConditionsFromDrh
6117
                ) $userUnion
6118
                ) as t1
6119
                ";
6120
6121
        if ($getCount) {
6122
            $result = Database::query($sql);
6123
6124
            $count = 0;
6125
            if (Database::num_rows($result)) {
6126
                $rows = Database::fetch_array($result);
6127
                $count = $rows['count'];
6128
            }
6129
6130
            return $count;
6131
        }
6132
6133
        if (!empty($column) && !empty($direction)) {
6134
            $column = str_replace('u.', '', $column);
6135
            $sql .= " ORDER BY $column $direction ";
6136
        }
6137
        $sql .= $limitCondition;
6138
        $result = Database::query($sql);
6139
        $result = Database::store_result($result);
6140
6141
        return $result;
6142
    }
6143
6144
    /**
6145
     * @param int   $sessionId
6146
     * @param int   $courseId
6147
     * @param array $coachList
6148
     * @param bool  $deleteCoachesNotInList
6149
     */
6150
    public static function updateCoaches(
6151
        $sessionId,
6152
        $courseId,
6153
        $coachList,
6154
        $deleteCoachesNotInList = false
6155
    ) {
6156
        $currentCoaches = self::getCoachesByCourseSession($sessionId, $courseId);
6157
6158
        if (!empty($coachList)) {
6159
            foreach ($coachList as $userId) {
6160
                self::set_coach_to_course_session($userId, $sessionId, $courseId);
6161
            }
6162
        }
6163
6164
        if ($deleteCoachesNotInList) {
6165
            if (!empty($coachList)) {
6166
                $coachesToDelete = array_diff($currentCoaches, $coachList);
6167
            } else {
6168
                $coachesToDelete = $currentCoaches;
6169
            }
6170
6171
            if (!empty($coachesToDelete)) {
6172
                foreach ($coachesToDelete as $userId) {
6173
                    self::set_coach_to_course_session(
6174
                        $userId,
6175
                        $sessionId,
6176
                        $courseId,
6177
                        true
6178
                    );
6179
                }
6180
            }
6181
        }
6182
    }
6183
6184
    /**
6185
     * @param array $sessions
6186
     * @param array $sessionsDestination
6187
     *
6188
     * @return array
6189
     */
6190
    public static function copyStudentsFromSession($sessions, $sessionsDestination)
6191
    {
6192
        $messages = [];
6193
        if (!empty($sessions)) {
6194
            foreach ($sessions as $sessionId) {
6195
                $sessionInfo = self::fetch($sessionId);
6196
                $userList = self::get_users_by_session($sessionId, 0);
6197
                if (!empty($userList)) {
6198
                    $newUserList = [];
6199
                    $userToString = null;
6200
                    foreach ($userList as $userInfo) {
6201
                        $newUserList[] = $userInfo['user_id'];
6202
                        $userToString .= $userInfo['firstname'].' '.$userInfo['lastname'].'<br />';
6203
                    }
6204
6205
                    if (!empty($sessionsDestination)) {
6206
                        foreach ($sessionsDestination as $sessionDestinationId) {
6207
                            $sessionDestinationInfo = self::fetch($sessionDestinationId);
6208
                            $messages[] = Display::return_message(
6209
                                sprintf(
6210
                                    get_lang(
6211
                                        'AddingStudentsFromSessionXToSessionY'
6212
                                    ),
6213
                                    $sessionInfo['name'],
6214
                                    $sessionDestinationInfo['name']
6215
                                ),
6216
                                'info',
6217
                                false
6218
                            );
6219
                            if ($sessionId == $sessionDestinationId) {
6220
                                $messages[] = Display::return_message(
6221
                                    sprintf(
6222
                                        get_lang('SessionXSkipped'),
6223
                                        $sessionDestinationId
6224
                                    ),
6225
                                    'warning',
6226
                                    false
6227
                                );
6228
                                continue;
6229
                            }
6230
                            $messages[] = Display::return_message(get_lang('StudentList').'<br />'.$userToString, 'info', false);
6231
                            self::subscribeUsersToSession(
6232
                                $sessionDestinationId,
6233
                                $newUserList,
6234
                                SESSION_VISIBLE_READ_ONLY,
6235
                                false
6236
                            );
6237
                        }
6238
                    } else {
6239
                        $messages[] = Display::return_message(get_lang('NoDestinationSessionProvided'), 'warning');
6240
                    }
6241
                } else {
6242
                    $messages[] = Display::return_message(
6243
                        get_lang('NoStudentsFoundForSession').' #'.$sessionInfo['name'],
6244
                        'warning'
6245
                    );
6246
                }
6247
            }
6248
        } else {
6249
            $messages[] = Display::return_message(get_lang('NoData'), 'warning');
6250
        }
6251
6252
        return $messages;
6253
    }
6254
6255
    /**
6256
     * Assign coaches of a session(s) as teachers to a given course (or courses).
6257
     *
6258
     * @param array A list of session IDs
6259
     * @param array A list of course IDs
6260
     *
6261
     * @return string
6262
     */
6263
    public static function copyCoachesFromSessionToCourse($sessions, $courses)
6264
    {
6265
        $coachesPerSession = [];
6266
        foreach ($sessions as $sessionId) {
6267
            $coaches = self::getCoachesBySession($sessionId);
6268
            $coachesPerSession[$sessionId] = $coaches;
6269
        }
6270
6271
        $result = [];
6272
6273
        if (!empty($courses)) {
6274
            foreach ($courses as $courseId) {
6275
                $courseInfo = api_get_course_info_by_id($courseId);
6276
                foreach ($coachesPerSession as $sessionId => $coachList) {
6277
                    CourseManager::updateTeachers(
6278
                        $courseInfo,
6279
                        $coachList,
6280
                        false,
6281
                        false,
6282
                        false
6283
                    );
6284
                    $result[$courseInfo['code']][$sessionId] = $coachList;
6285
                }
6286
            }
6287
        }
6288
        $sessionUrl = api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session=';
6289
        $htmlResult = null;
6290
6291
        if (!empty($result)) {
6292
            foreach ($result as $courseCode => $data) {
6293
                $url = api_get_course_url($courseCode);
6294
                $htmlResult .= sprintf(
6295
                    get_lang('CoachesSubscribedAsATeacherInCourseX'),
6296
                    Display::url($courseCode, $url, ['target' => '_blank'])
6297
                );
6298
                foreach ($data as $sessionId => $coachList) {
6299
                    $sessionInfo = self::fetch($sessionId);
6300
                    $htmlResult .= '<br />';
6301
                    $htmlResult .= Display::url(
6302
                        get_lang('Session').': '.$sessionInfo['name'].' <br />',
6303
                        $sessionUrl.$sessionId,
6304
                        ['target' => '_blank']
6305
                    );
6306
                    $teacherList = [];
6307
                    foreach ($coachList as $coachId) {
6308
                        $userInfo = api_get_user_info($coachId);
6309
                        $teacherList[] = $userInfo['complete_name'];
6310
                    }
6311
                    if (!empty($teacherList)) {
6312
                        $htmlResult .= implode(', ', $teacherList);
6313
                    } else {
6314
                        $htmlResult .= get_lang('NothingToAdd');
6315
                    }
6316
                }
6317
                $htmlResult .= '<br />';
6318
            }
6319
            $htmlResult = Display::return_message($htmlResult, 'normal', false);
6320
        }
6321
6322
        return $htmlResult;
6323
    }
6324
6325
    /**
6326
     * @param string $keyword
6327
     * @param string $active
6328
     * @param string $lastConnectionDate
6329
     * @param array  $sessionIdList
6330
     * @param array  $studentIdList
6331
     * @param int    $filterUserStatus   STUDENT|COURSEMANAGER constants
6332
     *
6333
     * @return array|int
6334
     */
6335
    public static function getCountUserTracking(
6336
        $keyword = null,
6337
        $active = null,
6338
        $lastConnectionDate = null,
6339
        $sessionIdList = [],
6340
        $studentIdList = [],
6341
        $filterUserStatus = null
6342
    ) {
6343
        $userId = api_get_user_id();
6344
        $drhLoaded = false;
6345
6346
        if (api_is_drh()) {
6347
            if (api_drh_can_access_all_session_content()) {
6348
                $count = self::getAllUsersFromCoursesFromAllSessionFromStatus(
6349
                    'drh_all',
6350
                    $userId,
6351
                    true,
6352
                    null,
6353
                    null,
6354
                    null,
6355
                    null,
6356
                    $keyword,
6357
                    $active,
6358
                    $lastConnectionDate,
6359
                    $sessionIdList,
6360
                    $studentIdList,
6361
                    $filterUserStatus
6362
                );
6363
                $drhLoaded = true;
6364
            }
6365
        }
6366
6367
        if ($drhLoaded == false) {
6368
            $count = UserManager::getUsersFollowedByUser(
6369
                $userId,
6370
                $filterUserStatus,
6371
                false,
6372
                false,
6373
                true,
6374
                null,
6375
                null,
6376
                null,
6377
                null,
6378
                $active,
6379
                $lastConnectionDate,
6380
                api_is_student_boss() ? STUDENT_BOSS : COURSEMANAGER,
6381
                $keyword
6382
            );
6383
        }
6384
6385
        return $count;
6386
    }
6387
6388
    /**
6389
     * Get teachers followed by a user.
6390
     *
6391
     * @param int    $userId
6392
     * @param int    $active
6393
     * @param string $lastConnectionDate
6394
     * @param bool   $getCount
6395
     * @param array  $sessionIdList
6396
     *
6397
     * @return array|int
6398
     */
6399
    public static function getTeacherTracking(
6400
        $userId,
6401
        $active = 1,
6402
        $lastConnectionDate = null,
6403
        $getCount = false,
6404
        $sessionIdList = []
6405
    ) {
6406
        $teacherListId = [];
6407
        if (api_is_drh() || api_is_platform_admin()) {
6408
            // Followed teachers by drh
6409
            if (api_drh_can_access_all_session_content()) {
6410
                if (empty($sessionIdList)) {
6411
                    $sessions = self::get_sessions_followed_by_drh($userId);
6412
                    $sessionIdList = [];
6413
                    foreach ($sessions as $session) {
6414
                        $sessionIdList[] = $session['id'];
6415
                    }
6416
                }
6417
6418
                $sessionIdList = array_map('intval', $sessionIdList);
6419
                $sessionToString = implode("', '", $sessionIdList);
6420
6421
                $course = Database::get_main_table(TABLE_MAIN_COURSE);
6422
                $sessionCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
6423
                $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
6424
6425
                // Select the teachers.
6426
                $sql = "SELECT DISTINCT(cu.user_id) 
6427
                        FROM $course c
6428
                        INNER JOIN $sessionCourse src 
6429
                        ON c.id = src.c_id
6430
                        INNER JOIN $courseUser cu 
6431
                        ON (cu.c_id = c.id)
6432
		                WHERE src.session_id IN ('$sessionToString') AND cu.status = 1";
6433
                $result = Database::query($sql);
6434
                while ($row = Database::fetch_array($result, 'ASSOC')) {
6435
                    $teacherListId[$row['user_id']] = $row['user_id'];
6436
                }
6437
            } else {
6438
                $teacherResult = UserManager::get_users_followed_by_drh($userId, COURSEMANAGER);
6439
                foreach ($teacherResult as $userInfo) {
6440
                    $teacherListId[] = $userInfo['user_id'];
6441
                }
6442
            }
6443
        }
6444
6445
        if (!empty($teacherListId)) {
6446
            $tableUser = Database::get_main_table(TABLE_MAIN_USER);
6447
6448
            $select = "SELECT DISTINCT u.* ";
6449
            if ($getCount) {
6450
                $select = "SELECT count(DISTINCT(u.user_id)) as count";
6451
            }
6452
6453
            $sql = "$select FROM $tableUser u";
6454
6455
            if (!empty($lastConnectionDate)) {
6456
                $tableLogin = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
6457
                //$sql .= " INNER JOIN $tableLogin l ON (l.login_user_id = u.user_id) ";
6458
            }
6459
            $active = intval($active);
6460
            $teacherListId = implode("','", $teacherListId);
6461
            $where = " WHERE u.active = $active AND u.user_id IN ('$teacherListId') ";
6462
6463
            if (!empty($lastConnectionDate)) {
6464
                $lastConnectionDate = Database::escape_string($lastConnectionDate);
6465
                //$where .= " AND l.login_date <= '$lastConnectionDate' ";
6466
            }
6467
6468
            $sql .= $where;
6469
            $result = Database::query($sql);
6470
            if (Database::num_rows($result)) {
6471
                if ($getCount) {
6472
                    $row = Database::fetch_array($result);
6473
6474
                    return $row['count'];
6475
                } else {
6476
                    return Database::store_result($result, 'ASSOC');
6477
                }
6478
            }
6479
        }
6480
6481
        return 0;
6482
    }
6483
6484
    /**
6485
     * Get the list of course tools that have to be dealt with in case of
6486
     * registering any course to a session.
6487
     *
6488
     * @return array The list of tools to be dealt with (literal names)
6489
     */
6490
    public static function getCourseToolToBeManaged()
6491
    {
6492
        return [
6493
            'courseDescription',
6494
            'courseIntroduction',
6495
        ];
6496
    }
6497
6498
    /**
6499
     * Calls the methods bound to each tool when a course is registered into a session.
6500
     *
6501
     * @param int $sessionId
6502
     * @param int $courseId
6503
     *
6504
     * @return bool
6505
     */
6506
    public static function installCourse($sessionId, $courseId)
6507
    {
6508
        return true;
6509
        $toolList = self::getCourseToolToBeManaged();
0 ignored issues
show
Unused Code introduced by
$toolList = self::getCourseToolToBeManaged() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
6510
6511
        foreach ($toolList as $tool) {
6512
            $method = 'add'.$tool;
6513
            if (method_exists(get_class(), $method)) {
6514
                self::$method($sessionId, $courseId);
6515
            }
6516
        }
6517
    }
6518
6519
    /**
6520
     * Calls the methods bound to each tool when a course is unregistered from
6521
     * a session.
6522
     *
6523
     * @param int $sessionId
6524
     * @param int $courseId
6525
     */
6526
    public static function unInstallCourse($sessionId, $courseId)
6527
    {
6528
        return true;
6529
        $toolList = self::getCourseToolToBeManaged();
0 ignored issues
show
Unused Code introduced by
$toolList = self::getCourseToolToBeManaged() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
6530
6531
        foreach ($toolList as $tool) {
6532
            $method = 'remove'.$tool;
6533
            if (method_exists(get_class(), $method)) {
6534
                self::$method($sessionId, $courseId);
6535
            }
6536
        }
6537
    }
6538
6539
    /**
6540
     * @param int $sessionId
6541
     * @param int $courseId
6542
     */
6543
    public static function addCourseIntroduction($sessionId, $courseId)
6544
    {
6545
        // @todo create a tool intro lib
6546
        $sessionId = intval($sessionId);
6547
        $courseId = intval($courseId);
6548
6549
        $TBL_INTRODUCTION = Database::get_course_table(TABLE_TOOL_INTRO);
6550
        $sql = "SELECT * FROM $TBL_INTRODUCTION WHERE c_id = $courseId";
6551
        $result = Database::query($sql);
6552
        $result = Database::store_result($result, 'ASSOC');
6553
6554
        if (!empty($result)) {
6555
            foreach ($result as $result) {
6556
                // @todo check if relation exits.
6557
                $result['session_id'] = $sessionId;
6558
                Database::insert($TBL_INTRODUCTION, $result);
6559
            }
6560
        }
6561
    }
6562
6563
    /**
6564
     * @param int $sessionId
6565
     * @param int $courseId
6566
     */
6567
    public static function removeCourseIntroduction($sessionId, $courseId)
6568
    {
6569
        $sessionId = intval($sessionId);
6570
        $courseId = intval($courseId);
6571
        $TBL_INTRODUCTION = Database::get_course_table(TABLE_TOOL_INTRO);
6572
        $sql = "DELETE FROM $TBL_INTRODUCTION
6573
                WHERE c_id = $courseId AND session_id = $sessionId";
6574
        Database::query($sql);
6575
    }
6576
6577
    /**
6578
     * @param int $sessionId
6579
     * @param int $courseId
6580
     */
6581
    public static function addCourseDescription($sessionId, $courseId)
6582
    {
6583
        /* $description = new CourseDescription();
6584
          $descriptions = $description->get_descriptions($courseId);
6585
          foreach ($descriptions as $description) {
6586
          } */
6587
    }
6588
6589
    /**
6590
     * @param int $sessionId
6591
     * @param int $courseId
6592
     */
6593
    public static function removeCourseDescription($sessionId, $courseId)
6594
    {
6595
    }
6596
6597
    /**
6598
     * @param array $userSessionList        format see self::importSessionDrhCSV()
6599
     * @param bool  $sendEmail
6600
     * @param bool  $removeOldRelationShips
6601
     */
6602
    public static function subscribeDrhToSessionList(
6603
        $userSessionList,
6604
        $sendEmail,
6605
        $removeOldRelationShips
6606
    ) {
6607
        if (!empty($userSessionList)) {
6608
            foreach ($userSessionList as $userId => $data) {
6609
                $sessionList = [];
6610
                foreach ($data['session_list'] as $sessionInfo) {
6611
                    $sessionList[] = $sessionInfo['session_id'];
6612
                }
6613
                $userInfo = $data['user_info'];
6614
                self::subscribeSessionsToDrh(
6615
                    $userInfo,
6616
                    $sessionList,
6617
                    $sendEmail,
6618
                    $removeOldRelationShips
6619
                );
6620
            }
6621
        }
6622
    }
6623
6624
    /**
6625
     * @param array $userSessionList format see self::importSessionDrhCSV()
6626
     *
6627
     * @return string
6628
     */
6629
    public static function checkSubscribeDrhToSessionList($userSessionList)
6630
    {
6631
        $message = null;
6632
        if (!empty($userSessionList)) {
6633
            if (!empty($userSessionList)) {
6634
                foreach ($userSessionList as $userId => $data) {
6635
                    $userInfo = $data['user_info'];
6636
6637
                    $sessionListSubscribed = self::get_sessions_followed_by_drh($userId);
6638
                    if (!empty($sessionListSubscribed)) {
6639
                        $sessionListSubscribed = array_keys($sessionListSubscribed);
6640
                    }
6641
6642
                    $sessionList = [];
6643
                    if (!empty($data['session_list'])) {
6644
                        foreach ($data['session_list'] as $sessionInfo) {
6645
                            if (in_array($sessionInfo['session_id'], $sessionListSubscribed)) {
6646
                                $sessionList[] = $sessionInfo['session_info']['name'];
6647
                            }
6648
                        }
6649
                    }
6650
6651
                    $message .= '<strong>'.get_lang('User').'</strong>: ';
6652
                    $message .= $userInfo['complete_name_with_username'].' <br />';
6653
6654
                    if (!in_array($userInfo['status'], [DRH]) && !api_is_platform_admin_by_id($userInfo['user_id'])) {
6655
                        $message .= get_lang('UserMustHaveTheDrhRole').'<br />';
6656
                        continue;
6657
                    }
6658
6659
                    if (!empty($sessionList)) {
6660
                        $message .= '<strong>'.get_lang('Sessions').':</strong> <br />';
6661
                        $message .= implode(', ', $sessionList).'<br /><br />';
6662
                    } else {
6663
                        $message .= get_lang('NoSessionProvided').' <br /><br />';
6664
                    }
6665
                }
6666
            }
6667
        }
6668
6669
        return $message;
6670
    }
6671
6672
    /**
6673
     * @param string $file
6674
     * @param bool   $sendEmail
6675
     * @param bool   $removeOldRelationShips
6676
     *
6677
     * @return string
6678
     */
6679
    public static function importSessionDrhCSV($file, $sendEmail, $removeOldRelationShips)
6680
    {
6681
        $list = Import::csv_reader($file);
6682
6683
        if (!empty($list)) {
6684
            $userSessionList = [];
6685
            foreach ($list as $data) {
6686
                $userInfo = api_get_user_info_from_username($data['Username']);
6687
                $sessionInfo = self::get_session_by_name($data['SessionName']);
6688
6689
                if (empty($sessionInfo)) {
6690
                    Display::addFlash(Display::return_message(get_lang('NoSessionId').' - '.$data['SessionName'], 'warning'));
6691
                }
6692
6693
                if (empty($userInfo)) {
6694
                    Display::addFlash(Display::return_message(get_lang('UserDoesNotExist').' - '.$data['Username'], 'warning'));
6695
                }
6696
6697
                if (!empty($userInfo) && !empty($sessionInfo)) {
6698
                    $userSessionList[$userInfo['user_id']]['session_list'][] = [
6699
                        'session_id' => $sessionInfo['id'],
6700
                        'session_info' => $sessionInfo,
6701
                    ];
6702
                    $userSessionList[$userInfo['user_id']]['user_info'] = $userInfo;
6703
                }
6704
            }
6705
6706
            self::subscribeDrhToSessionList($userSessionList, $sendEmail, $removeOldRelationShips);
6707
6708
            return self::checkSubscribeDrhToSessionList($userSessionList);
6709
        }
6710
    }
6711
6712
    /**
6713
     * Courses re-ordering in resume_session.php flag see BT#8316.
6714
     */
6715
    public static function orderCourseIsEnabled()
6716
    {
6717
        $sessionCourseOrder = api_get_setting('session_course_ordering');
6718
        if ($sessionCourseOrder === 'true') {
6719
            return true;
6720
        }
6721
6722
        return false;
6723
    }
6724
6725
    /**
6726
     * @param string $direction (up/down)
6727
     * @param int    $sessionId
6728
     * @param int    $courseId
6729
     *
6730
     * @return bool
6731
     */
6732
    public static function move($direction, $sessionId, $courseId)
6733
    {
6734
        if (!self::orderCourseIsEnabled()) {
6735
            return false;
6736
        }
6737
6738
        $sessionId = intval($sessionId);
6739
        $courseId = intval($courseId);
6740
6741
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
6742
        $courseList = self::get_course_list_by_session_id($sessionId, null, 'position');
6743
6744
        $position = [];
6745
        $count = 0;
6746
        foreach ($courseList as $course) {
6747
            if ($course['position'] == '') {
6748
                $course['position'] = $count;
6749
            }
6750
            $position[$course['code']] = $course['position'];
6751
            // Saving current order.
6752
            $sql = "UPDATE $table SET position = $count
6753
                    WHERE session_id = $sessionId AND c_id = '".$course['real_id']."'";
6754
            Database::query($sql);
6755
            $count++;
6756
        }
6757
6758
        // Loading new positions.
6759
        $courseList = self::get_course_list_by_session_id($sessionId, null, 'position');
6760
6761
        $found = false;
6762
6763
        switch ($direction) {
6764
            case 'up':
6765
                $courseList = array_reverse($courseList);
6766
                break;
6767
            case 'down':
6768
                break;
6769
        }
6770
6771
        foreach ($courseList as $course) {
6772
            if ($found) {
6773
                $nextId = $course['real_id'];
6774
                $nextOrder = $course['position'];
6775
                break;
6776
            }
6777
6778
            if ($courseId == $course['real_id']) {
6779
                $thisCourseCode = $course['real_id'];
6780
                $thisOrder = $course['position'];
6781
                $found = true;
6782
            }
6783
        }
6784
6785
        $sql1 = "UPDATE $table SET position = '".intval($nextOrder)."'
6786
                 WHERE session_id = $sessionId AND c_id =  $thisCourseCode";
6787
        Database::query($sql1);
6788
6789
        $sql2 = "UPDATE $table SET position = '".intval($thisOrder)."'
6790
                 WHERE session_id = $sessionId AND c_id = $nextId";
6791
        Database::query($sql2);
6792
6793
        return true;
6794
    }
6795
6796
    /**
6797
     * @param int $sessionId
6798
     * @param int $courseId
6799
     *
6800
     * @return bool
6801
     */
6802
    public static function moveUp($sessionId, $courseId)
6803
    {
6804
        return self::move('up', $sessionId, $courseId);
6805
    }
6806
6807
    /**
6808
     * @param int    $sessionId
6809
     * @param string $courseCode
6810
     *
6811
     * @return bool
6812
     */
6813
    public static function moveDown($sessionId, $courseCode)
6814
    {
6815
        return self::move('down', $sessionId, $courseCode);
6816
    }
6817
6818
    /**
6819
     * Use the session duration to allow/block user access see BT#8317
6820
     * Needs these DB changes
6821
     * ALTER TABLE session ADD COLUMN duration int;
6822
     * ALTER TABLE session_rel_user ADD COLUMN duration int;.
6823
     */
6824
    public static function durationPerUserIsEnabled()
6825
    {
6826
        return api_get_configuration_value('session_duration_feature');
6827
    }
6828
6829
    /**
6830
     * Returns the number of days the student has left in a session when using
6831
     * sessions durations.
6832
     *
6833
     * @param array $sessionInfo
6834
     * @param int   $userId
6835
     *
6836
     * @return int
6837
     */
6838
    public static function getDayLeftInSession(array $sessionInfo, $userId)
6839
    {
6840
        $sessionId = $sessionInfo['id'];
6841
        $subscription = self::getUserSession($userId, $sessionId);
6842
        $duration = empty($subscription['duration'])
6843
            ? $sessionInfo['duration']
6844
            : $sessionInfo['duration'] + $subscription['duration'];
6845
6846
        // Get an array with the details of the first access of the student to
6847
        // this session
6848
        $courseAccess = CourseManager::getFirstCourseAccessPerSessionAndUser(
6849
            $sessionId,
6850
            $userId
6851
        );
6852
6853
        $currentTime = time();
6854
6855
        // If no previous access, return false
6856
        if (count($courseAccess) == 0) {
6857
            return $duration;
6858
        }
6859
6860
        $firstAccess = api_strtotime($courseAccess['login_course_date'], 'UTC');
6861
        $endDateInSeconds = $firstAccess + $duration * 24 * 60 * 60;
6862
        $leftDays = round(($endDateInSeconds - $currentTime) / 60 / 60 / 24);
6863
6864
        return $leftDays;
6865
    }
6866
6867
    /**
6868
     * @param int $duration
6869
     * @param int $userId
6870
     * @param int $sessionId
6871
     *
6872
     * @return bool
6873
     */
6874
    public static function editUserSessionDuration($duration, $userId, $sessionId)
6875
    {
6876
        $duration = intval($duration);
6877
        $userId = intval($userId);
6878
        $sessionId = intval($sessionId);
6879
6880
        if (empty($userId) || empty($sessionId)) {
6881
            return false;
6882
        }
6883
6884
        $table = Database::get_main_table(TABLE_MAIN_SESSION_USER);
6885
        $parameters = ['duration' => $duration];
6886
        $where = ['session_id = ? AND user_id = ? ' => [$sessionId, $userId]];
6887
        Database::update($table, $parameters, $where);
6888
6889
        return true;
6890
    }
6891
6892
    /**
6893
     * Gets one row from the session_rel_user table.
6894
     *
6895
     * @param int $userId
6896
     * @param int $sessionId
6897
     *
6898
     * @return array
6899
     */
6900
    public static function getUserSession($userId, $sessionId)
6901
    {
6902
        $userId = (int) $userId;
6903
        $sessionId = (int) $sessionId;
6904
6905
        if (empty($userId) || empty($sessionId)) {
6906
            return false;
6907
        }
6908
6909
        $table = Database::get_main_table(TABLE_MAIN_SESSION_USER);
6910
        $sql = "SELECT * FROM $table
6911
                WHERE session_id = $sessionId AND user_id = $userId";
6912
        $result = Database::query($sql);
6913
        $values = [];
6914
        if (Database::num_rows($result)) {
6915
            $values = Database::fetch_array($result, 'ASSOC');
6916
        }
6917
6918
        return $values;
6919
    }
6920
6921
    /**
6922
     * Check if user is subscribed inside a session as student.
6923
     *
6924
     * @param int $sessionId The session id
6925
     * @param int $userId    The user id
6926
     *
6927
     * @return bool Whether is subscribed
6928
     */
6929
    public static function isUserSubscribedAsStudent($sessionId, $userId)
6930
    {
6931
        $sessionRelUserTable = Database::get_main_table(TABLE_MAIN_SESSION_USER);
6932
        $sessionId = intval($sessionId);
6933
        $userId = intval($userId);
6934
6935
        // COUNT(1) actually returns the number of rows from the table (as if
6936
        // counting the results from the first column)
6937
        $sql = "SELECT COUNT(1) AS qty FROM $sessionRelUserTable
6938
                WHERE
6939
                    session_id = $sessionId AND
6940
                    user_id = $userId AND
6941
                    relation_type = 0";
6942
6943
        $result = Database::fetch_assoc(Database::query($sql));
6944
6945
        if (!empty($result) && $result['qty'] > 0) {
6946
            return true;
6947
        }
6948
6949
        return false;
6950
    }
6951
6952
    /**
6953
     * Check if user is subscribed inside a session as a HRM.
6954
     *
6955
     * @param int $sessionId The session id
6956
     * @param int $userId    The user id
6957
     *
6958
     * @return bool Whether is subscribed
6959
     */
6960
    public static function isUserSubscribedAsHRM($sessionId, $userId)
6961
    {
6962
        $sessionRelUserTable = Database::get_main_table(TABLE_MAIN_SESSION_USER);
6963
6964
        $sessionId = intval($sessionId);
6965
        $userId = intval($userId);
6966
6967
        // COUNT(1) actually returns the number of rows from the table (as if
6968
        // counting the results from the first column)
6969
        $sql = "SELECT COUNT(1) AS qty FROM $sessionRelUserTable
6970
                WHERE
6971
                    session_id = $sessionId AND
6972
                    user_id = $userId AND
6973
                    relation_type = ".SESSION_RELATION_TYPE_RRHH;
6974
6975
        $result = Database::fetch_assoc(Database::query($sql));
6976
6977
        if (!empty($result) && $result['qty'] > 0) {
6978
            return true;
6979
        }
6980
6981
        return false;
6982
    }
6983
6984
    /**
6985
     * Get the session coached by a user (general coach and course-session coach).
6986
     *
6987
     * @param int  $coachId                       The coach id
6988
     * @param bool $checkSessionRelUserVisibility Check the session visibility
6989
     * @param bool $asPlatformAdmin               The user is a platform admin and we want all sessions
6990
     *
6991
     * @return array The session list
6992
     */
6993
    public static function getSessionsCoachedByUser(
6994
        $coachId,
6995
        $checkSessionRelUserVisibility = false,
6996
        $asPlatformAdmin = false
6997
    ) {
6998
        // Get all sessions where $coachId is the general coach
6999
        $sessions = self::get_sessions_by_general_coach($coachId, $asPlatformAdmin);
7000
        // Get all sessions where $coachId is the course - session coach
7001
        $courseSessionList = self::getCoursesListByCourseCoach($coachId);
7002
        $sessionsByCoach = [];
7003
        if (!empty($courseSessionList)) {
7004
            foreach ($courseSessionList as $userCourseSubscription) {
7005
                $session = $userCourseSubscription->getSession();
7006
                $sessionsByCoach[$session->getId()] = api_get_session_info(
7007
                    $session->getId()
7008
                );
7009
            }
7010
        }
7011
7012
        if (!empty($sessionsByCoach)) {
7013
            $sessions = array_merge($sessions, $sessionsByCoach);
7014
        }
7015
7016
        // Remove repeated sessions
7017
        if (!empty($sessions)) {
7018
            $cleanSessions = [];
7019
            foreach ($sessions as $session) {
7020
                $cleanSessions[$session['id']] = $session;
7021
            }
7022
            $sessions = $cleanSessions;
7023
        }
7024
7025
        if ($checkSessionRelUserVisibility) {
7026
            if (!empty($sessions)) {
7027
                $newSessions = [];
7028
                foreach ($sessions as $session) {
7029
                    $visibility = api_get_session_visibility($session['id']);
7030
                    if ($visibility == SESSION_INVISIBLE) {
7031
                        continue;
7032
                    }
7033
                    $newSessions[] = $session;
7034
                }
7035
                $sessions = $newSessions;
7036
            }
7037
        }
7038
7039
        return $sessions;
7040
    }
7041
7042
    /**
7043
     * Check if the course belongs to the session.
7044
     *
7045
     * @param int    $sessionId  The session id
7046
     * @param string $courseCode The course code
7047
     *
7048
     * @return bool
7049
     */
7050
    public static function sessionHasCourse($sessionId, $courseCode)
7051
    {
7052
        $sessionId = intval($sessionId);
7053
        $courseCode = Database::escape_string($courseCode);
7054
        $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
7055
        $sessionRelCourseTable = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
7056
7057
        $sql = "SELECT COUNT(1) AS qty
7058
                FROM $courseTable c
7059
                INNER JOIN $sessionRelCourseTable src
7060
                ON c.id = src.c_id
7061
                WHERE src.session_id = $sessionId
7062
                AND c.code = '$courseCode'  ";
7063
7064
        $result = Database::query($sql);
7065
7066
        if ($result !== false) {
7067
            $data = Database::fetch_assoc($result);
7068
7069
            if ($data['qty'] > 0) {
7070
                return true;
7071
            }
7072
        }
7073
7074
        return false;
7075
    }
7076
7077
    /**
7078
     * Get the list of course coaches.
7079
     *
7080
     * @return array The list
7081
     */
7082
    public static function getAllCourseCoaches()
7083
    {
7084
        $coaches = [];
7085
7086
        $scuTable = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
7087
        $userTable = Database::get_main_table(TABLE_MAIN_USER);
7088
7089
        $idResult = Database::select('DISTINCT user_id', $scuTable, [
7090
            'where' => [
7091
                'status = ?' => 2,
7092
            ],
7093
        ]);
7094
7095
        if ($idResult != false) {
7096
            foreach ($idResult as $idData) {
7097
                $userResult = Database::select(
7098
                    'user_id, lastname, firstname, username',
7099
                    $userTable,
7100
                    [
7101
                        'where' => [
7102
                            'user_id = ?' => $idData['user_id'],
7103
                        ],
7104
                    ],
7105
                    'first'
7106
                );
7107
7108
                if ($userResult != false) {
7109
                    $coaches[] = [
7110
                        'id' => $userResult['user_id'],
7111
                        'lastname' => $userResult['lastname'],
7112
                        'firstname' => $userResult['firstname'],
7113
                        'username' => $userResult['username'],
7114
                        'completeName' => api_get_person_name(
7115
                            $userResult['firstname'],
7116
                            $userResult['lastname']
7117
                        ),
7118
                    ];
7119
                }
7120
            }
7121
        }
7122
7123
        return $coaches;
7124
    }
7125
7126
    /**
7127
     * Calculate the total user time in the platform.
7128
     *
7129
     * @param int    $userId The user id
7130
     * @param string $from   Optional. From date
7131
     * @param string $until  Optional. Until date
7132
     *
7133
     * @return string The time (hh:mm:ss)
7134
     */
7135
    public static function getTotalUserTimeInPlatform($userId, $from = '', $until = '')
7136
    {
7137
        $userId = intval($userId);
7138
        $trackLoginTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
7139
        $whereConditions = [
7140
            'login_user_id = ? ' => $userId,
7141
        ];
7142
7143
        if (!empty($from) && !empty($until)) {
7144
            $whereConditions["AND (login_date >= '?' "] = $from;
7145
            $whereConditions["AND logout_date <= DATE_ADD('?', INTERVAL 1 DAY)) "] = $until;
7146
        }
7147
7148
        $trackResult = Database::select(
7149
            'SEC_TO_TIME(SUM(UNIX_TIMESTAMP(logout_date) - UNIX_TIMESTAMP(login_date))) as total_time',
7150
            $trackLoginTable,
7151
            [
7152
                'where' => $whereConditions,
7153
            ],
7154
            'first'
7155
        );
7156
7157
        if ($trackResult != false) {
7158
            return $trackResult['total_time'] ? $trackResult['total_time'] : '00:00:00';
7159
        }
7160
7161
        return '00:00:00';
7162
    }
7163
7164
    /**
7165
     * Get the courses list by a course coach.
7166
     *
7167
     * @param int $coachId The coach id
7168
     *
7169
     * @return array (id, user_id, session_id, c_id, visibility, status, legal_agreement)
7170
     */
7171
    public static function getCoursesListByCourseCoach($coachId)
7172
    {
7173
        $entityManager = Database::getManager();
7174
        $scuRepo = $entityManager->getRepository(
7175
            'ChamiloCoreBundle:SessionRelCourseRelUser'
7176
        );
7177
7178
        return $scuRepo->findBy([
7179
            'user' => $coachId,
7180
            'status' => SessionRelCourseRelUser::STATUS_COURSE_COACH,
7181
        ]);
7182
    }
7183
7184
    /**
7185
     * Get the count of user courses in session.
7186
     *
7187
     * @param int $sessionId
7188
     * @param int $courseId
7189
     *
7190
     * @return array
7191
     */
7192
    public static function getTotalUserCoursesInSession($sessionId, $courseId = 0)
7193
    {
7194
        $tableUser = Database::get_main_table(TABLE_MAIN_USER);
7195
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
7196
7197
        if (empty($sessionId)) {
7198
            return [];
7199
        }
7200
7201
        $courseCondition = '';
7202
        if (!empty($courseId)) {
7203
            $courseId = (int) $courseId;
7204
            $courseCondition = "  c_id = $courseId AND ";
7205
        }
7206
7207
        $sql = "SELECT 
7208
                    COUNT(u.id) as count, 
7209
                    u.id, 
7210
                    scu.status status_in_session, 
7211
                    u.status user_status
7212
                FROM $table scu
7213
                INNER JOIN $tableUser u 
7214
                ON scu.user_id = u.id
7215
                WHERE 
7216
                  $courseCondition
7217
                  scu.session_id = ".intval($sessionId)."
7218
                GROUP BY u.id";
7219
7220
        $result = Database::query($sql);
7221
7222
        $list = [];
7223
        while ($data = Database::fetch_assoc($result)) {
7224
            $list[] = $data;
7225
        }
7226
7227
        return $list;
7228
    }
7229
7230
    /**
7231
     * Returns list of a few data from session (name, short description, start
7232
     * date, end date) and the given extra fields if defined based on a
7233
     * session category Id.
7234
     *
7235
     * @param int    $categoryId  The internal ID of the session category
7236
     * @param string $target      Value to search for in the session field values
7237
     * @param array  $extraFields A list of fields to be scanned and returned
7238
     *
7239
     * @return mixed
7240
     */
7241
    public static function getShortSessionListAndExtraByCategory(
7242
        $categoryId,
7243
        $target,
7244
        $extraFields = null,
7245
        $publicationDate = null
7246
    ) {
7247
        $categoryId = (int) $categoryId;
7248
        $sessionList = [];
7249
        // Check if categoryId is valid
7250
        if ($categoryId > 0) {
7251
            $target = Database::escape_string($target);
7252
            $sTable = Database::get_main_table(TABLE_MAIN_SESSION);
7253
            $sfTable = Database::get_main_table(TABLE_EXTRA_FIELD);
7254
            $sfvTable = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
7255
            // Join session field and session field values tables
7256
            $joinTable = $sfTable.' sf INNER JOIN '.$sfvTable.' sfv ON sf.id = sfv.field_id';
7257
            $fieldsArray = [];
7258
            foreach ($extraFields as $field) {
7259
                $fieldsArray[] = Database::escape_string($field);
7260
            }
7261
            $extraFieldType = ExtraField::SESSION_FIELD_TYPE;
7262
            if (isset($publicationDate)) {
7263
                $publicationDateString = $publicationDate->format('Y-m-d H:i:s');
7264
                $wherePublication = " AND id NOT IN (
7265
                    SELECT sfv.item_id FROM $joinTable
7266
                    WHERE
7267
                        sf.extra_field_type = $extraFieldType AND
7268
                        ((sf.variable = 'publication_start_date' AND sfv.value > '$publicationDateString' and sfv.value != '') OR
7269
                        (sf.variable = 'publication_end_date' AND sfv.value < '$publicationDateString' and sfv.value != ''))
7270
                )";
7271
            }
7272
            // Get the session list from session category and target
7273
            $sessionList = Database::select(
7274
                'id, name, access_start_date, access_end_date',
7275
                $sTable,
7276
                [
7277
                    'where' => [
7278
                        "session_category_id = ? AND id IN (
7279
                            SELECT sfv.item_id FROM $joinTable
7280
                            WHERE
7281
                                sf.extra_field_type = $extraFieldType AND
7282
                                sfv.item_id = session.id AND
7283
                                sf.variable = 'target' AND
7284
                                sfv.value = ?
7285
                        ) $wherePublication" => [$categoryId, $target],
7286
                    ],
7287
                ]
7288
            );
7289
            $whereFieldVariables = [];
7290
            $whereFieldIds = [];
7291
            if (
7292
                is_array($fieldsArray) &&
7293
                count($fieldsArray) > 0
7294
            ) {
7295
                $whereParams = '?';
7296
                for ($i = 1; $i < count($fieldsArray); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
7297
                    $whereParams .= ', ?';
7298
                }
7299
                $whereFieldVariables = ' variable IN ( '.$whereParams.' )';
7300
                $whereFieldIds = 'field_id IN ( '.$whereParams.' )';
7301
            }
7302
            // Get session fields
7303
            $extraField = new ExtraFieldModel('session');
7304
            $questionMarks = substr(str_repeat('?, ', count($fieldsArray)), 0, -2);
7305
            $fieldsList = $extraField->get_all([
7306
                ' variable IN ( '.$questionMarks.' )' => $fieldsArray,
7307
            ]);
7308
            // Index session fields
7309
            foreach ($fieldsList as $field) {
7310
                $fields[$field['id']] = $field['variable'];
7311
            }
7312
            // Get session field values
7313
            $extra = new ExtraFieldValue('session');
7314
            $questionMarksFields = substr(str_repeat('?, ', count($fields)), 0, -2);
7315
            $sessionFieldValueList = $extra->get_all(['where' => ['field_id IN ( '.$questionMarksFields.' )' => array_keys($fields)]]);
7316
            // Add session fields values to session list
7317
            foreach ($sessionList as $id => &$session) {
7318
                foreach ($sessionFieldValueList as $sessionFieldValue) {
7319
                    // Match session field values to session
7320
                    if ($sessionFieldValue['item_id'] == $id) {
7321
                        // Check if session field value is set in session field list
7322
                        if (isset($fields[$sessionFieldValue['field_id']])) {
7323
                            // Avoid overwriting the session's ID field
7324
                            if ($fields[$sessionFieldValue['field_id']] != 'id') {
7325
                                $var = $fields[$sessionFieldValue['field_id']];
7326
                                $val = $sessionFieldValue['value'];
7327
                                // Assign session field value to session
7328
                                $session[$var] = $val;
7329
                            }
7330
                        }
7331
                    }
7332
                }
7333
            }
7334
        }
7335
7336
        return $sessionList;
7337
    }
7338
7339
    /**
7340
     * Return the Session Category id searched by name.
7341
     *
7342
     * @param string $categoryName Name attribute of session category used for search query
7343
     * @param bool   $force        boolean used to get even if something is wrong (e.g not unique name)
7344
     *
7345
     * @return int|array If success, return category id (int), else it will return an array
7346
     *                   with the next structure:
7347
     *                   array('error' => true, 'errorMessage' => ERROR_MESSAGE)
7348
     */
7349
    public static function getSessionCategoryIdByName($categoryName, $force = false)
7350
    {
7351
        // Start error result
7352
        $errorResult = ['error' => true, 'errorMessage' => get_lang('ThereWasAnError')];
7353
        $categoryName = Database::escape_string($categoryName);
7354
        // Check if is not empty category name
7355
        if (!empty($categoryName)) {
7356
            $sessionCategoryTable = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
7357
            // Get all session category with same name
7358
            $result = Database::select(
7359
                'id',
7360
                $sessionCategoryTable,
7361
                [
7362
                    'where' => [
7363
                        'name = ?' => $categoryName,
7364
                    ],
7365
                ]
7366
            );
7367
            // Check the result
7368
            if ($result < 1) {
7369
                // If not found any result, update error message
7370
                $errorResult['errorMessage'] = 'Not found any session category name '.$categoryName;
7371
            } elseif (count($result) > 1 && !$force) {
7372
                // If found more than one result and force is disabled, update error message
7373
                $errorResult['errorMessage'] = 'Found many session categories';
7374
            } elseif (count($result) == 1 || $force) {
7375
                // If found just one session category or force option is enabled
7376
7377
                return key($result);
7378
            }
7379
        } else {
7380
            // category name is empty, update error message
7381
            $errorResult['errorMessage'] = 'Not valid category name';
7382
        }
7383
7384
        return $errorResult;
7385
    }
7386
7387
    /**
7388
     * Return all data from sessions (plus extra field, course and coach data) by category id.
7389
     *
7390
     * @param int $sessionCategoryId session category id used to search sessions
7391
     *
7392
     * @return array If success, return session list and more session related data, else it will return an array
7393
     *               with the next structure:
7394
     *               array('error' => true, 'errorMessage' => ERROR_MESSAGE)
7395
     */
7396
    public static function getSessionListAndExtraByCategoryId($sessionCategoryId)
7397
    {
7398
        // Start error result
7399
        $errorResult = [
7400
            'error' => true,
7401
            'errorMessage' => get_lang('ThereWasAnError'),
7402
        ];
7403
7404
        $sessionCategoryId = intval($sessionCategoryId);
7405
        // Check if session category id is valid
7406
        if ($sessionCategoryId > 0) {
7407
            // Get table names
7408
            $sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
7409
            $sessionFieldTable = Database::get_main_table(TABLE_EXTRA_FIELD);
7410
            $sessionFieldValueTable = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
7411
            $sessionCourseUserTable = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
7412
            $userTable = Database::get_main_table(TABLE_MAIN_USER);
7413
            $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
7414
7415
            // Get all data from all sessions whit the session category specified
7416
            $sessionList = Database::select(
7417
                '*',
7418
                $sessionTable,
7419
                [
7420
                    'where' => [
7421
                        'session_category_id = ?' => $sessionCategoryId,
7422
                    ],
7423
                ]
7424
            );
7425
7426
            $extraFieldType = ExtraField::SESSION_FIELD_TYPE;
7427
7428
            // Check if session list query had result
7429
            if (!empty($sessionList)) {
7430
                // implode all session id
7431
                $sessionIdsString = '('.implode(', ', array_keys($sessionList)).')';
7432
                // Get all field variables
7433
                $sessionFieldList = Database::select(
7434
                    'id, variable',
7435
                    $sessionFieldTable,
7436
                    ['extra_field_type = ? ' => [$extraFieldType]]
7437
                );
7438
7439
                // Get all field values
7440
                $sql = "SELECT item_id, field_id, value FROM
7441
                        $sessionFieldValueTable v INNER JOIN $sessionFieldTable f
7442
                        ON (f.id = v.field_id)
7443
                        WHERE
7444
                            item_id IN $sessionIdsString AND
7445
                            extra_field_type = $extraFieldType
7446
                ";
7447
                $result = Database::query($sql);
7448
                $sessionFieldValueList = Database::store_result($result, 'ASSOC');
7449
7450
                // Check if session field values had result
7451
                if (!empty($sessionFieldValueList)) {
7452
                    $sessionFieldValueListBySession = [];
7453
                    foreach ($sessionFieldValueList as $key => $sessionFieldValue) {
7454
                        // Create an array to index ids to session id
7455
                        $sessionFieldValueListBySession[$sessionFieldValue['item_id']][] = $key;
7456
                    }
7457
                }
7458
                // Query used to find course-coaches from sessions
7459
                $sql = "SELECT
7460
                            scu.session_id,
7461
                            c.id AS course_id,
7462
                            c.code AS course_code,
7463
                            c.title AS course_title,
7464
                            u.username AS coach_username,
7465
                            u.firstname AS coach_firstname,
7466
                            u.lastname AS coach_lastname
7467
                        FROM $courseTable c
7468
                        INNER JOIN $sessionCourseUserTable scu ON c.id = scu.c_id
7469
                        INNER JOIN $userTable u ON scu.user_id = u.user_id
7470
                        WHERE scu.status = 2 AND scu.session_id IN $sessionIdsString
7471
                        ORDER BY scu.session_id ASC ";
7472
                $res = Database::query($sql);
7473
                $sessionCourseList = Database::store_result($res, 'ASSOC');
7474
                // Check if course list had result
7475
                if (!empty($sessionCourseList)) {
7476
                    foreach ($sessionCourseList as $key => $sessionCourse) {
7477
                        // Create an array to index ids to session_id
7478
                        $sessionCourseListBySession[$sessionCourse['session_id']][] = $key;
7479
                    }
7480
                }
7481
                // Join lists
7482
                if (is_array($sessionList)) {
7483
                    foreach ($sessionList as $id => &$row) {
7484
                        if (
7485
                            !empty($sessionFieldValueListBySession) &&
7486
                            is_array($sessionFieldValueListBySession[$id])
7487
                        ) {
7488
                            // If have an index array for session extra fields, use it to join arrays
7489
                            foreach ($sessionFieldValueListBySession[$id] as $key) {
7490
                                $row['extra'][$key] = [
7491
                                    'field_name' => $sessionFieldList[$sessionFieldValueList[$key]['field_id']]['variable'],
7492
                                    'value' => $sessionFieldValueList[$key]['value'],
7493
                                ];
7494
                            }
7495
                        }
7496
                        if (
7497
                            !empty($sessionCourseListBySession) &&
7498
                            is_array($sessionCourseListBySession[$id])
7499
                        ) {
7500
                            // If have an index array for session course coach, use it to join arrays
7501
                            foreach ($sessionCourseListBySession[$id] as $key) {
7502
                                $row['course'][$key] = [
7503
                                    'course_id' => $sessionCourseList[$key]['course_id'],
7504
                                    'course_code' => $sessionCourseList[$key]['course_code'],
7505
                                    'course_title' => $sessionCourseList[$key]['course_title'],
7506
                                    'coach_username' => $sessionCourseList[$key]['coach_username'],
7507
                                    'coach_firstname' => $sessionCourseList[$key]['coach_firstname'],
7508
                                    'coach_lastname' => $sessionCourseList[$key]['coach_lastname'],
7509
                                ];
7510
                            }
7511
                        }
7512
                    }
7513
                }
7514
7515
                return $sessionList;
7516
            } else {
7517
                // Not found result, update error message
7518
                $errorResult['errorMessage'] = 'Not found any session for session category id '.$sessionCategoryId;
7519
            }
7520
        }
7521
7522
        return $errorResult;
7523
    }
7524
7525
    /**
7526
     * Return session description from session id.
7527
     *
7528
     * @param int $sessionId
7529
     *
7530
     * @return string
7531
     */
7532
    public static function getDescriptionFromSessionId($sessionId)
7533
    {
7534
        // Init variables
7535
        $sessionId = intval($sessionId);
7536
        $description = '';
7537
        // Check if session id is valid
7538
        if ($sessionId > 0) {
7539
            // Select query from session id
7540
            $rows = Database::select(
7541
                'description',
7542
                Database::get_main_table(TABLE_MAIN_SESSION),
7543
                [
7544
                    'where' => [
7545
                        'id = ?' => $sessionId,
7546
                    ],
7547
                ]
7548
            );
7549
7550
            // Check if select query result is not empty
7551
            if (!empty($rows)) {
7552
                // Get session description
7553
                $description = $rows[0]['description'];
7554
            }
7555
        }
7556
7557
        return $description;
7558
    }
7559
7560
    /**
7561
     * Get a session list filtered by name, description or any of the given extra fields.
7562
     *
7563
     * @param string $term                 The term to search
7564
     * @param array  $extraFieldsToInclude Extra fields to include in the session data
7565
     *
7566
     * @return array The list
7567
     */
7568
    public static function searchSession($term, $extraFieldsToInclude = [])
7569
    {
7570
        $sTable = Database::get_main_table(TABLE_MAIN_SESSION);
7571
        $extraFieldTable = Database::get_main_table(TABLE_EXTRA_FIELD);
7572
        $sfvTable = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
7573
        $term = Database::escape_string($term);
7574
        $extraFieldType = ExtraField::SESSION_FIELD_TYPE;
7575
        if (is_array($extraFieldsToInclude) && count($extraFieldsToInclude) > 0) {
7576
            $resultData = Database::select('*', $sTable, [
7577
                'where' => [
7578
                    "name LIKE %?% " => $term,
7579
                    " OR description LIKE %?% " => $term,
7580
                    " OR id IN (
7581
                    SELECT item_id
7582
                    FROM $sfvTable v INNER JOIN $extraFieldTable e
7583
                    ON (v.field_id = e.id)
7584
                    WHERE value LIKE %?% AND extra_field_type = $extraFieldType
7585
                ) " => $term,
7586
                ],
7587
            ]);
7588
        } else {
7589
            $resultData = Database::select('*', $sTable, [
7590
                'where' => [
7591
                    "name LIKE %?% " => $term,
7592
                    "OR description LIKE %?% " => $term,
7593
                ],
7594
            ]);
7595
7596
            return $resultData;
7597
        }
7598
7599
        foreach ($resultData as $id => &$session) {
7600
            $session['extra'] = self::getFilteredExtraFields($id, $extraFieldsToInclude);
7601
        }
7602
7603
        return $resultData;
7604
    }
7605
7606
    /**
7607
     * @param int   $sessionId
7608
     * @param array $extraFieldsToInclude
7609
     *
7610
     * @return array
7611
     */
7612
    public static function getFilteredExtraFields($sessionId, $extraFieldsToInclude = [])
7613
    {
7614
        $extraData = [];
7615
        $variables = [];
7616
        $variablePlaceHolders = [];
7617
7618
        foreach ($extraFieldsToInclude as $sessionExtraField) {
7619
            $variablePlaceHolders[] = "?";
7620
            $variables[] = Database::escape_string($sessionExtraField);
7621
        }
7622
7623
        $sessionExtraField = new ExtraFieldModel('session');
7624
        $fieldList = $sessionExtraField->get_all([
7625
            "variable IN ( ".implode(", ", $variablePlaceHolders)." ) " => $variables,
7626
        ]);
7627
7628
        $fields = [];
7629
7630
        // Index session fields
7631
        foreach ($fieldList as $field) {
7632
            $fields[$field['id']] = $field['variable'];
7633
        }
7634
7635
        // Get session field values
7636
        $extra = new ExtraFieldValue('session');
7637
        $sessionFieldValueList = $extra->get_all(
7638
            [
7639
                "field_id IN ( ".implode(", ", $variablePlaceHolders)." )" => array_keys($fields),
7640
            ]
7641
        );
7642
7643
        foreach ($sessionFieldValueList as $sessionFieldValue) {
7644
            // Match session field values to session
7645
            if ($sessionFieldValue['item_id'] != $sessionId) {
7646
                continue;
7647
            }
7648
7649
            // Check if session field value is set in session field list
7650
            if (!isset($fields[$sessionFieldValue['field_id']])) {
7651
                continue;
7652
            }
7653
7654
            $extrafieldVariable = $fields[$sessionFieldValue['field_id']];
7655
            $extrafieldValue = $sessionFieldValue['value'];
7656
7657
            $extraData[] = [
7658
                'variable' => $extrafieldVariable,
7659
                'value' => $extrafieldValue,
7660
            ];
7661
        }
7662
7663
        return $extraData;
7664
    }
7665
7666
    /**
7667
     * @param int $sessionId
7668
     *
7669
     * @return bool
7670
     */
7671
    public static function isValidId($sessionId)
7672
    {
7673
        $sessionId = intval($sessionId);
7674
        if ($sessionId > 0) {
7675
            $rows = Database::select(
7676
                'id',
7677
                Database::get_main_table(TABLE_MAIN_SESSION),
7678
                ['where' => ['id = ?' => $sessionId]]
7679
            );
7680
            if (!empty($rows)) {
7681
                return true;
7682
            }
7683
        }
7684
7685
        return false;
7686
    }
7687
7688
    /**
7689
     * Get list of sessions based on users of a group for a group admin.
7690
     *
7691
     * @param int $userId The user id
7692
     *
7693
     * @return array
7694
     */
7695
    public static function getSessionsFollowedForGroupAdmin($userId)
7696
    {
7697
        $sessionList = [];
7698
        $sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
7699
        $sessionUserTable = Database::get_main_table(TABLE_MAIN_SESSION_USER);
7700
        $userGroup = new UserGroup();
7701
        $userIdList = $userGroup->getGroupUsersByUser($userId);
7702
7703
        if (empty($userIdList)) {
7704
            return [];
7705
        }
7706
7707
        $sql = "SELECT DISTINCT s.*
7708
                FROM $sessionTable s
7709
                INNER JOIN $sessionUserTable sru 
7710
                ON s.id = sru.id_session
7711
                WHERE
7712
                    (sru.id_user IN (".implode(', ', $userIdList).")
7713
                    AND sru.relation_type = 0
7714
                )";
7715
7716
        if (api_is_multiple_url_enabled()) {
7717
            $sessionAccessUrlTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
7718
            $accessUrlId = api_get_current_access_url_id();
7719
7720
            if ($accessUrlId != -1) {
7721
                $sql = "SELECT DISTINCT s.*
7722
                        FROM $sessionTable s
7723
                        INNER JOIN $sessionUserTable sru ON s.id = sru.id_session
7724
                        INNER JOIN $sessionAccessUrlTable srau ON s.id = srau.session_id
7725
                        WHERE
7726
                            srau.access_url_id = $accessUrlId
7727
                            AND (
7728
                                sru.id_user IN (".implode(', ', $userIdList).")
7729
                                AND sru.relation_type = 0
7730
                            )";
7731
            }
7732
        }
7733
7734
        $result = Database::query($sql);
7735
        while ($row = Database::fetch_assoc($result)) {
7736
            $sessionList[] = $row;
7737
        }
7738
7739
        return $sessionList;
7740
    }
7741
7742
    /**
7743
     * @param array $sessionInfo
7744
     *
7745
     * @return string
7746
     */
7747
    public static function getSessionVisibility($sessionInfo)
7748
    {
7749
        switch ($sessionInfo['visibility']) {
7750
            case 1:
7751
                return get_lang('ReadOnly');
7752
            case 2:
7753
               return get_lang('Visible');
7754
            case 3:
7755
                return api_ucfirst(get_lang('Invisible'));
7756
        }
7757
    }
7758
7759
    /**
7760
     * Returns a human readable string.
7761
     *
7762
     * @param array $sessionInfo An array with all the session dates
7763
     * @param bool  $showTime
7764
     *
7765
     * @return array
7766
     */
7767
    public static function parseSessionDates($sessionInfo, $showTime = false)
7768
    {
7769
        $displayDates = self::convertSessionDateToString(
7770
            $sessionInfo['display_start_date'],
7771
            $sessionInfo['display_end_date'],
7772
            $showTime,
7773
            true
7774
        );
7775
        $accessDates = self::convertSessionDateToString(
7776
            $sessionInfo['access_start_date'],
7777
            $sessionInfo['access_end_date'],
7778
            $showTime,
7779
            true
7780
        );
7781
7782
        $coachDates = self::convertSessionDateToString(
7783
            $sessionInfo['coach_access_start_date'],
7784
            $sessionInfo['coach_access_end_date'],
7785
            $showTime,
7786
            true
7787
        );
7788
7789
        $result = [
7790
            'access' => $accessDates,
7791
            'display' => $displayDates,
7792
            'coach' => $coachDates,
7793
        ];
7794
7795
        return $result;
7796
    }
7797
7798
    /**
7799
     * @param FormValidator $form
7800
     * @param array         $sessionInfo Optional
7801
     *
7802
     * @return array
7803
     */
7804
    public static function setForm(FormValidator $form, array $sessionInfo = [])
7805
    {
7806
        $sessionId = 0;
7807
        $coachInfo = [];
7808
7809
        if (!empty($sessionInfo)) {
7810
            $sessionId = (int) $sessionInfo['id'];
7811
            $coachInfo = api_get_user_info($sessionInfo['id_coach']);
7812
        }
7813
7814
        $categoriesList = self::get_all_session_category();
7815
        $userInfo = api_get_user_info();
7816
7817
        $categoriesOptions = [
7818
            '0' => get_lang('None'),
7819
        ];
7820
7821
        if ($categoriesList != false) {
7822
            foreach ($categoriesList as $categoryItem) {
7823
                $categoriesOptions[$categoryItem['id']] = $categoryItem['name'];
7824
            }
7825
        }
7826
7827
        // Database Table Definitions
7828
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
7829
7830
        $form->addText(
7831
            'name',
7832
            get_lang('SessionName'),
7833
            true,
7834
            ['maxlength' => 150, 'aria-label' => get_lang('SessionName')]
7835
        );
7836
        $form->addRule('name', get_lang('SessionNameAlreadyExists'), 'callback', 'check_session_name');
7837
7838
        if (!api_is_platform_admin() && api_is_teacher()) {
7839
            $form->addElement(
7840
                'select',
7841
                'coach_username',
7842
                get_lang('CoachName'),
7843
                [api_get_user_id() => $userInfo['complete_name']],
7844
                [
7845
                    'id' => 'coach_username',
7846
                    'style' => 'width:370px;',
7847
                ]
7848
            );
7849
        } else {
7850
            $sql = "SELECT COUNT(1) FROM $tbl_user WHERE status = 1";
7851
            $rs = Database::query($sql);
7852
            $countUsers = (int) Database::result($rs, 0, 0);
7853
7854
            if ($countUsers < 50) {
7855
                $orderClause = 'ORDER BY ';
7856
                $orderClause .= api_sort_by_first_name() ? 'firstname, lastname, username' : 'lastname, firstname, username';
7857
7858
                $sql = "SELECT user_id, lastname, firstname, username
7859
                        FROM $tbl_user
7860
                        WHERE status = '1' ".
7861
                        $orderClause;
7862
7863
                if (api_is_multiple_url_enabled()) {
7864
                    $userRelAccessUrlTable = Database::get_main_table(
7865
                        TABLE_MAIN_ACCESS_URL_REL_USER
7866
                    );
7867
                    $accessUrlId = api_get_current_access_url_id();
7868
                    if ($accessUrlId != -1) {
7869
                        $sql = "SELECT user.user_id, username, lastname, firstname
7870
                        FROM $tbl_user user
7871
                        INNER JOIN $userRelAccessUrlTable url_user
7872
                        ON (url_user.user_id = user.user_id)
7873
                        WHERE
7874
                            access_url_id = $accessUrlId AND
7875
                            status = 1 "
7876
                            .$orderClause;
7877
                    }
7878
                }
7879
7880
                $result = Database::query($sql);
7881
                $coachesList = Database::store_result($result);
7882
                $coachesOptions = [];
7883
                foreach ($coachesList as $coachItem) {
7884
                    $coachesOptions[$coachItem['user_id']] =
7885
                        api_get_person_name($coachItem['firstname'], $coachItem['lastname']).' ('.$coachItem['username'].')';
7886
                }
7887
7888
                $form->addElement(
7889
                    'select',
7890
                    'coach_username',
7891
                    get_lang('CoachName'),
7892
                    $coachesOptions,
7893
                    [
7894
                        'id' => 'coach_username',
7895
                        'style' => 'width:370px;',
7896
                    ]
7897
                );
7898
            } else {
7899
                $form->addElement(
7900
                    'select_ajax',
7901
                    'coach_username',
7902
                    get_lang('CoachName'),
7903
                    $coachInfo ? [$coachInfo['id'] => $coachInfo['complete_name_with_username']] : [],
7904
                    [
7905
                        'url' => api_get_path(WEB_AJAX_PATH).'session.ajax.php?a=search_general_coach',
7906
                        'width' => '100%',
7907
                        'id' => 'coach_username',
7908
                    ]
7909
                );
7910
            }
7911
        }
7912
7913
        $form->addRule('coach_username', get_lang('ThisFieldIsRequired'), 'required');
7914
        $form->addHtml('<div id="ajax_list_coachs"></div>');
7915
7916
        $form->addButtonAdvancedSettings('advanced_params');
7917
        $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
7918
7919
        if (empty($sessionId)) {
7920
            $sessions = SessionManager::formatSessionsAdminForGrid();
7921
            $sessionList = [];
7922
            $sessionList[] = '';
7923
            foreach ($sessions as $session) {
7924
                $sessionList[$session['id']] = strip_tags($session['name']);
7925
            }
7926
7927
            $form->addSelect(
7928
                'session_template',
7929
                get_lang('SessionTemplate'),
7930
                $sessionList,
7931
                ['id' => 'system_template']
7932
            );
7933
        }
7934
7935
        $form->addSelect(
7936
            'session_category',
7937
            get_lang('SessionCategory'),
7938
            $categoriesOptions,
7939
            [
7940
                'id' => 'session_category',
7941
            ]
7942
        );
7943
7944
        $form->addHtmlEditor(
7945
            'description',
7946
            get_lang('Description'),
7947
            false,
7948
            false,
7949
            [
7950
                'ToolbarSet' => 'Minimal',
7951
            ]
7952
        );
7953
7954
        $form->addElement('checkbox', 'show_description', null, get_lang('ShowDescription'));
7955
7956
        $visibilityGroup = [];
7957
        $visibilityGroup[] = $form->createElement(
7958
            'select',
7959
            'session_visibility',
7960
            null,
7961
            [
7962
                SESSION_VISIBLE_READ_ONLY => get_lang('SessionReadOnly'),
7963
                SESSION_VISIBLE => get_lang('SessionAccessible'),
7964
                SESSION_INVISIBLE => api_ucfirst(get_lang('SessionNotAccessible')),
7965
            ]
7966
        );
7967
        $form->addGroup(
7968
            $visibilityGroup,
7969
            'visibility_group',
7970
            get_lang('SessionVisibility'),
7971
            null,
7972
            false
7973
        );
7974
7975
        $options = [
7976
            0 => get_lang('ByDuration'),
7977
            1 => get_lang('ByDates'),
7978
        ];
7979
7980
        $form->addSelect('access', get_lang('Access'), $options, [
7981
            'onchange' => 'accessSwitcher()',
7982
            'id' => 'access',
7983
        ]);
7984
7985
        $form->addHtml('<div id="duration_div" style="display:none">');
7986
        $form->addElement(
7987
            'number',
7988
            'duration',
7989
            [
7990
                get_lang('SessionDurationTitle'),
7991
                get_lang('SessionDurationDescription'),
7992
            ],
7993
            [
7994
                'maxlength' => 50,
7995
            ]
7996
        );
7997
7998
        $form->addHtml('</div>');
7999
        $form->addHtml('<div id="date_fields" style="display:none">');
8000
8001
        // Dates
8002
        $form->addDateTimePicker(
8003
            'access_start_date',
8004
            [get_lang('SessionStartDate'), get_lang('SessionStartDateComment')],
8005
            ['id' => 'access_start_date']
8006
        );
8007
8008
        $form->addDateTimePicker(
8009
            'access_end_date',
8010
            [get_lang('SessionEndDate'), get_lang('SessionEndDateComment')],
8011
            ['id' => 'access_end_date']
8012
        );
8013
8014
        $form->addRule(
8015
            ['access_start_date', 'access_end_date'],
8016
            get_lang('StartDateMustBeBeforeTheEndDate'),
8017
            'compare_datetime_text',
8018
            '< allow_empty'
8019
        );
8020
8021
        $form->addDateTimePicker(
8022
            'display_start_date',
8023
            [
8024
                get_lang('SessionDisplayStartDate'),
8025
                get_lang('SessionDisplayStartDateComment'),
8026
            ],
8027
            ['id' => 'display_start_date']
8028
        );
8029
8030
        $form->addDateTimePicker(
8031
            'display_end_date',
8032
            [
8033
                get_lang('SessionDisplayEndDate'),
8034
                get_lang('SessionDisplayEndDateComment'),
8035
            ],
8036
            ['id' => 'display_end_date']
8037
        );
8038
8039
        $form->addRule(
8040
            ['display_start_date', 'display_end_date'],
8041
            get_lang('StartDateMustBeBeforeTheEndDate'),
8042
            'compare_datetime_text',
8043
            '< allow_empty'
8044
        );
8045
8046
        $form->addDateTimePicker(
8047
            'coach_access_start_date',
8048
            [
8049
                get_lang('SessionCoachStartDate'),
8050
                get_lang('SessionCoachStartDateComment'),
8051
            ],
8052
            ['id' => 'coach_access_start_date']
8053
        );
8054
8055
        $form->addDateTimePicker(
8056
            'coach_access_end_date',
8057
            [
8058
                get_lang('SessionCoachEndDate'),
8059
                get_lang('SessionCoachEndDateComment'),
8060
            ],
8061
            ['id' => 'coach_access_end_date']
8062
        );
8063
8064
        $form->addRule(
8065
            ['coach_access_start_date', 'coach_access_end_date'],
8066
            get_lang('StartDateMustBeBeforeTheEndDate'),
8067
            'compare_datetime_text',
8068
            '< allow_empty'
8069
        );
8070
8071
        $form->addElement('html', '</div>');
8072
8073
        $form->addCheckBox(
8074
            'send_subscription_notification',
8075
            [
8076
                get_lang('SendSubscriptionNotification'),
8077
                get_lang('SendAnEmailWhenAUserBeingSubscribed'),
8078
            ]
8079
        );
8080
8081
        // Extra fields
8082
        $extra_field = new ExtraFieldModel('session');
8083
        $extra = $extra_field->addElements($form, $sessionId);
8084
8085
        $form->addElement('html', '</div>');
8086
8087
        $js = $extra['jquery_ready_content'];
8088
8089
        return ['js' => $js];
8090
    }
8091
8092
    /**
8093
     * Gets the number of rows in the session table filtered through the given
8094
     * array of parameters.
8095
     *
8096
     * @param array Array of options/filters/keys
8097
     *
8098
     * @return int The number of rows, or false on wrong param
8099
     * @assert ('a') === false
8100
     */
8101
    public static function get_count_admin_complete($options = [])
8102
    {
8103
        if (!is_array($options)) {
8104
            return false;
8105
        }
8106
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
8107
        $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
8108
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
8109
        $sessionCourseUserTable = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
8110
        $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
8111
        $tbl_session_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
8112
        $tbl_session_field_options = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
8113
8114
        $where = 'WHERE 1 = 1 ';
8115
        $user_id = api_get_user_id();
8116
8117
        if (api_is_session_admin() &&
8118
            api_get_setting('allow_session_admins_to_see_all_sessions') == 'false'
8119
        ) {
8120
            $where .= " WHERE s.session_admin_id = $user_id ";
8121
        }
8122
8123
        $extraFieldTables = '';
8124
        if (!empty($options['where'])) {
8125
            $options['where'] = str_replace('course_title', 'c.title', $options['where']);
8126
            $options['where'] = str_replace("( session_active = '0' )", '1=1', $options['where']);
8127
8128
            $options['where'] = str_replace(
8129
                ["AND session_active = '1'  )", " AND (  session_active = '1'  )"],
8130
                [') GROUP BY s.name HAVING session_active = 1 ', " GROUP BY s.name HAVING session_active = 1 "],
8131
                $options['where']
8132
            );
8133
8134
            $options['where'] = str_replace(
8135
                ["AND session_active = '0'  )", " AND (  session_active = '0'  )"],
8136
                [') GROUP BY s.name HAVING session_active = 0 ', " GROUP BY s.name HAVING session_active = '0' "],
8137
                $options['where']
8138
            );
8139
8140
            if (!empty($options['extra'])) {
8141
                $options['where'] = str_replace(' 1 = 1  AND', '', $options['where']);
8142
                $options['where'] = str_replace('AND', 'OR', $options['where']);
8143
8144
                foreach ($options['extra'] as $extra) {
8145
                    $options['where'] = str_replace(
8146
                        $extra['field'],
8147
                        'fv.field_id = '.$extra['id'].' AND fvo.option_value',
8148
                        $options['where']
8149
                    );
8150
                    $extraFieldTables = "$tbl_session_field_values fv, $tbl_session_field_options fvo, ";
8151
                }
8152
            }
8153
            $where .= ' AND '.$options['where'];
8154
        }
8155
8156
        $today = api_get_utc_datetime();
8157
        $query_rows = "SELECT count(*) as total_rows, c.title as course_title, s.name,
8158
                        IF (
8159
                            (s.access_start_date <= '$today' AND '$today' < s.access_end_date) OR
8160
                            (s.access_start_date = '0000-00-00 00:00:00' AND s.access_end_date = '0000-00-00 00:00:00' ) OR
8161
                            (s.access_start_date IS NULL AND s.access_end_date IS NULL) OR
8162
                            (s.access_start_date <= '$today' AND ('0000-00-00 00:00:00' = s.access_end_date OR s.access_end_date IS NULL )) OR
8163
                            ('$today' < s.access_end_date AND ('0000-00-00 00:00:00' = s.access_start_date OR s.access_start_date IS NULL) )
8164
                        , 1, 0) as session_active
8165
                       FROM $extraFieldTables $tbl_session s
8166
                       LEFT JOIN  $tbl_session_category sc
8167
                       ON s.session_category_id = sc.id
8168
                       INNER JOIN $tbl_user u
8169
                       ON s.id_coach = u.user_id
8170
                       INNER JOIN $sessionCourseUserTable scu
8171
                       ON s.id = scu.session_id
8172
                       INNER JOIN $courseTable c
8173
                       ON c.id = scu.c_id
8174
                       $where ";
8175
8176
        if (api_is_multiple_url_enabled()) {
8177
            $table_access_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
8178
            $access_url_id = api_get_current_access_url_id();
8179
            if ($access_url_id != -1) {
8180
                $where .= " AND ar.access_url_id = $access_url_id ";
8181
                $query_rows = "SELECT count(*) as total_rows
8182
                               FROM $tbl_session s
8183
                               LEFT JOIN  $tbl_session_category sc
8184
                               ON s.session_category_id = sc.id
8185
                               INNER JOIN $tbl_user u
8186
                               ON s.id_coach = u.user_id
8187
                               INNER JOIN $table_access_url_rel_session ar
8188
                               ON ar.session_id = s.id $where ";
8189
            }
8190
        }
8191
8192
        $result = Database::query($query_rows);
8193
        $num = 0;
8194
        if (Database::num_rows($result)) {
8195
            $rows = Database::fetch_array($result);
8196
            $num = $rows['total_rows'];
8197
        }
8198
8199
        return $num;
8200
    }
8201
8202
    /**
8203
     * @param string $list_type
8204
     * @param array  $extraFields
8205
     *
8206
     * @return array
8207
     */
8208
    public static function getGridColumns(
8209
        $list_type = 'simple',
8210
        $extraFields = []
8211
    ) {
8212
        $showCount = api_get_configuration_value('session_list_show_count_users');
8213
        // Column config
8214
        $operators = ['cn', 'nc'];
8215
        $date_operators = ['gt', 'ge', 'lt', 'le'];
8216
8217
        switch ($list_type) {
8218
            case 'simple':
8219
                $columns = [
8220
                    '#',
8221
                    get_lang('Name'),
8222
                    get_lang('Category'),
8223
                    get_lang('SessionDisplayStartDate'),
8224
                    get_lang('SessionDisplayEndDate'),
8225
                    //get_lang('Coach'),
8226
                    //get_lang('Status'),
8227
                    //get_lang('CourseTitle'),
8228
                    get_lang('Visibility'),
8229
                ];
8230
8231
                $column_model = [
8232
                    [
8233
                        'name' => 'id',
8234
                        'index' => 's.id',
8235
                        'width' => '160',
8236
                        'hidden' => 'true',
8237
                    ],
8238
                    [
8239
                        'name' => 'name',
8240
                        'index' => 's.name',
8241
                        'width' => '160',
8242
                        'align' => 'left',
8243
                        'search' => 'true',
8244
                        'searchoptions' => ['sopt' => $operators],
8245
                    ],
8246
                    [
8247
                        'name' => 'category_name',
8248
                        'index' => 'category_name',
8249
                        'width' => '40',
8250
                        'align' => 'left',
8251
                        'search' => 'true',
8252
                        'searchoptions' => ['sopt' => $operators],
8253
                    ],
8254
                    [
8255
                        'name' => 'display_start_date',
8256
                        'index' => 'display_start_date',
8257
                        'width' => '50',
8258
                        'align' => 'left',
8259
                        'search' => 'true',
8260
                        'searchoptions' => [
8261
                            'dataInit' => 'date_pick_today',
8262
                            'sopt' => $date_operators,
8263
                        ],
8264
                    ],
8265
                    [
8266
                        'name' => 'display_end_date',
8267
                        'index' => 'display_end_date',
8268
                        'width' => '50',
8269
                        'align' => 'left',
8270
                        'search' => 'true',
8271
                        'searchoptions' => [
8272
                            'dataInit' => 'date_pick_one_month',
8273
                            'sopt' => $date_operators,
8274
                        ],
8275
                    ],
8276
                    [
8277
                        'name' => 'visibility',
8278
                        'index' => 'visibility',
8279
                        'width' => '40',
8280
                        'align' => 'left',
8281
                        'search' => 'false',
8282
                    ],
8283
                ];
8284
8285
                if ($showCount) {
8286
                    $columns[] = get_lang('Users');
8287
                    $column_model[] = [
8288
                        'name' => 'users',
8289
                        'index' => 'users',
8290
                        'width' => '20',
8291
                        'align' => 'left',
8292
                        'search' => 'false',
8293
                    ];
8294
                }
8295
                break;
8296
            case 'complete':
8297
                $columns = [
8298
                    get_lang('Name'),
8299
                    get_lang('SessionDisplayStartDate'),
8300
                    get_lang('SessionDisplayEndDate'),
8301
                    get_lang('Coach'),
8302
                    get_lang('Status'),
8303
                    get_lang('Visibility'),
8304
                    get_lang('CourseTitle'),
8305
                ];
8306
                $column_model = [
8307
                    ['name' => 'name', 'index' => 's.name', 'width' => '200', 'align' => 'left', 'search' => 'true', 'searchoptions' => ['sopt' => $operators]],
8308
                    ['name' => 'display_start_date', 'index' => 'display_start_date', 'width' => '70', 'align' => 'left', 'search' => 'true', 'searchoptions' => ['dataInit' => 'date_pick_today', 'sopt' => $date_operators]],
8309
                    ['name' => 'display_end_date', 'index' => 'display_end_date', 'width' => '70', 'align' => 'left', 'search' => 'true', 'searchoptions' => ['dataInit' => 'date_pick_one_month', 'sopt' => $date_operators]],
8310
                    ['name' => 'coach_name', 'index' => 'coach_name', 'width' => '70', 'align' => 'left', 'search' => 'false', 'searchoptions' => ['sopt' => $operators]],
8311
                    ['name' => 'session_active', 'index' => 'session_active', 'width' => '25', 'align' => 'left', 'search' => 'true', 'stype' => 'select',
8312
                        // for the bottom bar
8313
                        'searchoptions' => [
8314
                            'defaultValue' => '1',
8315
                            'value' => '1:'.get_lang('Active').';0:'.get_lang('Inactive'), ],
8316
                        // for the top bar
8317
                        'editoptions' => ['value' => '" ":'.get_lang('All').';1:'.get_lang('Active').';0:'.get_lang('Inactive')],
8318
                    ],
8319
                    ['name' => 'visibility', 'index' => 'visibility', 'width' => '40', 'align' => 'left', 'search' => 'false'],
8320
                    ['name' => 'course_title', 'index' => 'course_title', 'width' => '50', 'hidden' => 'true', 'search' => 'true', 'searchoptions' => ['searchhidden' => 'true', 'sopt' => $operators]],
8321
                ];
8322
                break;
8323
        }
8324
8325
        if (!empty($extraFields)) {
8326
            foreach ($extraFields as $field) {
8327
                $columns[] = $field['display_text'];
8328
                $column_model[] = [
8329
                    'name' => $field['variable'],
8330
                    'index' => $field['variable'],
8331
                    'width' => '80',
8332
                    'align' => 'center',
8333
                    'search' => 'false',
8334
                ];
8335
            }
8336
        }
8337
8338
        // Inject extra session fields
8339
        $session_field = new ExtraFieldModel('session');
8340
        $rules = $session_field->getRules($columns, $column_model);
8341
        $column_model[] = [
8342
            'name' => 'actions',
8343
            'index' => 'actions',
8344
            'width' => '80',
8345
            'align' => 'left',
8346
            'formatter' => 'action_formatter',
8347
            'sortable' => 'false',
8348
            'search' => 'false',
8349
        ];
8350
        $columns[] = get_lang('Actions');
8351
8352
        foreach ($column_model as $col_model) {
8353
            $simple_column_name[] = $col_model['name'];
8354
        }
8355
8356
        $return_array = [
8357
            'columns' => $columns,
8358
            'column_model' => $column_model,
8359
            'rules' => $rules,
8360
            'simple_column_name' => $simple_column_name,
8361
        ];
8362
8363
        return $return_array;
8364
    }
8365
8366
    /**
8367
     * Converts all dates sent through the param array (given form) to correct dates with timezones.
8368
     *
8369
     * @param array The dates The same array, with times converted
8370
     * @param bool $applyFormat Whether apply the DATE_TIME_FORMAT_SHORT format for sessions
8371
     *
8372
     * @return array The same array, with times converted
8373
     */
8374
    public static function convert_dates_to_local($params, $applyFormat = false)
8375
    {
8376
        if (!is_array($params)) {
8377
            return false;
8378
        }
8379
        $params['display_start_date'] = api_get_local_time($params['display_start_date'], null, null, true);
8380
        $params['display_end_date'] = api_get_local_time($params['display_end_date'], null, null, true);
8381
8382
        $params['access_start_date'] = api_get_local_time($params['access_start_date'], null, null, true);
8383
        $params['access_end_date'] = api_get_local_time($params['access_end_date'], null, null, true);
8384
8385
        $params['coach_access_start_date'] = isset($params['coach_access_start_date']) ? api_get_local_time($params['coach_access_start_date'], null, null, true) : null;
8386
        $params['coach_access_end_date'] = isset($params['coach_access_end_date']) ? api_get_local_time($params['coach_access_end_date'], null, null, true) : null;
8387
8388
        if ($applyFormat) {
8389
            if (isset($params['display_start_date'])) {
8390
                $params['display_start_date'] = api_format_date($params['display_start_date'], DATE_TIME_FORMAT_SHORT);
8391
            }
8392
8393
            if (isset($params['display_end_date'])) {
8394
                $params['display_end_date'] = api_format_date($params['display_end_date'], DATE_TIME_FORMAT_SHORT);
8395
            }
8396
8397
            if (isset($params['access_start_date'])) {
8398
                $params[''] = api_format_date($params['access_start_date'], DATE_TIME_FORMAT_SHORT);
8399
            }
8400
8401
            if (isset($params['access_end_date'])) {
8402
                $params['access_end_date'] = api_format_date($params['access_end_date'], DATE_TIME_FORMAT_SHORT);
8403
            }
8404
8405
            if (isset($params['coach_access_start_date'])) {
8406
                $params['coach_access_start_date'] = api_format_date($params['coach_access_start_date'], DATE_TIME_FORMAT_SHORT);
8407
            }
8408
8409
            if (isset($params['coach_access_end_date'])) {
8410
                $params['coach_access_end_date'] = api_format_date($params['coach_access_end_date'], DATE_TIME_FORMAT_SHORT);
8411
            }
8412
        }
8413
8414
        return $params;
8415
    }
8416
8417
    /**
8418
     * Gets the admin session list callback of the session/session_list.php
8419
     * page with all user/details in the right fomat.
8420
     *
8421
     * @param array $options
8422
     *
8423
     * @return array Array of rows results
8424
     * @asset ('a') === false
8425
     */
8426
    public static function get_sessions_admin_complete($options = [])
8427
    {
8428
        if (!is_array($options)) {
8429
            return false;
8430
        }
8431
8432
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
8433
        $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
8434
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
8435
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
8436
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
8437
8438
        $extraFieldTable = Database::get_main_table(TABLE_EXTRA_FIELD);
8439
        $tbl_session_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
8440
        $tbl_session_field_options = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
8441
8442
        $where = 'WHERE 1 = 1 ';
8443
        $user_id = api_get_user_id();
8444
8445
        if (!api_is_platform_admin()) {
8446
            if (api_is_session_admin() &&
8447
                api_get_setting('allow_session_admins_to_manage_all_sessions') == 'false'
8448
            ) {
8449
                $where .= " AND s.session_admin_id = $user_id ";
8450
            }
8451
        }
8452
8453
        $coach_name = " CONCAT(u.lastname , ' ', u.firstname) as coach_name ";
8454
        if (api_is_western_name_order()) {
8455
            $coach_name = " CONCAT(u.firstname, ' ', u.lastname) as coach_name ";
8456
        }
8457
8458
        $today = api_get_utc_datetime();
8459
        $inject_extra_fields = null;
8460
        $extra_fields = [];
8461
        $extra_fields_info = [];
8462
8463
        //for now only sessions
8464
        $extra_field = new ExtraFieldModel('session');
8465
        $double_fields = [];
8466
        $extra_field_option = new ExtraFieldOption('session');
8467
8468
        if (isset($options['extra'])) {
8469
            $extra_fields = $options['extra'];
8470
            if (!empty($extra_fields)) {
8471
                foreach ($extra_fields as $extra) {
8472
                    $inject_extra_fields .= " IF (fv.field_id = {$extra['id']}, fvo.option_display_text, NULL ) as {$extra['field']} , ";
8473
                    if (isset($extra_fields_info[$extra['id']])) {
8474
                        $info = $extra_fields_info[$extra['id']];
8475
                    } else {
8476
                        $info = $extra_field->get($extra['id']);
8477
                        $extra_fields_info[$extra['id']] = $info;
8478
                    }
8479
8480
                    if ($info['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
8481
                        $double_fields[$info['id']] = $info;
8482
                    }
8483
                }
8484
            }
8485
        }
8486
8487
        $options_by_double = [];
8488
        foreach ($double_fields as $double) {
8489
            $my_options = $extra_field_option->get_field_options_by_field(
8490
                $double['id'],
8491
                true
8492
            );
8493
            $options_by_double['extra_'.$double['field_variable']] = $my_options;
8494
        }
8495
8496
        //sc.name as category_name,
8497
        $select = "
8498
                SELECT * FROM (
8499
                    SELECT DISTINCT
8500
                        IF (
8501
                            (s.access_start_date <= '$today' AND '$today' < s.access_end_date) OR
8502
                            (s.access_start_date = '0000-00-00 00:00:00' AND s.access_end_date = '0000-00-00 00:00:00' ) OR
8503
                            (s.access_start_date IS NULL AND s.access_end_date IS NULL) OR
8504
                            (s.access_start_date <= '$today' AND ('0000-00-00 00:00:00' = s.access_end_date OR s.access_end_date IS NULL )) OR
8505
                            ('$today' < s.access_end_date AND ('0000-00-00 00:00:00' = s.access_start_date OR s.access_start_date IS NULL) )
8506
                        , 1, 0) as session_active,
8507
                s.name,
8508
                s.nbr_courses,
8509
                s.nbr_users,
8510
                s.display_start_date,
8511
                s.display_end_date,
8512
                $coach_name,
8513
                access_start_date,
8514
                access_end_date,
8515
                s.visibility,
8516
                u.user_id,
8517
                $inject_extra_fields
8518
                c.title as course_title,
8519
                s.id ";
8520
8521
        if (!empty($options['where'])) {
8522
            if (!empty($options['extra'])) {
8523
                $options['where'] = str_replace(' 1 = 1  AND', '', $options['where']);
8524
                $options['where'] = str_replace('AND', 'OR', $options['where']);
8525
                foreach ($options['extra'] as $extra) {
8526
                    $options['where'] = str_replace($extra['field'], 'fv.field_id = '.$extra['id'].' AND fvo.option_value', $options['where']);
8527
                }
8528
            }
8529
            $options['where'] = str_replace('course_title', 'c.title', $options['where']);
8530
            $options['where'] = str_replace("( session_active = '0' )", '1=1', $options['where']);
8531
            $options['where'] = str_replace(
8532
                ["AND session_active = '1'  )", " AND (  session_active = '1'  )"],
8533
                [') GROUP BY s.name HAVING session_active = 1 ', " GROUP BY s.name HAVING session_active = 1 "],
8534
                $options['where']
8535
            );
8536
8537
            $options['where'] = str_replace(
8538
                ["AND session_active = '0'  )", " AND (  session_active = '0'  )"],
8539
                [') GROUP BY s.name HAVING session_active = 0 ', " GROUP BY s.name HAVING session_active = '0' "],
8540
                $options['where']
8541
            );
8542
8543
            $where .= ' AND '.$options['where'];
8544
        }
8545
8546
        $limit = '';
8547
        if (!empty($options['limit'])) {
8548
            $limit = " LIMIT ".$options['limit'];
8549
        }
8550
8551
        $query = "$select FROM $tbl_session s
8552
                    LEFT JOIN $tbl_session_field_values fv
8553
                    ON (fv.item_id = s.id)
8554
                    LEFT JOIN $extraFieldTable f
8555
                    ON f.id = fv.field_id
8556
                    LEFT JOIN $tbl_session_field_options fvo
8557
                    ON (fv.field_id = fvo.field_id)
8558
                    LEFT JOIN $tbl_session_rel_course src
8559
                    ON (src.session_id = s.id)
8560
                    LEFT JOIN $tbl_course c
8561
                    ON (src.c_id = c.id)
8562
                    LEFT JOIN $tbl_session_category sc
8563
                    ON (s.session_category_id = sc.id)
8564
                    INNER JOIN $tbl_user u
8565
                    ON (s.id_coach = u.user_id) 
8566
                    $where
8567
                    $limit
8568
        ";
8569
8570
        if (api_is_multiple_url_enabled()) {
8571
            $table_access_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
8572
            $access_url_id = api_get_current_access_url_id();
8573
            if ($access_url_id != -1) {
8574
                $query = "$select
8575
                    FROM $tbl_session s
8576
                    LEFT JOIN $tbl_session_field_values fv 
8577
                    ON (fv.item_id = s.id)
8578
                    LEFT JOIN $tbl_session_field_options fvo 
8579
                    ON (fv.field_id = fvo.field_id)
8580
                    LEFT JOIN $tbl_session_rel_course src 
8581
                    ON (src.session_id = s.id)
8582
                    LEFT JOIN $tbl_course c 
8583
                    ON (src.c_id = c.id)
8584
                    LEFT JOIN $tbl_session_category sc 
8585
                    ON (s.session_category_id = sc.id)
8586
                    INNER JOIN $tbl_user u 
8587
                    ON (s.id_coach = u.user_id)
8588
                    INNER JOIN $table_access_url_rel_session ar 
8589
                    ON (ar.session_id = s.id AND ar.access_url_id = $access_url_id)
8590
                    $where
8591
                    $limit
8592
                ";
8593
            }
8594
        }
8595
8596
        $query .= ") AS session_table";
8597
8598
        if (!empty($options['order'])) {
8599
            $query .= " ORDER BY ".$options['order'];
8600
        }
8601
8602
        $result = Database::query($query);
8603
8604
        $acceptIcon = Display::return_icon(
8605
            'accept.png',
8606
            get_lang('Active'),
8607
            [],
8608
            ICON_SIZE_SMALL
8609
        );
8610
8611
        $errorIcon = Display::return_icon(
8612
            'error.png',
8613
            get_lang('Inactive'),
8614
            [],
8615
            ICON_SIZE_SMALL
8616
        );
8617
8618
        $formatted_sessions = [];
8619
        if (Database::num_rows($result)) {
8620
            $sessions = Database::store_result($result, 'ASSOC');
8621
            foreach ($sessions as $session) {
8622
                $session_id = $session['id'];
8623
                $session['name'] = Display::url($session['name'], "resume_session.php?id_session=".$session['id']);
8624
                $session['coach_name'] = Display::url($session['coach_name'], "user_information.php?user_id=".$session['user_id']);
8625
                if ($session['session_active'] == 1) {
8626
                    $session['session_active'] = $acceptIcon;
8627
                } else {
8628
                    $session['session_active'] = $errorIcon;
8629
                }
8630
8631
                $session = self::convert_dates_to_local($session);
8632
8633
                switch ($session['visibility']) {
8634
                    case SESSION_VISIBLE_READ_ONLY: //1
8635
                        $session['visibility'] = get_lang('ReadOnly');
8636
                        break;
8637
                    case SESSION_VISIBLE:           //2
8638
                    case SESSION_AVAILABLE:         //4
8639
                        $session['visibility'] = get_lang('Visible');
8640
                        break;
8641
                    case SESSION_INVISIBLE:         //3
8642
                        $session['visibility'] = api_ucfirst(get_lang('Invisible'));
8643
                        break;
8644
                }
8645
8646
                // Cleaning double selects
8647
                foreach ($session as $key => &$value) {
8648
                    if (isset($options_by_double[$key]) || isset($options_by_double[$key.'_second'])) {
8649
                        $options = explode('::', $value);
8650
                    }
8651
                    $original_key = $key;
8652
8653
                    if (strpos($key, '_second') === false) {
8654
                    } else {
8655
                        $key = str_replace('_second', '', $key);
8656
                    }
8657
8658
                    if (isset($options_by_double[$key])) {
8659
                        if (isset($options[0])) {
8660
                            if (isset($options_by_double[$key][$options[0]])) {
8661
                                if (strpos($original_key, '_second') === false) {
8662
                                    $value = $options_by_double[$key][$options[0]]['option_display_text'];
8663
                                } else {
8664
                                    $value = $options_by_double[$key][$options[1]]['option_display_text'];
8665
                                }
8666
                            }
8667
                        }
8668
                    }
8669
                }
8670
8671
                // Magic filter
8672
                if (isset($formatted_sessions[$session_id])) {
8673
                    $formatted_sessions[$session_id] = self::compareArraysToMerge(
8674
                        $formatted_sessions[$session_id],
8675
                        $session
8676
                    );
8677
                } else {
8678
                    $formatted_sessions[$session_id] = $session;
8679
                }
8680
            }
8681
        }
8682
8683
        return $formatted_sessions;
8684
    }
8685
8686
    /**
8687
     * Compare two arrays.
8688
     *
8689
     * @param array $array1
8690
     * @param array $array2
8691
     *
8692
     * @return array
8693
     */
8694
    public static function compareArraysToMerge($array1, $array2)
8695
    {
8696
        if (empty($array2)) {
8697
            return $array1;
8698
        }
8699
        foreach ($array1 as $key => $item) {
8700
            if (!isset($array1[$key])) {
8701
                //My string is empty try the other one
8702
                if (isset($array2[$key]) && !empty($array2[$key])) {
8703
                    $array1[$key] = $array2[$key];
8704
                }
8705
            }
8706
        }
8707
8708
        return $array1;
8709
    }
8710
8711
    /**
8712
     * Get link to the admin page for this session.
8713
     *
8714
     * @param int $id Session ID
8715
     *
8716
     * @return mixed URL to the admin page to manage the session, or false on error
8717
     */
8718
    public static function getAdminPath($id)
8719
    {
8720
        $id = (int) $id;
8721
        $session = self::fetch($id);
8722
        if (empty($session)) {
8723
            return false;
8724
        }
8725
8726
        return api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session='.$id;
8727
    }
8728
8729
    /**
8730
     * Get link to the user page for this session.
8731
     * If a course is provided, build the link to the course.
8732
     *
8733
     * @param int $id       Session ID
8734
     * @param int $courseId Course ID (optional) in case the link has to send straight to the course
8735
     *
8736
     * @return mixed URL to the page to use the session, or false on error
8737
     */
8738
    public static function getPath($id, $courseId = 0)
8739
    {
8740
        $id = (int) $id;
8741
        $session = self::fetch($id);
8742
        if (empty($session)) {
8743
            return false;
8744
        }
8745
        if (empty($courseId)) {
8746
            return api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$id;
8747
        } else {
8748
            $courseInfo = api_get_course_info_by_id($courseId);
8749
            if ($courseInfo) {
8750
                return $courseInfo['course_public_url'].'?id_session='.$id;
8751
            }
8752
        }
8753
8754
        return false;
8755
    }
8756
8757
    /**
8758
     * Return an associative array 'id_course' => [id_session1, id_session2...]
8759
     * where course id_course is in sessions id_session1, id_session2
8760
     * for course where user is coach
8761
     * i.e. coach for the course or
8762
     * main coach for a session the course is in
8763
     * for a session category (or woth no session category if empty).
8764
     *
8765
     * @param int $userId
8766
     *
8767
     * @return array
8768
     */
8769
    public static function getSessionCourseForUser($userId)
8770
    {
8771
        // list of COURSES where user is COURSE session coach
8772
        $listCourseCourseCoachSession = self::getCoursesForCourseSessionCoach($userId);
8773
        // list of courses where user is MAIN session coach
8774
        $listCourseMainCoachSession = self::getCoursesForMainSessionCoach($userId);
8775
        // merge these 2 array
8776
        $listResCourseSession = $listCourseCourseCoachSession;
8777
        foreach ($listCourseMainCoachSession as $courseId2 => $listSessionId2) {
8778
            if (isset($listResCourseSession[$courseId2])) {
8779
                // if sessionId array exists for this course
8780
                // same courseId, merge the list of session
8781
                foreach ($listCourseMainCoachSession[$courseId2] as $i => $sessionId2) {
8782
                    if (!in_array($sessionId2, $listResCourseSession[$courseId2])) {
8783
                        $listResCourseSession[$courseId2][] = $sessionId2;
8784
                    }
8785
                }
8786
            } else {
8787
                $listResCourseSession[$courseId2] = $listSessionId2;
8788
            }
8789
        }
8790
8791
        return $listResCourseSession;
8792
    }
8793
8794
    /**
8795
     * Return an associative array 'id_course' => [id_session1, id_session2...]
8796
     * where course id_course is in sessions id_session1, id_session2.
8797
     *
8798
     * @param int $userId
8799
     *
8800
     * @return array
8801
     */
8802
    public static function getCoursesForCourseSessionCoach($userId)
8803
    {
8804
        $userId = (int) $userId;
8805
        $listResCourseSession = [];
8806
        $tblCourse = Database::get_main_table(TABLE_MAIN_COURSE);
8807
        $tblSessionRelCourseRelUser = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
8808
8809
        $sql = "SELECT session_id, c_id, c.id
8810
                FROM $tblSessionRelCourseRelUser srcru
8811
                LEFT JOIN $tblCourse c
8812
                ON c.id = srcru.c_id
8813
                WHERE
8814
                    srcru.user_id = $userId AND
8815
                    srcru.status = 2";
8816
8817
        $res = Database::query($sql);
8818
8819
        while ($data = Database::fetch_assoc($res)) {
8820
            if (api_get_session_visibility($data['session_id'])) {
8821
                if (!isset($listResCourseSession[$data['id']])) {
8822
                    $listResCourseSession[$data['id']] = [];
8823
                }
8824
                $listResCourseSession[$data['id']][] = $data['session_id'];
8825
            }
8826
        }
8827
8828
        return $listResCourseSession;
8829
    }
8830
8831
    /**
8832
     * Return an associative array 'id_course' => [id_session1, id_session2...]
8833
     * where course id_course is in sessions id_session1, id_session2.
8834
     *
8835
     * @param $userId
8836
     *
8837
     * @return array
8838
     */
8839
    public static function getCoursesForMainSessionCoach($userId)
8840
    {
8841
        $userId = (int) $userId;
8842
        $listResCourseSession = [];
8843
        $tblSession = Database::get_main_table(TABLE_MAIN_SESSION);
8844
8845
        // list of SESSION where user is session coach
8846
        $sql = "SELECT id FROM $tblSession
8847
                WHERE id_coach = ".$userId;
8848
        $res = Database::query($sql);
8849
8850
        while ($data = Database::fetch_assoc($res)) {
8851
            $sessionId = $data['id'];
8852
            $listCoursesInSession = self::getCoursesInSession($sessionId);
8853
            foreach ($listCoursesInSession as $i => $courseId) {
8854
                if (api_get_session_visibility($sessionId)) {
8855
                    if (!isset($listResCourseSession[$courseId])) {
8856
                        $listResCourseSession[$courseId] = [];
8857
                    }
8858
                    $listResCourseSession[$courseId][] = $sessionId;
8859
                }
8860
            }
8861
        }
8862
8863
        return $listResCourseSession;
8864
    }
8865
8866
    /**
8867
     * Return an array of course_id used in session $sessionId.
8868
     *
8869
     * @param $sessionId
8870
     *
8871
     * @return array
8872
     */
8873
    public static function getCoursesInSession($sessionId)
8874
    {
8875
        if (empty($sessionId)) {
8876
            return [];
8877
        }
8878
8879
        $tblSessionRelCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
8880
        $tblCourse = Database::get_main_table(TABLE_MAIN_COURSE);
8881
8882
        // list of course in this session
8883
        $sql = "SELECT session_id, c.id
8884
                FROM $tblSessionRelCourse src
8885
                LEFT JOIN $tblCourse c
8886
                ON c.id = src.c_id
8887
                WHERE session_id = ".intval($sessionId);
8888
        $res = Database::query($sql);
8889
8890
        $listResultsCourseId = [];
8891
        while ($data = Database::fetch_assoc($res)) {
8892
            $listResultsCourseId[] = $data['id'];
8893
        }
8894
8895
        return $listResultsCourseId;
8896
    }
8897
8898
    /**
8899
     * Return an array of courses in session for user
8900
     * and for each courses the list of session that use this course for user.
8901
     *
8902
     * [0] => array
8903
     *      userCatId
8904
     *      userCatTitle
8905
     *      courseInUserCatList
8906
     *          [0] => array
8907
     *              courseId
8908
     *              title
8909
     *              courseCode
8910
     *              sessionCatList
8911
     *                  [0] => array
8912
     *                      catSessionId
8913
     *                      catSessionName
8914
     *                      sessionList
8915
     *                          [0] => array
8916
     *                              sessionId
8917
     *                              sessionName
8918
     *
8919
     * @param int $userId
8920
     *
8921
     * @return array
8922
     */
8923
    public static function getNamedSessionCourseForCoach($userId)
8924
    {
8925
        $listResults = [];
8926
        $listCourseSession = self::getSessionCourseForUser($userId);
8927
        foreach ($listCourseSession as $courseId => $listSessionId) {
8928
            // Course info
8929
            $courseInfo = api_get_course_info_by_id($courseId);
8930
            $listOneCourse = [];
8931
            $listOneCourse['courseId'] = $courseId;
8932
            $listOneCourse['title'] = $courseInfo['title'];
8933
            //$listOneCourse['courseCode'] = $courseInfo['code'];
8934
            $listOneCourse['course'] = $courseInfo;
8935
            $listOneCourse['sessionCatList'] = [];
8936
            $listCat = [];
8937
            foreach ($listSessionId as $i => $sessionId) {
8938
                // here we got all session for this course
8939
                // lets check there session categories
8940
                $sessionInfo = self::fetch($sessionId);
8941
                $catId = $sessionInfo['session_category_id'];
8942
                if (!isset($listCat[$catId])) {
8943
                    $listCatInfo = self::get_session_category($catId);
8944
                    $listCat[$catId] = [];
8945
                    $listCat[$catId]['catSessionId'] = $catId;
8946
                    $listCat[$catId]['catSessionName'] = $listCatInfo['name'];
8947
                    $listCat[$catId]['sessionList'] = [];
8948
                }
8949
                $listSessionInfo = self::fetch($sessionId);
8950
                $listSessionIdName = [
8951
                    'sessionId' => $sessionId,
8952
                    'sessionName' => $listSessionInfo['name'],
8953
                ];
8954
                $listCat[$catId]['sessionList'][] = $listSessionIdName;
8955
            }
8956
            // sort $listCat by catSessionName
8957
            usort($listCat, 'self::compareBySessionName');
8958
            // in each catSession sort sessionList by sessionName
8959
            foreach ($listCat as $i => $listCatSessionInfo) {
8960
                $listSessionList = $listCatSessionInfo['sessionList'];
8961
                usort($listSessionList, 'self::compareCatSessionInfo');
8962
                $listCat[$i]['sessionList'] = $listSessionList;
8963
            }
8964
8965
            $listOneCourse['sessionCatList'] = $listCat;
8966
8967
            // user course category
8968
            $courseCategory = CourseManager::getUserCourseCategoryForCourse(
8969
                $userId,
8970
                $courseId
8971
            );
8972
8973
            $userCatTitle = '';
8974
            $userCatId = 0;
8975
            if ($courseCategory) {
8976
                $userCatId = $courseCategory['user_course_cat'];
8977
                $userCatTitle = $courseCategory['title'];
8978
            }
8979
8980
            $listResults[$userCatId]['courseInUserCategoryId'] = $userCatId;
8981
            $listResults[$userCatId]['courseInUserCategoryTitle'] = $userCatTitle;
8982
            $listResults[$userCatId]['courseInUserCatList'][] = $listOneCourse;
8983
        }
8984
8985
        // sort by user course cat
8986
        uasort($listResults, 'self::compareByUserCourseCat');
8987
8988
        // sort by course title
8989
        foreach ($listResults as $userCourseCatId => $tabCoursesInCat) {
8990
            $courseInUserCatList = $tabCoursesInCat['courseInUserCatList'];
8991
            uasort($courseInUserCatList, 'self::compareByCourse');
8992
            $listResults[$userCourseCatId]['courseInUserCatList'] = $courseInUserCatList;
8993
        }
8994
8995
        return $listResults;
8996
    }
8997
8998
    /**
8999
     * Return HTML code for displaying session_course_for_coach.
9000
     *
9001
     * @param $userId
9002
     *
9003
     * @return string
9004
     */
9005
    public static function getHtmlNamedSessionCourseForCoach($userId)
9006
    {
9007
        $htmlRes = '';
9008
        $listInfo = self::getNamedSessionCourseForCoach($userId);
9009
        foreach ($listInfo as $i => $listCoursesInfo) {
9010
            $courseInfo = $listCoursesInfo['course'];
9011
            $courseCode = $listCoursesInfo['course']['code'];
9012
9013
            $listParamsCourse = [];
9014
            $listParamsCourse['icon'] = '<div style="float:left">
9015
                <input style="border:none;" type="button" onclick="$(\'#course-'.$courseCode.'\').toggle(\'fast\')" value="+" /></div>'.
9016
                Display::return_icon('blackboard.png', $courseInfo['title'], [], ICON_SIZE_LARGE);
9017
            $listParamsCourse['link'] = '';
9018
            $listParamsCourse['title'] = Display::tag(
9019
                'a',
9020
                $courseInfo['title'],
9021
                ['href' => $listParamsCourse['link']]
9022
            );
9023
            $htmlCourse = '<div class="well" style="border-color:#27587D">'.
9024
                CourseManager::course_item_html($listParamsCourse, true);
9025
            // for each category of session
9026
            $htmlCatSessions = '';
9027
            foreach ($listCoursesInfo['sessionCatList'] as $j => $listCatSessionsInfo) {
9028
                // we got an array of session categories
9029
                $catSessionId = $listCoursesInfo['sessionCatList'][$j]['catSessionId'];
9030
                $catSessionName = $listCoursesInfo['sessionCatList'][$j]['catSessionName'];
9031
9032
                $listParamsCatSession['icon'] = Display::return_icon('folder_blue.png', $catSessionName, [], ICON_SIZE_LARGE);
9033
                $listParamsCatSession['link'] = '';
9034
                $listParamsCatSession['title'] = $catSessionName;
9035
9036
                $marginShift = 20;
9037
                if ($catSessionName != '') {
9038
                    $htmlCatSessions .= '<div style="margin-left:'.$marginShift.'px;">'.
9039
                        CourseManager::course_item_html($listParamsCatSession, true).'</div>';
9040
                    $marginShift = 40;
9041
                }
9042
9043
                // for each sessions
9044
                $listCatSessionSessionList = $listCoursesInfo['sessionCatList'][$j]['sessionList'];
9045
                $htmlSession = '';
9046
                foreach ($listCatSessionSessionList as $k => $listSessionInfo) {
9047
                    // we got an array of session info
9048
                    $sessionId = $listSessionInfo['sessionId'];
9049
                    $sessionName = $listSessionInfo['sessionName'];
9050
9051
                    $listParamsSession['icon'] = Display::return_icon('blackboard_blue.png', $sessionName, [], ICON_SIZE_LARGE);
9052
                    $listParamsSession['link'] = '';
9053
                    $linkToCourseSession = $courseInfo['course_public_url'].'?id_session='.$sessionId;
9054
                    $listParamsSession['title'] =
9055
                        $sessionName.'<div style="font-weight:normal; font-style:italic">
9056
                            <a href="'.$linkToCourseSession.'">'.get_lang('GoToCourseInsideSession').'</a>
9057
                            </div>';
9058
                    $htmlSession .= '<div style="margin-left:'.$marginShift.'px;">'.
9059
                        CourseManager::course_item_html($listParamsSession, true).'</div>';
9060
                }
9061
                $htmlCatSessions .= $htmlSession;
9062
            }
9063
            $htmlRes .= $htmlCourse.'<div style="display:none" id="course-'.$courseCode.'">'.$htmlCatSessions.'</div></div>';
9064
        }
9065
9066
        return $htmlRes;
9067
    }
9068
9069
    /**
9070
     * @param int $userId
9071
     * @param int $courseId
9072
     *
9073
     * @return array
9074
     */
9075
    public static function searchCourseInSessionsFromUser($userId, $courseId)
9076
    {
9077
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
9078
        $userId = (int) $userId;
9079
        $courseId = (int) $courseId;
9080
        if (empty($userId) || empty($courseId)) {
9081
            return [];
9082
        }
9083
9084
        $sql = "SELECT * FROM $table 
9085
                WHERE c_id = $courseId AND user_id = $userId";
9086
        $result = Database::query($sql);
9087
9088
        return Database::store_result($result, 'ASSOC');
9089
    }
9090
9091
    /**
9092
     * Subscribe and redirect to session after inscription.
9093
     */
9094
    public static function redirectToSession()
9095
    {
9096
        $sessionId = ChamiloSession::read('session_redirect');
9097
        $onlyOneCourseSessionToRedirect = ChamiloSession::read('only_one_course_session_redirect');
9098
        if ($sessionId) {
9099
            $sessionInfo = api_get_session_info($sessionId);
9100
            if (!empty($sessionInfo)) {
9101
                $userId = api_get_user_id();
9102
                $response = self::isUserSubscribedAsStudent($sessionId, $userId);
9103
                if ($response) {
9104
                    $urlToRedirect = api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$sessionId;
9105
                    if (!empty($onlyOneCourseSessionToRedirect)) {
9106
                        $urlToRedirect = api_get_path(WEB_PATH).
9107
                            'courses/'.$onlyOneCourseSessionToRedirect.'/index.php?id_session='.$sessionId;
9108
                    }
9109
9110
                    header('Location: '.$urlToRedirect);
9111
                    exit;
9112
                }
9113
            }
9114
        }
9115
    }
9116
9117
    /**
9118
     * @param Course  $course
9119
     * @param Session $session
9120
     *
9121
     * @return int
9122
     */
9123
    public static function getCountUsersInCourseSession(
9124
        Course $course,
9125
        Session $session
9126
    ) {
9127
        return Database::getManager()
9128
            ->createQuery("
9129
                SELECT COUNT(scu)
9130
                FROM ChamiloCoreBundle:SessionRelCourseRelUser scu
9131
                INNER JOIN ChamiloCoreBundle:SessionRelUser su
9132
                    WITH scu.user = su.user
9133
                    AND scu.session = su.session
9134
                WHERE 
9135
                    scu.course = :course AND 
9136
                    su.relationType <> :relationType AND 
9137
                    scu.session = :session
9138
            ")
9139
            ->setParameters([
9140
                'course' => $course->getId(),
9141
                'relationType' => SESSION_RELATION_TYPE_RRHH,
9142
                'session' => $session->getId(),
9143
            ])
9144
            ->getSingleScalarResult();
9145
    }
9146
9147
    /**
9148
     * Get course IDs where user in not subscribed in session.
9149
     *
9150
     * @param User    $user
9151
     * @param Session $session
9152
     *
9153
     * @return array
9154
     */
9155
    public static function getAvoidedCoursesInSession(User $user, Session $session)
9156
    {
9157
        $courseIds = [];
9158
9159
        /** @var SessionRelCourse $sessionCourse */
9160
        foreach ($session->getCourses() as $sessionCourse) {
9161
            /** @var Course $course */
9162
            $course = $sessionCourse->getCourse();
9163
9164
            if ($session->getUserInCourse($user, $course)->count()) {
9165
                continue;
9166
            }
9167
9168
            $courseIds[] = $course->getId();
9169
        }
9170
9171
        return $courseIds;
9172
    }
9173
9174
    /**
9175
     * @param int             $userId
9176
     * @param int             $sessionId
9177
     * @param ExtraFieldValue $extraFieldValue
9178
     * @param string          $collapsableLink
9179
     *
9180
     * @return array
9181
     */
9182
    public static function getCollapsableData($userId, $sessionId, $extraFieldValue, $collapsableLink)
9183
    {
9184
        $collapsed = 0;
9185
9186
        // Get default collapsed value in extra field
9187
        $value = $extraFieldValue->get_values_by_handler_and_field_variable($sessionId, 'collapsed');
9188
        if (!empty($value) && isset($value['value'])) {
9189
            $collapsed = $value['value'];
9190
        }
9191
9192
        $userRelSession = Sessionmanager::getUserSession($userId, $sessionId);
9193
        if ($userRelSession) {
9194
            if (isset($userRelSession['collapsed']) && $userRelSession['collapsed'] != '') {
9195
                $collapsed = $userRelSession['collapsed'];
9196
            }
9197
        } else {
9198
            return ['collapsed' => $collapsed, 'collapsable_link' => '&nbsp;'];
9199
        }
9200
9201
        $link = $collapsableLink.'&session_id='.$sessionId.'&value=1';
9202
        $image = '<i class="fa fa-folder-open"></i>';
9203
        if ($collapsed == 1) {
9204
            $link = $collapsableLink.'&session_id='.$sessionId.'&value=0';
9205
            $image = '<i class="fa fa-folder"></i>';
9206
        }
9207
9208
        $link = Display::url(
9209
            $image,
9210
            $link
9211
        );
9212
9213
        return ['collapsed' => $collapsed, 'collapsable_link' => $link];
9214
    }
9215
9216
9217
    /**
9218
     * Converts "start date" and "end date" to "From start date to end date" string.
9219
     *
9220
     * @param string $startDate
9221
     * @param string $endDate
9222
     * @param bool   $showTime
9223
     * @param bool   $dateHuman
9224
     *
9225
     * @return string
9226
     */
9227
    public static function convertSessionDateToString($startDate, $endDate, $showTime, $dateHuman)
9228
    {
9229
        // api_get_local_time returns empty if date is invalid like 0000-00-00 00:00:00
9230
        $startDateToLocal = api_get_local_time(
9231
            $startDate,
9232
            null,
9233
            null,
9234
            true,
9235
            $showTime,
9236
            $dateHuman
9237
        );
9238
        $endDateToLocal = api_get_local_time(
9239
            $endDate,
9240
            null,
9241
            null,
9242
            true,
9243
            $showTime,
9244
            $dateHuman
9245
        );
9246
9247
        $format = $showTime ? DATE_TIME_FORMAT_LONG_24H : DATE_FORMAT_LONG_NO_DAY;
9248
9249
        $result = '';
9250
        if (!empty($startDateToLocal) && !empty($endDateToLocal)) {
9251
            $result = sprintf(
9252
                get_lang('FromDateXToDateY'),
9253
                api_format_date($startDateToLocal, $format),
9254
                api_format_date($endDateToLocal, $format)
9255
            );
9256
        } else {
9257
            if (!empty($startDateToLocal)) {
9258
                $result = get_lang('From').' '.api_format_date($startDateToLocal, $format);
9259
            }
9260
            if (!empty($endDateToLocal)) {
9261
                $result = get_lang('Until').' '.api_format_date($endDateToLocal, $format);
9262
            }
9263
        }
9264
        if (empty($result)) {
9265
            $result = get_lang('NoTimeLimits');
9266
        }
9267
9268
        return $result;
9269
    }
9270
9271
    /**
9272
     * @param int $id
9273
     *
9274
     * @return string
9275
     */
9276
    public static function getSessionChangeUserReason($id): string
9277
    {
9278
        $reasons = self::getSessionChangeUserReasons();
9279
9280
        return $reasons[$id] ?? '';
9281
    }
9282
9283
    /**
9284
     * @return array
9285
     */
9286
    public static function getSessionChangeUserReasons(): array
9287
    {
9288
        return [
9289
            self::SESSION_CHANGE_USER_REASON_SCHEDULE => get_lang('ScheduleChanged'),
9290
            self::SESSION_CHANGE_USER_REASON_CLASSROOM => get_lang('ClassRoomChanged'),
9291
            self::SESSION_CHANGE_USER_REASON_LOCATION => get_lang('LocationChanged'),
9292
            //self::SESSION_CHANGE_USER_REASON_ENROLLMENT_ANNULATION => get_lang('EnrollmentAnnulation'),
9293
        ];
9294
    }
9295
9296
    /**
9297
     * @param int $id
9298
     *
9299
     * @return bool
9300
     */
9301
    private static function allowed($id)
9302
    {
9303
        $sessionInfo = self::fetch($id);
9304
9305
        if (empty($sessionInfo)) {
9306
            return false;
9307
        }
9308
9309
        if (api_is_platform_admin()) {
9310
            return true;
9311
        }
9312
9313
        $userId = api_get_user_id();
9314
9315
        if (api_is_session_admin() &&
9316
            api_get_setting('allow_session_admins_to_manage_all_sessions') != 'true'
9317
        ) {
9318
            if ($sessionInfo['session_admin_id'] != $userId) {
9319
                return false;
9320
            }
9321
        }
9322
9323
        if (api_is_teacher() &&
9324
            api_get_setting('allow_teachers_to_create_sessions') == 'true'
9325
        ) {
9326
            if ($sessionInfo['id_coach'] != $userId) {
9327
                return false;
9328
            }
9329
        }
9330
9331
        return true;
9332
    }
9333
9334
    /**
9335
     * Add classes (by their names) to a session.
9336
     *
9337
     * @param int   $sessionId
9338
     * @param array $classesNames
9339
     * @param bool  $deleteClassSessions Optional. Empty the session list for the usergroup (class)
9340
     */
9341
    private static function addClassesByName($sessionId, $classesNames, $deleteClassSessions = true)
9342
    {
9343
        if (!$classesNames) {
9344
            return;
9345
        }
9346
9347
        $usergroup = new UserGroup();
9348
9349
        foreach ($classesNames as $className) {
9350
            if (empty($className)) {
9351
                continue;
9352
            }
9353
9354
            $usergroup->subscribe_sessions_to_usergroup(
9355
                $usergroup->get_id_by_name($className),
9356
                [$sessionId],
9357
                $deleteClassSessions
9358
            );
9359
        }
9360
    }
9361
9362
    /**
9363
     * @param array $listA
9364
     * @param array $listB
9365
     *
9366
     * @return int
9367
     */
9368
    private static function compareCatSessionInfo($listA, $listB)
9369
    {
9370
        if ($listA['sessionName'] == $listB['sessionName']) {
9371
            return 0;
9372
        } elseif ($listA['sessionName'] > $listB['sessionName']) {
9373
            return 1;
9374
        } else {
9375
            return -1;
9376
        }
9377
    }
9378
9379
    /**
9380
     * @param array $listA
9381
     * @param array $listB
9382
     *
9383
     * @return int
9384
     */
9385
    private static function compareBySessionName($listA, $listB)
9386
    {
9387
        if ($listB['catSessionName'] == '') {
9388
            return -1;
9389
        } elseif ($listA['catSessionName'] == '') {
9390
            return 1;
9391
        } elseif ($listA['catSessionName'] == $listB['catSessionName']) {
9392
            return 0;
9393
        } elseif ($listA['catSessionName'] > $listB['catSessionName']) {
9394
            return 1;
9395
        } else {
9396
            return -1;
9397
        }
9398
    }
9399
9400
    /**
9401
     * @param array $listA
9402
     * @param array $listB
9403
     *
9404
     * @return int
9405
     */
9406
    private static function compareByUserCourseCat($listA, $listB)
9407
    {
9408
        if ($listA['courseInUserCategoryTitle'] == $listB['courseInUserCategoryTitle']) {
9409
            return 0;
9410
        } elseif ($listA['courseInUserCategoryTitle'] > $listB['courseInUserCategoryTitle']) {
9411
            return 1;
9412
        } else {
9413
            return -1;
9414
        }
9415
    }
9416
9417
    /**
9418
     * @param array $listA
9419
     * @param array $listB
9420
     *
9421
     * @return int
9422
     */
9423
    private static function compareByCourse($listA, $listB)
9424
    {
9425
        if ($listA['title'] == $listB['title']) {
9426
            return 0;
9427
        } elseif ($listA['title'] > $listB['title']) {
9428
            return 1;
9429
        } else {
9430
            return -1;
9431
        }
9432
    }
9433
}
9434