Index: trunk/extensions/EducationProgram/includes/EPDBObject.php |
— | — | @@ -1232,7 +1232,7 @@ |
1233 | 1233 | public static function updateSummaryFields( $summaryFields = null, array $conditions = array() ) { |
1234 | 1234 | self::setReadDb( DB_MASTER ); |
1235 | 1235 | |
1236 | | - foreach ( self::select( 'id', $conditions ) as /* EPDBObject */ $item ) { |
| 1236 | + foreach ( self::select( null, $conditions ) as /* EPDBObject */ $item ) { |
1237 | 1237 | $item->loadSummaryFields( $summaryFields ); |
1238 | 1238 | $item->setSummaryMode( true ); |
1239 | 1239 | $item->saveExisting(); |
Index: trunk/extensions/EducationProgram/includes/EPOrg.php |
— | — | @@ -111,7 +111,7 @@ |
112 | 112 | |
113 | 113 | foreach ( array( 'student_count', 'instructor_count', 'oa_count', 'ca_count' ) as $field ) { |
114 | 114 | $fields[$field] = EPCourse::rawSelect( |
115 | | - array( 'SUM(' . $dbr->addQuotes( $field ) . ') AS sum' ), |
| 115 | + array( 'SUM(' . EPCourse::getPrefixedField( $field ). ') AS sum' ), |
116 | 116 | EPCourse::getPrefixedValues( array( |
117 | 117 | 'org_id' => $this->getId() |
118 | 118 | ) ) |
Index: trunk/extensions/EducationProgram/includes/EPRoleObject.php |
— | — | @@ -115,7 +115,7 @@ |
116 | 116 | } |
117 | 117 | |
118 | 118 | /** |
119 | | - * Associate the student with the provided courses. |
| 119 | + * Associate the user with the provided courses. |
120 | 120 | * |
121 | 121 | * @since 0.1 |
122 | 122 | * |
— | — | @@ -124,31 +124,43 @@ |
125 | 125 | * @return bool |
126 | 126 | */ |
127 | 127 | public function associateWithCourses( array /* of EPCourse */ $courses ) { |
128 | | -// $dbw = wfGetDB( DB_MASTER ); |
129 | | -// |
130 | | -// $success = true; |
131 | | -// |
132 | | -// $dbw->begin(); |
133 | | -// |
134 | | -// foreach ( $courses as /* EPCourse */ $course ) { |
135 | | -// $success = $dbw->insert( |
136 | | -// 'ep_users_per_course', |
137 | | -// array( |
138 | | -// 'upc_student_id' => $this->getId(), |
139 | | -// 'upc_course_id' => $course->getId(), |
140 | | -// 'upc_role' => EPUtils::getRoleId( $this->getRoleName() ), |
141 | | -// ) |
142 | | -// ) && $success; |
143 | | -// } |
144 | | -// |
145 | | -// $dbw->commit(); |
146 | | -// |
147 | | -// foreach ( $courses as /* EPCourse */ $course ) { |
148 | | -// EPOrg::updateSummaryFields( 'students', array( 'id' => $course->getField( 'org_id' ) ) ); |
149 | | -// EPCourse::updateSummaryFields( 'students', array( 'id' => $course->getId() ) ); |
150 | | -// } |
151 | | -// |
152 | | -// return $success; |
| 128 | + $success = true; |
| 129 | + |
| 130 | + if ( count( $courses ) > 0 ) { |
| 131 | + $courseIds = array(); |
| 132 | + $orgIds = array(); |
| 133 | + |
| 134 | + $dbw = wfGetDB( DB_MASTER ); |
| 135 | + $dbw->begin(); |
| 136 | + |
| 137 | + foreach ( $courses as /* EPCourse */ $course ) { |
| 138 | + $success = $dbw->insert( |
| 139 | + 'ep_users_per_course', |
| 140 | + array( |
| 141 | + 'upc_user_id' => $this->getId(), |
| 142 | + 'upc_course_id' => $course->getId(), |
| 143 | + 'upc_role' => EPUtils::getRoleId( $this->getRoleName() ), |
| 144 | + ) |
| 145 | + ) && $success; |
| 146 | + |
| 147 | + $courseIds[] = $course->getId(); |
| 148 | + } |
| 149 | + |
| 150 | + $dbw->commit(); |
| 151 | + |
| 152 | + $fieldMap = array( |
| 153 | + 'student' => 'student_count', |
| 154 | + 'online' => 'oa_count', |
| 155 | + 'campus' => 'ca_count', |
| 156 | + ); |
| 157 | + |
| 158 | + $field = $fieldMap[$this->getRoleName()]; |
| 159 | + |
| 160 | + EPOrg::updateSummaryFields( $field, array( 'id' => array_unique( $courseIds ) ) ); |
| 161 | + EPCourse::updateSummaryFields( $field, array( 'id' => $courseIds ) ); |
| 162 | + } |
| 163 | + |
| 164 | + return $success; |
153 | 165 | } |
154 | 166 | |
155 | 167 | /** |
Index: trunk/extensions/EducationProgram/includes/EPCourse.php |
— | — | @@ -163,6 +163,13 @@ |
164 | 164 | ); |
165 | 165 | } |
166 | 166 | |
| 167 | + protected static $countMap = array( |
| 168 | + 'student_count' => 'students', |
| 169 | + 'instructor_count' => 'instructors', |
| 170 | + 'oa_count' => 'online_ambs', |
| 171 | + 'ca_count' => 'campus_ambs', |
| 172 | + ); |
| 173 | + |
167 | 174 | /** |
168 | 175 | * (non-PHPdoc) |
169 | 176 | * @see EPDBObject::loadSummaryFields() |
— | — | @@ -181,16 +188,9 @@ |
182 | 189 | $fields['org_id'] = $this->getField( 'org_id' ); |
183 | 190 | } |
184 | 191 | |
185 | | - $map = array( |
186 | | - 'student_count' => 'students', |
187 | | - 'instructor_count' => 'instructors', |
188 | | - 'oa_count' => 'online_ambs', |
189 | | - 'ca_count' => 'campus_ambs', |
190 | | - ); |
191 | | - |
192 | 192 | foreach ( array( 'student_count', 'instructor_count', 'oa_count', 'ca_count' ) as $field ) { |
193 | 193 | if ( in_array( $field, $summaryFields ) ) { |
194 | | - $fields[$field] = count( $this->getField( $map[$field] ) ); |
| 194 | + $fields[$field] = count( $this->getField( self::$countMap[$field] ) ); |
195 | 195 | } |
196 | 196 | } |
197 | 197 | |
— | — | @@ -231,13 +231,9 @@ |
232 | 232 | */ |
233 | 233 | protected function onUpdated( EPRevisionedObject $originalCourse ) { |
234 | 234 | if ( $this->updateSummaries ) { |
235 | | - if ( $originalCourse->getField( 'org_id' ) !== $this->getField( 'org_id' ) ) { |
236 | | - $conds = array( 'id' => array( $originalCourse->getField( 'org_id' ), $this->getField( 'org_id' ) ) ); |
237 | | - EPOrg::updateSummaryFields( null, $conds ); |
238 | | - } |
239 | | - |
240 | 235 | $removedIds = array(); |
241 | 236 | $newUsers = array(); |
| 237 | + $changedSummaries = array(); |
242 | 238 | |
243 | 239 | $roleMap = array( |
244 | 240 | 'online_ambs' => EP_OA, |
— | — | @@ -246,17 +242,25 @@ |
247 | 243 | 'instructors' => EP_INSTRUCTOR, |
248 | 244 | ); |
249 | 245 | |
| 246 | + $countMap = array_flip( self::$countMap ); |
| 247 | + |
250 | 248 | foreach ( array( 'online_ambs', 'campus_ambs', 'students', 'instructors' ) as $usersField ) { |
251 | | - if ( $originalCourse->getField( $usersField ) !== $this->getField( $usersField ) ) { |
| 249 | + if ( $this->hasField( $usersField ) && $originalCourse->getField( $usersField ) !== $this->getField( $usersField ) ) { |
252 | 250 | $removedIds = array_diff( $originalCourse->getField( $usersField ), $this->getField( $usersField ) ); |
| 251 | + $addedIds = array_diff( $this->getField( $usersField ), $originalCourse->getField( $usersField ) ); |
253 | 252 | |
254 | | - foreach ( array_diff( $this->getField( $usersField ), $originalCourse->getField( $usersField ) ) as $addedId ) { |
255 | | - $newUsers = array( |
| 253 | + foreach ( $addedIds as $addedId ) { |
| 254 | + $newUsers[] = array( |
256 | 255 | 'upc_course_id' => $this->getId(), |
257 | 256 | 'upc_user_id' => $addedId, |
258 | 257 | 'upc_role' => $roleMap[$usersField], |
259 | 258 | ); |
260 | 259 | } |
| 260 | + |
| 261 | + if ( !empty( $removedIds ) || !empty( $addedIds ) ) { |
| 262 | + $this->loadSummaryFields( $countMap[$usersField] ); |
| 263 | + $changedSummaries[] = $countMap[$usersField]; |
| 264 | + } |
261 | 265 | } |
262 | 266 | } |
263 | 267 | |
— | — | @@ -278,6 +282,20 @@ |
279 | 283 | |
280 | 284 | $dbw->commit(); |
281 | 285 | } |
| 286 | + |
| 287 | + if ( !empty( $changedSummaries ) ) { |
| 288 | + $this->setSummaryMode( true ); |
| 289 | + $this->saveExisting(); |
| 290 | + $this->setSummaryMode( false ); |
| 291 | + } |
| 292 | + |
| 293 | + if ( $this->hasField( 'org_id' ) && $originalCourse->getField( 'org_id' ) !== $this->getField( 'org_id' ) ) { |
| 294 | + $conds = array( 'id' => array( $originalCourse->getField( 'org_id' ), $this->getField( 'org_id' ) ) ); |
| 295 | + EPOrg::updateSummaryFields( null, $conds ); |
| 296 | + } |
| 297 | + else if ( !empty( $changedSummaries ) ) { |
| 298 | + EPOrg::updateSummaryFields( $changedSummaries, array( 'id' => $originalCourse->getField( 'org_id' ) ) ); |
| 299 | + } |
282 | 300 | } |
283 | 301 | |
284 | 302 | parent::onUpdated( $originalCourse ); |