<?php
/*
Plugin Name: ViewCount
Plugin URI: http://www.boakes.org/index.php?p=94
Description: This plugin uses the StatTraq table to report on how many times a page has been viewed
Author: Rich Boakes
Author URI: http://boakes.org/
Version: 0.0.3
*/

/*
// release history
//
// version 0.0.1
//     original release
//
// version 0.0.2
//     modified to accept id rather than url - simplifying the api and the query.
//     note the id is both the article_id field in the wp_stattraq table and the
//     ID field in the wp_posts table - if this ever changes, then then the SQL
//     should be tweaked.
//
// version 0.0.3
//     added second function to check for unique users rather than mere page views
//     for efficiency this requires the sql statement to be run:
//     alter table wp_stattraq add index rjb (session_id);
//
//
// usage
//
// if you're interested in finding the number of views for a particular post, say:
//   http://example.org/index.php?p=27
// then the id would be
//   27
// so in the loop of your index.php file you might add the line:
//   <?php rjb_get_view_count( $post->ID, '', ' views<br />' ); ?>
// if you want to know how many unique people (viewers) there have been, use:
//   <?php rjb_get_viewer_count( $post->ID, '', ' views<br />' ); ?>
*/

function rjb_get_view_count($article_id$prefix=''$suffix=' views'$level=){
    global 
$wpdb$tablestattraq$user_level;

    
get_currentuserinfo();

   if (
$user_level >= $level) {
        
$output $wpdb->get_row"SELECT COUNT( * ) AS cnt FROM $tablestattraq WHERE article_id='$article_id'" );
        echo 
$prefix . ($output->cnt) . $suffix;
   }
   return;
}

function 
rjb_get_viewer_count($article_id$prefix=''$suffix=' views'$level=){
    global 
$wpdb$tablestattraq$user_level;

    
get_currentuserinfo();

   if (
$user_level >= $level) {
        
$output $wpdb->get_row"SELECT COUNT( DISTINCT (session_id) ) AS cnt FROM $tablestattraq WHERE article_id='$article_id'" );
        echo 
$prefix . ($output->cnt) . $suffix;
   }
   return;
}
  
function 
rjb_get_mod_count(){
    global 
$wpdb$tablestattraq$user_level;
    
get_currentuserinfo();
    return 
$wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '0'");
}
?>