group_creation.php 13 KB

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