usergroup.lib.php 82 KB

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