View | Details | Raw Unified | Return to bug 12609
Collapse All | Expand All

(-)a/C4/Ratings.pm (-37 / +51 lines)
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
203
      qq| INSERT INTO ratings (borrowernumber,biblionumber,rating_value)
206
    my $rating = Koha::Database->new()->schema->resultset('Rating')->create(
204
        VALUES (?,?,?)|;
207
        {
205
    my $sth = C4::Context->dbh->prepare($query);
208
            biblionumber   => $biblionumber,
206
    $sth->execute( $borrowernumber, $biblionumber, $rating_value );
209
            borrowernumber => $borrowernumber,
207
    my $rating = GetRating( $biblionumber, $borrowernumber );
210
            rating_value   => $rating_value
208
    return $rating;
211
        }
212
    );
213
214
    return GetRating( $biblionumber, $borrowernumber );
209
}
215
}
210
216
211
=head2 ModRating
217
=head2 ModRating
Lines 218-229 Mod a rating for a bib Link Here
218
224
219
sub ModRating {
225
sub ModRating {
220
    my ( $biblionumber, $borrowernumber, $rating_value ) = @_;
226
    my ( $biblionumber, $borrowernumber, $rating_value ) = @_;
221
    my $query =
227
222
qq|UPDATE ratings SET rating_value = ? WHERE borrowernumber = ? AND biblionumber = ?|;
228
    my $rating = Koha::Database->new()->schema->resultset('Rating')->find(
223
    my $sth = C4::Context->dbh->prepare($query);
229
        {
224
    $sth->execute( $rating_value, $borrowernumber, $biblionumber );
230
            borrowernumber => $borrowernumber,
225
    my $rating = GetRating( $biblionumber, $borrowernumber );
231
            biblionumber   => $biblionumber
226
    return $rating;
232
        }
233
    );
234
235
    $rating->update( { rating_value => $rating_value } );
236
237
    return GetRating( $biblionumber, $borrowernumber );
227
}
238
}
228
239
229
=head2 DelRating
240
=head2 DelRating
Lines 236-248 Delete a rating for a bib Link Here
236
247
237
sub DelRating {
248
sub DelRating {
238
    my ( $biblionumber, $borrowernumber ) = @_;
249
    my ( $biblionumber, $borrowernumber ) = @_;
239
    my $dbh = C4::Context->dbh;
250
240
    my $query =
251
    my $rating = Koha::Database->new()->schema->resultset('Rating')->find(
241
      "delete from ratings where borrowernumber = ? and biblionumber = ?";
252
        {
242
    my $sth    = C4::Context->dbh->prepare($query);
253
            borrowernumber => $borrowernumber,
243
    my $rv     = $sth->execute( $borrowernumber, $biblionumber );
254
            biblionumber   => $biblionumber
244
    my $rating = GetRating( $biblionumber, undef );
255
        }
245
    return $rating;
256
    );
257
258
    $rating->delete() if $rating;
259
260
    return GetRating($biblionumber);
246
}
261
}
247
262
248
1;
263
1;
249
- 

Return to bug 12609