Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

plugin to calculate "points" for users

Quote Reply
plugin to calculate "points" for users
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!


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
Quote Reply
Re: [Robert_B] plugin to calculate "points" for users In reply to
Great Idea,

I had the same idea in my head but my programming skills did not allow me to take it any further.
Quote Reply
Re: [rascal] plugin to calculate "points" for users In reply to
Hopefully I'll get something packaged up for it someday.

Does anyone know what the build hooks are? I couldn't find any in the lsql documentation.

Robert
http://www.pcprofiles.com
PC Profiles and hardware reviews
Quote Reply
Re: [Robert_B] plugin to calculate "points" for users In reply to
Judging by the hooks called in nph_build.cgi, I'd say you could run PRE hook on "create_home" (or any of the create_* hooks, such as "create_ratings"). Just remember to return @args and DON'T issue the STOP command, or else the normal build process for create_home won't run.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [Robert_B] plugin to calculate "points" for users In reply to
Ian and I worked on something like this in his UserMonitor plugin several years ago to rank users on their interaction with the site. The score I had, included number of logins and how many helpful votes their reviews received. I think it was just updated everytime the profile page for that user was viewed - this was on a dynamic site.

One way to update on each build is to use your sub as a global that doesn't print anything (either in the globals list or as a separate sub in your plugin). Then you can just call the global on the homepage and every time the homepage the global will invisibly do its stuff. (If I haven't explained that properly let me know).
Quote Reply
Re: [Robert_B] plugin to calculate "points" for users In reply to
Hi,

If you are trying to do this on a "static" site, there are a few ways to go about this, without dragging your system performance down.

One way is to run it as a cron job.

Once a day, or on a very active site, maybe every 4-6 hours.

The other way is to run it when a user actually contributes something. Linkit to the submit_success page that is shown to the user, and give them their current "stats" after each submission. It *should* be current all the time with this, but only updated when it has to update. You wouldn't need any hooks, just simply call the global to run on that users contributions and update their stats with a global call in the _success template.

The only time points are updated is when a user submits something, so this would be the most efficient way.


PUGDOG´┐Ż Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.