group_edit.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 '../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 the given max number of members per group
  79. */
  80. function check_max_number_of_members($value)
  81. {
  82. $max_member_no_limit = $value['max_member_no_limit'];
  83. if ($max_member_no_limit == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  84. return true;
  85. }
  86. $max_member = $value['max_member'];
  87. return is_numeric($max_member);
  88. }
  89. /**
  90. * Function to check if the number of selected group members is valid
  91. */
  92. function check_group_members($value)
  93. {
  94. if ($value['max_member_no_limit'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  95. return true;
  96. }
  97. if (isset($value['max_member']) && isset($value['group_members']) && $value['max_member'] < count($value['group_members'])) {
  98. return array ('group_members' => get_lang('GroupTooMuchMembers'));
  99. }
  100. return true;
  101. }
  102. /* MAIN CODE */
  103. $htmlHeadXtra[] = '<script>
  104. $(document).ready( function() {
  105. $("#max_member").on("focus", function() {
  106. $("#max_member_selected").attr("checked", true);
  107. });
  108. });
  109. </script>';
  110. // Build form
  111. $form = new FormValidator('group_edit', 'post', api_get_self().'?'.api_get_cidreq());
  112. $form->addElement('header', $nameTools);
  113. $form->addElement('hidden', 'action');
  114. $form->addElement('hidden', 'referer');
  115. // Group name
  116. $form->add_textfield('name', get_lang('GroupName'));
  117. // Description
  118. $form->addElement('textarea', 'description', get_lang('Description'), array ('class' => 'span6', 'rows' => 6));
  119. $complete_user_list = GroupManager :: fill_groups_list($current_group['id']);
  120. usort($complete_user_list, 'sort_users');
  121. $possible_users = array();
  122. foreach ($complete_user_list as $index => $user) {
  123. $possible_users[$user['user_id']] = api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].')';
  124. }
  125. // Group tutors
  126. $group_tutor_list = GroupManager :: get_subscribed_tutors($current_group['id']);
  127. $selected_users = array();
  128. $selected_tutors = array();
  129. foreach ($group_tutor_list as $index => $user) {
  130. $selected_tutors[] = $user['user_id'];
  131. }
  132. $group_tutors_element = $form->addElement('advmultiselect', 'group_tutors', get_lang('GroupTutors'), $possible_users, 'style="width: 280px;"');
  133. $group_tutors_element->setElementTemplate('
  134. {javascript}
  135. <table{class}>
  136. <!-- BEGIN label_2 --><tr><th>{label_2}</th><!-- END label_2 -->
  137. <!-- BEGIN label_3 --><th>&nbsp;</th><th>{label_3}</th></tr><!-- END label_3 -->
  138. <tr>
  139. <td valign="top">{unselected}</td>
  140. <td align="center">{add}<br /><br />{remove}</td>
  141. <td valign="top">{selected}</td>
  142. </tr>
  143. </table>
  144. ');
  145. $group_tutors_element->setButtonAttributes('add', array('class' => 'btn arrowr'));
  146. $group_tutors_element->setButtonAttributes('remove', array('class' => 'btn arrowl'));
  147. // Group members
  148. $group_member_list = GroupManager :: get_subscribed_users($current_group['id']);
  149. $selected_users = array ();
  150. foreach ($group_member_list as $index => $user) {
  151. $selected_users[] = $user['user_id'];
  152. }
  153. // possible : number_groups_left > 0 and is group member
  154. $possible_users = array();
  155. foreach ($complete_user_list as $index => $user) {
  156. if ($user['number_groups_left'] > 0 || in_array($user['user_id'], $selected_users)) {
  157. $possible_users[$user['user_id']] = api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].')';
  158. }
  159. }
  160. $group_members_element = $form->addElement('advmultiselect', 'group_members', get_lang('GroupMembers'), $possible_users, 'style="width: 280px;"');
  161. $group_members_element->setElementTemplate('
  162. {javascript}
  163. <table{class}>
  164. <!-- BEGIN label_2 --><tr><th>{label_2}</th><!-- END label_2 -->
  165. <!-- BEGIN label_3 --><th>&nbsp;</th><th>{label_3}</th></tr><!-- END label_3 -->
  166. <tr>
  167. <td valign="top">{unselected}</td>
  168. <td align="center">{add}<br /><br />{remove}</td>
  169. <td valign="top">{selected}</td>
  170. </tr>
  171. </table>');
  172. $group_members_element->setButtonAttributes('add', array('class' => 'btn arrowr'));
  173. $group_members_element->setButtonAttributes('remove', array('class' => 'btn arrowl'));
  174. $form->addFormRule('check_group_members');
  175. // Members per group
  176. $form->addElement('radio', 'max_member_no_limit', get_lang('GroupLimit'), get_lang('NoLimit'), GroupManager::MEMBER_PER_GROUP_NO_LIMIT);
  177. $group = array();
  178. $group[] = $form->createElement('radio', 'max_member_no_limit', null, get_lang('MaximumOfParticipants'), 1, array('id' => 'max_member_selected'));
  179. $group[] = $form->createElement('text', 'max_member', null, array('class' => 'span1', 'id' => 'max_member'));
  180. $group[] = $form->createElement('static', null, null, get_lang('GroupPlacesThis'));
  181. $form->addGroup($group, 'max_member_group', null, '', false);
  182. $form->addRule('max_member_group', get_lang('InvalidMaxNumberOfMembers'), 'callback', 'check_max_number_of_members');
  183. // Self registration
  184. $group = array(
  185. $form->createElement('checkbox', 'self_registration_allowed', get_lang('GroupSelfRegistration'), get_lang('GroupAllowStudentRegistration'), 1),
  186. $form->createElement('checkbox', 'self_unregistration_allowed', null, get_lang('GroupAllowStudentUnregistration'), 1)
  187. );
  188. $form->addGroup($group, '', Display::return_icon('user.png', get_lang('GroupSelfRegistration') , array(), ICON_SIZE_SMALL).' '.get_lang('GroupSelfRegistration'), '', false);
  189. // Documents settings
  190. $group = array();
  191. $group[] = $form->createElement('radio', 'doc_state', get_lang('GroupDocument'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
  192. $group[] = $form->createElement('radio', 'doc_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  193. $group[] = $form->createElement('radio', 'doc_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  194. $form->addGroup($group, '', Display::return_icon('folder.png', get_lang('GroupDocument') , array(), ICON_SIZE_SMALL).' '.get_lang('GroupDocument'), '', false);
  195. // Work settings
  196. $group = array();
  197. $group[] = $form->createElement('radio', 'work_state', get_lang('GroupWork'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
  198. $group[] = $form->createElement('radio', 'work_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  199. $group[] = $form->createElement('radio', 'work_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  200. $form->addGroup($group, '', Display::return_icon('work.png', get_lang('GroupWork') , array(), ICON_SIZE_SMALL).' '.get_lang('GroupWork'), '', false);
  201. // Calendar settings
  202. $group = array();
  203. $group[] = $form->createElement('radio', 'calendar_state', get_lang('GroupCalendar'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
  204. $group[] = $form->createElement('radio', 'calendar_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  205. $group[] = $form->createElement('radio', 'calendar_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  206. $form->addGroup($group, '', Display::return_icon('agenda.png', get_lang('GroupCalendar') , array(), ICON_SIZE_SMALL).' '.get_lang('GroupCalendar'), '', false);
  207. // Announcements settings
  208. $group = array();
  209. $group[] = $form->createElement('radio', 'announcements_state', get_lang('GroupAnnouncements'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
  210. $group[] = $form->createElement('radio', 'announcements_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  211. $group[] = $form->createElement('radio', 'announcements_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  212. $form->addGroup($group, '', Display::return_icon('announce.png', get_lang('GroupAnnouncements') , array(), ICON_SIZE_SMALL).' '.get_lang('GroupAnnouncements'), '', false);
  213. //Forum settings
  214. $group = array();
  215. $group[] = $form->createElement('radio', 'forum_state', get_lang('GroupForum'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
  216. $group[] = $form->createElement('radio', 'forum_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  217. $group[] = $form->createElement('radio', 'forum_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  218. $form->addGroup($group, '', Display::return_icon('forum.png', get_lang('GroupForum') , array(), ICON_SIZE_SMALL).' '.get_lang('GroupForum'), '', false);
  219. // Wiki settings
  220. $group = array(
  221. $form->createElement('radio', 'wiki_state', get_lang('GroupWiki'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  222. $form->createElement('radio', 'wiki_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  223. $form->createElement('radio', 'wiki_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  224. );
  225. $form->addGroup($group, '', Display::return_icon('wiki.png', get_lang('GroupWiki') , array(), ICON_SIZE_SMALL).' '.get_lang('GroupWiki'), '', false);
  226. // Chat settings
  227. $group = array(
  228. $form->createElement('radio', 'chat_state', get_lang('Chat'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  229. $form->createElement('radio', 'chat_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  230. $form->createElement('radio', 'chat_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  231. );
  232. $form->addGroup($group, '', Display::return_icon('chat.png', get_lang('Chat'), array(), ICON_SIZE_SMALL).' '.get_lang('Chat'), '', false);
  233. // submit button
  234. $form->addElement('style_submit_button', 'submit', get_lang('SaveSettings'), 'class="save"');
  235. if ($form->validate()) {
  236. $values = $form->exportValues();
  237. if ($values['max_member_no_limit'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  238. $max_member = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  239. } else {
  240. $max_member = $values['max_member'];
  241. }
  242. $self_registration_allowed = isset($values['self_registration_allowed']) ? 1 : 0;
  243. $self_unregistration_allowed = isset($values['self_unregistration_allowed']) ? 1 : 0;
  244. GroupManager::set_group_properties(
  245. $current_group['id'],
  246. $values['name'],
  247. $values['description'],
  248. $max_member,
  249. $values['doc_state'],
  250. $values['work_state'],
  251. $values['calendar_state'],
  252. $values['announcements_state'],
  253. $values['forum_state'],
  254. $values['wiki_state'],
  255. $values['chat_state'],
  256. $self_registration_allowed,
  257. $self_unregistration_allowed
  258. );
  259. // Storing the tutors (we first remove all the tutors and then add only those who were selected)
  260. GroupManager :: unsubscribe_all_tutors($current_group['id']);
  261. if (isset ($_POST['group_tutors']) && count($_POST['group_tutors']) > 0) {
  262. GroupManager :: subscribe_tutors($values['group_tutors'], $current_group['id']);
  263. }
  264. // Storing the users (we first remove all users and then add only those who were selected)
  265. GroupManager :: unsubscribe_all_users($current_group['id']);
  266. if (isset ($_POST['group_members']) && count($_POST['group_members']) > 0) {
  267. GroupManager :: subscribe_users($values['group_members'], $current_group['id']);
  268. }
  269. // Returning to the group area (note: this is inconsistent with the rest of chamilo)
  270. $cat = GroupManager :: get_category_from_group($current_group['id']);
  271. if (isset($_POST['group_members']) && count($_POST['group_members']) > $max_member && $max_member != GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  272. header('Location: group.php?'.api_get_cidreq(true, false).'&action=warning_message&msg='.get_lang('GroupTooMuchMembers'));
  273. } else {
  274. header('Location: group.php?'.api_get_cidreq(true, false).'&action=success_message&msg='.get_lang('GroupSettingsModified').'&category='.$cat['id']);
  275. }
  276. exit;
  277. }
  278. $defaults = $current_group;
  279. $defaults['group_members'] = $selected_users;
  280. $defaults['group_tutors'] = $selected_tutors;
  281. $action = isset($_GET['action']) ? $_GET['action'] : '';
  282. $defaults['action'] = $action;
  283. if ($defaults['maximum_number_of_students'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  284. $defaults['max_member_no_limit'] = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  285. } else {
  286. $defaults['max_member_no_limit'] = 1;
  287. $defaults['max_member'] = $defaults['maximum_number_of_students'];
  288. }
  289. if (!empty($_GET['keyword']) && !empty($_GET['submit'])) {
  290. $keyword_name = Security::remove_XSS($_GET['keyword']);
  291. echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  292. }
  293. Display :: display_header($nameTools, 'Group');
  294. //@todo fix this
  295. if (isset($_GET['show_message_warning'])) {
  296. echo Display::display_warning_message($_GET['show_message_warning']);
  297. }
  298. if (isset($_GET['show_message_sucess'])) {
  299. echo Display::display_normal_message($_GET['show_message_sucess']);
  300. }
  301. $form->setDefaults($defaults);
  302. $form->display();
  303. Display :: display_footer();