group_creation.php 12 KB

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