<?php
/************************************************** ***************************
$Id: question.php,v 1.2 2002/10/17 17:26:44 djresonance Exp $
Copyright 2002 Brandon Tallent
This file is part of phpTest.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
************************************************** ***************************/
function draw_sidebar()
{
global $test_session,
$PHP_SELF;
echo "<div class="menu
">n";
$count = 1;
foreach ($test_session->questions as $question_id) {
echo pt_make_href
("$PHP_SELF?question_id=$question_id",
$count++
);
}
}
function draw_question($question_id)
{
global $db,
$user,
$strings,
$question_number,
$test_session;
// edit will hold the html to display the 'edit' icon if the user is an admin
$edit = '';
// pull all of the info about the question from the db into our $row object
$result = $db->query("SELECT * FROM questions WHERE question_id = $question_id");
$row = $db->fetch_object($result);
// begin question
echo " <td bgcolor="" . pt_table_highlight(0) . "">n";
$image = 'edit_light.png';
$style = 'even';
// show the link to edit the question if user is an admin
if ($user->admin) {
$edit .= pt_make_href("edit_question.php?question_id=$question_id&question_type_id=$row->question_type_id", "<img src="images/$image" border="0">", $strings['TEST_EDIT_QUESTION']);
}
// create a question object based on the question_type_id
$question = pt_inst_qclass($row->question_type_id);
// draw the question
if (isset($test_session->
answers[$question_number])) { $question->draw_test_question($question_id, $question_number + 1, $edit, $style, $test_session->answers[$question_number]);
} else {
$question->draw_test_question($question_id, $question_number + 1, $edit, $style);
}
// end question
}
require_once('./include/h.inc.php');
pt_register('GET', 'question_id');
pt_register('POST', 'submit', 'questions', 'question_id', 'question_number', 'prev', 'next');
pt_register('SESSION', 'test_session');
$question_count =
count($test_session->
questions) -
1;
foreach($questions as $question_id => $answer) {
list($test_session->
answers[$question_number]) = pt_clean_vars
($answer);
}
$next_question_id = $test_session->questions[$question_number + 1];
pt_redirect("question.php?question_id=$next_question_id");
}
foreach($questions as $question_id => $answer) {
list($test_session->
answers[$question_number]) = pt_clean_vars
($answer);
// $test_session->answers[$question_number] = $answer;
}
$prev_question_id = $test_session->questions[$question_number - 1];
pt_redirect("question.php?question_id=$prev_question_id");
}
// make sure to update the answer into our test_session var
foreach($questions as $question_id => $answer) {
list($test_session->
answers[$question_number]) = pt_clean_vars
($answer);
}
$count = 0;
// now, we need to reconstruct the questions array in a format that
// $test->grade() will understand. This is just using a little array trickery.
foreach ($test_session->questions as $question_id) {
$questions[$question_id] = $test_session->answers[$count++];
}
// instantiate the test object so we can grade the test
$test = new cTest($test_session->test_id);
$test->grade($questions);
pt_redirect
("view_results.php?user_id=$user->user_id&feedback=" .
urlencode($strings['TEST_SCORED']));
} else {
require_once('./include/header.inc.php');
// draw_sidebar();
// print_r($test_session);
// figure out which question number we are on.
$question_number =
array_search($question_id,
$test_session->
questions);
// we need to compare types using the '===' comparison operator because array_search() can return
// 0 for the index which evaluates to false using the normal '==' comparison operator
if ($question_number === FALSE) {
pt_exit('Fatal Error: unable to determine question number.');
}
// output test title and additional notes
echo "<div class="form
"><center><h2>{$test_session->info['description']}</h2></center>";
if ($test_session->info['notes']) {
echo "<hr noshade="noshade
" size="1
" />n";
echo $test_session->
info['notes'];
}
echo "<form method="post
" name="test
" action="$PHP_SELF">n";
echo "<div class="test
">n";
echo "<table width="100%
" cellspacing="0
" cellpadding="8
">n";
draw_question($question_id);
echo "<input type="hidden
" name="question_number
" value="$question_number" />n";
echo "<input type="hidden
" name="question_id
" value="$question_id" />n";
// show the previous button unless we're on the first question
if ($question_id != $test_session->questions[0]) {
echo "<input type="submit
" name="prev" value="<<
{$strings['TEST_PREVIOUS_QUESTION']}" />n";
}
// show the next button unless we're on the last question
if ($question_id != $test_session->questions[$question_count]) {
echo "<input type="submit
" name="next" value="{$strings['TEST_NEXT_QUESTION']} >>
" />n";
}
echo "<input type="submit
" name="submit
" value="{$strings['TEST_SUBMIT']}" />n";
require_once('./include/footer.inc.php');
}
?>