group_edit.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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?'.api_get_cidreq(), '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. /* FUNCTIONS */
  26. /**
  27. * List all users registered to the course
  28. */
  29. function search_members_keyword($firstname, $lastname, $username, $official_code, $keyword)
  30. {
  31. if (api_strripos($firstname, $keyword) !== false ||
  32. api_strripos($lastname, $keyword) !== false ||
  33. api_strripos($username, $keyword) !== false ||
  34. api_strripos($official_code, $keyword) !== false
  35. ) {
  36. return true;
  37. } else {
  38. return false;
  39. }
  40. }
  41. /**
  42. * Function to sort users after getting the list in the DB.
  43. * Necessary because there are 2 or 3 queries. Called by usort()
  44. */
  45. function sort_users($user_a, $user_b)
  46. {
  47. if (api_sort_by_first_name()) {
  48. $cmp = api_strcmp($user_a['firstname'], $user_b['firstname']);
  49. if ($cmp !== 0) {
  50. return $cmp;
  51. } else {
  52. $cmp = api_strcmp($user_a['lastname'], $user_b['lastname']);
  53. if ($cmp !== 0) {
  54. return $cmp;
  55. } else {
  56. return api_strcmp($user_a['username'], $user_b['username']);
  57. }
  58. }
  59. } else {
  60. $cmp = api_strcmp($user_a['lastname'], $user_b['lastname']);
  61. if ($cmp !== 0) {
  62. return $cmp;
  63. } else {
  64. $cmp = api_strcmp($user_a['firstname'], $user_b['firstname']);
  65. if ($cmp !== 0) {
  66. return $cmp;
  67. } else {
  68. return api_strcmp($user_a['username'], $user_b['username']);
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * Function to check the given max number of members per group
  75. */
  76. function check_max_number_of_members($value)
  77. {
  78. $max_member_no_limit = $value['max_member_no_limit'];
  79. if ($max_member_no_limit == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  80. return true;
  81. }
  82. $max_member = $value['max_member'];
  83. return is_numeric($max_member);
  84. }
  85. /**
  86. * Function to check if the number of selected group members is valid
  87. */
  88. function check_group_members($value)
  89. {
  90. if ($value['max_member_no_limit'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  91. return true;
  92. }
  93. if (isset($value['max_member']) && isset($value['group_members']) && $value['max_member'] < count($value['group_members'])) {
  94. return array('group_members' => get_lang('GroupTooMuchMembers'));
  95. }
  96. return true;
  97. }
  98. /* MAIN CODE */
  99. $htmlHeadXtra[] = '<script>
  100. $(document).ready( function() {
  101. $("#max_member").on("focus", function() {
  102. $("#max_member_selected").attr("checked", true);
  103. });
  104. });
  105. </script>';
  106. // Build form
  107. $form = new FormValidator('group_edit', 'post', api_get_self().'?'.api_get_cidreq());
  108. $form->addElement('header', $nameTools);
  109. $form->addElement('hidden', 'action');
  110. $form->addElement('hidden', 'referer');
  111. // Group name
  112. $form->addText('name', get_lang('GroupName'));
  113. // Description
  114. $form->addElement('textarea', 'description', get_lang('Description'), array ('rows' => 6));
  115. $complete_user_list = GroupManager :: fill_groups_list($current_group['iid']);
  116. usort($complete_user_list, 'sort_users');
  117. $possible_users = array();
  118. foreach ($complete_user_list as $index => $user) {
  119. $possible_users[$user['user_id']] = api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].')';
  120. }
  121. // Group tutors
  122. $group_tutor_list = GroupManager :: get_subscribed_tutors($current_group['iid']);
  123. $selected_users = array();
  124. $selected_tutors = array();
  125. foreach ($group_tutor_list as $index => $user) {
  126. $selected_tutors[] = $user['user_id'];
  127. }
  128. $group_tutors_element = $form->addElement(
  129. 'advmultiselect',
  130. 'group_tutors',
  131. get_lang('GroupTutors'),
  132. $possible_users,
  133. 'style="width: 280px;"'
  134. );
  135. // Group members
  136. $group_member_list = GroupManager::get_subscribed_users($current_group['iid']);
  137. $selected_users = array ();
  138. foreach ($group_member_list as $index => $user) {
  139. $selected_users[] = $user['user_id'];
  140. }
  141. // possible : number_groups_left > 0 and is group member
  142. $possible_users = array();
  143. foreach ($complete_user_list as $index => $user) {
  144. if ($user['number_groups_left'] > 0 || in_array($user['user_id'], $selected_users)) {
  145. $possible_users[$user['user_id']] = api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].')';
  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. $form->addFormRule('check_group_members');
  156. // Members per group
  157. $form->addElement(
  158. 'radio',
  159. 'max_member_no_limit',
  160. get_lang('GroupLimit'),
  161. get_lang('NoLimit'),
  162. GroupManager::MEMBER_PER_GROUP_NO_LIMIT
  163. );
  164. $group = array();
  165. $group[] = $form->createElement('radio', 'max_member_no_limit', null, get_lang('MaximumOfParticipants'), 1, array('id' => 'max_member_selected'));
  166. $group[] = $form->createElement('text', 'max_member', null, array('class' => 'span1', 'id' => 'max_member'));
  167. $group[] = $form->createElement('static', null, null, get_lang('GroupPlacesThis'));
  168. $form->addGroup($group, 'max_member_group', null, null, false);
  169. $form->addRule('max_member_group', get_lang('InvalidMaxNumberOfMembers'), 'callback', 'check_max_number_of_members');
  170. // Self registration
  171. $group = array(
  172. $form->createElement('checkbox', 'self_registration_allowed', get_lang('GroupSelfRegistration'), get_lang('GroupAllowStudentRegistration'), 1),
  173. $form->createElement('checkbox', 'self_unregistration_allowed', null, get_lang('GroupAllowStudentUnregistration'), 1)
  174. );
  175. $form->addGroup(
  176. $group,
  177. '',
  178. Display::return_icon('user.png', get_lang('GroupSelfRegistration')) . ' ' . get_lang('GroupSelfRegistration'),
  179. null,
  180. false
  181. );
  182. // Documents settings
  183. $group = array();
  184. $group[] = $form->createElement('radio', 'doc_state', get_lang('GroupDocument'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
  185. $group[] = $form->createElement('radio', 'doc_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  186. $group[] = $form->createElement('radio', 'doc_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  187. $form->addGroup(
  188. $group,
  189. '',
  190. Display::return_icon('folder.png', get_lang('GroupDocument')).' '.get_lang('GroupDocument'),
  191. null,
  192. false
  193. );
  194. // Work settings
  195. $group = array();
  196. $group[] = $form->createElement('radio', 'work_state', get_lang('GroupWork'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
  197. $group[] = $form->createElement('radio', 'work_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  198. $group[] = $form->createElement('radio', 'work_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  199. $form->addGroup(
  200. $group,
  201. '',
  202. Display::return_icon('work.png', get_lang('GroupWork')) . ' ' . get_lang('GroupWork'),
  203. null,
  204. false
  205. );
  206. // Calendar settings
  207. $group = array();
  208. $group[] = $form->createElement('radio', 'calendar_state', get_lang('GroupCalendar'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
  209. $group[] = $form->createElement('radio', 'calendar_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  210. $group[] = $form->createElement('radio', 'calendar_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  211. $form->addGroup(
  212. $group,
  213. '',
  214. Display::return_icon('agenda.png', get_lang('GroupCalendar')) . ' ' . get_lang('GroupCalendar'),
  215. null,
  216. false
  217. );
  218. // Announcements settings
  219. $group = array();
  220. $group[] = $form->createElement('radio', 'announcements_state', get_lang('GroupAnnouncements'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
  221. $group[] = $form->createElement('radio', 'announcements_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  222. $group[] = $form->createElement('radio', 'announcements_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  223. $form->addGroup(
  224. $group,
  225. '',
  226. Display::return_icon('announce.png', get_lang('GroupAnnouncements')) . ' ' . get_lang('GroupAnnouncements'),
  227. null,
  228. false
  229. );
  230. //Forum settings
  231. $group = array();
  232. $group[] = $form->createElement('radio', 'forum_state', get_lang('GroupForum'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE);
  233. $group[] = $form->createElement('radio', 'forum_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  234. $group[] = $form->createElement('radio', 'forum_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  235. $form->addGroup(
  236. $group,
  237. '',
  238. Display::return_icon('forum.png', get_lang('GroupForum')) . ' ' . get_lang('GroupForum'),
  239. null,
  240. false
  241. );
  242. // Wiki settings
  243. $group = array(
  244. $form->createElement('radio', 'wiki_state', get_lang('GroupWiki'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  245. $form->createElement('radio', 'wiki_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  246. $form->createElement('radio', 'wiki_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  247. );
  248. $form->addGroup(
  249. $group,
  250. '',
  251. Display::return_icon('wiki.png', get_lang('GroupWiki')) . ' ' . get_lang('GroupWiki'),
  252. null,
  253. false
  254. );
  255. // Chat settings
  256. $group = array(
  257. $form->createElement('radio', 'chat_state', get_lang('Chat'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  258. $form->createElement('radio', 'chat_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  259. $form->createElement('radio', 'chat_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  260. );
  261. $form->addGroup(
  262. $group,
  263. '',
  264. Display::return_icon('chat.png', get_lang('Chat')).' '.get_lang('Chat'),
  265. null,
  266. false
  267. );
  268. // submit button
  269. $form->addButtonSave(get_lang('SaveSettings'), 'submit');
  270. if ($form->validate()) {
  271. $values = $form->exportValues();
  272. if ($values['max_member_no_limit'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  273. $max_member = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  274. } else {
  275. $max_member = $values['max_member'];
  276. }
  277. $self_registration_allowed = isset($values['self_registration_allowed']) ? 1 : 0;
  278. $self_unregistration_allowed = isset($values['self_unregistration_allowed']) ? 1 : 0;
  279. GroupManager::set_group_properties(
  280. $current_group['id'],
  281. $values['name'],
  282. $values['description'],
  283. $max_member,
  284. $values['doc_state'],
  285. $values['work_state'],
  286. $values['calendar_state'],
  287. $values['announcements_state'],
  288. $values['forum_state'],
  289. $values['wiki_state'],
  290. $values['chat_state'],
  291. $self_registration_allowed,
  292. $self_unregistration_allowed
  293. );
  294. // Storing the tutors (we first remove all the tutors and then add only those who were selected)
  295. GroupManager :: unsubscribe_all_tutors($current_group['iid']);
  296. if (isset($_POST['group_tutors']) && count($_POST['group_tutors']) > 0) {
  297. GroupManager :: subscribe_tutors($values['group_tutors'], $current_group['iid']);
  298. }
  299. // Storing the users (we first remove all users and then add only those who were selected)
  300. GroupManager :: unsubscribe_all_users($current_group['iid']);
  301. if (isset($_POST['group_members']) && count($_POST['group_members']) > 0) {
  302. GroupManager :: subscribe_users($values['group_members'], $current_group['iid']);
  303. }
  304. // Returning to the group area (note: this is inconsistent with the rest of chamilo)
  305. $cat = GroupManager :: get_category_from_group($current_group['iid']);
  306. if (isset($_POST['group_members']) &&
  307. count($_POST['group_members']) > $max_member &&
  308. $max_member != GroupManager::MEMBER_PER_GROUP_NO_LIMIT
  309. ) {
  310. Display::addFlash(Display::return_message(get_lang('GroupTooMuchMembers'), 'warning'));
  311. header('Location: group.php?'.api_get_cidreq(true, false));
  312. } else {
  313. Display::addFlash(Display::return_message(get_lang('GroupSettingsModified'), 'success'));
  314. header('Location: group.php?'.api_get_cidreq(true, false).'&category='.$cat['id']);
  315. }
  316. exit;
  317. }
  318. $defaults = $current_group;
  319. $defaults['group_members'] = $selected_users;
  320. $defaults['group_tutors'] = $selected_tutors;
  321. $action = isset($_GET['action']) ? $_GET['action'] : '';
  322. $defaults['action'] = $action;
  323. if ($defaults['maximum_number_of_students'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  324. $defaults['max_member_no_limit'] = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  325. } else {
  326. $defaults['max_member_no_limit'] = 1;
  327. $defaults['max_member'] = $defaults['maximum_number_of_students'];
  328. }
  329. if (!empty($_GET['keyword']) && !empty($_GET['submit'])) {
  330. $keyword_name = Security::remove_XSS($_GET['keyword']);
  331. echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  332. }
  333. Display::display_header($nameTools, 'Group');
  334. $form->setDefaults($defaults);
  335. $form->display();
  336. Display::display_footer();