https://www.mediawiki.org/wiki/Special:Code/MediaWiki/108297

Revision: 108297
Author:   bsitu
Date:     2012-01-07 00:56:39 +0000 (Sat, 07 Jan 2012)
Log Message:
-----------
Adding top responders leaderboard to feedback dashboard page

Modified Paths:
--------------
    trunk/extensions/MoodBar/MoodBar.i18n.php
    trunk/extensions/MoodBar/SpecialFeedbackDashboard.php
    trunk/extensions/MoodBar/include/MoodBarUtil.php

Modified: trunk/extensions/MoodBar/MoodBar.i18n.php
===================================================================
--- trunk/extensions/MoodBar/MoodBar.i18n.php   2012-01-07 00:48:27 UTC (rev 
108296)
+++ trunk/extensions/MoodBar/MoodBar.i18n.php   2012-01-07 00:56:39 UTC (rev 
108297)
@@ -141,6 +141,7 @@
        'moodbar-feedback-action-error' => 'An error occurred when trying to 
perform this action.',
        'moodbar-feedback-response-summary' => '[[$1|$2]] [[$3|responded]] $4 
ago',
        'moodbar-feedback-edit-summary' => 'Response to 
[[Special:FeedbackDashboard/$1|user feedback]]: $2',
+       'moodbar-feedback-top-responders-title' => 'Top Responders',
        // Mood types
        'moodbar-type-happy' => '{{GENDER:$1|Happy}}',
        'moodbar-type-sad' => '{{GENDER:$1|Sad}}',
@@ -310,6 +311,7 @@
        'moodbar-action-reason-required' => 'Text explaining admin action 
reason is required',
        'moodbar-feedback-response-summary' => 'Text providing a summary of a 
user response, $1 is user page, $2 is user name, $3 is user talk page, $4 is 
time',
        'moodbar-feedback-edit-summary' => 'Auto generated Edit summary for 
feedback response, $1 is the feedback id and $2 is the response text',
+       'moodbar-feedback-top-responders-title' => 'The title for Top 
Responders, which is located below feedback dashboard filter',
        'moodbar-type-happy' => '$1 is the username that can be used for 
GENDER. Message is used on Special:FeedbackDashboard.',
        'moodbar-type-sad' => '$1 is the username that can be used for GENDER. 
Message is used on Special:FeedbackDashboard.',
        'moodbar-type-confused' => '$1 is the username that can be used for 
GENDER. Message is used on Special:FeedbackDashboard.',

Modified: trunk/extensions/MoodBar/SpecialFeedbackDashboard.php
===================================================================
--- trunk/extensions/MoodBar/SpecialFeedbackDashboard.php       2012-01-07 
00:48:27 UTC (rev 108296)
+++ trunk/extensions/MoodBar/SpecialFeedbackDashboard.php       2012-01-07 
00:56:39 UTC (rev 108297)
@@ -70,43 +70,6 @@
        }
        
        /**
-        * Get the stats for the moodbar type in the last 24 hours
-        * @return array - count of number for each moodbar type
-        */
-       public function getMoodBarTypeStats( ) {
-       
-               global $wgMemc;
-
-               $timestamp = wfTimestamp( TS_UNIX ) - 24 * 60 * 60; // 24 hours 
ago
-               
-               // Try cache first
-               $key = wfMemcKey( 'moodbar_feedback', 'type_stats', 'last_day' 
);
-               $moodbarStat = $wgMemc->get( $key );
-
-               if ( $moodbarStat === false ) {
-                       $dbr = wfGetDB( DB_SLAVE );
-                       $res = $dbr->select( array( 'moodbar_feedback' ),
-                                               array( 'mbf_type', 'COUNT(*) AS 
number' ),
-                                               array( 'mbf_hidden_state' => 0, 
'mbf_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
-                                               __METHOD__,
-                                               array( 'GROUP BY' => 'mbf_type' 
)
-                       );
-
-                       $moodbarStat = array( 'happy' => 0, 'sad' => 0, 
'confused' => 0 );
-
-                       foreach ( $res as $row ) {
-                               $moodbarStat[$row->mbf_type] = $row->number;
-                       }
-
-                       // Cache the results in cache for 1 hour
-                       $wgMemc->set( $key, $moodbarStat, 60 * 60 );
-               }
-
-               return $moodbarStat;
-               
-       }
-       
-       /**
         * Build the filter form. The state of each form element is preserved
         * using data in $wgRequest.
         * @param $filterType string Value to pass in the <form>'s 
data-filtertype attribute
@@ -137,7 +100,7 @@
                $filterType = htmlspecialchars( $filterType );
                
                
-               $moodbarStat = $this->getMoodBarTypeStats();
+               $moodbarStat = MoodBarUtil::getMoodBarTypeStats();
                $moodbarStatMsg = wfMessage( 'moodbar-type-stats' )->params( 
$moodbarStat['happy'], $moodbarStat['sad'], $moodbarStat['confused'] 
)->escaped();
                $feedbackDashboardDescription = wfMessage( 
'moodbar-feedback-description' )->params( $wgSitename ); // don't escape 
because there is html 
 
@@ -159,7 +122,9 @@
                        
                $showUnansweredFilter = '<label 
for="fbd-filters-show-unanswered" id="fbd-filters-type-show-unanswered-label" 
class="fbd-filters-label">' . 
                                                                
$showUnansweredCheckbox . $showUnansweredMsg . '</label>';
-               
+
+               $leaderBoardElement = self::buildLeaderBoardElement();
+
                return <<<HTML
                <div id="fbd-description">
                        <div id="fbd-description-text">
@@ -196,6 +161,7 @@
                                <button type="submit" 
id="fbd-filters-set">$setFiltersMsg</button>
                        </form>
                        <a href="$whatIsURL" id="fbd-about">$whatIsMsg</a>
+                       $leaderBoardElement
                </div>
 HTML;
        }
@@ -296,6 +262,44 @@
 HTML;
        }
        
+       /**
+        * Build the HTML for leaderboard
+        * @return html string
+        */
+       protected static function buildLeaderBoardElement() {
+
+               $topResponders = MoodBarUtil::getTopResponders();
+
+               $html = '';
+               
+               if ( $topResponders ) {
+                       foreach ( $topResponders as $row ) {
+                               $user = User::newFromRow( $row );
+                               if ( $user && !$user->isAnon() ) {
+                                       $html .= '<li>' . Linker::userLink( 
$user->getId(), htmlspecialchars( $user->getName() ) ) .
+                                                       '<span>' . $row->number 
. '</span></li>';
+                               }
+                       }
+               }
+
+               if ( $html ) {
+                       $topRespondersTitle = wfMessage( 
'moodbar-feedback-top-responders-title' )->escaped();
+                       
+                       return <<<HTML
+                       <div class="fbd-leaderboard-top-responders">
+                               $topRespondersTitle
+                               <hr />
+                               <ul class="fbd-leaderboard">
+                                       $html
+                               </ul>
+                       </div>
+HTML;
+               }
+               
+               return $html;
+
+       }
+
        protected static function buildResponseElement( $feedbackItem, 
$response ) {
                global $wgLang, $wgUser;
                

Modified: trunk/extensions/MoodBar/include/MoodBarUtil.php
===================================================================
--- trunk/extensions/MoodBar/include/MoodBarUtil.php    2012-01-07 00:48:27 UTC 
(rev 108296)
+++ trunk/extensions/MoodBar/include/MoodBarUtil.php    2012-01-07 00:56:39 UTC 
(rev 108297)
@@ -65,5 +65,76 @@
                }
        
        }
+       
+       /**
+        * Get the top responders for feedback in past week, default is top 5
+        * @param $num int - the number of top responders we want to get
+        * @return array
+        */
+       public static function getTopResponders( $num = 5 ) {
+       
+               global $wgMemc;
 
+               $timestamp = wfTimestamp( TS_UNIX ) - 7 * 24 * 60 * 60; // 1 
week ago
+
+               // Try cache first
+               $key = wfMemcKey( 'moodbar_feedback_response', 
'top_responders', 'past_week' );
+               $topResponders = $wgMemc->get( $key );
+
+               if ( $topResponders === false ) {
+                       $dbr = wfGetDB( DB_SLAVE );
+                       $res = $dbr->select( array( 
'moodbar_feedback_response', 'user' ),
+                                                       array( 'COUNT(*) AS 
number', 'user_id', 'user_name', 'user_real_name' ),
+                                                       array( 'mbfr_user_id = 
user_id', 'mbfr_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( $timestamp ) 
) ),
+                                                       __METHOD__,
+                                                       array( 'GROUP BY' => 
'user_id', 'ORDER BY' => 'number DESC', 'LIMIT' => $num )
+                       );
+                       
+                       $topResponders = iterator_to_array( $res );
+
+                       // Cache the results in cache for 12 hour
+                       $wgMemc->set( $key, $topResponders, 12 * 60 * 60 );
+               }
+
+               return $topResponders;
+
+       }
+
+       /**
+        * Get the stats for the moodbar type in the last 24 hours
+        * @return array - count of number for each moodbar type
+        */
+       public function getMoodBarTypeStats() {
+
+               global $wgMemc;
+
+               $timestamp = wfTimestamp( TS_UNIX ) - 24 * 60 * 60; // 24 hours 
ago
+               
+               // Try cache first
+               $key = wfMemcKey( 'moodbar_feedback', 'type_stats', 'last_day' 
);
+               $moodbarStat = $wgMemc->get( $key );
+
+               if ( $moodbarStat === false ) {
+                       $dbr = wfGetDB( DB_SLAVE );
+                       $res = $dbr->select( array( 'moodbar_feedback' ),
+                                               array( 'mbf_type', 'COUNT(*) AS 
number' ),
+                                               array( 'mbf_hidden_state' => 0, 
'mbf_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
+                                               __METHOD__,
+                                               array( 'GROUP BY' => 'mbf_type' 
)
+                       );
+
+                       $moodbarStat = array( 'happy' => 0, 'sad' => 0, 
'confused' => 0 );
+
+                       foreach ( $res as $row ) {
+                               $moodbarStat[$row->mbf_type] = $row->number;
+                       }
+
+                       // Cache the results in cache for 1 hour
+                       $wgMemc->set( $key, $moodbarStat, 60 * 60 );
+               }
+
+               return $moodbarStat;
+
+       }
+
 }
\ No newline at end of file


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to