group_creation.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.group
  5. */
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. $this_section = SECTION_COURSES;
  8. $current_course_tool = TOOL_GROUP;
  9. // Notice for unauthorized people.
  10. api_protect_course_script(true);
  11. if (!api_is_allowed_to_edit(false, true)) {
  12. api_not_allowed(true);
  13. }
  14. $currentUrl = api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq();
  15. /* Create the groups */
  16. if (isset($_POST['action'])) {
  17. switch ($_POST['action']) {
  18. case 'create_groups':
  19. $groups = array();
  20. $useOnlyFirstCategory = false;
  21. $firstCategory = isset($_POST['group_0_category']) ? $_POST['group_0_category'] : 0;
  22. if (isset($_POST['same_category']) && $_POST['same_category']) {
  23. $useOnlyFirstCategory = true;
  24. }
  25. $group1['category'] = isset($_POST['group_'.$i.'_category']) ? $_POST['group_'.$i.'_category'] : null;
  26. for ($i = 0; $i < $_POST['number_of_groups']; $i++) {
  27. $group1['name'] = empty($_POST['group_'.$i.'_name']) ? get_lang('Group').' '.$i : $_POST['group_'.$i.'_name'];
  28. $group1['category'] = isset($_POST['group_'.$i.'_category']) ? $_POST['group_'.$i.'_category'] : null;
  29. if ($useOnlyFirstCategory) {
  30. $group1['category'] = $firstCategory;
  31. }
  32. $group1['tutor'] = isset($_POST['group_'.$i.'_tutor']) ? $_POST['group_'.$i.'_tutor'] : null;
  33. $group1['places'] = isset($_POST['group_'.$i.'_places']) ? $_POST['group_'.$i.'_places'] : null;
  34. $groups[] = $group1;
  35. }
  36. foreach ($groups as $index => $group) {
  37. if (!empty($_POST['same_tutor'])) {
  38. $group['tutor'] = $_POST['group_0_tutor'];
  39. }
  40. if (!empty($_POST['same_places'])) {
  41. $group['places'] = $_POST['group_0_places'];
  42. }
  43. GroupManager::create_group(
  44. $group['name'],
  45. $group['category'],
  46. $group['tutor'],
  47. $group['places']
  48. );
  49. }
  50. Display::addFlash(Display::return_message(get_lang('GroupsAdded')));
  51. header("Location: ".$currentUrl);
  52. exit;
  53. break;
  54. case 'create_subgroups':
  55. GroupManager::create_subgroups(
  56. $_POST['base_group'],
  57. $_POST['number_of_groups']
  58. );
  59. Display::addFlash(Display::return_message(get_lang('GroupsAdded')));
  60. header("Location: ".$currentUrl);
  61. exit;
  62. break;
  63. case 'create_class_groups':
  64. $ids = GroupManager::create_class_groups($_POST['group_category']);
  65. Display::addFlash(Display::return_message(get_lang('GroupsAdded')));
  66. header("Location: ".$currentUrl);
  67. exit;
  68. break;
  69. }
  70. }
  71. $nameTools = get_lang('GroupCreation');
  72. $interbreadcrumb[] = array(
  73. 'url' => api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq(),
  74. 'name' => get_lang('Groups'),
  75. );
  76. Display :: display_header($nameTools, 'Group');
  77. if (isset($_POST['number_of_groups'])) {
  78. if (!is_numeric($_POST['number_of_groups']) || intval($_POST['number_of_groups']) < 1) {
  79. echo Display::return_message(
  80. get_lang('PleaseEnterValidNumber').'<br /><br />
  81. <a href="group_creation.php?'.api_get_cidreq().'">&laquo; '.get_lang('Back').'</a>',
  82. 'error',
  83. false
  84. );
  85. } else {
  86. $number_of_groups = intval($_POST['number_of_groups']);
  87. if ($number_of_groups > 1) {
  88. ?>
  89. <script>
  90. var number_of_groups = <?php echo $number_of_groups; ?>;
  91. function switch_state(key) {
  92. ref = document.getElementById(key+'_0');
  93. for(i=1; i<number_of_groups; i++) {
  94. var id = "#"+key+'_'+i;
  95. element = document.getElementById(key+'_'+i);
  96. element.disabled = !element.disabled;
  97. disabled = element.disabled;
  98. $(id).prop('disabled', disabled);
  99. $(id).prop('value', ref.value);
  100. $(id).selectpicker('refresh');
  101. }
  102. if (disabled) {
  103. ref.addEventListener("change", copy, false);
  104. } else {
  105. ref.removeEventListener("change", copy, false);
  106. }
  107. copy_value(key);
  108. }
  109. function copy(e) {
  110. key = e.currentTarget.id;
  111. var re = new RegExp ('_0', '') ;
  112. var key = key.replace(re, '') ;
  113. copy_value(key);
  114. }
  115. function copy_value(key) {
  116. ref = document.getElementById(key+'_0');
  117. for( i=1; i<number_of_groups; i++) {
  118. element = document.getElementById(key+'_'+i);
  119. element.value = ref.value;
  120. }
  121. }
  122. </script>
  123. <?php
  124. }
  125. $group_categories = GroupManager::get_categories();
  126. $group_id = GroupManager:: get_number_of_groups() + 1;
  127. $cat_options = [];
  128. foreach ($group_categories as $index => $category) {
  129. $cat_options[$category['id']] = $category['title'];
  130. }
  131. $form = new FormValidator('create_groups_step2', 'POST', api_get_self().'?'.api_get_cidreq());
  132. // Modify the default templates
  133. $renderer = $form->defaultRenderer();
  134. $form_template = "<form {attributes}>\n<div class='create-groups'>\n<table>\n{content}\n</table>\n</div>\n</form>";
  135. $renderer->setFormTemplate($form_template);
  136. $element_template = <<<EOT
  137. <tr class="separate">
  138. <td>
  139. <!-- BEGIN required -->
  140. <span class="form_required">*</span> <!-- END required -->{label}
  141. </td>
  142. <td>
  143. <!-- BEGIN error -->
  144. <span class="form_error">{error}</span><br /><!-- END error --> {element}
  145. </td>
  146. </tr>
  147. EOT;
  148. $renderer->setCustomElementTemplate($element_template);
  149. $form->addElement('header', $nameTools);
  150. $form->addElement('hidden', 'action');
  151. $form->addElement('hidden', 'number_of_groups');
  152. $defaults = array();
  153. // Table heading
  154. $group_el = array();
  155. $group_el[] = $form->createElement('static', null, null, '<b>'.get_lang('GroupName').'</b>');
  156. if (api_get_setting('allow_group_categories') === 'true') {
  157. $group_el[] = $form->createElement('static', null, null, '<b>'.get_lang('GroupCategory').'</b>');
  158. }
  159. $group_el[] = $form->createElement('static', null, null, '<b>'.get_lang('GroupPlacesThis').'</b>');
  160. $form->addGroup($group_el, 'groups', null, "</td><td>", false);
  161. // Checkboxes
  162. if ($_POST['number_of_groups'] > 1) {
  163. $group_el = array();
  164. $group_el[] = $form->createElement('static', null, null, ' ');
  165. if (api_get_setting('allow_group_categories') === 'true') {
  166. $group_el[] = $form->createElement(
  167. 'checkbox',
  168. 'same_category',
  169. null,
  170. get_lang('SameForAll'),
  171. array('onclick' => "javascript: switch_state('category');")
  172. );
  173. }
  174. $group_el[] = $form->createElement(
  175. 'checkbox',
  176. 'same_places',
  177. null,
  178. get_lang('SameForAll'),
  179. array('onclick' => "javascript: switch_state('places');")
  180. );
  181. $form->addGroup($group_el, 'groups', null, '</td><td>', false);
  182. }
  183. // Properties for all groups
  184. for ($group_number = 0; $group_number < $_POST['number_of_groups']; $group_number++) {
  185. $group_el = array();
  186. $group_el[] = $form->createElement('text', 'group_'.$group_number.'_name');
  187. if (api_get_setting('allow_group_categories') === 'true') {
  188. $group_el[] = $form->createElement(
  189. 'select',
  190. 'group_'.$group_number.'_category',
  191. null,
  192. $cat_options,
  193. array('id' => 'category_'.$group_number)
  194. );
  195. } else {
  196. $group_el[] = $form->createElement('hidden', 'group_'.$group_number.'_category', 0);
  197. $defaults['group_'.$group_number.'_category'] = array_keys($cat_options)[0];
  198. }
  199. $group_el[] = $form->createElement(
  200. 'text',
  201. 'group_'.$group_number.'_places',
  202. null,
  203. array('class' => 'span1', 'id' => 'places_'.$group_number)
  204. );
  205. if ($_POST['number_of_groups'] < 10000) {
  206. if ($group_id < 10) {
  207. $prev = '000';
  208. } elseif ($group_id < 100) {
  209. $prev = '00';
  210. } elseif ($group_id < 1000) {
  211. $prev = '0';
  212. } else {
  213. $prev = '';
  214. }
  215. }
  216. $defaults['group_'.$group_number.'_name'] = get_lang('GroupSingle').' '.$prev.$group_id++;
  217. $form->addGroup($group_el, 'group_'.$group_number, null, '</td><td>', false);
  218. }
  219. $defaults['action'] = 'create_groups';
  220. $defaults['number_of_groups'] = intval($_POST['number_of_groups']);
  221. $form->setDefaults($defaults);
  222. $form->addButtonCreate(get_lang('CreateGroup'), 'submit');
  223. $form->display();
  224. }
  225. } else {
  226. /*
  227. * Show form to generate new groups
  228. */
  229. $create_groups_form = new FormValidator('create_groups', 'post', api_get_self().'?'.api_get_cidreq());
  230. $create_groups_form->addElement('header', $nameTools);
  231. $create_groups_form->addText('number_of_groups', get_lang('NumberOfGroupsToCreate'), null, array('value'=>'1'));
  232. $create_groups_form->addButton('submit', get_lang('ProceedToCreateGroup'), 'plus', 'primary');
  233. $defaults = array();
  234. $defaults['number_of_groups'] = 1;
  235. $create_groups_form->setDefaults($defaults);
  236. $create_groups_form->display();
  237. /*
  238. * Show form to generate subgroups
  239. */
  240. if (api_get_setting('allow_group_categories') === 'true' && count(GroupManager :: get_group_list()) > 0) {
  241. $base_group_options = array();
  242. $groups = GroupManager :: get_group_list();
  243. foreach ($groups as $index => $group) {
  244. $number_of_students = GroupManager :: number_of_students($group['id']);
  245. if ($number_of_students > 0) {
  246. $base_group_options[$group['id']] = $group['name'].' ('.$number_of_students.' '.get_lang('Users').')';
  247. }
  248. }
  249. if (count($base_group_options) > 0) {
  250. $create_subgroups_form = new FormValidator('create_subgroups', 'post', api_get_self().'?'.api_get_cidreq());
  251. $create_subgroups_form->addElement('header', get_lang('CreateSubgroups'));
  252. $create_subgroups_form->addElement('html', get_lang('CreateSubgroupsInfo'));
  253. $create_subgroups_form->addElement('hidden', 'action');
  254. $group_el = array();
  255. $group_el[] = $create_subgroups_form->createElement('static', null, null, get_lang('CreateNumberOfGroups'));
  256. $group_el[] = $create_subgroups_form->createElement('text', 'number_of_groups', null, array('size' => 3));
  257. $group_el[] = $create_subgroups_form->createElement('static', null, null, get_lang('WithUsersFrom'));
  258. $group_el[] = $create_subgroups_form->createElement('select', 'base_group', null, $base_group_options);
  259. $group_el[] = $create_subgroups_form->createElement('button', 'submit', get_lang('Ok'));
  260. $create_subgroups_form->addGroup($group_el, 'create_groups', null, null, false);
  261. $defaults = array();
  262. $defaults['action'] = 'create_subgroups';
  263. $create_subgroups_form->setDefaults($defaults);
  264. $create_subgroups_form->display();
  265. }
  266. }
  267. /*
  268. * Show form to generate groups from classes subscribed to the course
  269. */
  270. $options['where'] = array(" usergroup.course_id = ? " => api_get_course_int_id());
  271. $obj = new UserGroup();
  272. $classes = $obj->getUserGroupInCourse($options);
  273. if (count($classes) > 0) {
  274. echo '<b>'.get_lang('GroupsFromClasses').'</b>';
  275. echo '<blockquote>';
  276. echo '<p>'.get_lang('GroupsFromClassesInfo').'</p>';
  277. echo '<ul>';
  278. foreach ($classes as $index => $class) {
  279. $number_of_users = count($obj->get_users_by_usergroup($class['id']));
  280. echo '<li>';
  281. echo $class['name'];
  282. echo ' ('.$number_of_users.' '.get_lang('Users').')';
  283. echo '</li>';
  284. }
  285. echo '</ul>';
  286. $create_class_groups_form = new FormValidator('create_class_groups_form', 'post', api_get_self().'?'.api_get_cidreq());
  287. $create_class_groups_form->addElement('hidden', 'action');
  288. if (api_get_setting('allow_group_categories') === 'true') {
  289. $group_categories = GroupManager :: get_categories();
  290. $cat_options = array();
  291. foreach ($group_categories as $index => $category) {
  292. $cat_options[$category['id']] = $category['title'];
  293. }
  294. $create_class_groups_form->addElement('select', 'group_category', null, $cat_options);
  295. } else {
  296. $create_class_groups_form->addElement('hidden', 'group_category');
  297. }
  298. $create_class_groups_form->addElement('submit', 'submit', get_lang('Ok'));
  299. $defaults['group_category'] = GroupManager::DEFAULT_GROUP_CATEGORY;
  300. $defaults['action'] = 'create_class_groups';
  301. $create_class_groups_form->setDefaults($defaults);
  302. $create_class_groups_form->display();
  303. echo '</blockquote>';
  304. }
  305. }
  306. Display::display_footer();