member_settings.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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('Edit this group');
  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. * Function to sort users after getting the list in the DB.
  30. * Necessary because there are 2 or 3 queries. Called by usort().
  31. */
  32. function sort_users($user_a, $user_b)
  33. {
  34. $orderListByOfficialCode = api_get_setting('order_user_list_by_official_code');
  35. if ($orderListByOfficialCode === 'true') {
  36. $cmp = api_strcmp($user_a['official_code'], $user_b['official_code']);
  37. if ($cmp !== 0) {
  38. return $cmp;
  39. } else {
  40. $cmp = api_strcmp($user_a['lastname'], $user_b['lastname']);
  41. if ($cmp !== 0) {
  42. return $cmp;
  43. } else {
  44. return api_strcmp($user_a['username'], $user_b['username']);
  45. }
  46. }
  47. }
  48. if (api_sort_by_first_name()) {
  49. $cmp = api_strcmp($user_a['firstname'], $user_b['firstname']);
  50. if ($cmp !== 0) {
  51. return $cmp;
  52. } else {
  53. $cmp = api_strcmp($user_a['lastname'], $user_b['lastname']);
  54. if ($cmp !== 0) {
  55. return $cmp;
  56. } else {
  57. return api_strcmp($user_a['username'], $user_b['username']);
  58. }
  59. }
  60. } else {
  61. $cmp = api_strcmp($user_a['lastname'], $user_b['lastname']);
  62. if ($cmp !== 0) {
  63. return $cmp;
  64. } else {
  65. $cmp = api_strcmp($user_a['firstname'], $user_b['firstname']);
  66. if ($cmp !== 0) {
  67. return $cmp;
  68. } else {
  69. return api_strcmp($user_a['username'], $user_b['username']);
  70. }
  71. }
  72. }
  73. }
  74. /**
  75. * Function to check if the number of selected group members is valid.
  76. */
  77. function check_group_members($value)
  78. {
  79. if ($value['max_student'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  80. return true;
  81. }
  82. if (isset($value['max_student']) &&
  83. isset($value['group_members']) &&
  84. $value['max_student'] < count($value['group_members'])
  85. ) {
  86. return ['group_members' => get_lang('Number proposed exceeds max. that you allowed (you can modify in the group settings). Group composition has not been modified')];
  87. }
  88. return true;
  89. }
  90. $htmlHeadXtra[] = '<script>
  91. $(function() {
  92. $("#max_member").on("focus", function() {
  93. $("#max_member_selected").attr("checked", true);
  94. });
  95. });
  96. </script>';
  97. // Build form
  98. $form = new FormValidator(
  99. 'group_edit',
  100. 'post',
  101. api_get_self().'?'.api_get_cidreq()
  102. );
  103. $form->addElement('hidden', 'action');
  104. $form->addElement('hidden', 'max_student', $current_group['max_student']);
  105. $complete_user_list = CourseManager::get_user_list_from_course_code(
  106. api_get_course_id(),
  107. api_get_session_id()
  108. );
  109. $subscribedTutors = GroupManager::getTutors($current_group);
  110. if ($subscribedTutors) {
  111. $subscribedTutors = array_column($subscribedTutors, 'user_id');
  112. }
  113. $orderUserListByOfficialCode = api_get_setting('order_user_list_by_official_code');
  114. $possible_users = [];
  115. $userGroup = new UserGroup();
  116. if (!empty($complete_user_list)) {
  117. usort($complete_user_list, 'sort_users');
  118. foreach ($complete_user_list as $index => $user) {
  119. if (in_array($user['user_id'], $subscribedTutors)) {
  120. continue;
  121. }
  122. //prevent invitee users add to groups or tutors - see #8091
  123. if ($user['status'] != INVITEE) {
  124. $officialCode = !empty($user['official_code']) ? ' - '.$user['official_code'] : null;
  125. $groups = $userGroup->getUserGroupListByUser($user['user_id']);
  126. $groupNameListToString = '';
  127. if (!empty($groups)) {
  128. $groupNameList = array_column($groups, 'name');
  129. $groupNameListToString = ' - ['.implode(', ', $groupNameList).']';
  130. }
  131. $name = api_get_person_name($user['firstname'], $user['lastname']).
  132. ' ('.$user['username'].')'.$officialCode;
  133. if ($orderUserListByOfficialCode === 'true') {
  134. $officialCode = !empty($user['official_code']) ? $user['official_code']." - " : '? - ';
  135. $name = $officialCode.' '.api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].')';
  136. }
  137. $possible_users[$user['user_id']] = $name.$groupNameListToString;
  138. }
  139. }
  140. }
  141. // Group members
  142. $group_member_list = GroupManager::get_subscribed_users($current_group);
  143. $selected_users = [];
  144. if (!empty($group_member_list)) {
  145. foreach ($group_member_list as $index => $user) {
  146. $selected_users[] = $user['user_id'];
  147. }
  148. }
  149. $group_members_element = $form->addElement(
  150. 'advmultiselect',
  151. 'group_members',
  152. get_lang('Group members'),
  153. $possible_users
  154. );
  155. $form->addFormRule('check_group_members');
  156. // submit button
  157. $form->addButtonSave(get_lang('Save settings'));
  158. if ($form->validate()) {
  159. $values = $form->exportValues();
  160. // Storing the users (we first remove all users and then add only those who were selected)
  161. GroupManager::unsubscribe_all_users($current_group);
  162. if (isset($_POST['group_members']) && count($_POST['group_members']) > 0) {
  163. GroupManager::subscribe_users(
  164. $values['group_members'],
  165. $current_group
  166. );
  167. }
  168. // Returning to the group area (note: this is inconsistent with the rest of chamilo)
  169. $cat = GroupManager::get_category_from_group($current_group['iid']);
  170. $max_member = $current_group['max_student'];
  171. if (isset($_POST['group_members']) &&
  172. count($_POST['group_members']) > $max_member &&
  173. $max_member != GroupManager::MEMBER_PER_GROUP_NO_LIMIT
  174. ) {
  175. Display::addFlash(Display::return_message(get_lang('Number proposed exceeds max. that you allowed (you can modify in the group settings). Group composition has not been modified'), 'warning'));
  176. header('Location: group.php?'.api_get_cidreq(true, false));
  177. } else {
  178. Display::addFlash(Display::return_message(get_lang('Group settings modified'), 'success'));
  179. header('Location: group.php?'.api_get_cidreq(true, false).'&category='.$cat['id']);
  180. }
  181. exit;
  182. }
  183. $action = isset($_GET['action']) ? $_GET['action'] : null;
  184. switch ($action) {
  185. case 'empty':
  186. if (api_is_allowed_to_edit(false, true)) {
  187. GroupManager:: unsubscribe_all_users($current_group);
  188. echo Display::return_message(get_lang('The group is now empty'), 'confirm');
  189. }
  190. break;
  191. }
  192. $defaults = $current_group;
  193. $defaults['group_members'] = $selected_users;
  194. $action = isset($_GET['action']) ? $_GET['action'] : '';
  195. $defaults['action'] = $action;
  196. if (!empty($_GET['keyword']) && !empty($_GET['submit'])) {
  197. $keyword_name = Security::remove_XSS($_GET['keyword']);
  198. echo '<br/>'.get_lang('Search results for:').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  199. }
  200. Display::display_header($nameTools, 'Group');
  201. $form->setDefaults($defaults);
  202. echo GroupManager::getSettingBar('member');
  203. $form->display();
  204. Display::display_footer();