File: Messages.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 (78 lines) | stat: -rw-r--r-- 1,926 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
#
# 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::Messages;

use AMC::Basic;

# possible types: INFO, WARN, ERR

my %message_type = (
    INFO => 1,
    WARN => 2,
    ERR  => 3,
);

sub add_message {
    my ( $self, $type, $message ) = @_;
    if ( !$message_type{$type} ) {
        debug "WARNING: inexistant message type - $type";
    }
    push @{ $self->{messages} }, { type => $type, message => $message };
}

sub get_messages {
    my ( $self, $type ) = @_;
    return (
        map  { $_->{message} }
        grep { $_->{type} eq $type } @{ $self->{messages} }
    );
}

sub n_messages {
    my ( $self, $type ) = @_;
    my @m = $self->get_messages($type);
    return ( 1 + $#m );
}

sub messages_as_string {
    my ($self) = @_;
    my $s = '';
    for ( @{ $self->{messages} } ) {
        $s .= $_->{type} . ": " . $_->{message} . "\n";
    }
    return ($s);
}

sub higher_message_type {
    my ($self) = @_;
    my $h      = 0;
    my $type   = '';
    for ( @{ $self->{messages} } ) {
        if ( $message_type{ $_->{type} } > $h ) {
            $type = $_->{type};
            $h    = $message_type{$type};
        }
    }
    return ($type);
}