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