usergroup.lib.php 75 KB

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