From f85800f30510eba1964088725dea1f635461b084 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 8 Dec 2016 12:27:05 +0800 Subject: [PATCH] MDL-55062 tool_uploaduser: prevent defaults when not selected --- admin/tool/uploaduser/index.php | 58 ++++++++++--------- .../uploaduser/lang/en/tool_uploaduser.php | 1 + admin/tool/uploaduser/user_form.php | 7 +++ 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/admin/tool/uploaduser/index.php b/admin/tool/uploaduser/index.php index ed5e303092a..d11332b23a4 100644 --- a/admin/tool/uploaduser/index.php +++ b/admin/tool/uploaduser/index.php @@ -358,38 +358,40 @@ if ($formdata = $mform2->is_cancelled()) { // add default values for remaining fields $formdefaults = array(); - foreach ($STD_FIELDS as $field) { - if (isset($user->$field)) { - continue; - } - // all validation moved to form2 - if (isset($formdata->$field)) { - // process templates - $user->$field = uu_process_template($formdata->$field, $user); - $formdefaults[$field] = true; - if (in_array($field, $upt->columns)) { - $upt->track($field, s($user->$field), 'normal'); + if ($updatetype != UU_UPDATE_FILEOVERRIDE && $updatetype != UU_UPDATE_NOCHANGES) { + foreach ($STD_FIELDS as $field) { + if (isset($user->$field)) { + continue; + } + // all validation moved to form2 + if (isset($formdata->$field)) { + // process templates + $user->$field = uu_process_template($formdata->$field, $user); + $formdefaults[$field] = true; + if (in_array($field, $upt->columns)) { + $upt->track($field, s($user->$field), 'normal'); + } } } - } - foreach ($PRF_FIELDS as $field) { - if (isset($user->$field)) { - continue; - } - 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); + foreach ($PRF_FIELDS as $field) { + if (isset($user->$field)) { + continue; } + if (isset($formdata->$field)) { + // process templates + $user->$field = uu_process_template($formdata->$field, $user); - $formdefaults[$field] = true; + // 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; + } } } diff --git a/admin/tool/uploaduser/lang/en/tool_uploaduser.php b/admin/tool/uploaduser/lang/en/tool_uploaduser.php index 9aafcfcf15d..d499660db0c 100644 --- a/admin/tool/uploaduser/lang/en/tool_uploaduser.php +++ b/admin/tool/uploaduser/lang/en/tool_uploaduser.php @@ -33,6 +33,7 @@ $string['deleteerrors'] = 'Delete errors'; $string['encoding'] = 'Encoding'; $string['errormnetadd'] = 'Can not add remote users'; $string['errors'] = 'Errors'; +$string['invalidupdatetype'] = 'You can not select this option with the chosen \'Upload type\''; $string['invaliduserdata'] = 'Invalid data detected for user {$a} and it has been automatically cleaned.'; $string['nochanges'] = 'No changes'; $string['pluginname'] = 'User upload'; diff --git a/admin/tool/uploaduser/user_form.php b/admin/tool/uploaduser/user_form.php index 29cfec03e97..c291a735299 100644 --- a/admin/tool/uploaduser/user_form.php +++ b/admin/tool/uploaduser/user_form.php @@ -350,6 +350,7 @@ class admin_uploaduser_form2 extends moodleform { $errors = parent::validation($data, $files); $columns = $this->_customdata['columns']; $optype = $data['uutype']; + $updatetype = $data['uuupdatetype']; // detect if password column needed in file if (!in_array('password', $columns)) { @@ -382,6 +383,12 @@ class admin_uploaduser_form2 extends moodleform { } } + // If the 'Existing user details' value is set we need to ensure that the + // 'Upload type' is not set to something invalid. + if (!empty($updatetype) && ($optype == UU_USER_ADDNEW || $optype == UU_USER_ADDINC)) { + $errors['uuupdatetype'] = get_string('invalidupdatetype', 'tool_uploaduser'); + } + // look for other required data if ($optype != UU_USER_UPDATE) { $requiredusernames = useredit_get_required_name_fields();