member_settings.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script displays an area where teachers can edit the group properties and member list.
  5. * Groups are also often called "teams" in the Dokeos code.
  6. *
  7. * @author various contributors
  8. * @author Roan Embrechts (VUB), partial code cleanup, initial virtual course support
  9. *
  10. * @package chamilo.group
  11. *
  12. * @todo course admin functionality to create groups based on who is in which course (or class).
  13. */
  14. require_once __DIR__.'/../inc/global.inc.php';
  15. $this_section = SECTION_COURSES;
  16. $current_course_tool = TOOL_GROUP;
  17. // Notice for unauthorized people.
  18. api_protect_course_script(true);
  19. $group_id = api_get_group_id();
  20. $current_group = GroupManager::get_group_properties($group_id);
  21. $nameTools = get_lang('EditGroup');
  22. $interbreadcrumb[] = ['url' => 'group.php', 'name' => get_lang('Groups')];
  23. $interbreadcrumb[] = ['url' => 'group_space.php?'.api_get_cidreq(), 'name' => $current_group['name']];
  24. $is_group_member = GroupManager::is_tutor_of_group(api_get_user_id(), $current_group);
  25. if (!api_is_allowed_to_edit(false, true) && !$is_group_member) {
  26. api_not_allowed(true);
  27. }
  28. /**
  29. * List all users registered to the course.
  30. */
  31. function search_members_keyword($firstname, $lastname, $username, $official_code, $keyword)
  32. {
  33. if (api_strripos($firstname, $keyword) !== false ||
  34. api_strripos($lastname, $keyword) !== false ||
  35. api_strripos($username, $keyword) !== false ||
  36. api_strripos($official_code, $keyword) !== false
  37. ) {
  38. return true;
  39. } else {
  40. return false;
  41. }
  42. }
  43. /**
  44. * Function to sort users after getting the list in the DB.
  45. * Necessary because there are 2 or 3 queries. Called by usort().
  46. */
  47. function sort_users($user_a, $user_b)
  48. {
  49. $orderListByOfficialCode = api_get_setting('order_user_list_by_official_code');
  50. if ($orderListByOfficialCode === 'true') {
  51. $cmp = api_strcmp($user_a['official_code'], $user_b['official_code']);
  52. if ($cmp !== 0) {
  53. return $cmp;
  54. } else {
  55. $cmp = api_strcmp($user_a['lastname'], $user_b['lastname']);
  56. if ($cmp !== 0) {
  57. return $cmp;
  58. } else {
  59. return api_strcmp($user_a['username'], $user_b['username']);
  60. }
  61. }
  62. }
  63. if (api_sort_by_first_name()) {
  64. $cmp = api_strcmp($user_a['firstname'], $user_b['firstname']);
  65. if ($cmp !== 0) {
  66. return $cmp;
  67. } else {
  68. $cmp = api_strcmp($user_a['lastname'], $user_b['lastname']);
  69. if ($cmp !== 0) {
  70. return $cmp;
  71. } else {
  72. return api_strcmp($user_a['username'], $user_b['username']);
  73. }
  74. }
  75. } else {
  76. $cmp = api_strcmp($user_a['lastname'], $user_b['lastname']);
  77. if ($cmp !== 0) {
  78. return $cmp;
  79. } else {
  80. $cmp = api_strcmp($user_a['firstname'], $user_b['firstname']);
  81. if ($cmp !== 0) {
  82. return $cmp;
  83. } else {
  84. return api_strcmp($user_a['username'], $user_b['username']);
  85. }
  86. }
  87. }
  88. }
  89. /**
  90. * Function to check if the number of selected group members is valid.
  91. */
  92. function check_group_members($value)
  93. {
  94. if ($value['max_student'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  95. return true;
  96. }
  97. if (isset($value['max_student']) &&
  98. isset($value['group_members']) &&
  99. $value['max_student'] < count($value['group_members'])
  100. ) {
  101. return ['group_members' => get_lang('GroupTooMuchMembers')];
  102. }
  103. return true;
  104. }
  105. $htmlHeadXtra[] = '<script>
  106. $(function() {
  107. $("#max_member").on("focus", function() {
  108. $("#max_member_selected").attr("checked", true);
  109. });
  110. });
  111. </script>';
  112. // Build form
  113. $form = new FormValidator(
  114. 'group_edit',
  115. 'post',
  116. api_get_self().'?'.api_get_cidreq()
  117. );
  118. $form->addElement('hidden', 'action');
  119. $form->addElement('hidden', 'max_student', $current_group['max_student']);
  120. $complete_user_list = CourseManager::get_user_list_from_course_code(
  121. api_get_course_id(),
  122. api_get_session_id()
  123. );
  124. $subscribedTutors = GroupManager::getTutors($current_group);
  125. if ($subscribedTutors) {
  126. $subscribedTutors = array_column($subscribedTutors, 'user_id');
  127. }
  128. $orderUserListByOfficialCode = api_get_setting('order_user_list_by_official_code');
  129. $possible_users = [];
  130. $userGroup = new UserGroup();
  131. if (!empty($complete_user_list)) {
  132. usort($complete_user_list, 'sort_users');
  133. foreach ($complete_user_list as $index => $user) {
  134. if (in_array($user['user_id'], $subscribedTutors)) {
  135. continue;
  136. }
  137. //prevent invitee users add to groups or tutors - see #8091
  138. if ($user['status'] != INVITEE) {
  139. $officialCode = !empty($user['official_code']) ? ' - '.$user['official_code'] : null;
  140. $groups = $userGroup->getUserGroupListByUser($user['user_id']);
  141. $groupNameListToString = '';
  142. if (!empty($groups)) {
  143. $groupNameList = array_column($groups, 'name');
  144. $groupNameListToString = ' - ['.implode(', ', $groupNameList).']';
  145. }
  146. $name = api_get_person_name($user['firstname'], $user['lastname']).
  147. ' ('.$user['username'].')'.$officialCode;
  148. if ($orderUserListByOfficialCode === 'true') {
  149. $officialCode = !empty($user['official_code']) ? $user['official_code']." - " : '? - ';
  150. $name = $officialCode.' '.api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].')';
  151. }
  152. $possible_users[$user['user_id']] = $name.$groupNameListToString;
  153. }
  154. }
  155. }
  156. // Group members
  157. $group_member_list = GroupManager::get_subscribed_users($current_group);
  158. $selected_users = [];
  159. if (!empty($group_member_list)) {
  160. foreach ($group_member_list as $index => $user) {
  161. $selected_users[] = $user['user_id'];
  162. }
  163. }
  164. $group_members_element = $form->addElement(
  165. 'advmultiselect',
  166. 'group_members',
  167. get_lang('GroupMembers'),
  168. $possible_users
  169. );
  170. $form->addFormRule('check_group_members');
  171. // submit button
  172. $form->addButtonSave(get_lang('SaveSettings'));
  173. if ($form->validate()) {
  174. $values = $form->exportValues();
  175. // Storing the users (we first remove all users and then add only those who were selected)
  176. GroupManager::unsubscribe_all_users($current_group);
  177. if (isset($_POST['group_members']) && count($_POST['group_members']) > 0) {
  178. GroupManager::subscribe_users(
  179. $values['group_members'],
  180. $current_group
  181. );
  182. }
  183. // Returning to the group area (note: this is inconsistent with the rest of chamilo)
  184. $cat = GroupManager::get_category_from_group($current_group['iid']);
  185. $max_member = $current_group['max_student'];
  186. if (isset($_POST['group_members']) &&
  187. count($_POST['group_members']) > $max_member &&
  188. $max_member != GroupManager::MEMBER_PER_GROUP_NO_LIMIT
  189. ) {
  190. Display::addFlash(Display::return_message(get_lang('GroupTooMuchMembers'), 'warning'));
  191. header('Location: group.php?'.api_get_cidreq(true, false));
  192. } else {
  193. Display::addFlash(Display::return_message(get_lang('GroupSettingsModified'), 'success'));
  194. header('Location: group.php?'.api_get_cidreq(true, false).'&category='.$cat['id']);
  195. }
  196. exit;
  197. }
  198. $action = isset($_GET['action']) ? $_GET['action'] : null;
  199. switch ($action) {
  200. case 'empty':
  201. if (api_is_allowed_to_edit(false, true)) {
  202. GroupManager:: unsubscribe_all_users($current_group);
  203. echo Display::return_message(get_lang('GroupEmptied'), 'confirm');
  204. }
  205. break;
  206. }
  207. $defaults = $current_group;
  208. $defaults['group_members'] = $selected_users;
  209. $action = isset($_GET['action']) ? $_GET['action'] : '';
  210. $defaults['action'] = $action;
  211. if (!empty($_GET['keyword']) && !empty($_GET['submit'])) {
  212. $keyword_name = Security::remove_XSS($_GET['keyword']);
  213. echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  214. }
  215. Display::display_header($nameTools, 'Group');
  216. $form->setDefaults($defaults);
  217. echo GroupManager::getSettingBar('member');
  218. $form->display();
  219. Display::display_footer();