Example #1
0
/**
 * Increments username - increments trailing number or adds it if not present.
 * Varifies that the new username does not exist yet
 * @param string $username
 * @return incremented username which does not exist yet
 */
function uu_increment_username($username)
{
    global $DB, $CFG;
    if (!preg_match_all('/(.*?)([0-9]+)$/', $username, $matches)) {
        $username = $username . '2';
    } else {
        $username = $matches[1][0] . ($matches[2][0] + 1);
    }
    if ($DB->record_exists('user', array('username' => $username, 'mnethostid' => $CFG->mnet_localhost_id))) {
        return uu_increment_username($username);
    } else {
        return $username;
    }
}
Example #2
0
     $upt->track('username', $errorstr, 'error');
     $userserrors++;
     continue;
 } else {
     if ($user->username === 'guest') {
         $upt->track('status', get_string('guestnoeditprofileother', 'error'), 'error');
         $userserrors++;
         continue;
     }
 }
 if ($existinguser = $DB->get_record('user', array('username' => $user->username, 'mnethostid' => $CFG->mnet_localhost_id))) {
     $upt->track('id', $existinguser->id, 'normal', false);
 }
 // find out in username incrementing required
 if ($existinguser and $optype == UU_USER_ADDINC) {
     $user->username = uu_increment_username($user->username);
     $existinguser = false;
 }
 // notify about nay username changes
 if ($originalusername !== $user->username) {
     $upt->track('username', '', 'normal', false);
     // clear previous
     $upt->track('username', s($originalusername) . '-->' . s($user->username), 'info');
 } else {
     $upt->track('username', s($user->username), 'normal', false);
 }
 // add default values for remaining fields
 $formdefaults = array();
 foreach ($STD_FIELDS as $field) {
     if (isset($user->{$field})) {
         continue;
 /**
  * Validates and prepares the data.
  *
  * @return bool false is any error occured.
  */
 public function prepare()
 {
     global $DB, $CFG;
     global $UUC_DEFAULTS;
     $this->prepared = true;
     // Standardise username.
     if ($this->importoptions['standardise']) {
         $this->rawdata['username'] = clean_param($this->rawdata['username'], PARAM_USERNAME);
     }
     // Validate username format
     if ($this->rawdata['username'] !== clean_param($this->rawdata['username'], PARAM_USERNAME)) {
         $this->error('invalidusername', new lang_string('invalidusername', 'error', 'username'));
         return false;
     }
     // Validate moodle net host id.
     if (empty($this->rawdata['mnethostid'])) {
         $this->rawdata['mnethostid'] = $CFG->mnet_localhost_id;
     } else {
         if (!is_numeric($this->rawdata['mnethostid'])) {
             $this->error('mnethostidnotanumber', new lang_string('mnethostidnotanumber', 'tool_uploadusercli'));
             return false;
         }
     }
     if ($this->rawdata['mnethostid'] != $CFG->mnet_localhost_id) {
         $this->isremote = true;
     } else {
         $this->isremote = false;
     }
     $this->existing = $this->exists();
     // Can we delete the user? We only need username for deletion.
     if (!empty($this->options['deleted'])) {
         if (empty($this->existing)) {
             $this->error('usernotdeletedmissing', new lang_string('usernotdeletedmissing', 'error'));
             return false;
         } else {
             if (!$this->can_delete()) {
                 $this->error('usernotdeletedoff', new lang_string('usernotdeletedoff', 'error'));
                 return false;
             } else {
                 if ($this->isremote) {
                     $this->error('usernotdeletedremote', new lang_string('usernotdeletedremote', 'tool_uploadusercli'));
                     return false;
                 } else {
                     if (is_siteadmin($this->existing->id)) {
                         $this->error('usernotdeletedadmin', new lang_string('usernotdeletedadmin', 'error'));
                         return false;
                     } else {
                         if ($this->rawdata['username'] === 'guest') {
                             $this->error('usernotdeletedguest', new lang_string('usernotdeletedguest', 'tool_uploadusercli'));
                             return false;
                         }
                     }
                 }
             }
         }
         $this->do = self::DO_DELETE;
         return true;
     }
     // Validate id field.
     if (isset($this->rawdata['id']) && !is_numeric($this->rawdata['id'])) {
         $this->error('useridnotanumber', new lang_string('useridnotanumber', 'tool_uploadusercli'));
         return false;
     }
     // Checking mandatory fields.
     foreach (self::$mandatoryfields as $key => $field) {
         if (!isset($this->rawdata[$field])) {
             $this->error('missingfield', new lang_string('missingfield', 'error', $field));
             return false;
         }
     }
     // Can we create/update the user under those conditions?
     if ($this->existing) {
         if (!$this->can_update() && $this->mode != tool_uploadusercli_processor::MODE_CREATE_ALL) {
             $this->error('usernotupdatedoff', new lang_string('usernotupdatedoff', 'tool_uploadusercli'));
             return false;
         }
     } else {
         // If I cannot create the course.
         if (!$this->can_create() && !isset($this->rawdata['oldusername'])) {
             if ($this->isremote) {
                 $this->error('usernotcreatedremote', new lang_string('usernotcreatedremote', 'tool_uploadusercli'));
             } else {
                 $this->error('usernotcreatedoff', new lang_string('usernotcreatedoff', 'tool_uploadusercli'));
             }
             return false;
         }
         // If I'm in update only mode and I'm not renaming.
         if ($this->mode === tool_uploadusercli_processor::MODE_UPDATE_ONLY && !isset($this->rawdata['oldusername'])) {
             $this->error('usernotcreatedoff', new lang_string('usernotcreatedoff', 'tool_uploadusercli'));
             return false;
         }
     }
     // Preparing final user data.
     $finaldata = new stdClass();
     foreach ($this->rawdata as $field => $value) {
         if ($this->isremote) {
             // Remote users can only be suspended or enrolled.
             if ($field === 'suspended') {
                 $finaldata->{$field} = trim($value);
             } else {
                 if (in_array($field, $UUC_STD_FIELDS) || in_array($field, $UUC_PRF_FIELDS)) {
                     $finaldata->{$field} = $this->existing->{$field};
                     $this->rawdata[$field] = $this->existing->{$field};
                 } else {
                     $finaldata->{$field} = trim($value);
                 }
             }
         } else {
             $finaldata->{$field} = trim($value);
         }
     }
     // Can the user be renamed?
     if (!empty($finaldata->oldusername)) {
         if ($this->existing) {
             $this->error('usernotrenamedexists', new lang_string('usernotrenamedexists', 'error'));
             return false;
         }
         $oldusername = $finaldata->oldusername;
         if ($this->importoptions['standardise']) {
             $oldusername = clean_param($oldusername, PARAM_USERNAME);
         }
         $this->existing = $this->exists($oldusername);
         if (!$this->can_update()) {
             $this->error('usernotupdatederror', new lang_string('usernotupdatederror', 'error'));
             return false;
         } else {
             if (!$this->existing) {
                 $this->error('usernotrenamedmissing', new lang_string('usernotrenamedmissing', 'error'));
                 return false;
             } else {
                 if (!$this->can_rename()) {
                     $this->error('usernotrenamedoff', new lang_string('usernotrenamedoff', 'error'));
                     return false;
                 }
             }
         }
         $this->do = self::DO_UPDATE;
         $this->set_status('userrenamed', new lang_string('userrenamed', 'tool_uploadusercli', array('from' => $oldusername, 'to' => $finaldata->username)));
     }
     // Do not update admin and guest account through the csv.
     if ($this->existing) {
         if (is_siteadmin($this->existing)) {
             $this->error('usernotupdatedadmin', new lang_string('usernotupdatedadmin', 'error'));
             return false;
         } else {
             if ($this->existing->username === 'guest') {
                 $this->error('guestnoeditprofileother', new lang_string('guestnoeditprofileother', 'error'));
                 return false;
             }
         }
     }
     // If exists, but we only want to create users, increment the username.
     if ($this->existing && $this->mode === tool_uploadusercli_processor::MODE_CREATE_ALL) {
         $original = $finaldata->username;
         $finaldata->username = uu_increment_username($finaldata->username);
         // We are creating a new user.
         $this->existing = NULL;
         if ($finaldata->username !== $original) {
             $this->set_status('userrenamed', new lang_string('userrenamed', 'tool_uploadusercli', array('from' => $original, 'to' => $this->name)));
         }
     }
     // Ultimate check mode vs. existence.
     switch ($this->mode) {
         case tool_uploadusercli_processor::MODE_CREATE_NEW:
             if ($this->existing) {
                 $this->error('usernotaddedregistered', new lang_string('usernotaddedregistered', 'error'));
                 return false;
             }
             break;
         case tool_uploadusercli_processor::MODE_CREATE_ALL:
             // This should not happen, we set existing to NULL when we increment.
             if ($this->existing) {
                 $this->error('usernotaddederror', new lang_string('usernotaddederror', 'error'));
                 return false;
             }
             break;
         case tool_uploadusercli_processor::MODE_UPDATE_ONLY:
             if (!$this->existing) {
                 $this->error('usernotcreatedoff', new lang_string('usernotcreatedoff', 'tool_uploadusercli'));
                 return false;
             }
             break;
         case tool_uploadusercli_processor::MODE_CREATE_OR_UPDATE:
             if ($this->existing) {
                 if ($this->updatemode === tool_uploadusercli_processor::UPDATE_NOTHING) {
                     $this->error('updatemodedoessettonothing', new lang_string('updatemodedoessettonothing', 'tool_uploadusercli'));
                     return false;
                 }
             }
             break;
         default:
             // O_o Huh?! This should really never happen here!
             $this->error('unknownuploadmode', new lang_string('unknownuploadmode', 'tool_uploadusercli'));
             return false;
     }
     // Get final data.
     if ($this->existing) {
         $missingonly = $this->updatemode === tool_uploadusercli_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAULTS;
         $finaldata = $this->get_final_update_data($finaldata, $this->existing, $UUC_DEFAULTS, $missingonly);
         if (!$finaldata) {
             $this->error('usernotupdatederror', new lang_string('usernotupdatederror', 'error'));
             return false;
         } else {
             $this->do = self::DO_UPDATE;
         }
     } else {
         $finaldata = $this->get_final_create_data($finaldata);
         if (!$finaldata) {
             $this->error('usernotaddederror', new lang_string('usernotaddederror', 'error'));
             return false;
         } else {
             $this->do = self::DO_CREATE;
         }
     }
     // Saving data.
     $this->finaldata = $finaldata;
     return true;
 }