I've seen a lot of sites gives users "points" for contributing to the site, so I decided to work up a plugin to do the same.
It creates a User column called "Points" that stores the value, rather than calculate it each time it's requested.
When this sub is ran, it will recalculate the points the user has. There are admin options to assign how many points for each review and each link.
Obviously I'm a complete perl newbie. My code below works, but I didn't know if there was a more efficient way to do it?
Also, I'd like to have this hooked into the system to run when the site is built, preferably at the beginning of the build so that the new points values can be used through the site. What hook would I need for this?
Thanks!
sub recalculate {
# -------------------------------------------------------------------
# This subroutine will be called whenever the user clicks on 'Recalculate' in the
# admin menu. Remember, you need to print your own HTTP header; to do so you
# can use:
#
print $IN->header();
print q|Starting...<br>|;
my ($Total_Links_Points,$Total_Reviews_Points,$Total_Points,$total_links,$total_reviews);
my $cfg = Links::Plugins::get_plugin_user_cfg ('Points');
my $ppl = $cfg->{'Links_Points'};
my $ppr = $cfg->{'Reviews_Points'};
my $users_db = $DB->table('Users');
my $reviews_db = $DB->table('Reviews');
my $links_db = $DB->table('Links');
my $sth = $users_db->select('Username');
while (my $hit = $sth->fetchrow_hashref()) {
print $hit->{Username};
print q| <br>|;
$total_links = $links_db->count({ LinkOwner => $hit->{Username} });
$Total_Links_Points = $total_links*$ppl;
$total_reviews = $reviews_db->count({ Review_Owner => $hit->{Username} });
$Total_Reviews_Points = $total_reviews*$ppr;
$Total_Points = $Total_Links_Points+$Total_Reviews_Points;
$users_db->update({ Points => $Total_Points}, { Username => $hit->{Username} });
print $Total_Points;
print q| <br><br>|;
}
print 'Finished';
}
Robert
http://www.pcprofiles.com
PC Profiles and hardware reviews
It creates a User column called "Points" that stores the value, rather than calculate it each time it's requested.
When this sub is ran, it will recalculate the points the user has. There are admin options to assign how many points for each review and each link.
Obviously I'm a complete perl newbie. My code below works, but I didn't know if there was a more efficient way to do it?
Also, I'd like to have this hooked into the system to run when the site is built, preferably at the beginning of the build so that the new points values can be used through the site. What hook would I need for this?
Thanks!
Code:
sub recalculate {
# -------------------------------------------------------------------
# This subroutine will be called whenever the user clicks on 'Recalculate' in the
# admin menu. Remember, you need to print your own HTTP header; to do so you
# can use:
#
print $IN->header();
print q|Starting...<br>|;
my ($Total_Links_Points,$Total_Reviews_Points,$Total_Points,$total_links,$total_reviews);
my $cfg = Links::Plugins::get_plugin_user_cfg ('Points');
my $ppl = $cfg->{'Links_Points'};
my $ppr = $cfg->{'Reviews_Points'};
my $users_db = $DB->table('Users');
my $reviews_db = $DB->table('Reviews');
my $links_db = $DB->table('Links');
my $sth = $users_db->select('Username');
while (my $hit = $sth->fetchrow_hashref()) {
print $hit->{Username};
print q| <br>|;
$total_links = $links_db->count({ LinkOwner => $hit->{Username} });
$Total_Links_Points = $total_links*$ppl;
$total_reviews = $reviews_db->count({ Review_Owner => $hit->{Username} });
$Total_Reviews_Points = $total_reviews*$ppr;
$Total_Points = $Total_Links_Points+$Total_Reviews_Points;
$users_db->update({ Points => $Total_Points}, { Username => $hit->{Username} });
print $Total_Points;
print q| <br><br>|;
}
print 'Finished';
}
Robert
http://www.pcprofiles.com
PC Profiles and hardware reviews