group_creation.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.group
  5. */
  6. // Name of the language file that needs to be included
  7. $language_file = 'group';
  8. require_once '../inc/global.inc.php';
  9. $this_section = SECTION_COURSES;
  10. $current_course_tool = TOOL_GROUP;
  11. // Notice for unauthorized people.
  12. api_protect_course_script(true);
  13. if (!api_is_allowed_to_edit(false, true)) {
  14. api_not_allowed(true);
  15. }
  16. /* Create the groups */
  17. if (isset($_POST['action'])) {
  18. switch ($_POST['action']) {
  19. case 'create_groups':
  20. $groups = array();
  21. for ($i = 0; $i < $_POST['number_of_groups']; $i ++) {
  22. $group1['name'] = empty($_POST['group_'.$i.'_name']) ? get_lang('Group').' '.$i : $_POST['group_'.$i.'_name'];
  23. $group1['category'] = isset($_POST['group_'.$i.'_category']) ? $_POST['group_'.$i.'_category'] : null;
  24. $group1['tutor'] = isset($_POST['group_'.$i.'_tutor']) ? $_POST['group_'.$i.'_tutor'] : null;
  25. $group1['places'] = isset($_POST['group_'.$i.'_places']) ? $_POST['group_'.$i.'_places'] : null;
  26. $groups[] = $group1;
  27. }
  28. foreach ($groups as $index => $group) {
  29. if (!empty($_POST['same_tutor'])) {
  30. $group['tutor'] = $_POST['group_0_tutor'];
  31. }
  32. if (!empty($_POST['same_places'])) {
  33. $group['places'] = $_POST['group_0_places'];
  34. }
  35. if (api_get_setting('allow_group_categories') == 'false') {
  36. $group['category'] = GroupManager::DEFAULT_GROUP_CATEGORY;
  37. } elseif (isset($_POST['same_category']) && $_POST['same_category']) {
  38. $group['category'] = $_POST['group_0_category'];
  39. }
  40. GroupManager::create_group(
  41. $group['name'],
  42. $group['category'],
  43. $group['tutor'],
  44. $group['places']
  45. );
  46. }
  47. $msg = urlencode(count($groups).' '.get_lang('GroupsAdded'));
  48. header('Location: group.php?action=show_msg&msg='.$msg);
  49. exit;
  50. break;
  51. case 'create_virtual_groups':
  52. $ids = GroupManager::create_groups_from_virtual_courses();
  53. $msg = urlencode(count($ids).' '.get_lang('GroupsAdded'));
  54. header('Location: group.php?action=show_msg&msg='.$msg);
  55. exit;
  56. break;
  57. case 'create_subgroups':
  58. GroupManager::create_subgroups($_POST['base_group'], $_POST['number_of_groups']);
  59. $msg = urlencode($_POST['number_of_groups'].' '.get_lang('GroupsAdded'));
  60. header('Location: group.php?action=show_msg&msg='.$msg);
  61. exit;
  62. break;
  63. case 'create_class_groups':
  64. $ids = GroupManager::create_class_groups($_POST['group_category']);
  65. $msg = urlencode(count($ids).' '.get_lang('GroupsAdded'));
  66. header('Location: group.php?action=show_msg&msg='.$msg);
  67. exit;
  68. break;
  69. }
  70. }
  71. $nameTools = get_lang('GroupCreation');
  72. $interbreadcrumb[] = array ('url' => 'group.php', 'name' => get_lang('Groups'));
  73. Display :: display_header($nameTools, 'Group');
  74. /* Show group-settings-form */
  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(get_lang('PleaseEnterValidNumber').'<br /><br /><a href="group_creation.php?'.api_get_cidreq().'">&laquo; '.get_lang('Back').'</a>', false);
  78. } else {
  79. $number_of_groups = intval($_POST['number_of_groups']);
  80. if ($number_of_groups > 1) {
  81. ?>
  82. <script>
  83. var number_of_groups = <?php echo $number_of_groups; ?>;
  84. function switch_state(key) {
  85. for( i=1; i<number_of_groups; i++) {
  86. element = document.getElementById(key+'_'+i);
  87. element.disabled = !element.disabled;
  88. disabled = element.disabled;
  89. }
  90. ref = document.getElementById(key+'_0');
  91. if( disabled ) {
  92. ref.addEventListener("change", copy, false);
  93. } else {
  94. ref.removeEventListener("change", copy, false);
  95. }
  96. copy_value(key);
  97. }
  98. function copy(e) {
  99. key = e.currentTarget.id;
  100. var re = new RegExp ('_0', '') ;
  101. var key = key.replace(re, '') ;
  102. copy_value(key);
  103. }
  104. function copy_value(key) {
  105. ref = document.getElementById(key+'_0');
  106. for( i=1; i<number_of_groups; i++) {
  107. element = document.getElementById(key+'_'+i);
  108. element.value = ref.value;
  109. }
  110. }
  111. </script>
  112. <?php
  113. }
  114. $group_categories = GroupManager::get_categories();
  115. $group_id = GroupManager :: get_number_of_groups() + 1;
  116. foreach ($group_categories as $index => $category) {
  117. // Don't allow new groups in the virtual course category!
  118. if ($category['id'] != GroupManager::VIRTUAL_COURSE_CATEGORY) {
  119. $cat_options[$category['id']] = $category['title'];
  120. }
  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>\n<table>\n{content}\n</table>\n</div>\n</form>";
  126. $renderer->setFormTemplate($form_template);
  127. $element_template = <<<EOT
  128. <tr>
  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->setElementTemplate($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, "\n</td>\n<td>\n", 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('select', 'group_'.$group_number.'_category', null, $cat_options, array('id' => 'category_'.$group_number));
  168. }
  169. $group_el[] = $form->createElement('text', 'group_'.$group_number.'_places', null, array('class' => 'span1', 'id' => 'places_'.$group_number));
  170. if ($_POST['number_of_groups'] < 10000) {
  171. if ($group_id < 10) {
  172. $prev = '000';
  173. } elseif ($group_id < 100) {
  174. $prev = '00';
  175. } elseif ($group_id<1000) {
  176. $prev = '0';
  177. } else {
  178. $prev = '';
  179. }
  180. }
  181. $defaults['group_'.$group_number.'_name'] = get_lang('GroupSingle').' '.$prev.$group_id ++;
  182. $form->addGroup($group_el, 'group_'.$group_number, null, '</td><td>', false);
  183. }
  184. $defaults['action'] = 'create_groups';
  185. $defaults['number_of_groups'] = intval($_POST['number_of_groups']);
  186. $form->setDefaults($defaults);
  187. $form->addElement('style_submit_button', 'submit', get_lang('CreateGroup'), 'class="save"');
  188. $form->display();
  189. }
  190. } else {
  191. /*
  192. * Show form to generate new groups
  193. */
  194. $categories = GroupManager :: get_categories();
  195. if (count($categories) > 1 || isset ($categories[0]) && $categories[0]['id'] != GroupManager::VIRTUAL_COURSE_CATEGORY) {
  196. $create_groups_form = new FormValidator('create_groups', 'post', api_get_self().'?'.api_get_cidreq());
  197. $create_groups_form->addElement('header', $nameTools);
  198. $group_el = array ();
  199. $group_el[] = $create_groups_form->createElement('text', 'number_of_groups', array(get_lang('Create'), '1'));
  200. $group_el[] = $create_groups_form->createElement('style_submit_button', 'submit', get_lang('ProceedToCreateGroup'), 'class="save"');
  201. $create_groups_form->addGroup($group_el, 'create_groups', get_lang('NumberOfGroupsToCreate'), ' ', false);
  202. $defaults = array();
  203. $defaults['number_of_groups'] = 1;
  204. $create_groups_form->setDefaults($defaults);
  205. $create_groups_form->display();
  206. } else {
  207. echo get_lang('NoCategoriesDefined');
  208. }
  209. /*
  210. * Show form to generate subgroups
  211. */
  212. if (api_get_setting('allow_group_categories') == 'true' && count(GroupManager :: get_group_list()) > 0) {
  213. $base_group_options = array ();
  214. $groups = GroupManager :: get_group_list();
  215. foreach ($groups as $index => $group) {
  216. $number_of_students = GroupManager :: number_of_students($group['id']);
  217. if ($number_of_students > 0) {
  218. $base_group_options[$group['id']] = $group['name'].' ('.$number_of_students.' '.get_lang('Users').')';
  219. }
  220. }
  221. if (count($base_group_options) > 0) {
  222. $create_subgroups_form = new FormValidator('create_subgroups');
  223. $create_subgroups_form->addElement('header', get_lang('CreateSubgroups'));
  224. $create_subgroups_form->addElement('html', get_lang('CreateSubgroupsInfo'));
  225. $create_subgroups_form->addElement('hidden', 'action');
  226. $group_el = array();
  227. $group_el[] = $create_subgroups_form->createElement('static', null, null, get_lang('CreateNumberOfGroups'));
  228. $group_el[] = $create_subgroups_form->createElement('text', 'number_of_groups', null, array('size' => 3));
  229. $group_el[] = $create_subgroups_form->createElement('static', null, null, get_lang('WithUsersFrom'));
  230. $group_el[] = $create_subgroups_form->createElement('select', 'base_group', null, $base_group_options);
  231. $group_el[] = $create_subgroups_form->createElement('button', 'submit', get_lang('Ok'));
  232. $create_subgroups_form->addGroup($group_el, 'create_groups', null, ' ', false);
  233. $defaults = array();
  234. $defaults['action'] = 'create_subgroups';
  235. $create_subgroups_form->setDefaults($defaults);
  236. $create_subgroups_form->display();
  237. }
  238. }
  239. /*
  240. * Show form to generate groups from classes subscribed to the course
  241. */
  242. $options['where'] = array(" usergroup.course_id = ? " => api_get_real_course_id());
  243. $obj = new UserGroup();
  244. $classes = $obj->get_usergroup_in_course($options);
  245. if (count($classes) > 0) {
  246. echo '<b>'.get_lang('GroupsFromClasses').'</b>';
  247. echo '<blockquote>';
  248. echo '<p>'.get_lang('GroupsFromClassesInfo').'</p>';
  249. echo '<ul>';
  250. foreach ($classes as $index => $class) {
  251. $number_of_users = count($obj->get_users_by_usergroup($class['id']));
  252. echo '<li>';
  253. echo $class['name'];
  254. echo ' ('.$number_of_users.' '.get_lang('Users').')';
  255. echo '</li>';
  256. }
  257. echo '</ul>';
  258. $create_class_groups_form = new FormValidator('create_class_groups_form');
  259. $create_class_groups_form->addElement('hidden', 'action');
  260. if (api_get_setting('allow_group_categories') == 'true') {
  261. $group_categories = GroupManager :: get_categories();
  262. $cat_options = array ();
  263. foreach ($group_categories as $index => $category) {
  264. // Don't allow new groups in the virtual course category!
  265. if ($category['id'] != GroupManager::VIRTUAL_COURSE_CATEGORY) {
  266. $cat_options[$category['id']] = $category['title'];
  267. }
  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();