# ================================================================== # Gossamer Links - enhanced directory management system # # Website : http://gossamer-threads.com/ # Support : http://gossamer-threads.com/scripts/support/ # CVS Info : 087,071,086,086,085 # Revision : $Id: Rate.pm,v 1.20 2007/12/19 06:59:12 brewt Exp $ # # Copyright (c) 2001 Gossamer Threads Inc. All Rights Reserved. # Redistribution in part or in whole strictly prohibited. Please # see LICENSE file for full details. # ================================================================== package Links::User::Rate; # ================================================================== use strict; use Links qw/:objects/; use Links::Build; use Links::SiteHTML; sub handle { # --------------------------------------------------- # Determine what to do. # my $id = $IN->param('ID'); # Make sure we are allowed to rate it. if ($CFG->{user_rate_required} and not $USER) { print $IN->redirect(Links::redirect_login_url('rate')); return; } # Now figure out what to do. my $mtl = Links::Build::build('title', Links::language('LINKS_RATE'), "$CFG->{db_cgi_url}/rate.cgi"); if ($IN->param('rate')) { my $results = $PLG->dispatch('rate_link', \&rate_it, {}); $results->{main_title_loop} = $mtl; if (defined $results->{error}) { print $IN->header(); print Links::SiteHTML::display('rate', $results); } else { print $IN->header(); print Links::SiteHTML::display('rate_success', $results); } } elsif (defined $id and ($id =~ /^\d+$/)) { print $IN->header(); my $rec = $DB->table('Links')->get($id); unless ($rec) { print Links::SiteHTML::display('error', { error => Links::language('RATE_INVALIDID', $id), main_title_loop => $mtl }); return; } $rec->{detailed_url} = $CFG->{build_detail_url} . '/' . $DB->table('Links')->detailed_url($rec->{ID}) if $CFG->{build_detailed}; print Links::SiteHTML::display('rate', { %$rec, main_title_loop => $mtl }); } else { print $IN->redirect($IN->param('d') ? "$CFG->{db_cgi_url}/page.cgi?d=1" : $CFG->{build_root_url} . "/" . ($CFG->{build_home} || ($CFG->{build_index_include} ? $CFG->{build_index} : ''))); } } sub rate_it { # -------------------------------------------------------- # Give this link a rating. # my $id = $IN->param('ID'); my $rating = $IN->param('rate'); # Let's get the link information. my $links = $DB->table('Links'); my $rec = $links->get($id); $rec or return { error => Links::language('RATE_INVALIDID', $id) }; # Make sure we have a valid rating. unless ($rating =~ /^\d\d?$/ and $rating >= 1 and $rating <= 10) { return { error => Links::language('RATE_INVALIDRATE', $rating), %$rec }; } # Update the rating unless they have already voted. my $clicktrack = $DB->table('ClickTrack'); my $rows = $clicktrack->count({ LinkID => $id, IP => $ENV{REMOTE_ADDR}, ClickType => 'Rate' }); if ($rows) { return { error => Links::language('RATE_VOTED', $id), %$rec }; } else { eval { $clicktrack->insert({ LinkID => $id, IP => $ENV{REMOTE_ADDR}, ClickType => 'Rate', Created => \'NOW()' }); $rec->{Rating} = ($rec->{Rating} * $rec->{Votes} + $rating) / ++$rec->{Votes}; $links->update({ Rating => $rec->{Rating}, Votes => $rec->{Votes} }, { ID => $rec->{ID} }); }; return $rec; } } 1;