group_portal_manager.lib.php 44 KB

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