group_edit.php 14 KB

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