made a copy
[atutor.git] / tools / view_results.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay & Joel Kronenberg        */
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 require(AT_INCLUDE_PATH.'lib/test_result_functions.inc.php'); // for print_result and print_score
17 require(AT_INCLUDE_PATH.'classes/testQuestions.class.php');
18 $_letters = array(_AT('A'), _AT('B'), _AT('C'), _AT('D'), _AT('E'), _AT('F'), _AT('G'), _AT('H'), _AT('I'), _AT('J'));
19
20 if (defined('AT_FORCE_GET_FILE') && AT_FORCE_GET_FILE) {
21         $content_base_href = 'get.php/';
22 } else {
23         $course_base_href = 'content/' . $_SESSION['course_id'] . '/';
24 }
25
26 require(AT_INCLUDE_PATH.'header.inc.php');
27
28 $tid = intval($_GET['tid']);
29 $rid = intval($_GET['rid']);
30
31 $sql    = "SELECT title, random, passfeedback, failfeedback, passscore, passpercent FROM ".TABLE_PREFIX."tests WHERE test_id=$tid AND course_id=$_SESSION[course_id]";
32 $result = mysql_query($sql, $db);
33 $row    = mysql_fetch_array($result);
34 $test_title     = $row['title'];
35 $passfeedback   = $row['passfeedback'];
36 $failfeedback   = $row['failfeedback'];
37 $passscore      = $row['passscore'];
38 $passpercent    = $row['passpercent'];
39 $is_random  = $row['random'];
40
41 $mark_right = ' <img src="'.$_base_path.'images/checkmark.gif" alt="'._AT('correct_answer').'" title="'._AT('correct_answer').'" />';
42 $mark_wrong = ' <img src="'.$_base_path.'images/x.gif" alt="'._AT('wrong_answer').'" title="'._AT('wrong_answer').'" />';
43
44 $sql    = "SELECT * FROM ".TABLE_PREFIX."tests_results WHERE result_id=$rid";
45 $result = mysql_query($sql, $db); 
46 if (!$row = mysql_fetch_assoc($result)){
47         $msg->printErrors('ITEM_NOT_FOUND');
48         require(AT_INCLUDE_PATH.'footer.inc.php');
49         exit;
50 }
51 $final_score= $row['final_score'];
52
53 //make sure they're allowed to see results now
54 $sql    = "SELECT result_release, out_of FROM ".TABLE_PREFIX."tests WHERE test_id=$tid AND course_id=$_SESSION[course_id]";
55 $result = mysql_query($sql, $db); 
56 $row = mysql_fetch_assoc($result);
57
58 if ( ($row['result_release']==AT_RELEASE_NEVER) || ($row['result_release']==AT_RELEASE_MARKED && $final_score=='') ) {
59         $msg->printErrors('RESULTS_NOT_RELEASED');
60         require(AT_INCLUDE_PATH.'footer.inc.php');
61         exit;
62 }
63
64 $out_of = $row['out_of'];
65
66 /* Retrieve randomly choosed questions */
67 $sql    = "SELECT question_id FROM ".TABLE_PREFIX."tests_answers WHERE result_id=$rid";
68 $result = mysql_query($sql, $db); 
69 $row = mysql_fetch_array($result);
70 $random_id_string = $row[question_id];
71 $row = mysql_fetch_array($result);      
72 while ($row['question_id'] != '') {
73         $random_id_string = $random_id_string.','.$row['question_id'];
74         $row = mysql_fetch_array($result);
75 }
76 if (!$random_id_string) {
77         $random_id_string = 0;
78 }
79
80 if ($is_random) {
81         $sql    = "SELECT TQ.*, TQA.* FROM ".TABLE_PREFIX."tests_questions TQ INNER JOIN ".TABLE_PREFIX."tests_questions_assoc TQA USING (question_id) WHERE TQA.test_id=$tid AND TQ.question_id IN ($random_id_string) ORDER BY TQ.question_id";
82 } else {
83         $sql    = "SELECT TQ.*, TQA.* FROM ".TABLE_PREFIX."tests_questions TQ INNER JOIN ".TABLE_PREFIX."tests_questions_assoc TQA USING (question_id) WHERE TQA.test_id=$tid AND TQ.question_id IN ($random_id_string) ORDER BY TQA.ordering, TQ.question_id";
84 }
85 $result = mysql_query($sql, $db); 
86
87 if (mysql_num_rows($result) == 0) {
88         echo '<p>'._AT('no_questions').'</p>';
89         require(AT_INCLUDE_PATH.'footer.inc.php');
90         exit;
91 }
92
93 // calculate test/my total score to display pass/fail feedback
94 $sql_test_total = "SELECT sum(TQA.weight) test_total_score FROM ".TABLE_PREFIX."tests_questions TQ INNER JOIN ".TABLE_PREFIX."tests_questions_assoc TQA USING (question_id) WHERE TQA.test_id=$tid AND TQ.question_id IN ($random_id_string)";
95 $result_test_total      = mysql_query($sql_test_total, $db);
96 $row_test_total = mysql_fetch_array($result_test_total);
97 $test_total_score = $row_test_total["test_total_score"];
98
99 while ($row = mysql_fetch_assoc($result)) {
100         $sql_this_score = "SELECT * FROM ".TABLE_PREFIX."tests_answers WHERE result_id=$rid AND question_id=$row[question_id] AND member_id=$_SESSION[member_id]";
101         $result_this_score      = mysql_query($sql_this_score, $db); 
102         $this_score = mysql_fetch_assoc($result_this_score);
103
104         $my_score+=$this_score['score'];
105         $this_total += $row['weight'];
106 }
107 ?>
108 <form method="get" action="<?php echo AT_BASE_HREF; ?>tools/my_tests.php">
109 <div class="input-form">
110         <div class="row">
111                 <h2><?php echo AT_print($test_title, 'tests.title'); ?></h2>
112         </div>
113
114         <div class="row">
115                 <h3 align="center">
116                         <?php 
117                                 // don't display any feedback if test is created as "no pass score"
118                                 if (($passscore == 0 && $passpercent == 0) || ($passpercent <> 0 && $this_total == 0))
119                                         echo '';
120                                 // display pass feedback for passed students
121                                 elseif (($passscore<>0 && $my_score>=$passscore) ||
122                                     ($passpercent<>0 && ($my_score/$this_total*100)>=$passpercent))
123                                         echo '<font color="green">' . $passfeedback . '</font>';
124                                 // otherwise, display fail feedback
125                                 else
126                                         echo '<font color="red">' . $failfeedback . '</font>'; 
127                         ?>
128                 </h3>
129         </div>
130
131         <?php if ($row['instructions'] != ''): ?>
132                 <div style="background-color: #f3f3f3; padding: 5px 10px; margin: 0px; border-top: 1px solid">
133                         <strong><?php echo _AT('instructions'); ?></strong>
134                 </div>
135                 <div class="row" style="padding-bottom: 20px"><?php echo $row['instructions']; ?></div>
136         <?php endif; ?>
137
138         <?php
139         // reset the result cursor to beginning
140         mysql_data_seek ($result, 0);
141         
142         while ($row = mysql_fetch_assoc($result)) {
143                 $sql            = "SELECT * FROM ".TABLE_PREFIX."tests_answers WHERE result_id=$rid AND question_id=$row[question_id] AND member_id=$_SESSION[member_id]";
144                 $result_a       = mysql_query($sql, $db); 
145                 $answer_row = mysql_fetch_assoc($result_a);
146
147                 $obj = TestQuestions::getQuestion($row['type']);
148                 $obj->displayResult($row, $answer_row);
149
150                 if ($row['feedback']) {
151                         echo '<div class="row"><p><strong>'._AT('feedback').':</strong> ';
152                         echo nl2br($row['feedback']).'</p></div>';
153                 }
154         }
155         ?>
156
157         <?php if ($this_total): ?>
158                 <div style="background-color: #f3f3f3; padding: 5px 10px; margin: 0px; border-top: 1px solid">
159                         <strong>
160                                 <span style="float: right"><?php echo $my_score .' / '.$this_total; ?> <?php echo _AT('points'); ?></span>
161                                 <?php echo _AT('final_score'); ?>:
162                         </strong>
163                 </div>
164         <?php else: ?>
165                 <div style="background-color: #f3f3f3; padding: 5px 10px; margin: 0px; border-top: 1px solid">
166                         <strong>
167                                 <?php echo _AT('done'); ?>!
168                         </strong>
169                 </div>
170         <?php endif; ?>
171
172         <div class="row buttons">
173                 <input type="submit" value="<?php echo _AT('back'); ?>" name="back" />
174         </div>
175 </div>
176 </form>
177
178 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>