group_edit.php 14 KB

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