Review.pm

filter: Statementstmt 54.0% 40 / 74 Branchbran 43.3% 13 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 78.5% 11 / 14 Podpod 90.9% 10 / 11 totaltotal 53.2% 74 / 139 CC 18 SCAR 23.3
CC
18
Cov
48%
commit
33.7
delete
12.0
new
9.6
CRAP 10.3
« Resource/Location/Type.pm ↑ Summary Interface/Admin.pm »
1 package MyLibrary::Review;
2  
3 1 ▶use MyLibrary::DB;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 <emorgan@nd.edu>
203  
204  
205 =head1 HISTORY
206  
207 October 31, 2003 - first public release; Halloween
208  
209  
210 =cut
211  
212  
213 1 ▶sub new {
Subroutine new: called CC=4 SCAR=22.6
Pod documented
214  
215 # declare local variables
216 1 my ($class, %opts) = @_;
217 1 my $self = {};
218  
219 # check for an id
220 1 ▶ if ($opts{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"id"}) 0 1 1
221
222 # check for valid input, an integer
223 0 ▶ if ($opts{id} =~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"id"} =~ /\D/) 0 0 0
224
225 # output an error and return nothing
226 0 croak "The id passed as input to the new method must be an integer: id = $opts{id} ";
227 0 return;
228
229 }
230
231 # get a handle
232 0 my $dbh = MyLibrary::DB->dbh();
233
234 # find this record
235 0 my $rv = $dbh->selectrow_hashref('SELECT * FROM reviews WHERE review_id = ?', undef, $opts{id});
236
237 # check for a hash
238 0 ▶ return unless ref($rv) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rv eq "HASH" 0 0 0
239  
240 # fill myself up with the fetched data
241 0 $self = bless ($rv, $class);
242
243 }
244
245 # return the object
246 1 return bless ($self, $class);
247
248 }
249  
250  
251 0 ▶sub review_id {
Subroutine review_id: not called CC=1 SCAR=6.9
Pod documented
252  
253 0 my $self = shift;
254 0 return $self->{review_id};
255  
256 }
257  
258  
259 2 ▶sub review {
Subroutine review: called CC=2 SCAR=6.9
Pod documented
260  
261 # declare local variables
262 2 my ($self, $review) = @_;
263
264 # check for the existence of a telephone number
265 2 ▶ if ($review) { $self->{review} = $review }
Branch: 1 branch
BranchTrueFalseTotal
if ($review) 1 1 2
266
267 # return it
268 2 return $self->{review};
269
270 }
271  
272  
273 2 ▶sub reviewer_name {
Subroutine reviewer_name: called CC=2 SCAR=6.9
Pod documented
274  
275 # declare local variables
276 2 my ($self, $reviewer_name) = @_;
277
278 # check for the existence of a name
279 2 ▶ if ($reviewer_name) { $self->{reviewer_name} = $reviewer_name }
Branch: 1 branch
BranchTrueFalseTotal
if ($reviewer_name) 1 1 2
280
281 # return it
282 2 return $self->{reviewer_name};
283
284 }
285  
286  
287 2 ▶sub reviewer_email {
Subroutine reviewer_email: called CC=2 SCAR=6.9
Pod documented
288  
289 # declare local variables
290 2 my ($self, $reviewer_email) = @_;
291
292 # check for the existence of an email address
293 2 ▶ if ($reviewer_email) { $self->{reviewer_email} = $reviewer_email }
Branch: 1 branch
BranchTrueFalseTotal
if ($reviewer_email) 1 1 2
294
295 # return it
296 2 return $self->{reviewer_email};
297
298 }
299  
300  
301  
302 2 ▶sub review_date {
Subroutine review_date: called CC=2 SCAR=6.9
Pod undocumented
303  
304 # declare local variables
305 2 my ($self, $date) = @_;
306
307 # check for the existence of date
308 2 ▶ if ($date) { $self->{review_date} = $date }
Branch: 1 branch
BranchTrueFalseTotal
if ($date) 1 1 2
309
310 # return it
311 2 return $self->{review_date};
312
313 }
314  
315  
316 2 ▶sub review_rating {
Subroutine review_rating: called CC=2 SCAR=6.9
Pod documented
317  
318 # declare local variables
319 2 my ($self, $review_rating) = @_;
320
321 # check for the existence of rating
322 2 ▶ if ($review_rating) { $self->{review_rating} = $review_rating }
Branch: 1 branch
BranchTrueFalseTotal
if ($review_rating) 1 1 2
323
324 # return it
325 2 return $self->{review_rating};
326
327 }
328  
329  
330 2 ▶sub resource_id {
Subroutine resource_id: called CC=2 SCAR=6.9
Pod documented
331  
332 # declare local variables
333 2 my ($self, $resource_id) = @_;
334
335 # check for the existence of resource id
336 2 ▶ if ($resource_id) { $self->{resource_id} = $resource_id }
Branch: 1 branch
BranchTrueFalseTotal
if ($resource_id) 1 1 2
337
338 # return it
339 2 return $self->{resource_id};
340
341 }
342  
343  
344 1 ▶sub commit {
Subroutine commit: called CC=6 SCAR=35.2
Pod documented
345  
346 # get myself, :-)
347 1 my $self = shift;
348
349 # get a database handle
350 1 my $dbh = MyLibrary::DB->dbh();
351
352 # see if the object has an id
353 0 ▶ if ($self->review_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->review_id) { } 0 0 0
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 ▶ if ($return > 1 || ! $return) { croak "Review update in commit() failed. $return records were updated." }
Branch: 1 branch
BranchTrueFalseTotal
if ($return > 1 or not $return) 0 0 0
Condition0%0/3
$return > 1 or not $return
l!l&&r!l&&!r
000
MC/DC0%0/2
$return > 1 or not $return
$return > 1 not $return
Truth table0%0/3
A or not(B)
A= $return > 1B= not $return
ABresult
1X1
011
000
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 ▶ if ($return > 1 || ! $return) { croak 'Review commit() failed.'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($return > 1 or not $return) 0 0 0
Condition0%0/3
$return > 1 or not $return
l!l&&r!l&&!r
000
MC/DC0%0/2
$return > 1 or not $return
$return > 1 not $return
Truth table0%0/3
A or not(B)
A= $return > 1B= not $return
ABresult
1X1
011
000
369 0 $self->{review_id} = $id;
370
371 }
372
373 # done
374 0 return 1;
375
376 }
377  
378  
379 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
Pod documented
380  
381 # get myself
382 0 my $self = shift;
383  
384 # check for id
385 0 ▶ return 0 unless $self->{review_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"review_id"} 0 0 0
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 ▶ if ($rv != 1) { croak ("Deleted $rv records. I'll bet this isn't what you wanted.") }
Branch: 1 branch
BranchTrueFalseTotal
if ($rv != 1) 0 0 0
391
392 # done
393 0 return 1;
394  
395 }
396  
397  
398 0 ▶sub get_reviews {
Subroutine get_reviews: not called CC=2 SCAR=17.9
Pod documented
399  
400 # scope varibles
401 0 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) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $rows->fetchrow_array) 0 0 0
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 1;
« Resource/Location/Type.pm ↑ Summary Interface/Admin.pm »