urlmanager.lib.php 40 KB

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