r111976 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111975‎ | r111976 | r111977 >
Date:22:53, 20 February 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
fixed fail in import script and cleaned up some code
Modified paths:
  • /trunk/extensions/EducationProgram/includes/EPCourse.php (modified) (history)
  • /trunk/extensions/EducationProgram/maintenance/importWEPData.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/maintenance/importWEPData.php
@@ -154,9 +154,8 @@
155155 * @param array $courseIds Maps course names to ids
156156 */
157157 protected function insertStudents( array $students, array $courseIds ) {
158 - foreach ( $students as $student => $courseNames ) {
159 - $name = $student;
160 - $user = User::newFromName( $student );
 158+ foreach ( $students as $name => $courseNames ) {
 159+ $user = User::newFromName( $name );
161160
162161 if ( $user === false ) {
163162 echo "Failed to insert student '$name'. (invalid user name)\n";
@@ -184,16 +183,18 @@
185184
186185 foreach ( $courseNames as $courseName ) {
187186 if ( array_key_exists( $courseName, $courseIds ) ) {
188 - $courses[] = EPCourses::singleton()->newFromArray( array(
189 - 'id' => $courseIds[$courseName],
190 - 'students' => array(),
191 - ) );
 187+ $revAction = new EPRevisionAction();
 188+ $revAction->setUser( $user );
 189+ $revAction->setComment( 'Import' );
 190+
 191+ $course = EPCourses::singleton()->selectRow( null, array( 'id' => $courseIds[$courseName] ) );
 192+ $course->enlistUsers( array( $user->getId() ), 'student', true, $revAction );
192193 }
193194 else {
194195 echo "Failed to associate student '$name' with course '$courseName'.\n";
195196 }
196197 }
197 -
 198+
198199 if ( $student->associateWithCourses( $courses ) ) {
199200 echo "Inserted student '$name'\t\t and associated with courses: " . str_replace( '_', ' ', implode( ', ', $courseNames ) ) . "\n";
200201 }
Index: trunk/extensions/EducationProgram/includes/EPCourse.php
@@ -532,12 +532,23 @@
533533 * @see DBDataObject::setField()
534534 */
535535 public function setField( $name, $value ) {
536 - if ( $name === 'instructors' ) {
537 - $this->instructors = false;
 536+ switch ( $name ) {
 537+ case 'mc':
 538+ $value = str_replace( '_', ' ', $value );
 539+ break;
 540+ case 'instructors':
 541+ $this->instructors = false;
 542+ break;
 543+ case 'students':
 544+ $this->students = false;
 545+ break;
 546+ case 'oas':
 547+ $this->oas = false;
 548+ break;
 549+ case 'cas':
 550+ $this->cas = false;
 551+ break;
538552 }
539 - elseif ( $name === 'mc' ) {
540 - $value = str_replace( '_', ' ', $value );
541 - }
542553
543554 parent::setField( $name, $value );
544555 }
@@ -566,21 +577,10 @@
567578
568579 $field = $roleMap[$role];
569580 $users = $this->getField( $field );
570 - $addedUsers = array();
 581+ $addedUsers = array_diff( (array)$newUsers, $users );
571582
572 - foreach ( (array)$newUsers as $userId ) {
573 - if ( !is_integer( $userId ) ) {
574 - throw new MWException( 'Provided user id is not an integer' );
575 - }
576 -
577 - if ( !in_array( $userId, $users ) ) {
578 - $users[] = $userId;
579 - $addedUsers[] = $userId;
580 - }
581 - }
582 -
583583 if ( count( $addedUsers ) > 0 ) {
584 - $this->setField( $field, $users );
 584+ $this->setField( $field, array_merge( $users, $addedUsers ) );
585585
586586 $success = true;
587587

Status & tagging log