Dokeos PHP Cross Reference Learning Management Systems

Source: /main/inc/lib/urlmanager.lib.php - 824 lines - 33976 bytes - Summary - Text - Print

   1  <?php
   2  
   3  /*
   4    ==============================================================================
   5    Dokeos - elearning and course management software
   6  
   7    Copyright (c) 2009 Dokeos SPRL
   8    Copyright (c) 2009 Julio Montoya Armas <gugli100@gmail.com>
   9  
  10    For a full list of contributors, see "credits.txt".
  11    The full license can be read in "license.txt".
  12  
  13    This program is free software; you can redistribute it and/or
  14    modify it under the terms of the GNU General Public License
  15    as published by the Free Software Foundation; either version 2
  16    of the License, or (at your option) any later version.
  17  
  18    See the GNU General Public License for more details.
  19  
  20    Contact: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium, info@dokeos.com
  21    ==============================================================================
  22   */
  23  
  24  /**
  25    ==============================================================================
  26   *     This library provides functions for the access_url management.
  27   *     Include/require it in your code to use its functionality.
  28   *
  29   *     @package dokeos.library
  30    ==============================================================================
  31   */
  32  class UrlManager {
  33  
  34      /**
  35       * Creates a new url access to Dokeos
  36       *
  37       * @author Julio Montoya <gugli100@gmail.com>,
  38       *
  39       * @param    string    The URL of the site
  40       * @param    string  The description of the site
  41       * @param    int        is active or not
  42       * @param  int     the user_id of the owner
  43       * @return boolean if success
  44       */
  45      function add($url, $description, $active) {
  46          $tms = time();
  47          $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  48          $sql = "INSERT INTO $table_access_url
  49                  SET url     = '" . Database::escape_string($url) . "',
  50                  description = '" . Database::escape_string($description) . "',
  51                  active         = '" . Database::escape_string($active) . "',
  52                  created_by     = '" . Database::escape_string(api_get_user_id()) . "',
  53                  tms = FROM_UNIXTIME(" . $tms . ")";
  54          $result = Database::query($sql, __FILE__, __LINE__);
  55          return $result;
  56      }
  57  
  58      /**
  59       * Updates an URL access to Dokeos
  60       * @author Julio Montoya <gugli100@gmail.com>,
  61       *
  62       * @param    int     The url id
  63       * @param    string  The description of the site
  64       * @param    int        is active or not
  65       * @param    int     the user_id of the owner
  66       * @return     boolean if success
  67       */
  68      function udpate($url_id, $url, $description, $active) {
  69          $url_id = intval($url_id);
  70          $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  71          $tms = time();
  72          $sql = "UPDATE $table_access_url
  73                  SET url     = '" . Database::escape_string($url) . "',
  74                  description = '" . Database::escape_string($description) . "',
  75                  active         = '" . Database::escape_string($active) . "',
  76                  created_by     = '" . Database::escape_string(api_get_user_id()) . "',
  77                  tms         = FROM_UNIXTIME(" . $tms . ")
  78                  WHERE id = '$url_id'";
  79          $result = Database::query($sql, __FILE__, __LINE__);
  80          return $result;
  81      }
  82  
  83      /**
  84       * Deletes an url
  85       * @author Julio Montoya
  86       * @param int url id
  87       * @return boolean true if success
  88       * */
  89      function delete($id) {
  90          $id = intval($id);
  91          $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  92          $sql = "DELETE FROM $table_access_url WHERE id = " . Database::escape_string($id);
  93          $result = Database::query($sql, __FILE__, __LINE__);
  94          return $result;
  95      }
  96  
  97      /**
  98       *
  99       * */
 100      function url_exist($url) {
 101          $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
 102          $sql = "SELECT id FROM $table_access_url WHERE url = '" . Database::escape_string($url) . "' ";
 103          $res = Database::query($sql, __FILE__, __LINE__);
 104          $num = Database::num_rows($res);
 105          return $num;
 106      }
 107  
 108      /**
 109       *
 110       * */
 111      function url_id_exist($url) {
 112          $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
 113          $sql = "SELECT id FROM $table_access_url WHERE id = '" . Database::escape_string($url) . "' ";
 114          $res = Database::query($sql, __FILE__, __LINE__);
 115          $num = Database::num_rows($res);
 116          return $num;
 117      }
 118  
 119      /**
 120       * This function get the quantity of URLs
 121       * @author Julio Montoya
 122       * @return int count of urls
 123       * */
 124      function url_count() {
 125          $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
 126          $sql = "SELECT count(id) as count_result FROM $table_access_url";
 127          $res = Database::query($sql, __FILE__, __LINE__);
 128          $url = Database::fetch_array($res, 'ASSOC');
 129          $result = $url['count_result'];
 130          return $result;
 131      }
 132  
 133      /**
 134       * Gets the id, url, description, and active status of ALL URLs
 135       * @author Julio Montoya
 136       * @return array
 137       * */
 138      function get_url_data() {
 139          global $_configuration;
 140          $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
 141          $sql = "SELECT id, url, description, active,'0' AS main_url  FROM $table_access_url";
 142          $res = Database::query($sql, __FILE__, __LINE__);
 143          $urls = array();
 144          $main_url = $_configuration['root_web'];
 145          while ($url = Database::fetch_array($res)) {
 146              if ($url['url'] == $main_url) {
 147                  $url['main_url'] = '1';
 148              }
 149              $urls[] = $url;
 150          }
 151          return $urls;
 152      }
 153  
 154      /**
 155       * Gets the id, url, description, and active status of ALL URLs
 156       * @author Julio Montoya
 157       * @return array
 158       * */
 159      function get_url_data_from_id($url_id) {
 160          $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
 161          $sql = "SELECT id, url, description, active FROM $table_access_url WHERE id = " . Database::escape_string($url_id);
 162          $res = Database::query($sql, __FILE__, __LINE__);
 163          $row = Database::fetch_array($res);
 164          return $row;
 165      }
 166  
 167      /** Gets the inner join of users and urls table
 168       * @author Julio Montoya
 169       * @return int  access url id
 170       * @return array   Database::store_result of the result
 171       * */
 172      function get_url_rel_user_data($access_url_id='') {
 173          $where = '';
 174          $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
 175          $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
 176          if (!empty($access_url_id)) {
 177              $where = "WHERE $table_url_rel_user.access_url_id = " . Database::escape_string($access_url_id);
 178          }
 179          $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname, username' : ' ORDER BY lastname, firstname, username';
 180          $sql = "SELECT u.user_id, lastname, firstname, username, access_url_id
 181              FROM $tbl_user u
 182              INNER JOIN $table_url_rel_user
 183              ON $table_url_rel_user.user_id = u.user_id
 184              $where" . $order_clause;
 185          $result = Database::query($sql, __FILE__, __LINE__);
 186          $users = Database::store_result($result);
 187          return $users;
 188      }
 189  
 190      /** Gets the inner join of access_url and the course table
 191       * @author Julio Montoya
 192       * @return int  access url id
 193       * @return array   Database::store_result of the result
 194       * */
 195      function get_url_rel_course_data($access_url_id='') {
 196          $where = '';
 197          $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
 198          $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
 199  
 200          if (!empty($access_url_id))
 201              $where = "WHERE $table_url_rel_course.access_url_id = " . Database::escape_string($access_url_id);
 202  
 203          $sql = "SELECT course_code, title, access_url_id
 204                  FROM $tbl_course u
 205                  INNER JOIN $table_url_rel_course
 206                  ON $table_url_rel_course.course_code = code
 207                  $where
 208                  ORDER BY title, code";
 209  
 210          $result = Database::query($sql, __FILE__, __LINE__);
 211          $courses = Database::store_result($result);
 212          return $courses;
 213      }
 214  
 215      /** Gets the inner join of access_url and the session table
 216       * @author Julio Montoya
 217       * @return int  access url id
 218       * @return array   Database::store_result of the result
 219       * */
 220      function get_url_rel_session_data($access_url_id='') {
 221          $where = '';
 222          $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
 223          $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
 224  
 225          if (!empty($access_url_id))
 226              $where = "WHERE $table_url_rel_session.access_url_id = " . Database::escape_string($access_url_id);
 227  
 228          $sql = "SELECT id, name, access_url_id
 229                  FROM $tbl_session u
 230                  INNER JOIN $table_url_rel_session
 231                  ON $table_url_rel_session.session_id = id
 232                  $where
 233                  ORDER BY name, id";
 234  
 235          $result = Database::query($sql, __FILE__, __LINE__);
 236          $sessions = Database::store_result($result);
 237          return $sessions;
 238      }
 239  
 240      /**
 241       * Sets the status of an URL 1 or 0
 242       * @author Julio Montoya
 243       * @param string lock || unlock
 244       * @param int url id
 245       * */
 246      function set_url_status($status, $url_id) {
 247          $url_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
 248          if ($status == 'lock') {
 249              $status_db = '0';
 250          }
 251          if ($status == 'unlock') {
 252              $status_db = '1';
 253          }
 254          if (($status_db == '1' OR $status_db == '0') AND is_numeric($url_id)) {
 255              $sql = "UPDATE $url_table SET active='" . Database::escape_string($status_db) . "' WHERE id='" . Database::escape_string($url_id) . "'";
 256              $result = Database::query($sql, __FILE__, __LINE__);
 257          }
 258      }
 259  
 260      /**
 261       * Checks the relationship between an URL and a User (return the num_rows)
 262       * @author Julio Montoya
 263       * @param int user id
 264       * @param int url id
 265       * @return boolean true if success
 266       * */
 267      function relation_url_user_exist($user_id, $url_id) {
 268          $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
 269          $sql = "SELECT user_id FROM $table_url_rel_user WHERE access_url_id = " . Database::escape_string($url_id) . " AND  user_id = " . Database::escape_string($user_id) . " ";
 270          $result = Database::query($sql, __FILE__, __LINE__);
 271          $num = Database::num_rows($result);
 272          return $num;
 273      }
 274  
 275      /**
 276       * Checks the relationship between an URL and a Course (return the num_rows)
 277       * @author Julio Montoya
 278       * @param int user id
 279       * @param int url id
 280       * @return boolean true if success
 281       * */
 282      function relation_url_course_exist($course_id, $url_id) {
 283          $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
 284          $sql = "SELECT course_code FROM $table_url_rel_course WHERE access_url_id = " . Database::escape_string($url_id) . " AND course_code = '" . Database::escape_string($course_id) . "'";
 285          $result = Database::query($sql, __FILE__, __LINE__);
 286          $num = Database::num_rows($result);
 287          return $num;
 288      }
 289  
 290      /**
 291       * Checks the relationship between an URL and a Session (return the num_rows)
 292       * @author Julio Montoya
 293       * @param int user id
 294       * @param int url id
 295       * @return boolean true if success
 296       * */
 297      function relation_url_session_exist($session_id, $url_id) {
 298          $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
 299          $sql = "SELECT session_id FROM $table_url_rel_session WHERE access_url_id = " . Database::escape_string($url_id) . " AND session_id = " . Database::escape_string($session_id);
 300          $result = Database::query($sql, __FILE__, __LINE__);
 301          $num = Database::num_rows($result);
 302          return $num;
 303      }
 304  
 305      /**
 306       * Add a group of users into a group of URLs
 307       * @author Julio Montoya
 308       * @param  array of user_ids
 309       * @param  array of url_ids
 310       * */
 311      function add_users_to_urls($user_list, $url_list) {
 312          $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
 313          $result_array = array();
 314  
 315          if (is_array($user_list) && is_array($url_list)) {
 316              foreach ($url_list as $url_id) {
 317                  foreach ($user_list as $user_id) {
 318                      $count = UrlManager::relation_url_user_exist($user_id, $url_id);
 319                      if ($count == 0) {
 320                          $sql = "INSERT INTO $table_url_rel_user
 321                                     SET user_id = " . Database::escape_string($user_id) . ", access_url_id = " . Database::escape_string($url_id);
 322                          $result = Database::query($sql, __FILE__, __LINE__);
 323                          if ($result)
 324                              $result_array[$url_id][$user_id] = 1;
 325                          else
 326                              $result_array[$url_id][$user_id] = 0;
 327                      }
 328                  }
 329              }
 330          }
 331          return $result_array;
 332      }
 333  
 334      /**
 335       * Add a group of courses into a group of URLs
 336       * @author Julio Montoya
 337       * @param  array of course ids
 338       * @param  array of url_ids
 339       * */
 340      function add_courses_to_urls($course_list, $url_list) {
 341          $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
 342          $result_array = array();
 343  
 344          if (is_array($course_list) && is_array($url_list)) {
 345              foreach ($url_list as $url_id) {
 346                  foreach ($course_list as $course_code) {
 347                      $count = UrlManager::relation_url_course_exist($course_code, $url_id);
 348                      if ($count == 0) {
 349                          $sql = "INSERT INTO $table_url_rel_course
 350                                     SET course_code = '" . Database::escape_string($course_code) . "', access_url_id = " . Database::escape_string($url_id);
 351                          $result = Database::query($sql, __FILE__, __LINE__);
 352                          if ($result)
 353                              $result_array[$url_id][$course_code] = 1;
 354                          else
 355                              $result_array[$url_id][$course_code] = 0;
 356                      }
 357                  }
 358              }
 359          }
 360          return $result_array;
 361      }
 362  
 363      /**
 364       * Add a group of sessions into a group of URLs
 365       * @author Julio Montoya
 366       * @param  array of session ids
 367       * @param  array of url_ids
 368       * */
 369      function add_sessions_to_urls($session_list, $url_list) {
 370          $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
 371          $result_array = array();
 372  
 373          if (is_array($session_list) && is_array($url_list)) {
 374              foreach ($url_list as $url_id) {
 375                  foreach ($session_list as $session_id) {
 376                      $count = UrlManager::relation_url_session_exist($session_id, $url_id);
 377                      if ($count == 0) {
 378                          $sql = "INSERT INTO $table_url_rel_session
 379                                     SET session_id = " . Database::escape_string($session_id) . ", access_url_id = " . Database::escape_string($url_id);
 380                          $result = Database::query($sql, __FILE__, __LINE__);
 381                          if ($result)
 382                              $result_array[$url_id][$session_id] = 1;
 383                          else
 384                              $result_array[$url_id][$session_id] = 0;
 385                      }
 386                  }
 387              }
 388          }
 389          return $result_array;
 390      }
 391  
 392      /**
 393       * Add a user into a url
 394       * @author Julio Montoya
 395       * @param  user_id
 396       * @param  url_id
 397       * @return boolean true if success
 398       * */
 399      function add_user_to_url($user_id, $url_id=1) {
 400          $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
 401          if (empty($url_id))
 402              $url_id = 1;
 403          $count = UrlManager::relation_url_user_exist($user_id, $url_id);
 404          if (empty($count)) {
 405              $sql = "INSERT INTO $table_url_rel_user
 406                         SET user_id = " . Database::escape_string($user_id) . ", access_url_id = " . Database::escape_string($url_id);
 407              $result = Database::query($sql, __FILE__, __LINE__);
 408          }
 409          return $result;
 410      }
 411  
 412      function add_course_to_url($course_code, $url_id=1) {
 413          $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
 414          if (empty($url_id))
 415              $url_id = 1;
 416          $count = UrlManager::relation_url_course_exist($course_code, $url_id);
 417          if (empty($count)) {
 418              $sql = "INSERT INTO $table_url_rel_course
 419                         SET course_code = '" . Database::escape_string($course_code) . "', access_url_id = " . Database::escape_string($url_id);
 420              $result = Database::query($sql, __FILE__, __LINE__);
 421          }
 422          return $result;
 423      }
 424  
 425      function add_session_to_url($session_id, $url_id=1) {
 426          $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
 427          if (empty($url_id))
 428              $url_id = 1;
 429          $count = UrlManager::relation_url_session_exist($session_id, $url_id);
 430          if (empty($count)) {
 431              $sql = "INSERT INTO $table_url_rel_session
 432                         SET session_id = " . Database::escape_string($session_id) . ", access_url_id = " . Database::escape_string($url_id);
 433              $result = Database::query($sql, __FILE__, __LINE__);
 434          }
 435          return $result;
 436      }
 437  
 438      /**
 439       * Deletes an url and user relationship
 440       * @author Julio Montoya
 441       * @param int user id
 442       * @param int url id
 443       * @return boolean true if success
 444       * */
 445      function delete_url_rel_user($user_id, $url_id) {
 446          $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
 447          $sql = "DELETE FROM $table_url_rel_user WHERE user_id = " . Database::escape_string($user_id) . " AND access_url_id=" . Database::escape_string($url_id) . "  ";
 448          $result = Database::query($sql, __FILE__, __LINE__);
 449          return $result;
 450      }
 451  
 452      /**
 453       * Deletes an url and course relationship
 454       * @author Julio Montoya
 455       * @param  char  course code
 456       * @param  int url id
 457       * @return boolean true if success
 458       * */
 459      function delete_url_rel_course($course_code, $url_id) {
 460          $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
 461          $sql = "DELETE FROM $table_url_rel_course WHERE course_code = '" . Database::escape_string($course_code) . "' AND access_url_id=" . Database::escape_string($url_id) . "  ";
 462          $result = Database::query($sql, __FILE__, __LINE__);
 463          return $result;
 464      }
 465  
 466      /**
 467       * Deletes an url and session relationship
 468       * @author Julio Montoya
 469       * @param  char  course code
 470       * @param  int url id
 471       * @return boolean true if success
 472       * */
 473      function delete_url_rel_session($session_id, $url_id) {
 474          $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
 475          $sql = "DELETE FROM $table_url_rel_session WHERE session_id = " . Database::escape_string($session_id) . " AND access_url_id=" . Database::escape_string($url_id) . "  ";
 476          $result = Database::query($sql, __FILE__, __LINE__);
 477          return $result;
 478      }
 479  
 480      /**
 481       * Updates the access_url_rel_user table  with a given user list
 482       * @author Julio Montoya
 483       * @param array user list
 484       * @param int access_url_id
 485       * */
 486      function update_urls_rel_user($user_list, $access_url_id) {
 487          $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
 488          $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
 489  
 490          $sql = "SELECT user_id FROM $table_url_rel_user WHERE access_url_id=" . Database::escape_string($access_url_id);
 491          $result = Database::query($sql, __FILE__, __LINE__);
 492          $existingUsers = array();
 493  
 494          while ($row = Database::fetch_array($result)) {
 495              $existingUsers[] = $row['user_id'];
 496          }
 497  
 498          //adding users
 499          foreach ($user_list as $enreg_user) {
 500              if (!in_array($enreg_user, $existingUsers)) {
 501                  UrlManager::add_user_to_url($enreg_user, $access_url_id);
 502              }
 503          }
 504          //deleting old users
 505          foreach ($existingUsers as $existing_user) {
 506              if (!in_array($existing_user, $user_list)) {
 507                  UrlManager::delete_url_rel_user($existing_user, $access_url_id);
 508              }
 509          }
 510      }
 511  
 512      /**
 513       * Updates the access_url_rel_course table  with a given user list
 514       * @author Julio Montoya
 515       * @param array user list
 516       * @param int access_url_id
 517       * */
 518      function update_urls_rel_course($course_list, $access_url_id) {
 519          $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
 520          $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
 521  
 522          $sql = "SELECT course_code FROM $table_url_rel_course WHERE access_url_id=" . Database::escape_string($access_url_id);
 523          $result = Database::query($sql, __FILE__, __LINE__);
 524          $existing_courses = array();
 525  
 526          while ($row = Database::fetch_array($result)) {
 527              $existing_courses[] = $row['course_code'];
 528          }
 529  
 530          //adding courses
 531          foreach ($course_list as $course) {
 532              if (!in_array($course, $existing_courses)) {
 533                  UrlManager::add_course_to_url($course, $access_url_id);
 534              }
 535          }
 536  
 537          //deleting old courses
 538          foreach ($existing_courses as $existing_course) {
 539              if (!in_array($existing_course, $course_list)) {
 540                  UrlManager::delete_url_rel_course($existing_course, $access_url_id);
 541              }
 542          }
 543      }
 544  
 545      /**
 546       * Updates the access_url_rel_session table with a given user list
 547       * @author Julio Montoya
 548       * @param array user list
 549       * @param int access_url_id
 550       * */
 551      function update_urls_rel_session($session_list, $access_url_id) {
 552          $table_session = Database :: get_main_table(TABLE_MAIN_SESSION);
 553          $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
 554  
 555          $sql = "SELECT session_id FROM $table_url_rel_session WHERE access_url_id=" . Database::escape_string($access_url_id);
 556          $result = Database::query($sql, __FILE__, __LINE__);
 557          $existing_sessions = array();
 558  
 559          while ($row = Database::fetch_array($result)) {
 560              $existing_sessions[] = $row['session_id'];
 561          }
 562  
 563          //adding users
 564          foreach ($session_list as $session) {
 565              if (!in_array($session, $existing_sessions)) {
 566                  UrlManager::add_session_to_url($session, $access_url_id);
 567              }
 568          }
 569  
 570          //deleting old users
 571          foreach ($existing_sessions as $existing_session) {
 572              if (!in_array($existing_session, $session_list)) {
 573                  UrlManager::delete_url_rel_session($existing_session, $access_url_id);
 574              }
 575          }
 576      }
 577  
 578      function get_access_url_from_user($user_id) {
 579          $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
 580          $table_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
 581          $sql = "SELECT url, access_url_id FROM $table_url_rel_user url_rel_user INNER JOIN $table_url u
 582                  ON (url_rel_user.access_url_id = u.id)
 583                  WHERE user_id = " . Database::escape_string($user_id);
 584          $result = Database::query($sql, __FILE__, __LINE__);
 585          $url_list = Database::store_result($result);
 586          return $url_list;
 587      }
 588  
 589      /**
 590       *
 591       * */
 592      function get_url_id($url) {
 593          $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
 594          $sql = "SELECT id FROM $table_access_url WHERE url = '" . Database::escape_string($url) . "'";
 595          $result = Database::query($sql);
 596          $access_url_id = Database::result($result, 0, 0);
 597          return $access_url_id;
 598      }
 599  
 600      /**
 601       * Check if the URL passed is the main URL
 602       */
 603      function is_main_url($main_url_id) {
 604          global $_configuration;
 605          if ($_configuration['access_url'] == $main_url_id) {
 606              $count = 1;
 607          }
 608          $return = false;
 609          if ($count > 0) {
 610              $return = true;
 611          }
 612          return $return;
 613      }
 614  
 615      function get_main_url_id() {
 616          global $_configuration;
 617          return $this->get_url_id($_configuration['root_web']);
 618      }
 619  
 620      function get_list_admin_url($url_id) {
 621          $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
 622          $table_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
 623          $user = Database::get_main_table(TABLE_MAIN_USER);
 624          $admin = Database::get_main_table(TABLE_MAIN_ADMIN);
 625          $sql = "SELECT u.user_id, lastname, firstname, username, access_url_id FROM $table_url ur
 626            INNER JOIN $table_url_rel_user rel_user
 627            ON ur.id = rel_user.access_url_id
 628            INNER JOIN $user u
 629            ON rel_user.user_id = u.user_id
 630            INNER JOIN $admin a
 631            ON u.user_id = a.user_id
 632            WHERE ur.id = $url_id";
 633  
 634          $result = api_sql_query($sql, __FILE__, __LINE__);
 635          $admins = api_store_result($result);
 636          return $admins;
 637      }
 638  
 639      function get_list_super_admin_url($url_id) {
 640          $table_url_rel_admin = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_ADMIN);
 641          $user = Database::get_main_table(TABLE_MAIN_USER);
 642          $sql = "SELECT u.user_id, lastname, firstname, username, access_url_id FROM $table_url_rel_admin rel_adm
 643            INNER JOIN $user u
 644            ON rel_adm.user_id = u.user_id 
 645            WHERE rel_adm.access_url_id = $url_id";
 646          $result = api_sql_query($sql, __FILE__, __LINE__);
 647          $admins = api_store_result($result);
 648          return $admins;
 649      }
 650  
 651      function add_urls_rel_admin($user_id, $access_url_id) {
 652          $table_url_rel_admin = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_ADMIN);
 653          $sql = "INSERT INTO $table_url_rel_admin VALUES($access_url_id,$user_id)";
 654          $result = Database::query($sql);
 655      }
 656  
 657      function delete_urls_rel_admin($user_id, $access_url_id) {
 658          $table_url_rel_admin = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_ADMIN);
 659          $sql = "DELETE FROM $table_url_rel_admin  WHERE user_id = $user_id AND access_url_id = $access_url_id";
 660          $result = Database::query($sql);
 661      }
 662  
 663      function update_urls_rel_admin($admin_list, $access_url_id) {
 664          $table_url_rel_admin = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_ADMIN);
 665          $sql = "SELECT user_id FROM $table_url_rel_admin WHERE access_url_id = $access_url_id";
 666          $result = Database::query($sql, __FILE__, __LINE__);
 667          $existingAdmins = array();
 668          while ($row = Database::fetch_array($result)) {
 669              $existingAdmins[] = $row['user_id'];
 670          }
 671          foreach ($admin_list as $admin) {
 672              if (!in_array($admin, $existingAdmins)) {
 673                  UrlManager::add_urls_rel_admin($admin, $access_url_id);
 674              }
 675          }
 676          //deleting old users
 677          foreach ($existingAdmins as $existing_admin) {
 678              if (!in_array($existing_admin, $admin_list)) {
 679                  UrlManager::delete_urls_rel_admin($existing_admin, $access_url_id);
 680              }
 681          }
 682      }
 683  
 684      function is_superadmin_in_current_url($user_id) {
 685          global $_configuration;
 686          $url_id = api_get_current_access_url_id();
 687          return $this->is_superadmin_in_url($url_id, $user_id);
 688      }
 689  
 690      function is_superadmin($user_id) {
 691          $table_url_rel_admin = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_ADMIN);
 692          $sql = "SELECT COUNT(*) AS count FROM $table_url_rel_admin WHERE  user_id='" . Database::escape_string($user_id) . "'";
 693          $rs = Database::query($sql, __FILE__, __LINE__);
 694          $row = Database::fetch_array($rs, 'ASSOC');
 695          $return = false;
 696          if ($row['count'] > 0) {
 697              $return = true;
 698          }
 699          return $return;
 700      }
 701  
 702      function allow_superadmin(){
 703          $table_url_rel_admin = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_ADMIN);
 704          $sql = "SELECT COUNT(*) AS count FROM $table_url_rel_admin";
 705          $rs = Database::query($sql, __FILE__, __LINE__);
 706          $row = Database::fetch_array($rs, 'ASSOC');
 707          $return = false;
 708          if ($row['count'] > 0) {
 709              $return = true;
 710          }
 711          return $return;
 712      }
 713      
 714      function current_url_has_superadmins() {
 715          $current_url_id = api_get_current_access_url_id();
 716          $table_url_rel_admin = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_ADMIN);
 717          $sql = "SELECT COUNT(*) AS count FROM $table_url_rel_admin WHERE access_url_id='" . Database::escape_string($current_url_id) . "'";
 718          $rs = Database::query($sql, __FILE__, __LINE__);
 719          $row = Database::fetch_array($rs, 'ASSOC');
 720          $return = false;
 721          if ($row['count'] > 0) {
 722              $return = true;
 723          }
 724          return $return;
 725      }
 726  
 727      /**
 728       * Checks if the given user is super admin in the given URL
 729       * @param integer $url_id
 730       * @param integer $user_id
 731       * @return integer
 732       */
 733      function is_superadmin_in_url($url_id, $user_id) {
 734          $table_url_rel_admin = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_ADMIN);
 735          $sql = "SELECT COUNT(*) AS count FROM $table_url_rel_admin WHERE access_url_id='" . Database::escape_string($url_id) . "' AND user_id='" . Database::escape_string($user_id) . "'";
 736          $rs = Database::query($sql, __FILE__, __LINE__);
 737          $row = Database::fetch_array($rs, 'ASSOC');
 738  
 739          return $row['count'];
 740      }
 741  
 742      function url_has_active_tools($url_id) {
 743          $t_cs = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
 744          $sql = "SELECT count(*) as count FROM $t_cs WHERE variable='course_create_active_tools' AND access_url = '" . Database::escape_string($url_id) . "'";
 745          $rs = Database::query($sql, __FILE__, __LINE__);
 746          $row_count = Database::fetch_object($rs);
 747          $count = $row_count->count;
 748          if ($count > 0) {
 749              $return = true;
 750          } else {
 751              $return = false;
 752          }
 753          return $return;
 754      }
 755  
 756      function add_course_create_active_tools_to_urls() {
 757          $t_settings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
 758          $url_list = array();
 759          $url_list = $this->get_url_data();
 760          $active_tool_list = array();
 761          foreach ($url_list as $get_url) {
 762              $url_id = $get_url['id'];
 763              $rs_active_url = $this->url_has_active_tools($url_id);
 764              
 765              if ($rs_active_url === false) {
 766                  // Get active tools to create upon training creation process
 767                  $active_tool_list = $this->get_active_tools_for_create_upon_create_course();
 768                  // Add the active tools to each URL
 769                  foreach ($active_tool_list as $get_tool) {
 770                      if($get_tool['access_url_changeable'] == 1) {
 771                          $insert = "INSERT INTO $t_settings " .
 772                                  "(variable,subkey," .
 773                                  "type,category," .
 774                                  "selected_value,title," .
 775                                  "comment,scope," .
 776                                  "subkeytext,access_url,access_url_changeable)" .
 777                                  " VALUES " .
 778                                  "('" . $get_tool['variable'] . "'," . (!empty($get_tool['subkey']) ? "'" . $get_tool['subkey'] . "'" : "NULL") . "," .
 779                                  "'" . $get_tool['type'] . "','" . $get_tool['category'] . "'," .
 780                                  "'" . $get_tool['selected_value'] . "','" . $get_tool['title'] . "'," .
 781                                  "" . (!empty($get_tool['comment']) ? "'" . $get_tool['comment'] . "'" : "NULL") . "," . (!empty($get_tool['scope']) ? "'" . $get_tool['scope'] . "'" : "NULL") . "," .
 782                                  "" . (!empty($get_tool['subkeytext']) ? "'" . $get_tool['subkeytext'] . "'" : "NULL") . ",'" . $url_id . "','" . $get_tool['access_url_changeable'] . "')";
 783  
 784                          $res = Database::query($insert, __FILE__, __LINE__);
 785                      }
 786                  }
 787              }
 788          }
 789      }
 790      
 791      function get_active_tools_for_create_upon_create_course() {
 792          $t_cs = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
 793          $url_list = array();
 794          $url_list = $this->get_url_data();
 795          $active_tool_list = array();
 796          $active_list = array();
 797          foreach ($url_list as $get_url) {
 798              $url_id = $get_url['id'];
 799              $rs_active_url = $this->url_has_active_tools($url_id);
 800              if ($rs_active_url === true) {
 801                  $sql = "SELECT variable,subkey,type,category,selected_value,title,comment,scope,subkeytext,access_url_changeable FROM $t_cs WHERE variable='course_create_active_tools' AND access_url = '" . Database::escape_string($url_id) . "'";
 802                  $rs = Database::query($sql, __FILE__, __LINE__);
 803                  while ($row = Database::fetch_object($rs)) {
 804                      $active_list['variable'] = $row->variable;
 805                      $active_list['subkey'] = $row->subkey;
 806                      $active_list['type'] = $row->type;
 807                      $active_list['selected_value'] = $row->selected_value;
 808                      $active_list['title'] = $row->title;
 809                      $active_list['comment'] = $row->comment;
 810                      $active_list['scope'] = $row->scope;
 811                      $active_list['access_url_changeable'] = $row->access_url_changeable;
 812                      $active_list['subkeytext'] = $row->subkeytext;
 813                      $active_list['category'] = $row->category;
 814                      $active_tool_list[] = $active_list;
 815                  }
 816                  break;
 817              }
 818          }
 819          return $active_tool_list;
 820      }
 821  
 822  }
 823  
 824  ?>

title

Description

title

Description

title

Description

title

title

Body