group_portal_manager.lib.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This library provides functions for the group management.
  5. * Include/require it in your code to use its functionality.
  6. * @author Julio Montoya <gugli100@gmail.com>
  7. * @deprecated use usergroup.lib.php
  8. * @package chamilo.library
  9. */
  10. /**
  11. * Code
  12. */
  13. /**
  14. * Class
  15. * @package chamilo.library
  16. */
  17. class GroupPortalManager
  18. {
  19. /**
  20. * Creates a new group
  21. *
  22. * @author Julio Montoya <gugli100@gmail.com>,
  23. *
  24. * @param string The URL of the site
  25. * @param string The description of the site
  26. * @param int is active or not
  27. * @param int the user_id of the owner
  28. * @return boolean if success
  29. */
  30. public static function add($name, $description, $url, $visibility, $picture='') {
  31. $now = api_get_utc_datetime();
  32. $table = Database :: get_main_table(TABLE_MAIN_GROUP);
  33. $sql = "INSERT INTO $table
  34. SET name = '".Database::escape_string($name)."',
  35. description = '".Database::escape_string($description)."',
  36. picture_uri = '".Database::escape_string($picture)."',
  37. url = '".Database::escape_string($url)."',
  38. visibility = '".Database::escape_string($visibility)."',
  39. created_on = '".$now."',
  40. updated_on = '".$now."'";
  41. Database::query($sql);
  42. $return = Database::insert_id();
  43. return $return;
  44. }
  45. /**
  46. * Updates a group
  47. * @author Julio Montoya <gugli100@gmail.com>,
  48. *
  49. * @param int The id
  50. * @param string The description of the site
  51. * @param int is active or not
  52. * @param int the user_id of the owner
  53. * @return boolean if success
  54. */
  55. public static function update($group_id, $name, $description, $url, $visibility, $picture_uri) {
  56. $group_id = intval($group_id);
  57. $table = Database::get_main_table(TABLE_MAIN_GROUP);
  58. $now = api_get_utc_datetime();
  59. $sql = "UPDATE $table
  60. SET name = '".Database::escape_string($name)."',
  61. description = '".Database::escape_string($description)."',
  62. picture_uri = '".Database::escape_string($picture_uri)."',
  63. url = '".Database::escape_string($url)."',
  64. visibility = '".Database::escape_string($visibility)."',
  65. updated_on = '".$now."'
  66. WHERE id = '$group_id'";
  67. $result = Database::query($sql);
  68. return $result;
  69. }
  70. /**
  71. * Deletes a group
  72. * @author Julio Montoya
  73. * @param int id
  74. * @return boolean true if success
  75. * */
  76. public static function delete($id) {
  77. $id = intval($id);
  78. $table = Database :: get_main_table(TABLE_MAIN_GROUP);
  79. $sql= "DELETE FROM $table WHERE id = ".Database::escape_string($id);
  80. $result = Database::query($sql);
  81. //deleting all relationship with users and groups
  82. self::delete_users($id);
  83. // delete group image
  84. self::delete_group_picture($id);
  85. return $result;
  86. }
  87. /**
  88. * Gets data of all groups
  89. * @author Julio Montoya
  90. * @param int visibility
  91. * @param int from which record the results will begin (use for pagination)
  92. * @param int number of items
  93. * @return array
  94. * */
  95. public static function get_all_group_data($visibility = GROUP_PERMISSION_OPEN, $from = 0, $number_of_items = 10) {
  96. $table = Database :: get_main_table(TABLE_MAIN_GROUP);
  97. $visibility = intval($visibility);
  98. $sql = "SELECT name, description, picture_uri FROM $table WHERE visibility = $visibility ";
  99. $res = Database::query($sql);
  100. $data = array ();
  101. while ($item = Database::fetch_array($res)) {
  102. $data[] = $item;
  103. }
  104. return $data;
  105. }
  106. /**
  107. * Gets a list of all group
  108. * @param id of a group not to include (i.e. to exclude)
  109. * @return array : id => name
  110. **/
  111. public static function get_groups_list($without_this_one = NULL ) {
  112. $where='';
  113. if ( isset($without_this_one) && (intval($without_this_one) == $without_this_one) ) {
  114. $where = "WHERE id <> $without_this_one";
  115. }
  116. $table = Database :: get_main_table(TABLE_MAIN_GROUP);
  117. $sql = "SELECT id, name FROM $table $where order by name";
  118. $res = Database::query($sql);
  119. $list = array ();
  120. while ($item = Database::fetch_assoc($res)) {
  121. $list[$item['id']] = $item['name'];
  122. }
  123. return $list;
  124. }
  125. /**
  126. * Gets the group data
  127. *
  128. *
  129. */
  130. public static function get_group_data($group_id) {
  131. $table = Database :: get_main_table(TABLE_MAIN_GROUP);
  132. $group_id = intval($group_id);
  133. $sql = "SELECT id, name, description, picture_uri, url, visibility FROM $table WHERE id = $group_id ";
  134. $res = Database::query($sql);
  135. $item = array();
  136. if (Database::num_rows($res)>0) {
  137. $item = Database::fetch_array($res,'ASSOC');
  138. }
  139. return $item;
  140. }
  141. /**
  142. * Set a parent group
  143. * @param group_id
  144. * @param parent_group, if 0, we delete the parent_group association
  145. * @param relation_type
  146. * @return true or false
  147. **/
  148. public static function set_parent_group($group_id, $parent_group_id, $relation_type = 1){
  149. $table = Database :: get_main_table(TABLE_MAIN_GROUP_REL_GROUP);
  150. $group_id = intval($group_id);
  151. $parent_group_id = intval($parent_group_id);
  152. if ($parent_group_id == 0) {
  153. $sql = "DELETE FROM $table WHERE subgroup_id = $group_id";
  154. } else {
  155. $sql = "SELECT group_id FROM $table WHERE subgroup_id = $group_id";
  156. $res = Database::query($sql);
  157. if (Database::num_rows($res)==0) {
  158. $sql = "INSERT INTO $table SET group_id = $parent_group_id, subgroup_id = $group_id, relation_type = $relation_type";
  159. } else {
  160. $sql = "UPDATE $table SET group_id = $parent_group_id, relation_type = $relation_type WHERE subgroup_id = $group_id";
  161. }
  162. }
  163. $res = Database::query($sql);
  164. return($res);
  165. }
  166. /**
  167. * Get the parent group
  168. * @param group_id
  169. * @param relation_type
  170. * @return int parent_group_id or false
  171. **/
  172. public static function get_parent_group($group_id, $relation_type=1) {
  173. $table = Database :: get_main_table(TABLE_MAIN_GROUP_REL_GROUP);
  174. $group_id=intval($group_id);
  175. $parent_group_id=intval($parent_group_id);
  176. $sql = "SELECT group_id FROM $table WHERE subgroup_id = $group_id";
  177. $res = Database::query($sql);
  178. if (Database::num_rows($res)==0) {
  179. return 0;
  180. } else {
  181. $arr = Database::fetch_assoc($res);
  182. return $arr['group_id'];
  183. }
  184. }
  185. public static function get_subgroups($root, $level) {
  186. $t_group = Database::get_main_table(TABLE_MAIN_GROUP);
  187. $t_rel_group = Database :: get_main_table(TABLE_MAIN_GROUP_REL_GROUP);
  188. $select_part = "SELECT ";
  189. $cond_part='';
  190. for ($i=1; $i <= $level; $i++) {
  191. $g_number=$i;
  192. $rg_number=$i-1;
  193. if ( $i == $level) {
  194. $select_part .= "g$i.id as id_$i, g$i.name as name_$i ";
  195. } else {
  196. $select_part .="g$i.id as id_$i, g$i.name name_$i, ";
  197. }
  198. if ($i == 1) {
  199. $cond_part .= "FROM $t_group g1 JOIN $t_rel_group rg0 on g1.id = rg0.subgroup_id and rg0.group_id = $root ";
  200. } else {
  201. $cond_part .= "LEFT JOIN $t_rel_group rg$rg_number on g$rg_number.id = rg$rg_number.group_id ";
  202. $cond_part .= "LEFT JOIN $t_group g$g_number on rg$rg_number.subgroup_id = g$g_number.id ";
  203. }
  204. }
  205. $sql = $select_part.' '. $cond_part;
  206. $res = Database::query($sql);
  207. $toreturn = array();
  208. while ($item = Database::fetch_assoc($res)) {
  209. foreach ($item as $key => $value ){
  210. if ($key == 'id_1') {
  211. $toreturn[$value]['name'] = $item['name_1'];
  212. } else {
  213. $temp = explode('_',$key);
  214. $index_key = $temp[1];
  215. $string_key = $temp[0];
  216. $previous_key = $string_key.'_'.$index_key-1;
  217. if ( $string_key == 'id' && isset($item[$key]) ) {
  218. $toreturn[$item[$previous_key]]['hrms'][$index_key]['name'] = $item['name_'.$index_id];
  219. }
  220. }
  221. }
  222. }
  223. return $toreturn;
  224. }
  225. public static function get_parent_groups($group_id) {
  226. $t_rel_group = Database :: get_main_table(TABLE_MAIN_GROUP_REL_GROUP);
  227. $max_level = 10;
  228. $select_part = "SELECT ";
  229. $cond_part='';
  230. for ($i=1; $i <= $max_level; $i++) {
  231. $g_number=$i;
  232. $rg_number=$i-1;
  233. if ( $i == $max_level) {
  234. $select_part .= "rg$rg_number.group_id as id_$rg_number ";
  235. } else {
  236. $select_part .="rg$rg_number.group_id as id_$rg_number, ";
  237. }
  238. if ($i == 1) {
  239. $cond_part .= "FROM $t_rel_group rg0 LEFT JOIN $t_rel_group rg$i on rg$rg_number.group_id = rg$i.subgroup_id ";
  240. } else {
  241. $cond_part .= " LEFT JOIN $t_rel_group rg$i on rg$rg_number.group_id = rg$i.subgroup_id ";
  242. }
  243. }
  244. $sql = $select_part.' '. $cond_part . "WHERE rg0.subgroup_id='$group_id'";
  245. $res = Database::query($sql);
  246. $temp_arr = Database::fetch_array($res, 'NUM');
  247. $toreturn = array();
  248. if (is_array($temp_arr)) {
  249. foreach ($temp_arr as $elt) {
  250. if (isset($elt)) {
  251. $toreturn[] = $elt;
  252. }
  253. }
  254. }
  255. return $toreturn;
  256. }
  257. /**
  258. * Gets the tags from a given group
  259. * @param int group id
  260. * @param bool show group links or not
  261. *
  262. */
  263. public static function get_group_tags($group_id, $show_tag_links = true) {
  264. $tag = Database :: get_main_table(TABLE_MAIN_TAG);
  265. $table_group_rel_tag = Database :: get_main_table(TABLE_MAIN_GROUP_REL_TAG);
  266. $group_id = intval($group_id);
  267. $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 ";
  268. $res = Database::query($sql);
  269. $tags = array();
  270. if (Database::num_rows($res)>0) {
  271. while ($row = Database::fetch_array($res,'ASSOC')) {
  272. $tags[] = $row;
  273. }
  274. }
  275. if ($show_tag_links) {
  276. if (is_array($tags) && count($tags)>0) {
  277. foreach ($tags as $tag) {
  278. $tag_tmp[] = '<a href="'.api_get_path(WEB_PATH).'main/social/search.php?q='.$tag['tag'].'">'.$tag['tag'].'</a>';
  279. }
  280. if (is_array($tags) && count($tags)>0) {
  281. $tags= implode(', ',$tag_tmp);
  282. }
  283. } else {
  284. $tags = '';
  285. }
  286. }
  287. return $tags;
  288. }
  289. /** Gets the inner join from users and group table
  290. * @return array Database::store_result of the result
  291. * @author Julio Montoya
  292. * */
  293. public static function get_groups_by_user($user_id = '', $relation_type = GROUP_USER_PERMISSION_READER, $with_image = false) {
  294. $table_group_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_GROUP);
  295. $tbl_group = Database::get_main_table(TABLE_MAIN_GROUP);
  296. $user_id = intval($user_id);
  297. if ($relation_type == 0) {
  298. $where_relation_condition = '';
  299. } else {
  300. $relation_type = intval($relation_type);
  301. $where_relation_condition = "AND gu.relation_type = $relation_type ";
  302. }
  303. $sql = "SELECT g.picture_uri, g.name, g.description, g.id , gu.relation_type
  304. FROM $tbl_group g
  305. INNER JOIN $table_group_rel_user gu
  306. ON gu.group_id = g.id WHERE gu.user_id = $user_id $where_relation_condition ORDER BY created_on desc ";
  307. $result=Database::query($sql);
  308. $array = array();
  309. if (Database::num_rows($result) > 0) {
  310. while ($row = Database::fetch_array($result, 'ASSOC')) {
  311. if ($with_image) {
  312. $picture = self::get_picture_group($row['id'], $row['picture_uri'],80);
  313. $img = '<img src="'.$picture['file'].'" />';
  314. $row['picture_uri'] = $img;
  315. }
  316. $array[$row['id']] = $row;
  317. }
  318. }
  319. return $array;
  320. }
  321. /** Gets the inner join of users and group table
  322. * @param int quantity of records
  323. * @param bool show groups with image or not
  324. * @return array with group content
  325. * @author Julio Montoya
  326. * */
  327. public static function get_groups_by_popularity($num = 6, $with_image = true) {
  328. $table_group_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_GROUP);
  329. $tbl_group = Database::get_main_table(TABLE_MAIN_GROUP);
  330. if (empty($num)) {
  331. $num = 6;
  332. } else {
  333. $num = intval($num);
  334. }
  335. // only show admins and readers
  336. $where_relation_condition = " WHERE gu.relation_type IN ('".GROUP_USER_PERMISSION_ADMIN."' , '".GROUP_USER_PERMISSION_READER."', '".GROUP_USER_PERMISSION_HRM."') ";
  337. $sql = "SELECT DISTINCT count(user_id) as count, g.picture_uri, g.name, g.description, g.id
  338. FROM $tbl_group g
  339. INNER JOIN $table_group_rel_user gu
  340. ON gu.group_id = g.id $where_relation_condition
  341. GROUP BY g.id
  342. ORDER BY count DESC
  343. LIMIT $num";
  344. $result=Database::query($sql);
  345. $array = array();
  346. while ($row = Database::fetch_array($result, 'ASSOC')) {
  347. if ($with_image) {
  348. $picture = self::get_picture_group($row['id'], $row['picture_uri'],80);
  349. $img = '<img src="'.$picture['file'].'" />';
  350. $row['picture_uri'] = $img;
  351. }
  352. if (empty($row['id'])) {
  353. continue;
  354. }
  355. $array[$row['id']] = $row;
  356. }
  357. return $array;
  358. }
  359. /** Gets the last groups created
  360. * @param int quantity of records
  361. * @param bool show groups with image or not
  362. * @return array with group content
  363. * @author Julio Montoya
  364. * */
  365. public static function get_groups_by_age($num = 6, $with_image = true) {
  366. $table_group_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_GROUP);
  367. $tbl_group = Database::get_main_table(TABLE_MAIN_GROUP);
  368. if (empty($num)) {
  369. $num = 6;
  370. } else {
  371. $num = intval($num);
  372. }
  373. $where_relation_condition = " WHERE gu.relation_type IN ('".GROUP_USER_PERMISSION_ADMIN."' , '".GROUP_USER_PERMISSION_READER."', '".GROUP_USER_PERMISSION_HRM."') ";
  374. $sql = "SELECT DISTINCT count(user_id) as count, g.picture_uri, g.name, g.description, g.id
  375. FROM $tbl_group g INNER JOIN $table_group_rel_user gu ON gu.group_id = g.id
  376. $where_relation_condition
  377. GROUP BY g.id
  378. ORDER BY created_on DESC
  379. LIMIT $num ";
  380. $result=Database::query($sql);
  381. $array = array();
  382. while ($row = Database::fetch_array($result, 'ASSOC')) {
  383. if ($with_image) {
  384. $picture = self::get_picture_group($row['id'], $row['picture_uri'],80);
  385. $img = '<img src="'.$picture['file'].'" />';
  386. $row['picture_uri'] = $img;
  387. }
  388. if (empty($row['id'])) {
  389. continue;
  390. }
  391. $array[$row['id']] = $row;
  392. }
  393. return $array;
  394. }
  395. /**
  396. * Gets the group's members
  397. * @param int group id
  398. * @param bool show image or not of the group
  399. * @param array list of relation type use constants
  400. * @param int from value
  401. * @param int limit
  402. * @param array image configuration, i.e array('height'=>'20px', 'size'=> '20px')
  403. * @return array list of users in a group
  404. */
  405. public static function get_users_by_group($group_id, $with_image = false, $relation_type = array(), $from = null, $limit = null, $image_conf = array('size'=>USER_IMAGE_SIZE_MEDIUM,'height'=>80)) {
  406. $where = '';
  407. $table_group_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_GROUP);
  408. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  409. $group_id = intval($group_id);
  410. if (empty($group_id)){
  411. return array();
  412. }
  413. $limit_text = '';
  414. if (isset($from) && isset($limit)) {
  415. $from = intval($from);
  416. $limit = intval($limit);
  417. $limit_text = "LIMIT $from, $limit";
  418. }
  419. if (count($relation_type) == 0) {
  420. $where_relation_condition = '';
  421. } else {
  422. $new_relation_type = array();
  423. foreach($relation_type as $rel) {
  424. $rel = intval($rel);
  425. $new_relation_type[] ="'$rel'";
  426. }
  427. $relation_type = implode(',', $new_relation_type);
  428. if (!empty($relation_type))
  429. $where_relation_condition = "AND gu.relation_type IN ($relation_type) ";
  430. }
  431. $sql = "SELECT picture_uri as image, u.user_id, u.firstname, u.lastname, relation_type
  432. FROM $tbl_user u INNER JOIN $table_group_rel_user gu
  433. ON (gu.user_id = u.user_id)
  434. WHERE gu.group_id= $group_id $where_relation_condition
  435. ORDER BY relation_type, firstname $limit_text";
  436. $result = Database::query($sql);
  437. $array = array();
  438. while ($row = Database::fetch_array($result, 'ASSOC')) {
  439. if ($with_image) {
  440. $image_path = UserManager::get_user_picture_path_by_id($row['user_id'], 'web', false, true);
  441. $picture = UserManager::get_picture_user($row['user_id'], $image_path['file'], $image_conf['height'], $image_conf['size']);
  442. $row['image'] = '<img src="'.$picture['file'].'" '.$picture['style'].' />';
  443. }
  444. $array[$row['user_id']] = $row;
  445. }
  446. return $array;
  447. }
  448. /**
  449. * Gets all the members of a group no matter the relationship for more specifications use get_users_by_group
  450. * @param int group id
  451. * @return array
  452. */
  453. public static function get_all_users_by_group($group_id) {
  454. $table_group_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_GROUP);
  455. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  456. $group_id = intval($group_id);
  457. if (empty($group_id)){
  458. return array();
  459. }
  460. $sql="SELECT u.user_id, u.firstname, u.lastname, relation_type FROM $tbl_user u
  461. INNER JOIN $table_group_rel_user gu
  462. ON (gu.user_id = u.user_id) WHERE gu.group_id= $group_id ORDER BY relation_type, firstname";
  463. $result=Database::query($sql);
  464. $array = array();
  465. while ($row = Database::fetch_array($result, 'ASSOC')) {
  466. $array[$row['user_id']] = $row;
  467. }
  468. return $array;
  469. }
  470. /**
  471. * Gets the relationship between a group and a User
  472. * @author Julio Montoya
  473. * @param int user id
  474. * @param int group_id
  475. * @return int 0 if there are not relationship otherwise returns the user group
  476. * */
  477. public static function get_user_group_role($user_id, $group_id) {
  478. $table_group_rel_user= Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  479. $return_value = 0;
  480. if (!empty($user_id) && !empty($group_id)) {
  481. $sql = "SELECT relation_type FROM $table_group_rel_user WHERE group_id = ".intval($group_id)." AND user_id = ".intval($user_id)." ";
  482. $result = Database::query($sql);
  483. if (Database::num_rows($result)>0) {
  484. $row = Database::fetch_array($result,'ASSOC');
  485. $return_value = $row['relation_type'];
  486. }
  487. }
  488. return $return_value;
  489. }
  490. /**
  491. * Add a user into a group
  492. * @author Julio Montoya
  493. * @param user_id
  494. * @param url_id
  495. * @return boolean true if success
  496. * */
  497. public static function add_user_to_group($user_id, $group_id, $relation_type = GROUP_USER_PERMISSION_READER) {
  498. $table_url_rel_group = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  499. if (!empty($user_id) && !empty($group_id)) {
  500. $role = self::get_user_group_role($user_id,$group_id);
  501. if ($role == 0) {
  502. $sql = "INSERT INTO $table_url_rel_group
  503. SET user_id = ".intval($user_id).", group_id = ".intval($group_id).", relation_type = ".intval($relation_type);
  504. $result = Database::query($sql);
  505. } elseif ($role == GROUP_USER_PERMISSION_PENDING_INVITATION) {
  506. //if somebody already invited me I can be added
  507. self::update_user_role($user_id, $group_id, GROUP_USER_PERMISSION_READER);
  508. }
  509. }
  510. return $result;
  511. }
  512. /**
  513. * Add a group of users into a group of URLs
  514. * @author Julio Montoya
  515. * @param array of user_ids
  516. * @param array of url_ids
  517. * */
  518. public static function add_users_to_groups($user_list, $group_list, $relation_type = GROUP_USER_PERMISSION_READER) {
  519. $table_url_rel_group = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  520. $result_array = array();
  521. $relation_type = intval($relation_type);
  522. if (is_array($user_list) && is_array($group_list)) {
  523. foreach ($group_list as $group_id) {
  524. foreach ($user_list as $user_id) {
  525. $role = self::get_user_group_role($user_id,$group_id);
  526. if ($role == 0) {
  527. $sql = "INSERT INTO $table_url_rel_group
  528. SET user_id = ".intval($user_id).", group_id = ".intval($group_id).", relation_type = ".intval($relation_type);
  529. $result = Database::query($sql);
  530. if ($result)
  531. $result_array[$group_id][$user_id]=1;
  532. else
  533. $result_array[$group_id][$user_id]=0;
  534. }
  535. }
  536. }
  537. }
  538. return $result_array;
  539. }
  540. /**
  541. * Deletes a group and user relationship
  542. * @author Julio Montoya
  543. * @param int user id
  544. * @param int relation type (optional)
  545. * @return boolean true if success
  546. * */
  547. public static function delete_users($group_id, $relation_type = '') {
  548. $table_ = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  549. $condition_relation = "";
  550. if (!empty($relation_type)) {
  551. $relation_type = intval($relation_type);
  552. $condition_relation = " AND relation_type = '$relation_type'";
  553. }
  554. $sql = "DELETE FROM $table_ WHERE group_id = ".intval($group_id).$condition_relation;
  555. $result = Database::query($sql);
  556. return $result;
  557. }
  558. /**
  559. * Deletes an url and session relationship
  560. * @author Julio Montoya
  561. * @param char course code
  562. * @param int url id
  563. * @return boolean true if success
  564. * */
  565. public static function delete_user_rel_group($user_id, $group_id) {
  566. $table = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  567. $sql= "DELETE FROM $table WHERE user_id = ".intval($user_id)." AND group_id=".intval($group_id)." ";
  568. $result = Database::query($sql);
  569. return $result;
  570. }
  571. /**
  572. * Updates the group_rel_user table with a given user and group ids
  573. * @author Julio Montoya
  574. * @param int user id
  575. * @param int group id
  576. * @param int relation type
  577. * */
  578. public static function update_user_role($user_id, $group_id, $relation_type = GROUP_USER_PERMISSION_READER) {
  579. $table_group_rel_user = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  580. $group_id = intval($group_id);
  581. $user_id = intval($user_id);
  582. $sql = "UPDATE $table_group_rel_user
  583. SET relation_type = ".intval($relation_type)." WHERE user_id = $user_id AND group_id = $group_id" ;
  584. Database::query($sql);
  585. }
  586. public static function get_group_admin_list($user_id, $group_id) {
  587. $table_group_rel_user = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  588. $group_id = intval($group_id);
  589. $user_id = intval($user_id);
  590. $sql = "SELECT user_id FROM $table_group_rel_user WHERE
  591. relation_type = ".GROUP_USER_PERMISSION_ADMIN." AND user_id = $user_id AND group_id = $group_id" ;
  592. $result = Database::query($sql);
  593. }
  594. public static function get_all_group_tags($tag, $from=0, $number_of_items=10) {
  595. // database table definition
  596. $group_table = Database::get_main_table(TABLE_MAIN_GROUP);
  597. $table_tag = Database::get_main_table(TABLE_MAIN_TAG);
  598. $table_group_tag_values = Database::get_main_table(TABLE_MAIN_GROUP_REL_TAG);
  599. //default field_id == 1
  600. $field_id = 5;
  601. $tag = Database::escape_string($tag);
  602. $from = intval($from);
  603. $number_of_items = intval($number_of_items);
  604. // all the information of the field
  605. $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)
  606. INNER JOIN $group_table g ON(tv.group_id =g.id)
  607. WHERE tag LIKE '$tag%' AND field_id= $field_id ORDER BY tag";
  608. $sql .= " LIMIT $from,$number_of_items";
  609. $result = Database::query($sql);
  610. $return = array();
  611. if (Database::num_rows($result)> 0) {
  612. while ($row = Database::fetch_array($result,'ASSOC')) {
  613. $return[$row['id']] = $row;
  614. }
  615. }
  616. $keyword = $tag;
  617. $sql = "SELECT g.id, g.name, g.description, g.url, g.picture_uri FROM $group_table g";
  618. //@todo implement groups + multiple urls
  619. /*
  620. global $_configuration;
  621. if ($_configuration['multiple_access_urls'] && api_get_current_access_url_id()!=-1) {
  622. $access_url_rel_user_table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  623. $sql.= " INNER JOIN $access_url_rel_user_table url_rel_user ON (u.user_id=url_rel_user.user_id)";
  624. }*/
  625. //@todo implement visibility
  626. if (isset ($keyword)) {
  627. $sql .= " WHERE (g.name LIKE '%".$keyword."%' OR g.description LIKE '%".$keyword."%' OR g.url LIKE '%".$keyword."%' )";
  628. }
  629. $direction = 'ASC';
  630. if (!in_array($direction, array('ASC','DESC'))) {
  631. $direction = 'ASC';
  632. }
  633. //$column = intval($column);
  634. $from = intval($from);
  635. $number_of_items = intval($number_of_items);
  636. //$sql .= " ORDER BY col$column $direction ";
  637. $sql .= " LIMIT $from,$number_of_items";
  638. $res = Database::query($sql);
  639. if (Database::num_rows($res)> 0) {
  640. while ($row = Database::fetch_array($res,'ASSOC')) {
  641. if (!in_array($row['id'], $return)) {
  642. $return[$row['id']] = $row;
  643. }
  644. }
  645. }
  646. return $return;
  647. }
  648. /**
  649. * Creates new group pictures in various sizes of a user, or deletes user pfotos.
  650. * Note: This method relies on configuration setting from main/inc/conf/profile.conf.php
  651. * @param int The group id
  652. * @param string $file The common file name for the newly created pfotos. It will be checked and modified for compatibility with the file system.
  653. * If full name is provided, path component is ignored.
  654. * If an empty name is provided, then old user photos are deleted only, @see UserManager::delete_user_picture() as the prefered way for deletion.
  655. * @param string $source_file The full system name of the image from which user photos will be created.
  656. * @return string/bool Returns the resulting common file name of created images which usually should be stored in database.
  657. * When deletion is recuested returns empty string. In case of internal error or negative validation returns FALSE.
  658. */
  659. public static function update_group_picture($group_id, $file = null, $source_file = null) {
  660. // Validation 1.
  661. if (empty($group_id)) {
  662. return false;
  663. }
  664. $delete = empty($file);
  665. if (empty($source_file)) {
  666. $source_file = $file;
  667. }
  668. // User-reserved directory where photos have to be placed.
  669. $path_info = self::get_group_picture_path_by_id($group_id, 'system', true);
  670. $path = $path_info['dir'];
  671. // If this directory does not exist - we create it.
  672. if (!file_exists($path)) {
  673. @mkdir($path, api_get_permissions_for_new_directories(), true);
  674. }
  675. // The old photos (if any).
  676. $old_file = $path_info['file'];
  677. // Let us delete them.
  678. if (!empty($old_file)) {
  679. if (KEEP_THE_OLD_IMAGE_AFTER_CHANGE) {
  680. $prefix = 'saved_'.date('Y_m_d_H_i_s').'_'.uniqid('').'_';
  681. @rename($path.'small_'.$old_file, $path.$prefix.'small_'.$old_file);
  682. @rename($path.'medium_'.$old_file, $path.$prefix.'medium_'.$old_file);
  683. @rename($path.'big_'.$old_file, $path.$prefix.'big_'.$old_file);
  684. @rename($path.$old_file, $path.$prefix.$old_file);
  685. } else {
  686. @unlink($path.'small_'.$old_file);
  687. @unlink($path.'medium_'.$old_file);
  688. @unlink($path.'big_'.$old_file);
  689. @unlink($path.$old_file);
  690. }
  691. }
  692. // Exit if only deletion has been requested. Return an empty picture name.
  693. if ($delete) {
  694. return '';
  695. }
  696. // Validation 2.
  697. $allowed_types = array('jpg', 'jpeg', 'png', 'gif');
  698. $file = str_replace('\\', '/', $file);
  699. $filename = (($pos = strrpos($file, '/')) !== false) ? substr($file, $pos + 1) : $file;
  700. $extension = strtolower(substr(strrchr($filename, '.'), 1));
  701. if (!in_array($extension, $allowed_types)) {
  702. return false;
  703. }
  704. // This is the common name for the new photos.
  705. if (KEEP_THE_NAME_WHEN_CHANGE_IMAGE && !empty($old_file)) {
  706. $old_extension = strtolower(substr(strrchr($old_file, '.'), 1));
  707. $filename = in_array($old_extension, $allowed_types) ? substr($old_file, 0, -strlen($old_extension)) : $old_file;
  708. $filename = (substr($filename, -1) == '.') ? $filename.$extension : $filename.'.'.$extension;
  709. } else {
  710. $filename = api_replace_dangerous_char($filename);
  711. if (PREFIX_IMAGE_FILENAME_WITH_UID) {
  712. $filename = uniqid('').'_'.$filename;
  713. }
  714. // We always prefix user photos with user ids, so on setting
  715. // api_get_setting('split_users_upload_directory') === 'true'
  716. // the correspondent directories to be found successfully.
  717. $filename = $group_id.'_'.$filename;
  718. }
  719. // Storing the new photos in 4 versions with various sizes.
  720. $small = self::resize_picture($source_file, 22);
  721. $medium = self::resize_picture($source_file, 85);
  722. $normal = self::resize_picture($source_file, 200);
  723. $big = new Image($source_file); // This is the original picture.
  724. $ok = $small && $small->send_image($path.'small_'.$filename)
  725. && $medium && $medium->send_image($path.'medium_'.$filename)
  726. && $normal && $normal->send_image($path.'big_'.$filename)
  727. && $big && $big->send_image($path.$filename);
  728. return $ok ? $filename : false;
  729. }
  730. /**
  731. * Gets the group picture URL or path from group ID (returns an array).
  732. * The return format is a complete path, enabling recovery of the directory
  733. * with dirname() or the file with basename(). This also works for the
  734. * functions dealing with the user's productions, as they are located in
  735. * the same directory.
  736. * @param integer User ID
  737. * @param string Type of path to return (can be 'none', 'system', 'rel', 'web')
  738. * @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)
  739. * @param bool If we want that the function returns the /main/img/unknown.jpg image set it at true
  740. * @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
  741. */
  742. public static function get_group_picture_path_by_id($id, $type = 'none', $preview = false, $anonymous = false) {
  743. switch ($type) {
  744. case 'system': // Base: absolute system path.
  745. $base = api_get_path(SYS_CODE_PATH);
  746. break;
  747. case 'rel': // Base: semi-absolute web path (no server base).
  748. $base = api_get_path(REL_CODE_PATH);
  749. break;
  750. case 'web': // Base: absolute web path.
  751. $base = api_get_path(WEB_CODE_PATH);
  752. break;
  753. case 'none':
  754. default: // Base: empty, the result path below will be relative.
  755. $base = '';
  756. }
  757. if (empty($id) || empty($type)) {
  758. return $anonymous ? array('dir' => $base.'img/', 'file' => 'unknown.jpg') : array('dir' => '', 'file' => '');
  759. }
  760. $id = intval($id);
  761. $group_table = Database :: get_main_table(TABLE_MAIN_GROUP);
  762. $sql = "SELECT picture_uri FROM $group_table WHERE id=".$id;
  763. $res = Database::query($sql);
  764. if (!Database::num_rows($res)) {
  765. return $anonymous ? array('dir' => $base.'img/', 'file' => 'unknown.jpg') : array('dir' => '', 'file' => '');
  766. }
  767. $user = Database::fetch_array($res);
  768. $picture_filename = trim($user['picture_uri']);
  769. if (api_get_setting('split_users_upload_directory') === 'true') {
  770. if (!empty($picture_filename)) {
  771. $dir = $base.'upload/users/groups/'.substr($picture_filename, 0, 1).'/'.$id.'/';
  772. } elseif ($preview) {
  773. $dir = $base.'upload/users/groups/'.substr((string)$id, 0, 1).'/'.$id.'/';
  774. } else {
  775. $dir = $base.'upload/users/groups/'.$id.'/';
  776. }
  777. } else {
  778. $dir = $base.'upload/users/groups/'.$id.'/';
  779. }
  780. if (empty($picture_filename) && $anonymous) {
  781. return array('dir' => $base.'img/', 'file' => 'unknown.jpg');
  782. }
  783. return array('dir' => $dir, 'file' => $picture_filename);
  784. }
  785. /**
  786. * Resize a picture
  787. *
  788. * @param string file picture
  789. * @param int size in pixels
  790. * @return obj image object
  791. */
  792. public static function resize_picture($file, $max_size_for_picture) {
  793. $temp = new Image($file);
  794. $picture_infos = api_getimagesize($file);
  795. if ($picture_infos['width'] > $max_size_for_picture) {
  796. $thumbwidth = $max_size_for_picture;
  797. if (empty($thumbwidth) or $thumbwidth == 0) {
  798. $thumbwidth = $max_size_for_picture;
  799. }
  800. $new_height = round(($thumbwidth / $picture_infos['width']) * $picture_infos['height']);
  801. if ($new_height > $max_size_for_picture)
  802. $new_height = $thumbwidth;
  803. $temp->resize($thumbwidth, $new_height, 0);
  804. }
  805. return $temp;
  806. }
  807. /**
  808. * Gets the current group image
  809. * @param string group id
  810. * @param string picture group name
  811. * @param string height
  812. * @param string picture size it can be small_, medium_ or big_
  813. * @param string style css
  814. * @return array with the file and the style of an image i.e $array['file'] $array['style']
  815. */
  816. public static function get_picture_group($id, $picture_file, $height, $size_picture = GROUP_IMAGE_SIZE_MEDIUM , $style = '') {
  817. $patch_profile = 'upload/users/groups/';
  818. $picture = array();
  819. $picture['style'] = $style;
  820. if ($picture_file == 'unknown.jpg') {
  821. $picture['file'] = api_get_path(WEB_CODE_PATH).'img/'.$picture_file;
  822. return $picture;
  823. }
  824. switch ($size_picture) {
  825. case GROUP_IMAGE_SIZE_ORIGINAL :
  826. $size_picture = '';
  827. break;
  828. case GROUP_IMAGE_SIZE_BIG :
  829. $size_picture = 'big_';
  830. break;
  831. case GROUP_IMAGE_SIZE_MEDIUM :
  832. $size_picture = 'medium_';
  833. break;
  834. case GROUP_IMAGE_SIZE_SMALL :
  835. $size_picture = 'small_';
  836. break;
  837. default:
  838. $size_picture = 'medium_';
  839. }
  840. $image_array_sys = self::get_group_picture_path_by_id($id, 'system', false, true);
  841. $image_array = self::get_group_picture_path_by_id($id, 'web', false, true);
  842. $file = $image_array_sys['dir'].$size_picture.$picture_file;
  843. if (file_exists($file)) {
  844. $picture['file'] = $image_array['dir'].$size_picture.$picture_file;
  845. $picture['style'] = '';
  846. if ($height > 0) {
  847. $dimension = api_getimagesize($picture['file']);
  848. $margin = (($height - $dimension['width']) / 2);
  849. //@ todo the padding-top should not be here
  850. $picture['style'] = ' style="padding-top:'.$margin.'px; width:'.$dimension['width'].'px; height:'.$dimension['height'].';" ';
  851. }
  852. } else {
  853. //$file = api_get_path(SYS_CODE_PATH).$patch_profile.$user_id.'/'.$picture_file;
  854. $file = $image_array_sys['dir'].$picture_file;
  855. if (file_exists($file) && !is_dir($file)) {
  856. $picture['file'] = $image_array['dir'].$picture_file;
  857. } else {
  858. $picture['file'] = api_get_path(WEB_CODE_PATH).'img/unknown_group.png';
  859. }
  860. }
  861. return $picture;
  862. }
  863. public static function delete_group_picture($group_id) {
  864. return self::update_group_picture($group_id);
  865. }
  866. public static function is_group_admin($group_id, $user_id = 0) {
  867. if (empty($user_id)) {
  868. $user_id = api_get_user_id();
  869. }
  870. $user_role = GroupPortalManager::get_user_group_role($user_id, $group_id);
  871. if (in_array($user_role, array(GROUP_USER_PERMISSION_ADMIN))) {
  872. return true;
  873. } else {
  874. return false;
  875. }
  876. }
  877. public static function is_group_moderator($group_id, $user_id = 0) {
  878. if (empty($user_id)) {
  879. $user_id = api_get_user_id();
  880. }
  881. $user_role = GroupPortalManager::get_user_group_role($user_id, $group_id);
  882. if (in_array($user_role, array(GROUP_USER_PERMISSION_ADMIN, GROUP_USER_PERMISSION_MODERATOR))) {
  883. return true;
  884. } else {
  885. return false;
  886. }
  887. }
  888. public static function is_group_member($group_id, $user_id = 0) {
  889. if (empty($user_id)) {
  890. $user_id = api_get_user_id();
  891. }
  892. $user_role = GroupPortalManager::get_user_group_role($user_id, $group_id);
  893. if (in_array($user_role, array(GROUP_USER_PERMISSION_ADMIN, GROUP_USER_PERMISSION_MODERATOR, GROUP_USER_PERMISSION_READER, GROUP_USER_PERMISSION_HRM))) {
  894. return true;
  895. } else {
  896. return false;
  897. }
  898. }
  899. /**
  900. * Shows the left column of the group page
  901. * @param int group id
  902. * @param int user id
  903. *
  904. */
  905. public static function show_group_column_information($group_id, $user_id, $show = '') {
  906. global $relation_group_title, $my_group_role;
  907. $html = '';
  908. $group_info = GroupPortalManager::get_group_data($group_id);
  909. //$picture = GroupPortalManager::get_picture_group($group_id, $group_info['picture_uri'],160,GROUP_IMAGE_SIZE_MEDIUM);
  910. //$big_image = GroupPortalManager::get_picture_group($group_id, $group_info['picture_uri'],'',GROUP_IMAGE_SIZE_BIG);
  911. //$tags = GroupPortalManager::get_group_tags($group_id, true);
  912. //$groups_by_user = GroupPortalManager::get_groups_by_user($user_id, 0);
  913. //my relation with the group is set here
  914. $my_group_role = self::get_user_group_role($user_id, $group_id);
  915. //@todo this must be move to default.css for dev use only
  916. $html .= '<style>
  917. #group_members { width:270px; height:300px; overflow-x:none; overflow-y: auto;}
  918. .group_member_item { width:100px; height:130px; float:left; margin:5px 5px 15px 5px; }
  919. .group_member_picture { display:block;
  920. margin:0;
  921. overflow:hidden; };
  922. </style>';
  923. //Loading group permission
  924. $links = '';
  925. switch ($my_group_role) {
  926. case GROUP_USER_PERMISSION_READER:
  927. // I'm just a reader
  928. $relation_group_title = get_lang('IAmAReader');
  929. $links .= '<li><a href="group_invitation.php?id='.$group_id.'">'. Display::return_icon('invitation_friend.png', get_lang('InviteFriends'), array('hspace'=>'6')).'<span class="'.($show=='invite_friends'?'social-menu-text-active':'social-menu-text4').'" >'.get_lang('InviteFriends').'</span></a></li>';
  930. $links .= '<li><a href="groups.php?id='.$group_id.'&action=leave&u='.api_get_user_id().'">'. Display::return_icon('group_leave.png', get_lang('LeaveGroup'), array('hspace'=>'6')).'<span class="social-menu-text4" >'.get_lang('LeaveGroup').'</span></a></li>';
  931. break;
  932. case GROUP_USER_PERMISSION_ADMIN:
  933. $relation_group_title = get_lang('IAmAnAdmin');
  934. $links .= '<li><a href="group_edit.php?id='.$group_id.'">'. Display::return_icon('group_edit.png', get_lang('EditGroup'), array('hspace'=>'6')).'<span class="'.($show=='group_edit'?'social-menu-text-active':'social-menu-text4').'" >'.get_lang('EditGroup').'</span></a></li>';
  935. $links .= '<li><a href="group_waiting_list.php?id='.$group_id.'">'. Display::return_icon('waiting_list.png', get_lang('WaitingList'), array('hspace'=>'6')).'<span class="'.($show=='waiting_list'?'social-menu-text-active':'social-menu-text4').'" >'.get_lang('WaitingList').'</span></a></li>';
  936. $links .= '<li><a href="group_invitation.php?id='.$group_id.'">'. Display::return_icon('invitation_friend.png', get_lang('InviteFriends'), array('hspace'=>'6')).'<span class="'.($show=='invite_friends'?'social-menu-text-active':'social-menu-text4').'" >'.get_lang('InviteFriends').'</span></a></li>';
  937. $links .= '<li><a href="groups.php?id='.$group_id.'&action=leave&u='.api_get_user_id().'">'. Display::return_icon('group_leave.png', get_lang('LeaveGroup'), array('hspace'=>'6')).'<span class="social-menu-text4" >'.get_lang('LeaveGroup').'</span></a></li>';
  938. break;
  939. case GROUP_USER_PERMISSION_PENDING_INVITATION:
  940. // $links .= '<li><a href="groups.php?id='.$group_id.'&action=join&u='.api_get_user_id().'">'.Display::return_icon('addd.gif', get_lang('YouHaveBeenInvitedJoinNow'), array('hspace'=>'6')).'<span class="social-menu-text4" >'.get_lang('YouHaveBeenInvitedJoinNow').'</span></a></li>';
  941. break;
  942. case GROUP_USER_PERMISSION_PENDING_INVITATION_SENT_BY_USER:
  943. $relation_group_title = get_lang('WaitingForAdminResponse');
  944. break;
  945. case GROUP_USER_PERMISSION_MODERATOR:
  946. $relation_group_title = get_lang('IAmAModerator');
  947. //$links .= '<li><a href="'.api_get_path(WEB_CODE_PATH).'social/message_for_group_form.inc.php?view_panel=1&height=400&width=610&&user_friend='.api_get_user_id().'&group_id='.$group_id.'&action=add_message_group" class="thickbox" title="'.get_lang('ComposeMessage').'">'.Display::return_icon('compose_message.png', get_lang('NewTopic'), array('hspace'=>'6')).'<span class="social-menu-text4" >'.get_lang('NewTopic').'</span></a></li>';
  948. //$links .= '<li><a href="groups.php?id='.$group_id.'">'. Display::return_icon('message_list.png', get_lang('MessageList'), array('hspace'=>'6')).'<span class="'.($show=='messages_list'?'social-menu-text-active':'social-menu-text4').'" >'.get_lang('MessageList').'</span></a></li>';
  949. //$links .= '<li><a href="group_members.php?id='.$group_id.'">'. Display::return_icon('member_list.png', get_lang('MemberList'), array('hspace'=>'6')).'<span class="'.($show=='member_list'?'social-menu-text-active':'social-menu-text4').'" >'.get_lang('MemberList').'</span></a></li>';
  950. if ($group_info['visibility'] == GROUP_PERMISSION_CLOSED) {
  951. $links .= '<li><a href="group_waiting_list.php?id='.$group_id.'">'. Display::return_icon('waiting_list.png', get_lang('WaitingList'), array('hspace'=>'6')).'<span class="'.($show=='waiting_list'?'social-menu-text-active':'social-menu-text4').'" >'.get_lang('WaitingList').'</span></a></li>';
  952. }
  953. $links .= '<li><a href="group_invitation.php?id='.$group_id.'">'. Display::return_icon('invitation_friend.png', get_lang('InviteFriends'), array('hspace'=>'6')).'<span class="'.($show=='invite_friends'?'social-menu-text-active':'social-menu-text4').'" >'.get_lang('InviteFriends').'</span></a></li>';
  954. $links .= '<li><a href="groups.php?id='.$group_id.'&action=leave&u='.api_get_user_id().'">'. Display::return_icon('group_leave.png', get_lang('LeaveGroup'), array('hspace'=>'6')).'<span class="social-menu-text4" >'.get_lang('LeaveGroup').'</span></a></li>';
  955. break;
  956. case GROUP_USER_PERMISSION_HRM:
  957. $relation_group_title = get_lang('IAmAHRM');
  958. $links .= '<li><a href="'.api_get_path(WEB_CODE_PATH).'social/message_for_group_form.inc.php?view_panel=1&height=400&width=610&&user_friend='.api_get_user_id().'&group_id='.$group_id.'&action=add_message_group" class="ajax" title="'.get_lang('ComposeMessage').'">'.Display::return_icon('compose_message.png', get_lang('NewTopic'), array('hspace'=>'6')).'<span class="social-menu-text4" >'.get_lang('NewTopic').'</span></a></li>';
  959. $links .= '<li><a href="groups.php?id='.$group_id.'">'. Display::return_icon('message_list.png', get_lang('MessageList'), array('hspace'=>'6')).'<span class="'.($show=='messages_list'?'social-menu-text-active':'social-menu-text4').'" >'.get_lang('MessageList').'</span></a></li>';
  960. $links .= '<li><a href="group_invitation.php?id='.$group_id.'">'. Display::return_icon('invitation_friend.png', get_lang('InviteFriends'), array('hspace'=>'6')).'<span class="'.($show=='invite_friends'?'social-menu-text-active':'social-menu-text4').'" >'.get_lang('InviteFriends').'</span></a></li>';
  961. $links .= '<li><a href="group_members.php?id='.$group_id.'">'. Display::return_icon('member_list.png', get_lang('MemberList'), array('hspace'=>'6')).'<span class="'.($show=='member_list'?'social-menu-text-active':'social-menu-text4').'" >'.get_lang('MemberList').'</span></a></li>';
  962. $links .= '<li><a href="groups.php?id='.$group_id.'&action=leave&u='.api_get_user_id().'">'. Display::return_icon('delete_data.gif', get_lang('LeaveGroup'), array('hspace'=>'6')).'<span class="social-menu-text4" >'.get_lang('LeaveGroup').'</span></a></li>';
  963. break;
  964. default:
  965. //$links .= '<li><a href="groups.php?id='.$group_id.'&action=join&u='.api_get_user_id().'">'.Display::return_icon('addd.gif', get_lang('JoinGroup'), array('hspace'=>'6')).'<span class="social-menu-text4" >'.get_lang('JoinGroup').'</a></span></li>';
  966. break;
  967. }
  968. if (!empty($links)) {
  969. $html .= '<div class="well sidebar-nav"><ul class="nav nav-list">';
  970. if (!empty($group_info['description'])) {
  971. $html .= Display::tag('li', Security::remove_XSS($group_info['description'], STUDENT, true), array('class'=>'group_description'));
  972. }
  973. $html .= $links;
  974. $html .= '</ul></div>';
  975. }
  976. return $html;
  977. }
  978. function delete_topic($group_id, $topic_id) {
  979. $table_message = Database::get_main_table(TABLE_MESSAGE);
  980. $topic_id = intval($topic_id);
  981. $group_id = intval($group_id);
  982. $sql = "UPDATE $table_message SET msg_status=3 WHERE group_id = $group_id AND (id = '$topic_id' OR parent_id = $topic_id) ";
  983. Database::query($sql);
  984. }
  985. public static function get_groups_by_user_count($user_id = '', $relation_type = GROUP_USER_PERMISSION_READER, $with_image = false) {
  986. $table_group_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_GROUP);
  987. $tbl_group = Database::get_main_table(TABLE_MAIN_GROUP);
  988. $user_id = intval($user_id);
  989. if ($relation_type == 0) {
  990. $where_relation_condition = '';
  991. } else {
  992. $relation_type = intval($relation_type);
  993. $where_relation_condition = "AND gu.relation_type = $relation_type ";
  994. }
  995. $sql = "SELECT count(g.id) as count
  996. FROM $tbl_group g
  997. INNER JOIN $table_group_rel_user gu
  998. ON gu.group_id = g.id WHERE gu.user_id = $user_id $where_relation_condition ";
  999. $result = Database::query($sql);
  1000. if (Database::num_rows($result) > 0) {
  1001. $row = Database::fetch_array($result, 'ASSOC');
  1002. return $row['count'];
  1003. }
  1004. return 0;
  1005. }
  1006. }