87afb3403e28724937ea22ed119127b27ea3ad9d
[atutor.git] / docs / 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$
14
15 /*
16  * This script accept these _GET variables:
17  * tid: test id
18  * cid: this test is the pre-requisite of the content id cid.
19  *      It's embedded as hidden value and passed into take_test pages.
20  * in_cid: this test is the post test of the content id cid.
21  *     Also embedded as hidden value and passed into take_test pages.
22  * Only one of cid or in_cid should be presented.
23  * If cid or in_cid is presented, the script flow is,
24  * If the user passes the pre-requisite test, the page is re-directed 
25  * back to the content page of cid. Otherwise, re-take the test.
26  */
27
28 define('AT_INCLUDE_PATH', '../../../include/');
29 require(AT_INCLUDE_PATH.'vitals.inc.php');
30 require(AT_INCLUDE_PATH.'../mods/_standard/tests/lib/test_result_functions.inc.php');
31
32 // test authentication
33 if (isset($_GET['tid']))
34         $tid = intval($_GET['tid']);
35 else if (isset($_POST['tid']))
36         $tid = intval($_POST['tid']);
37
38 if (isset($_REQUEST['cid']))
39 {
40         $cid = intval($_REQUEST['cid']);
41         $msg->addInfo('PRETEST');
42 }
43
44 // this test is the post test of content in_cid
45 if (isset($_REQUEST['in_cid']))
46 {
47         $in_cid = intval($_REQUEST['in_cid']);
48         
49         // Check if the content has pre-requisite tests that the member has not passed.
50         // If there is, 
51         // 1. Instructors: see the message that a pretest is associated with this content and proceed to display the post test
52         // 2. Students: re-direct to pre-test, when pass the pre-test, direct to parent content page. The students must go thru
53         //              the content to get to the post test. 
54         if ($in_cid > 0) {
55                 $pretest = $contentManager->getPretest($in_cid);
56
57                 if ($pretest > 0) {
58                         if (authenticate(AT_PRIV_CONTENT, AT_PRIV_RETURN)) {
59                                 $msg->addInfo('PRETEST');
60                         }
61                         else {
62                                 header('Location: '.url_rewrite('mods/_standard/tests/test_intro.php?tid='.$pretest.SEP.'cid='.$in_cid, AT_PRETTY_URL_IS_HEADER));
63                                 exit;
64                         }
65                 } else if ($pretest == -1) { // pretest exists but cannot access because the pretest has expired.
66                         require(AT_INCLUDE_PATH.'header.inc.php');
67                         $msg->printAll();
68                         require(AT_INCLUDE_PATH.'footer.inc.php');
69                         exit;
70                 }
71         }
72 }
73
74 // make sure max attempts not reached, and still on going
75 $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'];
76 $result = mysql_query($sql, $db);
77 $test_row = mysql_fetch_assoc($result);
78 /* check to make sure we can access this test: */
79 if (!$test_row['guests'] && ($_SESSION['enroll'] == AT_ENROLL_NO || $_SESSION['enroll'] == AT_ENROLL_ALUMNUS)) {
80         require(AT_INCLUDE_PATH.'header.inc.php');
81         $msg->printInfos('NOT_ENROLLED');
82         require(AT_INCLUDE_PATH.'footer.inc.php');
83         exit;
84 }
85 if (!$test_row['guests'] && !authenticate_test($tid)) {
86         header('Location: my_tests.php');
87         exit;
88 }
89
90 // checks one/all questions per page, and forward user to the correct one
91 if (isset($_POST['cancel'])) 
92 {
93         if (isset($cid))
94         {
95                 require(AT_INCLUDE_PATH.'header.inc.php');
96                 $msg->printInfos(array('PRETEST_FAILED', $test_row['title']));
97                 require(AT_INCLUDE_PATH.'footer.inc.php');
98                 exit;
99         }
100         //Retrieve last visited page
101         if (isset($_SESSION['last_visited_page'])){
102                 $_last_visited_page = $_SESSION['last_visited_page'];
103                 unset($_SESSION['last_visited_page']);
104         } else {
105                 $_last_visited_page = url_rewrite('mods/_standard/tests/my_tests.php', AT_PRETTY_URL_IS_HEADER);
106         }
107
108         $msg->addFeedback('CANCELLED'); 
109         header('Location: '.$_last_visited_page);
110         exit;
111
112 else if (isset($_POST['submit'])) 
113 {
114         $guest_name = $addslashes(trim($_POST["guest_name"]));
115         $organization = $addslashes(trim($_POST["organization"]));
116         $location = $addslashes(trim($_POST["location"]));
117         $role = $addslashes(trim($_POST["role"]));
118         $focus = $addslashes(trim($_POST["focus"]));
119         
120         if ($guest_name <> "" || $organization <> "" || $location <> "" || $role <> "" || $focus <> "")
121         {
122                 $guest_id = get_next_guest_id();
123
124                 $sql    = "INSERT INTO ".TABLE_PREFIX."guests (guest_id, name, organization, location, role, focus)
125                                                  VALUES ('$guest_id', '$guest_name', '$organization', '$location', '$role', '$focus')";
126                 $result = mysql_query($sql, $db);
127                 $result_id = mysql_insert_id($db);
128         }
129         $gid_str = (isset($guest_id)) ? SEP."gid=".$guest_id : "";
130         if (isset($cid)) $gid_str .= SEP.'cid='.$cid;
131
132         if ($test_row['display']) {
133                 header('Location: '.url_rewrite('mods/_standard/tests/take_test_q.php?tid='.$tid.$gid_str, AT_PRETTY_URL_IS_HEADER));
134         } else {
135                 header('Location: '.url_rewrite('mods/_standard/tests/take_test.php?tid='.$tid.$gid_str, AT_PRETTY_URL_IS_HEADER));
136         }
137         exit;
138 }
139
140 /* 
141  * If max attempted reached, then stop it.
142  * @3300
143  */
144 $sql = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."tests_results WHERE status=1 AND test_id=".$tid." AND member_id='".$_SESSION['member_id']."'";
145 $takes_result = mysql_query($sql, $db);
146 $takes = mysql_fetch_assoc($takes_result);
147 if ( (($test_row['start_date2'] > time()) || ($test_row['end_date2'] < time())) || 
148    ( ($test_row['num_takes'] != AT_TESTS_TAKE_UNLIMITED) && ($takes['cnt'] >= $test_row['num_takes']) )  ) {
149         require(AT_INCLUDE_PATH.'header.inc.php');
150         $msg->printInfos('MAX_ATTEMPTS');
151         
152         require(AT_INCLUDE_PATH.'footer.inc.php');
153         exit;
154 }
155
156 require(AT_INCLUDE_PATH.'header.inc.php');
157
158 // get number of attempts
159 $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']."'";
160 $result = mysql_query($sql, $db);
161 if ($row = mysql_fetch_assoc($result)) {
162         $num_takes = $row['cnt'];
163 } else {
164         $num_takes = 0;
165 }
166
167 $sql    = "SELECT COUNT(*) AS num_questions FROM ".TABLE_PREFIX."tests_questions_assoc WHERE test_id=$tid";
168 $result = mysql_query($sql, $db);
169 $row = mysql_fetch_assoc($result);
170 if (!$test_row['random'] || $test_row['num_questions'] > $row['num_questions']) {
171         $test_row['num_questions'] = $row['num_questions'];
172 }
173
174 ?>
175
176 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
177 <input type="hidden" name="tid" value="<?php echo $tid; ?>" />
178 <?php if (isset($cid)) { ?><input type="hidden" name="cid" value="<?php echo $cid; ?>" /> <?php } ?>
179
180 <div class="input-form">
181         <fieldset class="group_form"><legend class="group_form"><?php echo $test_row['title']; ?></legend><div class="row">
182
183 <?php if ($test_row['guests'] && $test_row['show_guest_form'] && !$_SESSION['member_id']): ?>
184         <fieldset class="group_form"><legend class="group_form"><?php echo _AT("test_description"); ?></legend><div class="row">
185 <?php endif; ?>
186         <table>
187 <?php if ($test_row['description']<>""): ?>
188                 <tr>
189                         <td><?php echo _AT('test_description'); ?></td>
190                         <td><?php echo empty($test_row['description']) ? '&nbsp;' : $test_row['description']; ?></td>
191                 </tr>
192 <?php endif; ?>
193
194                 <tr>
195                         <td><?php echo _AT('questions'); ?></td>
196                         <td><?php echo $test_row['num_questions']; ?></td>
197                 </tr>
198
199                 <tr>
200                         <td><?php echo _AT('out_of'); ?></td>
201                         <td><?php echo $test_row['out_of']; ?></td>
202                 </tr>
203         
204                 <tr>
205                         <td><?php echo _AT('attempts'); ?></td>
206                         <td><?php echo $num_takes; ?> / <?php echo ($test_row['num_takes'] == AT_TESTS_TAKE_UNLIMITED) ? _AT('unlimited') : $test_row['num_takes']; ?></td>
207                 </tr>
208                         
209                 <tr>
210                         <td><?php echo _AT('start_date'); ?></td>
211                         <td><?php echo AT_date( _AT('startend_date_long_format'), $test_row['start_date'], AT_DATE_MYSQL_DATETIME); ?></td>
212                 </tr>
213
214                 <tr>
215                         <td><?php echo _AT('end_date'); ?></td>
216                         <td><?php echo AT_date( _AT('startend_date_long_format'), $test_row['end_date'], AT_DATE_MYSQL_DATETIME); ?></td>
217                 </tr>
218
219                 <tr>
220                         <td><?php echo _AT('anonymous'); ?></td>
221                         <td><?php echo $test_row['anonymous'] ? _AT('yes') : _AT('no'); ?></td>
222                 </tr>
223
224                 <tr>
225                         <td><?php echo _AT('display'); ?></td>
226                         <td><?php echo $test_row['display'] ? _AT('one_question_per_page') : _AT('all_questions_on_page'); ?></td>
227                 </tr>
228
229                 <tr>
230                         <td><?php echo _AT('instructions'); ?></td>
231                         <td><?php echo nl2br($test_row['instructions']); ?></td>
232                 </tr>
233                 </table>
234 <?php if ($test_row['guests'] && !$_SESSION['member_id']): ?>
235         </fieldset>
236 <?php endif; ?>
237
238 <?php if (($test_row['guests']) && $test_row['show_guest_form'] && !$_SESSION['member_id']): ?>
239         <fieldset class="group_form"><legend class="group_form"><?php echo _AT("guest_information").' ('._AT('optional').')'; ?></legend><div class="row">
240
241         <table class="none" width="100%">
242         <tr>
243                 <td width="20%"><label for="guest_name" style="float:right;"><?php echo _AT('guest_name'); ?></label></td>
244                 <td width="80%"><input id="guest_name" name="guest_name" size="50" type="text" value="<?php echo stripslashes(htmlspecialchars($_POST['guest_name'])); ?>"/></td>
245         </tr>
246
247         <tr>
248                 <td><label for="organization" style="float:right;"><?php echo _AT('organization'); ?></label></td>
249                 <td><input id="organization" name="organization" size="50" type="text" value="<?php echo stripslashes(htmlspecialchars($_POST['organization'])); ?>" /></td>
250         </tr>
251
252         <tr>
253                 <td><label for="location" style="float:right;"><?php echo _AT('location'); ?></label></td>
254                 <td><input id="location" name="location" size="50" type="text" value="<?php echo stripslashes(htmlspecialchars($_POST['location'])); ?>" /></td>
255         </tr>
256
257         <tr>
258                 <td><label for="role" style="float:right;"><?php echo _AT('role'); ?></label></td>
259                 <td><input id="role" name="role" size="50" type="text" value="<?php echo stripslashes(htmlspecialchars($_POST['role'])); ?>" /></td>
260         </tr>
261
262         <tr>
263                 <td><label for="focus" style="float:right;"><?php echo _AT('focus'); ?></label></td>
264                 <td><input id="focus" name="focus"  size="50" type="text" value="<?php echo stripslashes(htmlspecialchars($_POST['focus'])); ?>" /></td>
265
266         </tr>
267         </table>
268
269         </fieldset>
270
271
272
273 <?php endif; ?>
274 </div>
275         
276         <div class="row buttons">
277                 <input type="submit" name="submit" value=" <?php echo _AT('start_test'); ?> " accesskey="s" class="button"/>
278                 <input type="submit" name="cancel" value=" <?php echo _AT('cancel'); ?> "  class="button" />
279         </div>
280
281         </fieldset>
282 </div>
283
284 </form>
285
286 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>