remove old readme
[atutor.git] / docs / mods / _standard / tests / preview.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/classes/testQuestions.class.php');
17
18 authenticate(AT_PRIV_TESTS);
19 $_letters = array(_AT('A'), _AT('B'), _AT('C'), _AT('D'), _AT('E'), _AT('F'), _AT('G'), _AT('H'), _AT('I'), _AT('J'));
20
21 if ($_POST['back']) {
22         header('Location: index.php');
23         exit;
24
25
26 if (defined('AT_FORCE_GET_FILE') && AT_FORCE_GET_FILE) {
27         $content_base_href = 'get.php/';
28 } else {
29         $content_base_href = 'content/' . $_SESSION['course_id'] . '/';
30 }
31
32 require(AT_INCLUDE_PATH.'header.inc.php');
33
34 $tid = intval($_GET['tid']);
35
36 /* Retrieve the content_id of this test */
37 $sql = "SELECT title, random, num_questions, instructions FROM ".TABLE_PREFIX."tests WHERE test_id=$tid";
38 $result = mysql_query($sql, $db); 
39 if (!($test_row = mysql_fetch_assoc($result))) {
40         $msg->printErrors('ITEM_NOT_FOUND');
41         require (AT_INCLUDE_PATH.'footer.inc.php');
42         exit;
43 }
44 $num_questions = $test_row['num_questions'];
45 $rand_err = false;
46
47 if ($row['random']) {
48         /* !NOTE! this is a really awful way of randomizing questions !NOTE! */
49
50         /* Retrieve 'num_questions' question_id randomly choosed from  
51         those who are related to this content_id*/
52         $sql    = "SELECT question_id FROM ".TABLE_PREFIX."tests_questions_assoc WHERE test_id=$tid";
53         $result = mysql_query($sql, $db); 
54         $i = 0;
55         $row2 = mysql_fetch_assoc($result);
56         /* Store all related question in cr_questions */
57         while ($row2['question_id'] != '') {
58                 $cr_questions[$i] = $row2['question_id'];
59                 $row2 = mysql_fetch_assoc($result);
60                 $i++;
61         }
62         if ($i < $num_questions) {
63                 /* this if-statement is misleading. */
64                 /* one should still be able to preview a test before all its questions have been added. */
65                 /* ie. preview as questions are added. */
66                 /* bug # 0000615 */
67                 $rand_err = true;
68         } else {
69                 /* Randomly choose only 'num_question' question */
70                 $random_idx = rand(0, $i-1);
71                 $random_id_string = $cr_questions[$random_idx];
72                 $j = 0;
73                 $extracted[$j] = $random_idx;
74                 $j++;
75                 $num_questions--;
76                 while ($num_questions > 0) {
77                         $done = false;
78                         while (!$done) {
79                                 $random_idx = rand(0, $i-1);
80                                 $done = true;
81                                 for ($k=0;$k<$j;$k++) {
82                                         if ($extracted[$k]== $random_idx) {
83                                                 $done = false;
84                                                 break;
85                                         }
86                                 }
87                         }
88                         $extracted[$j] = $random_idx;
89                         $j++;
90                         $random_id_string = $random_id_string.','.$cr_questions[$random_idx];
91                         $num_questions--;
92                 }
93                 $sql = "SELECT TQ.*, TQA.* FROM ".TABLE_PREFIX."tests_questions TQ INNER JOIN ".TABLE_PREFIX."tests_questions_assoc TQA USING (question_id) WHERE TQ.course_id=$_SESSION[course_id] AND TQA.test_id=$tid AND TQA.question_id IN ($random_id_string) ORDER BY TQA.ordering, TQA.question_id";
94         }
95 } else {
96         $sql    = "SELECT TQ.*, TQA.* FROM ".TABLE_PREFIX."tests_questions TQ INNER JOIN ".TABLE_PREFIX."tests_questions_assoc TQA USING (question_id) WHERE TQ.course_id=$_SESSION[course_id] AND TQA.test_id=$tid ORDER BY TQA.ordering, TQA.question_id";
97 }
98 $result = mysql_query($sql, $db);
99 $count = 1;
100 echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'" name="preview">';
101
102 if (($row = mysql_fetch_assoc($result)) && !$rand_err) {
103         ?>
104         <div class="input-form" style="width:95%">
105         <div class="row"><h2><?php echo $test_row['title']; ?></h2></div>
106
107
108         <?php if ($test_row['instructions'] != ''): ?>
109                 <div style="background-color: #f3f3f3; padding: 5px 10px; margin: 0px; border-top: 1px solid">
110                         <strong><?php echo _AT('instructions'); ?></strong>
111                 </div>
112                 <div class="row" style="padding-bottom: 20px"><?php echo $test_row['instructions']; ?></div>
113         <?php endif; ?>
114         
115         <?php
116         do {
117                 $o = TestQuestions::getQuestion($row['type']);
118                 $o->display($row);
119         } while ($row = mysql_fetch_assoc($result));
120         ?>
121         <div class="row buttons">
122                 <input type="submit" value="<?php echo _AT('back'); ?>" name="back" />
123         </div>
124
125         </div>
126         </form>
127 <script type="text/javascript">
128 //<!--
129 function iframeSetHeight(id, height) {
130         document.getElementById("qframe" + id).style.height = (height + 20) + "px";
131 }
132 //-->
133 </script>
134 <?php
135 } else {
136         $msg->printErrors('NO_QUESTIONS');
137 }
138
139
140 require(AT_INCLUDE_PATH.'footer.inc.php');
141 ?>