r107301 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107300‎ | r107301 | r107302 >
Date:23:14, 25 December 2011
Author:jeroendedauw
Status:deferred
Tags:educationprogram 
Comment:
work on mentor class
Modified paths:
  • /trunk/extensions/EducationProgram/includes/EPMentor.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/includes/EPMentor.php
@@ -37,39 +37,73 @@
3838
3939 /**
4040 * Returns the orgs this mentor is part of.
 41+ * Caches the result when no conditions are provided and all fields are selected.
4142 *
4243 * @since 0.1
4344 *
4445 * @param array|null $fields
 46+ * @param array $conditions
4547 *
4648 * @return array of EPOrg
4749 */
48 - public function getOrgs( array $fields = null ) {
 50+ public function getOrgs( array $fields = null, array $conditions = array() ) {
 51+ if ( count( $conditions ) !== 0 ) {
 52+ return $this->doGetOrgs( $fields, $conditions );
 53+ }
 54+
4955 if ( $this->orgs === false ) {
50 - $this->orgs = EPOrg::select(
51 - $fields,
52 - array( array( 'ep_mentors_per_org', 'mentor_id' ), $this->getId() ),
53 - array(),
54 - array( 'orgs' => array( 'INNER JOIN', array( array( array( 'ep_mentors_per_org', 'org_id' ), array( 'orgs', 'id' ) ) ) ) )
55 - );
 56+ $orgs = $this->doGetOrgs( $fields, $conditions );
 57+
 58+ if ( is_null( $fields ) ) {
 59+ $this->orgs = $orgs;
 60+ }
5661 }
5762
58 - return $this->orgs;
 63+ return $orgs;
5964 }
6065
6166 /**
 67+ * Returns the orgs this mentor is part of.
 68+ *
 69+ * @since 0.1
 70+ *
 71+ * @param array|null $fields
 72+ * @param array $conditions
 73+ *
 74+ * @return array of EPOrg
 75+ */
 76+ protected function doGetOrgs( $fields, array $conditions ) {
 77+ $conditions = array_merge(
 78+ array( array( 'ep_mentors_per_org', 'mentor_id' ), $this->getId() ),
 79+ $conditions
 80+ );
 81+
 82+ return EPOrg::select(
 83+ $fields,
 84+ $conditions,
 85+ array(),
 86+ array( 'orgs' => array( 'INNER JOIN', array( array( array( 'ep_mentors_per_org', 'org_id' ), array( 'orgs', 'id' ) ) ) ) )
 87+ );
 88+ }
 89+
 90+ /**
6291 * Retruns the courses this mentor can manage.
6392 *
6493 * @since 0.1
6594 *
6695 * @param array|null $fields
 96+ * @param array $conditions
6797 *
6898 * @return array of EPCourse
6999 */
70 - public function getCourses( array $fields = null ) {
71 - return array_reduce( $this->getOrgs(), function( array $courses, EPOrg $org ) use ( $fields ) {
72 - return array_merge( $courses, $org->getCourses( $fields ) );
73 - }, array() );
 100+ public function getCourses( array $fields = null, array $conditions = array() ) {
 101+ return array_reduce(
 102+ $this->getOrgs( $fields, $conditions ),
 103+ function( array $courses, EPOrg $org ) use ( $fields ) {
 104+ return array_merge( $courses, $org->getCourses( $fields ) );
 105+ },
 106+ array()
 107+ );
74108 }
75109
76110 /**
@@ -78,13 +112,18 @@
79113 * @since 0.1
80114 *
81115 * @param array|null $fields
 116+ * @param array $conditions
82117 *
83118 * @return array of EPTerm
84119 */
85 - public function getTerms( array $fields = null ) {
86 - return array_reduce( $this->getOrgs(), function( array $terms, EPOrg $org ) use ( $fields ) {
87 - return array_merge( $terms, $org->getTerms( $fields ) );
88 - }, array() );
 120+ public function getTerms( array $fields = null, array $conditions = array() ) {
 121+ return array_reduce(
 122+ $this->getOrgs( $fields, $conditions ),
 123+ function( array $terms, EPOrg $org ) use ( $fields ) {
 124+ return array_merge( $terms, $org->getTerms( $fields ) );
 125+ },
 126+ array()
 127+ );
89128 }
90129
91130 /**
@@ -97,7 +136,7 @@
98137 * @return boolean
99138 */
100139 public function hasCourse( array $conditions = array() ) {
101 - return true; // TODO
 140+ return count( $this->getCourses( 'id', $conditions ) ) > 0;
102141 }
103142
104143 /**
@@ -110,7 +149,7 @@
111150 * @return boolean
112151 */
113152 public function hasTerm( array $conditions = array() ) {
114 - return true; // TODO
 153+ return count( $this->getTerms( 'id', $conditions ) ) > 0;
115154 }
116155
117156 }

Status & tagging log