<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+

$showm = '7';
include('header.php');

function count_avatars() {
	$q = mysql_query("SELECT avatar FROM pm_users WHERE avatar != 'no_avatar.gif'");
	$count = mysql_num_rows($q);
	return $count;
}

function is_today($timestamp){
	return date('Y-m-d',$timestamp) == date('Y-m-d');
}

function is_yesterday($timestamp){
	$yesterday  = mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"));
	return date('Y-m-d',$timestamp) == date('Y-m-d', $yesterday);
}

function is_thismonth($timestamp){
	$this_month  = mktime(0, 0, 0, date("m")  , date("d"), date("Y"));
	return date('Y-m',$timestamp) == date('Y-m', $this_month);
}

function top1_search() {
	$query = mysql_query("SELECT string, hits FROM pm_searches ORDER BY hits DESC LIMIT 1")or die(" Error: ".mysql_error());
	$r = mysql_fetch_array($query);
	if(!empty($r['string'])) {
		$keyword = $r['string'];
	return $keyword .' ('. pm_number_format($r['hits']).')';
	} else {
	return 'N/A yet.';
	}
}

function top1_rated() 
{
	
	$sql = "SELECT id, total_votes 
			FROM pm_ratings 
			ORDER BY total_votes 
			DESC LIMIT 1";
	$result = mysql_query($sql);
	$r = mysql_fetch_array($result);
	mysql_free_result($result);
	
	if ($r['id'] && $r['total_votes'] > 0) 
	{
		$vote = ($r['total_votes'] > 1) ? 'votes' : 'vote';

		return '<a href="'. _URL .'/musicvideo.php?vid='. $r['id'] .'">'. vnamefromvid($r['id']) .' ('. pm_number_format($r['total_votes']) ." $vote)</a>";
	} 
	else 
	{
		return 'N/A yet.';
	}
}

function top1_commented() 
{
	
	$sql = "SELECT uniq_id, COUNT(*) as total  
			FROM pm_comments 
			WHERE uniq_id NOT LIKE 'article-%' 
			GROUP BY uniq_id 
			ORDER BY total DESC 
			LIMIT 1";
	$result = mysql_query($sql);
	$r = mysql_fetch_array($result);
	mysql_free_result($result);
	
	if ($r['uniq_id']) 
	{
		return '<a href="comments.php?vid='. $r['uniq_id']. '">'. vnamefromvid($r['uniq_id']) .' ('. pm_number_format($r['total']) .')</a>';
	} 
	else 
	{
		return 'N/A yet.';
	}
}
function top1_commentor() 
{
	
	$sql = "SELECT pm_comments.user_id, pm_users.username, COUNT(*) as total 
			FROM pm_comments
			JOIN pm_users ON ( pm_comments.user_id = pm_users.id )
			WHERE pm_comments.user_id != '0' 
			GROUP BY pm_comments.user_id
			LIMIT 1 ";

	$result = mysql_query($sql);
	$r = mysql_fetch_array($result);
	mysql_free_result($result);
	
	if ($r['username']) 
	{
		return $r['username'] .' (<a href="comments.php?keywords='. $id .'&search_type=username&submit=Search">'. pm_number_format($r['total']) .'</a>)';
	} 
	else 
	{
		return 'N/A yet.';
	}
}
function member_searches() {
	$query = mysql_query("SELECT user FROM pm_searches WHERE user != 'guest'");
	$r = mysql_num_rows($query);
	return $r;
}


function recent_dates($date, $table, $orderby, $datefield) {

	$query = mysql_query("SELECT ".$datefield." FROM ".$table." ORDER BY ".$orderby." DESC")or die(" Error: ".mysql_error());
	$count=0;
	while ($row = mysql_fetch_array($query)) 
	{ 
			if($date($row[$datefield])) {
				$count++;
			} 
	}
	return $count;
}

?>
<div id="sub_menu">
<li><a href="show_searches.php">Top searches</a></li>
</div>
<div id="content">
<h2>General Statistics</h2>



<table width="100%" border="0" cellspacing="5" cellpadding="10">
  <tr>
    <td width="50%" valign="top">
	<fieldset><legend><strong>Video Related</strong></legend>
	<table width="100%" border="0" cellspacing="2" cellpadding="5">
      <tr class="table_row1">
        <td width="30%"><strong>Total videos:</strong></td>
        <td width="70%"><?php echo pm_number_format($config['total_videos']); ?> <a href="videos.php"><img src="images/go.gif" alt="Go" width="12" height="7" border="0" /></a></td>
      </tr>
      <tr class="table_row2">
        <td width="30%"><strong>Most comments:</strong></td>
        <td width="70%"><?php echo top1_commented(); ?></td>
      </tr>
      <tr class="table_row1">
        <td width="30%"><strong>Most rated:</strong></td>
        <td width="70%"><?php echo top1_rated(); ?></td>
      </tr>
      <tr class="table_row2">
        <td width="30%"><strong>Reported videos:</strong></td>
        <td width="70%"><?php echo pm_number_format(count_entries('pm_reports', 'r_type', '1')); ?> <a href="reports.php"><img src="images/go.gif" alt="Go" width="12" height="7" border="0" /></a></td>
      </tr>
    </table>
	</fieldset>
	</td>
    <td valign="top">
	<fieldset><legend><strong>Members</strong></legend>
	<table width="100%" border="0" cellspacing="2" cellpadding="5">
      <tr class="table_row1">
        <td width="30%"><strong>Total members:</strong></td>
        <td width="70%"><?php echo pm_number_format(count_entries('pm_users', '', '')); ?> <a href="members.php"><img src="images/go.gif" alt="Go" width="12" height="7" border="0" /></a></td>
      </tr>
      <tr class="table_row2">
        <td width="30%"><strong>Joined today:</strong></td>
        <td width="70%"><?php echo pm_number_format(recent_dates('is_today', 'pm_users', 'id', 'reg_date')); ?></td>
      </tr>
      <tr class="table_row1">
        <td width="30%"><strong>Joined yesterday:</strong></td>
        <td width="70%"><?php echo pm_number_format(recent_dates('is_yesterday', 'pm_users', 'id', 'reg_date')); ?></td>
      </tr>
      <tr class="table_row2">
        <td width="30%"><strong>Joined this month: </strong></td>
        <td width="70%"><?php echo pm_number_format(recent_dates('is_thismonth', 'pm_users', 'id', 'reg_date')); ?></td>
      </tr>
      <tr class="table_row1">
        <td width="30%"><strong>Avatars uploaded:</strong></td>
        <td width="70%"><?php echo pm_number_format(count_avatars()); ?></td>
      </tr>
    </table>
	</fieldset>
	</td>
  </tr>
  <tr>
    <td valign="top">
	<fieldset>
	<legend><strong>Video Comments </strong></legend>
	<table width="100%" border="0" cellspacing="2" cellpadding="5">
      <tr class="table_row1">
        <td width="30%"><strong>Total comments:</strong></td>
        <td width="70%"><?php echo pm_number_format(count_entries('pm_comments', '', '')); ?> <a href="comments.php?filter=videos"><img src="images/go.gif" alt="Go" width="12" height="7" border="0" /></a></td>
      </tr>
      <tr class="table_row2">
        <td width="30%"><strong>Posted today:</strong></td>
        <td width="70%"><?php echo pm_number_format(recent_dates('is_today', 'pm_comments', 'id', 'added')); ?></td>
      </tr>
      <tr class="table_row1">
        <td width="30%"><strong>Posted yesterday:</strong></td>
        <td width="70%"><?php echo pm_number_format(recent_dates('is_yesterday', 'pm_comments', 'id', 'added')); ?></td>
      </tr>
      <tr class="table_row2">
        <td width="30%"><strong>#1 Comment Poster: </strong></td>
        <td width="70%"><?php echo top1_commentor(); ?></td>
      </tr>
    </table>
	</fieldset>
	</td>
    <td valign="top">
	<fieldset><legend><strong>Searches</strong></legend>
	<table width="100%" border="0" cellspacing="2" cellpadding="5">
      <tr class="table_row1">
        <td width="30%"><strong>Searches:</strong></td>
        <td width="70%"><?php echo pm_number_format(count_entries('pm_searches', '', '')); ?> <a href="show_searches.php"><img src="images/go.gif" alt="Go" width="12" height="7" border="0" /></a></td>
      </tr>
      <tr class="table_row2">
        <td width="30%"><strong>Most popular query:</strong></td>
        <td width="70%"><?php echo top1_search(); ?></td>
      </tr>
    </table>
	</fieldset>
	</td>
  </tr>
  <tr>
    <td valign="top">
	<fieldset>
		<legend><strong>Articles</strong></legend>
		<table width="100%" border="0" cellspacing="2" cellpadding="5">
		  <tr class="table_row1">
			<td width="30%"><strong>Total articles:</strong></td>
			<td width="70%"><?php echo $config['total_articles']; ?> <a href="articles.php"><img src="images/go.gif" alt="Go" width="12" height="7" border="0" /></a></td>
		  </tr>
		  <tr class="table_row2">
			<td width="30%"><strong>Most commented:</strong></td>
			<td width="70%">
				<?php
				$sql = "SELECT uniq_id, COUNT(*) as total 
						 FROM pm_comments 
						 WHERE uniq_id LIKE 'article-%' 
						 GROUP BY uniq_id 
						 ORDER BY total DESC 
						 LIMIT 1";
				$result = mysql_query($sql);
				$row = mysql_fetch_assoc($result);
				mysql_free_result($result);
		
				if ($row['uniq_id'])
				{
					$row['article_id'] = str_replace('article-', '', $row['uniq_id']);
					$article = get_article($row['article_id']);
					
					echo '<a href="comments.php?vid=article-'. $article['id']. '">'. $article['title'] .' ('. $row['total'] .')</a>';
				}
				else
				{
					echo 'N/A yet.';
				}
				?>
			</td>
		  </tr>
		</table>
	</fieldset> 
	</td>
    <td valign="top">

	</td>
  </tr>
</table>
<p>&nbsp;</p>
</div>
<?php
include('footer.php');
?>