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