member_settings.php 8.3 KB

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