Gossamer Forum
Home : Products : Gossamer Links : Version 1.x :

Tweaks to Top Rated routines and template

Quote Reply
Tweaks to Top Rated routines and template
While I'm sure people have customized the routines a bit, here is a suggestion for people who have not yet (or have wished) to have a better way.

First, some caveats.

1) If in Links.pm your # of links is greater than the number of links in your database, then you'll get no links showing. I don't know why... but rather than all links, you get none. Example: you have added 5 links, but have your "Popular Cutoff" set to 10. NO links will show up. (Ok, this is a Cool.html thing, but it's related to the Top_Rated).

2) If you don't have links with 10 votes, nothing will show up in the Top_Rated, for a long, long time, unless your people like to vote. Then, when they do, the first link there starts to get skewd, etc. So, it pays to drop the cutoff early on to maybe 2 votes. (But that means changing the templates all the time as well.

Here are some fixes:

1) In the rate_top.html replace the hard-coded number of top links with <%$VoteS_Lim%> and the hard-coded "at least" # with : <%vote_cutoff%>

Example:
Code:
<p><strong>Top <%$VoteS_Lim%> Resources (by Rating)
-- with at least <%vote_cutoff%> votes</strong></p>

<table border=0>
<tr><th><strong>Rating</strong></th><th><strong># Votes</strong></th>
<th align=left><strong>Resource</strong></th></tr>
<%top_rated%>
</table>

<p><strong>Top <%$VoteS_Lim%> Resources (by Votes)
-- with at least <%vote_cutoff%> votes</strong></p>

<table border=0>
<tr><th><strong># Votes</strong></th><th><strong>Rating</strong></th>
<th align=left><strong>Resource</strong></th></tr>
<%top_votes%>
</table>
Note, I also reversed the display order of By Votes to list the ordered column first.

Then, you want to make the following changes to the build_rate routine:

Code:
sub build_rate_page {
# --------------------------------------------------------
# Creates a Top 10 ratings page.
#
my ($s, $e, $f, $rated, $voted, $link, $min_v, $max_v, $min_r, $max_r, $top_rated, $top_votes);

$USE_HTML ?
print "Building <a href='$LINKS{build_ratings_url}/$LINKS{build_index}' target='_blank'>Top Rated</a> ... \n" :
print "Building Top Rated Page ... \n";
$s = time();

my $VoteS = '6'; ## cut off for votes.
my $VoteS_Lim = '10'; ## number of votes.

# Create the directory.
($LINKS{build_ratings_path} =~ m,^$LINKS{build_root_path}/(.*)$,) and &build_dir ($1);

print "\tGetting list of top 10 rated links ... \n";
$rated = $LINKDB->prepare (qq!
SELECT *
FROM Links
WHERE Votes >= $VoteS
and ID NOT IN (341, 641)
ORDER BY Rating DESC
LIMIT $VoteS_Lim
!);
$rated->execute();
print "\tGetting list of top 10 voted links ... \n";
$voted = $LINKDB->prepare (qq!
SELECT *
FROM Links
WHERE Votes >= $VoteS
and ID NOT IN (341, 641)
ORDER BY Votes DESC
LIMIT $VoteS_Lim
!);
$voted->execute();

# Now build the html.
$top_rated = ''; $top_votes = '';
my $r_count = 0; my $v_count= 0;
$min_r = 99999999; $max_r = 0;
while ($link = $rated->fetchrow_hashref) {
$r_count++;
$min_r = $link->{'Votes'} if ($min_r > $link->{'Votes'});
$max_r = $link->{'Votes'} if ($max_r < $link->{'Votes'});
$top_rated .= qq~<tr><td align=center>${$link}{'Rating'}</td>
<td align=center>${$link}{'Votes'}</td>
<td><a href="$LINKS{db_cgi_url}/jump.cgi?ID=${$link}{'ID'}.html">${$link}{'Title'}</a></td></tr>\n~;
}
$min_v = 99999999; $max_v = 0;
while ($link = $voted->fetchrow_hashref) {
$v_count++;
$min_v = $link->{'Votes'} if ($min_v > $link->{'Votes'});
$max_v = $link->{'Votes'} if ($max_v < $link->{'Votes'});
$top_votes .= qq~<tr><td align=center>${$link}{'Votes'}</td>
<td align=center>${$link}{'Rating'}</td>
<td><a href="$LINKS{db_cgi_url}/jump.cgi?ID=${$link}{'ID'}.html">${$link}{'Title'}</a></td></tr>\n~;
}

# And write it to a file.
($min_v == 99999999) and ($min_v = 0);
($min_r == 99999999) and ($min_r = 0);
($r_count > $v_count) ? $VoteS_Lim = $r_count : $VoteS_Lim = $v_count;
open (RATE, ">$LINKS{build_ratings_path}/$LINKS{build_index}")
or die "unable to open top rated page: $LINKS{build_ratings_path}/$LINKS{build_index}. Reason: $!";
print "\tVote Range: $min_v .. $max_v\n";
print "\tRate Range: $min_r .. $max_r\n";
print RATE &site_html_ratings ( { vote_cutoff => $VoteS,
vote_limit => $VoteS_Lim,
vote_count => $v_count,
rate_count => $r_count,
top_rated => $top_rated,
top_votes => $top_votes } );
close RATE;

# All done.
$f = time();
$e = $f - $s;
print "Done ($e s)\n\n";
}
Note... the added variable definition $VoteS and the changes to the two SQL statements.


Here's another change, that answers one of the questions here "how to prevent certain links or categories from appearing on the ratings, but still otherwise treat them as normal" --

and ID NOT IN (341, 641)

What that does, is screen out any links that match the ID which are in the () list. You can exclude whole categories with:

and CategoryID NOT IN (40, 27, 32)

Or, you can mix and match.

This is really an example that shows the power of SQL, vs changing any perl code.

(It wasn't 10 minutes before I thought of an upgrade... by adding $count, the program now automatically adjusts if it doesn't pull enough items. I pass three variables to the template -- vote_limit, vote_count and rate_count so that you have a choice of using specific tags, or just vote_limit which _should_ be the same as vote_count and rate_count -- unless your SQL statements are different.)

PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://postcards.com/FAQ


Quote Reply
Re: Tweaks to Top Rated routines and template In reply to
Thanks for the codes, pugdog...helps to add more "dynamic" content in pages rather than hard coding values.

Regards,

Eliot Lee