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