group_portal_manager.lib.php 39 KB

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