Guest User

Untitled

a guest
Apr 24th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. use MooseX::Declare;
  2.  
  3. class Sparcq::Schema::Result::VoteLog
  4. extends Sparcq::Schema::Result {
  5.  
  6.  
  7. __PACKAGE__->table('vote_log');
  8.  
  9. __PACKAGE__->add_columns(
  10. vote_log_id => {
  11. data_type => 'int',
  12. size => '11',
  13. is_nullable => 0,
  14. extra => {
  15. unsigned => 1,
  16. },
  17. is_auto_increment => 1,
  18. },
  19.  
  20. fk_vote_id => {
  21. data_type => 'int',
  22. size => '11',
  23. is_nullable => 0,
  24. extra => {
  25. unsigned => 1,
  26. },
  27. },
  28.  
  29. fk_entry_id => {
  30. data_type => 'int',
  31. size => '11',
  32. is_nullable => 0,
  33. extra => {
  34. unsigned => 1,
  35. },
  36. },
  37.  
  38. fk_voter_id => {
  39. data_type => 'int',
  40. size => '11',
  41. is_nullable => 0,
  42. extra => {
  43. unsigned => 1,
  44. },
  45. },
  46.  
  47. status => {
  48. data_type => 'varchar',
  49. size => '15',
  50. is_nullable => 0,
  51. },
  52.  
  53. old_rating_value => {
  54. data_type => 'int',
  55. size => '11',
  56. is_nullable => 0,
  57. default_value => 0,
  58. extra => {
  59. unsigned => 1,
  60. },
  61. },
  62.  
  63. new_rating_value => {
  64. data_type => 'int',
  65. size => '11',
  66. is_nullable => 0,
  67. default_value => 0,
  68. extra => {
  69. unsigned => 1,
  70. },
  71. },
  72.  
  73. cr_date => {
  74. data_type => 'datetime',
  75. set_on_create => 1,
  76. is_nullable => 0,
  77. },
  78. );
  79.  
  80. __PACKAGE__->set_primary_key('vote_log_id');
  81.  
  82. __PACKAGE__->belongs_to(
  83. vote => 'Sparcq::Schema::Result::Vote',
  84. { 'foreign.vote_id' => 'self.fk_vote_id' },
  85. );
  86.  
  87. __PACKAGE__->belongs_to(
  88. member => 'Sparcq::Schema::Result::Member',
  89. { 'foreign.member_id' => 'self.fk_voter_id' },
  90. );
  91.  
  92. __PACKAGE__->belongs_to(
  93. vote => 'Sparcq::Schema::Result::Entry',
  94. { 'foreign.entry_id' => 'self.fk_entry_id' },
  95. );
  96.  
  97. __PACKAGE__->meta->make_immutable(inline_constructor => 0);
  98.  
  99.  
  100. sub calculate_change_in_trust {
  101. my $self = shift;
  102.  
  103. # Start Transaction
  104. my $schema = $self->result_source->schema;
  105. $schema->storage->txn_begin;
  106.  
  107. eval {
  108. my $method = "calculate_trust_for_" . $self->status . "_vote";
  109. $self->$method;
  110. $self->delete;
  111.  
  112. # Commit Transaction
  113. $schema->storage->txn_commit;
  114. };
  115. if ($@) {
  116. print ">: " . $@ . "\n";
  117. # Rollback Transaction
  118. $schema->storage->txn_rollback;
  119. }
  120. }
  121.  
  122. sub calculate_trust_for_new_vote {
  123. my $self = shift;
  124.  
  125. my $schema = $self->result_source->schema;
  126.  
  127. my $member = $self->member;
  128. my $other_members = $schema->resultset('Member')->search({
  129. member_id => { '!=' => $member->member_id },
  130. });
  131.  
  132. # Iterate through all of the other members
  133. while (my $other_member = $other_members->next) {
  134.  
  135. # Look up the trust between member and other_member.
  136. if ( my $trust = $schema->resultset( 'RecentTrust' )->find(
  137. $member->member_id,
  138. $other_member->member_id ) ) {
  139.  
  140. my $shared_vote_rating = $self->get_shared_rating(
  141. $other_member->member_id,
  142. $self->fk_entry_id
  143. );
  144.  
  145. if ( defined( $shared_vote_rating ) ) {
  146. my $shared_sum = 100 - abs($self->new_rating_value - $shared_vote_rating );
  147. $member->update_recent_trust( $other_member, $trust->shared_rating_sum + $shared_sum );
  148. }
  149. }
  150. else {
  151. # If there is no trust between the two members,
  152. # Find out whether the other user rated this vote.
  153.  
  154. my $shared_vote_rating = $self->get_shared_rating(
  155. $other_member->member_id,
  156. $self->fk_entry_id
  157. );
  158.  
  159. if ( defined( $shared_vote_rating ) ) {
  160. my $shared_sum = 100 - abs($self->new_rating_value - $shared_vote_rating);
  161. $member->update_recent_trust( $other_member, $shared_sum );
  162. }
  163. }
  164. }
  165.  
  166. }
  167.  
  168. sub calculate_trust_for_updated_vote {
  169. my $self = shift;
  170. die "Updated vote";
  171. }
  172.  
  173. sub calculate_trust_for_deleted_vote {
  174. my $self = shift;
  175. die "Deleted vote";
  176. }
  177.  
  178. sub get_shared_rating {
  179. my ($self, $member_id, $entry_id) = @_;
  180.  
  181. my $schema = $self->result_source->schema;
  182.  
  183. # The idea behind this method is to avoid double-counting the effects of an entry in
  184. # vote log.
  185.  
  186. my $vote_log_entry = $schema->resultset( 'VoteLog' )->search({
  187. fk_voter_id => $member_id,
  188. fk_entry_id => $entry_id,
  189. })->first;
  190.  
  191. if ( $vote_log_entry ) {
  192. if ( $vote_log_entry->status eq 'new' ) {
  193. # The shared vote was not created at this point in time.
  194. return undef;
  195. }
  196. else {
  197. return $vote_log_entry->old_rating_value;
  198. }
  199. }
  200.  
  201. my $shared_vote = $schema->resultset('RecentVote')->find({
  202. fk_voter_id => $member_id,
  203. fk_voted_id => $entry_id,
  204. });
  205.  
  206. if ( $shared_vote ) {
  207. return $shared_vote->rating->value;
  208. }
  209.  
  210. return;
  211. }
  212.  
  213.  
  214. } 1
  215.  
  216. __END__
  217.  
  218. =head1 NAME
  219.  
  220. Sparcq::Schema::Result::VoteStatus - The status of a vote
  221.  
  222. =head1 DESCRIPTION
  223.  
  224. Tracks votes that have not yet been accounted for in trust ratings
  225.  
  226. =head1 AUTHOR
  227.  
  228. See L<Sparcq> for more information regarding authors.
  229.  
  230. =head1 COPYRIGHT & LICENSE
  231.  
  232. See L<Sparcq> for the copyright & license information.
  233.  
  234. =cut
Add Comment
Please, Sign In to add comment