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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
|
# -*- perl -*-
#
# Copyright (C) 2011-2021 Alexis Bienvenüe <paamc@passoire.fr>
#
# This file is part of Auto-Multiple-Choice
#
# Auto-Multiple-Choice 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.
#
# Auto-Multiple-Choice 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 Auto-Multiple-Choice. If not, see
# <http://www.gnu.org/licenses/>.
use warnings;
use 5.012;
package AMC::DataModule::layout;
# AMC layout data management.
# This module is used to store (in a SQLite database) and handle all
# pages layouts: locations of all boxes, name field, marks on the
# pages.
# All coordinates are given in pixels, with (0,0)=TopLeft.
# TABLES:
#
# layout_page lists pages from the subject, with the following data:
#
# * student is the student number
#
# * page is the page number from the student copy (beginning from 1
# for each student)
#
# * checksum is a number that is used to check that the student and
# page numbers are properly recognized frm the scan
#
# * sourceid is an ID to get from table source the source information
#
# * subjectpage is the page number from the subject.pdf file
# containing all subjects
#
# * dpi is the DPI resolution of the page
#
# * height,width are the page dimensions in pixels
#
# * markdiameter is the diameter of the four marks in the corners, in pixels
#
# layout_mark lists the marks positions on all the pages:
#
# * student,page identifies the page
#
# * corner is the corner number, from 1..4
# (TopLeft=1, TopRight=2, BottomRight=3, BottomLeft=4)
#
# * x,y are the mark center coordinates (in pixels, (0,0)=TopLeft)
#
# layout_namefield lists the name fields on the pages:
#
# * student,page identifies the page
#
# * xmin,xmax,ymin,ymax give the box around the name field
#
# layout_box lists all the boxes to be ticked (and other
# question/answer-related zones in the subject) on all the pages:
#
# * student,page identifies the page
#
# * role determines the role of this box (see BOX_ROLE_* constants below)
#
# * question is the question number. This is NOT the question number
# that is printed on the question paper, but an internal question
# number associated with question identifier from the LaTeX file
# (strings used as the first argument of the \begin{question} or
# \begin{questionmult} environment) as in table layout_question (see
# next)
#
# * answer is the answer number for this question
#
# * xmin,xmax,ymin,ymax give the box coordinates
#
# * flags is an integer that contains the flags from BOX_FLAGS_* (see
# below)
#
# * char is the character associated with the box (written inside or
# beside the box)
#
# layout_digit lists all the binary boxes to read student/page number
# and checksum from the scans (boxes white for digit 0, black for
# digit 1):
#
# * student,page identifies the page
#
# * numberid is the ID of the number to be read (1=student number,
# 2=page number, 3=checksum)
#
# * digitid is the digit ID (1 is the most significant bit)
#
# * xmin,xmax,ymin,ymax give the box coordinates
#
# layout_source describes where are all these information computed
# from:
#
# * sourceid refers to the same field in the layout_page table
#
# * src describes the file from which layout is read
#
# * timestamp is the time when the src file were read to populate the
# layout_* tables
#
# layout_question describes the questions:
#
# * question is the question ID (see explanation in layout_box)
#
# * name is the question identifier from the LaTeX file
#
# layout_association contains the pre-association data
#
# * student is the student sheet number
#
# * id is the association value (student id from the students list)
# for the corresponding student sheet
#
# * filename is the filename suggested when printing the students
# sheets to files.
#
# layout_char contains the chars written inside the answer boxes in catalog mode
#
# * question is the question ID (see explanation in layout_box)
#
# * answer is the answer number
#
# * char is the character written inside the box
use Exporter qw(import);
use constant {
BOX_FLAGS_DONTSCAN => 0x1,
BOX_FLAGS_DONTANNOTATE => 0x2,
BOX_FLAGS_SHAPE_OVAL => 0x10,
# Do not change these values as they are hard-coded elsewhere
BOX_ROLE_ANSWER => 1, # Boxes to be ticked by the student
BOX_ROLE_QUESTIONONLY =>
2, # In separate answer sheet mode, boxes in the question section
BOX_ROLE_SCORE => 100, # Zones to write scores when annotating answer sheets
BOX_ROLE_SCOREQUESTION =>
101, # The same but for question (in separate answer sheet mode)
BOX_ROLE_QUESTIONTEXT => 102,
BOX_ROLE_ANSWERTEXT => 103,
};
our @EXPORT_OK =
qw(BOX_FLAGS_DONTSCAN BOX_FLAGS_DONTANNOTATE BOX_FLAGS_SHAPE_OVAL BOX_ROLE_ANSWER BOX_ROLE_QUESTIONONLY BOX_ROLE_SCORE BOX_ROLE_SCOREQUESTION BOX_ROLE_QUESTIONTEXT BOX_ROLE_ANSWERTEXT);
our %EXPORT_TAGS = (
flags => [
qw/BOX_FLAGS_DONTSCAN BOX_FLAGS_DONTANNOTATE BOX_FLAGS_SHAPE_OVAL BOX_ROLE_ANSWER BOX_ROLE_QUESTIONONLY BOX_ROLE_SCORE BOX_ROLE_SCOREQUESTION BOX_ROLE_QUESTIONTEXT BOX_ROLE_ANSWERTEXT/
],
);
use AMC::Basic;
use AMC::DataModule;
use XML::Simple;
our @ISA = ("AMC::DataModule");
sub version_current {
return (8);
}
sub drop_box_table {
my ($self) = @_;
$self->sql_do( "DROP INDEX " . $self->index("index_box_studentpage") );
$self->sql_do( "DROP TABLE " . $self->table("box") );
}
sub create_box_table {
my ( $self, $tmp ) = @_;
$self->sql_do( "CREATE "
. ( $tmp ? "TEMPORARY " : "" )
. "TABLE IF NOT EXISTS "
. ( $tmp ? "box_tmp" : $self->table("box") )
. " (student INTEGER, page INTEGER, role INTEGER DEFAULT 1, question INTEGER, answer INTEGER, xmin REAL, xmax REAL, ymin REAL, ymax REAL, flags INTEGER DEFAULT 0, PRIMARY KEY (student,role,question,answer))"
);
if ( !$tmp ) {
$self->sql_do( "CREATE INDEX "
. $self->index("index_box_studentpage") . " ON "
. $self->table( "box", "self" )
. " (student,page,role)" );
}
}
sub version_upgrade {
my ( $self, $old_version ) = @_;
if ( $old_version == 0 ) {
# Upgrading from version 0 (empty database) to version 4 :
# creates all the tables.
debug "Creating layout tables...";
$self->sql_do( "CREATE TABLE IF NOT EXISTS "
. $self->table("page")
. " (student INTEGER, page INTEGER, checksum INTEGER, sourceid INTEGER, subjectpage INTEGER, dpi REAL, width REAL, height REAL, markdiameter REAL, PRIMARY KEY (student,page))"
);
$self->sql_do( "CREATE TABLE IF NOT EXISTS "
. $self->table("mark")
. " (student INTEGER, page INTEGER, corner INTEGER, x REAL, y REAL, PRIMARY KEY (student,page,corner))"
);
$self->sql_do( "CREATE TABLE IF NOT EXISTS "
. $self->table("namefield")
. " (student INTEGER, page INTEGER, xmin REAL, xmax REAL, ymin REAL, ymax REAL)"
);
$self->sql_do( "CREATE INDEX "
. $self->index("index_namefield") . " ON "
. $self->table( "namefield", "self" )
. " (student,page)" );
$self->create_box_table;
$self->sql_do( "CREATE TABLE IF NOT EXISTS "
. $self->table("digit")
. " (student INTEGER, page INTEGER, numberid INTEGER, digitid INTEGER, xmin REAL, xmax REAL, ymin REAL, ymax REAL, PRIMARY KEY(student,page,numberid,digitid))"
);
$self->sql_do( "CREATE TABLE IF NOT EXISTS "
. $self->table("source")
. " (sourceid INTEGER PRIMARY KEY, src TEXT, timestamp INTEGER)"
);
$self->sql_do( "CREATE TABLE IF NOT EXISTS "
. $self->table("question")
. " (question INTEGER PRIMARY KEY, name TEXT)" );
$self->sql_do( "CREATE TABLE IF NOT EXISTS "
. $self->table("association")
. " (student INTEGER PRIMARY KEY, id TEXT)" );
$self->populate_from_xml;
return (5);
}
if ( $old_version == 1 ) {
$self->sql_do( "ALTER TABLE "
. $self->table("box")
. " ADD COLUMN flags DEFAULT 0" );
return (2);
}
if ( $old_version == 2 ) {
$self->sql_do( "CREATE TABLE IF NOT EXISTS "
. $self->table("association")
. " (student INTEGER, id TEXT)" );
return (3);
}
if ( $old_version == 3 ) {
$self->progression( 'begin',
__("Building layout database indexes...") );
# replaces missing PRIMARY KEYS with INDEXs
$self->sql_do( "CREATE UNIQUE INDEX IF NOT EXISTS "
. $self->index("index_box") . " ON "
. $self->table( "box", "self" )
. " (student,question,answer)" );
$self->progression( 'fraction', 1 / 6 );
$self->sql_do( "CREATE INDEX IF NOT EXISTS "
. $self->index("index_box_studentpage") . " ON "
. $self->table( "box", "self" )
. " (student,page)" );
$self->progression( 'fraction', 1 / 6 );
$self->sql_do( "CREATE INDEX IF NOT EXISTS "
. $self->index("index_namefield") . " ON "
. $self->table( "namefield", "self" )
. " (student,page)" );
$self->progression( 'fraction', 1 / 6 );
$self->sql_do( "CREATE UNIQUE INDEX IF NOT EXISTS "
. $self->index("index_digit") . " ON "
. $self->table( "digit", "self" )
. " (student,page,numberid,digitid)" );
$self->progression( 'fraction', 1 / 6 );
$self->sql_do( "CREATE UNIQUE INDEX IF NOT EXISTS "
. $self->index("index_mark") . " ON "
. $self->table( "mark", "self" )
. " (student,page,corner)" );
$self->progression( 'fraction', 1 / 6 );
$self->sql_do( "CREATE INDEX IF NOT EXISTS "
. $self->index("index_association") . " ON "
. $self->table( "association", "self" )
. " (student)" );
$self->progression('end');
return (4);
}
if ( $old_version == 4 ) {
# To change the box table columns, primary key and index, use a
# temporary table to transfer all rows
$self->create_box_table(1);
$self->sql_do(
"INSERT INTO box_tmp (student,page,question,answer,xmin,xmax,ymin,ymax,flags)"
. " SELECT student,page,question,answer,xmin,xmax,ymin,ymax,flags FROM "
. $self->table("box") );
$self->drop_box_table;
$self->create_box_table;
$self->sql_do(
"INSERT INTO " . $self->table("box") . " SELECT * FROM box_tmp" );
$self->sql_do("DROP TABLE box_tmp");
return (5);
}
if ( $old_version == 5 ) {
$self->sql_do(
"ALTER TABLE " . $self->table("box") . " ADD COLUMN char TEXT" );
return (6);
}
if ( $old_version == 6 ) {
$self->sql_do( "CREATE TABLE IF NOT EXISTS "
. $self->table("char")
. " (question INTEGER, answer INTEGER, char TEXT)" );
$self->sql_do( "CREATE UNIQUE INDEX IF NOT EXISTS "
. $self->index("index_char") . " ON "
. $self->table( "char", "self" )
. " (question,answer)" );
return (7);
}
if ( $old_version == 7 ) {
$self->sql_do(
"ALTER TABLE " . $self->table("association") . " ADD COLUMN filename TEXT" );
return (8);
}
return ('');
}
# populate_from_xml read the old format XML files (if any) and inserts
# them in the new SQLite database
sub populate_from_xml {
my ($self) = @_;
my $mep = $self->{data}->directory;
$mep =~ s/\/[^\/]+\/?$/\/mep/;
if ( -d $mep ) {
$self->progression( 'begin',
__ "Fetching layout data from old format XML files..." );
opendir( DIR, $mep ) || die "can't opendir $mep: $!";
my @xmls = grep { /\.xml$/ && -s "$mep/" . $_ } readdir(DIR);
closedir DIR;
my $frac = 0;
for my $f (@xmls) {
my $lay = XMLin(
"$mep/" . $f,
ForceArray => 1,
KeepRoot => 1,
KeyAttr => ['id']
);
if ( $lay->{mep} ) {
my @st = stat( "$mep/" . $f );
debug "Populating data from $f...";
for my $laymep ( keys %{ $lay->{mep} } ) {
my $l = $lay->{mep}->{$laymep};
my @epc;
if ( $laymep =~ /^\+([0-9]+)\/([0-9]+)\/([0-9]+)\+$/ ) {
@epc = ( $1, $2, $3 );
$self->statement('NEWLayout')->execute(
@epc,
(
map { $l->{$_} }
(qw/page dpi tx ty diametremarque/)
),
$self->source_id( $l->{src}, $st[9] )
);
}
my @lid = ( $epc[0], $epc[1] );
for my $n ( @{ $l->{nom} } ) {
$self->statement('NEWNameField')
->execute( @lid,
map { $n->{$_} } (qw/xmin xmax ymin ymax/) );
}
for my $c ( @{ $l->{case} } ) {
$self->statement('NEWBox0')->execute(
@lid,
BOX_ROLE_ANSWER,
(
map { $c->{$_} }
(qw/question reponse xmin xmax ymin ymax/)
),
0
);
}
for my $d ( @{ $l->{chiffre} } ) {
$self->statement('NEWDigit')
->execute( @lid,
map { $d->{$_} } (qw/n i xmin xmax ymin ymax/) );
}
my $marks = $l->{coin};
for my $i ( keys %$marks ) {
$self->statement('NEWMark')
->execute( @lid, $i,
map { $marks->{$i}->{$_}->[0] } (qw/x y/) );
}
}
}
$frac++;
$self->progression( 'fraction', $frac / ( $#xmls + 1 ) );
}
$self->progression('end');
}
my $scoring_file = $self->{data}->directory;
$scoring_file =~ s:/[^/]+/?$:/bareme.xml:;
if ( -f $scoring_file ) {
my $xml = XMLin( $scoring_file, ForceArray => 1, KeyAttr => ['id'] );
my @s = grep { /^[0-9]+$/ } ( keys %{ $xml->{etudiant} } );
for my $i (@s) {
my $student = $xml->{etudiant}->{$i};
for my $question ( keys %{ $student->{question} } ) {
my $q = $student->{question}->{$question};
$self->question_name( $question, $q->{titre} );
}
}
}
}
# defines all the SQL statements that will be used
sub define_statements {
my ($self) = @_;
$self->{statements} = {
CLEARPAGE => { sql => "DELETE FROM ? WHERE student=? AND page=?" },
COUNT => { sql => "SELECT COUNT(*) FROM " . $self->table("page") },
StudentsCount => {
sql => "SELECT COUNT(*) FROM"
. " ( SELECT student FROM "
. $self->table("page")
. " GROUP BY student )"
},
NEWLayout => {
sql => "INSERT INTO "
. $self->table("page")
. " (student,page,checksum,subjectpage,dpi,width,height,markdiameter,sourceid)"
. " VALUES (?,?,?,?,?,?,?,?,?)"
},
NEWMark => {
sql => "INSERT INTO "
. $self->table("mark")
. " (student,page,corner,x,y) VALUES (?,?,?,?,?)"
},
'NEWBox0' => {
sql => "INSERT INTO "
. $self->table("box")
. " (student,page,role,question,answer,xmin,xmax,ymin,ymax,flags)"
. " VALUES (?,?,?,?,?,?,?,?,?,?)"
},
NEWBox => {
sql => "INSERT INTO "
. $self->table("box")
. " (student,page,role,question,answer,xmin,xmax,ymin,ymax,flags,char)"
. " VALUES (?,?,?,?,?,?,?,?,?,?,?)"
},
NEWDigit => {
sql => "INSERT INTO "
. $self->table("digit")
. " (student,page,numberid,digitid,xmin,xmax,ymin,ymax)"
. " VALUES (?,?,?,?,?,?,?,?)"
},
NEWNameField => {
sql => "INSERT INTO "
. $self->table("namefield")
. " (student,page,xmin,xmax,ymin,ymax) VALUES (?,?,?,?,?,?)"
},
NEWQuestion => {
sql => "INSERT INTO "
. $self->table("question")
. " (question,name) VALUES (?,?)"
},
NEWAssociation => {
sql => "INSERT INTO "
. $self->table("association")
. " (student,id,filename) VALUES (?,?,?)"
},
AssociationID => { sql => "SELECT id FROM "
. $self->table("association")
. " WHERE student=?" },
AssociationFilename => { sql => "SELECT filename FROM "
. $self->table("association")
. " WHERE student=?" },
IDS => {
sql => "SELECT student || ',' || page FROM "
. $self->table("page")
. " ORDER BY student,page"
},
FULLIDS => {
sql =>
"SELECT '+' || student || '/' || page || '/' || checksum || '+' FROM "
. $self->table("page")
. " ORDER BY student,page"
},
PAGES_STUDENT_all => {
sql => "SELECT page FROM "
. $self->table("page")
. " WHERE student=? ORDER BY page"
},
STUDENTS => {
sql => "SELECT student FROM "
. $self->table("page")
. " GROUP BY student ORDER BY student"
},
pageQuestionBoxes => {
sql => "SELECT question AS id_a,answer AS id_b,role"
. " FROM "
. $self->table("box")
. " WHERE role=2 AND student=? AND page=?"
},
Q_Flag => {
sql => "UPDATE "
. $self->table("box")
. " SET flags=flags|? WHERE student=? AND question=? AND role=?"
},
A_Flags => {
sql => "SELECT flags FROM "
. $self->table("box")
. " WHERE student=? AND question=? AND answer=? AND role=?"
},
A_All => {
sql => "SELECT * FROM "
. $self->table("box")
. " WHERE student=? AND question=? AND answer=? AND role=?"
},
PAGES_STUDENT_box => {
sql => "SELECT page FROM "
. $self->table("box")
. " WHERE student=? AND role=? GROUP BY student,page"
},
PAGES_Q_box => {
sql =>
"SELECT student,page,min(ymin) as miny,max(ymax) as maxy FROM "
. $self->table("box")
. " WHERE role=? AND question=? GROUP BY student,page"
},
PAGES_STUDENT_namefield => {
sql => "SELECT page FROM "
. $self->table("namefield")
. " WHERE student=? GROUP BY student,page"
},
PAGES_STUDENT_enter => {
sql => "SELECT page FROM ("
. "SELECT student,page FROM "
. $self->table("box")
. " WHERE role=1 UNION "
. "SELECT student,page FROM "
. $self->table("namefield")
. ") AS enter WHERE student=? GROUP BY student,page"
},
PAGES_enter => {
sql => "SELECT student,page FROM ("
. "SELECT student,page FROM "
. $self->table("box")
. " WHERE role=1 UNION "
. "SELECT student,page FROM "
. $self->table("namefield")
. ") AS enter GROUP BY student,page ORDER BY student,page"
},
MAX_enter => {
sql => "SELECT MAX(n) FROM"
. " ( SELECT COUNT(*) AS n FROM"
. " ( SELECT student,page FROM "
. $self->table("box")
. " WHERE role=1"
. " UNION SELECT student,page FROM "
. $self->table("namefield")
. " ) GROUP BY student )"
},
DEFECT_NO_BOX => {
sql => "SELECT student FROM (SELECT student FROM "
. $self->table("page")
. " GROUP BY student) AS list"
. " WHERE student>0 AND"
. " NOT EXISTS(SELECT * FROM "
. $self->table("box")
. " AS local WHERE role=1 AND"
. " local.student=list.student)"
},
DEFECT_NO_NAME => {
sql => "SELECT student FROM (SELECT student FROM "
. $self->table("page")
. " GROUP BY student) AS list"
. " WHERE student>0 AND"
. " NOT EXISTS(SELECT * FROM "
. $self->table("namefield")
. " AS local"
. " WHERE local.student=list.student)"
},
DEFECT_SEVERAL_NAMES => {
sql => "SELECT student FROM (SELECT student,COUNT(*) AS n FROM "
. $self->table("namefield")
. " GROUP BY student) AS counts WHERE n>1"
},
pageFilename => {
sql => "SELECT student || '-' || page || '-' || checksum FROM "
. $self->table("page")
. " WHERE student=? AND page=?"
},
pageSubjectPage => {
sql => "SELECT subjectpage FROM "
. $self->table("page")
. " WHERE student=? AND page=?"
},
students => {
sql => "SELECT student FROM "
. $self->table("page")
. " GROUP BY student"
},
DEFECT_OUT_OF_PAGE => {
sql => "SELECT student,page,count() as n FROM "
. " (SELECT b.student,b.page,xmin,xmax,ymin,ymax,width,height FROM "
. $self->table("box")
. " as b, "
. $self->table("page")
. " as p "
. " ON b.student==p.student AND b.page==p.page)"
. " WHERE (xmin<0 OR ymin<0 OR xmax>width OR ymax>height)"
. " GROUP BY student,page ORDER BY student,page"
},
subjectpageForStudent => {
sql => "SELECT MIN(subjectpage),MAX(subjectpage) FROM "
. $self->table("page")
. " WHERE student=?"
},
subjectpageForStudentA => {
sql => "SELECT MIN(p.subjectpage),MAX(p.subjectpage) FROM "
. $self->table("page") . " AS p"
. " ,( SELECT student,page FROM "
. $self->table("box")
. " WHERE role=1"
. " UNION"
. " SELECT student,page FROM "
. $self->table("namefield")
. " ) AS a"
. " ON p.student=a.student AND p.page=a.page"
. " WHERE p.student=?"
},
studentPage => {
sql => "SELECT student,page FROM "
. $self->table("page")
. " WHERE markdiameter>0"
. " LIMIT 1"
},
boxChar => {
sql => "SELECT char FROM "
. $self->table("box")
. " WHERE student=? AND question=? AND answer=? AND role=?"
},
boxPage => {
sql => "SELECT page FROM "
. $self->table("box")
. " WHERE student=? AND question=? AND answer=? AND role=?"
},
namefieldPage => {
sql => "SELECT page FROM "
. $self->table("namefield")
. " WHERE student=?"
},
dims => {
sql => "SELECT width,height,markdiameter,dpi FROM "
. $self->table("page")
. " WHERE student=? AND page=?"
},
mark => {
sql => "SELECT x,y FROM "
. $self->table("mark")
. " WHERE student=? AND page=? AND corner=?"
},
pageInfo => {
sql => "SELECT * FROM "
. $self->table("page")
. " WHERE student=? AND page=?"
},
studentPageInfo => {
sql => "SELECT * FROM "
. $self->table("page")
. " WHERE student=? ORDER BY page"
},
digitInfo => {
sql => "SELECT * FROM "
. $self->table("digit")
. " WHERE student=? AND page=?"
},
boxInfo => {
sql => "SELECT * FROM "
. $self->table("box")
. " WHERE student=? AND page=? AND role>=? AND role<=?"
},
namefieldInfo => {
sql => "SELECT * FROM "
. $self->table("namefield")
. " WHERE student=? AND page=?"
},
scoreZones => {
sql => "SELECT * FROM "
. $self->table("box")
. " WHERE student=? AND page=? AND question=?"
. " AND role>=? AND role<=?"
},
exists => {
sql => "SELECT COUNT(*) FROM "
. $self->table("page")
. " WHERE student=? AND page=? AND checksum=?"
},
questionName => {
sql => "SELECT name FROM "
. $self->table("question")
. " WHERE question=?"
},
sourceID => {
sql => "SELECT sourceid FROM "
. $self->table("source")
. " WHERE src=? AND timestamp=?"
},
NEWsource => {
sql => "INSERT INTO "
. $self->table("source")
. " (src,timestamp) VALUES(?,?)"
},
checkPosDigits => {
sql => "SELECT a.student AS student_a,b.student AS student_b,"
. " a.page AS page_a, b.page AS page_b,* FROM"
. " (SELECT * FROM"
. " (SELECT * FROM "
. $self->table("digit")
. " ORDER BY student DESC,page DESC)"
. " GROUP BY numberid,digitid) AS a," . " "
. $self->table("digit") . " AS b"
. " ON a.digitid=b.digitid AND a.numberid=b.numberid"
. " AND (abs(a.xmin-b.xmin)>(?+0) OR abs(a.xmax-b.xmax)>(?+0)"
. " OR abs(a.ymin-b.ymin)>(?+0) OR abs(a.ymax-b.ymax)>(?+0))"
. " LIMIT 1"
},
checkPosMarks => {
sql => "SELECT a.student AS student_a,b.student AS student_b,"
. " a.page AS page_a, b.page AS page_b,* FROM"
. " (SELECT * FROM"
. " (SELECT * FROM "
. $self->table("mark")
. " ORDER BY student DESC,page DESC)"
. " GROUP BY corner) AS a," . " "
. $self->table("mark") . " AS b"
. " ON a.corner=b.corner"
. " AND (abs(a.x-b.x)>(?+0) OR abs(a.y-b.y)>(?+0))"
. " LIMIT 1"
},
AssocNumber =>
{ sql => "SELECT COUNT(*) FROM " . $self->table("association") },
orientation => {
sql => "SELECT MIN(ratio) AS minratio,"
. " MAX(ratio) AS maxratio FROM "
. " (SELECT CASE WHEN ABS(width)<1 THEN 1"
. " ELSE height/width END"
. " AS ratio"
. " FROM "
. $self->table("page") . " )"
},
MapQuestionPage => {
sql => "SELECT student, page, question "
. " FROM "
. $self->table("box")
. " WHERE answer=1"
},
QuestionsList => { sql => "SELECT * FROM " . $self->table("question") },
CharClear => { sql => "DELETE FROM " . $self->table("char") },
CharSet => {
sql => "INSERT OR IGNORE INTO "
. $self->table("char")
. " (question,answer,char) VALUES (?,?,?)"
},
CharGet => {
sql => "SELECT char FROM "
. $self->table("char")
. " WHERE question=? AND answer=?"
},
CharNb => { sql => "SELECT COUNT(*) FROM " . $self->table("char") },
};
}
# clear_page_layout($student,$page) clears all the layout data for a
# given page
sub clear_page_layout {
my ( $self, $student, $page ) = @_;
for my $t (qw/page box namefield digit/) {
$self->statement('CLEARPAGE')
->execute( $self->table($t), $student, $page );
}
}
# random_studentPage returns an existing student,page couple
sub random_studentPage {
my ($self) = @_;
return ( $self->dbh->selectrow_array( $self->statement('studentPage') ) );
}
# exists returns the number of pages with coresponding student, page
# and checksum. The result should be 0 (no such page in the subject)
# or 1.
sub exists {
my ( $self, $student, $page, $checksum ) = @_;
return (
$self->sql_single(
$self->statement('exists'),
$student, $page, $checksum
)
);
}
# dims($student,$page) returns a (width,height,markdiameter) array for the given
# (student,page) page.
sub dims {
my ( $self, $student, $page ) = @_;
return (
$self->dbh->selectrow_array(
$self->statement('dims'),
{}, $student, $page
)
);
}
# all_marks returns x,y coordinates for the four corner marks on the
# requested page: (x1,y1,x2,y2,x3,y3,x4,y4)
sub all_marks {
my ( $self, $student, $page ) = @_;
my @r = ();
for my $corner ( 1 .. 4 ) {
push @r,
$self->dbh->selectrow_array( $self->statement('mark'),
{}, $student, $page, $corner );
}
return (@r);
}
# page_count returns the number of pages
sub pages_count {
my ($self) = @_;
return ( $self->sql_single( $self->statement('COUNT') ) );
}
# page_count returns the number of different students
sub students_count {
my ($self) = @_;
return ( $self->sql_single( $self->statement('StudentsCount') ) );
}
# ids returns student,page string for all pages
sub ids {
my ($self) = @_;
return ( $self->sql_list( $self->statement('IDS') ) );
}
# full_ids returns +student/page/checksum+ string for all pages
sub full_ids {
my ($self) = @_;
return ( $self->sql_list( $self->statement('FULLIDS') ) );
}
#Return list of student, page, question
sub student_question_page {
my ($self) = @_;
my @list =
@{ $self->dbh->selectall_arrayref( $self->statement('MapQuestionPage') )
};
return (@list);
}
# page_info returns a HASH reference containing all fields in the
# layout_page row corresponding to the student,page page.
sub page_info {
my ( $self, $student, $page ) = @_;
return (
$self->dbh->selectrow_hashref(
$self->statement('pageInfo'),
{}, $student, $page
)
);
}
# type_info($type,$student,$page,$role) returns an array of HASH
# references containing all fiels in the $type table ($type may equal
# digit, box or namefield) corresponding to the $student,$page
# page. The $role argument (which defaults to BOX_ROLE_ANSWER) is only
# needed when $type is 'box' (or you can use $type='questionbox' with
# no $role).
sub type_info {
my ( $self, $type, $student, $page, $role, $rolemax ) = @_;
my @args = ( $student, $page );
if ( $type eq 'questionbox' ) {
$type = 'box';
$role = BOX_ROLE_QUESTIONONLY;
}
if ( $type eq 'scorezone' ) {
$type = 'box';
$role = BOX_ROLE_SCORE;
$rolemax = BOX_ROLE_SCOREQUESTION;
}
if ( $type eq 'text' ) {
$type = 'box';
$role = BOX_ROLE_QUESTIONTEXT;
$rolemax = BOX_ROLE_ANSWERTEXT;
}
if ( $type eq 'box' ) {
$role = BOX_ROLE_ANSWER if ( !$role );
$rolemax = $role if ( !$rolemax );
push @args, $role, $rolemax;
}
return (
@{
$self->dbh->selectall_arrayref( $self->statement( $type . 'Info' ),
{ Slice => {} }, @args )
}
);
}
# score_zones($student,$page,$question) returns an array of HASH
# references containing all the rows in the box table corresponding to
# the given parameters.
sub score_zones {
my ( $self, $student, $page, $question ) = @_;
return (
@{
$self->dbh->selectall_arrayref(
$self->statement('scoreZones'), { Slice => {} },
$student, $page,
$question, BOX_ROLE_SCORE,
BOX_ROLE_SCOREQUESTION
)
}
);
}
# pages_for_student($student,[%options]) returns a list of the page
# numbers on the subject (starting from 1 for each student) for this
# student. With 'select'=>'box' as an option, restricts to the pages
# where at least one box to be filled is present. With
# 'select'=>'namefield', restricts to the pages where the name field
# is. With 'select'=>'enter', restricts to the pages where the
# students has to write something (where there are boxes or name
# field).
sub pages_for_student {
my ( $self, $student, %oo ) = @_;
$oo{select} = 'all' if ( !$oo{select} );
my @args = ($student);
if ( $oo{select} eq 'box' ) {
$oo{role} = BOX_ROLE_ANSWER if ( !$oo{role} );
push @args, $oo{role};
}
return (
$self->sql_list(
$self->statement( 'PAGES_STUDENT_' . $oo{select} ), @args
)
);
}
# pages_for_question($question_id) returns all the pages (in the form
# {student=>xx,page=>xx}) where one can find boxes to be checked for
# this particular question.
sub pages_for_question {
my ( $self, $question_id ) = @_;
return (
@{
$self->dbh->selectall_arrayref(
$self->statement('PAGES_Q_box'), { Slice => {} },
BOX_ROLE_ANSWER, $question_id
)
}
);
}
# pages_info_for_student($student,[%options]) returns a list of
# hashrefs with all data from the table page for each page concerning
# student $student. With option enter_tag=>1, adds enter=>1 for each
# page where something has to be entered by the student (boxes or
# namefield).
sub pages_info_for_student {
my ( $self, $student, %oo ) = @_;
my $r = $self->dbh->selectall_arrayref( $self->statement('studentPageInfo'),
{ Slice => {} }, $student );
if ( $oo{enter_tag} ) {
my %enter_pages = map { $_ => 1 } (
$self->pages_for_student(
$student,
select => 'enter',
role => $oo{role}
)
);
for my $p (@$r) {
$p->{enter} = 1 if ( $enter_pages{ $p->{page} } );
}
}
return (@$r);
}
# students returns the list of the students numbers.
sub students {
my ($self) = @_;
return ( $self->sql_list( $self->statement('STUDENTS') ) );
}
# defects($delta) returns a hash of the defects found in the subject:
#
# * {'NO_BOX} is a pointer on an array containing all the student
# numbers for which there is no box to be filled in the subject
#
# * {NO_NAME} is a pointer on an array containing all the student
# numbers for which there is no name field
#
# * {SEVERAL_NAMES} is a pointer on an array containing all the student
# numbers for which there is more than one name field
#
# * {OUT_OF_PAGE} is a pointer on a array containing all pages where
# some box is outside the page.
#
# * {DIFFERENT_POSITIONS} is a pointer to a hash returned by
# check_positions($delta)
sub defects {
my ( $self, $delta ) = @_;
$delta = 0.1 if ( !defined($delta) );
my %r = ();
my @tests = (qw/NO_NAME SEVERAL_NAMES/);
push @tests, 'NO_BOX'
if ( $self->variable('build:extractonly') ne 'yes' );
for my $type (@tests) {
my @s = $self->sql_list( $self->statement( 'DEFECT_' . $type ) );
$r{$type} = [@s] if (@s);
}
for my $type (qw/OUT_OF_PAGE/) {
my @s = @{
$self->dbh->selectall_arrayref(
$self->statement( 'DEFECT_' . $type ),
{ Slice => {} } )
};
$r{$type} = [@s] if (@s);
}
my $pos = $self->check_positions($delta);
$r{DIFFERENT_POSITIONS} = $pos if ($pos);
return (%r);
}
# source_id($src,$timestamp) looks in the table source if a row with
# values ($src,$timestamp) already exists. If it does, source_id
# returns the sourceid value for this row. If not, it creates a row
# with these values and returns the primary key sourceid for this new
# row.
sub source_id {
my ( $self, $src, $timestamp ) = @_;
my $sid =
$self->sql_single( $self->statement('sourceID'), $src, $timestamp );
if ($sid) {
return ($sid);
} else {
$self->statement('NEWsource')->execute( $src, $timestamp );
return ( $self->dbh->sqlite_last_insert_rowid() );
}
}
# question_name($question) returns the question name for question
# number $question
#
# question_name($question,$name) sets the question name (identifier
# string from LaTeX file) for question number $question.
sub question_name {
my ( $self, $question, $name ) = @_;
if ( defined($name) ) {
my $n = $self->question_name($question);
if ($n) {
if ( $n ne $name ) {
debug
"ERROR: question ID=$question with different names ($n/$name)";
}
} else {
$self->statement('NEWQuestion')->execute( $question, $name );
}
} else {
return (
$self->sql_single( $self->statement('questionName'), $question ) );
}
}
# clear_all clears all the layout data tables.
sub clear_mep {
my ($self) = @_;
for my $t (qw/page mark namefield box digit source question association/) {
$self->sql_do( "DELETE FROM " . $self->table($t) );
}
}
sub clear_all {
my ($self) = @_;
$self->clear_mep;
$self->clear_char;
}
# get_pages returns a reference to an array like
# [[student_1,page_1],[student_2,page_2]] listing the pages where
# something has to be entered by the students (either answers boxes or
# name field).
sub get_pages {
my ( $self, $add_copy ) = @_;
my $r = $self->dbh->selectall_arrayref( $self->statement('PAGES_enter') );
if ( defined($add_copy) ) {
for (@$r) { push @{$_}, 0 }
}
return $r;
}
# check_positions($delta) checks if all pages has the same positions
# for marks and binary digits boxes. If this is the case (this SHOULD
# allways be the case), check_positions returns undef. If not,
# check_positions returns a hashref
# {student_a=>S1,page_a=>P1,student_b=>S2,page_b=>P2} showing an
# example for which (S1,P1) has not the same positions as (S2,P2)
# (with difference over $delta for at least one coordinate).
sub check_positions {
my ( $self, $delta ) = @_;
my $r = $self->dbh->selectrow_hashref( $self->statement('checkPosDigits'),
{}, $delta, $delta, $delta, $delta );
return ($r) if ($r);
$r = $self->dbh->selectrow_hashref( $self->statement('checkPosMarks'),
{}, $delta, $delta );
return ($r);
}
# max_enter() returns the maximum of enter pages (pages where the
# students are to write something: either boxes to tick either name
# field) per student.
sub max_enter {
my ($self) = @_;
return ( $self->sql_single( $self->statement("MAX_enter") ) );
}
# add_question_flag($student,$question,$flag) adds the flag to all
# answers boxes for a particular student and question.
sub add_question_flag {
my ( $self, $student, $question, $role, $flag ) = @_;
$self->statement('Q_Flag')->execute( $flag, $student, $question, $role );
}
# get_box_flags($student,$question,$answer) returns the flags for the
# corresponding box.
sub get_box_flags {
my ( $self, $student, $question, $answer, $role ) = @_;
return (
$self->sql_single(
$self->statement('A_Flags'),
$student, $question, $answer, $role
)
);
}
# get_box_info($student,$question,$answer) returns all data from table
# box for the corresponding box.
sub get_box_info {
my ( $self, $student, $question, $answer, $role ) = @_;
return (
$self->dbh->selectrow_hashref(
$self->statement('A_All'), {},
$student, $question,
$answer, $role
)
);
}
# new_association($student,$id) adds an association to the
# pre-association data (associations made before the exam)
sub new_association {
my ( $self, $student, $id, $filename ) = @_;
$self->statement('NEWAssociation')->execute( $student, $id, $filename );
}
# pre_association() returns the number of pre-asociations
sub pre_association {
my ($self) = @_;
return ( $self->sql_single( $self->statement("AssocNumber") ) );
}
# get_associated_filename(student) returns the filename suggested by
# pre-association for this student sheet number
sub get_associated_filename {
my ( $self, $student ) = @_;
return (
$self->sql_single( $self->statement("AssociationFilename"), $student )
);
}
# get_associated_id(student) returns the student ID associated by
# pre-association for this student sheet number
sub get_associated_id {
my ( $self, $student ) = @_;
return (
$self->sql_single( $self->statement("AssociationID"), $student )
);
}
# orientation() returns "portrait" or "landscape" if all pages have
# the same orientation, and "" otherwise. In array context,
# orientation() returns the min and max height/width ratio for all
# pages.
sub orientation {
my ($self) = @_;
my @ors = $self->sql_row( $self->statement("orientation") );
if (wantarray) {
return (@ors);
} else {
if ( $ors[0] > 1.1 ) {
return ("portrait");
} elsif ( $ors[1] < 0.9 ) {
return ("landscape");
} else {
return ("");
}
}
}
# code_digit_pattern() return the regular expression that can be used to
# detect a code digit.
sub code_digit_pattern {
my ($self) = @_;
my $type = $self->variable("build:codedigit");
if ( $type && $type eq 'squarebrackets' ) {
# 'codename[N]'
return ("\\[(\\d+)\\]");
} else {
# with older AMC version, look for 'codename.N' instead of
# 'codename[N]'
return ("\\.(\\d+)");
}
}
# Get the page where we can find the box for a particular student,
# question, box
sub box_page {
my ( $self, $student, $question, $answer, $role ) = @_;
$role = BOX_ROLE_ANSWER if ( !$role );
return (
$self->sql_single(
$self->statement('boxPage'),
$student, $question, $answer, $role
)
);
}
# Get the chararacter written inside or beside a box
sub box_char {
my ( $self, $student, $question, $answer, $role ) = @_;
$role = BOX_ROLE_ANSWER if ( !$role );
return (
$self->sql_single(
$self->statement('boxChar'),
$student, $question, $answer, $role
)
);
}
# Get the page where we can find the namefield for a particular student
sub namefield_page {
my ( $self, $student ) = @_;
return ( $self->sql_single( $self->statement('namefieldPage'), $student ) );
}
# Get the list of all questions
sub questions_list {
my ($self) = @_;
return @{
$self->dbh->selectall_arrayref( $self->statement('QuestionsList'),
{ Slice => {} } )
};
}
# clear_char() clears the char table
sub clear_char {
my ($self) = @_;
$self->statement("CharClear")->execute();
}
# char($question, $answer [,$char] ) gets or sets the character
# associated with a particular answer.
sub char {
my ( $self, $question, $answer, $char ) = @_;
if ( defined($char) ) {
$self->statement("CharSet")->execute( $question, $answer, $char );
} else {
return (
$self->sql_single(
$self->statement('CharGet'), $question, $answer
)
);
}
}
# nb_chars_transaction() returns the number of characters stored in
# the layout_char table. This is often used to know if the table is
# empty or not.
sub nb_chars_transaction {
my ($self) = @_;
$self->begin_read_transaction('nbCh');
my $n = $self->sql_single( $self->statement('CharNb') );
$self->end_transaction('nbCh');
return ($n);
}
1;
|