| Total Complexity | 156 |
| Total Lines | 1419 |
| 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) |
||
| 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) |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $url |
||
| 117 | * |
||
| 118 | * @return int |
||
| 119 | */ |
||
| 120 | public static function url_exist($url) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 557 | } |
||
| 558 | |||
| 559 | public static function remove_users_from_urls(array $userIds, array $urlIds): void |
||
| 560 | { |
||
| 561 | if (empty($userIds) || empty($urlIds)) { |
||
| 562 | return; |
||
| 563 | } |
||
| 564 | |||
| 565 | $conn = Database::getManager()->getConnection(); |
||
| 566 | $placeholdersUsers = implode(',', array_fill(0, count($userIds), '?')); |
||
| 567 | $placeholdersUrls = implode(',', array_fill(0, count($urlIds), '?')); |
||
| 568 | |||
| 569 | $sql = "DELETE FROM access_url_rel_user |
||
| 570 | WHERE user_id IN ($placeholdersUsers) |
||
| 571 | AND access_url_id IN ($placeholdersUrls)"; |
||
| 572 | |||
| 573 | $conn->executeQuery($sql, array_merge($userIds, $urlIds)); |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Add a group of courses into a group of URLs. |
||
| 578 | * |
||
| 579 | * @author Julio Montoya |
||
| 580 | * |
||
| 581 | * @param array $course_list of course ids |
||
| 582 | * @param array $url_list of url_ids |
||
| 583 | * |
||
| 584 | * @return array |
||
| 585 | */ |
||
| 586 | public static function add_courses_to_urls($course_list, $url_list) |
||
| 587 | { |
||
| 588 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 589 | $result_array = []; |
||
| 590 | |||
| 591 | if (is_array($course_list) && is_array($url_list)) { |
||
| 592 | foreach ($url_list as $urlId) { |
||
| 593 | foreach ($course_list as $course_code) { |
||
| 594 | $courseInfo = api_get_course_info($course_code); |
||
| 595 | $courseId = $courseInfo['real_id']; |
||
| 596 | |||
| 597 | $count = self::relation_url_course_exist($courseId, $urlId); |
||
| 598 | if (0 == $count) { |
||
| 599 | $sql = "INSERT INTO $table |
||
| 600 | SET c_id = '".$courseId."', access_url_id = ".intval($urlId); |
||
| 601 | $result = Database::query($sql); |
||
| 602 | if ($result) { |
||
| 603 | $result_array[$urlId][$course_code] = 1; |
||
| 604 | } else { |
||
| 605 | $result_array[$urlId][$course_code] = 0; |
||
| 606 | } |
||
| 607 | } |
||
| 608 | } |
||
| 609 | } |
||
| 610 | } |
||
| 611 | |||
| 612 | return $result_array; |
||
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Add a group of user group into a group of URLs. |
||
| 617 | * |
||
| 618 | * @author Julio Montoya |
||
| 619 | * |
||
| 620 | * @param array $userGroupList of course ids |
||
| 621 | * @param array $urlList of url_ids |
||
| 622 | * |
||
| 623 | * @return array |
||
| 624 | */ |
||
| 625 | public static function addUserGroupListToUrl($userGroupList, $urlList) |
||
| 626 | { |
||
| 627 | $resultArray = []; |
||
| 628 | if (is_array($userGroupList) && is_array($urlList)) { |
||
| 629 | foreach ($urlList as $urlId) { |
||
| 630 | foreach ($userGroupList as $userGroupId) { |
||
| 631 | $count = self::relationUrlUsergroupExist( |
||
| 632 | $userGroupId, |
||
| 633 | $urlId |
||
| 634 | ); |
||
| 635 | if (0 == $count) { |
||
| 636 | $result = self::addUserGroupToUrl($userGroupId, $urlId); |
||
| 637 | if ($result) { |
||
| 638 | $resultArray[$urlId][$userGroupId] = 1; |
||
| 639 | } else { |
||
| 640 | $resultArray[$urlId][$userGroupId] = 0; |
||
| 641 | } |
||
| 642 | } |
||
| 643 | } |
||
| 644 | } |
||
| 645 | } |
||
| 646 | |||
| 647 | return $resultArray; |
||
| 648 | } |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Remove a list of user groups from a list of URLs. |
||
| 652 | * |
||
| 653 | * @param array $userGroupList List of user group IDs |
||
| 654 | * @param array $urlList List of access_url IDs |
||
| 655 | * |
||
| 656 | * @return void |
||
| 657 | */ |
||
| 658 | public static function removeUserGroupListFromUrl(array $userGroupList, array $urlList): void |
||
| 659 | { |
||
| 660 | if (empty($userGroupList) || empty($urlList)) { |
||
| 661 | return; |
||
| 662 | } |
||
| 663 | |||
| 664 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP); |
||
| 665 | |||
| 666 | $placeholdersGroups = implode(',', array_fill(0, count($userGroupList), '?')); |
||
| 667 | $placeholdersUrls = implode(',', array_fill(0, count($urlList), '?')); |
||
| 668 | |||
| 669 | $sql = "DELETE FROM $table |
||
| 670 | WHERE usergroup_id IN ($placeholdersGroups) |
||
| 671 | AND access_url_id IN ($placeholdersUrls)"; |
||
| 672 | |||
| 673 | $params = array_merge($userGroupList, $urlList); |
||
| 674 | Database::getManager()->getConnection()->executeQuery($sql, $params); |
||
| 675 | } |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Add a group of user group into a group of URLs. |
||
| 679 | * |
||
| 680 | * @author Julio Montoya |
||
| 681 | * |
||
| 682 | * @param array of course ids |
||
| 683 | * @param array of url_ids |
||
| 684 | * |
||
| 685 | * @return array |
||
| 686 | */ |
||
| 687 | public static function addCourseCategoryListToUrl($courseCategoryList, $urlList) |
||
| 707 | } |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Checks the relationship between an URL and a UserGr |
||
| 711 | * oup (return the num_rows). |
||
| 712 | * |
||
| 713 | * @author Julio Montoya |
||
| 714 | * |
||
| 715 | * @param int $categoryCourseId |
||
| 716 | * @param int $urlId |
||
| 717 | * |
||
| 718 | * @return bool true if success |
||
| 719 | * */ |
||
| 720 | public static function relationUrlCourseCategoryExist($categoryCourseId, $urlId) |
||
| 721 | { |
||
| 722 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY); |
||
| 723 | $sql = "SELECT course_category_id FROM $table |
||
| 724 | WHERE |
||
| 725 | access_url_id = ".intval($urlId)." AND |
||
| 726 | course_category_id = ".intval($categoryCourseId); |
||
| 727 | $result = Database::query($sql); |
||
| 728 | $num = Database::num_rows($result); |
||
| 729 | |||
| 730 | return $num; |
||
| 731 | } |
||
| 732 | |||
| 733 | /** |
||
| 734 | * @param int $userGroupId |
||
| 735 | * @param int $urlId |
||
| 736 | * |
||
| 737 | * @return int |
||
| 738 | */ |
||
| 739 | public static function addUserGroupToUrl($userGroupId, $urlId) |
||
| 740 | { |
||
| 741 | $urlRelUserGroupTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP); |
||
| 742 | $sql = "INSERT INTO $urlRelUserGroupTable |
||
| 743 | SET |
||
| 744 | usergroup_id = '".intval($userGroupId)."', |
||
| 745 | access_url_id = ".intval($urlId); |
||
| 746 | Database::query($sql); |
||
| 747 | |||
| 748 | return Database::insert_id(); |
||
| 749 | } |
||
| 750 | |||
| 751 | /** |
||
| 752 | * @param int $categoryId |
||
| 753 | * @param int $urlId |
||
| 754 | * |
||
| 755 | * @return int |
||
| 756 | */ |
||
| 757 | public static function addCourseCategoryToUrl($categoryId, $urlId) |
||
| 758 | { |
||
| 759 | $exists = self::relationUrlCourseCategoryExist($categoryId, $urlId); |
||
| 760 | if (empty($exists)) { |
||
| 761 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY); |
||
| 762 | |||
| 763 | $sql = "INSERT INTO $table |
||
| 764 | SET |
||
| 765 | course_category_id = '".intval($categoryId)."', |
||
| 766 | access_url_id = ".intval($urlId); |
||
| 767 | Database::query($sql); |
||
| 768 | |||
| 769 | return Database::insert_id(); |
||
| 770 | } |
||
| 771 | |||
| 772 | return 0; |
||
| 773 | } |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Add a group of sessions into a group of URLs. |
||
| 777 | * |
||
| 778 | * @author Julio Montoya |
||
| 779 | * |
||
| 780 | * @param array $session_list of session ids |
||
| 781 | * @param array $url_list of url_ids |
||
| 782 | * |
||
| 783 | * @return array |
||
| 784 | * */ |
||
| 785 | public static function add_sessions_to_urls($session_list, $url_list) |
||
| 786 | { |
||
| 787 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 788 | $result_array = []; |
||
| 789 | |||
| 790 | if (is_array($session_list) && is_array($url_list)) { |
||
| 791 | foreach ($url_list as $urlId) { |
||
| 792 | foreach ($session_list as $session_id) { |
||
| 793 | $count = self::relation_url_session_exist($session_id, $urlId); |
||
| 794 | |||
| 795 | if (0 == $count) { |
||
| 796 | $sql = "INSERT INTO $table |
||
| 797 | SET |
||
| 798 | session_id = ".intval($session_id).", |
||
| 799 | access_url_id = ".intval($urlId); |
||
| 800 | $result = Database::query($sql); |
||
| 801 | if ($result) { |
||
| 802 | $result_array[$urlId][$session_id] = 1; |
||
| 803 | } else { |
||
| 804 | $result_array[$urlId][$session_id] = 0; |
||
| 805 | } |
||
| 806 | } |
||
| 807 | } |
||
| 808 | } |
||
| 809 | } |
||
| 810 | |||
| 811 | return $result_array; |
||
| 812 | } |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Add a user into a url. |
||
| 816 | * |
||
| 817 | * @author Julio Montoya |
||
| 818 | * |
||
| 819 | * @param int $user_id |
||
| 820 | * @param int $urlId |
||
| 821 | * |
||
| 822 | * @return bool true if success |
||
| 823 | * */ |
||
| 824 | public static function add_user_to_url($user_id, $urlId = 1) |
||
| 825 | { |
||
| 826 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER); |
||
| 827 | if (empty($urlId)) { |
||
| 828 | $urlId = 1; |
||
| 829 | } |
||
| 830 | $count = self::relation_url_user_exist($user_id, $urlId); |
||
| 831 | $result = true; |
||
| 832 | if (empty($count)) { |
||
| 833 | $sql = "INSERT INTO $table (user_id, access_url_id) |
||
| 834 | VALUES ('".intval($user_id)."', '".intval($urlId)."') "; |
||
| 835 | $result = Database::query($sql); |
||
| 836 | } |
||
| 837 | |||
| 838 | return $result; |
||
| 839 | } |
||
| 840 | |||
| 841 | /** |
||
| 842 | * @param int $courseId |
||
| 843 | * @param int $urlId |
||
| 844 | * |
||
| 845 | * @return resource |
||
| 846 | */ |
||
| 847 | public static function add_course_to_url($courseId, $urlId = 1) |
||
| 848 | { |
||
| 849 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 850 | if (empty($urlId)) { |
||
| 851 | $urlId = 1; |
||
| 852 | } |
||
| 853 | $count = self::relation_url_course_exist($courseId, $urlId); |
||
| 854 | if (empty($count)) { |
||
| 855 | $sql = "INSERT INTO $table |
||
| 856 | SET c_id = ".intval($courseId).", access_url_id = ".intval($urlId); |
||
| 857 | Database::query($sql); |
||
| 858 | } |
||
| 859 | |||
| 860 | return true; |
||
| 861 | } |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Inserts a session to a URL (access_url_rel_session table). |
||
| 865 | * |
||
| 866 | * @param int $session_id Session ID |
||
| 867 | * @param int URL ID |
||
| 868 | * |
||
| 869 | * @return bool True on success, false session already exists or insert failed |
||
| 870 | */ |
||
| 871 | public static function add_session_to_url($session_id, $urlId = 1) |
||
| 872 | { |
||
| 873 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 874 | $session_id = (int) $session_id; |
||
| 875 | $urlId = (int) $urlId; |
||
| 876 | |||
| 877 | if (empty($urlId)) { |
||
| 878 | $urlId = 1; |
||
| 879 | } |
||
| 880 | $result = false; |
||
| 881 | $count = self::relation_url_session_exist($session_id, $urlId); |
||
| 882 | if (empty($count) && !empty($session_id)) { |
||
| 883 | $sql = "INSERT INTO $table |
||
| 884 | SET session_id = ".$session_id.", access_url_id = ".$urlId; |
||
| 885 | try { |
||
| 886 | Database::query($sql); |
||
| 887 | } catch (Exception $e) { |
||
| 888 | return false; |
||
| 889 | } |
||
| 890 | |||
| 891 | return true; |
||
| 892 | } |
||
| 893 | |||
| 894 | return $result; |
||
| 895 | } |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Deletes an url and user relationship. |
||
| 899 | * |
||
| 900 | * @author Julio Montoya |
||
| 901 | * |
||
| 902 | * @param int user id |
||
| 903 | * @param int url id |
||
| 904 | * |
||
| 905 | * @return bool true if success |
||
| 906 | * */ |
||
| 907 | public static function delete_url_rel_user($user_id, $urlId) |
||
| 908 | { |
||
| 909 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER); |
||
| 910 | $result = true; |
||
| 911 | if (!empty($user_id) && !empty($urlId)) { |
||
| 912 | $sql = "DELETE FROM $table |
||
| 913 | WHERE |
||
| 914 | user_id = ".intval($user_id)." AND |
||
| 915 | access_url_id = ".intval($urlId); |
||
| 916 | $result = Database::query($sql); |
||
| 917 | } |
||
| 918 | |||
| 919 | return $result; |
||
| 920 | } |
||
| 921 | |||
| 922 | /** |
||
| 923 | * Deletes user from all portals. |
||
| 924 | * |
||
| 925 | * @author Julio Montoya |
||
| 926 | * |
||
| 927 | * @param int user id |
||
| 928 | * |
||
| 929 | * @return bool true if success |
||
| 930 | * */ |
||
| 931 | public static function deleteUserFromAllUrls($userId) |
||
| 932 | { |
||
| 933 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER); |
||
| 934 | $result = true; |
||
| 935 | if (!empty($userId)) { |
||
| 936 | $sql = "DELETE FROM $table |
||
| 937 | WHERE user_id = ".intval($userId); |
||
| 938 | Database::query($sql); |
||
| 939 | } |
||
| 940 | |||
| 941 | return $result; |
||
| 942 | } |
||
| 943 | |||
| 944 | /** |
||
| 945 | * Deletes an url and course relationship. |
||
| 946 | * |
||
| 947 | * @author Julio Montoya |
||
| 948 | * |
||
| 949 | * @param int $courseId |
||
| 950 | * @param int $urlId |
||
| 951 | * |
||
| 952 | * @return bool true if success |
||
| 953 | * */ |
||
| 954 | public static function delete_url_rel_course($courseId, $urlId) |
||
| 955 | { |
||
| 956 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 957 | $sql = "DELETE FROM $table |
||
| 958 | WHERE c_id = '".intval($courseId)."' AND access_url_id=".intval($urlId)." "; |
||
| 959 | $result = Database::query($sql); |
||
| 960 | |||
| 961 | return $result; |
||
| 962 | } |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Deletes an url and $userGroup relationship. |
||
| 966 | * |
||
| 967 | * @author Julio Montoya |
||
| 968 | * |
||
| 969 | * @param int $userGroupId |
||
| 970 | * @param int $urlId |
||
| 971 | * |
||
| 972 | * @return bool true if success |
||
| 973 | * */ |
||
| 974 | public static function delete_url_rel_usergroup($userGroupId, $urlId) |
||
| 975 | { |
||
| 976 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP); |
||
| 977 | $sql = "DELETE FROM $table |
||
| 978 | WHERE usergroup_id = '".intval($userGroupId)."' AND |
||
| 979 | access_url_id = ".intval($urlId); |
||
| 980 | $result = Database::query($sql); |
||
| 981 | |||
| 982 | return $result; |
||
| 983 | } |
||
| 984 | |||
| 985 | /** |
||
| 986 | * Deletes an url and $userGroup relationship. |
||
| 987 | * |
||
| 988 | * @author Julio Montoya |
||
| 989 | * |
||
| 990 | * @param int $userGroupId |
||
| 991 | * @param int $urlId |
||
| 992 | * |
||
| 993 | * @return bool true if success |
||
| 994 | * */ |
||
| 995 | public static function deleteUrlRelCourseCategory($userGroupId, $urlId) |
||
| 996 | { |
||
| 997 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY); |
||
| 998 | $sql = "DELETE FROM $table |
||
| 999 | WHERE |
||
| 1000 | course_category_id = '".intval($userGroupId)."' AND |
||
| 1001 | access_url_id=".intval($urlId)." "; |
||
| 1002 | $result = Database::query($sql); |
||
| 1003 | |||
| 1004 | return $result; |
||
| 1005 | } |
||
| 1006 | |||
| 1007 | /** |
||
| 1008 | * Deletes an url and session relationship. |
||
| 1009 | * |
||
| 1010 | * @author Julio Montoya |
||
| 1011 | * |
||
| 1012 | * @param int $session_id |
||
| 1013 | * @param int $urlId |
||
| 1014 | * |
||
| 1015 | * @return bool true if success |
||
| 1016 | * */ |
||
| 1017 | public static function delete_url_rel_session($session_id, $urlId) |
||
| 1018 | { |
||
| 1019 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 1020 | $sql = "DELETE FROM $table |
||
| 1021 | WHERE |
||
| 1022 | session_id = ".intval($session_id)." AND |
||
| 1023 | access_url_id=".intval($urlId)." "; |
||
| 1024 | $result = Database::query($sql, 'ASSOC'); |
||
| 1025 | |||
| 1026 | return $result; |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Updates the access_url_rel_user table with a given user list. |
||
| 1031 | * |
||
| 1032 | * @author Julio Montoya |
||
| 1033 | * |
||
| 1034 | * @param array $user_list |
||
| 1035 | * @param int $urlId |
||
| 1036 | * |
||
| 1037 | * @return bool|array |
||
| 1038 | */ |
||
| 1039 | public static function update_urls_rel_user($user_list, $urlId) |
||
| 1040 | { |
||
| 1041 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER); |
||
| 1042 | $urlId = (int) $urlId; |
||
| 1043 | |||
| 1044 | $sql = "SELECT user_id |
||
| 1045 | FROM $table |
||
| 1046 | WHERE access_url_id = $urlId"; |
||
| 1047 | $result = Database::query($sql); |
||
| 1048 | $existing_users = []; |
||
| 1049 | |||
| 1050 | // Getting all users |
||
| 1051 | while ($row = Database::fetch_array($result)) { |
||
| 1052 | $existing_users[] = $row['user_id']; |
||
| 1053 | } |
||
| 1054 | |||
| 1055 | // Adding users |
||
| 1056 | $users_added = []; |
||
| 1057 | foreach ($user_list as $user_id_to_add) { |
||
| 1058 | if (!in_array($user_id_to_add, $existing_users)) { |
||
| 1059 | $result = self::add_user_to_url($user_id_to_add, $urlId); |
||
| 1060 | if ($result) { |
||
| 1061 | $users_added[] = $user_id_to_add; |
||
| 1062 | } |
||
| 1063 | } |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | $users_deleted = []; |
||
| 1067 | // Deleting old users |
||
| 1068 | foreach ($existing_users as $user_id_to_delete) { |
||
| 1069 | if (!in_array($user_id_to_delete, $user_list)) { |
||
| 1070 | $result = self::delete_url_rel_user($user_id_to_delete, $urlId); |
||
| 1071 | if ($result) { |
||
| 1072 | $users_deleted[] = $user_id_to_delete; |
||
| 1073 | } |
||
| 1074 | } |
||
| 1075 | } |
||
| 1076 | |||
| 1077 | if (empty($users_added) && empty($users_deleted)) { |
||
| 1078 | return false; |
||
| 1079 | } |
||
| 1080 | |||
| 1081 | return [ |
||
| 1082 | 'users_added' => $users_added, |
||
| 1083 | 'users_deleted' => $users_deleted, |
||
| 1084 | ]; |
||
| 1085 | } |
||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * Updates the access_url_rel_course table with a given user list. |
||
| 1089 | * |
||
| 1090 | * @author Julio Montoya |
||
| 1091 | * |
||
| 1092 | * @param array $course_list |
||
| 1093 | * @param int $urlId |
||
| 1094 | * */ |
||
| 1095 | public static function update_urls_rel_course($course_list, $urlId) |
||
| 1119 | } |
||
| 1120 | } |
||
| 1121 | } |
||
| 1122 | |||
| 1123 | /** |
||
| 1124 | * Updates the access_url_rel_course table with a given user list. |
||
| 1125 | * |
||
| 1126 | * @author Julio Montoya |
||
| 1127 | * |
||
| 1128 | * @param array $userGroupList user list |
||
| 1129 | * @param int $urlId |
||
| 1130 | * */ |
||
| 1131 | public static function update_urls_rel_usergroup(array $userGroupList, int $urlId): void |
||
| 1132 | { |
||
| 1133 | if ($urlId <= 0) { |
||
| 1134 | return; |
||
| 1135 | } |
||
| 1136 | |||
| 1137 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP); |
||
| 1138 | |||
| 1139 | if (!is_array($userGroupList) || empty($userGroupList)) { |
||
| 1140 | return; |
||
| 1141 | } |
||
| 1142 | |||
| 1143 | $sql = "SELECT usergroup_id FROM $table WHERE access_url_id = ".intval($urlId); |
||
| 1144 | $result = Database::query($sql); |
||
| 1145 | |||
| 1146 | $existingItems = array_map( |
||
| 1147 | fn ($row) => $row['usergroup_id'], |
||
| 1148 | Database::store_result($result) |
||
| 1149 | ); |
||
| 1150 | |||
| 1151 | foreach ($userGroupList as $userGroupId) { |
||
| 1152 | if (!in_array($userGroupId, $existingItems)) { |
||
| 1153 | self::addUserGroupToUrl($userGroupId, $urlId); |
||
| 1154 | } |
||
| 1155 | } |
||
| 1156 | |||
| 1157 | foreach ($existingItems as $userGroupId) { |
||
| 1158 | if (!in_array($userGroupId, $userGroupList)) { |
||
| 1159 | self::delete_url_rel_usergroup($userGroupId, $urlId); |
||
| 1160 | } |
||
| 1161 | } |
||
| 1162 | } |
||
| 1163 | |||
| 1164 | /** |
||
| 1165 | * Updates the access_url_rel_course_category table with a given list. |
||
| 1166 | * |
||
| 1167 | * @author Julio Montoya |
||
| 1168 | * |
||
| 1169 | * @param array $list course category list |
||
| 1170 | * @param int $urlId access_url_id |
||
| 1171 | */ |
||
| 1172 | public static function updateUrlRelCourseCategory($list, $urlId) |
||
| 1173 | { |
||
| 1174 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY); |
||
| 1175 | $sql = "SELECT course_category_id |
||
| 1176 | FROM $table |
||
| 1177 | WHERE access_url_id = ".intval($urlId); |
||
| 1178 | $result = Database::query($sql); |
||
| 1179 | $existingItems = []; |
||
| 1180 | |||
| 1181 | while ($row = Database::fetch_array($result)) { |
||
| 1182 | $existingItems[] = $row['course_category_id']; |
||
| 1183 | } |
||
| 1184 | |||
| 1185 | // Adding |
||
| 1186 | foreach ($list as $id) { |
||
| 1187 | self::addCourseCategoryToUrl($id, $urlId); |
||
| 1188 | $categoryInfo = CourseCategory::getCategoryById($id); |
||
| 1189 | $children = CourseCategory::getChildren($categoryInfo['code']); |
||
| 1190 | if (!empty($children)) { |
||
| 1191 | foreach ($children as $category) { |
||
| 1192 | self::addCourseCategoryToUrl($category['id'], $urlId); |
||
| 1193 | } |
||
| 1194 | } |
||
| 1195 | } |
||
| 1196 | |||
| 1197 | // Deleting old items |
||
| 1198 | foreach ($existingItems as $id) { |
||
| 1199 | if (!in_array($id, $list)) { |
||
| 1200 | self::deleteUrlRelCourseCategory($id, $urlId); |
||
| 1201 | $categoryInfo = CourseCategory::getCategoryById($id); |
||
| 1202 | |||
| 1203 | $children = CourseCategory::getChildren($categoryInfo['code']); |
||
| 1204 | if (!empty($children)) { |
||
| 1205 | foreach ($children as $category) { |
||
| 1206 | self::deleteUrlRelCourseCategory($category['id'], $urlId); |
||
| 1207 | } |
||
| 1208 | } |
||
| 1209 | } |
||
| 1210 | } |
||
| 1211 | } |
||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Updates the access_url_rel_session table with a given user list. |
||
| 1215 | * |
||
| 1216 | * @author Julio Montoya |
||
| 1217 | * |
||
| 1218 | * @param array $session_list |
||
| 1219 | * @param int $urlId |
||
| 1220 | * */ |
||
| 1221 | public static function update_urls_rel_session($session_list, $urlId) |
||
| 1222 | { |
||
| 1223 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 1224 | |||
| 1225 | $sql = "SELECT session_id FROM $table |
||
| 1226 | WHERE access_url_id=".intval($urlId); |
||
| 1227 | $result = Database::query($sql); |
||
| 1228 | $existing_sessions = []; |
||
| 1229 | |||
| 1230 | while ($row = Database::fetch_array($result)) { |
||
| 1231 | $existing_sessions[] = $row['session_id']; |
||
| 1232 | } |
||
| 1233 | |||
| 1234 | // Adding users |
||
| 1235 | foreach ($session_list as $session) { |
||
| 1236 | if (!in_array($session, $existing_sessions)) { |
||
| 1237 | if (!empty($session) && !empty($urlId)) { |
||
| 1238 | self::add_session_to_url($session, $urlId); |
||
| 1239 | } |
||
| 1240 | } |
||
| 1241 | } |
||
| 1242 | |||
| 1243 | // Deleting old users |
||
| 1244 | foreach ($existing_sessions as $existing_session) { |
||
| 1245 | if (!in_array($existing_session, $session_list)) { |
||
| 1246 | if (!empty($existing_session) && !empty($urlId)) { |
||
| 1247 | self::delete_url_rel_session( |
||
| 1248 | $existing_session, |
||
| 1249 | $urlId |
||
| 1250 | ); |
||
| 1251 | } |
||
| 1252 | } |
||
| 1253 | } |
||
| 1254 | } |
||
| 1255 | |||
| 1256 | /** |
||
| 1257 | * @param int $user_id |
||
| 1258 | * |
||
| 1259 | * @return array |
||
| 1260 | */ |
||
| 1261 | public static function get_access_url_from_user($user_id) |
||
| 1262 | { |
||
| 1263 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER); |
||
| 1264 | $table_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL); |
||
| 1265 | $sql = "SELECT url, access_url_id |
||
| 1266 | FROM $table url_rel_user |
||
| 1267 | INNER JOIN $table_url u |
||
| 1268 | ON (url_rel_user.access_url_id = u.id) |
||
| 1269 | WHERE user_id = ".intval($user_id); |
||
| 1270 | $result = Database::query($sql); |
||
| 1271 | |||
| 1272 | return Database::store_result($result, 'ASSOC'); |
||
| 1273 | } |
||
| 1274 | |||
| 1275 | /** |
||
| 1276 | * @param int $courseId |
||
| 1277 | * |
||
| 1278 | * @return array |
||
| 1279 | */ |
||
| 1280 | public static function get_access_url_from_course($courseId) |
||
| 1281 | { |
||
| 1282 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 1283 | $table_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL); |
||
| 1284 | $courseId = (int) $courseId; |
||
| 1285 | $sql = "SELECT url, access_url_id FROM $table c |
||
| 1286 | INNER JOIN $table_url u |
||
| 1287 | ON (c.access_url_id = u.id) |
||
| 1288 | WHERE c_id = $courseId"; |
||
| 1289 | |||
| 1290 | $result = Database::query($sql); |
||
| 1291 | |||
| 1292 | return Database::store_result($result, 'ASSOC'); |
||
| 1293 | } |
||
| 1294 | |||
| 1295 | public static function getCountAccessUrlFromCourse($courseId) |
||
| 1296 | { |
||
| 1297 | $courseId = (int) $courseId; |
||
| 1298 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 1299 | $table_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL); |
||
| 1300 | $sql = "SELECT count(u.id) count FROM $table c |
||
| 1301 | INNER JOIN $table_url u |
||
| 1302 | ON (c.access_url_id = u.id) |
||
| 1303 | WHERE c_id = $courseId "; |
||
| 1304 | |||
| 1305 | $result = Database::query($sql); |
||
| 1306 | if ($result) { |
||
| 1307 | $row = Database::fetch_assoc($result); |
||
| 1308 | |||
| 1309 | return (int) $row['count']; |
||
| 1310 | } |
||
| 1311 | |||
| 1312 | return 0; |
||
| 1313 | } |
||
| 1314 | |||
| 1315 | /** |
||
| 1316 | * @param $sessionId |
||
| 1317 | * |
||
| 1318 | * @return array |
||
| 1319 | */ |
||
| 1320 | public static function get_access_url_from_session($sessionId) |
||
| 1321 | { |
||
| 1322 | $table_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 1323 | $table_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL); |
||
| 1324 | $sql = "SELECT url, access_url_id |
||
| 1325 | FROM $table_url_rel_session url_rel_session |
||
| 1326 | INNER JOIN $table_url u |
||
| 1327 | ON (url_rel_session.access_url_id = u.id) |
||
| 1328 | WHERE session_id = ".intval($sessionId); |
||
| 1329 | $result = Database::query($sql); |
||
| 1330 | $url_list = Database::store_result($result); |
||
| 1331 | |||
| 1332 | return $url_list; |
||
| 1333 | } |
||
| 1334 | |||
| 1335 | /** |
||
| 1336 | * @param string $url |
||
| 1337 | * |
||
| 1338 | * @return bool|mixed|null |
||
| 1339 | */ |
||
| 1340 | public static function get_url_id($url) |
||
| 1341 | { |
||
| 1342 | $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL); |
||
| 1343 | $sql = "SELECT id FROM $table |
||
| 1344 | WHERE url = '".Database::escape_string($url)."'"; |
||
| 1345 | $result = Database::query($sql); |
||
| 1346 | $urlId = Database::result($result, 0, 0); |
||
| 1347 | |||
| 1348 | return $urlId; |
||
| 1349 | } |
||
| 1350 | |||
| 1351 | /** |
||
| 1352 | * @param string $needle |
||
| 1353 | * |
||
| 1354 | * @return XajaxResponse |
||
| 1355 | */ |
||
| 1356 | public static function searchCourseCategoryAjax($needle) |
||
| 1395 | } |
||
| 1396 | |||
| 1397 | public static function remove_courses_from_urls(array $courseCodes, array $urlIds): void |
||
| 1398 | { |
||
| 1399 | if (empty($courseCodes) || empty($urlIds)) { |
||
| 1400 | return; |
||
| 1401 | } |
||
| 1402 | |||
| 1403 | $conn = Database::getManager()->getConnection(); |
||
| 1404 | $placeholdersCourses = implode(',', array_fill(0, count($courseCodes), '?')); |
||
| 1405 | $placeholdersUrls = implode(',', array_fill(0, count($urlIds), '?')); |
||
| 1406 | |||
| 1407 | $sql = "DELETE FROM access_url_rel_course |
||
| 1408 | WHERE c_id IN ( |
||
| 1409 | SELECT id FROM course WHERE code IN ($placeholdersCourses) |
||
| 1410 | ) |
||
| 1414 | } |
||
| 1415 | |||
| 1416 | public static function searchCoursesByTitleOrCode(string $needle): array |
||
| 1417 | { |
||
| 1418 | $tbl = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 1419 | $charset = api_get_system_encoding(); |
||
| 1420 | $needle = api_convert_encoding($needle, $charset, 'utf-8'); |
||
| 1421 | $needle = Database::escape_string($needle); |
||
| 1422 | |||
| 1423 | $sql = " |
||
| 1432 | } |
||
| 1433 | } |
||
| 1434 |