usergroup.lib.php 82 KB

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