File: Postcorrect.pm

package info (click to toggle)
auto-multiple-choice 1.5.0~rc2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 56,204 kB
  • sloc: perl: 27,850; xml: 23,201; cpp: 1,810; python: 700; makefile: 496; sh: 217; ansic: 195
file content (216 lines) | stat: -rw-r--r-- 6,314 bytes parent folder | download
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
# -*- perl -*-
#
# Copyright (C) 2020-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::Gui::Postcorrect;

use parent 'AMC::Gui';

use AMC::Basic;
use AMC::DataModule::capture ':zone';

sub new {
    my ( $class, %oo ) = @_;

    my $self = $class->SUPER::new(%oo);
    bless( $self, $class );

    $self->merge_config(
        {
            capture     => '',
            ok_callback => '',
        },
        %oo
    );

    return $self;
}

sub choose_reference {
    my ( $self, $ok_callback ) = @_;

    debug "PostCorrect option ON";

    $self->{ok_callback} = $ok_callback if ($ok_callback);

    # gets available sheet ids

    $self->{ids} = {};

    $self->{capture}->begin_read_transaction('PCex');
    my $sth = $self->{capture}->statement('studentCopies');
    $sth->execute;
    while ( my $sc = $sth->fetchrow_hashref ) {
        $self->{student_min} = $sc->{student}
          if ( !defined( $self->{student_min} ) );
        $self->{ids}->{ $sc->{student} }->{ $sc->{copy} } = 1;
        $self->{student_max} = $sc->{student};
    }
    $self->{capture}->end_transaction('PCex');

    debug "Student range: $self->{student_min}," . "$self->{student_max}\n";

    my $glade_xml = __FILE__;
    $glade_xml =~ s/\.p[ml]$/.glade/i;

    $self->read_glade(
        $glade_xml, qw/choix_postcorrect
          postcorrect_student postcorrect_copy
          postcorrect_set_multiple
          postcorrect_photo postcorrect_apply/
    );

    AMC::Gui::PageArea::add_feuille( $self->get_ui('postcorrect_photo') );

    $self->get_ui('postcorrect_student')
      ->set_range( $self->{student_min}, $self->{student_max} );

    if ( $self->get('postcorrect_student') ) {
        for (qw/student copy/) {
            $self->get_ui( 'postcorrect_' . $_ )
              ->set_value( $self->get( 'postcorrect_' . $_ ) );
        }
    } else {
        $self->get_ui('postcorrect_student')->set_value( $self->{student_min} );
        my @c = sort { $a <=> $b }
          ( keys %{ $self->{ids}->{ $self->{student_min} } } );
        $self->get_ui('postcorrect_copy')->set_value( $c[0] );
    }

    $self->get_ui('postcorrect_set_multiple')
        ->set_active( $self->get("postcorrect_set_multiple") );

    $self->change();

    $self->get_ui('choix_postcorrect')->show();
}

sub close_window {
    my ($self) = @_;

    $self->get_ui('choix_postcorrect')->destroy();
}

sub cancel {
    my ($self) = @_;

    $self->close_window();
}

sub ok {
    my ($self) = @_;

    my $student = $self->get_ui('postcorrect_student')->get_value();
    my $copy    = $self->get_ui('postcorrect_copy')->get_value();
    my $mult    = $self->get_ui('postcorrect_set_multiple')->get_active();
    $self->get_ui('choix_postcorrect')->destroy();

    $self->set( 'postcorrect_student',      $student );
    $self->set( 'postcorrect_copy',         $copy );
    $self->set( 'postcorrect_set_multiple', $mult );

    &{ $self->{ok_callback} }( $student, $copy, $mult );
}

sub student_exists {
    my ( $self, $student ) = @_;
    my @c = ();
    @c = ( keys %{ $self->{ids}->{$student} } )
      if ( $self->{ids}->{$student} );
    return ( $#c >= 0 ? 1 : 0 );
}

sub previous {
    my ($self)  = @_;
    my $student = $self->get_ui('postcorrect_student')->get_value();
    my $copy    = $self->get_ui('postcorrect_copy')->get_value();

    $copy--;
    if ( $copy < $self->{copy_0} ) {
        do { $student-- } while ( $student >= $self->{student_min}
            && !$self->student_exists($student) );
        if ( $student >= $self->{student_min} ) {
            $self->get_ui('postcorrect_student')->set_value($student);
            $self->get_ui('postcorrect_copy')->set_value(10000);
        }
    } else {
        $self->get_ui('postcorrect_copy')->set_value($copy);
    }
}

sub next {
    my ($self)  = @_;
    my $student = $self->get_ui('postcorrect_student')->get_value();
    my $copy    = $self->get_ui('postcorrect_copy')->get_value();

    $copy++;
    if ( $copy > $self->{copy_1} ) {
        do { $student++ } while ( $student <= $self->{student_max}
            && !$self->student_exists($student) );
        if ( $student <= $self->{student_max} ) {
            $self->get_ui('postcorrect_student')->set_value($student);
            $self->get_ui('postcorrect_copy')->set_value(0);
        }
    } else {
        $self->get_ui('postcorrect_copy')->set_value($copy);
    }
}

sub change_copy {
    my ($self)  = @_;
    my $student = $self->get_ui('postcorrect_student')->get_value();
    my $copy    = $self->get_ui('postcorrect_copy')->get_value();

    $self->get_ui('postcorrect_apply')
      ->set_sensitive( $self->{ids}->{$student}->{$copy} );

    $self->{capture}->begin_read_transaction('PCCN');
    my ($f) = $self->{capture}->zone_images( $student, $copy, ZONE_NAME );
    $self->{capture}->end_transaction('PCCN');
    if ( !defined($f) ) {
        $f = '';
    } else {
        $f = $self->get_absolute('cr') . "/$f";
    }
    debug "Postcorrect name field image: $f";
    if ( -f $f ) {
        $self->get_ui('postcorrect_photo')->set_content( image => $f );
    } else {
        $self->get_ui('postcorrect_photo')->set_content();
    }
}

sub change {
    my ($self) = @_;
    my $student = $self->get_ui('postcorrect_student')->get_value();

    my @c = sort { $a <=> $b } ( keys %{ $self->{ids}->{$student} } );
    $self->{copy_0} = $c[0];
    $self->{copy_1} = $c[$#c];

    debug "Postcorrect copy range for student $student: $c[0],$c[$#c]\n";
    $self->get_ui('postcorrect_copy')->set_range( $c[0], $c[$#c] );

    $self->change_copy();
}

1;