usergroup.lib.php 62 KB

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