urlmanager.lib.php 39 KB

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