ATutor 2.0
[atutor.git] / mods / _core / groups / create_automatic.php
1 <?php\r
2 /************************************************************************/\r
3 /* ATutor                                                                                                                               */\r
4 /************************************************************************/\r
5 /* Copyright (c) 2002-2010                                              */\r
6 /* Inclusive Design Institute                                           */\r
7 /* http://atutor.ca                                                                                                             */\r
8 /*                                                                                                                                              */\r
9 /* This program is free software. You can redistribute it and/or        */\r
10 /* modify it under the terms of the GNU General Public License          */\r
11 /* as published by the Free Software Foundation.                        */\r
12 /************************************************************************/\r
13 // $Id: create_automatic.php 7482 2008-05-06 17:44:49Z greg $\r
14 \r
15 define('AT_INCLUDE_PATH', '../../../include/');\r
16 require(AT_INCLUDE_PATH.'vitals.inc.php');\r
17 authenticate(AT_PRIV_GROUPS);\r
18 \r
19 if (isset($_POST['cancel'])) {\r
20         $msg->addFeedback('CANCELLED');\r
21         header('Location: index.php');\r
22         exit;\r
23 } else if (isset($_POST['submit'])) {\r
24         $modules = '';\r
25         if (isset($_POST['modules'])) {\r
26                 $modules = implode('|', $_POST['modules']);\r
27         }\r
28 \r
29         $_POST['type_title']   = trim($_POST['type_title']);\r
30         $_POST['num_students'] = abs($_POST['num_students']);\r
31         $_POST['num_groups']   = abs($_POST['num_groups']);\r
32         $_POST['num_g']        = intval($_POST['num_g']);\r
33 \r
34         $missing_fields = array();\r
35 \r
36         if (!$_POST['type_title']) {\r
37                 $missing_fields[] = _AT('groups_type');\r
38         }\r
39 \r
40         if (!$_POST['prefix']) {\r
41                 $missing_fields[] = _AT('group_prefix');\r
42         }\r
43 \r
44         $course_owner = $system_courses[$_SESSION['course_id']]['member_id'];\r
45         if (isset($_POST['fill'])) {\r
46                 $sql = "SELECT member_id FROM ".TABLE_PREFIX."course_enrollment WHERE course_id=$_SESSION[course_id] AND approved='y' AND `privileges`&".AT_PRIV_GROUPS."=0 AND member_id<>$course_owner";\r
47                 $result = mysql_query($sql, $db);\r
48                 $total_students = mysql_num_rows($result);\r
49                 $students = array();\r
50                 while ($row = mysql_fetch_assoc($result)) {\r
51                         $students[] = $row['member_id'];\r
52                 }\r
53                 shuffle($students);\r
54         } else {\r
55                 $sql = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."course_enrollment WHERE course_id=$_SESSION[course_id] AND approved='y' AND `privileges`&".AT_PRIV_GROUPS."=0 AND member_id<>$course_owner";\r
56                 $result = mysql_query($sql, $db);\r
57                 $row = mysql_fetch_assoc($result);\r
58 \r
59                 $total_students = $row['cnt']; // 4 students in the course\r
60         }\r
61 \r
62         if ($_POST['num_g'] == 1) { // number of students per group\r
63                 $num_students_per_group = $_POST['num_students'];\r
64 \r
65                 if ($num_students_per_group == 0) {\r
66                         $missing_fields[] = _AT('number_of_students_per_group');\r
67                 } else {\r
68                         if ($total_students == 0) {\r
69                                 $msg->addError('GROUP_NO_STUDENTS');\r
70                         } else {\r
71                                 $num_groups = ceil($total_students / $num_students_per_group);\r
72                         }\r
73                 }\r
74         } else { // number of groups\r
75                 $num_groups = $_POST['num_groups'];\r
76 \r
77                 if ($num_groups == 0) {\r
78                         $missing_fields[] = _AT('number_of_groups');\r
79                 } else {\r
80                         if ($total_students > 0) {\r
81                                 // to uniformly distribute all the groups we place the remaining students\r
82                                 // into the first n groups, where n is the number of remaining students.\r
83                                 $remainder = $total_students % $num_groups;\r
84                                 if ($remainder) {\r
85                                         $num_students_per_group = floor($total_students / $num_groups);\r
86                                 } else {\r
87                                         $num_students_per_group = $total_students / $num_groups;\r
88                                 }\r
89                         } else {\r
90                                 $num_students_per_group = 0;\r
91                         }\r
92                 }\r
93         }\r
94 \r
95         if ($missing_fields) {\r
96                 $missing_fields = implode(', ', $missing_fields);\r
97                 $msg->addError(array('EMPTY_FIELDS', $missing_fields));\r
98         }\r
99 \r
100         if (!$msg->containsErrors()) {\r
101                 $_POST['type_title']  = $addslashes($_POST['type_title']);\r
102                 $_POST['prefix']      = $addslashes($_POST['prefix']);\r
103                 $_POST['description'] = $addslashes($_POST['description']);\r
104 \r
105                 $sql = "INSERT INTO ".TABLE_PREFIX."groups_types VALUES (NULL, $_SESSION[course_id], '$_POST[type_title]')";\r
106                 $result = mysql_query($sql, $db);\r
107                 $group_type_id = mysql_insert_id($db);\r
108 \r
109                 $start_index = 0;\r
110 \r
111                 for($i=0; $i<$num_groups; $i++) {\r
112                         $group_title = $_POST['prefix'] . ' ' . ($i + 1);\r
113                         $sql = "INSERT INTO ".TABLE_PREFIX."groups VALUES (NULL, $group_type_id, '$group_title', '$_POST[description]', '$modules')";\r
114                         $result = mysql_query($sql, $db);\r
115 \r
116                         $group_id = mysql_insert_id($db);\r
117                         $_SESSION['groups'][$group_id] = $group_id;\r
118 \r
119                         // call module init scripts:\r
120                         if (isset($_POST['modules'])) {\r
121                                 foreach ($_POST['modules'] as $mod) {\r
122                                         $module =& $moduleFactory->getModule($mod);\r
123                                         $module->createGroup($group_id);\r
124                                 }\r
125                         }\r
126 \r
127                         if (isset($_POST['fill'])) {\r
128                                 // put students in this group\r
129                                 for ($j = $start_index; $j < min(($start_index + $num_students_per_group), $total_students); $j++) {\r
130                                         $sql = "INSERT INTO ".TABLE_PREFIX."groups_members VALUES ($group_id, $students[$j])";\r
131                                         mysql_query($sql, $db);\r
132                                 }\r
133 \r
134                                 $start_index = $j;\r
135                                 if ($remainder) {\r
136                                         $sql = "INSERT INTO ".TABLE_PREFIX."groups_members VALUES ($group_id, $students[$start_index])";\r
137                                         mysql_query($sql, $db);\r
138                                         $start_index++;\r
139                                         $remainder--;\r
140                                 }\r
141                         }\r
142                 }\r
143 \r
144                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');\r
145 \r
146                 header('Location: index.php');\r
147                 exit;\r
148         } else {\r
149                 $_POST['type_title']  = $stripslashes($_POST['type_title']);\r
150                 $_POST['prefix']      = $stripslashes($_POST['prefix']);\r
151                 $_POST['description'] = $stripslashes($_POST['description']);\r
152         }\r
153 }\r
154 \r
155 require(AT_INCLUDE_PATH.'header.inc.php');\r
156 ?>\r
157 \r
158 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="form">\r
159         <div class="input-form">\r
160         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('groups_create_automatic'); ?></legend>\r
161                 <div class="row">\r
162                         <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="type"><?php echo _AT('groups_type'); ?></label><br />\r
163                         <input type="text" name="type_title" id="type" value="<?php echo htmlentities_utf8($_POST['type_title']); ?>" size="30" maxlength="60" />\r
164                 </div>\r
165 \r
166                 <div class="row">\r
167                         <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="prefix"><?php echo _AT('group_prefix'); ?></label><br />\r
168                         <input type="text" name="prefix" id="prefix" value="<?php echo htmlentities_utf8($_POST['prefix']); ?>" size="20" maxlength="40" />\r
169                 </div>\r
170 \r
171                 <div class="row">\r
172                         <label for="description"><?php echo _AT('default_description'); ?></label><br />\r
173                         <textarea name="description" id="description" cols="10" rows="2"><?php echo htmlentities_utf8($_POST['description']); ?></textarea>\r
174                 </div>\r
175 \r
176                 <div class="row">\r
177                         <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><?php echo _AT('number_of_groups'); ?><br />\r
178                         <?php\r
179                                 $sql = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."course_enrollment WHERE course_id=$_SESSION[course_id] AND approved='y' AND `privileges`&".AT_PRIV_GROUPS."=0";\r
180                                 $result = mysql_query($sql, $db);\r
181                                 $row = mysql_fetch_assoc($result);\r
182                         ?>\r
183                         <p><?php echo _AT('num_students_currently_enrolled', $row['cnt']-1); ?></p>\r
184 \r
185                         <input type="radio" name="num_g" value="1" id="num1" checked="checked" onclick="javascript:changer('num_groups', 'num_students');" /><label for="num1"><?php echo _AT('number_of_students_per_group'); ?></label> <input type="text" name="num_students" size="3" style="text-align: right" maxlength="4" />\r
186                         <br />\r
187                         <input type="radio" name="num_g" value="2" id="num2" onclick="javascript:changer('num_students', 'num_groups');" /><label for="num2"><?php echo _AT('number_of_groups'); ?></label> <input type="text" name="num_groups" size="3" style="text-align: right" maxlength="4" value="-" />\r
188                 </div>\r
189 \r
190                 <div class="row">\r
191                         <?php echo _AT('fill_groups'); ?><br />\r
192                         <input type="checkbox" name="fill" value="1" id="fill_r" checked="checked" /><label for="fill_r"><?php echo _AT('fill_groups_randomly'); ?></label>\r
193                 </div>\r
194 \r
195                 <div class="row">\r
196                         <?php echo _AT('tools'); ?><br />\r
197                                 <?php\r
198                                 $modules = $moduleFactory->getModules(AT_MODULE_STATUS_ENABLED, 0, TRUE);\r
199                                 $keys = array_keys($modules);\r
200                                 $i=0;\r
201                                 ?>\r
202                                 <?php foreach($keys as $module_name): ?>\r
203                                         <?php $module =& $modules[$module_name]; ?>\r
204                                         <?php if ($module->getGroupTool() && (in_array($module->getGroupTool(),$_pages[AT_NAV_HOME]) || in_array($module->getGroupTool(),$_pages[AT_NAV_COURSE])) ): ?>\r
205                                                 <input type="checkbox" value="<?php echo $module_name; ?>" name="modules[]" id="m<?php echo ++$i; ?>" /><label for="m<?php echo $i; ?>"><?php echo $module->getName(); ?></label><br />\r
206                                         <?php endif; ?>\r
207                                 <?php endforeach; ?>\r
208                 </div>\r
209 \r
210                 <div class="row buttons">\r
211                         <input type="submit" name="submit" value="<?php echo _AT('create'); ?>" accesskey="s" />\r
212                         <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />\r
213                 </div>\r
214                 </fieldset>\r
215         </div>\r
216 </form>\r
217 <script type="text/javascript">\r
218 // <!--\r
219 document.form.num_groups.disabled = true;\r
220 function changer(name1, name2) {\r
221         document.form[name1].value= '-';\r
222         document.form[name1].disabled = true;\r
223         document.form[name2].disabled = false;\r
224 \r
225         document.form[name2].value= '';\r
226         document.form[name2].focus();\r
227 }\r
228 // -->\r
229 </script>\r
230 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>