group_creation.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. require_once api_get_path(LIBRARY_PATH).'groupmanager.lib.php';
  12. require_once api_get_path(LIBRARY_PATH).'classmanager.lib.php';
  13. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  14. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  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'] = DEFAULT_GROUP_CATEGORY;
  36. } elseif ($_POST['same_category']) {
  37. $group['category'] = $_POST['group_0_category'];
  38. }
  39. GroupManager :: create_group(strip_tags($group['name']), $group['category'], $group['tutor'], $group['places']);
  40. }
  41. $msg = urlencode(count($groups).' '.get_lang('GroupsAdded'));
  42. header('Location: group.php?action=show_msg&msg='.$msg);
  43. break;
  44. case 'create_virtual_groups':
  45. $ids = GroupManager :: create_groups_from_virtual_courses();
  46. $msg = urlencode(count($ids).' '.get_lang('GroupsAdded'));
  47. header('Location: group.php?action=show_msg&msg='.$msg);
  48. break;
  49. case 'create_subgroups':
  50. GroupManager :: create_subgroups($_POST['base_group'], $_POST['number_of_groups']);
  51. $msg = urlencode($_POST['number_of_groups'].' '.get_lang('GroupsAdded'));
  52. header('Location: group.php?action=show_msg&msg='.$msg);
  53. break;
  54. case 'create_class_groups':
  55. $ids = GroupManager :: create_class_groups($_POST['group_category']);
  56. $msg = urlencode(count($ids).' '.get_lang('GroupsAdded'));
  57. header('Location: group.php?action=show_msg&msg='.$msg);
  58. break;
  59. }
  60. }
  61. $nameTools = get_lang('GroupCreation');
  62. $interbreadcrumb[] = array ('url' => 'group.php', 'name' => get_lang('Groups'));
  63. Display :: display_header($nameTools, 'Group');
  64. if (!api_is_allowed_to_edit(false, true)) {
  65. api_not_allowed();
  66. }
  67. /* MAIN TOOL CODE */
  68. /* Show group-settings-form */
  69. elseif (isset($_POST['number_of_groups'])) {
  70. if (!is_numeric($_POST['number_of_groups']) || intval($_POST['number_of_groups']) < 1) {
  71. Display :: display_error_message(get_lang('PleaseEnterValidNumber').'<br /><br /><a href="group_creation.php?'.api_get_cidreq().'">&laquo; '.get_lang('Back').'</a>', false);
  72. } else {
  73. $number_of_groups = intval($_POST['number_of_groups']);
  74. if ($number_of_groups > 1) {
  75. ?>
  76. <script type="text/javascript">
  77. <!--
  78. var number_of_groups = <?php echo $number_of_groups; ?>;
  79. function switch_state(key)
  80. {
  81. for( i=1; i<number_of_groups; i++)
  82. {
  83. element = document.getElementById(key+'_'+i);
  84. element.disabled = !element.disabled;
  85. disabled = element.disabled;
  86. }
  87. ref = document.getElementById(key+'_0');
  88. if( disabled )
  89. {
  90. ref.addEventListener("change", copy, false);
  91. }
  92. else
  93. {
  94. ref.removeEventListener("change", copy, false);
  95. }
  96. copy_value(key);
  97. }
  98. function copy(e)
  99. {
  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. {
  107. ref = document.getElementById(key+'_0');
  108. for( i=1; i<number_of_groups; i++)
  109. {
  110. element = document.getElementById(key+'_'+i);
  111. element.value = ref.value;
  112. }
  113. }
  114. -->
  115. </script>
  116. <?php
  117. }
  118. $group_categories = GroupManager :: get_categories();
  119. $group_id = GroupManager :: get_number_of_groups() + 1;
  120. /*$tutors = GroupManager :: get_all_tutors();
  121. $tutor_options[0] = get_lang('GroupNoTutor');
  122. foreach ($tutors as $index => $tutor) {
  123. $tutor_options[$tutor['user_id']] = api_get_person_name($tutor['firstname'], $tutor['lastname']);
  124. }
  125. $cat_options = array ();
  126. */
  127. foreach ($group_categories as $index => $category) {
  128. // Don't allow new groups in the virtual course category!
  129. if ($category['id'] != VIRTUAL_COURSE_CATEGORY) {
  130. $cat_options[$category['id']] = $category['title'];
  131. }
  132. }
  133. $form = new FormValidator('create_groups_step2');
  134. // Modify the default templates
  135. $renderer = & $form->defaultRenderer();
  136. $form_template = "<form {attributes}>\n<div>\n<table>\n{content}\n</table>\n</div>\n</form>";
  137. $renderer->setFormTemplate($form_template);
  138. $element_template = <<<EOT
  139. <tr>
  140. <td>
  141. <!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->{label}
  142. </td>
  143. <td>
  144. <!-- BEGIN error --><span class="form_error">{error}</span><br /><!-- END error --> {element}
  145. </td>
  146. </tr>
  147. EOT;
  148. $renderer->setElementTemplate($element_template);
  149. $form->addElement('header', '', $nameTools);
  150. $form->addElement('hidden', 'action');
  151. $form->addElement('hidden', 'number_of_groups');
  152. $defaults = array ();
  153. // Table heading
  154. $group_el = array ();
  155. $group_el[] = & $form->createElement('static', null, null, '<b>'.get_lang('GroupName').'</b>');
  156. if (api_get_setting('allow_group_categories') == 'true') {
  157. $group_el[] = & $form->createElement('static', null, null, '<b>'.get_lang('GroupCategory').'</b>');
  158. }
  159. //$group_el[] = & $form->createElement('static', null, null, '<b>'.get_lang('GroupTutor').'</b>');
  160. $group_el[] = & $form->createElement('static', null, null, '<b>'.get_lang('GroupPlacesThis').'</b>');
  161. $form->addGroup($group_el, 'groups', null, "\n</td>\n<td>\n", false);
  162. // Checkboxes
  163. if ($_POST['number_of_groups'] > 1) {
  164. $group_el = array ();
  165. $group_el[] = & $form->createElement('static', null, null, ' ');
  166. if (api_get_setting('allow_group_categories') == 'true') {
  167. $group_el[] = & $form->createElement('checkbox', 'same_category', null, get_lang('SameForAll'), array('onclick' => "javascript: switch_state('category');"));
  168. }
  169. //$group_el[] = & $form->createElement('checkbox', 'same_tutor', null, get_lang('SameForAll'), array ('onclick' => "javascript: switch_state('tutor');"));
  170. $group_el[] = & $form->createElement('checkbox', 'same_places', null, get_lang('SameForAll'), array ('onclick' => "javascript: switch_state('places');"));
  171. $form->addGroup($group_el, 'groups', null, '</td><td>', false);
  172. }
  173. // Properties for all groups
  174. for ($group_number = 0; $group_number < $_POST['number_of_groups']; $group_number ++) {
  175. $group_el = array ();
  176. $group_el[] = & $form->createElement('text', 'group_'.$group_number.'_name');
  177. if (api_get_setting('allow_group_categories') == 'true') {
  178. $group_el[] = & $form->createElement('select', 'group_'.$group_number.'_category', null, $cat_options, array('id' => 'category_'.$group_number));
  179. }
  180. //$group_el[] = & $form->createElement('select', 'group_'.$group_number.'_tutor', null, $tutor_options, array('id' => 'tutor_'.$group_number));
  181. $group_el[] = & $form->createElement('text', 'group_'.$group_number.'_places', null, array('size' => 3, 'id' => 'places_'.$group_number));
  182. if ($_POST['number_of_groups'] < 10000) {
  183. if ($group_id < 10) {
  184. $prev = '000';
  185. } elseif ($group_id < 100) {
  186. $prev = '00';
  187. } elseif ($group_id<1000) {
  188. $prev = '0';
  189. } else {
  190. $prev = '';
  191. }
  192. }
  193. $defaults['group_'.$group_number.'_name'] = get_lang('GroupSingle').' '.$prev.$group_id ++;
  194. $form->addGroup($group_el, 'group_'.$group_number, null, '</td><td>', false);
  195. }
  196. $defaults['action'] = 'create_groups';
  197. $defaults['number_of_groups'] = intval($_POST['number_of_groups']);
  198. $form->setDefaults($defaults);
  199. $form->addElement('style_submit_button', 'submit', get_lang('CreateGroup'), 'class="save"');
  200. $form->display();
  201. }
  202. } else {
  203. /*
  204. * Show form to generate new groups
  205. */
  206. $categories = GroupManager :: get_categories();
  207. //echo '<blockquote>';
  208. if (count($categories) > 1 || isset ($categories[0]) && $categories[0]['id'] != VIRTUAL_COURSE_CATEGORY) {
  209. $create_groups_form = new FormValidator('create_groups');
  210. $create_groups_form->addElement('header', '', $nameTools);
  211. $group_el = array ();
  212. $group_el[] = & $create_groups_form->createElement('static', null, null, get_lang('Create'));
  213. $group_el[] = & $create_groups_form->createElement('text', 'number_of_groups', null, array('size' => 3));
  214. $group_el[] = & $create_groups_form->createElement('static', null, null, get_lang('NewGroups'));
  215. $group_el[] = & $create_groups_form->createElement('style_submit_button', 'submit', get_lang('ProceedToCreateGroup'), 'class="save"');
  216. $create_groups_form->addGroup($group_el, 'create_groups', null, ' ', false);
  217. $defaults = array();
  218. $defaults['number_of_groups'] = 1;
  219. $create_groups_form->setDefaults($defaults);
  220. $create_groups_form->display();
  221. } else {
  222. echo get_lang('NoCategoriesDefined');
  223. }
  224. //echo '</blockquote>';
  225. /*
  226. * Show form to generate groups from virtual courses
  227. */
  228. $virtual_courses = CourseManager :: get_virtual_courses_linked_to_real_course($_course['sysCode']);
  229. if (count($virtual_courses) > 0) {
  230. echo '<b>'.get_lang('CreateGroupsFromVirtualCourses').'</b>';
  231. echo '<blockquote>';
  232. echo get_lang('CreateGroupsFromVirtualCoursesInfo');
  233. $create_virtual_groups_form = new FormValidator('create_virtual_groups');
  234. $create_virtual_groups_form->addElement('hidden', 'action');
  235. $create_virtual_groups_form->addElement('submit', 'submit', get_lang('Ok'));
  236. $create_virtual_groups_form->setDefaults(array('action' => 'create_virtual_groups'));
  237. $create_virtual_groups_form->display();
  238. echo '</blockquote>';
  239. }
  240. /*
  241. * Show form to generate subgroups
  242. */
  243. if (api_get_setting('allow_group_categories') == 'true' && count(GroupManager :: get_group_list()) > 0) {
  244. $base_group_options = array ();
  245. $groups = GroupManager :: get_group_list();
  246. foreach ($groups as $index => $group) {
  247. $number_of_students = GroupManager :: number_of_students($group['id']);
  248. if ($number_of_students > 0) {
  249. $base_group_options[$group['id']] = $group['name'].' ('.$number_of_students.' '.get_lang('Users').')';
  250. }
  251. }
  252. if (count($base_group_options) > 0) {
  253. echo '<b>'.get_lang('CreateSubgroups').'</b>';
  254. echo '<blockquote>';
  255. echo '<p>'.get_lang('CreateSubgroupsInfo').'</p>';
  256. $create_subgroups_form = new FormValidator('create_subgroups');
  257. $create_subgroups_form->addElement('hidden', 'action');
  258. $group_el = array();
  259. $group_el[] = & $create_subgroups_form->createElement('static', null, null, get_lang('CreateNumberOfGroups'));
  260. $group_el[] = & $create_subgroups_form->createElement('text', 'number_of_groups', null, array('size' => 3));
  261. $group_el[] = & $create_subgroups_form->createElement('static', null, null, get_lang('WithUsersFrom'));
  262. $group_el[] = & $create_subgroups_form->createElement('select', 'base_group', null, $base_group_options);
  263. $group_el[] = & $create_subgroups_form->createElement('submit', 'submit', get_lang('Ok'));
  264. $create_subgroups_form->addGroup($group_el, 'create_groups', null, ' ', false);
  265. $defaults = array();
  266. $defaults['action'] = 'create_subgroups';
  267. $create_subgroups_form->setDefaults($defaults);
  268. $create_subgroups_form->display();
  269. echo '</blockquote>';
  270. }
  271. }
  272. /*
  273. * Show form to generate groups from classes subscribed to the course
  274. */
  275. $classes = ClassManager :: get_classes_in_course($_course['sysCode']);
  276. if (count($classes) > 0) {
  277. echo '<b>'.get_lang('GroupsFromClasses').'</b>';
  278. echo '<blockquote>';
  279. echo '<p>'.get_lang('GroupsFromClassesInfo').'</p>';
  280. echo '<ul>';
  281. foreach ($classes as $index => $class) {
  282. $number_of_users = count(ClassManager :: get_users($class['id']));
  283. echo '<li>';
  284. echo $class['name'];
  285. echo ' ('.$number_of_users.' '.get_lang('Users').')';
  286. echo '</li>';
  287. }
  288. echo '</ul>';
  289. $create_class_groups_form = new FormValidator('create_class_groups_form');
  290. $create_class_groups_form->addElement('hidden', 'action');
  291. if (api_get_setting('allow_group_categories') == 'true') {
  292. $group_categories = GroupManager :: get_categories();
  293. $cat_options = array ();
  294. foreach ($group_categories as $index => $category) {
  295. // Don't allow new groups in the virtual course category!
  296. if ($category['id'] != VIRTUAL_COURSE_CATEGORY) {
  297. $cat_options[$category['id']] = $category['title'];
  298. }
  299. }
  300. $create_class_groups_form->addElement('select', 'group_category', null, $cat_options);
  301. } else {
  302. $create_class_groups_form->addElement('hidden', 'group_category');
  303. }
  304. $create_class_groups_form->addElement('submit', 'submit', get_lang('Ok'));
  305. $defaults['group_category'] = DEFAULT_GROUP_CATEGORY;
  306. $defaults['action'] = 'create_class_groups';
  307. $create_class_groups_form->setDefaults($defaults);
  308. $create_class_groups_form->display();
  309. echo '</blockquote>';
  310. }
  311. }
  312. /* FOOTER */
  313. Display :: display_footer();