File: report.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 (387 lines) | stat: -rw-r--r-- 11,711 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
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
# -*- perl -*-
#
# Copyright (C) 2012-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::report;

# AMC reports data management.

# This module is used to store (in a SQLite database) and handle some
# data concerning reports.

# TABLES:
#
# student stores some student reports filenames.
#
# * type is the kind of report (see REPORT_* constants).
#
# * file is the filename of the report.
#
# * student
# * copy identify the student sheet.
#
# * timestamp is the time when the report were generated.
#
# * mail_status, mail_timestamp, mail_message reports mailing feadback

use Exporter qw(import);

use constant {
    REPORT_ANNOTATED_PDF        => 1,
    REPORT_SINGLE_ANNOTATED_PDF => 2,
    REPORT_PRINTED_COPY         => 3,

    REPORT_MAIL_NO     => 0,
    REPORT_MAIL_OK     => 1,
    REPORT_MAIL_FAILED => 100,
};

our @EXPORT_OK = qw(
  REPORT_ANNOTATED_PDF REPORT_SINGLE_ANNOTATED_PDF
  REPORT_PRINTED_COPY REPORT_MAIL_OK REPORT_MAIL_FAILED
);
our %EXPORT_TAGS = (
    const => [
        qw(
          REPORT_ANNOTATED_PDF REPORT_SINGLE_ANNOTATED_PDF
          REPORT_PRINTED_COPY REPORT_MAIL_OK REPORT_MAIL_FAILED
        ),
    ],
);

use AMC::Basic;
use AMC::DataModule;

our @ISA = ("AMC::DataModule");

sub version_current {
    return (2);
}

sub version_upgrade {
    my ( $self, $old_version ) = @_;
    if ( $old_version == 0 ) {

        # Upgrading from version 0 (empty database) to version 1 :
        # creates all the tables.

        debug "Creating capture tables...";
        $self->sql_do( "CREATE TABLE IF NOT EXISTS "
              . $self->table("student")
              . " (type INTEGER, file TEXT, student INTEGER, copy INTEGER DEFAULT 0, timestamp INTEGER, PRIMARY KEY (type,student,copy))"
        );
        $self->sql_do( "CREATE TABLE IF NOT EXISTS "
              . $self->table("directory")
              . " (type INTEGER PRIMARY KEY, directory TEXT)" );

        for ( REPORT_ANNOTATED_PDF, REPORT_SINGLE_ANNOTATED_PDF ) {
            $self->statement('addDirectory')
              ->execute( $_, 'cr/corrections/pdf' );
        }

        return (1);
    } elsif ( $old_version == 1 ) {

        # version 2 includes mail_* columns

        debug "Adding mailing data...";
        $self->sql_do( "ALTER TABLE "
              . $self->table("student")
              . " ADD COLUMN mail_status INTEGER DEFAULT 0" );
        $self->sql_do( "ALTER TABLE "
              . $self->table("student")
              . " ADD COLUMN mail_timestamp INTEGER DEFAULT 0" );
        $self->sql_do( "ALTER TABLE "
              . $self->table("student")
              . " ADD COLUMN mail_message TEXT" );

        return (2);
    }
    return ('');
}

# defines all the SQL statements that will be used

sub define_statements {
    my ($self)      = @_;
    my $t_student   = $self->table("student");
    my $t_directory = $self->table("directory");
    my $t_assoc = $self->table( "association", "association" );
    $self->{statements} = {
        addDirectory => {
                sql => "INSERT INTO $t_directory"
              . " (type,directory)"
              . " VALUES(?,?)"
        },
        getDir =>
          { sql => "SELECT directory FROM $t_directory" . " WHERE type=?" },
        numType =>
          { sql => "SELECT COUNT(*) FROM $t_student" . " WHERE type=?" },
        allType => { sql => "SELECT file FROM $t_student" . " WHERE type=?" },
        filesWithType => {
                sql => "SELECT file FROM $t_student"
              . " WHERE type IN"
              . " ( SELECT a.type FROM $t_directory AS a,$t_directory AS b"
              . "   ON a.directory=b.directory AND b.type=? )"
        },
        setStudent => {
                sql => "INSERT OR REPLACE INTO $t_student"
              . " (file,timestamp,type,student,copy,"
              . "  mail_status,mail_message,mail_timestamp)"
              . " VALUES (?,?,?,?,?,?,?,?)"
        },
        setMailing => {
                sql => "UPDATE $t_student SET "
              . " mail_status=?, mail_message=?, mail_timestamp=?"
              . " WHERE type=? AND student=? AND copy=?"
        },
        getStudent => {
            sql => "SELECT file FROM $t_student"
              . " WHERE type=? AND student=? AND copy=?"
        },
        getStudentTime => {
            sql => "SELECT file,timestamp FROM $t_student"
              . " WHERE type=? AND student=? AND copy=?"
        },
        deleteType   => { sql => "DELETE FROM $t_student" . " WHERE type=?" },
        deleteReport => {
            sql => "DELETE FROM $t_student"
              . " WHERE type=? AND student=? AND copy=?"
        },
        getAssociatedType => {
                sql => "SELECT CASE"
              . "  WHEN a.manual IS NOT NULL THEN a.manual"
              . "  ELSE a.auto END AS id,a.student AS student,a.copy AS copy,r.file AS file,r.mail_status AS mail_status"
              . " FROM $t_assoc AS a,"
              . "   (SELECT * FROM $t_student WHERE type=?) AS r"
              . " ON a.student=r.student AND a.copy=r.copy"
        },
        getPreAssociatedType => { sql =>
              "SELECT a.student AS student, r.copy AS copy, a.id AS id, r.file AS file, r.mail_status AS mail_status"
              . " FROM "
              . $self->table( "association", "layout" )
              . " AS a,"
              . "   (SELECT * FROM $t_student WHERE type=?) AS r"
              . " ON a.student=r.student" },
        clearReports =>
        { sql => "DELETE FROM $t_student"
              ." WHERE type=?"},
    };
}

# files_with_type($type) returns all registered files that are located
# in the same directory as files with type $type does (including files
# with type $type).

sub files_with_type {
    my ( $self, $type ) = @_;
    return ( $self->sql_list( $self->statement('filesWithType'), $type ) );
}

# delete_student_type($type) deletes all records for specified type.

sub delete_student_type {
    my ( $self, $type ) = @_;
    $self->statement('deleteType')->execute($type);
}

# delete_student_report() deletes a student report record.

sub delete_student_report {
    my ( $self, $type, $student, $copy ) = @_;
    $self->statement('deleteReport')->execute( $type, $student, $copy );
}

# set_student_type($type,$student,$copy,$file,$timestamp) creates a
# new record for a student report file, or replace data if this report
# is already in the table.

sub set_student_report {
    my ( $self, $type, $student, $copy, $file, $timestamp ) = @_;
    $timestamp = time() if ( $timestamp eq 'now' );
    $self->statement('setStudent')
      ->execute( $file, $timestamp, $type, $student, $copy, 0, "", 0 );
}

# report_mailing($type,$student,$copy,$status,$message,$timestamp)
# records mailing status for the report

sub report_mailing {
    my ( $self, $type, $student, $copy, $status, $message, $timestamp ) = @_;
    $timestamp = time() if ( $timestamp eq 'now' );
    $self->statement('setMailing')
      ->execute( $status, $message, $timestamp, $type, $student,
        $copy );
}

# free_student_report($type,$file) returns a filename based on $file
# that is not yet registered in the same directory as files with type
# $type.

sub free_student_report {
    my ( $self, $type, $file, $basedir ) = @_;

    my %registered = map { $_ => 1 } ( $self->files_with_type($type) );
    if ( $registered{$file} ) {
        my $template = $file;
        if ( !( $template =~ s/(\.[a-z0-9]+)$/_%04d$1/i ) ) {
            $template .= '_%04d';
        }
        my $i = 0;
        do {
            $i++;
            $file = sprintf( $template, $i );
        } while ( $registered{$file} );
    }

    return ($file);
}

# get_dir($type) returns subdirectory (in project directory) where
# files for type $type are stored.

sub get_dir {
    my ( $self, $type ) = @_;
    return ( $self->sql_single( $self->statement('getDir'), $type ) );
}

# get_student_report($type,$student,$copy) returns the filename of a
# given report.

sub get_student_report {
    my ( $self, $type, $student, $copy ) = @_;
    return (
        $self->sql_single(
            $self->statement('getStudent'),
            $type, $student, $copy
        )
    );
}

# get_student_report_time($type,$student,$copy) returns the filename of a
# given report, and the corresponding timestamp.

sub get_student_report_time {
    my ( $self, $type, $student, $copy ) = @_;
    return (
        $self->sql_row(
            $self->statement('getStudentTime'),
            $type, $student, $copy
        )
    );
}

# get_associated_type($type) returns a list of reports of a particular
# type $type with the corresponding association IDs (primary key of
# the student in the students list file), like
#
# [{file=>'001.pdf',id=>'001234',mail_status=>0,
#  {file=>'002.pdf',id=>'001538',mail_status=>1,
# ]

sub get_associated_type {
    my ( $self, $type ) = @_;
    $self->{data}->require_module('association');
    return (
        $self->dbh->selectall_arrayref(
            $self->statement('getAssociatedType'),
            { Slice => {} }, $type
        )
    );
}

# get_preassociated_type($type): same as get_associated_type, but
# using pre-association

sub get_preassociated_type {
    my ( $self, $type ) = @_;
    $self->{data}->require_module('layout');
    return (
        $self->dbh->selectall_arrayref(
            $self->statement('getPreAssociatedType'),
            { Slice => {} }, $type
        )
    );
}


# type_count($type) returns the number of recorded reports of type
# $type.

sub type_count {
    my ( $self, $type ) = @_;
    return ( $self->sql_single( $self->statement('numType'), $type ) );
}

# all_type($type) returns all the report filenames for type $type

sub all_type {
    my ( $self, $type ) = @_;
    return ( $self->sql_list( $self->statement('allType'), $type ) );
}

# all_there($type,$basedir) returns TRUE if all reports of type $type
# are distinct present files.

sub all_there {
    my ( $self, $type, $basedir ) = @_;
    my $dir    = $basedir . '/' . $self->get_dir($type);
    my @f      = $self->sql_list( $self->statement('allType'), $type );
    my $n      = $#f;
    my %f_here = ();
    for (@f) {
        $f_here{$_} = 1 if ( -f $dir . '/' . $_ );
    }
    @f = ( keys %f_here );
    return ( $#f == $n );
}

# printed_filename(student,filename) sets the PDF printed copy file
# name for this student, and printed_filename(student) gets it.

sub printed_filename {
    my ( $self, $student, $filename ) = @_;
    if ( defined($filename) ) {
        $self->set_student_report( REPORT_PRINTED_COPY, $student, 0, $filename,
            'now' );
    } else {
        return $self->get_student_report( REPORT_PRINTED_COPY, $student, 0 );
    }
}

# clear all filenames

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

    $self->statement("clearReports")->execute(REPORT_PRINTED_COPY);
}

# number of printed sbjects

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

    return $self->type_count(REPORT_PRINTED_COPY);
}

1;