group_edit.php 15 KB

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