<?php

include_once ("../includes/config.php");
include_once ("../includes/conex.php");
include_once ("../includes/utils_fbs.php");
include_once ("../includes/Mobile-Detect/Mobile_Detect.php");
include_once ("../includes/translate.php");
include_once ("../fdesign.php");


$rem_user   = getenv("REMOTE_USER");

$user              = get_var ('user');
$name              = get_var ('name');
$resource_id       = get_var ('resource_id');
$param_category_id = get_var ('category_id');
$myscore           = get_var ('myscore');

$L = get_lang ();

$PAGE = "";


$sql_score = "select count(*), coalesce (avg(score)::int, 0) from tvotings where resource_id = $resource_id";
$res_sql_score = pg_query ($dbmultimedia, $sql_score) or die('Query failed: ' . pg_last_error());
$row = pg_fetch_row($res_sql_score);
$num_votos = $row[0];
$media     = $row[1];
if ($num_votos > 0) {
  $PAGE .= "<SPAN id=\"voted\"><BR><BR><BR>" . t($L,"Number of votes") . ":</SPAN>  $num_votos";
  $PAGE .= "<SPAN id=\"voted\"><BR>" . t($L,"Score") . ": </SPAN> " . getStarsTable ($media);
}
$sql_ya_votado = "select count(*) from tvotings where resource_id = $resource_id and author = '$rem_user'";
$res_sql_ya_votado = pg_query ($dbmultimedia, $sql_ya_votado) or die('Query failed: ' . pg_last_error());
$row = pg_fetch_row($res_sql_ya_votado);
$ya_votado = $row[0];
if ($ya_votado > 0) {
  $PAGE .= "<DIV id=\"voted\"><BR><BR>" . t($L, "Already voted") . "</DIV>";
} else {

  if (($myscore != "") and (is_numeric ($myscore)) and ($myscore>=0) and ($myscore<=10)) {  
    $insert = "INSERT INTO tvotings (resource_id,author,creation_time,score) VALUES ($resource_id, '$rem_user', now(), $myscore)";
    $res_insert = pg_query ($dbmultimedia, $insert) or die('Insert failed: ' . pg_last_error());    
    $url = "/identificado/score?resource_id=$resource_id";
    header("Location: $url");
    exit;
    #$PAGE .= "<BR><BR>" . t($L, "Already voted");
  } else {  
    $PAGE .= "<BR><BR>\n";
    $PAGE .= "<FORM>\n";
    $PAGE .= "<INPUT TYPE=\"submit\" VALUE=\"" . t($L,"You can vote") . "\">\n";
    $PAGE .= "<INPUT TYPE=\"hidden\" NAME=\"resource_id\" VALUE=\"$resource_id\">\n";  
    $PAGE .= "<SELECT name=\"myscore\" SIZE=\"1\">\n";
    $PAGE .= "<OPTION SELECTED VALUE=\"-\">" . t($L,"Not rated") . "</OPTION>\n";
    $PAGE .= "<OPTION VALUE=\"0\">"  . t($L,"Very bad") . "</OPTION>\n";
    $PAGE .= "<OPTION VALUE=\"2\">"  . t($L,"Bad") . "</OPTION>\n";
    $PAGE .= "<OPTION VALUE=\"4\">"  . t($L,"Regular") . "</OPTION>\n";
    $PAGE .= "<OPTION VALUE=\"6\">"  . t($L,"Good") . "</OPTION>\n";
    $PAGE .= "<OPTION VALUE=\"8\">"  . t($L,"Very good") . "</OPTION>\n";
    $PAGE .= "<OPTION VALUE=\"10\">" . t($L,"Excellent") . "</OPTION>\n";
    $PAGE .= "</SELECT>\n";
    $PAGE .= "</FORM>\n";
    $PAGE .= "<BR><BR>\n";
  }
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <LInk rel="stylesheet" type="text/css" href="../css/mmedia.css" media="screen" />
</HEAD>
<BODY>
 
<DIV id="container">
  <?php include '../header.php'; ?>

  <div id="page">
    <?php echo $PAGE ?>
  </div>

  <?php include '../footer.php'; ?>

</BODY>
</HTML>



