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

SessionManager::getCareerDiagramPerSessionList()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 55
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 28
nc 9
nop 2
dl 0
loc 55
rs 7.6666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Entity\Course;
6
use Chamilo\CoreBundle\Entity\ExtraField;
7
use Chamilo\CoreBundle\Entity\Repository\SequenceRepository;
8
use Chamilo\CoreBundle\Entity\SequenceResource;
9
use Chamilo\CoreBundle\Entity\Session;
10
use Chamilo\CoreBundle\Entity\SessionRelCourse;
11
use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser;
12
use Chamilo\CoreBundle\Entity\SessionRelUser;
13
use Chamilo\UserBundle\Entity\User;
14
use ExtraField as ExtraFieldModel;
15
use Monolog\Logger;
16
17
/**
18
 * Class SessionManager.
19
 *
20
 * This is the session library for Chamilo
21
 * (as in courses>session, not as in PHP session)
22
 * All main sessions functions should be placed here.
23
 * This class provides methods for sessions management.
24
 * Include/require it in your code to use its features.
25
 */
26
class SessionManager
27
{
28
    const STATUS_PLANNED = 1;
29
    const STATUS_PROGRESS = 2;
30
    const STATUS_FINISHED = 3;
31
    const STATUS_CANCELLED = 4;
32
33
    public static $_debug = false;
34
35
    /**
36
     * Constructor.
37
     */
38
    public function __construct()
39
    {
40
    }
41
42
    /**
43
     * Fetches a session from the database.
44
     *
45
     * @param int $id Session Id
46
     *
47
     * @return array Session details
48
     */
49
    public static function fetch($id)
50
    {
51
        $em = Database::getManager();
52
53
        if (empty($id)) {
54
            return [];
55
        }
56
57
        /** @var Session $session */
58
        $session = $em->find('ChamiloCoreBundle:Session', $id);
59
60
        if (!$session) {
0 ignored issues
show
introduced by
$session is of type Chamilo\CoreBundle\Entity\Session, thus it always evaluated to true.
Loading history...
61
            return [];
62
        }
63
64
        $result = [
65
            'id' => $session->getId(),
66
            'id_coach' => $session->getGeneralCoach() ? $session->getGeneralCoach()->getId() : null,
67
            'session_category_id' => $session->getCategory() ? $session->getCategory()->getId() : null,
68
            'name' => $session->getName(),
69
            'description' => $session->getDescription(),
70
            'show_description' => $session->getShowDescription(),
71
            'duration' => $session->getDuration(),
72
            'nbr_courses' => $session->getNbrCourses(),
73
            'nbr_users' => $session->getNbrUsers(),
74
            'nbr_classes' => $session->getNbrClasses(),
75
            'session_admin_id' => $session->getSessionAdminId(),
76
            'visibility' => $session->getVisibility(),
77
            'promotion_id' => $session->getPromotionId(),
78
            'display_start_date' => $session->getDisplayStartDate()
79
                ? $session->getDisplayStartDate()->format('Y-m-d H:i:s')
80
                : null,
81
            'display_end_date' => $session->getDisplayEndDate()
82
                ? $session->getDisplayEndDate()->format('Y-m-d H:i:s')
83
                : null,
84
            'access_start_date' => $session->getAccessStartDate()
85
                ? $session->getAccessStartDate()->format('Y-m-d H:i:s')
86
                : null,
87
            'access_end_date' => $session->getAccessEndDate()
88
                ? $session->getAccessEndDate()->format('Y-m-d H:i:s')
89
                : null,
90
            'coach_access_start_date' => $session->getCoachAccessStartDate()
91
                ? $session->getCoachAccessStartDate()->format('Y-m-d H:i:s')
92
                : null,
93
            'coach_access_end_date' => $session->getCoachAccessEndDate()
94
                ? $session->getCoachAccessEndDate()->format('Y-m-d H:i:s')
95
                : null,
96
            'send_subscription_notification' => $session->getSendSubscriptionNotification(),
97
        ];
98
99
        if (api_get_configuration_value('allow_session_status')) {
100
            $table = Database::get_main_table(TABLE_MAIN_SESSION);
101
            $sql = "SELECT status FROM $table WHERE id = $id";
102
            $resultQuery = Database::query($sql);
103
            $row = Database::fetch_array($resultQuery);
104
            $result['status'] = $row['status'];
105
            $result['status_label'] = self::getStatusLabel($row['status']);
106
        }
107
108
        // Converted to local values
109
        $variables = [
110
            'display_start_date',
111
            'display_end_date',
112
            'access_start_date',
113
            'access_end_date',
114
            'coach_access_start_date',
115
            'coach_access_end_date',
116
        ];
117
118
        foreach ($variables as $value) {
119
            $result[$value.'_to_local_time'] = null;
120
            if (!empty($result[$value])) {
121
                $result[$value.'_to_local_time'] = api_get_local_time($result[$value]);
122
            }
123
        }
124
125
        return $result;
126
    }
127
128
    /**
129
     * Create a session.
130
     *
131
     * @author Carlos Vargas <[email protected]>, from existing code
132
     *
133
     * @param string   $name
134
     * @param string   $startDate                    (YYYY-MM-DD hh:mm:ss)
135
     * @param string   $endDate                      (YYYY-MM-DD hh:mm:ss)
136
     * @param string   $displayStartDate             (YYYY-MM-DD hh:mm:ss)
137
     * @param string   $displayEndDate               (YYYY-MM-DD hh:mm:ss)
138
     * @param string   $coachStartDate               (YYYY-MM-DD hh:mm:ss)
139
     * @param string   $coachEndDate                 (YYYY-MM-DD hh:mm:ss)
140
     * @param mixed    $coachId                      If int, this is the session coach id,
141
     *                                               if string, the coach ID will be looked for from the user table
142
     * @param int      $sessionCategoryId            ID of the session category in which this session is registered
143
     * @param int      $visibility                   Visibility after end date (0 = read-only, 1 = invisible, 2 = accessible)
144
     * @param bool     $fixSessionNameIfExists
145
     * @param string   $duration
146
     * @param string   $description                  Optional. The session description
147
     * @param int      $showDescription              Optional. Whether show the session description
148
     * @param array    $extraFields
149
     * @param int      $sessionAdminId               Optional. If this sessions was created by a session admin, assign it to him
150
     * @param bool     $sendSubscriptionNotification Optional.
151
     *                                               Whether send a mail notification to users being subscribed
152
     * @param int      $accessUrlId                  Optional.
153
     * @param int      $status
154
     *
155
     * @return mixed Session ID on success, error message otherwise
156
     *
157
     * @todo   use an array to replace all this parameters or use the model.lib.php ...
158
     */
159
    public static function create_session(
160
        $name,
161
        $startDate,
162
        $endDate,
163
        $displayStartDate,
164
        $displayEndDate,
165
        $coachStartDate,
166
        $coachEndDate,
167
        $coachId,
168
        $sessionCategoryId,
169
        $visibility = 1,
170
        $fixSessionNameIfExists = false,
171
        $duration = null,
172
        $description = null,
173
        $showDescription = 0,
174
        $extraFields = [],
175
        $sessionAdminId = 0,
176
        $sendSubscriptionNotification = false,
177
        $accessUrlId = 0,
178
        $status = 0
179
    ) {
180
        global $_configuration;
181
182
        // Check portal limits
183
        $accessUrlId = api_is_multiple_url_enabled()
184
            ? (empty($accessUrlId) ? api_get_current_access_url_id() : (int) $accessUrlId)
185
            : 1;
186
187
        if (isset($_configuration[$accessUrlId]) &&
188
            is_array($_configuration[$accessUrlId]) &&
189
            isset($_configuration[$accessUrlId]['hosting_limit_sessions']) &&
190
            $_configuration[$accessUrlId]['hosting_limit_sessions'] > 0
191
        ) {
192
            $num = self::count_sessions();
193
            if ($num >= $_configuration[$accessUrlId]['hosting_limit_sessions']) {
194
                api_warn_hosting_contact('hosting_limit_sessions');
195
196
                return get_lang('PortalSessionsLimitReached');
197
            }
198
        }
199
200
        $name = Database::escape_string(trim($name));
201
        $sessionCategoryId = (int) $sessionCategoryId;
202
        $visibility = (int) $visibility;
203
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
204
205
        $startDate = Database::escape_string($startDate);
206
        $endDate = Database::escape_string($endDate);
207
208
        if (empty($name)) {
209
            $msg = get_lang('SessionNameIsRequired');
210
211
            return $msg;
212
        } elseif (!empty($startDate) && !api_is_valid_date($startDate, 'Y-m-d H:i') &&
213
            !api_is_valid_date($startDate, 'Y-m-d H:i:s')
214
        ) {
215
            $msg = get_lang('InvalidStartDate');
216
217
            return $msg;
218
        } elseif (!empty($endDate) && !api_is_valid_date($endDate, 'Y-m-d H:i') &&
219
            !api_is_valid_date($endDate, 'Y-m-d H:i:s')
220
        ) {
221
            $msg = get_lang('InvalidEndDate');
222
223
            return $msg;
224
        } elseif (!empty($startDate) && !empty($endDate) && $startDate >= $endDate) {
225
            $msg = get_lang('StartDateShouldBeBeforeEndDate');
226
227
            return $msg;
228
        } else {
229
            $ready_to_create = false;
230
            if ($fixSessionNameIfExists) {
231
                $name = self::generateNextSessionName($name);
232
                if ($name) {
233
                    $ready_to_create = true;
234
                } else {
235
                    $msg = get_lang('SessionNameAlreadyExists');
236
237
                    return $msg;
238
                }
239
            } else {
240
                $rs = Database::query("SELECT 1 FROM $tbl_session WHERE name='".$name."'");
241
                if (Database::num_rows($rs)) {
242
                    $msg = get_lang('SessionNameAlreadyExists');
243
244
                    return $msg;
245
                }
246
                $ready_to_create = true;
247
            }
248
249
            if ($ready_to_create) {
250
                $sessionAdminId = !empty($sessionAdminId) ? $sessionAdminId : api_get_user_id();
251
                $values = [
252
                    'name' => $name,
253
                    'id_coach' => $coachId,
254
                    'session_admin_id' => $sessionAdminId,
255
                    'visibility' => $visibility,
256
                    'description' => $description,
257
                    'show_description' => $showDescription,
258
                    'send_subscription_notification' => (int) $sendSubscriptionNotification,
259
                ];
260
261
                if (!empty($startDate)) {
262
                    $values['access_start_date'] = api_get_utc_datetime($startDate);
263
                }
264
265
                if (!empty($endDate)) {
266
                    $values['access_end_date'] = api_get_utc_datetime($endDate);
267
                }
268
269
                if (!empty($displayStartDate)) {
270
                    $values['display_start_date'] = api_get_utc_datetime($displayStartDate);
271
                }
272
273
                if (!empty($displayEndDate)) {
274
                    $values['display_end_date'] = api_get_utc_datetime($displayEndDate);
275
                }
276
277
                if (!empty($coachStartDate)) {
278
                    $values['coach_access_start_date'] = api_get_utc_datetime($coachStartDate);
279
                }
280
                if (!empty($coachEndDate)) {
281
                    $values['coach_access_end_date'] = api_get_utc_datetime($coachEndDate);
282
                }
283
284
                if (!empty($sessionCategoryId)) {
285
                    $values['session_category_id'] = $sessionCategoryId;
286
                }
287
288
                if (api_get_configuration_value('allow_session_status')) {
289
                    $values['status'] = $status;
290
                }
291
292
                $session_id = Database::insert($tbl_session, $values);
293
                $duration = (int) $duration;
294
295
                if (!empty($duration)) {
296
                    $sql = "UPDATE $tbl_session SET
297
                        access_start_date = NULL,
298
                        access_end_date = NULL,
299
                        display_start_date = NULL,
300
                        display_end_date = NULL,
301
                        coach_access_start_date = NULL,
302
                        coach_access_end_date = NULL,
303
                        duration = $duration
304
                    WHERE id = $session_id";
305
                    Database::query($sql);
306
                } else {
307
                    $sql = "UPDATE $tbl_session
308
                        SET duration = 0
309
                        WHERE id = $session_id";
310
                    Database::query($sql);
311
                }
312
313
                if (!empty($session_id)) {
314
                    $extraFields['item_id'] = $session_id;
315
                    $sessionFieldValue = new ExtraFieldValue('session');
316
                    $sessionFieldValue->saveFieldValues($extraFields);
317
318
                    // Adding to the correct URL
319
                    UrlManager::add_session_to_url($session_id, $accessUrlId);
320
321
                    // add event to system log
322
                    Event::addEvent(
323
                        LOG_SESSION_CREATE,
324
                        LOG_SESSION_ID,
325
                        $session_id,
326
                        api_get_utc_datetime(),
327
                        api_get_user_id()
328
                    );
329
                }
330
331
                return $session_id;
332
            }
333
        }
334
    }
335
336
    /**
337
     * @param string $name
338
     *
339
     * @return bool
340
     */
341
    public static function sessionNameExists($name)
342
    {
343
        $name = Database::escape_string($name);
344
        $sql = "SELECT COUNT(*) as count FROM ".Database::get_main_table(TABLE_MAIN_SESSION)."
345
                WHERE name = '$name'";
346
        $result = Database::fetch_array(Database::query($sql));
347
348
        return $result['count'] > 0;
349
    }
350
351
    /**
352
     * @param string $where_condition
353
     *
354
     * @return mixed
355
     */
356
    public static function get_count_admin($where_condition = '')
357
    {
358
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
359
        $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
360
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
361
        $table_access_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
362
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
363
364
        $where = 'WHERE 1=1 ';
365
        $user_id = api_get_user_id();
366
        $extraJoin = '';
367
368
        if (api_is_session_admin() &&
369
            api_get_setting('allow_session_admins_to_manage_all_sessions') == 'false'
370
        ) {
371
            $where .= " AND (
372
                            s.session_admin_id = $user_id  OR
373
                            sru.user_id = '$user_id' AND
374
                            sru.relation_type = '".SESSION_RELATION_TYPE_RRHH."'
375
                            )
376
                      ";
377
378
            $extraJoin = " INNER JOIN $tbl_session_rel_user sru
379
                           ON sru.session_id = s.id ";
380
        }
381
382
        $today = api_get_utc_datetime();
383
        $today = api_strtotime($today, 'UTC');
384
        $today = date('Y-m-d', $today);
385
386
        if (!empty($where_condition)) {
387
            $where_condition = str_replace("(  session_active = ':'  )", '1=1', $where_condition);
388
389
            $where_condition = str_replace('category_name', 'sc.name', $where_condition);
390
            $where_condition = str_replace(
391
                ["AND session_active = '1'  )", " AND (  session_active = '1'  )"],
392
                [') GROUP BY s.name HAVING session_active = 1 ', " GROUP BY s.name HAVING session_active = 1 "],
393
                $where_condition
394
            );
395
            $where_condition = str_replace(
396
                ["AND session_active = '0'  )", " AND (  session_active = '0'  )"],
397
                [') GROUP BY s.name HAVING session_active = 0 ', " GROUP BY s.name HAVING session_active = '0' "],
398
                $where_condition
399
            );
400
        } else {
401
            $where_condition = " AND 1 = 1";
402
        }
403
404
        $courseCondition = null;
405
        if (strpos($where_condition, 'c.id')) {
406
            $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
407
            $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE);
408
            $courseCondition = " INNER JOIN $table course_rel_session
409
                                 ON (s.id = course_rel_session.session_id)
410
                                 INNER JOIN $tableCourse c
411
                                 ON (course_rel_session.c_id = c.id)
412
                                ";
413
        }
414
415
        $sql = "SELECT COUNT(id) as total_rows FROM (
416
                SELECT DISTINCT
417
                 IF (
418
					(s.access_start_date <= '$today' AND '$today' <= s.access_end_date) OR
419
                    (s.access_start_date IS NULL AND s.access_end_date IS NULL ) OR
420
					(s.access_start_date <= '$today' AND s.access_end_date IS NULL) OR
421
					('$today' <= s.access_end_date AND s.access_start_date IS NULL)
422
				, 1, 0) as session_active,
423
                s.id
424
                FROM $tbl_session s
425
                LEFT JOIN $tbl_session_category sc
426
                ON s.session_category_id = sc.id
427
                INNER JOIN $tbl_user u
428
                ON s.id_coach = u.id
429
                $courseCondition
430
                $extraJoin
431
                $where $where_condition ) as session_table";
432
433
        if (api_is_multiple_url_enabled()) {
434
            $access_url_id = api_get_current_access_url_id();
435
            if ($access_url_id != -1) {
436
                $where .= " AND ar.access_url_id = $access_url_id ";
437
438
                $sql = "SELECT count(id) as total_rows FROM (
439
                SELECT DISTINCT
440
                  IF (
441
					(s.access_start_date <= '$today' AND '$today' <= s.access_end_date) OR
442
                    (s.access_start_date IS NULL AND s.access_end_date IS NULL) OR
443
					(s.access_start_date <= '$today' AND s.access_end_date IS NULL) OR
444
					('$today' <= s.access_end_date AND s.access_start_date IS NULL)
445
				, 1, 0)
446
				as session_active,
447
				s.id
448
                FROM $tbl_session s
449
                    LEFT JOIN  $tbl_session_category sc
450
                    ON s.session_category_id = sc.id
451
                    INNER JOIN $tbl_user u ON s.id_coach = u.user_id
452
                    INNER JOIN $table_access_url_rel_session ar
453
                    ON ar.session_id = s.id
454
                    $courseCondition
455
                    $extraJoin
456
                $where $where_condition) as session_table";
457
            }
458
        }
459
460
        $result_rows = Database::query($sql);
461
        $row = Database::fetch_array($result_rows);
462
        $num = $row['total_rows'];
463
464
        return $num;
465
    }
466
467
    /**
468
     * Get session list for a session admin or platform admin.
469
     *
470
     * @param int   $userId   User Id for the session admin.
471
     * @param array $options  Optional. Order and limit keys.
472
     * @param bool  $getCount Optional. Whether to get all the results or only the count.
473
     * @param array $columns  Optional. Columns from jqGrid.
474
     *
475
     * @return array
476
     */
477
    public static function getSessionsForAdmin(
478
        $userId,
479
        $options = [],
480
        $getCount = false,
481
        $columns = []
482
    ) {
483
        $tblSession = Database::get_main_table(TABLE_MAIN_SESSION);
484
        $sessionCategoryTable = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
485
486
        $where = 'WHERE 1 = 1 ';
487
488
        $userId = (int) $userId;
489
490
        if (!api_is_platform_admin()) {
491
            if (api_is_session_admin() &&
492
                api_get_setting('allow_session_admins_to_manage_all_sessions') === 'false'
493
            ) {
494
                $where .= " AND s.session_admin_id = $userId ";
495
            }
496
        }
497
498
        if (!api_is_platform_admin() &&
499
            api_is_teacher() &&
500
            api_get_setting('allow_teachers_to_create_sessions') === 'true'
501
        ) {
502
            $where .= " AND s.id_coach = $userId ";
503
        }
504
505
        $extraFieldModel = new ExtraFieldModel('session');
506
        $conditions = $extraFieldModel->parseConditions($options);
507
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   $get_count         Whether to get all the results or only the count
613
     * @param array  $columns
614
     * @param array  $extraFieldsToLoad
615
     * @param string $language
616
     *
617
     * @return mixed Integer for number of rows, or array of results
618
     * @assert ([],true) !== false
619
     */
620
    public static function get_sessions_admin(
621
        $options = [],
622
        $get_count = false,
623
        $columns = [],
624
        $extraFieldsToLoad = [],
625
        $language = ''
626
    ) {
627
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
628
        $sessionCategoryTable = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
629
630
        $where = 'WHERE 1 = 1 ';
631
        $user_id = api_get_user_id();
632
633
        if (!api_is_platform_admin()) {
634
            if (api_is_session_admin() &&
635
                api_get_setting('allow_session_admins_to_manage_all_sessions') == 'false'
636
            ) {
637
                $where .= " AND s.session_admin_id = $user_id ";
638
            }
639
        }
640
641
        if (!api_is_platform_admin() &&
642
            api_is_teacher() &&
643
            api_get_setting('allow_teachers_to_create_sessions') == 'true'
644
        ) {
645
            $where .= " AND s.id_coach = $user_id ";
646
        }
647
        $extra_field = new ExtraFieldModel('session');
648
        $conditions = $extra_field->parseConditions($options);
649
        $inject_joins = $conditions['inject_joins'];
650
        $where .= $conditions['where'];
651
        $inject_where = $conditions['inject_where'];
652
        $inject_extra_fields = $conditions['inject_extra_fields'];
653
        $order = $conditions['order'];
654
        $limit = $conditions['limit'];
655
656
        $isMakingOrder = false;
657
        $showCountUsers = false;
658
659
        if ($get_count == true) {
660
            $select = " SELECT count(DISTINCT s.id) as total_rows";
661
        } else {
662
            if (!empty($columns['column_model'])) {
663
                foreach ($columns['column_model'] as $column) {
664
                    if ($column['name'] == 'users') {
665
                        $showCountUsers = true;
666
                    }
667
                }
668
            }
669
670
            $select =
671
                "SELECT DISTINCT
672
                     s.name,
673
                     s.display_start_date,
674
                     s.display_end_date,
675
                     access_start_date,
676
                     access_end_date,
677
                     s.visibility,
678
                     s.session_category_id,
679
                     $inject_extra_fields
680
                     s.id
681
             ";
682
683
            // ofaj fix
684
            if (!empty($extraFieldsToLoad)) {
685
                $select = "SELECT DISTINCT s.* ";
686
            }
687
688
            if ($showCountUsers) {
689
                $select .= ', count(su.user_id) users';
690
            }
691
692
            if (api_get_configuration_value('allow_session_status')) {
693
                $select .= ', status';
694
            }
695
            if (isset($options['order'])) {
696
                $isMakingOrder = strpos($options['order'], 'category_name') === 0;
697
            }
698
        }
699
700
        $isFilteringSessionCategory = strpos($where, 'category_name') !== false;
701
        $isFilteringSessionCategoryWithName = strpos($where, 'sc.name') !== false;
702
703
        if ($isMakingOrder || $isFilteringSessionCategory || $isFilteringSessionCategoryWithName) {
704
            $inject_joins .= " LEFT JOIN $sessionCategoryTable sc ON s.session_category_id = sc.id ";
705
706
            if ($isFilteringSessionCategory) {
707
                $where = str_replace('category_name', 'sc.name', $where);
708
            }
709
710
            if ($isMakingOrder) {
711
                $order = str_replace('category_name', 'sc.name', $order);
712
            }
713
        }
714
715
        if ($showCountUsers) {
716
            $table = Database::get_main_table(TABLE_MAIN_SESSION_USER);
717
            $inject_joins .= " LEFT JOIN $table su ON (su.session_id = s.id)";
718
        }
719
720
        if (!empty($language)) {
721
            $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
722
            $tableCourse = Database::get_main_table(TABLE_MAIN_COURSE);
723
            $inject_joins .= " INNER JOIN $table sc ON (sc.session_id = s.id)
724
                               INNER JOIN $tableCourse c ON (sc.c_id = c.id)";
725
            $language = Database::escape_string($language);
726
            $where .= " AND c.course_language = '$language' ";
727
        }
728
729
        $query = "$select FROM $tbl_session s $inject_joins $where $inject_where";
730
731
        if (api_is_multiple_url_enabled()) {
732
            $table_access_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
733
            $access_url_id = api_get_current_access_url_id();
734
            if ($access_url_id != -1) {
735
                $where .= " AND ar.access_url_id = $access_url_id ";
736
                $query = "$select
737
                        FROM $tbl_session s $inject_joins
738
                        INNER JOIN $table_access_url_rel_session ar
739
                        ON (ar.session_id = s.id) $where";
740
            }
741
        }
742
743
        if ($showCountUsers) {
744
            $query .= ' GROUP by s.id';
745
        }
746
        $allowOrder = api_get_configuration_value('session_list_order');
747
        if ($allowOrder) {
748
            $order = ' ORDER BY position ASC';
749
        }
750
751
        $query .= $order;
752
        $query .= $limit;
753
754
        $categories = self::get_all_session_category();
755
        $orderedCategories = [];
756
        if (!empty($categories)) {
757
            foreach ($categories as $category) {
758
                $orderedCategories[$category['id']] = $category['name'];
759
            }
760
        }
761
        $result = Database::query($query);
762
        $formatted_sessions = [];
763
        if (Database::num_rows($result)) {
764
            $sessions = Database::store_result($result, 'ASSOC');
765
            if ($get_count) {
766
                return $sessions[0]['total_rows'];
767
            }
768
769
            $activeIcon = Display::return_icon(
770
                'accept.png',
771
                get_lang('Active'),
772
                [],
773
                ICON_SIZE_SMALL
774
            );
775
            $inactiveIcon = Display::return_icon(
776
                'error.png',
777
                get_lang('Inactive'),
778
                [],
779
                ICON_SIZE_SMALL
780
            );
781
782
            foreach ($sessions as $session) {
783
                $session_id = $session['id'];
784
                if ($showCountUsers) {
785
                    $session['users'] = self::get_users_by_session(
786
                        $session['id'],
787
                        null,
788
                        true
789
                    );
790
                    $courses = self::getCoursesInSession($session_id);
791
                    $teachers = '';
792
                    foreach ($courses as $courseId) {
793
                        $courseInfo = api_get_course_info_by_id($courseId);
794
795
                        // Ofaj
796
                        $teachers = CourseManager::get_coachs_from_course_to_string($session_id, $courseInfo['real_id']);
797
                        /*$teachers .= CourseManager::get_teacher_list_from_course_code_to_string(
798
                            $courseInfo['code']
799
                        );*/
800
                    }
801
                    // ofaj
802
                    $session['teachers'] = '';
803
                    if (!empty($teachers)) {
804
                        $session['teachers'] = Display::return_icon('teacher.png', addslashes($teachers));
805
                    }
806
                }
807
                $url = api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session='.$session['id'];
808
                if (api_is_drh()) {
809
                    $url = api_get_path(WEB_CODE_PATH).'session/about.php?session_id='.$session['id'];
810
                }
811
                if (api_is_platform_admin()) {
812
                    $url = api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session='.$session['id'];
813
                }
814
815
                if ($extraFieldsToLoad) {
816
                    $url = api_get_path(WEB_CODE_PATH).'session/about.php?session_id='.$session['id'];
817
                }
818
                $session['name'] = Display::url($session['name'], $url);
819
820
                if (!empty($extraFieldsToLoad)) {
821
                    foreach ($extraFieldsToLoad as $field) {
822
                        $extraFieldValue = new ExtraFieldValue('session');
823
                        $fieldData = $extraFieldValue->getAllValuesByItemAndField(
824
                            $session['id'],
825
                            $field['id']
826
                        );
827
                        $fieldDataArray = [];
828
                        $fieldDataToString = '';
829
                        if (!empty($fieldData)) {
830
                            foreach ($fieldData as $data) {
831
                                $fieldDataArray[] = $data['value'];
832
                            }
833
                            $fieldDataToString = implode(', ', $fieldDataArray);
834
                        }
835
                        $session[$field['variable']] = $fieldDataToString;
836
                    }
837
                }
838
839
                if (isset($session['session_active']) && $session['session_active'] == 1) {
840
                    $session['session_active'] = $activeIcon;
841
                } else {
842
                    $session['session_active'] = $inactiveIcon;
843
                }
844
845
                $session = self::convert_dates_to_local($session, true);
846
847
                switch ($session['visibility']) {
848
                    case SESSION_VISIBLE_READ_ONLY: //1
849
                        $session['visibility'] = get_lang('ReadOnly');
850
                        break;
851
                    case SESSION_VISIBLE:           //2
852
                    case SESSION_AVAILABLE:         //4
853
                        $session['visibility'] = get_lang('Visible');
854
                        break;
855
                    case SESSION_INVISIBLE:         //3
856
                        $session['visibility'] = api_ucfirst(get_lang('Invisible'));
857
                        break;
858
                }
859
860
                // Cleaning double selects.
861
                foreach ($session as $key => &$value) {
862
                    if (isset($options_by_double[$key]) || isset($options_by_double[$key.'_second'])) {
863
                        $options = explode('::', $value);
864
                    }
865
                    $original_key = $key;
866
                    if (strpos($key, '_second') === false) {
867
                    } else {
868
                        $key = str_replace('_second', '', $key);
869
                    }
870
871
                    if (isset($options_by_double[$key])) {
872
                        if (isset($options[0])) {
873
                            if (isset($options_by_double[$key][$options[0]])) {
874
                                if (strpos($original_key, '_second') === false) {
875
                                    $value = $options_by_double[$key][$options[0]]['option_display_text'];
876
                                } else {
877
                                    $value = $options_by_double[$key][$options[1]]['option_display_text'];
878
                                }
879
                            }
880
                        }
881
                    }
882
                }
883
884
                $categoryName = isset($orderedCategories[$session['session_category_id']]) ? $orderedCategories[$session['session_category_id']] : '';
885
                $session['category_name'] = $categoryName;
886
                if (isset($session['status'])) {
887
                    $session['status'] = self::getStatusLabel($session['status']);
888
                }
889
890
                $formatted_sessions[] = $session;
891
            }
892
        }
893
894
        return $formatted_sessions;
895
    }
896
897
    /**
898
     *  Get total of records for progress of learning paths in the given session.
899
     *
900
     *  @param int session id
901
     *
902
     *  @return int
903
     */
904
    public static function get_count_session_lp_progress($sessionId = 0)
905
    {
906
        $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
907
        $tbl_lp_view = Database::get_course_table(TABLE_LP_VIEW);
908
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
909
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
910
911
        $sessionId = intval($sessionId);
912
913
        $sql = "SELECT  count(*) as total_rows
914
                FROM $tbl_lp_view v
915
                INNER JOIN $tbl_lp l ON l.id = v.lp_id
916
                INNER JOIN $tbl_user u ON u.user_id = v.user_id
917
                INNER JOIN $tbl_course c
918
                WHERE v.session_id = ".$sessionId;
919
        $result_rows = Database::query($sql);
920
        $row = Database::fetch_array($result_rows);
921
        $num = $row['total_rows'];
922
923
        return $num;
924
    }
925
926
    /**
927
     * Gets the admin session list callback of the session/session_list.php page.
928
     *
929
     * @param array $options           order and limit keys
930
     * @param bool  $getCount          Whether to get all the results or only the count
931
     * @param array $columns
932
     * @param array $extraFieldsToLoad
933
     *
934
     * @return mixed Integer for number of rows, or array of results
935
     * @assert ([],true) !== false
936
     */
937
    public static function formatSessionsAdminForGrid(
938
        $options = [],
939
        $getCount = false,
940
        $columns = [],
941
        $extraFieldsToLoad = []
942
    ) {
943
        $showCountUsers = false;
944
945
        if (!$getCount && !empty($columns['column_model'])) {
946
            foreach ($columns['column_model'] as $column) {
947
                if ($column['name'] == 'users') {
948
                    $showCountUsers = true;
949
                }
950
            }
951
        }
952
953
        $userId = api_get_user_id();
954
        $sessions = self::getSessionsForAdmin($userId, $options, $getCount, $columns);
955
956
        if ($getCount) {
957
            return (int) $sessions;
958
        }
959
960
        $formattedSessions = [];
961
        $categories = self::get_all_session_category();
962
        $orderedCategories = [];
963
        if (!empty($categories)) {
964
            foreach ($categories as $category) {
965
                $orderedCategories[$category['id']] = $category['name'];
966
            }
967
        }
968
969
        $activeIcon = Display::return_icon('accept.png', get_lang('Active'));
970
        $inactiveIcon = Display::return_icon('error.png', get_lang('Inactive'));
971
        $webPath = api_get_path(WEB_PATH);
972
973
        foreach ($sessions as $session) {
974
            if ($showCountUsers) {
975
                $session['users'] = self::get_users_by_session($session['id'], 0, true);
976
            }
977
            $url = $webPath.'main/session/resume_session.php?id_session='.$session['id'];
978
            if ($extraFieldsToLoad || api_is_drh()) {
979
                $url = $webPath.'session/'.$session['id'].'/about/';
980
            }
981
982
            $session['name'] = Display::url($session['name'], $url);
983
984
            if (!empty($extraFieldsToLoad)) {
985
                foreach ($extraFieldsToLoad as $field) {
986
                    $extraFieldValue = new ExtraFieldValue('session');
987
                    $fieldData = $extraFieldValue->getAllValuesByItemAndField(
988
                        $session['id'],
989
                        $field['id']
990
                    );
991
                    $fieldDataArray = [];
992
                    $fieldDataToString = '';
993
                    if (!empty($fieldData)) {
994
                        foreach ($fieldData as $data) {
995
                            $fieldDataArray[] = $data['value'];
996
                        }
997
                        $fieldDataToString = implode(', ', $fieldDataArray);
998
                    }
999
                    $session[$field['variable']] = $fieldDataToString;
1000
                }
1001
            }
1002
            if (isset($session['session_active']) && $session['session_active'] == 1) {
1003
                $session['session_active'] = $activeIcon;
1004
            } else {
1005
                $session['session_active'] = $inactiveIcon;
1006
            }
1007
1008
            $session = self::convert_dates_to_local($session, true);
1009
1010
            switch ($session['visibility']) {
1011
                case SESSION_VISIBLE_READ_ONLY: //1
1012
                    $session['visibility'] = get_lang('ReadOnly');
1013
                    break;
1014
                case SESSION_VISIBLE:           //2
1015
                case SESSION_AVAILABLE:         //4
1016
                    $session['visibility'] = get_lang('Visible');
1017
                    break;
1018
                case SESSION_INVISIBLE:         //3
1019
                    $session['visibility'] = api_ucfirst(get_lang('Invisible'));
1020
                    break;
1021
            }
1022
1023
            // Cleaning double selects.
1024
            foreach ($session as $key => &$value) {
1025
                if (isset($optionsByDouble[$key]) || isset($optionsByDouble[$key.'_second'])) {
1026
                    $options = explode('::', $value);
1027
                }
1028
                $original_key = $key;
1029
                if (strpos($key, '_second') !== false) {
1030
                    $key = str_replace('_second', '', $key);
1031
                }
1032
1033
                if (isset($optionsByDouble[$key]) &&
1034
                    isset($options[0]) &&
1035
                    isset($optionsByDouble[$key][$options[0]])
1036
                ) {
1037
                    if (strpos($original_key, '_second') === false) {
1038
                        $value = $optionsByDouble[$key][$options[0]]['option_display_text'];
1039
                    } else {
1040
                        $value = $optionsByDouble[$key][$options[1]]['option_display_text'];
1041
                    }
1042
                }
1043
            }
1044
1045
            $categoryName = isset($orderedCategories[$session['session_category_id']])
1046
                ? $orderedCategories[$session['session_category_id']]
1047
                : '';
1048
            $session['category_name'] = $categoryName;
1049
            $formattedSessions[] = $session;
1050
        }
1051
1052
        return $formattedSessions;
1053
    }
1054
1055
    /**
1056
     * Gets the progress of learning paths in the given session.
1057
     *
1058
     * @param int    $sessionId
1059
     * @param int    $courseId
1060
     * @param string $date_from
1061
     * @param string $date_to
1062
     * @param array options order and limit keys
1063
     *
1064
     * @return array table with user name, lp name, progress
1065
     */
1066
    public static function get_session_lp_progress(
1067
        $sessionId = 0,
1068
        $courseId = 0,
1069
        $date_from,
1070
        $date_to,
1071
        $options
1072
    ) {
1073
        //escaping vars
1074
        $sessionId = $sessionId == 'T' ? 'T' : intval($sessionId);
1075
        $courseId = intval($courseId);
1076
        $date_from = Database::escape_string($date_from);
1077
        $date_to = Database::escape_string($date_to);
1078
1079
        //tables
1080
        $session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
1081
        $user = Database::get_main_table(TABLE_MAIN_USER);
1082
        $tbl_course_lp_view = Database::get_course_table(TABLE_LP_VIEW);
1083
1084
        $course = api_get_course_info_by_id($courseId);
1085
        $sessionCond = 'and session_id = %s';
1086
        if ($sessionId === 'T') {
1087
            $sessionCond = '';
1088
        }
1089
1090
        $where = " WHERE c_id = '%s' AND s.status <> 2 $sessionCond";
1091
1092
        $limit = null;
1093
        if (!empty($options['limit'])) {
1094
            $limit = " LIMIT ".$options['limit'];
1095
        }
1096
1097
        if (!empty($options['where'])) {
1098
            $where .= ' '.$options['where'];
1099
        }
1100
1101
        $order = null;
1102
        if (!empty($options['order'])) {
1103
            $order = " ORDER BY ".$options['order'];
1104
        }
1105
1106
        $sql = "SELECT u.user_id, u.lastname, u.firstname, u.username, u.email, s.c_id
1107
                FROM $session_course_user s
1108
                INNER JOIN $user u ON u.user_id = s.user_id
1109
                $where
1110
                $order
1111
                $limit";
1112
1113
        $sql_query = sprintf($sql, Database::escape_string($course['real_id']), $sessionId);
1114
1115
        $rs = Database::query($sql_query);
1116
        while ($user = Database::fetch_array($rs)) {
1117
            $users[$user['user_id']] = $user;
1118
        }
1119
1120
        // Get lessons
1121
        $lessons = LearnpathList::get_course_lessons($course['code'], $sessionId);
1122
1123
        $table = [];
1124
        foreach ($users as $user) {
1125
            $data = [
1126
                'lastname' => $user[1],
1127
                'firstname' => $user[2],
1128
                'username' => $user[3],
1129
            ];
1130
1131
            $sessionCond = 'AND v.session_id = %d';
1132
            if ($sessionId == 'T') {
1133
                $sessionCond = "";
1134
            }
1135
1136
            //Get lessons progress by user
1137
            $sql = "SELECT v.lp_id as id, v.progress
1138
                    FROM  $tbl_course_lp_view v
1139
                    WHERE v.c_id = %d
1140
                    AND v.user_id = %d
1141
            $sessionCond";
1142
1143
            $sql_query = sprintf(
1144
                $sql,
1145
                intval($courseId),
1146
                intval($user['user_id']),
1147
                $sessionId
1148
            );
1149
1150
            $result = Database::query($sql_query);
1151
1152
            $user_lessons = [];
1153
            while ($row = Database::fetch_array($result)) {
1154
                $user_lessons[$row['id']] = $row;
1155
            }
1156
1157
            //Match course lessons with user progress
1158
            $progress = 0;
1159
            $count = 0;
1160
            foreach ($lessons as $lesson) {
1161
                $data[$lesson['id']] = (!empty($user_lessons[$lesson['id']]['progress'])) ? $user_lessons[$lesson['id']]['progress'] : 0;
1162
                $progress += $data[$lesson['id']];
1163
                $data[$lesson['id']] = $data[$lesson['id']].'%';
1164
                $count++;
1165
            }
1166
            if ($count == 0) {
1167
                $data['total'] = 0;
1168
            } else {
1169
                $data['total'] = round($progress / $count, 2).'%';
1170
            }
1171
            $table[] = $data;
1172
        }
1173
1174
        return $table;
1175
    }
1176
1177
    /**
1178
     * Gets the survey answers.
1179
     *
1180
     * @param int $sessionId
1181
     * @param int $courseId
1182
     * @param int $surveyId
1183
     * @param array options order and limit keys
1184
     *
1185
     * @todo fix the query
1186
     *
1187
     * @return array table with user name, lp name, progress
1188
     */
1189
    public static function get_survey_overview(
1190
        $sessionId,
1191
        $courseId,
1192
        $surveyId,
1193
        $date_from,
1194
        $date_to,
1195
        $options
1196
    ) {
1197
        //escaping vars
1198
        $sessionId = intval($sessionId);
1199
        $courseId = intval($courseId);
1200
        $surveyId = intval($surveyId);
1201
1202
        //tables
1203
        $session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
1204
        $user = Database::get_main_table(TABLE_MAIN_USER);
1205
        $c_survey = Database::get_course_table(TABLE_SURVEY);
1206
        $c_survey_answer = Database::get_course_table(TABLE_SURVEY_ANSWER);
1207
        $c_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
1208
        $c_survey_question_option = Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION);
1209
1210
        $course = api_get_course_info_by_id($courseId);
1211
1212
        $where = " WHERE c_id = '%s' AND s.status <> 2 AND session_id = %s";
1213
1214
        $limit = null;
1215
        if (!empty($options['limit'])) {
1216
            $limit = " LIMIT ".$options['limit'];
1217
        }
1218
1219
        if (!empty($options['where'])) {
1220
            $where .= ' '.$options['where'];
1221
        }
1222
1223
        $order = null;
1224
        if (!empty($options['order'])) {
1225
            $order = " ORDER BY ".$options['order'];
1226
        }
1227
1228
        $sql = "SELECT u.user_id, u.lastname, u.firstname, u.username, u.email, s.c_id
1229
                FROM $session_course_user s
1230
                INNER JOIN $user u ON u.user_id = s.user_id
1231
                $where $order $limit";
1232
1233
        $sql_query = sprintf($sql, intval($course['real_id']), $sessionId);
1234
        $rs = Database::query($sql_query);
1235
        while ($user = Database::fetch_array($rs)) {
1236
            $users[$user['user_id']] = $user;
1237
        }
1238
1239
        //Get survey questions
1240
        $questions = SurveyManager::get_questions($surveyId, $courseId);
1241
1242
        //Survey is anonymous?
1243
        $result = Database::query(sprintf("SELECT anonymous FROM $c_survey WHERE survey_id = %d", $surveyId));
1244
        $row = Database::fetch_array($result);
1245
        $anonymous = ($row['anonymous'] == 1) ? true : false;
1246
1247
        $table = [];
1248
        foreach ($users as $user) {
1249
            $data = [
1250
                'lastname' => ($anonymous ? '***' : $user[1]),
1251
                'firstname' => ($anonymous ? '***' : $user[2]),
1252
                'username' => ($anonymous ? '***' : $user[3]),
1253
            ];
1254
1255
            //Get questions by user
1256
            $sql = "SELECT sa.question_id, sa.option_id, sqo.option_text, sq.type
1257
                    FROM $c_survey_answer sa
1258
                    INNER JOIN $c_survey_question sq
1259
                    ON sq.question_id = sa.question_id
1260
                    LEFT JOIN $c_survey_question_option sqo
1261
                    ON
1262
                      sqo.c_id = sa.c_id AND
1263
                      sqo.question_id = sq.question_id AND
1264
                      sqo.question_option_id = sa.option_id AND
1265
                      sqo.survey_id = sq.survey_id
1266
                    WHERE
1267
                      sa.survey_id = %d AND
1268
                      sa.c_id = %d AND
1269
                      sa.user = %d
1270
            "; //. $where_survey;
1271
            $sql_query = sprintf($sql, $surveyId, $courseId, $user['user_id']);
1272
1273
            $result = Database::query($sql_query);
1274
1275
            $user_questions = [];
1276
            while ($row = Database::fetch_array($result)) {
1277
                $user_questions[$row['question_id']] = $row;
1278
            }
1279
1280
            //Match course lessons with user progress
1281
            foreach ($questions as $question_id => $question) {
1282
                $option_text = 'option_text';
1283
                if ($user_questions[$question_id]['type'] == 'open') {
1284
                    $option_text = 'option_id';
1285
                }
1286
                $data[$question_id] = $user_questions[$question_id][$option_text];
1287
            }
1288
1289
            $table[] = $data;
1290
        }
1291
1292
        return $table;
1293
    }
1294
1295
    /**
1296
     * Gets the progress of the given session.
1297
     *
1298
     * @param int $sessionId
1299
     * @param int $courseId
1300
     * @param array options order and limit keys
1301
     *
1302
     * @return array table with user name, lp name, progress
1303
     */
1304
    public static function get_session_progress(
1305
        $sessionId,
1306
        $courseId,
1307
        $date_from,
1308
        $date_to,
1309
        $options
1310
    ) {
1311
        $sessionId = (int) $sessionId;
1312
1313
        $getAllSessions = false;
1314
        if (empty($sessionId)) {
1315
            $sessionId = 0;
1316
            $getAllSessions = true;
1317
        }
1318
1319
        //tables
1320
        $session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
1321
        $user = Database::get_main_table(TABLE_MAIN_USER);
1322
        $workTable = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
1323
        $workTableAssignment = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
1324
        $tbl_course_lp = Database::get_course_table(TABLE_LP_MAIN);
1325
        $wiki = Database::get_course_table(TABLE_WIKI);
1326
        $table_stats_default = Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT);
1327
        $table_stats_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
1328
1329
        $course = api_get_course_info_by_id($courseId);
1330
        $where = " WHERE c_id = '%s' AND s.status <> 2 ";
1331
1332
        $limit = null;
1333
        if (!empty($options['limit'])) {
1334
            $limit = " LIMIT ".$options['limit'];
1335
        }
1336
1337
        if (!empty($options['where'])) {
1338
            $where .= ' '.$options['where'];
1339
        }
1340
1341
        $order = null;
1342
        if (!empty($options['order'])) {
1343
            $order = " ORDER BY ".$options['order'];
1344
        }
1345
1346
        //TODO, fix create report without session
1347
        $queryVariables = [$course['real_id']];
1348
        if (!empty($sessionId)) {
1349
            $where .= ' AND session_id = %s';
1350
            $queryVariables[] = $sessionId;
1351
            $sql = "SELECT
1352
                        u.user_id, u.lastname, u.firstname, u.username,
1353
                        u.email, s.c_id, s.session_id
1354
                    FROM $session_course_user s
1355
                    INNER JOIN $user u
1356
                    ON u.user_id = s.user_id
1357
                    $where $order $limit";
1358
        } else {
1359
            $sql = "SELECT
1360
                        u.user_id, u.lastname, u.firstname, u.username,
1361
                        u.email, s.c_id, s.session_id
1362
                    FROM $session_course_user s
1363
                    INNER JOIN $user u ON u.user_id = s.user_id
1364
                    $where $order $limit";
1365
        }
1366
1367
        $sql_query = vsprintf($sql, $queryVariables);
1368
        $rs = Database::query($sql_query);
1369
        while ($user = Database::fetch_array($rs)) {
1370
            $users[$user['user_id']] = $user;
1371
        }
1372
1373
        /**
1374
         *  Lessons.
1375
         */
1376
        $sql = "SELECT * FROM $tbl_course_lp WHERE c_id = %s "; //AND session_id = %s
1377
        $sql_query = sprintf($sql, $course['real_id']);
1378
        $result = Database::query($sql_query);
1379
        $arrLesson = [[]];
1380
        while ($row = Database::fetch_array($result)) {
1381
            if (empty($arrLesson[$row['session_id']]['lessons_total'])) {
1382
                $arrLesson[$row['session_id']]['lessons_total'] = 1;
1383
            } else {
1384
                $arrLesson[$row['session_id']]['lessons_total']++;
1385
            }
1386
        }
1387
1388
        /**
1389
         *  Exercises.
1390
         */
1391
        $exercises = ExerciseLib::get_all_exercises(
1392
            $course,
1393
            $sessionId,
1394
            false,
1395
            '',
1396
            $getAllSessions
1397
        );
1398
        $exercises_total = count($exercises);
1399
1400
        /**
1401
         *  Assignments.
1402
         */
1403
        //total
1404
        $params = [$course['real_id']];
1405
        if ($getAllSessions) {
1406
            $sql = "SELECT count(w.id) as count
1407
                    FROM $workTable w
1408
                    LEFT JOIN $workTableAssignment a
1409
                    ON (a.publication_id = w.id AND a.c_id = w.c_id)
1410
                    WHERE
1411
                        w.c_id = %s AND
1412
                        parent_id = 0 AND
1413
                        active IN (1, 0)";
1414
        } else {
1415
            $sql = "SELECT count(w.id) as count
1416
                    FROM $workTable w
1417
                    LEFT JOIN $workTableAssignment a
1418
                    ON (a.publication_id = w.id AND a.c_id = w.c_id)
1419
                    WHERE
1420
                        w.c_id = %s AND
1421
                        parent_id = 0 AND
1422
                        active IN (1, 0)";
1423
1424
            if (empty($sessionId)) {
1425
                $sql .= ' AND w.session_id = NULL ';
1426
            } else {
1427
                $sql .= ' AND w.session_id = %s ';
1428
                $params[] = $sessionId;
1429
            }
1430
        }
1431
1432
        $sql_query = vsprintf($sql, $params);
1433
        $result = Database::query($sql_query);
1434
        $row = Database::fetch_array($result);
1435
        $assignments_total = $row['count'];
1436
1437
        /**
1438
         * Wiki.
1439
         */
1440
        if ($getAllSessions) {
1441
            $sql = "SELECT count(distinct page_id)  as count FROM $wiki
1442
                    WHERE c_id = %s";
1443
        } else {
1444
            $sql = "SELECT count(distinct page_id)  as count FROM $wiki
1445
                    WHERE c_id = %s and session_id = %s";
1446
        }
1447
        $sql_query = sprintf($sql, $course['real_id'], $sessionId);
1448
        $result = Database::query($sql_query);
1449
        $row = Database::fetch_array($result);
1450
        $wiki_total = $row['count'];
1451
1452
        /**
1453
         * Surveys.
1454
         */
1455
        $survey_user_list = [];
1456
        $survey_list = SurveyManager::get_surveys($course['code'], $sessionId);
1457
1458
        $surveys_total = count($survey_list);
1459
        foreach ($survey_list as $survey) {
1460
            $user_list = SurveyManager::get_people_who_filled_survey(
1461
                $survey['survey_id'],
1462
                false,
1463
                $course['real_id']
1464
            );
1465
            foreach ($user_list as $user_id) {
1466
                isset($survey_user_list[$user_id]) ? $survey_user_list[$user_id]++ : $survey_user_list[$user_id] = 1;
1467
            }
1468
        }
1469
1470
        /**
1471
         * Forums.
1472
         */
1473
        $forums_total = CourseManager::getCountForum(
1474
            $course['real_id'],
1475
            $sessionId,
1476
            $getAllSessions
1477
        );
1478
1479
        //process table info
1480
        foreach ($users as $user) {
1481
            //Course description
1482
            $sql = "SELECT count(*) as count
1483
                    FROM $table_stats_access
1484
                    WHERE access_tool = 'course_description'
1485
                    AND c_id = '%s'
1486
                    AND access_session_id = %s
1487
                    AND access_user_id = %s ";
1488
            $sql_query = sprintf($sql, $course['real_id'], $user['id_session'], $user['user_id']);
1489
1490
            $result = Database::query($sql_query);
1491
            $row = Database::fetch_array($result);
1492
            $course_description_progress = ($row['count'] > 0) ? 100 : 0;
1493
1494
            if (!empty($arrLesson[$user['id_session']]['lessons_total'])) {
1495
                $lessons_total = $arrLesson[$user['id_session']]['lessons_total'];
1496
            } else {
1497
                $lessons_total = !empty($arrLesson[0]['lessons_total']) ? $arrLesson[0]['lessons_total'] : 0;
1498
            }
1499
1500
            //Lessons
1501
            //TODO: Lessons done and left is calculated by progress per item in lesson, maybe we should calculate it only per completed lesson?
1502
            $lessons_progress = Tracking::get_avg_student_progress(
1503
                $user['user_id'],
1504
                $course['code'],
1505
                [],
1506
                $user['id_session']
1507
            );
1508
            $lessons_done = ($lessons_progress * $lessons_total) / 100;
1509
            $lessons_left = $lessons_total - $lessons_done;
1510
1511
            // Exercises
1512
            $exercises_progress = str_replace(
1513
                '%',
1514
                '',
1515
                Tracking::get_exercise_student_progress(
1516
                    $exercises,
1517
                    $user['user_id'],
1518
                    $course['real_id'],
1519
                    $user['id_session']
1520
                )
1521
            );
1522
            $exercises_done = round(($exercises_progress * $exercises_total) / 100);
1523
            $exercises_left = $exercises_total - $exercises_done;
1524
1525
            //Assignments
1526
            $assignments_done = Tracking::count_student_assignments($user['user_id'], $course['code'], $user['id_session']);
1527
            $assignments_left = $assignments_total - $assignments_done;
1528
            if (!empty($assignments_total)) {
1529
                $assignments_progress = round((($assignments_done * 100) / $assignments_total), 2);
1530
            } else {
1531
                $assignments_progress = 0;
1532
            }
1533
1534
            // Wiki
1535
            // total revisions per user
1536
            $sql = "SELECT count(*) as count
1537
                    FROM $wiki
1538
                    WHERE c_id = %s and session_id = %s and user_id = %s";
1539
            $sql_query = sprintf($sql, $course['real_id'], $user['id_session'], $user['user_id']);
1540
            $result = Database::query($sql_query);
1541
            $row = Database::fetch_array($result);
1542
            $wiki_revisions = $row['count'];
1543
            //count visited wiki pages
1544
            $sql = "SELECT count(distinct default_value) as count
1545
                    FROM $table_stats_default
1546
                    WHERE
1547
                        default_user_id = %s AND
1548
                        default_event_type = 'wiki_page_view' AND
1549
                        default_value_type = 'wiki_page_id' AND
1550
                        c_id = %s
1551
                    ";
1552
            $sql_query = sprintf($sql, $user['user_id'], $course['real_id']);
1553
            $result = Database::query($sql_query);
1554
            $row = Database::fetch_array($result);
1555
1556
            $wiki_read = $row['count'];
1557
            $wiki_unread = $wiki_total - $wiki_read;
1558
            if (!empty($wiki_total)) {
1559
                $wiki_progress = round((($wiki_read * 100) / $wiki_total), 2);
1560
            } else {
1561
                $wiki_progress = 0;
1562
            }
1563
1564
            //Surveys
1565
            $surveys_done = (isset($survey_user_list[$user['user_id']]) ? $survey_user_list[$user['user_id']] : 0);
1566
            $surveys_left = $surveys_total - $surveys_done;
1567
            if (!empty($surveys_total)) {
1568
                $surveys_progress = round((($surveys_done * 100) / $surveys_total), 2);
1569
            } else {
1570
                $surveys_progress = 0;
1571
            }
1572
1573
            //Forums
1574
            $forums_done = CourseManager::getCountForumPerUser(
1575
                $user['user_id'],
1576
                $course['real_id'],
1577
                $user['id_session']
1578
            );
1579
            $forums_left = $forums_total - $forums_done;
1580
            if (!empty($forums_total)) {
1581
                $forums_progress = round((($forums_done * 100) / $forums_total), 2);
1582
            } else {
1583
                $forums_progress = 0;
1584
            }
1585
1586
            // Overall Total
1587
            $overall_total = ($course_description_progress + $exercises_progress + $forums_progress + $assignments_progress + $wiki_progress + $surveys_progress) / 6;
1588
1589
            $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>';
1590
            $linkForum = '<a href="'.api_get_path(WEB_CODE_PATH).'forum/index.php?cidReq='.$course['code'].'&id_session='.$user['id_session'].'"> %s </a>';
1591
            $linkWork = '<a href="'.api_get_path(WEB_CODE_PATH).'work/work.php?cidReq='.$course['code'].'&id_session='.$user['id_session'].'"> %s </a>';
1592
            $linkWiki = '<a href="'.api_get_path(WEB_CODE_PATH).'wiki/index.php?cidReq='.$course['code'].'&session_id='.$user['id_session'].'&action=statistics"> %s </a>';
1593
            $linkSurvey = '<a href="'.api_get_path(WEB_CODE_PATH).'survey/survey_list.php?cidReq='.$course['code'].'&id_session='.$user['id_session'].'"> %s </a>';
1594
1595
            $table[] = [
1596
                'lastname' => $user[1],
1597
                'firstname' => $user[2],
1598
                'username' => $user[3],
1599
                //'profile'   => '',
1600
                'total' => round($overall_total, 2).'%',
1601
                'courses' => sprintf($link, $course_description_progress.'%'),
1602
                'lessons' => sprintf($link, $lessons_progress.'%'),
1603
                'exercises' => sprintf($link, $exercises_progress.'%'),
1604
                'forums' => sprintf($link, $forums_progress.'%'),
1605
                'homeworks' => sprintf($link, $assignments_progress.'%'),
1606
                'wikis' => sprintf($link, $wiki_progress.'%'),
1607
                'surveys' => sprintf($link, $surveys_progress.'%'),
1608
                //course description
1609
                'course_description_progress' => $course_description_progress.'%',
1610
                //lessons
1611
                'lessons_total' => sprintf($link, $lessons_total),
1612
                'lessons_done' => sprintf($link, $lessons_done),
1613
                'lessons_left' => sprintf($link, $lessons_left),
1614
                'lessons_progress' => sprintf($link, $lessons_progress.'%'),
1615
                //exercises
1616
                'exercises_total' => sprintf($link, $exercises_total),
1617
                'exercises_done' => sprintf($link, $exercises_done),
1618
                'exercises_left' => sprintf($link, $exercises_left),
1619
                'exercises_progress' => sprintf($link, $exercises_progress.'%'),
1620
                //forums
1621
                'forums_total' => sprintf($linkForum, $forums_total),
1622
                'forums_done' => sprintf($linkForum, $forums_done),
1623
                'forums_left' => sprintf($linkForum, $forums_left),
1624
                'forums_progress' => sprintf($linkForum, $forums_progress.'%'),
1625
                //assignments
1626
                'assignments_total' => sprintf($linkWork, $assignments_total),
1627
                'assignments_done' => sprintf($linkWork, $assignments_done),
1628
                'assignments_left' => sprintf($linkWork, $assignments_left),
1629
                'assignments_progress' => sprintf($linkWork, $assignments_progress.'%'),
1630
                //wiki
1631
                'wiki_total' => sprintf($linkWiki, $wiki_total),
1632
                'wiki_revisions' => sprintf($linkWiki, $wiki_revisions),
1633
                'wiki_read' => sprintf($linkWiki, $wiki_read),
1634
                'wiki_unread' => sprintf($linkWiki, $wiki_unread),
1635
                'wiki_progress' => sprintf($linkWiki, $wiki_progress.'%'),
1636
                //survey
1637
                'surveys_total' => sprintf($linkSurvey, $surveys_total),
1638
                'surveys_done' => sprintf($linkSurvey, $surveys_done),
1639
                'surveys_left' => sprintf($linkSurvey, $surveys_left),
1640
                'surveys_progress' => sprintf($linkSurvey, $surveys_progress.'%'),
1641
            ];
1642
        }
1643
1644
        return $table;
1645
    }
1646
1647
    /**
1648
     * Get the ip, total of clicks, login date and time logged in for all user, in one session.
1649
     *
1650
     * @todo track_e_course_access table should have ip so we dont have to look for it in track_e_login
1651
     *
1652
     * @author César Perales <[email protected]>, Beeznest Team
1653
     *
1654
     * @version 1.9.6
1655
     */
1656
    public static function get_user_data_access_tracking_overview(
1657
        $sessionId,
1658
        $courseId,
1659
        $studentId = 0,
1660
        $profile = '',
1661
        $date_from = '',
1662
        $date_to = '',
1663
        $options = []
1664
    ) {
1665
        $sessionId = intval($sessionId);
1666
        $courseId = intval($courseId);
1667
        $studentId = intval($studentId);
1668
        $profile = intval($profile);
1669
        $date_from = Database::escape_string($date_from);
1670
        $date_to = Database::escape_string($date_to);
1671
1672
        // database table definition
1673
        $user = Database::get_main_table(TABLE_MAIN_USER);
1674
        $course = Database::get_main_table(TABLE_MAIN_COURSE);
1675
        $track_e_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
1676
        $track_e_course_access = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
1677
        $sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
1678
1679
        global $export_csv;
1680
        if ($export_csv) {
1681
            $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT);
1682
        } else {
1683
            $is_western_name_order = api_is_western_name_order();
1684
        }
1685
1686
        $where = null;
1687
        if (isset($sessionId) && !empty($sessionId)) {
1688
            $where = sprintf(" WHERE a.session_id = %d", $sessionId);
1689
        }
1690
        if (isset($courseId) && !empty($courseId)) {
1691
            $where .= sprintf(" AND c.id = %d", $courseId);
1692
        }
1693
        if (isset($studentId) && !empty($studentId)) {
1694
            $where .= sprintf(" AND u.user_id = %d", $studentId);
1695
        }
1696
        if (isset($profile) && !empty($profile)) {
1697
            $where .= sprintf(" AND u.status = %d", $profile);
1698
        }
1699
        if (!empty($date_to) && !empty($date_from)) {
1700
            $where .= sprintf(
1701
                " AND a.login_course_date >= '%s 00:00:00'
1702
                 AND a.login_course_date <= '%s 23:59:59'",
1703
                $date_from,
1704
                $date_to
1705
            );
1706
        }
1707
1708
        $limit = null;
1709
        if (!empty($options['limit'])) {
1710
            $limit = " LIMIT ".$options['limit'];
1711
        }
1712
1713
        if (!empty($options['where'])) {
1714
            $where .= ' '.$options['where'];
1715
        }
1716
1717
        $order = null;
1718
        if (!empty($options['order'])) {
1719
            $order = " ORDER BY ".$options['order'];
1720
        }
1721
1722
        //TODO add course name
1723
        $sql = "SELECT
1724
                a.login_course_date ,
1725
                u.username ,
1726
                ".($is_western_name_order ? "
1727
                    u.firstname,
1728
                    u.lastname,
1729
                    " : "
1730
                    u.lastname,
1731
                    u.firstname,
1732
                ")."
1733
                a.logout_course_date,
1734
                a.counter,
1735
                c.title,
1736
                c.code,
1737
                u.user_id,
1738
                a.session_id
1739
            FROM $track_e_course_access a
1740
            INNER JOIN $user u ON a.user_id = u.user_id
1741
            INNER JOIN $course c ON a.c_id = c.id
1742
            $where $order $limit";
1743
        $result = Database::query(sprintf($sql, $sessionId, $courseId));
1744
1745
        $data = [];
1746
        while ($user = Database::fetch_assoc($result)) {
1747
            $data[] = $user;
1748
        }
1749
1750
        foreach ($data as $key => $info) {
1751
            $sql = "SELECT
1752
                    name
1753
                    FROM $sessionTable
1754
                    WHERE
1755
                    id = {$info['session_id']}";
1756
            $result = Database::query($sql);
1757
            $session = Database::fetch_assoc($result);
1758
1759
            // building array to display
1760
            $return[] = [
1761
                'user_id' => $info['user_id'],
1762
                'logindate' => $info['login_course_date'],
1763
                'username' => $info['username'],
1764
                'firstname' => $info['firstname'],
1765
                'lastname' => $info['lastname'],
1766
                'clicks' => $info['counter'], //+ $clicks[$info['user_id']],
1767
                'ip' => '',
1768
                'timeLoggedIn' => gmdate("H:i:s", strtotime($info['logout_course_date']) - strtotime($info['login_course_date'])),
1769
                'session' => $session['name'],
1770
            ];
1771
        }
1772
1773
        foreach ($return as $key => $info) {
1774
            //Search for ip, we do less querys if we iterate the final array
1775
            $sql = sprintf(
1776
                "SELECT user_ip FROM $track_e_login WHERE login_user_id = %d AND login_date < '%s' ORDER BY login_date DESC LIMIT 1",
1777
                $info['user_id'],
1778
                $info['logindate']
1779
            ); //TODO add select by user too
1780
            $result = Database::query($sql);
1781
            $ip = Database::fetch_assoc($result);
1782
            //if no ip founded, we search the closest higher ip
1783
            if (empty($ip['user_ip'])) {
1784
                $sql = sprintf(
1785
                    "SELECT user_ip FROM $track_e_login WHERE login_user_id = %d AND login_date > '%s'  ORDER BY login_date ASC LIMIT 1",
1786
                    $info['user_id'],
1787
                    $info['logindate']
1788
                ); //TODO add select by user too
1789
                $result = Database::query($sql);
1790
                $ip = Database::fetch_assoc($result);
1791
            }
1792
            //add ip to final array
1793
            $return[$key]['ip'] = $ip['user_ip'];
1794
        }
1795
1796
        return $return;
1797
    }
1798
1799
    /**
1800
     * Creates a new course code based in given code.
1801
     *
1802
     * @param string $session_name
1803
     *                             <code>
1804
     *                             $wanted_code = 'curse' if there are in the DB codes like curse1 curse2 the function will return: course3
1805
     *                             if the course code doest not exist in the DB the same course code will be returned
1806
     *                             </code>
1807
     *
1808
     * @return string wanted unused code
1809
     */
1810
    public static function generateNextSessionName($session_name)
1811
    {
1812
        $session_name_ok = !self::sessionNameExists($session_name);
1813
        if (!$session_name_ok) {
1814
            $table = Database::get_main_table(TABLE_MAIN_SESSION);
1815
            $session_name = Database::escape_string($session_name);
1816
            $sql = "SELECT count(*) as count FROM $table
1817
                    WHERE name LIKE '$session_name%'";
1818
            $result = Database::query($sql);
1819
            if (Database::num_rows($result) > 0) {
1820
                $row = Database::fetch_array($result);
1821
                $count = $row['count'] + 1;
1822
                $session_name = $session_name.'_'.$count;
1823
                $result = self::sessionNameExists($session_name);
1824
                if (!$result) {
1825
                    return $session_name;
1826
                }
1827
            }
1828
1829
            return false;
1830
        }
1831
1832
        return $session_name;
1833
    }
1834
1835
    /**
1836
     * Edit a session.
1837
     *
1838
     * @author Carlos Vargas from existing code
1839
     *
1840
     * @param int    $id                           Session primary key
1841
     * @param string $name
1842
     * @param string $startDate
1843
     * @param string $endDate
1844
     * @param string $displayStartDate
1845
     * @param string $displayEndDate
1846
     * @param string $coachStartDate
1847
     * @param string $coachEndDate
1848
     * @param int    $coachId
1849
     * @param int    $sessionCategoryId
1850
     * @param int    $visibility
1851
     * @param string $description
1852
     * @param int    $showDescription
1853
     * @param int    $duration
1854
     * @param array  $extraFields
1855
     * @param int    $sessionAdminId
1856
     * @param bool   $sendSubscriptionNotification Optional. Whether send a mail notification to users being subscribed
1857
     * @param int    $status
1858
     *
1859
     * @return mixed
1860
     */
1861
    public static function edit_session(
1862
        $id,
1863
        $name,
1864
        $startDate,
1865
        $endDate,
1866
        $displayStartDate,
1867
        $displayEndDate,
1868
        $coachStartDate,
1869
        $coachEndDate,
1870
        $coachId,
1871
        $sessionCategoryId,
1872
        $visibility,
1873
        $description = null,
1874
        $showDescription = 0,
1875
        $duration = null,
1876
        $extraFields = [],
1877
        $sessionAdminId = 0,
1878
        $sendSubscriptionNotification = false,
1879
        $status = 0
1880
    ) {
1881
        $status = (int) $status;
1882
        $coachId = (int) $coachId;
1883
        $sessionCategoryId = (int) $sessionCategoryId;
1884
        $visibility = (int) $visibility;
1885
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
1886
1887
        if (empty($name)) {
1888
            Display::addFlash(
1889
                Display::return_message(get_lang('SessionNameIsRequired'), 'warning')
1890
            );
1891
1892
            return false;
1893
        } elseif (empty($coachId)) {
1894
            Display::addFlash(
1895
                Display::return_message(get_lang('CoachIsRequired'), 'warning')
1896
            );
1897
1898
            return false;
1899
        } elseif (!empty($startDate) &&
1900
            !api_is_valid_date($startDate, 'Y-m-d H:i') &&
1901
            !api_is_valid_date($startDate, 'Y-m-d H:i:s')
1902
        ) {
1903
            Display::addFlash(
1904
                Display::return_message(get_lang('InvalidStartDate'), 'warning')
1905
            );
1906
1907
            return false;
1908
        } elseif (!empty($endDate) &&
1909
            !api_is_valid_date($endDate, 'Y-m-d H:i') &&
1910
            !api_is_valid_date($endDate, 'Y-m-d H:i:s')
1911
        ) {
1912
            Display::addFlash(
1913
                Display::return_message(get_lang('InvalidEndDate'), 'warning')
1914
            );
1915
1916
            return false;
1917
        } elseif (!empty($startDate) && !empty($endDate) && $startDate >= $endDate) {
1918
            Display::addFlash(
1919
                Display::return_message(get_lang('StartDateShouldBeBeforeEndDate'), 'warning')
1920
            );
1921
1922
            return false;
1923
        } else {
1924
            $sessionInfo = self::get_session_by_name($name);
1925
            $exists = false;
1926
1927
            if (!empty($sessionInfo)) {
1928
                if ($sessionInfo['id'] != $id) {
1929
                    $exists = true;
1930
                }
1931
            }
1932
1933
            if ($exists) {
1934
                Display::addFlash(
1935
                    Display::return_message(get_lang('SessionNameAlreadyExists'), 'warning')
1936
                );
1937
1938
                return false;
1939
            } else {
1940
                $values = [
1941
                    'name' => $name,
1942
                    'duration' => $duration,
1943
                    'id_coach' => $coachId,
1944
                    'description' => $description,
1945
                    'show_description' => intval($showDescription),
1946
                    'visibility' => $visibility,
1947
                    'send_subscription_notification' => $sendSubscriptionNotification,
1948
                    'access_start_date' => null,
1949
                    'access_end_date' => null,
1950
                    'display_start_date' => null,
1951
                    'display_end_date' => null,
1952
                    'coach_access_start_date' => null,
1953
                    'coach_access_end_date' => null,
1954
                ];
1955
1956
                if (!empty($sessionAdminId)) {
1957
                    $values['session_admin_id'] = $sessionAdminId;
1958
                }
1959
1960
                if (!empty($startDate)) {
1961
                    $values['access_start_date'] = api_get_utc_datetime($startDate);
1962
                }
1963
1964
                if (!empty($endDate)) {
1965
                    $values['access_end_date'] = api_get_utc_datetime($endDate);
1966
                }
1967
1968
                if (!empty($displayStartDate)) {
1969
                    $values['display_start_date'] = api_get_utc_datetime($displayStartDate);
1970
                }
1971
1972
                if (!empty($displayEndDate)) {
1973
                    $values['display_end_date'] = api_get_utc_datetime($displayEndDate);
1974
                }
1975
1976
                if (!empty($coachStartDate)) {
1977
                    $values['coach_access_start_date'] = api_get_utc_datetime($coachStartDate);
1978
                }
1979
                if (!empty($coachEndDate)) {
1980
                    $values['coach_access_end_date'] = api_get_utc_datetime($coachEndDate);
1981
                }
1982
1983
                $values['session_category_id'] = null;
1984
                if (!empty($sessionCategoryId)) {
1985
                    $values['session_category_id'] = $sessionCategoryId;
1986
                }
1987
1988
                if (api_get_configuration_value('allow_session_status')) {
1989
                    $values['status'] = $status;
1990
                }
1991
1992
                Database::update(
1993
                    $tbl_session,
1994
                    $values,
1995
                    ['id = ?' => $id]
1996
                );
1997
1998
                if (!empty($extraFields)) {
1999
                    $extraFields['item_id'] = $id;
2000
                    $sessionFieldValue = new ExtraFieldValue('session');
2001
                    $sessionFieldValue->saveFieldValues($extraFields);
2002
                }
2003
2004
                return $id;
2005
            }
2006
        }
2007
    }
2008
2009
    /**
2010
     * Delete session.
2011
     *
2012
     * @author Carlos Vargas  from existing code
2013
     *
2014
     * @param array $id_checked an array to delete sessions
2015
     * @param bool  $from_ws    optional, true if the function is called
2016
     *                          by a webservice, false otherwise
2017
     *
2018
     * @return bool
2019
     * */
2020
    public static function delete($id_checked, $from_ws = false)
2021
    {
2022
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
2023
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
2024
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2025
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
2026
        $tbl_url_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
2027
        $tbl_item_properties = Database::get_course_table(TABLE_ITEM_PROPERTY);
2028
        $tbl_student_publication = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
2029
        $tbl_student_publication_assignment = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
2030
        $userGroupSessionTable = Database::get_main_table(TABLE_USERGROUP_REL_SESSION);
2031
        $trackCourseAccess = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
2032
        $trackAccess = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS);
2033
2034
        $ticket = Database::get_main_table(TABLE_TICKET_TICKET);
2035
        $em = Database::getManager();
2036
        $userId = api_get_user_id();
2037
2038
        /** @var SequenceRepository $repo */
2039
        $repo = Database::getManager()->getRepository('ChamiloCoreBundle:SequenceResource');
2040
        $sequenceResource = $repo->findRequirementForResource(
2041
            $id_checked,
2042
            SequenceResource::SESSION_TYPE
2043
        );
2044
2045
        if ($sequenceResource) {
2046
            Display::addFlash(
2047
                Display::return_message(
2048
                    get_lang('ThereIsASequenceResourceLinkedToThisSessionYouNeedToDeleteItFirst'),
2049
                    'error'
2050
                )
2051
            );
2052
2053
            return false;
2054
        }
2055
2056
        if (is_array($id_checked)) {
2057
            foreach ($id_checked as $sessionId) {
2058
                self::delete($sessionId);
2059
            }
2060
        } else {
2061
            $id_checked = intval($id_checked);
2062
        }
2063
2064
        if (self::allowed($id_checked) && !$from_ws) {
2065
            $qb = $em
2066
                ->createQuery('
2067
                    SELECT s.sessionAdminId FROM ChamiloCoreBundle:Session s
2068
                    WHERE s.id = ?1
2069
                ')
2070
                ->setParameter(1, $id_checked);
2071
2072
            $res = $qb->getSingleScalarResult();
2073
2074
            if ($res != $userId && !api_is_platform_admin()) {
2075
                api_not_allowed(true);
2076
            }
2077
        }
2078
2079
        $sessionInfo = api_get_session_info($id_checked);
2080
2081
        // Delete documents inside a session
2082
        $courses = self::getCoursesInSession($id_checked);
2083
        foreach ($courses as $courseId) {
2084
            $courseInfo = api_get_course_info_by_id($courseId);
2085
            DocumentManager::deleteDocumentsFromSession($courseInfo, $id_checked);
2086
            $works = Database::select(
2087
                '*',
2088
                $tbl_student_publication,
2089
                [
2090
                    'where' => ['session_id = ? AND c_id = ?' => [$id_checked, $courseId]],
2091
                ]
2092
            );
2093
2094
            $currentCourseRepositorySys = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/';
2095
            foreach ($works as $index => $work) {
2096
                if ($work['filetype'] = 'folder') {
2097
                    Database::query("DELETE FROM $tbl_student_publication_assignment WHERE publication_id = $index");
2098
                }
2099
                my_delete($currentCourseRepositorySys.'/'.$work['url']);
2100
            }
2101
        }
2102
2103
        // Class
2104
        $sql = "DELETE FROM $userGroupSessionTable
2105
                WHERE session_id IN($id_checked)";
2106
        Database::query($sql);
2107
2108
        Database::query("DELETE FROM $tbl_student_publication WHERE session_id IN($id_checked)");
2109
        Database::query("DELETE FROM $tbl_session_rel_course WHERE session_id IN($id_checked)");
2110
        Database::query("DELETE FROM $tbl_session_rel_course_rel_user WHERE session_id IN($id_checked)");
2111
        Database::query("DELETE FROM $tbl_session_rel_user WHERE session_id IN($id_checked)");
2112
        Database::query("DELETE FROM $tbl_item_properties WHERE session_id IN ($id_checked)");
2113
        Database::query("DELETE FROM $tbl_url_session WHERE session_id IN($id_checked)");
2114
2115
        Database::query("DELETE FROM $trackCourseAccess WHERE session_id IN($id_checked)");
2116
        Database::query("DELETE FROM $trackAccess WHERE access_session_id IN($id_checked)");
2117
2118
        $sql = "UPDATE $ticket SET session_id = NULL WHERE session_id IN ($id_checked)";
2119
        Database::query($sql);
2120
2121
        $app_plugin = new AppPlugin();
2122
        $app_plugin->performActionsWhenDeletingItem('session', $id_checked);
2123
2124
        $sql = "DELETE FROM $tbl_session WHERE id IN ($id_checked)";
2125
        Database::query($sql);
2126
2127
        $extraFieldValue = new ExtraFieldValue('session');
2128
        $extraFieldValue->deleteValuesByItem($id_checked);
2129
2130
        $repo->deleteResource(
2131
            $id_checked,
2132
            SequenceResource::SESSION_TYPE
2133
        );
2134
2135
        // Add event to system log
2136
        Event::addEvent(
2137
            LOG_SESSION_DELETE,
2138
            LOG_SESSION_ID,
2139
            $sessionInfo['name'].' - id:'.$id_checked,
2140
            api_get_utc_datetime(),
2141
            $userId
2142
        );
2143
2144
        return true;
2145
    }
2146
2147
    /**
2148
     * @param int $id promotion id
2149
     *
2150
     * @return bool
2151
     */
2152
    public static function clear_session_ref_promotion($id)
2153
    {
2154
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
2155
        $id = intval($id);
2156
        $sql = "UPDATE $tbl_session
2157
                SET promotion_id = 0
2158
                WHERE promotion_id = $id";
2159
        if (Database::query($sql)) {
2160
            return true;
2161
        } else {
2162
            return false;
2163
        }
2164
    }
2165
2166
    /**
2167
     * Subscribes students to the given session and optionally (default)
2168
     * unsubscribes previous users.
2169
     *
2170
     * @author Carlos Vargas from existing code
2171
     * @author Julio Montoya. Cleaning code.
2172
     *
2173
     * @param int   $sessionId
2174
     * @param array $userList
2175
     * @param int   $session_visibility
2176
     * @param bool  $empty_users
2177
     * @param bool  $registerUsersToAllCourses
2178
     *
2179
     * @return bool
2180
     */
2181
    public static function subscribeUsersToSession(
2182
        $sessionId,
2183
        $userList,
2184
        $session_visibility = SESSION_VISIBLE_READ_ONLY,
2185
        $empty_users = true,
2186
        $registerUsersToAllCourses = true
2187
    ) {
2188
        $sessionId = (int) $sessionId;
2189
2190
        if (empty($sessionId)) {
2191
            return false;
2192
        }
2193
2194
        foreach ($userList as $intUser) {
2195
            if ($intUser != strval(intval($intUser))) {
2196
                return false;
2197
            }
2198
        }
2199
2200
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
2201
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2202
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
2203
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
2204
2205
        $session = api_get_session_entity($sessionId);
2206
2207
        // from function parameter
2208
        if (empty($session_visibility)) {
2209
            $session_visibility = $session->getVisibility();
2210
            //default status loaded if empty
2211
            // by default readonly 1
2212
            if (empty($session_visibility)) {
2213
                $session_visibility = SESSION_VISIBLE_READ_ONLY;
2214
            }
2215
        } else {
2216
            if (!in_array($session_visibility, [SESSION_VISIBLE_READ_ONLY, SESSION_VISIBLE, SESSION_INVISIBLE])) {
2217
                $session_visibility = SESSION_VISIBLE_READ_ONLY;
2218
            }
2219
        }
2220
2221
        $sql = "SELECT user_id FROM $tbl_session_rel_course_rel_user
2222
                WHERE session_id = $sessionId AND status = 0";
2223
        $result = Database::query($sql);
2224
        $existingUsers = [];
2225
        while ($row = Database::fetch_array($result)) {
2226
            $existingUsers[] = $row['user_id'];
2227
        }
2228
2229
        $sql = "SELECT c_id FROM $tbl_session_rel_course
2230
                WHERE session_id = $sessionId";
2231
        $result = Database::query($sql);
2232
        $course_list = [];
2233
        while ($row = Database::fetch_array($result)) {
2234
            $course_list[] = $row['c_id'];
2235
        }
2236
2237
        if ($session->getSendSubscriptionNotification() &&
2238
            is_array($userList)
2239
        ) {
2240
            // Sending emails only
2241
            foreach ($userList as $user_id) {
2242
                if (in_array($user_id, $existingUsers)) {
2243
                    continue;
2244
                }
2245
2246
                $tplSubject = new Template(
2247
                    null,
2248
                    false,
2249
                    false,
2250
                    false,
2251
                    false,
2252
                    false
2253
                );
2254
                $layoutSubject = $tplSubject->get_template(
2255
                    'mail/subject_subscription_to_session_confirmation.tpl'
2256
                );
2257
                $subject = $tplSubject->fetch($layoutSubject);
2258
                $user_info = api_get_user_info($user_id);
2259
2260
                $tplContent = new Template(
2261
                    null,
2262
                    false,
2263
                    false,
2264
                    false,
2265
                    false,
2266
                    false
2267
                );
2268
                // Variables for default template
2269
                $tplContent->assign('complete_name', stripslashes($user_info['complete_name']));
2270
                $tplContent->assign('session_name', $session->getName());
2271
                $tplContent->assign('session_coach', $session->getGeneralCoach()->getCompleteName());
2272
                $layoutContent = $tplContent->get_template(
2273
                    'mail/content_subscription_to_session_confirmation.tpl'
2274
                );
2275
                $content = $tplContent->fetch($layoutContent);
2276
2277
                api_mail_html(
2278
                    $user_info['complete_name'],
2279
                    $user_info['mail'],
2280
                    $subject,
2281
                    $content,
2282
                    api_get_person_name(
2283
                        api_get_setting('administratorName'),
2284
                        api_get_setting('administratorSurname')
2285
                    ),
2286
                    api_get_setting('emailAdministrator')
2287
                );
2288
            }
2289
        }
2290
2291
        if ($registerUsersToAllCourses) {
2292
            foreach ($course_list as $courseId) {
2293
                // for each course in the session
2294
                $nbr_users = 0;
2295
                $courseId = (int) $courseId;
2296
2297
                $sql = "SELECT DISTINCT user_id
2298
                        FROM $tbl_session_rel_course_rel_user
2299
                        WHERE
2300
                            session_id = $sessionId AND
2301
                            c_id = $courseId AND
2302
                            status = 0
2303
                        ";
2304
                $result = Database::query($sql);
2305
                $existingUsers = [];
2306
                while ($row = Database::fetch_array($result)) {
2307
                    $existingUsers[] = $row['user_id'];
2308
                }
2309
2310
                // Delete existing users
2311
                if ($empty_users) {
2312
                    foreach ($existingUsers as $existing_user) {
2313
                        if (!in_array($existing_user, $userList)) {
2314
                            self::unSubscribeUserFromCourseSession($existing_user, $courseId, $sessionId);
2315
                        }
2316
                    }
2317
                }
2318
2319
                // Replace with this new function
2320
                // insert new users into session_rel_course_rel_user and ignore if they already exist
2321
                foreach ($userList as $enreg_user) {
2322
                    if (!in_array($enreg_user, $existingUsers)) {
2323
                        $status = self::get_user_status_in_course_session(
2324
                            $enreg_user,
2325
                            $courseId,
2326
                            $sessionId
2327
                        );
2328
2329
                        // Avoid duplicate entries.
2330
                        if ($status === false || ($status !== false && $status != 0)) {
2331
                            $enreg_user = (int) $enreg_user;
2332
                            $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user (session_id, c_id, user_id, visibility, status)
2333
                                    VALUES($sessionId, $courseId, $enreg_user, $session_visibility, 0)";
2334
                            $result = Database::query($sql);
2335
                            if (Database::affected_rows($result)) {
2336
                                $nbr_users++;
2337
                            }
2338
2339
                            Event::addEvent(
2340
                                LOG_SESSION_ADD_USER_COURSE,
2341
                                LOG_USER_ID,
2342
                                $enreg_user,
2343
                                api_get_utc_datetime(),
2344
                                api_get_user_id(),
2345
                                $courseId,
2346
                                $sessionId
2347
                            );
2348
                        }
2349
                    }
2350
                }
2351
2352
                // Count users in this session-course relation
2353
                $sql = "SELECT COUNT(user_id) as nbUsers
2354
                        FROM $tbl_session_rel_course_rel_user
2355
                        WHERE session_id = $sessionId AND c_id = $courseId AND status<>2";
2356
                $rs = Database::query($sql);
2357
                list($nbr_users) = Database::fetch_array($rs);
2358
                // update the session-course relation to add the users total
2359
                $sql = "UPDATE $tbl_session_rel_course SET nbr_users = $nbr_users
2360
                        WHERE session_id = $sessionId AND c_id = $courseId";
2361
                Database::query($sql);
2362
            }
2363
        }
2364
2365
        // Delete users from the session
2366
        if ($empty_users === true) {
2367
            $sql = "DELETE FROM $tbl_session_rel_user
2368
                    WHERE
2369
                      session_id = $sessionId AND
2370
                      relation_type <> ".SESSION_RELATION_TYPE_RRHH;
2371
            // Don't reset session_rel_user.registered_at of users that will be registered later anyways.
2372
            if (!empty($userList)) {
2373
                $avoidDeleteThisUsers = " AND user_id NOT IN ('".implode("','", $userList)."')";
2374
                $sql .= $avoidDeleteThisUsers;
2375
            }
2376
            Event::addEvent(
2377
                LOG_SESSION_DELETE_USER,
2378
                LOG_USER_ID,
2379
                'all',
2380
                api_get_utc_datetime(),
2381
                api_get_user_id(),
2382
                null,
2383
                $sessionId
2384
            );
2385
            Database::query($sql);
2386
        }
2387
2388
        // Insert missing users into session
2389
        foreach ($userList as $enreg_user) {
2390
            $isUserSubscribed = self::isUserSubscribedAsStudent($sessionId, $enreg_user);
2391
            if ($isUserSubscribed === false) {
2392
                $enreg_user = (int) $enreg_user;
2393
                $sql = "INSERT IGNORE INTO $tbl_session_rel_user (relation_type, session_id, user_id, registered_at)
2394
                        VALUES (0, $sessionId, $enreg_user, '".api_get_utc_datetime()."')";
2395
                Database::query($sql);
2396
                Event::addEvent(
2397
                    LOG_SESSION_ADD_USER,
2398
                    LOG_USER_ID,
2399
                    $enreg_user,
2400
                    api_get_utc_datetime(),
2401
                    api_get_user_id(),
2402
                    null,
2403
                    $sessionId
2404
                );
2405
            }
2406
        }
2407
2408
        // update number of users in the session
2409
        $sql = "UPDATE $tbl_session
2410
                SET nbr_users = (SELECT count(user_id) FROM $tbl_session_rel_user WHERE session_id = $sessionId)
2411
                WHERE id = $sessionId";
2412
        Database::query($sql);
2413
2414
        return true;
2415
    }
2416
2417
    /**
2418
     * Returns user list of the current users subscribed in the course-session.
2419
     *
2420
     * @param int   $sessionId
2421
     * @param array $courseInfo
2422
     * @param int   $status
2423
     *
2424
     * @return array
2425
     */
2426
    public static function getUsersByCourseSession(
2427
        $sessionId,
2428
        $courseInfo,
2429
        $status = null
2430
    ) {
2431
        $sessionId = (int) $sessionId;
2432
        $courseId = $courseInfo['real_id'];
2433
2434
        if (empty($sessionId) || empty($courseId)) {
2435
            return [];
2436
        }
2437
2438
        $statusCondition = null;
2439
        if (isset($status) && !is_null($status)) {
2440
            $status = (int) $status;
2441
            $statusCondition = " AND status = $status";
2442
        }
2443
2444
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2445
2446
        $sql = "SELECT DISTINCT user_id
2447
                FROM $table
2448
                WHERE
2449
                    session_id = $sessionId AND
2450
                    c_id = $courseId
2451
                    $statusCondition
2452
                ";
2453
2454
        $result = Database::query($sql);
2455
        $existingUsers = [];
2456
        while ($row = Database::fetch_array($result)) {
2457
            $existingUsers[] = $row['user_id'];
2458
        }
2459
2460
        return $existingUsers;
2461
    }
2462
2463
    /**
2464
     * Returns user list of the current users subscribed in the course-session.
2465
     *
2466
     * @param array $sessionList
2467
     * @param array $courseList
2468
     * @param int   $status
2469
     * @param int   $start
2470
     * @param int   $limit
2471
     *
2472
     * @return array
2473
     */
2474
    public static function getUsersByCourseAndSessionList(
2475
        $sessionList,
2476
        $courseList,
2477
        $status = null,
2478
        $start = null,
2479
        $limit = null
2480
    ) {
2481
        if (empty($sessionList) || empty($courseList)) {
2482
            return [];
2483
        }
2484
        $sessionListToString = implode("','", $sessionList);
2485
        $courseListToString = implode("','", $courseList);
2486
2487
        $statusCondition = null;
2488
        if (isset($status) && !is_null($status)) {
2489
            $status = (int) $status;
2490
            $statusCondition = " AND status = $status";
2491
        }
2492
2493
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2494
2495
        $sql = "SELECT DISTINCT user_id
2496
                FROM $table
2497
                WHERE
2498
                    session_id IN ('$sessionListToString') AND
2499
                    c_id IN ('$courseListToString')
2500
                    $statusCondition
2501
                ";
2502
        if (!is_null($start) && !is_null($limit)) {
2503
            $start = (int) $start;
2504
            $limit = (int) $limit;
2505
            $sql .= "LIMIT $start, $limit";
2506
        }
2507
        $result = Database::query($sql);
2508
        $existingUsers = [];
2509
        while ($row = Database::fetch_array($result)) {
2510
            $existingUsers[] = $row['user_id'];
2511
        }
2512
2513
        return $existingUsers;
2514
    }
2515
2516
    /**
2517
     * Remove a list of users from a course-session.
2518
     *
2519
     * @param array $userList
2520
     * @param int   $sessionId
2521
     * @param array $courseInfo
2522
     * @param int   $status
2523
     * @param bool  $updateTotal
2524
     *
2525
     * @return bool
2526
     */
2527
    public static function removeUsersFromCourseSession(
2528
        $userList,
2529
        $sessionId,
2530
        $courseInfo,
2531
        $status = null,
2532
        $updateTotal = true
2533
    ) {
2534
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2535
        $tableSessionCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
2536
        $sessionId = (int) $sessionId;
2537
2538
        if (empty($sessionId) || empty($userList) || empty($courseInfo)) {
2539
            return false;
2540
        }
2541
2542
        is_array($courseInfo) ? $courseId = $courseInfo['real_id'] : $courseId = $courseInfo;
2543
2544
        $statusCondition = null;
2545
        if (isset($status) && !is_null($status)) {
2546
            $status = (int) $status;
2547
            $statusCondition = " AND status = $status";
2548
        }
2549
2550
        foreach ($userList as $userId) {
2551
            $userId = (int) $userId;
2552
            $sql = "DELETE FROM $table
2553
                    WHERE
2554
                        session_id = $sessionId AND
2555
                        c_id = $courseId AND
2556
                        user_id = $userId
2557
                        $statusCondition
2558
                    ";
2559
            Database::query($sql);
2560
2561
            Event::addEvent(
2562
                LOG_SESSION_DELETE_USER_COURSE,
2563
                LOG_USER_ID,
2564
                $userId,
2565
                api_get_utc_datetime(),
2566
                api_get_user_id(),
2567
                $courseId,
2568
                $sessionId
2569
            );
2570
        }
2571
2572
        if ($updateTotal) {
2573
            // Count users in this session-course relation
2574
            $sql = "SELECT COUNT(user_id) as nbUsers
2575
                    FROM $table
2576
                    WHERE
2577
                        session_id = $sessionId AND
2578
                        c_id = $courseId AND
2579
                        status <> 2";
2580
            $result = Database::query($sql);
2581
            list($userCount) = Database::fetch_array($result);
2582
2583
            // update the session-course relation to add the users total
2584
            $sql = "UPDATE $tableSessionCourse
2585
                    SET nbr_users = $userCount
2586
                    WHERE
2587
                        session_id = $sessionId AND
2588
                        c_id = $courseId";
2589
            Database::query($sql);
2590
        }
2591
    }
2592
2593
    /**
2594
     * Subscribe a user to an specific course inside a session.
2595
     *
2596
     * @param array  $user_list
2597
     * @param int    $session_id
2598
     * @param string $course_code
2599
     * @param int    $session_visibility
2600
     * @param bool   $removeUsersNotInList
2601
     *
2602
     * @return bool
2603
     */
2604
    public static function subscribe_users_to_session_course(
2605
        $user_list,
2606
        $session_id,
2607
        $course_code,
2608
        $session_visibility = SESSION_VISIBLE_READ_ONLY,
2609
        $removeUsersNotInList = false
2610
    ) {
2611
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
2612
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
2613
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2614
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
2615
2616
        if (empty($session_id) || empty($course_code)) {
2617
            return false;
2618
        }
2619
2620
        $session_id = (int) $session_id;
2621
        $session_visibility = (int) $session_visibility;
2622
        $course_code = Database::escape_string($course_code);
2623
        $courseInfo = api_get_course_info($course_code);
2624
        $courseId = $courseInfo['real_id'];
2625
        $subscribe = (int) api_get_course_setting('subscribe_users_to_forum_notifications', $course_code);
2626
        $forums = [];
2627
        if ($subscribe === 1) {
2628
            require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
2629
            $forums = get_forums(0, $course_code, true, $session_id);
2630
        }
2631
2632
        if ($removeUsersNotInList) {
2633
            $currentUsers = self::getUsersByCourseSession($session_id, $courseInfo, 0);
2634
2635
            if (!empty($user_list)) {
2636
                $userToDelete = array_diff($currentUsers, $user_list);
2637
            } else {
2638
                $userToDelete = $currentUsers;
2639
            }
2640
2641
            if (!empty($userToDelete)) {
2642
                self::removeUsersFromCourseSession(
2643
                    $userToDelete,
2644
                    $session_id,
2645
                    $courseInfo,
2646
                    0,
2647
                    true
2648
                );
2649
            }
2650
        }
2651
2652
        $nbr_users = 0;
2653
        foreach ($user_list as $enreg_user) {
2654
            $enreg_user = (int) $enreg_user;
2655
            // Checking if user exists in session - course - user table.
2656
            $sql = "SELECT count(user_id) as count
2657
                    FROM $tbl_session_rel_course_rel_user
2658
                    WHERE
2659
                        session_id = $session_id AND
2660
                        c_id = $courseId and
2661
                        user_id = $enreg_user ";
2662
            $result = Database::query($sql);
2663
            $count = 0;
2664
2665
            if (Database::num_rows($result) > 0) {
2666
                $row = Database::fetch_array($result, 'ASSOC');
2667
                $count = $row['count'];
2668
            }
2669
2670
            if ($count == 0) {
2671
                $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user (session_id, c_id, user_id, visibility)
2672
                        VALUES ($session_id, $courseId, $enreg_user, $session_visibility)";
2673
                $result = Database::query($sql);
2674
                if (Database::affected_rows($result)) {
2675
                    $nbr_users++;
2676
                }
2677
            }
2678
2679
            if (!empty($forums)) {
2680
                $userInfo = api_get_user_info($enreg_user);
2681
                foreach ($forums as $forum) {
2682
                    $forumId = $forum['iid'];
2683
                    set_notification('forum', $forumId, false, $userInfo, $courseInfo);
2684
                }
2685
            }
2686
2687
            // Checking if user exists in session - user table.
2688
            $sql = "SELECT count(user_id) as count
2689
                    FROM $tbl_session_rel_user
2690
                    WHERE session_id = $session_id AND user_id = $enreg_user ";
2691
            $result = Database::query($sql);
2692
            $count = 0;
2693
2694
            if (Database::num_rows($result) > 0) {
2695
                $row = Database::fetch_array($result, 'ASSOC');
2696
                $count = $row['count'];
2697
            }
2698
2699
            if (empty($count)) {
2700
                // If user is not registered to a session then add it.
2701
                $sql = "INSERT IGNORE INTO $tbl_session_rel_user (session_id, user_id, registered_at)
2702
                        VALUES ($session_id, $enreg_user, '".api_get_utc_datetime()."')";
2703
                Database::query($sql);
2704
2705
                $sql = "UPDATE $tbl_session SET nbr_users = nbr_users + 1
2706
                        WHERE id = $session_id ";
2707
                Database::query($sql);
2708
            }
2709
        }
2710
2711
        // count users in this session-course relation
2712
        $sql = "SELECT COUNT(user_id) as nbUsers
2713
                FROM $tbl_session_rel_course_rel_user
2714
                WHERE session_id = $session_id AND c_id = $courseId AND status <> 2";
2715
        $rs = Database::query($sql);
2716
        list($nbr_users) = Database::fetch_array($rs);
2717
        // update the session-course relation to add the users total
2718
        $sql = "UPDATE $tbl_session_rel_course
2719
                SET nbr_users = $nbr_users
2720
                WHERE session_id = $session_id AND c_id = $courseId";
2721
        Database::query($sql);
2722
    }
2723
2724
    /**
2725
     * Unsubscribe user from session.
2726
     *
2727
     * @param int Session id
2728
     * @param int User id
2729
     *
2730
     * @return bool True in case of success, false in case of error
2731
     */
2732
    public static function unsubscribe_user_from_session($session_id, $user_id)
2733
    {
2734
        $session_id = (int) $session_id;
2735
        $user_id = (int) $user_id;
2736
2737
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
2738
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
2739
2740
        $sql = "DELETE FROM $tbl_session_rel_user
2741
                WHERE
2742
                    session_id = $session_id AND
2743
                    user_id = $user_id AND
2744
                    relation_type <> ".SESSION_RELATION_TYPE_RRHH;
2745
        $result = Database::query($sql);
2746
        $return = Database::affected_rows($result);
2747
2748
        // Update number of users
2749
        $sql = "UPDATE $tbl_session
2750
                SET nbr_users = nbr_users - $return
2751
                WHERE id = $session_id ";
2752
        Database::query($sql);
2753
2754
        Event::addEvent(
2755
            LOG_SESSION_DELETE_USER,
2756
            LOG_USER_ID,
2757
            $user_id,
2758
            api_get_utc_datetime(),
2759
            api_get_user_id(),
2760
            null,
2761
            $session_id
2762
        );
2763
2764
        // Get the list of courses related to this session
2765
        $course_list = self::get_course_list_by_session_id($session_id);
2766
        if (!empty($course_list)) {
2767
            foreach ($course_list as $course) {
2768
                self::unSubscribeUserFromCourseSession($user_id, $course['id'], $session_id);
2769
            }
2770
        }
2771
2772
        return true;
2773
    }
2774
2775
    /**
2776
     * @param int $user_id
2777
     * @param int $courseId
2778
     * @param int $session_id
2779
     */
2780
    public static function unSubscribeUserFromCourseSession($user_id, $courseId, $session_id)
2781
    {
2782
        $user_id = (int) $user_id;
2783
        $courseId = (int) $courseId;
2784
        $session_id = (int) $session_id;
2785
2786
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2787
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
2788
2789
        // Delete user from course
2790
        $sql = "DELETE FROM $tbl_session_rel_course_rel_user
2791
                WHERE session_id = $session_id AND c_id = $courseId AND user_id = $user_id";
2792
        $result = Database::query($sql);
2793
2794
        if (Database::affected_rows($result)) {
2795
            // Update number of users in this relation
2796
            $sql = "UPDATE $tbl_session_rel_course SET
2797
                    nbr_users = nbr_users - 1
2798
                    WHERE session_id = $session_id AND c_id = $courseId";
2799
            Database::query($sql);
2800
        }
2801
2802
        Event::addEvent(
2803
            LOG_SESSION_DELETE_USER_COURSE,
2804
            LOG_USER_ID,
2805
            $user_id,
2806
            api_get_utc_datetime(),
2807
            api_get_user_id(),
2808
            $courseId,
2809
            $session_id
2810
        );
2811
    }
2812
2813
    /**
2814
     * Subscribes courses to the given session and optionally (default)
2815
     * unsubscribe previous users.
2816
     *
2817
     * @author Carlos Vargas from existing code
2818
     *
2819
     * @param int   $sessionId
2820
     * @param array $courseList                     List of courses int ids
2821
     * @param bool  $removeExistingCoursesWithUsers Whether to unsubscribe
2822
     *                                              existing courses and users (true, default) or not (false)
2823
     * @param bool  $copyEvaluation                 from base course to session course
2824
     * @param bool  $copyCourseTeachersAsCoach
2825
     *
2826
     * @throws Exception
2827
     *
2828
     * @return bool False on failure, true otherwise
2829
     * */
2830
    public static function add_courses_to_session(
2831
        $sessionId,
2832
        $courseList,
2833
        $removeExistingCoursesWithUsers = true,
2834
        $copyEvaluation = false,
2835
        $copyCourseTeachersAsCoach = false
2836
    ) {
2837
        $sessionId = (int) $sessionId;
2838
2839
        if (empty($sessionId) || empty($courseList)) {
2840
            return false;
2841
        }
2842
2843
        $em = Database::getManager();
2844
2845
        /** @var Session $session */
2846
        $session = $em->find('ChamiloCoreBundle:Session', $sessionId);
2847
2848
        if (!$session) {
2849
            return false;
2850
        }
2851
        $sessionVisibility = $session->getVisibility();
2852
2853
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
2854
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
2855
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
2856
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
2857
2858
        // Get list of courses subscribed to this session
2859
        $sql = "SELECT c_id
2860
                FROM $tbl_session_rel_course
2861
                WHERE session_id = $sessionId";
2862
        $rs = Database::query($sql);
2863
        $existingCourses = Database::store_result($rs);
2864
        $nbr_courses = count($existingCourses);
2865
2866
        // Get list of users subscribed to this session
2867
        $sql = "SELECT user_id
2868
                FROM $tbl_session_rel_user
2869
                WHERE
2870
                    session_id = $sessionId AND
2871
                    relation_type<>".SESSION_RELATION_TYPE_RRHH;
2872
        $result = Database::query($sql);
2873
        $user_list = Database::store_result($result);
2874
2875
        // Remove existing courses from the session.
2876
        if ($removeExistingCoursesWithUsers === true && !empty($existingCourses)) {
2877
            foreach ($existingCourses as $existingCourse) {
2878
                if (!in_array($existingCourse['c_id'], $courseList)) {
2879
                    $sql = "DELETE FROM $tbl_session_rel_course
2880
                            WHERE
2881
                                c_id = ".$existingCourse['c_id']." AND
2882
                                session_id = $sessionId";
2883
                    Database::query($sql);
2884
2885
                    $sql = "DELETE FROM $tbl_session_rel_course_rel_user
2886
                            WHERE
2887
                                c_id = ".$existingCourse['c_id']." AND
2888
                                session_id = $sessionId";
2889
                    Database::query($sql);
2890
2891
                    Event::addEvent(
2892
                        LOG_SESSION_DELETE_COURSE,
2893
                        LOG_COURSE_ID,
2894
                        $existingCourse['c_id'],
2895
                        api_get_utc_datetime(),
2896
                        api_get_user_id(),
2897
                        $existingCourse['c_id'],
2898
                        $sessionId
2899
                    );
2900
2901
                    CourseManager::remove_course_ranking(
2902
                        $existingCourse['c_id'],
2903
                        $sessionId
2904
                    );
2905
                    $nbr_courses--;
2906
                }
2907
            }
2908
        }
2909
2910
        // Pass through the courses list we want to add to the session
2911
        foreach ($courseList as $courseId) {
2912
            $courseInfo = api_get_course_info_by_id($courseId);
2913
2914
            // If course doesn't exists continue!
2915
            if (empty($courseInfo)) {
2916
                continue;
2917
            }
2918
2919
            $exists = false;
2920
            // check if the course we want to add is already subscribed
2921
            foreach ($existingCourses as $existingCourse) {
2922
                if ($courseId == $existingCourse['c_id']) {
2923
                    $exists = true;
2924
                }
2925
            }
2926
2927
            if (!$exists) {
2928
                // Copy gradebook categories and links (from base course)
2929
                // to the new course session
2930
                if ($copyEvaluation) {
2931
                    $cats = Category::load(null, null, $courseInfo['code']);
2932
                    if (!empty($cats)) {
2933
                        $sessionCategory = Category:: load(
2934
                            null,
2935
                            null,
2936
                            $courseInfo['code'],
2937
                            null,
2938
                            null,
2939
                            $sessionId,
2940
                            false
2941
                        );
2942
2943
                        // @todo remove commented code
2944
                        if (empty($sessionCategory)) {
2945
                            // There is no category for this course+session, so create one
2946
                            $cat = new Category();
2947
                            $sessionName = $session->getName();
2948
                            $cat->set_name($courseInfo['code'].' - '.get_lang('Session').' '.$sessionName);
2949
                            $cat->set_session_id($sessionId);
2950
                            $cat->set_course_code($courseInfo['code']);
2951
                            $cat->set_description(null);
2952
                            //$cat->set_user_id($stud_id);
2953
                            $cat->set_parent_id(0);
2954
                            $cat->set_weight(100);
2955
                            $cat->set_visible(0);
2956
                            $cat->set_certificate_min_score(75);
2957
                            $cat->add();
2958
                            $sessionGradeBookCategoryId = $cat->get_id();
2959
                        } else {
2960
                            if (!empty($sessionCategory[0])) {
2961
                                $sessionGradeBookCategoryId = $sessionCategory[0]->get_id();
2962
                            }
2963
                        }
2964
2965
                        $categoryIdList = [];
2966
                        /** @var Category $cat */
2967
                        foreach ($cats as $cat) {
2968
                            $categoryIdList[$cat->get_id()] = $cat->get_id();
2969
                        }
2970
2971
                        $newCategoryIdList = [];
2972
                        foreach ($cats as $cat) {
2973
                            $links = $cat->get_links(
2974
                                null,
2975
                                false,
2976
                                $courseInfo['code'],
2977
                                0
2978
                            );
2979
2980
                            //$cat->set_session_id($sessionId);
2981
                            //$oldCategoryId = $cat->get_id();
2982
                            //$newId = $cat->add();
2983
                            //$newCategoryIdList[$oldCategoryId] = $newId;
2984
                            //$parentId = $cat->get_parent_id();
2985
2986
                            /*if (!empty($parentId)) {
2987
                                $newParentId = $newCategoryIdList[$parentId];
2988
                                $cat->set_parent_id($newParentId);
2989
                                $cat->save();
2990
                            }*/
2991
2992
                            if (!empty($links)) {
2993
                                /** @var AbstractLink $link */
2994
                                foreach ($links as $link) {
2995
                                    //$newCategoryId = $newCategoryIdList[$link->get_category_id()];
2996
                                    $link->set_category_id($sessionGradeBookCategoryId);
2997
                                    $link->add();
2998
                                }
2999
                            }
3000
3001
                            $evaluationList = $cat->get_evaluations(
3002
                                null,
3003
                                false,
3004
                                $courseInfo['code'],
3005
                                0
3006
                            );
3007
3008
                            if (!empty($evaluationList)) {
3009
                                /** @var Evaluation $evaluation */
3010
                                foreach ($evaluationList as $evaluation) {
3011
                                    //$evaluationId = $newCategoryIdList[$evaluation->get_category_id()];
3012
                                    $evaluation->set_category_id($sessionGradeBookCategoryId);
3013
                                    $evaluation->add();
3014
                                }
3015
                            }
3016
                        }
3017
3018
                        // Create
3019
                        DocumentManager::generateDefaultCertificate(
3020
                            $courseInfo,
3021
                            true,
3022
                            $sessionId
3023
                        );
3024
                    }
3025
                }
3026
3027
                // If the course isn't subscribed yet
3028
                $sql = "INSERT INTO $tbl_session_rel_course (session_id, c_id, nbr_users, position)
3029
                        VALUES ($sessionId, $courseId, 0, 0)";
3030
                Database::query($sql);
3031
3032
                Event::addEvent(
3033
                    LOG_SESSION_ADD_COURSE,
3034
                    LOG_COURSE_ID,
3035
                    $courseId,
3036
                    api_get_utc_datetime(),
3037
                    api_get_user_id(),
3038
                    $courseId,
3039
                    $sessionId
3040
                );
3041
3042
                // We add the current course in the existing courses array,
3043
                // to avoid adding another time the current course
3044
                $existingCourses[] = ['c_id' => $courseId];
3045
                $nbr_courses++;
3046
3047
                // Subscribe all the users from the session to this course inside the session
3048
                $nbr_users = 0;
3049
                foreach ($user_list as $enreg_user) {
3050
                    $enreg_user_id = (int) $enreg_user['user_id'];
3051
                    $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user (session_id, c_id, user_id, visibility)
3052
                            VALUES ($sessionId, $courseId, $enreg_user_id, $sessionVisibility)";
3053
                    $result = Database::query($sql);
3054
3055
                    Event::addEvent(
3056
                        LOG_SESSION_ADD_USER_COURSE,
3057
                        LOG_USER_ID,
3058
                        $enreg_user_id,
3059
                        api_get_utc_datetime(),
3060
                        api_get_user_id(),
3061
                        $courseId,
3062
                        $sessionId
3063
                    );
3064
3065
                    if (Database::affected_rows($result)) {
3066
                        $nbr_users++;
3067
                    }
3068
                }
3069
                $sql = "UPDATE $tbl_session_rel_course
3070
                        SET nbr_users = $nbr_users
3071
                        WHERE session_id = $sessionId AND c_id = $courseId";
3072
                Database::query($sql);
3073
            }
3074
3075
            if ($copyCourseTeachersAsCoach) {
3076
                $teachers = CourseManager::get_teacher_list_from_course_code($courseInfo['code']);
3077
                if (!empty($teachers)) {
3078
                    foreach ($teachers as $teacher) {
3079
                        self::updateCoaches(
3080
                            $sessionId,
3081
                            $courseId,
3082
                            [$teacher['user_id']],
3083
                            false
3084
                        );
3085
                    }
3086
                }
3087
            }
3088
        }
3089
3090
        $sql = "UPDATE $tbl_session SET nbr_courses = $nbr_courses WHERE id = $sessionId";
3091
        Database::query($sql);
3092
3093
        return true;
3094
    }
3095
3096
    /**
3097
     * Unsubscribe course from a session.
3098
     *
3099
     * @param int $session_id
3100
     * @param int $course_id
3101
     *
3102
     * @return bool True in case of success, false otherwise
3103
     */
3104
    public static function unsubscribe_course_from_session($session_id, $course_id)
3105
    {
3106
        $session_id = (int) $session_id;
3107
        $course_id = (int) $course_id;
3108
3109
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
3110
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
3111
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
3112
3113
        // Get course code
3114
        $course_code = CourseManager::get_course_code_from_course_id($course_id);
3115
3116
        if (empty($course_code)) {
3117
            return false;
3118
        }
3119
3120
        // Unsubscribe course
3121
        $sql = "DELETE FROM $tbl_session_rel_course
3122
                WHERE c_id = $course_id AND session_id = $session_id";
3123
        $result = Database::query($sql);
3124
        $nb_affected = Database::affected_rows($result);
3125
3126
        $sql = "DELETE FROM $tbl_session_rel_course_rel_user
3127
                WHERE c_id = $course_id AND session_id = $session_id";
3128
        Database::query($sql);
3129
3130
        Event::addEvent(
3131
            LOG_SESSION_DELETE_COURSE,
3132
            LOG_COURSE_ID,
3133
            $course_id,
3134
            api_get_utc_datetime(),
3135
            api_get_user_id(),
3136
            $course_id,
3137
            $session_id
3138
        );
3139
3140
        if ($nb_affected > 0) {
3141
            // Update number of courses in the session
3142
            $sql = "UPDATE $tbl_session SET nbr_courses= nbr_courses - $nb_affected
3143
                    WHERE id = $session_id";
3144
            Database::query($sql);
3145
3146
            return true;
3147
        }
3148
3149
        return false;
3150
    }
3151
3152
    /**
3153
     * Creates a new extra field for a given session.
3154
     *
3155
     * @param string $variable    Field's internal variable name
3156
     * @param int    $fieldType   Field's type
3157
     * @param string $displayText Field's language var name
3158
     * @param string $default     Field's default value
3159
     *
3160
     * @return int new extra field id
3161
     */
3162
    public static function create_session_extra_field(
3163
        $variable,
3164
        $fieldType,
3165
        $displayText,
3166
        $default = ''
3167
    ) {
3168
        $extraField = new ExtraFieldModel('session');
3169
        $params = [
3170
            'variable' => $variable,
3171
            'field_type' => $fieldType,
3172
            'display_text' => $displayText,
3173
            'default_value' => $default,
3174
        ];
3175
3176
        return $extraField->save($params);
3177
    }
3178
3179
    /**
3180
     * Update an extra field value for a given session.
3181
     *
3182
     * @param int    $sessionId Session ID
3183
     * @param string $variable  Field variable name
3184
     * @param string $value     Optional. Default field value
3185
     *
3186
     * @return bool|int An integer when register a new extra field. And boolean when update the extrafield
3187
     */
3188
    public static function update_session_extra_field_value($sessionId, $variable, $value = '')
3189
    {
3190
        $extraFieldValue = new ExtraFieldValue('session');
3191
        $params = [
3192
            'item_id' => $sessionId,
3193
            'variable' => $variable,
3194
            'value' => $value,
3195
        ];
3196
3197
        return $extraFieldValue->save($params);
3198
    }
3199
3200
    /**
3201
     * Checks the relationship between a session and a course.
3202
     *
3203
     * @param int $session_id
3204
     * @param int $courseId
3205
     *
3206
     * @return bool returns TRUE if the session and the course are related, FALSE otherwise
3207
     * */
3208
    public static function relation_session_course_exist($session_id, $courseId)
3209
    {
3210
        $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
3211
        $return_value = false;
3212
        $sql = "SELECT c_id FROM $tbl_session_course
3213
                WHERE
3214
                  session_id = ".intval($session_id)." AND
3215
                  c_id = ".intval($courseId);
3216
        $result = Database::query($sql);
3217
        $num = Database::num_rows($result);
3218
        if ($num > 0) {
3219
            $return_value = true;
3220
        }
3221
3222
        return $return_value;
3223
    }
3224
3225
    /**
3226
     * Get the session information by name.
3227
     *
3228
     * @param string $name
3229
     *
3230
     * @return mixed false if the session does not exist, array if the session exist
3231
     */
3232
    public static function get_session_by_name($name)
3233
    {
3234
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
3235
        $name = Database::escape_string(trim($name));
3236
        if (empty($name)) {
3237
            return false;
3238
        }
3239
3240
        $sql = 'SELECT *
3241
		        FROM '.$tbl_session.'
3242
		        WHERE name = "'.$name.'"';
3243
        $result = Database::query($sql);
3244
        $num = Database::num_rows($result);
3245
        if ($num > 0) {
3246
            return Database::fetch_array($result);
3247
        } else {
3248
            return false;
3249
        }
3250
    }
3251
3252
    /**
3253
     * @param int $sessionId
3254
     * @param int $name
3255
     *
3256
     * @return bool
3257
     */
3258
    public static function sessionNameExistBesidesMySession($sessionId, $name)
3259
    {
3260
        $table = Database::get_main_table(TABLE_MAIN_SESSION);
3261
        $name = Database::escape_string(trim($name));
3262
        $sessionId = (int) $sessionId;
3263
3264
        if (empty($name)) {
3265
            return false;
3266
        }
3267
3268
        $sql = "SELECT *
3269
		        FROM $table
3270
		        WHERE name = '$name' AND id <> $sessionId ";
3271
        $result = Database::query($sql);
3272
        $num = Database::num_rows($result);
3273
        if ($num > 0) {
3274
            return true;
3275
        }
3276
3277
        return false;
3278
    }
3279
3280
    /**
3281
     * Create a session category.
3282
     *
3283
     * @author Jhon Hinojosa <[email protected]>, from existing code
3284
     *
3285
     * @param string        name
3286
     * @param int        year_start
3287
     * @param int        month_start
3288
     * @param int        day_start
3289
     * @param int        year_end
3290
     * @param int        month_end
3291
     * @param int        day_end
3292
     *
3293
     * @return int session ID
3294
     * */
3295
    public static function create_category_session(
3296
        $sname,
3297
        $syear_start,
3298
        $smonth_start,
3299
        $sday_start,
3300
        $syear_end,
3301
        $smonth_end,
3302
        $sday_end
3303
    ) {
3304
        $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
3305
        $name = trim($sname);
3306
        $year_start = intval($syear_start);
3307
        $month_start = intval($smonth_start);
3308
        $day_start = intval($sday_start);
3309
        $year_end = intval($syear_end);
3310
        $month_end = intval($smonth_end);
3311
        $day_end = intval($sday_end);
3312
3313
        $date_start = "$year_start-".(($month_start < 10) ? "0$month_start" : $month_start)."-".(($day_start < 10) ? "0$day_start" : $day_start);
3314
        $date_end = "$year_end-".(($month_end < 10) ? "0$month_end" : $month_end)."-".(($day_end < 10) ? "0$day_end" : $day_end);
3315
3316
        if (empty($name)) {
3317
            $msg = get_lang('SessionCategoryNameIsRequired');
3318
3319
            return $msg;
3320
        } elseif (!$month_start || !$day_start || !$year_start || !checkdate($month_start, $day_start, $year_start)) {
3321
            $msg = get_lang('InvalidStartDate');
3322
3323
            return $msg;
3324
        } elseif (!$month_end && !$day_end && !$year_end) {
3325
            $date_end = '';
3326
        } elseif (!$month_end || !$day_end || !$year_end || !checkdate($month_end, $day_end, $year_end)) {
3327
            $msg = get_lang('InvalidEndDate');
3328
3329
            return $msg;
3330
        } elseif ($date_start >= $date_end) {
3331
            $msg = get_lang('StartDateShouldBeBeforeEndDate');
3332
3333
            return $msg;
3334
        }
3335
3336
        $access_url_id = api_get_current_access_url_id();
3337
        $params = [
3338
            'name' => $name,
3339
            'date_start' => $date_start,
3340
            'access_url_id' => $access_url_id,
3341
        ];
3342
3343
        if (!empty($date_end)) {
3344
            $params['date_end'] = $date_end;
3345
        }
3346
3347
        $id = Database::insert($tbl_session_category, $params);
3348
3349
        // Add event to system log
3350
        $user_id = api_get_user_id();
3351
        Event::addEvent(
3352
            LOG_SESSION_CATEGORY_CREATE,
3353
            LOG_SESSION_CATEGORY_ID,
3354
            $id,
3355
            api_get_utc_datetime(),
3356
            $user_id
3357
        );
3358
3359
        return $id;
3360
    }
3361
3362
    /**
3363
     * Edit a sessions category.
3364
     *
3365
     * @author Jhon Hinojosa <[email protected]>,from existing code
3366
     *
3367
     * @param int        id
3368
     * @param string        name
3369
     * @param int        year_start
3370
     * @param int        month_start
3371
     * @param int        day_start
3372
     * @param int        year_end
3373
     * @param int        month_end
3374
     * @param int        day_end
3375
     *
3376
     * @return bool
3377
     *              The parameter id is a primary key
3378
     * */
3379
    public static function edit_category_session(
3380
        $id,
3381
        $sname,
3382
        $syear_start,
3383
        $smonth_start,
3384
        $sday_start,
3385
        $syear_end,
3386
        $smonth_end,
3387
        $sday_end
3388
    ) {
3389
        $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
3390
        $name = trim($sname);
3391
        $year_start = intval($syear_start);
3392
        $month_start = intval($smonth_start);
3393
        $day_start = intval($sday_start);
3394
        $year_end = intval($syear_end);
3395
        $month_end = intval($smonth_end);
3396
        $day_end = intval($sday_end);
3397
        $id = intval($id);
3398
        $date_start = "$year_start-".(($month_start < 10) ? "0$month_start" : $month_start)."-".(($day_start < 10) ? "0$day_start" : $day_start);
3399
        $date_end = "$year_end-".(($month_end < 10) ? "0$month_end" : $month_end)."-".(($day_end < 10) ? "0$day_end" : $day_end);
3400
3401
        if (empty($name)) {
3402
            $msg = get_lang('SessionCategoryNameIsRequired');
3403
3404
            return $msg;
3405
        } elseif (!$month_start || !$day_start || !$year_start || !checkdate($month_start, $day_start, $year_start)) {
3406
            $msg = get_lang('InvalidStartDate');
3407
3408
            return $msg;
3409
        } elseif (!$month_end && !$day_end && !$year_end) {
3410
            $date_end = null;
3411
        } elseif (!$month_end || !$day_end || !$year_end || !checkdate($month_end, $day_end, $year_end)) {
3412
            $msg = get_lang('InvalidEndDate');
3413
3414
            return $msg;
3415
        } elseif ($date_start >= $date_end) {
3416
            $msg = get_lang('StartDateShouldBeBeforeEndDate');
3417
3418
            return $msg;
3419
        }
3420
        if ($date_end != null) {
3421
            $sql = "UPDATE $tbl_session_category
3422
                    SET
3423
                        name = '".Database::escape_string($name)."',
3424
                        date_start = '$date_start' ,
3425
                        date_end = '$date_end'
3426
                    WHERE id= $id";
3427
        } else {
3428
            $sql = "UPDATE $tbl_session_category SET
3429
                        name = '".Database::escape_string($name)."',
3430
                        date_start = '$date_start',
3431
                        date_end = NULL
3432
                    WHERE id= $id";
3433
        }
3434
        $result = Database::query($sql);
3435
3436
        return $result ? true : false;
3437
    }
3438
3439
    /**
3440
     * Delete sessions categories.
3441
     *
3442
     * @param array|int $categoryId
3443
     * @param bool      $deleteSessions Optional. Include delete session.
3444
     * @param bool      $fromWs         Optional. True if the function is called by a webservice, false otherwise.
3445
     *
3446
     * @return bool Nothing, or false on error
3447
     *              The parameters is a array to delete sessions
3448
     *
3449
     * @author Jhon Hinojosa <[email protected]>, from existing code
3450
     */
3451
    public static function delete_session_category($categoryId, $deleteSessions = false, $fromWs = false)
3452
    {
3453
        $tblSessionCategory = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
3454
        $tblSession = Database::get_main_table(TABLE_MAIN_SESSION);
3455
3456
        if (is_array($categoryId)) {
3457
            $categoryId = array_map('intval', $categoryId);
3458
        } else {
3459
            $categoryId = [(int) $categoryId];
3460
        }
3461
3462
        $categoryId = implode(', ', $categoryId);
3463
3464
        if ($deleteSessions) {
3465
            $sql = "SELECT id FROM $tblSession WHERE session_category_id IN ($categoryId)";
3466
            $result = Database::query($sql);
3467
            while ($rows = Database::fetch_array($result)) {
3468
                $sessionId = $rows['id'];
3469
                self::delete($sessionId, $fromWs);
3470
            }
3471
        } else {
3472
            $sql = "UPDATE $tblSession SET session_category_id = NULL WHERE session_category_id IN ($categoryId)";
3473
            Database::query($sql);
3474
        }
3475
3476
        $sql = "DELETE FROM $tblSessionCategory WHERE id IN ($categoryId)";
3477
        Database::query($sql);
3478
3479
        // Add event to system log
3480
        Event::addEvent(
3481
            LOG_SESSION_CATEGORY_DELETE,
3482
            LOG_SESSION_CATEGORY_ID,
3483
            $categoryId,
3484
            api_get_utc_datetime(),
3485
            api_get_user_id()
3486
        );
3487
3488
        return true;
3489
    }
3490
3491
    /**
3492
     * Get a list of sessions of which the given conditions match with an = 'cond'.
3493
     *
3494
     * @param array $conditions          a list of condition example :
3495
     *                                   array('status' => STUDENT) or
3496
     *                                   array('s.name' => array('operator' => 'LIKE', value = '%$needle%'))
3497
     * @param array $order_by            a list of fields on which sort
3498
     * @param int   $urlId
3499
     * @param array $onlyThisSessionList
3500
     *
3501
     * @return array an array with all sessions of the platform
3502
     *
3503
     * @todo   optional course code parameter, optional sorting parameters...
3504
     */
3505
    public static function get_sessions_list(
3506
        $conditions = [],
3507
        $order_by = [],
3508
        $from = null,
3509
        $to = null,
3510
        $urlId = 0,
3511
        $onlyThisSessionList = []
3512
    ) {
3513
        $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
3514
        $session_category_table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
3515
        $user_table = Database::get_main_table(TABLE_MAIN_USER);
3516
        $table_access_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
3517
        $session_course_table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
3518
        $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
3519
        $urlId = empty($urlId) ? api_get_current_access_url_id() : (int) $urlId;
3520
        $return_array = [];
3521
3522
        $sql_query = " SELECT
3523
                    DISTINCT(s.id),
3524
                    s.name,
3525
                    s.nbr_courses,
3526
                    s.access_start_date,
3527
                    s.access_end_date,
3528
                    u.firstname,
3529
                    u.lastname,
3530
                    sc.name as category_name,
3531
                    s.promotion_id
3532
				FROM $session_table s
3533
				INNER JOIN $user_table u ON s.id_coach = u.user_id
3534
				INNER JOIN $table_access_url_rel_session ar ON ar.session_id = s.id
3535
				LEFT JOIN  $session_category_table sc ON s.session_category_id = sc.id
3536
				LEFT JOIN $session_course_table sco ON (sco.session_id = s.id)
3537
				INNER JOIN $course_table c ON sco.c_id = c.id
3538
				WHERE ar.access_url_id = $urlId ";
3539
3540
        $availableFields = [
3541
            's.id',
3542
            's.name',
3543
            'c.id',
3544
        ];
3545
3546
        $availableOperator = [
3547
            'like',
3548
            '>=',
3549
            '<=',
3550
            '=',
3551
        ];
3552
3553
        if (count($conditions) > 0) {
3554
            foreach ($conditions as $field => $options) {
3555
                $operator = strtolower($options['operator']);
3556
                $value = Database::escape_string($options['value']);
3557
                $sql_query .= ' AND ';
3558
                if (in_array($field, $availableFields) && in_array($operator, $availableOperator)) {
3559
                    $sql_query .= $field." $operator '".$value."'";
3560
                }
3561
            }
3562
        }
3563
3564
        if (!empty($onlyThisSessionList)) {
3565
            $onlyThisSessionList = array_map('intval', $onlyThisSessionList);
3566
            $onlyThisSessionList = implode("','", $onlyThisSessionList);
3567
            $sql_query .= " AND s.id IN ('$onlyThisSessionList') ";
3568
        }
3569
3570
        $orderAvailableList = ['name'];
3571
        if (count($order_by) > 0) {
3572
            $order = null;
3573
            $direction = null;
3574
            if (isset($order_by[0]) && in_array($order_by[0], $orderAvailableList)) {
3575
                $order = $order_by[0];
3576
            }
3577
            if (isset($order_by[1]) && in_array(strtolower($order_by[1]), ['desc', 'asc'])) {
3578
                $direction = $order_by[1];
3579
            }
3580
3581
            if (!empty($order)) {
3582
                $sql_query .= " ORDER BY `$order` $direction ";
3583
            }
3584
        }
3585
3586
        if (!is_null($from) && !is_null($to)) {
3587
            $to = (int) $to;
3588
            $from = (int) $from;
3589
            $sql_query .= "LIMIT $from, $to";
3590
        }
3591
3592
        $sql_result = Database::query($sql_query);
3593
        if (Database::num_rows($sql_result) > 0) {
3594
            while ($result = Database::fetch_array($sql_result)) {
3595
                $return_array[$result['id']] = $result;
3596
            }
3597
        }
3598
3599
        return $return_array;
3600
    }
3601
3602
    /**
3603
     * Get the session category information by id.
3604
     *
3605
     * @param string session category ID
3606
     *
3607
     * @return mixed false if the session category does not exist, array if the session category exists
3608
     */
3609
    public static function get_session_category($id)
3610
    {
3611
        $table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
3612
        $id = (int) $id;
3613
        $sql = "SELECT id, name, date_start, date_end
3614
                FROM $table
3615
                WHERE id= $id";
3616
        $result = Database::query($sql);
3617
        $num = Database::num_rows($result);
3618
        if ($num > 0) {
3619
            return Database::fetch_array($result);
3620
        } else {
3621
            return false;
3622
        }
3623
    }
3624
3625
    /**
3626
     * Get the session image.
3627
     *
3628
     * @param int $id
3629
     *
3630
     * @return image path
3631
     */
3632
    public static function getSessionImage($id)
3633
    {
3634
        $id = (int) $id;
3635
        $extraFieldValuesTable = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
3636
        $sql = "SELECT value  FROM $extraFieldValuesTable WHERE field_id = 16 AND item_id = ".$id;
3637
        $result = Database::query($sql);
3638
        if (Database::num_rows($result) > 0) {
3639
            while ($row = Database::fetch_array($result, 'ASSOC')) {
3640
                $sessionImage = $row['value'];
3641
                $sessionImage = api_get_path(WEB_UPLOAD_PATH).$sessionImage;
3642
            }
3643
3644
            return $sessionImage;
3645
        } else {
3646
            $sessionImage = api_get_path(WEB_IMG_PATH)."session_default.png";
3647
3648
            return $sessionImage;
3649
        }
3650
    }
3651
3652
    /**
3653
     * Get Hot Sessions (limit 8).
3654
     *
3655
     * @return array with sessions
3656
     */
3657
    public static function getHotSessions()
3658
    {
3659
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
3660
        $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
3661
        $tbl_users = Database::get_main_table(TABLE_MAIN_USER);
3662
        $tbl_extra_fields = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
3663
        $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
3664
        $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
3665
3666
        $extraField = new ExtraFieldModel('session');
3667
        $field = $extraField->get_handler_field_info_by_field_variable('image');
3668
3669
        $sql = "SELECT
3670
                s.id,
3671
                s.name,
3672
                s.id_coach,
3673
                u.firstname,
3674
                u.lastname,
3675
                s.session_category_id,
3676
                c.name as category_name,
3677
                s.description,
3678
                (SELECT COUNT(*) FROM $tbl_session_user WHERE session_id = s.id) as users,
3679
				(SELECT COUNT(*) FROM $tbl_lp WHERE session_id = s.id) as lessons ";
3680
        if ($field !== false) {
3681
            $fieldId = $field['id'];
3682
            $sql .= ",(SELECT value FROM $tbl_extra_fields WHERE field_id = $fieldId AND item_id = s.id) as image ";
3683
        }
3684
        $sql .= " FROM $tbl_session s
3685
                LEFT JOIN $tbl_session_category c
3686
                    ON s.session_category_id = c.id
3687
                INNER JOIN $tbl_users u
3688
                    ON s.id_coach = u.id
3689
                ORDER BY 9 DESC
3690
                LIMIT 8";
3691
        $result = Database::query($sql);
3692
3693
        if (Database::num_rows($result) > 0) {
3694
            $plugin = BuyCoursesPlugin::create();
3695
            $checker = $plugin->isEnabled();
3696
            $sessions = [];
3697
            while ($row = Database::fetch_array($result, 'ASSOC')) {
3698
                if (!isset($row['image'])) {
3699
                    $row['image'] = '';
3700
                }
3701
                $row['on_sale'] = '';
3702
                if ($checker) {
3703
                    $row['on_sale'] = $plugin->getItemByProduct(
3704
                        $row['id'],
3705
                        BuyCoursesPlugin::PRODUCT_TYPE_SESSION
3706
                    );
3707
                }
3708
                $sessions[] = $row;
3709
            }
3710
3711
            return $sessions;
3712
        }
3713
3714
        return false;
3715
    }
3716
3717
    /**
3718
     * Get all session categories (filter by access_url_id).
3719
     *
3720
     * @return mixed false if the session category does not exist, array if the session category exists
3721
     */
3722
    public static function get_all_session_category()
3723
    {
3724
        $table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
3725
        $id = api_get_current_access_url_id();
3726
        $sql = 'SELECT * FROM '.$table.'
3727
                WHERE access_url_id = '.$id.'
3728
                ORDER BY name ASC';
3729
        $result = Database::query($sql);
3730
        if (Database::num_rows($result) > 0) {
3731
            $data = Database::store_result($result, 'ASSOC');
3732
3733
            return $data;
3734
        }
3735
3736
        return false;
3737
    }
3738
3739
    /**
3740
     * Assign a coach to course in session with status = 2.
3741
     *
3742
     * @param int  $userId
3743
     * @param int  $sessionId
3744
     * @param int  $courseId
3745
     * @param bool $noCoach   optional, if is true the user don't be a coach now,
3746
     *                        otherwise it'll assign a coach
3747
     *
3748
     * @return bool true if there are affected rows, otherwise false
3749
     */
3750
    public static function set_coach_to_course_session(
3751
        $userId,
3752
        $sessionId = 0,
3753
        $courseId = 0,
3754
        $noCoach = false
3755
    ) {
3756
        // Definition of variables
3757
        $userId = (int) $userId;
3758
3759
        $sessionId = !empty($sessionId) ? (int) $sessionId : api_get_session_id();
3760
        $courseId = !empty($courseId) ? (int) $courseId : api_get_course_id();
3761
3762
        if (empty($sessionId) || empty($courseId) || empty($userId)) {
3763
            return false;
3764
        }
3765
3766
        // Table definition
3767
        $tblSessionRelCourseRelUser = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
3768
        $tblSessionRelUser = Database::get_main_table(TABLE_MAIN_SESSION_USER);
3769
        $tblUser = Database::get_main_table(TABLE_MAIN_USER);
3770
3771
        // check if user is a teacher
3772
        $sql = "SELECT * FROM $tblUser
3773
                WHERE status = 1 AND user_id = $userId";
3774
3775
        $rsCheckUser = Database::query($sql);
3776
3777
        if (Database::num_rows($rsCheckUser) <= 0) {
3778
            return false;
3779
        }
3780
3781
        if ($noCoach) {
3782
            // check if user_id exists in session_rel_user (if the user is
3783
            // subscribed to the session in any manner)
3784
            $sql = "SELECT user_id FROM $tblSessionRelUser
3785
                    WHERE
3786
                        session_id = $sessionId AND
3787
                        user_id = $userId";
3788
            $res = Database::query($sql);
3789
3790
            if (Database::num_rows($res) > 0) {
3791
                // The user is already subscribed to the session. Change the
3792
                // record so the user is NOT a coach for this course anymore
3793
                // and then exit
3794
                $sql = "UPDATE $tblSessionRelCourseRelUser
3795
                        SET status = 0
3796
                        WHERE
3797
                            session_id = $sessionId AND
3798
                            c_id = $courseId AND
3799
                            user_id = $userId ";
3800
                $result = Database::query($sql);
3801
3802
                return Database::affected_rows($result) > 0;
3803
            }
3804
3805
            // The user is not subscribed to the session, so make sure
3806
            // he isn't subscribed to a course in this session either
3807
            // and then exit
3808
            $sql = "DELETE FROM $tblSessionRelCourseRelUser
3809
                    WHERE
3810
                        session_id = $sessionId AND
3811
                        c_id = $courseId AND
3812
                        user_id = $userId ";
3813
            $result = Database::query($sql);
3814
3815
            return Database::affected_rows($result) > 0;
3816
        }
3817
3818
        // Assign user as a coach to course
3819
        // First check if the user is registered to the course
3820
        $sql = "SELECT user_id FROM $tblSessionRelCourseRelUser
3821
                WHERE
3822
                    session_id = $sessionId AND
3823
                    c_id = $courseId AND
3824
                    user_id = $userId";
3825
        $rs_check = Database::query($sql);
3826
3827
        // Then update or insert.
3828
        if (Database::num_rows($rs_check) > 0) {
3829
            $sql = "UPDATE $tblSessionRelCourseRelUser SET status = 2
3830
                    WHERE
3831
                        session_id = $sessionId AND
3832
                        c_id = $courseId AND
3833
                        user_id = $userId ";
3834
            $result = Database::query($sql);
3835
3836
            return Database::affected_rows($result) > 0;
3837
        }
3838
3839
        $sql = "INSERT INTO $tblSessionRelCourseRelUser(session_id, c_id, user_id, status)
3840
                VALUES($sessionId, $courseId, $userId, 2)";
3841
        $result = Database::query($sql);
3842
3843
        return Database::affected_rows($result) > 0;
3844
    }
3845
3846
    /**
3847
     * @param int $sessionId
3848
     *
3849
     * @return bool
3850
     */
3851
    public static function removeAllDrhFromSession($sessionId)
3852
    {
3853
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
3854
        $sessionId = (int) $sessionId;
3855
3856
        if (empty($sessionId)) {
3857
            return false;
3858
        }
3859
3860
        $sql = "DELETE FROM $tbl_session_rel_user
3861
                WHERE
3862
                    session_id = $sessionId AND
3863
                    relation_type =".SESSION_RELATION_TYPE_RRHH;
3864
        Database::query($sql);
3865
3866
        return true;
3867
    }
3868
3869
    /**
3870
     * Subscribes sessions to human resource manager (Dashboard feature).
3871
     *
3872
     * @param array $userInfo               Human Resource Manager info
3873
     * @param array $sessions_list          Sessions id
3874
     * @param bool  $sendEmail
3875
     * @param bool  $removeSessionsFromUser
3876
     *
3877
     * @return int
3878
     * */
3879
    public static function subscribeSessionsToDrh(
3880
        $userInfo,
3881
        $sessions_list,
3882
        $sendEmail = false,
3883
        $removeSessionsFromUser = true
3884
    ) {
3885
        // Database Table Definitions
3886
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
3887
        $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
3888
3889
        if (empty($userInfo)) {
3890
            return 0;
3891
        }
3892
3893
        $userId = $userInfo['user_id'];
3894
3895
        // Only subscribe DRH users.
3896
        $rolesAllowed = [
3897
            DRH,
3898
            SESSIONADMIN,
3899
            PLATFORM_ADMIN,
3900
            COURSE_TUTOR,
3901
        ];
3902
        $isAdmin = api_is_platform_admin_by_id($userInfo['user_id']);
3903
        if (!$isAdmin && !in_array($userInfo['status'], $rolesAllowed)) {
3904
            return 0;
3905
        }
3906
3907
        $affected_rows = 0;
3908
        // Deleting assigned sessions to hrm_id.
3909
        if ($removeSessionsFromUser) {
3910
            if (api_is_multiple_url_enabled()) {
3911
                $sql = "SELECT s.session_id
3912
                        FROM $tbl_session_rel_user s
3913
                        INNER JOIN $tbl_session_rel_access_url a
3914
                        ON (a.session_id = s.session_id)
3915
                        WHERE
3916
                            s.user_id = $userId AND
3917
                            relation_type = ".SESSION_RELATION_TYPE_RRHH." AND
3918
                            access_url_id = ".api_get_current_access_url_id();
3919
            } else {
3920
                $sql = "SELECT s.session_id
3921
                        FROM $tbl_session_rel_user s
3922
                        WHERE user_id = $userId AND relation_type=".SESSION_RELATION_TYPE_RRHH;
3923
            }
3924
            $result = Database::query($sql);
3925
3926
            if (Database::num_rows($result) > 0) {
3927
                while ($row = Database::fetch_array($result)) {
3928
                    $sql = "DELETE FROM $tbl_session_rel_user
3929
                            WHERE
3930
                                session_id = {$row['session_id']} AND
3931
                                user_id = $userId AND
3932
                                relation_type =".SESSION_RELATION_TYPE_RRHH;
3933
                    Database::query($sql);
3934
                    Event::addEvent(
3935
                        LOG_SESSION_DELETE_USER,
3936
                        LOG_USER_ID,
3937
                        $userId,
3938
                        api_get_utc_datetime(),
3939
                        api_get_user_id(),
3940
                        null,
3941
                        $row['session_id']
3942
                    );
3943
                }
3944
            }
3945
        }
3946
3947
        // Inserting new sessions list.
3948
        if (!empty($sessions_list) && is_array($sessions_list)) {
3949
            foreach ($sessions_list as $session_id) {
3950
                $session_id = (int) $session_id;
3951
                $sql = "SELECT session_id
3952
                        FROM $tbl_session_rel_user
3953
                        WHERE
3954
                            session_id = $session_id AND
3955
                            user_id = $userId AND
3956
                            relation_type = '".SESSION_RELATION_TYPE_RRHH."'";
3957
                $result = Database::query($sql);
3958
                if (Database::num_rows($result) == 0) {
3959
                    $sql = "INSERT IGNORE INTO $tbl_session_rel_user (session_id, user_id, relation_type, registered_at)
3960
                            VALUES (
3961
                                $session_id,
3962
                                $userId,
3963
                                '".SESSION_RELATION_TYPE_RRHH."',
3964
                                '".api_get_utc_datetime()."'
3965
                            )";
3966
                    Database::query($sql);
3967
                    Event::addEvent(
3968
                        LOG_SESSION_ADD_USER,
3969
                        LOG_USER_ID,
3970
                        $userId,
3971
                        api_get_utc_datetime(),
3972
                        api_get_user_id(),
3973
                        null,
3974
                        $session_id
3975
                    );
3976
                    $affected_rows++;
3977
                }
3978
            }
3979
        }
3980
3981
        return $affected_rows;
3982
    }
3983
3984
    /**
3985
     * @param int $sessionId
3986
     *
3987
     * @return array
3988
     */
3989
    public static function getDrhUsersInSession($sessionId)
3990
    {
3991
        return self::get_users_by_session($sessionId, SESSION_RELATION_TYPE_RRHH);
3992
    }
3993
3994
    /**
3995
     * @param int $userId
3996
     * @param int $sessionId
3997
     *
3998
     * @return array
3999
     */
4000
    public static function getSessionFollowedByDrh($userId, $sessionId)
4001
    {
4002
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
4003
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
4004
        $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
4005
4006
        $userId = (int) $userId;
4007
        $sessionId = (int) $sessionId;
4008
4009
        $select = " SELECT * ";
4010
        if (api_is_multiple_url_enabled()) {
4011
            $sql = " $select FROM $tbl_session s
4012
                    INNER JOIN $tbl_session_rel_user sru ON (sru.session_id = s.id)
4013
                    LEFT JOIN $tbl_session_rel_access_url a ON (s.id = a.session_id)
4014
                    WHERE
4015
                        sru.user_id = '$userId' AND
4016
                        sru.session_id = '$sessionId' AND
4017
                        sru.relation_type = '".SESSION_RELATION_TYPE_RRHH."' AND
4018
                        access_url_id = ".api_get_current_access_url_id()."
4019
                    ";
4020
        } else {
4021
            $sql = "$select FROM $tbl_session s
4022
                     INNER JOIN $tbl_session_rel_user sru
4023
                     ON
4024
                        sru.session_id = s.id AND
4025
                        sru.user_id = '$userId' AND
4026
                        sru.session_id = '$sessionId' AND
4027
                        sru.relation_type = '".SESSION_RELATION_TYPE_RRHH."'
4028
                    ";
4029
        }
4030
4031
        $result = Database::query($sql);
4032
        if (Database::num_rows($result)) {
4033
            $row = Database::fetch_array($result, 'ASSOC');
4034
            $row['course_list'] = self::get_course_list_by_session_id($sessionId);
4035
4036
            return $row;
4037
        }
4038
4039
        return [];
4040
    }
4041
4042
    /**
4043
     * Get sessions followed by human resources manager.
4044
     *
4045
     * @param int    $userId
4046
     * @param int    $start
4047
     * @param int    $limit
4048
     * @param bool   $getCount
4049
     * @param bool   $getOnlySessionId
4050
     * @param bool   $getSql
4051
     * @param string $orderCondition
4052
     * @param string $keyword
4053
     * @param string $description
4054
     * @param array  $options
4055
     *
4056
     * @return array sessions
4057
     */
4058
    public static function get_sessions_followed_by_drh(
4059
        $userId,
4060
        $start = null,
4061
        $limit = null,
4062
        $getCount = false,
4063
        $getOnlySessionId = false,
4064
        $getSql = false,
4065
        $orderCondition = null,
4066
        $keyword = '',
4067
        $description = '',
4068
        $options = []
4069
    ) {
4070
        return self::getSessionsFollowedByUser(
4071
            $userId,
4072
            DRH,
4073
            $start,
4074
            $limit,
4075
            $getCount,
4076
            $getOnlySessionId,
4077
            $getSql,
4078
            $orderCondition,
4079
            $keyword,
4080
            $description,
4081
            $options
4082
        );
4083
    }
4084
4085
    /**
4086
     * Get sessions followed by human resources manager.
4087
     *
4088
     * @param int    $userId
4089
     * @param int    $status           DRH Optional
4090
     * @param int    $start
4091
     * @param int    $limit
4092
     * @param bool   $getCount
4093
     * @param bool   $getOnlySessionId
4094
     * @param bool   $getSql
4095
     * @param string $orderCondition
4096
     * @param string $keyword
4097
     * @param string $description
4098
     * @param array  $options
4099
     *
4100
     * @return array sessions
4101
     */
4102
    public static function getSessionsFollowedByUser(
4103
        $userId,
4104
        $status = null,
4105
        $start = null,
4106
        $limit = null,
4107
        $getCount = false,
4108
        $getOnlySessionId = false,
4109
        $getSql = false,
4110
        $orderCondition = null,
4111
        $keyword = '',
4112
        $description = '',
4113
        $options = []
4114
    ) {
4115
        // Database Table Definitions
4116
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
4117
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
4118
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
4119
        $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
4120
4121
        $extraFieldModel = new ExtraFieldModel('session');
4122
        $conditions = $extraFieldModel->parseConditions($options);
4123
        $sqlInjectJoins = $conditions['inject_joins'];
4124
        $extraFieldsConditions = $conditions['where'];
4125
        $sqlInjectWhere = $conditions['inject_where'];
4126
        $injectExtraFields = $conditions['inject_extra_fields'];
4127
4128
        if (!empty($injectExtraFields)) {
4129
            $injectExtraFields = ' , '.$injectExtraFields.' s.id';
4130
        }
4131
4132
        $userId = (int) $userId;
4133
4134
        $select = ' SELECT DISTINCT * '.$injectExtraFields;
4135
        if ($getCount) {
4136
            $select = ' SELECT count(DISTINCT(s.id)) as count ';
4137
        }
4138
4139
        if ($getOnlySessionId) {
4140
            $select = ' SELECT DISTINCT(s.id) ';
4141
        }
4142
4143
        $limitCondition = null;
4144
        if (!is_null($start) && !is_null($limit)) {
4145
            $limitCondition = " LIMIT ".intval($start).", ".intval($limit);
4146
        }
4147
4148
        if (empty($orderCondition)) {
4149
            $orderCondition = ' ORDER BY s.name ';
4150
        }
4151
4152
        $whereConditions = null;
4153
        $sessionCourseConditions = null;
4154
        $sessionConditions = null;
4155
        $sessionQuery = '';
4156
        $courseSessionQuery = null;
4157
4158
        switch ($status) {
4159
            case DRH:
4160
                $sessionQuery = "SELECT sru.session_id
4161
                                 FROM
4162
                                 $tbl_session_rel_user sru
4163
                                 WHERE
4164
                                    sru.relation_type = '".SESSION_RELATION_TYPE_RRHH."' AND
4165
                                    sru.user_id = $userId";
4166
                break;
4167
            case COURSEMANAGER:
4168
                $courseSessionQuery = "
4169
                    SELECT scu.session_id as id
4170
                    FROM $tbl_session_rel_course_rel_user scu
4171
                    WHERE (scu.status = 2 AND scu.user_id = $userId)";
4172
4173
                $whereConditions = " OR (s.id_coach = $userId) ";
4174
                break;
4175
            default:
4176
                $sessionQuery = "SELECT sru.session_id
4177
                                 FROM
4178
                                 $tbl_session_rel_user sru
4179
                                 WHERE
4180
                                    sru.user_id = $userId";
4181
                break;
4182
        }
4183
4184
        $keywordCondition = '';
4185
        if (!empty($keyword)) {
4186
            $keyword = Database::escape_string($keyword);
4187
            $keywordCondition = " AND (s.name LIKE '%$keyword%' ) ";
4188
4189
            if (!empty($description)) {
4190
                $description = Database::escape_string($description);
4191
                $keywordCondition = " AND (s.name LIKE '%$keyword%' OR s.description LIKE '%$description%' ) ";
4192
            }
4193
        }
4194
4195
        $whereConditions .= $keywordCondition;
4196
        $subQuery = $sessionQuery.$courseSessionQuery;
4197
4198
        $sql = " $select
4199
                FROM $tbl_session s
4200
                INNER JOIN $tbl_session_rel_access_url a
4201
                ON (s.id = a.session_id)
4202
                $sqlInjectJoins
4203
                WHERE
4204
                    access_url_id = ".api_get_current_access_url_id()." AND
4205
                    s.id IN (
4206
                        $subQuery
4207
                    )
4208
                    $whereConditions
4209
                    $extraFieldsConditions
4210
                    $sqlInjectWhere
4211
                    $orderCondition
4212
                    $limitCondition";
4213
4214
        if ($getSql) {
4215
            return $sql;
4216
        }
4217
        $result = Database::query($sql);
4218
4219
        if ($getCount) {
4220
            $row = Database::fetch_array($result);
4221
            if ($row) {
4222
                return (int) $row['count'];
4223
            }
4224
4225
            return 0;
4226
        }
4227
4228
        $sessions = [];
4229
        if (Database::num_rows($result) > 0) {
4230
            $sysUploadPath = api_get_path(SYS_UPLOAD_PATH).'sessions/';
4231
            $webUploadPath = api_get_path(WEB_UPLOAD_PATH).'sessions/';
4232
            $imgPath = Display::return_icon(
4233
                'session_default_small.png',
4234
                null,
4235
                [],
4236
                ICON_SIZE_SMALL,
4237
                false,
4238
                true
4239
            );
4240
4241
            while ($row = Database::fetch_array($result)) {
4242
                if ($getOnlySessionId) {
4243
                    $sessions[$row['id']] = $row;
4244
                    continue;
4245
                }
4246
                $imageFilename = ExtraFieldModel::FIELD_TYPE_FILE_IMAGE.'_'.$row['id'].'.png';
4247
                $row['image'] = is_file($sysUploadPath.$imageFilename) ? $webUploadPath.$imageFilename : $imgPath;
4248
4249
                if ($row['display_start_date'] == '0000-00-00 00:00:00' || $row['display_start_date'] == '0000-00-00') {
4250
                    $row['display_start_date'] = null;
4251
                }
4252
4253
                if ($row['display_end_date'] == '0000-00-00 00:00:00' || $row['display_end_date'] == '0000-00-00') {
4254
                    $row['display_end_date'] = null;
4255
                }
4256
4257
                if ($row['access_start_date'] == '0000-00-00 00:00:00' || $row['access_start_date'] == '0000-00-00') {
4258
                    $row['access_start_date'] = null;
4259
                }
4260
4261
                if ($row['access_end_date'] == '0000-00-00 00:00:00' || $row['access_end_date'] == '0000-00-00') {
4262
                    $row['access_end_date'] = null;
4263
                }
4264
4265
                if ($row['coach_access_start_date'] === '0000-00-00 00:00:00' ||
4266
                    $row['coach_access_start_date'] === '0000-00-00'
4267
                ) {
4268
                    $row['coach_access_start_date'] = null;
4269
                }
4270
4271
                if ($row['coach_access_end_date'] === '0000-00-00 00:00:00' ||
4272
                    $row['coach_access_end_date'] === '0000-00-00'
4273
                ) {
4274
                    $row['coach_access_end_date'] = null;
4275
                }
4276
4277
                $sessions[$row['id']] = $row;
4278
            }
4279
        }
4280
4281
        return $sessions;
4282
    }
4283
4284
    /**
4285
     * Gets the list (or the count) of courses by session filtered by access_url.
4286
     *
4287
     * @param int    $session_id  The session id
4288
     * @param string $course_name The course code
4289
     * @param string $orderBy     Field to order the data
4290
     * @param bool   $getCount    Optional. Count the session courses
4291
     *
4292
     * @return array|int List of courses. Whether $getCount is true, return the count
4293
     */
4294
    public static function get_course_list_by_session_id(
4295
        $session_id,
4296
        $course_name = '',
4297
        $orderBy = null,
4298
        $getCount = false
4299
    ) {
4300
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
4301
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
4302
        $session_id = (int) $session_id;
4303
        $sqlSelect = '*, c.id, c.id as real_id, c.code as course_code';
4304
4305
        if ($getCount) {
4306
            $sqlSelect = 'COUNT(1) as count';
4307
        }
4308
4309
        // select the courses
4310
        $sql = "SELECT $sqlSelect
4311
                FROM $tbl_course c
4312
                INNER JOIN $tbl_session_rel_course src
4313
                ON (c.id = src.c_id)
4314
		        WHERE src.session_id = '$session_id' ";
4315
4316
        if (!empty($course_name)) {
4317
            $course_name = Database::escape_string($course_name);
4318
            $sql .= " AND c.title LIKE '%$course_name%' ";
4319
        }
4320
4321
        if (!empty($orderBy)) {
4322
            $orderBy = Database::escape_string($orderBy);
4323
            $orderBy = " ORDER BY $orderBy";
4324
        } else {
4325
            if (self::orderCourseIsEnabled()) {
4326
                $orderBy .= ' ORDER BY position ';
4327
            } else {
4328
                $orderBy .= ' ORDER BY title ';
4329
            }
4330
        }
4331
4332
        $sql .= Database::escape_string($orderBy);
4333
        $result = Database::query($sql);
4334
        $num_rows = Database::num_rows($result);
4335
        $courses = [];
4336
        if ($num_rows > 0) {
4337
            if ($getCount) {
4338
                $count = Database::fetch_assoc($result);
4339
4340
                return (int) $count['count'];
4341
            }
4342
4343
            while ($row = Database::fetch_array($result, 'ASSOC')) {
4344
                $courses[$row['real_id']] = $row;
4345
            }
4346
        }
4347
4348
        return $courses;
4349
    }
4350
4351
    /**
4352
     * Gets the list of courses by session filtered by access_url.
4353
     *
4354
     * @param $userId
4355
     * @param $sessionId
4356
     * @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...
4357
     * @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...
4358
     * @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...
4359
     * @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...
4360
     * @param bool   $getCount
4361
     * @param string $keyword
4362
     *
4363
     * @return array
4364
     */
4365
    public static function getAllCoursesFollowedByUser(
4366
        $userId,
4367
        $sessionId,
4368
        $from = null,
4369
        $limit = null,
4370
        $column = null,
4371
        $direction = null,
4372
        $getCount = false,
4373
        $keyword = ''
4374
    ) {
4375
        if (empty($sessionId)) {
4376
            $sessionsSQL = self::get_sessions_followed_by_drh(
4377
                $userId,
4378
                null,
4379
                null,
4380
                null,
4381
                true,
4382
                true
4383
            );
4384
        } else {
4385
            $sessionsSQL = intval($sessionId);
4386
        }
4387
4388
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
4389
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
4390
4391
        if ($getCount) {
4392
            $select = "SELECT COUNT(DISTINCT(c.code)) as count ";
4393
        } else {
4394
            $select = "SELECT DISTINCT c.* ";
4395
        }
4396
4397
        $keywordCondition = null;
4398
        if (!empty($keyword)) {
4399
            $keyword = Database::escape_string($keyword);
4400
            $keywordCondition = " AND (c.code LIKE '%$keyword%' OR c.title LIKE '%$keyword%' ) ";
4401
        }
4402
4403
        // Select the courses
4404
        $sql = "$select
4405
                FROM $tbl_course c
4406
                INNER JOIN $tbl_session_rel_course src
4407
                ON c.id = src.c_id
4408
		        WHERE
4409
		            src.session_id IN ($sessionsSQL)
4410
		            $keywordCondition
4411
		        ";
4412
        if ($getCount) {
4413
            $result = Database::query($sql);
4414
            $row = Database::fetch_array($result, 'ASSOC');
4415
4416
            return $row['count'];
4417
        }
4418
4419
        if (isset($from) && isset($limit)) {
4420
            $from = intval($from);
4421
            $limit = intval($limit);
4422
            $sql .= " LIMIT $from, $limit";
4423
        }
4424
4425
        $result = Database::query($sql);
4426
        $num_rows = Database::num_rows($result);
4427
        $courses = [];
4428
4429
        if ($num_rows > 0) {
4430
            while ($row = Database::fetch_array($result, 'ASSOC')) {
4431
                $courses[$row['id']] = $row;
4432
            }
4433
        }
4434
4435
        return $courses;
4436
    }
4437
4438
    /**
4439
     * Gets the list of courses by session filtered by access_url.
4440
     *
4441
     * @param int    $session_id
4442
     * @param string $course_name
4443
     *
4444
     * @return array list of courses
4445
     */
4446
    public static function get_course_list_by_session_id_like($session_id, $course_name = '')
4447
    {
4448
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
4449
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
4450
4451
        $session_id = (int) $session_id;
4452
        $course_name = Database::escape_string($course_name);
4453
4454
        // select the courses
4455
        $sql = "SELECT c.id, c.title FROM $tbl_course c
4456
                INNER JOIN $tbl_session_rel_course src
4457
                ON c.id = src.c_id
4458
		        WHERE ";
4459
4460
        if (!empty($session_id)) {
4461
            $sql .= "src.session_id LIKE '$session_id' AND ";
4462
        }
4463
4464
        if (!empty($course_name)) {
4465
            $sql .= "UPPER(c.title) LIKE UPPER('%$course_name%') ";
4466
        }
4467
4468
        $sql .= "ORDER BY title;";
4469
        $result = Database::query($sql);
4470
        $num_rows = Database::num_rows($result);
4471
        $courses = [];
4472
        if ($num_rows > 0) {
4473
            while ($row = Database::fetch_array($result, 'ASSOC')) {
4474
                $courses[$row['id']] = $row;
4475
            }
4476
        }
4477
4478
        return $courses;
4479
    }
4480
4481
    /**
4482
     * Gets the count of courses by session filtered by access_url.
4483
     *
4484
     * @param int session id
4485
     * @param string $keyword
4486
     *
4487
     * @return array list of courses
4488
     */
4489
    public static function getCourseCountBySessionId($session_id, $keyword = '')
4490
    {
4491
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
4492
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
4493
        $session_id = (int) $session_id;
4494
4495
        // select the courses
4496
        $sql = "SELECT COUNT(c.code) count
4497
                FROM $tbl_course c
4498
                INNER JOIN $tbl_session_rel_course src
4499
                ON c.id = src.c_id
4500
		        WHERE src.session_id = '$session_id' ";
4501
4502
        $keywordCondition = null;
4503
        if (!empty($keyword)) {
4504
            $keyword = Database::escape_string($keyword);
4505
            $keywordCondition = " AND (c.code LIKE '%$keyword%' OR c.title LIKE '%$keyword%' ) ";
4506
        }
4507
        $sql .= $keywordCondition;
4508
4509
        $result = Database::query($sql);
4510
        $num_rows = Database::num_rows($result);
4511
        if ($num_rows > 0) {
4512
            $row = Database::fetch_array($result, 'ASSOC');
4513
4514
            return $row['count'];
4515
        }
4516
4517
        return null;
4518
    }
4519
4520
    /**
4521
     * Get the session id based on the original id and field name in the extra fields.
4522
     * Returns 0 if session was not found.
4523
     *
4524
     * @param string $value    Original session id
4525
     * @param string $variable Original field name
4526
     *
4527
     * @return int Session id
4528
     */
4529
    public static function getSessionIdFromOriginalId($value, $variable)
4530
    {
4531
        $extraFieldValue = new ExtraFieldValue('session');
4532
        $result = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
4533
            $variable,
4534
            $value
4535
        );
4536
4537
        if (!empty($result)) {
4538
            return $result['item_id'];
4539
        }
4540
4541
        return 0;
4542
    }
4543
4544
    /**
4545
     * Get users by session.
4546
     *
4547
     * @param int  $id       session id
4548
     * @param int  $status   filter by status coach = 2
4549
     * @param bool $getCount Optional. Allow get the number of rows from the result
4550
     * @param int  $urlId
4551
     *
4552
     * @return array|int A list with an user list. If $getCount is true then return a the count of registers
4553
     */
4554
    public static function get_users_by_session(
4555
        $id,
4556
        $status = null,
4557
        $getCount = false,
4558
        $urlId = 0
4559
    ) {
4560
        if (empty($id)) {
4561
            return [];
4562
        }
4563
        $id = (int) $id;
4564
        $urlId = empty($urlId) ? api_get_current_access_url_id() : (int) $urlId;
4565
4566
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
4567
        $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
4568
        $table_access_url_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
4569
4570
        $selectedField = '
4571
            u.user_id, u.lastname, u.firstname, u.username, su.relation_type, au.access_url_id,
4572
            su.moved_to, su.moved_status, su.moved_at, su.registered_at
4573
        ';
4574
4575
        if ($getCount) {
4576
            $selectedField = 'count(1) AS count';
4577
        }
4578
4579
        $sql = "SELECT $selectedField
4580
                FROM $tbl_user u
4581
                INNER JOIN $tbl_session_rel_user su
4582
                ON u.user_id = su.user_id AND
4583
                su.session_id = $id
4584
                LEFT OUTER JOIN $table_access_url_user au
4585
                ON (au.user_id = u.user_id)
4586
                ";
4587
4588
        if (is_numeric($status)) {
4589
            $status = (int) $status;
4590
            $sql .= " WHERE su.relation_type = $status AND (au.access_url_id = $urlId OR au.access_url_id is null)";
4591
        } else {
4592
            $sql .= " WHERE (au.access_url_id = $urlId OR au.access_url_id is null )";
4593
        }
4594
4595
        $sql .= ' ORDER BY su.relation_type, ';
4596
        $sql .= api_sort_by_first_name() ? ' u.firstname, u.lastname' : '  u.lastname, u.firstname';
4597
4598
        $result = Database::query($sql);
4599
        if ($getCount) {
4600
            $count = Database::fetch_assoc($result);
4601
            if ($count) {
4602
                return (int) $count['count'];
4603
            }
4604
4605
            return 0;
4606
        }
4607
4608
        $return = [];
4609
        while ($row = Database::fetch_array($result, 'ASSOC')) {
4610
            $return[] = $row;
4611
        }
4612
4613
        return $return;
4614
    }
4615
4616
    /**
4617
     * The general coach (field: session.id_coach).
4618
     *
4619
     * @param int  $user_id         user id
4620
     * @param bool $asPlatformAdmin The user is platform admin, return everything
4621
     *
4622
     * @return array
4623
     */
4624
    public static function get_sessions_by_general_coach($user_id, $asPlatformAdmin = false)
4625
    {
4626
        $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
4627
        $user_id = (int) $user_id;
4628
4629
        // Session where we are general coach
4630
        $sql = "SELECT DISTINCT *
4631
                FROM $session_table";
4632
4633
        if (!$asPlatformAdmin) {
4634
            $sql .= " WHERE id_coach = $user_id";
4635
        }
4636
4637
        if (api_is_multiple_url_enabled()) {
4638
            $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
4639
            $access_url_id = api_get_current_access_url_id();
4640
4641
            $sqlCoach = '';
4642
            if (!$asPlatformAdmin) {
4643
                $sqlCoach = " id_coach = $user_id AND ";
4644
            }
4645
4646
            if ($access_url_id != -1) {
4647
                $sql = 'SELECT DISTINCT session.*
4648
                    FROM '.$session_table.' session INNER JOIN '.$tbl_session_rel_access_url.' session_rel_url
4649
                    ON (session.id = session_rel_url.session_id)
4650
                    WHERE '.$sqlCoach.' access_url_id = '.$access_url_id;
4651
            }
4652
        }
4653
        $sql .= ' ORDER by name';
4654
        $result = Database::query($sql);
4655
4656
        return Database::store_result($result, 'ASSOC');
4657
    }
4658
4659
    /**
4660
     * @param int $user_id
4661
     *
4662
     * @return array
4663
     *
4664
     * @deprecated use get_sessions_by_general_coach()
4665
     */
4666
    public static function get_sessions_by_coach($user_id)
4667
    {
4668
        $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
4669
4670
        return Database::select(
4671
            '*',
4672
            $session_table,
4673
            ['where' => ['id_coach = ?' => $user_id]]
4674
        );
4675
    }
4676
4677
    /**
4678
     * @param int $user_id
4679
     * @param int $courseId
4680
     * @param int $session_id
4681
     *
4682
     * @return array|bool
4683
     */
4684
    public static function get_user_status_in_course_session($user_id, $courseId, $session_id)
4685
    {
4686
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
4687
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
4688
        $sql = "SELECT session_rcru.status
4689
                FROM $table session_rcru
4690
                INNER JOIN $tbl_user user
4691
                ON (session_rcru.user_id = user.user_id)
4692
                WHERE
4693
                    session_rcru.session_id = '".intval($session_id)."' AND
4694
                    session_rcru.c_id ='".intval($courseId)."' AND
4695
                    user.user_id = ".intval($user_id);
4696
4697
        $result = Database::query($sql);
4698
        $status = false;
4699
        if (Database::num_rows($result)) {
4700
            $status = Database::fetch_row($result);
4701
            $status = $status['0'];
4702
        }
4703
4704
        return $status;
4705
    }
4706
4707
    /**
4708
     * Gets user status within a session.
4709
     *
4710
     * @param int $userId
4711
     * @param int $sessionId
4712
     *
4713
     * @return SessionRelUser
4714
     */
4715
    public static function getUserStatusInSession($userId, $sessionId)
4716
    {
4717
        $em = Database::getManager();
4718
        $subscriptions = $em
4719
            ->getRepository('ChamiloCoreBundle:SessionRelUser')
4720
            ->findBy(['session' => $sessionId, 'user' => $userId]);
4721
4722
        /** @var SessionRelUser $subscription */
4723
        $subscription = current($subscriptions);
4724
4725
        return $subscription;
4726
    }
4727
4728
    /**
4729
     * @param int $id
4730
     *
4731
     * @return array
4732
     */
4733
    public static function get_all_sessions_by_promotion($id)
4734
    {
4735
        $table = Database::get_main_table(TABLE_MAIN_SESSION);
4736
4737
        return Database::select(
4738
            '*',
4739
            $table,
4740
            ['where' => ['promotion_id = ?' => $id]]
4741
        );
4742
    }
4743
4744
    /**
4745
     * @param int   $promotion_id
4746
     * @param array $list
4747
     */
4748
    public static function subscribe_sessions_to_promotion($promotion_id, $list)
4749
    {
4750
        $table = Database::get_main_table(TABLE_MAIN_SESSION);
4751
        $params = [];
4752
        $params['promotion_id'] = 0;
4753
        Database::update(
4754
            $table,
4755
            $params,
4756
            ['promotion_id = ?' => $promotion_id]
4757
        );
4758
4759
        $params['promotion_id'] = $promotion_id;
4760
        if (!empty($list)) {
4761
            foreach ($list as $session_id) {
4762
                $session_id = (int) $session_id;
4763
                Database::update($table, $params, ['id = ?' => $session_id]);
4764
            }
4765
        }
4766
    }
4767
4768
    /**
4769
     * Updates a session status.
4770
     *
4771
     * @param int session id
4772
     * @param int status
4773
     */
4774
    public static function set_session_status($session_id, $status)
4775
    {
4776
        $t = Database::get_main_table(TABLE_MAIN_SESSION);
4777
        $params['visibility'] = $status;
4778
        Database::update($t, $params, ['id = ?' => $session_id]);
4779
    }
4780
4781
    /**
4782
     * Copies a session with the same data to a new session.
4783
     * The new copy is not assigned to the same promotion.
4784
     *
4785
     * @param int  $id                         Session ID
4786
     * @param bool $copy_courses               Whether to copy the relationship with courses
4787
     * @param bool $copyTeachersAndDrh
4788
     * @param bool $create_new_courses         New courses will be created
4789
     * @param bool $set_exercises_lp_invisible Set exercises and LPs in the new session to invisible by default
4790
     *
4791
     * @return int The new session ID on success, 0 otherwise
4792
     *
4793
     * @see subscribe_sessions_to_promotions() for that.
4794
     *
4795
     * @todo make sure the extra session fields are copied too
4796
     */
4797
    public static function copy(
4798
        $id,
4799
        $copy_courses = true,
4800
        $copyTeachersAndDrh = true,
4801
        $create_new_courses = false,
4802
        $set_exercises_lp_invisible = false
4803
    ) {
4804
        $id = (int) $id;
4805
        $s = self::fetch($id);
4806
4807
        if (empty($s)) {
4808
            return false;
4809
        }
4810
4811
        // Check all dates before copying
4812
        // Get timestamp for now in UTC - see http://php.net/manual/es/function.time.php#117251
4813
        $now = time() - date('Z');
4814
        // Timestamp in one month
4815
        $inOneMonth = $now + (30 * 24 * 3600);
4816
        $inOneMonth = api_get_local_time($inOneMonth);
4817
        if (api_strtotime($s['access_start_date']) < $now) {
4818
            $s['access_start_date'] = api_get_local_time($now);
4819
        }
4820
        if (api_strtotime($s['display_start_date']) < $now) {
4821
            $s['display_start_date'] = api_get_local_time($now);
4822
        }
4823
        if (api_strtotime($s['coach_access_start_date']) < $now) {
4824
            $s['coach_access_start_date'] = api_get_local_time($now);
4825
        }
4826
        if (api_strtotime($s['access_end_date']) < $now) {
4827
            $s['access_end_date'] = $inOneMonth;
4828
        }
4829
        if (api_strtotime($s['display_end_date']) < $now) {
4830
            $s['display_end_date'] = $inOneMonth;
4831
        }
4832
        if (api_strtotime($s['coach_access_end_date']) < $now) {
4833
            $s['coach_access_end_date'] = $inOneMonth;
4834
        }
4835
        // Now try to create the session
4836
        $extraFieldValue = new ExtraFieldValue('session');
4837
        $extraFieldsValues = $extraFieldValue->getAllValuesByItem($id);
4838
        $extraFieldsValuesToCopy = [];
4839
        if (!empty($extraFieldsValues)) {
4840
            foreach ($extraFieldsValues as $extraFieldValue) {
4841
                //$extraFieldsValuesToCopy['extra_'.$extraFieldValue['variable']] = $extraFieldValue['value'];
4842
                $extraFieldsValuesToCopy['extra_'.$extraFieldValue['variable']]['extra_'.$extraFieldValue['variable']] = $extraFieldValue['value'];
4843
            }
4844
        }
4845
4846
        if (isset($extraFieldsValuesToCopy['extra_image']) && isset($extraFieldsValuesToCopy['extra_image']['extra_image'])) {
4847
            $extraFieldsValuesToCopy['extra_image'] = [
4848
                'tmp_name' => api_get_path(SYS_UPLOAD_PATH).$extraFieldsValuesToCopy['extra_image']['extra_image'],
4849
                'error' => 0,
4850
            ];
4851
        }
4852
4853
        // Now try to create the session
4854
        $sid = self::create_session(
4855
            $s['name'].' '.get_lang('CopyLabelSuffix'),
4856
            $s['access_start_date'],
4857
            $s['access_end_date'],
4858
            $s['display_start_date'],
4859
            $s['display_end_date'],
4860
            $s['coach_access_start_date'],
4861
            $s['coach_access_end_date'],
4862
            (int) $s['id_coach'],
4863
            $s['session_category_id'],
4864
            (int) $s['visibility'],
4865
            true,
4866
            $s['duration'],
4867
            $s['description'],
4868
            $s['show_description'],
4869
            $extraFieldsValuesToCopy
4870
        );
4871
4872
        if (!is_numeric($sid) || empty($sid)) {
4873
            return false;
4874
        }
4875
4876
        if ($copy_courses) {
4877
            // Register courses from the original session to the new session
4878
            $courses = self::get_course_list_by_session_id($id);
4879
            $short_courses = $new_short_courses = [];
4880
            if (is_array($courses) && count($courses) > 0) {
4881
                foreach ($courses as $course) {
4882
                    $short_courses[] = $course;
4883
                }
4884
            }
4885
4886
            // We will copy the current courses of the session to new courses
4887
            if (!empty($short_courses)) {
4888
                if ($create_new_courses) {
4889
                    api_set_more_memory_and_time_limits();
4890
                    $params = [];
4891
                    $params['skip_lp_dates'] = true;
4892
4893
                    foreach ($short_courses as $course_data) {
4894
                        $course_info = CourseManager::copy_course_simple(
4895
                            $course_data['title'].' '.get_lang(
4896
                                'CopyLabelSuffix'
4897
                            ),
4898
                            $course_data['course_code'],
4899
                            $id,
4900
                            $sid,
4901
                            $params
4902
                        );
4903
4904
                        if ($course_info) {
4905
                            //By default new elements are invisible
4906
                            if ($set_exercises_lp_invisible) {
4907
                                $list = new LearnpathList('', $course_info, $sid);
4908
                                $flat_list = $list->get_flat_list();
4909
                                if (!empty($flat_list)) {
4910
                                    foreach ($flat_list as $lp_id => $data) {
4911
                                        api_item_property_update(
4912
                                            $course_info,
4913
                                            TOOL_LEARNPATH,
4914
                                            $lp_id,
4915
                                            'invisible',
4916
                                            api_get_user_id(),
4917
                                            0,
4918
                                            0,
4919
                                            0,
4920
                                            0,
4921
                                            $sid
4922
                                        );
4923
                                    }
4924
                                }
4925
                                $quiz_table = Database::get_course_table(TABLE_QUIZ_TEST);
4926
                                $course_id = $course_info['real_id'];
4927
                                //@todo check this query
4928
                                $sql = "UPDATE $quiz_table SET active = 0
4929
                                        WHERE c_id = $course_id AND session_id = $sid";
4930
                                Database::query($sql);
4931
                            }
4932
                            $new_short_courses[] = $course_info['real_id'];
4933
                        }
4934
                    }
4935
                } else {
4936
                    foreach ($short_courses as $course_data) {
4937
                        $new_short_courses[] = $course_data['id'];
4938
                    }
4939
                }
4940
4941
                $short_courses = $new_short_courses;
4942
                self::add_courses_to_session($sid, $short_courses, true);
4943
4944
                if ($create_new_courses === false && $copyTeachersAndDrh) {
4945
                    foreach ($short_courses as $courseItemId) {
4946
                        $coachList = self::getCoachesByCourseSession($id, $courseItemId);
4947
                        foreach ($coachList as $userId) {
4948
                            self::set_coach_to_course_session($userId, $sid, $courseItemId);
4949
            }
4950
        }
4951
                }
4952
            }
4953
        }
4954
4955
        if ($copyTeachersAndDrh) {
4956
            // Register users from the original session to the new session
4957
            $users = self::get_users_by_session($id);
4958
            if (!empty($users)) {
4959
                $userListByStatus = [];
4960
                foreach ($users as $userData) {
4961
                    $userData['relation_type'] = (int) $userData['relation_type'];
4962
                    $userListByStatus[$userData['relation_type']][] = $userData;
4963
                }
4964
            //Subscribing in read only mode
4965
                foreach ($userListByStatus as $status => $userList) {
4966
                    $userList = array_column($userList, 'user_id');
4967
                    switch ($status) {
4968
                        case 0:
4969
                            /*self::subscribeUsersToSession(
4970
                $sid,
4971
                                $userList,
4972
                SESSION_VISIBLE_READ_ONLY,
4973
                                false,
4974
                true
4975
                            );*/
4976
                            break;
4977
                        case 1:
4978
                            // drh users
4979
                            foreach ($userList as $drhId) {
4980
                                $userInfo = api_get_user_info($drhId);
4981
                                self::subscribeSessionsToDrh($userInfo, [$sid], false, false);
4982
                            }
4983
                            break;
4984
                    }
4985
                }
4986
            }
4987
        }
4988
4989
        return $sid;
4990
    }
4991
4992
    /**
4993
     * @param int $user_id
4994
     * @param int $session_id
4995
     *
4996
     * @return bool
4997
     */
4998
    public static function user_is_general_coach($user_id, $session_id)
4999
    {
5000
        $session_id = (int) $session_id;
5001
        $user_id = (int) $user_id;
5002
        $table = Database::get_main_table(TABLE_MAIN_SESSION);
5003
        $sql = "SELECT DISTINCT id
5004
	         	FROM $table
5005
	         	WHERE session.id_coach = '".$user_id."' AND id = '$session_id'";
5006
        $result = Database::query($sql);
5007
        if ($result && Database::num_rows($result)) {
5008
            return true;
5009
        }
5010
5011
        return false;
5012
    }
5013
5014
    /**
5015
     * Get the number of sessions.
5016
     *
5017
     * @param int $access_url_id ID of the URL we want to filter on (optional)
5018
     *
5019
     * @return int Number of sessions
5020
     */
5021
    public static function count_sessions($access_url_id = 0)
5022
    {
5023
        $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
5024
        $access_url_rel_session_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
5025
        $access_url_id = (int) $access_url_id;
5026
        $sql = "SELECT count(s.id) FROM $session_table s";
5027
        if (!empty($access_url_id)) {
5028
            $sql .= ", $access_url_rel_session_table u ".
5029
                " WHERE s.id = u.session_id AND u.access_url_id = $access_url_id";
5030
        }
5031
        $res = Database::query($sql);
5032
        $row = Database::fetch_row($res);
5033
5034
        return $row[0];
5035
    }
5036
5037
    /**
5038
     * Return a COUNT from Session table.
5039
     *
5040
     * @param string $date in Y-m-d format
5041
     *
5042
     * @return int
5043
     */
5044
    public static function countSessionsByEndDate($date = null)
5045
    {
5046
        $sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
5047
        $url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
5048
        $date = Database::escape_string($date);
5049
        $urlId = api_get_current_access_url_id();
5050
        $dateFilter = '';
5051
        if (!empty($date)) {
5052
            $dateFilter = <<<SQL
5053
                AND ('$date' BETWEEN s.access_start_date AND s.access_end_date)
5054
                OR (s.access_end_date IS NULL)
5055
                OR (
5056
                    s.access_start_date IS NULL AND
5057
                    s.access_end_date IS NOT NULL AND s.access_end_date > '$date'
5058
                )
5059
SQL;
5060
        }
5061
        $sql = "SELECT COUNT(*)
5062
                FROM $sessionTable s
5063
                INNER JOIN $url u
5064
                ON (s.id = u.session_id)
5065
                WHERE u.access_url_id = $urlId $dateFilter";
5066
        $res = Database::query($sql);
5067
5068
        $count = 0;
5069
        if ($res !== false && Database::num_rows($res) > 0) {
5070
            $count = (int) current(Database::fetch_row($res));
5071
        }
5072
5073
        return $count;
5074
    }
5075
5076
    /**
5077
     * @param int  $id
5078
     * @param bool $checkSession
5079
     *
5080
     * @return bool
5081
     */
5082
    public static function cantEditSession($id, $checkSession = true)
5083
    {
5084
        if (!self::allowToManageSessions()) {
5085
            return false;
5086
        }
5087
5088
        if (api_is_platform_admin() && self::allowed($id)) {
5089
            return true;
5090
        }
5091
5092
        if ($checkSession) {
5093
            if (self::allowed($id)) {
5094
                return true;
5095
            }
5096
5097
            return false;
5098
        }
5099
5100
        return true;
5101
    }
5102
5103
    /**
5104
     * Protect a session to be edited.
5105
     *
5106
     * @param int  $id
5107
     * @param bool $checkSession
5108
     *
5109
     * @return mixed | bool true if pass the check, api_not_allowed otherwise
5110
     */
5111
    public static function protectSession($id, $checkSession = true)
5112
    {
5113
        if (!self::cantEditSession($id, $checkSession)) {
5114
            api_not_allowed(true);
5115
        }
5116
    }
5117
5118
    /**
5119
     * @return bool
5120
     */
5121
    public static function allowToManageSessions()
5122
    {
5123
        if (self::allowManageAllSessions()) {
5124
            return true;
5125
        }
5126
5127
        $setting = api_get_setting('allow_teachers_to_create_sessions');
5128
5129
        if (api_is_teacher() && $setting == 'true') {
5130
            return true;
5131
        }
5132
5133
        return false;
5134
    }
5135
5136
    /**
5137
     * @return bool
5138
     */
5139
    public static function allowOnlyMySessions()
5140
    {
5141
        if (self::allowToManageSessions() &&
5142
            !api_is_platform_admin() &&
5143
            api_is_teacher()
5144
        ) {
5145
            return true;
5146
        }
5147
5148
        return false;
5149
    }
5150
5151
    /**
5152
     * @return bool
5153
     */
5154
    public static function allowManageAllSessions()
5155
    {
5156
        if (api_is_platform_admin() || api_is_session_admin()) {
5157
            return true;
5158
        }
5159
5160
        return false;
5161
    }
5162
5163
    /**
5164
     * @param $id
5165
     *
5166
     * @return bool
5167
     */
5168
    public static function protect_teacher_session_edit($id)
5169
    {
5170
        if (!api_is_coach($id) && !api_is_platform_admin()) {
5171
            api_not_allowed(true);
5172
        } else {
5173
            return true;
5174
        }
5175
    }
5176
5177
    /**
5178
     * @param int $courseId
5179
     *
5180
     * @return array
5181
     */
5182
    public static function get_session_by_course($courseId)
5183
    {
5184
        $table_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
5185
        $table_session = Database::get_main_table(TABLE_MAIN_SESSION);
5186
        $url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
5187
        $courseId = (int) $courseId;
5188
        $urlId = api_get_current_access_url_id();
5189
5190
        if (empty($courseId)) {
5191
            return [];
5192
        }
5193
5194
        $sql = "SELECT name, s.id
5195
                FROM $table_session_course sc
5196
                INNER JOIN $table_session s
5197
                ON (sc.session_id = s.id)
5198
                INNER JOIN $url u
5199
                ON (u.session_id = s.id)
5200
                WHERE
5201
                    u.access_url_id = $urlId AND
5202
                    sc.c_id = '$courseId' ";
5203
        $result = Database::query($sql);
5204
5205
        return Database::store_result($result);
5206
    }
5207
5208
    /**
5209
     * @param int  $userId
5210
     * @param bool $ignoreVisibilityForAdmins
5211
     * @param bool $ignoreTimeLimit
5212
     *
5213
     * @return array
5214
     */
5215
    public static function get_sessions_by_user(
5216
        $userId,
5217
        $ignoreVisibilityForAdmins = false,
5218
        $ignoreTimeLimit = false
5219
    ) {
5220
        $sessionCategories = UserManager::get_sessions_by_category(
5221
            $userId,
5222
            false,
5223
            $ignoreVisibilityForAdmins,
5224
            $ignoreTimeLimit
5225
        );
5226
5227
        $sessionArray = [];
5228
        if (!empty($sessionCategories)) {
5229
            foreach ($sessionCategories as $category) {
5230
                if (isset($category['sessions'])) {
5231
                    foreach ($category['sessions'] as $session) {
5232
                        $sessionArray[] = $session;
5233
                    }
5234
                }
5235
            }
5236
        }
5237
5238
        return $sessionArray;
5239
    }
5240
5241
    /**
5242
     * @param string $file
5243
     * @param bool   $updateSession                                   true: if the session exists it will be updated.
5244
     *                                                                false: if session exists a new session will be created adding a counter session1, session2, etc
5245
     * @param int    $defaultUserId
5246
     * @param Logger $logger
5247
     * @param array  $extraFields                                     convert a file row to an extra field. Example in CSV file there's a SessionID
5248
     *                                                                then it will converted to extra_external_session_id if you set: array('SessionId' => 'extra_external_session_id')
5249
     * @param string $extraFieldId
5250
     * @param int    $daysCoachAccessBeforeBeginning
5251
     * @param int    $daysCoachAccessAfterBeginning
5252
     * @param int    $sessionVisibility
5253
     * @param array  $fieldsToAvoidUpdate
5254
     * @param bool   $deleteUsersNotInList
5255
     * @param bool   $updateCourseCoaches
5256
     * @param bool   $sessionWithCoursesModifier
5257
     * @param bool   $addOriginalCourseTeachersAsCourseSessionCoaches
5258
     * @param bool   $removeAllTeachersFromCourse
5259
     * @param int    $showDescription
5260
     * @param array  $teacherBackupList
5261
     * @param array  $groupBackup
5262
     *
5263
     * @return array
5264
     */
5265
    public static function importCSV(
5266
        $file,
5267
        $updateSession,
5268
        $defaultUserId = null,
5269
        $logger = null,
5270
        $extraFields = [],
5271
        $extraFieldId = null,
5272
        $daysCoachAccessBeforeBeginning = null,
5273
        $daysCoachAccessAfterBeginning = null,
5274
        $sessionVisibility = 1,
5275
        $fieldsToAvoidUpdate = [],
5276
        $deleteUsersNotInList = false,
5277
        $updateCourseCoaches = false,
5278
        $sessionWithCoursesModifier = false,
5279
        $addOriginalCourseTeachersAsCourseSessionCoaches = true,
5280
        $removeAllTeachersFromCourse = true,
5281
        $showDescription = null,
5282
        &$teacherBackupList = [],
5283
        &$groupBackup = []
5284
    ) {
5285
        $content = file($file);
5286
        $error_message = null;
5287
        $session_counter = 0;
5288
        $defaultUserId = empty($defaultUserId) ? api_get_user_id() : (int) $defaultUserId;
5289
5290
        $eol = PHP_EOL;
5291
        if (PHP_SAPI != 'cli') {
5292
            $eol = '<br />';
5293
        }
5294
5295
        $debug = false;
5296
        if (isset($logger)) {
5297
            $debug = true;
5298
        }
5299
5300
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
5301
        $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
5302
        $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
5303
        $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
5304
        $sessions = [];
5305
        if (!api_strstr($content[0], ';')) {
5306
            $error_message = get_lang('NotCSV');
5307
        } else {
5308
            $tag_names = [];
5309
            foreach ($content as $key => $enreg) {
5310
                $enreg = explode(';', trim($enreg));
5311
                if ($key) {
5312
                    foreach ($tag_names as $tag_key => $tag_name) {
5313
                        if (isset($enreg[$tag_key])) {
5314
                            $sessions[$key - 1][$tag_name] = $enreg[$tag_key];
5315
                        }
5316
                    }
5317
                } else {
5318
                    foreach ($enreg as $tag_name) {
5319
                        $tag_names[] = api_preg_replace('/[^a-zA-Z0-9_\-]/', '', $tag_name);
5320
                    }
5321
                    if (!in_array('SessionName', $tag_names) ||
5322
                        !in_array('DateStart', $tag_names) ||
5323
                        !in_array('DateEnd', $tag_names)
5324
                    ) {
5325
                        $error_message = get_lang('NoNeededData');
5326
                        break;
5327
                    }
5328
                }
5329
            }
5330
5331
            $sessionList = [];
5332
            $report = [];
5333
5334
            // Looping the sessions.
5335
            foreach ($sessions as $enreg) {
5336
                $user_counter = 0;
5337
                $course_counter = 0;
5338
5339
                if (isset($extraFields) && !empty($extraFields)) {
5340
                    foreach ($extraFields as $original => $to) {
5341
                        $enreg[$to] = isset($enreg[$original]) ? $enreg[$original] : null;
5342
                    }
5343
                }
5344
5345
                $session_name = $enreg['SessionName'];
5346
5347
                if ($debug) {
5348
                    $logger->addInfo('---------------------------------------');
5349
                    $logger->addInfo("Sessions - Start process of session: $session_name");
5350
                    $logger->addInfo('---------------------------------------');
5351
                }
5352
5353
                // Default visibility
5354
                $visibilityAfterExpirationPerSession = $sessionVisibility;
5355
5356
                if (isset($enreg['VisibilityAfterExpiration'])) {
5357
                    $visibility = $enreg['VisibilityAfterExpiration'];
5358
                    switch ($visibility) {
5359
                        case 'read_only':
5360
                            $visibilityAfterExpirationPerSession = SESSION_VISIBLE_READ_ONLY;
5361
                            break;
5362
                        case 'accessible':
5363
                            $visibilityAfterExpirationPerSession = SESSION_VISIBLE;
5364
                            break;
5365
                        case 'not_accessible':
5366
                            $visibilityAfterExpirationPerSession = SESSION_INVISIBLE;
5367
                            break;
5368
                    }
5369
                }
5370
5371
                if (empty($session_name)) {
5372
                    continue;
5373
                }
5374
5375
                $displayAccessStartDate = isset($enreg['DisplayStartDate']) ? $enreg['DisplayStartDate'] : $enreg['DateStart'];
5376
                $displayAccessEndDate = isset($enreg['DisplayEndDate']) ? $enreg['DisplayEndDate'] : $enreg['DateEnd'];
5377
                $coachAccessStartDate = isset($enreg['CoachStartDate']) ? $enreg['CoachStartDate'] : $enreg['DateStart'];
5378
                $coachAccessEndDate = isset($enreg['CoachEndDate']) ? $enreg['CoachEndDate'] : $enreg['DateEnd'];
5379
                // We assume the dates are already in UTC
5380
                $dateStart = explode('/', $enreg['DateStart']);
5381
                $dateEnd = explode('/', $enreg['DateEnd']);
5382
                $dateStart = $dateStart[0].'-'.$dateStart[1].'-'.$dateStart[2].' 00:00:00';
5383
                $dateEnd = $dateEnd[0].'-'.$dateEnd[1].'-'.$dateEnd[2].' 23:59:59';
5384
                $displayAccessStartDate = explode('/', $displayAccessStartDate);
5385
                $displayAccessStartDate = implode('-', $displayAccessStartDate).' 00:00:00';
5386
                $displayAccessEndDate = explode('/', $displayAccessEndDate);
5387
                $displayAccessEndDate = implode('-', $displayAccessEndDate).' 23:59:59';
5388
                $coachAccessStartDate = explode('/', $coachAccessStartDate);
5389
                $coachAccessStartDate = implode('-', $coachAccessStartDate).' 00:00:00';
5390
                $coachAccessEndDate = explode('/', $coachAccessEndDate);
5391
                $coachAccessEndDate = implode('-', $coachAccessEndDate).' 23:59:59';
5392
                $session_category_id = isset($enreg['SessionCategory']) ? $enreg['SessionCategory'] : null;
5393
                $sessionDescription = isset($enreg['SessionDescription']) ? $enreg['SessionDescription'] : null;
5394
                $classes = isset($enreg['Classes']) ? explode('|', $enreg['Classes']) : [];
5395
                $extraParams = [];
5396
                if (!is_null($showDescription)) {
5397
                    $extraParams['show_description'] = intval($showDescription);
5398
                }
5399
5400
                $coachBefore = '';
5401
                $coachAfter = '';
5402
                if (!empty($daysCoachAccessBeforeBeginning) && !empty($daysCoachAccessAfterBeginning)) {
5403
                    $date = new \DateTime($dateStart);
5404
                    $interval = new DateInterval('P'.$daysCoachAccessBeforeBeginning.'D');
5405
                    $date->sub($interval);
5406
                    $coachBefore = $date->format('Y-m-d h:i');
5407
                    $coachAccessStartDate = $coachBefore;
5408
                    $coachBefore = api_get_utc_datetime($coachBefore);
5409
5410
                    $date = new \DateTime($dateEnd);
5411
                    $interval = new DateInterval('P'.$daysCoachAccessAfterBeginning.'D');
5412
                    $date->add($interval);
5413
                    $coachAfter = $date->format('Y-m-d h:i');
5414
                    $coachAccessEndDate = $coachAfter;
5415
                    $coachAfter = api_get_utc_datetime($coachAfter);
5416
                }
5417
5418
                $dateStart = api_get_utc_datetime($dateStart);
5419
                $dateEnd = api_get_utc_datetime($dateEnd);
5420
                $displayAccessStartDate = api_get_utc_datetime($displayAccessStartDate);
5421
                $displayAccessEndDate = api_get_utc_datetime($displayAccessEndDate);
5422
                $coachAccessStartDate = api_get_utc_datetime($coachAccessStartDate);
5423
                $coachAccessEndDate = api_get_utc_datetime($coachAccessEndDate);
5424
5425
                if (!empty($sessionDescription)) {
5426
                    $extraParams['description'] = $sessionDescription;
5427
                }
5428
5429
                if (!empty($session_category_id)) {
5430
                    $extraParams['session_category_id'] = $session_category_id;
5431
                }
5432
5433
                // Searching a general coach.
5434
                if (!empty($enreg['Coach'])) {
5435
                    $coach_id = UserManager::get_user_id_from_username($enreg['Coach']);
5436
                    if ($coach_id === false) {
5437
                        // If the coach-user does not exist - I'm the coach.
5438
                        $coach_id = $defaultUserId;
5439
                    }
5440
                } else {
5441
                    $coach_id = $defaultUserId;
5442
                }
5443
5444
                $users = explode('|', $enreg['Users']);
5445
                $courses = explode('|', $enreg['Courses']);
5446
5447
                $deleteOnlyCourseCoaches = false;
5448
                if (count($courses) == 1) {
5449
                    if ($logger) {
5450
                        $logger->addInfo('Only one course delete old coach list');
5451
                    }
5452
                    $deleteOnlyCourseCoaches = true;
5453
                }
5454
5455
                if (!$updateSession) {
5456
                    // Create a session.
5457
                    $unique_name = false;
5458
                    $i = 0;
5459
                    // Change session name, verify that session doesn't exist.
5460
                    $suffix = null;
5461
                    while (!$unique_name) {
5462
                        if ($i > 1) {
5463
                            $suffix = ' - '.$i;
5464
                        }
5465
                        $sql = 'SELECT 1 FROM '.$tbl_session.'
5466
                                WHERE name="'.Database::escape_string($session_name).$suffix.'"';
5467
                        $rs = Database::query($sql);
5468
                        if (Database::result($rs, 0, 0)) {
5469
                            $i++;
5470
                        } else {
5471
                            $unique_name = true;
5472
                            $session_name .= $suffix;
5473
                        }
5474
                    }
5475
5476
                    $sessionParams = [
5477
                        'name' => $session_name,
5478
                        'id_coach' => $coach_id,
5479
                        'access_start_date' => $dateStart,
5480
                        'access_end_date' => $dateEnd,
5481
                        'display_start_date' => $displayAccessStartDate,
5482
                        'display_end_date' => $displayAccessEndDate,
5483
                        'coach_access_start_date' => $coachAccessStartDate,
5484
                        'coach_access_end_date' => $coachAccessEndDate,
5485
                        'visibility' => $visibilityAfterExpirationPerSession,
5486
                        'session_admin_id' => $defaultUserId,
5487
                    ];
5488
5489
                    if (!empty($extraParams)) {
5490
                        $sessionParams = array_merge($sessionParams, $extraParams);
5491
                    }
5492
                    // Creating the session.
5493
                    $session_id = Database::insert($tbl_session, $sessionParams);
5494
                    if ($debug) {
5495
                        if ($session_id) {
5496
                            foreach ($enreg as $key => $value) {
5497
                                if (substr($key, 0, 6) == 'extra_') { //an extra field
5498
                                    self::update_session_extra_field_value($session_id, substr($key, 6), $value);
5499
                                }
5500
                            }
5501
                            $logger->addInfo("Session created: #$session_id - $session_name");
5502
                        } else {
5503
                            $message = "Sessions - Session NOT created: $session_name";
5504
                            $logger->addError($message);
5505
                            $report[] = $message;
5506
                        }
5507
                    }
5508
                    $session_counter++;
5509
                } else {
5510
                    $sessionId = null;
5511
                    if (isset($extraFields) && !empty($extraFields) && !empty($enreg['extra_'.$extraFieldId])) {
5512
                        $sessionId = self::getSessionIdFromOriginalId($enreg['extra_'.$extraFieldId], $extraFieldId);
5513
                        if (empty($sessionId)) {
5514
                            $my_session_result = false;
5515
                        } else {
5516
                            $my_session_result = true;
5517
                        }
5518
                    } else {
5519
                        $my_session_result = self::get_session_by_name($enreg['SessionName']);
5520
                    }
5521
5522
                    if ($my_session_result === false) {
5523
                        // One more check
5524
                        $sessionExistsWithName = self::get_session_by_name($session_name);
5525
                        if ($sessionExistsWithName) {
5526
                            if ($debug) {
5527
                                $message = "Skip Session - Trying to update a session, but name already exists: $session_name";
5528
                                $logger->addError($message);
5529
                                $report[] = $message;
5530
                            }
5531
                            continue;
5532
                        }
5533
5534
                        $sessionParams = [
5535
                            'name' => $session_name,
5536
                            'id_coach' => $coach_id,
5537
                            'access_start_date' => $dateStart,
5538
                            'access_end_date' => $dateEnd,
5539
                            'display_start_date' => $displayAccessStartDate,
5540
                            'display_end_date' => $displayAccessEndDate,
5541
                            'coach_access_start_date' => $coachAccessStartDate,
5542
                            'coach_access_end_date' => $coachAccessEndDate,
5543
                            'visibility' => $visibilityAfterExpirationPerSession,
5544
                            'session_admin_id' => $defaultUserId,
5545
                        ];
5546
5547
                        if (!empty($extraParams)) {
5548
                            $sessionParams = array_merge($sessionParams, $extraParams);
5549
                        }
5550
                        Database::insert($tbl_session, $sessionParams);
5551
5552
                        // We get the last insert id.
5553
                        $my_session_result = self::get_session_by_name($session_name);
5554
                        $session_id = $my_session_result['id'];
5555
5556
                        if ($session_id) {
5557
                            foreach ($enreg as $key => $value) {
5558
                                if (substr($key, 0, 6) == 'extra_') { //an extra field
5559
                                    self::update_session_extra_field_value($session_id, substr($key, 6), $value);
5560
                                }
5561
                            }
5562
                            if ($debug) {
5563
                                $logger->addInfo("Sessions - #$session_id created: $session_name");
5564
                            }
5565
5566
                            // Delete session-user relation only for students
5567
                            $sql = "DELETE FROM $tbl_session_user
5568
                                    WHERE session_id = '$session_id' AND relation_type <> ".SESSION_RELATION_TYPE_RRHH;
5569
                            Database::query($sql);
5570
5571
                            $sql = "DELETE FROM $tbl_session_course WHERE session_id = '$session_id'";
5572
                            Database::query($sql);
5573
5574
                            // Delete session-course-user relationships students and coaches.
5575
                            if ($updateCourseCoaches) {
5576
                                $sql = "DELETE FROM $tbl_session_course_user
5577
                                        WHERE session_id = '$session_id' AND status in ('0', '2')";
5578
                                Database::query($sql);
5579
                            } else {
5580
                                // Delete session-course-user relation ships *only* for students.
5581
                                $sql = "DELETE FROM $tbl_session_course_user
5582
                                        WHERE session_id = '$session_id' AND status <> 2";
5583
                                Database::query($sql);
5584
                            }
5585
                            if ($deleteOnlyCourseCoaches) {
5586
                                $sql = "DELETE FROM $tbl_session_course_user
5587
                                        WHERE session_id = '$session_id' AND status in ('2')";
5588
                                Database::query($sql);
5589
                            }
5590
                        }
5591
                    } else {
5592
                        // Updating the session.
5593
                        $params = [
5594
                            'id_coach' => $coach_id,
5595
                            'access_start_date' => $dateStart,
5596
                            'access_end_date' => $dateEnd,
5597
                            'display_start_date' => $displayAccessStartDate,
5598
                            'display_end_date' => $displayAccessEndDate,
5599
                            'coach_access_start_date' => $coachAccessStartDate,
5600
                            'coach_access_end_date' => $coachAccessEndDate,
5601
                            'visibility' => $visibilityAfterExpirationPerSession,
5602
                            'session_category_id' => $session_category_id,
5603
                        ];
5604
5605
                        if (!empty($sessionDescription)) {
5606
                            $params['description'] = $sessionDescription;
5607
                        }
5608
5609
                        if (!empty($fieldsToAvoidUpdate)) {
5610
                            foreach ($fieldsToAvoidUpdate as $field) {
5611
                                unset($params[$field]);
5612
                            }
5613
                        }
5614
5615
                        if (isset($sessionId) && !empty($sessionId)) {
5616
                            $session_id = $sessionId;
5617
                            if (!empty($enreg['SessionName'])) {
5618
                                $sessionExistsWithName = self::get_session_by_name($session_name);
5619
                                if ($sessionExistsWithName === false) {
5620
                                    $sessionName = Database::escape_string($enreg['SessionName']);
5621
                                    $sql = "UPDATE $tbl_session SET name = '$sessionName' WHERE id = $session_id";
5622
                                    Database::query($sql);
5623
                                    $logger->addInfo(
5624
                                        "Session #$session_id name IS updated with: '$session_name' External id: ".$enreg['extra_'.$extraFieldId]
5625
                                    );
5626
                                } else {
5627
                                    $sessionExistsBesidesMe = self::sessionNameExistBesidesMySession(
5628
                                        $session_id,
5629
                                        $session_name
5630
                                    );
5631
                                    if ($sessionExistsBesidesMe === true) {
5632
                                        if ($debug) {
5633
                                            $message = "Skip Session. Error when update session Session #$session_id Name: '$session_name'. Other session has the same name. External id: ".$enreg['extra_'.$extraFieldId];
5634
                                            $logger->addError($message);
5635
                                            $report[] = $message;
5636
                                        }
5637
                                        continue;
5638
                                    } else {
5639
                                        if ($debug) {
5640
                                            $logger->addInfo(
5641
                                                "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]
5642
                                            );
5643
                                        }
5644
                                    }
5645
                                }
5646
                            }
5647
                        } else {
5648
                            $my_session_result = self::get_session_by_name($session_name);
5649
                            $session_id = $my_session_result['id'];
5650
                        }
5651
5652
                        if ($debug) {
5653
                            $logger->addInfo("Session #$session_id to be updated: '$session_name'");
5654
                        }
5655
5656
                        if ($session_id) {
5657
                            $sessionInfo = api_get_session_info($session_id);
5658
                            $params['show_description'] = isset($sessionInfo['show_description']) ? $sessionInfo['show_description'] : intval($showDescription);
5659
5660
                            if (!empty($daysCoachAccessBeforeBeginning) && !empty($daysCoachAccessAfterBeginning)) {
5661
                                if (empty($sessionInfo['nb_days_access_before_beginning']) ||
5662
                                    (!empty($sessionInfo['nb_days_access_before_beginning']) &&
5663
                                        $sessionInfo['nb_days_access_before_beginning'] < $daysCoachAccessBeforeBeginning)
5664
                                ) {
5665
                                    $params['coach_access_start_date'] = $coachBefore;
5666
                                }
5667
5668
                                if (empty($sessionInfo['nb_days_access_after_end']) ||
5669
                                    (!empty($sessionInfo['nb_days_access_after_end']) &&
5670
                                        $sessionInfo['nb_days_access_after_end'] < $daysCoachAccessAfterBeginning)
5671
                                ) {
5672
                                    $params['coach_access_end_date'] = $coachAfter;
5673
                                }
5674
                            }
5675
5676
                            Database::update($tbl_session, $params, ['id = ?' => $session_id]);
5677
                            foreach ($enreg as $key => $value) {
5678
                                if (substr($key, 0, 6) == 'extra_') { //an extra field
5679
                                    self::update_session_extra_field_value($session_id, substr($key, 6), $value);
5680
                                }
5681
                            }
5682
5683
                            if ($debug) {
5684
                                $logger->addInfo("Session updated #$session_id");
5685
                            }
5686
5687
                            // Delete session-user relation only for students
5688
                            $sql = "DELETE FROM $tbl_session_user
5689
                                    WHERE session_id = '$session_id' AND relation_type <> ".SESSION_RELATION_TYPE_RRHH;
5690
                            Database::query($sql);
5691
5692
                            $sql = "DELETE FROM $tbl_session_course WHERE session_id = '$session_id'";
5693
                            Database::query($sql);
5694
5695
                            // Delete session-course-user relationships students and coaches.
5696
                            if ($updateCourseCoaches) {
5697
                                $sql = "DELETE FROM $tbl_session_course_user
5698
                                        WHERE session_id = '$session_id' AND status in ('0', '2')";
5699
                                Database::query($sql);
5700
                            } else {
5701
                                // Delete session-course-user relation ships *only* for students.
5702
                                $sql = "DELETE FROM $tbl_session_course_user
5703
                                        WHERE session_id = '$session_id' AND status <> 2";
5704
                                Database::query($sql);
5705
                            }
5706
5707
                            if ($deleteOnlyCourseCoaches) {
5708
                                $sql = "DELETE FROM $tbl_session_course_user
5709
                                        WHERE session_id = '$session_id' AND status in ('2')";
5710
                                Database::query($sql);
5711
                            }
5712
                        } else {
5713
                            if ($debug) {
5714
                                $logger->addError(
5715
                                    "Sessions - Session not found"
5716
                                );
5717
                            }
5718
                        }
5719
                    }
5720
                    $session_counter++;
5721
                }
5722
5723
                $sessionList[] = $session_id;
5724
5725
                // Adding the relationship "Session - User" for students
5726
                $userList = [];
5727
                if (is_array($users)) {
5728
                    $extraFieldValueCareer = new ExtraFieldValue('career');
5729
                    $careerList = isset($enreg['extra_careerid']) && !empty($enreg['extra_careerid']) ? $enreg['extra_careerid'] : [];
5730
                    $careerList = str_replace(['[', ']'], '', $careerList);
5731
                    $careerList = explode(',', $careerList);
5732
                    $finalCareerIdList = [];
5733
                    foreach ($careerList as $careerId) {
5734
                        $realCareerIdList = $extraFieldValueCareer->get_item_id_from_field_variable_and_field_value(
5735
                            'external_career_id',
5736
                            $careerId
5737
                        );
5738
                        if (isset($realCareerIdList['item_id'])) {
5739
                            $finalCareerIdList[] = $realCareerIdList['item_id'];
5740
                        }
5741
                    }
5742
5743
                    foreach ($users as $user) {
5744
                        $user_id = UserManager::get_user_id_from_username($user);
5745
                        if ($user_id !== false) {
5746
                            if (!empty($finalCareerIdList)) {
5747
                                foreach ($finalCareerIdList as $careerId) {
5748
                                    UserManager::addUserCareer($user_id, $careerId);
5749
                                }
5750
                            }
5751
5752
                            $userList[] = $user_id;
5753
                            // Insert new users.
5754
                            $sql = "INSERT IGNORE INTO $tbl_session_user SET
5755
                                    user_id = '$user_id',
5756
                                    session_id = '$session_id',
5757
                                    registered_at = '".api_get_utc_datetime()."'";
5758
                            Database::query($sql);
5759
                            if ($debug) {
5760
                                $logger->addInfo("Adding User #$user_id ($user) to session #$session_id");
5761
                            }
5762
                            $user_counter++;
5763
                        }
5764
                    }
5765
                }
5766
5767
                if ($deleteUsersNotInList) {
5768
                    // Getting user in DB in order to compare to the new list.
5769
                    $usersListInDatabase = self::get_users_by_session($session_id, 0);
5770
                    if (!empty($usersListInDatabase)) {
5771
                        if (empty($userList)) {
5772
                            foreach ($usersListInDatabase as $userInfo) {
5773
                                self::unsubscribe_user_from_session($session_id, $userInfo['user_id']);
5774
                            }
5775
                        } else {
5776
                            foreach ($usersListInDatabase as $userInfo) {
5777
                                if (!in_array($userInfo['user_id'], $userList)) {
5778
                                    self::unsubscribe_user_from_session($session_id, $userInfo['user_id']);
5779
                                }
5780
                            }
5781
                        }
5782
                    }
5783
                }
5784
5785
                // See BT#6449
5786
                $onlyAddFirstCoachOrTeacher = false;
5787
                if ($sessionWithCoursesModifier) {
5788
                    if (count($courses) >= 2) {
5789
                        // Only first teacher in course session;
5790
                        $onlyAddFirstCoachOrTeacher = true;
5791
                        // Remove all teachers from course.
5792
                        $removeAllTeachersFromCourse = false;
5793
                    }
5794
                }
5795
5796
                foreach ($courses as $course) {
5797
                    $courseArray = bracketsToArray($course);
5798
                    $course_code = $courseArray[0];
5799
5800
                    if (CourseManager::course_exists($course_code)) {
5801
                        $courseInfo = api_get_course_info($course_code);
5802
                        $courseId = $courseInfo['real_id'];
5803
5804
                        // Adding the course to a session.
5805
                        $sql = "INSERT IGNORE INTO $tbl_session_course
5806
                                SET c_id = '$courseId', session_id='$session_id'";
5807
                        Database::query($sql);
5808
5809
                        self::installCourse($session_id, $courseInfo['real_id']);
5810
5811
                        if ($debug) {
5812
                            $logger->addInfo("Adding course '$course_code' to session #$session_id");
5813
                        }
5814
5815
                        $course_counter++;
5816
                        $course_coaches = isset($courseArray[1]) ? $courseArray[1] : null;
5817
                        $course_users = isset($courseArray[2]) ? $courseArray[2] : null;
5818
                        $course_users = explode(',', $course_users);
5819
                        $course_coaches = explode(',', $course_coaches);
5820
5821
                        // Checking if the flag is set TeachersWillBeAddedAsCoachInAllCourseSessions (course_edit.php)
5822
                        $addTeachersToSession = true;
5823
5824
                        if (array_key_exists('add_teachers_to_sessions_courses', $courseInfo)) {
5825
                            $addTeachersToSession = $courseInfo['add_teachers_to_sessions_courses'];
5826
                        }
5827
5828
                        // If any user provided for a course, use the users array.
5829
                        if (empty($course_users)) {
5830
                            if (!empty($userList)) {
5831
                                self::subscribe_users_to_session_course(
5832
                                    $userList,
5833
                                    $session_id,
5834
                                    $course_code
5835
                                );
5836
                                if ($debug) {
5837
                                    $msg = "Adding student list ".implode(', #', $userList)." to course: '$course_code' and session #$session_id";
5838
                                    $logger->addInfo($msg);
5839
                                }
5840
                            }
5841
                        }
5842
5843
                        // Adding coaches to session course user.
5844
                        if (!empty($course_coaches)) {
5845
                            $savedCoaches = [];
5846
                            // only edit if add_teachers_to_sessions_courses is set.
5847
                            if ($addTeachersToSession) {
5848
                                if ($addOriginalCourseTeachersAsCourseSessionCoaches) {
5849
                                    // Adding course teachers as course session teachers.
5850
                                    $alreadyAddedTeachers = CourseManager::get_teacher_list_from_course_code(
5851
                                        $course_code
5852
                                    );
5853
5854
                                    if (!empty($alreadyAddedTeachers)) {
5855
                                        $teachersToAdd = [];
5856
                                        foreach ($alreadyAddedTeachers as $user) {
5857
                                            $teachersToAdd[] = $user['username'];
5858
                                        }
5859
                                        $course_coaches = array_merge(
5860
                                            $course_coaches,
5861
                                            $teachersToAdd
5862
                                        );
5863
                                    }
5864
                                }
5865
5866
                                foreach ($course_coaches as $course_coach) {
5867
                                    $coach_id = UserManager::get_user_id_from_username($course_coach);
5868
                                    if ($coach_id !== false) {
5869
                                        // Just insert new coaches
5870
                                        self::updateCoaches(
5871
                                            $session_id,
5872
                                            $courseId,
5873
                                            [$coach_id],
5874
                                            false
5875
                                        );
5876
5877
                                        if ($debug) {
5878
                                            $logger->addInfo("Adding course coach: user #$coach_id ($course_coach) to course: '$course_code' and session #$session_id");
5879
                                        }
5880
                                        $savedCoaches[] = $coach_id;
5881
                                    } else {
5882
                                        $error_message .= get_lang('UserDoesNotExist').' : '.$course_coach.$eol;
5883
                                    }
5884
                                }
5885
                            }
5886
5887
                            // Custom courses/session coaches
5888
                            $teacherToAdd = null;
5889
                            // Only one coach is added.
5890
                            if ($onlyAddFirstCoachOrTeacher == true) {
5891
                                if ($debug) {
5892
                                    $logger->addInfo("onlyAddFirstCoachOrTeacher : true");
5893
                                }
5894
5895
                                foreach ($course_coaches as $course_coach) {
5896
                                    $coach_id = UserManager::get_user_id_from_username($course_coach);
5897
                                    if ($coach_id !== false) {
5898
                                        $teacherToAdd = $coach_id;
5899
                                        break;
5900
                                    }
5901
                                }
5902
5903
                                // Un subscribe everyone that's not in the list.
5904
                                $teacherList = CourseManager::get_teacher_list_from_course_code($course_code);
5905
                                if (!empty($teacherList)) {
5906
                                    foreach ($teacherList as $teacher) {
5907
                                        if ($teacherToAdd != $teacher['user_id']) {
5908
                                            $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
5909
                                                    WHERE
5910
                                                        user_id = ".$teacher['user_id']." AND
5911
                                                        c_id = '".$courseId."'
5912
                                                    ";
5913
5914
                                            $result = Database::query($sql);
5915
                                            $rows = Database::num_rows($result);
5916
                                            if ($rows > 0) {
5917
                                                $userCourseData = Database::fetch_array($result, 'ASSOC');
5918
                                                if (!empty($userCourseData)) {
5919
                                                    $teacherBackupList[$teacher['user_id']][$course_code] = $userCourseData;
5920
                                                }
5921
                                            }
5922
5923
                                            $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_USER)."
5924
                                                    WHERE
5925
                                                        user_id = ".$teacher['user_id']." AND
5926
                                                        c_id = '".$courseInfo['real_id']."'
5927
                                                    ";
5928
5929
                                            $result = Database::query($sql);
5930
                                            while ($groupData = Database::fetch_array($result, 'ASSOC')) {
5931
                                                $groupBackup['user'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData;
5932
                                            }
5933
5934
                                            $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)."
5935
                                                    WHERE
5936
                                                        user_id = ".$teacher['user_id']." AND
5937
                                                        c_id = '".$courseInfo['real_id']."'
5938
                                                    ";
5939
5940
                                            $result = Database::query($sql);
5941
                                            while ($groupData = Database::fetch_array($result, 'ASSOC')) {
5942
                                                $groupBackup['tutor'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData;
5943
                                            }
5944
5945
                                            CourseManager::unsubscribe_user(
5946
                                                $teacher['user_id'],
5947
                                                $course_code
5948
                                            );
5949
5950
                                            if ($debug) {
5951
                                                $logger->addInfo("Delete user #".$teacher['user_id']." from base course: $course_code");
5952
                                            }
5953
                                        }
5954
                                    }
5955
                                }
5956
5957
                                if (!empty($teacherToAdd)) {
5958
                                    self::updateCoaches(
5959
                                        $session_id,
5960
                                        $courseId,
5961
                                        [$teacherToAdd],
5962
                                        true
5963
                                    );
5964
5965
                                    if ($debug) {
5966
                                        $logger->addInfo("Add coach #$teacherToAdd to course $courseId and session $session_id");
5967
                                    }
5968
5969
                                    $userCourseCategory = '';
5970
                                    if (isset($teacherBackupList[$teacherToAdd]) &&
5971
                                        isset($teacherBackupList[$teacherToAdd][$course_code])
5972
                                    ) {
5973
                                        $courseUserData = $teacherBackupList[$teacherToAdd][$course_code];
5974
                                        $userCourseCategory = $courseUserData['user_course_cat'];
5975
                                    }
5976
5977
                                    CourseManager::subscribeUser(
5978
                                        $teacherToAdd,
5979
                                        $course_code,
5980
                                        COURSEMANAGER,
5981
                                        0,
5982
                                        $userCourseCategory
5983
                                    );
5984
5985
                                    if ($debug) {
5986
                                        $logger->addInfo("Subscribe user #$teacherToAdd as teacher in course $course_code with user userCourseCategory $userCourseCategory");
5987
                                    }
5988
5989
                                    if (isset($groupBackup['user'][$teacherToAdd]) &&
5990
                                        isset($groupBackup['user'][$teacherToAdd][$course_code]) &&
5991
                                        !empty($groupBackup['user'][$teacherToAdd][$course_code])
5992
                                    ) {
5993
                                        foreach ($groupBackup['user'][$teacherToAdd][$course_code] as $data) {
5994
                                            $groupInfo = GroupManager::get_group_properties($data['group_id']);
5995
                                            GroupManager::subscribe_users(
5996
                                                $teacherToAdd,
5997
                                                $groupInfo,
5998
                                                $data['c_id']
5999
                                            );
6000
                                        }
6001
                                    }
6002
6003
                                    if (isset($groupBackup['tutor'][$teacherToAdd]) &&
6004
                                        isset($groupBackup['tutor'][$teacherToAdd][$course_code]) &&
6005
                                        !empty($groupBackup['tutor'][$teacherToAdd][$course_code])
6006
                                    ) {
6007
                                        foreach ($groupBackup['tutor'][$teacherToAdd][$course_code] as $data) {
6008
                                            $groupInfo = GroupManager::get_group_properties($data['group_id']);
6009
                                            GroupManager::subscribe_tutors(
6010
                                                $teacherToAdd,
6011
                                                $groupInfo,
6012
                                                $data['c_id']
6013
                                            );
6014
                                        }
6015
                                    }
6016
                                }
6017
                            }
6018
6019
                            // See BT#6449#note-195
6020
                            // All coaches are added.
6021
                            if ($removeAllTeachersFromCourse) {
6022
                                if ($debug) {
6023
                                    $logger->addInfo("removeAllTeachersFromCourse true");
6024
                                }
6025
                                $teacherToAdd = null;
6026
                                foreach ($course_coaches as $course_coach) {
6027
                                    $coach_id = UserManager::get_user_id_from_username(
6028
                                        $course_coach
6029
                                    );
6030
                                    if ($coach_id !== false) {
6031
                                        $teacherToAdd[] = $coach_id;
6032
                                    }
6033
                                }
6034
6035
                                if (!empty($teacherToAdd)) {
6036
                                    // Deleting all course teachers and adding the only coach as teacher.
6037
                                    $teacherList = CourseManager::get_teacher_list_from_course_code($course_code);
6038
6039
                                    if (!empty($teacherList)) {
6040
                                        foreach ($teacherList as $teacher) {
6041
                                            if (!in_array($teacher['user_id'], $teacherToAdd)) {
6042
                                                $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
6043
                                                        WHERE
6044
                                                            user_id = ".$teacher['user_id']." AND
6045
                                                            c_id = '".$courseId."'
6046
                                                        ";
6047
6048
                                                $result = Database::query($sql);
6049
                                                $rows = Database::num_rows($result);
6050
                                                if ($rows > 0) {
6051
                                                    $userCourseData = Database::fetch_array($result, 'ASSOC');
6052
                                                    if (!empty($userCourseData)) {
6053
                                                        $teacherBackupList[$teacher['user_id']][$course_code] = $userCourseData;
6054
                                                    }
6055
                                                }
6056
6057
                                                $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_USER)."
6058
                                                        WHERE
6059
                                                            user_id = ".$teacher['user_id']." AND
6060
                                                            c_id = '".$courseInfo['real_id']."'
6061
                                                        ";
6062
6063
                                                $result = Database::query($sql);
6064
                                                while ($groupData = Database::fetch_array($result, 'ASSOC')) {
6065
                                                    $groupBackup['user'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData;
6066
                                                }
6067
6068
                                                $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)."
6069
                                                        WHERE
6070
                                                            user_id = ".$teacher['user_id']." AND
6071
                                                            c_id = '".$courseInfo['real_id']."'
6072
                                                        ";
6073
6074
                                                $result = Database::query($sql);
6075
                                                while ($groupData = Database::fetch_array($result, 'ASSOC')) {
6076
                                                    $groupBackup['tutor'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData;
6077
                                                }
6078
6079
                                                CourseManager::unsubscribe_user(
6080
                                                    $teacher['user_id'],
6081
                                                    $course_code
6082
                                                );
6083
6084
                                                if ($debug) {
6085
                                                    $logger->addInfo("Delete user #".$teacher['user_id']." from base course: $course_code");
6086
                                                }
6087
                                            }
6088
                                        }
6089
                                    }
6090
6091
                                    foreach ($teacherToAdd as $teacherId) {
6092
                                        $userCourseCategory = '';
6093
                                        if (isset($teacherBackupList[$teacherId]) &&
6094
                                            isset($teacherBackupList[$teacherId][$course_code])
6095
                                        ) {
6096
                                            $courseUserData = $teacherBackupList[$teacherId][$course_code];
6097
                                            $userCourseCategory = $courseUserData['user_course_cat'];
6098
                                        }
6099
6100
                                        CourseManager::subscribeUser(
6101
                                            $teacherId,
6102
                                            $course_code,
6103
                                            COURSEMANAGER,
6104
                                            0,
6105
                                            $userCourseCategory
6106
                                        );
6107
6108
                                        if ($debug) {
6109
                                            $logger->addInfo("Add user as teacher #".$teacherId." in base course: $course_code with userCourseCategory: $userCourseCategory");
6110
                                        }
6111
6112
                                        if (isset($groupBackup['user'][$teacherId]) &&
6113
                                            isset($groupBackup['user'][$teacherId][$course_code]) &&
6114
                                            !empty($groupBackup['user'][$teacherId][$course_code])
6115
                                        ) {
6116
                                            foreach ($groupBackup['user'][$teacherId][$course_code] as $data) {
6117
                                                $groupInfo = GroupManager::get_group_properties($data['group_id']);
6118
                                                GroupManager::subscribe_users(
6119
                                                    $teacherId,
6120
                                                    $groupInfo,
6121
                                                    $data['c_id']
6122
                                                );
6123
                                            }
6124
                                        }
6125
6126
                                        if (isset($groupBackup['tutor'][$teacherId]) &&
6127
                                            isset($groupBackup['tutor'][$teacherId][$course_code]) &&
6128
                                            !empty($groupBackup['tutor'][$teacherId][$course_code])
6129
                                        ) {
6130
                                            foreach ($groupBackup['tutor'][$teacherId][$course_code] as $data) {
6131
                                                $groupInfo = GroupManager::get_group_properties($data['group_id']);
6132
                                                GroupManager::subscribe_tutors(
6133
                                                    $teacherId,
6134
                                                    $groupInfo,
6135
                                                    $data['c_id']
6136
                                                );
6137
                                            }
6138
                                        }
6139
                                    }
6140
                                }
6141
                            }
6142
6143
                            // Continue default behaviour.
6144
                            if ($onlyAddFirstCoachOrTeacher == false) {
6145
                                // Checking one more time see BT#6449#note-149
6146
                                $coaches = self::getCoachesByCourseSession($session_id, $courseId);
6147
                                // Update coaches if only there's 1 course see BT#6449#note-189
6148
                                if (empty($coaches) || count($courses) == 1) {
6149
                                    foreach ($course_coaches as $course_coach) {
6150
                                        $course_coach = trim($course_coach);
6151
                                        $coach_id = UserManager::get_user_id_from_username($course_coach);
6152
                                        if ($coach_id !== false) {
6153
                                            // Just insert new coaches
6154
                                            self::updateCoaches(
6155
                                                $session_id,
6156
                                                $courseId,
6157
                                                [$coach_id],
6158
                                                false
6159
                                            );
6160
6161
                                            if ($debug) {
6162
                                                $logger->addInfo("Sessions - Adding course coach: user #$coach_id ($course_coach) to course: '$course_code' and session #$session_id");
6163
                                            }
6164
                                            $savedCoaches[] = $coach_id;
6165
                                        } else {
6166
                                            $error_message .= get_lang('UserDoesNotExist').' : '.$course_coach.$eol;
6167
                                        }
6168
                                    }
6169
                                }
6170
                            }
6171
                        }
6172
6173
                        // Adding Students, updating relationship "Session - Course - User".
6174
                        $course_users = array_filter($course_users);
6175
                        if (!empty($course_users)) {
6176
                            foreach ($course_users as $user) {
6177
                                $user_id = UserManager::get_user_id_from_username($user);
6178
6179
                                if ($user_id !== false) {
6180
                                    self::subscribe_users_to_session_course(
6181
                                        [$user_id],
6182
                                        $session_id,
6183
                                        $course_code
6184
                                    );
6185
                                    if ($debug) {
6186
                                        $logger->addInfo("Adding student: user #$user_id ($user) to course: '$course_code' and session #$session_id");
6187
                                    }
6188
                                } else {
6189
                                    $error_message .= get_lang('UserDoesNotExist').': '.$user.$eol;
6190
                                }
6191
                            }
6192
                        }
6193
                        $inserted_in_course[$course_code] = $courseInfo['title'];
6194
                    }
6195
                }
6196
                $access_url_id = api_get_current_access_url_id();
6197
                UrlManager::add_session_to_url($session_id, $access_url_id);
6198
                $sql = "UPDATE $tbl_session SET nbr_users = '$user_counter', nbr_courses = '$course_counter'
6199
                        WHERE id = '$session_id'";
6200
                Database::query($sql);
6201
6202
                self::addClassesByName($session_id, $classes, false);
6203
6204
                if ($debug) {
6205
                    $logger->addInfo("End process session #$session_id -------------------- ");
6206
                }
6207
            }
6208
6209
            if (!empty($report)) {
6210
                if ($debug) {
6211
                    $logger->addInfo("--Summary--");
6212
                    foreach ($report as $line) {
6213
                        $logger->addInfo($line);
6214
                    }
6215
                }
6216
            }
6217
        }
6218
6219
        return [
6220
            'error_message' => $error_message,
6221
            'session_counter' => $session_counter,
6222
            'session_list' => $sessionList,
6223
        ];
6224
    }
6225
6226
    /**
6227
     * @param int $sessionId
6228
     * @param int $courseId
6229
     *
6230
     * @return array
6231
     */
6232
    public static function getCoachesByCourseSession($sessionId, $courseId)
6233
    {
6234
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
6235
        $sessionId = (int) $sessionId;
6236
        $courseId = (int) $courseId;
6237
6238
        $sql = "SELECT user_id FROM $table
6239
                WHERE
6240
                    session_id = '$sessionId' AND
6241
                    c_id = '$courseId' AND
6242
                    status = 2";
6243
        $result = Database::query($sql);
6244
6245
        $coaches = [];
6246
        if (Database::num_rows($result) > 0) {
6247
            while ($row = Database::fetch_array($result)) {
6248
                $coaches[] = $row['user_id'];
6249
            }
6250
        }
6251
6252
        return $coaches;
6253
    }
6254
6255
    /**
6256
     * @param int    $sessionId
6257
     * @param int    $courseId
6258
     * @param string $separator
6259
     *
6260
     * @return string
6261
     */
6262
    public static function getCoachesByCourseSessionToString(
6263
        $sessionId,
6264
        $courseId,
6265
        $separator = ''
6266
    ) {
6267
        $coaches = self::getCoachesByCourseSession($sessionId, $courseId);
6268
        $list = [];
6269
        if (!empty($coaches)) {
6270
            foreach ($coaches as $coachId) {
6271
                $userInfo = api_get_user_info($coachId);
6272
                if ($userInfo) {
6273
                    $list[] = $userInfo['complete_name'];
6274
                }
6275
            }
6276
        }
6277
6278
        $separator = empty($separator) ? CourseManager::USER_SEPARATOR : $separator;
6279
6280
        return array_to_string($list, $separator);
6281
    }
6282
6283
    /**
6284
     * Get all coaches added in the session - course relationship.
6285
     *
6286
     * @param int $sessionId
6287
     *
6288
     * @return array
6289
     */
6290
    public static function getCoachesBySession($sessionId)
6291
    {
6292
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
6293
        $sessionId = intval($sessionId);
6294
6295
        $sql = "SELECT DISTINCT user_id
6296
                FROM $table
6297
                WHERE session_id = '$sessionId' AND status = 2";
6298
        $result = Database::query($sql);
6299
6300
        $coaches = [];
6301
        if (Database::num_rows($result) > 0) {
6302
            while ($row = Database::fetch_array($result)) {
6303
                $coaches[] = $row['user_id'];
6304
            }
6305
        }
6306
6307
        return $coaches;
6308
    }
6309
6310
    /**
6311
     * @param int $userId
6312
     *
6313
     * @return array
6314
     */
6315
    public static function getAllCoursesFromAllSessionFromDrh($userId)
6316
    {
6317
        $sessions = self::get_sessions_followed_by_drh($userId);
6318
        $coursesFromSession = [];
6319
        if (!empty($sessions)) {
6320
            foreach ($sessions as $session) {
6321
                $courseList = self::get_course_list_by_session_id($session['id']);
6322
                foreach ($courseList as $course) {
6323
                    $coursesFromSession[] = $course['code'];
6324
                }
6325
            }
6326
        }
6327
6328
        return $coursesFromSession;
6329
    }
6330
6331
    /**
6332
     * getAllCoursesFromAllSessions.
6333
     *
6334
     * @return array
6335
     */
6336
    public static function getAllCoursesFromAllSessions()
6337
    {
6338
        $sessions = self::get_sessions_list();
6339
        $coursesFromSession = [];
6340
        if (!empty($sessions)) {
6341
            foreach ($sessions as $session) {
6342
                $courseList = self::get_course_list_by_session_id($session['id']);
6343
                foreach ($courseList as $course) {
6344
                    $coursesFromSession[$course['code'].':'.$session['id']] = $course['visual_code'].' - '.$course['title'].' ('.$session['name'].')';
6345
                }
6346
            }
6347
        }
6348
6349
        return $coursesFromSession;
6350
    }
6351
6352
    /**
6353
     * Return user id list or count of users depending of the $getCount parameter.
6354
     *
6355
     * @param string $status
6356
     * @param int    $userId
6357
     * @param bool   $getCount
6358
     * @param int    $from
6359
     * @param int    $numberItems
6360
     * @param int    $column
6361
     * @param string $direction
6362
     * @param string $keyword
6363
     * @param string $active
6364
     * @param string $lastConnectionDate
6365
     * @param array  $sessionIdList
6366
     * @param array  $studentIdList
6367
     * @param int    $filterByStatus
6368
     *
6369
     * @return array|int
6370
     */
6371
    public static function getAllUsersFromCoursesFromAllSessionFromStatus(
6372
        $status,
6373
        $userId,
6374
        $getCount = false,
6375
        $from = null,
6376
        $numberItems = null,
6377
        $column = 1,
6378
        $direction = 'asc',
6379
        $keyword = null,
6380
        $active = null,
6381
        $lastConnectionDate = null,
6382
        $sessionIdList = [],
6383
        $studentIdList = [],
6384
        $filterByStatus = null
6385
    ) {
6386
        $filterByStatus = (int) $filterByStatus;
6387
        $userId = (int) $userId;
6388
6389
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
6390
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
6391
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
6392
        $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
6393
        $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
6394
        $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
6395
        $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
6396
        $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
6397
6398
        $direction = in_array(strtolower($direction), ['asc', 'desc']) ? $direction : 'asc';
6399
        $column = Database::escape_string($column);
6400
6401
        $limitCondition = '';
6402
        if (isset($from) && isset($numberItems)) {
6403
            $from = (int) $from;
6404
            $numberItems = (int) $numberItems;
6405
            $limitCondition = "LIMIT $from, $numberItems";
6406
        }
6407
6408
        $urlId = api_get_current_access_url_id();
6409
6410
        $sessionConditions = '';
6411
        $courseConditions = '';
6412
        $userConditions = '';
6413
6414
        if (isset($active)) {
6415
            $active = (int) $active;
6416
            $userConditions .= " AND active = $active";
6417
        }
6418
6419
        $courseList = CourseManager::get_courses_followed_by_drh($userId, DRH);
6420
        if (!empty($courseList)) {
6421
            $courseIdList = array_column($courseList, 'id');
6422
            $courseConditions = ' AND c.id IN ("'.implode('","', $courseIdList).'")';
6423
        }
6424
6425
        $userConditionsFromDrh = '';
6426
6427
        // Classic DRH
6428
        if (empty($studentIdList)) {
6429
            $studentListSql = UserManager::get_users_followed_by_drh(
6430
                $userId,
6431
                $filterByStatus,
6432
                true,
6433
                false
6434
            );
6435
            if (!empty($studentListSql)) {
6436
                $studentIdList = array_keys($studentListSql);
6437
                $studentListSql = "'".implode("','", $studentIdList)."'";
6438
            }
6439
        } else {
6440
            $studentIdList = array_map('intval', $studentIdList);
6441
            $studentListSql = "'".implode("','", $studentIdList)."'";
6442
        }
6443
        if (!empty($studentListSql)) {
6444
            $userConditionsFromDrh = " AND u.user_id IN ($studentListSql) ";
6445
        }
6446
6447
        switch ($status) {
6448
            case 'drh':
6449
                break;
6450
            case 'drh_all':
6451
                // Show all by DRH
6452
                if (empty($sessionIdList)) {
6453
                    $sessionsListSql = self::get_sessions_followed_by_drh(
6454
                        $userId,
6455
                        null,
6456
                        null,
6457
                        false,
6458
                        true,
6459
                        true
6460
                    );
6461
                } else {
6462
                    $sessionIdList = array_map('intval', $sessionIdList);
6463
                    $sessionsListSql = "'".implode("','", $sessionIdList)."'";
6464
                }
6465
                if (!empty($sessionsListSql)) {
6466
                    $sessionConditions = " AND s.id IN ($sessionsListSql) ";
6467
                }
6468
                break;
6469
            case 'session_admin':
6470
                $sessionConditions = " AND s.id_coach = $userId ";
6471
                $userConditionsFromDrh = '';
6472
                break;
6473
            case 'admin':
6474
                break;
6475
            case 'teacher':
6476
                $sessionConditions = " AND s.id_coach = $userId ";
6477
                $userConditionsFromDrh = '';
6478
                break;
6479
        }
6480
6481
        $select = 'SELECT DISTINCT u.* ';
6482
        $masterSelect = 'SELECT DISTINCT user_id FROM ';
6483
6484
        if ($getCount) {
6485
            $select = 'SELECT DISTINCT u.user_id ';
6486
            $masterSelect = 'SELECT COUNT(DISTINCT(user_id)) as count FROM ';
6487
        }
6488
6489
        if (!empty($filterByStatus)) {
6490
            $userConditions .= " AND u.status = $filterByStatus";
6491
        }
6492
6493
        if (!empty($lastConnectionDate)) {
6494
            $lastConnectionDate = Database::escape_string($lastConnectionDate);
6495
            $userConditions .= " AND u.last_login <= '$lastConnectionDate' ";
6496
        }
6497
6498
        if (!empty($keyword)) {
6499
            $keyword = Database::escape_string($keyword);
6500
            $userConditions .= " AND (
6501
                u.username LIKE '%$keyword%' OR
6502
                u.firstname LIKE '%$keyword%' OR
6503
                u.lastname LIKE '%$keyword%' OR
6504
                u.official_code LIKE '%$keyword%' OR
6505
                u.email LIKE '%$keyword%'
6506
            )";
6507
        }
6508
6509
        $where = " WHERE
6510
                   access_url_id = $urlId
6511
                   $userConditions
6512
        ";
6513
6514
        $userUnion = '';
6515
        if (!empty($userConditionsFromDrh)) {
6516
            $userUnion = "
6517
            UNION (
6518
                $select
6519
                FROM $tbl_user u
6520
                INNER JOIN $tbl_user_rel_access_url url ON (url.user_id = u.id)
6521
                $where
6522
                $userConditionsFromDrh
6523
            )";
6524
        }
6525
6526
        $sql = "$masterSelect (
6527
                ($select
6528
                FROM $tbl_session s
6529
                    INNER JOIN $tbl_session_rel_course_rel_user su ON (s.id = su.session_id)
6530
                    INNER JOIN $tbl_user u ON (u.user_id = su.user_id)
6531
                    INNER JOIN $tbl_session_rel_access_url url ON (url.session_id = s.id)
6532
                    $where
6533
                    $sessionConditions
6534
                    $userConditionsFromDrh
6535
                ) UNION (
6536
                    $select
6537
                    FROM $tbl_course c
6538
                    INNER JOIN $tbl_course_user cu ON (cu.c_id = c.id)
6539
                    INNER JOIN $tbl_user u ON (u.user_id = cu.user_id)
6540
                    INNER JOIN $tbl_course_rel_access_url url ON (url.c_id = c.id)
6541
                    $where
6542
                    $courseConditions
6543
                    $userConditionsFromDrh
6544
                ) $userUnion
6545
                ) as t1
6546
                ";
6547
6548
        if ($getCount) {
6549
            $result = Database::query($sql);
6550
6551
            $count = 0;
6552
            if (Database::num_rows($result)) {
6553
                $rows = Database::fetch_array($result);
6554
                $count = $rows['count'];
6555
            }
6556
6557
            return $count;
6558
        }
6559
6560
        if (!empty($column) && !empty($direction)) {
6561
            $column = str_replace('u.', '', $column);
6562
            $sql .= " ORDER BY `$column` $direction ";
6563
        }
6564
        $sql .= $limitCondition;
6565
        $result = Database::query($sql);
6566
        $result = Database::store_result($result);
6567
6568
        return $result;
6569
    }
6570
6571
    /**
6572
     * @param int   $sessionId
6573
     * @param int   $courseId
6574
     * @param array $coachList
6575
     * @param bool  $deleteCoachesNotInList
6576
     */
6577
    public static function updateCoaches(
6578
        $sessionId,
6579
        $courseId,
6580
        $coachList,
6581
        $deleteCoachesNotInList = false
6582
    ) {
6583
        $currentCoaches = self::getCoachesByCourseSession($sessionId, $courseId);
6584
6585
        if (!empty($coachList)) {
6586
            foreach ($coachList as $userId) {
6587
                self::set_coach_to_course_session($userId, $sessionId, $courseId);
6588
            }
6589
        }
6590
6591
        if ($deleteCoachesNotInList) {
6592
            if (!empty($coachList)) {
6593
                $coachesToDelete = array_diff($currentCoaches, $coachList);
6594
            } else {
6595
                $coachesToDelete = $currentCoaches;
6596
            }
6597
6598
            if (!empty($coachesToDelete)) {
6599
                foreach ($coachesToDelete as $userId) {
6600
                    self::set_coach_to_course_session(
6601
                        $userId,
6602
                        $sessionId,
6603
                        $courseId,
6604
                        true
6605
                    );
6606
                }
6607
            }
6608
        }
6609
    }
6610
6611
    /**
6612
     * @param array $sessions
6613
     * @param array $sessionsDestination
6614
     *
6615
     * @return array
6616
     */
6617
    public static function copyStudentsFromSession($sessions, $sessionsDestination)
6618
    {
6619
        $messages = [];
6620
        if (!empty($sessions)) {
6621
            foreach ($sessions as $sessionId) {
6622
                $sessionInfo = self::fetch($sessionId);
6623
                $userList = self::get_users_by_session($sessionId, 0);
6624
                if (!empty($userList)) {
6625
                    $newUserList = [];
6626
                    $userToString = null;
6627
                    foreach ($userList as $userInfo) {
6628
                        $newUserList[] = $userInfo['user_id'];
6629
                        $userToString .= $userInfo['firstname'].' '.$userInfo['lastname'].'<br />';
6630
                    }
6631
6632
                    if (!empty($sessionsDestination)) {
6633
                        foreach ($sessionsDestination as $sessionDestinationId) {
6634
                            $sessionDestinationInfo = self::fetch($sessionDestinationId);
6635
                            $messages[] = Display::return_message(
6636
                                sprintf(
6637
                                    get_lang(
6638
                                        'AddingStudentsFromSessionXToSessionY'
6639
                                    ),
6640
                                    $sessionInfo['name'],
6641
                                    $sessionDestinationInfo['name']
6642
                                ),
6643
                                'info',
6644
                                false
6645
                            );
6646
                            if ($sessionId == $sessionDestinationId) {
6647
                                $messages[] = Display::return_message(
6648
                                    sprintf(
6649
                                        get_lang('SessionXSkipped'),
6650
                                        $sessionDestinationId
6651
                                    ),
6652
                                    'warning',
6653
                                    false
6654
                                );
6655
                                continue;
6656
                            }
6657
                            $messages[] = Display::return_message(get_lang('StudentList').'<br />'.$userToString, 'info', false);
6658
                            self::subscribeUsersToSession(
6659
                                $sessionDestinationId,
6660
                                $newUserList,
6661
                                SESSION_VISIBLE_READ_ONLY,
6662
                                false
6663
                            );
6664
                        }
6665
                    } else {
6666
                        $messages[] = Display::return_message(get_lang('NoDestinationSessionProvided'), 'warning');
6667
                    }
6668
                } else {
6669
                    $messages[] = Display::return_message(
6670
                        get_lang('NoStudentsFoundForSession').' #'.$sessionInfo['name'],
6671
                        'warning'
6672
                    );
6673
                }
6674
            }
6675
        } else {
6676
            $messages[] = Display::return_message(get_lang('NoData'), 'warning');
6677
        }
6678
6679
        return $messages;
6680
    }
6681
6682
    /**
6683
     * Assign coaches of a session(s) as teachers to a given course (or courses).
6684
     *
6685
     * @param array A list of session IDs
6686
     * @param array A list of course IDs
6687
     *
6688
     * @return string
6689
     */
6690
    public static function copyCoachesFromSessionToCourse($sessions, $courses)
6691
    {
6692
        $coachesPerSession = [];
6693
        foreach ($sessions as $sessionId) {
6694
            $coaches = self::getCoachesBySession($sessionId);
6695
            $coachesPerSession[$sessionId] = $coaches;
6696
        }
6697
6698
        $result = [];
6699
6700
        if (!empty($courses)) {
6701
            foreach ($courses as $courseId) {
6702
                $courseInfo = api_get_course_info_by_id($courseId);
6703
                foreach ($coachesPerSession as $sessionId => $coachList) {
6704
                    CourseManager::updateTeachers(
6705
                        $courseInfo,
6706
                        $coachList,
6707
                        false,
6708
                        false,
6709
                        false
6710
                    );
6711
                    $result[$courseInfo['code']][$sessionId] = $coachList;
6712
                }
6713
            }
6714
        }
6715
        $sessionUrl = api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session=';
6716
        $htmlResult = null;
6717
6718
        if (!empty($result)) {
6719
            foreach ($result as $courseCode => $data) {
6720
                $url = api_get_course_url($courseCode);
6721
                $htmlResult .= sprintf(
6722
                    get_lang('CoachesSubscribedAsATeacherInCourseX'),
6723
                    Display::url($courseCode, $url, ['target' => '_blank'])
6724
                );
6725
                foreach ($data as $sessionId => $coachList) {
6726
                    $sessionInfo = self::fetch($sessionId);
6727
                    $htmlResult .= '<br />';
6728
                    $htmlResult .= Display::url(
6729
                        get_lang('Session').': '.$sessionInfo['name'].' <br />',
6730
                        $sessionUrl.$sessionId,
6731
                        ['target' => '_blank']
6732
                    );
6733
                    $teacherList = [];
6734
                    foreach ($coachList as $coachId) {
6735
                        $userInfo = api_get_user_info($coachId);
6736
                        $teacherList[] = $userInfo['complete_name'];
6737
                    }
6738
                    if (!empty($teacherList)) {
6739
                        $htmlResult .= implode(', ', $teacherList);
6740
                    } else {
6741
                        $htmlResult .= get_lang('NothingToAdd');
6742
                    }
6743
                }
6744
                $htmlResult .= '<br />';
6745
            }
6746
            $htmlResult = Display::return_message($htmlResult, 'normal', false);
6747
        }
6748
6749
        return $htmlResult;
6750
    }
6751
6752
    /**
6753
     * @param string $keyword
6754
     * @param string $active
6755
     * @param string $lastConnectionDate
6756
     * @param array  $sessionIdList
6757
     * @param array  $studentIdList
6758
     * @param int    $filterUserStatus   STUDENT|COURSEMANAGER constants
6759
     *
6760
     * @return array|int
6761
     */
6762
    public static function getCountUserTracking(
6763
        $keyword = null,
6764
        $active = null,
6765
        $lastConnectionDate = null,
6766
        $sessionIdList = [],
6767
        $studentIdList = [],
6768
        $filterUserStatus = null
6769
    ) {
6770
        $userId = api_get_user_id();
6771
        $drhLoaded = false;
6772
6773
        if (api_is_drh()) {
6774
            if (api_drh_can_access_all_session_content()) {
6775
                $count = self::getAllUsersFromCoursesFromAllSessionFromStatus(
6776
                    'drh_all',
6777
                    $userId,
6778
                    true,
6779
                    null,
6780
                    null,
6781
                    null,
6782
                    null,
6783
                    $keyword,
6784
                    $active,
6785
                    $lastConnectionDate,
6786
                    $sessionIdList,
6787
                    $studentIdList,
6788
                    $filterUserStatus
6789
                );
6790
                $drhLoaded = true;
6791
            }
6792
        }
6793
6794
        if ($drhLoaded == false) {
6795
            $count = UserManager::getUsersFollowedByUser(
6796
                $userId,
6797
                $filterUserStatus,
6798
                false,
6799
                false,
6800
                true,
6801
                null,
6802
                null,
6803
                null,
6804
                null,
6805
                $active,
6806
                $lastConnectionDate,
6807
                api_is_student_boss() ? STUDENT_BOSS : COURSEMANAGER,
6808
                $keyword
6809
            );
6810
        }
6811
6812
        return $count;
6813
    }
6814
6815
    /**
6816
     * Get teachers followed by a user.
6817
     *
6818
     * @param int    $userId
6819
     * @param int    $active
6820
     * @param string $lastConnectionDate
6821
     * @param bool   $getCount
6822
     * @param array  $sessionIdList
6823
     *
6824
     * @return array|int
6825
     */
6826
    public static function getTeacherTracking(
6827
        $userId,
6828
        $active = 1,
6829
        $lastConnectionDate = null,
6830
        $getCount = false,
6831
        $sessionIdList = []
6832
    ) {
6833
        $teacherListId = [];
6834
        if (api_is_drh() || api_is_platform_admin()) {
6835
            // Followed teachers by drh
6836
            if (api_drh_can_access_all_session_content()) {
6837
                if (empty($sessionIdList)) {
6838
                    $sessions = self::get_sessions_followed_by_drh($userId);
6839
                    $sessionIdList = [];
6840
                    foreach ($sessions as $session) {
6841
                        $sessionIdList[] = $session['id'];
6842
                    }
6843
                }
6844
6845
                $sessionIdList = array_map('intval', $sessionIdList);
6846
                $sessionToString = implode("', '", $sessionIdList);
6847
6848
                $course = Database::get_main_table(TABLE_MAIN_COURSE);
6849
                $sessionCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
6850
                $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
6851
6852
                // Select the teachers.
6853
                $sql = "SELECT DISTINCT(cu.user_id)
6854
                        FROM $course c
6855
                        INNER JOIN $sessionCourse src
6856
                        ON c.id = src.c_id
6857
                        INNER JOIN $courseUser cu
6858
                        ON (cu.c_id = c.id)
6859
		                WHERE src.session_id IN ('$sessionToString') AND cu.status = 1";
6860
                $result = Database::query($sql);
6861
                while ($row = Database::fetch_array($result, 'ASSOC')) {
6862
                    $teacherListId[$row['user_id']] = $row['user_id'];
6863
                }
6864
            } else {
6865
                $teacherResult = UserManager::get_users_followed_by_drh($userId, COURSEMANAGER);
6866
                foreach ($teacherResult as $userInfo) {
6867
                    $teacherListId[] = $userInfo['user_id'];
6868
                }
6869
            }
6870
        }
6871
6872
        if (!empty($teacherListId)) {
6873
            $tableUser = Database::get_main_table(TABLE_MAIN_USER);
6874
6875
            $select = "SELECT DISTINCT u.* ";
6876
            if ($getCount) {
6877
                $select = "SELECT count(DISTINCT(u.user_id)) as count";
6878
            }
6879
6880
            $sql = "$select FROM $tableUser u";
6881
6882
            if (!empty($lastConnectionDate)) {
6883
                $tableLogin = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
6884
                //$sql .= " INNER JOIN $tableLogin l ON (l.login_user_id = u.user_id) ";
6885
            }
6886
            $active = intval($active);
6887
            $teacherListId = implode("','", $teacherListId);
6888
            $where = " WHERE u.active = $active AND u.user_id IN ('$teacherListId') ";
6889
6890
            if (!empty($lastConnectionDate)) {
6891
                $lastConnectionDate = Database::escape_string($lastConnectionDate);
6892
                //$where .= " AND l.login_date <= '$lastConnectionDate' ";
6893
            }
6894
6895
            $sql .= $where;
6896
            $result = Database::query($sql);
6897
            if (Database::num_rows($result)) {
6898
                if ($getCount) {
6899
                    $row = Database::fetch_array($result);
6900
6901
                    return $row['count'];
6902
                } else {
6903
                    return Database::store_result($result, 'ASSOC');
6904
                }
6905
            }
6906
        }
6907
6908
        return 0;
6909
    }
6910
6911
    /**
6912
     * Get the list of course tools that have to be dealt with in case of
6913
     * registering any course to a session.
6914
     *
6915
     * @return array The list of tools to be dealt with (literal names)
6916
     */
6917
    public static function getCourseToolToBeManaged()
6918
    {
6919
        return [
6920
            'courseDescription',
6921
            'courseIntroduction',
6922
        ];
6923
    }
6924
6925
    /**
6926
     * Calls the methods bound to each tool when a course is registered into a session.
6927
     *
6928
     * @param int $sessionId
6929
     * @param int $courseId
6930
     *
6931
     * @return bool
6932
     */
6933
    public static function installCourse($sessionId, $courseId)
6934
    {
6935
        return true;
6936
        $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...
6937
6938
        foreach ($toolList as $tool) {
6939
            $method = 'add'.$tool;
6940
            if (method_exists(get_class(), $method)) {
6941
                self::$method($sessionId, $courseId);
6942
            }
6943
        }
6944
    }
6945
6946
    /**
6947
     * Calls the methods bound to each tool when a course is unregistered from
6948
     * a session.
6949
     *
6950
     * @param int $sessionId
6951
     * @param int $courseId
6952
     */
6953
    public static function unInstallCourse($sessionId, $courseId)
6954
    {
6955
        return true;
6956
        $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...
6957
6958
        foreach ($toolList as $tool) {
6959
            $method = 'remove'.$tool;
6960
            if (method_exists(get_class(), $method)) {
6961
                self::$method($sessionId, $courseId);
6962
            }
6963
        }
6964
    }
6965
6966
    /**
6967
     * @param array $userSessionList        format see self::importSessionDrhCSV()
6968
     * @param bool  $sendEmail
6969
     * @param bool  $removeOldRelationShips
6970
     */
6971
    public static function subscribeDrhToSessionList(
6972
        $userSessionList,
6973
        $sendEmail,
6974
        $removeOldRelationShips
6975
    ) {
6976
        if (!empty($userSessionList)) {
6977
            foreach ($userSessionList as $userId => $data) {
6978
                $sessionList = [];
6979
                foreach ($data['session_list'] as $sessionInfo) {
6980
                    $sessionList[] = $sessionInfo['session_id'];
6981
                }
6982
                $userInfo = $data['user_info'];
6983
                self::subscribeSessionsToDrh(
6984
                    $userInfo,
6985
                    $sessionList,
6986
                    $sendEmail,
6987
                    $removeOldRelationShips
6988
                );
6989
            }
6990
        }
6991
    }
6992
6993
    /**
6994
     * @param array $userSessionList format see self::importSessionDrhCSV()
6995
     *
6996
     * @return string
6997
     */
6998
    public static function checkSubscribeDrhToSessionList($userSessionList)
6999
    {
7000
        $message = null;
7001
        if (!empty($userSessionList)) {
7002
            if (!empty($userSessionList)) {
7003
                foreach ($userSessionList as $userId => $data) {
7004
                    $userInfo = $data['user_info'];
7005
7006
                    $sessionListSubscribed = self::get_sessions_followed_by_drh($userId);
7007
                    if (!empty($sessionListSubscribed)) {
7008
                        $sessionListSubscribed = array_keys($sessionListSubscribed);
7009
                    }
7010
7011
                    $sessionList = [];
7012
                    if (!empty($data['session_list'])) {
7013
                        foreach ($data['session_list'] as $sessionInfo) {
7014
                            if (in_array($sessionInfo['session_id'], $sessionListSubscribed)) {
7015
                                $sessionList[] = $sessionInfo['session_info']['name'];
7016
                            }
7017
                        }
7018
                    }
7019
7020
                    $message .= '<strong>'.get_lang('User').'</strong>: ';
7021
                    $message .= $userInfo['complete_name_with_username'].' <br />';
7022
7023
                    if (!in_array($userInfo['status'], [DRH]) && !api_is_platform_admin_by_id($userInfo['user_id'])) {
7024
                        $message .= get_lang('UserMustHaveTheDrhRole').'<br />';
7025
                        continue;
7026
                    }
7027
7028
                    if (!empty($sessionList)) {
7029
                        $message .= '<strong>'.get_lang('Sessions').':</strong> <br />';
7030
                        $message .= implode(', ', $sessionList).'<br /><br />';
7031
                    } else {
7032
                        $message .= get_lang('NoSessionProvided').' <br /><br />';
7033
                    }
7034
                }
7035
            }
7036
        }
7037
7038
        return $message;
7039
    }
7040
7041
    /**
7042
     * @param string $file
7043
     * @param bool   $sendEmail
7044
     * @param bool   $removeOldRelationShips
7045
     *
7046
     * @return string
7047
     */
7048
    public static function importSessionDrhCSV($file, $sendEmail, $removeOldRelationShips)
7049
    {
7050
        $list = Import::csv_reader($file);
7051
7052
        if (!empty($list)) {
7053
            $userSessionList = [];
7054
            foreach ($list as $data) {
7055
                $userInfo = api_get_user_info_from_username($data['Username']);
7056
                $sessionInfo = self::get_session_by_name($data['SessionName']);
7057
7058
                if (empty($sessionInfo)) {
7059
                    Display::addFlash(Display::return_message(get_lang('NoSessionId').' - '.$data['SessionName'], 'warning'));
7060
                }
7061
7062
                if (empty($userInfo)) {
7063
                    Display::addFlash(Display::return_message(get_lang('UserDoesNotExist').' - '.$data['Username'], 'warning'));
7064
                }
7065
7066
                if (!empty($userInfo) && !empty($sessionInfo)) {
7067
                    $userSessionList[$userInfo['user_id']]['session_list'][] = [
7068
                        'session_id' => $sessionInfo['id'],
7069
                        'session_info' => $sessionInfo,
7070
                    ];
7071
                    $userSessionList[$userInfo['user_id']]['user_info'] = $userInfo;
7072
                }
7073
            }
7074
7075
            self::subscribeDrhToSessionList($userSessionList, $sendEmail, $removeOldRelationShips);
7076
7077
            return self::checkSubscribeDrhToSessionList($userSessionList);
7078
        }
7079
    }
7080
7081
    /**
7082
     * Courses re-ordering in resume_session.php flag see BT#8316.
7083
     */
7084
    public static function orderCourseIsEnabled()
7085
    {
7086
        $sessionCourseOrder = api_get_setting('session_course_ordering');
7087
        if ($sessionCourseOrder === 'true') {
7088
            return true;
7089
        }
7090
7091
        return false;
7092
    }
7093
7094
    /**
7095
     * @param string $direction (up/down)
7096
     * @param int    $sessionId
7097
     * @param int    $courseId
7098
     *
7099
     * @return bool
7100
     */
7101
    public static function move($direction, $sessionId, $courseId)
7102
    {
7103
        if (!self::orderCourseIsEnabled()) {
7104
            return false;
7105
        }
7106
7107
        $sessionId = intval($sessionId);
7108
        $courseId = intval($courseId);
7109
7110
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
7111
        $courseList = self::get_course_list_by_session_id($sessionId, null, 'position');
7112
7113
        $position = [];
7114
        $count = 0;
7115
        foreach ($courseList as $course) {
7116
            if ($course['position'] == '') {
7117
                $course['position'] = $count;
7118
            }
7119
            $position[$course['code']] = $course['position'];
7120
            // Saving current order.
7121
            $sql = "UPDATE $table SET position = $count
7122
                    WHERE session_id = $sessionId AND c_id = '".$course['real_id']."'";
7123
            Database::query($sql);
7124
            $count++;
7125
        }
7126
7127
        // Loading new positions.
7128
        $courseList = self::get_course_list_by_session_id($sessionId, null, 'position');
7129
7130
        $found = false;
7131
7132
        switch ($direction) {
7133
            case 'up':
7134
                $courseList = array_reverse($courseList);
7135
                break;
7136
            case 'down':
7137
                break;
7138
        }
7139
7140
        foreach ($courseList as $course) {
7141
            if ($found) {
7142
                $nextId = $course['real_id'];
7143
                $nextOrder = $course['position'];
7144
                break;
7145
            }
7146
7147
            if ($courseId == $course['real_id']) {
7148
                $thisCourseCode = $course['real_id'];
7149
                $thisOrder = $course['position'];
7150
                $found = true;
7151
            }
7152
        }
7153
7154
        $sql1 = "UPDATE $table SET position = '".intval($nextOrder)."'
7155
                 WHERE session_id = $sessionId AND c_id =  $thisCourseCode";
7156
        Database::query($sql1);
7157
7158
        $sql2 = "UPDATE $table SET position = '".intval($thisOrder)."'
7159
                 WHERE session_id = $sessionId AND c_id = $nextId";
7160
        Database::query($sql2);
7161
7162
        return true;
7163
    }
7164
7165
    /**
7166
     * @param int $sessionId
7167
     * @param int $courseId
7168
     *
7169
     * @return bool
7170
     */
7171
    public static function moveUp($sessionId, $courseId)
7172
    {
7173
        return self::move('up', $sessionId, $courseId);
7174
    }
7175
7176
    /**
7177
     * @param int    $sessionId
7178
     * @param string $courseCode
7179
     *
7180
     * @return bool
7181
     */
7182
    public static function moveDown($sessionId, $courseCode)
7183
    {
7184
        return self::move('down', $sessionId, $courseCode);
7185
    }
7186
7187
    /**
7188
     * Use the session duration to allow/block user access see BT#8317
7189
     * Needs these DB changes
7190
     * ALTER TABLE session ADD COLUMN duration int;
7191
     * ALTER TABLE session_rel_user ADD COLUMN duration int;.
7192
     */
7193
    public static function durationPerUserIsEnabled()
7194
    {
7195
        return api_get_configuration_value('session_duration_feature');
7196
    }
7197
7198
    /**
7199
     * Returns the number of days the student has left in a session when using
7200
     * sessions durations.
7201
     *
7202
     * @param int   $userId
7203
     *
7204
     * @return int
7205
     */
7206
    public static function getDayLeftInSession(array $sessionInfo, $userId)
7207
    {
7208
        $sessionId = $sessionInfo['id'];
7209
        $subscription = self::getUserSession($userId, $sessionId);
7210
        $duration = empty($subscription['duration'])
7211
            ? $sessionInfo['duration']
7212
            : $sessionInfo['duration'] + $subscription['duration'];
7213
7214
        // Get an array with the details of the first access of the student to
7215
        // this session
7216
        $courseAccess = CourseManager::getFirstCourseAccessPerSessionAndUser(
7217
            $sessionId,
7218
            $userId
7219
        );
7220
7221
        $currentTime = time();
7222
7223
        // If no previous access, return false
7224
        if (count($courseAccess) == 0) {
7225
            return $duration;
7226
        }
7227
7228
        $firstAccess = api_strtotime($courseAccess['login_course_date'], 'UTC');
7229
        $endDateInSeconds = $firstAccess + $duration * 24 * 60 * 60;
7230
        $leftDays = round(($endDateInSeconds - $currentTime) / 60 / 60 / 24);
7231
7232
        return $leftDays;
7233
    }
7234
7235
    /**
7236
     * @param int $duration
7237
     * @param int $userId
7238
     * @param int $sessionId
7239
     *
7240
     * @return bool
7241
     */
7242
    public static function editUserSessionDuration($duration, $userId, $sessionId)
7243
    {
7244
        $duration = (int) $duration;
7245
        $userId = (int) $userId;
7246
        $sessionId = (int) $sessionId;
7247
7248
        if (empty($userId) || empty($sessionId)) {
7249
            return false;
7250
        }
7251
7252
        $table = Database::get_main_table(TABLE_MAIN_SESSION_USER);
7253
        $parameters = ['duration' => $duration];
7254
        $where = ['session_id = ? AND user_id = ? ' => [$sessionId, $userId]];
7255
        Database::update($table, $parameters, $where);
7256
7257
        return true;
7258
    }
7259
7260
    /**
7261
     * Gets one row from the session_rel_user table.
7262
     *
7263
     * @param int $userId
7264
     * @param int $sessionId
7265
     *
7266
     * @return array
7267
     */
7268
    public static function getUserSession($userId, $sessionId)
7269
    {
7270
        $userId = (int) $userId;
7271
        $sessionId = (int) $sessionId;
7272
7273
        if (empty($userId) || empty($sessionId)) {
7274
            return false;
7275
        }
7276
7277
        $table = Database::get_main_table(TABLE_MAIN_SESSION_USER);
7278
        $sql = "SELECT * FROM $table
7279
                WHERE session_id = $sessionId AND user_id = $userId";
7280
        $result = Database::query($sql);
7281
        $values = [];
7282
        if (Database::num_rows($result)) {
7283
            $values = Database::fetch_array($result, 'ASSOC');
7284
        }
7285
7286
        return $values;
7287
    }
7288
7289
    /**
7290
     * Check if user is subscribed inside a session as student.
7291
     *
7292
     * @param int $sessionId The session id
7293
     * @param int $userId    The user id
7294
     *
7295
     * @return bool Whether is subscribed
7296
     */
7297
    public static function isUserSubscribedAsStudent($sessionId, $userId)
7298
    {
7299
        $sessionRelUserTable = Database::get_main_table(TABLE_MAIN_SESSION_USER);
7300
        $sessionId = (int) $sessionId;
7301
        $userId = (int) $userId;
7302
7303
        // COUNT(1) actually returns the number of rows from the table (as if
7304
        // counting the results from the first column)
7305
        $sql = "SELECT COUNT(1) AS qty FROM $sessionRelUserTable
7306
                WHERE
7307
                    session_id = $sessionId AND
7308
                    user_id = $userId AND
7309
                    relation_type = 0";
7310
7311
        $result = Database::fetch_assoc(Database::query($sql));
7312
7313
        if (!empty($result) && $result['qty'] > 0) {
7314
            return true;
7315
        }
7316
7317
        return false;
7318
    }
7319
7320
    /**
7321
     * Check if user is subscribed inside a session as a HRM.
7322
     *
7323
     * @param int $sessionId The session id
7324
     * @param int $userId    The user id
7325
     *
7326
     * @return bool Whether is subscribed
7327
     */
7328
    public static function isUserSubscribedAsHRM($sessionId, $userId)
7329
    {
7330
        $sessionRelUserTable = Database::get_main_table(TABLE_MAIN_SESSION_USER);
7331
7332
        $sessionId = (int) $sessionId;
7333
        $userId = (int) $userId;
7334
7335
        // COUNT(1) actually returns the number of rows from the table (as if
7336
        // counting the results from the first column)
7337
        $sql = "SELECT COUNT(1) AS qty FROM $sessionRelUserTable
7338
                WHERE
7339
                    session_id = $sessionId AND
7340
                    user_id = $userId AND
7341
                    relation_type = ".SESSION_RELATION_TYPE_RRHH;
7342
7343
        $result = Database::fetch_assoc(Database::query($sql));
7344
7345
        if (!empty($result) && $result['qty'] > 0) {
7346
            return true;
7347
        }
7348
7349
        return false;
7350
    }
7351
7352
    /**
7353
     * Get the session coached by a user (general coach and course-session coach).
7354
     *
7355
     * @param int  $coachId                       The coach id
7356
     * @param bool $checkSessionRelUserVisibility Check the session visibility
7357
     * @param bool $asPlatformAdmin               The user is a platform admin and we want all sessions
7358
     *
7359
     * @return array The session list
7360
     */
7361
    public static function getSessionsCoachedByUser(
7362
        $coachId,
7363
        $checkSessionRelUserVisibility = false,
7364
        $asPlatformAdmin = false
7365
    ) {
7366
        // Get all sessions where $coachId is the general coach
7367
        $sessions = self::get_sessions_by_general_coach($coachId, $asPlatformAdmin);
7368
        // Get all sessions where $coachId is the course - session coach
7369
        $courseSessionList = self::getCoursesListByCourseCoach($coachId);
7370
        $sessionsByCoach = [];
7371
        if (!empty($courseSessionList)) {
7372
            foreach ($courseSessionList as $userCourseSubscription) {
7373
                $session = $userCourseSubscription->getSession();
7374
                $sessionsByCoach[$session->getId()] = api_get_session_info(
7375
                    $session->getId()
7376
                );
7377
            }
7378
        }
7379
7380
        if (!empty($sessionsByCoach)) {
7381
            $sessions = array_merge($sessions, $sessionsByCoach);
7382
        }
7383
7384
        // Remove repeated sessions
7385
        if (!empty($sessions)) {
7386
            $cleanSessions = [];
7387
            foreach ($sessions as $session) {
7388
                $cleanSessions[$session['id']] = $session;
7389
            }
7390
            $sessions = $cleanSessions;
7391
        }
7392
7393
        if ($checkSessionRelUserVisibility) {
7394
            if (!empty($sessions)) {
7395
                $newSessions = [];
7396
                foreach ($sessions as $session) {
7397
                    $visibility = api_get_session_visibility($session['id']);
7398
                    if ($visibility == SESSION_INVISIBLE) {
7399
                        continue;
7400
                    }
7401
                    $newSessions[] = $session;
7402
                }
7403
                $sessions = $newSessions;
7404
            }
7405
        }
7406
7407
        return $sessions;
7408
    }
7409
7410
    /**
7411
     * Check if the course belongs to the session.
7412
     *
7413
     * @param int    $sessionId  The session id
7414
     * @param string $courseCode The course code
7415
     *
7416
     * @return bool
7417
     */
7418
    public static function sessionHasCourse($sessionId, $courseCode)
7419
    {
7420
        $sessionId = (int) $sessionId;
7421
        $courseCode = Database::escape_string($courseCode);
7422
        $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
7423
        $sessionRelCourseTable = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
7424
7425
        $sql = "SELECT COUNT(1) AS qty
7426
                FROM $courseTable c
7427
                INNER JOIN $sessionRelCourseTable src
7428
                ON c.id = src.c_id
7429
                WHERE src.session_id = $sessionId
7430
                AND c.code = '$courseCode'  ";
7431
7432
        $result = Database::query($sql);
7433
7434
        if ($result !== false) {
7435
            $data = Database::fetch_assoc($result);
7436
7437
            if ($data['qty'] > 0) {
7438
                return true;
7439
            }
7440
        }
7441
7442
        return false;
7443
    }
7444
7445
    /**
7446
     * Calculate the total user time in the platform.
7447
     *
7448
     * @param int    $userId The user id
7449
     * @param string $from   Optional. From date
7450
     * @param string $until  Optional. Until date
7451
     *
7452
     * @return string The time (hh:mm:ss)
7453
     */
7454
    public static function getTotalUserTimeInPlatform($userId, $from = '', $until = '')
7455
    {
7456
        $userId = (int) $userId;
7457
        $trackLoginTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
7458
        $whereConditions = [
7459
            'login_user_id = ? ' => $userId,
7460
        ];
7461
7462
        if (!empty($from) && !empty($until)) {
7463
            $whereConditions["AND (login_date >= '?' "] = $from;
7464
            $whereConditions["AND logout_date <= DATE_ADD('?', INTERVAL 1 DAY)) "] = $until;
7465
        }
7466
7467
        $trackResult = Database::select(
7468
            'SEC_TO_TIME(SUM(UNIX_TIMESTAMP(logout_date) - UNIX_TIMESTAMP(login_date))) as total_time',
7469
            $trackLoginTable,
7470
            [
7471
                'where' => $whereConditions,
7472
            ],
7473
            'first'
7474
        );
7475
7476
        if ($trackResult != false) {
7477
            return $trackResult['total_time'] ? $trackResult['total_time'] : '00:00:00';
7478
        }
7479
7480
        return '00:00:00';
7481
    }
7482
7483
    /**
7484
     * Get the courses list by a course coach.
7485
     *
7486
     * @param int $coachId The coach id
7487
     *
7488
     * @return array (id, user_id, session_id, c_id, visibility, status, legal_agreement)
7489
     */
7490
    public static function getCoursesListByCourseCoach($coachId)
7491
    {
7492
        $entityManager = Database::getManager();
7493
        $scuRepo = $entityManager->getRepository(
7494
            'ChamiloCoreBundle:SessionRelCourseRelUser'
7495
        );
7496
7497
        return $scuRepo->findBy([
7498
            'user' => $coachId,
7499
            'status' => SessionRelCourseRelUser::STATUS_COURSE_COACH,
7500
        ]);
7501
    }
7502
7503
    /**
7504
     * Get the count of user courses in session.
7505
     *
7506
     * @param int $sessionId
7507
     * @param int $courseId
7508
     *
7509
     * @return array
7510
     */
7511
    public static function getTotalUserCoursesInSession($sessionId, $courseId = 0)
7512
    {
7513
        $tableUser = Database::get_main_table(TABLE_MAIN_USER);
7514
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
7515
7516
        $sessionId = (int) $sessionId;
7517
7518
        if (empty($sessionId)) {
7519
            return [];
7520
        }
7521
7522
        $courseCondition = '';
7523
        if (!empty($courseId)) {
7524
            $courseId = (int) $courseId;
7525
            $courseCondition = "  c_id = $courseId AND ";
7526
        }
7527
7528
        $sql = "SELECT
7529
                    COUNT(u.id) as count,
7530
                    u.id,
7531
                    scu.status status_in_session,
7532
                    u.status user_status
7533
                FROM $table scu
7534
                INNER JOIN $tableUser u
7535
                ON scu.user_id = u.id
7536
                WHERE
7537
                  $courseCondition
7538
                  scu.session_id = ".$sessionId."
7539
                GROUP BY u.id";
7540
7541
        $result = Database::query($sql);
7542
7543
        $list = [];
7544
        while ($data = Database::fetch_assoc($result)) {
7545
            $list[] = $data;
7546
        }
7547
7548
        return $list;
7549
    }
7550
7551
    /**
7552
     * Returns list of a few data from session (name, short description, start
7553
     * date, end date) and the given extra fields if defined based on a
7554
     * session category Id.
7555
     *
7556
     * @param int    $categoryId  The internal ID of the session category
7557
     * @param string $target      Value to search for in the session field values
7558
     * @param array  $extraFields A list of fields to be scanned and returned
7559
     *
7560
     * @return mixed
7561
     */
7562
    public static function getShortSessionListAndExtraByCategory(
7563
        $categoryId,
7564
        $target,
7565
        $extraFields = null,
7566
        $publicationDate = null
7567
    ) {
7568
        $categoryId = (int) $categoryId;
7569
        $sessionList = [];
7570
        // Check if categoryId is valid
7571
        if ($categoryId > 0) {
7572
            $target = Database::escape_string($target);
7573
            $sTable = Database::get_main_table(TABLE_MAIN_SESSION);
7574
            $sfTable = Database::get_main_table(TABLE_EXTRA_FIELD);
7575
            $sfvTable = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
7576
            // Join session field and session field values tables
7577
            $joinTable = $sfTable.' sf INNER JOIN '.$sfvTable.' sfv ON sf.id = sfv.field_id';
7578
            $fieldsArray = [];
7579
            foreach ($extraFields as $field) {
7580
                $fieldsArray[] = Database::escape_string($field);
7581
            }
7582
            $extraFieldType = ExtraField::SESSION_FIELD_TYPE;
7583
            if (isset($publicationDate)) {
7584
                $publicationDateString = $publicationDate->format('Y-m-d H:i:s');
7585
                $wherePublication = " AND id NOT IN (
7586
                    SELECT sfv.item_id FROM $joinTable
7587
                    WHERE
7588
                        sf.extra_field_type = $extraFieldType AND
7589
                        ((sf.variable = 'publication_start_date' AND sfv.value > '$publicationDateString' and sfv.value != '') OR
7590
                        (sf.variable = 'publication_end_date' AND sfv.value < '$publicationDateString' and sfv.value != ''))
7591
                )";
7592
            }
7593
            // Get the session list from session category and target
7594
            $sessionList = Database::select(
7595
                'id, name, access_start_date, access_end_date',
7596
                $sTable,
7597
                [
7598
                    'where' => [
7599
                        "session_category_id = ? AND id IN (
7600
                            SELECT sfv.item_id FROM $joinTable
7601
                            WHERE
7602
                                sf.extra_field_type = $extraFieldType AND
7603
                                sfv.item_id = session.id AND
7604
                                sf.variable = 'target' AND
7605
                                sfv.value = ?
7606
                        ) $wherePublication" => [$categoryId, $target],
7607
                    ],
7608
                ]
7609
            );
7610
            $whereFieldVariables = [];
7611
            $whereFieldIds = [];
7612
            if (
7613
                is_array($fieldsArray) &&
7614
                count($fieldsArray) > 0
7615
            ) {
7616
                $whereParams = '?';
7617
                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...
7618
                    $whereParams .= ', ?';
7619
                }
7620
                $whereFieldVariables = ' variable IN ( '.$whereParams.' )';
7621
                $whereFieldIds = 'field_id IN ( '.$whereParams.' )';
7622
            }
7623
            // Get session fields
7624
            $extraField = new ExtraFieldModel('session');
7625
            $questionMarks = substr(str_repeat('?, ', count($fieldsArray)), 0, -2);
7626
            $fieldsList = $extraField->get_all([
7627
                ' variable IN ( '.$questionMarks.' )' => $fieldsArray,
7628
            ]);
7629
            // Index session fields
7630
            foreach ($fieldsList as $field) {
7631
                $fields[$field['id']] = $field['variable'];
7632
            }
7633
            // Get session field values
7634
            $extra = new ExtraFieldValue('session');
7635
            $questionMarksFields = substr(str_repeat('?, ', count($fields)), 0, -2);
7636
            $sessionFieldValueList = $extra->get_all(['where' => ['field_id IN ( '.$questionMarksFields.' )' => array_keys($fields)]]);
7637
            // Add session fields values to session list
7638
            foreach ($sessionList as $id => &$session) {
7639
                foreach ($sessionFieldValueList as $sessionFieldValue) {
7640
                    // Match session field values to session
7641
                    if ($sessionFieldValue['item_id'] == $id) {
7642
                        // Check if session field value is set in session field list
7643
                        if (isset($fields[$sessionFieldValue['field_id']])) {
7644
                            // Avoid overwriting the session's ID field
7645
                            if ($fields[$sessionFieldValue['field_id']] != 'id') {
7646
                                $var = $fields[$sessionFieldValue['field_id']];
7647
                                $val = $sessionFieldValue['value'];
7648
                                // Assign session field value to session
7649
                                $session[$var] = $val;
7650
                            }
7651
                        }
7652
                    }
7653
                }
7654
            }
7655
        }
7656
7657
        return $sessionList;
7658
    }
7659
7660
    /**
7661
     * Return the Session Category id searched by name.
7662
     *
7663
     * @param string $categoryName Name attribute of session category used for search query
7664
     * @param bool   $force        boolean used to get even if something is wrong (e.g not unique name)
7665
     *
7666
     * @return int|array If success, return category id (int), else it will return an array
7667
     *                   with the next structure:
7668
     *                   array('error' => true, 'errorMessage' => ERROR_MESSAGE)
7669
     */
7670
    public static function getSessionCategoryIdByName($categoryName, $force = false)
7671
    {
7672
        // Start error result
7673
        $errorResult = ['error' => true, 'errorMessage' => get_lang('ThereWasAnError')];
7674
        $categoryName = Database::escape_string($categoryName);
7675
        // Check if is not empty category name
7676
        if (!empty($categoryName)) {
7677
            $sessionCategoryTable = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
7678
            // Get all session category with same name
7679
            $result = Database::select(
7680
                'id',
7681
                $sessionCategoryTable,
7682
                [
7683
                    'where' => [
7684
                        'name = ?' => $categoryName,
7685
                    ],
7686
                ]
7687
            );
7688
            // Check the result
7689
            if ($result < 1) {
7690
                // If not found any result, update error message
7691
                $errorResult['errorMessage'] = 'Not found any session category name '.$categoryName;
7692
            } elseif (count($result) > 1 && !$force) {
7693
                // If found more than one result and force is disabled, update error message
7694
                $errorResult['errorMessage'] = 'Found many session categories';
7695
            } elseif (count($result) == 1 || $force) {
7696
                // If found just one session category or force option is enabled
7697
7698
                return key($result);
7699
            }
7700
        } else {
7701
            // category name is empty, update error message
7702
            $errorResult['errorMessage'] = 'Not valid category name';
7703
        }
7704
7705
        return $errorResult;
7706
    }
7707
7708
    /**
7709
     * Return all data from sessions (plus extra field, course and coach data) by category id.
7710
     *
7711
     * @param int $sessionCategoryId session category id used to search sessions
7712
     *
7713
     * @return array If success, return session list and more session related data, else it will return an array
7714
     *               with the next structure:
7715
     *               array('error' => true, 'errorMessage' => ERROR_MESSAGE)
7716
     */
7717
    public static function getSessionListAndExtraByCategoryId($sessionCategoryId)
7718
    {
7719
        // Start error result
7720
        $errorResult = [
7721
            'error' => true,
7722
            'errorMessage' => get_lang('ThereWasAnError'),
7723
        ];
7724
7725
        $sessionCategoryId = intval($sessionCategoryId);
7726
        // Check if session category id is valid
7727
        if ($sessionCategoryId > 0) {
7728
            // Get table names
7729
            $sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
7730
            $sessionFieldTable = Database::get_main_table(TABLE_EXTRA_FIELD);
7731
            $sessionFieldValueTable = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
7732
            $sessionCourseUserTable = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
7733
            $userTable = Database::get_main_table(TABLE_MAIN_USER);
7734
            $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
7735
7736
            // Get all data from all sessions whit the session category specified
7737
            $sessionList = Database::select(
7738
                '*',
7739
                $sessionTable,
7740
                [
7741
                    'where' => [
7742
                        'session_category_id = ?' => $sessionCategoryId,
7743
                    ],
7744
                ]
7745
            );
7746
7747
            $extraFieldType = ExtraField::SESSION_FIELD_TYPE;
7748
7749
            // Check if session list query had result
7750
            if (!empty($sessionList)) {
7751
                // implode all session id
7752
                $sessionIdsString = '('.implode(', ', array_keys($sessionList)).')';
7753
                // Get all field variables
7754
                $sessionFieldList = Database::select(
7755
                    'id, variable',
7756
                    $sessionFieldTable,
7757
                    ['extra_field_type = ? ' => [$extraFieldType]]
7758
                );
7759
7760
                // Get all field values
7761
                $sql = "SELECT item_id, field_id, value FROM
7762
                        $sessionFieldValueTable v INNER JOIN $sessionFieldTable f
7763
                        ON (f.id = v.field_id)
7764
                        WHERE
7765
                            item_id IN $sessionIdsString AND
7766
                            extra_field_type = $extraFieldType
7767
                ";
7768
                $result = Database::query($sql);
7769
                $sessionFieldValueList = Database::store_result($result, 'ASSOC');
7770
7771
                // Check if session field values had result
7772
                if (!empty($sessionFieldValueList)) {
7773
                    $sessionFieldValueListBySession = [];
7774
                    foreach ($sessionFieldValueList as $key => $sessionFieldValue) {
7775
                        // Create an array to index ids to session id
7776
                        $sessionFieldValueListBySession[$sessionFieldValue['item_id']][] = $key;
7777
                    }
7778
                }
7779
                // Query used to find course-coaches from sessions
7780
                $sql = "SELECT
7781
                            scu.session_id,
7782
                            c.id AS course_id,
7783
                            c.code AS course_code,
7784
                            c.title AS course_title,
7785
                            u.username AS coach_username,
7786
                            u.firstname AS coach_firstname,
7787
                            u.lastname AS coach_lastname
7788
                        FROM $courseTable c
7789
                        INNER JOIN $sessionCourseUserTable scu ON c.id = scu.c_id
7790
                        INNER JOIN $userTable u ON scu.user_id = u.user_id
7791
                        WHERE scu.status = 2 AND scu.session_id IN $sessionIdsString
7792
                        ORDER BY scu.session_id ASC ";
7793
                $res = Database::query($sql);
7794
                $sessionCourseList = Database::store_result($res, 'ASSOC');
7795
                // Check if course list had result
7796
                if (!empty($sessionCourseList)) {
7797
                    foreach ($sessionCourseList as $key => $sessionCourse) {
7798
                        // Create an array to index ids to session_id
7799
                        $sessionCourseListBySession[$sessionCourse['session_id']][] = $key;
7800
                    }
7801
                }
7802
                // Join lists
7803
                if (is_array($sessionList)) {
7804
                    foreach ($sessionList as $id => &$row) {
7805
                        if (
7806
                            !empty($sessionFieldValueListBySession) &&
7807
                            is_array($sessionFieldValueListBySession[$id])
7808
                        ) {
7809
                            // If have an index array for session extra fields, use it to join arrays
7810
                            foreach ($sessionFieldValueListBySession[$id] as $key) {
7811
                                $row['extra'][$key] = [
7812
                                    'field_name' => $sessionFieldList[$sessionFieldValueList[$key]['field_id']]['variable'],
7813
                                    'value' => $sessionFieldValueList[$key]['value'],
7814
                                ];
7815
                            }
7816
                        }
7817
                        if (
7818
                            !empty($sessionCourseListBySession) &&
7819
                            is_array($sessionCourseListBySession[$id])
7820
                        ) {
7821
                            // If have an index array for session course coach, use it to join arrays
7822
                            foreach ($sessionCourseListBySession[$id] as $key) {
7823
                                $row['course'][$key] = [
7824
                                    'course_id' => $sessionCourseList[$key]['course_id'],
7825
                                    'course_code' => $sessionCourseList[$key]['course_code'],
7826
                                    'course_title' => $sessionCourseList[$key]['course_title'],
7827
                                    'coach_username' => $sessionCourseList[$key]['coach_username'],
7828
                                    'coach_firstname' => $sessionCourseList[$key]['coach_firstname'],
7829
                                    'coach_lastname' => $sessionCourseList[$key]['coach_lastname'],
7830
                                ];
7831
                            }
7832
                        }
7833
                    }
7834
                }
7835
7836
                return $sessionList;
7837
            } else {
7838
                // Not found result, update error message
7839
                $errorResult['errorMessage'] = 'Not found any session for session category id '.$sessionCategoryId;
7840
            }
7841
        }
7842
7843
        return $errorResult;
7844
    }
7845
7846
    /**
7847
     * Return session description from session id.
7848
     *
7849
     * @param int $sessionId
7850
     *
7851
     * @return string
7852
     */
7853
    public static function getDescriptionFromSessionId($sessionId)
7854
    {
7855
        // Init variables
7856
        $sessionId = (int) $sessionId;
7857
        $description = '';
7858
        // Check if session id is valid
7859
        if ($sessionId > 0) {
7860
            // Select query from session id
7861
            $rows = Database::select(
7862
                'description',
7863
                Database::get_main_table(TABLE_MAIN_SESSION),
7864
                [
7865
                    'where' => [
7866
                        'id = ?' => $sessionId,
7867
                    ],
7868
                ]
7869
            );
7870
7871
            // Check if select query result is not empty
7872
            if (!empty($rows)) {
7873
                // Get session description
7874
                $description = $rows[0]['description'];
7875
            }
7876
        }
7877
7878
        return $description;
7879
    }
7880
7881
    /**
7882
     * Get a session list filtered by name, description or any of the given extra fields.
7883
     *
7884
     * @param string $term                 The term to search
7885
     * @param array  $extraFieldsToInclude Extra fields to include in the session data
7886
     *
7887
     * @return array The list
7888
     */
7889
    public static function searchSession($term, $extraFieldsToInclude = [])
7890
    {
7891
        $sTable = Database::get_main_table(TABLE_MAIN_SESSION);
7892
        $extraFieldTable = Database::get_main_table(TABLE_EXTRA_FIELD);
7893
        $sfvTable = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
7894
        $term = Database::escape_string($term);
7895
        $extraFieldType = ExtraField::SESSION_FIELD_TYPE;
7896
        if (is_array($extraFieldsToInclude) && count($extraFieldsToInclude) > 0) {
7897
            $resultData = Database::select('*', $sTable, [
7898
                'where' => [
7899
                    "name LIKE %?% " => $term,
7900
                    " OR description LIKE %?% " => $term,
7901
                    " OR id IN (
7902
                    SELECT item_id
7903
                    FROM $sfvTable v INNER JOIN $extraFieldTable e
7904
                    ON (v.field_id = e.id)
7905
                    WHERE value LIKE %?% AND extra_field_type = $extraFieldType
7906
                ) " => $term,
7907
                ],
7908
            ]);
7909
        } else {
7910
            $resultData = Database::select('*', $sTable, [
7911
                'where' => [
7912
                    "name LIKE %?% " => $term,
7913
                    "OR description LIKE %?% " => $term,
7914
                ],
7915
            ]);
7916
7917
            return $resultData;
7918
        }
7919
7920
        foreach ($resultData as $id => &$session) {
7921
            $session['extra'] = self::getFilteredExtraFields($id, $extraFieldsToInclude);
7922
        }
7923
7924
        return $resultData;
7925
    }
7926
7927
    /**
7928
     * @param int   $sessionId
7929
     * @param array $extraFieldsToInclude (empty means all)
7930
     *
7931
     * @return array
7932
     */
7933
    public static function getFilteredExtraFields($sessionId, $extraFieldsToInclude = [])
7934
    {
7935
        $extraData = [];
7936
        $variables = [];
7937
        $variablePlaceHolders = [];
7938
7939
        foreach ($extraFieldsToInclude as $sessionExtraField) {
7940
            $variablePlaceHolders[] = "?";
7941
            $variables[] = Database::escape_string($sessionExtraField);
7942
        }
7943
7944
        $sessionExtraField = new ExtraFieldModel('session');
7945
        $fieldList = $sessionExtraField->get_all([
7946
            "variable IN ( ".implode(", ", $variablePlaceHolders)." ) " => $variables,
7947
        ]);
7948
7949
        if (empty($fieldList)) {
7950
            return [];
7951
        }
7952
7953
        $fields = [];
7954
7955
        // Index session fields
7956
        foreach ($fieldList as $field) {
7957
            $fields[$field['id']] = $field['variable'];
7958
        }
7959
7960
        // Get session field values
7961
        $extra = new ExtraFieldValue('session');
7962
        $sessionFieldValueList = $extra->get_all(
7963
            [
7964
                "field_id IN ( ".implode(", ", $variablePlaceHolders)." )" => array_keys($fields),
7965
            ]
7966
        );
7967
7968
        foreach ($sessionFieldValueList as $sessionFieldValue) {
7969
            // Match session field values to session
7970
            if ($sessionFieldValue['item_id'] != $sessionId) {
7971
                continue;
7972
            }
7973
7974
            // Check if session field value is set in session field list
7975
            if (!isset($fields[$sessionFieldValue['field_id']])) {
7976
                continue;
7977
            }
7978
7979
            $extrafieldVariable = $fields[$sessionFieldValue['field_id']];
7980
            $extrafieldValue = $sessionFieldValue['value'];
7981
7982
            $extraData[] = [
7983
                'variable' => $extrafieldVariable,
7984
                'value' => $extrafieldValue,
7985
            ];
7986
        }
7987
7988
        return $extraData;
7989
    }
7990
7991
    /**
7992
     * @param int $sessionId
7993
     *
7994
     * @return bool
7995
     */
7996
    public static function isValidId($sessionId)
7997
    {
7998
        $sessionId = (int) $sessionId;
7999
        if ($sessionId > 0) {
8000
            $rows = Database::select(
8001
                'id',
8002
                Database::get_main_table(TABLE_MAIN_SESSION),
8003
                ['where' => ['id = ?' => $sessionId]]
8004
            );
8005
            if (!empty($rows)) {
8006
                return true;
8007
            }
8008
        }
8009
8010
        return false;
8011
    }
8012
8013
    /**
8014
     * Get list of sessions based on users of a group for a group admin.
8015
     *
8016
     * @param int $userId The user id
8017
     *
8018
     * @return array
8019
     */
8020
    public static function getSessionsFollowedForGroupAdmin($userId)
8021
    {
8022
        $sessionList = [];
8023
        $sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
8024
        $sessionUserTable = Database::get_main_table(TABLE_MAIN_SESSION_USER);
8025
        $userGroup = new UserGroup();
8026
        $userIdList = $userGroup->getGroupUsersByUser($userId);
8027
8028
        if (empty($userIdList)) {
8029
            return [];
8030
        }
8031
8032
        $sql = "SELECT DISTINCT s.*
8033
                FROM $sessionTable s
8034
                INNER JOIN $sessionUserTable sru
8035
                ON s.id = sru.id_session
8036
                WHERE
8037
                    (sru.id_user IN (".implode(', ', $userIdList).")
8038
                    AND sru.relation_type = 0
8039
                )";
8040
8041
        if (api_is_multiple_url_enabled()) {
8042
            $sessionAccessUrlTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
8043
            $accessUrlId = api_get_current_access_url_id();
8044
8045
            if ($accessUrlId != -1) {
8046
                $sql = "SELECT DISTINCT s.*
8047
                        FROM $sessionTable s
8048
                        INNER JOIN $sessionUserTable sru ON s.id = sru.id_session
8049
                        INNER JOIN $sessionAccessUrlTable srau ON s.id = srau.session_id
8050
                        WHERE
8051
                            srau.access_url_id = $accessUrlId
8052
                            AND (
8053
                                sru.id_user IN (".implode(', ', $userIdList).")
8054
                                AND sru.relation_type = 0
8055
                            )";
8056
            }
8057
        }
8058
8059
        $result = Database::query($sql);
8060
        while ($row = Database::fetch_assoc($result)) {
8061
            $sessionList[] = $row;
8062
        }
8063
8064
        return $sessionList;
8065
    }
8066
8067
    /**
8068
     * @param array $sessionInfo
8069
     *
8070
     * @return string
8071
     */
8072
    public static function getSessionVisibility($sessionInfo)
8073
    {
8074
        switch ($sessionInfo['visibility']) {
8075
            case 1:
8076
                return get_lang('ReadOnly');
8077
            case 2:
8078
                return get_lang('Visible');
8079
            case 3:
8080
                return api_ucfirst(get_lang('Invisible'));
8081
        }
8082
    }
8083
8084
    /**
8085
     * Returns a human readable string.
8086
     *
8087
     * @param array $sessionInfo An array with all the session dates
8088
     * @param bool  $showTime
8089
     *
8090
     * @return array
8091
     */
8092
    public static function parseSessionDates($sessionInfo, $showTime = false)
8093
    {
8094
        $displayDates = self::convertSessionDateToString(
8095
            $sessionInfo['display_start_date'],
8096
            $sessionInfo['display_end_date'],
8097
            $showTime,
8098
            true
8099
        );
8100
        $accessDates = self::convertSessionDateToString(
8101
            $sessionInfo['access_start_date'],
8102
            $sessionInfo['access_end_date'],
8103
            $showTime,
8104
            true
8105
        );
8106
8107
        $coachDates = self::convertSessionDateToString(
8108
            $sessionInfo['coach_access_start_date'],
8109
            $sessionInfo['coach_access_end_date'],
8110
            $showTime,
8111
            true
8112
        );
8113
8114
        $result = [
8115
            'access' => $accessDates,
8116
            'display' => $displayDates,
8117
            'coach' => $coachDates,
8118
        ];
8119
8120
        return $result;
8121
    }
8122
8123
    /**
8124
     * @param array         $sessionInfo Optional
8125
     *
8126
     * @return array
8127
     */
8128
    public static function setForm(FormValidator $form, array $sessionInfo = [])
8129
    {
8130
        $sessionId = 0;
8131
        $coachInfo = [];
8132
8133
        if (!empty($sessionInfo)) {
8134
            $sessionId = (int) $sessionInfo['id'];
8135
            $coachInfo = api_get_user_info($sessionInfo['id_coach']);
8136
        }
8137
8138
        $categoriesList = self::get_all_session_category();
8139
        $userInfo = api_get_user_info();
8140
8141
        $categoriesOptions = [
8142
            '0' => get_lang('None'),
8143
        ];
8144
8145
        if ($categoriesList != false) {
8146
            foreach ($categoriesList as $categoryItem) {
8147
                $categoriesOptions[$categoryItem['id']] = $categoryItem['name'];
8148
            }
8149
        }
8150
8151
        // Database Table Definitions
8152
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
8153
8154
        $form->addText(
8155
            'name',
8156
            get_lang('SessionName'),
8157
            true,
8158
            ['maxlength' => 150, 'aria-label' => get_lang('SessionName')]
8159
        );
8160
        $form->addRule('name', get_lang('SessionNameAlreadyExists'), 'callback', 'check_session_name');
8161
8162
        if (!api_is_platform_admin() && api_is_teacher()) {
8163
            $form->addElement(
8164
                'select',
8165
                'coach_username',
8166
                get_lang('CoachName'),
8167
                [api_get_user_id() => $userInfo['complete_name']],
8168
                [
8169
                    'id' => 'coach_username',
8170
                    'style' => 'width:370px;',
8171
                ]
8172
            );
8173
        } else {
8174
            $sql = "SELECT COUNT(1) FROM $tbl_user WHERE status = 1";
8175
            $rs = Database::query($sql);
8176
            $countUsers = (int) Database::result($rs, 0, 0);
8177
8178
            if ($countUsers < 50) {
8179
                $orderClause = 'ORDER BY ';
8180
                $orderClause .= api_sort_by_first_name() ? 'firstname, lastname, username' : 'lastname, firstname, username';
8181
8182
                $sql = "SELECT user_id, lastname, firstname, username
8183
                        FROM $tbl_user
8184
                        WHERE status = '1' ".
8185
                        $orderClause;
8186
8187
                if (api_is_multiple_url_enabled()) {
8188
                    $userRelAccessUrlTable = Database::get_main_table(
8189
                        TABLE_MAIN_ACCESS_URL_REL_USER
8190
                    );
8191
                    $accessUrlId = api_get_current_access_url_id();
8192
                    if ($accessUrlId != -1) {
8193
                        $sql = "SELECT user.user_id, username, lastname, firstname
8194
                        FROM $tbl_user user
8195
                        INNER JOIN $userRelAccessUrlTable url_user
8196
                        ON (url_user.user_id = user.user_id)
8197
                        WHERE
8198
                            access_url_id = $accessUrlId AND
8199
                            status = 1 "
8200
                            .$orderClause;
8201
                    }
8202
                }
8203
8204
                $result = Database::query($sql);
8205
                $coachesList = Database::store_result($result);
8206
                $coachesOptions = [];
8207
                foreach ($coachesList as $coachItem) {
8208
                    $coachesOptions[$coachItem['user_id']] =
8209
                        api_get_person_name($coachItem['firstname'], $coachItem['lastname']).' ('.$coachItem['username'].')';
8210
                }
8211
8212
                $form->addElement(
8213
                    'select',
8214
                    'coach_username',
8215
                    get_lang('CoachName'),
8216
                    $coachesOptions,
8217
                    [
8218
                        'id' => 'coach_username',
8219
                        'style' => 'width:370px;',
8220
                    ]
8221
                );
8222
            } else {
8223
                $form->addElement(
8224
                    'select_ajax',
8225
                    'coach_username',
8226
                    get_lang('CoachName'),
8227
                    $coachInfo ? [$coachInfo['id'] => $coachInfo['complete_name_with_username']] : [],
8228
                    [
8229
                        'url' => api_get_path(WEB_AJAX_PATH).'session.ajax.php?a=search_general_coach',
8230
                        'width' => '100%',
8231
                        'id' => 'coach_username',
8232
                    ]
8233
                );
8234
            }
8235
        }
8236
8237
        $form->addRule('coach_username', get_lang('ThisFieldIsRequired'), 'required');
8238
        $form->addHtml('<div id="ajax_list_coachs"></div>');
8239
8240
        $form->addButtonAdvancedSettings('advanced_params');
8241
        $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
8242
8243
        if (empty($sessionId)) {
8244
            $sessions = self::formatSessionsAdminForGrid();
8245
            $sessionList = [];
8246
            $sessionList[] = '';
8247
            foreach ($sessions as $session) {
8248
                $sessionList[$session['id']] = strip_tags($session['name']);
8249
            }
8250
8251
            $form->addSelect(
8252
                'session_template',
8253
                get_lang('SessionTemplate'),
8254
                $sessionList,
8255
                ['id' => 'system_template']
8256
            );
8257
        }
8258
8259
        $form->addSelect(
8260
            'session_category',
8261
            get_lang('SessionCategory'),
8262
            $categoriesOptions,
8263
            [
8264
                'id' => 'session_category',
8265
            ]
8266
        );
8267
8268
        if (api_get_configuration_value('allow_session_status')) {
8269
            $statusList = self::getStatusList();
8270
            $form->addSelect(
8271
                'status',
8272
                get_lang('SessionStatus'),
8273
                $statusList,
8274
                [
8275
                    'id' => 'status',
8276
                ]
8277
            );
8278
        }
8279
8280
        $form->addHtmlEditor(
8281
            'description',
8282
            get_lang('Description'),
8283
            false,
8284
            false,
8285
            [
8286
                'ToolbarSet' => 'Minimal',
8287
            ]
8288
        );
8289
8290
        $form->addElement('checkbox', 'show_description', null, get_lang('ShowDescription'));
8291
8292
        $visibilityGroup = [];
8293
        $visibilityGroup[] = $form->createElement(
8294
            'select',
8295
            'session_visibility',
8296
            null,
8297
            [
8298
                SESSION_VISIBLE_READ_ONLY => get_lang('SessionReadOnly'),
8299
                SESSION_VISIBLE => get_lang('SessionAccessible'),
8300
                SESSION_INVISIBLE => api_ucfirst(get_lang('SessionNotAccessible')),
8301
            ]
8302
        );
8303
        $form->addGroup(
8304
            $visibilityGroup,
8305
            'visibility_group',
8306
            get_lang('SessionVisibility'),
8307
            null,
8308
            false
8309
        );
8310
8311
        $options = [
8312
            0 => get_lang('ByDuration'),
8313
            1 => get_lang('ByDates'),
8314
        ];
8315
8316
        $form->addSelect('access', get_lang('Access'), $options, [
8317
            'onchange' => 'accessSwitcher()',
8318
            'id' => 'access',
8319
        ]);
8320
8321
        $form->addHtml('<div id="duration_div" style="display:none">');
8322
        $form->addElement(
8323
            'number',
8324
            'duration',
8325
            [
8326
                get_lang('SessionDurationTitle'),
8327
                get_lang('SessionDurationDescription'),
8328
            ],
8329
            [
8330
                'maxlength' => 50,
8331
            ]
8332
        );
8333
8334
        $form->addHtml('</div>');
8335
        $form->addHtml('<div id="date_fields" style="display:none">');
8336
8337
        // Dates
8338
        $form->addDateTimePicker(
8339
            'access_start_date',
8340
            [get_lang('SessionStartDate'), get_lang('SessionStartDateComment')],
8341
            ['id' => 'access_start_date']
8342
        );
8343
8344
        $form->addDateTimePicker(
8345
            'access_end_date',
8346
            [get_lang('SessionEndDate'), get_lang('SessionEndDateComment')],
8347
            ['id' => 'access_end_date']
8348
        );
8349
8350
        $form->addRule(
8351
            ['access_start_date', 'access_end_date'],
8352
            get_lang('StartDateMustBeBeforeTheEndDate'),
8353
            'compare_datetime_text',
8354
            '< allow_empty'
8355
        );
8356
8357
        $form->addDateTimePicker(
8358
            'display_start_date',
8359
            [
8360
                get_lang('SessionDisplayStartDate'),
8361
                get_lang('SessionDisplayStartDateComment'),
8362
            ],
8363
            ['id' => 'display_start_date']
8364
        );
8365
8366
        $form->addDateTimePicker(
8367
            'display_end_date',
8368
            [
8369
                get_lang('SessionDisplayEndDate'),
8370
                get_lang('SessionDisplayEndDateComment'),
8371
            ],
8372
            ['id' => 'display_end_date']
8373
        );
8374
8375
        $form->addRule(
8376
            ['display_start_date', 'display_end_date'],
8377
            get_lang('StartDateMustBeBeforeTheEndDate'),
8378
            'compare_datetime_text',
8379
            '< allow_empty'
8380
        );
8381
8382
        $form->addDateTimePicker(
8383
            'coach_access_start_date',
8384
            [
8385
                get_lang('SessionCoachStartDate'),
8386
                get_lang('SessionCoachStartDateComment'),
8387
            ],
8388
            ['id' => 'coach_access_start_date']
8389
        );
8390
8391
        $form->addDateTimePicker(
8392
            'coach_access_end_date',
8393
            [
8394
                get_lang('SessionCoachEndDate'),
8395
                get_lang('SessionCoachEndDateComment'),
8396
            ],
8397
            ['id' => 'coach_access_end_date']
8398
        );
8399
8400
        $form->addRule(
8401
            ['coach_access_start_date', 'coach_access_end_date'],
8402
            get_lang('StartDateMustBeBeforeTheEndDate'),
8403
            'compare_datetime_text',
8404
            '< allow_empty'
8405
        );
8406
8407
        $form->addElement('html', '</div>');
8408
8409
        $form->addCheckBox(
8410
            'send_subscription_notification',
8411
            [
8412
                get_lang('SendSubscriptionNotification'),
8413
                get_lang('SendAnEmailWhenAUserBeingSubscribed'),
8414
            ]
8415
        );
8416
8417
        // Extra fields
8418
        $extra_field = new ExtraFieldModel('session');
8419
        $extra = $extra_field->addElements($form, $sessionId);
8420
8421
        $form->addElement('html', '</div>');
8422
8423
        $js = $extra['jquery_ready_content'];
8424
8425
        return ['js' => $js];
8426
    }
8427
8428
    /**
8429
     * Gets the number of rows in the session table filtered through the given
8430
     * array of parameters.
8431
     *
8432
     * @param array Array of options/filters/keys
8433
     *
8434
     * @return int The number of rows, or false on wrong param
8435
     * @assert ('a') === false
8436
     */
8437
    public static function get_count_admin_complete($options = [])
8438
    {
8439
        if (!is_array($options)) {
8440
            return false;
8441
        }
8442
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
8443
        $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
8444
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
8445
        $sessionCourseUserTable = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
8446
        $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
8447
        $tbl_session_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
8448
        $tbl_session_field_options = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
8449
8450
        $where = 'WHERE 1 = 1 ';
8451
        $user_id = api_get_user_id();
8452
8453
        if (api_is_session_admin() &&
8454
            api_get_setting('allow_session_admins_to_see_all_sessions') == 'false'
8455
        ) {
8456
            $where .= " WHERE s.session_admin_id = $user_id ";
8457
        }
8458
8459
        $extraFieldTables = '';
8460
        if (!empty($options['where'])) {
8461
            $options['where'] = str_replace('course_title', 'c.title', $options['where']);
8462
            $options['where'] = str_replace("( session_active = '0' )", '1=1', $options['where']);
8463
8464
            $options['where'] = str_replace(
8465
                ["AND session_active = '1'  )", " AND (  session_active = '1'  )"],
8466
                [') GROUP BY s.name HAVING session_active = 1 ', " GROUP BY s.name HAVING session_active = 1 "],
8467
                $options['where']
8468
            );
8469
8470
            $options['where'] = str_replace(
8471
                ["AND session_active = '0'  )", " AND (  session_active = '0'  )"],
8472
                [') GROUP BY s.name HAVING session_active = 0 ', " GROUP BY s.name HAVING session_active = '0' "],
8473
                $options['where']
8474
            );
8475
8476
            if (!empty($options['extra'])) {
8477
                $options['where'] = str_replace(' 1 = 1  AND', '', $options['where']);
8478
                $options['where'] = str_replace('AND', 'OR', $options['where']);
8479
8480
                foreach ($options['extra'] as $extra) {
8481
                    $options['where'] = str_replace(
8482
                        $extra['field'],
8483
                        'fv.field_id = '.$extra['id'].' AND fvo.option_value',
8484
                        $options['where']
8485
                    );
8486
                    $extraFieldTables = "$tbl_session_field_values fv, $tbl_session_field_options fvo, ";
8487
                }
8488
            }
8489
            $where .= ' AND '.$options['where'];
8490
        }
8491
8492
        $today = api_get_utc_datetime();
8493
        $query_rows = "SELECT count(*) as total_rows, c.title as course_title, s.name,
8494
                        IF (
8495
                            (s.access_start_date <= '$today' AND '$today' < s.access_end_date) OR
8496
                            (s.access_start_date = '0000-00-00 00:00:00' AND s.access_end_date = '0000-00-00 00:00:00' ) OR
8497
                            (s.access_start_date IS NULL AND s.access_end_date IS NULL) OR
8498
                            (s.access_start_date <= '$today' AND ('0000-00-00 00:00:00' = s.access_end_date OR s.access_end_date IS NULL )) OR
8499
                            ('$today' < s.access_end_date AND ('0000-00-00 00:00:00' = s.access_start_date OR s.access_start_date IS NULL) )
8500
                        , 1, 0) as session_active
8501
                       FROM $extraFieldTables $tbl_session s
8502
                       LEFT JOIN  $tbl_session_category sc
8503
                       ON s.session_category_id = sc.id
8504
                       INNER JOIN $tbl_user u
8505
                       ON s.id_coach = u.user_id
8506
                       INNER JOIN $sessionCourseUserTable scu
8507
                       ON s.id = scu.session_id
8508
                       INNER JOIN $courseTable c
8509
                       ON c.id = scu.c_id
8510
                       $where ";
8511
8512
        if (api_is_multiple_url_enabled()) {
8513
            $table_access_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
8514
            $access_url_id = api_get_current_access_url_id();
8515
            if ($access_url_id != -1) {
8516
                $where .= " AND ar.access_url_id = $access_url_id ";
8517
                $query_rows = "SELECT count(*) as total_rows
8518
                               FROM $tbl_session s
8519
                               LEFT JOIN  $tbl_session_category sc
8520
                               ON s.session_category_id = sc.id
8521
                               INNER JOIN $tbl_user u
8522
                               ON s.id_coach = u.user_id
8523
                               INNER JOIN $table_access_url_rel_session ar
8524
                               ON ar.session_id = s.id $where ";
8525
            }
8526
        }
8527
8528
        $result = Database::query($query_rows);
8529
        $num = 0;
8530
        if (Database::num_rows($result)) {
8531
            $rows = Database::fetch_array($result);
8532
            $num = $rows['total_rows'];
8533
        }
8534
8535
        return $num;
8536
    }
8537
8538
    /**
8539
     * @param string $listType
8540
     * @param array  $extraFields
8541
     *
8542
     * @return array
8543
     */
8544
    public static function getGridColumns(
8545
        $listType = 'simple',
8546
        $extraFields = [],
8547
        $addExtraFields = true
8548
    ) {
8549
        $showCount = api_get_configuration_value('session_list_show_count_users');
8550
        // Column config
8551
        $operators = ['cn', 'nc'];
8552
        $date_operators = ['gt', 'ge', 'lt', 'le'];
8553
8554
        switch ($listType) {
8555
            case 'my_space':
8556
                $columns = [
8557
                    get_lang('Title'),
8558
                    get_lang('Date'),
8559
                    get_lang('NbCoursesPerSession'),
8560
                    get_lang('NbStudentPerSession'),
8561
                    get_lang('Details'),
8562
                ];
8563
8564
                $columnModel = [
8565
                    ['name' => 'name', 'index' => 'name', 'width' => '255', 'align' => 'left'],
8566
                    ['name' => 'date', 'index' => 'access_start_date', 'width' => '150', 'align' => 'left'],
8567
                    [
8568
                        'name' => 'course_per_session',
8569
                        'index' => 'course_per_session',
8570
                        'width' => '150',
8571
                        'sortable' => 'false',
8572
                        'search' => 'false',
8573
                    ],
8574
                    [
8575
                        'name' => 'student_per_session',
8576
                        'index' => 'student_per_session',
8577
                        'width' => '100',
8578
                        'sortable' => 'false',
8579
                        'search' => 'false',
8580
                    ],
8581
                    ['name' => 'actions', 'index' => 'actions', 'width' => '100', 'sortable' => 'false', 'search' => 'false'],
8582
                ];
8583
                break;
8584
            case 'simple':
8585
                $columns = [
8586
                    '#',
8587
                    get_lang('Name'),
8588
                    get_lang('Category'),
8589
                    get_lang('SessionDisplayStartDate'),
8590
                    get_lang('SessionDisplayEndDate'),
8591
                    get_lang('Visibility'),
8592
                ];
8593
8594
                $columnModel = [
8595
                    [
8596
                        'name' => 'id',
8597
                        'index' => 's.id',
8598
                        'width' => '160',
8599
                        'hidden' => 'true',
8600
                    ],
8601
                    [
8602
                        'name' => 'name',
8603
                        'index' => 's.name',
8604
                        'width' => '160',
8605
                        'align' => 'left',
8606
                        'search' => 'true',
8607
                        'searchoptions' => ['sopt' => $operators],
8608
                    ],
8609
                    [
8610
                        'name' => 'category_name',
8611
                        'index' => 'category_name',
8612
                        'width' => '40',
8613
                        'align' => 'left',
8614
                        'search' => 'true',
8615
                        'searchoptions' => ['sopt' => $operators],
8616
                    ],
8617
                    [
8618
                        'name' => 'display_start_date',
8619
                        'index' => 'display_start_date',
8620
                        'width' => '50',
8621
                        'align' => 'left',
8622
                        'search' => 'true',
8623
                        'searchoptions' => [
8624
                            'dataInit' => 'date_pick_today',
8625
                            'sopt' => $date_operators,
8626
                        ],
8627
                    ],
8628
                    [
8629
                        'name' => 'display_end_date',
8630
                        'index' => 'display_end_date',
8631
                        'width' => '50',
8632
                        'align' => 'left',
8633
                        'search' => 'true',
8634
                        'searchoptions' => [
8635
                            'dataInit' => 'date_pick_one_month',
8636
                            'sopt' => $date_operators,
8637
                        ],
8638
                    ],
8639
                    [
8640
                        'name' => 'visibility',
8641
                        'index' => 'visibility',
8642
                        'width' => '40',
8643
                        'align' => 'left',
8644
                        'search' => 'false',
8645
                    ],
8646
                ];
8647
8648
                if ($showCount) {
8649
                    $columns[] = get_lang('Users');
8650
                    $columnModel[] = [
8651
                        'name' => 'users',
8652
                        'index' => 'users',
8653
                        'width' => '20',
8654
                        'align' => 'left',
8655
                        'search' => 'false',
8656
                    ];
8657
8658
                    // ofaj
8659
                    $columns[] = get_lang('Teachers');
8660
                    $columnModel[] = [
8661
                        'name' => 'teachers',
8662
                        'index' => 'teachers',
8663
                        'width' => '20',
8664
                        'align' => 'left',
8665
                        'search' => 'false',
8666
                    ];
8667
                }
8668
8669
                if (api_get_configuration_value('allow_session_status')) {
8670
                    $columns[] = get_lang('SessionStatus');
8671
                    $list = self::getStatusList();
8672
                    $listToString = '';
8673
                    foreach ($list as $statusId => $status) {
8674
                        $listToString .= $statusId.':'.$status.';';
8675
                    }
8676
8677
                    $columnModel[] = ['name' => 'status', 'index' => 'status', 'width' => '25', 'align' => 'left', 'search' => 'true', 'stype' => 'select',
8678
                        // for the bottom bar
8679
                        'searchoptions' => [
8680
                            'defaultValue' => '1',
8681
                            'value' => $listToString,
8682
                        ],
8683
                    ];
8684
                }
8685
                break;
8686
            case 'complete':
8687
                $columns = [
8688
                    get_lang('Name'),
8689
                    get_lang('SessionDisplayStartDate'),
8690
                    get_lang('SessionDisplayEndDate'),
8691
                    get_lang('Coach'),
8692
                    get_lang('Status'),
8693
                    get_lang('Visibility'),
8694
                    get_lang('CourseTitle'),
8695
                ];
8696
                $columnModel = [
8697
                    ['name' => 'name', 'index' => 's.name', 'width' => '200', 'align' => 'left', 'search' => 'true', 'searchoptions' => ['sopt' => $operators]],
8698
                    ['name' => 'display_start_date', 'index' => 'display_start_date', 'width' => '70', 'align' => 'left', 'search' => 'true', 'searchoptions' => ['dataInit' => 'date_pick_today', 'sopt' => $date_operators]],
8699
                    ['name' => 'display_end_date', 'index' => 'display_end_date', 'width' => '70', 'align' => 'left', 'search' => 'true', 'searchoptions' => ['dataInit' => 'date_pick_one_month', 'sopt' => $date_operators]],
8700
                    ['name' => 'coach_name', 'index' => 'coach_name', 'width' => '70', 'align' => 'left', 'search' => 'false', 'searchoptions' => ['sopt' => $operators]],
8701
                    ['name' => 'session_active', 'index' => 'session_active', 'width' => '25', 'align' => 'left', 'search' => 'true', 'stype' => 'select',
8702
                        // for the bottom bar
8703
                        'searchoptions' => [
8704
                            'defaultValue' => '1',
8705
                            'value' => '1:'.get_lang('Active').';0:'.get_lang('Inactive'), ],
8706
                        // for the top bar
8707
                        'editoptions' => ['value' => '" ":'.get_lang('All').';1:'.get_lang('Active').';0:'.get_lang('Inactive')],
8708
                    ],
8709
                    ['name' => 'visibility', 'index' => 'visibility', 'width' => '40', 'align' => 'left', 'search' => 'false'],
8710
                    ['name' => 'course_title', 'index' => 'course_title', 'width' => '50', 'hidden' => 'true', 'search' => 'true', 'searchoptions' => ['searchhidden' => 'true', 'sopt' => $operators]],
8711
                ];
8712
8713
                break;
8714
            case 'custom':
8715
                $columns = [
8716
                    '#',
8717
                    get_lang('Name'),
8718
                    get_lang('Category'),
8719
                    get_lang('SessionDisplayStartDate'),
8720
                    get_lang('SessionDisplayEndDate'),
8721
                    get_lang('Visibility'),
8722
                ];
8723
                $columnModel = [
8724
                    [
8725
                        'name' => 'id',
8726
                        'index' => 's.id',
8727
                        'width' => '160',
8728
                        'hidden' => 'true',
8729
                    ],
8730
                    [
8731
                        'name' => 'name',
8732
                        'index' => 's.name',
8733
                        'width' => '160',
8734
                        'align' => 'left',
8735
                        'search' => 'true',
8736
                        'searchoptions' => ['sopt' => $operators],
8737
                    ],
8738
                    [
8739
                        'name' => 'category_name',
8740
                        'index' => 'category_name',
8741
                        'width' => '40',
8742
                        'align' => 'left',
8743
                        'search' => 'true',
8744
                        'searchoptions' => ['sopt' => $operators],
8745
                    ],
8746
                    [
8747
                        'name' => 'display_start_date',
8748
                        'index' => 'display_start_date',
8749
                        'width' => '50',
8750
                        'align' => 'left',
8751
                        'search' => 'true',
8752
                        'searchoptions' => [
8753
                            'dataInit' => 'date_pick_today',
8754
                            'sopt' => $date_operators,
8755
                        ],
8756
                    ],
8757
                    [
8758
                        'name' => 'display_end_date',
8759
                        'index' => 'display_end_date',
8760
                        'width' => '50',
8761
                        'align' => 'left',
8762
                        'search' => 'true',
8763
                        'searchoptions' => [
8764
                            'dataInit' => 'date_pick_one_month',
8765
                            'sopt' => $date_operators,
8766
                        ],
8767
                    ],
8768
                    [
8769
                        'name' => 'visibility',
8770
                        'index' => 'visibility',
8771
                        'width' => '40',
8772
                        'align' => 'left',
8773
                        'search' => 'false',
8774
                    ],
8775
                ];
8776
8777
                if ($showCount) {
8778
                    $columns[] = get_lang('Users');
8779
                    $columnModel[] = [
8780
                        'name' => 'users',
8781
                        'index' => 'users',
8782
                        'width' => '20',
8783
                        'align' => 'left',
8784
                        'search' => 'false',
8785
                    ];
8786
8787
                    // ofaj
8788
                    $columns[] = get_lang('Teachers');
8789
                    $columnModel[] = [
8790
                        'name' => 'teachers',
8791
                        'index' => 'teachers',
8792
                        'width' => '20',
8793
                        'align' => 'left',
8794
                        'search' => 'false',
8795
                    ];
8796
                }
8797
8798
                if (api_get_configuration_value('allow_session_status')) {
8799
                    $columns[] = get_lang('SessionStatus');
8800
                    $list = self::getStatusList();
8801
                    $listToString = '';
8802
                    foreach ($list as $statusId => $status) {
8803
                        $listToString .= $statusId.':'.$status.';';
8804
                    }
8805
8806
                    $columnModel[] = ['name' => 'status', 'index' => 'status', 'width' => '25', 'align' => 'left', 'search' => 'true', 'stype' => 'select',
8807
                        // for the bottom bar
8808
                        'searchoptions' => [
8809
                            'defaultValue' => '1',
8810
                             'value' => $listToString,
8811
                        ],
8812
                    ];
8813
                }
8814
8815
                break;
8816
        }
8817
8818
        if (!empty($extraFields)) {
8819
            foreach ($extraFields as $field) {
8820
                $columns[] = $field['display_text'];
8821
                $columnModel[] = [
8822
                    'name' => $field['variable'],
8823
                    'index' => $field['variable'],
8824
                    'width' => '80',
8825
                    'align' => 'center',
8826
                    'search' => 'false',
8827
                ];
8828
            }
8829
        }
8830
8831
        // Inject extra session fields
8832
        $rules = [];
8833
        if ($addExtraFields) {
8834
            $sessionField = new ExtraFieldModel('session');
8835
            $rules = $sessionField->getRules($columns, $columnModel);
8836
        }
8837
8838
        if (!in_array('actions', array_column($columnModel, 'name'))) {
8839
            $columnModel[] = [
8840
                'name' => 'actions',
8841
                'index' => 'actions',
8842
                'width' => '80',
8843
                'align' => 'left',
8844
                'formatter' => 'action_formatter',
8845
                'sortable' => 'false',
8846
                'search' => 'false',
8847
            ];
8848
            $columns[] = get_lang('Actions');
8849
        }
8850
8851
        $columnName = [];
8852
        foreach ($columnModel as $col) {
8853
            $columnName[] = $col['name'];
8854
        }
8855
8856
        $return = [
8857
            'columns' => $columns,
8858
            'column_model' => $columnModel,
8859
            'rules' => $rules,
8860
            'simple_column_name' => $columnName,
8861
        ];
8862
8863
        return $return;
8864
    }
8865
8866
    /**
8867
     * Converts all dates sent through the param array (given form) to correct dates with timezones.
8868
     *
8869
     * @param array The dates The same array, with times converted
8870
     * @param bool $applyFormat Whether apply the DATE_TIME_FORMAT_SHORT format for sessions
8871
     *
8872
     * @return array The same array, with times converted
8873
     */
8874
    public static function convert_dates_to_local($params, $applyFormat = false)
8875
    {
8876
        if (!is_array($params)) {
8877
            return false;
8878
        }
8879
        $params['display_start_date'] = api_get_local_time($params['display_start_date'], null, null, true);
8880
        $params['display_end_date'] = api_get_local_time($params['display_end_date'], null, null, true);
8881
8882
        $params['access_start_date'] = api_get_local_time($params['access_start_date'], null, null, true);
8883
        $params['access_end_date'] = api_get_local_time($params['access_end_date'], null, null, true);
8884
8885
        $params['coach_access_start_date'] = isset($params['coach_access_start_date']) ? api_get_local_time($params['coach_access_start_date'], null, null, true) : null;
8886
        $params['coach_access_end_date'] = isset($params['coach_access_end_date']) ? api_get_local_time($params['coach_access_end_date'], null, null, true) : null;
8887
8888
        if ($applyFormat) {
8889
            if (isset($params['display_start_date'])) {
8890
                $params['display_start_date'] = api_format_date($params['display_start_date'], DATE_TIME_FORMAT_SHORT);
8891
            }
8892
8893
            if (isset($params['display_end_date'])) {
8894
                $params['display_end_date'] = api_format_date($params['display_end_date'], DATE_TIME_FORMAT_SHORT);
8895
            }
8896
8897
            if (isset($params['access_start_date'])) {
8898
                $params['access_start_date'] = api_format_date($params['access_start_date'], DATE_TIME_FORMAT_SHORT);
8899
            }
8900
8901
            if (isset($params['access_end_date'])) {
8902
                $params['access_end_date'] = api_format_date($params['access_end_date'], DATE_TIME_FORMAT_SHORT);
8903
            }
8904
8905
            if (isset($params['coach_access_start_date'])) {
8906
                $params['coach_access_start_date'] = api_format_date($params['coach_access_start_date'], DATE_TIME_FORMAT_SHORT);
8907
            }
8908
8909
            if (isset($params['coach_access_end_date'])) {
8910
                $params['coach_access_end_date'] = api_format_date($params['coach_access_end_date'], DATE_TIME_FORMAT_SHORT);
8911
            }
8912
        }
8913
8914
        return $params;
8915
    }
8916
8917
    /**
8918
     * Gets the admin session list callback of the session/session_list.php
8919
     * page with all user/details in the right fomat.
8920
     *
8921
     * @param array $options
8922
     *
8923
     * @return array Array of rows results
8924
     * @asset ('a') === false
8925
     */
8926
    public static function get_sessions_admin_complete($options = [])
8927
    {
8928
        if (!is_array($options)) {
8929
            return false;
8930
        }
8931
8932
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
8933
        $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
8934
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
8935
        $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
8936
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
8937
8938
        $extraFieldTable = Database::get_main_table(TABLE_EXTRA_FIELD);
8939
        $tbl_session_field_values = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
8940
        $tbl_session_field_options = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
8941
8942
        $where = 'WHERE 1 = 1 ';
8943
        $user_id = api_get_user_id();
8944
8945
        if (!api_is_platform_admin()) {
8946
            if (api_is_session_admin() &&
8947
                api_get_setting('allow_session_admins_to_manage_all_sessions') == 'false'
8948
            ) {
8949
                $where .= " AND s.session_admin_id = $user_id ";
8950
            }
8951
        }
8952
8953
        $coach_name = " CONCAT(u.lastname , ' ', u.firstname) as coach_name ";
8954
        if (api_is_western_name_order()) {
8955
            $coach_name = " CONCAT(u.firstname, ' ', u.lastname) as coach_name ";
8956
        }
8957
8958
        $today = api_get_utc_datetime();
8959
        $inject_extra_fields = null;
8960
        $extra_fields_info = [];
8961
8962
        //for now only sessions
8963
        $extra_field = new ExtraFieldModel('session');
8964
        $double_fields = [];
8965
        $extra_field_option = new ExtraFieldOption('session');
8966
8967
        if (isset($options['extra'])) {
8968
            $extra_fields = $options['extra'];
8969
            if (!empty($extra_fields)) {
8970
                foreach ($extra_fields as $extra) {
8971
                    $inject_extra_fields .= " IF (fv.field_id = {$extra['id']}, fvo.option_display_text, NULL ) as {$extra['field']} , ";
8972
                    if (isset($extra_fields_info[$extra['id']])) {
8973
                        $info = $extra_fields_info[$extra['id']];
8974
                    } else {
8975
                        $info = $extra_field->get($extra['id']);
8976
                        $extra_fields_info[$extra['id']] = $info;
8977
                    }
8978
8979
                    if ($info['field_type'] == ExtraFieldModel::FIELD_TYPE_DOUBLE_SELECT) {
8980
                        $double_fields[$info['id']] = $info;
8981
                    }
8982
                }
8983
            }
8984
        }
8985
8986
        $options_by_double = [];
8987
        foreach ($double_fields as $double) {
8988
            $my_options = $extra_field_option->get_field_options_by_field(
8989
                $double['id'],
8990
                true
8991
            );
8992
            $options_by_double['extra_'.$double['field_variable']] = $my_options;
8993
        }
8994
8995
        //sc.name as category_name,
8996
        $select = "
8997
                SELECT * FROM (
8998
                    SELECT DISTINCT
8999
                        IF (
9000
                            (s.access_start_date <= '$today' AND '$today' < s.access_end_date) OR
9001
                            (s.access_start_date = '0000-00-00 00:00:00' AND s.access_end_date = '0000-00-00 00:00:00' ) OR
9002
                            (s.access_start_date IS NULL AND s.access_end_date IS NULL) OR
9003
                            (s.access_start_date <= '$today' AND ('0000-00-00 00:00:00' = s.access_end_date OR s.access_end_date IS NULL )) OR
9004
                            ('$today' < s.access_end_date AND ('0000-00-00 00:00:00' = s.access_start_date OR s.access_start_date IS NULL) )
9005
                        , 1, 0) as session_active,
9006
                s.name,
9007
                s.nbr_courses,
9008
                s.nbr_users,
9009
                s.display_start_date,
9010
                s.display_end_date,
9011
                $coach_name,
9012
                access_start_date,
9013
                access_end_date,
9014
                s.visibility,
9015
                u.user_id,
9016
                $inject_extra_fields
9017
                c.title as course_title,
9018
                s.id ";
9019
9020
        if (!empty($options['where'])) {
9021
            if (!empty($options['extra'])) {
9022
                $options['where'] = str_replace(' 1 = 1  AND', '', $options['where']);
9023
                $options['where'] = str_replace('AND', 'OR', $options['where']);
9024
                foreach ($options['extra'] as $extra) {
9025
                    $options['where'] = str_replace($extra['field'], 'fv.field_id = '.$extra['id'].' AND fvo.option_value', $options['where']);
9026
                }
9027
            }
9028
            $options['where'] = str_replace('course_title', 'c.title', $options['where']);
9029
            $options['where'] = str_replace("( session_active = '0' )", '1=1', $options['where']);
9030
            $options['where'] = str_replace(
9031
                ["AND session_active = '1'  )", " AND (  session_active = '1'  )"],
9032
                [') GROUP BY s.name HAVING session_active = 1 ', " GROUP BY s.name HAVING session_active = 1 "],
9033
                $options['where']
9034
            );
9035
9036
            $options['where'] = str_replace(
9037
                ["AND session_active = '0'  )", " AND (  session_active = '0'  )"],
9038
                [') GROUP BY s.name HAVING session_active = 0 ', " GROUP BY s.name HAVING session_active = '0' "],
9039
                $options['where']
9040
            );
9041
9042
            $where .= ' AND '.$options['where'];
9043
        }
9044
9045
        $limit = '';
9046
        if (!empty($options['limit'])) {
9047
            $limit = ' LIMIT '.$options['limit'];
9048
        }
9049
9050
        $query = "$select FROM $tbl_session s
9051
                    LEFT JOIN $tbl_session_field_values fv
9052
                    ON (fv.item_id = s.id)
9053
                    LEFT JOIN $extraFieldTable f
9054
                    ON f.id = fv.field_id
9055
                    LEFT JOIN $tbl_session_field_options fvo
9056
                    ON (fv.field_id = fvo.field_id)
9057
                    LEFT JOIN $tbl_session_rel_course src
9058
                    ON (src.session_id = s.id)
9059
                    LEFT JOIN $tbl_course c
9060
                    ON (src.c_id = c.id)
9061
                    LEFT JOIN $tbl_session_category sc
9062
                    ON (s.session_category_id = sc.id)
9063
                    INNER JOIN $tbl_user u
9064
                    ON (s.id_coach = u.id)
9065
                    $where
9066
                    $limit
9067
        ";
9068
9069
        if (api_is_multiple_url_enabled()) {
9070
            $table_access_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
9071
            $access_url_id = api_get_current_access_url_id();
9072
            if (-1 != $access_url_id) {
9073
                $query = "$select
9074
                    FROM $tbl_session s
9075
                    LEFT JOIN $tbl_session_field_values fv
9076
                    ON (fv.item_id = s.id)
9077
                    LEFT JOIN $tbl_session_field_options fvo
9078
                    ON (fv.field_id = fvo.field_id)
9079
                    LEFT JOIN $tbl_session_rel_course src
9080
                    ON (src.session_id = s.id)
9081
                    LEFT JOIN $tbl_course c
9082
                    ON (src.c_id = c.id)
9083
                    LEFT JOIN $tbl_session_category sc
9084
                    ON (s.session_category_id = sc.id)
9085
                    INNER JOIN $tbl_user u
9086
                    ON (s.id_coach = u.id)
9087
                    INNER JOIN $table_access_url_rel_session ar
9088
                    ON (ar.session_id = s.id AND ar.access_url_id = $access_url_id)
9089
                    $where
9090
                    $limit
9091
                ";
9092
            }
9093
        }
9094
9095
        $query .= ') AS s';
9096
9097
        if (!empty($options['order'])) {
9098
            $query .= ' ORDER BY '.$options['order'];
9099
        }
9100
9101
        $result = Database::query($query);
9102
9103
        $acceptIcon = Display::return_icon(
9104
            'accept.png',
9105
            get_lang('Active'),
9106
            [],
9107
            ICON_SIZE_SMALL
9108
        );
9109
9110
        $errorIcon = Display::return_icon(
9111
            'error.png',
9112
            get_lang('Inactive'),
9113
            [],
9114
            ICON_SIZE_SMALL
9115
        );
9116
9117
        $formatted_sessions = [];
9118
        if (Database::num_rows($result)) {
9119
            $sessions = Database::store_result($result, 'ASSOC');
9120
            foreach ($sessions as $session) {
9121
                $session_id = $session['id'];
9122
                $session['name'] = Display::url($session['name'], "resume_session.php?id_session=".$session['id']);
9123
                $session['coach_name'] = Display::url($session['coach_name'], "user_information.php?user_id=".$session['user_id']);
9124
                if ($session['session_active'] == 1) {
9125
                    $session['session_active'] = $acceptIcon;
9126
                } else {
9127
                    $session['session_active'] = $errorIcon;
9128
                }
9129
9130
                $session = self::convert_dates_to_local($session);
9131
9132
                switch ($session['visibility']) {
9133
                    case SESSION_VISIBLE_READ_ONLY: //1
9134
                        $session['visibility'] = get_lang('ReadOnly');
9135
                        break;
9136
                    case SESSION_VISIBLE:           //2
9137
                    case SESSION_AVAILABLE:         //4
9138
                        $session['visibility'] = get_lang('Visible');
9139
                        break;
9140
                    case SESSION_INVISIBLE:         //3
9141
                        $session['visibility'] = api_ucfirst(get_lang('Invisible'));
9142
                        break;
9143
                }
9144
9145
                // Cleaning double selects
9146
                foreach ($session as $key => &$value) {
9147
                    if (isset($options_by_double[$key]) || isset($options_by_double[$key.'_second'])) {
9148
                        $options = explode('::', $value);
9149
                    }
9150
                    $original_key = $key;
9151
9152
                    if (strpos($key, '_second') === false) {
9153
                    } else {
9154
                        $key = str_replace('_second', '', $key);
9155
                    }
9156
9157
                    if (isset($options_by_double[$key])) {
9158
                        if (isset($options[0])) {
9159
                            if (isset($options_by_double[$key][$options[0]])) {
9160
                                if (strpos($original_key, '_second') === false) {
9161
                                    $value = $options_by_double[$key][$options[0]]['option_display_text'];
9162
                                } else {
9163
                                    $value = $options_by_double[$key][$options[1]]['option_display_text'];
9164
                                }
9165
                            }
9166
                        }
9167
                    }
9168
                }
9169
9170
                // Magic filter
9171
                if (isset($formatted_sessions[$session_id])) {
9172
                    $formatted_sessions[$session_id] = self::compareArraysToMerge(
9173
                        $formatted_sessions[$session_id],
9174
                        $session
9175
                    );
9176
                } else {
9177
                    $formatted_sessions[$session_id] = $session;
9178
                }
9179
            }
9180
        }
9181
9182
        return $formatted_sessions;
9183
    }
9184
9185
    /**
9186
     * Compare two arrays.
9187
     *
9188
     * @param array $array1
9189
     * @param array $array2
9190
     *
9191
     * @return array
9192
     */
9193
    public static function compareArraysToMerge($array1, $array2)
9194
    {
9195
        if (empty($array2)) {
9196
            return $array1;
9197
        }
9198
        foreach ($array1 as $key => $item) {
9199
            if (!isset($array1[$key])) {
9200
                //My string is empty try the other one
9201
                if (isset($array2[$key]) && !empty($array2[$key])) {
9202
                    $array1[$key] = $array2[$key];
9203
                }
9204
            }
9205
        }
9206
9207
        return $array1;
9208
    }
9209
9210
    /**
9211
     * Get link to the admin page for this session.
9212
     *
9213
     * @param int $id Session ID
9214
     *
9215
     * @return mixed URL to the admin page to manage the session, or false on error
9216
     */
9217
    public static function getAdminPath($id)
9218
    {
9219
        $id = (int) $id;
9220
        $session = self::fetch($id);
9221
        if (empty($session)) {
9222
            return false;
9223
        }
9224
9225
        return api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session='.$id;
9226
    }
9227
9228
    /**
9229
     * Get link to the user page for this session.
9230
     * If a course is provided, build the link to the course.
9231
     *
9232
     * @param int $id       Session ID
9233
     * @param int $courseId Course ID (optional) in case the link has to send straight to the course
9234
     *
9235
     * @return mixed URL to the page to use the session, or false on error
9236
     */
9237
    public static function getPath($id, $courseId = 0)
9238
    {
9239
        $id = (int) $id;
9240
        $session = self::fetch($id);
9241
        if (empty($session)) {
9242
            return false;
9243
        }
9244
        if (empty($courseId)) {
9245
            return api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$id;
9246
        } else {
9247
            $courseInfo = api_get_course_info_by_id($courseId);
9248
            if ($courseInfo) {
9249
                return $courseInfo['course_public_url'].'?id_session='.$id;
9250
            }
9251
        }
9252
9253
        return false;
9254
    }
9255
9256
    /**
9257
     * Return an associative array 'id_course' => [id_session1, id_session2...]
9258
     * where course id_course is in sessions id_session1, id_session2
9259
     * for course where user is coach
9260
     * i.e. coach for the course or
9261
     * main coach for a session the course is in
9262
     * for a session category (or woth no session category if empty).
9263
     *
9264
     * @param int $userId
9265
     *
9266
     * @return array
9267
     */
9268
    public static function getSessionCourseForUser($userId)
9269
    {
9270
        // list of COURSES where user is COURSE session coach
9271
        $listCourseCourseCoachSession = self::getCoursesForCourseSessionCoach($userId);
9272
        // list of courses where user is MAIN session coach
9273
        $listCourseMainCoachSession = self::getCoursesForMainSessionCoach($userId);
9274
        // merge these 2 array
9275
        $listResCourseSession = $listCourseCourseCoachSession;
9276
        foreach ($listCourseMainCoachSession as $courseId2 => $listSessionId2) {
9277
            if (isset($listResCourseSession[$courseId2])) {
9278
                // if sessionId array exists for this course
9279
                // same courseId, merge the list of session
9280
                foreach ($listCourseMainCoachSession[$courseId2] as $i => $sessionId2) {
9281
                    if (!in_array($sessionId2, $listResCourseSession[$courseId2])) {
9282
                        $listResCourseSession[$courseId2][] = $sessionId2;
9283
                    }
9284
                }
9285
            } else {
9286
                $listResCourseSession[$courseId2] = $listSessionId2;
9287
            }
9288
        }
9289
9290
        return $listResCourseSession;
9291
    }
9292
9293
    /**
9294
     * Return an associative array 'id_course' => [id_session1, id_session2...]
9295
     * where course id_course is in sessions id_session1, id_session2.
9296
     *
9297
     * @param int $userId
9298
     *
9299
     * @return array
9300
     */
9301
    public static function getCoursesForCourseSessionCoach($userId)
9302
    {
9303
        $userId = (int) $userId;
9304
        $listResCourseSession = [];
9305
        $tblCourse = Database::get_main_table(TABLE_MAIN_COURSE);
9306
        $tblSessionRelCourseRelUser = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
9307
9308
        $sql = "SELECT session_id, c_id, c.id
9309
                FROM $tblSessionRelCourseRelUser srcru
9310
                LEFT JOIN $tblCourse c
9311
                ON c.id = srcru.c_id
9312
                WHERE
9313
                    srcru.user_id = $userId AND
9314
                    srcru.status = 2";
9315
9316
        $res = Database::query($sql);
9317
9318
        while ($data = Database::fetch_assoc($res)) {
9319
            if (api_get_session_visibility($data['session_id'])) {
9320
                if (!isset($listResCourseSession[$data['id']])) {
9321
                    $listResCourseSession[$data['id']] = [];
9322
                }
9323
                $listResCourseSession[$data['id']][] = $data['session_id'];
9324
            }
9325
        }
9326
9327
        return $listResCourseSession;
9328
    }
9329
9330
    /**
9331
     * Return an associative array 'id_course' => [id_session1, id_session2...]
9332
     * where course id_course is in sessions id_session1, id_session2.
9333
     *
9334
     * @param $userId
9335
     *
9336
     * @return array
9337
     */
9338
    public static function getCoursesForMainSessionCoach($userId)
9339
    {
9340
        $userId = (int) $userId;
9341
        $listResCourseSession = [];
9342
        $tblSession = Database::get_main_table(TABLE_MAIN_SESSION);
9343
9344
        // list of SESSION where user is session coach
9345
        $sql = "SELECT id FROM $tblSession
9346
                WHERE id_coach = ".$userId;
9347
        $res = Database::query($sql);
9348
9349
        while ($data = Database::fetch_assoc($res)) {
9350
            $sessionId = $data['id'];
9351
            $listCoursesInSession = self::getCoursesInSession($sessionId);
9352
            foreach ($listCoursesInSession as $i => $courseId) {
9353
                if (api_get_session_visibility($sessionId)) {
9354
                    if (!isset($listResCourseSession[$courseId])) {
9355
                        $listResCourseSession[$courseId] = [];
9356
                    }
9357
                    $listResCourseSession[$courseId][] = $sessionId;
9358
                }
9359
            }
9360
        }
9361
9362
        return $listResCourseSession;
9363
    }
9364
9365
    /**
9366
     * Return an array of course_id used in session $sessionId.
9367
     *
9368
     * @param $sessionId
9369
     *
9370
     * @return array
9371
     */
9372
    public static function getCoursesInSession($sessionId)
9373
    {
9374
        if (empty($sessionId)) {
9375
            return [];
9376
        }
9377
9378
        $tblSessionRelCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
9379
        $tblCourse = Database::get_main_table(TABLE_MAIN_COURSE);
9380
9381
        // list of course in this session
9382
        $sql = "SELECT session_id, c.id
9383
                FROM $tblSessionRelCourse src
9384
                LEFT JOIN $tblCourse c
9385
                ON c.id = src.c_id
9386
                WHERE session_id = ".intval($sessionId);
9387
        $res = Database::query($sql);
9388
9389
        $listResultsCourseId = [];
9390
        while ($data = Database::fetch_assoc($res)) {
9391
            $listResultsCourseId[] = $data['id'];
9392
        }
9393
9394
        return $listResultsCourseId;
9395
    }
9396
9397
    /**
9398
     * Return an array of courses in session for user
9399
     * and for each courses the list of session that use this course for user.
9400
     *
9401
     * [0] => array
9402
     *      userCatId
9403
     *      userCatTitle
9404
     *      courseInUserCatList
9405
     *          [0] => array
9406
     *              courseId
9407
     *              title
9408
     *              courseCode
9409
     *              sessionCatList
9410
     *                  [0] => array
9411
     *                      catSessionId
9412
     *                      catSessionName
9413
     *                      sessionList
9414
     *                          [0] => array
9415
     *                              sessionId
9416
     *                              sessionName
9417
     *
9418
     * @param int $userId
9419
     *
9420
     * @return array
9421
     */
9422
    public static function getNamedSessionCourseForCoach($userId)
9423
    {
9424
        $listResults = [];
9425
        $listCourseSession = self::getSessionCourseForUser($userId);
9426
        foreach ($listCourseSession as $courseId => $listSessionId) {
9427
            // Course info
9428
            $courseInfo = api_get_course_info_by_id($courseId);
9429
            $listOneCourse = [];
9430
            $listOneCourse['courseId'] = $courseId;
9431
            $listOneCourse['title'] = $courseInfo['title'];
9432
            //$listOneCourse['courseCode'] = $courseInfo['code'];
9433
            $listOneCourse['course'] = $courseInfo;
9434
            $listOneCourse['sessionCatList'] = [];
9435
            $listCat = [];
9436
            foreach ($listSessionId as $i => $sessionId) {
9437
                // here we got all session for this course
9438
                // lets check there session categories
9439
                $sessionInfo = self::fetch($sessionId);
9440
                $catId = $sessionInfo['session_category_id'];
9441
                if (!isset($listCat[$catId])) {
9442
                    $listCatInfo = self::get_session_category($catId);
9443
                    $listCat[$catId] = [];
9444
                    $listCat[$catId]['catSessionId'] = $catId;
9445
                    $listCat[$catId]['catSessionName'] = $listCatInfo['name'];
9446
                    $listCat[$catId]['sessionList'] = [];
9447
                }
9448
                $listSessionInfo = self::fetch($sessionId);
9449
                $listSessionIdName = [
9450
                    'sessionId' => $sessionId,
9451
                    'sessionName' => $listSessionInfo['name'],
9452
                ];
9453
                $listCat[$catId]['sessionList'][] = $listSessionIdName;
9454
            }
9455
            // sort $listCat by catSessionName
9456
            usort($listCat, 'self::compareBySessionName');
9457
            // in each catSession sort sessionList by sessionName
9458
            foreach ($listCat as $i => $listCatSessionInfo) {
9459
                $listSessionList = $listCatSessionInfo['sessionList'];
9460
                usort($listSessionList, 'self::compareCatSessionInfo');
9461
                $listCat[$i]['sessionList'] = $listSessionList;
9462
            }
9463
9464
            $listOneCourse['sessionCatList'] = $listCat;
9465
9466
            // user course category
9467
            $courseCategory = CourseManager::getUserCourseCategoryForCourse(
9468
                $userId,
9469
                $courseId
9470
            );
9471
9472
            $userCatTitle = '';
9473
            $userCatId = 0;
9474
            if ($courseCategory) {
9475
                $userCatId = $courseCategory['user_course_cat'];
9476
                $userCatTitle = $courseCategory['title'];
9477
            }
9478
9479
            $listResults[$userCatId]['courseInUserCategoryId'] = $userCatId;
9480
            $listResults[$userCatId]['courseInUserCategoryTitle'] = $userCatTitle;
9481
            $listResults[$userCatId]['courseInUserCatList'][] = $listOneCourse;
9482
        }
9483
9484
        // sort by user course cat
9485
        uasort($listResults, 'self::compareByUserCourseCat');
9486
9487
        // sort by course title
9488
        foreach ($listResults as $userCourseCatId => $tabCoursesInCat) {
9489
            $courseInUserCatList = $tabCoursesInCat['courseInUserCatList'];
9490
            uasort($courseInUserCatList, 'self::compareByCourse');
9491
            $listResults[$userCourseCatId]['courseInUserCatList'] = $courseInUserCatList;
9492
        }
9493
9494
        return $listResults;
9495
    }
9496
9497
    /**
9498
     * @param int $userId
9499
     * @param int $courseId
9500
     *
9501
     * @return array
9502
     */
9503
    public static function searchCourseInSessionsFromUser($userId, $courseId)
9504
    {
9505
        $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
9506
        $userId = (int) $userId;
9507
        $courseId = (int) $courseId;
9508
        if (empty($userId) || empty($courseId)) {
9509
            return [];
9510
        }
9511
9512
        $sql = "SELECT * FROM $table
9513
                WHERE c_id = $courseId AND user_id = $userId";
9514
        $result = Database::query($sql);
9515
9516
        return Database::store_result($result, 'ASSOC');
9517
    }
9518
9519
    /**
9520
     * Subscribe and redirect to session after inscription.
9521
     */
9522
    public static function redirectToSession()
9523
    {
9524
        $sessionId = (int) ChamiloSession::read('session_redirect');
9525
        $onlyOneCourseSessionToRedirect = ChamiloSession::read('only_one_course_session_redirect');
9526
        if ($sessionId) {
9527
            $sessionInfo = api_get_session_info($sessionId);
9528
            if (!empty($sessionInfo)) {
9529
                $userId = api_get_user_id();
9530
                $response = self::isUserSubscribedAsStudent($sessionId, $userId);
9531
                if ($response) {
9532
                    $urlToRedirect = api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$sessionId;
9533
                    if (!empty($onlyOneCourseSessionToRedirect)) {
9534
                        $urlToRedirect = api_get_path(WEB_PATH).
9535
                            'courses/'.$onlyOneCourseSessionToRedirect.'/index.php?id_session='.$sessionId;
9536
                    }
9537
9538
                    header('Location: '.$urlToRedirect);
9539
                    exit;
9540
                }
9541
            }
9542
        }
9543
    }
9544
9545
    /**
9546
     * @return int
9547
     */
9548
    public static function getCountUsersInCourseSession(
9549
        Course $course,
9550
        Session $session
9551
    ) {
9552
        return Database::getManager()
9553
            ->createQuery("
9554
                SELECT COUNT(scu)
9555
                FROM ChamiloCoreBundle:SessionRelCourseRelUser scu
9556
                INNER JOIN ChamiloCoreBundle:SessionRelUser su
9557
                    WITH scu.user = su.user
9558
                    AND scu.session = su.session
9559
                WHERE
9560
                    scu.course = :course AND
9561
                    su.relationType <> :relationType AND
9562
                    scu.session = :session
9563
            ")
9564
            ->setParameters([
9565
                'course' => $course->getId(),
9566
                'relationType' => SESSION_RELATION_TYPE_RRHH,
9567
                'session' => $session->getId(),
9568
            ])
9569
            ->getSingleScalarResult();
9570
    }
9571
9572
    /**
9573
     * Get course IDs where user in not subscribed in session.
9574
     *
9575
     * @return array
9576
     */
9577
    public static function getAvoidedCoursesInSession(User $user, Session $session)
9578
    {
9579
        $courseIds = [];
9580
9581
        /** @var SessionRelCourse $sessionCourse */
9582
        foreach ($session->getCourses() as $sessionCourse) {
9583
            /** @var Course $course */
9584
            $course = $sessionCourse->getCourse();
9585
9586
            if ($session->getUserInCourse($user, $course)->count()) {
9587
                continue;
9588
            }
9589
9590
            $courseIds[] = $course->getId();
9591
        }
9592
9593
        return $courseIds;
9594
    }
9595
9596
    /**
9597
     * @param int             $userId
9598
     * @param int             $sessionId
9599
     * @param ExtraFieldValue $extraFieldValue
9600
     * @param string          $collapsableLink
9601
     *
9602
     * @return array
9603
     */
9604
    public static function getCollapsableData($userId, $sessionId, $extraFieldValue, $collapsableLink)
9605
    {
9606
        $collapsed = 0;
9607
9608
        // Get default collapsed value in extra field
9609
        $value = $extraFieldValue->get_values_by_handler_and_field_variable($sessionId, 'collapsed');
9610
        if (!empty($value) && isset($value['value'])) {
9611
            $collapsed = $value['value'];
9612
        }
9613
9614
        $userRelSession = self::getUserSession($userId, $sessionId);
9615
9616
        if ($userRelSession) {
9617
            if (isset($userRelSession['collapsed']) && '' != $userRelSession['collapsed']) {
9618
                $collapsed = $userRelSession['collapsed'];
9619
            }
9620
        } else {
9621
            return ['collapsed' => $collapsed, 'collapsable_link' => '&nbsp;'];
9622
        }
9623
9624
        $link = $collapsableLink.'&session_id='.$sessionId.'&value=1';
9625
        $image = '<i class="fa fa-folder-open"></i>';
9626
        if (1 == $collapsed) {
9627
            $link = $collapsableLink.'&session_id='.$sessionId.'&value=0';
9628
            $image = '<i class="fa fa-folder"></i>';
9629
        }
9630
9631
        $link = Display::url(
9632
            $image,
9633
            $link
9634
        );
9635
9636
        return ['collapsed' => $collapsed, 'collapsable_link' => $link];
9637
    }
9638
9639
    /**
9640
     * Converts "start date" and "end date" to "From start date to end date" string.
9641
     *
9642
     * @param string $startDate
9643
     * @param string $endDate
9644
     * @param bool   $showTime
9645
     * @param bool   $dateHuman
9646
     *
9647
     * @return string
9648
     */
9649
    public static function convertSessionDateToString($startDate, $endDate, $showTime, $dateHuman)
9650
    {
9651
        // api_get_local_time returns empty if date is invalid like 0000-00-00 00:00:00
9652
        $startDateToLocal = api_get_local_time(
9653
            $startDate,
9654
            null,
9655
            null,
9656
            true,
9657
            $showTime,
9658
            $dateHuman
9659
        );
9660
        $endDateToLocal = api_get_local_time(
9661
            $endDate,
9662
            null,
9663
            null,
9664
            true,
9665
            $showTime,
9666
            $dateHuman
9667
        );
9668
9669
        $format = $showTime ? DATE_TIME_FORMAT_LONG_24H : DATE_FORMAT_LONG_NO_DAY;
9670
9671
        $result = '';
9672
        if (!empty($startDateToLocal) && !empty($endDateToLocal)) {
9673
            $result = sprintf(
9674
                get_lang('FromDateXToDateY'),
9675
                api_format_date($startDateToLocal, $format),
9676
                api_format_date($endDateToLocal, $format)
9677
            );
9678
        } else {
9679
            if (!empty($startDateToLocal)) {
9680
                $result = get_lang('From').' '.api_format_date($startDateToLocal, $format);
9681
            }
9682
            if (!empty($endDateToLocal)) {
9683
                $result = get_lang('Until').' '.api_format_date($endDateToLocal, $format);
9684
            }
9685
        }
9686
        if (empty($result)) {
9687
            $result = get_lang('NoTimeLimits');
9688
        }
9689
9690
        return $result;
9691
    }
9692
9693
    public static function getStatusList()
9694
    {
9695
        return [
9696
            self::STATUS_PLANNED => get_lang('Planned'),
9697
            self::STATUS_PROGRESS => get_lang('InProgress'),
9698
            self::STATUS_FINISHED => get_lang('Finished'),
9699
            self::STATUS_CANCELLED => get_lang('Cancelled'),
9700
        ];
9701
    }
9702
9703
    public static function getStatusLabel($status)
9704
    {
9705
        $list = self::getStatusList();
9706
9707
        if (!isset($list[$status])) {
9708
            return get_lang('NoStatus');
9709
        }
9710
9711
        return $list[$status];
9712
    }
9713
9714
    public static function getDefaultSessionTab()
9715
    {
9716
        $default = 'all';
9717
        $view = api_get_configuration_value('default_session_list_view');
9718
9719
        if (!empty($view)) {
9720
            $default = $view;
9721
        }
9722
9723
        return $default;
9724
    }
9725
9726
    /**
9727
     * @return string
9728
     */
9729
    public static function getSessionListTabs($listType)
9730
    {
9731
        $tabs = [
9732
            [
9733
                'content' => get_lang('AllSessionsShort'),
9734
                'url' => api_get_path(WEB_CODE_PATH).'session/session_list.php?list_type=all',
9735
            ],
9736
            [
9737
                'content' => get_lang('ActiveSessionsShort'),
9738
                'url' => api_get_path(WEB_CODE_PATH).'session/session_list.php?list_type=active',
9739
            ],
9740
            [
9741
                'content' => get_lang('ClosedSessionsShort'),
9742
                'url' => api_get_path(WEB_CODE_PATH).'session/session_list.php?list_type=close',
9743
            ],
9744
            [
9745
                'content' => get_lang('SessionListCustom'),
9746
                'url' => api_get_path(WEB_CODE_PATH).'session/session_list.php?list_type=custom',
9747
            ],
9748
            /*[
9749
                'content' => get_lang('Complete'),
9750
                'url' => api_get_path(WEB_CODE_PATH).'session/session_list_simple.php?list_type=complete',
9751
            ],*/
9752
        ];
9753
9754
        switch ($listType) {
9755
            case 'all':
9756
                $default = 1;
9757
                break;
9758
            case 'active':
9759
                $default = 2;
9760
                break;
9761
            case 'close':
9762
                $default = 3;
9763
                break;
9764
            case 'custom':
9765
                $default = 4;
9766
                break;
9767
        }
9768
9769
        return Display::tabsOnlyLink($tabs, $default);
9770
    }
9771
9772
    /**
9773
     * Check if a session is followed by human resources manager.
9774
     *
9775
     * @param int $sessionId
9776
     * @param int $userId
9777
     *
9778
     * @return bool
9779
     */
9780
    public static function isSessionFollowedByDrh($sessionId, $userId)
9781
    {
9782
        $userId = (int) $userId;
9783
        $sessionId = (int) $sessionId;
9784
9785
        $tblSession = Database::get_main_table(TABLE_MAIN_SESSION);
9786
        $tblSessionRelUser = Database::get_main_table(TABLE_MAIN_SESSION_USER);
9787
9788
        if (api_is_multiple_url_enabled()) {
9789
            $tblSessionRelAccessUrl = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
9790
9791
            $sql = "SELECT s.id FROM $tblSession s
9792
                INNER JOIN $tblSessionRelUser sru ON (sru.session_id = s.id)
9793
                LEFT JOIN $tblSessionRelAccessUrl a ON (s.id = a.session_id)
9794
                WHERE
9795
                    sru.user_id = '$userId' AND
9796
                    sru.session_id = '$sessionId' AND
9797
                    sru.relation_type = '".SESSION_RELATION_TYPE_RRHH."' AND
9798
                    access_url_id = ".api_get_current_access_url_id();
9799
        } else {
9800
            $sql = "SELECT s.id FROM $tblSession s
9801
                INNER JOIN $tblSessionRelUser sru ON sru.session_id = s.id
9802
                WHERE
9803
                    sru.user_id = '$userId' AND
9804
                    sru.session_id = '$sessionId' AND
9805
                    sru.relation_type = '".SESSION_RELATION_TYPE_RRHH."'";
9806
        }
9807
9808
        $result = Database::query($sql);
9809
9810
        return Database::num_rows($result) > 0;
9811
    }
9812
9813
    /**
9814
     * Add a warning message when session is read-only mode.
9815
     */
9816
    public static function addFlashSessionReadOnly()
9817
    {
9818
        if (api_get_session_id() && !api_is_allowed_to_session_edit()) {
9819
            Display::addFlash(
9820
                Display::return_message(get_lang('SessionIsReadOnly'), 'warning')
9821
            );
9822
        }
9823
    }
9824
9825
    public static function insertUsersInCourses(array $studentIds, array $courseIds, int $sessionId)
9826
    {
9827
        $tblSessionUser = Database::get_main_table(TABLE_MAIN_SESSION_USER);
9828
        $tblSession = Database::get_main_table(TABLE_MAIN_SESSION);
9829
9830
        foreach ($courseIds as $courseId) {
9831
            self::insertUsersInCourse($studentIds, $courseId, $sessionId, [], false);
9832
        }
9833
9834
        foreach ($studentIds as $studentId) {
9835
            Database::query(
9836
                "INSERT IGNORE INTO $tblSessionUser (session_id, user_id, registered_at)
9837
                VALUES ($sessionId, $studentId, '".api_get_utc_datetime()."')"
9838
            );
9839
        }
9840
9841
        Database::query(
9842
            "UPDATE $tblSession s
9843
            SET s.nbr_users = (
9844
                SELECT COUNT(1) FROM session_rel_user sru
9845
                WHERE sru.session_id = $sessionId AND sru.relation_type <> ".Session::DRH."
9846
            )
9847
            WHERE s.id = $sessionId"
9848
        );
9849
    }
9850
9851
    public static function insertUsersInCourse(
9852
        array $studentIds,
9853
        int $courseId,
9854
        int $sessionId,
9855
        array $relationInfo = [],
9856
        bool $updateSession = true
9857
    ) {
9858
        $tblSessionCourseUser = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
9859
        $tblSessionCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
9860
        $tblSessionUser = Database::get_main_table(TABLE_MAIN_SESSION_USER);
9861
        $tblSession = Database::get_main_table(TABLE_MAIN_SESSION);
9862
9863
        $relationInfo = array_merge(['visibility' => 0, 'status' => Session::STUDENT], $relationInfo);
9864
9865
        $sessionCourseUser = [
9866
            'session_id' => $sessionId,
9867
            'c_id' => $courseId,
9868
            'visibility' => $relationInfo['visibility'],
9869
            'status' => $relationInfo['status'],
9870
        ];
9871
        $sessionUser = [
9872
            'session_id' => $sessionId,
9873
            'registered_at' => api_get_utc_datetime(),
9874
        ];
9875
9876
        foreach ($studentIds as $studentId) {
9877
            $sessionCourseUser['user_id'] = $studentId;
9878
9879
            $count = Database::select(
9880
                'COUNT(1) as nbr',
9881
                $tblSessionCourseUser,
9882
                ['where' => ['session_id = ? AND c_id = ? AND user_id = ?' => [$sessionId, $courseId, $studentId]]],
9883
                'first'
9884
            );
9885
9886
            if (empty($count['nbr'])) {
9887
                Database::insert($tblSessionCourseUser, $sessionCourseUser);
9888
9889
                Event::logUserSubscribedInCourseSession($studentId, $courseId, $sessionId);
9890
            }
9891
9892
            if ($updateSession) {
9893
                $sessionUser['user_id'] = $studentId;
9894
9895
                $count = Database::select(
9896
                    'COUNT(1) as nbr',
9897
                    $tblSessionUser,
9898
                    ['where' => ['session_id = ? AND user_id = ?' => [$sessionId, $studentId]]],
9899
                    'first'
9900
                );
9901
9902
                if (empty($count['nbr'])) {
9903
                    Database::insert($tblSessionUser, $sessionUser);
9904
                }
9905
            }
9906
        }
9907
9908
        Database::query(
9909
            "UPDATE $tblSessionCourse src
9910
            SET src.nbr_users = (
9911
                SELECT COUNT(1) FROM $tblSessionCourseUser srcru
9912
                WHERE
9913
                    srcru.session_id = $sessionId AND srcru.c_id = $courseId AND srcru.status <> ".Session::COACH."
9914
            )
9915
            WHERE src.session_id = $sessionId AND src.c_id = $courseId"
9916
        );
9917
9918
        if ($updateSession) {
9919
            Database::query(
9920
                "UPDATE $tblSession s
9921
                SET s.nbr_users = (
9922
                    SELECT COUNT(1) FROM session_rel_user sru
9923
                    WHERE sru.session_id = $sessionId AND sru.relation_type <> ".Session::DRH."
9924
                )
9925
                WHERE s.id = $sessionId"
9926
            );
9927
        }
9928
    }
9929
9930
    public static function getCareersFromSession(int $sessionId): array
9931
    {
9932
        $extraFieldValueSession = new ExtraFieldValue('session');
9933
        $extraFieldValueCareer = new ExtraFieldValue('career');
9934
9935
        $value = $extraFieldValueSession->get_values_by_handler_and_field_variable($sessionId, 'careerid');
9936
        $careers = [];
9937
        if (isset($value['value']) && !empty($value['value'])) {
9938
            $careerList = str_replace(['[', ']'], '', $value['value']);
9939
            $careerList = explode(',', $careerList);
9940
            $careerManager = new Career();
9941
            foreach ($careerList as $career) {
9942
                $careerIdValue = $extraFieldValueCareer->get_item_id_from_field_variable_and_field_value(
9943
                    'external_career_id',
9944
                    $career
9945
                );
9946
                if (isset($careerIdValue['item_id']) && !empty($careerIdValue['item_id'])) {
9947
                    $finalCareerId = $careerIdValue['item_id'];
9948
                    $careerInfo = $careerManager->get($finalCareerId);
9949
                    if (!empty($careerInfo)) {
9950
                        $careers[] = $careerInfo;
9951
                    }
9952
                }
9953
            }
9954
        }
9955
9956
        return $careers;
9957
    }
9958
9959
    public static function getCareerDiagramPerSessionList($sessionList, $userId)
9960
    {
9961
        if (empty($sessionList) || empty($userId)) {
9962
            return '';
9963
        }
9964
9965
        $userId = (int) $userId;
9966
        $careersAdded = [];
9967
        $careerModel = new Career();
9968
        $frames = '';
9969
        foreach ($sessionList as $sessionId) {
9970
            $visibility = api_get_session_visibility($sessionId, null, false, $userId);
9971
            if (SESSION_AVAILABLE === $visibility) {
9972
                $careerList = self::getCareersFromSession($sessionId);
9973
                if (empty($careerList)) {
9974
                    continue;
9975
                }
9976
                foreach ($careerList as $career) {
9977
                    $careerId = $careerIdToShow = $career['id'];
9978
                    if (api_get_configuration_value('use_career_external_id_as_identifier_in_diagrams')) {
9979
                        $careerIdToShow = $careerModel->getCareerIdFromInternalToExternal($careerId);
9980
                    }
9981
9982
                    if (!in_array($careerId, $careersAdded)) {
9983
                        $careersAdded[] = $careerId;
9984
                        $careerUrl = api_get_path(WEB_CODE_PATH).'user/career_diagram.php?iframe=1&career_id='.$careerIdToShow.'&user_id='.$userId;
9985
                        $frames .= '
9986
                            <iframe
9987
                                onload="resizeIframe(this)"
9988
                                style="width:100%;"
9989
                                border="0"
9990
                                frameborder="0"
9991
                                scrolling="no"
9992
                                src="'.$careerUrl.'"
9993
                            ></iframe>';
9994
                    }
9995
                }
9996
            }
9997
        }
9998
9999
        $content = '';
10000
        if (!empty($frames)) {
10001
            $content = Display::page_subheader(get_lang('OngoingTraining'));
10002
            $content .= '
10003
               <script>
10004
                resizeIframe = function(iFrame) {
10005
                    iFrame.height = iFrame.contentWindow.document.body.scrollHeight + 20;
10006
                }
10007
                </script>
10008
            ';
10009
            $content .= $frames;
10010
            $content .= Career::renderDiagramFooter();
10011
        }
10012
10013
        return $content;
10014
    }
10015
10016
    /**
10017
     * @param int $id
10018
     *
10019
     * @return bool
10020
     */
10021
    private static function allowed($id)
10022
    {
10023
        $sessionInfo = self::fetch($id);
10024
10025
        if (empty($sessionInfo)) {
10026
            return false;
10027
        }
10028
10029
        if (api_is_platform_admin()) {
10030
            return true;
10031
        }
10032
10033
        $userId = api_get_user_id();
10034
10035
        if (api_is_session_admin() &&
10036
            api_get_setting('allow_session_admins_to_manage_all_sessions') != 'true'
10037
        ) {
10038
            if ($sessionInfo['session_admin_id'] != $userId) {
10039
                return false;
10040
            }
10041
        }
10042
10043
        if (api_is_teacher() &&
10044
            api_get_setting('allow_teachers_to_create_sessions') == 'true'
10045
        ) {
10046
            if ($sessionInfo['id_coach'] != $userId) {
10047
                return false;
10048
            }
10049
        }
10050
10051
        return true;
10052
    }
10053
10054
    /**
10055
     * Add classes (by their names) to a session.
10056
     *
10057
     * @param int   $sessionId
10058
     * @param array $classesNames
10059
     * @param bool  $deleteClassSessions Optional. Empty the session list for the usergroup (class)
10060
     */
10061
    private static function addClassesByName($sessionId, $classesNames, $deleteClassSessions = true)
10062
    {
10063
        if (!$classesNames) {
10064
            return;
10065
        }
10066
10067
        $usergroup = new UserGroup();
10068
10069
        foreach ($classesNames as $className) {
10070
            if (empty($className)) {
10071
                continue;
10072
            }
10073
10074
            $usergroup->subscribe_sessions_to_usergroup(
10075
                $usergroup->getIdByName($className),
10076
                [$sessionId],
10077
                $deleteClassSessions
10078
            );
10079
        }
10080
    }
10081
10082
    /**
10083
     * @param array $listA
10084
     * @param array $listB
10085
     *
10086
     * @return int
10087
     */
10088
    private static function compareCatSessionInfo($listA, $listB)
10089
    {
10090
        if ($listA['sessionName'] == $listB['sessionName']) {
10091
            return 0;
10092
        } elseif ($listA['sessionName'] > $listB['sessionName']) {
10093
            return 1;
10094
        } else {
10095
            return -1;
10096
        }
10097
    }
10098
10099
    /**
10100
     * @param array $listA
10101
     * @param array $listB
10102
     *
10103
     * @return int
10104
     */
10105
    private static function compareBySessionName($listA, $listB)
10106
    {
10107
        if ('' == $listB['catSessionName']) {
10108
            return -1;
10109
        } elseif ('' == $listA['catSessionName']) {
10110
            return 1;
10111
        } elseif ($listA['catSessionName'] == $listB['catSessionName']) {
10112
            return 0;
10113
        } elseif ($listA['catSessionName'] > $listB['catSessionName']) {
10114
            return 1;
10115
        } else {
10116
            return -1;
10117
        }
10118
    }
10119
10120
    /**
10121
     * @param array $listA
10122
     * @param array $listB
10123
     *
10124
     * @return int
10125
     */
10126
    private static function compareByUserCourseCat($listA, $listB)
10127
    {
10128
        if ($listA['courseInUserCategoryTitle'] == $listB['courseInUserCategoryTitle']) {
10129
            return 0;
10130
        } elseif ($listA['courseInUserCategoryTitle'] > $listB['courseInUserCategoryTitle']) {
10131
            return 1;
10132
        } else {
10133
            return -1;
10134
        }
10135
    }
10136
10137
    /**
10138
     * @param array $listA
10139
     * @param array $listB
10140
     *
10141
     * @return int
10142
     */
10143
    private static function compareByCourse($listA, $listB)
10144
    {
10145
        if ($listA['title'] == $listB['title']) {
10146
            return 0;
10147
        } elseif ($listA['title'] > $listB['title']) {
10148
            return 1;
10149
        } else {
10150
            return -1;
10151
        }
10152
    }
10153
}
10154