remove old readme
[atutor.git] / mods / _standard / tests / results.php
1 <?php
2 /****************************************************************************/
3 /* ATutor                                                                                                                                       */
4 /****************************************************************************/
5 /* Copyright (c) 2002-2010                                                  */
6 /* Inclusive Design Institute                                               */
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 function sortByFullName($cell1, $cell2)
19 {
20         global $order;
21         
22         if ($order == 'asc') return (strcmp($cell1['full_name'], $cell2['full_name']) > 0) ? 1 : -1;
23         else return (strcmp($cell1['full_name'], $cell2['full_name']) < 0) ? 1 : -1;
24 }
25
26 $tid = intval($_REQUEST['tid']);
27
28 if (isset($_GET['delete'], $_GET['id'])) {
29         $ids = implode(',', $_GET['id']);
30         header('Location:delete_result.php?tid='.$tid.SEP.'rid='.$ids);
31         exit;
32 } else if (isset($_GET['edit'], $_GET['id'])) {
33         if (count($_GET['id']) > 1) {
34                 $msg->addError('SELECT_ONE_ITEM');
35         } else {
36                 header('Location:view_results_manage.php?tid='.$tid.SEP.'rid='.$_GET['id'][0]);
37                 exit;
38         }
39 } else if ((isset($_GET['edit']) || isset($_GET['delete'])) && !$_GET['id'] && !$_GET['asc'] && !$_GET['desc'] && !$_GET['filter'] && !$_GET['reset_filter']) {
40         $msg->addError('NO_ITEM_SELECTED');
41 }
42
43 require(AT_INCLUDE_PATH.'../mods/_standard/tests/lib/test_result_functions.inc.php');
44
45 if ($_GET['reset_filter']) {
46         unset($_GET);
47 }
48
49 $orders = array('asc' => 'desc', 'desc' => 'asc');
50 $cols   = array('login' => 1, 'full_name' => 1, 'date_taken' => 1, 'fs' => 1, 'time_spent' => 1);
51
52 if (isset($_GET['asc'])) {
53         $order = 'asc';
54         $col   = isset($cols[$_GET['asc']]) ? $_GET['asc'] : 'login';
55 } else if (isset($_GET['desc'])) {
56         $order = 'desc';
57         $col   = isset($cols[$_GET['desc']]) ? $_GET['desc'] : 'login';
58 } else {
59         // no order set
60         $order = 'asc';
61         $col   = 'login';
62 }
63
64 require(AT_INCLUDE_PATH.'header.inc.php');
65
66 if (isset($_GET['status']) && ($_GET['status'] != '') && ($_GET['status'] != 2)) {
67         if ($_GET['status'] == 0) {
68                 $status = " AND R.final_score=''";
69         } else {
70                 $status = " AND R.final_score<>''";
71         }
72         $page_string .= SEP.'status='.$_GET['status'];
73 } else {
74         $status = '';
75 }
76
77 if ($_GET['user_type'] == 1 || $_GET['user_type'] == 2) {
78         if ($_GET['user_type'] == 1) {
79                 $status = " AND R.member_id not like 'G_%' AND R.member_id > 0 ";
80         } else {
81                 $status = " AND (R.member_id like 'G_%' OR R.member_id = 0) ";
82         }
83         $page_string .= SEP.'user_type='.$_GET['user_type'];
84 }
85
86 //get test info
87 $sql    = "SELECT out_of, anonymous, random, title FROM ".TABLE_PREFIX."tests WHERE test_id=$tid AND course_id=$_SESSION[course_id]";
88 $result = mysql_query($sql, $db);
89 if (!($row = mysql_fetch_array($result))){
90         $msg->printErrors('ITEM_NOT_FOUND');
91         require (AT_INCLUDE_PATH.'footer.inc.php');
92         exit;
93 }
94 $out_of = $row['out_of'];
95 $anonymous = $row['anonymous'];
96 $random = $row['random'];
97 $title = $row['title'];
98
99 //count total
100 $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";
101 $result = mysql_query($sql, $db);
102 $row    = mysql_fetch_array($result);
103 $num_sub = $row['cnt'];
104
105 //get results based on filtre and sorting
106 if ($anonymous == 1) {
107         // Keep login, full_name and fs fields even if not used: ORDER BY relies upon them
108         $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 ";
109 } else {        
110 //      $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";
111         //added by Indirect
112         $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 ";
113 }
114
115 // handle order by full_name separately
116 if ($col <> 'full_name') $sql .= " ORDER BY $col $order";
117
118 if ($anonymous <> 1) 
119 {
120         if ($col <> 'full_name')
121                 $sql .= ", R.final_score $order";
122         else
123                 $sql .= " ORDER BY R.final_score $order";
124 }
125
126 $result = mysql_query($sql, $db);
127
128 if ($anonymous == 1) {
129         $guest_text = '<strong>'._AT('anonymous').'</strong>';
130 } else {
131         $guest_text = '- '._AT('guest').' -';
132 }
133 while ($row = mysql_fetch_assoc($result)) {
134         $full_name = AT_print(get_display_name($row['member_id']), 'members.full_name');
135         $row['full_name'] = $full_name ? $full_name : $guest_text;
136         $row['login']     = $row['login']     ? $row['login']     : $guest_text;
137         $rows[$row['result_id']] = $row;
138 }
139
140 if ($col == "full_name") usort($rows, "sortByFullName");
141
142 $num_results = mysql_num_rows($result);
143
144 //count unmarked: no need to do this query if filtre is already getting unmarked
145 if (isset($_GET['status']) && ($_GET['status'] != '') && ($_GET['status'] == 0)) {
146         $num_unmarked = $num_results;
147 } else {
148         $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=''";
149         $result = mysql_query($sql, $db);
150         $row = mysql_fetch_array($result);
151         $num_unmarked = $row['cnt'];
152 }
153
154 ?>
155 <!--h3><?php //echo AT_print($row['title'], 'tests.title'); ?></h3><br / -->
156 <div id="container">
157 <h3><?php echo AT_print($title, 'tests.title'); ?></h3><br />
158 <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
159         <input type="hidden" name="tid" value="<?php echo $tid; ?>" />
160
161         <div class="input-form">
162         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('filter'); ?></legend>
163                 <div class="row">
164                         <h3><?php echo _AT('results_found', $num_results); ?></h3>
165                 </div>
166
167                 <div class="row">
168                         <?php echo _AT('status'); ?><br />
169                         <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> 
170                         <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> 
171                         <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> 
172
173                 </div>
174
175                 <div class="row">
176                         <?php echo _AT('user_type'); ?><br />
177                         <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> 
178                         <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> 
179                         <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> 
180                 </div>
181
182                 <div class="row buttons">
183                         <input type="submit" name="filter" value="<?php echo _AT('filter'); ?>" />
184                         <input type="submit" name="reset_filter" value="<?php echo _AT('reset_filter'); ?>" />
185                 </div>
186                 </fieldset>
187         </div>
188 </form>
189
190 <form name="form" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
191 <input type="hidden" name="tid" value="<?php echo $tid; ?>" />
192
193 <table class="data" summary="" rules="cols">
194 <colgroup>
195         <?php if ($col == 'login'): ?>
196                 <col />
197                 <col class="sort" />
198                 <col span="4" />
199         <?php elseif ($col == 'full_name'): ?>
200                 <col span="2" />
201                 <col class="sort" />
202                 <col span="3" />
203         <?php elseif($col == 'date_taken'): ?>
204                 <col span="3" />
205                 <col class="sort" />
206                 <col span="2" />
207         <?php elseif($col == 'time_spent'): ?>
208                 <col span="4" />
209                 <col class="sort" />
210                 <col span="1" />
211         <?php elseif($col == 'fs'): ?>
212                 <col span="5" />
213                 <col class="sort" />
214         <?php endif; ?>
215 </colgroup>
216 <thead>
217 <tr>
218         <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>
219         <th scope="col"><a href="mods/_standard/tests/results.php?tid=<?php echo $tid.$page_string.SEP.$orders[$order]; ?>=login"><?php echo _AT('login_name'); ?></a></th>
220         <th scope="col"><a href="mods/_standard/tests/results.php?tid=<?php echo $tid.$page_string.SEP.$orders[$order]; ?>=full_name"><?php echo _AT('full_name'); ?></a></th>
221         <th scope="col"><a href="mods/_standard/tests/results.php?tid=<?php echo $tid.$page_string.SEP.$orders[$order]; ?>=date_taken"><?php echo _AT('date_taken'); ?></a></th>
222         <th scope="col"><a href="mods/_standard/tests/results.php?tid=<?php echo $tid.$page_string.SEP.$orders[$order]; ?>=time_spent"><?php echo _AT('time_spent'); ?></a></th>
223         <th scope="col"><a href="mods/_standard/tests/results.php?tid=<?php echo $tid.$page_string.SEP.$orders[$order]; ?>=fs"><?php echo _AT('mark'); ?></a></th>
224 </tr>
225 </thead>
226 <tfoot>
227 <tr>
228         <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>
229 </tr>
230 </tfoot>
231 <tbody>
232 <?php if ($rows): ?>
233         <?php foreach ($rows as $row): ?>
234                 <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']; ?>">
235                         <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>
236                         <td><?php echo $row['login']; ?></td>
237                         <td><?php 
238                                 if ($anonymous == 0 && $row['member_id']){
239                                         echo $row['full_name']; 
240                                 } else {
241                                         echo $guest_text; // no need in AT_print(): $guest_text is a trusted _AT() output
242                                 }
243                                 ?></td>
244                         <td><?php $startend_date_format=_AT('startend_date_format'); echo AT_date( $startend_date_format, $row['date_taken'], AT_DATE_MYSQL_DATETIME); ?></td>
245                         <td><?php echo get_human_time($row['time_spent']); ?></td>
246
247                         <td align="center">
248                                 <?php if ($out_of) {
249                                         if ($random) {
250                                                 $out_of = get_random_outof($tid, $row['result_id']);
251                                         }
252
253                                         if ($row['final_score'] != '') { 
254                                                 echo $row['final_score'].'/'.$out_of;
255                                         } else {
256                                                 echo _AT('unmarked');
257                                         }
258                                 } else {
259                                         echo _AT('na');
260                                 }
261                                 ?>
262                         </td>
263                 </tr>
264         <?php endforeach; ?>
265 <?php else: ?>
266         <tr>
267                 <td colspan="6"><?php echo _AT('none_found'); ?></td>
268         </tr>
269 <?php endif; ?>
270 </tbody>
271 </table>
272 </form>
273 </div>
274 <script language="JavaScript" type="text/javascript">
275 //<!--
276 function CheckAll() {
277         for (var i=0;i<document.form.elements.length;i++)       {
278                 var e = document.form.elements[i];
279                 if ((e.name == 'id[]') && (e.type=='checkbox')) {
280                         e.checked = document.form.selectall.checked;
281                         togglerowhighlight(document.getElementById("r" + e.id), e.id);
282                 }
283         }
284 }
285
286 function togglerowhighlight(obj, boxid) {
287         if (document.getElementById(boxid).checked) {
288                 obj.className = 'selected';
289         } else {
290                 obj.className = '';
291         }
292 }
293 //-->
294 </script>
295 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>