Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.00% covered (success)
90.00%
9 / 10
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
SwitchType
90.00% covered (success)
90.00%
9 / 10
83.33% covered (warning)
83.33%
5 / 6
9.08
0.00% covered (danger)
0.00%
0 / 1
 setConfig
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 process
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getResults
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getNewHandler
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getNewHandlerName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * SwitchType Recommendations Module
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
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   Demian Katz <demian.katz@villanova.edu>
26 * @author   Chris Hallberg <challber@villanova.edu>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development:plugins:recommendation_modules Wiki
29 */
30
31namespace VuFind\Recommend;
32
33/**
34 * SwitchType Recommendations Module
35 *
36 * This class recommends switching to a different search type.
37 *
38 * @category VuFind
39 * @package  Recommendations
40 * @author   Demian Katz <demian.katz@villanova.edu>
41 * @author   Chris Hallberg <challber@villanova.edu>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/development:plugins:recommendation_modules Wiki
44 */
45class SwitchType implements RecommendInterface
46{
47    /**
48     * Search handler to try
49     *
50     * @var string
51     */
52    protected $newHandler;
53
54    /**
55     * On-screen description of handler
56     *
57     * @var string
58     */
59    protected $newHandlerName;
60
61    /**
62     * Is this module active?
63     *
64     * @var bool
65     */
66    protected $active;
67
68    /**
69     * Results object
70     *
71     * @var \VuFind\Search\Base\Results
72     */
73    protected $results;
74
75    /**
76     * Store the configuration of the recommendation module.
77     *
78     * @param string $settings Settings from searches.ini.
79     *
80     * @return void
81     */
82    public function setConfig($settings)
83    {
84        $params = explode(':', $settings);
85        $this->newHandler = !empty($params[0]) ? $params[0] : 'AllFields';
86        $this->newHandlerName = $params[1] ?? 'All Fields';
87    }
88
89    /**
90     * Called before the Search Results object performs its main search
91     * (specifically, in response to \VuFind\Search\SearchRunner::EVENT_CONFIGURED).
92     * This method is responsible for setting search parameters needed by the
93     * recommendation module and for reading any existing search parameters that may
94     * be needed.
95     *
96     * @param \VuFind\Search\Base\Params $params  Search parameter object
97     * @param \Laminas\Stdlib\Parameters $request Parameter object representing user
98     * request.
99     *
100     * @return void
101     */
102    public function init($params, $request)
103    {
104    }
105
106    /**
107     * Called after the Search Results object has performed its main search. This
108     * may be used to extract necessary information from the Search Results object
109     * or to perform completely unrelated processing.
110     *
111     * @param \VuFind\Search\Base\Results $results Search results object
112     *
113     * @return void
114     */
115    public function process($results)
116    {
117        $handler = $results->getParams()->getSearchHandler();
118        $this->results = $results;
119
120        // If the handler is null, we can't figure out a single handler, so this
121        // is probably an advanced search. In that case, we shouldn't try to change
122        // anything!  We should only show recommendations if we know what handler is
123        // being used and can determine that it is not the same as the new handler
124        // that we want to recommend.
125        $this->active = (null !== $handler && $handler != $this->newHandler);
126    }
127
128    /**
129     * Get results stored in the object.
130     *
131     * @return \VuFind\Search\Base\Results
132     */
133    public function getResults()
134    {
135        return $this->results;
136    }
137
138    /**
139     * Get the new search handler, or false if it does not apply.
140     *
141     * @return string|false
142     */
143    public function getNewHandler()
144    {
145        return $this->active ? $this->newHandler : false;
146    }
147
148    /**
149     * Get the description of the new search handler.
150     *
151     * @return string
152     */
153    public function getNewHandlerName()
154    {
155        return $this->newHandlerName;
156    }
157}