r113747 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113746‎ | r113747 | r113748 >
Date:18:43, 13 March 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
fix code to use single queries and removed a completely redundant one
Modified paths:
  • /trunk/extensions/EducationProgram/actions/ViewCourseAction.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPCourse.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/actions/ViewCourseAction.php
@@ -47,12 +47,7 @@
4848
4949 $out->addHTML( $this->getOutput()->parse( $course->getField( 'description' ) ) );
5050
51 - $studentIds = array_map(
52 - function( EPStudent $student ) {
53 - return $student->getId();
54 - },
55 - $course->getStudents( 'id' )
56 - );
 51+ $studentIds = $course->getField( 'students' );
5752
5853 if ( !empty( $studentIds ) ) {
5954 $out->addElement( 'h2', array(), wfMsg( 'ep-course-students' ) );
Index: trunk/extensions/EducationProgram/includes/EPCourse.php
@@ -430,15 +430,7 @@
431431 * @return array of EPStudent
432432 */
433433 public function getStudents() {
434 - if ( $this->students === false ) {
435 - $this->students = array();
436 -
437 - foreach ( $this->getField( 'students' ) as $userId ) {
438 - $this->students[] = EPStudent::newFromUserId( $userId, true );
439 - }
440 - }
441 -
442 - return $this->students;
 434+ return $this->getRoleList( 'students', 'EPStudents', 'students' );
443435 }
444436
445437 /**
@@ -449,15 +441,36 @@
450442 * @return array of EPInstructor
451443 */
452444 public function getInstructors() {
453 - if ( $this->instructors === false ) {
454 - $this->instructors = array();
 445+ return $this->getRoleList( 'instructors', 'EPInstructors', 'instructors' );
 446+ }
455447
456 - foreach ( $this->getField( 'instructors' ) as $userId ) {
457 - $this->instructors[] = EPInstructor::newFromUserId( $userId );
 448+ /**
 449+ * Returns the users that have the specified role.
 450+ *
 451+ * @since 0.1
 452+ *
 453+ * @param string $fieldName Name of the role field.
 454+ * @param string $tableName Name of the table class in which this role is kept track of.
 455+ * @param string $classField Name of the field in which the list is cached in this class.
 456+ *
 457+ * @return array of EPRole
 458+ */
 459+ protected function getRoleList( $fieldName, $tableName, $classField ) {
 460+ if ( $this->$classField === false ) {
 461+ $userIds = $this->getField( $fieldName );
 462+
 463+ if ( empty( $userIds ) ) {
 464+ $this->$classField = array();
458465 }
 466+ else {
 467+ $this->$classField = $tableName::singleton()->select(
 468+ null,
 469+ array( 'user_id' => $userIds )
 470+ );
 471+ }
459472 }
460473
461 - return $this->instructors;
 474+ return $this->$classField;
462475 }
463476
464477 /**
@@ -468,15 +481,7 @@
469482 * @return array of EPCA
470483 */
471484 public function getCampusAmbassadors() {
472 - if ( $this->cas === false ) {
473 - $this->cas = array();
474 -
475 - foreach ( $this->getField( 'campus_ambs' ) as $userId ) {
476 - $this->cas[] = EPCA::newFromUserId( $userId );
477 - }
478 - }
479 -
480 - return $this->cas;
 485+ return $this->getRoleList( 'campus_ambs', 'EPCAs', 'cas' );
481486 }
482487
483488 /**
@@ -487,15 +492,7 @@
488493 * @return array of EPOA
489494 */
490495 public function getOnlineAmbassadors() {
491 - if ( $this->oas === false ) {
492 - $this->oas = array();
493 -
494 - foreach ( $this->getField( 'online_ambs' ) as $userId ) {
495 - $this->oas[] = EPOA::newFromUserId( $userId );
496 - }
497 - }
498 -
499 - return $this->oas;
 496+ return $this->getRoleList( 'online_ambs', 'EPOAs', 'oas' );
500497 }
501498
502499 /**

Status & tagging log