File Coverage

blib/lib/MyLibrary/Review.pm
Criterion Covered Total %
statement 39 73 53.4
branch 13 28 46.4
condition 0 6 0.0
subroutine 11 14 78.5
pod 10 11 90.9
total 73 132 55.3


line stmt bran cond sub pod time code
1             package MyLibrary::Review;
2              
3 1     1   1451 use MyLibrary::DB;
  1         4  
  1         25  
4 1     1   4 use Carp;
  1         2  
  1         44  
5 1     1   4 use strict;
  1         1  
  1         799  
6              
7             =head1 NAME
8              
9             MyLibrary::Review
10              
11              
12             =head1 SYNOPSIS
13              
14             # use the module
15             use MyLibrary::Review;
16            
17             # create a new review
18             my $review = MyLibrary::Review->new;
19            
20             # give the review characteristics
21             $review->review('This resource worked just fine more me.');
22             $review->reviewer_name('Fred Kilgour');
23             $review->reviewer_email('kilgour@oclc.org');
24             $review->review_date('2002-10-31');
25             $review->review_rating('3');
26            
27             # associate the review with a resource
28             $review->resource_id(601);
29            
30             # save the review; create a new record or update it
31             $review->commit;
32            
33             # get the id of the current review object
34             $id = $review->review_id;
35            
36             # create a new review object based on an id
37             my $review = MyLibrary::Review->new(id => $id);
38            
39             # display a review
40             print ' Resource ID: ', $review->resource_id, "\n";
41             print ' Review: ', $review->review, "\n";
42             print ' Reviewer: ', $review->reviewer_name, "\n";
43             print ' Email: ', $review->reviewer_email, "\n";
44             print ' Rating: ', $review->review_rating, "\n";
45             print ' Date: ', $review->review_date, "\n";
46            
47              
48             =head1 DESCRIPTION
49              
50             The module provides a means of saving reviews of information resources to the underlying MyLibrary database.
51              
52              
53             =head1 METHODS
54              
55             This section describes the methods available in the package.
56              
57              
58             =head2 new
59              
60             Use this method to create a new review object. Called with no arguments, this method creates an empty object. Given an id, this method gets the review from the database associated accordingly.
61              
62             # create a new review object
63             my $review = MyLibrary::Review->new;
64            
65             # create a review object based on a previously existing ID
66             my $review = MyLibrary::Review->new(id => 3);
67              
68              
69             =head2 review_id
70              
71             This method returns an integer representing the database key of the currently created review object.
72              
73             # get id of current review object
74             my $id = $review->review_id;
75              
76             You cannot set the review_id attribute.
77              
78              
79             =head2 review
80              
81             This method gets and sets the text of the review for the current review object:
82              
83             # get the text of the current review object
84             my $text = $review->review;
85            
86             # set the current review object's text
87             $review->review('I would recommend this resoruce to anyone.');
88            
89              
90             =head2 reviewer_name
91              
92             Use this method to get and set the name of a review's reviewer:
93              
94             # get the reviewer's name
95             my $reviewer = $review->reviewer_name;
96            
97             # set the reviwer's name
98             $librarian->reviewer_name('Paul Evan Peters');
99              
100              
101             =head2 reviewer_email
102              
103             Usse this method to get and set the reviewer's email address of the review object:
104              
105             # get the email address
106             my $email_address = $review->reviewer_email;
107            
108             # set the email address
109             $review->reviewer_email('pep@greatbeyond.org');
110              
111              
112             =head2 date
113              
114             Set or get the date attribute of the review object with this method:
115              
116             # get the date attribute
117             my $review_date = $review->review_date;
118            
119             # set the date
120             $review->review_date('2003-10-31');
121              
122             The date is expected to be in the format of YYYY-MM-DD.
123              
124              
125             =head2 review_rating
126              
127             Use this method to set a rating in the review.
128              
129             # set the rating
130             $review->review_rating('3');
131            
132             # get rating
133             my $review_rating = $review->review_rating;
134            
135             Ratings can be strings up to 255 characters in length, but this attribute is intended to be an integer value for calculating purposes. The programer can use the attribute in another manner if they so choose.
136              
137             =head2 resource_id
138              
139             Use this method to get and set what resource is being reviewed:
140              
141             # set the resource
142             $review->resource_id('601');
143            
144             # get resource id
145             my $resource_id = $review->resource_id;
146            
147              
148             =head2 commit
149              
150             Use this method to save the review object's attributes to the underlying database. If the object's data has never been saved before, then this method will create a new record in the database. If you used the new and passed it an id option, then this method will update the underlying database.
151              
152             This method will return true upon success.
153              
154             # save the current review object to the underlying database
155             $review->commit;
156              
157              
158             =head2 delete
159              
160             This method simply deletes the current review object from the underlying database.
161              
162             # delete (drop) this review from the database
163             $review->delete();
164            
165            
166             =head2 get_reviews
167              
168             Use this method to get all the reviews from the underlying database. It method returns an array of objects enabling you to loop through each object in the array and subsequent characteristics of each object;
169              
170             # get all reviews
171             my @reviews = MyLibrary::Review->get_reviews;
172            
173             # initialize counters
174             my $total_rating = 0;
175             my $total_reviews = 0;
176            
177             # process each review
178             foreach my $r (@reviews) {
179            
180             # look for a particular resource
181             if ($r->resource_id == 601) {
182            
183             # update counters
184             $total_rating = $total_rating + $r->review_rating;
185             $total_reviews = $total_reviews + 1;
186            
187             }
188            
189             }
190              
191             # check for reviews
192             if ($total_reviews) {
193            
194             # print the average rating
195             print "The average rating for resource 601 is: " . ($total_rating / $total_reviews)
196            
197             }
198              
199              
200             =head1 AUTHOR
201              
202             Eric Lease Morgan
203              
204              
205             =head1 HISTORY
206              
207             October 31, 2003 - first public release; Halloween
208              
209              
210             =cut
211              
212              
213             sub new {
214              
215             # declare local variables
216 1     1 1 726 my ($class, %opts) = @_;
217 1         2 my $self = {};
218              
219             # check for an id
220 1 50       5 if ($opts{id}) {
221            
222             # check for valid input, an integer
223 0 0       0 if ($opts{id} =~ /\D/) {
224            
225             # output an error and return nothing
226 0         0 croak "The id passed as input to the new method must be an integer: id = $opts{id} ";
227 0         0 return;
228            
229             }
230            
231             # get a handle
232 0         0 my $dbh = MyLibrary::DB->dbh();
233            
234             # find this record
235 0         0 my $rv = $dbh->selectrow_hashref('SELECT * FROM reviews WHERE review_id = ?', undef, $opts{id});
236            
237             # check for a hash
238 0 0       0 return unless ref($rv) eq 'HASH';
239              
240             # fill myself up with the fetched data
241 0         0 $self = bless ($rv, $class);
242            
243             }
244            
245             # return the object
246 1         3 return bless ($self, $class);
247            
248             }
249              
250              
251             sub review_id {
252              
253 0     0 1 0 my $self = shift;
254 0         0 return $self->{review_id};
255              
256             }
257              
258              
259             sub review {
260              
261             # declare local variables
262 2     2 1 492 my ($self, $review) = @_;
263            
264             # check for the existence of a telephone number
265 2 100       7 if ($review) { $self->{review} = $review }
  1         5  
266            
267             # return it
268 2         7 return $self->{review};
269            
270             }
271              
272              
273             sub reviewer_name {
274              
275             # declare local variables
276 2     2 1 4 my ($self, $reviewer_name) = @_;
277            
278             # check for the existence of a name
279 2 100       7 if ($reviewer_name) { $self->{reviewer_name} = $reviewer_name }
  1         3  
280            
281             # return it
282 2         6 return $self->{reviewer_name};
283            
284             }
285              
286              
287             sub reviewer_email {
288              
289             # declare local variables
290 2     2 1 18 my ($self, $reviewer_email) = @_;
291            
292             # check for the existence of an email address
293 2 100       6 if ($reviewer_email) { $self->{reviewer_email} = $reviewer_email }
  1         2  
294            
295             # return it
296 2         6 return $self->{reviewer_email};
297            
298             }
299              
300              
301              
302             sub review_date {
303              
304             # declare local variables
305 2     2 0 4 my ($self, $date) = @_;
306            
307             # check for the existence of date
308 2 100       6 if ($date) { $self->{review_date} = $date }
  1         3  
309            
310             # return it
311 2         5 return $self->{review_date};
312            
313             }
314              
315              
316             sub review_rating {
317              
318             # declare local variables
319 2     2 1 3 my ($self, $review_rating) = @_;
320            
321             # check for the existence of rating
322 2 100       6 if ($review_rating) { $self->{review_rating} = $review_rating }
  1         2  
323            
324             # return it
325 2         5 return $self->{review_rating};
326            
327             }
328              
329              
330             sub resource_id {
331              
332             # declare local variables
333 2     2 1 5 my ($self, $resource_id) = @_;
334            
335             # check for the existence of resource id
336 2 100       7 if ($resource_id) { $self->{resource_id} = $resource_id }
  1         3  
337            
338             # return it
339 2         6 return $self->{resource_id};
340            
341             }
342              
343              
344             sub commit {
345              
346             # get myself, :-)
347 1     1 1 2 my $self = shift;
348            
349             # get a database handle
350 1         5 my $dbh = MyLibrary::DB->dbh();
351            
352             # see if the object has an id
353 0 0         if ($self->review_id) {
354            
355             # update the review table with this id
356 0           my $return = $dbh->do('UPDATE reviews SET review = ?, reviewer_name = ?, reviewer_email = ?, review_date = ?, review_rating = ?, resource_id = ? WHERE review_id = ?', undef, $self->review, $self->reviewer_name, $self->reviewer_email, $self->review_date, $self->review_rating, $self->resource_id, $self->review_id);
357 0 0 0       if ($return > 1 || ! $return) { croak "Review update in commit() failed. $return records were updated." }
  0            
358            
359             }
360            
361             else {
362            
363             # get a new sequence
364 0           my $id = MyLibrary::DB->nextID();
365            
366             # create a new record
367 0           my $return = $dbh->do('INSERT INTO reviews (review_id, review, reviewer_name, reviewer_email, review_date, review_rating, resource_id) VALUES (?, ?, ?, ?, ?, ?, ?)', undef, $id, $self->review, $self->reviewer_name, $self->reviewer_email, $self->review_date, $self->review_rating, $self->resource_id);
368 0 0 0       if ($return > 1 || ! $return) { croak 'Review commit() failed.'; }
  0            
369 0           $self->{review_id} = $id;
370            
371             }
372            
373             # done
374 0           return 1;
375            
376             }
377              
378              
379             sub delete {
380              
381             # get myself
382 0     0 1   my $self = shift;
383              
384             # check for id
385 0 0         return 0 unless $self->{review_id};
386              
387             # delete this record
388 0           my $dbh = MyLibrary::DB->dbh();
389 0           my $rv = $dbh->do('DELETE FROM reviews WHERE review_id = ?', undef, $self->{review_id});
390 0 0         if ($rv != 1) { croak ("Deleted $rv records. I'll bet this isn't what you wanted.") }
  0            
391            
392             # done
393 0           return 1;
394              
395             }
396              
397              
398             sub get_reviews {
399              
400             # scope varibles
401 0     0 1   my $self = shift;
402 0           my @rv = ();
403            
404             # create and execute a query
405 0           my $dbh = MyLibrary::DB->dbh();
406 0           my $rows = $dbh->prepare('SELECT review_id FROM reviews');
407 0           $rows->execute;
408            
409             # process each found row
410 0           while (my $r = $rows->fetchrow_array) {
411            
412             # fill up the return value
413 0           push(@rv, $self->new(id => $r));
414            
415             }
416            
417             # return the array
418 0           return @rv;
419            
420             }
421              
422              
423             # return true, or else
424             1;