member_settings.php 8.1 KB

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