From 80b70a98bf261c6ba2e5a36a7276d089559b1d19 Mon Sep 17 00:00:00 2001
From: Aleisha Amohia <aleishaamohia@hotmail.com>
Date: Wed, 26 Apr 2017 05:18:13 +0000
Subject: [PATCH] Bug 2093: Add OPAC Summary for logged-in users

This patch adds a summary to the OPAC once the user has logged in that
shows the users number of checkouts, overdues, holds pending, holds
waiting and total fines. We also have a syspref OPACUserSummary to turn
this feature on and off. Default is ON.

To test:
1) Apply patch and update database
2) Set up some checkouts, overdues, holds pending AND waiting and fines
for a user
3) Log into OPAC as that user, see summary. Confirm links all work as
expected
4) Confirm that if there are no checkouts / overdues etc that the link
disappears from the summary
5) Turn OPACUserSummary OFF and confirm the summary does not show on the
mainpage.

Sponsored-by: Catalyst IT

Signed-off-by: Hugo Agud <hagud@orex.es>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
---
 .../bug_2093_-_add_OPACUserSummary_syspref.sql     |  1 +
 installer/data/mysql/sysprefs.sql                  |  1 +
 .../prog/en/modules/admin/preferences/opac.pref    |  6 +++++
 .../opac-tmpl/bootstrap/en/modules/opac-main.tt    | 26 +++++++++++++++++++++-
 opac/opac-main.pl                                  | 21 +++++++++++++++++
 5 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100644 installer/data/mysql/atomicupdate/bug_2093_-_add_OPACUserSummary_syspref.sql

diff --git a/installer/data/mysql/atomicupdate/bug_2093_-_add_OPACUserSummary_syspref.sql b/installer/data/mysql/atomicupdate/bug_2093_-_add_OPACUserSummary_syspref.sql
new file mode 100644
index 0000000..bd8bba3
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_2093_-_add_OPACUserSummary_syspref.sql
@@ -0,0 +1 @@
+INSERT IGNORE INTO systempreferences (`variable`,`value`,`options`,`explanation`,`type`) VALUES ('OPACUserSummary', 1, NULL, "Show the summary of a logged in user's checkouts, overdues, holds and fines on the mainpage", 'YesNo');
diff --git a/installer/data/mysql/sysprefs.sql b/installer/data/mysql/sysprefs.sql
index 4647789..fdecaa8 100644
--- a/installer/data/mysql/sysprefs.sql
+++ b/installer/data/mysql/sysprefs.sql
@@ -394,6 +394,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `
 ('OPACUserCSS','',NULL,'Add CSS to be included in the OPAC in an embedded <style> tag.','free'),
 ('OPACUserJS','','70|10','Define custom javascript for inclusion in OPAC','Textarea'),
 ('opacuserlogin','1',NULL,'Enable or disable display of user login features','YesNo'),
+('OPACUserSummary', 1, NULL, "Show the summary of a logged in user's checkouts, overdues, holds and fines on the mainpage", 'YesNo'),
 ('OPACViewOthersSuggestions','0',NULL,'If ON, allows all suggestions to be displayed in the OPAC','YesNo'),
 ('OPACXSLTDetailsDisplay','default','','Enable XSL stylesheet control over details page display on OPAC','Free'),
 ('OPACXSLTListsDisplay','default','','Enable XSLT stylesheet control over lists pages display on OPAC','Free'),
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref
index d76c27b..c44b109 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref
@@ -1,6 +1,12 @@
 OPAC:
     Appearance:
         -
+            - pref: OPACUserSummary
+              choices:
+                  yes: Show
+                  no: "Don't show"
+            - "a summary of a logged in user's checkouts, overdues, holds and fines on the mainpage"
+        -
             - For search results in the OPAC, show the item's
             - pref: OPACResultsLibrary
               choices:
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt
index ba5372e..e60087b 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt
@@ -1,5 +1,6 @@
 [% USE Koha %]
 [% USE Branches %]
+[% USE Price %]
 [% INCLUDE 'doc-head-open.inc' %]
 <title>[% IF ( LibraryNameTitle ) %][% LibraryNameTitle %][% ELSE %]Koha online[% END %] catalog</title>
 [% INCLUDE 'doc-head-close.inc' %]
@@ -133,7 +134,30 @@
                                 </form>
                             </div> <!-- /#login -->
                         [% END # /casAuthentication %]
-                    [% END # / loggedinusername %]
+                    [% ELSE %]
+                        [% IF Koha.Preference('OPACUserSummary') %]
+                            <div id="user_summary">
+                                <h1>Welcome, <a href="/cgi-bin/koha/opac-user.pl"><span class="loggedinusername">[% USER_INFO.title %] [% USER_INFO.firstname %] [% USER_INFO.surname %]</span></a></h1>
+                                <ul id="user_summary_shortcuts">
+                                    [% IF checkouts && checkouts > 0 %]
+                                        <li><a href="/cgi-bin/koha/opac-user.pl">[% checkouts %] checkout(s)</a></li>
+                                    [% END %]
+                                    [% IF overdues && overdues > 0 %]
+                                        <li><a href="/cgi-bin/koha/opac-user.pl">[% overdues %] overdue(s)</a></li>
+                                    [% END %]
+                                    [% IF holds_pending && holds_pending > 0 %]
+                                        <li><a href="/cgi-bin/koha/opac-user.pl">[% holds_pending %] hold(s) pending</a></li>
+                                    [% END %]
+                                    [% IF holds_waiting && holds_waiting > 0 %]
+                                        <li><a href="/cgi-bin/koha/opac-user.pl">[% holds_waiting %] hold(s) waiting</a></li>
+                                    [% END %]
+                                    [% IF total_owing && total_owing > 0 %]
+                                        <li><a href="/cgi-bin/koha/opac-account.pl">[% total_owing | $Price %] due</a></li>
+                                    [% END %]
+                                </ul>
+                            </div>
+                        [% END %]
+                    [% END # /loggedinusername %]
                 [% END # /opacuserlogin %]
                 [% IF ( OpacNavRight ) %]
                     <div id="opacnavright">
diff --git a/opac/opac-main.pl b/opac/opac-main.pl
index 2f1f18c..d63392b 100755
--- a/opac/opac-main.pl
+++ b/opac/opac-main.pl
@@ -25,6 +25,10 @@ use C4::Output;
 use C4::NewsChannels;    # GetNewsToDisplay
 use C4::Languages qw(getTranslatedLanguages accept_language);
 use C4::Koha qw( GetDailyQuote );
+use C4::Members;
+use C4::Overdues;
+use C4::Reserves;
+use Koha::Checkouts;
 
 my $input = new CGI;
 my $dbh   = C4::Context->dbh;
@@ -62,7 +66,24 @@ my $koha_news_count = scalar @$all_koha_news;
 
 my $quote = GetDailyQuote();   # other options are to pass in an exact quote id or select a random quote each pass... see perldoc C4::Koha
 
+my $checkouts = Koha::Checkouts->search({ borrowernumber => $borrowernumber })->count;
+my ( $overdues_count, $overdues ) = checkoverdues($borrowernumber);
+my @holds = GetReservesFromBorrowernumber($borrowernumber);
+my $holds_pending = 0;
+foreach my $hold (@holds) {
+    if (not defined($hold->{found})){
+        $holds_pending++;
+    }
+}
+my @holds_waiting = GetReservesFromBorrowernumber($borrowernumber, 'W');
+my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
+
 $template->param(
+    checkouts           => $checkouts,
+    overdues            => $overdues_count,
+    holds_pending       => $holds_pending,
+    holds_waiting       => scalar @holds_waiting,
+    total_owing         => $total,
     koha_news           => $all_koha_news,
     koha_news_count     => $koha_news_count,
     branchcode          => $homebranch,
-- 
2.1.4