group_creation.php 12 KB

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