<?php

require_once "../../config.inc.php";
$auth = new auth("ANALYSE_RATINGS");
$page_action = "update";
$go = "update";
$page = new Page("Rating Results");
$form = new EditForm($lang->get("rateres", "Rate Results"));
$interval = array();
$count = countRows("pgn_rating", "RATINGID", "COMMENT <>''");
$steps = $count / 50;
for ($i = 0; $i < $steps; $i++) {
    $interval[$i][0] = $i * 50 . " - " . (($i + 1) * 50 - 1);
    $interval[$i][1] = $i;
}
$form->add(new Label("lbl", $lang->get("display", "Display"), "standard"));
$form->add(new Select("display", $interval, "standardlight", value("display"), 1));
$grid = new NXGrid("grid", 3);
$grid->setRatio(array(150, 350, 100));
$grid->addRow(array(new Label("lbl", "<b>" . $lang->get("page", "Page") . "</b>"), new Label("lbl", "<b>" . $lang->get("comment", "Comment") . "</b>"), new Label("lbl", "<b>" . $lang->get("date", "Date") . "</b>")));
$thisInterval = value("interval");
$sql = "SELECT SOURCEID, COMMENT, TIMESTAMP FROM pgn_rating WHERE COMMENT <> '' ORDER BY TIMESTAMP DESC";
$query = new query($db, $sql);
while ($query->getrow()) {
    $grid->addRow(array(new Label("lbl", resolvePageToLink($query->field("SOURCEID"))), new Label("lbl", str_replace('\\\\', '\\', $query->field("COMMENT"))), new Label("lbl", formatDBTimestamp($query->field('TIMESTAMP')))));
}
$query->free();
$form->add($grid);
$page->add($form);
$page->draw();
 /**
  * query the rows from the database
  */
 function getRows()
 {
     global $db, $c, $sid, $lang, $auth;
     $result = array();
     $order = "ca.TITLE";
     if ($this->order == "NAME") {
         $order = "ca.TITLE";
     } else {
         if ($this->order == "CATEGORY") {
             $order = "ca.CH_CAT_ID";
         } else {
             if ($this->order == "EDITED") {
                 $order = "cv.LAST_CHANGED";
             } else {
                 if ($this->order == "CREATED") {
                     $order = "cv.CREATED_AT";
                 } else {
                     if ($this->order == "POSITION") {
                         $order = "ca.POSITION";
                     }
                 }
             }
         }
     }
     $order .= " " . $this->orderdir;
     // $sql_articles = "Select ca.*, cv.LAST_USER, cv.CREATED_AT, cv.LAST_CHANGED FROM channel_articles ca, cluster_variations cv ".$this->getFilterJoin()." WHERE ca.CHID = ".$this->channelId." AND ca.ARTICLE_ID = cv.CLNID AND cv.VARIATION_ID = ".variation()." AND ca.VERSION=0 " . $this->getFilterSQL() . " ORDER BY $order LIMIT ".(($this->page - 1) * $this->recordsPerPage).",".$this->recordsPerPage;
     $sql_articles = "SELECT DISTINCT ca.* FROM channel_articles ca, cluster_variations cv " . $this->getFilterJoin() . " WHERE ca.ARTICLE_ID = cv.CLNID AND ca.CHID = " . $this->channelId . " AND ca.VERSION = 0 " . $this->getFilterSQL() . " ORDER BY {$order} LIMIT " . ($this->page - 1) * $this->recordsPerPage . "," . $this->recordsPerPage;
     // echo $sql_articles;
     $query = new query($db, $sql_articles);
     while ($query->getrow()) {
         $tmp = array();
         $varexists = true;
         $article_id = $query->field("ARTICLE_ID");
         $cvdatasql = "SELECT * FROM cluster_variations WHERE CLNID = " . $article_id . " AND VARIATION_ID=" . variation();
         $cvdata = new query($db, $cvdatasql);
         $cvdata->getrow();
         if ($cvdata->count() < 1) {
             $varexists = false;
         }
         array_push($tmp, $query->field("ARTICLE_ID"));
         $clid = getDBCell("cluster_variations", "CLID", "CLNID = " . $query->field("ARTICLE_ID") . " AND VARIATION_ID = " . variation());
         $live = isClusterLive($clid);
         if ($varexists) {
             if ($live) {
                 array_push($tmp, drawImage("green.gif", $lang->get("article_is_live", "Article is live")));
             } else {
                 array_push($tmp, drawImage("red.gif", $lang->get("article_is_expired", "Article is expired")));
             }
         } else {
             array_push($tmp, drawImage("gray.gif", $lang->get("article_variation_missing", "Variation of this article does not exist yet")));
         }
         array_push($tmp, $query->field("POSITION"));
         array_push($tmp, '<b>' . $query->field("TITLE") . '</b>');
         array_push($tmp, $this->categories[$query->field("CH_CAT_ID")]);
         array_push($tmp, formatDBTimestamp($cvdata->field("LAST_CHANGED")));
         $buttons = "&nbsp;" . crLink(drawImage('up.gif'), $c["docroot"] . "modules/channels/overview.php?sid={$sid}&action=up&article=" . $query->field("ARTICLE_ID"), "navelement");
         $buttons .= "&nbsp;" . crLink(drawImage('down.gif'), $c["docroot"] . "modules/channels/overview.php?sid={$sid}&action=down&article=" . $query->field("ARTICLE_ID"), "navelement");
         $buttons .= "&nbsp;";
         if ($auth->checkAccessToFunction("CHANNEL_DELETE")) {
             $buttons .= "&nbsp;" . crLink($lang->get("delete"), "javascript:confirmAction('" . $lang->get("del_article") . "', '" . $c["docroot"] . "modules/channels/overview.php?sid={$sid}&action=deletearticle&article=" . $query->field("ARTICLE_ID") . "');", "navelement");
         }
         if ($auth->checkAccessToFunction("CHANNEL_LAUNCH")) {
             $buttons .= "&nbsp;" . crLink($lang->get("launch", "Launch"), $c["docroot"] . "modules/channels/overview.php?sid={$sid}&action=launcharticle&article=" . $query->field("ARTICLE_ID"), "navelement");
             $buttons .= "&nbsp;" . crLink($lang->get("expire", "Expire"), $c["docroot"] . "modules/channels/overview.php?sid={$sid}&action=expirearticle&article=" . $query->field("ARTICLE_ID"), "navelement");
         }
         array_push($tmp, $buttons);
         array_push($result, $tmp);
     }
     return $result;
 }