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 = 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';
45 my @recent_searches = get_from_session
( { cgi
=> $cgi } );
46 push @recent_searches,
48 query_desc
=> $query_desc,
49 query_cgi
=> $query_cgi,
52 time => output_pref
( { dt
=> dt_from_string
(), dateformat
=> 'iso', timeformat
=> '24hr' } ),
55 shift @recent_searches if ( @recent_searches > 15 );
56 set_to_session
( { cgi
=> $cgi, search_history
=> \
@recent_searches } );
61 my $userid = $params->{userid
};
62 my $sessionid = $params->{sessionid
};
63 my $type = $params->{type
} || q{};
64 my $previous = $params->{previous
} || 0;
67 warn "ERROR: userid is required for history search";
71 my $dbh = C4
::Context
->dbh;
73 DELETE FROM search_history
80 ?
q{ AND sessionid != ?}
81 : q{ AND sessionid = ?};
84 $query .= q{ AND type = ?}
89 ( $sessionid ?
$sessionid : () ),
90 ( $type ?
$type : () )
96 my $userid = $params->{userid
};
97 my $sessionid = $params->{sessionid
};
98 my $type = $params->{type
};
99 my $previous = $params->{previous
};
102 warn "ERROR: userid is required for history search";
115 ?
q{ AND sessionid != ?}
116 : q{ AND sessionid = ?};
119 $query .= q{ AND type = ?}
122 my $dbh = C4
::Context
->dbh;
123 my $sth = $dbh->prepare($query);
126 ( $sessionid ?
$sessionid : () ),
127 ( $type ?
$type : () )
129 return $sth->fetchall_arrayref( {} );
132 sub get_from_session
{
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');
140 eval { decode_json
( uri_unescape
( $session->param('search_history') ) ) };
141 return () unless defined $obj;
142 return () unless ref $obj eq 'ARRAY';
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) ) );
166 C4::Search::History - Manage search history
170 This module provides some routines for the search history management.
171 It deals with session or database.
177 C4::Search::History::add({
179 sessionid => $cgi->cookie("CGIESSID"),
180 query_desc => $query_desc,
181 query_cgi => $query_cgi,
186 type is "biblio" or "authority".
188 Add a new search to the user's history.
190 =head2 add_to_session
192 my $value = C4::Search::History::add_to_session({
194 query_desc => $query_desc,
195 query_cgi => $query_cgi,
200 Add a search to the session. The number of searches to keep is hardcoded to 15.
204 C4::Search::History::delete({
205 userid => $loggedinuser,
206 sessionid => $sessionid,
208 previous => $previous
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.
219 my $searches C4::Search::History::get({
221 sessionsid => $sessionid,
223 previous => $previous
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.
230 =head2 get_from_session
232 my $searches = C4::Search::History::get_from_session({
236 Return all searches present for the given session.
238 =head2 set_to_session
240 C4::Search::History::set_to_session({
242 search_history => $search_history
245 Store searches into the session.
249 Jonathan Druart <jonathan.druart@biblibre.com>
253 This file is part of Koha.
255 Copyright 2013 BibLibre SARL
257 Koha is free software; you can redistribute it and/or modify it
258 under the terms of the GNU General Public License as published by
259 the Free Software Foundation; either version 3 of the License, or
260 (at your option) any later version.
262 Koha is distributed in the hope that it will be useful, but
263 WITHOUT ANY WARRANTY; without even the implied warranty of
264 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
265 GNU General Public License for more details.
267 You should have received a copy of the GNU General Public License
268 along with Koha; if not, see <http://www.gnu.org/licenses>.