<?php

/**
* @package Reports
*/
class Most_Popular_Articles extends Report
{

    function process()
    {
        ?>
        <script>
        function viewMostPopularArticlesSelected()
        {   
            var maxItemsPerPage = $('maxItemsPerPage').value;
            location.href='index.php?fuse=reports&view=ViewReport&report=Most+Popular+Articles&type=Knowledgebase&maxItemsPerPage=' + maxItemsPerPage;
        }
        </script>

        <?php
        $aGroup = array();                        
        $this->SetDescription($this->user->lang('This report displays the Most Popular Articles on the knowledgebase'));
        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()){
            $aGroup[] = array("<a href=\"index.php?fuse=knowledgebase&view=KB_viewArticle&articleId=".$id."\">".$title."</a>", 
                              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;
    }
}
?>
