r108297 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108296‎ | r108297 | r108298 >
Date:00:56, 7 January 2012
Author:bsitu
Status:resolved (Comments)
Tags:
Comment:
Adding top responders leaderboard to feedback dashboard page
Modified paths:
  • /trunk/extensions/MoodBar/MoodBar.i18n.php (modified) (history)
  • /trunk/extensions/MoodBar/SpecialFeedbackDashboard.php (modified) (history)
  • /trunk/extensions/MoodBar/include/MoodBarUtil.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MoodBar/include/MoodBarUtil.php
@@ -65,5 +65,76 @@
6666 }
6767
6868 }
 69+
 70+ /**
 71+ * Get the top responders for feedback in past week, default is top 5
 72+ * @param $num int - the number of top responders we want to get
 73+ * @return array
 74+ */
 75+ public static function getTopResponders( $num = 5 ) {
 76+
 77+ global $wgMemc;
6978
 79+ $timestamp = wfTimestamp( TS_UNIX ) - 7 * 24 * 60 * 60; // 1 week ago
 80+
 81+ // Try cache first
 82+ $key = wfMemcKey( 'moodbar_feedback_response', 'top_responders', 'past_week' );
 83+ $topResponders = $wgMemc->get( $key );
 84+
 85+ if ( $topResponders === false ) {
 86+ $dbr = wfGetDB( DB_SLAVE );
 87+ $res = $dbr->select( array( 'moodbar_feedback_response', 'user' ),
 88+ array( 'COUNT(*) AS number', 'user_id', 'user_name', 'user_real_name' ),
 89+ array( 'mbfr_user_id = user_id', 'mbfr_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
 90+ __METHOD__,
 91+ array( 'GROUP BY' => 'user_id', 'ORDER BY' => 'number DESC', 'LIMIT' => $num )
 92+ );
 93+
 94+ $topResponders = iterator_to_array( $res );
 95+
 96+ // Cache the results in cache for 12 hour
 97+ $wgMemc->set( $key, $topResponders, 12 * 60 * 60 );
 98+ }
 99+
 100+ return $topResponders;
 101+
 102+ }
 103+
 104+ /**
 105+ * Get the stats for the moodbar type in the last 24 hours
 106+ * @return array - count of number for each moodbar type
 107+ */
 108+ public function getMoodBarTypeStats() {
 109+
 110+ global $wgMemc;
 111+
 112+ $timestamp = wfTimestamp( TS_UNIX ) - 24 * 60 * 60; // 24 hours ago
 113+
 114+ // Try cache first
 115+ $key = wfMemcKey( 'moodbar_feedback', 'type_stats', 'last_day' );
 116+ $moodbarStat = $wgMemc->get( $key );
 117+
 118+ if ( $moodbarStat === false ) {
 119+ $dbr = wfGetDB( DB_SLAVE );
 120+ $res = $dbr->select( array( 'moodbar_feedback' ),
 121+ array( 'mbf_type', 'COUNT(*) AS number' ),
 122+ array( 'mbf_hidden_state' => 0, 'mbf_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
 123+ __METHOD__,
 124+ array( 'GROUP BY' => 'mbf_type' )
 125+ );
 126+
 127+ $moodbarStat = array( 'happy' => 0, 'sad' => 0, 'confused' => 0 );
 128+
 129+ foreach ( $res as $row ) {
 130+ $moodbarStat[$row->mbf_type] = $row->number;
 131+ }
 132+
 133+ // Cache the results in cache for 1 hour
 134+ $wgMemc->set( $key, $moodbarStat, 60 * 60 );
 135+ }
 136+
 137+ return $moodbarStat;
 138+
 139+ }
 140+
70141 }
\ No newline at end of file
Index: trunk/extensions/MoodBar/SpecialFeedbackDashboard.php
@@ -70,43 +70,6 @@
7171 }
7272
7373 /**
74 - * Get the stats for the moodbar type in the last 24 hours
75 - * @return array - count of number for each moodbar type
76 - */
77 - public function getMoodBarTypeStats( ) {
78 -
79 - global $wgMemc;
80 -
81 - $timestamp = wfTimestamp( TS_UNIX ) - 24 * 60 * 60; // 24 hours ago
82 -
83 - // Try cache first
84 - $key = wfMemcKey( 'moodbar_feedback', 'type_stats', 'last_day' );
85 - $moodbarStat = $wgMemc->get( $key );
86 -
87 - if ( $moodbarStat === false ) {
88 - $dbr = wfGetDB( DB_SLAVE );
89 - $res = $dbr->select( array( 'moodbar_feedback' ),
90 - array( 'mbf_type', 'COUNT(*) AS number' ),
91 - array( 'mbf_hidden_state' => 0, 'mbf_timestamp > ' . $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
92 - __METHOD__,
93 - array( 'GROUP BY' => 'mbf_type' )
94 - );
95 -
96 - $moodbarStat = array( 'happy' => 0, 'sad' => 0, 'confused' => 0 );
97 -
98 - foreach ( $res as $row ) {
99 - $moodbarStat[$row->mbf_type] = $row->number;
100 - }
101 -
102 - // Cache the results in cache for 1 hour
103 - $wgMemc->set( $key, $moodbarStat, 60 * 60 );
104 - }
105 -
106 - return $moodbarStat;
107 -
108 - }
109 -
110 - /**
11174 * Build the filter form. The state of each form element is preserved
11275 * using data in $wgRequest.
11376 * @param $filterType string Value to pass in the <form>'s data-filtertype attribute
@@ -137,7 +100,7 @@
138101 $filterType = htmlspecialchars( $filterType );
139102
140103
141 - $moodbarStat = $this->getMoodBarTypeStats();
 104+ $moodbarStat = MoodBarUtil::getMoodBarTypeStats();
142105 $moodbarStatMsg = wfMessage( 'moodbar-type-stats' )->params( $moodbarStat['happy'], $moodbarStat['sad'], $moodbarStat['confused'] )->escaped();
143106 $feedbackDashboardDescription = wfMessage( 'moodbar-feedback-description' )->params( $wgSitename ); // don't escape because there is html
144107
@@ -159,7 +122,9 @@
160123
161124 $showUnansweredFilter = '<label for="fbd-filters-show-unanswered" id="fbd-filters-type-show-unanswered-label" class="fbd-filters-label">' .
162125 $showUnansweredCheckbox . $showUnansweredMsg . '</label>';
163 -
 126+
 127+ $leaderBoardElement = self::buildLeaderBoardElement();
 128+
164129 return <<<HTML
165130 <div id="fbd-description">
166131 <div id="fbd-description-text">
@@ -196,6 +161,7 @@
197162 <button type="submit" id="fbd-filters-set">$setFiltersMsg</button>
198163 </form>
199164 <a href="$whatIsURL" id="fbd-about">$whatIsMsg</a>
 165+ $leaderBoardElement
200166 </div>
201167 HTML;
202168 }
@@ -296,6 +262,44 @@
297263 HTML;
298264 }
299265
 266+ /**
 267+ * Build the HTML for leaderboard
 268+ * @return html string
 269+ */
 270+ protected static function buildLeaderBoardElement() {
 271+
 272+ $topResponders = MoodBarUtil::getTopResponders();
 273+
 274+ $html = '';
 275+
 276+ if ( $topResponders ) {
 277+ foreach ( $topResponders as $row ) {
 278+ $user = User::newFromRow( $row );
 279+ if ( $user && !$user->isAnon() ) {
 280+ $html .= '<li>' . Linker::userLink( $user->getId(), htmlspecialchars( $user->getName() ) ) .
 281+ '<span>' . $row->number . '</span></li>';
 282+ }
 283+ }
 284+ }
 285+
 286+ if ( $html ) {
 287+ $topRespondersTitle = wfMessage( 'moodbar-feedback-top-responders-title' )->escaped();
 288+
 289+ return <<<HTML
 290+ <div class="fbd-leaderboard-top-responders">
 291+ $topRespondersTitle
 292+ <hr />
 293+ <ul class="fbd-leaderboard">
 294+ $html
 295+ </ul>
 296+ </div>
 297+HTML;
 298+ }
 299+
 300+ return $html;
 301+
 302+ }
 303+
300304 protected static function buildResponseElement( $feedbackItem, $response ) {
301305 global $wgLang, $wgUser;
302306
Index: trunk/extensions/MoodBar/MoodBar.i18n.php
@@ -141,6 +141,7 @@
142142 'moodbar-feedback-action-error' => 'An error occurred when trying to perform this action.',
143143 'moodbar-feedback-response-summary' => '[[$1|$2]] [[$3|responded]] $4 ago',
144144 'moodbar-feedback-edit-summary' => 'Response to [[Special:FeedbackDashboard/$1|user feedback]]: $2',
 145+ 'moodbar-feedback-top-responders-title' => 'Top Responders',
145146 // Mood types
146147 'moodbar-type-happy' => '{{GENDER:$1|Happy}}',
147148 'moodbar-type-sad' => '{{GENDER:$1|Sad}}',
@@ -310,6 +311,7 @@
311312 'moodbar-action-reason-required' => 'Text explaining admin action reason is required',
312313 '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',
313314 'moodbar-feedback-edit-summary' => 'Auto generated Edit summary for feedback response, $1 is the feedback id and $2 is the response text',
 315+ 'moodbar-feedback-top-responders-title' => 'The title for Top Responders, which is located below feedback dashboard filter',
314316 'moodbar-type-happy' => '$1 is the username that can be used for GENDER. Message is used on Special:FeedbackDashboard.',
315317 'moodbar-type-sad' => '$1 is the username that can be used for GENDER. Message is used on Special:FeedbackDashboard.',
316318 'moodbar-type-confused' => '$1 is the username that can be used for GENDER. Message is used on Special:FeedbackDashboard.',

Follow-up revisions

RevisionCommit summaryAuthorDate
r108298followup to -r108297 - adding the keyword static to function prototypebsitu01:01, 7 January 2012
r108531follow up to -r108297 - cast numbers when outputting to htmlbsitu18:28, 10 January 2012

Comments

#Comment by Catrope (talk | contribs)   17:04, 10 January 2012
+							'<span>' . $row->number . '</span></li>';

To reduce reviewer anxiety, please cast $row->number to an integer, or escape it. I know it's an integer by tracking it back to the query, but you should aim to write code in a way that doesn't require a reviewer to track a variable through the file to determine if your code is secure.

OK otherwise.

#Comment by Bsitu (talk | contribs)   18:25, 10 January 2012

Will do, thank you!

Status & tagging log