-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAMC-external.pl.in
127 lines (106 loc) · 3.6 KB
/
AMC-external.pl.in
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
#! @/PERLPATH/@
#
# Copyright (C) 2021-2022 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 Getopt::Long;
use Text::CSV;
use AMC::Basic;
use AMC::Data;
use AMC::Gui::Avancement;
my $data_dir = '';
my $source = '';
GetProjectOptions( ":data:dir|data=s" => \$data_dir,
"source=s" => \$source );
sub error {
my ($text) = @_;
debug "AMC-external ERROR: $text";
print "ERROR: $text\n";
exit(1);
}
error("source file not found: $source") if ( !-f $source );
my $data = AMC::Data->new($data_dir);
my $scoring = $data->module('scoring');
my $association = $data->module('association');
my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1 });
open my $fh, "<:encoding(utf8)", $source or error("Error opening $source: $!");
my %headers = map { $_ => 1 } (
$csv->header(
$fh, { sep_set => [ ";", ",", "\t" ], munge_column_names => 'none' }
)
);
# checks that the aID column exists
error("Missing aID column") if(!$headers{aID});
delete $headers{aID};
$data->begin_read_transaction('xQnu');
# get questions numbers for columns corresponding to existing questions
for my $q ( keys %headers ) {
my $n = $scoring->question_number($q);
if ( defined($n) ) {
$headers{$q} = $n;
} else {
debug("Warning: unknown question $q");
delete $headers{$q};
}
}
$data->end_transaction('xQnu');
error("No question column") if(!%headers);
my @wrong_aid = ();
my @overwrites = ();
my $n_scores = 0;
my $n_students = 0;
$data->begin_transaction('xRfc');
while ( my $row = $csv->getline_hr($fh) ) {
my ( $student, $copy ) = $association->de_anonymized( $row->{aID} );
if ( defined($student) ) {
$n_students++;
for my $q ( keys %headers ) {
if ( $row->{$q} =~ /[0-9]/ ) {
if (
defined(
$scoring->get_external_score(
$student, $copy, $headers{$q}
)
)
)
{
debug
"Already existing score for $row->{aID} ($student:$copy) $q";
push @overwrites, "$row->{aID}/$q";
print "WARN: ".__("Already existing score:")." $row->{aID} ($student:$copy) $q\n";
} else {
$row->{$q} =~ s/,/./;
$row->{$q} =~ s/[^0-9.]//g;
$scoring->set_external_score( $student, $copy, $headers{$q},
$row->{$q} );
$n_scores++;
}
}
}
} else {
debug "Unknown anonymous ID $row->{aID}";
print "WARN: ".__("Unknown anonymous ID:")." $row->{aID}\n";
push @wrong_aid, $row->{aID};
}
}
close $fh;
$data->end_transaction('xRfc');
debug("Read $n_scores scores for $n_students students");
print "VAR: nscores=$n_scores\n";
print "VAR: nstudents=$n_students\n";