group_creation.php 12 KB

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