urlmanager.lib.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class UrlManager
  5. * This library provides functions for the access_url management.
  6. * Include/require it in your code to use its functionality.
  7. *
  8. * @package chamilo.library
  9. */
  10. class UrlManager
  11. {
  12. /**
  13. * Creates a new url access
  14. *
  15. * @author Julio Montoya <gugli100@gmail.com>,
  16. *
  17. * @param string The URL of the site
  18. * @param string The description of the site
  19. * @param int is active or not
  20. * @return boolean if success
  21. */
  22. public static function add($url, $description, $active)
  23. {
  24. $tms = time();
  25. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  26. $sql = "INSERT INTO $table_access_url
  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. $result = Database::query($sql);
  33. return $result;
  34. }
  35. /**
  36. * Updates an URL access
  37. * @author Julio Montoya <gugli100@gmail.com>,
  38. *
  39. * @param int The url id
  40. * @param string The description of the site
  41. * @param int is active or not
  42. * @return boolean if success
  43. */
  44. public static function update($url_id, $url, $description, $active)
  45. {
  46. $url_id = intval($url_id);
  47. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  48. $tms = time();
  49. $sql = "UPDATE $table_access_url
  50. SET url = '".Database::escape_string($url)."',
  51. description = '".Database::escape_string($description)."',
  52. active = '".intval($active)."',
  53. created_by = '".api_get_user_id()."',
  54. tms = FROM_UNIXTIME(".$tms.")
  55. WHERE id = '$url_id'";
  56. $result = Database::query($sql);
  57. return $result;
  58. }
  59. /**
  60. * Deletes an url
  61. * @author Julio Montoya
  62. * @param int url id
  63. * @return boolean true if success
  64. * */
  65. public static function delete($id)
  66. {
  67. $id = intval($id);
  68. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  69. $sql= "DELETE FROM $table_access_url WHERE id = ".$id;
  70. $result = Database::query($sql);
  71. return $result;
  72. }
  73. /**
  74. * @param string $url
  75. * @return int
  76. */
  77. public static function url_exist($url)
  78. {
  79. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  80. $sql = "SELECT id FROM $table_access_url WHERE url = '".Database::escape_string($url)."' ";
  81. $res = Database::query($sql);
  82. $num = Database::num_rows($res);
  83. return $num;
  84. }
  85. /**
  86. * @param string $url
  87. * @return int
  88. */
  89. public static function url_id_exist($url)
  90. {
  91. if (empty($url)) { return false; }
  92. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  93. $sql = "SELECT id FROM $table_access_url WHERE id = ".intval($url)."";
  94. $res = Database::query($sql);
  95. $num = Database::num_rows($res);
  96. return $num;
  97. }
  98. /**
  99. * This function get the quantity of URLs
  100. * @author Julio Montoya
  101. * @return int count of urls
  102. * */
  103. public static function url_count()
  104. {
  105. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  106. $sql = "SELECT count(id) as count_result FROM $table_access_url";
  107. $res = Database::query($sql);
  108. $url = Database::fetch_array($res,'ASSOC');
  109. $result = $url['count_result'];
  110. return $result;
  111. }
  112. /**
  113. * Gets the id, url, description, and active status of ALL URLs
  114. * @author Julio Montoya
  115. * @return array
  116. * */
  117. public static function get_url_data()
  118. {
  119. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  120. $sql = "SELECT id, url, description, active FROM $table_access_url ORDER BY id";
  121. $res = Database::query($sql);
  122. $urls = array ();
  123. while ($url = Database::fetch_array($res)) {
  124. $urls[] = $url;
  125. }
  126. return $urls;
  127. }
  128. /**
  129. * Gets the id, url, description, and active status of ALL URLs
  130. * @author Julio Montoya
  131. * @param int $url_id
  132. * @return array
  133. * */
  134. public static function get_url_data_from_id($url_id)
  135. {
  136. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  137. $sql = "SELECT id, url, description, active FROM $table_access_url WHERE id = ".intval($url_id);
  138. $res = Database::query($sql);
  139. $row = Database::fetch_array($res);
  140. return $row;
  141. }
  142. /**
  143. * Gets the inner join of users and urls table
  144. * @author Julio Montoya
  145. * @param int access url id
  146. * @param string $order_by
  147. * @return array Database::store_result of the result
  148. **/
  149. public static function get_url_rel_user_data($access_url_id = null, $order_by = null)
  150. {
  151. $where = '';
  152. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  153. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  154. if (!empty($access_url_id)) {
  155. $where = "WHERE $table_url_rel_user.access_url_id = ".intval($access_url_id);
  156. }
  157. if (empty($order_by)) {
  158. $order_clause = api_sort_by_first_name(
  159. ) ? ' ORDER BY firstname, lastname, username' : ' ORDER BY lastname, firstname, username';
  160. } else {
  161. $order_clause = $order_by;
  162. }
  163. $sql = "SELECT u.user_id, lastname, firstname, username, official_code, access_url_id
  164. FROM $tbl_user u
  165. INNER JOIN $table_url_rel_user
  166. ON $table_url_rel_user.user_id = u.user_id
  167. $where $order_clause";
  168. $result = Database::query($sql);
  169. $users = Database::store_result($result);
  170. return $users;
  171. }
  172. /**
  173. * Gets the inner join of access_url and the course table
  174. *
  175. * @author Julio Montoya
  176. * @param int access url id
  177. * @return array Database::store_result of the result
  178. **/
  179. public static function get_url_rel_course_data($access_url_id = null)
  180. {
  181. $where ='';
  182. $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  183. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  184. if (!empty($access_url_id)) {
  185. $where ="WHERE $table_url_rel_course.access_url_id = ".intval($access_url_id);
  186. }
  187. $sql = "SELECT course_code, title, access_url_id
  188. FROM $tbl_course u
  189. INNER JOIN $table_url_rel_course
  190. ON $table_url_rel_course.course_code = code
  191. $where
  192. ORDER BY title, code";
  193. $result=Database::query($sql);
  194. $courses=Database::store_result($result);
  195. return $courses;
  196. }
  197. /**
  198. * Gets the number of rows with a specific course_code in access_url_rel_course table
  199. * @author Yoselyn Castillo
  200. * @param string code
  201. * @return int Database::num_rows($res);
  202. *
  203. **/
  204. public static function getCountUrlRelCourse($code)
  205. {
  206. $code = Database::escape_string($code);
  207. $tableUrlRelCourse = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  208. $sql = "SELECT *
  209. FROM $tableUrlRelCourse
  210. WHERE $tableUrlRelCourse.course_code = '$code'";
  211. $res = Database::query($sql);
  212. return Database::num_rows($res);
  213. }
  214. /**
  215. * Gets the inner join of access_url and the session table
  216. * @author Julio Montoya
  217. * @param int access url id
  218. * @return array Database::store_result of the result
  219. *
  220. **/
  221. public static function get_url_rel_session_data($access_url_id = null)
  222. {
  223. $where ='';
  224. $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  225. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  226. if (!empty($access_url_id))
  227. $where ="WHERE $table_url_rel_session.access_url_id = ".intval($access_url_id);
  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. $result=Database::query($sql);
  235. $sessions=Database::store_result($result);
  236. return $sessions;
  237. }
  238. /**
  239. * Gets the inner join of access_url and the usergroup table
  240. *
  241. * @author Julio Montoya
  242. * @param int access url id
  243. * @return array Database::store_result of the result
  244. **/
  245. public static function get_url_rel_usergroup_data($access_url_id = null)
  246. {
  247. $where = '';
  248. $table_url_rel_usergroup = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
  249. $table_user_group = Database::get_main_table(TABLE_USERGROUP);
  250. if (!empty($access_url_id)) {
  251. $where ="WHERE $table_url_rel_usergroup.access_url_id = ".intval($access_url_id);
  252. }
  253. $sql = "SELECT id, name, access_url_id
  254. FROM $table_user_group u
  255. INNER JOIN $table_url_rel_usergroup
  256. ON $table_url_rel_usergroup.usergroup_id = u.id
  257. $where
  258. ORDER BY name";
  259. $result = Database::query($sql);
  260. $courses = Database::store_result($result);
  261. return $courses;
  262. }
  263. /**
  264. * Gets the inner join of access_url and the usergroup table
  265. *
  266. * @author Julio Montoya
  267. * @param int access url id
  268. * @return array Database::store_result of the result
  269. **/
  270. public static function getUrlRelCourseCategory($access_url_id = null)
  271. {
  272. $table_url_rel = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
  273. $table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  274. $where = " WHERE 1=1 ";
  275. if (!empty($access_url_id)) {
  276. $where .= " AND $table_url_rel.access_url_id = ".intval($access_url_id);
  277. }
  278. $where .= " AND (parent_id IS NULL) ";
  279. $sql = "SELECT id, name, access_url_id
  280. FROM $table u
  281. INNER JOIN $table_url_rel
  282. ON $table_url_rel.course_category_id = u.id
  283. $where
  284. ORDER BY name";
  285. $result = Database::query($sql);
  286. $courses = Database::store_result($result, 'ASSOC');
  287. return $courses;
  288. }
  289. /**
  290. * Sets the status of an URL 1 or 0
  291. * @author Julio Montoya
  292. * @param string lock || unlock
  293. * @param int url id
  294. * */
  295. public static function set_url_status($status, $url_id)
  296. {
  297. $url_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  298. if ($status == 'lock') {
  299. $status_db = '0';
  300. }
  301. if ($status == 'unlock') {
  302. $status_db = '1';
  303. }
  304. if (($status_db == '1' OR $status_db == '0') AND is_numeric($url_id)) {
  305. $sql = "UPDATE $url_table SET active='".intval(
  306. $status_db
  307. )."' WHERE id='".intval($url_id)."'";
  308. $result = Database::query($sql);
  309. }
  310. }
  311. /**
  312. * Checks the relationship between an URL and a User (return the num_rows)
  313. * @author Julio Montoya
  314. * @param int user id
  315. * @param int url id
  316. * @return boolean true if success
  317. * */
  318. public static function relation_url_user_exist($user_id, $url_id)
  319. {
  320. $table_url_rel_user= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  321. $sql= "SELECT user_id FROM $table_url_rel_user
  322. WHERE access_url_id = ".intval($url_id)." AND user_id = ".intval($user_id)." ";
  323. $result = Database::query($sql);
  324. $num = Database::num_rows($result);
  325. return $num;
  326. }
  327. /**
  328. * Checks the relationship between an URL and a Course (return the num_rows)
  329. * @author Julio Montoya
  330. * @param int user id
  331. * @param int url id
  332. * @return boolean true if success
  333. * */
  334. public static function relation_url_course_exist($course_id, $url_id)
  335. {
  336. $table_url_rel_course= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  337. $sql= "SELECT course_code FROM $table_url_rel_course
  338. WHERE access_url_id = ".intval($url_id)." AND
  339. course_code = '".Database::escape_string($course_id)."'";
  340. $result = Database::query($sql);
  341. $num = Database::num_rows($result);
  342. return $num;
  343. }
  344. /**
  345. * Checks the relationship between an URL and a UserGr
  346. * oup (return the num_rows)
  347. * @author Julio Montoya
  348. * @param int $userGroupId
  349. * @param int $urlId
  350. * @return boolean true if success
  351. * */
  352. public static function relation_url_usergroup_exist($userGroupId, $urlId)
  353. {
  354. $table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
  355. $sql= "SELECT usergroup_id FROM $table
  356. WHERE access_url_id = ".intval($urlId)." AND
  357. usergroup_id = ".intval($userGroupId);
  358. $result = Database::query($sql);
  359. $num = Database::num_rows($result);
  360. return $num;
  361. }
  362. /**
  363. * Checks the relationship between an URL and a Session (return the num_rows)
  364. * @author Julio Montoya
  365. * @param int user id
  366. * @param int url id
  367. * @return boolean true if success
  368. * */
  369. public static function relation_url_session_exist($session_id, $url_id)
  370. {
  371. $table_url_rel_session= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  372. $session_id = intval($session_id);
  373. $url_id = intval($url_id);
  374. $sql= "SELECT session_id FROM $table_url_rel_session WHERE access_url_id = ".intval($url_id)." AND session_id = ".Database::escape_string($session_id);
  375. $result = Database::query($sql);
  376. $num = Database::num_rows($result);
  377. return $num;
  378. }
  379. /**
  380. * Add a group of users into a group of URLs
  381. * @author Julio Montoya
  382. * @param array of user_ids
  383. * @param array of url_ids
  384. * @return array
  385. * */
  386. public static function add_users_to_urls($user_list, $url_list)
  387. {
  388. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  389. $result_array = array();
  390. if (is_array($user_list) && is_array($url_list)){
  391. foreach ($url_list as $url_id) {
  392. foreach ($user_list as $user_id) {
  393. $count = UrlManager::relation_url_user_exist($user_id,$url_id);
  394. if ($count==0) {
  395. $sql = "INSERT INTO $table_url_rel_user
  396. SET user_id = ".intval($user_id).", access_url_id = ".intval($url_id);
  397. $result = Database::query($sql);
  398. if ($result) {
  399. $result_array[$url_id][$user_id] = 1;
  400. } else {
  401. $result_array[$url_id][$user_id] = 0;
  402. }
  403. }
  404. }
  405. }
  406. }
  407. return $result_array;
  408. }
  409. /**
  410. * Add a group of courses into a group of URLs
  411. * @author Julio Montoya
  412. * @param array of course ids
  413. * @param array of url_ids
  414. * @return array
  415. **/
  416. public static function add_courses_to_urls($course_list,$url_list)
  417. {
  418. $table_url_rel_course= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  419. $result_array = array();
  420. if (is_array($course_list) && is_array($url_list)){
  421. foreach ($url_list as $url_id) {
  422. foreach ($course_list as $course_code) {
  423. $count = self::relation_url_course_exist($course_code,$url_id);
  424. if ($count==0) {
  425. $sql = "INSERT INTO $table_url_rel_course
  426. SET course_code = '".Database::escape_string($course_code)."', access_url_id = ".intval($url_id);
  427. $result = Database::query($sql);
  428. if($result)
  429. $result_array[$url_id][$course_code]=1;
  430. else
  431. $result_array[$url_id][$course_code]=0;
  432. }
  433. }
  434. }
  435. }
  436. return $result_array;
  437. }
  438. /**
  439. * Add a group of user group into a group of URLs
  440. * @author Julio Montoya
  441. * @param array of course ids
  442. * @param array of url_ids
  443. * @return array
  444. **/
  445. public static function addUserGroupListToUrl($userGroupList, $urlList)
  446. {
  447. $resultArray = array();
  448. if (is_array($userGroupList) && is_array($urlList)) {
  449. foreach ($urlList as $urlId) {
  450. foreach ($userGroupList as $userGroupId) {
  451. $count = self::relation_url_usergroup_exist($userGroupId, $urlId);
  452. if ($count == 0) {
  453. $result = self::addUserGroupToUrl($userGroupId, $urlId);
  454. if ($result) {
  455. $resultArray[$urlId][$userGroupId] = 1;
  456. } else {
  457. $resultArray[$urlId][$userGroupId] = 0;
  458. }
  459. }
  460. }
  461. }
  462. }
  463. return $resultArray;
  464. }
  465. /**
  466. * Add a group of user group into a group of URLs
  467. * @author Julio Montoya
  468. * @param array of course ids
  469. * @param array of url_ids
  470. * @return array
  471. **/
  472. public static function addCourseCategoryListToUrl($courseCategoryList, $urlList)
  473. {
  474. $resultArray = array();
  475. if (is_array($courseCategoryList) && is_array($urlList)) {
  476. foreach ($urlList as $urlId) {
  477. foreach ($courseCategoryList as $categoryCourseId) {
  478. $count = self::relationUrlCourseCategoryExist($categoryCourseId, $urlId);
  479. if ($count == 0) {
  480. $result = self::addCourseCategoryToUrl($categoryCourseId, $urlId);
  481. if ($result) {
  482. $resultArray[$urlId][$categoryCourseId] = 1;
  483. } else {
  484. $resultArray[$urlId][$categoryCourseId] = 0;
  485. }
  486. }
  487. }
  488. }
  489. }
  490. return $resultArray;
  491. }
  492. /**
  493. * Checks the relationship between an URL and a UserGr
  494. * oup (return the num_rows)
  495. * @author Julio Montoya
  496. * @param int $categoryCourseId
  497. * @param int $urlId
  498. * @return boolean true if success
  499. * */
  500. public static function relationUrlCourseCategoryExist($categoryCourseId, $urlId)
  501. {
  502. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
  503. $sql= "SELECT course_category_id FROM $table
  504. WHERE access_url_id = ".intval($urlId)." AND
  505. course_category_id = ".intval($categoryCourseId);
  506. $result = Database::query($sql);
  507. $num = Database::num_rows($result);
  508. return $num;
  509. }
  510. /**
  511. * @param int $userGroupId
  512. * @param int $urlId
  513. * @return int
  514. */
  515. public static function addUserGroupToUrl($userGroupId, $urlId)
  516. {
  517. $urlRelUserGroupTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
  518. $sql = "INSERT INTO $urlRelUserGroupTable
  519. SET
  520. usergroup_id = '".intval($userGroupId)."',
  521. access_url_id = ".intval($urlId);
  522. Database::query($sql);
  523. return Database::insert_id();
  524. }
  525. /**
  526. * @param int $categoryId
  527. * @param int $urlId
  528. * @return int
  529. */
  530. public static function addCourseCategoryToUrl($categoryId, $urlId)
  531. {
  532. $exists = self::relationUrlCourseCategoryExist($categoryId, $urlId);
  533. if (empty($exists)) {
  534. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
  535. $sql = "INSERT INTO $table
  536. SET
  537. course_category_id = '".intval($categoryId)."',
  538. access_url_id = ".intval($urlId);
  539. Database::query($sql);
  540. return Database::insert_id();
  541. }
  542. return 0;
  543. }
  544. /**
  545. * Add a group of sessions into a group of URLs
  546. * @author Julio Montoya
  547. * @param array of session ids
  548. * @param array of url_ids
  549. * @return array
  550. * */
  551. public static function add_sessions_to_urls($session_list, $url_list)
  552. {
  553. $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  554. $result_array = array();
  555. if (is_array($session_list) && is_array($url_list)) {
  556. foreach ($url_list as $url_id) {
  557. foreach ($session_list as $session_id) {
  558. $count = UrlManager::relation_url_session_exist($session_id, $url_id);
  559. if ($count == 0) {
  560. $sql = "INSERT INTO $table_url_rel_session
  561. SET session_id = ".intval(
  562. $session_id
  563. ).", access_url_id = ".intval($url_id);
  564. $result = Database::query($sql);
  565. if ($result) {
  566. $result_array[$url_id][$session_id] = 1;
  567. } else {
  568. $result_array[$url_id][$session_id] = 0;
  569. }
  570. }
  571. }
  572. }
  573. }
  574. return $result_array;
  575. }
  576. /**
  577. * Add a user into a url
  578. * @author Julio Montoya
  579. * @param user_id
  580. * @param url_id
  581. * @return boolean true if success
  582. * */
  583. public static function add_user_to_url($user_id, $url_id = 1)
  584. {
  585. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  586. if (empty($url_id)) {
  587. $url_id = 1;
  588. }
  589. $count = UrlManager::relation_url_user_exist($user_id, $url_id);
  590. $result = true;
  591. if (empty($count)) {
  592. $sql = "INSERT INTO $table_url_rel_user (user_id, access_url_id) VALUES ('".intval($user_id)."', '".intval($url_id)."') ";
  593. $result = Database::query($sql);
  594. }
  595. return $result;
  596. }
  597. /**
  598. * @param string $course_code
  599. * @param int $url_id
  600. * @return resource
  601. */
  602. public static function add_course_to_url($course_code, $url_id=1)
  603. {
  604. $table_url_rel_course= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  605. if (empty($url_id)) {
  606. $url_id = 1;
  607. }
  608. $count = UrlManager::relation_url_course_exist($course_code,$url_id);
  609. if (empty($count)) {
  610. $sql = "INSERT INTO $table_url_rel_course
  611. SET course_code = '".Database::escape_string($course_code)."', access_url_id = ".intval($url_id);
  612. $result = Database::query($sql);
  613. }
  614. return $result;
  615. }
  616. /**
  617. * Inserts a session to a URL (access_url_rel_session table)
  618. * @param int Session ID
  619. * @param int URL ID
  620. * @return bool True on success, false session already exists or insert failed
  621. */
  622. public static function add_session_to_url($session_id, $url_id = 1)
  623. {
  624. $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  625. if (empty($url_id)) {
  626. $url_id = 1;
  627. }
  628. $result = false;
  629. $count = UrlManager::relation_url_session_exist($session_id, $url_id);
  630. $session_id = intval($session_id);
  631. if (empty($count) && !empty($session_id)) {
  632. $url_id = intval($url_id);
  633. $sql = "INSERT INTO $table_url_rel_session
  634. SET session_id = ".intval($session_id).", access_url_id = ".intval($url_id);
  635. $result = Database::query($sql);
  636. }
  637. return $result;
  638. }
  639. /**
  640. * Deletes an url and user relationship
  641. * @author Julio Montoya
  642. * @param int user id
  643. * @param int url id
  644. * @return boolean true if success
  645. * */
  646. public static function delete_url_rel_user($user_id, $url_id)
  647. {
  648. $table_url_rel_user= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  649. $result = true;
  650. if (!empty($user_id) && !empty($url_id)) {
  651. $sql= "DELETE FROM $table_url_rel_user
  652. WHERE user_id = ".intval($user_id)." AND access_url_id = ".intval($url_id);
  653. $result = Database::query($sql);
  654. }
  655. return $result;
  656. }
  657. /**
  658. * Deletes an url and course relationship
  659. * @author Julio Montoya
  660. * @param char course code
  661. * @param int url id
  662. * @return boolean true if success
  663. * */
  664. public static function delete_url_rel_course($course_code, $url_id)
  665. {
  666. $table_url_rel_course= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  667. $sql= "DELETE FROM $table_url_rel_course WHERE course_code = '".Database::escape_string($course_code)."' AND access_url_id=".intval($url_id)." ";
  668. $result = Database::query($sql);
  669. return $result;
  670. }
  671. /**
  672. * Deletes an url and $userGroup relationship
  673. * @author Julio Montoya
  674. * @param int $userGroupId
  675. * @param int $urlId
  676. * @return boolean true if success
  677. * */
  678. public static function delete_url_rel_usergroup($userGroupId, $urlId)
  679. {
  680. $table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
  681. $sql= "DELETE FROM $table
  682. WHERE usergroup_id = '".intval($userGroupId)."' AND
  683. access_url_id=".intval($urlId)." ";
  684. $result = Database::query($sql);
  685. return $result;
  686. }
  687. /**
  688. * Deletes an url and $userGroup relationship
  689. * @author Julio Montoya
  690. * @param int $userGroupId
  691. * @param int $urlId
  692. * @return boolean true if success
  693. * */
  694. public static function deleteUrlRelCourseCategory($userGroupId, $urlId)
  695. {
  696. $table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
  697. $sql= "DELETE FROM $table
  698. WHERE course_category_id = '".intval($userGroupId)."' AND
  699. access_url_id=".intval($urlId)." ";
  700. $result = Database::query($sql);
  701. return $result;
  702. }
  703. /**
  704. * Deletes an url and session relationship
  705. * @author Julio Montoya
  706. * @param char course code
  707. * @param int url id
  708. * @return boolean true if success
  709. * */
  710. public static function delete_url_rel_session($session_id, $url_id)
  711. {
  712. $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  713. $sql= "DELETE FROM $table_url_rel_session
  714. WHERE session_id = ".intval($session_id)." AND access_url_id=".intval($url_id)." ";
  715. $result = Database::query($sql,'ASSOC');
  716. return $result;
  717. }
  718. /**
  719. * Updates the access_url_rel_user table with a given user list
  720. * @author Julio Montoya
  721. * @param array user list
  722. * @param int access_url_id
  723. * */
  724. public static function update_urls_rel_user($user_list, $access_url_id)
  725. {
  726. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  727. $sql = "SELECT user_id FROM $table_url_rel_user WHERE access_url_id = ".intval($access_url_id);
  728. $result = Database::query($sql);
  729. $existing_users = array();
  730. //Getting all users
  731. while ($row = Database::fetch_array($result)) {
  732. $existing_users[] = $row['user_id'];
  733. }
  734. // Adding users
  735. $users_added = array();
  736. foreach ($user_list as $user_id_to_add) {
  737. if (!in_array($user_id_to_add, $existing_users)) {
  738. $result = UrlManager::add_user_to_url($user_id_to_add, $access_url_id);
  739. if ($result) {
  740. $users_added[] = $user_id_to_add;
  741. }
  742. }
  743. }
  744. $users_deleted = array();
  745. // Deleting old users
  746. foreach ($existing_users as $user_id_to_delete) {
  747. if (!in_array($user_id_to_delete, $user_list)) {
  748. $result = UrlManager::delete_url_rel_user($user_id_to_delete, $access_url_id);
  749. if ($result) {
  750. $users_deleted[] = $user_id_to_delete;
  751. }
  752. }
  753. }
  754. if (empty($users_added) && empty($users_deleted)) {
  755. return false;
  756. }
  757. return array('users_added' => $users_added, 'users_deleted' => $users_deleted);
  758. }
  759. /**
  760. * Updates the access_url_rel_course table with a given user list
  761. * @author Julio Montoya
  762. * @param array user list
  763. * @param int access_url_id
  764. * */
  765. public static function update_urls_rel_course($course_list,$access_url_id)
  766. {
  767. $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  768. $sql = "SELECT course_code FROM $table_url_rel_course WHERE access_url_id=".intval($access_url_id);
  769. $result = Database::query($sql);
  770. $existing_courses = array();
  771. while($row = Database::fetch_array($result)){
  772. $existing_courses[] = $row['course_code'];
  773. }
  774. // Adding courses
  775. foreach ($course_list as $course_code) {
  776. if(!in_array($course_code, $existing_courses)) {
  777. UrlManager::add_course_to_url($course_code, $access_url_id);
  778. $course_info = api_get_course_info($course_code);
  779. CourseManager::update_course_ranking($course_info['real_id'], 0, $access_url_id);
  780. }
  781. }
  782. // Deleting old courses
  783. foreach ($existing_courses as $existing_course) {
  784. if(!in_array($existing_course, $course_list)) {
  785. UrlManager::delete_url_rel_course($existing_course,$access_url_id);
  786. $course_info = api_get_course_info($existing_course);
  787. CourseManager::update_course_ranking($course_info['real_id'], 0, $access_url_id);
  788. }
  789. }
  790. }
  791. /**
  792. * Updates the access_url_rel_course table with a given user list
  793. * @author Julio Montoya
  794. * @param array user list
  795. * @param int access_url_id
  796. * */
  797. public static function update_urls_rel_usergroup($userGroupList, $urlId)
  798. {
  799. $table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
  800. $sql = "SELECT usergroup_id FROM $table WHERE access_url_id = ".intval($urlId);
  801. $result = Database::query($sql);
  802. $existingItems = array();
  803. while ($row = Database::fetch_array($result)){
  804. $existingItems[] = $row['usergroup_id'];
  805. }
  806. // Adding
  807. foreach ($userGroupList as $userGroupId) {
  808. if (!in_array($userGroupId, $existingItems)) {
  809. UrlManager::addUserGroupToUrl($userGroupId, $urlId);
  810. }
  811. }
  812. // Deleting old items
  813. foreach ($existingItems as $userGroupId) {
  814. if (!in_array($userGroupId, $userGroupList)) {
  815. UrlManager::delete_url_rel_usergroup($userGroupId, $urlId);
  816. }
  817. }
  818. }
  819. /**
  820. * Updates the access_url_rel_course_category table with a given list
  821. * @author Julio Montoya
  822. * @param array course category list
  823. * @param int access_url_id
  824. **/
  825. public static function updateUrlRelCourseCategory($list, $urlId)
  826. {
  827. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE_CATEGORY);
  828. $sql = "SELECT course_category_id FROM $table WHERE access_url_id = ".intval($urlId);
  829. $result = Database::query($sql);
  830. $existingItems = array();
  831. while ($row = Database::fetch_array($result)){
  832. $existingItems[] = $row['course_category_id'];
  833. }
  834. // Adding
  835. foreach ($list as $id) {
  836. UrlManager::addCourseCategoryToUrl($id, $urlId);
  837. $categoryInfo = getCategoryById($id);
  838. $children = getChildren($categoryInfo['code']);
  839. if (!empty($children)) {
  840. foreach ($children as $category) {
  841. UrlManager::addCourseCategoryToUrl($category['id'], $urlId);
  842. }
  843. }
  844. }
  845. // Deleting old items
  846. foreach ($existingItems as $id) {
  847. if (!in_array($id, $list)) {
  848. UrlManager::deleteUrlRelCourseCategory($id, $urlId);
  849. $categoryInfo = getCategoryById($id);
  850. $children = getChildren($categoryInfo['code']);
  851. if (!empty($children)) {
  852. foreach ($children as $category) {
  853. UrlManager::deleteUrlRelCourseCategory($category['id'], $urlId);
  854. }
  855. }
  856. }
  857. }
  858. }
  859. /**
  860. * Updates the access_url_rel_session table with a given user list
  861. * @author Julio Montoya
  862. * @param array user list
  863. * @param int access_url_id
  864. * */
  865. public static function update_urls_rel_session($session_list,$access_url_id)
  866. {
  867. $table_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  868. $sql = "SELECT session_id FROM $table_url_rel_session WHERE access_url_id=".intval($access_url_id);
  869. $result = Database::query($sql);
  870. $existing_sessions = array();
  871. while ($row = Database::fetch_array($result)){
  872. $existing_sessions[] = $row['session_id'];
  873. }
  874. // Adding users
  875. foreach ($session_list as $session) {
  876. if (!in_array($session, $existing_sessions)) {
  877. if (!empty($session) && !empty($access_url_id)) {
  878. UrlManager::add_session_to_url($session, $access_url_id);
  879. }
  880. }
  881. }
  882. // Deleting old users
  883. foreach ($existing_sessions as $existing_session) {
  884. if (!in_array($existing_session, $session_list)) {
  885. if (!empty($existing_session) && !empty($access_url_id)) {
  886. UrlManager::delete_url_rel_session($existing_session, $access_url_id);
  887. }
  888. }
  889. }
  890. }
  891. /**
  892. * @param int $user_id
  893. * @return array
  894. */
  895. public static function get_access_url_from_user($user_id)
  896. {
  897. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  898. $table_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  899. $sql = "SELECT url, access_url_id FROM $table_url_rel_user url_rel_user INNER JOIN $table_url u
  900. ON (url_rel_user.access_url_id = u.id)
  901. WHERE user_id = ".intval($user_id);
  902. $result = Database::query($sql);
  903. $url_list = Database::store_result($result,'ASSOC');
  904. return $url_list;
  905. }
  906. /**
  907. * @param $session_id
  908. * @return array
  909. */
  910. public static function get_access_url_from_session($session_id)
  911. {
  912. $table_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  913. $table_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  914. $sql = "SELECT url, access_url_id FROM $table_url_rel_session url_rel_session INNER JOIN $table_url u
  915. ON (url_rel_session.access_url_id = u.id)
  916. WHERE session_id = ".intval($session_id);
  917. $result = Database::query($sql);
  918. $url_list = Database::store_result($result);
  919. return $url_list;
  920. }
  921. /**
  922. * @param string $url
  923. * @return bool|mixed|null
  924. */
  925. public static function get_url_id($url)
  926. {
  927. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  928. $sql = "SELECT id FROM $table_access_url WHERE url = '".Database::escape_string($url)."'";
  929. $result = Database::query($sql);
  930. $access_url_id = Database::result($result, 0, 0);
  931. return $access_url_id;
  932. }
  933. /**
  934. *
  935. * @param string $needle
  936. * @return XajaxResponse
  937. */
  938. public static function searchCourseCategoryAjax($needle)
  939. {
  940. $response = new XajaxResponse();
  941. $return = '';
  942. if (!empty($needle)) {
  943. // xajax send utf8 datas... datas in db can be non-utf8 datas
  944. $charset = api_get_system_encoding();
  945. $needle = api_convert_encoding($needle, $charset, 'utf-8');
  946. $needle = Database::escape_string($needle);
  947. // search courses where username or firstname or lastname begins likes $needle
  948. $sql = 'SELECT id, name FROM '.Database::get_main_table(TABLE_MAIN_CATEGORY).' u
  949. WHERE name LIKE "'.$needle.'%" AND (parent_id IS NULL or parent_id = 0)
  950. ORDER BY name
  951. LIMIT 11';
  952. $result = Database::query($sql);
  953. $i = 0;
  954. while ($data = Database::fetch_array($result)) {
  955. $i++;
  956. if ($i <= 10) {
  957. $return .= '<a
  958. href="javascript: void(0);"
  959. onclick="javascript: add_user_to_url(\''.addslashes($data['id']).'\',\''.addslashes($data['name']).' \')">'.$data['name'].' </a><br />';
  960. } else {
  961. $return .= '...<br />';
  962. }
  963. }
  964. }
  965. $response->addAssign('ajax_list_courses', 'innerHTML', api_utf8_encode($return));
  966. return $response;
  967. }
  968. }