Merge branch 'wip-mdl-41744' of https://github.com/rajeshtaneja/moodle
authorSam Hemelryk <sam@moodle.com>
Thu, 10 Oct 2013 01:40:02 +0000 (14:40 +1300)
committerSam Hemelryk <sam@moodle.com>
Thu, 10 Oct 2013 01:40:02 +0000 (14:40 +1300)
1  2 
admin/tool/uploaduser/index.php

@@@ -100,12 -100,16 +100,16 @@@ $STD_FIELDS = array('id', 'firstname', 
  
  $PRF_FIELDS = array();
  
- if ($prof_fields = $DB->get_records('user_info_field')) {
-     foreach ($prof_fields as $prof_field) {
-         $PRF_FIELDS[] = 'profile_field_'.$prof_field->shortname;
+ if ($proffields = $DB->get_records('user_info_field')) {
+     foreach ($proffields as $key => $proffield) {
+         $profilefieldname = 'profile_field_'.$proffield->shortname;
+         $PRF_FIELDS[] = $profilefieldname;
+         // Re-index $proffields with key as shortname. This will be
+         // used while checking if profile data is key and needs to be converted (eg. menu profile field)
+         $proffields[$profilefieldname] = $proffield;
+         unset($proffields[$key]);
      }
  }
- unset($prof_fields);
  
  if (empty($iid)) {
      $mform1 = new admin_uploaduser_form1();
@@@ -372,6 -376,16 +376,16 @@@ if ($formdata = $mform2->is_cancelled()
              if (isset($formdata->$field)) {
                  // process templates
                  $user->$field = uu_process_template($formdata->$field, $user);
+                 // Form contains key and later code expects value.
+                 // Convert key to value for required profile fields.
+                 require_once($CFG->dirroot.'/user/profile/field/'.$proffields[$field]->datatype.'/field.class.php');
+                 $profilefieldclass = 'profile_field_'.$proffields[$field]->datatype;
+                 $profilefield = new $profilefieldclass($proffields[$field]->id);
+                 if (method_exists($profilefield, 'convert_external_data')) {
+                     $user->$field = $profilefield->edit_save_data_preprocess($user->$field, null);
+                 }
                  $formdefaults[$field] = true;
              }
          }
                  }
  
                  if ($rid) {
 -                    // find duration
 -                    $timeend   = 0;
 +                    // Find duration and/or enrol status.
 +                    $timeend = 0;
 +                    $status = null;
 +
 +                    if (isset($user->{'enrolstatus'.$i})) {
 +                        $enrolstatus = trim($user->{'enrolstatus'.$i});
 +                        if ($enrolstatus == '') {
 +                            $status = null;
 +                        } else if ($enrolstatus === (string)ENROL_USER_ACTIVE) {
 +                            $status = ENROL_USER_ACTIVE;
 +                        } else if ($enrolstatus === (string)ENROL_USER_SUSPENDED) {
 +                            $status = ENROL_USER_SUSPENDED;
 +                        } else {
 +                            debugging('Unknown enrolment status.');
 +                        }
 +                    }
 +
                      if (!empty($user->{'enrolperiod'.$i})) {
                          $duration = (int)$user->{'enrolperiod'.$i} * 60*60*24; // convert days to seconds
                          if ($duration > 0) { // sanity check
                          $timeend = $today + $manualcache[$courseid]->enrolperiod;
                      }
  
 -                    $manual->enrol_user($manualcache[$courseid], $user->id, $rid, $today, $timeend);
 +                    $manual->enrol_user($manualcache[$courseid], $user->id, $rid, $today, $timeend, $status);
  
                      $a = new stdClass();
                      $a->course = $shortname;