urlmanager.lib.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This library provides functions for the access_url management.
  5. * Include/require it in your code to use its functionality.
  6. * @package chamilo.library
  7. */
  8. class UrlManager
  9. {
  10. /**
  11. * Creates a new url access
  12. *
  13. * @author Julio Montoya <gugli100@gmail.com>,
  14. *
  15. * @param string The URL of the site
  16. * @param string The description of the site
  17. * @param int is active or not
  18. * @param int the user_id of the owner
  19. * @param int The type of URL (1=multiple-access-url, 2=sincro-server, 3=sincro-client)
  20. * @param array If the type is different than 1, then there might be extra URL parameters to take into account
  21. * @return boolean if success
  22. */
  23. public static function add($url, $description, $active, $type = 1, $extra_params)
  24. {
  25. $tms = time();
  26. $type = intval($type);
  27. $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  28. $u = api_get_user_id();
  29. if ($u == 0) {
  30. $u = api_get_anonymous_id();
  31. }
  32. if ($type > 1) {
  33. $active = 0;
  34. }
  35. $sql = "INSERT INTO $table_access_url ".
  36. " SET url = '".Database::escape_string($url)."', ".
  37. " description = '".Database::escape_string($description)."', ".
  38. " active = $active, ".
  39. " created_by = $u, ".
  40. " url_type = $type, ".
  41. " tms = FROM_UNIXTIME(".$tms.")";
  42. $result = Database::query($sql);
  43. $id = Database::insert_id();
  44. if ($result !== false && $type == 3 && count($extra_params) > 0) {
  45. // Register extra parameters in the branch_sync table
  46. $t = Database::get_main_table(TABLE_BRANCH_SYNC);
  47. $sql = "INSERT INTO $t SET ".
  48. " access_url_id = $id ".
  49. (!empty($extra_params['ip']) ? ", branch_ip = '".Database::escape_string($extra_params['ip'])."'" : "").
  50. (!empty($extra_params['name']) ? ", branch_name = '".Database::escape_string(
  51. $extra_params['name']
  52. )."'" : "").
  53. (!empty($extra_params['last_sync']) ? ", last_sync_trans_id = '".Database::escape_string(
  54. $extra_params['last_sync']
  55. )."'" : "").
  56. (!empty($extra_params['dwn_speed']) ? ", dwn_speed = '".Database::escape_string(
  57. $extra_params['dwn_speed']
  58. )."'" : "").
  59. (!empty($extra_params['up_speed']) ? ", up_speed = '".Database::escape_string(
  60. $extra_params['up_speed']
  61. )."'" : "").
  62. (!empty($extra_params['delay']) ? ", delay = '".Database::escape_string(
  63. $extra_params['delay']
  64. )."'" : "").
  65. (!empty($extra_params['admin_mail']) ? ", admin_mail = '".Database::escape_string(
  66. $extra_params['admin_mail']
  67. )."'" : "").
  68. (!empty($extra_params['admin_name']) ? ", admin_name = '".Database::escape_string(
  69. $extra_params['admin_name']
  70. )."'" : "").
  71. (!empty($extra_params['admin_phone']) ? ", admin_phone = '".Database::escape_string(
  72. $extra_params['admin_phone']
  73. )."'" : "").
  74. (!empty($extra_params['latitude']) ? ", latitude = '".Database::escape_string(
  75. $extra_params['latitude']
  76. )."'" : "").
  77. (!empty($extra_params['longitude']) ? ", longitude = '".Database::escape_string(
  78. $extra_params['longitude']
  79. )."'" : "").
  80. ", last_sync_trans_date = '".api_get_utc_datetime()."'";
  81. $result = $result && Database::query($sql);
  82. }
  83. return $result;
  84. }
  85. /**
  86. * Updates an URL access to Dokeos
  87. * @author Julio Montoya <gugli100@gmail.com>,
  88. *
  89. * @param int The url id
  90. * @param string The description of the site
  91. * @param int is active or not
  92. * @param int the user_id of the owner
  93. * @param int The URL type
  94. * @param array Extra parameters for type > 1
  95. * @return boolean if success
  96. */
  97. public static function udpate($url_id, $url, $description, $active, $type, $extra_params)
  98. {
  99. $url_id = intval($url_id);
  100. $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  101. $tms = time();
  102. $sql = "UPDATE $table_access_url
  103. SET url = '".Database::escape_string($url)."',
  104. description = '".Database::escape_string($description)."',
  105. active = '".Database::escape_string($active)."',
  106. created_by = '".api_get_user_id()."',
  107. tms = FROM_UNIXTIME(".$tms.")
  108. WHERE id = '$url_id'";
  109. $result = Database::query($sql);
  110. return $result;
  111. }
  112. /**
  113. * Deletes an url
  114. * @author Julio Montoya
  115. * @param int url id
  116. * @return boolean true if success
  117. * */
  118. public static function delete($id)
  119. {
  120. $id = intval($id);
  121. $table_bs = Database :: get_main_table(TABLE_BRANCH_SYNC);
  122. $table_bsl = Database :: get_main_table(TABLE_BRANCH_SYNC_LOG);
  123. $table_bt = Database :: get_main_table(TABLE_BRANCH_TRANSACTION);
  124. $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  125. $sql = "DELETE FROM $table_bt WHERE branch_id = ".$id;
  126. $result = Database::query($sql);
  127. $sql = "DELETE FROM $table_bsl WHERE branch_sync_id = ".$id;
  128. $result = Database::query($sql);
  129. $sql = "DELETE FROM $table_bs WHERE access_url_id = ".$id;
  130. $result = Database::query($sql);
  131. $sql = "DELETE FROM $table_access_url WHERE id = ".$id;
  132. $result = Database::query($sql);
  133. return $result;
  134. }
  135. /**
  136. *
  137. * */
  138. public static function url_exist($url)
  139. {
  140. $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  141. $sql = "SELECT id FROM $table_access_url WHERE url = '".Database::escape_string($url)."' ";
  142. $res = Database::query($sql);
  143. $num = Database::num_rows($res);
  144. return $num;
  145. }
  146. /**
  147. *
  148. * */
  149. public static function url_id_exist($url)
  150. {
  151. $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  152. $sql = "SELECT id FROM $table_access_url WHERE id = '".Database::escape_string($url)."' ";
  153. $res = Database::query($sql);
  154. $num = Database::num_rows($res);
  155. return $num;
  156. }
  157. /**
  158. * This function get the quantity of URLs
  159. * @author Julio Montoya
  160. * @return int count of urls
  161. * */
  162. public static function url_count()
  163. {
  164. $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  165. $sql = "SELECT count(id) as count_result FROM $table_access_url";
  166. $res = Database::query($sql);
  167. $url = Database::fetch_array($res, 'ASSOC');
  168. $result = $url['count_result'];
  169. return $result;
  170. }
  171. /**
  172. * Gets the id, url, description, and active status of ALL URLs
  173. * @author Julio Montoya
  174. * @return array
  175. * */
  176. public static function get_url_data()
  177. {
  178. $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  179. $table_branch_sync = Database :: get_main_table(TABLE_BRANCH_SYNC);
  180. $sql = "SELECT id, url, description, active, url_type FROM $table_access_url ORDER BY id";
  181. $res = Database::query($sql);
  182. $urls = array();
  183. while ($url = Database::fetch_assoc($res)) {
  184. if ($url['url_type'] > 1) {
  185. $sql2 = "SELECT branch_name, branch_ip, latitude, longitude, dwn_speed, up_speed, delay, admin_mail, admin_name, admin_phone, last_sync_trans_id, last_sync_trans_date, last_sync_type FROM $table_branch_sync WHERE access_url_id = ".$url['id'];
  186. $res2 = Database::query($sql2);
  187. $url2 = Database::fetch_assoc($res2);
  188. $url = array_merge($url, $url2);
  189. }
  190. $urls[] = $url;
  191. }
  192. return $urls;
  193. }
  194. /**
  195. * Gets the id, url, description, and active status of ALL URLs
  196. * @author Julio Montoya
  197. * @return array
  198. * */
  199. public static function get_url_data_from_id($url_id)
  200. {
  201. $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  202. $sql = "SELECT id, url, description, active, url_type FROM $table_access_url WHERE id = ".Database::escape_string(
  203. $url_id
  204. );
  205. $res = Database::query($sql);
  206. $row = Database::fetch_array($res);
  207. if ($row['url_type'] > 1) {
  208. $sql2 = "SELECT * FROM $table_branch_sync WHERE access_url_id = ".$url['id'];
  209. $res2 = Database::query($sql);
  210. $row2 = Database::fetch_array($res2);
  211. $row = array_merge($row, $row2);
  212. }
  213. return $row;
  214. }
  215. /** Gets the inner join of users and urls table
  216. * @author Julio Montoya
  217. * @param int access url id
  218. * @return array Database::store_result of the result
  219. * */
  220. public static function get_url_rel_user_data($access_url_id = null, $order_by = null)
  221. {
  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($access_url_id)) {
  226. $where = "WHERE $table_url_rel_user.access_url_id = ".Database::escape_string($access_url_id);
  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. return $users;
  242. }
  243. /**
  244. * Gets the inner join of access_url and the course table
  245. * @author Julio Montoya
  246. * @param int access url id
  247. * @return array Database::store_result of the result
  248. * */
  249. public static function get_url_rel_course_data($access_url_id = '')
  250. {
  251. $where = '';
  252. $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  253. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  254. if (!empty($access_url_id)) {
  255. $where = "WHERE $table_url_rel_course.access_url_id = ".Database::escape_string($access_url_id);
  256. }
  257. $sql = "SELECT c_id, code, title, access_url_id
  258. FROM $tbl_course u
  259. INNER JOIN $table_url_rel_course
  260. ON $table_url_rel_course.c_id = u.id
  261. $where
  262. ORDER BY title, c_id";
  263. $result = Database::query($sql);
  264. $courses = Database::store_result($result);
  265. return $courses;
  266. }
  267. /** Gets the inner join of access_url and the session table
  268. * @author Julio Montoya
  269. * @param int access url id
  270. * @return array Database::store_result of the result
  271. * */
  272. public static function get_url_rel_session_data($access_url_id = '')
  273. {
  274. $where = '';
  275. $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  276. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  277. if (!empty($access_url_id)) {
  278. $where = "WHERE $table_url_rel_session.access_url_id = ".Database::escape_string($access_url_id);
  279. }
  280. $sql = "SELECT id, name, access_url_id
  281. FROM $tbl_session u
  282. INNER JOIN $table_url_rel_session
  283. ON $table_url_rel_session.session_id = id
  284. $where
  285. ORDER BY name, id";
  286. $result = Database::query($sql);
  287. $sessions = Database::store_result($result);
  288. return $sessions;
  289. }
  290. /**
  291. * Sets the status of an URL 1 or 0
  292. * @author Julio Montoya
  293. * @param string lock || unlock
  294. * @param int url id
  295. * */
  296. public static function set_url_status($status, $url_id)
  297. {
  298. $url_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  299. if ($status == 'lock') {
  300. $status_db = '0';
  301. }
  302. if ($status == 'unlock') {
  303. $status_db = '1';
  304. }
  305. if (($status_db == '1' OR $status_db == '0') AND is_numeric($url_id)) {
  306. $sql = "UPDATE $url_table SET active='".Database::escape_string(
  307. $status_db
  308. )."' WHERE id='".Database::escape_string($url_id)."'";
  309. $result = Database::query($sql);
  310. }
  311. }
  312. /**
  313. * Checks the relationship between an URL and a User (return the num_rows)
  314. * @author Julio Montoya
  315. * @param int user id
  316. * @param int url id
  317. * @return boolean true if success
  318. * */
  319. public static function relation_url_user_exist($user_id, $url_id)
  320. {
  321. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  322. $sql = "SELECT user_id FROM $table_url_rel_user WHERE access_url_id = ".Database::escape_string(
  323. $url_id
  324. )." AND user_id = ".Database::escape_string($user_id)." ";
  325. $result = Database::query($sql);
  326. $num = Database::num_rows($result);
  327. return $num;
  328. }
  329. /**
  330. * Checks the relationship between an URL and a Course (return the num_rows)
  331. * @author Julio Montoya
  332. * @param int user id
  333. * @param int url id
  334. * @return boolean true if success
  335. * */
  336. public static function relation_url_course_exist($course_id, $url_id)
  337. {
  338. $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  339. $sql = "SELECT c_id FROM $table_url_rel_course
  340. WHERE access_url_id = ".Database::escape_string(
  341. $url_id
  342. )." AND c_id = '".Database::escape_string($course_id)."'";
  343. $result = Database::query($sql);
  344. $num = Database::num_rows($result);
  345. return $num;
  346. }
  347. /**
  348. * Checks the relationship between an URL and a Session (return the num_rows)
  349. * @author Julio Montoya
  350. * @param int user id
  351. * @param int url id
  352. * @return boolean true if success
  353. * */
  354. public static function relation_url_session_exist($session_id, $url_id)
  355. {
  356. $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  357. $session_id = intval($session_id);
  358. $url_id = intval($url_id);
  359. $sql = "SELECT session_id FROM $table_url_rel_session WHERE access_url_id = ".Database::escape_string(
  360. $url_id
  361. )." AND session_id = ".Database::escape_string($session_id);
  362. $result = Database::query($sql);
  363. $num = Database::num_rows($result);
  364. return $num;
  365. }
  366. /**
  367. * Add a group of users into a group of URLs
  368. * @author Julio Montoya
  369. * @param array of user_ids
  370. * @param array of url_ids
  371. * */
  372. public static function add_users_to_urls($user_list, $url_list)
  373. {
  374. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  375. $result_array = array();
  376. if (is_array($user_list) && is_array($url_list)) {
  377. foreach ($url_list as $url_id) {
  378. foreach ($user_list as $user_id) {
  379. $count = UrlManager::relation_url_user_exist($user_id, $url_id);
  380. if ($count == 0) {
  381. $sql = "INSERT INTO $table_url_rel_user
  382. SET user_id = ".Database::escape_string(
  383. $user_id
  384. ).", access_url_id = ".Database::escape_string($url_id);
  385. $result = Database::query($sql);
  386. if ($result) {
  387. $result_array[$url_id][$user_id] = 1;
  388. } else {
  389. $result_array[$url_id][$user_id] = 0;
  390. }
  391. }
  392. }
  393. }
  394. }
  395. return $result_array;
  396. }
  397. /**
  398. * Add a group of courses into a group of URLs
  399. * @author Julio Montoya
  400. * @param array of course ids
  401. * @param array of url_ids
  402. * */
  403. public static function add_courses_to_urls($course_list, $url_list)
  404. {
  405. $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  406. $result_array = array();
  407. if (is_array($course_list) && is_array($url_list)) {
  408. foreach ($url_list as $url_id) {
  409. foreach ($course_list as $courseId) {
  410. $count = UrlManager::relation_url_course_exist($courseId, $url_id);
  411. if ($count == 0) {
  412. $sql = "INSERT INTO $table_url_rel_course
  413. SET c_id = '".Database::escape_string(
  414. $courseId
  415. )."', access_url_id = ".Database::escape_string($url_id);
  416. $result = Database::query($sql);
  417. if ($result) {
  418. $result_array[$url_id][$courseId] = 1;
  419. } else {
  420. $result_array[$url_id][$courseId] = 0;
  421. }
  422. }
  423. }
  424. }
  425. }
  426. return $result_array;
  427. }
  428. /**
  429. * Add a group of sessions into a group of URLs
  430. * @author Julio Montoya
  431. * @param array of session ids
  432. * @param array of url_ids
  433. * */
  434. public static function add_sessions_to_urls($session_list, $url_list)
  435. {
  436. $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  437. $result_array = array();
  438. if (is_array($session_list) && is_array($url_list)) {
  439. foreach ($url_list as $url_id) {
  440. foreach ($session_list as $session_id) {
  441. $count = UrlManager::relation_url_session_exist($session_id, $url_id);
  442. if ($count == 0) {
  443. $sql = "INSERT INTO $table_url_rel_session
  444. SET session_id = ".Database::escape_string(
  445. $session_id
  446. ).", access_url_id = ".Database::escape_string($url_id);
  447. $result = Database::query($sql);
  448. if ($result) {
  449. $result_array[$url_id][$session_id] = 1;
  450. } else {
  451. $result_array[$url_id][$session_id] = 0;
  452. }
  453. }
  454. }
  455. }
  456. }
  457. return $result_array;
  458. }
  459. /**
  460. * Add a user into a url
  461. * @author Julio Montoya
  462. * @param user_id
  463. * @param url_id
  464. * @return boolean true if success
  465. * */
  466. public static function add_user_to_url($user_id, $url_id = 1)
  467. {
  468. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  469. if (empty($url_id)) {
  470. $url_id = 1;
  471. }
  472. $count = UrlManager::relation_url_user_exist($user_id, $url_id);
  473. $result = true;
  474. if (empty($count)) {
  475. $sql = "INSERT INTO $table_url_rel_user (user_id, access_url_id) VALUES ('".Database::escape_string(
  476. $user_id
  477. )."', '".Database::escape_string($url_id)."') ";
  478. $result = Database::query($sql);
  479. }
  480. return $result;
  481. }
  482. public static function add_course_to_url($courseId, $url_id = 1)
  483. {
  484. $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  485. if (empty($url_id)) {
  486. $url_id = 1;
  487. }
  488. $count = UrlManager::relation_url_course_exist($courseId, $url_id);
  489. $result = false;
  490. if (empty($count)) {
  491. $sql = "INSERT INTO $table_url_rel_course
  492. SET c_id = '".Database::escape_string(
  493. $courseId
  494. )."', access_url_id = ".Database::escape_string($url_id);
  495. $result = Database::query($sql);
  496. }
  497. return $result;
  498. }
  499. /**
  500. * Inserts a session to a URL (access_url_rel_session table)
  501. * @param int Session ID
  502. * @param int URL ID
  503. * @return bool True on success, false session already exists or insert failed
  504. */
  505. public static function add_session_to_url($session_id, $url_id = 1)
  506. {
  507. $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  508. if (empty($url_id)) {
  509. $url_id = 1;
  510. }
  511. $result = false;
  512. $count = UrlManager::relation_url_session_exist($session_id, $url_id);
  513. $session_id = intval($session_id);
  514. if (empty($count) && !empty($session_id)) {
  515. $url_id = intval($url_id);
  516. $sql = "INSERT INTO $table_url_rel_session
  517. SET session_id = ".Database::escape_string(
  518. $session_id
  519. ).", access_url_id = ".Database::escape_string($url_id);
  520. $result = Database::query($sql);
  521. }
  522. return $result;
  523. }
  524. /**
  525. * Deletes an url and user relationship
  526. * @author Julio Montoya
  527. * @param int user id
  528. * @param int url id
  529. * @return boolean true if success
  530. * */
  531. public static function delete_url_rel_user($user_id, $url_id)
  532. {
  533. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  534. $result = true;
  535. if (!empty($user_id) && !empty($url_id)) {
  536. $sql = "DELETE FROM $table_url_rel_user WHERE user_id = ".Database::escape_string(
  537. $user_id
  538. )." AND access_url_id = ".Database::escape_string($url_id);
  539. $result = Database::query($sql);
  540. }
  541. return $result;
  542. }
  543. /**
  544. * Deletes an url and course relationship
  545. * @author Julio Montoya
  546. * @param int course id
  547. * @param int url id
  548. * @return boolean true if success
  549. * */
  550. public static function delete_url_rel_course($courseId, $url_id)
  551. {
  552. $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  553. $sql = "DELETE FROM $table_url_rel_course
  554. WHERE c_id = '".Database::escape_string($courseId)."' AND access_url_id=".Database::escape_string($url_id)." ";
  555. $result = Database::query($sql);
  556. return $result;
  557. }
  558. /**
  559. * Deletes an url and session relationship
  560. * @author Julio Montoya
  561. * @param char course code
  562. * @param int url id
  563. * @return boolean true if success
  564. * */
  565. public static function delete_url_rel_session($session_id, $url_id)
  566. {
  567. $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  568. $sql = "DELETE FROM $table_url_rel_session WHERE session_id = ".Database::escape_string(
  569. $session_id
  570. )." AND access_url_id=".Database::escape_string($url_id)." ";
  571. $result = Database::query($sql, 'ASSOC');
  572. return $result;
  573. }
  574. /**
  575. * Updates the access_url_rel_user table with a given user list
  576. * @author Julio Montoya
  577. * @param array user list
  578. * @param int access_url_id
  579. * */
  580. public static function update_urls_rel_user($user_list, $access_url_id)
  581. {
  582. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  583. $sql = "SELECT user_id FROM $table_url_rel_user WHERE access_url_id = ".intval($access_url_id);
  584. $result = Database::query($sql);
  585. $existing_users = array();
  586. // Getting all users
  587. while ($row = Database::fetch_array($result)) {
  588. $existing_users[] = $row['user_id'];
  589. }
  590. // Adding users
  591. $users_added = array();
  592. foreach ($user_list as $user_id_to_add) {
  593. if (!in_array($user_id_to_add, $existing_users)) {
  594. $result = UrlManager::add_user_to_url($user_id_to_add, $access_url_id);
  595. if ($result) {
  596. $users_added[] = $user_id_to_add;
  597. }
  598. }
  599. }
  600. $users_deleted = array();
  601. //deleting old users
  602. foreach ($existing_users as $user_id_to_delete) {
  603. if (!in_array($user_id_to_delete, $user_list)) {
  604. $result = UrlManager::delete_url_rel_user($user_id_to_delete, $access_url_id);
  605. if ($result) {
  606. $users_deleted[] = $user_id_to_delete;
  607. }
  608. }
  609. }
  610. if (empty($users_added) && empty($users_deleted)) {
  611. return false;
  612. }
  613. return array('users_added' => $users_added, 'users_deleted' => $users_deleted);
  614. }
  615. /**
  616. * Updates the access_url_rel_course table with a given user list
  617. * @author Julio Montoya
  618. * @param array user list
  619. * @param int access_url_id
  620. * */
  621. public static function update_urls_rel_course($course_list, $access_url_id)
  622. {
  623. $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  624. $sql = "SELECT c_id FROM $table_url_rel_course WHERE access_url_id=".intval($access_url_id);
  625. $result = Database::query($sql);
  626. $existing_courses = array();
  627. while ($row = Database::fetch_array($result)) {
  628. $existing_courses[] = $row['c_id'];
  629. }
  630. //adding courses
  631. foreach ($course_list as $courseId) {
  632. if (!in_array($courseId, $existing_courses)) {
  633. UrlManager::add_course_to_url($courseId, $access_url_id);
  634. CourseManager::update_course_ranking($courseId, 0, $access_url_id);
  635. }
  636. }
  637. //deleting old courses
  638. foreach ($existing_courses as $courseId) {
  639. if (!in_array($courseId, $course_list)) {
  640. UrlManager::delete_url_rel_course($courseId, $access_url_id);
  641. CourseManager::update_course_ranking($courseId, 0, $access_url_id);
  642. }
  643. }
  644. }
  645. /**
  646. * Updates the access_url_rel_session table with a given user list
  647. * @author Julio Montoya
  648. * @param array user list
  649. * @param int access_url_id
  650. * */
  651. public static function update_urls_rel_session($session_list, $access_url_id)
  652. {
  653. $table_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  654. $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  655. $sql = "SELECT session_id FROM $table_url_rel_session WHERE access_url_id=".Database::escape_string(
  656. $access_url_id
  657. );
  658. $result = Database::query($sql);
  659. $existing_sessions = array();
  660. while ($row = Database::fetch_array($result)) {
  661. $existing_sessions[] = $row['session_id'];
  662. }
  663. //adding users
  664. foreach ($session_list as $session) {
  665. if (!in_array($session, $existing_sessions)) {
  666. if (!empty($session) && !empty($access_url_id)) {
  667. UrlManager::add_session_to_url($session, $access_url_id);
  668. }
  669. }
  670. }
  671. //deleting old users
  672. foreach ($existing_sessions as $existing_session) {
  673. if (!in_array($existing_session, $session_list)) {
  674. if (!empty($existing_session) && !empty($access_url_id)) {
  675. UrlManager::delete_url_rel_session($existing_session, $access_url_id);
  676. }
  677. }
  678. }
  679. }
  680. public static function get_access_url_from_user($user_id)
  681. {
  682. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  683. $table_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  684. $sql = "SELECT url, access_url_id FROM $table_url_rel_user url_rel_user INNER JOIN $table_url u
  685. ON (url_rel_user.access_url_id = u.id)
  686. WHERE user_id = ".Database::escape_string($user_id);
  687. $result = Database::query($sql);
  688. $url_list = Database::store_result($result, 'ASSOC');
  689. return $url_list;
  690. }
  691. public static function get_access_url_from_session($session_id)
  692. {
  693. $table_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  694. $table_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  695. $sql = "SELECT url, access_url_id FROM $table_url_rel_session url_rel_session INNER JOIN $table_url u
  696. ON (url_rel_session.access_url_id = u.id)
  697. WHERE session_id = ".Database::escape_string($session_id);
  698. $result = Database::query($sql);
  699. $url_list = Database::store_result($result);
  700. return $url_list;
  701. }
  702. /**
  703. *
  704. * */
  705. public static function get_url_id($url)
  706. {
  707. $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  708. $sql = "SELECT id FROM $table_access_url WHERE url = '".Database::escape_string($url)."'";
  709. $result = Database::query($sql);
  710. $access_url_id = Database::result($result, 0, 0);
  711. return $access_url_id;
  712. }
  713. }