group_portal_manager.lib.php 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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.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.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['id']);
  541. $row['image'] = '<img src="'.$picture.'" />';
  542. }
  543. $array[$row['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.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.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['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. $em = Database::getManager();
  583. $result = $em
  584. ->getRepository('ChamiloCoreBundle:UsergroupRelUser')
  585. ->findOneBy([
  586. 'usergroup' => intval($group_id),
  587. 'user' => intval($user_id)
  588. ]);
  589. if (!$result) {
  590. return 0;
  591. }
  592. return $result->getRelationType();
  593. }
  594. /**
  595. * Add a user into a group
  596. * @author Julio Montoya
  597. * @param int user_id
  598. * @param int url_id
  599. * @return boolean true if success
  600. **/
  601. public static function add_user_to_group($user_id, $group_id, $relation_type = GROUP_USER_PERMISSION_READER)
  602. {
  603. $result = false;
  604. if (empty($user_id) || empty($group_id)) {
  605. return false;
  606. }
  607. $em = Database::getManager();
  608. $user = $em->find('ChamiloCoreBundle:User', $user_id);
  609. $usergroup = $em->find('ChamiloCoreBundle:Usergroup', $group_id);
  610. $role = self::get_user_group_role($user_id, $group_id);
  611. if ($role == 0) {
  612. $usergroupRelUser = new \Chamilo\CoreBundle\Entity\UsergroupRelUser();
  613. $usergroupRelUser
  614. ->setUser($user)
  615. ->setUsergroup($usergroup)
  616. ->setRelationType($relation_type);
  617. $em->persist($usergroupRelUser);
  618. $em->flush();
  619. Event::addEvent(
  620. LOG_GROUP_PORTAL_USER_SUBSCRIBED,
  621. LOG_GROUP_PORTAL_REL_USER_ARRAY,
  622. array(
  623. 'user_id' => $user_id,
  624. 'group_id' => $group_id,
  625. 'relation_type' => $relation_type,
  626. )
  627. );
  628. return true;
  629. } else if ($role == GROUP_USER_PERMISSION_PENDING_INVITATION) {
  630. // If somebody already invited me I can be added
  631. self::update_user_role(
  632. $user_id,
  633. $group_id,
  634. GROUP_USER_PERMISSION_READER
  635. );
  636. return true;
  637. }
  638. return $result;
  639. }
  640. /**
  641. * Add a group of users into a group of URLs
  642. * @author Julio Montoya
  643. * @param array $user_list of user_ids
  644. * @param array $group_list of url_ids
  645. * @param string $relation_type
  646. **/
  647. public static function add_users_to_groups($user_list, $group_list, $relation_type = GROUP_USER_PERMISSION_READER)
  648. {
  649. $result_array = array();
  650. $relation_type = intval($relation_type);
  651. if (is_array($user_list) && is_array($group_list)) {
  652. foreach ($group_list as $group_id) {
  653. foreach ($user_list as $user_id) {
  654. $result = self::add_user_to_group($user_id, $group_id, $relation_type);
  655. if ($result) {
  656. $result_array[$group_id][$user_id] = 1;
  657. } else {
  658. $result_array[$group_id][$user_id] = 0;
  659. }
  660. }
  661. }
  662. }
  663. return $result_array;
  664. }
  665. /**
  666. * Deletes a group and user relationship
  667. * @author Julio Montoya
  668. * @param int $group_id
  669. * @param int $relation_type (optional)
  670. * @return boolean true if success
  671. * */
  672. public static function delete_users($group_id, $relation_type = null)
  673. {
  674. $table = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  675. $condition_relation = "";
  676. if (!empty($relation_type)) {
  677. $relation_type = intval($relation_type);
  678. $condition_relation = " AND relation_type = '$relation_type'";
  679. }
  680. $sql = "DELETE FROM $table
  681. WHERE group_id = ".intval($group_id).$condition_relation;
  682. $result = Database::query($sql);
  683. Event::addEvent(
  684. LOG_GROUP_PORTAL_USER_DELETE_ALL,
  685. LOG_GROUP_PORTAL_REL_USER_ARRAY,
  686. array('group_id' => $group_id, 'relation_type' => $relation_type)
  687. );
  688. return $result;
  689. }
  690. /**
  691. * Deletes an url and session relationship
  692. * @author Julio Montoya
  693. * @param int $user_id
  694. * @param int $group_id
  695. * @return boolean true if success
  696. * */
  697. public static function delete_user_rel_group($user_id, $group_id)
  698. {
  699. $em = Database::getManager();
  700. $result = $em
  701. ->getRepository('ChamiloCoreBundle:UsergroupRelUser')
  702. ->findOneBy([
  703. 'usergroup' => intval($group_id),
  704. 'user' => intval($user_id)
  705. ]);
  706. if (!$result) {
  707. return false;
  708. }
  709. $em->remove($result);
  710. $em->flush();
  711. Event::addEvent(
  712. LOG_GROUP_PORTAL_USER_UNSUBSCRIBED,
  713. LOG_GROUP_PORTAL_REL_USER_ARRAY,
  714. array('user_id' => $user_id, 'group_id' => $group_id)
  715. );
  716. return true;
  717. }
  718. /**
  719. * Updates the group_rel_user table with a given user and group ids
  720. * @author Julio Montoya
  721. * @param int $user_id
  722. * @param int $group_id
  723. * @param int $relation_type
  724. *
  725. * @return bool
  726. **/
  727. public static function update_user_role($user_id, $group_id, $relation_type = GROUP_USER_PERMISSION_READER)
  728. {
  729. if (empty($user_id) || empty($group_id) || empty($relation_type)) {
  730. return false;
  731. }
  732. $em = Database::getManager();
  733. $group_id = intval($group_id);
  734. $user_id = intval($user_id);
  735. $usergroupUser = $em
  736. ->getRepository('ChamiloCoreBundle:UsergroupRelUser')
  737. ->findOneBy([
  738. 'user' => $user_id,
  739. 'usergroup' => $group_id
  740. ]);
  741. if (!$usergroupUser) {
  742. return false;
  743. }
  744. $usergroupUser->setRelationType($relation_type);
  745. $em->merge($usergroupUser);
  746. $em->flush();
  747. Event::addEvent(
  748. LOG_GROUP_PORTAL_USER_UPDATE_ROLE,
  749. LOG_GROUP_PORTAL_REL_USER_ARRAY,
  750. array('user_id' => $user_id, 'group_id' => $group_id, 'relation_type' => $relation_type)
  751. );
  752. return true;
  753. }
  754. /**
  755. * @param int $user_id
  756. * @param int $group_id
  757. */
  758. public static function get_group_admin_list($user_id, $group_id)
  759. {
  760. $table_group_rel_user = Database :: get_main_table(TABLE_MAIN_USER_REL_GROUP);
  761. $group_id = intval($group_id);
  762. $user_id = intval($user_id);
  763. $sql = "SELECT user_id FROM $table_group_rel_user
  764. WHERE
  765. relation_type = ".GROUP_USER_PERMISSION_ADMIN." AND
  766. user_id = $user_id AND
  767. group_id = $group_id";
  768. Database::query($sql);
  769. }
  770. /**
  771. * @param string $tag
  772. * @param int $from
  773. * @param int $number_of_items
  774. * @param bool $getCount
  775. * @return array
  776. */
  777. public static function get_all_group_tags($tag, $from = 0, $number_of_items = 10, $getCount = false)
  778. {
  779. // Database table definition
  780. $group_table = Database::get_main_table(TABLE_MAIN_GROUP);
  781. $table_tag = Database::get_main_table(TABLE_MAIN_TAG);
  782. $table_group_tag_values = Database::get_main_table(TABLE_MAIN_GROUP_REL_TAG);
  783. $field_id = 5;
  784. $from = intval($from);
  785. $number_of_items = intval($number_of_items);
  786. // all the information of the field
  787. if ($getCount) {
  788. $select = "SELECT count(DISTINCT g.id) count";
  789. } else {
  790. $select = " SELECT DISTINCT g.id, g.name, g.description, g.picture_uri ";
  791. }
  792. $sql = " $select
  793. FROM $group_table g
  794. LEFT JOIN $table_group_tag_values tv ON (g.id AND tv.group_id)
  795. LEFT JOIN $table_tag t ON (tv.tag_id = t.id)
  796. WHERE
  797. tag LIKE '$tag%' AND field_id= $field_id OR
  798. (
  799. g.name LIKE '".Database::escape_string('%'.$tag.'%')."' OR
  800. g.description LIKE '".Database::escape_string('%'.$tag.'%')."' OR
  801. g.url LIKE '".Database::escape_string('%'.$tag.'%')."'
  802. )";
  803. $sql .= " LIMIT $from, $number_of_items";
  804. $result = Database::query($sql);
  805. $return = array();
  806. if (Database::num_rows($result) > 0) {
  807. if ($getCount) {
  808. $row = Database::fetch_array($result, 'ASSOC');
  809. return $row['count'];
  810. }
  811. while ($row = Database::fetch_array($result, 'ASSOC')) {
  812. $return[$row['id']] = $row;
  813. }
  814. }
  815. return $return;
  816. }
  817. /**
  818. * Creates new group pictures in various sizes of a user, or deletes user photos.
  819. * Note: This method relies on configuration setting from main/inc/conf/profile.conf.php
  820. * @param int The group id
  821. * @param string $file The common file name for the newly created photos.
  822. * It will be checked and modified for compatibility with the file system.
  823. * If full name is provided, path component is ignored.
  824. * If an empty name is provided, then old user photos are deleted only, @see UserManager::delete_user_picture()
  825. * as the prefered way for deletion.
  826. * @param string $source_file The full system name of the image from which user photos will be created.
  827. * @return string/bool Returns the resulting file name of created images which usually should be stored in DB.
  828. * When deletion is recuested returns empty string. In case of internal error or negative validation returns FALSE.
  829. */
  830. public static function update_group_picture($group_id, $file = null, $source_file = null)
  831. {
  832. // Validation.
  833. if (empty($group_id)) {
  834. return false;
  835. }
  836. $delete = empty($file);
  837. if (empty($source_file)) {
  838. $source_file = $file;
  839. }
  840. // User-reserved directory where photos have to be placed.
  841. $path_info = self::get_group_picture_path_by_id($group_id, 'system', true);
  842. $path = $path_info['dir'];
  843. // If this directory does not exist - we create it.
  844. if (!file_exists($path)) {
  845. @mkdir($path, api_get_permissions_for_new_directories(), true);
  846. }
  847. // The old photos (if any).
  848. $old_file = $path_info['file'];
  849. // Let us delete them.
  850. if (!empty($old_file)) {
  851. if (KEEP_THE_OLD_IMAGE_AFTER_CHANGE) {
  852. $prefix = 'saved_'.date('Y_m_d_H_i_s').'_'.uniqid('').'_';
  853. @rename($path.'small_'.$old_file, $path.$prefix.'small_'.$old_file);
  854. @rename($path.'medium_'.$old_file, $path.$prefix.'medium_'.$old_file);
  855. @rename($path.'big_'.$old_file, $path.$prefix.'big_'.$old_file);
  856. @rename($path.$old_file, $path.$prefix.$old_file);
  857. } else {
  858. @unlink($path.'small_'.$old_file);
  859. @unlink($path.'medium_'.$old_file);
  860. @unlink($path.'big_'.$old_file);
  861. @unlink($path.$old_file);
  862. }
  863. }
  864. // Exit if only deletion has been requested. Return an empty picture name.
  865. if ($delete) {
  866. return '';
  867. }
  868. // Validation 2.
  869. $allowed_types = array('jpg', 'jpeg', 'png', 'gif');
  870. $file = str_replace('\\', '/', $file);
  871. $filename = (($pos = strrpos($file, '/')) !== false) ? substr($file, $pos + 1) : $file;
  872. $extension = strtolower(substr(strrchr($filename, '.'), 1));
  873. if (!in_array($extension, $allowed_types)) {
  874. return false;
  875. }
  876. // This is the common name for the new photos.
  877. if (KEEP_THE_NAME_WHEN_CHANGE_IMAGE && !empty($old_file)) {
  878. $old_extension = strtolower(substr(strrchr($old_file, '.'), 1));
  879. $filename = in_array($old_extension, $allowed_types) ? substr($old_file, 0, -strlen($old_extension)) : $old_file;
  880. $filename = (substr($filename, -1) == '.') ? $filename.$extension : $filename.'.'.$extension;
  881. } else {
  882. $filename = api_replace_dangerous_char($filename);
  883. if (PREFIX_IMAGE_FILENAME_WITH_UID) {
  884. $filename = uniqid('').'_'.$filename;
  885. }
  886. // We always prefix user photos with user ids, so on setting
  887. // api_get_setting('split_users_upload_directory') === 'true'
  888. // the correspondent directories to be found successfully.
  889. $filename = $group_id.'_'.$filename;
  890. }
  891. // Storing the new photos in 4 versions with various sizes.
  892. $small = self::resize_picture($source_file, 22);
  893. $medium = self::resize_picture($source_file, 85);
  894. $normal = self::resize_picture($source_file, 200);
  895. $big = new Image($source_file); // This is the original picture.
  896. $ok = $small && $small->send_image($path.'small_'.$filename)
  897. && $medium && $medium->send_image($path.'medium_'.$filename)
  898. && $normal && $normal->send_image($path.'big_'.$filename)
  899. && $big && $big->send_image($path.$filename);
  900. return $ok ? $filename : false;
  901. }
  902. /**
  903. * Gets the group picture URL or path from group ID (returns an array).
  904. * The return format is a complete path, enabling recovery of the directory
  905. * with dirname() or the file with basename(). This also works for the
  906. * functions dealing with the user's productions, as they are located in
  907. * the same directory.
  908. * @internal Don't delete this function
  909. * @param integer $id
  910. * @param string $type Type of path to return (can be 'system', 'web')
  911. * @param bool $preview Whether we want to have the directory name returned 'as if' there was a file or not
  912. * (in the case we want to know which directory to create - otherwise no file means no split subdir)
  913. * @param bool $anonymous If we want that the function returns the /main/img/unknown.jpg image set it at true
  914. *
  915. * @return array Array of 2 elements: 'dir' and 'file' which contain the dir and file as the name implies
  916. * if image does not exist it will return the unknow image if anonymous parameter is true if not it returns an empty
  917. */
  918. public static function get_group_picture_path_by_id($id, $type = 'web', $preview = false, $anonymous = false)
  919. {
  920. switch ($type) {
  921. case 'system': // Base: absolute system path.
  922. $base = api_get_path(SYS_UPLOAD_PATH);
  923. break;
  924. case 'web': // Base: absolute web path.
  925. default:
  926. $base = api_get_path(WEB_UPLOAD_PATH);
  927. break;
  928. }
  929. $noPicturePath = array('dir' => $base.'img/', 'file' => 'unknown.jpg');
  930. if (empty($id) || empty($type)) {
  931. return $anonymous ? $noPicturePath : array('dir' => '', 'file' => '');
  932. }
  933. $id = intval($id);
  934. $group_table = Database :: get_main_table(TABLE_MAIN_GROUP);
  935. $sql = "SELECT picture_uri FROM $group_table WHERE id=".$id;
  936. $res = Database::query($sql);
  937. if (!Database::num_rows($res)) {
  938. return $anonymous ? $noPicturePath : array('dir' => '', 'file' => '');
  939. }
  940. $user = Database::fetch_array($res);
  941. $picture_filename = trim($user['picture_uri']);
  942. if (api_get_setting(
  943. 'profile.split_users_upload_directory'
  944. ) === 'true'
  945. ) {
  946. if (!empty($picture_filename)) {
  947. $dir = $base.'groups/'.substr($picture_filename, 0, 1).'/'.$id.'/';
  948. } elseif ($preview) {
  949. $dir = $base.'groups/'.substr((string) $id, 0, 1).'/'.$id.'/';
  950. } else {
  951. $dir = $base.'groups/'.$id.'/';
  952. }
  953. } else {
  954. $dir = $base.'groups/'.$id.'/';
  955. }
  956. if (empty($picture_filename) && $anonymous) {
  957. return $noPicturePath;
  958. }
  959. return array('dir' => $dir, 'file' => $picture_filename);
  960. }
  961. /**
  962. * Resize a picture
  963. *
  964. * @param string file picture
  965. * @param int size in pixels
  966. * @return obj image object
  967. */
  968. public static function resize_picture($file, $max_size_for_picture)
  969. {
  970. $temp = new Image($file);
  971. $picture_infos = api_getimagesize($file);
  972. if ($picture_infos['width'] > $max_size_for_picture) {
  973. $thumbwidth = $max_size_for_picture;
  974. if (empty($thumbwidth) or $thumbwidth == 0) {
  975. $thumbwidth = $max_size_for_picture;
  976. }
  977. $new_height = round(($thumbwidth / $picture_infos['width']) * $picture_infos['height']);
  978. if ($new_height > $max_size_for_picture)
  979. $new_height = $thumbwidth;
  980. $temp->resize($thumbwidth, $new_height, 0);
  981. }
  982. return $temp;
  983. }
  984. /**
  985. * Gets the current group image
  986. * @param string $id group id
  987. * @param string $picture_file picture group name
  988. * @param string $height
  989. * @param string $size_picture picture size it can be small_, medium_or big_
  990. * @param string $style css
  991. * @return array with the file and the style of an image i.e $array['file'] $array['style']
  992. */
  993. public static function get_picture_group(
  994. $id,
  995. $picture_file,
  996. $height,
  997. $size_picture = GROUP_IMAGE_SIZE_MEDIUM,
  998. $style = ''
  999. ) {
  1000. $picture = array();
  1001. $picture['style'] = $style;
  1002. if ($picture_file == 'unknown.jpg') {
  1003. $picture['file'] = api_get_path(WEB_CODE_PATH).'img/'.$picture_file;
  1004. return $picture;
  1005. }
  1006. switch ($size_picture) {
  1007. case GROUP_IMAGE_SIZE_ORIGINAL:
  1008. $size_picture = '';
  1009. break;
  1010. case GROUP_IMAGE_SIZE_BIG:
  1011. $size_picture = 'big_';
  1012. break;
  1013. case GROUP_IMAGE_SIZE_MEDIUM:
  1014. $size_picture = 'medium_';
  1015. break;
  1016. case GROUP_IMAGE_SIZE_SMALL:
  1017. $size_picture = 'small_';
  1018. break;
  1019. default:
  1020. $size_picture = 'medium_';
  1021. }
  1022. $image_array_sys = self::get_group_picture_path_by_id($id, 'system', false, true);
  1023. $image_array = self::get_group_picture_path_by_id($id, 'web', false, true);
  1024. $file = $image_array_sys['dir'].$size_picture.$picture_file;
  1025. if (file_exists($file)) {
  1026. $picture['file'] = $image_array['dir'].$size_picture.$picture_file;
  1027. $picture['style'] = '';
  1028. if ($height > 0) {
  1029. $dimension = api_getimagesize($picture['file']);
  1030. $margin = (($height - $dimension['width']) / 2);
  1031. //@ todo the padding-top should not be here
  1032. $picture['style'] = ' style="padding-top:'.$margin.'px; width:'.$dimension['width'].'px; height:'.$dimension['height'].';" ';
  1033. }
  1034. } else {
  1035. $file = $image_array_sys['dir'].$picture_file;
  1036. if (file_exists($file) && !is_dir($file)) {
  1037. $picture['file'] = $image_array['dir'].$picture_file;
  1038. } else {
  1039. $picture['file'] = api_get_path(WEB_CODE_PATH).'img/unknown_group.png';
  1040. }
  1041. }
  1042. return $picture;
  1043. }
  1044. /**
  1045. * @param int $group_id
  1046. * @return string
  1047. */
  1048. public static function delete_group_picture($group_id)
  1049. {
  1050. return self::update_group_picture($group_id);
  1051. }
  1052. /**
  1053. * @param int $group_id
  1054. * @param int $user_id
  1055. * @return bool
  1056. */
  1057. public static function is_group_admin($group_id, $user_id = 0)
  1058. {
  1059. if (empty($user_id)) {
  1060. $user_id = api_get_user_id();
  1061. }
  1062. $user_role = GroupPortalManager::get_user_group_role($user_id, $group_id);
  1063. if (in_array($user_role, array(GROUP_USER_PERMISSION_ADMIN))) {
  1064. return true;
  1065. } else {
  1066. return false;
  1067. }
  1068. }
  1069. /**
  1070. * @param int $group_id
  1071. * @param int $user_id
  1072. * @return bool
  1073. */
  1074. public static function is_group_moderator($group_id, $user_id = 0)
  1075. {
  1076. if (empty($user_id)) {
  1077. $user_id = api_get_user_id();
  1078. }
  1079. $user_role = GroupPortalManager::get_user_group_role($user_id, $group_id);
  1080. if (in_array($user_role, array(GROUP_USER_PERMISSION_ADMIN, GROUP_USER_PERMISSION_MODERATOR))) {
  1081. return true;
  1082. } else {
  1083. return false;
  1084. }
  1085. }
  1086. /**
  1087. * @param int $group_id
  1088. * @param int $user_id
  1089. * @return bool
  1090. */
  1091. public static function is_group_member($group_id, $user_id = 0)
  1092. {
  1093. if (empty($user_id)) {
  1094. $user_id = api_get_user_id();
  1095. }
  1096. $user_role = GroupPortalManager::get_user_group_role($user_id, $group_id);
  1097. $permissions = array(
  1098. GROUP_USER_PERMISSION_ADMIN,
  1099. GROUP_USER_PERMISSION_MODERATOR,
  1100. GROUP_USER_PERMISSION_READER,
  1101. GROUP_USER_PERMISSION_HRM
  1102. );
  1103. if (in_array($user_role, $permissions)) {
  1104. return true;
  1105. } else {
  1106. return false;
  1107. }
  1108. }
  1109. /**
  1110. * Shows the left column of the group page
  1111. * @param int $group_id
  1112. * @param int $user_id
  1113. *
  1114. */
  1115. public static function show_group_column_information($group_id, $user_id, $show = '')
  1116. {
  1117. global $relation_group_title, $my_group_role;
  1118. $html = '';
  1119. $group_info = GroupPortalManager::get_group_data($group_id);
  1120. // My relation with the group is set here.
  1121. $my_group_role = self::get_user_group_role($user_id, $group_id);
  1122. //@todo this must be move to default.css for dev use only
  1123. $html .= '<style>
  1124. #group_members { width:270px; height:300px; overflow-x:none; overflow-y: auto;}
  1125. .group_member_item { width:100px; height:130px; float:left; margin:5px 5px 15px 5px; }
  1126. .group_member_picture { display:block;
  1127. margin:0;
  1128. overflow:hidden; };
  1129. </style>';
  1130. //Loading group permission
  1131. $links = '';
  1132. switch ($my_group_role) {
  1133. case GROUP_USER_PERMISSION_READER:
  1134. // I'm just a reader
  1135. $relation_group_title = get_lang('IAmAReader');
  1136. $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>';
  1137. if (GroupPortalManager::canLeave($group_info)) {
  1138. $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>';
  1139. }
  1140. break;
  1141. case GROUP_USER_PERMISSION_ADMIN:
  1142. $relation_group_title = get_lang('IAmAnAdmin');
  1143. $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>';
  1144. $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>';
  1145. $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>';
  1146. if (GroupPortalManager::canLeave($group_info)) {
  1147. $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>';
  1148. }
  1149. break;
  1150. case GROUP_USER_PERMISSION_PENDING_INVITATION:
  1151. // $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>';
  1152. break;
  1153. case GROUP_USER_PERMISSION_PENDING_INVITATION_SENT_BY_USER:
  1154. $relation_group_title = get_lang('WaitingForAdminResponse');
  1155. break;
  1156. case GROUP_USER_PERMISSION_MODERATOR:
  1157. $relation_group_title = get_lang('IAmAModerator');
  1158. if ($group_info['visibility'] == GROUP_PERMISSION_CLOSED) {
  1159. $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>';
  1160. }
  1161. $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>';
  1162. if (GroupPortalManager::canLeave($group_info)) {
  1163. $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>';
  1164. }
  1165. break;
  1166. case GROUP_USER_PERMISSION_HRM:
  1167. $relation_group_title = get_lang('IAmAHRM');
  1168. $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-size="lg" data-title="'.get_lang('ComposeMessage').' title="'.get_lang('ComposeMessage').'">'.Display::return_icon('new-message.png', get_lang('NewTopic'), array('hspace' => '6')).'<span class="social-menu-text4" >'.get_lang('NewTopic').'</span></a></li>';
  1169. $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>';
  1170. $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>';
  1171. $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>';
  1172. $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>';
  1173. break;
  1174. default:
  1175. //$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>';
  1176. break;
  1177. }
  1178. if (!empty($links)) {
  1179. $html .= '<div class="well sidebar-nav"><ul class="nav nav-list">';
  1180. if (!empty($group_info['description'])) {
  1181. $html .= Display::tag('li', Security::remove_XSS($group_info['description'], STUDENT, true), array('class' => 'group_description'));
  1182. }
  1183. $html .= $links;
  1184. $html .= '</ul></div>';
  1185. }
  1186. return $html;
  1187. }
  1188. /**
  1189. * @param int $group_id
  1190. * @param int $topic_id
  1191. */
  1192. function delete_topic($group_id, $topic_id)
  1193. {
  1194. $table_message = Database::get_main_table(TABLE_MESSAGE);
  1195. $topic_id = intval($topic_id);
  1196. $group_id = intval($group_id);
  1197. $sql = "UPDATE $table_message SET msg_status=3
  1198. WHERE group_id = $group_id AND (id = '$topic_id' OR parent_id = $topic_id) ";
  1199. Database::query($sql);
  1200. }
  1201. /**
  1202. * @param int $user_id
  1203. * @param int $relation_type
  1204. * @param bool $with_image
  1205. * @return int
  1206. */
  1207. public static function get_groups_by_user_count($user_id = null, $relation_type = GROUP_USER_PERMISSION_READER, $with_image = false)
  1208. {
  1209. $table_group_rel_user = Database::get_main_table(TABLE_MAIN_USER_REL_GROUP);
  1210. $tbl_group = Database::get_main_table(TABLE_MAIN_GROUP);
  1211. $user_id = intval($user_id);
  1212. if ($relation_type == 0) {
  1213. $where_relation_condition = '';
  1214. } else {
  1215. $relation_type = intval($relation_type);
  1216. $where_relation_condition = "AND gu.relation_type = $relation_type ";
  1217. }
  1218. $sql = "SELECT count(g.id) as count
  1219. FROM $tbl_group g
  1220. INNER JOIN $table_group_rel_user gu
  1221. ON gu.group_id = g.id WHERE gu.user_id = $user_id $where_relation_condition ";
  1222. $result = Database::query($sql);
  1223. if (Database::num_rows($result) > 0) {
  1224. $row = Database::fetch_array($result, 'ASSOC');
  1225. return $row['count'];
  1226. }
  1227. return 0;
  1228. }
  1229. /**
  1230. * @param FormValidator $form
  1231. * @param array
  1232. *
  1233. * @return FormValidator
  1234. */
  1235. public static function setGroupForm($form, $groupData = array())
  1236. {
  1237. // Name
  1238. $form->addElement('text', 'name', get_lang('Name'), array('maxlength'=>120));
  1239. $form->applyFilter('name', 'html_filter');
  1240. $form->applyFilter('name', 'trim');
  1241. $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
  1242. // Description
  1243. $form->addElement(
  1244. 'textarea',
  1245. 'description',
  1246. get_lang('Description'),
  1247. array(
  1248. 'cols' => 58,
  1249. 'onKeyDown' => "textarea_maxlength()",
  1250. 'onKeyUp' => "textarea_maxlength()",
  1251. )
  1252. );
  1253. $form->applyFilter('description', 'html_filter');
  1254. $form->applyFilter('description', 'trim');
  1255. $form->addRule('name', '', 'maxlength', 255);
  1256. // Url
  1257. $form->addElement('text', 'url', 'URL');
  1258. $form->applyFilter('url', 'html_filter');
  1259. $form->applyFilter('url', 'trim');
  1260. // Picture
  1261. $form->addElement('file', 'picture', get_lang('AddPicture'));
  1262. $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
  1263. $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  1264. if (!empty($groupData)) {
  1265. if (isset($groupData['picture_uri']) && strlen($groupData['picture_uri']) > 0) {
  1266. $form->addElement('checkbox', 'delete_picture', '', get_lang('DelImage'));
  1267. }
  1268. }
  1269. // Status
  1270. $status = array();
  1271. $status[GROUP_PERMISSION_OPEN] = get_lang('Open');
  1272. $status[GROUP_PERMISSION_CLOSED] = get_lang('Closed');
  1273. $form->addElement('select', 'visibility', get_lang('GroupPermissions'), $status, array());
  1274. if (!empty($groupData)) {
  1275. if (self::canLeaveFeatureEnabled($groupData)) {
  1276. $form->addElement('checkbox', 'allow_members_leave_group', '', get_lang('AllowMemberLeaveGroup'));
  1277. }
  1278. // Set default values
  1279. $form->setDefaults($groupData);
  1280. }
  1281. return $form;
  1282. }
  1283. /**
  1284. * Check if the can leave feature exists.
  1285. * @param array $groupData
  1286. * @return bool
  1287. */
  1288. public static function canLeaveFeatureEnabled($groupData)
  1289. {
  1290. if (isset($groupData['allow_members_leave_group'])) {
  1291. return true;
  1292. }
  1293. return false;
  1294. }
  1295. /**
  1296. * @param array $groupData
  1297. * @return bool
  1298. */
  1299. public static function canLeave($groupData)
  1300. {
  1301. if (self::canLeaveFeatureEnabled($groupData)) {
  1302. return $groupData['allow_members_leave_group'] == 1 ? true : false;
  1303. }
  1304. return true;
  1305. }
  1306. /**
  1307. * Get the group member list by a user and his group role
  1308. * @param int $userId The user ID
  1309. * @param int $relationType Optional. The relation type. GROUP_USER_PERMISSION_ADMIN by default
  1310. * @param boolean $includeSubgroupsUsers Optional. Whether include the users from subgroups
  1311. * @return array
  1312. */
  1313. public static function getGroupUsersByUser(
  1314. $userId,
  1315. $relationType = GROUP_USER_PERMISSION_ADMIN,
  1316. $includeSubgroupsUsers = true
  1317. )
  1318. {
  1319. $userId = intval($userId);
  1320. $groups = GroupPortalManager::get_groups_by_user($userId, $relationType);
  1321. $groupsId = array_keys($groups);
  1322. $subgroupsId = [];
  1323. $userIdList = [];
  1324. if ($includeSubgroupsUsers) {
  1325. foreach ($groupsId as $groupId) {
  1326. $subgroupsId = array_merge($subgroupsId, GroupPortalManager::getGroupsByDepthLevel($groupId));
  1327. }
  1328. $groupsId = array_merge($groupsId, $subgroupsId);
  1329. }
  1330. $groupsId = array_unique($groupsId);
  1331. if (empty($groupsId)) {
  1332. return [];
  1333. }
  1334. foreach ($groupsId as $groupId) {
  1335. $groupUsers = GroupPortalManager::get_users_by_group($groupId);
  1336. if (empty($groupUsers)) {
  1337. continue;
  1338. }
  1339. foreach ($groupUsers as $member) {
  1340. if ($member['user_id'] == $userId) {
  1341. continue;
  1342. }
  1343. $userIdList[] = intval($member['user_id']);
  1344. }
  1345. }
  1346. return array_unique($userIdList);
  1347. }
  1348. }