usergroup.lib.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class provides methods for the UserGroup management.
  5. * Include/require it in your code to use its features.
  6. * @package chamilo.library
  7. */
  8. /**
  9. * Code
  10. */
  11. require_once 'model.lib.php';
  12. /**
  13. * Class
  14. * @package chamilo.library
  15. */
  16. class UserGroup extends Model
  17. {
  18. public $columns = array('id', 'name', 'description');
  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->table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  26. }
  27. public function get_count()
  28. {
  29. $row = Database::select('count(*) as count', $this->table, array(), 'first');
  30. return $row['count'];
  31. }
  32. public function get_usergroup_by_course_with_data_count($course_id)
  33. {
  34. $row = Database::select('count(*) as count', $this->usergroup_rel_course_table, array('where' => array('course_id = ?' => $course_id)), 'first');
  35. return $row['count'];
  36. }
  37. public function get_id_by_name($name)
  38. {
  39. $row = Database::select('id', $this->table, array('where' => array('name = ?' => $name)), 'first');
  40. return $row['id'];
  41. }
  42. /**
  43. * Displays the title + grid
  44. */
  45. function display()
  46. {
  47. // action links
  48. echo '<div class="actions">';
  49. echo '<a href="../admin/index.php">'.Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'), '', '32').'</a>';
  50. echo '<a href="'.api_get_self().'?action=add">'.Display::return_icon('new_class.png', get_lang('AddClasses'), '', '32').'</a>';
  51. echo Display::url(Display::return_icon('import_csv.png', get_lang('Import'), array(), ICON_SIZE_MEDIUM), 'usergroup_import.php');
  52. echo Display::url(Display::return_icon('export_csv.png', get_lang('Export'), array(), ICON_SIZE_MEDIUM), 'usergroup_export.php');
  53. echo '</div>';
  54. echo Display::grid_html('usergroups');
  55. }
  56. function display_teacher_view()
  57. {
  58. // action links
  59. echo Display::grid_html('usergroups');
  60. }
  61. /**
  62. * Gets a list of course ids by user group
  63. * @param int user group id
  64. * @return array
  65. */
  66. public function get_courses_by_usergroup($id)
  67. {
  68. $results = Database::select('course_id', $this->usergroup_rel_course_table, array('where' => array('usergroup_id = ?' => $id)));
  69. $array = array();
  70. if (!empty($results)) {
  71. foreach ($results as $row) {
  72. $array[] = $row['course_id'];
  73. }
  74. }
  75. return $array;
  76. }
  77. public function get_usergroup_in_course($options = array())
  78. {
  79. $sql = "SELECT u.* FROM {$this->usergroup_rel_course_table} usergroup
  80. INNER JOIN {$this->table} u
  81. ON (u.id = usergroup.usergroup_id)
  82. INNER JOIN {$this->table_course} c
  83. ON (usergroup.course_id = c.id)
  84. ";
  85. $conditions = Database::parse_conditions($options);
  86. $sql .= $conditions;
  87. $result = Database::query($sql);
  88. $array = Database::store_result($result, 'ASSOC');
  89. return $array;
  90. }
  91. public function get_usergroup_not_in_course($options = array())
  92. {
  93. $course_id = null;
  94. if (isset($options['course_id'])) {
  95. $course_id = intval($options['course_id']);
  96. unset($options['course_id']);
  97. }
  98. if (empty($course_id)) {
  99. return false;
  100. }
  101. $sql = "SELECT DISTINCT u.id, name
  102. FROM {$this->table} u
  103. LEFT OUTER JOIN {$this->usergroup_rel_course_table} urc
  104. ON (u.id = urc.usergroup_id AND course_id = $course_id)
  105. ";
  106. $conditions = Database::parse_conditions($options);
  107. $sql .= $conditions;
  108. $result = Database::query($sql);
  109. $array = Database::store_result($result, 'ASSOC');
  110. return $array;
  111. }
  112. public function get_usergroup_by_course($course_id)
  113. {
  114. $options = array('where' => array('course_id = ?' => $course_id));
  115. $results = Database::select('usergroup_id', $this->usergroup_rel_course_table, $options);
  116. $array = array();
  117. if (!empty($results)) {
  118. foreach ($results as $row) {
  119. $array[] = $row['usergroup_id'];
  120. }
  121. }
  122. return $array;
  123. }
  124. public function usergroup_was_added_in_course($usergroup_id, $course_id)
  125. {
  126. $results = Database::select('usergroup_id', $this->usergroup_rel_course_table, array('where' => array('course_id = ? AND usergroup_id = ?' => array($course_id, $usergroup_id))));
  127. if (empty($results)) {
  128. return false;
  129. }
  130. return true;
  131. }
  132. /**
  133. * Gets a list of session ids by user group
  134. * @param int user group id
  135. * @return array
  136. */
  137. public function get_sessions_by_usergroup($id)
  138. {
  139. $results = Database::select('session_id', $this->usergroup_rel_session_table, array('where' => array('usergroup_id = ?' => $id)));
  140. $array = array();
  141. if (!empty($results)) {
  142. foreach ($results as $row) {
  143. $array[] = $row['session_id'];
  144. }
  145. }
  146. return $array;
  147. }
  148. /**
  149. * Gets a list of user ids by user group
  150. * @param int user group id
  151. * @return array with a list of user ids
  152. */
  153. public function get_users_by_usergroup($id = null)
  154. {
  155. if (empty($id)) {
  156. $conditions = array();
  157. } else {
  158. $conditions = array('where' => array('usergroup_id = ?' => $id));
  159. }
  160. $results = Database::select('user_id', $this->usergroup_rel_user_table, $conditions, true);
  161. $array = array();
  162. if (!empty($results)) {
  163. foreach ($results as $row) {
  164. $array[] = $row['user_id'];
  165. }
  166. }
  167. return $array;
  168. }
  169. /**
  170. * Gets the usergroup id list by user id
  171. * @param int user id
  172. */
  173. public function get_usergroup_by_user($id)
  174. {
  175. $results = Database::select('usergroup_id', $this->usergroup_rel_user_table, array('where' => array('user_id = ?' => $id)));
  176. $array = array();
  177. if (!empty($results)) {
  178. foreach ($results as $row) {
  179. $array[] = $row['usergroup_id'];
  180. }
  181. }
  182. return $array;
  183. }
  184. /**
  185. * Subscribes sessions to a group (also adding the members of the group in the session and course)
  186. * @param int usergroup id
  187. * @param array list of session ids
  188. */
  189. function subscribe_sessions_to_usergroup($usergroup_id, $list)
  190. {
  191. $current_list = self::get_sessions_by_usergroup($usergroup_id);
  192. $user_list = self::get_users_by_usergroup($usergroup_id);
  193. $delete_items = $new_items = array();
  194. if (!empty($list)) {
  195. foreach ($list as $session_id) {
  196. if (!in_array($session_id, $current_list)) {
  197. $new_items[] = $session_id;
  198. }
  199. }
  200. }
  201. if (!empty($current_list)) {
  202. foreach ($current_list as $session_id) {
  203. if (!in_array($session_id, $list)) {
  204. $delete_items[] = $session_id;
  205. }
  206. }
  207. }
  208. //Deleting items
  209. if (!empty($delete_items)) {
  210. foreach ($delete_items as $session_id) {
  211. if (!empty($user_list)) {
  212. foreach ($user_list as $user_id) {
  213. SessionManager::unsubscribe_user_from_session($session_id, $user_id);
  214. }
  215. }
  216. Database::delete($this->usergroup_rel_session_table, array('usergroup_id = ? AND session_id = ?' => array($usergroup_id, $session_id)));
  217. }
  218. }
  219. //Addding new relationships
  220. if (!empty($new_items)) {
  221. foreach ($new_items as $session_id) {
  222. $params = array('session_id' => $session_id, 'usergroup_id' => $usergroup_id);
  223. Database::insert($this->usergroup_rel_session_table, $params);
  224. if (!empty($user_list)) {
  225. SessionManager::suscribe_users_to_session($session_id, $user_list, null, false);
  226. }
  227. }
  228. }
  229. }
  230. /**
  231. * Subscribes courses to a group (also adding the members of the group in the course)
  232. * @param int usergroup id
  233. * @param array list of course ids (integers)
  234. */
  235. function subscribe_courses_to_usergroup($usergroup_id, $list, $delete_groups = true)
  236. {
  237. $current_list = self::get_courses_by_usergroup($usergroup_id);
  238. $user_list = self::get_users_by_usergroup($usergroup_id);
  239. $delete_items = $new_items = array();
  240. if (!empty($list)) {
  241. foreach ($list as $id) {
  242. if (!in_array($id, $current_list)) {
  243. $new_items[] = $id;
  244. }
  245. }
  246. }
  247. if (!empty($current_list)) {
  248. foreach ($current_list as $id) {
  249. if (!in_array($id, $list)) {
  250. $delete_items[] = $id;
  251. }
  252. }
  253. }
  254. if ($delete_groups) {
  255. self::unsubscribe_courses_from_usergroup($usergroup_id, $delete_items);
  256. }
  257. //Addding new relationships
  258. if (!empty($new_items)) {
  259. foreach ($new_items as $course_id) {
  260. $course_info = api_get_course_info_by_id($course_id);
  261. if (!empty($user_list)) {
  262. foreach ($user_list as $user_id) {
  263. CourseManager::subscribe_user($user_id, $course_info['code']);
  264. }
  265. }
  266. $params = array('course_id' => $course_id, 'usergroup_id' => $usergroup_id);
  267. Database::insert($this->usergroup_rel_course_table, $params);
  268. }
  269. }
  270. }
  271. function unsubscribe_courses_from_usergroup($usergroup_id, $delete_items)
  272. {
  273. //Deleting items
  274. if (!empty($delete_items)) {
  275. $user_list = self::get_users_by_usergroup($usergroup_id);
  276. foreach ($delete_items as $course_id) {
  277. $course_info = api_get_course_info_by_id($course_id);
  278. if (!empty($user_list)) {
  279. foreach ($user_list as $user_id) {
  280. CourseManager::unsubscribe_user($user_id, $course_info['code']);
  281. }
  282. }
  283. Database::delete($this->usergroup_rel_course_table, array('usergroup_id = ? AND course_id = ?' => array($usergroup_id, $course_id)));
  284. }
  285. }
  286. }
  287. /**
  288. * Subscribes users to a group
  289. * @param int usergroup id
  290. * @param array list of user ids
  291. */
  292. function subscribe_users_to_usergroup($usergroup_id, $list, $delete_users_not_present_in_list = true)
  293. {
  294. $current_list = self::get_users_by_usergroup($usergroup_id);
  295. $course_list = self::get_courses_by_usergroup($usergroup_id);
  296. $session_list = self::get_sessions_by_usergroup($usergroup_id);
  297. $delete_items = array();
  298. $new_items = array();
  299. if (!empty($list)) {
  300. foreach ($list as $user_id) {
  301. if (!in_array($user_id, $current_list)) {
  302. $new_items[] = $user_id;
  303. }
  304. }
  305. }
  306. if (!empty($current_list)) {
  307. foreach ($current_list as $user_id) {
  308. if (!in_array($user_id, $list)) {
  309. $delete_items[] = $user_id;
  310. }
  311. }
  312. }
  313. //Deleting items
  314. if (!empty($delete_items) && $delete_users_not_present_in_list) {
  315. foreach ($delete_items as $user_id) {
  316. //Removing courses
  317. if (!empty($course_list)) {
  318. foreach ($course_list as $course_id) {
  319. $course_info = api_get_course_info_by_id($course_id);
  320. CourseManager::unsubscribe_user($user_id, $course_info['code']);
  321. }
  322. }
  323. //Removing sessions
  324. if (!empty($session_list)) {
  325. foreach ($session_list as $session_id) {
  326. SessionManager::unsubscribe_user_from_session($session_id, $user_id);
  327. }
  328. }
  329. Database::delete($this->usergroup_rel_user_table, array('usergroup_id = ? AND user_id = ?' => array($usergroup_id, $user_id)));
  330. }
  331. }
  332. //Addding new relationships
  333. if (!empty($new_items)) {
  334. //Adding sessions
  335. if (!empty($session_list)) {
  336. foreach ($session_list as $session_id) {
  337. SessionManager::suscribe_users_to_session($session_id, $new_items, null, false);
  338. }
  339. }
  340. foreach ($new_items as $user_id) {
  341. //Adding courses
  342. if (!empty($course_list)) {
  343. foreach ($course_list as $course_id) {
  344. $course_info = api_get_course_info_by_id($course_id);
  345. CourseManager::subscribe_user($user_id, $course_info['code']);
  346. }
  347. }
  348. $params = array('user_id' => $user_id, 'usergroup_id' => $usergroup_id);
  349. Database::insert($this->usergroup_rel_user_table, $params);
  350. }
  351. }
  352. }
  353. function usergroup_exists($name)
  354. {
  355. $sql = "SELECT * FROM $this->table WHERE name='".Database::escape_string($name)."'";
  356. $res = Database::query($sql);
  357. return Database::num_rows($res) != 0;
  358. }
  359. }