r108685 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108684‎ | r108685 | r108686 >
Date:21:58, 11 January 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on enrtollment stuff
Modified paths:
  • /trunk/extensions/EducationProgram/EducationProgram.i18n.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPStudent.php (modified) (history)
  • /trunk/extensions/EducationProgram/specials/SpecialEnroll.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/specials/SpecialEnroll.php
@@ -64,13 +64,14 @@
6565 if ( $this->getUser()->isLoggedIn() ) {
6666 if ( $this->getUser()->isAllowed( 'epstudent' ) ) {
6767 $user = $this->getUser();
68 - $hasFields = trim( $user->getRealName() ) !== '' || $user->getOption( 'gender' ) !== 'unknown';
 68+ $hasFields = trim( $user->getRealName() ) !== '' && $user->getOption( 'gender' ) !== 'unknown';
6969
7070 if ( $hasFields ) {
71 - $this->showEnrollmentForm( $term );
 71+ $this->doEnroll( $term );
 72+ $this->onSuccess();
7273 }
7374 else {
74 -
 75+ $this->showEnrollmentForm( $term );
7576 }
7677 }
7778 else {
@@ -137,16 +138,41 @@
138139 }
139140
140141 /**
141 - * Just enroll the user in the term. This is useful when
142 - * there are no things for the user to fill out in the
143 - * enrollment form, making that step not needed.
 142+ * Just enroll the user in the term.
144143 *
145144 * @since 0.1
146145 *
147146 * @param EPTerm $term
 147+ *
 148+ * @return boolean Success indicator
148149 */
149 - protected function autoEnroll( EPTerm $term ) {
150 - // TODO
 150+ protected function doEnroll( EPTerm $term ) {
 151+ $student = EPStudent::newFromUser( $this->getUser(), array( 'id' ) );
 152+ $hadStudent = $student !== false;
 153+
 154+ $fields = array(
 155+ 'active_enroll' => 1
 156+ );
 157+
 158+ if ( !$hadStudent ) {
 159+ $student = new EPStudent( array( 'user_id' => $this->getUser()->getId() ), true );
 160+ $fields['first_enroll'] = wfTimestamp( TS_MW );
 161+ }
 162+
 163+ $student->setFields( $fields );
 164+
 165+ $success = $student->writeToDB();
 166+
 167+ if ( $success ) {
 168+ $success = $student->associateWithTerms( array( $term ) ) && $success;
 169+
 170+ if ( !$hadStudent ) {
 171+ $this->getUser()->setOption( 'ep_showtoplink', true );
 172+ $this->getUser()->saveSettings();
 173+ }
 174+ }
 175+
 176+ return $success;
151177 }
152178
153179 /**
@@ -205,7 +231,7 @@
206232 'default' => 'unknown',
207233 'label-message' => 'ep-enroll-gender',
208234 'validation-callback' => function( $value, array $alldata = null ) {
209 - return strlen( $value ) < 2 ? wfMsg( 'ep-enroll-invalid-gender' ) : true;
 235+ return in_array( $value, array( 'male', 'female', 'unknown' ) ) ? true : wfMsg( 'ep-enroll-invalid-gender' );
210236 },
211237 'options' => array(
212238 wfMsg( 'gender-male' ) => 'male',
@@ -227,25 +253,17 @@
228254 * @return Bool|Array
229255 */
230256 public function handleSubmission( array $data ) {
231 - $student = EPStudent::newFromUser( $this->getUser(), array( 'id' ) );
 257+ if ( array_key_exists( 'realname', $data ) ) {
 258+ $this->getUser()->setRealName( $data['realname'] );
 259+ }
232260
233 - if ( $student === false ) {
234 - $student = new EPStudent( array( 'user_id' => $this->getUser()->getId() ), true );
 261+ if ( array_key_exists( 'gender', $data ) ) {
 262+ $this->getUser()->setOption( 'gender', $data['gender'] );
235263 }
236264
237 - $fields = array(); // TODO
 265+ $this->getUser()->saveSettings();
238266
239 - $student->setFields( $fields );
240 -
241 - $success = $student->writeToDB();
242 -
243 - if ( $success ) {
244 - $success = $student->associateWithTerms( array( $this->term ) ) && $success;
245 - $this->getUser()->setOption( 'ep_showtoplink', true );
246 - $this->getUser()->saveSettings(); // TODO: can't we just save this single option instead of everything?
247 - }
248 -
249 - if ( $success ) {
 267+ if ( $this->doEnroll( $this->term ) ) {
250268 return true;
251269 }
252270 else {
Index: trunk/extensions/EducationProgram/includes/EPStudent.php
@@ -82,6 +82,11 @@
8383
8484 $dbw->commit();
8585
 86+ foreach ( $terms as /* EPTerm */ $term ) {
 87+ EPCourse::updateSummaryFields( 'students', array( 'id' => $term->getField( 'course_id' ) ) );
 88+ EPOrg::updateSummaryFields( 'students', array( 'id' => $term->getField( 'org_id' ) ) );
 89+ }
 90+
8691 return $success;
8792 }
8893
Index: trunk/extensions/EducationProgram/EducationProgram.i18n.php
@@ -268,6 +268,10 @@
269269 'ep-enroll-invalid-token' => 'The token you provided is invalid.',
270270 'ep-enroll-legend' => 'Enroll',
271271 'ep-enroll-header' => 'In order to enroll for this course, all you need to do is fill out this form and click the submission button. After that you will be enrolled.',
 272+ 'ep-enroll-gender' => 'Gender (optional)',
 273+ 'ep-enroll-realname' => 'Real name (required)',
 274+ 'ep-enroll-invalid-name' => 'The name needs to be at least contain $1 {{PLURAL:$1|character|characters}}.',
 275+ 'ep-enroll-invalid-gender' => 'Please select one of these genders',
272276
273277 // Special:MyCourses
274278 'ep-mycourses-enrolled' => 'You have successfully enrolled for $1 at $2.',

Status & tagging log