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
|
#! @/PERLPATH/@
#
# Copyright (C) 2017-2025 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;
use AMC::Basic;
use AMC::Gui::Avancement;
use AMC::Data;
use AMC::DataModule::capture qw/ :zone /;
use Encode;
use Getopt::Long;
use Data::Dumper;
use_gettext;
my $list_file = '';
my $progress_id = '';
my $data_dir = '';
my $multiple = '';
my $password = '';
GetProjectOptions(
"list=s" => \$list_file,
"progression-id=s" => \$progress_id,
":auto_capture_mode|multiple!" => \$multiple,
":data:dir|data=s" => \$data_dir,
":pdf_password|password=s" => \$password,
);
die "data directory not found: $data_dir" if ( !-d $data_dir );
my $p = AMC::Gui::Avancement::new( 1, id => $progress_id )
if ($progress_id);
my @forms = (@ARGV);
if ( -f $list_file ) {
open( LIST, $list_file );
while (<LIST>) {
chomp;
push @forms, $_;
}
close(LIST);
}
my $data = AMC::Data->new($data_dir);
my $layout = $data->module('layout');
my $capture = $data->module('capture');
sub value_is_true {
my ($s) = @_;
return ( $s =~ /yes/i || $s =~ /^on/i );
}
my $copy_id = {};
my @pages = ();
sub clear_copy_id {
$copy_id = {};
@pages = ();
}
sub get_copy_id {
my ( $student_id, $page ) = @_;
my $key = "$student_id/$page";
if ( !exists( $copy_id->{$key} ) ) {
if ($multiple) {
$copy_id->{$key} = $capture->new_page_copy( $student_id, $page );
} else {
$copy_id->{$key} = 0;
}
push @pages,
{ student => $student_id, page => $page, copy => $copy_id->{$key} };
if (
$capture->set_page_auto(
undef, $student_id, $page, $copy_id->{$key},
time(), undef, undef, undef,
undef, undef, undef, 0
)
)
{
debug "Overwritten page data for [FORM] "
. pageids_string( $student_id, $page, $copy_id->{$key} );
$capture->tag_overwritten( $student_id, $page, $copy_id->{$key} );
print "VAR+: overwritten\n";
}
}
return ( $copy_id->{$key} );
}
sub get_fields_from_pdf {
my ($pdf_file) = @_;
open( FORM, "-|", "auto-multiple-choice", "pdfformfields", $pdf_file,
$password )
or die "Error with pdfformfields: $!";
my @fields = ();
my $field = {};
while (<FORM>) {
chomp;
if (/^---/) {
push @fields, $field if (%$field);
$field = {};
}
if (/^Field([^\s]*):\s(.*)/) {
$field->{$1} = $2;
}
}
close FORM;
push @fields, $field if (%$field);
return (@fields);
}
sub field_is_ticked_box {
my ( $field ) = @_;
return( $field->{Name} =~ /^([0-9]+):case:(.*):([0-9]+),([0-9]+)$/ &&
value_is_true( $field->{Value} )
);
}
sub count_ticked_boxes {
my ($fields) = @_;
my $c = 0;
for my $f (@$fields) {
$c += ( field_is_ticked_box($f) ? 1 : 0 );
}
return ($c);
}
sub handle_field {
my ( $field ) = @_;
return (0) if ( !$field->{Name} );
if ( $field->{Name} =~ /^([0-9]+):case:(.*):([0-9]+),([0-9]+)$/ ) {
my ( $student_id, $q_name, $q_id, $a_id ) = ( $1, $2, $3, $4 );
my $page = $layout->box_page( $student_id, $q_id, $a_id );
my $copy = get_copy_id( $student_id, $page );
my $ticked = value_is_true( $field->{Value} );
debug( "Field " . $field->{Name} . " got PAGE=$page and COPY=$copy" );
$capture->set_zone_auto( $student_id, $page, $copy,
ZONE_BOX, $q_id, $a_id, 100, ( $ticked ? 100 : 0 ),
undef, undef );
return (1);
}
if ( $field->{Name} =~ /^([0-9]+):namefield$/ ) {
my $student_id = $1;
my $page = $layout->namefield_page($student_id);
my $copy = get_copy_id( $student_id, $page );
debug( "Field " . $field->{Name} . " got PAGE=$page and COPY=$copy" );
my $zoneid =
$capture->get_zoneid( $student_id, $page, $copy, ZONE_NAME, 0, 0, 1 );
my $value = decode_utf8( $field->{Value} );
$value = '' if ( !defined($value) );
$capture->set_zone_auto_id( $zoneid, -1, -1, "text:" . $value, undef );
return (1);
}
return (0);
}
my @not_considered;
if (@forms) {
$p->text( __("Reading PDF forms...") ) if ($p);
my $dp = 1 / ( 1 + $#forms );
for my $f (@forms) {
my $n_fields = 0;
if ( $f =~ /\.pdf$/i ) {
if ( -f $f ) {
my @fields = get_fields_from_pdf($f);
if ( count_ticked_boxes( \@fields ) ) {
clear_copy_id();
$data->begin_transaction('PDFF');
for my $field (@fields) {
$n_fields += handle_field($field);
}
$data->end_transaction('PDFF');
}
debug "Read $n_fields fields from $f";
} else {
debug "Skip file not found: $f";
}
} else {
debug "Skip file without PDF extension: $f";
}
$p->progres($dp) if ($p);
push @not_considered, $f if ( $n_fields == 0 );
}
}
# write back PDF files not used to files list
if ($list_file) {
open( LIST, ">", $list_file );
for (@not_considered) {
print LIST "$_\n";
}
close(LIST);
} else {
debug "WARNING: no output list file requested";
}
$p->text('') if ($p);
|