usergroup.lib.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'model.lib.php';
  4. /**
  5. * Class UserGroup
  6. *
  7. * This class provides methods for the UserGroup management.
  8. * Include/require it in your code to use its features.
  9. * @package chamilo.library
  10. *
  11. */
  12. class UserGroup extends Model
  13. {
  14. public $columns = array('id', 'name', 'description');
  15. public $useMultipleUrl = false;
  16. /**
  17. * Set ups DB tables
  18. */
  19. public function __construct()
  20. {
  21. $this->table = Database::get_main_table(TABLE_USERGROUP);
  22. $this->usergroup_rel_user_table = Database::get_main_table(TABLE_USERGROUP_REL_USER);
  23. $this->usergroup_rel_course_table = Database::get_main_table(TABLE_USERGROUP_REL_COURSE);
  24. $this->usergroup_rel_session_table = Database::get_main_table(TABLE_USERGROUP_REL_SESSION);
  25. $this->access_url_rel_usergroup = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USERGROUP);
  26. $this->table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  27. $this->table_user = Database::get_main_table(TABLE_MAIN_USER);
  28. global $_configuration;
  29. if (isset($_configuration['enable_multiple_url_support_for_classes'])) {
  30. $this->useMultipleUrl = $_configuration['enable_multiple_url_support_for_classes'];
  31. }
  32. }
  33. /**
  34. * @return bool
  35. */
  36. public function getUseMultipleUrl()
  37. {
  38. return $this->useMultipleUrl;
  39. }
  40. /**
  41. * @return int
  42. */
  43. public function getTotalCount()
  44. {
  45. $row = Database::select('count(*) as count', $this->table, array(), 'first');
  46. return $row['count'];
  47. }
  48. /**
  49. * @return int
  50. */
  51. public function get_count()
  52. {
  53. if ($this->useMultipleUrl) {
  54. $urlId = api_get_current_access_url_id();
  55. $sql = "SELECT count(u.id) as count FROM ".$this->table." u
  56. INNER JOIN ".$this->access_url_rel_usergroup." a
  57. ON (u.id = a.usergroup_id)
  58. WHERE access_url_id = $urlId
  59. ";
  60. $result = Database::query($sql);
  61. if (Database::num_rows($result)) {
  62. $row = Database::fetch_array($result);
  63. return $row['count'];
  64. }
  65. return 0;
  66. } else {
  67. return $this->getTotalCount();
  68. }
  69. }
  70. /**
  71. * @param int $course_id
  72. *
  73. * @return mixed
  74. */
  75. public function get_usergroup_by_course_with_data_count($course_id)
  76. {
  77. if ($this->useMultipleUrl) {
  78. $course_id = intval($course_id);
  79. $urlId = api_get_current_access_url_id();
  80. $sql = "SELECT count(c.usergroup_id) as count FROM {$this->usergroup_rel_course_table} c
  81. INNER JOIN {$this->access_url_rel_usergroup} a ON (c.usergroup_id = a.usergroup_id)
  82. WHERE access_url_id = $urlId AND course_id = $course_id
  83. ";
  84. $result = Database::query($sql);
  85. if (Database::num_rows($result)) {
  86. $row = Database::fetch_array($result);
  87. return $row['count'];
  88. }
  89. return 0;
  90. } else {
  91. $row = Database::select(
  92. 'count(*) as count',
  93. $this->usergroup_rel_course_table,
  94. array('where' => array('course_id = ?' => $course_id)),
  95. 'first'
  96. );
  97. return $row['count'];
  98. }
  99. }
  100. /**
  101. * @param string $name
  102. *
  103. * @return mixed
  104. */
  105. public function get_id_by_name($name)
  106. {
  107. $row = Database::select('id', $this->table, array('where' => array('name = ?' => $name)), 'first');
  108. return $row['id'];
  109. }
  110. /**
  111. * Displays the title + grid
  112. */
  113. public function display()
  114. {
  115. // action links
  116. echo '<div class="actions">';
  117. echo '<a href="../admin/index.php">'.Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'), '', '32').'</a>';
  118. echo '<a href="'.api_get_self().'?action=add">'.Display::return_icon('new_class.png', get_lang('AddClasses'), '', '32').'</a>';
  119. echo Display::url(Display::return_icon('import_csv.png', get_lang('Import'), array(), ICON_SIZE_MEDIUM), 'usergroup_import.php');
  120. echo Display::url(Display::return_icon('export_csv.png', get_lang('Export'), array(), ICON_SIZE_MEDIUM), 'usergroup_export.php');
  121. echo '</div>';
  122. echo Display::grid_html('usergroups');
  123. }
  124. /**
  125. * Get HTML grid
  126. */
  127. public function display_teacher_view()
  128. {
  129. echo Display::grid_html('usergroups');
  130. }
  131. /**
  132. * Gets a list of course ids by user group
  133. * @param int $id user group id
  134. * @param array $loadCourseData
  135. * @return array
  136. */
  137. public function get_courses_by_usergroup($id, $loadCourseData = false)
  138. {
  139. if ($this->useMultipleUrl) {
  140. $urlId = api_get_current_access_url_id();
  141. $from = $this->usergroup_rel_course_table." c
  142. INNER JOIN {$this->access_url_rel_usergroup} a
  143. ON (a.usergroup_id = c.usergroup_id) ";
  144. $whereConditionSql = 'a.usergroup_id = ? AND access_url_id = ? ';
  145. $whereConditionValues = array($id, $urlId);
  146. } else {
  147. $whereConditionSql = 'usergroup_id = ?';
  148. $whereConditionValues = array($id);
  149. $from = $this->usergroup_rel_course_table." c ";
  150. }
  151. if ($loadCourseData) {
  152. $from .= " INNER JOIN {$this->table_course} as course ON c.course_id = course.id";
  153. }
  154. /*
  155. if (!empty($conditionsLike)) {
  156. $from .= " INNER JOIN {$this->table_course} as course ON c.course_id = course.id";
  157. $conditionSql = array();
  158. foreach ($conditionsLike as $field => $value) {
  159. $conditionSql[] = $field.' LIKE %?%';
  160. $whereConditionValues[] = $value;
  161. }
  162. $whereConditionSql .= ' AND '.implode(' AND ', $conditionSql);
  163. }*/
  164. $where = array('where' => array($whereConditionSql => $whereConditionValues));
  165. if ($loadCourseData) {
  166. $select = 'course.*';
  167. } else {
  168. $select = 'course_id';
  169. }
  170. $results = Database::select(
  171. $select,
  172. $from,
  173. $where
  174. );
  175. $array = array();
  176. if (!empty($results)) {
  177. foreach ($results as $row) {
  178. if ($loadCourseData) {
  179. $array[$row['id']] = $row;
  180. } else {
  181. $array[] = $row['course_id'];
  182. }
  183. }
  184. }
  185. return $array;
  186. }
  187. /**
  188. * @param array $options
  189. *
  190. * @return array
  191. */
  192. public function get_usergroup_in_course($options = array())
  193. {
  194. if ($this->useMultipleUrl) {
  195. $sql = "SELECT u.* FROM {$this->usergroup_rel_course_table} usergroup
  196. INNER JOIN {$this->table} u
  197. ON (u.id = usergroup.usergroup_id)
  198. INNER JOIN {$this->table_course} c
  199. ON (usergroup.course_id = c.id)
  200. INNER JOIN {$this->access_url_rel_usergroup} a ON (a.usergroup_id = u.id)
  201. ";
  202. } else {
  203. $sql = "SELECT u.* FROM {$this->usergroup_rel_course_table} usergroup
  204. INNER JOIN {$this->table} u
  205. ON (u.id = usergroup.usergroup_id)
  206. INNER JOIN {$this->table_course} c
  207. ON (usergroup.course_id = c.id)
  208. ";
  209. }
  210. $conditions = Database::parse_conditions($options);
  211. $sql .= $conditions;
  212. if ($this->useMultipleUrl) {
  213. $urlId = api_get_current_access_url_id();
  214. $sql .= " AND access_url_id = $urlId ";
  215. }
  216. if (isset($options['LIMIT'])) {
  217. $limits = explode(',', $options['LIMIT']);
  218. $limits = array_map('intval', $limits);
  219. if (isset($limits[0]) && isset($limits[1])) {
  220. $sql .= " LIMIT ".$limits[0].', '.$limits[1];
  221. }
  222. }
  223. $result = Database::query($sql);
  224. $array = Database::store_result($result, 'ASSOC');
  225. return $array;
  226. }
  227. /**
  228. * @param array $options
  229. *
  230. * @return array|bool
  231. */
  232. public function get_usergroup_not_in_course($options = array())
  233. {
  234. $course_id = null;
  235. if (isset($options['course_id'])) {
  236. $course_id = intval($options['course_id']);
  237. unset($options['course_id']);
  238. }
  239. if (empty($course_id)) {
  240. return false;
  241. }
  242. if ($this->useMultipleUrl) {
  243. $urlId = api_get_current_access_url_id();
  244. $sql = "SELECT DISTINCT u.id, name
  245. FROM {$this->table} u
  246. INNER JOIN {$this->access_url_rel_usergroup} a
  247. ON (a.usergroup_id = u.id)
  248. LEFT OUTER JOIN {$this->usergroup_rel_course_table} urc
  249. ON (u.id = urc.usergroup_id AND course_id = $course_id)
  250. ";
  251. } else {
  252. $sql = "SELECT DISTINCT u.id, name
  253. FROM {$this->table} u
  254. LEFT OUTER JOIN {$this->usergroup_rel_course_table} urc
  255. ON (u.id = urc.usergroup_id AND course_id = $course_id)
  256. ";
  257. }
  258. $conditions = Database::parse_conditions($options);
  259. $sql .= $conditions;
  260. if ($this->useMultipleUrl) {
  261. $sql .= " AND access_url_id = $urlId";
  262. }
  263. if (isset($options['LIMIT'])) {
  264. $limits = explode(',', $options['LIMIT']);
  265. $limits = array_map('intval', $limits);
  266. if (isset($limits[0]) && isset($limits[1])) {
  267. $sql .= " LIMIT ".$limits[0].', '.$limits[1];
  268. }
  269. }
  270. $result = Database::query($sql);
  271. $array = Database::store_result($result, 'ASSOC');
  272. return $array;
  273. }
  274. /**
  275. * @param int $course_id
  276. * @return array
  277. */
  278. public function get_usergroup_by_course($course_id)
  279. {
  280. if ($this->useMultipleUrl) {
  281. $urlId = api_get_current_access_url_id();
  282. $options = array('where' => array('c.course_id = ? AND access_url_id = ?' => array($course_id, $urlId)));
  283. $from = $this->usergroup_rel_course_table." as c INNER JOIN ".$this->access_url_rel_usergroup." a
  284. ON c.usergroup_id = a.usergroup_id";
  285. } else {
  286. $options = array('where' => array('c.course_id = ?' => $course_id));
  287. $from = $this->usergroup_rel_course_table." c";
  288. }
  289. $results = Database::select('c.usergroup_id', $from, $options);
  290. $array = array();
  291. if (!empty($results)) {
  292. foreach ($results as $row) {
  293. $array[] = $row['usergroup_id'];
  294. }
  295. }
  296. return $array;
  297. }
  298. /**
  299. * @param int $usergroup_id
  300. * @param int $course_id
  301. * @return bool
  302. */
  303. public function usergroup_was_added_in_course($usergroup_id, $course_id)
  304. {
  305. $results = Database::select(
  306. 'usergroup_id',
  307. $this->usergroup_rel_course_table,
  308. array('where' => array('course_id = ? AND usergroup_id = ?' => array($course_id, $usergroup_id)))
  309. );
  310. if (empty($results)) {
  311. return false;
  312. }
  313. return true;
  314. }
  315. /**
  316. * Gets a list of session ids by user group
  317. * @param int $id user group id
  318. * @return array
  319. */
  320. public function get_sessions_by_usergroup($id)
  321. {
  322. $results = Database::select(
  323. 'session_id',
  324. $this->usergroup_rel_session_table,
  325. array('where' => array('usergroup_id = ?' => $id))
  326. );
  327. $array = array();
  328. if (!empty($results)) {
  329. foreach ($results as $row) {
  330. $array[] = $row['session_id'];
  331. }
  332. }
  333. return $array;
  334. }
  335. /**
  336. * Gets a list of user ids by user group
  337. * @param int $id user group id
  338. * @return array with a list of user ids
  339. */
  340. public function get_users_by_usergroup($id = null)
  341. {
  342. if (empty($id)) {
  343. $conditions = array();
  344. } else {
  345. $conditions = array('where' => array('usergroup_id = ?' => $id));
  346. }
  347. $results = Database::select('user_id', $this->usergroup_rel_user_table, $conditions, true);
  348. $array = array();
  349. if (!empty($results)) {
  350. foreach ($results as $row) {
  351. $array[] = $row['user_id'];
  352. }
  353. }
  354. return $array;
  355. }
  356. /**
  357. * Gets the usergroup id list by user id
  358. * @param int $userId user id
  359. * @return array
  360. */
  361. public function get_usergroup_by_user($userId)
  362. {
  363. if ($this->useMultipleUrl) {
  364. $urlId = api_get_current_access_url_id();
  365. $from = $this->usergroup_rel_user_table." u
  366. INNER JOIN {$this->access_url_rel_usergroup} a ON (a.usergroup_id AND u.usergroup_id)";
  367. $where = array('where' => array('user_id = ? AND access_url_id = ? ' => array($userId, $urlId)));
  368. } else {
  369. $from = $this->usergroup_rel_user_table." u ";
  370. $where = array('where' => array('user_id = ?' => $userId));
  371. }
  372. $results = Database::select(
  373. 'u.usergroup_id',
  374. $from,
  375. $where
  376. );
  377. $array = array();
  378. if (!empty($results)) {
  379. foreach ($results as $row) {
  380. $array[] = $row['usergroup_id'];
  381. }
  382. }
  383. return $array;
  384. }
  385. /**
  386. * Subscribes sessions to a group (also adding the members of the group in the session and course)
  387. * @param int $usergroup_id usergroup id
  388. * @param array $list list of session ids
  389. */
  390. public function subscribe_sessions_to_usergroup($usergroup_id, $list)
  391. {
  392. $current_list = self::get_sessions_by_usergroup($usergroup_id);
  393. $user_list = self::get_users_by_usergroup($usergroup_id);
  394. $delete_items = $new_items = array();
  395. if (!empty($list)) {
  396. foreach ($list as $session_id) {
  397. if (!in_array($session_id, $current_list)) {
  398. $new_items[] = $session_id;
  399. }
  400. }
  401. }
  402. if (!empty($current_list)) {
  403. foreach ($current_list as $session_id) {
  404. if (!in_array($session_id, $list)) {
  405. $delete_items[] = $session_id;
  406. }
  407. }
  408. }
  409. // Deleting items
  410. if (!empty($delete_items)) {
  411. foreach ($delete_items as $session_id) {
  412. if (!empty($user_list)) {
  413. foreach ($user_list as $user_id) {
  414. SessionManager::unsubscribe_user_from_session($session_id, $user_id);
  415. }
  416. }
  417. Database::delete(
  418. $this->usergroup_rel_session_table,
  419. array('usergroup_id = ? AND session_id = ?' => array($usergroup_id, $session_id))
  420. );
  421. }
  422. }
  423. // Adding new relationships.
  424. if (!empty($new_items)) {
  425. foreach ($new_items as $session_id) {
  426. $params = array('session_id' => $session_id, 'usergroup_id' => $usergroup_id);
  427. Database::insert($this->usergroup_rel_session_table, $params);
  428. if (!empty($user_list)) {
  429. SessionManager::suscribe_users_to_session($session_id, $user_list, null, false);
  430. }
  431. }
  432. }
  433. }
  434. /**
  435. * Subscribes courses to a group (also adding the members of the group in the course)
  436. * @param int $usergroup_id usergroup id
  437. * @param array $list list of course ids (integers)
  438. * @param bool $delete_groups
  439. */
  440. public function subscribe_courses_to_usergroup($usergroup_id, $list, $delete_groups = true)
  441. {
  442. $current_list = self::get_courses_by_usergroup($usergroup_id);
  443. $user_list = self::get_users_by_usergroup($usergroup_id);
  444. $delete_items = $new_items = array();
  445. if (!empty($list)) {
  446. foreach ($list as $id) {
  447. if (!in_array($id, $current_list)) {
  448. $new_items[] = $id;
  449. }
  450. }
  451. }
  452. if (!empty($current_list)) {
  453. foreach ($current_list as $id) {
  454. if (!in_array($id, $list)) {
  455. $delete_items[] = $id;
  456. }
  457. }
  458. }
  459. if ($delete_groups) {
  460. self::unsubscribe_courses_from_usergroup($usergroup_id, $delete_items);
  461. }
  462. // Adding new relationships
  463. if (!empty($new_items)) {
  464. foreach ($new_items as $course_id) {
  465. $course_info = api_get_course_info_by_id($course_id);
  466. if (!empty($user_list)) {
  467. foreach ($user_list as $user_id) {
  468. CourseManager::subscribe_user($user_id, $course_info['code']);
  469. }
  470. }
  471. $params = array('course_id' => $course_id, 'usergroup_id' => $usergroup_id);
  472. Database::insert($this->usergroup_rel_course_table, $params);
  473. }
  474. }
  475. }
  476. /**
  477. * @param int $usergroup_id
  478. * @param bool $delete_items
  479. */
  480. public function unsubscribe_courses_from_usergroup($usergroup_id, $delete_items)
  481. {
  482. // Deleting items.
  483. if (!empty($delete_items)) {
  484. $user_list = self::get_users_by_usergroup($usergroup_id);
  485. foreach ($delete_items as $course_id) {
  486. $course_info = api_get_course_info_by_id($course_id);
  487. if (!empty($user_list)) {
  488. foreach ($user_list as $user_id) {
  489. CourseManager::unsubscribe_user($user_id, $course_info['code']);
  490. }
  491. }
  492. Database::delete(
  493. $this->usergroup_rel_course_table,
  494. array('usergroup_id = ? AND course_id = ?' => array($usergroup_id, $course_id))
  495. );
  496. }
  497. }
  498. }
  499. /**
  500. * Subscribe users to a group
  501. * @param int $usergroup_id usergroup id
  502. * @param array $list list of user ids
  503. * @param bool $delete_users_not_present_in_list
  504. */
  505. public function subscribe_users_to_usergroup($usergroup_id, $list, $delete_users_not_present_in_list = true)
  506. {
  507. $current_list = self::get_users_by_usergroup($usergroup_id);
  508. $course_list = self::get_courses_by_usergroup($usergroup_id);
  509. $session_list = self::get_sessions_by_usergroup($usergroup_id);
  510. $delete_items = array();
  511. $new_items = array();
  512. if (!empty($list)) {
  513. foreach ($list as $user_id) {
  514. if (!in_array($user_id, $current_list)) {
  515. $new_items[] = $user_id;
  516. }
  517. }
  518. }
  519. if (!empty($current_list)) {
  520. foreach ($current_list as $user_id) {
  521. if (!in_array($user_id, $list)) {
  522. $delete_items[] = $user_id;
  523. }
  524. }
  525. }
  526. // Deleting items
  527. if (!empty($delete_items) && $delete_users_not_present_in_list) {
  528. foreach ($delete_items as $user_id) {
  529. // Removing courses
  530. if (!empty($course_list)) {
  531. foreach ($course_list as $course_id) {
  532. $course_info = api_get_course_info_by_id($course_id);
  533. CourseManager::unsubscribe_user($user_id, $course_info['code']);
  534. }
  535. }
  536. // Removing sessions
  537. if (!empty($session_list)) {
  538. foreach ($session_list as $session_id) {
  539. SessionManager::unsubscribe_user_from_session($session_id, $user_id);
  540. }
  541. }
  542. Database::delete(
  543. $this->usergroup_rel_user_table,
  544. array('usergroup_id = ? AND user_id = ?' => array($usergroup_id, $user_id))
  545. );
  546. }
  547. }
  548. // Adding new relationships
  549. if (!empty($new_items)) {
  550. //Adding sessions
  551. if (!empty($session_list)) {
  552. foreach ($session_list as $session_id) {
  553. SessionManager::suscribe_users_to_session($session_id, $new_items, null, false);
  554. }
  555. }
  556. foreach ($new_items as $user_id) {
  557. // Adding courses
  558. if (!empty($course_list)) {
  559. foreach ($course_list as $course_id) {
  560. $course_info = api_get_course_info_by_id($course_id);
  561. CourseManager::subscribe_user($user_id, $course_info['code']);
  562. }
  563. }
  564. $params = array('user_id' => $user_id, 'usergroup_id' => $usergroup_id);
  565. Database::insert($this->usergroup_rel_user_table, $params);
  566. }
  567. }
  568. }
  569. /**
  570. * @param string $name
  571. * @return bool
  572. */
  573. public function usergroup_exists($name)
  574. {
  575. if ($this->useMultipleUrl) {
  576. $urlId = api_get_current_access_url_id();
  577. $sql = "SELECT * FROM $this->table u
  578. INNER JOIN {$this->access_url_rel_usergroup} a ON (a.usergroup_id = u.id)
  579. WHERE name = '".Database::escape_string($name)."' AND access_url_id = $urlId";
  580. } else {
  581. $sql = "SELECT * FROM $this->table WHERE name = '".Database::escape_string($name)."'";
  582. }
  583. $res = Database::query($sql);
  584. return Database::num_rows($res) != 0;
  585. }
  586. /**
  587. * @param int $sidx
  588. * @param int $sord
  589. * @param int $start
  590. * @param int $limit
  591. * @return array
  592. */
  593. public function getUsergroupsPagination($sidx, $sord, $start, $limit)
  594. {
  595. $sord = in_array(strtolower($sord), array('asc', 'desc')) ? $sord : 'desc';
  596. $start = intval($start);
  597. $limit = intval($limit);
  598. if ($this->useMultipleUrl) {
  599. $urlId = api_get_current_access_url_id();
  600. $from = $this->table." u INNER JOIN {$this->access_url_rel_usergroup} a ON (u.id = a.usergroup_id)";
  601. $where = array(' access_url_id = ?' => $urlId);
  602. } else {
  603. $from = $this->table." u ";
  604. $where = array();
  605. }
  606. $result = Database::select(
  607. 'u.*',
  608. $from,
  609. array('where' => $where, 'order'=> "name $sord", 'LIMIT'=> "$start , $limit")
  610. );
  611. $new_result = array();
  612. if (!empty($result)) {
  613. foreach ($result as $group) {
  614. $group['sessions'] = count($this->get_sessions_by_usergroup($group['id']));
  615. $group['courses'] = count($this->get_courses_by_usergroup($group['id']));
  616. $group['users'] = count($this->get_users_by_usergroup($group['id']));
  617. $new_result[] = $group;
  618. }
  619. $result = $new_result;
  620. }
  621. $columns = array('id', 'name', 'users', 'courses','sessions');
  622. if (!in_array($sidx, $columns)) {
  623. $sidx = 'name';
  624. }
  625. // Multidimensional sort
  626. $result = msort($result, $sidx, $sord);
  627. return $result;
  628. }
  629. /**
  630. * @param array $options
  631. * @return array
  632. */
  633. public function getDataToExport($options = array())
  634. {
  635. if ($this->useMultipleUrl) {
  636. $urlId = api_get_current_access_url_id();
  637. $from = $this->table." u INNER JOIN {$this->access_url_rel_usergroup} a
  638. ON (u.id = a.usergroup_id)";
  639. $options = array('where' => array('access_url_id = ? ' => $urlId));
  640. $classes = Database::select('a.id, name, description', $from, $options);
  641. } else {
  642. $classes = Database::select('id, name, description', $this->table, $options);
  643. }
  644. $result = array();
  645. if (!empty($classes)) {
  646. foreach ($classes as $data) {
  647. $users = self::getUserListByUserGroup($data['id']);
  648. $userToString = null;
  649. if (!empty($users)) {
  650. $userNameList = array();
  651. foreach ($users as $userData) {
  652. $userNameList[] = $userData['username'];
  653. }
  654. $userToString = implode(',', $userNameList);
  655. }
  656. $data['users'] = $userToString;
  657. $result[] = $data;
  658. }
  659. }
  660. return $result;
  661. }
  662. /**
  663. * @param string $firstLetter
  664. * @return array
  665. */
  666. public function filterByFirstLetter($firstLetter)
  667. {
  668. $firstLetter = Database::escape_string($firstLetter);
  669. $sql = "SELECT id, name FROM $this->table
  670. WHERE name LIKE '".$firstLetter."%' OR name LIKE '".api_strtolower($firstLetter)."%'
  671. ORDER BY name DESC ";
  672. $result = Database::query($sql);
  673. return Database::store_result($result);
  674. }
  675. /**
  676. * Select user group not in list
  677. * @param array $list
  678. * @return array
  679. */
  680. public function getUserGroupNotInList($list)
  681. {
  682. if (empty($list)) {
  683. return array();
  684. }
  685. $list = array_map('intval', $list);
  686. $listToString = implode("','", $list);
  687. $sql = "SELECT * FROM {$this->table} WHERE id NOT IN ('$listToString')";
  688. $result = Database::query($sql);
  689. return Database::store_result($result, 'ASSOC');
  690. }
  691. /**
  692. * @param $params
  693. * @param bool $show_query
  694. * @return bool|int
  695. */
  696. public function save($params, $show_query = false)
  697. {
  698. $groupExists = $this->usergroup_exists(trim($params['name']));
  699. if ($groupExists == false) {
  700. $id = parent::save($params, $show_query);
  701. if ($this->useMultipleUrl) {
  702. $this->subscribeToUrl($id, api_get_current_access_url_id());
  703. }
  704. return $id;
  705. }
  706. return false;
  707. }
  708. /**
  709. * @inheritdoc
  710. */
  711. public function update($params)
  712. {
  713. $groupExists = $this->usergroup_exists(trim($params['name']));
  714. if ($groupExists == false) {
  715. return parent::update($params);
  716. }
  717. return false;
  718. }
  719. /**
  720. * @param int $id
  721. * @return bool|void
  722. */
  723. public function delete($id)
  724. {
  725. $result = parent::delete($id);
  726. if ($this->useMultipleUrl) {
  727. if ($result) {
  728. $this->unsubscribeToUrl($id, api_get_current_access_url_id());
  729. }
  730. }
  731. }
  732. /**
  733. * @param int $id
  734. * @param int $urlId
  735. */
  736. public function subscribeToUrl($id, $urlId)
  737. {
  738. Database::insert(
  739. $this->access_url_rel_usergroup,
  740. array(
  741. 'access_url_id' => $urlId,
  742. 'usergroup_id' =>$id
  743. )
  744. );
  745. }
  746. /**
  747. * @param int $id
  748. * @param int $urlId
  749. */
  750. public function unsubscribeToUrl($id, $urlId)
  751. {
  752. Database::delete(
  753. $this->access_url_rel_usergroup,
  754. array(
  755. 'access_url_id = ? AND usergroup_id = ? ' => array($urlId, $id)
  756. )
  757. );
  758. }
  759. public static function searchUserGroupAjax($needle)
  760. {
  761. $response = new XajaxResponse();
  762. $return = '';
  763. if (!empty($needle)) {
  764. // xajax send utf8 datas... datas in db can be non-utf8 datas
  765. $charset = api_get_system_encoding();
  766. $needle = api_convert_encoding($needle, $charset, 'utf-8');
  767. $needle = Database::escape_string($needle);
  768. // search courses where username or firstname or lastname begins likes $needle
  769. $sql = 'SELECT id, name FROM '.Database::get_main_table(TABLE_USERGROUP).' u
  770. WHERE name LIKE "'.$needle.'%"
  771. ORDER BY name
  772. LIMIT 11';
  773. $result = Database::query($sql);
  774. $i = 0;
  775. while ($data = Database::fetch_array($result)) {
  776. $i++;
  777. if ($i <= 10) {
  778. $return .= '<a
  779. href="javascript: void(0);"
  780. onclick="javascript: add_user_to_url(\''.addslashes($data['id']).'\',\''.addslashes($data['name']).' \')">'.$data['name'].' </a><br />';
  781. } else {
  782. $return .= '...<br />';
  783. }
  784. }
  785. }
  786. $response->addAssign('ajax_list_courses','innerHTML', api_utf8_encode($return));
  787. return $response;
  788. }
  789. /**
  790. * Get user list by usergroup
  791. * @param $id
  792. * @return array
  793. */
  794. public function getUserListByUserGroup($id)
  795. {
  796. $id = intval($id);
  797. $sql = "SELECT u.* FROM ".$this->table_user." u
  798. INNER JOIN ".$this->usergroup_rel_user_table." c
  799. ON c.user_id = u.user_id
  800. WHERE c.usergroup_id = $id"
  801. ;
  802. $result = Database::query($sql);
  803. return Database::store_result($result);
  804. }
  805. }
  806. /* CREATE TABLE IF NOT EXISTS access_url_rel_usergroup (access_url_id int unsigned NOT NULL, usergroup_id int unsigned NOT NULL, PRIMARY KEY (access_url_id, usergroup_id));*/