group_creation.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 (!is_allowed_to_edit())
  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. $defaults['group_'.$group_number.'_name'] = get_lang('GroupSingle').' '.$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('style_submit_button', 'submit', get_lang('CreateGroup'), 'class="save"');
  250. $form->display();
  251. }
  252. }
  253. else
  254. {
  255. /*
  256. * Show form to generate new groups
  257. */
  258. $categories = GroupManager :: get_categories();
  259. //echo '<blockquote>';
  260. if (count($categories) > 1 || isset ($categories[0]) && $categories[0]['id'] != VIRTUAL_COURSE_CATEGORY)
  261. {
  262. $create_groups_form = new FormValidator('create_groups');
  263. $create_groups_form->addElement('header', '', $nameTools);
  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('style_submit_button', 'submit', get_lang('ProceedToCreateGroup'), 'class="save"');
  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 (api_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 (api_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. ?>