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 (+35 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 653-658 sub account_locked { Link Here
653
          and $self->login_attempts >= $FailedLoginAttempts )? 1 : 0;
654
          and $self->login_attempts >= $FailedLoginAttempts )? 1 : 0;
654
}
655
}
655
656
657
=head3 set_default_messaging_preferences
658
659
    $patron->set_default_messaging_preferences
660
661
Sets default messaging preferences on patron.
662
663
See Koha::Patron::Message::Preference(s) for more documentation, especially on
664
thrown exceptions.
665
666
=cut
667
668
sub set_default_messaging_preferences {
669
    my ($self, $categorycode) = @_;
670
671
    my $options = Koha::Patron::Message::Preferences->get_options;
672
673
    foreach my $option (@$options) {
674
        # Check that this option has preference configuration for this category
675
        unless (Koha::Patron::Message::Preferences->search({
676
            message_attribute_id => $option->{message_attribute_id},
677
            categorycode         => $categorycode || $self->categorycode,
678
        })->count) {
679
            next;
680
        }
681
        Koha::Patron::Message::Preference->new_from_default({
682
            borrowernumber => $self->borrowernumber,
683
            categorycode   => $categorycode || $self->categorycode,
684
            message_attribute_id => $option->{message_attribute_id},
685
        })->store;
686
    }
687
688
    return $self;
689
}
690
656
=head3 type
691
=head3 type
657
692
658
=cut
693
=cut
(-)a/Koha/Patron/Message/Preference.pm (+74 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::Attributes;
25
use Koha::Patron::Message::Attributes;
26
use Koha::Patron::Message::Preferences;
27
use Koha::Patron::Message::Transport::Preferences;
26
use Koha::Patron::Message::Transports;
28
use Koha::Patron::Message::Transports;
27
use Koha::Patrons;
29
use Koha::Patrons;
28
30
Lines 70-75 sub new { Link Here
70
    return $self;
72
    return $self;
71
}
73
}
72
74
75
=head3 new_from_default
76
77
my $preference = Koha::Patron::Message::Preference->new_from_default({
78
    borrowernumber => 123,
79
    categorycode   => 'ABC',   # if not given, patron's categorycode will be used
80
    message_attribute_id => 1,
81
});
82
83
NOTE: This subroutine initializes and STORES the object (in order to set
84
message transport types for the preference), so no need to call ->store when
85
preferences are initialized via this method.
86
87
Stores default messaging preference for C<categorycode> to patron for given
88
C<message_attribute_id>.
89
90
Throws Koha::Exceptions::MissingParameter if any of following is missing:
91
- borrowernumber
92
- message_attribute_id
93
94
Throws Koha::Exceptions::UnknownObject if default preferences are not found.
95
96
=cut
97
98
sub new_from_default {
99
    my ($class, $params) = @_;
100
101
    my @required = qw(borrowernumber message_attribute_id);
102
    foreach my $p (@required) {
103
        Koha::Exceptions::MissingParameter->throw(
104
            error => 'Missing required parameter.',
105
            parameter => $p,
106
        ) unless exists $params->{$p};
107
    }
108
    unless ($params->{'categorycode'}) {
109
        my $patron = Koha::Patrons->find($params->{borrowernumber});
110
        $params->{'categorycode'} = $patron->categorycode;
111
    }
112
113
    my $default = Koha::Patron::Message::Preferences->find({
114
        categorycode => $params->{'categorycode'},
115
        message_attribute_id => $params->{'message_attribute_id'},
116
    });
117
    Koha::Exceptions::UnknownObject->throw(
118
        error => 'Default messaging preference for given categorycode and'
119
        .' message_attribute_id cannot be found.',
120
    ) unless $default;
121
    $default = $default->unblessed;
122
123
    # Add a new messaging preference for patron
124
    my $self = $class->SUPER::new({
125
        borrowernumber => $params->{'borrowernumber'},
126
        message_attribute_id => $default->{'message_attribute_id'},
127
        days_in_advance => $default->{'days_in_advance'},
128
        wants_digest => $default->{'wants_digest'},
129
    })->store;
130
131
    # Set default messaging transport types
132
    my $default_transport_types =
133
    Koha::Patron::Message::Transport::Preferences->search({
134
        borrower_message_preference_id =>
135
                    $default->{'borrower_message_preference_id'}
136
    });
137
    while (my $transport = $default_transport_types->next) {
138
        Koha::Patron::Message::Transport::Preference->new({
139
            borrower_message_preference_id => $self->borrower_message_preference_id,
140
            message_transport_type => $transport->message_transport_type,
141
        })->store;
142
    }
143
144
    return $self;
145
}
146
73
=head3 store
147
=head3 store
74
148
75
Makes a validation before actual Koha::Object->store so that proper exceptions
149
Makes a validation before actual Koha::Object->store so that proper exceptions
(-)a/t/db_dependent/Koha/Patron/Message/Preferences.t (-2 / +105 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 4;
22
use Test::More tests => 5;
23
23
24
use t::lib::Mocks;
24
use t::lib::Mocks;
25
use t::lib::TestBuilder;
25
use t::lib::TestBuilder;
Lines 148-153 subtest 'Test Koha::Patron::Message::Preferences->get_options' => sub { Link Here
148
    };
148
    };
149
};
149
};
150
150
151
subtest 'Add preferences from defaults' => sub {
152
    plan tests => 3;
153
154
    $schema->storage->txn_begin;
155
156
    my $patron = build_a_test_patron();
157
    my ($default, $mtt1, $mtt2) = build_a_test_category_preference({
158
        patron => $patron,
159
    });
160
    ok(Koha::Patron::Message::Preference->new_from_default({
161
        borrowernumber       => $patron->borrowernumber,
162
        categorycode         => $patron->categorycode,
163
        message_attribute_id => $default->message_attribute_id,
164
    })->store, 'Added a default preference to patron.');
165
    ok(my $pref = Koha::Patron::Message::Preferences->find({
166
        borrowernumber       => $patron->borrowernumber,
167
        message_attribute_id => $default->message_attribute_id,
168
    }), 'Found the default preference from patron.');
169
    is(Koha::Patron::Message::Transport::Preferences->search({
170
        borrower_message_preference_id => $pref->borrower_message_preference_id
171
    })->count, 2, 'Found the two transport types that we set earlier');
172
173
    $schema->storage->txn_rollback;
174
};
175
151
subtest 'Test adding a new preference with invalid parameters' => sub {
176
subtest 'Test adding a new preference with invalid parameters' => sub {
152
    plan tests => 4;
177
    plan tests => 4;
153
178
Lines 280-285 sub build_a_test_category { Link Here
280
    return Koha::Patron::Categories->find($categorycode);
305
    return Koha::Patron::Categories->find($categorycode);
281
}
306
}
282
307
308
sub build_a_test_letter {
309
    my ($params) = @_;
310
311
    my $mtt = $params->{mtt} ? $params->{mtt} : 'email';
312
    my $branchcode     = $builder->build({
313
        source => 'Branch' })->{branchcode};
314
    my $letter = $builder->build({
315
        source => 'Letter',
316
        value => {
317
            branchcode => '',
318
            is_html => 0,
319
            message_transport_type => $mtt
320
        }
321
    });
322
323
    return Koha::Notice::Templates->find({
324
        module     => $letter->{module},
325
        code       => $letter->{code},
326
        branchcode => $letter->{branchcode},
327
    });
328
}
329
283
sub build_a_test_patron {
330
sub build_a_test_patron {
284
    my $categorycode   = $builder->build({
331
    my $categorycode   = $builder->build({
285
        source => 'Category' })->{categorycode};
332
        source => 'Category' })->{categorycode};
Lines 291-294 sub build_a_test_patron { Link Here
291
    return Koha::Patrons->find($borrowernumber);
338
    return Koha::Patrons->find($borrowernumber);
292
}
339
}
293
340
341
sub build_a_test_transport_type {
342
    my $mtt = $builder->build({
343
        source => 'MessageTransportType' });
344
345
    return Koha::Patron::Message::Transport::Types->find(
346
        $mtt->{message_transport_type}
347
    );
348
}
349
350
sub build_a_test_category_preference {
351
    my ($params) = @_;
352
353
    my $patron = $params->{patron};
354
    my $attr = $params->{attr}
355
                    ? $params->{attr}
356
                    : build_a_test_attribute($params->{days_in_advance});
357
358
    my $letter = $params->{letter} ? $params->{letter} : build_a_test_letter();
359
    my $mtt1 = $params->{mtt1} ? $params->{mtt1} : build_a_test_transport_type();
360
    my $mtt2 = $params->{mtt2} ? $params->{mtt2} : build_a_test_transport_type();
361
362
    Koha::Patron::Message::Transport->new({
363
        message_attribute_id   => $attr->message_attribute_id,
364
        message_transport_type => $mtt1->message_transport_type,
365
        is_digest              => $params->{digest} ? 1 : 0,
366
        letter_module          => $letter->module,
367
        letter_code            => $letter->code,
368
    })->store;
369
370
    Koha::Patron::Message::Transport->new({
371
        message_attribute_id   => $attr->message_attribute_id,
372
        message_transport_type => $mtt2->message_transport_type,
373
        is_digest              => $params->{digest} ? 1 : 0,
374
        letter_module          => $letter->module,
375
        letter_code            => $letter->code,
376
    })->store;
377
378
    my $default = Koha::Patron::Message::Preference->new({
379
        categorycode         => $patron->categorycode,
380
        message_attribute_id => $attr->message_attribute_id,
381
        wants_digest         => $params->{digest} ? 1 : 0,
382
        days_in_advance      => $params->{days_in_advance}
383
                                 ? $params->{days_in_advance} : 0,
384
    })->store;
385
386
    Koha::Patron::Message::Transport::Preference->new({
387
        borrower_message_preference_id => $default->borrower_message_preference_id,
388
        message_transport_type         => $mtt1->message_transport_type,
389
    })->store;
390
    Koha::Patron::Message::Transport::Preference->new({
391
        borrower_message_preference_id => $default->borrower_message_preference_id,
392
        message_transport_type         => $mtt2->message_transport_type,
393
    })->store;
394
395
    return ($default, $mtt1, $mtt2);
396
}
397
294
1;
398
1;
295
- 

Return to bug 17499