|
Lines 147-187
A hashref containing:
Link Here
|
| 147 |
|
147 |
|
| 148 |
sub GetRating { |
148 |
sub GetRating { |
| 149 |
my ( $biblionumber, $borrowernumber ) = @_; |
149 |
my ( $biblionumber, $borrowernumber ) = @_; |
| 150 |
my $query = qq| SELECT COUNT(*) AS total, SUM(rating_value) AS sum |
|
|
| 151 |
FROM ratings WHERE biblionumber = ? |; |
| 152 |
|
150 |
|
| 153 |
my $sth = C4::Context->dbh->prepare($query); |
151 |
my $ratings = Koha::Database->new()->schema->resultset('Rating')->search( |
| 154 |
$sth->execute($biblionumber); |
152 |
{ |
| 155 |
my $res = $sth->fetchrow_hashref(); |
153 |
biblionumber => $biblionumber, |
|
|
154 |
} |
| 155 |
); |
| 156 |
|
| 157 |
my $sum = $ratings->get_column('rating_value')->sum(); |
| 158 |
my $total = $ratings->count(); |
| 156 |
|
159 |
|
| 157 |
my ( $avg, $avg_int ) = 0; |
160 |
my ( $avg, $avg_int ) = 0; |
| 158 |
|
161 |
|
| 159 |
if ( $res->{sum} and $res->{total} ) { |
162 |
if ( $sum and $total ) { |
| 160 |
eval { $avg = $res->{sum} / $res->{total} }; |
163 |
eval { $avg = $sum / $total }; |
| 161 |
} |
164 |
} |
| 162 |
|
165 |
|
| 163 |
$avg_int = sprintf( "%.1f", $avg ); |
166 |
$avg_int = sprintf( "%.1f", $avg ); |
| 164 |
$avg = sprintf( "%.0f", $avg ); |
167 |
$avg = sprintf( "%.0f", $avg ); |
| 165 |
|
168 |
|
| 166 |
my %rating_hash; |
169 |
my %rating_hash; |
| 167 |
$rating_hash{rating_total} = $res->{total} || 0; |
170 |
$rating_hash{rating_total} = $total; |
| 168 |
$rating_hash{rating_avg} = $avg || 0; |
171 |
$rating_hash{rating_avg} = $avg || 0; |
| 169 |
$rating_hash{rating_avg_int} = $avg_int ||0; |
172 |
$rating_hash{rating_avg_int} = $avg_int ||0; |
| 170 |
|
173 |
|
| 171 |
if ($borrowernumber) { |
174 |
if ($borrowernumber) { |
| 172 |
my $q2 = qq| |
175 |
my $rating = Koha::Database->new()->schema->resultset('Rating')->find( |
| 173 |
SELECT rating_value FROM ratings WHERE biblionumber = ? AND borrowernumber = ?|; |
176 |
{ |
| 174 |
my $sth1 = C4::Context->dbh->prepare($q2); |
177 |
biblionumber => $biblionumber, |
| 175 |
$sth1->execute( $biblionumber, $borrowernumber ); |
178 |
borrowernumber => $borrowernumber, |
| 176 |
my $res1 = $sth1->fetchrow_hashref(); |
179 |
} |
| 177 |
$rating_hash{'rating_value'} = $res1->{"rating_value"}; |
180 |
); |
|
|
181 |
$rating_hash{'rating_value'} = $rating->rating_value(); |
| 178 |
} |
182 |
} |
| 179 |
else { |
183 |
else { |
| 180 |
$rating_hash{rating_borrowernumber} = undef; |
184 |
$rating_hash{rating_borrowernumber} = undef; |
| 181 |
$rating_hash{rating_value} = undef; |
185 |
$rating_hash{rating_value} = undef; |
| 182 |
} |
186 |
} |
| 183 |
|
187 |
|
| 184 |
#### %rating_hash |
|
|
| 185 |
return \%rating_hash; |
188 |
return \%rating_hash; |
| 186 |
} |
189 |
} |
| 187 |
|
190 |
|
|
Lines 199-211
is 0, then the rating will be deleted. If the value is out of the range of
Link Here
|
| 199 |
|
202 |
|
| 200 |
sub AddRating { |
203 |
sub AddRating { |
| 201 |
my ( $biblionumber, $borrowernumber, $rating_value ) = @_; |
204 |
my ( $biblionumber, $borrowernumber, $rating_value ) = @_; |
| 202 |
my $query = |
205 |
my $rating = Koha::Database->new()->schema->resultset('Rating')->create( |
| 203 |
qq| INSERT INTO ratings (borrowernumber,biblionumber,rating_value) |
206 |
{ |
| 204 |
VALUES (?,?,?)|; |
207 |
biblionumber => $biblionumber, |
| 205 |
my $sth = C4::Context->dbh->prepare($query); |
208 |
borrowernumber => $borrowernumber, |
| 206 |
$sth->execute( $borrowernumber, $biblionumber, $rating_value ); |
209 |
rating_value => $rating_value |
| 207 |
my $rating = GetRating( $biblionumber, $borrowernumber ); |
210 |
} |
| 208 |
return $rating; |
211 |
); |
|
|
212 |
|
| 213 |
return GetRating( $biblionumber, $borrowernumber ); |
| 209 |
} |
214 |
} |
| 210 |
|
215 |
|
| 211 |
=head2 ModRating |
216 |
=head2 ModRating |
|
Lines 218-229
Mod a rating for a bib
Link Here
|
| 218 |
|
223 |
|
| 219 |
sub ModRating { |
224 |
sub ModRating { |
| 220 |
my ( $biblionumber, $borrowernumber, $rating_value ) = @_; |
225 |
my ( $biblionumber, $borrowernumber, $rating_value ) = @_; |
| 221 |
my $query = |
226 |
|
| 222 |
qq|UPDATE ratings SET rating_value = ? WHERE borrowernumber = ? AND biblionumber = ?|; |
227 |
my $rating = Koha::Database->new()->schema->resultset('Rating')->find( |
| 223 |
my $sth = C4::Context->dbh->prepare($query); |
228 |
{ |
| 224 |
$sth->execute( $rating_value, $borrowernumber, $biblionumber ); |
229 |
borrowernumber => $borrowernumber, |
| 225 |
my $rating = GetRating( $biblionumber, $borrowernumber ); |
230 |
biblionumber => $biblionumber |
| 226 |
return $rating; |
231 |
} |
|
|
232 |
); |
| 233 |
|
| 234 |
$rating->update( { rating_value => $rating_value } ); |
| 235 |
|
| 236 |
return GetRating( $biblionumber, $borrowernumber ); |
| 227 |
} |
237 |
} |
| 228 |
|
238 |
|
| 229 |
=head2 DelRating |
239 |
=head2 DelRating |
|
Lines 236-248
Delete a rating for a bib
Link Here
|
| 236 |
|
246 |
|
| 237 |
sub DelRating { |
247 |
sub DelRating { |
| 238 |
my ( $biblionumber, $borrowernumber ) = @_; |
248 |
my ( $biblionumber, $borrowernumber ) = @_; |
| 239 |
my $dbh = C4::Context->dbh; |
249 |
my $rating = Koha::Database->new()->schema->resultset('Rating')->find( |
| 240 |
my $query = |
250 |
{ |
| 241 |
"delete from ratings where borrowernumber = ? and biblionumber = ?"; |
251 |
borrowernumber => $borrowernumber, |
| 242 |
my $sth = C4::Context->dbh->prepare($query); |
252 |
biblionumber => $biblionumber |
| 243 |
my $rv = $sth->execute( $borrowernumber, $biblionumber ); |
253 |
} |
| 244 |
my $rating = GetRating( $biblionumber, undef ); |
254 |
); |
| 245 |
return $rating; |
255 |
|
|
|
256 |
$rating->delete() if $rating; |
| 257 |
|
| 258 |
return GetRating($biblionumber); |
| 246 |
} |
259 |
} |
| 247 |
|
260 |
|
| 248 |
1; |
261 |
1; |
| 249 |
- |
|
|