Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use MooseX::Declare;
- class Sparcq::Schema::Result::VoteLog
- extends Sparcq::Schema::Result {
- __PACKAGE__->table('vote_log');
- __PACKAGE__->add_columns(
- vote_log_id => {
- data_type => 'int',
- size => '11',
- is_nullable => 0,
- extra => {
- unsigned => 1,
- },
- is_auto_increment => 1,
- },
- fk_vote_id => {
- data_type => 'int',
- size => '11',
- is_nullable => 0,
- extra => {
- unsigned => 1,
- },
- },
- fk_entry_id => {
- data_type => 'int',
- size => '11',
- is_nullable => 0,
- extra => {
- unsigned => 1,
- },
- },
- fk_voter_id => {
- data_type => 'int',
- size => '11',
- is_nullable => 0,
- extra => {
- unsigned => 1,
- },
- },
- status => {
- data_type => 'varchar',
- size => '15',
- is_nullable => 0,
- },
- old_rating_value => {
- data_type => 'int',
- size => '11',
- is_nullable => 0,
- default_value => 0,
- extra => {
- unsigned => 1,
- },
- },
- new_rating_value => {
- data_type => 'int',
- size => '11',
- is_nullable => 0,
- default_value => 0,
- extra => {
- unsigned => 1,
- },
- },
- cr_date => {
- data_type => 'datetime',
- set_on_create => 1,
- is_nullable => 0,
- },
- );
- __PACKAGE__->set_primary_key('vote_log_id');
- __PACKAGE__->belongs_to(
- vote => 'Sparcq::Schema::Result::Vote',
- { 'foreign.vote_id' => 'self.fk_vote_id' },
- );
- __PACKAGE__->belongs_to(
- member => 'Sparcq::Schema::Result::Member',
- { 'foreign.member_id' => 'self.fk_voter_id' },
- );
- __PACKAGE__->belongs_to(
- vote => 'Sparcq::Schema::Result::Entry',
- { 'foreign.entry_id' => 'self.fk_entry_id' },
- );
- __PACKAGE__->meta->make_immutable(inline_constructor => 0);
- sub calculate_change_in_trust {
- my $self = shift;
- # Start Transaction
- my $schema = $self->result_source->schema;
- $schema->storage->txn_begin;
- eval {
- my $method = "calculate_trust_for_" . $self->status . "_vote";
- $self->$method;
- $self->delete;
- # Commit Transaction
- $schema->storage->txn_commit;
- };
- if ($@) {
- print ">: " . $@ . "\n";
- # Rollback Transaction
- $schema->storage->txn_rollback;
- }
- }
- sub calculate_trust_for_new_vote {
- my $self = shift;
- my $schema = $self->result_source->schema;
- my $member = $self->member;
- my $other_members = $schema->resultset('Member')->search({
- member_id => { '!=' => $member->member_id },
- });
- # Iterate through all of the other members
- while (my $other_member = $other_members->next) {
- # Look up the trust between member and other_member.
- if ( my $trust = $schema->resultset( 'RecentTrust' )->find(
- $member->member_id,
- $other_member->member_id ) ) {
- my $shared_vote_rating = $self->get_shared_rating(
- $other_member->member_id,
- $self->fk_entry_id
- );
- if ( defined( $shared_vote_rating ) ) {
- my $shared_sum = 100 - abs($self->new_rating_value - $shared_vote_rating );
- $member->update_recent_trust( $other_member, $trust->shared_rating_sum + $shared_sum );
- }
- }
- else {
- # If there is no trust between the two members,
- # Find out whether the other user rated this vote.
- my $shared_vote_rating = $self->get_shared_rating(
- $other_member->member_id,
- $self->fk_entry_id
- );
- if ( defined( $shared_vote_rating ) ) {
- my $shared_sum = 100 - abs($self->new_rating_value - $shared_vote_rating);
- $member->update_recent_trust( $other_member, $shared_sum );
- }
- }
- }
- }
- sub calculate_trust_for_updated_vote {
- my $self = shift;
- die "Updated vote";
- }
- sub calculate_trust_for_deleted_vote {
- my $self = shift;
- die "Deleted vote";
- }
- sub get_shared_rating {
- my ($self, $member_id, $entry_id) = @_;
- my $schema = $self->result_source->schema;
- # The idea behind this method is to avoid double-counting the effects of an entry in
- # vote log.
- my $vote_log_entry = $schema->resultset( 'VoteLog' )->search({
- fk_voter_id => $member_id,
- fk_entry_id => $entry_id,
- })->first;
- if ( $vote_log_entry ) {
- if ( $vote_log_entry->status eq 'new' ) {
- # The shared vote was not created at this point in time.
- return undef;
- }
- else {
- return $vote_log_entry->old_rating_value;
- }
- }
- my $shared_vote = $schema->resultset('RecentVote')->find({
- fk_voter_id => $member_id,
- fk_voted_id => $entry_id,
- });
- if ( $shared_vote ) {
- return $shared_vote->rating->value;
- }
- return;
- }
- } 1
- __END__
- =head1 NAME
- Sparcq::Schema::Result::VoteStatus - The status of a vote
- =head1 DESCRIPTION
- Tracks votes that have not yet been accounted for in trust ratings
- =head1 AUTHOR
- See L<Sparcq> for more information regarding authors.
- =head1 COPYRIGHT & LICENSE
- See L<Sparcq> for the copyright & license information.
- =cut
Add Comment
Please, Sign In to add comment