1 package C4::Members::Messaging;
3 # Copyright (C) 2008 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
31 C4::Members::Messaging - manage patron messaging preferences
35 use C4::Members::Messaging
39 This module lets you modify a patron's messaging preferences.
43 =head2 GetMessagingPreferences
45 my $preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrower->{'borrowernumber'},
46 message_name => 'DUE' } );
48 my $preferences = C4::Members::Messaging::GetMessagingPreferences( { categorycode => 'LIBRARY',
49 message_name => 'DUE' } );
51 returns: a hashref of messaging preferences for a borrower or patron category for a particlar message_name
53 Requires either a borrowernumber or a categorycode key, but not both.
57 sub GetMessagingPreferences {
60 return unless exists $params->{message_name};
61 return unless exists $params->{borrowernumber} xor exists $params->{categorycode}; # yes, xor
62 my $sql = <<'END_SQL';
63 SELECT borrower_message_preferences.*,
64 borrower_message_transport_preferences.message_transport_type,
65 message_attributes.message_name,
66 message_attributes.takes_days,
67 message_transports.is_digest,
68 message_transports.letter_module,
69 message_transports.letter_code
70 FROM borrower_message_preferences
71 LEFT JOIN borrower_message_transport_preferences
72 ON borrower_message_transport_preferences.borrower_message_preference_id = borrower_message_preferences.borrower_message_preference_id
73 LEFT JOIN message_attributes
74 ON message_attributes.message_attribute_id = borrower_message_preferences.message_attribute_id
75 LEFT JOIN message_transports
76 ON message_transports.message_attribute_id = message_attributes.message_attribute_id
77 AND message_transports.message_transport_type = borrower_message_transport_preferences.message_transport_type
78 WHERE message_attributes.message_name = ?
81 my @bind_params = ( $params->{'message_name'} );
82 if ( exists $params->{'borrowernumber'} ) {
83 $sql .= " AND borrower_message_preferences.borrowernumber = ? ";
84 push @bind_params, $params->{borrowernumber};
86 $sql .= " AND borrower_message_preferences.categorycode = ? ";
87 push @bind_params, $params->{categorycode};
90 my $sth = C4::Context->dbh->prepare($sql);
91 $sth->execute(@bind_params);
93 my %transports; # helps build a list of unique message_transport_types
94 ROW: while ( my $row = $sth->fetchrow_hashref() ) {
95 next ROW unless $row->{'message_attribute_id'};
96 $return->{'days_in_advance'} = $row->{'days_in_advance'} if defined $row->{'days_in_advance'};
97 $return->{'wants_digest'} = $row->{'wants_digest'} if defined $row->{'wants_digest'};
98 $return->{'letter_code'} = $row->{'letter_code'};
99 next unless defined $row->{'message_transport_type'};
100 $return->{'transports'}->{ $row->{'message_transport_type'} } = $row->{'letter_code'};
105 =head2 SetMessagingPreference
107 This method defines how a user (or a default for a patron category) wants to get a certain
108 message delivered. The list of valid message types can be delivered can be found in the
109 C<message_attributes> table, and the list of valid message transports can be
110 found in the C<message_transport_types> table.
112 C4::Members::Messaging::SetMessagingPreference( { borrowernumber => $borrower->{'borrowernumber'}
113 message_attribute_id => $message_attribute_id,
114 message_transport_types => [ qw( email sms ) ],
116 wants_digest => 1 } )
118 returns nothing useful.
122 sub SetMessagingPreference {
125 unless (exists $params->{borrowernumber} xor exists $params->{categorycode}) { # yes, xor
126 warn "SetMessagingPreference called without exactly one of borrowernumber or categorycode";
129 foreach my $required ( qw( message_attribute_id message_transport_types ) ) {
130 if ( ! exists $params->{ $required } ) {
131 warn "SetMessagingPreference called without required parameter: $required";
135 $params->{'days_in_advance'} = undef unless exists ( $params->{'days_in_advance'} );
136 $params->{'wants_digest'} = 0 unless exists ( $params->{'wants_digest'} );
138 my $dbh = C4::Context->dbh();
140 my $delete_sql = <<'END_SQL';
141 DELETE FROM borrower_message_preferences
142 WHERE message_attribute_id = ?
144 my @bind_params = ( $params->{'message_attribute_id'} );
145 if ( exists $params->{'borrowernumber'} ) {
146 $delete_sql .= " AND borrowernumber = ? ";
147 push @bind_params, $params->{borrowernumber};
149 $delete_sql .= " AND categorycode = ? ";
150 push @bind_params, $params->{categorycode};
152 my $sth = $dbh->prepare( $delete_sql );
153 my $deleted = $sth->execute( @bind_params );
155 if ( $params->{'message_transport_types'} ) {
156 my $insert_bmp = <<'END_SQL';
157 INSERT INTO borrower_message_preferences
158 (borrower_message_preference_id, borrowernumber, categorycode, message_attribute_id, days_in_advance, wants_digest)
160 (NULL, ?, ?, ?, ?, ?)
163 $sth = C4::Context->dbh()->prepare($insert_bmp);
164 # set up so that we can easily construct the insert SQL
165 $params->{'borrowernumber'} = undef unless exists ( $params->{'borrowernumber'} );
166 $params->{'categorycode'} = undef unless exists ( $params->{'categorycode'} );
167 my $success = $sth->execute( $params->{'borrowernumber'},
168 $params->{'categorycode'},
169 $params->{'message_attribute_id'},
170 $params->{'days_in_advance'},
171 $params->{'wants_digest'} );
172 # my $borrower_message_preference_id = $dbh->last_insert_id();
173 my $borrower_message_preference_id = $dbh->{'mysql_insertid'};
175 my $insert_bmtp = <<'END_SQL';
176 INSERT INTO borrower_message_transport_preferences
177 (borrower_message_preference_id, message_transport_type)
181 $sth = C4::Context->dbh()->prepare($insert_bmtp);
182 foreach my $transport ( @{$params->{'message_transport_types'}}) {
183 my $success = $sth->execute( $borrower_message_preference_id, $transport );
189 =head2 GetMessagingOptions
191 my $messaging_options = C4::Members::Messaging::GetMessagingOptions()
193 returns a hashref of messaging options available.
197 sub GetMessagingOptions {
199 my $sql = <<'END_SQL';
200 select message_attributes.message_attribute_id, takes_days, message_name, message_transport_type, is_digest
201 FROM message_attributes
202 LEFT JOIN message_transports
203 ON message_attributes.message_attribute_id = message_transports.message_attribute_id
206 my $sth = C4::Context->dbh->prepare($sql);
209 while ( my $row = $sth->fetchrow_hashref() ) {
210 $choices->{ $row->{'message_name'} }->{'message_attribute_id'} = $row->{'message_attribute_id'};
211 $choices->{ $row->{'message_name'} }->{'message_name'} = $row->{'message_name'};
212 $choices->{ $row->{'message_name'} }->{'takes_days'} = $row->{'takes_days'};
213 $choices->{ $row->{'message_name'} }->{'has_digest'} = 1 if $row->{'is_digest'};
214 $choices->{ $row->{'message_name'} }->{'transport_' . $row->{'message_transport_type'}} = ' ';
217 my @return = values %$choices;
219 @return = sort { $a->{message_attribute_id} <=> $b->{message_attribute_id} } @return;
221 # warn( Data::Dumper->Dump( [ \@return ], [ 'return' ] ) );
225 =head2 SetMessagingPreferencesFromDefaults
227 C4::Members::Messaging::SetMessagingPreferencesFromDefaults( { borrowernumber => $borrower->{'borrowernumber'}
228 categorycode => 'CPL' } );
230 Given a borrowernumber and a patron category code (from the C<borrowernumber> and C<categorycode> keys
231 in the parameter hashref), replace all of the patron's current messaging preferences with
232 whatever defaults are defined for the patron category.
236 sub SetMessagingPreferencesFromDefaults {
239 foreach my $required ( qw( borrowernumber categorycode ) ) {
240 unless ( exists $params->{ $required } ) {
241 die "SetMessagingPreferencesFromDefaults called without required parameter: $required";
245 my $messaging_options = GetMessagingOptions();
246 OPTION: foreach my $option ( @$messaging_options ) {
247 my $default_pref = GetMessagingPreferences( { categorycode => $params->{categorycode},
248 message_name => $option->{'message_name'} } );
249 # FIXME - except for setting the borrowernumber, it really ought to be possible
250 # to have the output of GetMessagingPreferences be able to be the input
251 # to SetMessagingPreference
252 my @message_transport_types = keys %{ $default_pref->{transports} };
253 $default_pref->{message_attribute_id} = $option->{'message_attribute_id'};
254 $default_pref->{message_transport_types} = \@message_transport_types;
255 $default_pref->{borrowernumber} = $params->{borrowernumber};
256 SetMessagingPreference( $default_pref );
264 The actual messages which will be sent via a cron job running
265 F<misc/cronjobs/process_message_queue.pl>.
267 =head2 message_attributes
269 What kinds of messages can be sent?
271 =head2 message_transport_types
273 What transports can messages be sent vith? (email, sms, etc.)
275 =head2 message_transports
277 How are message_attributes and message_transport_types correlated?
279 =head2 borrower_message_preferences
281 What messages do the borrowers want to receive?
283 =head2 borrower_message_transport_preferences
285 What transport should a message be sent with?
289 =head2 Adding a New Kind of Message to the System
295 Add a new template to the `letter` table.
299 Insert a row into the `message_attributes` table.
303 Insert rows into `message_transports` for each message_transport_type.
313 Koha Development Team <http://koha-community.org/>
315 Andrew Moore <andrew.moore@liblime.com>