File: AMC-read-pdfform.pl

package info (click to toggle)
auto-multiple-choice 1.4.0-2~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 18,520 kB
  • sloc: perl: 23,839; xml: 20,920; cpp: 1,791; makefile: 472; ansic: 186; sh: 80
file content (192 lines) | stat: -rw-r--r-- 4,969 bytes parent folder | download | duplicates (2)
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
#! /usr/bin/perl -w
#
# Copyright (C) 2017 Alexis Bienvenue <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 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;
binmode(STDOUT, ":utf8");

my $list_file='';
my $progress_id='';
my $debug='';
my $data_dir='';
my $multiple='';

GetOptions("list=s"=>\$list_file,
           "progression-id=s"=>\$progress_id,
           "debug=s"=>\$debug,
           "multiple!"=>\$multiple,
           "data=s"=>\$data_dir,
	  );

die "data directory not found: $data_dir" if(!-d $data_dir);

set_debug($debug);

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 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);
    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,(value_is_true($field->{Value}) ? 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});
    $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) {
        # Extract form data with pdfformfields (before, we were using pdftk):
        open(FORM,"-|","auto-multiple-choice","pdfformfields",$f)
          or die "Error with pdfformfields: $!";
        my $field={};
        clear_copy_id();
        $data->begin_transaction('PDFF');

        while(<FORM>) {
          chomp;
          if(/^---/) {
            $n_fields += handle_field($field);
            $field={};
          }
          if(/^Field([^\s]*):\s(.*)/) {
            $field->{$1}=$2;
          }
        }
        close FORM;

        $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);