Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
30 / 30 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
RandomRecommend | |
100.00% |
30 / 30 |
|
100.00% |
6 / 6 |
13 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setConfig | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
init | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |||
process | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getResults | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getDisplayMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * RandomRecommend Recommendations Module |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2012, 2022. |
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 Recommendations |
25 | * @author Luke O'Sullivan <vufind-tech@lists.sourceforge.net> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org Main Page |
28 | */ |
29 | |
30 | namespace VuFind\Recommend; |
31 | |
32 | use VuFindSearch\Command\RandomCommand; |
33 | |
34 | use function count; |
35 | |
36 | /** |
37 | * RandomRecommend Module |
38 | * |
39 | * This class provides random recommendations based on the Solr random field |
40 | * |
41 | * Originally developed by Luke O'Sullivan at Swansea University. |
42 | * |
43 | * @category VuFind |
44 | * @package Recommendations |
45 | * @author Luke O'Sullivan <vufind-tech@lists.sourceforge.net> |
46 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
47 | * @link https://vufind.org Main Page |
48 | */ |
49 | class RandomRecommend implements RecommendInterface |
50 | { |
51 | /** |
52 | * Results |
53 | * |
54 | * @var array |
55 | */ |
56 | protected $results; |
57 | |
58 | /** |
59 | * Backend to use |
60 | * |
61 | * @var string |
62 | */ |
63 | protected $backend = 'Solr'; |
64 | |
65 | /** |
66 | * Results Limit |
67 | * |
68 | * @var int |
69 | */ |
70 | protected $limit = 10; |
71 | |
72 | /** |
73 | * Display Mode |
74 | * |
75 | * @var string |
76 | */ |
77 | protected $displayMode = 'standard'; |
78 | |
79 | /** |
80 | * Mode |
81 | * |
82 | * @var string |
83 | */ |
84 | protected $mode = 'retain'; |
85 | |
86 | /** |
87 | * Result Set Minimum |
88 | * |
89 | * @var number |
90 | */ |
91 | protected $minimum = 0; |
92 | |
93 | /** |
94 | * Filters |
95 | * |
96 | * @var array |
97 | */ |
98 | protected $filters = []; |
99 | |
100 | /** |
101 | * Settings from configuration |
102 | * |
103 | * @var string |
104 | */ |
105 | protected $settings; |
106 | |
107 | /** |
108 | * Search Service |
109 | * |
110 | * @var \VuFindSearch\Service |
111 | */ |
112 | protected $searchService; |
113 | |
114 | /** |
115 | * Params manager |
116 | * |
117 | * @var \VuFind\Search\Params\PluginManager |
118 | */ |
119 | protected $paramManager; |
120 | |
121 | /** |
122 | * Constructor |
123 | * |
124 | * @param \VuFindSearch\Service $searchService VuFind Search Service |
125 | * @param \VuFind\Search\Params\PluginManager $paramManager Params manager |
126 | */ |
127 | public function __construct( |
128 | \VuFindSearch\Service $searchService, |
129 | \VuFind\Search\Params\PluginManager $paramManager |
130 | ) { |
131 | $this->searchService = $searchService; |
132 | $this->paramManager = $paramManager; |
133 | } |
134 | |
135 | /** |
136 | * Store the configuration of the recommendation module. |
137 | * |
138 | * @param string $settings Settings from searches.ini. |
139 | * |
140 | * @return void |
141 | */ |
142 | public function setConfig($settings) |
143 | { |
144 | // Save the basic parameters: |
145 | $this->settings = $settings; |
146 | |
147 | // Apply any settings that override the defaults by being non-empty: |
148 | $properties = ['backend', 'limit', 'displayMode', 'mode', 'minimum']; |
149 | $settings = explode(':', $settings); |
150 | foreach ($properties as $i => $property) { |
151 | if (!empty($settings[$i])) { |
152 | $this->$property = $settings[$i]; |
153 | } |
154 | } |
155 | |
156 | // all other params are filters and their values respectively |
157 | for ($i = 5; $i < count($settings); $i += 2) { |
158 | if (isset($settings[$i + 1])) { |
159 | $this->filters[] = $settings[$i] . ':' . $settings[$i + 1]; |
160 | } |
161 | } |
162 | } |
163 | |
164 | /** |
165 | * Called before the Search Results object performs its main search |
166 | * (specifically, in response to \VuFind\Search\SearchRunner::EVENT_CONFIGURED). |
167 | * This method is responsible for setting search parameters needed by the |
168 | * recommendation module and for reading any existing search parameters that may |
169 | * be needed. |
170 | * |
171 | * @param \VuFind\Search\Base\Params $params Search parameter object |
172 | * @param \Laminas\Stdlib\Parameters $request Parameter object representing user |
173 | * request. |
174 | * |
175 | * @return void |
176 | */ |
177 | public function init($params, $request) |
178 | { |
179 | if ('retain' !== $this->mode) { |
180 | $randomParams = $this->paramManager->get($params->getSearchClassId()); |
181 | } else { |
182 | $randomParams = clone $params; |
183 | } |
184 | foreach ($this->filters as $filter) { |
185 | $randomParams->addFilter($filter); |
186 | } |
187 | $query = $randomParams->getQuery(); |
188 | $paramBag = $randomParams->getBackendParameters(); |
189 | $command = new RandomCommand( |
190 | $this->backend, |
191 | $query, |
192 | $this->limit, |
193 | $paramBag |
194 | ); |
195 | $this->results = $this->searchService->invoke($command) |
196 | ->getResult()->getRecords(); |
197 | } |
198 | |
199 | /** |
200 | * Called after the Search Results object has performed its main search. This |
201 | * may be used to extract necessary information from the Search Results object |
202 | * or to perform completely unrelated processing. |
203 | * |
204 | * @param \VuFind\Search\Base\Results $results Search results object |
205 | * |
206 | * @return void |
207 | */ |
208 | public function process($results) |
209 | { |
210 | } |
211 | |
212 | /** |
213 | * Get Results |
214 | * |
215 | * @return array |
216 | */ |
217 | public function getResults() |
218 | { |
219 | if (count($this->results) < $this->minimum) { |
220 | return []; |
221 | } |
222 | return $this->results; |
223 | } |
224 | |
225 | /** |
226 | * Get Display Mode |
227 | * |
228 | * @return string |
229 | */ |
230 | public function getDisplayMode() |
231 | { |
232 | return $this->displayMode; |
233 | } |
234 | } |