r111184 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111183‎ | r111184 | r111185 >
Date:19:21, 10 February 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Follow up to r111172;
Modified paths:
  • /trunk/extensions/EducationProgram/includes/EPCourse.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPOrg.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPRevisionedObject.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/includes/EPOrg.php
@@ -38,10 +38,10 @@
3939
4040 'active' => 'bool',
4141 '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',
4646 );
4747 }
4848
@@ -57,10 +57,10 @@
5858
5959 'active' => false,
6060 '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,
6565 );
6666 }
6767
@@ -72,10 +72,10 @@
7373 return array(
7474 'active',
7575 'courses',
76 - 'students',
77 - 'instructors',
78 - 'campus_ambs',
79 - 'online_ambs',
 76+ 'student_count',
 77+ 'instructor_count',
 78+ 'ca_count',
 79+ 'oa_count',
8080 );
8181 }
8282
Index: trunk/extensions/EducationProgram/includes/EPRevisionedObject.php
@@ -137,24 +137,41 @@
138138 protected function saveExisting() {
139139 if ( !$this->inSummaryMode ) {
140140 static::setReadDb( DB_MASTER );
141 - $currentObject = static::selectRow( null, array( 'id' => $this->getId() ) );
 141+ $originalObject = static::selectRow( null, array( 'id' => $this->getId() ) );
142142 static::setReadDb( DB_SLAVE );
143143 }
144144
 145+ if ( $originalObject === false ) {
 146+ return false;
 147+ }
 148+
145149 $success = true;
146150
147 - if ( $this->inSummaryMode || $currentObject === false || $this->fieldsChanged( $currentObject, true ) ) {
 151+ if ( $this->inSummaryMode || $this->fieldsChanged( $originalObject, true ) ) {
148152
149153 $success = parent::saveExisting();
150154
151 - if ( $success && !$this->inSummaryMode ) {
152 - $this->storeRevision( $currentObject );
153 - $this->log( 'update' );
 155+ if ( $success ) {
 156+ $this->onUpdated( $originalObject );
154157 }
155158 }
156159
157160 return $success;
158161 }
 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+ }
159176
160177 /**
161178 * (non-PHPdoc)
Index: trunk/extensions/EducationProgram/includes/EPCourse.php
@@ -217,79 +217,70 @@
218218 */
219219 protected function onRemoved() {
220220 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' ) ) );
222222 }
223223
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+
228226 parent::onRemoved();
229227 }
230228
231229 /**
232230 * (non-PHPdoc)
233 - * @see EPDBObject::saveExisting()
 231+ * @see EPRevisionedObject::onUpdated()
234232 */
235 - protected function saveExisting() {
 233+ protected function onUpdated( EPRevisionedObject $originalCourse ) {
236234 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+ }
243261 }
244262 }
245 -
246 - if ( count( $currentFields ) > 1 ) {
247 - $currentCourse = self::selectRow( $currentFields, array( 'id' => $this->getId() ) );
248 - }
249 - }
250263
251 - $success = parent::saveExisting();
 264+ $dbw = wfGetDB( DB_MASTER );
252265
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+ ) );
257271 }
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 ) );
269272
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();
280275
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 );
289278 }
 279+
 280+ $dbw->commit();
290281 }
291282 }
292283
293 - return $success;
 284+ parent::onUpdated( $originalCourse );
294285 }
295286
296287 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r111196Follow up to r111172; r111184jeroendedauw20:31, 10 February 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r111172schema changesjeroendedauw18:07, 10 February 2012

Status & tagging log