group_portal_manager.lib.php 39 KB

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