group_edit.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. // Getting course info
  120. $course = Database::getManager()->getRepository('ChamiloCoreBundle:Course')->find(api_get_course_int_id());
  121. //Getting subscribed students
  122. $subscribedUsers = Database::getManager()->getRepository('ChamiloCoreBundle:Course')->getSubscribedStudents($course);
  123. $subscribedUsers = $subscribedUsers->getQuery();
  124. $subscribedUsers = $subscribedUsers->execute();
  125. // Getting all users
  126. $complete_user_list = array();
  127. foreach ($subscribedUsers as $user) {
  128. $complete_user_list[$user->getUserId()] = $user->getCompleteNameWithClasses();
  129. }
  130. $possible_users = array();
  131. foreach ($complete_user_list as $userId => $user) {
  132. $possible_users[$userId] = $user;
  133. }
  134. // Group tutors
  135. $group_tutor_list = GroupManager :: get_subscribed_tutors($current_group['id']);
  136. $selected_users = array();
  137. $selected_tutors = array();
  138. foreach ($group_tutor_list as $index => $user) {
  139. $selected_tutors[] = $user['user_id'];
  140. }
  141. $group_tutors_element = $form->addElement('advmultiselect', 'group_tutors', get_lang('GroupTutors'), $possible_users);
  142. $group_tutors_element->setButtonAttributes('add');
  143. $group_tutors_element->setButtonAttributes('remove');
  144. // Group members
  145. $group_member_list = GroupManager :: get_subscribed_users($current_group['id']);
  146. $selected_users = array();
  147. foreach ($group_member_list as $userId => $userName) {
  148. $selected_users[] = $userId;
  149. }
  150. // possible : number_groups_left > 0 and is group member
  151. $possible_users = array();
  152. foreach ($complete_user_list as $userId => $userName) {
  153. $possible_users[$userId] = $userName;
  154. }
  155. $group_members_element = $form->addElement(
  156. 'advmultiselect',
  157. 'group_members',
  158. get_lang('GroupMembers'),
  159. $possible_users
  160. );
  161. $group_members_element->setButtonAttributes('add');
  162. $group_members_element->setButtonAttributes('remove');
  163. $form->addFormRule('check_group_members');
  164. // Members per group
  165. $form->addElement(
  166. 'radio',
  167. 'max_member_no_limit',
  168. get_lang('GroupLimit'),
  169. get_lang('NoLimit'),
  170. GroupManager::MEMBER_PER_GROUP_NO_LIMIT
  171. );
  172. $group = array();
  173. $group[] = $form->createElement(
  174. 'radio',
  175. 'max_member_no_limit',
  176. null,
  177. get_lang('MaximumOfParticipants'),
  178. 1,
  179. array('id' => 'max_member_selected')
  180. );
  181. $group[] = $form->createElement('text', 'max_member', null, array('class' => 'span1', 'id' => 'max_member'));
  182. $group[] = $form->createElement('static', null, null, get_lang('GroupPlacesThis'));
  183. $form->addGroup($group, 'max_member_group', null, '', false);
  184. $form->addRule('max_member_group', get_lang('InvalidMaxNumberOfMembers'), 'callback', 'check_max_number_of_members');
  185. // Self registration
  186. $group = array();
  187. $group[] = $form->createElement(
  188. 'checkbox',
  189. 'self_registration_allowed',
  190. get_lang('GroupSelfRegistration'),
  191. get_lang('GroupAllowStudentRegistration'),
  192. 1
  193. );
  194. $group[] = $form->createElement(
  195. 'checkbox',
  196. 'self_unregistration_allowed',
  197. null,
  198. get_lang('GroupAllowStudentUnregistration'),
  199. 1
  200. );
  201. $form->addGroup(
  202. $group,
  203. '',
  204. Display::return_icon('user.png', get_lang('GroupSelfRegistration'), array(), ICON_SIZE_SMALL).' '.get_lang(
  205. 'GroupSelfRegistration'
  206. ),
  207. '',
  208. false
  209. );
  210. // Documents settings
  211. $group = array();
  212. $group[] = $form->createElement(
  213. 'radio',
  214. 'doc_state',
  215. get_lang('GroupDocument'),
  216. get_lang('NotAvailable'),
  217. GroupManager::TOOL_NOT_AVAILABLE
  218. );
  219. $group[] = $form->createElement('radio', 'doc_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  220. $group[] = $form->createElement('radio', 'doc_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  221. $form->addGroup(
  222. $group,
  223. '',
  224. Display::return_icon('folder.png', get_lang('GroupDocument'), array(), ICON_SIZE_SMALL).' '.get_lang(
  225. 'GroupDocument'
  226. ),
  227. '',
  228. false
  229. );
  230. // Work settings
  231. $group = array();
  232. $group[] = $form->createElement(
  233. 'radio',
  234. 'work_state',
  235. get_lang('GroupWork'),
  236. get_lang('NotAvailable'),
  237. GroupManager::TOOL_NOT_AVAILABLE
  238. );
  239. $group[] = $form->createElement('radio', 'work_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  240. $group[] = $form->createElement('radio', 'work_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  241. $form->addGroup(
  242. $group,
  243. '',
  244. Display::return_icon('work.png', get_lang('GroupWork'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupWork'),
  245. '',
  246. false
  247. );
  248. // Calendar settings
  249. $group = array();
  250. $group[] = $form->createElement(
  251. 'radio',
  252. 'calendar_state',
  253. get_lang('GroupCalendar'),
  254. get_lang('NotAvailable'),
  255. GroupManager::TOOL_NOT_AVAILABLE
  256. );
  257. $group[] = $form->createElement('radio', 'calendar_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  258. $group[] = $form->createElement('radio', 'calendar_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  259. $form->addGroup(
  260. $group,
  261. '',
  262. Display::return_icon('agenda.png', get_lang('GroupCalendar'), array(), ICON_SIZE_SMALL).' '.get_lang(
  263. 'GroupCalendar'
  264. ),
  265. '',
  266. false
  267. );
  268. // Announcements settings
  269. $group = array();
  270. $group[] = $form->createElement(
  271. 'radio',
  272. 'announcements_state',
  273. get_lang('GroupAnnouncements'),
  274. get_lang('NotAvailable'),
  275. GroupManager::TOOL_NOT_AVAILABLE
  276. );
  277. $group[] = $form->createElement('radio', 'announcements_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  278. $group[] = $form->createElement('radio', 'announcements_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  279. $form->addGroup(
  280. $group,
  281. '',
  282. Display::return_icon('announce.png', get_lang('GroupAnnouncements'), array(), ICON_SIZE_SMALL).' '.get_lang(
  283. 'GroupAnnouncements'
  284. ),
  285. '',
  286. false
  287. );
  288. //Forum settings
  289. $group = array();
  290. $group[] = $form->createElement(
  291. 'radio',
  292. 'forum_state',
  293. get_lang('GroupForum'),
  294. get_lang('NotAvailable'),
  295. GroupManager::TOOL_NOT_AVAILABLE
  296. );
  297. $group[] = $form->createElement('radio', 'forum_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  298. $group[] = $form->createElement('radio', 'forum_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  299. $form->addGroup(
  300. $group,
  301. '',
  302. Display::return_icon('forum.png', get_lang('GroupForum'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupForum'),
  303. '',
  304. false
  305. );
  306. // Wiki settings
  307. $group = array();
  308. $group[] = $form->createElement(
  309. 'radio',
  310. 'wiki_state',
  311. get_lang('GroupWiki'),
  312. get_lang('NotAvailable'),
  313. GroupManager::TOOL_NOT_AVAILABLE
  314. );
  315. $group[] = $form->createElement('radio', 'wiki_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  316. $group[] = $form->createElement('radio', 'wiki_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  317. $form->addGroup(
  318. $group,
  319. '',
  320. Display::return_icon('wiki.png', get_lang('GroupWiki'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupWiki'),
  321. '',
  322. false
  323. );
  324. // Chat settings
  325. $group = array();
  326. $group[] = $form->createElement(
  327. 'radio',
  328. 'chat_state',
  329. get_lang('Chat'),
  330. get_lang('NotAvailable'),
  331. GroupManager::TOOL_NOT_AVAILABLE
  332. );
  333. $group[] = $form->createElement('radio', 'chat_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC);
  334. $group[] = $form->createElement('radio', 'chat_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE);
  335. $form->addGroup(
  336. $group,
  337. '',
  338. Display::return_icon('chat.png', get_lang('Chat'), array(), ICON_SIZE_SMALL).' '.get_lang('Chat'),
  339. '',
  340. false
  341. );
  342. // submit button
  343. $form->addElement('style_submit_button', 'submit', get_lang('SaveSettings'), 'class="save"');
  344. if ($form->validate()) {
  345. $values = $form->exportValues();
  346. if ($values['max_member_no_limit'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  347. $max_member = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  348. } else {
  349. $max_member = $values['max_member'];
  350. }
  351. $self_registration_allowed = isset($values['self_registration_allowed']) ? 1 : 0;
  352. $self_unregistration_allowed = isset($values['self_unregistration_allowed']) ? 1 : 0;
  353. GroupManager :: set_group_properties(
  354. $current_group['id'],
  355. $values['name'],
  356. $values['description'],
  357. $max_member,
  358. $values['doc_state'],
  359. $values['work_state'],
  360. $values['calendar_state'],
  361. $values['announcements_state'],
  362. $values['forum_state'],
  363. $values['wiki_state'],
  364. $values['chat_state'],
  365. $self_registration_allowed,
  366. $self_unregistration_allowed
  367. );
  368. // Storing the tutors (we first remove all the tutors and then add only those who were selected)
  369. GroupManager :: unsubscribe_all_tutors($current_group['id']);
  370. if (isset ($_POST['group_tutors']) && count($_POST['group_tutors']) > 0) {
  371. GroupManager :: subscribe_tutors($values['group_tutors'], $current_group['id']);
  372. }
  373. // Storing the users (we first remove all users and then add only those who were selected)
  374. GroupManager :: unsubscribe_all_users($current_group['id']);
  375. if (isset ($_POST['group_members']) && count($_POST['group_members']) > 0) {
  376. GroupManager :: subscribe_users($values['group_members'], $current_group['id']);
  377. }
  378. // Returning to the group area (note: this is inconsistent with the rest of chamilo)
  379. $cat = GroupManager :: get_category_from_group($current_group['id']);
  380. if (isset($_POST['group_members']) && count(
  381. $_POST['group_members']
  382. ) > $max_member && $max_member != GroupManager::MEMBER_PER_GROUP_NO_LIMIT
  383. ) {
  384. header(
  385. 'Location: group.php?'.api_get_cidreq(true, false).'&action=warning_message&msg='.get_lang(
  386. 'GroupTooMuchMembers'
  387. )
  388. );
  389. } else {
  390. header(
  391. 'Location: group.php?'.api_get_cidreq(true, false).'&action=success_message&msg='.get_lang(
  392. 'GroupSettingsModified'
  393. ).'&category='.$cat['id']
  394. );
  395. }
  396. exit;
  397. }
  398. $defaults = $current_group;
  399. $defaults['group_members'] = $selected_users;
  400. $defaults['group_tutors'] = $selected_tutors;
  401. $action = isset($_GET['action']) ? $_GET['action'] : '';
  402. $defaults['action'] = $action;
  403. if ($defaults['maximum_number_of_students'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  404. $defaults['max_member_no_limit'] = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  405. } else {
  406. $defaults['max_member_no_limit'] = 1;
  407. $defaults['max_member'] = $defaults['maximum_number_of_students'];
  408. }
  409. if (!empty($_GET['keyword']) && !empty($_GET['submit'])) {
  410. $keyword_name = Security::remove_XSS($_GET['keyword']);
  411. echo '<br/>'.get_lang('SearchResultsFor').' <span style="font-style: italic ;"> '.$keyword_name.' </span><br>';
  412. }
  413. Display :: display_header($nameTools, 'Group');
  414. //@todo fix this
  415. if (isset($_GET['show_message_warning'])) {
  416. echo Display::display_warning_message($_GET['show_message_warning']);
  417. }
  418. if (isset($_GET['show_message_sucess'])) {
  419. echo Display::display_normal_message($_GET['show_message_sucess']);
  420. }
  421. $form->setDefaults($defaults);
  422. $form->display();
  423. Display :: display_footer();