urlmanager.lib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2009 Dokeos SPRL
  6. Copyright (c) 2009 Julio Montoya Armas <gugli100@gmail.com>
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium, info@dokeos.com
  15. ==============================================================================
  16. */
  17. /**
  18. ==============================================================================
  19. * This library provides functions for the URL management.
  20. * Include/require it in your code to use its functionality.
  21. *
  22. * @package dokeos.library
  23. ==============================================================================
  24. */
  25. class UrlManager
  26. {
  27. /**
  28. * Creates a new url access to Dokeos
  29. *
  30. * @author Julio Montoya <gugli100@gmail.com>,
  31. *
  32. * @param string The URL of the site
  33. * @param string The description of the site
  34. * @param int is active or not
  35. * @param int the user_id of the owner
  36. * @return boolean if success
  37. */
  38. function add($url, $description, $active)
  39. {
  40. $tms = time();
  41. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  42. $sql = "INSERT INTO $table_access_url
  43. SET url = '".Database::escape_string($url)."/',
  44. description = '".Database::escape_string($description)."',
  45. active = '".Database::escape_string($active)."',
  46. created_by = '".Database::escape_string(api_get_user_id())."',
  47. tms = FROM_UNIXTIME(".$tms.")";
  48. $result = api_sql_query($sql, __FILE__, __LINE__);
  49. return $result;
  50. }
  51. /**
  52. * Updates an URL access to Dokeos
  53. * @author Julio Montoya <gugli100@gmail.com>,
  54. *
  55. * @param int The url id
  56. * @param string The description of the site
  57. * @param int is active or not
  58. * @param int the user_id of the owner
  59. * @return boolean if success
  60. */
  61. function udpate($url_id, $url, $description, $active)
  62. {
  63. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  64. $tms = time();
  65. $sql = "UPDATE $table_access_url
  66. SET url = '".Database::escape_string($url)."',
  67. description = '".Database::escape_string($description)."',
  68. active = '".Database::escape_string($active)."',
  69. created_by = '".Database::escape_string(api_get_user_id())."',
  70. tms = FROM_UNIXTIME(".$tms.")
  71. WHERE id = '$url_id'";
  72. $result = api_sql_query($sql, __FILE__, __LINE__);
  73. return $result;
  74. }
  75. /**
  76. *
  77. * */
  78. function url_exist($url)
  79. {
  80. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  81. $sql = "SELECT id FROM $table_access_url WHERE url = '".Database::escape_string($url)."' ";
  82. $res = api_sql_query($sql,__FILE__,__LINE__);
  83. $num = Database::num_rows($res);
  84. return $num;
  85. }
  86. /**
  87. *
  88. * */
  89. function url_id_exist($url)
  90. {
  91. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  92. $sql = "SELECT id FROM $table_access_url WHERE id = '".Database::escape_string($url)."' ";
  93. $res = api_sql_query($sql,__FILE__,__LINE__);
  94. $num = Database::num_rows($res);
  95. return $num;
  96. }
  97. /**
  98. * This function get the quantity of URL
  99. * @author Julio Montoya
  100. * @return int count of urls
  101. * */
  102. function url_count()
  103. {
  104. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  105. $sql = "SELECT count(id) as count_result FROM $table_access_url";
  106. $res = api_sql_query($sql, __FILE__, __LINE__);
  107. $url = Database::fetch_row($res);
  108. $result = $url['0'];
  109. return $result;
  110. }
  111. /**
  112. * Gets the id, url, description, and active status of ALL URLs
  113. * @author Julio Montoya
  114. * @return array
  115. * */
  116. function get_url_data()
  117. {
  118. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  119. $sql = "SELECT id, url, description, active FROM $table_access_url";
  120. $res = api_sql_query($sql, __FILE__, __LINE__);
  121. $urls = array ();
  122. while ($url = Database::fetch_row($res))
  123. {
  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. * @return array
  132. * */
  133. function get_url_data_from_id($url_id)
  134. {
  135. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  136. $sql = "SELECT id, url, description, active FROM $table_access_url WHERE id = ".Database::escape_string($url_id);
  137. $res = api_sql_query($sql, __FILE__, __LINE__);
  138. $row = Database::fetch_array($res);
  139. return $row;
  140. }
  141. /** Gets the inner join of users and urls table
  142. * @author Julio Montoya
  143. * @return int access url id
  144. * @return array api_store_result of the result
  145. * */
  146. function get_url_rel_user_data($access_url_id='')
  147. {
  148. $where ='';
  149. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  150. $tbl_user = Database :: get_main_table(TABLE_MAIN_USER);
  151. if (!empty($access_url_id))
  152. $where ="WHERE $table_url_rel_user.access_url_id = ".Database::escape_string($access_url_id);
  153. $sql="SELECT u.user_id, lastname, firstname, username, access_url_id
  154. FROM $tbl_user u
  155. INNER JOIN $table_url_rel_user
  156. ON $table_url_rel_user.user_id = u.user_id
  157. $where
  158. ORDER BY lastname,firstname,username";
  159. $result=api_sql_query($sql,__FILE__,__LINE__);
  160. $Users=api_store_result($result);
  161. return $Users;
  162. }
  163. /**
  164. * Sets the status of an URL 1 or 0
  165. * @author Julio Montoya
  166. * @param string lock || unlock
  167. * @param int url id
  168. * */
  169. function set_url_status($status,$url_id)
  170. {
  171. $url_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  172. if ($status=='lock') {
  173. $status_db='0';
  174. }
  175. if ($status=='unlock') {
  176. $status_db='1';
  177. }
  178. if(($status_db=='1' OR $status_db=='0') AND is_numeric($url_id)) {
  179. $sql="UPDATE $url_table SET active='".Database::escape_string($status_db)."' WHERE id='".Database::escape_string($url_id)."'";
  180. $result = api_sql_query($sql, __FILE__, __LINE__);
  181. }
  182. }
  183. /**
  184. * Deletes an url
  185. * @author Julio Montoya
  186. * @param int url id
  187. * @return boolean true if success
  188. * */
  189. function delete($id)
  190. {
  191. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  192. $sql= "DELETE FROM $table_access_url WHERE id = ".Database::escape_string($id);
  193. $result = api_sql_query($sql, __FILE__, __LINE__);
  194. return $result;
  195. }
  196. /**
  197. * Deletes an url
  198. * @author Julio Montoya
  199. * @param int user id
  200. * @param int url id
  201. * @return boolean true if success
  202. * */
  203. function relation_url_user_exist($user_id, $url_id)
  204. {
  205. $table_url_rel_user= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  206. $sql= "SELECT user_id FROM $table_url_rel_user WHERE access_url_id = ".Database::escape_string($url_id)." AND user_id = ".Database::escape_string($user_id)." ";
  207. $result = api_sql_query($sql, __FILE__, __LINE__);
  208. $num = Database::num_rows($result);
  209. return $num;
  210. }
  211. /**
  212. * Add a group of users into a group of URLs
  213. * @author Julio Montoya
  214. * @param array of user_ids
  215. * @param array of url_ids
  216. * */
  217. function add_users_to_urls($user_list,$url_list)
  218. {
  219. $table_url_rel_user= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  220. $result_array=array();
  221. if (is_array($user_list) && is_array($url_list)){
  222. foreach ($url_list as $url_id) {
  223. foreach ($user_list as $user_id) {
  224. $count = UrlManager::relation_url_user_exist($user_id,$url_id);
  225. if ($count==0) {
  226. $sql = "INSERT INTO $table_url_rel_user
  227. SET user_id = ".Database::escape_string($user_id).", access_url_id = ".Database::escape_string($url_id);
  228. $result = api_sql_query($sql, __FILE__, __LINE__);
  229. if($result)
  230. $result_array[$url_id][$user_id]=1;
  231. else
  232. $result_array[$url_id][$user_id]=0;
  233. }
  234. }
  235. }
  236. }
  237. return $result_array;
  238. }
  239. /**
  240. * Add a user into a url
  241. * @author Julio Montoya
  242. * @param user_id
  243. * @param url_id
  244. * @return boolean true if success
  245. * */
  246. function add_user_to_url($user_id,$url_id=1)
  247. {
  248. $table_url_rel_user= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  249. $count = UrlManager::relation_url_user_exist($user_id,$url_id);
  250. if (empty($count)) {
  251. $sql = "INSERT INTO $table_url_rel_user
  252. SET user_id = ".Database::escape_string($user_id).", access_url_id = ".Database::escape_string($url_id);
  253. $result = api_sql_query($sql, __FILE__, __LINE__);
  254. }
  255. return $result;
  256. }
  257. /**
  258. * Deletes an url and user relationship
  259. * @author Julio Montoya
  260. * @param int url id
  261. * @return boolean true if success
  262. * */
  263. function delete_url_rel_user($user_id, $url_id)
  264. {
  265. $table_url_rel_user= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  266. $sql= "DELETE FROM $table_url_rel_user WHERE user_id = ".Database::escape_string($user_id)." AND access_url_id=".Database::escape_string($url_id)." ";
  267. $result = api_sql_query($sql, __FILE__, __LINE__);
  268. return $result;
  269. }
  270. /**
  271. * Updates the url_rel_user table with a given user list
  272. * @author Julio Montoya
  273. * @param array user list
  274. * @param int access_url_id
  275. * */
  276. function update_urls_rel_user($user_list,$access_url_id)
  277. {
  278. $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  279. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  280. $sql = "SELECT user_id FROM $table_url_rel_user WHERE access_url_id=".Database::escape_string($access_url_id);
  281. $result = api_sql_query($sql,__FILE__,__LINE__ );
  282. $existingUsers = array();
  283. while($row = Database::fetch_array($result)){
  284. $existingUsers[] = $row['user_id'];
  285. }
  286. //adding users
  287. foreach($user_list as $enreg_user) {
  288. if(!in_array($enreg_user, $existingUsers)) {
  289. UrlManager::add_user_to_url($enreg_user,$access_url_id);
  290. }
  291. }
  292. //deleting old users
  293. foreach($existingUsers as $existing_user) {
  294. if(!in_array($existing_user, $user_list)) {
  295. UrlManager::delete_url_rel_user($existing_user,$access_url_id);
  296. }
  297. }
  298. }
  299. /**
  300. *
  301. * */
  302. function get_url_id($url)
  303. {
  304. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  305. $sql = "SELECT id FROM $table_access_url WHERE url = '".Database::escape_string($url)."'";
  306. $result = api_sql_query($sql);
  307. $access_url_id = Database::result($result, 0, 0);
  308. return $access_url_id;
  309. }
  310. }
  311. ?>