member_settings.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. /* FUNCTIONS */
  30. /**
  31. * List all users registered to the course
  32. */
  33. function search_members_keyword($firstname, $lastname, $username, $official_code, $keyword)
  34. {
  35. if (api_strripos($firstname, $keyword) !== false ||
  36. api_strripos($lastname, $keyword) !== false ||
  37. api_strripos($username, $keyword) !== false ||
  38. api_strripos($official_code, $keyword) !== false
  39. ) {
  40. return true;
  41. } else {
  42. return false;
  43. }
  44. }
  45. /**
  46. * Function to sort users after getting the list in the DB.
  47. * Necessary because there are 2 or 3 queries. Called by usort()
  48. */
  49. function sort_users($user_a, $user_b)
  50. {
  51. if (api_sort_by_first_name()) {
  52. $cmp = api_strcmp($user_a['firstname'], $user_b['firstname']);
  53. if ($cmp !== 0) {
  54. return $cmp;
  55. } else {
  56. $cmp = api_strcmp($user_a['lastname'], $user_b['lastname']);
  57. if ($cmp !== 0) {
  58. return $cmp;
  59. } else {
  60. return api_strcmp($user_a['username'], $user_b['username']);
  61. }
  62. }
  63. } else {
  64. $cmp = api_strcmp($user_a['lastname'], $user_b['lastname']);
  65. if ($cmp !== 0) {
  66. return $cmp;
  67. } else {
  68. $cmp = api_strcmp($user_a['firstname'], $user_b['firstname']);
  69. if ($cmp !== 0) {
  70. return $cmp;
  71. } else {
  72. return api_strcmp($user_a['username'], $user_b['username']);
  73. }
  74. }
  75. }
  76. }
  77. /**
  78. * Function to check if the number of selected group members is valid
  79. */
  80. function check_group_members($value)
  81. {
  82. if ($value['max_student'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  83. return true;
  84. }
  85. if (isset($value['max_student']) && isset($value['group_members']) && $value['max_student'] < count($value['group_members'])) {
  86. return array('group_members' => get_lang('GroupTooMuchMembers'));
  87. }
  88. return true;
  89. }
  90. /* MAIN CODE */
  91. $htmlHeadXtra[] = '<script>
  92. $(document).ready( function() {
  93. $("#max_member").on("focus", function() {
  94. $("#max_member_selected").attr("checked", true);
  95. });
  96. });
  97. </script>';
  98. // Build form
  99. $form = new FormValidator('group_edit', 'post', api_get_self().'?'.api_get_cidreq());
  100. $form->addElement('hidden', 'action');
  101. $form->addElement('hidden', 'max_student', $current_group['max_student']);
  102. $complete_user_list = GroupManager::fill_groups_list($current_group['id']);
  103. $possible_users = array();
  104. if (!empty($complete_user_list)) {
  105. usort($complete_user_list, 'sort_users');
  106. foreach ($complete_user_list as $index => $user) {
  107. $possible_users[$user['user_id']] = api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].')';
  108. }
  109. }
  110. // Group members
  111. $group_member_list = GroupManager :: get_subscribed_users($current_group['id']);
  112. $selected_users = array();
  113. if (!empty($group_member_list)) {
  114. foreach ($group_member_list as $index => $user) {
  115. $selected_users[] = $user['user_id'];
  116. }
  117. }
  118. $group_members_element = $form->addElement('advmultiselect', 'group_members', get_lang('GroupMembers'), $possible_users, 'style="width: 280px;"');
  119. $group_members_element->setElementTemplate('
  120. {javascript}
  121. <table{class}>
  122. <!-- BEGIN label_2 --><tr><th>{label_2}</th><!-- END label_2 -->
  123. <!-- BEGIN label_3 --><th>&nbsp;</th><th>{label_3}</th></tr><!-- END label_3 -->
  124. <tr>
  125. <td valign="top">{unselected}</td>
  126. <td align="center">{add}<br /><br />{remove}</td>
  127. <td valign="top">{selected}</td>
  128. </tr>
  129. </table>');
  130. $group_members_element->setButtonAttributes('add', array('class' => 'btn arrowr'));
  131. $group_members_element->setButtonAttributes('remove', array('class' => 'btn arrowl'));
  132. $form->addFormRule('check_group_members');
  133. // submit button
  134. $form->addElement('style_submit_button', 'submit', get_lang('SaveSettings'), 'class="save"');
  135. if ($form->validate()) {
  136. $values = $form->exportValues();
  137. // Storing the users (we first remove all users and then add only those who were selected)
  138. GroupManager :: unsubscribe_all_users($current_group['id']);
  139. if (isset ($_POST['group_members']) && count($_POST['group_members']) > 0) {
  140. GroupManager :: subscribe_users($values['group_members'], $current_group['id']);
  141. }
  142. // Returning to the group area (note: this is inconsistent with the rest of chamilo)
  143. $cat = GroupManager :: get_category_from_group($current_group['id']);
  144. if (isset($_POST['group_members']) && count($_POST['group_members']) > $max_member && $max_member != GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  145. header('Location: group.php?'.api_get_cidreq(true, false).'&action=warning_message&msg='.get_lang('GroupTooMuchMembers'));
  146. } else {
  147. header('Location: group.php?'.api_get_cidreq(true, false).'&action=success_message&msg='.get_lang('GroupSettingsModified').'&category='.$cat['id']);
  148. }
  149. exit;
  150. }
  151. $action = isset($_GET['action']) ? $_GET['action'] : null;
  152. switch ($action) {
  153. case 'empty':
  154. if (api_is_allowed_to_edit(false, true)) {
  155. GroupManager :: unsubscribe_all_users($group_id);
  156. Display :: display_confirmation_message(get_lang('GroupEmptied'));
  157. }
  158. break;
  159. }
  160. $defaults = $current_group;
  161. $defaults['group_members'] = $selected_users;
  162. $action = isset($_GET['action']) ? $_GET['action'] : '';
  163. $defaults['action'] = $action;
  164. if (!empty($_GET['keyword']) && !empty($_GET['submit'])) {
  165. $keyword_name = Security::remove_XSS($_GET['keyword']);
  166. echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  167. }
  168. Display :: display_header($nameTools, 'Group');
  169. //@todo fix this
  170. if (isset($_GET['show_message_warning'])) {
  171. echo Display::display_warning_message($_GET['show_message_warning']);
  172. }
  173. if (isset($_GET['show_message_sucess'])) {
  174. echo Display::display_normal_message($_GET['show_message_sucess']);
  175. }
  176. $form->setDefaults($defaults);
  177. echo GroupManager::getSettingBar('member');
  178. $form->display();
  179. Display :: display_footer();