Bug 2176 (2/5): adding patron interface to update messaging preferences

This patch allows patrons to update their messaging preferences. This
includes methods in C4::Members to manage patron messaging preferences.

added cgi script to allow patron to edit their messaging preferences

Signed-off-by: Joshua Ferraro <jmf@liblime.com>
This commit is contained in:
Andrew Moore 2008-06-20 13:01:59 -05:00 committed by Joshua Ferraro
parent 3c547de448
commit 401c84cc09
5 changed files with 472 additions and 12 deletions

View file

@ -250,16 +250,17 @@ sub get_template_and_user {
# these template parameters are set the same regardless of $in->{'type'}
$template->param(
"BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
GoogleJackets => C4::Context->preference("GoogleJackets"),
KohaAdminEmailAddress => "" . C4::Context->preference("KohaAdminEmailAddress"),
LoginBranchcode => (C4::Context->userenv?C4::Context->userenv->{"branch"}:"insecure"),
LoginFirstname => (C4::Context->userenv?C4::Context->userenv->{"firstname"}:"Bel"),
LoginSurname => C4::Context->userenv?C4::Context->userenv->{"surname"}:"Inconnu",
TagsEnabled => C4::Context->preference("TagsEnabled"),
hide_marc => C4::Context->preference("hide_marc"),
'item-level_itypes' => C4::Context->preference('item-level_itypes'),
patronimages => C4::Context->preference("patronimages"),
singleBranchMode => C4::Context->preference("singleBranchMode"),
EnhancedMessagingPreferences => C4::Context->preference('EnhancedMessagingPreferences'),
GoogleJackets => C4::Context->preference("GoogleJackets"),
KohaAdminEmailAddress => "" . C4::Context->preference("KohaAdminEmailAddress"),
LoginBranchcode => (C4::Context->userenv?C4::Context->userenv->{"branch"}:"insecure"),
LoginFirstname => (C4::Context->userenv?C4::Context->userenv->{"firstname"}:"Bel"),
LoginSurname => C4::Context->userenv?C4::Context->userenv->{"surname"}:"Inconnu",
TagsEnabled => C4::Context->preference("TagsEnabled"),
hide_marc => C4::Context->preference("hide_marc"),
'item-level_itypes' => C4::Context->preference('item-level_itypes'),
patronimages => C4::Context->preference("patronimages"),
singleBranchMode => C4::Context->preference("singleBranchMode"),
);
if ( $in->{'type'} eq "intranet" ) {

215
C4/Members/Messaging.pm Normal file
View file

@ -0,0 +1,215 @@
package C4::Members::Messaging;
# Copyright (C) 2008 LibLime
#
# This file is part of Koha.
#
# Koha 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.
#
# Koha 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
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
use strict;
use warnings;
use C4::Context;
use vars qw($VERSION);
BEGIN {
# set the version for version checking
$VERSION = 3.00;
}
=head1 NAME
C4::Members::Messaging - manage patron messaging preferences
=head1 SYNOPSIS
=over 4
=back
=head1 FUNCTIONS
=head2 GetMessagingPreferences
=over 4
my $preferences = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrower->{'borrowernumber'},
message_name => 'DUE' } );
returns: a hashref of messaging preferences for this borrower for a particlar message_name
=back
=cut
sub GetMessagingPreferences {
my $params = shift;
foreach my $required ( qw( borrowernumber message_name ) ) {
if ( ! exists $params->{ $required } ) {
return;
}
}
my $sql = <<'END_SQL';
SELECT borrower_message_preferences.*,
borrower_message_transport_preferences.message_transport_type,
message_attributes.*,
message_transports.*
FROM borrower_message_preferences
LEFT JOIN borrower_message_transport_preferences
ON borrower_message_transport_preferences.borrower_message_preference_id = borrower_message_preferences.borrower_message_preference_id
LEFT JOIN message_attributes
ON message_attributes.message_attribute_id = borrower_message_preferences.message_attribute_id
LEFT JOIN message_transports
ON message_transports.message_attribute_id = message_attributes.message_attribute_id
AND message_transports.message_transport_type = borrower_message_transport_preferences.message_transport_type
WHERE borrower_message_preferences.borrowernumber = ?
AND message_attributes.message_name = ?
END_SQL
my @bind_params = ( $params->{'borrowernumber'}, $params->{'message_name'} );
my $sth = C4::Context->dbh->prepare($sql);
$sth->execute(@bind_params);
my $return;
my %transports; # helps build a list of unique message_transport_types
ROW: while ( my $row = $sth->fetchrow_hashref() ) {
next ROW unless $row->{'message_attribute_id'};
# warn( Data::Dumper->Dump( [ $row ], [ 'row' ] ) );
$return->{'days_in_advance'} = $row->{'days_in_advance'} if defined $row->{'days_in_advance'};
$return->{'wants_digest'} = $row->{'wants_digest'} if defined $row->{'wants_digest'};
$transports{$row->{'message_transport_type'}} = 1;
}
@{$return->{'transports'}} = keys %transports;
return $return;
}
=head2 SetMessagingPreferences
=over 4
C4::Members::Messaging::SetMessagingPreference( { borrowernumber => $borrower->{'borrowernumber'}
message_attribute_id => $message_attribute_id,
message_transport_types => [ qw( email sms ) ],
days_in_advance => 5
wants_digest => 1 } )
returns nothing useful.
=back
=cut
sub SetMessagingPreference {
my $params = shift;
foreach my $required ( qw( borrowernumber message_attribute_id message_transport_types ) ) {
if ( ! exists $params->{ $required } ) {
warn "SetMessagingPreference called without required parameter: $required";
return;
}
}
$params->{'days_in_advance'} = undef unless exists ( $params->{'days_in_advance'} );
$params->{'wants_digest'} = 0 unless exists ( $params->{'wants_digest'} );
my $dbh = C4::Context->dbh();
my $delete_sql = <<'END_SQL';
DELETE FROM borrower_message_preferences
WHERE borrowernumber = ?
AND message_attribute_id = ?
END_SQL
my $sth = $dbh->prepare( $delete_sql );
my $deleted = $sth->execute( $params->{'borrowernumber'}, $params->{'message_attribute_id'} );
if ( $params->{'message_transport_types'} ) {
my $insert_bmp = <<'END_SQL';
INSERT INTO borrower_message_preferences
(borrower_message_preference_id, borrowernumber, message_attribute_id, days_in_advance, wants_digest)
VALUES
(NULL, ?, ?, ?, ?)
END_SQL
$sth = C4::Context->dbh()->prepare($insert_bmp);
my $success = $sth->execute( $params->{'borrowernumber'},
$params->{'message_attribute_id'},
$params->{'days_in_advance'},
$params->{'wants_digest'} );
# my $borrower_message_preference_id = $dbh->last_insert_id();
my $borrower_message_preference_id = $dbh->{'mysql_insertid'};
my $insert_bmtp = <<'END_SQL';
INSERT INTO borrower_message_transport_preferences
(borrower_message_preference_id, message_transport_type)
VALUES
(?, ?)
END_SQL
$sth = C4::Context->dbh()->prepare($insert_bmtp);
foreach my $transport ( @{$params->{'message_transport_types'}}) {
my $success = $sth->execute( $borrower_message_preference_id, $transport );
}
}
return;
}
=head2 GetMessagingOptions
=over 4
my $messaging_options = C4::Members::Messaging::SetMessagingPreference()
returns a hashref of messaing options available.
=back
=cut
sub GetMessagingOptions {
my $sql = <<'END_SQL';
select message_attributes.message_attribute_id, takes_days, message_name, message_transport_type, is_digest
FROM message_attributes
LEFT JOIN message_transports
ON message_attributes.message_attribute_id = message_transports.message_attribute_id
END_SQL
my $sth = C4::Context->dbh->prepare($sql);
$sth->execute();
my $choices;
while ( my $row = $sth->fetchrow_hashref() ) {
$choices->{ $row->{'message_name'} }->{'message_attribute_id'} = $row->{'message_attribute_id'};
$choices->{ $row->{'message_name'} }->{'message_name'} = $row->{'message_name'};
$choices->{ $row->{'message_name'} }->{'takes_days'} = $row->{'takes_days'};
$choices->{ $row->{'message_name'} }->{'has_digest'} = 1 if $row->{'is_digest'};
$choices->{ $row->{'message_name'} }->{'transport-' . $row->{'message_transport_type'}} = ' ';
}
my @return = values %$choices;
# warn( Data::Dumper->Dump( [ \@return ], [ 'return' ] ) );
return \@return;
}
=head1 AUTHOR
Koha Development Team <info@koha.org>
Andrew Moore <andrew.moore@liblime.com>
=cut
1;

View file

@ -1,4 +1,5 @@
<!-- TMPL_IF NAME="loggedinusername" --><div id="menu">
<!-- TMPL_IF NAME="loggedinusername" -->
<div id="menu">
<ul>
<!-- TMPL_IF NAME="userview" --><li class="active"><!-- TMPL_ELSE --><li><!-- /TMPL_IF --><a href="/cgi-bin/koha/opac-user.pl">my summary</a></li>
<!-- TMPL_IF NAME="accountview" --><li class="active"><!-- TMPL_ELSE --><li><!-- /TMPL_IF --><a href="/cgi-bin/koha/opac-account.pl">my fines</a></li>
@ -17,5 +18,10 @@
<!-- TMPL_IF NAME="suggestionsview" --><li class="active"><!-- TMPL_ELSE --><li><!-- /TMPL_IF --><a href="/cgi-bin/koha/opac-suggestions.pl">my purchase suggestions</a></li>
<!-- /TMPL_UNLESS -->
<!-- /TMPL_IF -->
<!-- TMPL_IF NAME="EnhancedMessagingPreferences" -->
<!-- TMPL_IF NAME="messagingview" --><li class="active"><!-- TMPL_ELSE --><li><!-- /TMPL_IF --><a href="/cgi-bin/koha/opac-messaging.pl">my messaging</a></li>
<!-- /TMPL_IF -->
</ul>
</div><!-- /TMPL_IF -->
</div>
<!-- /TMPL_IF -->

View file

@ -0,0 +1,116 @@
<!-- TMPL_INCLUDE name="doc-head-open.inc" --><!-- TMPL_IF NAME="LibraryNameTitle" --><!-- TMPL_IF NAME="LibraryNameTitle" --><!-- TMPL_VAR NAME="LibraryNameTitle" --><!-- TMPL_ELSE -->Koha Online<!-- /TMPL_IF --><!-- TMPL_ELSE -->Koha Online<!-- /TMPL_IF --> Catalog &rsaquo; Updating Details for <!-- TMPL_LOOP name="BORROWER_INFO" --><!-- TMPL_VAR name="firstname" --> <!-- TMPL_VAR name="surname" --><!-- /TMPL_LOOP -->
<!-- TMPL_INCLUDE NAME="doc-head-close.inc" -->
</head>
<body>
<!-- TMPL_INCLUDE name="masthead.inc" -->
<div id="doc3" class="yui-t1">
<div id="bd">
<div id="yui-main">
<div class="yui-b"><div class="yui-g" id="userupdate">
<div class="container" style="overflow:auto;">
<!-- TMPL_LOOP name="BORROWER_INFO" -->
<h3><a href="/cgi-bin/koha/opac-user.pl"><!-- TMPL_VAR NAME="firstname" --> <!-- TMPL_VAR NAME="surname" -->'s account</a> <img src="<!-- TMPL_VAR NAME="themelang" -->../../images/caret.gif" width="16" height="16" alt="&gt;" border="0" /> Your Messaging Settings</h3>
<!-- /TMPL_LOOP -->
<form action="/cgi-bin/koha/opac-messaging.pl" method="get" name="opacmessaging">
<div class="yui-u first">
<input type="hidden" name="modify" value="yes" />
<fieldset class="brief"><legend>Mange your Messaging settings</legend>
<!-- TMPL_IF NAME="settings_updated" -->
<div class="dialog message"><h1>Settings Updated</h1></div>
<!-- /TMPL_IF -->
<table>
<tr><TH></TH><TH># days in advance</TH><TH>sms</TH><TH>email</TH><TH>Digests only?</TH><TH>rss</TH><TH>do not notify</TH></tr>
<!-- TMPL_LOOP name="messaging_preferences" -->
<tr>
<td><label for="firstname"><!-- TMPL_VAR NAME="message_name" --></label></td>
<!-- TMPL_IF NAME="takes_days" -->
<td><SELECT name="<!-- TMPL_VAR NAME="message_attribute_id" -->-DAYS" id="<!-- TMPL_VAR NAME="message_name" -->-DAYS" >
<!-- TMPL_LOOP name="select_days" -->
<OPTION VALUE="<!-- TMPL_VAR NAME="day" -->" <!-- TMPL_VAR NAME="selected" --> ><!-- TMPL_VAR NAME="day" --></OPTION>
<!-- /TMPL_LOOP -->
</SELECT>
</td>
<!-- TMPL_ELSE -->
<td>-</td>
<!-- /TMPL_IF -->
<!-- TMPL_IF NAME="transport-sms" -->
<td><input type="checkbox"
id="sms<!-- TMPL_VAR NAME="message_attribute_id" -->"
name="<!-- TMPL_VAR NAME="message_attribute_id" -->"
value="sms" <!-- TMPL_VAR NAME="transport-sms" -->
onclick = "document.opacmessaging.none<!-- TMPL_VAR NAME="message_attribute_id" -->.checked=false;">
</td>
<!-- TMPL_ELSE -->
<td>-</td>
<!-- /TMPL_IF -->
<!-- TMPL_IF NAME="transport-email" -->
<td><input type="checkbox"
id="email<!-- TMPL_VAR NAME="message_attribute_id" -->"
name="<!-- TMPL_VAR NAME="message_attribute_id" -->"
value="email" <!-- TMPL_VAR NAME="transport-email" -->
onclick = "document.opacmessaging.none<!-- TMPL_VAR NAME="message_attribute_id" -->.checked=false;">
</td>
<!-- TMPL_ELSE -->
<td>-</td>
<!-- /TMPL_IF -->
<!-- TMPL_IF NAME="has_digest" -->
<td><input type="checkbox"
id="digest<!-- TMPL_VAR NAME="message_attribute_id" -->"
value="<!-- TMPL_VAR NAME="message_attribute_id" -->"
name="digest" <!-- TMPL_VAR NAME="digest" -->
onclick = "document.opacmessaging.none<!-- TMPL_VAR NAME="message_attribute_id" -->.checked=false;">
</td>
<!-- TMPL_ELSE -->
<td>-</td>
<!-- /TMPL_IF -->
<!-- TMPL_IF NAME="transport-rss" -->
<td><input type="checkbox"
id="rss<!-- TMPL_VAR NAME="message_attribute_id" -->"
name="<!-- TMPL_VAR NAME="message_attribute_id" -->"
value="rss" <!-- TMPL_VAR NAME="transport-rss" -->
onclick = "document.opacmessaging.none<!-- TMPL_VAR NAME="message_attribute_id" -->.checked=false;">
</td>
<!-- TMPL_ELSE -->
<td>-</td>
<!-- /TMPL_IF -->
<td><input type="checkbox" id="none<!-- TMPL_VAR NAME="message_attribute_id" -->"
onclick = "if ( document.opacmessaging.none<!-- TMPL_VAR NAME="message_attribute_id" -->.checked == true ) {
document.opacmessaging.sms<!-- TMPL_VAR NAME="message_attribute_id" -->.checked=false;
document.opacmessaging.email<!-- TMPL_VAR NAME="message_attribute_id" -->.checked=false;
document.opacmessaging.digest<!-- TMPL_VAR NAME="message_attribute_id" -->.checked=false;
document.opacmessaging.rss<!-- TMPL_VAR NAME="message_attribute_id" -->.checked=false; }" /></td>
</tr>
<!-- /TMPL_LOOP -->
</table>
SMS number: <input type="text" name="SMSnumber" value="<!-- TMPL_VAR NAME="SMSnumber" -->" />
</fieldset>
</div>
<fieldset class="action">
<input type="submit" value="Submit Changes" /> <a class="cancel" href="/cgi-bin/koha/opac-user.pl">Cancel</a>
</fieldset>
</form>
</div>
</div>
</div>
</div>
<div class="yui-b">
<div class="container">
<!--TMPL_INCLUDE NAME="navigation.inc" -->
<!-- TMPL_INCLUDE name="usermenu.inc" -->
</div>
</div>
</div>
<!-- TMPL_INCLUDE NAME="opac-bottom.inc" -->

122
opac/opac-messaging.pl Executable file
View file

@ -0,0 +1,122 @@
#!/usr/bin/perl
# Copyright 2008 LibLime
#
# This file is part of Koha.
#
# Koha 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.
#
# Koha 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
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
use strict;
use warnings;
require Exporter;
use CGI;
use C4::Auth; # checkauth, getborrowernumber.
use C4::Context;
use C4::Koha;
use C4::Circulation;
use C4::Output;
use C4::Dates qw/format_date/;
use C4::Members;
use C4::Members::Messaging;
use C4::Branch;
my $query = CGI->new();
my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
{
template_name => 'opac-messaging.tmpl',
query => $query,
type => 'opac',
authnotrequired => 0,
flagsrequired => { borrow => 1 },
debug => 1,
}
);
my $borrower = GetMemberDetails( $borrowernumber );
my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
if ( defined $query->param('modify') && $query->param('modify') eq 'yes' ) {
# If they've modified the SMS number, record it.
if ( ( defined $query->param('SMSnumber') ) && ( $query->param('SMSnumber') ne $borrower->{'mobile'} ) ) {
ModMember( borrowernumber => $borrowernumber,
smsalertnumber => $query->param('SMSnumber') );
$borrower = GetMemberDetails( $borrowernumber );
}
# TODO: If a "NONE" box and another are checked somehow (javascript failed), we should pay attention to the "NONE" box
# warn( Data::Dumper->Dump( [ $messaging_options ], [ 'messaging_options' ] ) );
OPTION: foreach my $option ( @$messaging_options ) {
# warn( Data::Dumper->Dump( [ $option ], [ 'option' ] ) );
my $updater = { borrowernumber => $borrower->{'borrowernumber'},
message_attribute_id => $option->{'message_attribute_id'} };
# find the desired transports
@{$updater->{'message_transport_types'}} = $query->param( $option->{'message_attribute_id'} );
next OPTION unless $updater->{'message_transport_types'};
if ( $option->{'has_digest'} ) {
if ( List::Util::first { $_ == $option->{'message_attribute_id'} } $query->param( 'digest' ) ) {
$updater->{'wants_digest'} = 1;
}
}
if ( $option->{'takes_days'} ) {
if ( defined $query->param( $option->{'message_attribute_id'} . '-DAYS' ) ) {
$updater->{'days_in_advance'} = $query->param( $option->{'message_attribute_id'} . '-DAYS' );
}
}
# warn( 'calling SetMessaginPreferencse with ' . Data::Dumper->Dump( [ $updater ], [ 'updater' ] ) );
C4::Members::Messaging::SetMessagingPreference( $updater );
}
# show the success message
$template->param( settings_updated => 1 );
}
$messaging_options = C4::Members::Messaging::GetMessagingOptions();
# walk through the options and update them with these borrower_preferences
PREF: foreach my $option ( @$messaging_options ) {
my $pref = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrower->{'borrowernumber'},
message_name => $option->{'message_name'} } );
# warn( Data::Dumper->Dump( [ $pref ], [ 'pref' ] ) );
# make a hashref of the days, selecting one.
if ( $option->{'takes_days'} ) {
foreach my $day ( 0..30 ) { # FIXME: 30 is a magic number.
my $dayrow = { day => $day,
selected => '' };
if ( defined $pref->{'days_in_advance'} && $pref->{'days_in_advance'} == $day ) {
$dayrow->{'selected'} = 'SELECTED';
}
push @{$option->{'select_days'}}, $dayrow
}
}
foreach my $transport ( @{$pref->{'transports'}} ) {
$option->{'transport-'.$transport} = 'CHECKED';
}
$option->{'digest'} = $pref->{'wants_digest'} ? 'CHECKED' : '';
}
# warn( Data::Dumper->Dump( [ $messaging_options ], [ 'messaging_options' ] ) );
$template->param( BORROWER_INFO => [ $borrower ],
messagingview => 1,
messaging_preferences => $messaging_options,
SMSnumber => defined $borrower->{'smsalertnumber'} ? $borrower->{'smsalertnumber'} : $borrower->{'mobile'} );
output_html_with_http_headers $query, $cookie, $template->output;