group_portal_manager.lib.php 54 KB

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