made a copy
[atutor.git] / tools / tests / results.php
1 <?php
2 /****************************************************************************/
3 /* ATutor                                                                                                                                       */
4 /****************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton        */
6 /* Adaptive Technology Resource Centre / University of Toronto                          */
7 /* http://atutor.ca                                                                                                                     */
8 /*                                                                                                                                                      */
9 /* This program is free software. You can redistribute it and/or                        */
10 /* modify it under the terms of the GNU General Public License                          */
11 /* as published by the Free Software Foundation.                                                        */
12 /****************************************************************************/
13 // $Id$
14 define('AT_INCLUDE_PATH', '../../include/');
15 require(AT_INCLUDE_PATH.'vitals.inc.php');
16 authenticate(AT_PRIV_TESTS);
17
18 $tid = intval($_REQUEST['tid']);
19
20 if (isset($_GET['delete'], $_GET['id'])) {
21         $ids = implode(',', $_GET['id']);
22         header('Location:delete_result.php?tid='.$tid.SEP.'rid='.$ids);
23         exit;
24 } else if (isset($_GET['edit'], $_GET['id'])) {
25         if (count($_GET['id']) > 1) {
26                 $msg->addError('SELECT_ONE_ITEM');
27         } else {
28                 header('Location:view_results.php?tid='.$tid.SEP.'rid='.$_GET['id'][0]);
29                 exit;
30         }
31 } else if ((isset($_GET['edit']) || isset($_GET['delete'])) && !$_GET['id'] && !$_GET['asc'] && !$_GET['desc'] && !$_GET['filter'] && !$_GET['reset_filter']) {
32         $msg->addError('NO_ITEM_SELECTED');
33 }
34
35 require(AT_INCLUDE_PATH.'lib/test_result_functions.inc.php');
36
37 if ($_GET['reset_filter']) {
38         unset($_GET);
39 }
40
41 $orders = array('asc' => 'desc', 'desc' => 'asc');
42 $cols   = array('login' => 1, 'full_name' => 1, 'date_taken' => 1, 'fs' => 1, 'time_spent' => 1);
43
44 if (isset($_GET['asc'])) {
45         $order = 'asc';
46         $col   = isset($cols[$_GET['asc']]) ? $_GET['asc'] : 'login';
47 } else if (isset($_GET['desc'])) {
48         $order = 'desc';
49         $col   = isset($cols[$_GET['desc']]) ? $_GET['desc'] : 'login';
50 } else {
51         // no order set
52         $order = 'asc';
53         $col   = 'login';
54 }
55
56 require(AT_INCLUDE_PATH.'header.inc.php');
57
58 if (isset($_GET['status']) && ($_GET['status'] != '') && ($_GET['status'] != 2)) {
59         if ($_GET['status'] == 0) {
60                 $status = " AND R.final_score=''";
61         } else {
62                 $status = " AND R.final_score<>''";
63         }
64         $page_string .= SEP.'status='.$_GET['status'];
65 } else {
66         $status = '';
67 }
68
69 if ($_GET['user_type'] == 1 || $_GET['user_type'] == 2) {
70         if ($_GET['user_type'] == 1) {
71                 $status = " AND R.member_id not like 'G_%' AND R.member_id > 0 ";
72         } else {
73                 $status = " AND (R.member_id like 'G_%' OR R.member_id = 0) ";
74         }
75         $page_string .= SEP.'user_type='.$_GET['user_type'];
76 }
77
78 //get test info
79 $sql    = "SELECT out_of, anonymous, random, title FROM ".TABLE_PREFIX."tests WHERE test_id=$tid AND course_id=$_SESSION[course_id]";
80 $result = mysql_query($sql, $db);
81 if (!($row = mysql_fetch_array($result))){
82         $msg->printErrors('ITEM_NOT_FOUND');
83         require (AT_INCLUDE_PATH.'footer.inc.php');
84         exit;
85 }
86 $out_of = $row['out_of'];
87 $anonymous = $row['anonymous'];
88 $random = $row['random'];
89 $title = $row['title'];
90
91 //count total
92 $sql    = "SELECT count(*) as cnt FROM ".TABLE_PREFIX."tests_results R LEFT JOIN ".TABLE_PREFIX."members M USING (member_id) WHERE R.test_id=$tid AND R.status=1";
93 $result = mysql_query($sql, $db);
94 $row    = mysql_fetch_array($result);
95 $num_sub = $row['cnt'];
96
97 //get results based on filtre and sorting
98 if ($anonymous == 1) {
99         // Keep login, full_name and fs fields even if not used: ORDER BY relies upon them
100         $sql    = "SELECT R.*, (UNIX_TIMESTAMP(R.end_time) - UNIX_TIMESTAMP(R.date_taken)) AS time_spent, '' AS login, '' AS full_name, R.final_score+0.0 AS fs FROM ".TABLE_PREFIX."tests_results R WHERE R.test_id=$tid AND R.status=1 $status ORDER BY $col $order";
101 } else {        
102 //      $sql    = "SELECT R.*, M.login, (UNIX_TIMESTAMP(R.end_time) - UNIX_TIMESTAMP(R.date_taken)) AS time_spent, CONCAT(M.first_name, ' ', M.second_name, ' ', M.last_name) AS full_name, R.final_score+0.0 AS fs FROM ".TABLE_PREFIX."tests_results R LEFT JOIN  ".TABLE_PREFIX."members M USING (member_id) WHERE R.test_id=$tid AND R.status=1 $status ORDER BY $col $order, R.final_score $order";
103         //added by Indirect
104         $sql    = "SELECT R.*, login, (UNIX_TIMESTAMP(R.end_time) - UNIX_TIMESTAMP(R.date_taken)) AS time_spent, R.final_score+0.0 AS fs FROM ".TABLE_PREFIX."tests_results R LEFT JOIN  ".TABLE_PREFIX."members M USING (member_id) WHERE R.test_id=$tid AND R.status=1 $status ORDER BY $col $order, R.final_score $order";
105 }
106
107 $result = mysql_query($sql, $db);
108 if ($anonymous == 1) {
109         $guest_text = '<em>'._AT('anonymous').'</em>';
110 } else {
111         $guest_text = '- '._AT('guest').' -';
112 }
113 while ($row = mysql_fetch_assoc($result)) {
114         $row['full_name'] = $row['full_name'] ? $row['full_name'] : $guest_text;
115         $row['login']     = $row['login']     ? $row['login']     : $guest_text;
116         $rows[$row['result_id']] = $row;
117 }
118
119 $num_results = mysql_num_rows($result);
120
121 //count unmarked: no need to do this query if filtre is already getting unmarked
122 if (isset($_GET['status']) && ($_GET['status'] != '') && ($_GET['status'] == 0)) {
123         $num_unmarked = $num_results;
124 } else {
125         $sql            = "SELECT count(*) as cnt FROM ".TABLE_PREFIX."tests_results R, ".TABLE_PREFIX."members M WHERE R.test_id=$tid AND R.status=1 AND R.member_id=M.member_id AND R.final_score=''";
126         $result = mysql_query($sql, $db);
127         $row = mysql_fetch_array($result);
128         $num_unmarked = $row['cnt'];
129 }
130
131 ?>
132 <!--h3><?php //echo AT_print($row['title'], 'tests.title'); ?></h3><br / -->
133 <h3><?php echo AT_print($title, 'tests.title'); ?></h3><br />
134 <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
135         <input type="hidden" name="tid" value="<?php echo $tid; ?>" />
136
137         <div class="input-form">
138         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('filter'); ?></legend>
139                 <div class="row">
140                         <h3><?php echo _AT('results_found', $num_results); ?></h3>
141                 </div>
142
143                 <div class="row">
144                         <?php echo _AT('status'); ?><br />
145                         <input type="radio" name="status" value="1" id="s0" <?php if ($_GET['status'] == 1) { echo 'checked="checked"'; } ?> /><label for="s0"><?php echo _AT('marked_label', $num_sub - $num_unmarked); ?></label> 
146                         <input type="radio" name="status" value="0" id="s1" <?php if ($_GET['status'] == 0) { echo 'checked="checked"'; } ?> /><label for="s1"><?php echo _AT('unmarked_label', $num_unmarked); ?></label> 
147                         <input type="radio" name="status" value="2" id="s2" <?php if (!isset($_GET['status']) || ($_GET['status'] != 0 && $_GET['status'] != 1)) { echo 'checked="checked"'; } ?> /><label for="s2"><?php echo _AT('all_label', $num_sub); ?></label> 
148
149                 </div>
150
151                 <div class="row">
152                         <?php echo _AT('user_type'); ?><br />
153                         <input type="radio" name="user_type" value="1" id="u0" <?php if ($_GET['user_type'] == 1) { echo 'checked="checked"'; } ?> /><label for="u0"><?php echo _AT('registered_members'); ?></label> 
154                         <input type="radio" name="user_type" value="2" id="u1" <?php if ($_GET['user_type'] == 2) { echo 'checked="checked"'; } ?> /><label for="u1"><?php echo _AT('guests'); ?></label> 
155                         <input type="radio" name="user_type" value="0" id="u2" <?php if (!isset($_GET['user_type']) || ($_GET['user_type'] != 1 && $_GET['user_type'] != 2)) { echo 'checked="checked"'; } ?> /><label for="u2"><?php echo _AT('all'); ?></label> 
156                 </div>
157
158                 <div class="row buttons">
159                         <input type="submit" name="filter" value="<?php echo _AT('filter'); ?>" />
160                         <input type="submit" name="reset_filter" value="<?php echo _AT('reset_filter'); ?>" />
161                 </div>
162                 </fieldset>
163         </div>
164 </form>
165
166 <form name="form" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
167 <input type="hidden" name="tid" value="<?php echo $tid; ?>" />
168
169 <table class="data" summary="" rules="cols">
170 <colgroup>
171         <?php if ($col == 'login'): ?>
172                 <col />
173                 <col class="sort" />
174                 <col span="4" />
175         <?php elseif ($col == 'full_name'): ?>
176                 <col span="2" />
177                 <col class="sort" />
178                 <col span="3" />
179         <?php elseif($col == 'date_taken'): ?>
180                 <col span="3" />
181                 <col class="sort" />
182                 <col span="2" />
183         <?php elseif($col == 'time_spent'): ?>
184                 <col span="4" />
185                 <col class="sort" />
186                 <col span="1" />
187         <?php elseif($col == 'fs'): ?>
188                 <col span="5" />
189                 <col class="sort" />
190         <?php endif; ?>
191 </colgroup>
192 <thead>
193 <tr>
194         <th scope="col" align="left"><input type="checkbox" value="<?php echo _AT('select_all'); ?>" id="all" title="<?php echo _AT('select_all'); ?>" name="selectall" onclick="CheckAll();" /></th>
195         <th scope="col"><a href="tools/tests/results.php?tid=<?php echo $tid.$page_string.SEP.$orders[$order]; ?>=login"><?php echo _AT('login_name'); ?></a></th>
196         <th scope="col"><a href="tools/tests/results.php?tid=<?php echo $tid.$page_string.SEP.$orders[$order]; ?>=full_name"><?php echo _AT('full_name'); ?></a></th>
197         <th scope="col"><a href="tools/tests/results.php?tid=<?php echo $tid.$page_string.SEP.$orders[$order]; ?>=date_taken"><?php echo _AT('date_taken'); ?></a></th>
198         <th scope="col"><a href="tools/tests/results.php?tid=<?php echo $tid.$page_string.SEP.$orders[$order]; ?>=time_spent"><?php echo _AT('time_spent'); ?></a></th>
199         <th scope="col"><a href="tools/tests/results.php?tid=<?php echo $tid.$page_string.SEP.$orders[$order]; ?>=fs"><?php echo _AT('mark'); ?></a></th>
200 </tr>
201 </thead>
202 <tfoot>
203 <tr>
204         <td colspan="6"><input type="submit" name="edit" value="<?php echo _AT('view_mark_test'); ?>" /> <input type="submit" name="delete" value="<?php echo _AT('delete'); ?>" /></td>
205 </tr>
206 </tfoot>
207 <tbody>
208 <?php if ($rows): ?>
209         <?php foreach ($rows as $row): ?>
210                 <tr onmousedown="document.form['r<?php echo $row['result_id']; ?>'].checked = !document.form['r<?php echo $row['result_id']; ?>'].checked; togglerowhighlight(this, 'r<?php echo $row['result_id']; ?>');" id="rr<?php echo $row['result_id']; ?>">
211                         <td><input type="checkbox" name="id[]" value="<?php echo $row['result_id']; ?>" id="r<?php echo $row['result_id']; ?>" onmouseup="this.checked=!this.checked" /></td>
212                         <td><?php echo $row['login']; ?></td>
213                         <td><?php 
214                                 if ($anonymous == 0 && $row['member_id']){
215                                         echo AT_print(get_display_name($row['member_id']), 'members.full_name'); /*$row['full_name'] */ 
216                                 } else {
217                                         echo $guest_text; // no need in AT_print(): $guest_text is a trusted _AT() output
218                                 }
219                                 ?></td>
220                         <td><?php $startend_date_format=_AT('startend_date_format'); echo AT_date( $startend_date_format, $row['date_taken'], AT_DATE_MYSQL_DATETIME); ?></td>
221                         <td><?php echo get_human_time($row['time_spent']); ?></td>
222
223                         <td align="center">
224                                 <?php if ($out_of) {
225                                         if ($random) {
226                                                 $out_of = get_random_outof($tid, $row['result_id']);
227                                         }
228
229                                         if ($row['final_score'] != '') { 
230                                                 echo $row['final_score'].'/'.$out_of;
231                                         } else {
232                                                 echo _AT('unmarked');
233                                         }
234                                 } else {
235                                         echo _AT('na');
236                                 }
237                                 ?>
238                         </td>
239                 </tr>
240         <?php endforeach; ?>
241 <?php else: ?>
242         <tr>
243                 <td colspan="6"><?php echo _AT('none_found'); ?></td>
244         </tr>
245 <?php endif; ?>
246 </tbody>
247 </table>
248 </form>
249 <script language="JavaScript" type="text/javascript">
250 //<!--
251 function CheckAll() {
252         for (var i=0;i<document.form.elements.length;i++)       {
253                 var e = document.form.elements[i];
254                 if ((e.name == 'id[]') && (e.type=='checkbox')) {
255                         e.checked = document.form.selectall.checked;
256                         togglerowhighlight(document.getElementById("r" + e.id), e.id);
257                 }
258         }
259 }
260
261 function togglerowhighlight(obj, boxid) {
262         if (document.getElementById(boxid).checked) {
263                 obj.className = 'selected';
264         } else {
265                 obj.className = '';
266         }
267 }
268 //-->
269 </script>
270 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>