group_creation.php 12 KB

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