r111204 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111203‎ | r111204 | r111205 >
Date:21:54, 10 February 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on secondary storage
Modified paths:
  • /trunk/extensions/EducationProgram/includes/EPCourse.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPDBObject.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPOrg.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPRoleObject.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/includes/EPDBObject.php
@@ -1232,7 +1232,7 @@
12331233 public static function updateSummaryFields( $summaryFields = null, array $conditions = array() ) {
12341234 self::setReadDb( DB_MASTER );
12351235
1236 - foreach ( self::select( 'id', $conditions ) as /* EPDBObject */ $item ) {
 1236+ foreach ( self::select( null, $conditions ) as /* EPDBObject */ $item ) {
12371237 $item->loadSummaryFields( $summaryFields );
12381238 $item->setSummaryMode( true );
12391239 $item->saveExisting();
Index: trunk/extensions/EducationProgram/includes/EPOrg.php
@@ -111,7 +111,7 @@
112112
113113 foreach ( array( 'student_count', 'instructor_count', 'oa_count', 'ca_count' ) as $field ) {
114114 $fields[$field] = EPCourse::rawSelect(
115 - array( 'SUM(' . $dbr->addQuotes( $field ) . ') AS sum' ),
 115+ array( 'SUM(' . EPCourse::getPrefixedField( $field ). ') AS sum' ),
116116 EPCourse::getPrefixedValues( array(
117117 'org_id' => $this->getId()
118118 ) )
Index: trunk/extensions/EducationProgram/includes/EPRoleObject.php
@@ -115,7 +115,7 @@
116116 }
117117
118118 /**
119 - * Associate the student with the provided courses.
 119+ * Associate the user with the provided courses.
120120 *
121121 * @since 0.1
122122 *
@@ -124,31 +124,43 @@
125125 * @return bool
126126 */
127127 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;
153165 }
154166
155167 /**
Index: trunk/extensions/EducationProgram/includes/EPCourse.php
@@ -163,6 +163,13 @@
164164 );
165165 }
166166
 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+
167174 /**
168175 * (non-PHPdoc)
169176 * @see EPDBObject::loadSummaryFields()
@@ -181,16 +188,9 @@
182189 $fields['org_id'] = $this->getField( 'org_id' );
183190 }
184191
185 - $map = array(
186 - 'student_count' => 'students',
187 - 'instructor_count' => 'instructors',
188 - 'oa_count' => 'online_ambs',
189 - 'ca_count' => 'campus_ambs',
190 - );
191 -
192192 foreach ( array( 'student_count', 'instructor_count', 'oa_count', 'ca_count' ) as $field ) {
193193 if ( in_array( $field, $summaryFields ) ) {
194 - $fields[$field] = count( $this->getField( $map[$field] ) );
 194+ $fields[$field] = count( $this->getField( self::$countMap[$field] ) );
195195 }
196196 }
197197
@@ -231,13 +231,9 @@
232232 */
233233 protected function onUpdated( EPRevisionedObject $originalCourse ) {
234234 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 -
240235 $removedIds = array();
241236 $newUsers = array();
 237+ $changedSummaries = array();
242238
243239 $roleMap = array(
244240 'online_ambs' => EP_OA,
@@ -246,17 +242,25 @@
247243 'instructors' => EP_INSTRUCTOR,
248244 );
249245
 246+ $countMap = array_flip( self::$countMap );
 247+
250248 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 ) ) {
252250 $removedIds = array_diff( $originalCourse->getField( $usersField ), $this->getField( $usersField ) );
 251+ $addedIds = array_diff( $this->getField( $usersField ), $originalCourse->getField( $usersField ) );
253252
254 - foreach ( array_diff( $this->getField( $usersField ), $originalCourse->getField( $usersField ) ) as $addedId ) {
255 - $newUsers = array(
 253+ foreach ( $addedIds as $addedId ) {
 254+ $newUsers[] = array(
256255 'upc_course_id' => $this->getId(),
257256 'upc_user_id' => $addedId,
258257 'upc_role' => $roleMap[$usersField],
259258 );
260259 }
 260+
 261+ if ( !empty( $removedIds ) || !empty( $addedIds ) ) {
 262+ $this->loadSummaryFields( $countMap[$usersField] );
 263+ $changedSummaries[] = $countMap[$usersField];
 264+ }
261265 }
262266 }
263267
@@ -278,6 +282,20 @@
279283
280284 $dbw->commit();
281285 }
 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+ }
282300 }
283301
284302 parent::onUpdated( $originalCourse );

Status & tagging log