[Koha-patches] [PATCH] [SIGNED-OFF] Bug 5981 : MT 4231: Adds limits to search history

Nicole C. Engard nengard at bywatersolutions.com
Fri Aug 5 14:00:30 CEST 2011


From: Matthias Meusburger <matthias.meusburger at biblibre.com>


Signed-off-by: Nicole C. Engard <nengard at bywatersolutions.com>
---
 C4/Auth.pm                                         |   11 ++-
 C4/Search.pm                                       |   48 +++++++++--
 installer/data/mysql/kohastructure.sql             |    2 +
 installer/data/mysql/updatedatabase.pl             |   11 +++
 .../prog/en/modules/opac-search-history.tt         |    5 +-
 opac/opac-search-history.pl                        |   44 +++++-----
 opac/opac-search.pl                                |   90 ++++++++++++++++++-
 7 files changed, 172 insertions(+), 39 deletions(-)

diff --git a/C4/Auth.pm b/C4/Auth.pm
index 495f04c..3a79926 100644
--- a/C4/Auth.pm
+++ b/C4/Auth.pm
@@ -130,8 +130,8 @@ Output.pm module.
 =cut
 
 my $SEARCH_HISTORY_INSERT_SQL =<<EOQ;
-INSERT INTO search_history(userid, sessionid, query_desc, query_cgi, total, time            )
-VALUES                    (     ?,         ?,          ?,         ?,     ?, FROM_UNIXTIME(?))
+INSERT INTO search_history(userid, sessionid, query_desc, query_cgi, limit_desc, limit_cgi, total, time            )
+VALUES                    (     ?,         ?,          ?,         ?,          ?,         ?,     ?, FROM_UNIXTIME(?))
 EOQ
 sub get_template_and_user {
     my $in       = shift;
@@ -369,6 +369,7 @@ sub get_template_and_user {
             IntranetmainUserblock       => C4::Context->preference("IntranetmainUserblock"),
             LibraryName                 => C4::Context->preference("LibraryName"),
             LoginBranchname             => (C4::Context->userenv?C4::Context->userenv->{"branchname"}:"insecure"),
+            TemplateEncoding                                                           => C4::Context->preference("TemplateEncoding"),
             advancedMARCEditor          => C4::Context->preference("advancedMARCEditor"),
             canreservefromotherbranches => C4::Context->preference('canreservefromotherbranches'),
             intranetcolorstylesheet     => C4::Context->preference("intranetcolorstylesheet"),
@@ -382,9 +383,10 @@ sub get_template_and_user {
             virtualshelves              => C4::Context->preference("virtualshelves"),
             StaffSerialIssueDisplayCount => C4::Context->preference("StaffSerialIssueDisplayCount"),
             NoZebra                     => C4::Context->preference('NoZebra'),
+            IntranetXSLTDetailsDisplay                                                 => C4::Context->preference("IntranetXSLTDetailsDisplay"),
+            IntranetXSLTResultsDisplay                                                 => C4::Context->preference("IntranetXSLTResultsDisplay"),
         );
-    }
-    else {
+    } else {
         warn "template type should be OPAC, here it is=[" . $in->{'type'} . "]" unless ( $in->{'type'} eq 'opac' );
         #TODO : replace LibraryName syspref with 'system name', and remove this html processing
         my $LibraryNameTitle = C4::Context->preference("LibraryName");
@@ -443,6 +445,7 @@ sub get_template_and_user {
             OPACFinesTab              => C4::Context->preference("OPACFinesTab"),
             OpacTopissue              => C4::Context->preference("OpacTopissue"),
             RequestOnOpac             => C4::Context->preference("RequestOnOpac"),
+            TemplateEncoding               => "" . C4::Context->preference("TemplateEncoding"),
             'Version'                 => C4::Context->preference('Version'),
             hidelostitems             => C4::Context->preference("hidelostitems"),
             mylibraryfirst            => (C4::Context->preference("SearchMyLibraryFirst") && C4::Context->userenv) ? C4::Context->userenv->{'branch'} : '',
diff --git a/C4/Search.pm b/C4/Search.pm
index c3cff65..49e03fa 100644
--- a/C4/Search.pm
+++ b/C4/Search.pm
@@ -1238,6 +1238,11 @@ sub buildQuery {
                         $truncated_operand .= $index_plus_comma . "rltrn:@$rightlefttruncated ";
                         $previous_truncation_operand = 1;
                     }
+                    if ( scalar @$regexpr ) {
+                        $truncated_operand .= "and " if $previous_truncation_operand;
+                        $truncated_operand .= $index_plus_comma . "regExpr-1:@$regexpr ";
+                        $previous_truncation_operand = 1;
+                    }
                 }
                 $operand = $truncated_operand if $truncated_operand;
                 warn "TRUNCATED OPERAND: >$truncated_operand<" if $DEBUG;
@@ -2534,15 +2539,46 @@ sub enabled_staff_search_views
 	);
 }
 
-sub AddSearchHistory{
-	my ($borrowernumber,$session,$query_desc,$query_cgi, $total)=@_;
+=head2 enabled_opac_search_views
+
+%hash = enabled_opac_search_views()
+
+This function returns a hash that contains two flags obtained from the system
+preferences, used to determine whether a particular opac search results view
+is enabled.
+
+=over 2
+
+=item C<Output arg:>
+
+    * $hash{can_view_MARC} is true only if the MARC view is enabled
+    * $hash{can_view_ISBD} is true only if the ISBD view is enabled
+
+=item C<usage in the script:>
+
+=back
+
+$template->param ( C4::Search::enabled_opac_search_views );
+
+=cut
+
+sub enabled_opac_search_views
+{
+	return (
+		can_opac_view_MARC		=> C4::Context->preference('OPACviewMARC'),		# 1 if the opac search allows the MARC view
+		can_opac_view_ISBD		=> C4::Context->preference('OPACviewISBD'),		# 1 if the opac search allows the ISBD view
+	);
+}
+
+sub AddSearchHistory {
+    my ( $borrowernumber, $session, $query_desc, $query_cgi, $limit_desc, $limit_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);
+    my $sql = "INSERT INTO search_history(userid, sessionid, query_desc, query_cgi, limit_desc, limit_cgi, total, time) VALUES(?, ?, ?, ?, ?, ?, ?, NOW())";
+    my $sth = $dbh->prepare($sql);
+    $sth->execute( $borrowernumber, $session, $query_desc, $query_cgi, $limit_desc, $limit_cgi, $total );
+    return $dbh->last_insert_id( undef, 'search_history', undef, undef, undef );
 }
 
 sub GetSearchHistory{
diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index e9edf25..46bf002 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -1635,6 +1635,8 @@ CREATE TABLE IF NOT EXISTS `search_history` (
   `sessionid` varchar(32) NOT NULL,
   `query_desc` varchar(255) NOT NULL,
   `query_cgi` varchar(255) NOT NULL,
+  `limit_desc` varchar(255) DEFAULT NULL,
+  `limit_cgi` varchar(255) DEFAULT NULL,
   `total` int(11) NOT NULL,
   `time` timestamp NOT NULL default CURRENT_TIMESTAMP,
   KEY `userid` (`userid`),
diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl
index 720bd8c..288c8e3 100755
--- a/installer/data/mysql/updatedatabase.pl
+++ b/installer/data/mysql/updatedatabase.pl
@@ -4399,6 +4399,17 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     SetVersion($DBversion);
 }
 
+$DBversion = "3.05.00.XXX";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+	$dbh->do(q{
+	ALTER TABLE `search_history` ADD `limit_desc` VARCHAR( 255 ) NULL DEFAULT NULL AFTER `query_cgi` ,
+	ADD `limit_cgi` VARCHAR( 255 ) NULL DEFAULT NULL AFTER `limit_desc` 
+    });
+	print "Upgrade to $DBversion done (adding limits to the opac search history)\n";
+	SetVersion ($DBversion);
+}
+
+
 =head1 FUNCTIONS
 
 =head2 DropAllForeignKeys($table)
diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-search-history.tt b/koha-tmpl/opac-tmpl/prog/en/modules/opac-search-history.tt
index e9f6be1..6de4756 100644
--- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-search-history.tt
+++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-search-history.tt
@@ -47,7 +47,8 @@
 		    [% FOREACH recentSearche IN recentSearches %]
 		    <tr>
 			<td>[% recentSearche.time %]</td>
-			<td><a href="/cgi-bin/koha/opac-search.pl?[% recentSearche.query_cgi |html %]">[% recentSearche.query_desc |html %]</a></td>
+			<td><a href="/cgi-bin/koha/opac-search.pl?[% recentSearche.query_cgi |html %][% recentSearche.limit_cgi|html %]">[% recentSearche.query_desc|html %] [% recentSearche.limit_desc|html %]</a></td>
+
 			<td>[% recentSearche.total %]</td>
 		    </tr>
 		    [% END %]
@@ -65,7 +66,7 @@
 		    [% FOREACH previousSearche IN previousSearches %]
 		    <tr>
 			<td>[% previousSearche.time %]</td>
-			<td><a href="/cgi-bin/koha/opac-search.pl?[% previousSearche.query_cgi |html %]">[% previousSearche.query_desc |html %]</a></td>
+			<td><a href="/cgi-bin/koha/opac-search.pl?[% previousSearche.query_cgi |html %][% previousSearche.limit_cgi|html %]">[% previousSearche.query_desc|html %] [% previousSearche.limit_desc|html %]</a></td>
 			<td>[% previousSearche.total %]</td>
 		    </tr>
 		    [% END %]
diff --git a/opac/opac-search-history.pl b/opac/opac-search-history.pl
index 65141b9..d52e57d 100755
--- a/opac/opac-search-history.pl
+++ b/opac/opac-search-history.pl
@@ -113,28 +113,28 @@ if (!$loggedinuser) {
     # Showing search history
     } else {
 
-	my $date = C4::Dates->new();
-	my $dateformat = $date->DHTMLcalendar() . " %H:%i:%S"; # Current syspref date format + standard time format
-
-	# Getting the data with date format work done by mysql
-	my $query = "SELECT userid, sessionid, query_desc, query_cgi, total, DATE_FORMAT(time, \"$dateformat\") as time FROM search_history WHERE userid = ? AND sessionid = ?";
-	my $sth   = $dbh->prepare($query);
-	$sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
-	my $searches = $sth->fetchall_arrayref({});
-	$template->param(recentSearches => $searches);
-	
-	# Getting searches from previous sessions
-	$query = "SELECT COUNT(*) FROM search_history WHERE userid = ? AND sessionid != ?";
-	$sth   = $dbh->prepare($query);
-	$sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
-
-	# If at least one search from previous sessions has been performed
-        if ($sth->fetchrow_array > 0) {
-	    $query = "SELECT userid, sessionid, query_desc, query_cgi, total, DATE_FORMAT(time, \"$dateformat\") as time FROM search_history WHERE userid = ? AND sessionid != ?";
-	    $sth   = $dbh->prepare($query);
-	    $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
-    	    my $previoussearches = $sth->fetchall_arrayref({});
-    	    $template->param(previousSearches => $previoussearches);
+        my $date       = C4::Dates->new();
+        my $dateformat = $date->DHTMLcalendar() . " %H:%i:%S";    # Current syspref date format + standard time format
+
+        # Getting the data with date format work done by mysql
+        my $query = "SELECT userid, sessionid, query_desc, query_cgi, limit_desc, limit_cgi, total, DATE_FORMAT(time, \"$dateformat\") as time FROM search_history WHERE userid = ? AND sessionid = ?";
+        my $sth   = $dbh->prepare($query);
+        $sth->execute( $loggedinuser, $cgi->cookie("CGISESSID") );
+        my $searches = $sth->fetchall_arrayref( {} );
+        $template->param( recentSearches => $searches );
+
+        # Getting searches from previous sessions
+        $query = "SELECT COUNT(*) FROM search_history WHERE userid = ? AND sessionid != ?";
+        $sth   = $dbh->prepare($query);
+        $sth->execute( $loggedinuser, $cgi->cookie("CGISESSID") );
+
+        # If at least one search from previous sessions has been performed
+        if ( $sth->fetchrow_array > 0 ) {
+            $query = "SELECT userid, sessionid, query_desc, query_cgi, limit_desc, limit_cgi, total, DATE_FORMAT(time, \"$dateformat\") as time FROM search_history WHERE userid = ? AND sessionid != ?";
+            $sth   = $dbh->prepare($query);
+            $sth->execute( $loggedinuser, $cgi->cookie("CGISESSID") );
+            my $previoussearches = $sth->fetchall_arrayref( {} );
+            $template->param( previousSearches => $previoussearches );
 	
 	}
 
diff --git a/opac/opac-search.pl b/opac/opac-search.pl
index 81b7ce6..e89bdeb 100755
--- a/opac/opac-search.pl
+++ b/opac/opac-search.pl
@@ -541,11 +541,91 @@ for (my $i=0;$i<@servers;$i++) {
      		}
  	    }
  	}
-    ## If there's just one result, redirect to the detail page
-        if ($total == 1 && $format ne 'rss2'
-	    && $format ne 'opensearchdescription' && $format ne 'atom') {   
-            my $biblionumber=$newresults[0]->{biblionumber};
-            if (C4::Context->preference('BiblioDefaultView') eq 'isbd') {
+        my $tag_quantity;
+        if ( C4::Context->preference('TagsEnabled')
+            and $tag_quantity = C4::Context->preference('TagsShowOnList') ) {
+            foreach (@newresults) {
+                my $bibnum = $_->{biblionumber} or next;
+                $_->{itemsissued} = CountItemsIssued($bibnum);
+                $_->{'TagLoop'} = get_tags(
+                    {   biblionumber => $bibnum,
+                        approved     => 1,
+                        'sort'       => '-weight',
+                        limit        => $tag_quantity
+                    }
+                );
+            }
+        }
+        foreach (@newresults) {
+            $_->{coins} = GetCOinSBiblio( $_->{'biblionumber'} );
+        }
+
+        if ( $results_hashref->{$server}->{"hits"} ) {
+            $total = $total + $results_hashref->{$server}->{"hits"};
+        }
+
+        # Opac search history
+        my $newsearchcookie;
+        if ( C4::Context->preference('EnableOpacSearchHistory') ) {
+            my @recentSearches;
+
+            # Getting the (maybe) already sent cookie
+            my $searchcookie = $cgi->cookie('KohaOpacRecentSearches');
+            if ($searchcookie) {
+                $searchcookie = uri_unescape($searchcookie);
+                if ( thaw($searchcookie) ) {
+                    @recentSearches = @{ thaw($searchcookie) };
+                }
+            }
+
+            # Adding the new search if needed
+            if ( not defined $borrowernumber or $borrowernumber eq '' ) {
+
+                # To a cookie (the user is not logged in)
+
+                if ( not defined $params->{'offset'} or $params->{'offset'} eq '' ) {
+                    push @recentSearches,
+                      { "query_desc" => $query_desc || "unknown",
+                        "query_cgi"  => $query_cgi  || "unknown",
+                        "limit_desc" => $limit_desc,
+                        "limit_cgi"  => $limit_cgi,
+                        "time"       => time(),
+                        "total"      => $total
+                      };
+                    $template->param( ShowOpacRecentSearchLink => 1 );
+                }
+
+		# Only the 15 more recent searches are kept
+		# TODO: This has been done because of cookies' max size, which is
+		# usually 4KB. A real check on cookie actual size would be better
+		# than setting an arbitrary limit on the number of searches
+		shift @recentSearches if (@recentSearches > 15);
+
+                # Pushing the cookie back
+                $newsearchcookie = $cgi->cookie(
+                    -name => 'KohaOpacRecentSearches',
+
+                    # We uri_escape the whole freezed structure so we're sure we won't have any encoding problems
+                    -value   => uri_escape( freeze( \@recentSearches ) ),
+                    -expires => ''
+                );
+                $cookie = [ $cookie, $newsearchcookie ];
+            } else {
+
+                # To the session (the user is logged in)
+                if ( not defined $params->{'offset'} or $params->{'offset'} eq '' ) {
+                    AddSearchHistory( $borrowernumber, $cgi->cookie("CGISESSID"), $query_desc, $query_cgi, $limit_desc, $limit_cgi, $total );
+                    $template->param( ShowOpacRecentSearchLink => 1 );
+                }
+            }
+        }
+        ## If there's just one result, redirect to the detail page
+        if (   $total == 1
+            && $format ne 'rss2'
+            && $format ne 'opensearchdescription'
+            && $format ne 'atom' ) {
+            my $biblionumber = $newresults[0]->{biblionumber};
+            if ( C4::Context->preference('BiblioDefaultView') eq 'isbd' ) {
                 print $cgi->redirect("/cgi-bin/koha/opac-ISBDdetail.pl?biblionumber=$biblionumber");
             } elsif  (C4::Context->preference('BiblioDefaultView') eq 'marc') {
                 print $cgi->redirect("/cgi-bin/koha/opac-MARCdetail.pl?biblionumber=$biblionumber");
-- 
1.7.2.3



More information about the Koha-patches mailing list