Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
47.83% |
11 / 23 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
History | |
47.83% |
11 / 23 |
|
75.00% |
3 / 4 |
28.18 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
purgeSearchHistory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSearchHistory | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
getScheduleOptions | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | |
3 | /** |
4 | * VuFind Search History Helper |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2017. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Search |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @author Sebastian Böttger <boettger@hebis.uni-frankfurt.de> |
27 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
28 | * @link https://vufind.org Main Page |
29 | */ |
30 | |
31 | namespace VuFind\Search; |
32 | |
33 | use Exception; |
34 | use Laminas\Config\Config; |
35 | use VuFind\Db\Service\SearchServiceInterface; |
36 | |
37 | /** |
38 | * VuFind Search History Helper |
39 | * |
40 | * @category VuFind |
41 | * @package Search |
42 | * @author Demian Katz <demian.katz@villanova.edu> |
43 | * @author Sebastian Böttger <boettger@hebis.uni-frankfurt.de> |
44 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
45 | * @link https://vufind.org Main Page |
46 | */ |
47 | class History |
48 | { |
49 | /** |
50 | * History constructor |
51 | * |
52 | * @param SearchServiceInterface $searchService Search table |
53 | * @param string $sessionId Session ID |
54 | * @param \VuFind\Search\Results\PluginManager $resultsManager Results manager |
55 | * @param ?\Laminas\Config\Config $config Configuration |
56 | */ |
57 | public function __construct( |
58 | protected SearchServiceInterface $searchService, |
59 | protected string $sessionId, |
60 | protected \VuFind\Search\Results\PluginManager $resultsManager, |
61 | protected ?\Laminas\Config\Config $config = null |
62 | ) { |
63 | } |
64 | |
65 | /** |
66 | * Purge the user's unsaved search history. |
67 | * |
68 | * @param int $userId User ID (null if logged out) |
69 | * |
70 | * @return void |
71 | */ |
72 | public function purgeSearchHistory($userId = null) |
73 | { |
74 | $this->searchService->destroySession($this->sessionId, $userId); |
75 | } |
76 | |
77 | /** |
78 | * Get the user's saved and temporary search histories. |
79 | * |
80 | * @param int $userId User ID (null if logged out) |
81 | * |
82 | * @return array |
83 | */ |
84 | public function getSearchHistory($userId = null) |
85 | { |
86 | // Retrieve search history |
87 | $searchHistory = $this->searchService->getSearches($this->sessionId, $userId); |
88 | |
89 | // Loop through and sort the history |
90 | $saved = $schedule = $unsaved = []; |
91 | foreach ($searchHistory as $current) { |
92 | $search = $current->getSearchObject()?->deminify($this->resultsManager); |
93 | if (!$search) { |
94 | throw new Exception("Problem getting search object from search {$current->getId()}."); |
95 | } |
96 | if ($current->getSaved()) { |
97 | $saved[] = $search; |
98 | } else { |
99 | $unsaved[] = $search; |
100 | } |
101 | if ($search->getOptions()->supportsScheduledSearch()) { |
102 | $schedule[$search->getSearchId()] = $current->getNotificationFrequency(); |
103 | } |
104 | } |
105 | |
106 | return compact('saved', 'schedule', 'unsaved'); |
107 | } |
108 | |
109 | /** |
110 | * Get a list of scheduling options (empty list if scheduling disabled). |
111 | * |
112 | * @return array |
113 | */ |
114 | public function getScheduleOptions() |
115 | { |
116 | // If scheduled searches are disabled, return no options: |
117 | if (!($this->config->Account->schedule_searches ?? false)) { |
118 | return []; |
119 | } |
120 | // If custom frequencies are not provided, return defaults: |
121 | if (!isset($this->config->Account->scheduled_search_frequencies)) { |
122 | return [ |
123 | 0 => 'schedule_none', 1 => 'schedule_daily', 7 => 'schedule_weekly', |
124 | ]; |
125 | } |
126 | // If we have a setting, make sure it is properly formatted as an array: |
127 | return $this->config->Account->scheduled_search_frequencies instanceof Config |
128 | ? $this->config->Account->scheduled_search_frequencies->toArray() |
129 | : (array)$this->config->Account->scheduled_search_frequencies; |
130 | } |
131 | } |