ATutor 2.0
[atutor.git] / mods / _standard / tests / test_intro.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: test_intro.php 9034 2009-12-14 19:47:30Z cindy $
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');
17
18 // test authentication
19 if (isset($_GET['tid']))
20         $tid = intval($_GET['tid']);
21 else if (isset($_POST['tid']))
22         $tid = intval($_POST['tid']);
23
24 if (isset($_REQUEST['cid']))
25 {
26         $cid = intval($_REQUEST['cid']);
27         $msg->addInfo('PRETEST');
28 }
29
30 // make sure max attempts not reached, and still on going
31 $sql            = "SELECT *, UNIX_TIMESTAMP(start_date) AS start_date2, UNIX_TIMESTAMP(end_date) AS end_date2 FROM ".TABLE_PREFIX."tests WHERE test_id=".$tid." AND course_id=".$_SESSION['course_id'];
32 $result = mysql_query($sql, $db);
33 $test_row = mysql_fetch_assoc($result);
34 /* check to make sure we can access this test: */
35 if (!$test_row['guests'] && ($_SESSION['enroll'] == AT_ENROLL_NO || $_SESSION['enroll'] == AT_ENROLL_ALUMNUS)) {
36         require(AT_INCLUDE_PATH.'header.inc.php');
37         $msg->printInfos('NOT_ENROLLED');
38         require(AT_INCLUDE_PATH.'footer.inc.php');
39         exit;
40 }
41 if (!$test_row['guests'] && !authenticate_test($tid)) {
42         header('Location: my_tests.php');
43         exit;
44 }
45
46 // checks one/all questions per page, and forward user to the correct one
47 if (isset($_POST['cancel'])) 
48 {
49         if (isset($cid))
50         {
51                 require(AT_INCLUDE_PATH.'header.inc.php');
52                 $msg->printInfos(array('PRETEST_FAILED', $test_row['title']));
53                 require(AT_INCLUDE_PATH.'footer.inc.php');
54                 exit;
55         }
56         //Retrieve last visited page
57         if (isset($_SESSION['last_visited_page'])){
58                 $_last_visited_page = $_SESSION['last_visited_page'];
59                 unset($_SESSION['last_visited_page']);
60         } else {
61                 $_last_visited_page = url_rewrite('mods/_standard/tests/my_tests.php', AT_PRETTY_URL_IS_HEADER);
62         }
63
64         $msg->addFeedback('CANCELLED'); 
65         header('Location: '.$_last_visited_page);
66         exit;
67
68 else if (isset($_POST['submit'])) 
69 {
70         $guest_name = $addslashes(trim($_POST["guest_name"]));
71         $organization = $addslashes(trim($_POST["organization"]));
72         $location = $addslashes(trim($_POST["location"]));
73         $role = $addslashes(trim($_POST["role"]));
74         $focus = $addslashes(trim($_POST["focus"]));
75         
76         if ($guest_name <> "" || $organization <> "" || $location <> "" || $role <> "" || $focus <> "")
77         {
78                 $guest_id = get_next_guest_id();
79
80                 $sql    = "INSERT INTO ".TABLE_PREFIX."guests (guest_id, name, organization, location, role, focus)
81                                                  VALUES ('$guest_id', '$guest_name', '$organization', '$location', '$role', '$focus')";
82                 $result = mysql_query($sql, $db);
83                 $result_id = mysql_insert_id($db);
84         }
85         $gid_str = (isset($guest_id)) ? SEP."gid=".$guest_id : "";
86         if (isset($cid)) $gid_str .= SEP.'cid='.$cid;
87
88         if ($test_row['display']) {
89                 header('Location: '.url_rewrite('mods/_standard/tests/take_test_q.php?tid='.$tid.$gid_str, AT_PRETTY_URL_IS_HEADER));
90         } else {
91                 header('Location: '.url_rewrite('mods/_standard/tests/take_test.php?tid='.$tid.$gid_str, AT_PRETTY_URL_IS_HEADER));
92         }
93         exit;
94 }
95
96 /* 
97  * If max attempted reached, then stop it.
98  * @3300
99  */
100 $sql = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."tests_results WHERE status=1 AND test_id=".$tid." AND member_id='".$_SESSION['member_id']."'";
101 if ( (($test_row['start_date2'] > time()) || ($test_row['end_date2'] < time())) || 
102    ( ($test_row['num_takes'] != AT_TESTS_TAKE_UNLIMITED) && ($takes['cnt'] >= $test_row['num_takes']) )  ) {
103         require(AT_INCLUDE_PATH.'header.inc.php');
104         $msg->printInfos('MAX_ATTEMPTS');
105         
106         require(AT_INCLUDE_PATH.'footer.inc.php');
107         exit;
108 }
109
110 require(AT_INCLUDE_PATH.'header.inc.php');
111
112 // get number of attempts
113 $sql    = "SELECT COUNT(test_id) AS cnt FROM ".TABLE_PREFIX."tests_results WHERE status=1 AND test_id=$tid AND member_id='".$_SESSION['member_id']."'";
114 $result = mysql_query($sql, $db);
115 if ($row = mysql_fetch_assoc($result)) {
116         $num_takes = $row['cnt'];
117 } else {
118         $num_takes = 0;
119 }
120
121 $sql    = "SELECT COUNT(*) AS num_questions FROM ".TABLE_PREFIX."tests_questions_assoc WHERE test_id=$tid";
122 $result = mysql_query($sql, $db);
123 $row = mysql_fetch_assoc($result);
124 if (!$test_row['random'] || $test_row['num_questions'] > $row['num_questions']) {
125         $test_row['num_questions'] = $row['num_questions'];
126 }
127
128 ?>
129
130 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
131 <input type="hidden" name="tid" value="<?php echo $tid; ?>" />
132 <?php if (isset($cid)) { ?><input type="hidden" name="cid" value="<?php echo $cid; ?>" /> <?php } ?>
133
134 <div class="input-form">
135         <fieldset class="group_form"><legend class="group_form"><?php echo $test_row['title']; ?></legend><div class="row">
136
137 <?php if ($test_row['guests'] && $test_row['show_guest_form'] && !$_SESSION['member_id']): ?>
138         <fieldset class="group_form"><legend class="group_form"><?php echo _AT("test_description"); ?></legend><div class="row">
139 <?php endif; ?>
140         <table>
141 <?php if ($test_row['description']<>""): ?>
142                 <tr>
143                         <td><?php echo _AT('test_description'); ?></td>
144                         <td><?php echo empty($test_row['description']) ? '&nbsp;' : $test_row['description']; ?></td>
145                 </tr>
146 <?php endif; ?>
147
148                 <tr>
149                         <td><?php echo _AT('questions'); ?></td>
150                         <td><?php echo $test_row['num_questions']; ?></td>
151                 </tr>
152
153                 <tr>
154                         <td><?php echo _AT('out_of'); ?></td>
155                         <td><?php echo $test_row['out_of']; ?></td>
156                 </tr>
157         
158                 <tr>
159                         <td><?php echo _AT('attempts'); ?></td>
160                         <td><?php echo $num_takes; ?> / <?php echo ($test_row['num_takes'] == AT_TESTS_TAKE_UNLIMITED) ? _AT('unlimited') : $test_row['num_takes']; ?></td>
161                 </tr>
162                         
163                 <tr>
164                         <td><?php echo _AT('start_date'); ?></td>
165                         <td><?php echo AT_date( _AT('startend_date_long_format'), $test_row['start_date'], AT_DATE_MYSQL_DATETIME); ?></td>
166                 </tr>
167
168                 <tr>
169                         <td><?php echo _AT('end_date'); ?></td>
170                         <td><?php echo AT_date( _AT('startend_date_long_format'), $test_row['end_date'], AT_DATE_MYSQL_DATETIME); ?></td>
171                 </tr>
172
173                 <tr>
174                         <td><?php echo _AT('anonymous'); ?></td>
175                         <td><?php echo $test_row['anonymous'] ? _AT('yes') : _AT('no'); ?></td>
176                 </tr>
177
178                 <tr>
179                         <td><?php echo _AT('display'); ?></td>
180                         <td><?php echo $test_row['display'] ? _AT('one_question_per_page') : _AT('all_questions_on_page'); ?></td>
181                 </tr>
182
183                 <tr>
184                         <td><?php echo _AT('instructions'); ?></td>
185                         <td><?php echo nl2br($test_row['instructions']); ?></td>
186                 </tr>
187                 </table>
188 <?php if ($test_row['guests'] && !$_SESSION['member_id']): ?>
189         </fieldset>
190 <?php endif; ?>
191
192 <?php if (($test_row['guests']) && $test_row['show_guest_form'] && !$_SESSION['member_id']): ?>
193         <fieldset class="group_form"><legend class="group_form"><?php echo _AT("guest_information").' ('._AT('optional').')'; ?></legend><div class="row">
194
195         <table class="none" width="100%">
196         <tr>
197                 <td width="20%"><label for="guest_name" style="float:right;"><?php echo _AT('guest_name'); ?></label></td>
198                 <td width="80%"><input id="guest_name" name="guest_name" size="50" type="text" value="<?php echo stripslashes(htmlspecialchars($_POST['guest_name'])); ?>"/></td>
199         </tr>
200
201         <tr>
202                 <td><label for="organization" style="float:right;"><?php echo _AT('organization'); ?></label></td>
203                 <td><input id="organization" name="organization" size="50" type="text" value="<?php echo stripslashes(htmlspecialchars($_POST['organization'])); ?>" /></td>
204         </tr>
205
206         <tr>
207                 <td><label for="location" style="float:right;"><?php echo _AT('location'); ?></label></td>
208                 <td><input id="location" name="location" size="50" type="text" value="<?php echo stripslashes(htmlspecialchars($_POST['location'])); ?>" /></td>
209         </tr>
210
211         <tr>
212                 <td><label for="role" style="float:right;"><?php echo _AT('role'); ?></label></td>
213                 <td><input id="role" name="role" size="50" type="text" value="<?php echo stripslashes(htmlspecialchars($_POST['role'])); ?>" /></td>
214         </tr>
215
216         <tr>
217                 <td><label for="focus" style="float:right;"><?php echo _AT('focus'); ?></label></td>
218                 <td><input id="focus" name="focus"  size="50" type="text" value="<?php echo stripslashes(htmlspecialchars($_POST['focus'])); ?>" /></td>
219
220         </tr>
221         </table>
222
223         </fieldset>
224
225
226
227 <?php endif; ?>
228 </div>
229         
230         <div class="row buttons">
231                 <input type="submit" name="submit" value=" <?php echo _AT('start_test'); ?> " accesskey="s" class="button"/>
232                 <input type="submit" name="cancel" value=" <?php echo _AT('cancel'); ?> "  class="button" />
233         </div>
234
235         </fieldset>
236 </div>
237
238 </form>
239
240 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>