/**
  * wp_ajax_wpsd_load_authors function.
  * 
  * @access public
  * @return void
  */
 function wp_ajax_wpsd_load_authors()
 {
     global $wpdb;
     $form = new WPSDAdminConfigForm();
     if (!$form->getWpsdWidgetAuthors()) {
         _e('Widget disabled check', 'wpsd');
         echo ' <a href="' . wpsd_get_settings_url() . '" title="wp-stats-dashboard settings" target="_self">' . __('settings', 'wpsd') . '</a>';
         die;
     }
     $results = $wpdb->get_results("\r\n\t\t\tSELECT COUNT(*) as c, post_author as user_id\r\n\t\t\tFROM {$wpdb->posts} WHERE post_status = 'publish'\r\n\t\t\tGROUP BY post_author\r\n\t\t\tORDER BY c DESC\r\n\t\t\tLIMIT 5");
     if (null != $results && is_array($results)) {
         print '<style>table.wpsd_top5_authors { width:100%; } </style>';
         echo '<table class="wpsd_top5_authors">';
         printf('<thead><tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr></thead>', __('Rank', 'wpsd'), __('WPSD Score', 'wpsd'), __('Author', 'wpsd'), __('Post Count', 'wpsd'), __('Comments Received', 'wpsd'), __('Comments Placed', 'wpsd'), __('Klout Score', 'wpsd'), __('Twitter Ratio', 'wpsd'));
         echo '<tbody>';
         $list = array();
         foreach ($results as $r) {
             if (null == $r->user_id) {
                 continue;
             }
             $metrics = new WPSDUserMetrics($r->user_id);
             $avatar = get_avatar($r->user_id, 32);
             $user_data = get_userdata($r->user_id);
             $author = sprintf('<a href="%s">%s <br/> <span>%s</span></a>', 'user-edit.php?user_id=' . $r->user_id, $avatar, $user_data->user_login);
             $wpsd_score = $metrics->getWpsdScore();
             $list[$wpsd_score] = array('metrics' => $metrics, 'avatar' => $avatar, 'user_data' => $user_data, 'author' => $author);
         }
         ksort($list, SORT_NUMERIC);
         $list = array_reverse($list);
         $i = 0;
         foreach ($list as $item) {
             $metrics = $item['metrics'];
             $avatar = $item['avatar'];
             $user_data = $item['user_data'];
             $author = $item['author'];
             if ($i % 2 == 0) {
                 echo '<tr>';
             } else {
                 echo '<tr class="highlight">';
             }
             $this->render_admin('admin_user_metrics', array('metrics' => $metrics, 'author' => $author, 'rank' => $i + 1));
             echo '</tr>';
             $i++;
         }
         echo '</tbody>';
         echo '</table>';
     } else {
         _e('No authors found with comments placed', 'wpsd');
     }
     die;
 }
    function widget($args, $instance)
    {
        extract($args);
        global $wpdb;
        $title = apply_filters('widget_title', $instance['title']);
        $desc = $instance['desc'];
        $results = $wpdb->get_results("\n\t\t\tSELECT COUNT(*) as c, post_author as user_id\n\t\t\tFROM {$wpdb->posts} WHERE post_status = 'publish'\n\t\t\tGROUP BY post_author\n\t\t\tORDER BY c DESC\n\t\t\tLIMIT 5");
        ?>
        <?php 
        echo $before_widget;
        ?>
       	<!-- wp-stats-dashboard widget by http://daveligthart.com -->
        <div id="wpsd-author-widget">
        <?php 
        if ($title) {
            echo $before_title . $title . $after_title;
        }
        ?>
          	<?php 
        if ('' != trim($desc)) {
            ?>
             
          	<p><?php 
            echo $desc;
            ?>
</p>
          	<?php 
        }
        ?>
          	
          	<?php 
        if (null != $results && is_array($results) && count($results) > 0) {
            echo '<ul>';
            $list = array();
            foreach ($results as $r) {
                if (null == $r->user_id) {
                    continue;
                }
                $metrics = new WPSDUserMetrics($r->user_id);
                $avatar = get_avatar($r->user_id, 32);
                $user_data = get_userdata($r->user_id);
                $author = sprintf('%s %s', $avatar, $user_data->user_login);
                $wpsd_score = $metrics->getWpsdScore();
                $list[$wpsd_score] = array('metrics' => $metrics, 'avatar' => $avatar, 'user_data' => $user_data, 'author' => $author);
            }
            ksort($list, SORT_NUMERIC);
            $list = array_reverse($list);
            $i = 0;
            foreach ($list as $item) {
                $metrics = $item['metrics'];
                $avatar = $item['avatar'];
                $user_data = $item['user_data'];
                $author = $item['author'];
                $rank = $i + 1;
                ?>
					
					<li> 
						<ul style="list-style:none;margin:0;padding:0;">	
							<li><span class="wpsd-author-avatar"><?php 
                echo $avatar;
                ?>
</span></li>					
							<li>
							<span class="wpsd-rank">
								<strong>
								<span><?php 
                _e('Rank', 'wpsd');
                ?>
:</span>
								<span class="value"><?php 
                echo $rank;
                ?>
</span>
								</strong>
							</span> 
							</li>
							<li>						
							<span class="wpsd-score">
								<strong>
								<span><?php 
                _e('WPSD Score', 'wpsd');
                ?>
:</span>
								<span class="value">
									<?php 
                echo round($metrics->getWpsdScore(), 2);
                ?>
								</span>
								</strong>
							</span>
							</li>
							<li>
							<span class="wpsd-post-count">
								<span><?php 
                _e('Posts', 'wpsd');
                ?>
:</span>
								<span class="value"><?php 
                echo $metrics->getPostCount();
                ?>
</span>
							</span>
							</li>
							<li>
							<span class="wpsd-comments-received-count">
								<span><?php 
                _e('Comments In', 'wpsd');
                ?>
:</span>
								<span class="value"><?php 
                echo $metrics->getCommentsReceived();
                ?>
</span>
							</span>	
							</li>
							<li>
							<span class="wpsd-comments-received-count">
								<span><?php 
                _e('Comments Out', 'wpsd');
                ?>
:</span>
								<span class="value"><?php 
                echo $metrics->getCommentsPlaced();
                ?>
</span>
							</span>	
							</li>
							<li>
							<span class="wpsd-klout-score">
								<span><?php 
                _e('Klout Score', 'wpsd');
                ?>
:</span>
								<span class="value"><?php 
                echo $metrics->getKloutScore();
                ?>
</span>
							</span>
							</li>
							<li>
							<span class="wpsd-post-count">
								<span><?php 
                _e('Twitter Ratio', 'wpsd');
                ?>
:</span>
								<span class="value"><?php 
                echo $metrics->getTwitterRatio();
                ?>
</span>
							</span>
							</li>
						</ul>
					</li>
					
					<?php 
                $i++;
            }
            echo '</ul>';
        } else {
            _e('No authors found with comments placed...', 'wpsd');
        }
        ?>
					
		</div>
		<!-- wp-stats-dashboard widget by http://daveligthart.com -->	
        <?php 
        echo $after_widget;
        ?>
        
<?php 
    }
/**
 * wpsd_get_author_wpsd_score function.
 * 
 * @access public
 * @param mixed $user_id
 * @return integer
 */
function wpsd_get_author_wpsd_score($user_id)
{
    $o = new WPSDUserMetrics($user_id);
    return round($o->getWpsdScore(), 2);
}