5
5
use DBD::Pg;
6
6
use PDF::Reuse;
7
7
use utf8;
8
+ no warnings ' utf8' ;
9
+ use Data::Dumper;
8
10
9
11
#
10
12
# ./generate-tests.pl [variant]
@@ -16,23 +18,25 @@ sub logger {
16
18
print ' Error: ' . $_ [0] ." \n " if defined ($_ [0]);
17
19
}
18
20
21
+ # define which test we are going to generate, first or second
22
+ my $first_test = 1;
19
23
my $debug = 0;
20
24
my $pguser = ' smalusr' ;
21
25
my $pgpass = ' kokoshka' ;
22
26
my $pgdb = ' DBI:Pg:database=smal;host=localhost;port=5432' ;
23
27
my $pgconn = DBI-> connect_cached( $pgdb , $pguser , $pgpass , { PrintError => 1, AutoCommit => 1 }) or die (" $DBI::errstr \n " );
24
28
my $schema = ' public' ;
25
- # define which test we are going to generate, first or second
26
- my $first_test = 0;
27
29
my %ques = ();
28
30
my $qcount = 0;
29
31
my $question_count = 1;
30
32
my $max_questions = 50;
31
33
my $variant = 1;
32
34
$variant = $ARGV [0] if (defined ($ARGV [0]) && $ARGV [0] =~ / ^[0-9]+$ / );
33
- my $filename = " test1-variant$variant " ;
35
+ my $filename = $first_test ? " test1-variant $variant " : " test2 -variant$variant " ;
34
36
my $first_qid ;
35
37
my ($sec ,$min ,$hour ,$mday ,$mon ,$year ,$wday ,$yday ,$isdst ) = localtime ();
38
+ # define which test we are going to generate, first or second
39
+ my $first_test = 1;
36
40
37
41
$year = $year +1900;
38
42
@@ -57,9 +61,16 @@ sub logger {
57
61
}
58
62
my $fqid = $first_qid -> execute();
59
63
print " First quid: $fqid \n " if $debug ;
60
-
61
- # my $get_question = $pgconn->prepare(sprintf('SELECT question FROM "%s".questions WHERE id = ? OFFSET random()*%d LIMIT 1', $schema, $qcount))
62
- my $get_question = $pgconn -> prepare(sprintf (' SELECT question FROM "%s".questions WHERE id = ? LIMIT 1' , $schema ))
64
+ my %all_questions = ();
65
+ my $max_q = 0;
66
+ my $q_ids = $pgconn -> prepare(sprintf (' SELECT id,first_test FROM "%s".questions ORDER BY id' , $schema ))
67
+ or logger($DBI::errstr );
68
+ $q_ids -> execute();
69
+ foreach my $q_id (@{$q_ids -> fetchall_arrayref}) {
70
+ $all_questions {$$q_id [0]} = $$q_id [1];
71
+ $max_q = $$q_id [0];
72
+ }
73
+ my $get_question = $pgconn -> prepare(sprintf (' SELECT question FROM "%s".questions WHERE id = ? AND %s first_test LIMIT 1' , $schema , $first_test ? ' ' : ' NOT' ))
63
74
or logger($DBI::errstr );
64
75
my $right_asnwares = $pgconn -> prepare(sprintf (' SELECT answer FROM "%s".right_answers WHERE q_id = ?' , $schema ))
65
76
or logger($DBI::errstr );
@@ -80,17 +91,27 @@ sub logger {
80
91
my $id = $id_compensation + int (rand ($qcount ));
81
92
# generate random question positions
82
93
while ($question_count <= $max_questions ) {
83
- print " Num: $question_count " if $debug ;
94
+ printf " Num: %2d " , $question_count if $debug ;
84
95
my $rcount = 0;
85
- until (! exists $questions_check {$id } && ! exists $excluded {$id }) {
86
- $id = $id_compensation + int (rand ($qcount ));
87
- if ($rcount == 350) {
88
- print " Too random\n " if $debug ;
96
+ while (1) {
97
+ $id = $id_compensation + int (rand ($max_q ));
98
+ next if (exists $questions_check {$id });
99
+ if ($first_test and $all_questions {$id }) {
100
+ $questions_check {$id } = 1;
89
101
last ;
90
102
}
103
+ if (!$first_test and !$all_questions {$id }) {
104
+ $questions_check {$id } = 1;
105
+ last ;
106
+ }
107
+ if ($rcount > 350) {
108
+ # We can't find a random question id, that is also part of the QIDs from the DB.
109
+ print " - Too random - " if $debug ;
110
+ next ;
111
+ }
91
112
$rcount ++;
92
113
}
93
- print " q_id: $id \n " if $debug ;
114
+ printf " q_id: %3d %d \n " , $id , $all_questions { $id } if $debug ;
94
115
$questions_check {$id } = 1;
95
116
$ques {$question_count } = [ -1, -1, -1, -1, -1, -1 ];
96
117
$ques {$question_count }[6] = $id ;
@@ -99,25 +120,30 @@ sub logger {
99
120
$ques {$question_count }[0] = $location ;
100
121
$question_count ++;
101
122
}
102
- for (sort keys (%ques )) {print " $_ not defined\n " if (!defined ($ques {$_ }[0]));}
123
+
124
+ # Check if there are questions without correct answer location
125
+ if ($debug ) {
126
+ for (sort keys (%ques )) {print " $_ not defined\n " if (!defined ($ques {$_ }[0]));}
127
+ }
103
128
104
129
my $page_count = 1;
105
130
# Starting position on the page(after the answer boxes)
106
- my $last_line = 666 ;
131
+ my $last_line = 700 ;
107
132
108
133
sub page_check {
109
134
my $last_pos = $_ [0];
110
135
my $page_count = $_ [1];
111
- print ' Page: ' .${$page_count} .' Line: ' .${$last_pos} ." \n " if $debug ;
136
+ # print 'Page: '.${$page_count}.' Line: '.${$last_pos}."\n" if $debug;
112
137
${$last_pos} -= 16;
113
138
if (${$last_pos} < 40) {
114
139
if (${$page_count} == 1) {
115
140
prAdd(" 0.0 0.0 0.0 RG\n " );
116
141
prAdd(" 9.0 9.0 9.0 rg\n " );
142
+ # Generate the answer boxes
117
143
for my $l (1..25) {
118
144
my $pos = ($l * 18) + 20;
119
- prAdd(" $pos 711 18 20 re\n " );
120
- prAdd(" $pos 676 18 20 re\n " );
145
+ prAdd(" $pos 744 18 20 re\n " );
146
+ prAdd(" $pos 712 18 20 re\n " );
121
147
}
122
148
prAdd(" B\n " );
123
149
}
@@ -132,18 +158,18 @@ sub page_check {
132
158
prFile(" $filename .pdf" );
133
159
prTTFont(' /usr/share/fonts/arial.ttf' );
134
160
prText(35,800," Linux System & Network Administration" );
135
- prText(340,800," TEST $test $year " );
161
+ prText(340,800," TEST $test $year variant: $variant " );
136
162
prText(532,800," Page $page_count " );
137
- prText(35,780," Name: ______________________________________________________________________ " );
138
- prText(35,760, " SoftUni username: ______________________________ " );
139
- prText(510,760, " Variant: $variant " );
163
+ prText(35,780," Name: _______________________________________________________ FN:_______________ " );
164
+
165
+ # Generate the answer numbers
140
166
for my $l (1..25) {
141
167
my $pos = ($l * 18) + 24;
142
168
if ($l > 9) {
143
169
$pos = ($l * 18) + 22;
144
170
}
145
- prText($pos ,734 ,$l );
146
- prText($pos -1,699 ,$l +25);
171
+ prText($pos ,766 ,$l );
172
+ prText($pos -1,734 ,$l +25);
147
173
}
148
174
149
175
prFontSize(' 10' );
@@ -218,25 +244,24 @@ sub page_check {
218
244
if ($q_len > 92) {
219
245
my @lines = split /\n/, $ques {$qid }[5];
220
246
for (my $l =0;$l <=$#lines ;$l ++) {
221
- $lines [$l ] =~ s /\n / / g ;
222
247
if ($l ==0) {
223
- prText(35,$last_line ,sprintf (' %d. %s ' , $qid , $lines [$l ]));
248
+ prText(35,$last_line ,sprintf (' %d. %ls ' , $qid , $lines [$l ]));
224
249
} else {
225
250
prText(35,$last_line ,$lines [$l ]) if ($lines [$l ] !~ / ^[\s |\n ]*$ / );
226
251
}
227
252
page_check(\$last_line ,\$page_count );
228
253
}
229
254
} else {
230
- prText(35,$last_line ,sprintf (' %d. %s ' , $qid , $ques {$qid }[5]));
255
+ prText(35,$last_line ,sprintf (' %d. %ls ' , $qid , $ques {$qid }[5]));
231
256
}
232
257
233
258
my %a_names = ( 1 => ' a' , 2 => ' b' , 3 => ' c' , 4 => ' d' );
234
259
$entry_count = 1;
235
260
for (my $c =1;$c <5;$c ++) {
236
261
if (defined ($ques {$qid }[$c ]) && $ques {$qid }[$c ] ne ' -1' ) {
237
- printf (" %s ) %s \n " , $a_names {$entry_count }, $ques {$qid }[$c ]) if $debug ;
262
+ printf (" %ls ) %ls \n " , $a_names {$entry_count }, $ques {$qid }[$c ]) if $debug ;
238
263
page_check(\$last_line ,\$page_count );
239
- prText(35,$last_line ,sprintf (" %s ) %s " , $a_names {$entry_count }, $ques {$qid }[$c ]));
264
+ prText(35,$last_line ,sprintf (" %ls ) %ls " , $a_names {$entry_count }, $ques {$qid }[$c ]));
240
265
$entry_count ++;
241
266
}
242
267
}
@@ -256,15 +281,13 @@ sub page_check {
256
281
# print the right answares :)
257
282
$tcount = 1;
258
283
print " \n Num: \n " if $debug ;
259
- # prAdd("0.0 0.0 0.0 RG\n");
260
- # for (sort keys(%ques)) {
261
284
for (1..$max_questions ) {
262
285
print " $_ not defined\n " if (!defined ($ques {$_ }[0]) && $debug );
263
286
my $right = ' a' ;
264
287
$right = ' b' if ($ques {$_ }[0] == 2);
265
288
$right = ' c' if ($ques {$_ }[0] == 3);
266
289
$right = ' d' if ($ques {$_ }[0] == 4);
267
- printf (" %2d: %s \n " ,$tcount , $right ) if $debug ;
290
+ printf (" %2d: %ls \n " ,$tcount , $right ) if $debug ;
268
291
prAdd(" q 38 717 m 490 717 l S Q" );
269
292
prAdd(" q 38 698 m 490 698 l S Q" );
270
293
prAdd(" q 38 677 m 490 677 l S Q" );
0 commit comments