ONO::FW::Apps::Rank

package ONO::FW::Apps::Rank;
################################################################################
# COPYRIGHT / LICENSE #
################################################################################
#
# This file is part of the ONO Software Project.
#
# Copyright (C) 2000-2025 Jos KIRPS [ www.kirps.com | jos_AT_kirps_DOT_com ]
# and The Joopita Project [ www.joopita.org | contact_AT_joopita_DOT_com ]
#
# This file, as well as other parts of the ONO Software Project or related
# elements, are FREE SOFTWARE available under the ARTISTIC LICENSE 2.0.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# For the full license, see /ono/osr/license/LICENSE.txt, or write to
# jos_AT_kirps_DOT_com or contact_AT_joopita_DOT_com.
#
################################################################################
# END OF COPYRIGHT / LICENSE, HERE COMES THE CODE ... #
################################################################################

use strict;

use ONO::IO;

use ONO::Lib::UI::Rate;
use ONO::DB;
use ONO::Lib::DateTime::ToolBox;
use ONO::Lib::UI::MSG;
use ONO::Lib::SQL::Quote;

#: This module provides app ranking related features.

###############################################################################
# get app stars
###############################################################################

sub stars {

my (
$self,
$db,
$community,
$table,
$type,
$vars_ref,
) = @_;

#: Get app star ranking.

my %vars = %$vars_ref;

my $sql_ref = ONO::Lib::SQL::Quote->vars($vars_ref);
my %sql = %$sql_ref;

my $stars1 = ONO::DB->get($db,"stars","${community}_${table}_${type}","app = '$sql{'app'}");
my $stars2 = $stars1;
if (!$stars2) {
$stars2 = "XX";
}
if (length $stars2 < 2) {
$stars2 = "0$stars2";
}

return ($stars1,$stars2);

}

###############################################################################
# comments
###############################################################################

sub comments {

my (
$self,
$db,
$community,
$table,
$type,
$switches,
$vars_ref,
) = @_;

#: Display app comments.

my %vars = %$vars_ref;

my $sql_ref = ONO::Lib::SQL::Quote->vars($vars_ref);
my %sql = %$sql_ref;

my ($comments,$rank,$counter,$ranked,$commented);

my ($width,$height,$right) = &sizes($switches);

foreach my $line (ONO::DB->select($db,"${community}_${table}_rank","app = '$sql{'app'}'","ORDER BY timestamp DESC")) {
my @row = ONO::DB->readcols($line);

$counter++;

my $trstyle = qq~border-top:1px dotted #cccccc~;

if ($row[2] eq $vars{'username'}) {
$ranked = int($row[3]/100000);
$commented = $row[4];
$trstyle = qq~border:1px solid #cccccc;background-color:#cceecc;~;
}

my $stars = int($row[3]/100000);
if (length $stars < 2) {
$stars = "0$stars";
}
$rank = $rank + $row[3];

my $time = ONO::Lib::DateTime::ToolBox->get($row[5],0,"iz");

my ($text,$stron,$stroff);
if ($row[4] =~ /[A-Za-z0-9]/) {
$text = qq~<br>$row[4]~;
$stron = qq~<strong>~;
$stroff = qq~:</strong>~;
}

if ($switches =~ /m/) {
$comments .= qq~<tr>
<td class="bt small" style="padding:10px">
<img class="block fr" src="/ono/osr/images/rating/stars_$stars.gif" style="width:${width}px;height:${height}px" alt="">
$stron<a href="/users/$row[2]">$row[2]</a> - $time$stroff$text
</td>
</tr>
~;
} else {
$comments .= qq~<tr class="vtop" style="$trstyle">
<td style="padding:10px">
<img class="block" src="/ono/osr/images/rating/stars_$stars.gif" style="width:80px;height:16px" alt="">
</td>
<td class="w100" style="padding:10px">
$stron<a href="/users/$row[2]">$row[2]</a> - $time$stroff$text
</td>
</tr>
~;
}
}

if ($counter) {

$comments = qq~<table class="wide_table">$comments</table>~;

$rank = int($rank/$counter);
my $stars = int($rank/100000);

ONO::DB->command($db,"UPDATE ${community}_${table}_${type} SET rank = '$rank', stars = '$stars' WHERE app = '$sql{'app'}';");

ONO::IO->mkdir("var/community/$community/$table/$vars{'app'}");
ONO::IO->store("var/community/$community/$table/$vars{'app'}/rank.txt",$rank);

}

return ($comments,$rank,$counter,$ranked,$commented);

}

###############################################################################
# add comment
###############################################################################

sub add_comment {

my (
$self,
$db,
$community,
$table,
$type,
$text_ref,
$vars_ref,
) = @_;

#: Add a new app comment.

my @text = @$text_ref;
my %vars = %$vars_ref;

my $sql_ref = ONO::Lib::SQL::Quote->vars($vars_ref);
my %sql = %$sql_ref;

my $MSG;

if ($vars{'sid'} && $vars{'app'} && $vars{'rating_app_rank_selected'} ne "" && $vars{'rating_app_rank_selected'} < 11) {

$sql{'app_comment'} =~ s~(\<|\>|\[|\])~~g;
$vars{'rating_app_rank_selected'} =~ s~[^0-9]~~g;
my $rank = $vars{'rating_app_rank_selected'} * 100000;
my $timestamp = time;
my $ranked;

# check if user already ranked this app
# if yes, ask if we should overwrite this

foreach my $line (ONO::DB->select($db,"${community}_${table}_rank","username = '$vars{'username'}' AND app = '$sql{'app'}'")) {
my @row = ONO::DB->readcols($line);

$ranked++;

ONO::DB->command($db,"UPDATE ${community}_${table}_rank SET
rank = '$rank',
comment = '$sql{'app_comment'}',
timestamp = '$timestamp'
WHERE username = '$vars{'username'}' AND app = '$sql{'app'}';");

}

# if not, add ranking now

if (!$ranked) {

ONO::DB->command($db,"INSERT INTO ${community}_${table}_rank
(app,username,rank,comment,timestamp) VALUES
('$sql{'app'}','$vars{'username'}','$rank','$vars{'app_comment'}','$timestamp');");

}

$MSG .= ONO::Lib::UI::MSG->success($text[1]);

}

return $MSG;

}

###############################################################################
# star selection
###############################################################################

sub star_sel {

my (
$self,
$ranked,
$switches,
) = @_;

#: Select star ranking.

return ONO::Lib::UI::Rate->stars("app_rank",$ranked);

}

###############################################################################
# add app
###############################################################################

sub add_app {

my (
$self,
$db,
$community,
$table,
$type,
$vars_ref,
) = @_;

#: Add app (?)

my %vars = %$vars_ref;

my $sql_ref = ONO::Lib::SQL::Quote->vars($vars_ref);
my %sql = %$sql_ref;

if ($vars{'sid'} && $vars{'app'} && $vars{'rating_app_rank_selected'} ne "" && $vars{'rating_app_rank_selected'} < 11) {

if (!ONO::DB->count($db,"${community}_${table}_${type}","app = '$sql{'app'}'")) {
ONO::DB->command($db,"INSERT INTO ${community}_${table}_${type} (app) VALUES ('$sql{'app'}');");
}

}

}

###############################################################################
# sub: sizes
###############################################################################

sub sizes {

#: Star ranking sizes

my $switches = $_[0];

my $width = 80;
my $height = 16;
my $right = 20;
if ($switches =~ /m/) {
$width = 60;
$height = 12;
$right = 10;
}
if ($switches =~ /s/) {
$width = 40;
$height = 8;
$right = 5;
}

return ($width,$height,$right);

}

###############################################################################
# end of script
###############################################################################

1;

__END__