<?php
/**
 * Most Popular KB Articles Report
 *
 * @category Report
 * @package  ClientExec
 * @author   Jason Yates <jason@clientexec.com>
 * @license  ClientExec License
 * @version  1.1
 * @link     http://www.clientexec.com
 *
 *************************************************
 *   1.1 Updated the report to use Pear Commenting & the new title handing to make app reports consistent.
 ************************************************
 */


/**
 * Most_Popular_Articles Report Class
 *
 * @category Report
 * @package  ClientExec
 * @author   Jason Yates <jason@clientexec.com>
 * @license  ClientExec License
 * @version  1.1
 * @link     http://www.clientexec.com
 */
class Most_Popular_Articles extends Report
{
    protected $featureSet = 'support';

    /**
     * Report Process Method
     *
     * @return null - direct output
     */
    function process()
    {

        // Set the report information
        $this->setName('Most Popular Knowledgebase Articles', true);
        $this->setVersion("1.1");
        $this->SetDescription($this->user->lang('This report displays the Most Popular Articles on the knowledgebase'));

        // Output the headers before anything else
        $this->showReportHeader();
        ?>
<script>
    function viewMostPopularArticlesSelected()
    {
        var maxItemsPerPage = document.getElementById('maxItemsPerPage').value;
        location.href='index.php?fuse=reports&view=ViewReport&report=Most+Popular+Articles&type=Knowledgebase&maxItemsPerPage=' + maxItemsPerPage;
    }
</script>

        <?php
        $aGroup = array();
        echo "<br>".$this->user->lang('Display:').' ';
        echo "<select id=maxItemsPerPage name=maxItemsPerPage onChange='viewMostPopularArticlesSelected()'>";
        $array = array(10,20,50,70,100);
        for ($i = 0; $i < count($array); $i++) {
            if (isset($_REQUEST['maxItemsPerPage']) && ($_REQUEST['maxItemsPerPage'] == $array[$i])) {
                echo"<option value=".$array[$i]." selected>".$array[$i]."</option>";
            }else {
                echo"<option value=".$array[$i].">".$array[$i]."</option>";
            }
        }
        echo "</select><br /><br />";
        if (isset($_REQUEST['maxItemsPerPage'])) {
            $reportSQL = "SELECT id, title, created, rating, totalvisitors FROM  kb_articles ORDER BY rating DESC LIMIT 0,".$_REQUEST['maxItemsPerPage'];
        }else {
            $reportSQL = "SELECT id, title, created, rating, totalvisitors FROM  kb_articles ORDER BY rating DESC LIMIT 0,10";
        }

        $result = $this->db->query($reportSQL);
        while (list($id, $title, $created, $rating, $visitors) = $result->fetch()) {
            ////$id of article for future links
            $aGroup[] = array("&nbsp;".$title,
                    NE_Lib::db_to_form($created, $this->settings->get('Date Format'), '-', true),
                    '<center>'.$rating.'</center>',
                    '<center>'.$visitors.'</center>',
            );
        }
        $this->Add($aGroup,
                $this->user->lang('Most Popular Articles'),
                array ('<center>'.$this->user->lang('Article Title').'</center>',
                '<center>'.$this->user->lang('Date Created').'</center>',
                '<center>'.$this->user->lang('Article Rating').'</center>',
                '<center>'.$this->user->lang('Total Visitors').'</center>'
        ));
        $this->Show();
    }

    function convertDate($date)
    {
        $date = date("F j, Y", strtotime($date));
        return $date;
    }
}
?>
