diff --git a/C4/Auth.pm b/C4/Auth.pm index 7574f9eb7f..d32d4daf8c 100644 --- a/C4/Auth.pm +++ b/C4/Auth.pm @@ -19,6 +19,8 @@ package C4::Auth; use strict; use Digest::MD5 qw(md5_base64); +use Storable qw(thaw freeze); +use URI::Escape; use CGI::Session; require Exporter; @@ -225,6 +227,58 @@ sub get_template_and_user { } } } + # Logged-in opac search history + # If the requested template is an opac one and opac search history is enabled + if ($in->{'type'} == "opac" && C4::Context->preference('EnableOpacSearchHistory')) { + my $dbh = C4::Context->dbh; + my $query = "SELECT COUNT(*) FROM search_history WHERE userid=?"; + my $sth = $dbh->prepare($query); + $sth->execute($borrowernumber); + + # If at least one search has already been performed + if ($sth->fetchrow_array > 0) { + # We show the link in opac + $template->param(ShowOpacRecentSearchLink => 1); + } + + # And if there's a cookie with searches performed when the user was not logged in, + # we add them to the logged-in search history + my @recentSearches; + my $searchcookie = $in->{'query'}->cookie('KohaOpacRecentSearches'); + if ($searchcookie){ + $searchcookie = uri_unescape($searchcookie); + if (thaw($searchcookie)) { + @recentSearches = @{thaw($searchcookie)}; + } + + if (@recentSearches > 0) { + my $query = "INSERT INTO search_history(userid, sessionid, query_desc, query_cgi, total, time) VALUES"; + my $icount = 1; + foreach my $asearch (@recentSearches) { + $query .= "("; + $query .= $borrowernumber . ", "; + $query .= '"' . $in->{'query'}->cookie("CGISESSID") . "\", "; + $query .= '"' . $asearch->{'query_desc'} . "\", "; + $query .= '"' . $asearch->{'query_cgi'} . "\", "; + $query .= $asearch->{'total'} . ", "; + $query .= 'FROM_UNIXTIME(' . $asearch->{'time'} . "))"; + if ($icount < @recentSearches) { $query .= ", ";} + $icount++; + } + + my $sth = $dbh->prepare($query); + $sth->execute; + + # And then, delete the cookie's content + my $newsearchcookie = $in->{'query'}->cookie( + -name => 'KohaOpacRecentSearches', + -value => freeze([]), + -expires => '' + ); + $cookie = [$cookie, $newsearchcookie]; + } + } + } } else { # if this is an anonymous session, setup to display public lists... @@ -250,6 +304,17 @@ sub get_template_and_user { } } + # Anonymous opac search history + # If opac search history is enabled and at least one search has already been performed + if (C4::Context->preference('EnableOpacSearchHistory') && $in->{'query'}->cookie('KohaOpacRecentSearches')) { + # We show the link in opac + if (thaw(uri_unescape($in->{'query'}->cookie('KohaOpacRecentSearches')))) { + my @recentSearches = @{thaw(uri_unescape($in->{'query'}->cookie('KohaOpacRecentSearches')))}; + if (@recentSearches > 0) { + $template->param(ShowOpacRecentSearchLink => 1); + } + } + } # these template parameters are set the same regardless of $in->{'type'} $template->param( diff --git a/C4/Search.pm b/C4/Search.pm index f72bb11571..b6bec0fe1a 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -61,6 +61,7 @@ This module provides searching functions for Koha's bibliographic databases &getRecords &buildQuery &NZgetRecords + &AddSearchHistory ); # make all your functions, whether exported or not; @@ -2051,6 +2052,27 @@ sub enabled_staff_search_views ); } +sub AddSearchHistory{ + my ($borrowernumber,$session,$query_desc,$query_cgi, $total)=@_; + my $dbh = C4::Context->dbh; + + # Add the request the user just made + my $sql = "INSERT INTO search_history(userid, sessionid, query_desc, query_cgi, total, time) VALUES(?, ?, ?, ?, ?, NOW())"; + my $sth = $dbh->prepare($sql); + $sth->execute($borrowernumber, $session, $query_desc, $query_cgi, $total); + return $dbh->last_insert_id(undef, 'search_history', undef,undef,undef); +} + +sub GetSearchHistory{ + my ($borrowernumber,$session)=@_; + my $dbh = C4::Context->dbh; + + # Add the request the user just made + my $query = "SELECT FROM search_history WHERE (userid=? OR sessionid=?)"; + my $sth = $dbh->prepare($query); + $sth->execute($borrowernumber, $session); + return $sth->fetchall_hashref({}); +} =head2 z3950_search_args diff --git a/Makefile.PL b/Makefile.PL index e3957dc363..d0e7460135 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -593,6 +593,7 @@ WriteMakefile( 'POE' => 0.9999, 'POSIX' => 1.09, 'Schedule::At' => 1.06, + 'Storable' => 2.21, 'SMS::Send' => 0.05, # optional 'Term::ANSIColor' => 1.10, 'Test' => 1.25, diff --git a/installer/data/mysql/atomicupdate/0007-Opac-search-history.pl b/installer/data/mysql/atomicupdate/0007-Opac-search-history.pl new file mode 100644 index 0000000000..db902a9a4f --- /dev/null +++ b/installer/data/mysql/atomicupdate/0007-Opac-search-history.pl @@ -0,0 +1,24 @@ +#! /usr/bin/perl +use strict; +use warnings; +use C4::Context; +my $dbh=C4::Context->dbh; + +$dbh->do("INSERT INTO `systempreferences` (`variable`, `value`, `options`, `explanation`, `type`) VALUES ('EnableOpacSearchHistory', '1', '', 'Enable or disable opac search history', 'YesNo')"); + +my $create = << END; +CREATE TABLE IF NOT EXISTS `search_history` ( + `userid` int(11) NOT NULL, + `sessionid` varchar(32) NOT NULL, + `query_desc` varchar(255) NOT NULL, + `query_cgi` varchar(255) NOT NULL, + `total` int(11) NOT NULL, + `time` timestamp NOT NULL default CURRENT_TIMESTAMP, + KEY `userid` (`userid`), + KEY `sessionid` (`sessionid`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Opac search history results'; +END + +$dbh->do($create); + +print "Upgrade done (added OPAC search history preference and table)\n"; diff --git a/koha-tmpl/opac-tmpl/prog/en/includes/masthead.inc b/koha-tmpl/opac-tmpl/prog/en/includes/masthead.inc index afe34d3d20..0112166096 100644 --- a/koha-tmpl/opac-tmpl/prog/en/includes/masthead.inc +++ b/koha-tmpl/opac-tmpl/prog/en/includes/masthead.inc @@ -1,7 +1,7 @@