group_creation.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /*
  3. ===============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Hugues Peeters
  9. Copyright (c) Thomas Depraetere
  10. Copyright (c) Christophe Gesche
  11. Copyright (c) Roan Embrechts
  12. Copyright (c) Bart Mollet
  13. For a full list of contributors, see "credits.txt".
  14. The full license can be read in "license.txt".
  15. This program is free software; you can redistribute it and/or
  16. modify it under the terms of the GNU General Public License
  17. as published by the Free Software Foundation; either version 2
  18. of the License, or (at your option) any later version.
  19. See the GNU General Public License for more details.
  20. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  21. ===============================================================================
  22. */
  23. /**
  24. ==============================================================================
  25. * @package dokeos.group
  26. ==============================================================================
  27. */
  28. /*
  29. ===============================================================================
  30. DOKEOS INIT SETTINGS
  31. ===============================================================================
  32. */
  33. // name of the language file that needs to be included
  34. $language_file = "group";
  35. require_once ('../inc/global.inc.php');
  36. $this_section = SECTION_COURSES;
  37. require_once (api_get_path(LIBRARY_PATH).'groupmanager.lib.php');
  38. require_once (api_get_path(LIBRARY_PATH).'classmanager.lib.php');
  39. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  40. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  41. /*
  42. --------------------------------------
  43. Create the groups
  44. --------------------------------------
  45. */
  46. if (isset ($_POST['action']))
  47. {
  48. switch ($_POST['action'])
  49. {
  50. case 'create_groups' :
  51. $groups = array ();
  52. for ($i = 0; $i < $_POST['number_of_groups']; $i ++)
  53. {
  54. $group['name'] = strlen($_POST['group_'.$i.'_name']) == 0 ? get_lang('Group').' '.$i : $_POST['group_'.$i.'_name'] ;
  55. $group['category'] = $_POST['group_'.$i.'_category'];
  56. $group['tutor'] = $_POST['group_'.$i.'_tutor'];
  57. $group['places'] = $_POST['group_'.$i.'_places'];
  58. $groups[] = $group;
  59. }
  60. foreach ($groups as $index => $group)
  61. {
  62. if ($_POST['same_tutor'])
  63. {
  64. $group['tutor'] = $_POST['group_0_tutor'];
  65. }
  66. if ($_POST['same_places'])
  67. {
  68. $group['places'] = $_POST['group_0_places'];
  69. }
  70. if (get_setting('allow_group_categories') == 'false')
  71. {
  72. $group['category'] = DEFAULT_GROUP_CATEGORY;
  73. }
  74. elseif ($_POST['same_category'])
  75. {
  76. $group['category'] = $_POST['group_0_category'];
  77. }
  78. GroupManager :: create_group(strip_tags($group['name']), $group['category'],$group['tutor'] , $group['places']);
  79. }
  80. $msg = urlencode(count($groups).' '.get_lang('GroupsAdded'));
  81. header('Location: group.php?action=show_msg&msg='.$msg);
  82. break;
  83. case 'create_virtual_groups' :
  84. $ids = GroupManager :: create_groups_from_virtual_courses();
  85. $msg = urlencode(count($ids).' '.get_lang('GroupsAdded'));
  86. header('Location: group.php?action=show_msg&msg='.$msg);
  87. break;
  88. case 'create_subgroups' :
  89. GroupManager :: create_subgroups($_POST['base_group'], $_POST['number_of_groups']);
  90. $msg = urlencode($_POST['number_of_groups'].' '.get_lang('GroupsAdded'));
  91. header('Location: group.php?action=show_msg&msg='.$msg);
  92. break;
  93. case 'create_class_groups' :
  94. $ids = GroupManager :: create_class_groups($_POST['group_category']);
  95. $msg = urlencode(count($ids).' '.get_lang('GroupsAdded'));
  96. header('Location: group.php?action=show_msg&msg='.$msg);
  97. break;
  98. }
  99. }
  100. $nameTools = get_lang('GroupCreation');
  101. $interbreadcrumb[] = array ("url" => "group.php", "name" => get_lang('GroupManagement'));
  102. Display :: display_header($nameTools, "Group");
  103. api_display_tool_title($nameTools);
  104. if (!is_allowed_to_edit())
  105. {
  106. api_not_allowed();
  107. }
  108. /*
  109. ===============================================================================
  110. MAIN TOOL CODE
  111. ===============================================================================
  112. */
  113. /*
  114. --------------------------------------
  115. Show group-settings-form
  116. --------------------------------------
  117. */
  118. elseif (isset ($_POST['number_of_groups']))
  119. {
  120. if (!is_numeric($_POST['number_of_groups']) || intval($_POST['number_of_groups']) < 1)
  121. {
  122. Display :: display_error_message(get_lang('PleaseEnterValidNumber').'<br/><br/><a href="group_creation.php">&laquo; '.get_lang('Back').'</a>');
  123. }
  124. else
  125. {
  126. $number_of_groups = intval($_POST['number_of_groups']);
  127. if ($number_of_groups > 1)
  128. {
  129. ?>
  130. <script type="text/javascript">
  131. <!--
  132. var number_of_groups = <?php echo $number_of_groups; ?>;
  133. function switch_state(key)
  134. {
  135. for( i=1; i<number_of_groups; i++)
  136. {
  137. element = document.getElementById(key+'_'+i);
  138. element.disabled = !element.disabled;
  139. disabled = element.disabled;
  140. }
  141. ref = document.getElementById(key+'_0');
  142. if( disabled )
  143. {
  144. ref.addEventListener("change", copy, false);
  145. }
  146. else
  147. {
  148. ref.removeEventListener("change", copy, false);
  149. }
  150. copy_value(key);
  151. }
  152. function copy(e)
  153. {
  154. key = e.currentTarget.id;
  155. var re = new RegExp ('_0', '') ;
  156. var key = key.replace(re, '') ;
  157. copy_value(key);
  158. }
  159. function copy_value(key)
  160. {
  161. ref = document.getElementById(key+'_0');
  162. for( i=1; i<number_of_groups; i++)
  163. {
  164. element = document.getElementById(key+'_'+i);
  165. element.value = ref.value;
  166. }
  167. }
  168. -->
  169. </script>
  170. <?php
  171. }
  172. $group_categories = GroupManager :: get_categories();
  173. $group_id = GroupManager :: get_number_of_groups() + 1;
  174. /*$tutors = GroupManager :: get_all_tutors();
  175. $tutor_options[0] = get_lang('GroupNoTutor');
  176. foreach ($tutors as $index => $tutor)
  177. {
  178. $tutor_options[$tutor['user_id']] = $tutor['firstname'].' '.$tutor['lastname'];
  179. }
  180. $cat_options = array ();
  181. */
  182. foreach ($group_categories as $index => $category)
  183. {
  184. // Don't allow new groups in the virtual course category!
  185. if ($category['id'] != VIRTUAL_COURSE_CATEGORY)
  186. {
  187. $cat_options[$category['id']] = $category['title'];
  188. }
  189. }
  190. $form = new FormValidator('create_groups_step2');
  191. // Modify the default templates
  192. $renderer = & $form->defaultRenderer();
  193. $form_template = "<form {attributes}>\n<div>\n<table>\n{content}\n</table>\n</div>\n</form>";
  194. $renderer->setFormTemplate($form_template);
  195. $element_template = <<<EOT
  196. <tr>
  197. <td>
  198. <!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->{label}
  199. </td>
  200. <td>
  201. <!-- BEGIN error --><span class="form_error">{error}</span><br /><!-- END error --> {element}
  202. </td>
  203. </tr>
  204. EOT;
  205. $renderer->setElementTemplate($element_template);
  206. $form->addElement('hidden', 'action');
  207. $form->addElement('hidden', 'number_of_groups');
  208. $defaults = array ();
  209. // Table heading
  210. $group_el = array ();
  211. $group_el[] = & $form->createElement('static', null, null, '<b>'.get_lang('GroupName').'</b>');
  212. if (get_setting('allow_group_categories') == 'true')
  213. {
  214. $group_el[] = & $form->createElement('static', null, null, '<b>'.get_lang('GroupCategory').'</b>');
  215. }
  216. //$group_el[] = & $form->createElement('static', null, null, '<b>'.get_lang('GroupTutor').'</b>');
  217. $group_el[] = & $form->createElement('static', null, null, '<b>'.get_lang('GroupPlacesThis').'</b>');
  218. $form->addGroup($group_el, 'groups', null, "\n</td>\n<td>\n", false);
  219. // Checkboxes
  220. if ($_POST['number_of_groups'] > 1)
  221. {
  222. $group_el = array ();
  223. $group_el[] = & $form->createElement('static', null, null, ' ');
  224. if (get_setting('allow_group_categories') == 'true')
  225. {
  226. $group_el[] = & $form->createElement('checkbox', 'same_category', null, get_lang('SameForAll'), array ('onclick' => "javascript:switch_state('category')"));
  227. }
  228. //$group_el[] = & $form->createElement('checkbox', 'same_tutor', null, get_lang('SameForAll'), array ('onclick' => "javascript:switch_state('tutor')"));
  229. $group_el[] = & $form->createElement('checkbox', 'same_places', null, get_lang('SameForAll'), array ('onclick' => "javascript:switch_state('places')"));
  230. $form->addGroup($group_el, 'groups', null, '</td><td>', false);
  231. }
  232. // Properties for all groups
  233. for ($group_number = 0; $group_number < $_POST['number_of_groups']; $group_number ++)
  234. {
  235. $group_el = array ();
  236. $group_el[] = & $form->createElement('text', 'group_'.$group_number.'_name');
  237. if (get_setting('allow_group_categories') == 'true')
  238. {
  239. $group_el[] = & $form->createElement('select', 'group_'.$group_number.'_category', null, $cat_options, array ('id' => 'category_'.$group_number));
  240. }
  241. //$group_el[] = & $form->createElement('select', 'group_'.$group_number.'_tutor', null, $tutor_options, array ('id' => 'tutor_'.$group_number));
  242. $group_el[] = & $form->createElement('text', 'group_'.$group_number.'_places', null, array ('size' => 3, 'id' => 'places_'.$group_number));
  243. $defaults['group_'.$group_number.'_name'] = get_lang('Group').' '.$group_id ++;
  244. $form->addGroup($group_el, 'group_'.$group_number, null, '</td><td>', false);
  245. }
  246. $defaults['action'] = 'create_groups';
  247. $defaults['number_of_groups'] = $_POST['number_of_groups'];
  248. $form->setDefaults($defaults);
  249. $form->addElement('submit', 'submit', get_lang('Ok'));
  250. $form->display();
  251. }
  252. }
  253. else
  254. {
  255. /*
  256. * Show form to generate new groups
  257. */
  258. $categories = GroupManager :: get_categories();
  259. echo '<b>'.get_lang('NewGroups').'</b>';
  260. echo '<blockquote>';
  261. if (count($categories) > 1 || isset ($categories[0]) && $categories[0]['id'] != VIRTUAL_COURSE_CATEGORY)
  262. {
  263. $create_groups_form = new FormValidator('create_groups');
  264. $group_el = array ();
  265. $group_el[] = & $create_groups_form->createElement('static', null, null, get_lang('Create'));
  266. $group_el[] = & $create_groups_form->createElement('text', 'number_of_groups', null, array ('size' => 3));
  267. $group_el[] = & $create_groups_form->createElement('static', null, null, get_lang('NewGroups'));
  268. $group_el[] = & $create_groups_form->createElement('submit', 'submit', get_lang('Ok'));
  269. $create_groups_form->addGroup($group_el, 'create_groups', null, ' ', false);
  270. $defaults = array ();
  271. $defaults['number_of_groups'] = 1;
  272. $create_groups_form->setDefaults($defaults);
  273. $create_groups_form->display();
  274. }
  275. else
  276. {
  277. echo get_lang('NoCategoriesDefined');
  278. }
  279. echo '</blockquote>';
  280. /*
  281. * Show form to generate groups from virtual courses
  282. */
  283. $virtual_courses = CourseManager :: get_virtual_courses_linked_to_real_course($_course['sysCode']);
  284. if (count($virtual_courses) > 0)
  285. {
  286. echo '<b>'.get_lang('CreateGroupsFromVirtualCourses').'</b>';
  287. echo '<blockquote>';
  288. echo get_lang('CreateGroupsFromVirtualCoursesInfo');
  289. $create_virtual_groups_form = new FormValidator('create_virtual_groups');
  290. $create_virtual_groups_form->addElement('hidden', 'action');
  291. $create_virtual_groups_form->addElement('submit', 'submit', get_lang('Ok'));
  292. $create_virtual_groups_form->setDefaults(array ('action' => 'create_virtual_groups'));
  293. $create_virtual_groups_form->display();
  294. echo '</blockquote>';
  295. }
  296. /*
  297. * Show form to generate subgroups
  298. */
  299. if (get_setting('allow_group_categories') == 'true' && count(GroupManager :: get_group_list()) > 0)
  300. {
  301. $base_group_options = array ();
  302. $groups = GroupManager :: get_group_list();
  303. foreach ($groups as $index => $group)
  304. {
  305. $number_of_students = GroupManager :: number_of_students($group['id']);
  306. if ($number_of_students > 0)
  307. {
  308. $base_group_options[$group['id']] = $group['name'].' ('.$number_of_students.' '.get_lang('Users').')';
  309. }
  310. }
  311. if (count($base_group_options) > 0)
  312. {
  313. echo '<b>'.get_lang('CreateSubgroups').'</b>';
  314. echo '<blockquote>';
  315. echo '<p>'.get_lang('CreateSubgroupsInfo').'</p>';
  316. $create_subgroups_form = new FormValidator('create_subgroups');
  317. $create_subgroups_form->addElement('hidden', 'action');
  318. $group_el = array ();
  319. $group_el[] = & $create_subgroups_form->createElement('static', null, null, get_lang('CreateNumberOfGroups'));
  320. $group_el[] = & $create_subgroups_form->createElement('text', 'number_of_groups', null, array ('size' => 3));
  321. $group_el[] = & $create_subgroups_form->createElement('static', null, null, get_lang('WithUsersFrom'));
  322. $group_el[] = & $create_subgroups_form->createElement('select', 'base_group', null, $base_group_options);
  323. $group_el[] = & $create_subgroups_form->createElement('submit', 'submit', get_lang('Ok'));
  324. $create_subgroups_form->addGroup($group_el, 'create_groups', null, ' ', false);
  325. $defaults = array ();
  326. $defaults['action'] = 'create_subgroups';
  327. $create_subgroups_form->setDefaults($defaults);
  328. $create_subgroups_form->display();
  329. echo '</blockquote>';
  330. }
  331. }
  332. /*
  333. * Show form to generate groups from classes subscribed to the course
  334. */
  335. $classes = ClassManager :: get_classes_in_course($_course['sysCode']);
  336. if (count($classes) > 0)
  337. {
  338. echo '<b>'.get_lang('GroupsFromClasses').'</b>';
  339. echo '<blockquote>';
  340. echo '<p>'.get_lang('GroupsFromClassesInfo').'</p>';
  341. echo '<ul>';
  342. foreach ($classes as $index => $class)
  343. {
  344. $number_of_users = count(ClassManager :: get_users($class['id']));
  345. echo '<li>';
  346. echo $class['name'];
  347. echo ' ('.$number_of_users.' '.get_lang('Users').')';
  348. echo '</li>';
  349. }
  350. echo '</ul>';
  351. $create_class_groups_form = new FormValidator('create_class_groups_form');
  352. $create_class_groups_form->addElement('hidden', 'action');
  353. if (get_setting('allow_group_categories') == 'true')
  354. {
  355. $group_categories = GroupManager :: get_categories();
  356. $cat_options = array ();
  357. foreach ($group_categories as $index => $category)
  358. {
  359. // Don't allow new groups in the virtual course category!
  360. if ($category['id'] != VIRTUAL_COURSE_CATEGORY)
  361. {
  362. $cat_options[$category['id']] = $category['title'];
  363. }
  364. }
  365. $create_class_groups_form->addElement('select', 'group_category', null, $cat_options);
  366. }
  367. else
  368. {
  369. $create_class_groups_form->addElement('hidden', 'group_category');
  370. }
  371. $create_class_groups_form->addElement('submit', 'submit', get_lang('Ok'));
  372. $defaults['group_category'] = DEFAULT_GROUP_CATEGORY;
  373. $defaults['action'] = 'create_class_groups';
  374. $create_class_groups_form->setDefaults($defaults);
  375. $create_class_groups_form->display();
  376. echo '</blockquote>';
  377. }
  378. }
  379. /*
  380. ===============================================================================
  381. DOKEOS FOOTER
  382. ===============================================================================
  383. */
  384. Display :: display_footer();
  385. ?>