group_edit.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. require '../inc/global.inc.php';
  14. $this_section = SECTION_COURSES;
  15. $current_course_tool = TOOL_GROUP;
  16. // Notice for unauthorized people.
  17. api_protect_course_script(true);
  18. $group_id = api_get_group_id();
  19. $current_group = GroupManager :: get_group_properties($group_id);
  20. $nameTools = get_lang('EditGroup');
  21. $interbreadcrumb[] = array ('url' => 'group.php', 'name' => get_lang('Groups'));
  22. $interbreadcrumb[] = array ('url' => 'group_space.php?'.api_get_cidReq(), 'name' => $current_group['name']);
  23. $is_group_member = GroupManager :: is_tutor_of_group(api_get_user_id(), $group_id);
  24. if (!api_is_allowed_to_edit(false, true) && !$is_group_member) {
  25. api_not_allowed(true);
  26. }
  27. /* FUNCTIONS */
  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. if (api_sort_by_first_name()) {
  50. $cmp = api_strcmp($user_a['firstname'], $user_b['firstname']);
  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. } else {
  62. $cmp = api_strcmp($user_a['lastname'], $user_b['lastname']);
  63. if ($cmp !== 0) {
  64. return $cmp;
  65. } else {
  66. $cmp = api_strcmp($user_a['firstname'], $user_b['firstname']);
  67. if ($cmp !== 0) {
  68. return $cmp;
  69. } else {
  70. return api_strcmp($user_a['username'], $user_b['username']);
  71. }
  72. }
  73. }
  74. }
  75. /**
  76. * Function to check the given max number of members per group
  77. */
  78. function check_max_number_of_members($value)
  79. {
  80. $max_member_no_limit = $value['max_member_no_limit'];
  81. if ($max_member_no_limit == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  82. return true;
  83. }
  84. $max_member = $value['max_member'];
  85. return is_numeric($max_member);
  86. }
  87. /**
  88. * Function to check if the number of selected group members is valid
  89. */
  90. function check_group_members($value)
  91. {
  92. if ($value['max_member_no_limit'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  93. return true;
  94. }
  95. if (isset($value['max_member']) && isset($value['group_members']) && $value['max_member'] < count($value['group_members'])) {
  96. return array ('group_members' => get_lang('GroupTooMuchMembers'));
  97. }
  98. return true;
  99. }
  100. /* MAIN CODE */
  101. $htmlHeadXtra[] = '<script>
  102. $(document).ready( function() {
  103. $("#max_member").on("focus", function() {
  104. $("#max_member_selected").attr("checked", true);
  105. });
  106. });
  107. </script>';
  108. // Build form
  109. $form = new FormValidator('group_edit', 'post', api_get_self().'?'.api_get_cidreq());
  110. $form->addElement('header', $nameTools);
  111. $form->addElement('hidden', 'action');
  112. $form->addElement('hidden', 'referer');
  113. // Group name
  114. $form->addText('name', get_lang('GroupName'));
  115. // Description
  116. $form->addElement('textarea', 'description', get_lang('Description'), array ('class' => 'span6', 'rows' => 6));
  117. $complete_user_list = GroupManager :: fill_groups_list($current_group['id']);
  118. usort($complete_user_list, 'sort_users');
  119. $possible_users = array();
  120. foreach ($complete_user_list as $index => $user) {
  121. $possible_users[$user['user_id']] = api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].')';
  122. }
  123. // Group tutors
  124. $group_tutor_list = GroupManager :: get_subscribed_tutors($current_group['id']);
  125. $selected_users = array();
  126. $selected_tutors = array();
  127. foreach ($group_tutor_list as $index => $user) {
  128. $selected_tutors[] = $user['user_id'];
  129. }
  130. $group_tutors_element = $form->addElement('advmultiselect', 'group_tutors', get_lang('GroupTutors'), $possible_users, 'style="width: 280px;"');
  131. // Group members
  132. $group_member_list = GroupManager :: get_subscribed_users($current_group['id']);
  133. $selected_users = array ();
  134. foreach ($group_member_list as $index => $user) {
  135. $selected_users[] = $user['user_id'];
  136. }
  137. // possible : number_groups_left > 0 and is group member
  138. $possible_users = array();
  139. foreach ($complete_user_list as $index => $user) {
  140. if ($user['number_groups_left'] > 0 || in_array($user['user_id'], $selected_users)) {
  141. $possible_users[$user['user_id']] = api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].')';
  142. }
  143. }
  144. $group_members_element = $form->addElement('advmultiselect', 'group_members', get_lang('GroupMembers'), $possible_users, 'style="width: 280px;"');
  145. $form->addFormRule('check_group_members');
  146. // Members per group
  147. $form->addElement('radio', 'max_member_no_limit', get_lang('GroupLimit'), get_lang('NoLimit'), GroupManager::MEMBER_PER_GROUP_NO_LIMIT);
  148. $group = array();
  149. $group[] = $form->createElement('radio', 'max_member_no_limit', null, get_lang('MaximumOfParticipants'), 1, array('id' => 'max_member_selected'));
  150. $group[] = $form->createElement('text', 'max_member', null, array('class' => 'span1', 'id' => 'max_member'));
  151. $group[] = $form->createElement('static', null, null, get_lang('GroupPlacesThis'));
  152. $form->addGroup($group, 'max_member_group', null, '', false);
  153. $form->addRule('max_member_group', get_lang('InvalidMaxNumberOfMembers'), 'callback', 'check_max_number_of_members');
  154. // Self registration
  155. $group = array(
  156. $form->createElement('checkbox', 'self_registration_allowed', get_lang('GroupSelfRegistration'), get_lang('GroupAllowStudentRegistration'), 1),
  157. $form->createElement('checkbox', 'self_unregistration_allowed', null, get_lang('GroupAllowStudentUnregistration'), 1)
  158. );
  159. $form->addGroup($group, '', Display::return_icon('user.png', get_lang('GroupSelfRegistration') , array(), ICON_SIZE_SMALL).' '.get_lang('GroupSelfRegistration'), '', false);
  160. // Documents settings
  161. $group = array();
  162. $group[] = $form->createElement('radio', 'doc_state', get_lang('GroupDocument'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
  163. $group[] = $form->createElement('radio', 'doc_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  164. $group[] = $form->createElement('radio', 'doc_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  165. $form->addGroup($group, '', Display::return_icon('folder.png', get_lang('GroupDocument') , array(), ICON_SIZE_SMALL).' '.get_lang('GroupDocument'), '', false);
  166. // Work settings
  167. $group = array();
  168. $group[] = $form->createElement('radio', 'work_state', get_lang('GroupWork'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
  169. $group[] = $form->createElement('radio', 'work_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  170. $group[] = $form->createElement('radio', 'work_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  171. $form->addGroup($group, '', Display::return_icon('work.png', get_lang('GroupWork') , array(), ICON_SIZE_SMALL).' '.get_lang('GroupWork'), '', false);
  172. // Calendar settings
  173. $group = array();
  174. $group[] = $form->createElement('radio', 'calendar_state', get_lang('GroupCalendar'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
  175. $group[] = $form->createElement('radio', 'calendar_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  176. $group[] = $form->createElement('radio', 'calendar_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  177. $form->addGroup($group, '', Display::return_icon('agenda.png', get_lang('GroupCalendar') , array(), ICON_SIZE_SMALL).' '.get_lang('GroupCalendar'), '', false);
  178. // Announcements settings
  179. $group = array();
  180. $group[] = $form->createElement('radio', 'announcements_state', get_lang('GroupAnnouncements'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
  181. $group[] = $form->createElement('radio', 'announcements_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  182. $group[] = $form->createElement('radio', 'announcements_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  183. $form->addGroup($group, '', Display::return_icon('announce.png', get_lang('GroupAnnouncements') , array(), ICON_SIZE_SMALL).' '.get_lang('GroupAnnouncements'), '', false);
  184. //Forum settings
  185. $group = array();
  186. $group[] = $form->createElement('radio', 'forum_state', get_lang('GroupForum'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
  187. $group[] = $form->createElement('radio', 'forum_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  188. $group[] = $form->createElement('radio', 'forum_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  189. $form->addGroup($group, '', Display::return_icon('forum.png', get_lang('GroupForum') , array(), ICON_SIZE_SMALL).' '.get_lang('GroupForum'), '', false);
  190. // Wiki settings
  191. $group = array(
  192. $form->createElement('radio', 'wiki_state', get_lang('GroupWiki'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  193. $form->createElement('radio', 'wiki_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  194. $form->createElement('radio', 'wiki_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  195. );
  196. $form->addGroup($group, '', Display::return_icon('wiki.png', get_lang('GroupWiki') , array(), ICON_SIZE_SMALL).' '.get_lang('GroupWiki'), '', false);
  197. // Chat settings
  198. $group = array(
  199. $form->createElement('radio', 'chat_state', get_lang('Chat'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  200. $form->createElement('radio', 'chat_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  201. $form->createElement('radio', 'chat_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  202. );
  203. $form->addGroup($group, '', Display::return_icon('chat.png', get_lang('Chat'), array(), ICON_SIZE_SMALL).' '.get_lang('Chat'), '', false);
  204. // submit button
  205. $form->addButtonSave(get_lang('SaveSettings'), 'submit');
  206. if ($form->validate()) {
  207. $values = $form->exportValues();
  208. if ($values['max_member_no_limit'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  209. $max_member = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  210. } else {
  211. $max_member = $values['max_member'];
  212. }
  213. $self_registration_allowed = isset($values['self_registration_allowed']) ? 1 : 0;
  214. $self_unregistration_allowed = isset($values['self_unregistration_allowed']) ? 1 : 0;
  215. GroupManager::set_group_properties(
  216. $current_group['id'],
  217. $values['name'],
  218. $values['description'],
  219. $max_member,
  220. $values['doc_state'],
  221. $values['work_state'],
  222. $values['calendar_state'],
  223. $values['announcements_state'],
  224. $values['forum_state'],
  225. $values['wiki_state'],
  226. $values['chat_state'],
  227. $self_registration_allowed,
  228. $self_unregistration_allowed
  229. );
  230. // Storing the tutors (we first remove all the tutors and then add only those who were selected)
  231. GroupManager :: unsubscribe_all_tutors($current_group['id']);
  232. if (isset ($_POST['group_tutors']) && count($_POST['group_tutors']) > 0) {
  233. GroupManager :: subscribe_tutors($values['group_tutors'], $current_group['id']);
  234. }
  235. // Storing the users (we first remove all users and then add only those who were selected)
  236. GroupManager :: unsubscribe_all_users($current_group['id']);
  237. if (isset ($_POST['group_members']) && count($_POST['group_members']) > 0) {
  238. GroupManager :: subscribe_users($values['group_members'], $current_group['id']);
  239. }
  240. // Returning to the group area (note: this is inconsistent with the rest of chamilo)
  241. $cat = GroupManager :: get_category_from_group($current_group['id']);
  242. if (isset($_POST['group_members']) && count($_POST['group_members']) > $max_member && $max_member != GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  243. Display::addFlash(Display::return_message(get_lang('GroupTooMuchMembers'), 'warning'));
  244. header('Location: group.php?'.api_get_cidreq(true, false));
  245. } else {
  246. Display::addFlash(Display::return_message(get_lang('GroupSettingsModified'), 'success'));
  247. header('Location: group.php?'.api_get_cidreq(true, false).'&category='.$cat['id']);
  248. }
  249. exit;
  250. }
  251. $defaults = $current_group;
  252. $defaults['group_members'] = $selected_users;
  253. $defaults['group_tutors'] = $selected_tutors;
  254. $action = isset($_GET['action']) ? $_GET['action'] : '';
  255. $defaults['action'] = $action;
  256. if ($defaults['maximum_number_of_students'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  257. $defaults['max_member_no_limit'] = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  258. } else {
  259. $defaults['max_member_no_limit'] = 1;
  260. $defaults['max_member'] = $defaults['maximum_number_of_students'];
  261. }
  262. if (!empty($_GET['keyword']) && !empty($_GET['submit'])) {
  263. $keyword_name = Security::remove_XSS($_GET['keyword']);
  264. echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  265. }
  266. Display :: display_header($nameTools, 'Group');
  267. $form->setDefaults($defaults);
  268. $form->display();
  269. Display :: display_footer();