group_portal_manager.lib.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. <?php
  2. /* For licensing terms, see /dokeos_license.txt */
  3. /**
  4. ==============================================================================
  5. * This library provides functions for the access_url management.
  6. * Include/require it in your code to use its functionality.
  7. * @author Julio Montoya <gugli100@gmail.com>
  8. * @package dokeos.library
  9. ==============================================================================
  10. */
  11. // Group permissions
  12. define('GROUP_PERMISSION_OPEN' , '1');
  13. define('GROUP_PERMISSION_CLOSED', '2');
  14. // Group user permissions
  15. define('GROUP_USER_PERMISSION_ADMIN' ,'1'); // the admin of a group
  16. define('GROUP_USER_PERMISSION_READER' ,'2'); // a normal user
  17. define('GROUP_USER_PERMISSION_PENDING_INVITATION' ,'3'); // user pending invitation to a group
  18. class GroupPortalManager
  19. {
  20. /**
  21. * Creates a new group
  22. *
  23. * @author Julio Montoya <gugli100@gmail.com>,
  24. *
  25. * @param string The URL of the site
  26. * @param string The description of the site
  27. * @param int is active or not
  28. * @param int the user_id of the owner
  29. * @return boolean if success
  30. */
  31. function add($name, $description, $url, $visibility, $picture='')
  32. {
  33. $tms = time();
  34. $table = Database :: get_main_table(TABLE_MAIN_GROUP);
  35. $sql = "INSERT INTO $table
  36. SET name = '".Database::escape_string($name)."',
  37. description = '".Database::escape_string($description)."',
  38. picture_uri = '".Database::escape_string($picture)."',
  39. url = '".Database::escape_string($url)."',
  40. visibility = '".Database::escape_string($visibility)."',
  41. created_on = FROM_UNIXTIME(".$tms."),
  42. updated_on = FROM_UNIXTIME(".$tms.")";
  43. $result = Database::query($sql, __FILE__, __LINE__);
  44. $return = Database::insert_id();
  45. return $return;
  46. }
  47. /**
  48. * Updates a group
  49. * @author Julio Montoya <gugli100@gmail.com>,
  50. *
  51. * @param int The id
  52. * @param string The description of the site
  53. * @param int is active or not
  54. * @param int the user_id of the owner
  55. * @return boolean if success
  56. */
  57. function update($group_id, $name, $description, $url, $visibility, $picture_uri)
  58. {
  59. $group_id = intval($group_id);
  60. $table = Database::get_main_table(TABLE_MAIN_GROUP);
  61. $tms = time();
  62. $sql = "UPDATE $table
  63. SET name = '".Database::escape_string($name)."',
  64. description = '".Database::escape_string($description)."',
  65. picture_uri = '".Database::escape_string($picture_uri)."',
  66. url = '".Database::escape_string($url)."',
  67. visibility = '".Database::escape_string($visibility)."',
  68. updated_on = FROM_UNIXTIME(".$tms.")
  69. WHERE id = '$group_id'";
  70. $result = Database::query($sql, __FILE__, __LINE__);
  71. return $result;
  72. }
  73. /**
  74. * Deletes a group
  75. * @author Julio Montoya
  76. * @param int id
  77. * @return boolean true if success
  78. * */
  79. function delete($id)
  80. {
  81. $id = intval($id);
  82. $table = Database :: get_main_table(TABLE_MAIN_GROUP);
  83. $sql= "DELETE FROM $table WHERE id = ".Database::escape_string($id);
  84. $result = Database::query($sql, __FILE__, __LINE__);
  85. //deleting all relationshop with users and groups
  86. self::delete_users($id);
  87. return $result;
  88. }
  89. /**
  90. *
  91. * */
  92. function url_exist($url)
  93. {
  94. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  95. $sql = "SELECT id FROM $table_access_url WHERE url = '".Database::escape_string($url)."' ";
  96. $res = Database::query($sql,__FILE__,__LINE__);
  97. $num = Database::num_rows($res);
  98. return $num;
  99. }
  100. /**
  101. *
  102. * */
  103. function url_id_exist($url)
  104. {
  105. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  106. $sql = "SELECT id FROM $table_access_url WHERE id = '".Database::escape_string($url)."' ";
  107. $res = Database::query($sql,__FILE__,__LINE__);
  108. $num = Database::num_rows($res);
  109. return $num;
  110. }
  111. /**
  112. * This function get the quantity of URLs
  113. * @author Julio Montoya
  114. * @return int count of urls
  115. * */
  116. function url_count()
  117. {
  118. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  119. $sql = "SELECT count(id) as count_result FROM $table_access_url";
  120. $res = Database::query($sql, __FILE__, __LINE__);
  121. $url = Database::fetch_array($res,'ASSOC');
  122. $result = $url['count_result'];
  123. return $result;
  124. }
  125. /**
  126. * Gets the id, url, description, and active status of ALL URLs
  127. * @author Julio Montoya
  128. * @return array
  129. * */
  130. function get_url_data()
  131. {
  132. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  133. $sql = "SELECT id, url, description, active FROM $table_access_url";
  134. $res = Database::query($sql, __FILE__, __LINE__);
  135. $urls = array ();
  136. while ($url = Database::fetch_array($res)) {
  137. $urls[] = $url;
  138. }
  139. return $urls;
  140. }
  141. /**
  142. * Gets data of all groups
  143. * @author Julio Montoya
  144. * @param int visibility
  145. * @param int from which record the results will begin (use for pagination)
  146. * @param int number of items
  147. * @return array
  148. * */
  149. function get_all_group_data($visibility = GROUP_PERMISSION_OPEN, $from=0, $number_of_items=10)
  150. {
  151. $table = Database :: get_main_table(TABLE_MAIN_GROUP);
  152. $visibility = intval($visibility);
  153. $user_condition = '';
  154. $sql = "SELECT name, description, picture_uri FROM $table WHERE visibility = $visibility ";
  155. $res = Database::query($sql, __FILE__, __LINE__);
  156. $data = array ();
  157. while ($item = Database::fetch_array($res)) {
  158. $data[] = $item;
  159. }
  160. return $data;
  161. }
  162. /**
  163. * Gets the group data
  164. *
  165. *
  166. */
  167. function get_group_data($group_id)
  168. {
  169. $table = Database :: get_main_table(TABLE_MAIN_GROUP);
  170. $group_id = intval($group_id);
  171. $user_condition = '';
  172. $sql = "SELECT name, description, picture_uri, visibility FROM $table WHERE id = $group_id ";
  173. $res = Database::query($sql, __FILE__, __LINE__);
  174. $item = array();
  175. if (Database::num_rows($res)>0) {
  176. $item = Database::fetch_array($res,'ASSOC');
  177. }
  178. return $item;
  179. }
  180. /**
  181. * Gets the tags from a given group
  182. * @param int group id
  183. * @param bool show group links or not
  184. *
  185. */
  186. function get_group_tags($group_id, $show_tag_links = true)
  187. {
  188. $tag = Database :: get_main_table(TABLE_MAIN_TAG);
  189. $table_group_rel_tag = Database :: get_main_table(TABLE_MAIN_GROUP_REL_TAG);
  190. $group_id = intval($group_id);
  191. $user_condition = '';
  192. $sql = "SELECT tag FROM $tag t INNER JOIN $table_group_rel_tag gt ON (gt.tag_id= t.id) WHERE gt.group_id = $group_id ";
  193. $res = Database::query($sql, __FILE__, __LINE__);
  194. $tags = array();
  195. if (Database::num_rows($res)>0) {
  196. while ($row = Database::fetch_array($res,'ASSOC')) {
  197. $tags[] = $row;
  198. }
  199. }
  200. if ($show_tag_links == true) {
  201. if (is_array($tags) && count($tags)>0) {
  202. foreach ($tags as $tag) {
  203. $tag_tmp[] = '<a href="'.api_get_path(WEB_PATH).'main/social/search.php?q='.$tag['tag'].'">'.$tag['tag'].'</a>';
  204. }
  205. if (is_array($tags) && count($tags)>0) {
  206. $tags= implode(', ',$tag_tmp);
  207. }
  208. } else {
  209. $tags = '';
  210. }
  211. }
  212. return $tags;
  213. }
  214. /**
  215. * Gets the id, url, description, and active status of ALL URLs
  216. * @author Julio Montoya
  217. * @return array
  218. * */
  219. function get_url_data_from_id($url_id)
  220. {
  221. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  222. $sql = "SELECT id, url, description, active FROM $table_access_url WHERE id = ".Database::escape_string($url_id);
  223. $res = Database::query($sql, __FILE__, __LINE__);
  224. $row = Database::fetch_array($res);
  225. return $row;
  226. }
  227. /** Gets the inner join of users and group table
  228. * @author Julio Montoya
  229. * @return int access url id
  230. * @return array Database::store_result of the result
  231. * */
  232. function get_groups_by_user($user_id='', $relation_type = GROUP_USER_PERMISSION_READER, $with_image = false)
  233. {
  234. $where = '';
  235. $table_group_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_GROUP);
  236. $tbl_group = Database::get_main_table(TABLE_MAIN_GROUP);
  237. $user_id = intval($user_id);
  238. if ($relation_type == 0) {
  239. $where_relation_condition = '';
  240. } else {
  241. $relation_type = intval($relation_type);
  242. $where_relation_condition = "AND gu.relation_type = $relation_type ";
  243. }
  244. $sql = "SELECT g.picture_uri, g.name, g.description, g.id , gu.relation_type
  245. FROM $tbl_group g
  246. INNER JOIN $table_group_rel_user gu
  247. ON gu.group_id = g.id WHERE gu.user_id = $user_id $where_relation_condition ORDER BY created_on desc ";
  248. $result=Database::query($sql,__FILE__,__LINE__);
  249. $array = array();
  250. while ($row = Database::fetch_array($result, 'ASSOC')) {
  251. if ($with_image == true) {
  252. $picture = self::get_picture_group($row['id'], $row['picture_uri'],80);
  253. $img = '<img src="'.$picture['file'].'" />';
  254. $row['picture_uri'] = $img;
  255. }
  256. $array[$row['id']] = $row;
  257. }
  258. return $array;
  259. }
  260. /** Gets the inner join of users and group table
  261. * @author Julio Montoya
  262. * @return int access url id
  263. * @return array Database::store_result of the result
  264. * */
  265. function get_groups_by_popularity($num = 10, $with_image = false)
  266. {
  267. $where = '';
  268. $table_group_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_GROUP);
  269. $tbl_group = Database::get_main_table(TABLE_MAIN_GROUP);
  270. if (empty($num)) {
  271. $num = 10;
  272. } else {
  273. $num = intval($num);
  274. }
  275. // only show admins and readers
  276. $where_relation_condition = " WHERE gu.relation_type IN ('".GROUP_USER_PERMISSION_ADMIN."' , '".GROUP_USER_PERMISSION_READER."') ";
  277. $sql = "SELECT count(user_id) as count, g.picture_uri, g.name, g.description, g.id
  278. FROM $tbl_group g
  279. INNER JOIN $table_group_rel_user gu
  280. ON gu.group_id = g.id $where_relation_condition GROUP BY g.id ORDER BY count DESC LIMIT $num";
  281. $result=Database::query($sql,__FILE__,__LINE__);
  282. $array = array();
  283. while ($row = Database::fetch_array($result, 'ASSOC')) {
  284. if ($with_image == true) {
  285. $picture = self::get_picture_group($row['id'], $row['picture_uri'],80);
  286. $img = '<img src="'.$picture['file'].'" />';
  287. $row['picture_uri'] = $img;
  288. }
  289. $array[$row['id']] = $row;
  290. }
  291. return $array;
  292. }
  293. /** Gets the last groups created
  294. * @author Julio Montoya
  295. * @return int access url id
  296. * @return array Database::store_result of the result
  297. * */
  298. function get_groups_by_age($num = 10, $with_image = false, $relation_type = GROUP_USER_PERMISSION_READER)
  299. {
  300. $where = '';
  301. $table_group_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_GROUP);
  302. $tbl_group = Database::get_main_table(TABLE_MAIN_GROUP);
  303. if (empty($num)) {
  304. $num = 10;
  305. } else {
  306. $num = intval($num);
  307. }
  308. // only show admins and readers
  309. $where_relation_condition = " WHERE gu.relation_type IN ('".GROUP_USER_PERMISSION_ADMIN."' , '".GROUP_USER_PERMISSION_READER."') ";
  310. $sql = "SELECT g.picture_uri, g.name, g.description, g.id
  311. FROM $tbl_group g
  312. INNER JOIN $table_group_rel_user gu
  313. ON gu.group_id = g.id $where_relation_condition ORDER BY created_on desc LIMIT $num ";
  314. $result=Database::query($sql,__FILE__,__LINE__);
  315. $array = array();
  316. while ($row = Database::fetch_array($result, 'ASSOC')) {
  317. if ($with_image == true) {
  318. $picture = self::get_picture_group($row['id'], $row['picture_uri'],80);
  319. $img = '<img src="'.$picture['file'].'" />';
  320. $row['picture_uri'] = $img;
  321. }
  322. $array[$row['id']] = $row;
  323. }
  324. return $array;
  325. }
  326. function get_users_by_group($group_id='', $with_image = false, $relation_type = 0, $limit = 100)
  327. {
  328. $where = '';
  329. $table_group_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_GROUP);
  330. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  331. $group_id = intval($group_id);
  332. $limit = intval($limit);
  333. if ($relation_type == 0) {
  334. $where_relation_condition = '';
  335. } else {
  336. $relation_type = intval($relation_type);
  337. $where_relation_condition = "AND gu.relation_type = $relation_type ";
  338. }
  339. $sql="SELECT picture_uri, u.user_id, u.firstname, u.lastname, relation_type FROM $tbl_user u
  340. INNER JOIN $table_group_rel_user gu
  341. ON (gu.user_id = u.user_id) WHERE gu.group_id= $group_id $where_relation_condition ORDER BY relation_type, firstname LIMIT $limit";
  342. $result=Database::query($sql,__FILE__,__LINE__);
  343. $array = array();
  344. while ($row = Database::fetch_array($result, 'ASSOC')) {
  345. if ($with_image == true) {
  346. $picture = UserManager::get_picture_user($row['user_id'], $row['picture_uri'],80,'medium_');
  347. $img = '<img src="'.$picture['file'].'" />';
  348. $row['picture_uri'] = $img;
  349. }
  350. $array[$row['user_id']] = $row;
  351. }
  352. return $array;
  353. }
  354. /** Gets the inner join of access_url and the course table
  355. * @author Julio Montoya
  356. * @return int access url id
  357. * @return array Database::store_result of the result
  358. * */
  359. function get_url_rel_course_data($access_url_id='')
  360. {
  361. $where ='';
  362. $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  363. $tbl_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  364. if (!empty($access_url_id))
  365. $where ="WHERE $table_url_rel_course.access_url_id = ".Database::escape_string($access_url_id);
  366. $sql="SELECT course_code, title, access_url_id
  367. FROM $tbl_course u
  368. INNER JOIN $table_url_rel_course
  369. ON $table_url_rel_course.course_code = code
  370. $where
  371. ORDER BY title, code";
  372. $result=Database::query($sql,__FILE__,__LINE__);
  373. $courses=Database::store_result($result);
  374. return $courses;
  375. }
  376. /** Gets the inner join of access_url and the session table
  377. * @author Julio Montoya
  378. * @return int access url id
  379. * @return array Database::store_result of the result
  380. * */
  381. function get_url_rel_session_data($access_url_id='')
  382. {
  383. $where ='';
  384. $table_url_rel_session = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  385. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  386. if (!empty($access_url_id))
  387. $where ="WHERE $table_url_rel_session.access_url_id = ".Database::escape_string($access_url_id);
  388. $sql="SELECT id, name, access_url_id
  389. FROM $tbl_session u
  390. INNER JOIN $table_url_rel_session
  391. ON $table_url_rel_session.session_id = id
  392. $where
  393. ORDER BY name, id";
  394. $result=Database::query($sql,__FILE__,__LINE__);
  395. $sessions=Database::store_result($result);
  396. return $sessions;
  397. }
  398. /**
  399. * Sets the status of an URL 1 or 0
  400. * @author Julio Montoya
  401. * @param string lock || unlock
  402. * @param int url id
  403. * */
  404. function set_url_status($status, $url_id)
  405. {
  406. $url_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  407. if ($status=='lock') {
  408. $status_db='0';
  409. }
  410. if ($status=='unlock') {
  411. $status_db='1';
  412. }
  413. if(($status_db=='1' OR $status_db=='0') AND is_numeric($url_id)) {
  414. $sql="UPDATE $url_table SET active='".Database::escape_string($status_db)."' WHERE id='".Database::escape_string($url_id)."'";
  415. $result = Database::query($sql, __FILE__, __LINE__);
  416. }
  417. }
  418. /**
  419. * Gets the relationship between a group and a User
  420. * @author Julio Montoya
  421. * @param int user id
  422. * @param int group_id
  423. * @return int 0 if there are not relationship otherwise return GROUP_USER_PERMISSION_ADMIN or GROUP_USER_PERMISSION_READER constants
  424. * */
  425. function get_user_group_role($user_id, $group_id)
  426. {
  427. $table_group_rel_user= Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  428. $return_value = 0;
  429. if (!empty($user_id) && !empty($group_id)) {
  430. $sql = "SELECT relation_type FROM $table_group_rel_user WHERE group_id = ".intval($group_id)." AND user_id = ".intval($user_id)." ";
  431. $result = Database::query($sql, __FILE__, __LINE__);
  432. if (Database::num_rows($result)>0) {
  433. $row = Database::fetch_array($result,'ASSOC');
  434. $return_value = $row['relation_type'];
  435. }
  436. }
  437. return $return_value;
  438. }
  439. /**
  440. * Checks the relationship between an URL and a Course (return the num_rows)
  441. * @author Julio Montoya
  442. * @param int user id
  443. * @param int url id
  444. * @return boolean true if success
  445. * */
  446. function relation_url_course_exist($course_id, $url_id)
  447. {
  448. $table_url_rel_course= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  449. $sql= "SELECT course_code FROM $table_url_rel_course WHERE access_url_id = ".Database::escape_string($url_id)." AND course_code = '".Database::escape_string($course_id)."'";
  450. $result = Database::query($sql, __FILE__, __LINE__);
  451. $num = Database::num_rows($result);
  452. return $num;
  453. }
  454. /**
  455. * Add a user into a group
  456. * @author Julio Montoya
  457. * @param user_id
  458. * @param url_id
  459. * @return boolean true if success
  460. * */
  461. function add_user_to_group($user_id, $group_id, $relation_type = GROUP_USER_PERMISSION_READER)
  462. {
  463. $table_url_rel_group = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  464. if (!empty($user_id) && !empty($group_id)) {
  465. $role = self::get_user_group_role($user_id,$group_id);
  466. if ($role==0) {
  467. $sql = "INSERT INTO $table_url_rel_group
  468. SET user_id = ".intval($user_id).", group_id = ".intval($group_id).", relation_type = ".intval($relation_type);
  469. $result = Database::query($sql, __FILE__, __LINE__);
  470. }
  471. }
  472. return $result;
  473. }
  474. /**
  475. * Add a group of users into a group of URLs
  476. * @author Julio Montoya
  477. * @param array of user_ids
  478. * @param array of url_ids
  479. * */
  480. function add_users_to_groups($user_list, $group_list, $relation_type = GROUP_USER_PERMISSION_READER) {
  481. $table_url_rel_group = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  482. $result_array = array();
  483. $relation_type = intval($relation_type);
  484. if (is_array($user_list) && is_array($group_list)) {
  485. foreach ($group_list as $group_id) {
  486. foreach ($user_list as $user_id) {
  487. $role = self::get_user_group_role($user_id,$group_id);
  488. if ($role == 0) {
  489. $sql = "INSERT INTO $table_url_rel_group
  490. SET user_id = ".intval($user_id).", group_id = ".intval($group_id).", relation_type = ".intval($relation_type)."";
  491. $result = Database::query($sql, __FILE__, __LINE__);
  492. if ($result)
  493. $result_array[$group_id][$user_id]=1;
  494. else
  495. $result_array[$group_id][$user_id]=0;
  496. }
  497. }
  498. }
  499. }
  500. return $result_array;
  501. }
  502. /**
  503. * Deletes a group and user relationship
  504. * @author Julio Montoya
  505. * @param int user id
  506. * @param int url id
  507. * @return boolean true if success
  508. * */
  509. function delete_users($group_id)
  510. {
  511. $table_= Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  512. $sql= "DELETE FROM $table_ WHERE group_id = ".intval($group_id);
  513. $result = Database::query($sql, __FILE__, __LINE__);
  514. return $result;
  515. }
  516. /**
  517. * Deletes an url and course relationship
  518. * @author Julio Montoya
  519. * @param char course code
  520. * @param int url id
  521. * @return boolean true if success
  522. * */
  523. function delete_url_rel_course($course_code, $url_id)
  524. {
  525. $table_url_rel_course= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  526. $sql= "DELETE FROM $table_url_rel_course WHERE course_code = '".Database::escape_string($course_code)."' AND access_url_id=".Database::escape_string($url_id)." ";
  527. $result = Database::query($sql, __FILE__, __LINE__);
  528. return $result;
  529. }
  530. /**
  531. * Deletes an url and session relationship
  532. * @author Julio Montoya
  533. * @param char course code
  534. * @param int url id
  535. * @return boolean true if success
  536. * */
  537. function delete_user_rel_group($user_id, $group_id)
  538. {
  539. $table = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  540. $sql= "DELETE FROM $table WHERE user_id = ".intval($user_id)." AND group_id=".intval($group_id)." ";
  541. $result = Database::query($sql, __FILE__, __LINE__);
  542. return $result;
  543. }
  544. /**
  545. * Updates the access_url_rel_user table with a given user list
  546. * @author Julio Montoya
  547. * @param array user list
  548. * @param int access_url_id
  549. * */
  550. function update_urls_rel_user($user_list,$access_url_id)
  551. {
  552. $table_access_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  553. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  554. $sql = "SELECT user_id FROM $table_url_rel_user WHERE access_url_id=".Database::escape_string($access_url_id);
  555. $result = Database::query($sql,__FILE__,__LINE__ );
  556. $existingUsers = array();
  557. while($row = Database::fetch_array($result)){
  558. $existingUsers[] = $row['user_id'];
  559. }
  560. //adding users
  561. foreach($user_list as $enreg_user) {
  562. if(!in_array($enreg_user, $existingUsers)) {
  563. UrlManager::add_user_to_url($enreg_user,$access_url_id);
  564. }
  565. }
  566. //deleting old users
  567. foreach($existingUsers as $existing_user) {
  568. if(!in_array($existing_user, $user_list)) {
  569. UrlManager::delete_url_rel_user($existing_user,$access_url_id);
  570. }
  571. }
  572. }
  573. /**
  574. * Updates the group_rel_user table with a given user and group ids
  575. * @author Julio Montoya
  576. * @param int user id
  577. * @param int group id
  578. * @param int relation type
  579. * */
  580. function update_user_permission($user_id, $group_id, $relation_type = GROUP_USER_PERMISSION_READER)
  581. {
  582. $table_group_rel_user = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  583. $group_id = intval($group_id);
  584. $user_id = intval($user_id);
  585. $sql = "UPDATE $table_group_rel_user
  586. SET relation_type = ".intval($relation_type)." WHERE user_id = $user_id AND group_id = $group_id" ;
  587. $result = Database::query($sql, __FILE__, __LINE__);
  588. }
  589. function get_group_admin_list($user_id, $group_id)
  590. {
  591. $table_group_rel_user = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  592. $group_id = intval($group_id);
  593. $user_id = intval($user_id);
  594. $sql = "UPDATE $table_group_rel_user
  595. SET relation_type = ".intval($relation_type)." WHERE user_id = $user_id AND group_id = $group_id" ;
  596. $result = Database::query($sql, __FILE__, __LINE__);
  597. }
  598. /**
  599. * Updates the access_url_rel_course table with a given user list
  600. * @author Julio Montoya
  601. * @param array user list
  602. * @param int access_url_id
  603. * */
  604. function update_urls_rel_course($course_list,$access_url_id)
  605. {
  606. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  607. $table_url_rel_course = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  608. $sql = "SELECT course_code FROM $table_url_rel_course WHERE access_url_id=".Database::escape_string($access_url_id);
  609. $result = Database::query($sql,__FILE__,__LINE__ );
  610. $existing_courses = array();
  611. while($row = Database::fetch_array($result)){
  612. $existing_courses[] = $row['course_code'];
  613. }
  614. //adding courses
  615. foreach($course_list as $course) {
  616. if(!in_array($course, $existing_courses)) {
  617. UrlManager::add_course_to_url($course,$access_url_id);
  618. }
  619. }
  620. //deleting old courses
  621. foreach($existing_courses as $existing_course) {
  622. if(!in_array($existing_course, $course_list)) {
  623. UrlManager::delete_url_rel_course($existing_course,$access_url_id);
  624. }
  625. }
  626. }
  627. function get_access_url_from_user($user_id) {
  628. $table_url_rel_user = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  629. $table_url = Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  630. $sql = "SELECT url, access_url_id FROM $table_url_rel_user url_rel_user INNER JOIN $table_url u
  631. ON (url_rel_user.access_url_id = u.id)
  632. WHERE user_id = ".Database::escape_string($user_id);
  633. $result = Database::query($sql, __FILE__, __LINE__);
  634. $url_list = Database::store_result($result);
  635. return $url_list;
  636. }
  637. /**
  638. *
  639. * */
  640. function get_url_id($url)
  641. {
  642. $table_access_url= Database :: get_main_table(TABLE_MAIN_ACCESS_URL);
  643. $sql = "SELECT id FROM $table_access_url WHERE url = '".Database::escape_string($url)."'";
  644. $result = Database::query($sql);
  645. $access_url_id = Database::result($result, 0, 0);
  646. return $access_url_id;
  647. }
  648. public static function get_all_group_tags($tag, $from=0, $number_of_items=10) {
  649. // database table definition
  650. $group_table = Database::get_main_table(TABLE_MAIN_GROUP);
  651. $table_tag = Database::get_main_table(TABLE_MAIN_TAG);
  652. $table_group_tag_values = Database::get_main_table(TABLE_MAIN_GROUP_REL_TAG);
  653. //default field_id == 1
  654. $field_id = 5;
  655. $tag = Database::escape_string($tag);
  656. $from = intval($from);
  657. $number_of_items = intval($number_of_items);
  658. // all the information of the field
  659. $sql = "SELECT g.id, g.name, g.description, g.picture_uri FROM $table_tag t INNER JOIN $table_group_tag_values tv ON (tv.tag_id=t.id)
  660. INNER JOIN $group_table g ON(tv.group_id =g.id)
  661. WHERE tag LIKE '$tag%' AND field_id= $field_id ORDER BY tag";
  662. $sql .= " LIMIT $from,$number_of_items";
  663. $result = Database::query($sql, __FILE__, __LINE__);
  664. $return = array();
  665. if (Database::num_rows($result)> 0) {
  666. while ($row = Database::fetch_array($result,'ASSOC')) {
  667. $return[$row['id']] = $row;
  668. }
  669. }
  670. $keyword = $tag;
  671. $sql = "SELECT g.id, g.name, g.description, g.url, g.picture_uri FROM $group_table g";
  672. //@todo implement groups + multiple urls
  673. /*
  674. global $_configuration;
  675. if ($_configuration['multiple_access_urls']==true && api_get_current_access_url_id()!=-1) {
  676. $access_url_rel_user_table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  677. $sql.= " INNER JOIN $access_url_rel_user_table url_rel_user ON (u.user_id=url_rel_user.user_id)";
  678. }*/
  679. //@todo implement visibility
  680. if (isset ($keyword)) {
  681. $keyword = Database::escape_string($keyword);
  682. $sql .= " WHERE (g.name LIKE '%".$keyword."%' OR g.description LIKE '%".$keyword."%' OR g.url LIKE '%".$keyword."%' )";
  683. }
  684. $direction = 'ASC';
  685. if (!in_array($direction, array('ASC','DESC'))) {
  686. $direction = 'ASC';
  687. }
  688. $column = intval($column);
  689. $from = intval($from);
  690. $number_of_items = intval($number_of_items);
  691. //$sql .= " ORDER BY col$column $direction ";
  692. $sql .= " LIMIT $from,$number_of_items";
  693. $res = Database::query($sql, __FILE__, __LINE__);
  694. if (Database::num_rows($res)> 0) {
  695. while ($row = Database::fetch_array($res,'ASSOC')) {
  696. if (!in_array($row['id'], $return)) {
  697. $return[$row['id']] = $row;
  698. }
  699. }
  700. }
  701. return $return;
  702. }
  703. /**
  704. * Creates new group pictures in various sizes of a user, or deletes user pfotos.
  705. * Note: This method relies on configuration setting from dokeos/main/inc/conf/profile.conf.php
  706. * @param int The group id
  707. * @param string $file The common file name for the newly created pfotos. It will be checked and modified for compatibility with the file system.
  708. * If full name is provided, path component is ignored.
  709. * If an empty name is provided, then old user photos are deleted only, @see UserManager::delete_user_picture() as the prefered way for deletion.
  710. * @param string $source_file The full system name of the image from which user photos will be created.
  711. * @return string/bool Returns the resulting common file name of created images which usually should be stored in database.
  712. * When deletion is recuested returns empty string. In case of internal error or negative validation returns FALSE.
  713. */
  714. public static function update_group_picture($group_id, $file = null, $source_file = null) {
  715. // Validation 1.
  716. if (empty($group_id)) {
  717. return false;
  718. }
  719. $delete = empty($file);
  720. if (empty($source_file)) {
  721. $source_file = $file;
  722. }
  723. // Configuration options about user photos.
  724. require_once api_get_path(CONFIGURATION_PATH).'profile.conf.php';
  725. // User-reserved directory where photos have to be placed.
  726. $path_info = self::get_group_picture_path_by_id($group_id, 'system', true);
  727. $path = $path_info['dir'];
  728. // If this directory does not exist - we create it.
  729. if (!file_exists($path)) {
  730. $perm = api_get_setting('permissions_for_new_directories');
  731. $perm = octdec(!empty($perm) ? $perm : '0770');
  732. @mkdir($path, $perm, true);
  733. }
  734. // The old photos (if any).
  735. $old_file = $path_info['file'];
  736. // Let us delete them.
  737. if (!empty($old_file)) {
  738. if (KEEP_THE_OLD_IMAGE_AFTER_CHANGE) {
  739. $prefix = 'saved_'.date('Y_m_d_H_i_s').'_'.uniqid('').'_';
  740. @rename($path.'small_'.$old_file, $path.$prefix.'small_'.$old_file);
  741. @rename($path.'medium_'.$old_file, $path.$prefix.'medium_'.$old_file);
  742. @rename($path.'big_'.$old_file, $path.$prefix.'big_'.$old_file);
  743. @rename($path.$old_file, $path.$prefix.$old_file);
  744. } else {
  745. @unlink($path.'small_'.$old_file);
  746. @unlink($path.'medium_'.$old_file);
  747. @unlink($path.'big_'.$old_file);
  748. @unlink($path.$old_file);
  749. }
  750. }
  751. // Exit if only deletion has been requested. Return an empty picture name.
  752. if ($delete) {
  753. return '';
  754. }
  755. // Validation 2.
  756. $allowed_types = array('jpg', 'jpeg', 'png', 'gif');
  757. $file = str_replace('\\', '/', $file);
  758. $filename = (($pos = strrpos($file, '/')) !== false) ? substr($file, $pos + 1) : $file;
  759. $extension = strtolower(substr(strrchr($filename, '.'), 1));
  760. if (!in_array($extension, $allowed_types)) {
  761. return false;
  762. }
  763. // This is the common name for the new photos.
  764. if (KEEP_THE_NAME_WHEN_CHANGE_IMAGE && !empty($old_file)) {
  765. $old_extension = strtolower(substr(strrchr($old_file, '.'), 1));
  766. $filename = in_array($old_extension, $allowed_types) ? substr($old_file, 0, -strlen($old_extension)) : $old_file;
  767. $filename = (substr($filename, -1) == '.') ? $filename.$extension : $filename.'.'.$extension;
  768. } else {
  769. $filename = replace_dangerous_char($filename);
  770. if (PREFIX_IMAGE_FILENAME_WITH_UID) {
  771. $filename = uniqid('').'_'.$filename;
  772. }
  773. // We always prefix user photos with user ids, so on setting
  774. // api_get_setting('split_users_upload_directory') === 'true'
  775. // the correspondent directories to be found successfully.
  776. $filename = $group_id.'_'.$filename;
  777. }
  778. // Storing the new photos in 4 versions with various sizes.
  779. $picture_info = @getimagesize($source_file);
  780. $type = $picture_info[2];
  781. $small = self::resize_picture($source_file, 22);
  782. $medium = self::resize_picture($source_file, 85);
  783. $normal = self::resize_picture($source_file, 200);
  784. $big = new image($source_file); // This is the original picture.
  785. $ok = false;
  786. $detected = array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG');
  787. if (in_array($type, array_keys($detected))) {
  788. $ok = $small->send_image($detected[$type], $path.'small_'.$filename)
  789. && $medium->send_image($detected[$type], $path.'medium_'.$filename)
  790. && $normal->send_image($detected[$type], $path.$filename)
  791. && $big->send_image($detected[$type], $path.'big_'.$filename);
  792. }
  793. return $ok ? $filename : false;
  794. }
  795. /**
  796. * Gets the group picture URL or path from group ID (returns an array).
  797. * The return format is a complete path, enabling recovery of the directory
  798. * with dirname() or the file with basename(). This also works for the
  799. * functions dealing with the user's productions, as they are located in
  800. * the same directory.
  801. * @param integer User ID
  802. * @param string Type of path to return (can be 'none', 'system', 'rel', 'web')
  803. * @param bool Whether we want to have the directory name returned 'as if' there was a file or not (in the case we want to know which directory to create - otherwise no file means no split subdir)
  804. * @param bool If we want that the function returns the /main/img/unknown.jpg image set it at true
  805. * @return array Array of 2 elements: 'dir' and 'file' which contain the dir and file as the name implies if image does not exist it will return the unknow image if anonymous parameter is true if not it returns an empty er's
  806. */
  807. public static function get_group_picture_path_by_id($id, $type = 'none', $preview = false, $anonymous = false) {
  808. switch ($type) {
  809. case 'system': // Base: absolute system path.
  810. $base = api_get_path(SYS_CODE_PATH);
  811. break;
  812. case 'rel': // Base: semi-absolute web path (no server base).
  813. $base = api_get_path(REL_CODE_PATH);
  814. break;
  815. case 'web': // Base: absolute web path.
  816. $base = api_get_path(WEB_CODE_PATH);
  817. break;
  818. case 'none':
  819. default: // Base: empty, the result path below will be relative.
  820. $base = '';
  821. }
  822. if (empty($id) || empty($type)) {
  823. return $anonymous ? array('dir' => $base.'img/', 'file' => 'unknown.jpg') : array('dir' => '', 'file' => '');
  824. }
  825. $id = intval($id);
  826. $group_table = Database :: get_main_table(TABLE_MAIN_GROUP);
  827. $sql = "SELECT picture_uri FROM $group_table WHERE id=".$id;
  828. $res = Database::query($sql, __FILE__, __LINE__);
  829. if (!Database::num_rows($res)) {
  830. return $anonymous ? array('dir' => $base.'img/', 'file' => 'unknown.jpg') : array('dir' => '', 'file' => '');
  831. }
  832. $user = Database::fetch_array($res);
  833. $picture_filename = trim($user['picture_uri']);
  834. if (api_get_setting('split_users_upload_directory') === 'true') {
  835. if (!empty($picture_filename)) {
  836. $dir = $base.'upload/users/groups/'.substr($picture_filename, 0, 1).'/'.$id.'/';
  837. } elseif ($preview) {
  838. $dir = $base.'upload/users/groups/'.substr((string)$id, 0, 1).'/'.$id.'/';
  839. } else {
  840. $dir = $base.'upload/users/groups/'.$id.'/';
  841. }
  842. } else {
  843. $dir = $base.'upload/users/groups/'.$id.'/';
  844. }
  845. if (empty($picture_filename) && $anonymous) {
  846. return array('dir' => $base.'img/', 'file' => 'unknown.jpg');
  847. }
  848. return array('dir' => $dir, 'file' => $picture_filename);
  849. }
  850. /**
  851. * Resize a picture
  852. *
  853. * @param string file picture
  854. * @param int size in pixels
  855. * @return obj image object
  856. */
  857. public static function resize_picture($file, $max_size_for_picture) {
  858. if (!class_exists('image')) {
  859. require_once api_get_path(LIBRARY_PATH).'image.lib.php';
  860. }
  861. $temp = new image($file);
  862. $picture_infos = api_getimagesize($file);
  863. if ($picture_infos[0] > $max_size_for_picture) {
  864. $thumbwidth = $max_size_for_picture;
  865. if (empty($thumbwidth) or $thumbwidth == 0) {
  866. $thumbwidth = $max_size_for_picture;
  867. }
  868. $new_height = round(($thumbwidth / $picture_infos[0]) * $picture_infos[1]);
  869. if ($new_height > $max_size_for_picture)
  870. $new_height = $thumbwidth;
  871. $temp->resize($thumbwidth, $new_height, 0);
  872. }
  873. return $temp;
  874. }
  875. /**
  876. * Gets the current group image
  877. * @param string group id
  878. * @param string picture group name
  879. * @param string height
  880. * @param string picture size it can be small_, medium_ or big_
  881. * @param string style css
  882. * @return array with the file and the style of an image i.e $array['file'] $array['style']
  883. */
  884. public static function get_picture_group($id, $picture_file, $height, $size_picture = 'medium_', $style = '') {
  885. $patch_profile = 'upload/users/groups/';
  886. $picture = array();
  887. $picture['style'] = $style;
  888. if ($picture_file == 'unknown.jpg') {
  889. $picture['file'] = api_get_path(WEB_CODE_PATH).'img/'.$picture_file;
  890. return $picture;
  891. }
  892. $image_array_sys = self::get_group_picture_path_by_id($id, 'system', false, true);
  893. $image_array = self::get_group_picture_path_by_id($id, 'web', false, true);
  894. $file = $image_array_sys['dir'].$size_picture.$picture_file;
  895. if (file_exists($file)) {
  896. $picture['file'] = $image_array['dir'].$size_picture.$picture_file;
  897. $picture['style'] = '';
  898. if ($height > 0) {
  899. $dimension = api_getimagesize($picture['file']);
  900. $margin = (($height - $dimension[1]) / 2);
  901. //@ todo the padding-top should not be here
  902. $picture['style'] = ' style="padding-top:'.$margin.'px; width:'.$dimension[0].'px; height:'.$dimension[1].';" ';
  903. }
  904. } else {
  905. //$file = api_get_path(SYS_CODE_PATH).$patch_profile.$user_id.'/'.$picture_file;
  906. $file = $image_array_sys['dir'].$picture_file;
  907. if (file_exists($file) && !is_dir($file)) {
  908. $picture['file'] = $image_array['dir'].$picture_file;
  909. } else {
  910. $picture['file'] = api_get_path(WEB_CODE_PATH).'img/unknown_group.png';
  911. }
  912. }
  913. return $picture;
  914. }
  915. public static function delete_group_picture($user_id) {
  916. return self::update_group_picture($user_id);
  917. }
  918. }
  919. ?>