From: Paul Poulain <[email protected]>

before this patch, if you do an advanced search, with limits like
branch/itemtype, it was not saved correctly in the search history, so if you
re-do a search from there you won't get the same results

Signed-off-by: Owen Leonard <[email protected]>
---
 C4/Auth.pm                                         |    7 +--
 C4/Search.pm                                       |   17 +++++---
 installer/data/mysql/kohastructure.sql             |    4 +-
 installer/data/mysql/updatedatabase.pl             |   16 +++++++
 .../prog/en/modules/opac-search-history.tt         |    5 +-
 opac/opac-search-history.pl                        |   44 ++++++++++----------
 opac/opac-search.pl                                |   12 ++++-
 7 files changed, 68 insertions(+), 37 deletions(-)

diff --git a/C4/Auth.pm b/C4/Auth.pm
index 7211769..bab94cd 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;
@@ -383,8 +383,7 @@ sub get_template_and_user {
             StaffSerialIssueDisplayCount => 
C4::Context->preference("StaffSerialIssueDisplayCount"),
             NoZebra                     => C4::Context->preference('NoZebra'),
         );
-    }
-    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");
diff --git a/C4/Search.pm b/C4/Search.pm
index c3cff65..fbd4abd 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,15 @@ sub enabled_staff_search_views
        );
 }
 
-sub AddSearchHistory{
-       my ($borrowernumber,$session,$query_desc,$query_cgi, $total)=@_;
+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 3a51df5..049e50c 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -1641,7 +1641,9 @@ 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,
+  `limit_desc` varchar(255) DEFAULT NULL,
+  `limit_cgi` varchar(255) DEFAULT NULL,
+  `query_cgi` text NOT 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 6b88c29..8412666 100755
--- a/installer/data/mysql/updatedatabase.pl
+++ b/installer/data/mysql/updatedatabase.pl
@@ -4446,6 +4446,22 @@ 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);
+}
+$DBversion = "3.05.00.xxx";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("ALTER TABLE search_history MODIFY COLUMN query_cgi text NOT 
NULL");
+    print "Upgrade to $DBversion done (Change search_history.query_cgi type to 
text. bug 9581)\n";
+    SetVersion($DBversion);
+}
+
 
 =head1 FUNCTIONS
 
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 ff437da..a09aa21 100755
--- a/opac/opac-search.pl
+++ b/opac/opac-search.pl
@@ -509,6 +509,12 @@ for (my $i=0;$i<@servers;$i++) {
            }
  
            # Adding the new search if needed
+           my $path_info = $cgi->url(-path_info=>1);
+           $query_cgi = $cgi->url(-query=>1);
+           $query_cgi =~ s/^$path_info\?//;
+           $query_cgi =~ s/;/&/g;
+           $query_desc .= ", $limit_desc";
+
            if (!$borrowernumber || $borrowernumber eq '') {
            # To a cookie (the user is not logged in)
  
@@ -517,6 +523,8 @@ for (my $i=0;$i<@servers;$i++) {
                    push @recentSearches, {
                                            "query_desc" => $query_desc || 
"unknown", 
                                            "query_cgi"  => $query_cgi  || 
"unknown", 
+                            "limit_desc" => $limit_desc,
+                            "limit_cgi"  => $limit_cgi,
                                            "time"       => time(),
                                            "total"      => $total
                                          };
@@ -535,8 +543,8 @@ for (my $i=0;$i<@servers;$i++) {
            } 
                else {
            # To the session (the user is logged in)
-                       if (($params->{'offset'}||'') eq '') {
-                               AddSearchHistory($borrowernumber, 
$cgi->cookie("CGISESSID"), $query_desc, $query_cgi, $total);
+            if (($params->{'offset'}||'') eq '') {
+                AddSearchHistory( $borrowernumber, $cgi->cookie("CGISESSID"), 
$query_desc, $query_cgi, $limit_desc, $limit_cgi, $total );
                    $template->param(ShowOpacRecentSearchLink => 1);
                }
            }
-- 
1.7.3

_______________________________________________
Koha-patches mailing list
[email protected]
http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-patches
website : http://www.koha-community.org/
git : http://git.koha-community.org/
bugs : http://bugs.koha-community.org/

Reply via email to