group_creation.php 14 KB

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