group_portal_manager.lib.php 56 KB

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