Index: trunk/extensions/EducationProgram/includes/EPMentor.php |
— | — | @@ -37,39 +37,73 @@ |
38 | 38 | |
39 | 39 | /** |
40 | 40 | * Returns the orgs this mentor is part of. |
| 41 | + * Caches the result when no conditions are provided and all fields are selected. |
41 | 42 | * |
42 | 43 | * @since 0.1 |
43 | 44 | * |
44 | 45 | * @param array|null $fields |
| 46 | + * @param array $conditions |
45 | 47 | * |
46 | 48 | * @return array of EPOrg |
47 | 49 | */ |
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 | + |
49 | 55 | 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 | + } |
56 | 61 | } |
57 | 62 | |
58 | | - return $this->orgs; |
| 63 | + return $orgs; |
59 | 64 | } |
60 | 65 | |
61 | 66 | /** |
| 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 | + /** |
62 | 91 | * Retruns the courses this mentor can manage. |
63 | 92 | * |
64 | 93 | * @since 0.1 |
65 | 94 | * |
66 | 95 | * @param array|null $fields |
| 96 | + * @param array $conditions |
67 | 97 | * |
68 | 98 | * @return array of EPCourse |
69 | 99 | */ |
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 | + ); |
74 | 108 | } |
75 | 109 | |
76 | 110 | /** |
— | — | @@ -78,13 +112,18 @@ |
79 | 113 | * @since 0.1 |
80 | 114 | * |
81 | 115 | * @param array|null $fields |
| 116 | + * @param array $conditions |
82 | 117 | * |
83 | 118 | * @return array of EPTerm |
84 | 119 | */ |
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 | + ); |
89 | 128 | } |
90 | 129 | |
91 | 130 | /** |
— | — | @@ -97,7 +136,7 @@ |
98 | 137 | * @return boolean |
99 | 138 | */ |
100 | 139 | public function hasCourse( array $conditions = array() ) { |
101 | | - return true; // TODO |
| 140 | + return count( $this->getCourses( 'id', $conditions ) ) > 0; |
102 | 141 | } |
103 | 142 | |
104 | 143 | /** |
— | — | @@ -110,7 +149,7 @@ |
111 | 150 | * @return boolean |
112 | 151 | */ |
113 | 152 | public function hasTerm( array $conditions = array() ) { |
114 | | - return true; // TODO |
| 153 | + return count( $this->getTerms( 'id', $conditions ) ) > 0; |
115 | 154 | } |
116 | 155 | |
117 | 156 | } |