View | Details | Raw Unified | Return to bug 10807
Collapse All | Expand All

(-)a/C4/Auth.pm (-40 / +22 lines)
Lines 20-26 package C4::Auth; Link Here
20
use strict;
20
use strict;
21
use warnings;
21
use warnings;
22
use Digest::MD5 qw(md5_base64);
22
use Digest::MD5 qw(md5_base64);
23
use JSON qw/encode_json decode_json/;
23
use JSON qw/encode_json/;
24
use URI::Escape;
24
use URI::Escape;
25
use CGI::Session;
25
use CGI::Session;
26
26
Lines 28-33 require Exporter; Link Here
28
use C4::Context;
28
use C4::Context;
29
use C4::Templates;    # to get the template
29
use C4::Templates;    # to get the template
30
use C4::Branch; # GetBranches
30
use C4::Branch; # GetBranches
31
use C4::Search::History;
31
use C4::VirtualShelves;
32
use C4::VirtualShelves;
32
use Koha::AuthUtils qw(hash_password);
33
use Koha::AuthUtils qw(hash_password);
33
use POSIX qw/strftime/;
34
use POSIX qw/strftime/;
Lines 49-55 BEGIN { Link Here
49
    @EXPORT      = qw(&checkauth &get_template_and_user &haspermission &get_user_subpermissions);
50
    @EXPORT      = qw(&checkauth &get_template_and_user &haspermission &get_user_subpermissions);
50
    @EXPORT_OK   = qw(&check_api_auth &get_session &check_cookie_auth &checkpw &checkpw_internal &checkpw_hash
51
    @EXPORT_OK   = qw(&check_api_auth &get_session &check_cookie_auth &checkpw &checkpw_internal &checkpw_hash
51
                      &get_all_subpermissions &get_user_subpermissions
52
                      &get_all_subpermissions &get_user_subpermissions
52
                      ParseSearchHistorySession SetSearchHistorySession
53
                   );
53
                   );
54
    %EXPORT_TAGS = ( EditPermissions => [qw(get_all_subpermissions get_user_subpermissions)] );
54
    %EXPORT_TAGS = ( EditPermissions => [qw(get_all_subpermissions get_user_subpermissions)] );
55
    $ldap        = C4::Context->config('useldapserver') || 0;
55
    $ldap        = C4::Context->config('useldapserver') || 0;
Lines 129-139 Output.pm module. Link Here
129
129
130
=cut
130
=cut
131
131
132
my $SEARCH_HISTORY_INSERT_SQL =<<EOQ;
133
INSERT INTO search_history(userid, sessionid, query_desc, query_cgi, total, time            )
134
VALUES                    (     ?,         ?,          ?,         ?,          ?, FROM_UNIXTIME(?))
135
EOQ
136
137
sub get_template_and_user {
132
sub get_template_and_user {
138
133
139
    my $in       = shift;
134
    my $in       = shift;
Lines 256-281 sub get_template_and_user { Link Here
256
251
257
            # If at least one search has already been performed
252
            # If at least one search has already been performed
258
            if ($sth->fetchrow_array > 0) {
253
            if ($sth->fetchrow_array > 0) {
259
            # We show the link in opac
254
                # We show the link in opac
260
            $template->param(ShowOpacRecentSearchLink => 1);
255
                $template->param( EnableOpacSearchHistory => 1 );
261
            }
256
            }
262
257
263
            # And if there are searches performed when the user was not logged in,
258
            # And if there are searches performed when the user was not logged in,
264
            # we add them to the logged-in search history
259
            # we add them to the logged-in search history
265
            my @recentSearches = ParseSearchHistorySession($in->{'query'});
260
            my @recentSearches = C4::Search::History::get_from_session({ cgi => $in->{'query'} });
266
            if (@recentSearches) {
261
            if (@recentSearches) {
267
                my $sth = $dbh->prepare($SEARCH_HISTORY_INSERT_SQL);
262
                my $dbh = C4::Context->dbh;
263
264
                my $query = q{
265
                    INSERT INTO search_history(userid, sessionid, query_desc, query_cgi, type,  total, time )
266
                    VALUES (?, ?, ?, ?, ?, ?, ?)
267
                };
268
269
                my $sth = $dbh->prepare($query);
268
                $sth->execute( $borrowernumber,
270
                $sth->execute( $borrowernumber,
269
                           $in->{'query'}->cookie("CGISESSID"),
271
                           $in->{query}->cookie("CGISESSID"),
270
                           $_->{'query_desc'},
272
                           $_->{query_desc},
271
                           $_->{'query_cgi'},
273
                           $_->{query_cgi},
272
                           $_->{'total'},
274
                           $_->{type} || 'biblio',
273
                           $_->{'time'},
275
                           $_->{total},
276
                           $_->{time},
274
                        ) foreach @recentSearches;
277
                        ) foreach @recentSearches;
275
278
276
                # clear out the search history from the session now that
279
                # clear out the search history from the session now that
277
                # we've saved it to the database
280
                # we've saved it to the database
278
                SetSearchHistorySession($in->{'query'}, []);
281
                C4::Search::History::set_to_session({ cgi => $in->{'query'}, search_history => [] });
279
            }
282
            }
280
        }
283
        }
281
    }
284
    }
Lines 292-300 sub get_template_and_user { Link Here
292
     # Anonymous opac search history
295
     # Anonymous opac search history
293
     # If opac search history is enabled and at least one search has already been performed
296
     # If opac search history is enabled and at least one search has already been performed
294
     if (C4::Context->preference('EnableOpacSearchHistory')) {
297
     if (C4::Context->preference('EnableOpacSearchHistory')) {
295
        my @recentSearches = ParseSearchHistorySession($in->{'query'});
298
        my @recentSearches = C4::Search::History::get_from_session({ cgi => $in->{'query'} });
296
        if (@recentSearches) {
299
        if (@recentSearches) {
297
            $template->param(ShowOpacRecentSearchLink => 1);
300
            $template->param(EnableOpacSearchHistory => 1);
298
        }
301
        }
299
     }
302
     }
300
303
Lines 1791-1817 sub getborrowernumber { Link Here
1791
    return 0;
1794
    return 0;
1792
}
1795
}
1793
1796
1794
sub ParseSearchHistorySession {
1795
    my $cgi = shift;
1796
    my $sessionID = $cgi->cookie('CGISESSID');
1797
    return () unless $sessionID;
1798
    my $session = get_session($sessionID);
1799
    return () unless $session and $session->param('search_history');
1800
    my $obj = eval { decode_json(uri_unescape($session->param('search_history'))) };
1801
    return () unless defined $obj;
1802
    return () unless ref $obj eq 'ARRAY';
1803
    return @{ $obj };
1804
}
1805
1806
sub SetSearchHistorySession {
1807
    my ($cgi, $search_history) = @_;
1808
    my $sessionID = $cgi->cookie('CGISESSID');
1809
    return () unless $sessionID;
1810
    my $session = get_session($sessionID);
1811
    return () unless $session;
1812
    $session->param('search_history', uri_escape(encode_json($search_history)));
1813
}
1814
1815
END { }    # module clean-up code here (global destructor)
1797
END { }    # module clean-up code here (global destructor)
1816
1;
1798
1;
1817
__END__
1799
__END__
(-)a/C4/Search.pm (-23 lines)
Lines 68-74 This module provides searching functions for Koha's bibliographic databases Link Here
68
  &searchResults
68
  &searchResults
69
  &getRecords
69
  &getRecords
70
  &buildQuery
70
  &buildQuery
71
  &AddSearchHistory
72
  &GetDistinctValues
71
  &GetDistinctValues
73
  &enabled_staff_search_views
72
  &enabled_staff_search_views
74
  &PurgeSearchHistory
73
  &PurgeSearchHistory
Lines 2223-2250 sub enabled_staff_search_views Link Here
2223
	);
2222
	);
2224
}
2223
}
2225
2224
2226
sub AddSearchHistory{
2227
	my ($borrowernumber,$session,$query_desc,$query_cgi, $total)=@_;
2228
    my $dbh = C4::Context->dbh;
2229
2230
    # Add the request the user just made
2231
    my $sql = "INSERT INTO search_history(userid, sessionid, query_desc, query_cgi, total, time) VALUES(?, ?, ?, ?, ?, NOW())";
2232
    my $sth   = $dbh->prepare($sql);
2233
    $sth->execute($borrowernumber, $session, $query_desc, $query_cgi, $total);
2234
	return $dbh->last_insert_id(undef, 'search_history', undef,undef,undef);
2235
}
2236
2237
sub GetSearchHistory{
2238
	my ($borrowernumber,$session)=@_;
2239
    my $dbh = C4::Context->dbh;
2240
2241
    # Add the request the user just made
2242
    my $query = "SELECT FROM search_history WHERE (userid=? OR sessionid=?)";
2243
    my $sth   = $dbh->prepare($query);
2244
	$sth->execute($borrowernumber, $session);
2245
    return  $sth->fetchall_hashref({});
2246
}
2247
2248
sub PurgeSearchHistory{
2225
sub PurgeSearchHistory{
2249
    my ($pSearchhistory)=@_;
2226
    my ($pSearchhistory)=@_;
2250
    my $dbh = C4::Context->dbh;
2227
    my $dbh = C4::Context->dbh;
(-)a/C4/Search/History.pm (+268 lines)
Line 0 Link Here
1
package C4::Search::History;
2
3
use Modern::Perl;
4
5
use C4::Auth qw( get_session );
6
use C4::Context;
7
use Koha::DateUtils;
8
9
use JSON qw( encode_json decode_json );
10
use URI::Escape;
11
use Encode;
12
13
sub add {
14
    my ($params)   = @_;
15
    my $userid     = $params->{userid};
16
    my $sessionid  = $params->{sessionid};
17
    my $query_desc = $params->{query_desc};
18
    my $query_cgi  = $params->{query_cgi};
19
    my $total      = $params->{total} // 0;
20
    my $type       = $params->{type} || 'biblio';
21
22
    my $dbh = C4::Context->dbh;
23
24
    # Add the request the user just made
25
    my $query = q{
26
        INSERT INTO search_history(
27
            userid, sessionid, query_desc, query_cgi, type, total, time
28
        ) VALUES(
29
            ?, ?, ?, ?, ?, ?, NOW()
30
        )
31
    };
32
    my $sth = $dbh->prepare($query);
33
    $sth->execute( $userid, $sessionid, $query_desc, $query_cgi, $type,
34
        $total );
35
}
36
37
sub add_to_session {
38
    my ($params) = @_;
39
    my $cgi = $params->{cgi};
40
    my $query_desc = Encode::decode_utf8( $params->{query_desc} ) || "unknown";
41
    my $query_cgi  = Encode::decode_utf8( $params->{query_cgi} )  || "unknown";
42
    my $total      = $params->{total};
43
    my $type       = $params->{type}                              || 'biblio';
44
45
    my @recent_searches = get_from_session( { cgi => $cgi } );
46
    push @recent_searches,
47
      {
48
        query_desc => $query_desc,
49
        query_cgi  => $query_cgi,
50
        total      => "$total",
51
        type       => $type,
52
        time       => output_pref( { dt => dt_from_string(), dateformat => 'iso' } ),
53
      };
54
55
    shift @recent_searches if ( @recent_searches > 15 );
56
    set_to_session( { cgi => $cgi, search_history => \@recent_searches } );
57
}
58
59
sub delete {
60
    my ($params)  = @_;
61
    my $userid    = $params->{userid};
62
    my $sessionid = $params->{sessionid};
63
    my $type      = $params->{type}     || q{};
64
    my $previous  = $params->{previous} || 0;
65
66
    unless ($userid) {
67
        warn "ERROR: userid is required for history search";
68
        return;
69
    }
70
71
    my $dbh   = C4::Context->dbh;
72
    my $query = q{
73
        DELETE FROM search_history
74
        WHERE userid = ?
75
    };
76
77
    if ($sessionid) {
78
        $query .=
79
          $previous
80
          ? q{ AND sessionid != ?}
81
          : q{ AND sessionid = ?};
82
    }
83
84
    $query .= q{ AND type = ?}
85
      if $type;
86
87
    $dbh->do(
88
        $query, {}, $userid,
89
        ( $sessionid ? $sessionid : () ),
90
        ( $type      ? $type      : () )
91
    );
92
}
93
94
sub get {
95
    my ($params)  = @_;
96
    my $userid    = $params->{userid};
97
    my $sessionid = $params->{sessionid};
98
    my $type      = $params->{type};
99
    my $previous  = $params->{previous};
100
101
    unless ($userid) {
102
        warn "ERROR: userid is required for history search";
103
        return;
104
    }
105
106
    my $query = q{
107
        SELECT *
108
        FROM search_history
109
        WHERE userid = ?
110
    };
111
112
    if ($sessionid) {
113
        $query .=
114
          $previous
115
          ? q{ AND sessionid != ?}
116
          : q{ AND sessionid = ?};
117
    }
118
119
    $query .= q{ AND type = ?}
120
      if $type;
121
122
    my $dbh = C4::Context->dbh;
123
    my $sth = $dbh->prepare($query);
124
    $sth->execute(
125
        $userid,
126
        ( $sessionid ? $sessionid : () ),
127
        ( $type      ? $type      : () )
128
    );
129
    return $sth->fetchall_arrayref( {} );
130
}
131
132
sub get_from_session {
133
    my ($params)  = @_;
134
    my $cgi       = $params->{cgi};
135
    my $sessionID = $cgi->cookie('CGISESSID');
136
    return () unless $sessionID;
137
    my $session = C4::Auth::get_session($sessionID);
138
    return () unless $session and $session->param('search_history');
139
    my $obj =
140
      eval { decode_json( uri_unescape( $session->param('search_history') ) ) };
141
    return () unless defined $obj;
142
    return () unless ref $obj eq 'ARRAY';
143
    return @{$obj};
144
}
145
146
sub set_to_session {
147
    my ($params)       = @_;
148
    my $cgi            = $params->{cgi};
149
    my $search_history = $params->{search_history};
150
    my $sessionID      = $cgi->cookie('CGISESSID');
151
    return () unless $sessionID;
152
    my $session = C4::Auth::get_session($sessionID);
153
    return () unless $session;
154
    $session->param( 'search_history',
155
        uri_escape( encode_json($search_history) ) );
156
}
157
158
1;
159
160
__END__
161
162
=pod
163
164
=head1 NAME
165
166
C4::Search::History - Manage search history
167
168
=head1 DESCRIPTION
169
170
This module provides some routines for the search history management.
171
It deals with session or database.
172
173
=head1 ROUTINES
174
175
=head2 add
176
177
    C4::Search::History::add({
178
        userid => $userid,
179
        sessionid => $cgi->cookie("CGIESSID"),
180
        query_desc => $query_desc,
181
        query_cgi => $query_cgi,
182
        total => $total,
183
        type => $type,
184
    });
185
186
type is "biblio" or "authority".
187
188
Add a new search to the user's history.
189
190
=head2 add_to_session
191
192
    my $value = C4::Search::History::add_to_session({
193
        cgi => $cgi,
194
        query_desc => $query_desc,
195
        query_cgi => $query_cgi,
196
        total => $total,
197
        type => $type,
198
    });
199
200
Add a search to the session. The number of searches to keep is hardcoded to 15.
201
202
=head2 delete
203
204
    C4::Search::History::delete({
205
        userid => $loggedinuser,
206
        sessionid => $sessionid,
207
        type => $type,
208
        previous => $previous
209
    });
210
211
Delete searches in the database.
212
If the sessionid is missing all searches for all sessions will be deleted.
213
It is possible to delete searches for current session or all previous sessions using the previous flag.
214
If the type ("biblio" or "authority") is missing, all type will be deleted.
215
To delete *all* searches for a given userid, just pass a userid.
216
217
=head2 get
218
219
    my $searches C4::Search::History::get({
220
        userid => $userid,
221
        sessionsid => $sessionid,
222
        type => $type,
223
        previous => $previous
224
    });
225
226
Return a list of searches for a given userid.
227
If a sessionid is given, searches are limited to the matching session.
228
type and previous follow the same behavior as the delete routine.
229
230
=head2 get_from_session
231
232
    my $searches = C4::Search::History::get_from_session({
233
        cgi => $cgi
234
    });
235
236
Return all searches present for the given session.
237
238
=head2 set_to_session
239
240
    C4::Search::History::set_to_session({
241
        cgi => $cgi,
242
        search_history => $search_history
243
    });
244
245
Store searches into the session.
246
247
=head1 AUTHORS
248
249
Jonathan Druart <jonathan.druart@biblibre.com>
250
251
=head1 LICENSE
252
253
Copyright 2013 BibLibre SARL
254
255
This file is part of Koha.
256
257
Koha is free software; you can redistribute it and/or modify it under the
258
terms of the GNU General Public License as published by the Free Software
259
Foundation; either version 2 of the License, or (at your option) any later
260
version.
261
262
Koha is distributed in the hope that it will be useful, but WITHOUT ANY
263
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
264
A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
265
266
You should have received a copy of the GNU General Public License along
267
with Koha; if not, write to the Free Software Foundation, Inc.,
268
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
(-)a/installer/data/mysql/kohastructure.sql (+1 lines)
Lines 1904-1909 CREATE TABLE IF NOT EXISTS `search_history` ( -- patron's opac search history Link Here
1904
  `sessionid` varchar(32) NOT NULL, -- a system generated session id
1904
  `sessionid` varchar(32) NOT NULL, -- a system generated session id
1905
  `query_desc` varchar(255) NOT NULL, -- the search that was performed
1905
  `query_desc` varchar(255) NOT NULL, -- the search that was performed
1906
  `query_cgi` text NOT NULL, -- the string to append to the search url to rerun the search
1906
  `query_cgi` text NOT NULL, -- the string to append to the search url to rerun the search
1907
  `type` varchar(255) NOT NULL DEFAULT 'biblio', -- search type, must be 'biblio' or 'authority'
1907
  `total` int(11) NOT NULL, -- the total of results found
1908
  `total` int(11) NOT NULL, -- the total of results found
1908
  `time` timestamp NOT NULL default CURRENT_TIMESTAMP, -- the date and time the search was run
1909
  `time` timestamp NOT NULL default CURRENT_TIMESTAMP, -- the date and time the search was run
1909
  KEY `userid` (`userid`),
1910
  KEY `userid` (`userid`),
(-)a/installer/data/mysql/updatedatabase.pl (+10 lines)
Lines 8202-8207 if ( CheckVersion($DBversion) ) { Link Here
8202
    SetVersion($DBversion);
8202
    SetVersion($DBversion);
8203
}
8203
}
8204
8204
8205
8206
$DBversion = "3.15.00.XXX";
8207
if ( CheckVersion($DBversion) ) {
8208
    $dbh->do(q|
8209
        ALTER TABLE search_history ADD COLUMN type VARCHAR(255) NOT NULL DEFAULT 'biblio' AFTER query_cgi
8210
    |);
8211
    print "Upgrade to $DBversion done (Bug 10807 - Add db field search_history.type)\n";
8212
    SetVersion($DBversion);
8213
}
8214
8205
=head1 FUNCTIONS
8215
=head1 FUNCTIONS
8206
8216
8207
=head2 TableExists($table)
8217
=head2 TableExists($table)
(-)a/koha-tmpl/opac-tmpl/ccsr/en/includes/top-bar.inc (-1 / +1 lines)
Lines 60-66 Link Here
60
                    <li><span class="members">Welcome, <a href="/cgi-bin/koha/opac-user.pl"><span class="loggedinusername">[% FOREACH USER_INF IN USER_INFO %][% USER_INF.title %] [% USER_INF.firstname %] [% USER_INF.surname %][% END %]</span></a></span></li>
60
                    <li><span class="members">Welcome, <a href="/cgi-bin/koha/opac-user.pl"><span class="loggedinusername">[% FOREACH USER_INF IN USER_INFO %][% USER_INF.title %] [% USER_INF.firstname %] [% USER_INF.surname %][% END %]</span></a></span></li>
61
61
62
                [% END %]
62
                [% END %]
63
                [% IF ( ShowOpacRecentSearchLink ) %]
63
                [% IF ( EnableOpacSearchHistory ) %]
64
                    <li><a href="/cgi-bin/koha/opac-search-history.pl" title="View your search history">Search history</a></li>
64
                    <li><a href="/cgi-bin/koha/opac-search-history.pl" title="View your search history">Search history</a></li>
65
                [% END %]
65
                [% END %]
66
 [% IF ( loggedinusername ) %]<li>[% IF persona %]<a class="logout" id="logout" href="/cgi-bin/koha/opac-main.pl?logout.x=1" onclick='navigator.id.logout();'>[% ELSE %]<a class="logout" id="logout" href="/cgi-bin/koha/opac-main.pl?logout.x=1">[% END %]Log Out</a></li>[% END %]
66
 [% IF ( loggedinusername ) %]<li>[% IF persona %]<a class="logout" id="logout" href="/cgi-bin/koha/opac-main.pl?logout.x=1" onclick='navigator.id.logout();'>[% ELSE %]<a class="logout" id="logout" href="/cgi-bin/koha/opac-main.pl?logout.x=1">[% END %]Log Out</a></li>[% END %]
(-)a/koha-tmpl/opac-tmpl/ccsr/en/includes/usermenu.inc (-1 / +1 lines)
Lines 12-18 Link Here
12
  [% IF ( OpacPasswordChange ) %]
12
  [% IF ( OpacPasswordChange ) %]
13
    [% IF ( passwdview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/opac-passwd.pl">change my password</a></li>
13
    [% IF ( passwdview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/opac-passwd.pl">change my password</a></li>
14
  [% END %]
14
  [% END %]
15
  [% IF ( ShowOpacRecentSearchLink ) %]
15
  [% IF EnableOpacSearchHistory %]
16
  [% IF ( searchhistoryview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/opac-search-history.pl">my search history</a></li>
16
  [% IF ( searchhistoryview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/opac-search-history.pl">my search history</a></li>
17
  [% END %]
17
  [% END %]
18
  [% IF ( opacreadinghistory ) %]
18
  [% IF ( opacreadinghistory ) %]
(-)a/koha-tmpl/opac-tmpl/prog/en/includes/masthead.inc (-1 / +1 lines)
Lines 7-13 Link Here
7
                <li><span class="members">Welcome, <a href="/cgi-bin/koha/opac-user.pl"><span class="loggedinusername">[% FOREACH USER_INF IN USER_INFO %][% USER_INF.title %] [% USER_INF.firstname %] [% USER_INF.surname %][% END %]</span></a></span></li>
7
                <li><span class="members">Welcome, <a href="/cgi-bin/koha/opac-user.pl"><span class="loggedinusername">[% FOREACH USER_INF IN USER_INFO %][% USER_INF.title %] [% USER_INF.firstname %] [% USER_INF.surname %][% END %]</span></a></span></li>
8
8
9
            [% END %]
9
            [% END %]
10
            [% IF ( ShowOpacRecentSearchLink ) %]
10
            [% IF EnableOpacSearchHistory %]
11
                <li><a href="/cgi-bin/koha/opac-search-history.pl" title="View your search history">Search history</a> [<a class="logout" href="/cgi-bin/koha/opac-search-history.pl?action=delete" title="Delete your search history" onclick="return confirm(MSG_DELETE_SEARCH_HISTORY);">x</a>]</li>
11
                <li><a href="/cgi-bin/koha/opac-search-history.pl" title="View your search history">Search history</a> [<a class="logout" href="/cgi-bin/koha/opac-search-history.pl?action=delete" title="Delete your search history" onclick="return confirm(MSG_DELETE_SEARCH_HISTORY);">x</a>]</li>
12
            [% END %]
12
            [% END %]
13
			[% IF ( loggedinusername ) %]<li>[% IF persona %]<a class="logout" id="logout" href="/cgi-bin/koha/opac-main.pl?logout.x=1" onclick='navigator.id.logout();'>[% ELSE %]<a class="logout" id="logout" href="/cgi-bin/koha/opac-main.pl?logout.x=1">[% END %]Log Out</a></li>[% END %]
13
			[% IF ( loggedinusername ) %]<li>[% IF persona %]<a class="logout" id="logout" href="/cgi-bin/koha/opac-main.pl?logout.x=1" onclick='navigator.id.logout();'>[% ELSE %]<a class="logout" id="logout" href="/cgi-bin/koha/opac-main.pl?logout.x=1">[% END %]Log Out</a></li>[% END %]
(-)a/koha-tmpl/opac-tmpl/prog/en/includes/usermenu.inc (-1 / +1 lines)
Lines 12-18 Link Here
12
  [% IF ( OpacPasswordChange ) %]
12
  [% IF ( OpacPasswordChange ) %]
13
    [% IF ( passwdview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/opac-passwd.pl">change my password</a></li>
13
    [% IF ( passwdview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/opac-passwd.pl">change my password</a></li>
14
  [% END %]
14
  [% END %]
15
  [% IF ( ShowOpacRecentSearchLink ) %]
15
  [% IF EnableOpacSearchHistory %]
16
  [% IF ( searchhistoryview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/opac-search-history.pl">my search history</a></li>
16
  [% IF ( searchhistoryview ) %]<li class="active">[% ELSE %]<li>[% END %]<a href="/cgi-bin/koha/opac-search-history.pl">my search history</a></li>
17
  [% END %]
17
  [% END %]
18
  [% IF ( opacreadinghistory ) %]
18
  [% IF ( opacreadinghistory ) %]
(-)a/koha-tmpl/opac-tmpl/prog/en/modules/opac-search-history.tt (-67 / +152 lines)
Lines 6-24 Link Here
6
[% INCLUDE 'datatables.inc' %]
6
[% INCLUDE 'datatables.inc' %]
7
<script type="text/javascript">
7
<script type="text/javascript">
8
//<![CDATA[
8
//<![CDATA[
9
	var MSG_CONFIRM_DELETE_HISTORY = _("Are you sure you want to delete your search history?");
9
var MSG_CONFIRM_DELETE_HISTORY = _("Are you sure you want to delete your search history?");
10
         $(document).ready(function() {
10
$(document).ready(function() {
11
		// We show table ordered by descending dates by default
11
    // We show table ordered by descending dates by default
12
		// (so that the more recent query is shown first)
12
    // (so that the more recent query is shown first)
13
            $(".historyt").dataTable($.extend(true, {}, dataTablesDefaults, {
13
    $(".historyt").dataTable($.extend(true, {}, dataTablesDefaults, {
14
                "aaSorting": [[ 0, "desc" ]],
14
        "aaSorting": [[ 0, "desc" ]],
15
                "aoColumns": [
15
        "aoColumns": [
16
                    { "sType": "title-string" },
16
            { "sType": "title-string" },
17
                    null,
17
            null,
18
                    null
18
            null
19
                ]
19
        ]
20
            }));
20
    }));
21
        });
21
22
    $('#tabs').tabs();
23
});
22
//]]>
24
//]]>
23
25
24
</script>
26
</script>
Lines 34-101 Link Here
34
   <div id="bd">
36
   <div id="bd">
35
[% INCLUDE 'masthead.inc' %]
37
[% INCLUDE 'masthead.inc' %]
36
38
37
	<div id="yui-main">
39
<div id="yui-main">
38
<div class="yui-b"><div class="yui-g">
40
  <div class="yui-b">
39
        <div id="searchhistory" class="container">
41
    <div class="yui-g">
40
	<h1>Search history</h1>
42
      <h1>Search history</h1>
41
	[% IF ( recentSearches ) %]<form action="/cgi-bin/koha/opac-search-history.pl" method="get"><input type="hidden" name="action" value="delete" /><input type="submit" class="deleteshelf" value="Delete your search history" onclick="return confirm(MSG_CONFIRM_DELETE_HISTORY);" /></form>[% ELSE %][% IF ( previousSearches ) %]<form action="/cgi-bin/koha/opac-search-history.pl" method="get"><input type="hidden" name="action" value="delete" /><input type="submit" class="deleteshelf" value="Delete your search history" onclick="return confirm(MSG_CONFIRM_DELETE_HISTORY);" /></form>[% END %][% END %]
43
      <div id="tabs" class="toptabs">
44
        <ul>
45
          <li><a href="#biblio_tab">Biblio</a></li>
46
          <li><a href="#authority_tab">Authority</a></li>
47
        </ul>
48
        <div id="biblio_tab">
49
          [% IF ( current_biblio_searches ) %]
50
            <h2>Current session</h2>
51
            <form action="/cgi-bin/koha/opac-search-history.pl" method="get">
52
              <input type="hidden" name="action" value="delete" />
53
              <input type="hidden" name="previous" value="0" />
54
              <input type="hidden" name="type" value="biblio" />
55
              <input type="submit" class="deleteshelf" value="Delete your current biblio history" onclick="return confirm(MSG_CONFIRM_DELETE_HISTORY);" />
56
            </form>
57
            <table class="historyt">
58
              <thead>
59
                <tr>
60
                  <th>Date</th>
61
                  <th>Search</th>
62
                  <th>Results</th>
63
                </tr>
64
              </thead>
65
              <tbody>
66
              [% FOREACH s IN current_biblio_searches %]
67
                <tr>
68
                  <td><span title="[% s.time %]">[% s.time |$KohaDates with_hours => 1 %]</span></td>
69
                  <td><a href="/cgi-bin/koha/opac-search.pl?[% s.query_cgi |html %]">[% s.query_desc |html %]</a></td>
70
                  <td>[% s.total %]</td>
71
                </tr>
72
              [% END %]
73
              </tbody>
74
            </table>
75
          [% END %]
42
76
43
	    [% IF ( recentSearches ) %]
77
          [% IF ( previous_biblio_searches ) %]
44
	    <table class="historyt">
78
            <h2>Previous sessions</h2>
45
	    [% IF ( previousSearches ) %]
79
            <form action="/cgi-bin/koha/opac-search-history.pl" method="get">
46
	    <caption>Current session</caption>
80
              <input type="hidden" name="action" value="delete" />
47
	    [% END %]
81
              <input type="hidden" name="previous" value="1" />
48
		<thead>
82
              <input type="hidden" name="type" value="biblio" />
49
		    <tr><th>Date</th><th>Search</th><th>Results</th></tr>
83
              <input type="submit" class="deleteshelf" value="Delete your previous biblio search history" onclick="return confirm(MSG_CONFIRM_DELETE_HISTORY);" />
50
		</thead>
84
            </form>
51
		<tbody>
85
            <table class="historyt">
52
		    [% FOREACH recentSearche IN recentSearches %]
86
              <thead>
53
		    <tr>
87
                <tr>
54
            <td><span title="[% recentSearche.time %]">[% recentSearche.time %]</span></td>
88
                  <th>Date</th>
55
			<td><a href="/cgi-bin/koha/opac-search.pl?[% recentSearche.query_cgi |html %]">[% recentSearche.query_desc |html %]</a></td>
89
                  <th>Search</th>
56
			<td>[% recentSearche.total %]</td>
90
                  <th>Results</th>
57
		    </tr>
91
                </tr>
58
		    [% END %]
92
              </thead>
59
		</tbody>
93
              <tbody>
60
	    </table>
94
              [% FOREACH s IN previous_biblio_searches %]
61
	    [% END %]
95
                <tr>
96
                  <td><span title="[% s.time %]">[% s.time |$KohaDates with_hours => 1 %]</span></td>
97
                  <td><a href="/cgi-bin/koha/opac-search.pl?[% s.query_cgi |html %]">[% s.query_desc |html %]</a></td>
98
                  <td>[% s.total %]</td>
99
                </tr>
100
              [% END %]
101
              </tbody>
102
            </table>
103
          [% END %]
62
104
63
	    [% IF ( previousSearches ) %]
105
        <div id="authority_tab">
64
	    <table class="historyt">
106
          [% IF ( current_authority_searches ) %]
65
	    <caption>Previous sessions</caption>
107
            <h2>Current session</h2>
66
		<thead>
108
            <form action="/cgi-bin/koha/opac-search-history.pl" method="get">
67
		    <tr><th>Date</th><th>Search</th><th>Results</th></tr>
109
              <input type="hidden" name="action" value="delete" />
68
		</thead>
110
              <input type="hidden" name="previous" value="0" />
69
		<tbody>
111
              <input type="hidden" name="type" value="authority" />
70
		    [% FOREACH previousSearche IN previousSearches %]
112
              <input type="submit" class="deleteshelf" value="Delete your current authority search history" onclick="return confirm(MSG_CONFIRM_DELETE_HISTORY);" />
71
		    <tr>
113
            </form>
72
            <td><span title="[% previousSearche.time %]">[% previousSearche.time |$KohaDates with_hours => 1 %]</span></td>
114
            <table class="historyt">
73
			<td><a href="/cgi-bin/koha/opac-search.pl?[% previousSearche.query_cgi |html %]">[% previousSearche.query_desc |html %]</a></td>
115
              <thead>
74
			<td>[% previousSearche.total %]</td>
116
                <tr>
75
		    </tr>
117
                  <th>Date</th>
76
		    [% END %]
118
                  <th>Search</th>
77
		</tbody>
119
                  <th>Results</th>
78
	    </table>
120
                </tr>
79
	    [% END %]
121
              </thead>
122
              <tbody>
123
              [% FOREACH s IN current_authority_searches %]
124
                <tr>
125
                  <td><span title="[% s.time %]">[% s.time %]</span></td>
126
                  <td><a href="/cgi-bin/koha/opac-authorities-home.pl?[% s.query_cgi |html %]">[% s.query_desc |html %]</a></td>
127
                  <td>[% s.total %]</td>
128
                </tr>
129
              [% END %]
130
              </tbody>
131
            </table>
132
          [% END %]
80
133
81
[% IF ( recentSearches ) %][% ELSE %][% IF ( previousSearches ) %][% ELSE %]<p>Your search history is empty.</p>[% END %][% END %]
134
          [% IF ( previous_authority_searches ) %]
135
            <h2>Previous sessions</h2>
136
            <form action="/cgi-bin/koha/opac-search-history.pl" method="get">
137
              <input type="hidden" name="action" value="delete" />
138
              <input type="hidden" name="previous" value="1" />
139
              <input type="hidden" name="type" value="authority" />
140
              <input type="submit" class="deleteshelf" value="Delete your previous authority search history" onclick="return confirm(MSG_CONFIRM_DELETE_HISTORY);" />
141
            </form>
142
            <table class="historyt">
143
              <thead>
144
                <tr>
145
                  <th>Date</th>
146
                  <th>Search</th>
147
                  <th>Results</th>
148
                </tr>
149
              </thead>
150
              <tbody>
151
              [% FOREACH s IN previous_authority_searches %]
152
                <tr>
153
                  <td><span title="[% s.time %]">[% s.time |$KohaDates with_hours => 1 %]</span></td>
154
                  <td><a href="/cgi-bin/koha/opac-authorities-home.pl?[% s.query_cgi |html %]">[% s.query_desc |html %]</a></td>
155
                  <td>[% s.total %]</td>
156
                </tr>
157
              [% END %]
158
              </tbody>
159
            </table>
160
          [% END %]
82
161
83
     </div>
162
          [% IF !current_authority_searches && !previous_authority_searches %]
84
     </div>
163
            <p>Your authority search history is empty.</p>
85
     </div>
164
          [% END %]
86
     </div>
165
        </div>
166
      </div>
167
    </div>
168
  </div>
169
</div>
87
170
88
[% IF ( OpacNav ) %]
171
[% IF ( OpacNav ) %]
89
<div class="yui-b"><div id="leftmenus" class="container">
172
  <div class="yui-b">
90
[% INCLUDE 'navigation.inc' IsPatronPage=1 %]
173
    <div id="leftmenus" class="container">
91
</div></div>
174
      [% INCLUDE 'navigation.inc' IsPatronPage=1 %]
175
    </div>
176
  </div>
92
[% ELSIF ( loggedinusername ) %]
177
[% ELSIF ( loggedinusername ) %]
93
<div class="yui-b"><div id="leftmenus" class="container">
178
  <div class="yui-b">
94
[% INCLUDE 'navigation.inc' IsPatronPage=1 %]
179
    <div id="leftmenus" class="container">
95
</div></div>
180
      [% INCLUDE 'navigation.inc' IsPatronPage=1 %]
96
[% ELSE %]
181
    </div>
182
  </div>
97
[% END %]
183
[% END %]
98
184
99
100
</div>
185
</div>
101
[% INCLUDE 'opac-bottom.inc' %]
186
[% INCLUDE 'opac-bottom.inc' %]
(-)a/opac/opac-authorities-home.pl (+33 lines)
Lines 22-27 use strict; Link Here
22
use warnings;
22
use warnings;
23
23
24
use CGI;
24
use CGI;
25
25
use C4::Auth;
26
use C4::Auth;
26
27
27
use C4::Context;
28
use C4::Context;
Lines 29-34 use C4::Auth; Link Here
29
use C4::Output;
30
use C4::Output;
30
use C4::AuthoritiesMarc;
31
use C4::AuthoritiesMarc;
31
use C4::Koha;    # XXX subfield_is_koha_internal_p
32
use C4::Koha;    # XXX subfield_is_koha_internal_p
33
use C4::Search::History;
32
34
33
my $query        = new CGI;
35
my $query        = new CGI;
34
my $op           = $query->param('op') || '';
36
my $op           = $query->param('op') || '';
Lines 129-134 if ( $op eq "do_search" ) { Link Here
129
        my @usedauths = grep { $_->{used} > 0 } @$results;
131
        my @usedauths = grep { $_->{used} > 0 } @$results;
130
        $results = \@usedauths;
132
        $results = \@usedauths;
131
    }
133
    }
134
135
    # Opac search history
136
    if (C4::Context->preference('EnableOpacSearchHistory')) {
137
        unless ( $startfrom ) {
138
            my $path_info = $query->url(-path_info=>1);
139
            my $query_cgi_history = $query->url(-query=>1);
140
            $query_cgi_history =~ s/^$path_info\?//;
141
            $query_cgi_history =~ s/;/&/g;
142
143
            unless ( $loggedinuser ) {
144
                my $new_search = C4::Search::History::add_to_session({
145
                        cgi => $query,
146
                        query_desc => $value[0],
147
                        query_cgi => $query_cgi_history,
148
                        total => $total,
149
                        type => "authority",
150
                });
151
            } else {
152
                # To the session (the user is logged in)
153
                C4::Search::History::add({
154
                    userid => $loggedinuser,
155
                    sessionid => $query->cookie("CGISESSID"),
156
                    query_desc => $value[0],
157
                    query_cgi => $query_cgi_history,
158
                    total => $total,
159
                    type => "authority",
160
                });
161
            }
162
        }
163
    }
164
132
    $template->param( result => $results ) if $results;
165
    $template->param( result => $results ) if $results;
133
    $template->param( FIELDS => \@fields );
166
    $template->param( FIELDS => \@fields );
134
    $template->param( orderby => $orderby );
167
    $template->param( orderby => $orderby );
(-)a/opac/opac-search-history.pl (-77 / +92 lines)
Lines 1-6 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
# Copyright 2009 BibLibre SARL
3
# Copyright 2013 BibLibre SARL
4
#
4
#
5
# This file is part of Koha.
5
# This file is part of Koha.
6
#
6
#
Lines 17-34 Link Here
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
19
20
use strict;
20
use Modern::Perl;
21
use warnings;
22
21
23
use C4::Auth qw(:DEFAULT get_session ParseSearchHistorySession SetSearchHistorySession);
22
use C4::Auth qw(:DEFAULT get_session);
24
use CGI;
23
use CGI;
25
use JSON qw/decode_json encode_json/;
26
use C4::Context;
24
use C4::Context;
27
use C4::Output;
25
use C4::Output;
28
use C4::Log;
26
use C4::Log;
29
use C4::Items;
27
use C4::Items;
30
use C4::Debug;
28
use C4::Debug;
31
use C4::Dates;
29
use C4::Dates;
30
use C4::Search::History;
32
use URI::Escape;
31
use URI::Escape;
33
use POSIX qw(strftime);
32
use POSIX qw(strftime);
34
33
Lines 36-135 use POSIX qw(strftime); Link Here
36
my $cgi = new CGI;
35
my $cgi = new CGI;
37
36
38
# Getting the template and auth
37
# Getting the template and auth
39
my ($template, $loggedinuser, $cookie)= get_template_and_user({template_name => "opac-search-history.tmpl",
38
my ($template, $loggedinuser, $cookie) = get_template_and_user(
40
                                query => $cgi,
39
    {
41
                                type => "opac",
40
        template_name => "opac-search-history.tmpl",
42
                                authnotrequired => 1,
41
        query => $cgi,
43
                                flagsrequired => {borrowers => 1},
42
        type => "opac",
44
                                debug => 1,
43
        authnotrequired => 1,
45
                                });
44
        flagsrequired => {borrowers => 1},
45
        debug => 1,
46
    }
47
);
46
48
47
# If the user is not logged in, we deal with the session
49
my $type = $cgi->param('type');
48
if (!$loggedinuser) {
50
my $action = $cgi->param('action') || q{};
51
my $previous = $cgi->param('previous');
49
52
53
# If the user is not logged in, we deal with the session
54
unless ( $loggedinuser ) {
50
    # Deleting search history
55
    # Deleting search history
51
    if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
56
    if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
52
        # Deleting session's search history
57
        # Deleting session's search history
53
        SetSearchHistorySession($cgi, []);
58
        my $type = $cgi->param('type');
59
        my @searches = ();
60
        if ( $type ) {
61
            @searches = C4::Search::History::get_from_session({ cgi => $cgi });
62
            @searches = map { $_->{type} ne $type ? $_ : () } @searches;
63
        }
64
        C4::Search::History::set_to_session({ cgi => $cgi, search_history => \@searches });
54
65
55
        # Redirecting to this same url so the user won't see the search history link in the header
66
        # Redirecting to this same url so the user won't see the search history link in the header
56
        my $uri = $cgi->url();
67
        my $uri = $cgi->url();
57
        print $cgi->redirect(-uri => $uri);
68
        print $cgi->redirect(-uri => $uri);
58
    # Showing search history
69
    # Showing search history
59
    } else {
70
    } else {
60
71
        # Getting the searches from session
61
        my @recentSearches = ParseSearchHistorySession($cgi);
72
        my @current_searches = C4::Search::History::get_from_session({
62
	    if (@recentSearches) {
73
            cgi => $cgi,
63
74
        });
64
		# As the dates are stored as unix timestamps, let's do some formatting
75
65
		foreach my $asearch (@recentSearches) {
76
        my @current_biblio_searches = map {
66
77
            $_->{type} eq 'biblio' ? $_ : ()
67
		    # We create an iso date from the unix timestamp
78
        } @current_searches;
68
		    my $isodate = strftime "%Y-%m-%d", localtime($asearch->{'time'});
79
69
80
        my @current_authority_searches = map {
70
		    # We also get the time of the day from the unix timestamp
81
            $_->{type} eq 'authority' ? $_ : ()
71
		    my $time = strftime " %H:%M:%S", localtime($asearch->{'time'});
82
        } @current_searches;
72
83
73
		    # And we got our human-readable date : 
84
        $template->param(
74
            $asearch->{'time'} = $isodate . $time;
85
            current_biblio_searches => \@current_biblio_searches,
75
		}
86
            current_authority_searches => \@current_authority_searches,
76
87
        );
77
		$template->param(recentSearches => \@recentSearches);
78
	    }
79
    }
88
    }
80
} else {
89
} else {
81
# And if the user is logged in, we deal with the database
90
    # And if the user is logged in, we deal with the database
82
   
83
    my $dbh = C4::Context->dbh;
91
    my $dbh = C4::Context->dbh;
84
92
85
    # Deleting search history
93
    # Deleting search history
86
    if ($cgi->param('action') && $cgi->param('action') eq 'delete') {
94
    if ( $action eq 'delete' ) {
87
	my $query = "DELETE FROM search_history WHERE userid = ?";
95
        my $sessionid = defined $previous
88
	my $sth   = $dbh->prepare($query);
96
            ? $cgi->cookie("CGISESSID")
89
	$sth->execute($loggedinuser);
97
            : q{};
90
98
        C4::Search::History::delete(
91
	# Redirecting to this same url so the user won't see the search history link in the header
99
            {
92
	my $uri = $cgi->url();
100
                userid => $loggedinuser,
93
	print $cgi->redirect($uri);
101
                sessionid => $sessionid,
94
102
                type => $type,
103
                previous => $previous
104
            }
105
        );
106
        # Redirecting to this same url so the user won't see the search history link in the header
107
        my $uri = $cgi->url();
108
        print $cgi->redirect($uri);
95
109
96
    # Showing search history
110
    # Showing search history
97
    } else {
111
    } else {
98
112
        my $current_searches = C4::Search::History::get({
99
	my $date = C4::Dates->new();
113
            userid => $loggedinuser,
100
	my $dateformat = $date->DHTMLcalendar() . " %H:%i:%S"; # Current syspref date format + standard time format
114
            sessionid => $cgi->cookie("CGISESSID")
101
115
        });
102
	# Getting the data with date format work done by mysql
116
        my @current_biblio_searches = map {
103
    my $query = "SELECT userid, sessionid, query_desc, query_cgi, total, time FROM search_history WHERE userid = ? AND sessionid = ?";
117
            $_->{type} eq 'biblio' ? $_ : ()
104
	my $sth   = $dbh->prepare($query);
118
        } @$current_searches;
105
	$sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
119
106
	my $searches = $sth->fetchall_arrayref({});
120
        my @current_authority_searches = map {
107
	$template->param(recentSearches => $searches);
121
            $_->{type} eq 'authority' ? $_ : ()
108
	
122
        } @$current_searches;
109
	# Getting searches from previous sessions
123
110
	$query = "SELECT COUNT(*) FROM search_history WHERE userid = ? AND sessionid != ?";
124
        my $previous_searches = C4::Search::History::get({
111
	$sth   = $dbh->prepare($query);
125
            userid => $loggedinuser,
112
	$sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
126
            sessionid => $cgi->cookie("CGISESSID"),
113
127
            previous => 1
114
	# If at least one search from previous sessions has been performed
128
        });
115
        if ($sth->fetchrow_array > 0) {
129
116
        $query = "SELECT userid, sessionid, query_desc, query_cgi, total, time FROM search_history WHERE userid = ? AND sessionid != ?";
130
        my @previous_biblio_searches = map {
117
	    $sth   = $dbh->prepare($query);
131
            $_->{type} eq 'biblio' ? $_ : ()
118
	    $sth->execute($loggedinuser, $cgi->cookie("CGISESSID"));
132
        } @$previous_searches;
119
    	    my $previoussearches = $sth->fetchall_arrayref({});
133
120
    	    $template->param(previousSearches => $previoussearches);
134
        my @previous_authority_searches = map {
121
	
135
            $_->{type} eq 'authority' ? $_ : ()
122
	}
136
        } @$previous_searches;
123
137
124
	$sth->finish;
138
        $template->param(
125
139
            current_biblio_searches => \@current_biblio_searches,
126
140
            current_authority_searches => \@current_authority_searches,
141
            previous_biblio_searches => \@previous_biblio_searches,
142
            previous_authority_searches => \@previous_authority_searches,
143
144
        );
127
    }
145
    }
128
129
}
146
}
130
147
131
$template->param(searchhistoryview => 1);
148
$template->param(searchhistoryview => 1);
132
149
133
output_html_with_http_headers $cgi, $cookie, $template->output;
150
output_html_with_http_headers $cgi, $cookie, $template->output;
134
135
(-)a/opac/opac-search.pl (-30 / +29 lines)
Lines 42-50 for ( $searchengine ) { Link Here
42
}
42
}
43
43
44
use C4::Output;
44
use C4::Output;
45
use C4::Auth qw(:DEFAULT get_session ParseSearchHistorySession SetSearchHistorySession);
45
use C4::Auth qw(:DEFAULT get_session);
46
use C4::Languages qw(getLanguages);
46
use C4::Languages qw(getLanguages);
47
use C4::Search;
47
use C4::Search;
48
use C4::Search::History;
48
use C4::Biblio;  # GetBiblioData
49
use C4::Biblio;  # GetBiblioData
49
use C4::Koha;
50
use C4::Koha;
50
use C4::Tags qw(get_tags);
51
use C4::Tags qw(get_tags);
Lines 616-653 for (my $i=0;$i<@servers;$i++) { Link Here
616
617
617
        # Opac search history
618
        # Opac search history
618
        if (C4::Context->preference('EnableOpacSearchHistory')) {
619
        if (C4::Context->preference('EnableOpacSearchHistory')) {
619
            my @recentSearches = ParseSearchHistorySession($cgi);
620
            unless ( $offset ) {
620
621
                my $path_info = $cgi->url(-path_info=>1);
621
            # Adding the new search if needed
622
                my $query_cgi_history = $cgi->url(-query=>1);
622
            my $path_info = $cgi->url(-path_info=>1);
623
                $query_cgi_history =~ s/^$path_info\?//;
623
            my $query_cgi_history = $cgi->url(-query=>1);
624
                $query_cgi_history =~ s/;/&/g;
624
            $query_cgi_history =~ s/^$path_info\?//;
625
                my $query_desc_history = join ", ", grep { defined $_ } $query_desc, $limit_desc;
625
            $query_cgi_history =~ s/;/&/g;
626
626
            my $query_desc_history = join ", ", grep { defined $_ } $query_desc, $limit_desc;
627
                unless ( $borrowernumber ) {
627
628
                    my $new_searches = C4::Search::History::add_to_session({
628
            if (!$borrowernumber || $borrowernumber eq '') {
629
                            cgi => $cgi,
629
                # To the session (the user is not logged in)
630
                            query_desc => $query_desc_history,
630
                if (!$offset) {
631
                            query_cgi => $query_cgi_history,
631
                    push @recentSearches, {
632
                            total => $total,
632
                                "query_desc" => Encode::decode_utf8($query_desc_history) || "unknown",
633
                            type => "biblio",
633
                                "query_cgi"  => Encode::decode_utf8($query_cgi_history)  || "unknown",
634
                    });
634
                                "time"       => time(),
635
                } else {
635
                                "total"      => $total
636
                    # To the session (the user is logged in)
636
                              };
637
                    C4::Search::History::add({
637
                    $template->param(ShowOpacRecentSearchLink => 1);
638
                        userid => $borrowernumber,
638
                }
639
                        sessionid => $cgi->cookie("CGISESSID"),
639
640
                        query_desc => $query_desc_history,
640
                shift @recentSearches if (@recentSearches > 15);
641
                        query_cgi => $query_cgi_history,
641
                SetSearchHistorySession($cgi, \@recentSearches);
642
                        total => $total,
642
            }
643
                        type => "biblio",
643
            else {
644
                    });
644
                # To the database (the user is logged in)
645
                if (!$offset) {
646
                    AddSearchHistory($borrowernumber, $cgi->cookie("CGISESSID"), $query_desc_history, $query_cgi_history, $total);
647
                    $template->param(ShowOpacRecentSearchLink => 1);
648
                }
645
                }
649
            }
646
            }
647
            $template->param( EnableOpacSearchHistory => 1 );
650
        }
648
        }
649
651
        ## If there's just one result, redirect to the detail page
650
        ## If there's just one result, redirect to the detail page
652
        if ($total == 1 && $format ne 'rss2'
651
        if ($total == 1 && $format ne 'rss2'
653
        && $format ne 'opensearchdescription' && $format ne 'atom') {
652
        && $format ne 'opensearchdescription' && $format ne 'atom') {
(-)a/t/db_dependent/Auth_SearchHistorySession.t (-12 / +15 lines)
Lines 1-12 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/env perl
2
2
3
use strict;
3
use Modern::Perl;
4
use warnings;
5
4
6
use Test::More tests => 4;
5
use Test::More tests => 6;
6
use URI::Escape;
7
use JSON qw( decode_json );
7
8
8
use_ok('C4::Auth', qw/ParseSearchHistorySession SetSearchHistorySession get_session/);
9
use_ok('Koha::DateUtils');
10
use_ok('C4::Search::History');
11
use_ok('C4::Auth', qw/get_session/ );
9
12
13
# Test session
10
my $expected_recent_searches = [
14
my $expected_recent_searches = [
11
    {
15
    {
12
        'time' => 1374978877,
16
        'time' => 1374978877,
Lines 17-35 my $expected_recent_searches = [ Link Here
17
];
21
];
18
22
19
# Create new session and put its id into CGISESSID cookie
23
# Create new session and put its id into CGISESSID cookie
20
my $session = get_session("");
24
my $session = C4::Auth::get_session("");
21
$session->flush;
25
$session->flush;
22
my $input = new CookieSimulator({CGISESSID => $session->id});
26
my $input = new CookieSimulator({CGISESSID => $session->id});
23
27
24
my @recent_searches = ParseSearchHistorySession($input);
28
my @recent_searches = C4::Search::History::get_from_session({ cgi => $input });
25
is_deeply(\@recent_searches, [], 'at start, there is no recent searches');
29
is_deeply(\@recent_searches, [], 'at start, there is no recent searches');
26
30
27
SetSearchHistorySession($input, $expected_recent_searches);
31
C4::Search::History::set_to_session({ cgi => $input, search_history => $expected_recent_searches });
28
@recent_searches = ParseSearchHistorySession($input);
32
@recent_searches = C4::Search::History::get_from_session({ cgi => $input });
29
is_deeply(\@recent_searches, $expected_recent_searches, 'recent searches set and retrieved successfully');
33
is_deeply(\@recent_searches, $expected_recent_searches, 'recent searches set and retrieved successfully');
30
34
31
SetSearchHistorySession($input, []);
35
C4::Search::History::set_to_session({ cgi => $input, search_history => [] });
32
@recent_searches = ParseSearchHistorySession($input);
36
@recent_searches = C4::Search::History::get_from_session({ cgi => $input });
33
is_deeply(\@recent_searches, [], 'recent searches emptied successfully');
37
is_deeply(\@recent_searches, [], 'recent searches emptied successfully');
34
38
35
# Delete session
39
# Delete session
Lines 49-52 sub cookie { Link Here
49
    return $self->{$name};
53
    return $self->{$name};
50
}
54
}
51
55
52
1;
(-)a/t/db_dependent/Search/History.t (-1 / +248 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
use Modern::Perl;
4
5
use Test::More tests => 16;
6
use URI::Escape;
7
8
use C4::Context;
9
my $dbh = C4::Context->dbh;
10
$dbh->{AutoCommit} = 0;
11
$dbh->{RaiseError} = 1;
12
13
use_ok('Koha::DateUtils');
14
use_ok('C4::Search::History');
15
16
my $userid = 123;
17
my $previous_sessionid = "PREVIOUS_SESSIONID";
18
my $current_sessionid = "CURRENT_SESSIONID";
19
my $total = 42;
20
my $query_cgi_b = q{idx=kw&idx=ti&idx=au%2Cwrdl&q=word1é&q=word2è&q=word3à&do=Search&sort_by=author_az};
21
my $query_cgi_a = q{op=do_search&type=opac&authtypecode=NP&operator=start&value=Harry&marclist=match&and_or=and&orderby=HeadingAsc};
22
23
# add
24
my $added = add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
25
is ( $added, 9, '9 searches are added' );
26
27
# get
28
my $searches_for_userid = C4::Search::History::get({
29
    userid => $userid,
30
});
31
is( scalar(@$searches_for_userid), 9, 'There are 9 searches in all' );
32
33
my $searches_for_current_session = C4::Search::History::get({
34
    userid => $userid,
35
    sessionid => $current_sessionid,
36
});
37
is( scalar(@$searches_for_current_session), 5, 'There are 5 searches for the current session' );
38
39
my $searches_for_previous_sessions = C4::Search::History::get({
40
    userid => $userid,
41
    sessionid => $current_sessionid,
42
    previous => 1,
43
});
44
is( scalar(@$searches_for_previous_sessions), 4, 'There are 4 searches for previous sessions' );
45
46
my $authority_searches_for_current_session = C4::Search::History::get({
47
    userid => $userid,
48
    sessionid => $current_sessionid,
49
    type => 'authority',
50
});
51
is( scalar(@$authority_searches_for_current_session), 3, 'There are 3 authority searches for the current session' );
52
53
my $authority_searches_for_previous_session = C4::Search::History::get({
54
    userid => $userid,
55
    sessionid => $current_sessionid,
56
    type => 'authority',
57
    previous => 1,
58
});
59
is( scalar(@$authority_searches_for_previous_session), 2, 'There are 2 authority searches for previous sessions' );
60
61
my $biblio_searches_for_userid = C4::Search::History::get({
62
    userid => $userid,
63
    type => 'biblio',
64
});
65
is( scalar(@$biblio_searches_for_userid), 4, 'There are 5 searches for the current session' );
66
67
my $authority_searches_for_userid = C4::Search::History::get({
68
    userid => $userid,
69
    type => 'authority',
70
});
71
is( scalar(@$authority_searches_for_userid), 5, 'There are 4 searches for previous sessions' );
72
73
delete_all( $userid );
74
75
# delete
76
add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
77
C4::Search::History::delete({
78
    userid => $userid,
79
    sessionid => $current_sessionid,
80
    type => 'authority',
81
});
82
my $all = C4::Search::History::get({userid => $userid});
83
is( scalar(@$all), 6, 'There are 6 searches in all after deleting current biblio searches' );
84
delete_all( $userid );
85
86
add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
87
C4::Search::History::delete({
88
    userid => $userid,
89
    sessionid => $current_sessionid,
90
    type => 'biblio',
91
    previous => 1,
92
});
93
$all = C4::Search::History::get({userid => $userid});
94
is( scalar(@$all), 7, 'There are 7 searches in all after deleting previous authority searches' );
95
delete_all( $userid );
96
97
add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
98
C4::Search::History::delete({
99
    userid => $userid,
100
    sessionid => $current_sessionid,
101
    previous => 1,
102
});
103
$all = C4::Search::History::get({userid => $userid});
104
is( scalar(@$all), 5, 'There are 5 searches in all after deleting all previous searches' );
105
delete_all( $userid );
106
107
add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
108
C4::Search::History::delete({
109
    userid => $userid,
110
    sessionid => $current_sessionid,
111
});
112
$all = C4::Search::History::get({userid => $userid});
113
is( scalar(@$all), 4, 'There are 5 searches in all after deleting all searches for a sessionid' );
114
delete_all( $userid );
115
116
add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
117
C4::Search::History::delete({
118
    userid => $userid,
119
});
120
$all = C4::Search::History::get({userid => $userid});
121
is( scalar(@$all), 0, 'There are 0 search after deleting all searches for a userid' );
122
delete_all( $userid );
123
124
add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
125
C4::Search::History::delete({});
126
$all = C4::Search::History::get({userid => $userid});
127
is( scalar(@$all), 9, 'There are still 9 searches after calling delete without userid' );
128
delete_all( $userid );
129
130
sub add {
131
    my ( $userid, $current_session_id, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a ) = @_;
132
133
    my $query_desc_b1_p = q{first previous biblio search};
134
    my $first_previous_biblio_search = {
135
        userid => $userid,
136
        sessionid => $previous_sessionid,
137
        query_desc => $query_desc_b1_p,
138
        query_cgi => $query_cgi_b,
139
        total => $total,
140
        type => 'biblio',
141
    };
142
143
    my $query_desc_a1_p = q{first previous authority search};
144
    my $first_previous_authority_search = {
145
        userid => $userid,
146
        sessionid => $previous_sessionid,
147
        query_desc => $query_desc_a1_p,
148
        query_cgi => $query_cgi_a,
149
        total => $total,
150
        type => 'authority',
151
    };
152
153
    my $query_desc_b2_p = q{second previous biblio search};
154
    my $second_previous_biblio_search = {
155
        userid => $userid,
156
        sessionid => $previous_sessionid,
157
        query_desc => $query_desc_b2_p,
158
        query_cgi => $query_cgi_b,
159
        total => $total,
160
        type => 'biblio',
161
    };
162
163
    my $query_desc_a2_p = q{second previous authority search};
164
    my $second_previous_authority_search = {
165
        userid => $userid,
166
        sessionid => $previous_sessionid,
167
        query_desc => $query_desc_a2_p,
168
        query_cgi => $query_cgi_a,
169
        total => $total,
170
        type => 'authority',
171
    };
172
173
174
    my $query_desc_b1_c = q{first current biblio search};
175
176
    my $first_current_biblio_search = {
177
        userid => $userid,
178
        sessionid => $current_sessionid,
179
        query_desc => $query_desc_b1_c,
180
        query_cgi => $query_cgi_b,
181
        total => $total,
182
        type => 'biblio',
183
    };
184
185
    my $query_desc_a1_c = q{first current authority search};
186
    my $first_current_authority_search = {
187
        userid => $userid,
188
        sessionid => $current_sessionid,
189
        query_desc => $query_desc_a1_c,
190
        query_cgi => $query_cgi_a,
191
        total => $total,
192
        type => 'authority',
193
    };
194
195
    my $query_desc_b2_c = q{second current biblio search};
196
    my $second_current_biblio_search = {
197
        userid => $userid,
198
        sessionid => $current_sessionid,
199
        query_desc => $query_desc_b2_c,
200
        query_cgi => $query_cgi_b,
201
        total => $total,
202
        type => 'biblio',
203
    };
204
205
    my $query_desc_a2_c = q{second current authority search};
206
    my $second_current_authority_search = {
207
        userid => $userid,
208
        sessionid => $current_sessionid,
209
        query_desc => $query_desc_a2_c,
210
        query_cgi => $query_cgi_a,
211
        total => $total,
212
        type => 'authority',
213
    };
214
215
    my $query_desc_a3_c = q{third current authority search};
216
    my $third_current_authority_search = {
217
        userid => $userid,
218
        sessionid => $current_sessionid,
219
        query_desc => $query_desc_a3_c,
220
        query_cgi => $query_cgi_a,
221
        total => $total,
222
        type => 'authority',
223
    };
224
225
226
    my $r = 0;
227
    $r += C4::Search::History::add( $first_current_biblio_search );
228
    $r += C4::Search::History::add( $first_current_authority_search );
229
    $r += C4::Search::History::add( $second_current_biblio_search );
230
    $r += C4::Search::History::add( $second_current_authority_search );
231
    $r += C4::Search::History::add( $first_previous_biblio_search );
232
    $r += C4::Search::History::add( $first_previous_authority_search );
233
    $r += C4::Search::History::add( $second_previous_biblio_search );
234
    $r += C4::Search::History::add( $second_previous_authority_search );
235
    $r += C4::Search::History::add( $third_current_authority_search );
236
    return $r;
237
}
238
239
sub delete_all {
240
    my $userid = shift;
241
    C4::Search::History::delete({
242
        userid => $userid,
243
    });
244
}
245
246
$dbh->rollback;
247
248
done_testing;

Return to bug 10807