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