group_portal_manager.lib.php 54 KB

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