Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
33.33% |
3 / 9 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
RatingsService | |
33.33% |
3 / 9 |
|
20.00% |
1 / 5 |
26.96 | |
0.00% |
0 / 1 |
getRecordRatings | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getCountsForRecord | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
deleteByUser | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getStatistics | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addOrUpdateRating | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | /** |
4 | * Database service for Ratings. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2023. |
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 Database |
25 | * @author Sudharma Kellampalli <skellamp@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org/wiki/development:plugins:database_gateways Wiki |
28 | */ |
29 | |
30 | namespace VuFind\Db\Service; |
31 | |
32 | use VuFind\Db\Entity\ResourceEntityInterface; |
33 | use VuFind\Db\Entity\UserEntityInterface; |
34 | use VuFind\Db\Table\DbTableAwareInterface; |
35 | use VuFind\Db\Table\DbTableAwareTrait; |
36 | |
37 | use function is_int; |
38 | |
39 | /** |
40 | * Database service for Ratings. |
41 | * |
42 | * @category VuFind |
43 | * @package Database |
44 | * @author Sudharma Kellampalli <skellamp@villanova.edu> |
45 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
46 | * @link https://vufind.org/wiki/development:plugins:database_gateways Wiki |
47 | */ |
48 | class RatingsService extends AbstractDbService implements |
49 | DbTableAwareInterface, |
50 | RatingsServiceInterface |
51 | { |
52 | use DbTableAwareTrait; |
53 | |
54 | /** |
55 | * Get average rating and rating count associated with the specified record. |
56 | * |
57 | * @param string $id Record ID to look up |
58 | * @param string $source Source of record to look up |
59 | * @param ?int $userId User ID, or null for all users |
60 | * |
61 | * @return array Array with keys count and rating (between 0 and 100) |
62 | */ |
63 | public function getRecordRatings(string $id, string $source, ?int $userId): array |
64 | { |
65 | return $this->getDbTable('ratings')->getForResource($id, $source, $userId); |
66 | } |
67 | |
68 | /** |
69 | * Get rating breakdown for the specified record. |
70 | * |
71 | * @param string $id Record ID to look up |
72 | * @param string $source Source of record to look up |
73 | * @param array $groups Group definition (key => [min, max]) |
74 | * |
75 | * @return array Array with keys count and rating (between 0 and 100) as well as |
76 | * an groups array with ratings from lowest to highest |
77 | */ |
78 | public function getCountsForRecord( |
79 | string $id, |
80 | string $source, |
81 | array $groups |
82 | ): array { |
83 | return $this->getDbTable('ratings')->getCountsForResource($id, $source, $groups); |
84 | } |
85 | |
86 | /** |
87 | * Deletes all ratings by a user. |
88 | * |
89 | * @param UserEntityInterface|int $userOrId User object or identifier |
90 | * |
91 | * @return void |
92 | */ |
93 | public function deleteByUser(UserEntityInterface|int $userOrId): void |
94 | { |
95 | $this->getDbTable('ratings')->deleteByUser( |
96 | is_int($userOrId) ? $this->getDbTable('user')->getById($userOrId) : $userOrId |
97 | ); |
98 | } |
99 | |
100 | /** |
101 | * Get statistics on use of Ratings. |
102 | * |
103 | * @return array |
104 | */ |
105 | public function getStatistics(): array |
106 | { |
107 | return $this->getDbTable('ratings')->getStatistics(); |
108 | } |
109 | |
110 | /** |
111 | * Add or update user's rating for a resource. |
112 | * |
113 | * @param ResourceEntityInterface|int $resourceOrId Resource to add or update rating. |
114 | * @param UserEntityInterface|int $userOrId User |
115 | * @param ?int $rating Rating (null to delete) |
116 | * |
117 | * @throws \Exception |
118 | * @return int ID of rating added, deleted or updated |
119 | */ |
120 | public function addOrUpdateRating( |
121 | ResourceEntityInterface|int $resourceOrId, |
122 | UserEntityInterface|int $userOrId, |
123 | ?int $rating |
124 | ): int { |
125 | $resource = is_int($resourceOrId) |
126 | ? $this->getDbTable('resource')->select(['id' => $resourceOrId])->current() : $resourceOrId; |
127 | return $resource->addOrUpdateRating(is_int($userOrId) ? $userOrId : $userOrId->getId(), $rating); |
128 | } |
129 | } |