Index: trunk/extensions/EducationProgram/includes/EPOrg.php |
— | — | @@ -38,10 +38,10 @@ |
39 | 39 | |
40 | 40 | 'active' => 'bool', |
41 | 41 | 'courses' => 'int', |
42 | | - 'students' => 'int', |
43 | | - 'instructors' => 'int', |
44 | | - 'campus_ambs' => 'int', |
45 | | - 'online_ambs' => 'int', |
| 42 | + 'student_count' => 'int', |
| 43 | + 'instructor_count' => 'int', |
| 44 | + 'ca_count' => 'int', |
| 45 | + 'oa_count' => 'int', |
46 | 46 | ); |
47 | 47 | } |
48 | 48 | |
— | — | @@ -57,10 +57,10 @@ |
58 | 58 | |
59 | 59 | 'active' => false, |
60 | 60 | 'courses' => 0, |
61 | | - 'students' => 0, |
62 | | - 'instructors' => 0, |
63 | | - 'campus_ambs' => 0, |
64 | | - 'online_ambs' => 0, |
| 61 | + 'student_count' => 0, |
| 62 | + 'instructor_count' => 0, |
| 63 | + 'ca_count' => 0, |
| 64 | + 'oa_count' => 0, |
65 | 65 | ); |
66 | 66 | } |
67 | 67 | |
— | — | @@ -72,10 +72,10 @@ |
73 | 73 | return array( |
74 | 74 | 'active', |
75 | 75 | 'courses', |
76 | | - 'students', |
77 | | - 'instructors', |
78 | | - 'campus_ambs', |
79 | | - 'online_ambs', |
| 76 | + 'student_count', |
| 77 | + 'instructor_count', |
| 78 | + 'ca_count', |
| 79 | + 'oa_count', |
80 | 80 | ); |
81 | 81 | } |
82 | 82 | |
Index: trunk/extensions/EducationProgram/includes/EPRevisionedObject.php |
— | — | @@ -137,24 +137,41 @@ |
138 | 138 | protected function saveExisting() { |
139 | 139 | if ( !$this->inSummaryMode ) { |
140 | 140 | static::setReadDb( DB_MASTER ); |
141 | | - $currentObject = static::selectRow( null, array( 'id' => $this->getId() ) ); |
| 141 | + $originalObject = static::selectRow( null, array( 'id' => $this->getId() ) ); |
142 | 142 | static::setReadDb( DB_SLAVE ); |
143 | 143 | } |
144 | 144 | |
| 145 | + if ( $originalObject === false ) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
145 | 149 | $success = true; |
146 | 150 | |
147 | | - if ( $this->inSummaryMode || $currentObject === false || $this->fieldsChanged( $currentObject, true ) ) { |
| 151 | + if ( $this->inSummaryMode || $this->fieldsChanged( $originalObject, true ) ) { |
148 | 152 | |
149 | 153 | $success = parent::saveExisting(); |
150 | 154 | |
151 | | - if ( $success && !$this->inSummaryMode ) { |
152 | | - $this->storeRevision( $currentObject ); |
153 | | - $this->log( 'update' ); |
| 155 | + if ( $success ) { |
| 156 | + $this->onUpdated( $originalObject ); |
154 | 157 | } |
155 | 158 | } |
156 | 159 | |
157 | 160 | return $success; |
158 | 161 | } |
| 162 | + |
| 163 | + /** |
| 164 | + * |
| 165 | + * |
| 166 | + * @since 0.1 |
| 167 | + * |
| 168 | + * @param EPRevisionedObject $originalObject |
| 169 | + */ |
| 170 | + protected function onUpdated( EPRevisionedObject $originalObject ) { |
| 171 | + if ( !$this->inSummaryMode ) { |
| 172 | + $this->storeRevision( $originalObject ); |
| 173 | + $this->log( 'update' ); |
| 174 | + } |
| 175 | + } |
159 | 176 | |
160 | 177 | /** |
161 | 178 | * (non-PHPdoc) |
Index: trunk/extensions/EducationProgram/includes/EPCourse.php |
— | — | @@ -217,79 +217,70 @@ |
218 | 218 | */ |
219 | 219 | protected function onRemoved() { |
220 | 220 | if ( $this->updateSummaries ) { |
221 | | - EPOrg::updateSummaryFields( array( 'courses', 'students', 'active', 'instructors', 'oas', 'cas' ), array( 'id' => $this->getField( 'org_id' ) ) ); |
| 221 | + EPOrg::updateSummaryFields( null, array( 'id' => $this->getField( 'org_id' ) ) ); |
222 | 222 | } |
223 | 223 | |
224 | | - wfGetDB( DB_MASTER )->delete( 'ep_students_per_course', array( 'spc_course_id' => $this->getId() ) ); |
225 | | - wfGetDB( DB_MASTER )->delete( 'ep_cas_per_course', array( 'cpc_course_id' => $this->getId() ) ); |
226 | | - wfGetDB( DB_MASTER )->delete( 'ep_oas_per_course', array( 'opc_course_id' => $this->getId() ) ); |
227 | | - |
| 224 | + wfGetDB( DB_MASTER )->delete( 'ep_users_per_course', array( 'upc_course_id' => $this->getId() ) ); |
| 225 | + |
228 | 226 | parent::onRemoved(); |
229 | 227 | } |
230 | 228 | |
231 | 229 | /** |
232 | 230 | * (non-PHPdoc) |
233 | | - * @see EPDBObject::saveExisting() |
| 231 | + * @see EPRevisionedObject::onUpdated() |
234 | 232 | */ |
235 | | - protected function saveExisting() { |
| 233 | + protected function onUpdated( EPRevisionedObject $originalCourse ) { |
236 | 234 | if ( $this->updateSummaries ) { |
237 | | - $currentCourse = false; |
238 | | - $currentFields = array( 'id' ); |
239 | | - |
240 | | - foreach ( array( 'org_id', 'online_ambs', 'campus_ambs' ) as $field ) { |
241 | | - if ( $this->hasField( $field ) ) { |
242 | | - $currentFields[] = $field; |
| 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 | + $removedIds = array(); |
| 241 | + $newUsers = array(); |
| 242 | + |
| 243 | + $roleMap = array( |
| 244 | + 'online_ambs' => EP_OA, |
| 245 | + 'campus_ambs' => EP_CA, |
| 246 | + 'students' => EP_STUDENT, |
| 247 | + 'instructors' => EP_INSTRUCTOR, |
| 248 | + ); |
| 249 | + |
| 250 | + foreach ( array( 'online_ambs', 'campus_ambs', 'students', 'instructors' ) as $usersField ) { |
| 251 | + if ( $originalCourse->getField( $usersField ) !== $this->getField( $usersField ) ) { |
| 252 | + $removedIds = array_diff( $originalCourse->getField( $usersField ), $this->getField( $usersField ) ); |
| 253 | + |
| 254 | + foreach ( array_diff( $this->getField( $usersField ), $originalCourse->getField( $usersField ) ) as $addedId ) { |
| 255 | + $newUsers = array( |
| 256 | + 'upc_course_id' => $this->getId(), |
| 257 | + 'upc_user_id' => $addedId, |
| 258 | + 'upc_role' => $roleMap[$usersField], |
| 259 | + ); |
| 260 | + } |
243 | 261 | } |
244 | 262 | } |
245 | | - |
246 | | - if ( count( $currentFields ) > 1 ) { |
247 | | - $currentCourse = self::selectRow( $currentFields, array( 'id' => $this->getId() ) ); |
248 | | - } |
249 | | - } |
250 | 263 | |
251 | | - $success = parent::saveExisting(); |
| 264 | + $dbw = wfGetDB( DB_MASTER ); |
252 | 265 | |
253 | | - if ( $this->updateSummaries && $currentCourse !== false && $success ) { |
254 | | - if ( $currentCourse->hasField( 'org_id' ) && $currentCourse->getField( 'org_id' ) !== $this->getField( 'org_id' ) ) { |
255 | | - $conds = array( 'id' => array( $currentCourse->getField( 'org_id' ), $this->getField( 'org_id' ) ) ); |
256 | | - EPOrg::updateSummaryFields( array( 'courses', 'students', 'active', 'instructors', 'oas', 'cas' ), $conds ); |
| 266 | + if ( count( $removedIds ) > 0 ) { |
| 267 | + $dbw->delete( 'ep_users_per_course', array( |
| 268 | + 'upc_course_id' => $this->getId(), |
| 269 | + 'upc_user_id' => $removedIds |
| 270 | + ) ); |
257 | 271 | } |
258 | | - |
259 | | - foreach ( array( 'oas', 'cas' ) as $ambs ) { |
260 | | - $field = $ambs === 'oas' ? 'online_ambs' : 'campus_ambs'; |
261 | | - |
262 | | - if ( $currentCourse->hasField( $field ) && $currentCourse->getField( $field ) !== $this->getField( $field ) ) { |
263 | | - $courseField = $ambs === 'oas' ? 'opc_course_id' : 'cpc_course_id'; |
264 | | - $userField = $ambs === 'oas' ? 'opc_user_id' : 'cpc_user_id'; |
265 | | - $table = 'ep_' . $ambs . '_per_course'; |
266 | | - |
267 | | - $addedIds = array_diff( $this->getField( $field ), $currentCourse->getField( $field ) ); |
268 | | - $removedIds = array_diff( $currentCourse->getField( $field ), $this->getField( $field ) ); |
269 | 272 | |
270 | | - $dbw = wfGetDB( DB_MASTER ); |
271 | | - |
272 | | - if ( count( $removedIds ) > 0 ) { |
273 | | - $dbw->delete( $table, array( |
274 | | - $courseField => $this->getId(), |
275 | | - $userField => $removedIds |
276 | | - ) ); |
277 | | - } |
278 | | - |
279 | | - $dbw->begin(); |
| 273 | + if ( count( $newUsers ) > 0 ) { |
| 274 | + $dbw->begin(); |
280 | 275 | |
281 | | - foreach ( $addedIds as $ambassadorId ) { |
282 | | - $dbw->insert( $table, array( |
283 | | - $courseField => $this->getId(), |
284 | | - $userField => $ambassadorId |
285 | | - ) ); |
286 | | - } |
287 | | - |
288 | | - $dbw->commit(); |
| 276 | + foreach ( $newUsers as $userLink ) { |
| 277 | + $dbw->insert( 'ep_users_per_course', $userLink ); |
289 | 278 | } |
| 279 | + |
| 280 | + $dbw->commit(); |
290 | 281 | } |
291 | 282 | } |
292 | 283 | |
293 | | - return $success; |
| 284 | + parent::onUpdated( $originalCourse ); |
294 | 285 | } |
295 | 286 | |
296 | 287 | /** |