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