-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquiz.pl
300 lines (241 loc) · 6.54 KB
/
quiz.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/perl -T
=pod
=head1 NAME
quiz.pl - A CGI quiz program.
=head1 SYNOPSIS
http://www.server.com/cgi-bin/quiz.pl
=head1 DESCRIPTION
The I<quiz.pl> program is designed give the user a quiz.
The input this program is a series of question files in
the following format:
=question
<question page>
=answer value
<answer page>
=answer value
<answer page>
=right value
<answer page for the right answer>
The sections in this file are:
=over 4
=item B<=question>
The text (html format) for the question.
=item B<=answer> I<value>
This entry specifies an incorrect value. The text that follows
this entry is displayed when the user selects this (wrong) answer.
=item B<=right> I<right>
Same as B<=answer> except this answer is right
=back
=head1 SAMPLE INPUT FILE
=question
<HEAD><TITLE>Question 1</TITLE></HEAD>
<H1>Question 1:</H1>
<P>
What does the following regular expression mean:
<pre>
/\S+/
</pre>
<P>
<FORM ACTION="quiz.pl">
<P>
<INPUT TYPE="submit" NAME="answer" VALUE="1">
One or more spaces.<BR>
<INPUT TYPE="submit" NAME="answer" VALUE="2">
Zero or more spaces.<BR>
<INPUT TYPE="submit" NAME="answer" VALUE="3">
One or more non-space characters.<BR>
</FORM>
=answer 1
<HEAD><TITLE>Wrong</TITLE></HEAD>
<H1>Wrong</H1>
<P>
Lower case 's' (<code>\s</code>) is used to specify
spaces. The regular expression given uses an upper
case 'S'. (See <i>perldoc perlre</i> for a reference.)
=answer 2
<HEAD><TITLE>Wrong</TITLE></HEAD>
<H1>Wrong</H1>
<P>
The star character (<code>*</code>) denotes zero
or more characters. This expression uses the
plus (<code>+</code>) character.
(See <i>perldoc perlre</i> for a reference.)
=right 3
<HEAD><TITLE>Right</TITLE></HEAD>
<H1>Right</H1>
Go on to the next questions.
=head1 AUTHOR
Steve Oualline, E<lt>[email protected]<gt>.
=head1 COPYRIGHT
Copyright 2005 Steve Oualline.
This program is distributed under the GPL.
=cut
#
# File format
# =question
# <question page>
# =answer value
# <answer page>
# =answer value
# <answer page>
# =right value
# <answer page for the right answer>
#
use strict;
use warnings;
use CGI::Thin;
use CGI::Thin::Cookies;
use CGI::Carp;
use POSIX;
use HTML::Entities;
use Scalar::Util qw(tainted);
use Storable qw(retrieve nstore);
# Place the questions and session files are
# stored in
my $quiz_dir = "/var/quiz";
# The data from the form
my %cgi_data = Parse_CGI();
# Cookie information
my %cookies = Parse_Cookies();
# The weight from the cookie
my $session_cookie = $cookies{QUIZ};
my $session = undef; # The session name
# Taint checking and cleaning
if (defined($session_cookie) &&
($session_cookie =~ /^$quiz_dir\/session\/session.(\d+)$/)) {
$session_cookie =~ /(\d+)$/;
$session = "$quiz_dir/session/session.$1";
} else {
$session = undef;
}
if (! -f $session) {
$session = undef;
}
if (not defined ($session)) {
for (my $i = 0; ; $i++) {
# Generate a new session
$session = "$quiz_dir/session/session.$i";
if (! -f "$quiz_dir/session/session.$i") {
last;
}
}
}
# The cookie to send to the user
my $cookie;
$cookie = Set_Cookie(
NAME => "QUIZ", # Cookie's name
VALUE => $session, # Value for the cookie
EXPIRE => "+3h", # Keep cookie for 3 hours
);
print "$cookie";
print "Content-type: text/html\n";
print "\n";
my $session_info;
if (-f $session) {
$session_info = retrieve($session);
} else {
my @files = glob("$quiz_dir/questions/*");
$session_info->{files} = [@files];
$session_info->{mode} = 'question';
}
################################################################
# parse_file($file_name) -- Read and a parse a file
#
# Returns a hash containing the file information
################################################################
sub parse_file($)
{
my $file_name = shift;
open IN_FILE, "<$file_name" or
die("Unable to open $file_name");
my %file_info; # Information about the file
my $field; # Field we are defining
my $item = undef;# Item we are defining in the fields
while (my $line = <IN_FILE>) {
if ($line =~ /^=question/) {
$field = 'question';
$item = undef;
} elsif ($line =~ /=answer\s+(\S+)/) {
$field = 'answer';
$item = $1;
} elsif ($line =~ /=right\s+(\S+)/) {
$field = 'answer';
$item = $1;
$file_info{right} = $1;
} else {
if (defined($item)) {
$file_info{$field}->{$item} .= $line;
} else {
$file_info{$field} .= $line;
}
}
}
close (IN_FILE);
return (%file_info);
}
################################################################
# display_done -- Tell the user he's done.
################################################################
sub display_done()
{
$session_info->{mode} = 'done';
print <<EOF
<H1>Test Complete</H1>
<P>
Congratulations, you have finished the quiz.
EOF
#TODO: Need something here to go somewhere else
}
################################################################
# display_question -- Display the current question
################################################################
sub display_question()
{
if ($#{$session_info->{files}} == -1) {
display_done();
return;
}
# Information about the file
my %file_info = parse_file($session_info->{files}->[0]);
print $file_info{question};
$session_info->{mode} = 'answer';
}
################################################################
# display_answer -- Display the answer
################################################################
sub display_answer()
{
# The information from the question file
my %file_info = parse_file($session_info->{files}->[0]);
# The answer the user submitted
my $answer = $cgi_data{answer};
# Display the answer
if (defined($file_info{answer}->{$answer})) {
print $file_info{answer}->{$answer};
} else {
print "<H1>Internal error: Undefined answer $answer</H1>\n";
$answer = "";
}
if ($answer eq $file_info{right}) {
shift @{$session_info->{files}};
} else {
my $last = @{$session_info->{files}};
push(@{$session_info->{files}}, $last);
}
$session_info->{mode} = 'question';
print <<EOF ;
<FORM ACTION="quiz.pl">
<INPUT TYPE="submit" NAME="next" VALUE="next">
</FORM>
EOF
}
if ($session_info->{mode} eq 'answer') {
display_answer();
} elsif ($session_info->{mode} eq 'question') {
display_question();
} else {
display_done();
}
# Store the data for laster use
nstore($session_info, $session);