1 package C4
::Search
::History
;
5 use C4
::Auth
qw( get_session );
9 use JSON
qw( encode_json decode_json );
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';
22 my $dbh = C4
::Context
->dbh;
24 # Add the request the user just made
26 INSERT INTO search_history(
27 userid, sessionid, query_desc, query_cgi, type, total, time
29 ?, ?, ?, ?, ?, ?, NOW()
32 my $sth = $dbh->prepare($query);
33 $sth->execute( $userid, $sessionid, $query_desc, $query_cgi, $type,
39 my $cgi = $params->{cgi
};
40 my $query_desc = $params->{query_desc
} || "unknown";
41 my $query_cgi = $params->{query_cgi
} || "unknown";
42 my $total = $params->{total
};
43 my $type = $params->{type
} || 'biblio';
45 # To a cookie (the user is not logged in)
46 my $now = dt_from_string
;
47 my $id = $now->year . $now->month . $now->day . $now->hour . $now->minute . $now->second . int(rand(100));
48 my @recent_searches = get_from_session
( { cgi
=> $cgi } );
49 push @recent_searches, {
50 query_desc
=> $query_desc,
51 query_cgi
=> $query_cgi,
54 time => output_pref
( { dt
=> $now, dateformat
=> 'iso', timeformat
=> '24hr' } ),
58 shift @recent_searches if ( @recent_searches > 15 );
59 set_to_session
( { cgi
=> $cgi, search_history
=> \
@recent_searches } );
64 my $id = $params->{id
};
65 my $userid = $params->{userid
};
66 my $sessionid = $params->{sessionid
};
67 my $type = $params->{type
} || q{};
68 my $previous = $params->{previous
} || 0;
70 unless ( ref( $id ) ) {
71 $id = $id ?
[ $id ] : [];
74 unless ( $userid or @
$id ) {
75 warn "ERROR: userid or id is required for history deletion";
79 my $dbh = C4
::Context
->dbh;
81 DELETE FROM search_history
85 $query .= q{ AND id IN ( } . join( q{,}, (q{?}) x @
$id ) . q{ )}
95 ?
q{ AND sessionid != ?}
96 : q{ AND sessionid = ?};
99 $query .= q{ AND type = ?}
104 ( @
$id ?
( @
$id ) : () ),
105 ( $userid ?
$userid : () ),
106 ( $sessionid ?
$sessionid : () ),
107 ( $type ?
$type : () )
111 sub delete_from_cookie
{
113 my $cookie = $params->{cookie
};
114 my $id = $params->{id
};
116 return unless $cookie;
118 unless ( ref( $id ) ) {
119 $id = $id ?
[ $id ] : [];
125 $cookie = uri_unescape
( $cookie );
126 if (decode_json
( $cookie )) {
127 @searches = @
{decode_json
( $cookie )}
133 ( grep { $_ != $search->{id
} } @
$id ) ?
$search : ()
136 return uri_escape
( encode_json
( \
@searches ) );
142 my $id = $params->{id
};
143 my $userid = $params->{userid
};
144 my $sessionid = $params->{sessionid
};
145 my $type = $params->{type
};
146 my $previous = $params->{previous
};
148 unless ( ref( $id ) ) {
149 $id = $id ?
[ $id ] : [];
152 unless ( $userid or @
$id ) {
153 warn "ERROR: userid is required for history search";
163 $query .= q{ AND id IN ( } . join( q{,}, (q{?}) x @
$id ) . q{ )}
173 ?
q{ AND sessionid != ?}
174 : q{ AND sessionid = ?};
177 $query .= q{ AND type = ?}
180 my $dbh = C4
::Context
->dbh;
181 my $sth = $dbh->prepare($query);
183 ( @
$id ?
( @
$id ) : () ),
184 ( $userid ?
$userid : () ),
185 ( $sessionid ?
$sessionid : () ),
186 ( $type ?
$type : () )
188 return $sth->fetchall_arrayref( {} );
191 sub get_from_session
{
193 my $cgi = $params->{cgi
};
194 my $sessionID = $cgi->cookie('CGISESSID');
195 return () unless $sessionID;
196 my $session = C4
::Auth
::get_session
($sessionID);
197 return () unless $session and $session->param('search_history');
199 eval { decode_json
( uri_unescape
( $session->param('search_history') ) ) };
200 return () unless defined $obj;
201 return () unless ref $obj eq 'ARRAY';
207 my $cgi = $params->{cgi
};
208 my $search_history = $params->{search_history
};
209 my $sessionID = $cgi->cookie('CGISESSID');
210 return () unless $sessionID;
211 my $session = C4
::Auth
::get_session
($sessionID);
212 return () unless $session;
213 $session->param( 'search_history',
214 uri_escape
( encode_json
($search_history) ) );
225 C4::Search::History - Manage search history
229 This module provides some routines for the search history management.
230 It deals with session or database.
236 C4::Search::History::add({
238 sessionid => $cgi->cookie("CGIESSID"),
239 query_desc => $query_desc,
240 query_cgi => $query_cgi,
245 type is "biblio" or "authority".
247 Add a new search to the user's history.
249 =head2 add_to_session
251 my $value = C4::Search::History::add_to_session({
253 query_desc => $query_desc,
254 query_cgi => $query_cgi,
259 Add a search to the session. The number of searches to keep is hardcoded to 15.
263 C4::Search::History::delete({
264 userid => $loggedinuser,
265 sessionid => $sessionid,
267 previous => $previous
270 Delete searches in the database.
271 If the sessionid is missing all searches for all sessions will be deleted.
272 It is possible to delete searches for current session or all previous sessions using the previous flag.
273 If the type ("biblio" or "authority") is missing, all type will be deleted.
274 To delete *all* searches for a given userid, just pass a userid.
278 my $searches C4::Search::History::get({
280 sessionsid => $sessionid,
282 previous => $previous
285 Return a list of searches for a given userid.
286 If a sessionid is given, searches are limited to the matching session.
287 type and previous follow the same behavior as the delete routine.
289 =head2 get_from_session
291 my $searches = C4::Search::History::get_from_session({
295 Return all searches present for the given session.
297 =head2 set_to_session
299 C4::Search::History::set_to_session({
301 search_history => $search_history
304 Store searches into the session.
308 Jonathan Druart <jonathan.druart@biblibre.com>
312 This file is part of Koha.
314 Copyright 2013 BibLibre SARL
316 Koha is free software; you can redistribute it and/or modify it
317 under the terms of the GNU General Public License as published by
318 the Free Software Foundation; either version 3 of the License, or
319 (at your option) any later version.
321 Koha is distributed in the hope that it will be useful, but
322 WITHOUT ANY WARRANTY; without even the implied warranty of
323 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
324 GNU General Public License for more details.
326 You should have received a copy of the GNU General Public License
327 along with Koha; if not, see <http://www.gnu.org/licenses>.