| 1 | # Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd. |
|---|
| 2 | # This program is distributed under the terms of the |
|---|
| 3 | # GNU General Public License, version 2. |
|---|
| 4 | # |
|---|
| 5 | # $Id$ |
|---|
| 6 | |
|---|
| 7 | package MT::ObjectScore; |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | use base qw( MT::Object ); |
|---|
| 11 | |
|---|
| 12 | __PACKAGE__->install_properties({ |
|---|
| 13 | column_defs => { |
|---|
| 14 | 'id' => 'integer not null auto_increment', |
|---|
| 15 | 'namespace' => 'string(255) not null', |
|---|
| 16 | 'object_id' => 'integer', |
|---|
| 17 | 'author_id' => 'integer', |
|---|
| 18 | 'score' => 'float', |
|---|
| 19 | 'object_ds' => 'string(50) not null', |
|---|
| 20 | 'ip' => 'string(16)', |
|---|
| 21 | }, |
|---|
| 22 | indexes => { |
|---|
| 23 | # usually used to remove all scores for a given object |
|---|
| 24 | ds_obj => { |
|---|
| 25 | columns => ['object_ds', 'object_id'], |
|---|
| 26 | }, |
|---|
| 27 | # common requests for scoring |
|---|
| 28 | ns_user_ds_obj => { |
|---|
| 29 | columns => ['namespace', 'author_id', 'object_ds', 'object_id'], |
|---|
| 30 | }, |
|---|
| 31 | # common requests for anonymous scoring (ip-based) |
|---|
| 32 | ns_ip_ds_obj => { |
|---|
| 33 | columns => ['namespace', 'ip', 'object_ds', 'object_id'], |
|---|
| 34 | }, |
|---|
| 35 | # for scored_by method |
|---|
| 36 | user_ns => { |
|---|
| 37 | columns => ['author_id', 'namespace'], |
|---|
| 38 | }, |
|---|
| 39 | }, |
|---|
| 40 | defaults => { |
|---|
| 41 | object_id => 0, |
|---|
| 42 | author_id => 0, |
|---|
| 43 | }, |
|---|
| 44 | audit => 1, |
|---|
| 45 | datasource => 'objectscore', |
|---|
| 46 | primary_key => 'id', |
|---|
| 47 | }); |
|---|
| 48 | |
|---|
| 49 | sub class_label { |
|---|
| 50 | MT->translate("Object Score"); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | sub class_label_plural { |
|---|
| 54 | MT->translate("Object Scores"); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | sub scored_by { |
|---|
| 58 | my $class = shift; |
|---|
| 59 | my ($namespace, $user) = @_; |
|---|
| 60 | MT::ObjectScore->load_iter({ |
|---|
| 61 | author_id => $user->id, |
|---|
| 62 | defined($namespace) ? (namespace => $namespace) : (), |
|---|
| 63 | }); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | 1; |
|---|
| 67 | __END__ |
|---|
| 68 | |
|---|
| 69 | =head1 NAME |
|---|
| 70 | |
|---|
| 71 | MT::Scorable - An interface for any MT::Object that wishes to be rated. |
|---|
| 72 | |
|---|
| 73 | =head1 SYNOPSIS |
|---|
| 74 | |
|---|
| 75 | use MT::Entry; |
|---|
| 76 | my $entry = MT::Entry->load($id); |
|---|
| 77 | $entry->set_score('key', $app->user, 100, $overwrite); |
|---|
| 78 | |
|---|
| 79 | =head1 METHODS |
|---|
| 80 | |
|---|
| 81 | =head2 get_score($namespace, $user) |
|---|
| 82 | |
|---|
| 83 | Return the score of the object, scored by the user specified. |
|---|
| 84 | This is not for total score of an object. This is to get a score |
|---|
| 85 | specified by a user to an object. |
|---|
| 86 | |
|---|
| 87 | =head2 set_score($namespace, $user, $score, $overwrite) |
|---|
| 88 | |
|---|
| 89 | Set specified score to the object by the user. If $overwrite argument |
|---|
| 90 | is false and the user has already scored the object before, error results. |
|---|
| 91 | |
|---|
| 92 | =head2 score_for($namespace) |
|---|
| 93 | |
|---|
| 94 | Return the total score of the object. |
|---|
| 95 | |
|---|
| 96 | =head2 vote_for($namespace) |
|---|
| 97 | |
|---|
| 98 | Return how many users scored to the object. |
|---|
| 99 | |
|---|
| 100 | =head2 score_high($namespace) |
|---|
| 101 | |
|---|
| 102 | Return the highest score to the object. |
|---|
| 103 | |
|---|
| 104 | =head2 score_low($namespace) |
|---|
| 105 | |
|---|
| 106 | Return the lowest score to the object. |
|---|
| 107 | |
|---|
| 108 | =head2 score_avg($namespace) |
|---|
| 109 | |
|---|
| 110 | Return the average score of the object. |
|---|
| 111 | |
|---|
| 112 | =head2 rank_for($namespace, $max) |
|---|
| 113 | |
|---|
| 114 | Return the rank of the object based on its score among other objects |
|---|
| 115 | of the same type. The smaller the number is, the higher the object's rank is. |
|---|
| 116 | |
|---|
| 117 | =head1 AUTHOR & COPYRIGHT |
|---|
| 118 | |
|---|
| 119 | Please see L<MT/AUTHOR & COPYRIGHT>. |
|---|
| 120 | |
|---|
| 121 | =cut |
|---|
| 122 | |
|---|