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

Revision: 114473
Author:   jeroendedauw
Date:     2012-03-24 07:21:45 +0000 (Sat, 24 Mar 2012)
Log Message:
-----------
added gender breakdown to stats on special:educationprogram

Modified Paths:
--------------
    trunk/extensions/EducationProgram/EducationProgram.i18n.php
    trunk/extensions/EducationProgram/specials/SpecialEducationProgram.php

Modified: trunk/extensions/EducationProgram/EducationProgram.i18n.php
===================================================================
--- trunk/extensions/EducationProgram/EducationProgram.i18n.php 2012-03-24 
05:38:39 UTC (rev 114472)
+++ trunk/extensions/EducationProgram/EducationProgram.i18n.php 2012-03-24 
07:21:45 UTC (rev 114473)
@@ -231,6 +231,14 @@
        'ep-educationprogram-cas' => 'Number of Campus Ambassadors',
        'ep-educationprogram-orgs' => 'Number of institutions',
        'ep-educationprogram-articles' => 'Number of articles',
+       'ep-educationprogram-gender-students' => 'Students',
+       'ep-educationprogram-gender-oas' => 'Online Ambassadors',
+       'ep-educationprogram-gender-cas' => 'Campus Ambassadors',
+       'ep-educationprogram-gender-instructors' => 'Instructors',
+       'ep-educationprogram-male' => 'Male',
+       'ep-educationprogram-female' => 'Female',
+       'ep-educationprogram-unknown' => 'Unknown',
+       'ep-educationprogram-genders' => 'Gender breakdown',
 
        // Special:Institutions
        'ep-institutions-noresults' => 'There are no institutions to list.',

Modified: trunk/extensions/EducationProgram/specials/SpecialEducationProgram.php
===================================================================
--- trunk/extensions/EducationProgram/specials/SpecialEducationProgram.php      
2012-03-24 05:38:39 UTC (rev 114472)
+++ trunk/extensions/EducationProgram/specials/SpecialEducationProgram.php      
2012-03-24 07:21:45 UTC (rev 114473)
@@ -127,16 +127,76 @@
        }
 
        public function displayByTerm() {
-               $terms = $this->getTermData();
+               $termsData = $this->getTermData();
 
                $html = Html::element( 'h2', array(), $this->msgTxt( 'by-term' 
) );
 
-               $html .= Html::openElement( 'table', array( 'class' => 
'wikitable ep-termbreakdown' ) );
+               $html .= $this->getByTermTable( $termsData['terms'] );
 
+               $html .= Html::element( 'h2', array(), $this->msgTxt( 'genders' 
) );
+
+               $html .= $this->getByGenderTable( $termsData['bygender'] );
+
+               return $html;
+       }
+
+       protected function getByGenderTable( $terms ) {
+               $html = Html::openElement( 'table', array( 'class' => 
'wikitable ep-termbreakdown' ) );
+
                $term = array_shift( $terms );
                $rows = array_keys( $term );
                array_unshift( $terms, $term );
 
+               $html .= '<tr>';
+
+               $html .= Html::element( 'th', array( 'colspan' => 2 ), '' );
+
+               foreach ( $terms as $termName => $term ) {
+                       $html .= Html::element(
+                               'th',
+                               array(),
+                               $termName
+                       );
+               }
+
+               $html .= '</tr>';
+
+               foreach ( $rows as $row ) {
+                       $html .= '<tr>';
+
+                       $html .= Html::element( 'th', array( 'rowspan' => 3 ), 
$this->msgTxt( 'gender-' . $row ) );
+
+                       foreach ( array( 'male', 'female', 'unknown' ) as 
$gender ) {
+                               if ( $gender !== 'male' ) {
+                                       $html .= '</tr><tr>';
+                               }
+
+                               $html .= Html::element( 'th', array(), 
$this->msgTxt( $gender ) );
+
+                               foreach ( $terms as $term ) {
+                                       $html .= Html::element(
+                                               'td',
+                                               array(),
+                                               
$this->getLanguage()->formatNum( round( $term[$row][$gender], 2 ) * 100 ) . '%'
+                                       );
+                               }
+                       }
+
+                       $html .= '</tr>';
+               }
+
+               $html .= Html::closeElement( 'table' );
+
+               return $html;
+       }
+
+       protected function getByTermTable( $terms ) {
+               $html = Html::openElement( 'table', array( 'class' => 
'wikitable ep-termbreakdown' ) );
+
+               $term = array_shift( $terms );
+               $rows = array_keys( $term );
+               array_unshift( $terms, $term );
+
                array_unshift( $rows, 'header' );
 
                foreach ( $rows as $row ) {
@@ -165,6 +225,7 @@
        protected function getTermData() {
                $termNames = EPCourses::singleton()->selectFields( 'term', 
array(), array( 'DISTINCT' ) );
                $terms = array();
+               $byGender = array();
 
                foreach ( $termNames as $termName ) {
                        $courses = EPCourses::singleton()->select( null, array( 
'term' => $termName ) );
@@ -205,11 +266,58 @@
                        );
 
                        $terms[$termName] = $term;
+                       $byGender[$termName] = $this->getByGender( $students, 
$oas, $cas, $instructors );
                }
 
-               return $terms;
+               return array( 'terms' => $terms, 'bygender' => $byGender );
        }
 
+       protected function getByGender( array $students, array $oas, array 
$cas, array $instructors ) {
+               $genders = $this->getGenders( array_unique( array_merge( 
$students, $oas, $cas, $instructors ) ) );
+
+               return array(
+                       'students' => $this->getGenderDistribution( $students, 
$genders ),
+                       'oas' => $this->getGenderDistribution( $oas, $genders ),
+                       'cas' => $this->getGenderDistribution( $cas, $genders ),
+                       'instructors' => $this->getGenderDistribution( 
$instructors, $genders ),
+               );
+       }
+
+       protected function getGenderDistribution( array $users, array $genders 
) {
+               $distibution = array( 'unknown' => 0, 'male' => 0, 'female' => 
0 );
+
+               foreach ( $users as $userId ) {
+                       $distibution[$genders[$userId]]++;
+               }
+
+               $userCount = count( $users );
+
+               foreach ( $distibution as &$amount ) {
+                       $amount = $userCount === 0 ? 1 : $amount / $userCount;
+               }
+
+               return $distibution;
+       }
+
+       protected function getGenders( $userIds ) {
+               $dbr = wfGetDB( DB_SLAVE );
+
+               $users = $dbr->select(
+                       'user_properties',
+                       array( 'up_user', 'up_value' ),
+                       array( 'up_property' => 'gender' ),
+                       __METHOD__
+               );
+
+               $genders = array_fill_keys( $userIds, 'unknown' );
+
+               while ( $user = $users->fetchObject() ) {
+                       $genders[$user->up_user] = $user->up_value;
+               }
+
+               return $genders;
+       }
+
        protected function msgTxt() {
                $args = func_get_args();
                array_unshift( $args, $this->prefixKey( array_shift( $args ) ) 
);


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to