From 4e141472b341826545d92c2a988706fdc700bd8c Mon Sep 17 00:00:00 2001 From: Matthias Meusburger Date: Fri, 6 Aug 2010 17:18:40 +0200 Subject: [PATCH] Bug 5981 : MT 4231: Adds limits to search history --- C4/Auth.pm | 11 ++- C4/Search.pm | 48 +++++++++-- installer/data/mysql/kohastructure.sql | 2 + installer/data/mysql/updatedatabase.pl | 13 +++ .../prog/en/modules/opac-search-history.tmpl | 4 +- opac/opac-search-history.pl | 44 +++++----- opac/opac-search.pl | 89 +++++++++++++++++++- 7 files changed, 175 insertions(+), 36 deletions(-) diff --git a/C4/Auth.pm b/C4/Auth.pm index 5f28d5c..4ae7ca0 100644 --- a/C4/Auth.pm +++ b/C4/Auth.pm @@ -121,8 +121,8 @@ Output.pm module. =cut my $SEARCH_HISTORY_INSERT_SQL =< 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"), @@ -369,9 +370,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"); @@ -421,6 +423,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 b698a93..535f62a 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -1223,6 +1223,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; @@ -2473,15 +2478,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 + + * $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 + +=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 a62e07c..ace4681 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -1623,6 +1623,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 23b5d67..4d1210b 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -4134,10 +4134,23 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) { SetVersion ($DBversion); } +$DBversion = "3.03.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) +=item DropAllForeignKeys($table) + Drop all foreign keys of the table $table =cut diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-search-history.tmpl b/koha-tmpl/opac-tmpl/prog/en/modules/opac-search-history.tmpl index 3c370f0..22af0b7 100644 --- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-search-history.tmpl +++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-search-history.tmpl @@ -47,7 +47,7 @@ - "> + "> @@ -65,7 +65,7 @@ - "> + "> 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 d22049f..20ca528 100755 --- a/opac/opac-search.pl +++ b/opac/opac-search.pl @@ -455,8 +455,8 @@ for (my $i=0;$i<@servers;$i++) { push @newresults, { group_label => $group->{'group_label'}, GROUP_RESULTS => \@group_results }; } } else { - @newresults = searchResults('opac', $query_desc, $hits, $results_per_page, $offset, $scan, - @{$results_hashref->{$server}->{"RECORDS"}},, C4::Context->preference('hidelostitems')); + @newresults = + searchResults( $query_desc, $hits, $results_per_page, $offset, $scan, @{ $results_hashref->{$server}->{"RECORDS"} },, C4::Context->preference('hidelostitems') ); } my $tag_quantity; if (C4::Context->preference('TagsEnabled') and @@ -526,6 +526,91 @@ for (my $i=0;$i<@servers;$i++) { && $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.1