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

Revision: 113747
Author:   jeroendedauw
Date:     2012-03-13 18:43:43 +0000 (Tue, 13 Mar 2012)
Log Message:
-----------
fix code to use single queries and removed a completely redundant one

Modified Paths:
--------------
    trunk/extensions/EducationProgram/actions/ViewCourseAction.php
    trunk/extensions/EducationProgram/includes/EPCourse.php

Modified: trunk/extensions/EducationProgram/actions/ViewCourseAction.php
===================================================================
--- trunk/extensions/EducationProgram/actions/ViewCourseAction.php      
2012-03-13 18:41:56 UTC (rev 113746)
+++ trunk/extensions/EducationProgram/actions/ViewCourseAction.php      
2012-03-13 18:43:43 UTC (rev 113747)
@@ -47,12 +47,7 @@
 
                $out->addHTML( $this->getOutput()->parse( $course->getField( 
'description' ) ) );
 
-               $studentIds = array_map(
-                       function( EPStudent $student ) {
-                               return $student->getId();
-                       },
-                       $course->getStudents( 'id' )
-               );
+               $studentIds = $course->getField( 'students' );
 
                if ( !empty( $studentIds ) ) {
                        $out->addElement( 'h2', array(), wfMsg( 
'ep-course-students' ) );

Modified: trunk/extensions/EducationProgram/includes/EPCourse.php
===================================================================
--- trunk/extensions/EducationProgram/includes/EPCourse.php     2012-03-13 
18:41:56 UTC (rev 113746)
+++ trunk/extensions/EducationProgram/includes/EPCourse.php     2012-03-13 
18:43:43 UTC (rev 113747)
@@ -430,15 +430,7 @@
         * @return array of EPStudent
         */
        public function getStudents() {
-               if ( $this->students === false ) {
-                       $this->students = array();
-
-                       foreach ( $this->getField( 'students' ) as $userId ) {
-                               $this->students[] = EPStudent::newFromUserId( 
$userId, true );
-                       }
-               }
-
-               return $this->students;
+               return $this->getRoleList( 'students', 'EPStudents', 'students' 
);
        }
 
        /**
@@ -449,15 +441,36 @@
         * @return array of EPInstructor
         */
        public function getInstructors() {
-               if ( $this->instructors === false ) {
-                       $this->instructors = array();
+               return $this->getRoleList( 'instructors', 'EPInstructors', 
'instructors' );
+       }
 
-                       foreach ( $this->getField( 'instructors' ) as $userId ) 
{
-                               $this->instructors[] = 
EPInstructor::newFromUserId( $userId );
+       /**
+        * Returns the users that have the specified role.
+        *
+        * @since 0.1
+        *
+        * @param string $fieldName Name of the role field.
+        * @param string $tableName Name of the table class in which this role 
is kept track of.
+        * @param string $classField Name of the field in which the list is 
cached in this class.
+        *
+        * @return array of EPRole
+        */
+       protected function getRoleList( $fieldName, $tableName, $classField ) {
+               if ( $this->$classField === false ) {
+                       $userIds = $this->getField( $fieldName );
+
+                       if ( empty( $userIds ) ) {
+                               $this->$classField = array();
                        }
+                       else {
+                               $this->$classField = 
$tableName::singleton()->select(
+                                       null,
+                                       array( 'user_id' => $userIds )
+                               );
+                       }
                }
 
-               return $this->instructors;
+               return $this->$classField;
        }
        
        /**
@@ -468,15 +481,7 @@
         * @return array of EPCA
         */
        public function getCampusAmbassadors() {
-               if ( $this->cas === false ) {
-                       $this->cas = array();
-
-                       foreach ( $this->getField( 'campus_ambs' ) as $userId ) 
{
-                               $this->cas[] = EPCA::newFromUserId( $userId );
-                       }
-               }
-
-               return $this->cas;
+               return $this->getRoleList( 'campus_ambs', 'EPCAs', 'cas' );
        }
        
        /**
@@ -487,15 +492,7 @@
         * @return array of EPOA
         */
        public function getOnlineAmbassadors() {
-               if ( $this->oas === false ) {
-                       $this->oas = array();
-
-                       foreach ( $this->getField( 'online_ambs' ) as $userId ) 
{
-                               $this->oas[] = EPOA::newFromUserId( $userId );
-                       }
-               }
-
-               return $this->oas;
+               return $this->getRoleList( 'online_ambs', 'EPOAs', 'oas' );
        }
        
        /**


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

Reply via email to