241991185451a68e493a60dcd815ca159e6dac9e
[atutor.git] / mods / gradebook / update_gradebook.php
1 <?php\r
2 /************************************************************************/\r
3 /* ATutor                                                                                                                               */\r
4 /************************************************************************/\r
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/\r
6 /* Adaptive Technology Resource Centre / University of Toronto                  */\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: grade_scale_add.php 7208 2008-05-28 16:07:24Z cindy $\r
14 \r
15 $page = 'gradebook';\r
16 \r
17 define('AT_INCLUDE_PATH', '../../include/');\r
18 require_once(AT_INCLUDE_PATH.'vitals.inc.php');\r
19 authenticate(AT_PRIV_GRADEBOOK);\r
20 \r
21 require_once("lib/gradebook.inc.php");\r
22 \r
23 // Checks if the given test has students taken it more than once, if has,\r
24 // print feedback and return false, otherwise, return true.\r
25 function is_test_updatable($gradebook_test_id)\r
26 {\r
27         global $db, $msg;\r
28         \r
29         $sql = "SELECT g.id, t.title FROM ".TABLE_PREFIX."gradebook_tests g, ".TABLE_PREFIX."tests t WHERE g.id=t.test_id AND g.type='ATutor Test' AND g.gradebook_test_id = ". $gradebook_test_id;\r
30         $result = mysql_query($sql, $db) or die(mysql_error());\r
31         $row = mysql_fetch_assoc($result);\r
32         \r
33         $no_error = true;\r
34         \r
35         $studs_take_num = get_studs_take_more_than_once($_SESSION["course_id"], $row["id"]);\r
36         \r
37         foreach ($studs_take_num as $student => $num)\r
38         {\r
39                 if ($no_error) $no_error = false;\r
40                 $error_msg .= $student . ": " . $num . " times<br>";\r
41         }\r
42         \r
43         if (!$no_error)\r
44         {\r
45                 $f = array('UPDATE_GRADEBOOK',\r
46                                                 $row['title'], \r
47                                                 $error_msg);\r
48                 $msg->addFeedback($f);\r
49         }\r
50 \r
51         if ($no_error) \r
52                 return true;\r
53         else \r
54                 return false;\r
55 }\r
56 \r
57 function update_gradebook($gradebook_test_id, $member_id)\r
58 {\r
59         global $db;\r
60 \r
61         $sql = "SELECT id, grade_scale_id FROM ".TABLE_PREFIX."gradebook_tests WHERE gradebook_test_id = ". $gradebook_test_id;\r
62         $result = mysql_query($sql, $db) or die(mysql_error());\r
63         $row = mysql_fetch_assoc($result);\r
64         $test_id = $row["id"];\r
65         $grade_scale_id = $row["grade_scale_id"];\r
66         \r
67         // get grade\r
68         $grade = get_member_grade($test_id, $member_id, $grade_scale_id);\r
69         \r
70         if ($grade <> "")\r
71         {\r
72                 $sql = "REPLACE INTO ".TABLE_PREFIX."gradebook_detail(gradebook_test_id, member_id, grade) VALUES (".$gradebook_test_id.", ".$member_id.", '".$grade."')";\r
73                 $result = mysql_query($sql, $db) or die(mysql_error());\r
74         }\r
75 }\r
76 \r
77 // Initialize all applicable tests array and all enrolled students array\r
78 $tests = array();\r
79 $students = array();\r
80 \r
81 // generate gradebook test array\r
82 $sql = "SELECT *, t.title FROM ".TABLE_PREFIX."gradebook_tests g, ".TABLE_PREFIX."tests t WHERE g.id = t.test_id AND g.type='ATutor Test' AND t.course_id=".$_SESSION["course_id"];\r
83 $result = mysql_query($sql, $db) or die(mysql_error());\r
84 while ($row = mysql_fetch_assoc($result))\r
85 {\r
86         $test["gradebook_test_id"] =  $row["gradebook_test_id"];\r
87         $test["title"] =  $row["title"];\r
88         \r
89         array_push($tests, $test);\r
90 }\r
91 \r
92 // generate students array\r
93 $sql = "SELECT m.first_name, m.last_name, e.member_id FROM ".TABLE_PREFIX."members m, ".TABLE_PREFIX."course_enrollment e WHERE m.member_id = e.member_id AND e.course_id=".$_SESSION["course_id"]." AND e.approved='y' AND e.role<>'Instructor' ORDER BY m.first_name,m.last_name";\r
94 $result = mysql_query($sql, $db) or die(mysql_error());\r
95 \r
96 while ($row = mysql_fetch_assoc($result))\r
97 {\r
98         $student["first_name"] = $row["first_name"];\r
99         $student["last_name"] = $row["last_name"];\r
100         $student["member_id"] = $row["member_id"];\r
101         \r
102         array_push($students, $student);\r
103 }\r
104 // end of initialization\r
105 \r
106 if (isset($_POST['cancel'])) \r
107 {\r
108         $msg->addFeedback('CANCELLED');\r
109         header('Location: gradebook_tests.php');\r
110         exit;\r
111\r
112 else if (isset($_POST['update'])) \r
113 {\r
114         if (!$msg->containsErrors()) \r
115         {\r
116                 if ($_POST["gradebook_test_id"] == 0)\r
117                 {\r
118                         foreach($tests as $test)\r
119                         {\r
120                                 if (is_test_updatable($test["gradebook_test_id"]))\r
121                                 {\r
122                                         if ($_POST["member_id"]==0)\r
123                                         {\r
124                                                 // delete old data for this test\r
125                                                 $sql = "DELETE from ".TABLE_PREFIX."gradebook_detail WHERE gradebook_test_id = ".$test["gradebook_test_id"];\r
126                                                 $result = mysql_query($sql, $db) or die(mysql_error());\r
127                                                 \r
128                                                 foreach($students as $student)\r
129                                                         update_gradebook($test["gradebook_test_id"], $student["member_id"]);\r
130                                         }\r
131                                         else\r
132                                                 update_gradebook($test["gradebook_test_id"], $_POST["member_id"]);\r
133                                 }\r
134                         }\r
135                 }\r
136                 else\r
137                 {\r
138                         if (is_test_updatable($_POST["gradebook_test_id"]))\r
139                         {\r
140                                 if ($_POST["member_id"]==0)\r
141                                 {\r
142                                         // delete old data for this test\r
143                                         $sql = "DELETE from ".TABLE_PREFIX."gradebook_detail WHERE gradebook_test_id = ".$_POST["gradebook_test_id"];\r
144                                         $result = mysql_query($sql, $db) or die(mysql_error());\r
145                                         \r
146                                         foreach($students as $student)\r
147                                                 update_gradebook($_POST["gradebook_test_id"], $student["member_id"]);\r
148                                 }\r
149                                 else\r
150                                         update_gradebook($_POST["gradebook_test_id"], $_POST["member_id"]);\r
151                         }\r
152                 }\r
153                 \r
154                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');\r
155         }\r
156\r
157 \r
158 require(AT_INCLUDE_PATH.'header.inc.php');\r
159 \r
160 ?>\r
161 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">\r
162 <div class="input-form">\r
163         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('update_gradebook'); ?></legend>\r
164 \r
165 <?php\r
166 if (count($tests) == 0)\r
167 {\r
168 ?>\r
169         <div class="row">\r
170                 <strong><?php echo _AT('none_found'); ?></strong>\r
171         </div>\r
172 <?php \r
173 }\r
174 else\r
175 {\r
176         // list of tests\r
177         echo '  <div class="row">'."\n\r";\r
178         echo '          <label for="select_tid1">'. _AT("tests") .'</label><br />'."\n\r";\r
179         echo '          <select name="gradebook_test_id" id="select_tid1">'."\n\r";\r
180         echo '                  <option value="0">'. _AT('all') .'</option>'."\n\r";\r
181 \r
182         foreach($tests as $test)\r
183         {\r
184                 echo '                  <option value="'.$test[gradebook_test_id].'">'.$test[title].'</option>'."\n\r";\r
185         }\r
186         echo '          </select>'."\n\r";\r
187         echo '  </div>'."\n\r";\r
188 \r
189         // list of students\r
190         echo '  <div class="row">'."\n\r";\r
191         echo '          <label for="select_sid">'. _AT("students") .'</label><br />'."\n\r";\r
192         echo '          <select name="member_id" id="select_sid">'."\n\r";\r
193         echo '                  <option value="0">'. _AT('all') .'</option>'."\n\r";\r
194 \r
195         foreach($students as $student)\r
196         {\r
197                 echo '                  <option value="'.$student[member_id].'">'.$student[first_name].' '.$student[last_name].'</option>'."\n\r";\r
198         }\r
199         echo '          </select>'."\n\r";\r
200         echo '  </div>'."\n\r";\r
201 ?>\r
202 \r
203         <div class="row buttons">\r
204                 <input type="submit" name="update" value="<?php echo _AT('update'); ?>" />\r
205                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />\r
206         </div>\r
207 <?php\r
208 }\r
209 ?>\r
210         </fieldset>\r
211 \r
212 </div>\r
213 </form>\r
214 \r
215 <form name="form1" method="post" action="mods/gradebook/verify_tests.php">\r
216 <div class="input-form">\r
217         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('combine_tests'); ?></legend>\r
218         <div class="row">\r
219                 <p><?php echo _AT('combine_tests_info'); ?></p>\r
220         </div>\r
221 \r
222 <?php\r
223 if (count($tests) == 0)\r
224 {\r
225 ?>\r
226         <div class="row">\r
227                 <strong><?php echo _AT('none_found'); ?></strong>\r
228         </div>\r
229 <?php \r
230 }\r
231 else\r
232 {\r
233         // list of tests\r
234         echo '  <div class="row">'."\n\r";\r
235         echo '          <label for="select_tid2">'. _AT("tests") .' '. _AT("combine_into").'</label><br />'."\n\r";\r
236         echo '          <select name="gradebook_test_id" id="select_tid2">'."\n\r";\r
237 \r
238         foreach($tests as $test)\r
239                 echo '                  <option value="'.$test[gradebook_test_id].'">'.$test[title].'</option>'."\n\r";\r
240 \r
241         echo '          </select>'."\n\r";\r
242         echo '  </div>'."\n\r";\r
243 \r
244         // list of atutor tests that can be combined. \r
245         // These tests can only be taken once and are not in gradebook yet\r
246         // note: surveys are excluded by checking if question weights are defined\r
247         $sql_at = "SELECT * FROM ".TABLE_PREFIX."tests t".\r
248                                         " WHERE course_id=".$_SESSION["course_id"].\r
249                                         " AND num_takes = 1".\r
250                                         " AND NOT EXISTS (SELECT 1".\r
251                                                                                                         " FROM ".TABLE_PREFIX."gradebook_tests g".\r
252                                                                                                         " WHERE g.id = t.test_id".\r
253                                                                                                         " AND g.type='ATutor Test')".\r
254                                 " AND test_id IN (SELECT test_id FROM ".TABLE_PREFIX."tests_questions_assoc ".\r
255                                                                 " WHERE weight > 0 ".\r
256                                                                 " GROUP BY test_id ".\r
257                                                                 " HAVING count(*) > 1) ".\r
258                                         " ORDER BY title";\r
259         $result_at = mysql_query($sql_at, $db) or die(mysql_error());\r
260         \r
261         if (mysql_num_rows($result_at) == 0)\r
262                  echo _AT('none_found');\r
263         else\r
264         {\r
265                 echo '  <div class="row">'."\n\r";\r
266                 echo '          <label for="select_tid3">'. _AT("tests") .' '. _AT("combine_from").'</label><br />'."\n\r";\r
267                 echo '          <select name="test_id" id="select_tid3">'."\n\r";\r
268                 \r
269                 if (mysql_num_rows($result_at) > 0)\r
270                 {\r
271                         while ($row_at = mysql_fetch_assoc($result_at))\r
272                         {\r
273                                 echo '                  <option value="'.$row_at[test_id].'">'.$row_at[title].'</option>'."\n\r";\r
274                         }\r
275                 }\r
276         \r
277                 echo '          </select>'."\n\r";\r
278                 echo '  </div>'."\n\r";\r
279         }\r
280 ?>\r
281 \r
282         <div class="row buttons">\r
283                 <input type="submit" name="combine" value="<?php echo _AT('combine'); ?>" />\r
284                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />\r
285         </div>\r
286 <?php\r
287 }\r
288 ?>\r
289         </fieldset>\r
290 \r
291 </div>\r
292 </form>\r
293 \r
294 <?php require (AT_INCLUDE_PATH.'footer.inc.php');  ?>\r