Completed
Push — master ( 27e209...a08afa )
by Julito
186:04 queued 150:53
created

UrlManager   F

Complexity

Total Complexity 139

Size/Duplication

Total Lines 1204
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1204
rs 0.6314
c 0
b 0
f 0
wmc 139

46 Methods

Rating   Name   Duplication   Size   Complexity  
A url_exist() 0 9 1
B addCourseCategoryListToUrl() 0 20 7
B update_urls_rel_course() 0 27 5
A get_url_rel_usergroup_data() 0 21 2
A update() 0 15 1
C add_courses_to_urls() 0 27 7
B searchCourseCategoryAjax() 0 38 4
A add_user_to_url() 0 15 3
A addCourseCategoryToUrl() 0 16 2
C add_sessions_to_urls() 0 27 7
A get_url_id() 0 9 1
C addUserGroupListToUrl() 0 23 7
A url_id_exist() 0 11 2
A delete_url_rel_course() 0 8 1
B delete() 0 26 1
A getUrlRelCourseCategory() 0 21 2
A add_session_to_url() 0 17 4
A delete_url_rel_session() 0 10 1
A delete_url_rel_usergroup() 0 9 1
A deleteUserFromAllUrls() 0 11 2
A relationUrlUsergroupExist() 0 11 1
A delete_url_rel_user() 0 13 3
A get_url_rel_course_data() 0 21 2
B update_urls_rel_usergroup() 0 23 6
A getCountUrlRelCourse() 0 10 1
B get_url_rel_user_data() 0 23 4
A get_access_url_from_session() 0 13 1
D update_urls_rel_user() 0 43 10
C add_users_to_urls() 0 26 7
A relation_url_course_exist() 0 11 1
A relation_url_session_exist() 0 13 1
B set_url_status() 0 13 6
A addUserGroupToUrl() 0 10 1
A get_url_data() 0 13 2
A relationUrlCourseCategoryExist() 0 11 1
A get_access_url_from_user() 0 13 1
A relation_url_user_exist() 0 9 1
A get_url_data_from_id() 0 10 1
A add_course_to_url() 0 14 3
A url_count() 0 9 1
D update_urls_rel_session() 0 29 10
A deleteUrlRelCourseCategory() 0 10 1
A get_access_url_from_course() 0 13 1
A get_url_rel_session_data() 0 21 2
D updateUrlRelCourseCategory() 0 35 9
A add() 0 13 1

How to fix   Complexity   

Complex Class

Complex classes like UrlManager 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 UrlManager, and based on these observations, apply Extract Interface, too.

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