Index: trunk/extensions/EducationProgram/maintenance/importWEPData.php |
— | — | @@ -62,10 +62,24 @@ |
63 | 63 | } |
64 | 64 | } |
65 | 65 | |
66 | | - echo 'Found ' . count( $orgs ) . ' orgs, ' . count( $courses ) . ' courses and ' . count( $students ) . " students.\n"; |
| 66 | + echo "\nFound " . count( $orgs ) . ' orgs, ' . count( $courses ) . ' courses and ' . count( $students ) . " students.\n\n"; |
67 | 67 | |
68 | 68 | echo "Inserting orgs ..."; |
| 69 | + $this->insertOrgs( $orgs ); |
| 70 | + echo " done!\n"; |
69 | 71 | |
| 72 | + echo "Inserting courses ...\n"; |
| 73 | + $courseIds = $this->insertCourses( $courses, $orgs ); |
| 74 | + echo "Inserted courses\n"; |
| 75 | + |
| 76 | + echo "Inserting students ...\n"; |
| 77 | + $this->insertStudents( $students, $courseIds ); |
| 78 | + echo "Inserted students\n"; |
| 79 | + |
| 80 | + echo "Import completed!\n\n"; |
| 81 | + } |
| 82 | + |
| 83 | + protected function insertOrgs( array $orgs ) { |
70 | 84 | wfGetDB( DB_MASTER )->begin(); |
71 | 85 | |
72 | 86 | foreach ( $orgs as $org => &$id ) { |
— | — | @@ -81,12 +95,12 @@ |
82 | 96 | $id = $org->getId(); |
83 | 97 | } |
84 | 98 | |
85 | | - wfGetDB( DB_MASTER )->commit(); |
| 99 | + wfGetDB( DB_MASTER )->commit(); |
| 100 | + } |
| 101 | + |
| 102 | + protected function insertCourses( array $courses, array $orgs ) { |
| 103 | + $courseIds = array(); |
86 | 104 | |
87 | | - echo " done!\n"; |
88 | | - |
89 | | - echo "Inserting courses ...\n"; |
90 | | - |
91 | 105 | foreach ( $courses as $course => $org ) { |
92 | 106 | $name = $course; |
93 | 107 | |
— | — | @@ -101,17 +115,76 @@ |
102 | 116 | |
103 | 117 | try{ |
104 | 118 | $course->save(); |
| 119 | + $courseIds[$name] = $course->getId(); |
105 | 120 | } |
106 | 121 | catch ( Exception $ex ) { |
107 | 122 | echo "Failed to insert course '$name'.\n"; |
108 | 123 | } |
109 | 124 | } |
110 | | - |
111 | | - echo "Inserted courses\n"; |
112 | | - |
113 | | - echo "\n\n"; |
| 125 | + |
| 126 | + return $courseIds; |
114 | 127 | } |
115 | 128 | |
| 129 | + protected function insertStudents( array $students, array $courseIds ) { |
| 130 | + foreach ( $students as $student => $courseNames ) { |
| 131 | + $name = $student; |
| 132 | + $user = User::newFromName( $student ); |
| 133 | + |
| 134 | + if ( $user === false ) { |
| 135 | + echo "Failed to insert student '$name'. (invalid user name)\n"; |
| 136 | + } |
| 137 | + else { |
| 138 | + if ( $user->getId() === 0 ) { |
| 139 | + $user->setPassword( 'ohithere' ); |
| 140 | + $user->addToDatabase(); |
| 141 | + } |
| 142 | + |
| 143 | + if ( $user->getId() === 0 ) { |
| 144 | + echo "Failed to insert student '$name'. (failed to create user)\n"; |
| 145 | + } |
| 146 | + else { |
| 147 | + $student = EPStudent::newFromUser( $user ); |
| 148 | + |
| 149 | + if ( $student === false ) { |
| 150 | + $student = new EPStudent( |
| 151 | + array( |
| 152 | + 'user_id' => $user->getId(), |
| 153 | + 'first_enroll' => wfTimestamp( TS_MW ) |
| 154 | + ), |
| 155 | + true |
| 156 | + ); |
| 157 | + |
| 158 | + if ( !$student->save() ) { |
| 159 | + echo "Failed to insert student '$name'. (failed create student profile)\n"; |
| 160 | + continue; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + $courses = array(); |
| 165 | + |
| 166 | + foreach ( $courseNames as $courseName ) { |
| 167 | + if ( array_key_exists( $courseName, $courseIds ) ) { |
| 168 | + $courses[] = EPCourses::singleton()->newFromArray( array( |
| 169 | + 'id' => $courseIds[$courseName], |
| 170 | + 'students' => array(), |
| 171 | + ) ); |
| 172 | + } |
| 173 | + else { |
| 174 | + echo "Failed to associate student '$name' with course '$courseName'.\n"; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + if ( $student->associateWithCourses( $courses ) ) { |
| 179 | + echo "Imported student $name\n"; |
| 180 | + } |
| 181 | + else { |
| 182 | + echo "Failed to insert student '$name'. (failed to associate courses)\n"; |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
116 | 189 | } |
117 | 190 | |
118 | 191 | $maintClass = 'ImportWEPData'; |
Index: trunk/extensions/EducationProgram/includes/EPRoleObject.php |
— | — | @@ -137,6 +137,8 @@ |
138 | 138 | public function associateWithCourses( array /* of EPCourse */ $courses ) { |
139 | 139 | $success = true; |
140 | 140 | |
| 141 | + $courseIds = array(); |
| 142 | + |
141 | 143 | foreach ( $courses as /* EPCourse */ $course ) { |
142 | 144 | $courseIds[] = $course->getId(); |
143 | 145 | $course->setUpdateSummaries( false ); |