group_creation.php 12 KB

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