group_creation.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. $group1['name'] = api_strlen($_POST['group_'.$i.'_name']) == 0 ? get_lang('Group').' '.$i : $_POST['group_'.$i.'_name'] ;
  55. $group1['category'] = isset($_POST['group_'.$i.'_category'])?$_POST['group_'.$i.'_category']:null;
  56. $group1['tutor'] = isset($_POST['group_'.$i.'_tutor'])?$_POST['group_'.$i.'_tutor']:null;
  57. $group1['places'] = isset($_POST['group_'.$i.'_places'])?$_POST['group_'.$i.'_places']:null;
  58. $groups[] = $group1;
  59. }
  60. foreach ($groups as $index => $group)
  61. {
  62. if (!empty($_POST['same_tutor']))
  63. {
  64. $group['tutor'] = $_POST['group_0_tutor'];
  65. }
  66. if (!empty($_POST['same_places']))
  67. {
  68. $group['places'] = $_POST['group_0_places'];
  69. }
  70. if (api_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('Groups'));
  102. Display :: display_header($nameTools, "Group");
  103. if (!api_is_allowed_to_edit(false,true))
  104. {
  105. api_not_allowed();
  106. }
  107. /*
  108. ===============================================================================
  109. MAIN TOOL CODE
  110. ===============================================================================
  111. */
  112. /*
  113. --------------------------------------
  114. Show group-settings-form
  115. --------------------------------------
  116. */
  117. elseif (isset ($_POST['number_of_groups']))
  118. {
  119. if (!is_numeric($_POST['number_of_groups']) || intval($_POST['number_of_groups']) < 1)
  120. {
  121. Display :: display_error_message(get_lang('PleaseEnterValidNumber').'<br/><br/><a href="group_creation.php?'.api_get_cidreq().'">&laquo; '.get_lang('Back').'</a>',false);
  122. }
  123. else
  124. {
  125. $number_of_groups = intval($_POST['number_of_groups']);
  126. if ($number_of_groups > 1)
  127. {
  128. ?>
  129. <script type="text/javascript">
  130. <!--
  131. var number_of_groups = <?php echo $number_of_groups; ?>;
  132. function switch_state(key)
  133. {
  134. for( i=1; i<number_of_groups; i++)
  135. {
  136. element = document.getElementById(key+'_'+i);
  137. element.disabled = !element.disabled;
  138. disabled = element.disabled;
  139. }
  140. ref = document.getElementById(key+'_0');
  141. if( disabled )
  142. {
  143. ref.addEventListener("change", copy, false);
  144. }
  145. else
  146. {
  147. ref.removeEventListener("change", copy, false);
  148. }
  149. copy_value(key);
  150. }
  151. function copy(e)
  152. {
  153. key = e.currentTarget.id;
  154. var re = new RegExp ('_0', '') ;
  155. var key = key.replace(re, '') ;
  156. copy_value(key);
  157. }
  158. function copy_value(key)
  159. {
  160. ref = document.getElementById(key+'_0');
  161. for( i=1; i<number_of_groups; i++)
  162. {
  163. element = document.getElementById(key+'_'+i);
  164. element.value = ref.value;
  165. }
  166. }
  167. -->
  168. </script>
  169. <?php
  170. }
  171. $group_categories = GroupManager :: get_categories();
  172. $group_id = GroupManager :: get_number_of_groups() + 1;
  173. /*$tutors = GroupManager :: get_all_tutors();
  174. $tutor_options[0] = get_lang('GroupNoTutor');
  175. foreach ($tutors as $index => $tutor)
  176. {
  177. $tutor_options[$tutor['user_id']] = api_get_person_name($tutor['firstname'], $tutor['lastname']);
  178. }
  179. $cat_options = array ();
  180. */
  181. foreach ($group_categories as $index => $category)
  182. {
  183. // Don't allow new groups in the virtual course category!
  184. if ($category['id'] != VIRTUAL_COURSE_CATEGORY)
  185. {
  186. $cat_options[$category['id']] = $category['title'];
  187. }
  188. }
  189. $form = new FormValidator('create_groups_step2');
  190. // Modify the default templates
  191. $renderer = & $form->defaultRenderer();
  192. $form_template = "<form {attributes}>\n<div>\n<table>\n{content}\n</table>\n</div>\n</form>";
  193. $renderer->setFormTemplate($form_template);
  194. $element_template = <<<EOT
  195. <tr>
  196. <td>
  197. <!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->{label}
  198. </td>
  199. <td>
  200. <!-- BEGIN error --><span class="form_error">{error}</span><br /><!-- END error --> {element}
  201. </td>
  202. </tr>
  203. EOT;
  204. $renderer->setElementTemplate($element_template);
  205. $form->addElement('header', '', $nameTools);
  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 (api_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 (api_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 (api_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. if($_POST['number_of_groups']<10000)
  244. {
  245. if ($group_id<10)
  246. {
  247. $prev='000';
  248. }
  249. elseif ($group_id<100)
  250. {
  251. $prev='00';
  252. }
  253. elseif ($group_id<1000)
  254. {
  255. $prev='0';
  256. }
  257. else
  258. {
  259. $prev='';
  260. }
  261. }
  262. $defaults['group_'.$group_number.'_name'] = get_lang('GroupSingle').' '.$prev.$group_id ++;
  263. $form->addGroup($group_el, 'group_'.$group_number, null, '</td><td>', false);
  264. }
  265. $defaults['action'] = 'create_groups';
  266. $defaults['number_of_groups'] = $_POST['number_of_groups'];
  267. $form->setDefaults($defaults);
  268. $form->addElement('style_submit_button', 'submit', get_lang('CreateGroup'), 'class="save"');
  269. $form->display();
  270. }
  271. }
  272. else
  273. {
  274. /*
  275. * Show form to generate new groups
  276. */
  277. $categories = GroupManager :: get_categories();
  278. //echo '<blockquote>';
  279. if (count($categories) > 1 || isset ($categories[0]) && $categories[0]['id'] != VIRTUAL_COURSE_CATEGORY)
  280. {
  281. $create_groups_form = new FormValidator('create_groups');
  282. $create_groups_form->addElement('header', '', $nameTools);
  283. $group_el = array ();
  284. $group_el[] = & $create_groups_form->createElement('static', null, null, get_lang('Create'));
  285. $group_el[] = & $create_groups_form->createElement('text', 'number_of_groups', null, array ('size' => 3));
  286. $group_el[] = & $create_groups_form->createElement('static', null, null, get_lang('NewGroups'));
  287. $group_el[] = & $create_groups_form->createElement('style_submit_button', 'submit', get_lang('ProceedToCreateGroup'), 'class="save"');
  288. $create_groups_form->addGroup($group_el, 'create_groups', null, ' ', false);
  289. $defaults = array ();
  290. $defaults['number_of_groups'] = 1;
  291. $create_groups_form->setDefaults($defaults);
  292. $create_groups_form->display();
  293. }
  294. else
  295. {
  296. echo get_lang('NoCategoriesDefined');
  297. }
  298. //echo '</blockquote>';
  299. /*
  300. * Show form to generate groups from virtual courses
  301. */
  302. $virtual_courses = CourseManager :: get_virtual_courses_linked_to_real_course($_course['sysCode']);
  303. if (count($virtual_courses) > 0)
  304. {
  305. echo '<b>'.get_lang('CreateGroupsFromVirtualCourses').'</b>';
  306. echo '<blockquote>';
  307. echo get_lang('CreateGroupsFromVirtualCoursesInfo');
  308. $create_virtual_groups_form = new FormValidator('create_virtual_groups');
  309. $create_virtual_groups_form->addElement('hidden', 'action');
  310. $create_virtual_groups_form->addElement('submit', 'submit', get_lang('Ok'));
  311. $create_virtual_groups_form->setDefaults(array ('action' => 'create_virtual_groups'));
  312. $create_virtual_groups_form->display();
  313. echo '</blockquote>';
  314. }
  315. /*
  316. * Show form to generate subgroups
  317. */
  318. if (api_get_setting('allow_group_categories') == 'true' && count(GroupManager :: get_group_list()) > 0)
  319. {
  320. $base_group_options = array ();
  321. $groups = GroupManager :: get_group_list();
  322. foreach ($groups as $index => $group)
  323. {
  324. $number_of_students = GroupManager :: number_of_students($group['id']);
  325. if ($number_of_students > 0)
  326. {
  327. $base_group_options[$group['id']] = $group['name'].' ('.$number_of_students.' '.get_lang('Users').')';
  328. }
  329. }
  330. if (count($base_group_options) > 0)
  331. {
  332. echo '<b>'.get_lang('CreateSubgroups').'</b>';
  333. echo '<blockquote>';
  334. echo '<p>'.get_lang('CreateSubgroupsInfo').'</p>';
  335. $create_subgroups_form = new FormValidator('create_subgroups');
  336. $create_subgroups_form->addElement('hidden', 'action');
  337. $group_el = array ();
  338. $group_el[] = & $create_subgroups_form->createElement('static', null, null, get_lang('CreateNumberOfGroups'));
  339. $group_el[] = & $create_subgroups_form->createElement('text', 'number_of_groups', null, array ('size' => 3));
  340. $group_el[] = & $create_subgroups_form->createElement('static', null, null, get_lang('WithUsersFrom'));
  341. $group_el[] = & $create_subgroups_form->createElement('select', 'base_group', null, $base_group_options);
  342. $group_el[] = & $create_subgroups_form->createElement('submit', 'submit', get_lang('Ok'));
  343. $create_subgroups_form->addGroup($group_el, 'create_groups', null, ' ', false);
  344. $defaults = array ();
  345. $defaults['action'] = 'create_subgroups';
  346. $create_subgroups_form->setDefaults($defaults);
  347. $create_subgroups_form->display();
  348. echo '</blockquote>';
  349. }
  350. }
  351. /*
  352. * Show form to generate groups from classes subscribed to the course
  353. */
  354. $classes = ClassManager :: get_classes_in_course($_course['sysCode']);
  355. if (count($classes) > 0)
  356. {
  357. echo '<b>'.get_lang('GroupsFromClasses').'</b>';
  358. echo '<blockquote>';
  359. echo '<p>'.get_lang('GroupsFromClassesInfo').'</p>';
  360. echo '<ul>';
  361. foreach ($classes as $index => $class)
  362. {
  363. $number_of_users = count(ClassManager :: get_users($class['id']));
  364. echo '<li>';
  365. echo $class['name'];
  366. echo ' ('.$number_of_users.' '.get_lang('Users').')';
  367. echo '</li>';
  368. }
  369. echo '</ul>';
  370. $create_class_groups_form = new FormValidator('create_class_groups_form');
  371. $create_class_groups_form->addElement('hidden', 'action');
  372. if (api_get_setting('allow_group_categories') == 'true')
  373. {
  374. $group_categories = GroupManager :: get_categories();
  375. $cat_options = array ();
  376. foreach ($group_categories as $index => $category)
  377. {
  378. // Don't allow new groups in the virtual course category!
  379. if ($category['id'] != VIRTUAL_COURSE_CATEGORY)
  380. {
  381. $cat_options[$category['id']] = $category['title'];
  382. }
  383. }
  384. $create_class_groups_form->addElement('select', 'group_category', null, $cat_options);
  385. }
  386. else
  387. {
  388. $create_class_groups_form->addElement('hidden', 'group_category');
  389. }
  390. $create_class_groups_form->addElement('submit', 'submit', get_lang('Ok'));
  391. $defaults['group_category'] = DEFAULT_GROUP_CATEGORY;
  392. $defaults['action'] = 'create_class_groups';
  393. $create_class_groups_form->setDefaults($defaults);
  394. $create_class_groups_form->display();
  395. echo '</blockquote>';
  396. }
  397. }
  398. /*
  399. ===============================================================================
  400. DOKEOS FOOTER
  401. ===============================================================================
  402. */
  403. Display :: display_footer();
  404. ?>