View | Details | Raw Unified | Return to bug 17499
Collapse All | Expand All

(-)a/Koha/Exceptions.pm (+4 lines)
Lines 44-49 use Exception::Class ( Link Here
44
        isa => 'Koha::Exceptions::Exception',
44
        isa => 'Koha::Exceptions::Exception',
45
        description => 'General problem adding a library limit'
45
        description => 'General problem adding a library limit'
46
    },
46
    },
47
    'Koha::Exceptions::UnknownObject' => {
48
        isa => 'Koha::Exceptions::Exception',
49
        description => 'Object cannot be found or is not known',
50
    },
47
    # Virtualshelves exceptions
51
    # Virtualshelves exceptions
48
    'Koha::Exceptions::Virtualshelves::DuplicateObject' => {
52
    'Koha::Exceptions::Virtualshelves::DuplicateObject' => {
49
        isa => 'Koha::Exceptions::DuplicateObject',
53
        isa => 'Koha::Exceptions::DuplicateObject',
(-)a/Koha/Patron.pm (+28 lines)
Lines 33-38 use Koha::Patron::Categories; Link Here
33
use Koha::Patron::HouseboundProfile;
33
use Koha::Patron::HouseboundProfile;
34
use Koha::Patron::HouseboundRole;
34
use Koha::Patron::HouseboundRole;
35
use Koha::Patron::Images;
35
use Koha::Patron::Images;
36
use Koha::Patron::Message::Preferences;
36
use Koha::Patrons;
37
use Koha::Patrons;
37
use Koha::Virtualshelves;
38
use Koha::Virtualshelves;
38
use Koha::Club::Enrollments;
39
use Koha::Club::Enrollments;
Lines 630-635 sub get_enrollable_clubs { Link Here
630
    return wantarray ? $e->as_list : $e;
631
    return wantarray ? $e->as_list : $e;
631
}
632
}
632
633
634
=head3 set_default_messaging_preferences
635
636
    $patron->set_default_messaging_preferences
637
638
Sets default messaging preferences on patron.
639
640
See Koha::Patron::Message::Preference(s) for more documentation, especially on
641
thrown exceptions.
642
643
=cut
644
645
sub set_default_messaging_preferences {
646
    my ($self, $categorycode) = @_;
647
648
    my $options = Koha::Patron::Message::Preferences->get_options;
649
650
    foreach my $option (@$options) {
651
        Koha::Patron::Message::Preference->new_from_default({
652
            borrowernumber => $self->borrowernumber,
653
            categorycode   => $categorycode || $self->categorycode,
654
            message_attribute_id => $option->{message_attribute_id},
655
        })->store;
656
    }
657
658
    return $self;
659
}
660
633
=head3 type
661
=head3 type
634
662
635
=cut
663
=cut
(-)a/Koha/Patron/Message/Preference.pm (+63 lines)
Lines 23-28 use Koha::Database; Link Here
23
use Koha::Exceptions;
23
use Koha::Exceptions;
24
use Koha::Patron::Categories;
24
use Koha::Patron::Categories;
25
use Koha::Patron::Message::Preferences;
25
use Koha::Patron::Message::Preferences;
26
use Koha::Patron::Message::Transport::Preferences;
26
use Koha::Patrons;
27
use Koha::Patrons;
27
28
28
use base qw(Koha::Object);
29
use base qw(Koha::Object);
Lines 77-82 sub new { Link Here
77
    return $self;
78
    return $self;
78
}
79
}
79
80
81
=head3 new_from_default
82
83
my $preference = Koha::Patron::Message::Preference->new_from_default({
84
    borrowernumber => 123,
85
    categorycode   => 'ABC',
86
    message_attribute_id => 1,
87
})->store;
88
89
Sets default messaging preference for C<categorycode> to patron for given
90
C<message_attribute_id>.
91
92
Throws Koha::Exceptions::MissingParameter if any of following is missing:
93
- categorycode
94
- borrowernumber
95
- message_attribute_id
96
97
Throws Koha::Exceptions::UnknownObject if default preferences are not found.
98
99
=cut
100
101
sub new_from_default {
102
    my ($class, $params) = @_;
103
104
    my @required = qw(borrowernumber categorycode message_attribute_id);
105
    foreach my $p (@required) {
106
        Koha::Exceptions::MissingParameter->throw(
107
            error => "Missing required parameter.",
108
            parameter => $p,
109
        ) unless exists $params->{$p};
110
    }
111
112
    my $default = Koha::Patron::Message::Preferences->find({
113
        categorycode => $params->{'categorycode'},
114
        message_attribute_id => $params->{'message_attribute_id'},
115
    });
116
    Koha::Exceptions::UnknownObject->throw(
117
        error => "Default messaging preference for given categorycode and"
118
        ." message_attribute_id cannot be found.",
119
    ) unless $default;
120
    $default = $default->unblessed;
121
122
    # Add a new messaging preference for patron
123
    my $self = $class->SUPER::new({
124
        borrowernumber => $params->{'borrowernumber'},
125
        message_attribute_id => $default->{'message_attribute_id'},
126
        days_in_advance => $default->{'days_in_advance'},
127
        wants_digest => $default->{'wants_digest'},
128
    })->store;
129
130
    # Set default messaging transport types
131
    my $default_transport_types = Koha::Patron::Message::Transport::Preferences->
132
    search({ borrower_message_preference_id => $default->{'borrower_message_preference_id'} });
133
    while (my $transport = $default_transport_types->next) {
134
        Koha::Patron::Message::Transport::Preference->new({
135
            borrower_message_preference_id => $self->borrower_message_preference_id,
136
            message_transport_type => $transport->message_transport_type,
137
        })->store;
138
    }
139
140
    return $self;
141
}
142
80
=head3 store
143
=head3 store
81
144
82
Makes a validation before actual Koha::Object->store so that proper exceptions
145
Makes a validation before actual Koha::Object->store so that proper exceptions
(-)a/t/db_dependent/Koha/Patron/Message/Preferences.t (-2 / +35 lines)
Lines 126-132 subtest 'Add a test messaging transport' => sub { Link Here
126
};
126
};
127
127
128
subtest 'Add a messaging preference to patron' => sub {
128
subtest 'Add a messaging preference to patron' => sub {
129
    plan tests => 4;
129
    plan tests => 5;
130
130
131
    ok($preference = Koha::Patron::Message::Preference->new({
131
    ok($preference = Koha::Patron::Message::Preference->new({
132
        borrowernumber => $patron->{'borrowernumber'},
132
        borrowernumber => $patron->{'borrowernumber'},
Lines 147-152 subtest 'Add a messaging preference to patron' => sub { Link Here
147
            "Adding a duplicate preference"
147
            "Adding a duplicate preference"
148
            ." => Koha::Exceptions::DuplicateObject");
148
            ." => Koha::Exceptions::DuplicateObject");
149
149
150
    subtest 'Add preferences from defaults' => sub {
151
        plan tests => 4;
152
153
        my $attr = Koha::Patron::Message::Attribute->new({
154
            message_name => "default",
155
        })->store;
156
        ok(my $default = Koha::Patron::Message::Preference->new({
157
            categorycode   => $patron->{'categorycode'},
158
            message_attribute_id => $attr->message_attribute_id,
159
            wants_digest   => 1,
160
            days_in_advance => 1337,
161
        })->store, "Created a new default preference for category.");
162
        Koha::Patron::Message::Transport::Preference->new({
163
            borrower_message_preference_id => $default->borrower_message_preference_id,
164
            message_transport_type => "sms",
165
        })->store;
166
        Koha::Patron::Message::Transport::Preference->new({
167
            borrower_message_preference_id => $default->borrower_message_preference_id,
168
            message_transport_type => "email",
169
        })->store;
170
        ok(Koha::Patron::Message::Preference->new_from_default({
171
            borrowernumber => $patron->{'borrowernumber'},
172
            categorycode   => $patron->{'categorycode'},
173
            message_attribute_id => $attr->message_attribute_id,
174
        })->store, "Added a default preference to patron.");
175
        ok(my $pref = Koha::Patron::Message::Preferences->find({
176
            borrowernumber => $patron->{'borrowernumber'},
177
            message_attribute_id => $attr->message_attribute_id,
178
        }), "Found the default preference from patron.");
179
        is(Koha::Patron::Message::Transport::Preferences->search({
180
            borrower_message_preference_id => $pref->borrower_message_preference_id
181
        })->count, 2, "Found the two transport types that we set earlier");
182
    };
183
150
    subtest 'Attempt to add a messaging preference with invalid parameters' => sub {
184
    subtest 'Attempt to add a messaging preference with invalid parameters' => sub {
151
        plan tests => 6;
185
        plan tests => 6;
152
186
153
- 

Return to bug 17499