Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 15 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
RatingsService | |
0.00% |
0 / 15 |
|
0.00% |
0 / 4 |
30 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getRatingData | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
getRatingBreakdown | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
saveRating | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Ratings service |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2024. |
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 Ratings |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
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\Ratings; |
31 | |
32 | use VuFind\Db\Service\RatingsServiceInterface; |
33 | use VuFind\Record\ResourcePopulator; |
34 | use VuFind\RecordDriver\AbstractBase as RecordDriver; |
35 | |
36 | /** |
37 | * Ratings service |
38 | * |
39 | * @category VuFind |
40 | * @package Ratings |
41 | * @author Demian Katz <demian.katz@villanova.edu> |
42 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
43 | * @link https://vufind.org Main Page |
44 | */ |
45 | class RatingsService |
46 | { |
47 | /** |
48 | * Cache for rating data |
49 | * |
50 | * @var array |
51 | */ |
52 | protected $ratingCache = []; |
53 | |
54 | /** |
55 | * Constructor |
56 | * |
57 | * @param RatingsServiceInterface $dbService Ratings database service |
58 | * @param ResourcePopulator $resourcePopulator Resource populator |
59 | */ |
60 | public function __construct( |
61 | protected RatingsServiceInterface $dbService, |
62 | protected ResourcePopulator $resourcePopulator |
63 | ) { |
64 | } |
65 | |
66 | /** |
67 | * Get rating information for the provided record. |
68 | * |
69 | * Returns an array with the following keys: |
70 | * |
71 | * rating - average rating (0-100) |
72 | * count - count of ratings |
73 | * |
74 | * @param RecordDriver $driver Record to look up |
75 | * @param ?int $userId User ID, or null for all users |
76 | * |
77 | * @return array |
78 | */ |
79 | public function getRatingData(RecordDriver $driver, ?int $userId = null) |
80 | { |
81 | // Cache data since comments list may ask for same information repeatedly: |
82 | $recordId = $driver->getUniqueId(); |
83 | $source = $driver->getSourceIdentifier(); |
84 | $cacheKey = $recordId . '-' . $source . '-' . ($userId ?? ''); |
85 | if (!isset($this->ratingCache[$cacheKey])) { |
86 | $this->ratingCache[$cacheKey] = $this->dbService->getRecordRatings($recordId, $source, $userId); |
87 | } |
88 | return $this->ratingCache[$cacheKey]; |
89 | } |
90 | |
91 | /** |
92 | * Get rating breakdown for the provided record. |
93 | * |
94 | * Returns an array with the following keys: |
95 | * |
96 | * rating - average rating (0-100) |
97 | * count - count of ratings |
98 | * groups - grouped counts |
99 | * |
100 | * @param RecordDriver $driver Record to look up |
101 | * @param array $groups Group definition (key => [min, max]) |
102 | * |
103 | * @return array |
104 | */ |
105 | public function getRatingBreakdown(RecordDriver $driver, array $groups) |
106 | { |
107 | return $this->dbService->getCountsForRecord( |
108 | $driver->getUniqueId(), |
109 | $driver->getSourceIdentifier(), |
110 | $groups |
111 | ); |
112 | } |
113 | |
114 | /** |
115 | * Add or update user's rating for the record. |
116 | * |
117 | * @param RecordDriver $driver Record associated with rating |
118 | * @param int $userId ID of the user posting the rating |
119 | * @param ?int $rating The user-provided rating, or null to clear any existing |
120 | * rating |
121 | * |
122 | * @return void |
123 | */ |
124 | public function saveRating(RecordDriver $driver, int $userId, ?int $rating): void |
125 | { |
126 | // Clear rating cache: |
127 | $this->ratingCache = []; |
128 | $resource = $this->resourcePopulator->getOrCreateResourceForDriver($driver); |
129 | $this->dbService->addOrUpdateRating($resource, $userId, $rating); |
130 | } |
131 | } |