Completed
Push — master ( ae5621...ef667c )
by Julito
13:23
created

UrlManager::update_urls_rel_session()   D

Complexity

Conditions 10
Paths 32

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 17
nc 32
nop 2
dl 0
loc 29
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Class UrlManager
6
 * This library provides functions for the access_url management.
7
 * Include/require it in your code to use its functionality.
8
 *
9
 * @package chamilo.library
10
 */
11
class UrlManager
12
{
13
    /**
14
     * Creates a new url access.
15
     *
16
     * @author Julio Montoya <[email protected]>,
17
     *
18
     * @param string $url         The URL of the site
19
     * @param string $description The description of the site
20
     * @param int    $active      is active or not
21
     *
22
     * @return bool if success
23
     */
24
    public static function add($url, $description, $active)
25
    {
26
        $tms = time();
27
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
28
        $sql = "INSERT INTO $table
29
                SET url 	= '".Database::escape_string($url)."',
30
                description = '".Database::escape_string($description)."',
31
                active 		= '".intval($active)."',
32
                created_by 	= '".api_get_user_id()."',
33
                tms = FROM_UNIXTIME(".$tms.")";
34
        $result = Database::query($sql);
35
36
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type Doctrine\DBAL\Driver\Statement which is incompatible with the documented return type boolean.
Loading history...
37
    }
38
39
    /**
40
     * Updates an URL access.
41
     *
42
     * @author Julio Montoya <[email protected]>,
43
     *
44
     * @param int    $urlId       The url id
45
     * @param string $url
46
     * @param string $description The description of the site
47
     * @param int    $active      is active or not
48
     *
49
     * @return bool if success
50
     */
51
    public static function update($urlId, $url, $description, $active)
52
    {
53
        $urlId = intval($urlId);
54
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
55
        $sql = "UPDATE $table
56
                SET url 	= '".Database::escape_string($url)."',
57
                description = '".Database::escape_string($description)."',
58
                active 		= '".intval($active)."',
59
                created_by 	= '".api_get_user_id()."',
60
                tms 		= '".api_get_utc_datetime()."'
61
                WHERE id = '$urlId'";
62
63
        $result = Database::query($sql);
64
65
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type Doctrine\DBAL\Driver\Statement which is incompatible with the documented return type boolean.
Loading history...
66
    }
67
68
    /**
69
     * Deletes an url.
70
     *
71
     * @author Julio Montoya
72
     *
73
     * @param int $id url id
74
     *
75
     * @return bool true if success
76
     * */
77
    public static function delete($id)
78
    {
79
        $id = intval($id);
80
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
81
        $tableUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
82
        $tableCourse = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
83
        $tableSession = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
84
        $tableGroup = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
85
86
        $sql = "DELETE FROM $tableCourse WHERE access_url_id = ".$id;
87
        Database::query($sql);
88
        /*
89
         * $tableCourseCategory = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
90
        $sql = "DELETE FROM $tableCourseCategory WHERE access_url_id = ".$id;
91
        Database::query($sql);
92
        */
93
        $sql = "DELETE FROM $tableSession WHERE access_url_id = ".$id;
94
        Database::query($sql);
95
        $sql = "DELETE FROM $tableGroup WHERE access_url_id = ".$id;
96
        Database::query($sql);
97
        $sql = "DELETE FROM $tableUser WHERE access_url_id = ".$id;
98
        Database::query($sql);
99
        $sql = "DELETE FROM $table WHERE id = ".$id;
100
        Database::query($sql);
101
102
        return true;
103
    }
104
105
    /**
106
     * @param string $url
107
     *
108
     * @return int
109
     */
110
    public static function url_exist($url)
111
    {
112
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
113
        $sql = "SELECT id FROM $table
114
                WHERE url = '".Database::escape_string($url)."' ";
115
        $res = Database::query($sql);
116
        $num = Database::num_rows($res);
117
118
        return $num;
119
    }
120
121
    /**
122
     * @param string $url
123
     *
124
     * @return int
125
     */
126
    public static function url_id_exist($url)
127
    {
128
        if (empty($url)) {
129
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type integer.
Loading history...
130
        }
131
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
132
        $sql = "SELECT id FROM $table WHERE id = ".intval($url)."";
133
        $res = Database::query($sql);
134
        $num = Database::num_rows($res);
135
136
        return $num;
137
    }
138
139
    /**
140
     * This function get the quantity of URLs.
141
     *
142
     * @author Julio Montoya
143
     *
144
     * @return int count of urls
145
     * */
146
    public static function url_count()
147
    {
148
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
149
        $sql = "SELECT count(id) as count_result FROM $table";
150
        $res = Database::query($sql);
151
        $url = Database::fetch_array($res, 'ASSOC');
152
        $result = $url['count_result'];
153
154
        return $result;
155
    }
156
157
    /**
158
     * Gets the id, url, description, and active status of ALL URLs.
159
     *
160
     * @author Julio Montoya
161
     *
162
     * @param string $orderBy
163
     *
164
     * @return array
165
     * */
166
    public static function get_url_data($orderBy = '')
167
    {
168
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
169
        $orderBy = empty($orderBy) ? ' id ' : Database::escape_string($orderBy);
170
171
        $sql = "SELECT id, url, description, active
172
                FROM $table
173
                ORDER BY $orderBy";
174
175
        $res = Database::query($sql);
176
        $urls = [];
177
        while ($url = Database::fetch_array($res)) {
178
            $urls[] = $url;
179
        }
180
181
        return $urls;
182
    }
183
184
    /**
185
     * Gets the id, url, description, and active status of ALL URLs.
186
     *
187
     * @author Julio Montoya
188
     *
189
     * @param int $urlId
190
     *
191
     * @return array
192
     * */
193
    public static function get_url_data_from_id($urlId)
194
    {
195
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
196
        $sql = "SELECT id, url, description, active
197
                FROM $table
198
                WHERE id = ".intval($urlId);
199
        $res = Database::query($sql);
200
        $row = Database::fetch_array($res);
201
202
        return $row;
203
    }
204
205
    /**
206
     * Gets the inner join of users and urls table.
207
     *
208
     * @author Julio Montoya
209
     *
210
     * @param int  access url id
211
     * @param string $order_by
212
     *
213
     * @return array Database::store_result of the result
214
     */
215
    public static function get_url_rel_user_data($urlId = 0, $order_by = null)
216
    {
217
        $where = '';
218
        $table_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
219
        $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
220
        if (!empty($urlId)) {
221
            $where = "WHERE $table_url_rel_user.access_url_id = ".intval($urlId);
222
        }
223
        if (empty($order_by)) {
224
            $order_clause = api_sort_by_first_name(
225
            ) ? ' ORDER BY firstname, lastname, username' : ' ORDER BY lastname, firstname, username';
226
        } else {
227
            $order_clause = $order_by;
228
        }
229
        $sql = "SELECT u.user_id, lastname, firstname, username, official_code, access_url_id
230
                FROM $tbl_user u
231
                INNER JOIN $table_url_rel_user
232
                ON $table_url_rel_user.user_id = u.user_id
233
                $where  $order_clause";
234
        $result = Database::query($sql);
235
        $users = Database::store_result($result);
236
237
        return $users;
238
    }
239
240
    /**
241
     * Gets the inner join of access_url and the course table.
242
     *
243
     * @author Julio Montoya
244
     *
245
     * @param int  access url id
246
     *
247
     * @return array Database::store_result of the result
248
     */
249
    public static function get_url_rel_course_data($urlId = 0)
250
    {
251
        $where = '';
252
        $table_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
253
        $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
254
255
        if (!empty($urlId)) {
256
            $where = " WHERE uc.access_url_id = ".intval($urlId);
257
        }
258
259
        $sql = "SELECT u.id, c_id, title, uc.access_url_id
260
                FROM $tbl_course u
261
                INNER JOIN $table_url_rel_course uc
262
                ON uc.c_id = u.id
263
                $where
264
                ORDER BY title, code";
265
266
        $result = Database::query($sql);
267
        $courses = Database::store_result($result);
268
269
        return $courses;
270
    }
271
272
    /**
273
     * Gets the number of rows with a specific course_code in access_url_rel_course table.
274
     *
275
     * @author Yoselyn Castillo
276
     *
277
     * @param int $courseId
278
     *
279
     * @return int Database::num_rows($res);
280
     */
281
    public static function getCountUrlRelCourse($courseId)
282
    {
283
        $courseId = intval($courseId);
284
        $tableUrlRelCourse = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
285
        $sql = "SELECT *
286
                FROM $tableUrlRelCourse
287
                WHERE c_id = '$courseId'";
288
        $res = Database::query($sql);
289
290
        return Database::num_rows($res);
291
    }
292
293
    /**
294
     * Gets the inner join of access_url and the session table.
295
     *
296
     * @author Julio Montoya
297
     *
298
     * @param int $urlId access url id
299
     *
300
     * @return array Database::store_result of the result
301
     */
302
    public static function get_url_rel_session_data($urlId = 0)
303
    {
304
        $where = '';
305
        $table_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
306
        $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
307
308
        if (!empty($urlId)) {
309
            $where = "WHERE $table_url_rel_session.access_url_id = ".intval($urlId);
310
        }
311
312
        $sql = "SELECT id, name, access_url_id
313
                FROM $tbl_session u
314
                INNER JOIN $table_url_rel_session
315
                ON $table_url_rel_session.session_id = id
316
                $where
317
                ORDER BY name, id";
318
319
        $result = Database::query($sql);
320
        $sessions = Database::store_result($result);
321
322
        return $sessions;
323
    }
324
325
    /**
326
     * Gets the inner join of access_url and the usergroup table.
327
     *
328
     * @author Julio Montoya
329
     *
330
     * @param int $urlId
331
     *
332
     * @return array Database::store_result of the result
333
     */
334
    public static function get_url_rel_usergroup_data($urlId = 0)
335
    {
336
        $where = '';
337
        $table_url_rel_usergroup = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
338
        $table_user_group = Database::get_main_table(TABLE_USERGROUP);
339
340
        if (!empty($urlId)) {
341
            $where = " WHERE $table_url_rel_usergroup.access_url_id = ".intval($urlId);
342
        }
343
344
        $sql = "SELECT u.id, u.name, access_url_id
345
                FROM $table_user_group u
346
                INNER JOIN $table_url_rel_usergroup
347
                ON $table_url_rel_usergroup.usergroup_id = u.id
348
                $where
349
                ORDER BY name";
350
351
        $result = Database::query($sql);
352
        $courses = Database::store_result($result);
353
354
        return $courses;
355
    }
356
357
    /**
358
     * Gets the inner join of access_url and the usergroup table.
359
     *
360
     * @author Julio Montoya
361
     *
362
     * @param int $urlId
363
     *
364
     * @return array Database::store_result of the result
365
     */
366
    public static function getUrlRelCourseCategory($urlId = 0)
367
    {
368
        $table_url_rel = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
369
        $table = Database::get_main_table(TABLE_MAIN_CATEGORY);
370
        $where = " WHERE 1=1 ";
371
        if (!empty($urlId)) {
372
            $where .= " AND $table_url_rel.access_url_id = ".intval($urlId);
373
        }
374
        $where .= " AND (parent_id IS NULL) ";
375
376
        $sql = "SELECT u.id, name, access_url_id
377
                FROM $table u
378
                INNER JOIN $table_url_rel
379
                ON $table_url_rel.course_category_id = u.id
380
                $where
381
                ORDER BY name";
382
383
        $result = Database::query($sql);
384
        $courses = Database::store_result($result, 'ASSOC');
385
386
        return $courses;
387
    }
388
389
    /**
390
     * Sets the status of an URL 1 or 0.
391
     *
392
     * @author Julio Montoya
393
     *
394
     * @param string $status lock || unlock
395
     * @param int url id
396
     * */
397
    public static function set_url_status($status, $urlId)
398
    {
399
        $url_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
400
        if ($status == 'lock') {
401
            $status_db = '0';
402
        }
403
        if ($status == 'unlock') {
404
            $status_db = '1';
405
        }
406
        if (($status_db == '1' || $status_db == '0') && is_numeric($urlId)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $status_db does not seem to be defined for all execution paths leading up to this point.
Loading history...
407
            $sql = "UPDATE $url_table SET active='".intval($status_db)."'
408
                    WHERE id='".intval($urlId)."'";
409
            Database::query($sql);
410
        }
411
    }
412
413
    /**
414
     * Checks the relationship between an URL and a User (return the num_rows).
415
     *
416
     * @author Julio Montoya
417
     *
418
     * @param int user id
419
     * @param int url id
420
     *
421
     * @return bool true if success
422
     * */
423
    public static function relation_url_user_exist($user_id, $urlId)
424
    {
425
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
426
        $sql = "SELECT user_id FROM $table
427
               WHERE access_url_id = ".intval($urlId)." AND user_id = ".intval($user_id)." ";
428
        $result = Database::query($sql);
429
        $num = Database::num_rows($result);
430
431
        return $num;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $num returns the type integer which is incompatible with the documented return type boolean.
Loading history...
432
    }
433
434
    /**
435
     * Checks the relationship between an URL and a Course (return the num_rows).
436
     *
437
     * @author Julio Montoya
438
     *
439
     * @param int $courseId
440
     * @param int $urlId
441
     *
442
     * @return bool true if success
443
     * */
444
    public static function relation_url_course_exist($courseId, $urlId)
445
    {
446
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
447
        $sql = "SELECT c_id FROM $table
448
               WHERE
449
                    access_url_id = ".intval($urlId)." AND
450
                    c_id = '".intval($courseId)."'";
451
        $result = Database::query($sql);
452
        $num = Database::num_rows($result);
453
454
        return $num;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $num returns the type integer which is incompatible with the documented return type boolean.
Loading history...
455
    }
456
457
    /**
458
     * Checks the relationship between an URL and a UserGr
459
     * oup (return the num_rows).
460
     *
461
     * @author Julio Montoya
462
     *
463
     * @param int $userGroupId
464
     * @param int $urlId
465
     *
466
     * @return bool true if success
467
     * */
468
    public static function relationUrlUsergroupExist($userGroupId, $urlId)
469
    {
470
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
471
        $sql = "SELECT usergroup_id FROM $table
472
               WHERE 
473
                    access_url_id = ".intval($urlId)." AND
474
                    usergroup_id = ".intval($userGroupId);
475
        $result = Database::query($sql);
476
        $num = Database::num_rows($result);
477
478
        return $num;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $num returns the type integer which is incompatible with the documented return type boolean.
Loading history...
479
    }
480
481
    /**
482
     * Checks the relationship between an URL and a Session (return the num_rows).
483
     *
484
     * @author Julio Montoya
485
     *
486
     * @param int user id
487
     * @param int url id
488
     *
489
     * @return bool true if success
490
     * */
491
    public static function relation_url_session_exist($session_id, $urlId)
492
    {
493
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
494
        $session_id = intval($session_id);
495
        $urlId = intval($urlId);
496
        $sql = "SELECT session_id FROM $table
497
                WHERE
498
                    access_url_id = ".intval($urlId)." AND
499
                    session_id = ".Database::escape_string($session_id);
500
        $result = Database::query($sql);
501
        $num = Database::num_rows($result);
502
503
        return $num;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $num returns the type integer which is incompatible with the documented return type boolean.
Loading history...
504
    }
505
506
    /**
507
     * Add a group of users into a group of URLs.
508
     *
509
     * @author Julio Montoya
510
     *
511
     * @param  array of user_ids
512
     * @param  array of url_ids
513
     *
514
     * @return array
515
     * */
516
    public static function add_users_to_urls($user_list, $url_list)
517
    {
518
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
519
        $result_array = [];
520
521
        if (is_array($user_list) && is_array($url_list)) {
522
            foreach ($url_list as $urlId) {
523
                foreach ($user_list as $user_id) {
524
                    $count = self::relation_url_user_exist($user_id, $urlId);
525
                    if ($count == 0) {
526
                        $sql = "INSERT INTO $table
527
                                SET 
528
                                    user_id = ".intval($user_id).", 
529
                                    access_url_id = ".intval($urlId);
530
                        $result = Database::query($sql);
531
                        if ($result) {
532
                            $result_array[$urlId][$user_id] = 1;
533
                        } else {
534
                            $result_array[$urlId][$user_id] = 0;
535
                        }
536
                    }
537
                }
538
            }
539
        }
540
541
        return $result_array;
542
    }
543
544
    /**
545
     * Add a group of courses into a group of URLs.
546
     *
547
     * @author Julio Montoya
548
     *
549
     * @param array $course_list of course ids
550
     * @param array $url_list    of url_ids
551
     *
552
     * @return array
553
     */
554
    public static function add_courses_to_urls($course_list, $url_list)
555
    {
556
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
557
        $result_array = [];
558
559
        if (is_array($course_list) && is_array($url_list)) {
560
            foreach ($url_list as $urlId) {
561
                foreach ($course_list as $course_code) {
562
                    $courseInfo = api_get_course_info($course_code);
563
                    $courseId = $courseInfo['real_id'];
564
565
                    $count = self::relation_url_course_exist($courseId, $urlId);
566
                    if ($count == 0) {
567
                        $sql = "INSERT INTO $table
568
                                SET c_id = '".$courseId."', access_url_id = ".intval($urlId);
569
                        $result = Database::query($sql);
570
                        if ($result) {
571
                            $result_array[$urlId][$course_code] = 1;
572
                        } else {
573
                            $result_array[$urlId][$course_code] = 0;
574
                        }
575
                    }
576
                }
577
            }
578
        }
579
580
        return $result_array;
581
    }
582
583
    /**
584
     * Add a group of user group into a group of URLs.
585
     *
586
     * @author Julio Montoya
587
     *
588
     * @param array $userGroupList of course ids
589
     * @param array $urlList       of url_ids
590
     *
591
     * @return array
592
     */
593
    public static function addUserGroupListToUrl($userGroupList, $urlList)
594
    {
595
        $resultArray = [];
596
        if (is_array($userGroupList) && is_array($urlList)) {
597
            foreach ($urlList as $urlId) {
598
                foreach ($userGroupList as $userGroupId) {
599
                    $count = self::relationUrlUsergroupExist(
600
                        $userGroupId,
601
                        $urlId
602
                    );
603
                    if ($count == 0) {
604
                        $result = self::addUserGroupToUrl($userGroupId, $urlId);
605
                        if ($result) {
606
                            $resultArray[$urlId][$userGroupId] = 1;
607
                        } else {
608
                            $resultArray[$urlId][$userGroupId] = 0;
609
                        }
610
                    }
611
                }
612
            }
613
        }
614
615
        return $resultArray;
616
    }
617
618
    /**
619
     * Add a group of user group into a group of URLs.
620
     *
621
     * @author Julio Montoya
622
     *
623
     * @param  array of course ids
624
     * @param  array of url_ids
625
     *
626
     * @return array
627
     */
628
    public static function addCourseCategoryListToUrl($courseCategoryList, $urlList)
629
    {
630
        $resultArray = [];
631
        if (is_array($courseCategoryList) && is_array($urlList)) {
632
            foreach ($urlList as $urlId) {
633
                foreach ($courseCategoryList as $categoryCourseId) {
634
                    $count = self::relationUrlCourseCategoryExist($categoryCourseId, $urlId);
635
                    if ($count == 0) {
636
                        $result = self::addCourseCategoryToUrl($categoryCourseId, $urlId);
637
                        if ($result) {
638
                            $resultArray[$urlId][$categoryCourseId] = 1;
639
                        } else {
640
                            $resultArray[$urlId][$categoryCourseId] = 0;
641
                        }
642
                    }
643
                }
644
            }
645
        }
646
647
        return $resultArray;
648
    }
649
650
    /**
651
     * Checks the relationship between an URL and a UserGr
652
     * oup (return the num_rows).
653
     *
654
     * @author Julio Montoya
655
     *
656
     * @param int $categoryCourseId
657
     * @param int $urlId
658
     *
659
     * @return bool true if success
660
     * */
661
    public static function relationUrlCourseCategoryExist($categoryCourseId, $urlId)
662
    {
663
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
664
        $sql = "SELECT course_category_id FROM $table
665
                WHERE 
666
                    access_url_id = ".intval($urlId)." AND
667
                    course_category_id = ".intval($categoryCourseId);
668
        $result = Database::query($sql);
669
        $num = Database::num_rows($result);
670
671
        return $num;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $num returns the type integer which is incompatible with the documented return type boolean.
Loading history...
672
    }
673
674
    /**
675
     * @param int $userGroupId
676
     * @param int $urlId
677
     *
678
     * @return int
679
     */
680
    public static function addUserGroupToUrl($userGroupId, $urlId)
681
    {
682
        $urlRelUserGroupTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
683
        $sql = "INSERT INTO $urlRelUserGroupTable
684
                SET
685
                usergroup_id = '".intval($userGroupId)."',
686
                access_url_id = ".intval($urlId);
687
        Database::query($sql);
688
689
        return Database::insert_id();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Database::insert_id() returns the type string which is incompatible with the documented return type integer.
Loading history...
690
    }
691
692
    /**
693
     * @param int $categoryId
694
     * @param int $urlId
695
     *
696
     * @return int
697
     */
698
    public static function addCourseCategoryToUrl($categoryId, $urlId)
699
    {
700
        $exists = self::relationUrlCourseCategoryExist($categoryId, $urlId);
701
        if (empty($exists)) {
702
            $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
703
704
            $sql = "INSERT INTO $table
705
                    SET
706
                    course_category_id = '".intval($categoryId)."',
707
                    access_url_id = ".intval($urlId);
708
            Database::query($sql);
709
710
            return Database::insert_id();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Database::insert_id() returns the type string which is incompatible with the documented return type integer.
Loading history...
711
        }
712
713
        return 0;
714
    }
715
716
    /**
717
     * Add a group of sessions into a group of URLs.
718
     *
719
     * @author Julio Montoya
720
     *
721
     * @param array $session_list of session ids
722
     * @param array $url_list     of url_ids
723
     *
724
     * @return array
725
     * */
726
    public static function add_sessions_to_urls($session_list, $url_list)
727
    {
728
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
729
        $result_array = [];
730
731
        if (is_array($session_list) && is_array($url_list)) {
732
            foreach ($url_list as $urlId) {
733
                foreach ($session_list as $session_id) {
734
                    $count = self::relation_url_session_exist($session_id, $urlId);
735
736
                    if ($count == 0) {
737
                        $sql = "INSERT INTO $table
738
                                SET
739
                                session_id = ".intval($session_id).",
740
                                access_url_id = ".intval($urlId);
741
                        $result = Database::query($sql);
742
                        if ($result) {
743
                            $result_array[$urlId][$session_id] = 1;
744
                        } else {
745
                            $result_array[$urlId][$session_id] = 0;
746
                        }
747
                    }
748
                }
749
            }
750
        }
751
752
        return $result_array;
753
    }
754
755
    /**
756
     * Add a user into a url.
757
     *
758
     * @author Julio Montoya
759
     *
760
     * @param int $user_id
761
     * @param int $urlId
762
     *
763
     * @return bool true if success
764
     * */
765
    public static function add_user_to_url($user_id, $urlId = 1)
766
    {
767
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
768
        if (empty($urlId)) {
769
            $urlId = 1;
770
        }
771
        $count = self::relation_url_user_exist($user_id, $urlId);
772
        $result = true;
773
        if (empty($count)) {
774
            $sql = "INSERT INTO $table (user_id, access_url_id)
775
                    VALUES ('".intval($user_id)."', '".intval($urlId)."') ";
776
            $result = Database::query($sql);
777
        }
778
779
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result also could return the type Doctrine\DBAL\Driver\Statement which is incompatible with the documented return type boolean.
Loading history...
780
    }
781
782
    /**
783
     * @param int $courseId
784
     * @param int $urlId
785
     *
786
     * @return resource
787
     */
788
    public static function add_course_to_url($courseId, $urlId = 1)
789
    {
790
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
791
        if (empty($urlId)) {
792
            $urlId = 1;
793
        }
794
        $count = self::relation_url_course_exist($courseId, $urlId);
795
        if (empty($count)) {
796
            $sql = "INSERT INTO $table
797
                    SET c_id = '".intval($courseId)."', access_url_id = ".intval($urlId);
798
            Database::query($sql);
799
        }
800
801
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type resource.
Loading history...
802
    }
803
804
    /**
805
     * Inserts a session to a URL (access_url_rel_session table).
806
     *
807
     * @param int $session_id Session ID
808
     * @param   int     URL ID
809
     *
810
     * @return bool True on success, false session already exists or insert failed
811
     */
812
    public static function add_session_to_url($session_id, $urlId = 1)
813
    {
814
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
815
        if (empty($urlId)) {
816
            $urlId = 1;
817
        }
818
        $result = false;
819
        $count = self::relation_url_session_exist($session_id, $urlId);
820
        $session_id = intval($session_id);
821
        if (empty($count) && !empty($session_id)) {
822
            $urlId = intval($urlId);
823
            $sql = "INSERT INTO $table
824
                    SET session_id = ".intval($session_id).", access_url_id = ".$urlId;
825
            try {
826
                Database::query($sql);
827
            } catch (Exception $e) {
828
                return false;
829
            }
830
831
            return true;
832
        }
833
834
        return $result;
835
    }
836
837
    /**
838
     * Deletes an url and user relationship.
839
     *
840
     * @author Julio Montoya
841
     *
842
     * @param int user id
843
     * @param int url id
844
     *
845
     * @return bool true if success
846
     * */
847
    public static function delete_url_rel_user($user_id, $urlId)
848
    {
849
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
850
        $result = true;
851
        if (!empty($user_id) && !empty($urlId)) {
852
            $sql = "DELETE FROM $table
853
                   WHERE 
854
                        user_id = ".intval($user_id)." AND 
855
                        access_url_id = ".intval($urlId);
856
            $result = Database::query($sql);
857
        }
858
859
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result also could return the type Doctrine\DBAL\Driver\Statement which is incompatible with the documented return type boolean.
Loading history...
860
    }
861
862
    /**
863
     * Deletes user from all portals.
864
     *
865
     * @author Julio Montoya
866
     *
867
     * @param int user id
868
     *
869
     * @return bool true if success
870
     * */
871
    public static function deleteUserFromAllUrls($userId)
872
    {
873
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
874
        $result = true;
875
        if (!empty($userId)) {
876
            $sql = "DELETE FROM $table
877
                   WHERE user_id = ".intval($userId);
878
            Database::query($sql);
879
        }
880
881
        return $result;
882
    }
883
884
    /**
885
     * Deletes an url and course relationship.
886
     *
887
     * @author Julio Montoya
888
     *
889
     * @param int $courseId
890
     * @param int $urlId
891
     *
892
     * @return bool true if success
893
     * */
894
    public static function delete_url_rel_course($courseId, $urlId)
895
    {
896
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
897
        $sql = "DELETE FROM $table
898
               WHERE c_id = '".intval($courseId)."' AND access_url_id=".intval($urlId)."  ";
899
        $result = Database::query($sql);
900
901
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type Doctrine\DBAL\Driver\Statement which is incompatible with the documented return type boolean.
Loading history...
902
    }
903
904
    /**
905
     * Deletes an url and $userGroup relationship.
906
     *
907
     * @author Julio Montoya
908
     *
909
     * @param int $userGroupId
910
     * @param int $urlId
911
     *
912
     * @return bool true if success
913
     * */
914
    public static function delete_url_rel_usergroup($userGroupId, $urlId)
915
    {
916
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
917
        $sql = "DELETE FROM $table
918
                WHERE usergroup_id = '".intval($userGroupId)."' AND
919
                     access_url_id = ".intval($urlId);
920
        $result = Database::query($sql);
921
922
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type Doctrine\DBAL\Driver\Statement which is incompatible with the documented return type boolean.
Loading history...
923
    }
924
925
    /**
926
     * Deletes an url and $userGroup relationship.
927
     *
928
     * @author Julio Montoya
929
     *
930
     * @param int $userGroupId
931
     * @param int $urlId
932
     *
933
     * @return bool true if success
934
     * */
935
    public static function deleteUrlRelCourseCategory($userGroupId, $urlId)
936
    {
937
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
938
        $sql = "DELETE FROM $table
939
                WHERE 
940
                    course_category_id = '".intval($userGroupId)."' AND
941
                    access_url_id=".intval($urlId)."  ";
942
        $result = Database::query($sql);
943
944
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type Doctrine\DBAL\Driver\Statement which is incompatible with the documented return type boolean.
Loading history...
945
    }
946
947
    /**
948
     * Deletes an url and session relationship.
949
     *
950
     * @author Julio Montoya
951
     *
952
     * @param int $session_id
953
     * @param int $urlId
954
     *
955
     * @return bool true if success
956
     * */
957
    public static function delete_url_rel_session($session_id, $urlId)
958
    {
959
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
960
        $sql = "DELETE FROM $table
961
                WHERE 
962
                    session_id = ".intval($session_id)." AND 
963
                    access_url_id=".intval($urlId)."  ";
964
        $result = Database::query($sql, 'ASSOC');
965
966
        return $result;
967
    }
968
969
    /**
970
     * Updates the access_url_rel_user table  with a given user list.
971
     *
972
     * @author Julio Montoya
973
     *
974
     * @param array $user_list
975
     * @param int   $urlId
976
     *
977
     * @return bool|array
978
     */
979
    public static function update_urls_rel_user($user_list, $urlId)
980
    {
981
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
982
        $sql = "SELECT user_id 
983
                FROM $table 
984
                WHERE access_url_id = ".intval($urlId);
985
        $result = Database::query($sql);
986
        $existing_users = [];
987
988
        // Getting all users
989
        while ($row = Database::fetch_array($result)) {
990
            $existing_users[] = $row['user_id'];
991
        }
992
993
        // Adding users
994
        $users_added = [];
995
        foreach ($user_list as $user_id_to_add) {
996
            if (!in_array($user_id_to_add, $existing_users)) {
997
                $result = self::add_user_to_url($user_id_to_add, $urlId);
998
                if ($result) {
999
                    $users_added[] = $user_id_to_add;
1000
                }
1001
            }
1002
        }
1003
1004
        $users_deleted = [];
1005
        // Deleting old users
1006
        foreach ($existing_users as $user_id_to_delete) {
1007
            if (!in_array($user_id_to_delete, $user_list)) {
1008
                $result = self::delete_url_rel_user($user_id_to_delete, $urlId);
1009
                if ($result) {
1010
                    $users_deleted[] = $user_id_to_delete;
1011
                }
1012
            }
1013
        }
1014
1015
        if (empty($users_added) && empty($users_deleted)) {
1016
            return false;
1017
        }
1018
1019
        return [
1020
            'users_added' => $users_added,
1021
            'users_deleted' => $users_deleted,
1022
        ];
1023
    }
1024
1025
    /**
1026
     * Updates the access_url_rel_course table  with a given user list.
1027
     *
1028
     * @author Julio Montoya
1029
     *
1030
     * @param array $course_list
1031
     * @param int   $urlId
1032
     * */
1033
    public static function update_urls_rel_course($course_list, $urlId)
1034
    {
1035
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
1036
1037
        $sql = "SELECT c_id FROM $table
1038
                WHERE access_url_id = ".intval($urlId);
1039
        $result = Database::query($sql);
1040
1041
        $existing_courses = [];
1042
        while ($row = Database::fetch_array($result)) {
1043
            $existing_courses[] = $row['c_id'];
1044
        }
1045
1046
        // Adding courses
1047
        foreach ($course_list as $courseId) {
1048
            self::add_course_to_url($courseId, $urlId);
1049
            CourseManager::update_course_ranking($courseId, 0, $urlId);
1050
        }
1051
1052
        // Deleting old courses
1053
        foreach ($existing_courses as $courseId) {
1054
            if (!in_array($courseId, $course_list)) {
1055
                self::delete_url_rel_course($courseId, $urlId);
1056
                CourseManager::update_course_ranking(
1057
                    $courseId,
1058
                    0,
1059
                    $urlId
1060
                );
1061
            }
1062
        }
1063
    }
1064
1065
    /**
1066
     * Updates the access_url_rel_course table  with a given user list.
1067
     *
1068
     * @author Julio Montoya
1069
     *
1070
     * @param array $userGroupList user list
1071
     * @param int   $urlId
1072
     * */
1073
    public static function update_urls_rel_usergroup($userGroupList, $urlId)
1074
    {
1075
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
1076
1077
        $sql = "SELECT usergroup_id FROM $table 
1078
                WHERE access_url_id = ".intval($urlId);
1079
        $result = Database::query($sql);
1080
        $existingItems = [];
1081
        while ($row = Database::fetch_array($result)) {
1082
            $existingItems[] = $row['usergroup_id'];
1083
        }
1084
1085
        // Adding
1086
        foreach ($userGroupList as $userGroupId) {
1087
            if (!in_array($userGroupId, $existingItems)) {
1088
                self::addUserGroupToUrl($userGroupId, $urlId);
1089
            }
1090
        }
1091
1092
        // Deleting old items
1093
        foreach ($existingItems as $userGroupId) {
1094
            if (!in_array($userGroupId, $userGroupList)) {
1095
                self::delete_url_rel_usergroup($userGroupId, $urlId);
1096
            }
1097
        }
1098
    }
1099
1100
    /**
1101
     * Updates the access_url_rel_course_category table with a given list.
1102
     *
1103
     * @author Julio Montoya
1104
     *
1105
     * @param array $list  course category list
1106
     * @param int   $urlId access_url_id
1107
     */
1108
    public static function updateUrlRelCourseCategory($list, $urlId)
1109
    {
1110
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
1111
        $sql = "SELECT course_category_id 
1112
                FROM $table 
1113
                WHERE access_url_id = ".intval($urlId);
1114
        $result = Database::query($sql);
1115
        $existingItems = [];
1116
1117
        while ($row = Database::fetch_array($result)) {
1118
            $existingItems[] = $row['course_category_id'];
1119
        }
1120
1121
        // Adding
1122
        foreach ($list as $id) {
1123
            self::addCourseCategoryToUrl($id, $urlId);
1124
            $categoryInfo = CourseCategory::getCategoryById($id);
1125
            $children = CourseCategory::getChildren($categoryInfo['code']);
1126
            if (!empty($children)) {
1127
                foreach ($children as $category) {
1128
                    self::addCourseCategoryToUrl($category['id'], $urlId);
1129
                }
1130
            }
1131
        }
1132
1133
        // Deleting old items
1134
        foreach ($existingItems as $id) {
1135
            if (!in_array($id, $list)) {
1136
                self::deleteUrlRelCourseCategory($id, $urlId);
1137
                $categoryInfo = CourseCategory::getCategoryById($id);
1138
1139
                $children = CourseCategory::getChildren($categoryInfo['code']);
1140
                if (!empty($children)) {
1141
                    foreach ($children as $category) {
1142
                        self::deleteUrlRelCourseCategory($category['id'], $urlId);
1143
                    }
1144
                }
1145
            }
1146
        }
1147
    }
1148
1149
    /**
1150
     * Updates the access_url_rel_session table with a given user list.
1151
     *
1152
     * @author Julio Montoya
1153
     *
1154
     * @param array $session_list
1155
     * @param int   $urlId
1156
     * */
1157
    public static function update_urls_rel_session($session_list, $urlId)
1158
    {
1159
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
1160
1161
        $sql = "SELECT session_id FROM $table 
1162
                WHERE access_url_id=".intval($urlId);
1163
        $result = Database::query($sql);
1164
        $existing_sessions = [];
1165
1166
        while ($row = Database::fetch_array($result)) {
1167
            $existing_sessions[] = $row['session_id'];
1168
        }
1169
1170
        // Adding users
1171
        foreach ($session_list as $session) {
1172
            if (!in_array($session, $existing_sessions)) {
1173
                if (!empty($session) && !empty($urlId)) {
1174
                    self::add_session_to_url($session, $urlId);
1175
                }
1176
            }
1177
        }
1178
1179
        // Deleting old users
1180
        foreach ($existing_sessions as $existing_session) {
1181
            if (!in_array($existing_session, $session_list)) {
1182
                if (!empty($existing_session) && !empty($urlId)) {
1183
                    self::delete_url_rel_session(
1184
                        $existing_session,
1185
                        $urlId
1186
                    );
1187
                }
1188
            }
1189
        }
1190
    }
1191
1192
    /**
1193
     * @param int $user_id
1194
     *
1195
     * @return array
1196
     */
1197
    public static function get_access_url_from_user($user_id)
1198
    {
1199
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
1200
        $table_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
1201
        $sql = "SELECT url, access_url_id 
1202
                FROM $table url_rel_user 
1203
                INNER JOIN $table_url u
1204
                ON (url_rel_user.access_url_id = u.id)
1205
                WHERE user_id = ".intval($user_id);
1206
        $result = Database::query($sql);
1207
        $url_list = Database::store_result($result, 'ASSOC');
1208
1209
        return $url_list;
1210
    }
1211
1212
    /**
1213
     * @param int $courseId
1214
     *
1215
     * @return array
1216
     */
1217
    public static function get_access_url_from_course($courseId)
1218
    {
1219
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
1220
        $table_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
1221
        $sql = "SELECT url, access_url_id FROM $table c 
1222
                INNER JOIN $table_url u
1223
                ON (c.access_url_id = u.id)
1224
                WHERE c_id = ".intval($courseId);
1225
1226
        $result = Database::query($sql);
1227
        $url_list = Database::store_result($result, 'ASSOC');
1228
1229
        return $url_list;
1230
    }
1231
1232
    /**
1233
     * @param $sessionId
1234
     *
1235
     * @return array
1236
     */
1237
    public static function get_access_url_from_session($sessionId)
1238
    {
1239
        $table_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
1240
        $table_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
1241
        $sql = "SELECT url, access_url_id 
1242
                FROM $table_url_rel_session url_rel_session 
1243
                INNER JOIN $table_url u
1244
                ON (url_rel_session.access_url_id = u.id)
1245
                WHERE session_id = ".intval($sessionId);
1246
        $result = Database::query($sql);
1247
        $url_list = Database::store_result($result);
1248
1249
        return $url_list;
1250
    }
1251
1252
    /**
1253
     * @param string $url
1254
     *
1255
     * @return bool|mixed|null
1256
     */
1257
    public static function get_url_id($url)
1258
    {
1259
        $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
1260
        $sql = "SELECT id FROM $table 
1261
                WHERE url = '".Database::escape_string($url)."'";
1262
        $result = Database::query($sql);
1263
        $urlId = Database::result($result, 0, 0);
1264
1265
        return $urlId;
1266
    }
1267
1268
    /**
1269
     * @param string $needle
1270
     *
1271
     * @return XajaxResponse
1272
     */
1273
    public static function searchCourseCategoryAjax($needle)
1274
    {
1275
        $response = new xajaxResponse();
1276
        $return = '';
1277
1278
        if (!empty($needle)) {
1279
            // xajax send utf8 datas... datas in db can be non-utf8 datas
1280
            $charset = api_get_system_encoding();
1281
            $needle = api_convert_encoding($needle, $charset, 'utf-8');
1282
            $needle = Database::escape_string($needle);
1283
            // search courses where username or firstname or lastname begins likes $needle
1284
            $sql = 'SELECT id, name 
1285
                    FROM '.Database::get_main_table(TABLE_MAIN_CATEGORY).' u
1286
                    WHERE 
1287
                        name LIKE "'.$needle.'%" AND 
1288
                        (parent_id IS NULL or parent_id = 0)
1289
                    ORDER BY name
1290
                    LIMIT 11';
1291
            $result = Database::query($sql);
1292
            $i = 0;
1293
            while ($data = Database::fetch_array($result)) {
1294
                $i++;
1295
                if ($i <= 10) {
1296
                    $return .= '<a
1297
                    href="javascript: void(0);"
1298
                    onclick="javascript: add_user_to_url(\''.addslashes($data['id']).'\',\''.addslashes($data['name']).' \')">'.$data['name'].' </a><br />';
1299
                } else {
1300
                    $return .= '...<br />';
1301
                }
1302
            }
1303
        }
1304
        $response->addAssign(
1305
            'ajax_list_courses',
1306
            'innerHTML',
1307
            api_utf8_encode($return)
1308
        );
1309
1310
        return $response;
1311
    }
1312
}
1313