Passed
Push — 1.11.x ( 39bed2...1896a1 )
by Angel Fernando Quiroz
13:05 queued 30s
created

SessionManager   F

Complexity

Total Complexity 1237

Size/Duplication

Total Lines 9850
Duplicated Lines 0 %

Importance

Changes 8
Bugs 2 Features 0
Metric Value
eloc 5039
c 8
b 2
f 0
dl 0
loc 9850
rs 0.8
wmc 1237

145 Methods

Rating   Name   Duplication   Size   Complexity  
A getDrhUsersInSession() 0 3 1
A getSessionFollowedByDrh() 0 40 3
F edit_category_session() 0 58 20
D create_category_session() 0 65 19
A sessionNameExists() 0 8 1
B get_count_admin() 0 109 7
A get_course_list_by_session_id_like() 0 33 5
A getSessionIdFromOriginalId() 0 13 2
A getCourseCountBySessionId() 0 29 3
B get_course_list_by_session_id() 0 55 8
B getAllCoursesFollowedByUser() 0 71 9
F fetch() 0 77 14
A __construct() 0 2 1
F create_session() 0 173 33
F formatSessionsAdminForGrid() 0 117 32
F edit_session() 0 144 25
A generateNextSessionName() 0 23 4
F get_user_data_access_tracking_overview() 0 141 20
F get_session_lp_progress() 0 107 13
F get_survey_overview() 0 104 13
F getSessionsForAdmin() 0 176 29
F get_session_progress() 0 341 24
A clear_session_ref_promotion() 0 11 2
C delete() 0 132 12
F add_courses_to_session() 0 277 29
C subscribeSessionsToDrh() 0 106 12
A unsubscribe_user_from_session() 0 41 3
A getCoachesBySession() 0 18 3
A getUsersByCourseSession() 0 35 6
A create_session_extra_field() 0 15 1
A user_is_general_coach() 0 14 3
B getUsersByCourseAndSessionList() 0 40 8
A getUserStatusInSession() 0 11 1
B subscribe_users_to_session_course() 0 56 10
A protectSession() 0 4 2
A allowOnlyMySessions() 0 10 4
A removeAllDrhFromSession() 0 16 2
A unsubscribe_course_from_session() 0 46 3
A cantEditSession() 0 19 6
B removeUsersFromCourseSession() 0 63 9
A sessionNameExistBesidesMySession() 0 20 3
A get_sessions_followed_by_drh() 0 24 1
B set_coach_to_course_session() 0 94 10
F get_sessions_list() 0 94 17
A subscribe_sessions_to_promotion() 0 16 3
A get_all_session_category() 0 15 2
A getAllCoursesFromAllSessionFromDrh() 0 14 4
B get_users_by_session() 0 60 9
A unSubscribeUserFromCourseSession() 0 30 2
A get_sessions_by_coach() 0 8 1
A get_all_sessions_by_promotion() 0 8 1
A getCoachesByCourseSessionToString() 0 19 5
A get_session_by_name() 0 17 3
A get_user_status_in_course_session() 0 21 2
F getSessionsFollowedByUser() 0 187 33
A allowManageAllSessions() 0 7 3
A get_session_category() 0 13 2
A get_session_by_course() 0 24 2
A delete_session_category() 0 38 4
F subscribeUsersToSession() 0 204 23
A get_sessions_by_user() 0 24 5
B getHotSessions() 0 58 6
A set_session_status() 0 5 1
A getCoachesByCourseSession() 0 21 3
F importCSV() 0 958 189
A update_session_extra_field_value() 0 10 1
A count_sessions() 0 14 2
F copy() 0 216 39
A protect_teacher_session_edit() 0 6 3
A get_sessions_by_general_coach() 0 33 5
A relation_session_course_exist() 0 15 2
A getAllCoursesFromAllSessions() 0 14 4
A allowToManageSessions() 0 13 4
B getSessionsCoachedByUser() 0 47 10
A addFlashSessionReadOnly() 0 5 3
B getSessionCategoryIdByName() 0 36 7
A getDefaultSessionTab() 0 10 2
A getSessionListTabs() 0 41 5
A getStatusList() 0 7 1
A compareArraysToMerge() 0 15 6
A searchSession() 0 36 4
A getUserSession() 0 19 4
A getAvoidedCoursesInSession() 0 17 3
C getShortSessionListAndExtraByCategory() 0 96 13
B convertSessionDateToString() 0 42 7
F getGridColumns() 0 393 18
B getCollapsableData() 0 33 7
C getTeacherTracking() 0 83 14
A getDescriptionFromSessionId() 0 26 3
A getSessionCourseForUser() 0 23 5
F getAllUsersFromCoursesFromAllSessionFromStatus() 0 220 30
B move() 0 62 9
A getDayLeftInSession() 0 27 3
A getSessionVisibility() 0 9 4
A getPath() 0 17 4
A getCoursesListByCourseCoach() 0 10 1
A isValidId() 0 15 3
A getTotalUserCoursesInSession() 0 38 4
A getSessionsFollowedForGroupAdmin() 0 45 5
A moveDown() 0 3 1
A sessionHasCourse() 0 25 3
B copyStudentsFromSession() 0 63 8
C getSessionListAndExtraByCategoryId() 0 127 15
A getStatusLabel() 0 9 2
A insertUsersInCourses() 0 23 3
A getCountUserTracking() 0 54 5
A parseSessionDates() 0 29 1
A getCoursesForCourseSessionCoach() 0 27 4
C importSessionDrhCSV() 0 49 12
B updateCoaches() 0 28 7
C convert_dates_to_local() 0 41 11
A getCountUsersInCourseSession() 0 26 1
A getCourseToolToBeManaged() 0 5 1
A redirectToSession() 0 18 5
A isUserSubscribedAsStudent() 0 21 3
A moveUp() 0 3 1
A getCoursesInSession() 0 23 3
F get_sessions_admin_complete() 0 257 35
A durationPerUserIsEnabled() 0 3 1
B checkSubscribeDrhToSessionList() 0 41 11
A getTotalUserTimeInPlatform() 0 27 5
D setForm() 0 291 14
A getAdminPath() 0 9 2
B copyCoachesFromSessionToCourse() 0 60 10
A getCoursesForMainSessionCoach() 0 25 5
A editUserSessionDuration() 0 16 3
A isSessionFollowedByDrh() 0 31 2
A installCourse() 0 9 3
B getNamedSessionCourseForCoach() 0 75 8
A subscribeDrhToSessionList() 0 17 4
C get_count_admin_complete() 0 99 10
A searchCourseInSessionsFromUser() 0 14 3
A unInstallCourse() 0 9 3
B getFilteredExtraFields() 0 48 8
A isUserSubscribedAsHRM() 0 22 3
A orderCourseIsEnabled() 0 8 2
A addClassesByName() 0 17 4
B getCareersFromSession() 0 27 7
A compareBySessionName() 0 12 5
A compareByCourse() 0 8 3
B allowed() 0 31 9
A compareCatSessionInfo() 0 8 3
B insertUsersInCourse() 0 75 6
A compareByUserCourseCat() 0 8 3
B getCareerDiagramPerSessionList() 0 43 8

How to fix   Complexity   

Complex Class

Complex classes like SessionManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SessionManager, and based on these observations, apply Extract Interface, too.

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