|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00026 defined('MOODLE_INTERNAL') || die(); 00027 00028 require_once $CFG->libdir.'/formslib.php'; 00029 00030 00037 class admin_uploaduser_form1 extends moodleform { 00038 function definition () { 00039 $mform = $this->_form; 00040 00041 $mform->addElement('header', 'settingsheader', get_string('upload')); 00042 00043 $mform->addElement('filepicker', 'userfile', get_string('file')); 00044 $mform->addRule('userfile', null, 'required'); 00045 00046 $choices = csv_import_reader::get_delimiter_list(); 00047 $mform->addElement('select', 'delimiter_name', get_string('csvdelimiter', 'tool_uploaduser'), $choices); 00048 if (array_key_exists('cfg', $choices)) { 00049 $mform->setDefault('delimiter_name', 'cfg'); 00050 } else if (get_string('listsep', 'langconfig') == ';') { 00051 $mform->setDefault('delimiter_name', 'semicolon'); 00052 } else { 00053 $mform->setDefault('delimiter_name', 'comma'); 00054 } 00055 00056 $textlib = textlib_get_instance(); 00057 $choices = $textlib->get_encodings(); 00058 $mform->addElement('select', 'encoding', get_string('encoding', 'tool_uploaduser'), $choices); 00059 $mform->setDefault('encoding', 'UTF-8'); 00060 00061 $choices = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000); 00062 $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'tool_uploaduser'), $choices); 00063 $mform->setType('previewrows', PARAM_INT); 00064 00065 $this->add_action_buttons(false, get_string('uploadusers', 'tool_uploaduser')); 00066 } 00067 } 00068 00069 00076 class admin_uploaduser_form2 extends moodleform { 00077 function definition () { 00078 global $CFG, $USER; 00079 00080 $mform = $this->_form; 00081 $columns = $this->_customdata['columns']; 00082 $data = $this->_customdata['data']; 00083 00084 // I am the template user, why should it be the administrator? we have roles now, other ppl may use this script ;-) 00085 $templateuser = $USER; 00086 00087 // upload settings and file 00088 $mform->addElement('header', 'settingsheader', get_string('settings')); 00089 00090 $choices = array(UU_USER_ADDNEW => get_string('uuoptype_addnew', 'tool_uploaduser'), 00091 UU_USER_ADDINC => get_string('uuoptype_addinc', 'tool_uploaduser'), 00092 UU_USER_ADD_UPDATE => get_string('uuoptype_addupdate', 'tool_uploaduser'), 00093 UU_USER_UPDATE => get_string('uuoptype_update', 'tool_uploaduser')); 00094 $mform->addElement('select', 'uutype', get_string('uuoptype', 'tool_uploaduser'), $choices); 00095 00096 $choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth')); 00097 $mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'tool_uploaduser'), $choices); 00098 $mform->setDefault('uupasswordnew', 1); 00099 $mform->disabledIf('uupasswordnew', 'uutype', 'eq', UU_USER_UPDATE); 00100 00101 $choices = array(UU_UPDATE_NOCHANGES => get_string('nochanges', 'tool_uploaduser'), 00102 UU_UPDATE_FILEOVERRIDE => get_string('uuupdatefromfile', 'tool_uploaduser'), 00103 UU_UPDATE_ALLOVERRIDE => get_string('uuupdateall', 'tool_uploaduser'), 00104 UU_UPDATE_MISSING => get_string('uuupdatemissing', 'tool_uploaduser')); 00105 $mform->addElement('select', 'uuupdatetype', get_string('uuupdatetype', 'tool_uploaduser'), $choices); 00106 $mform->setDefault('uuupdatetype', UU_UPDATE_NOCHANGES); 00107 $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDNEW); 00108 $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDINC); 00109 00110 $choices = array(0 => get_string('nochanges', 'tool_uploaduser'), 1 => get_string('update')); 00111 $mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'tool_uploaduser'), $choices); 00112 $mform->setDefault('uupasswordold', 0); 00113 $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDNEW); 00114 $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDINC); 00115 $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 0); 00116 $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 3); 00117 00118 $choices = array(UU_PWRESET_WEAK => get_string('usersweakpassword', 'tool_uploaduser'), 00119 UU_PWRESET_NONE => get_string('none'), 00120 UU_PWRESET_ALL => get_string('all')); 00121 if (empty($CFG->passwordpolicy)) { 00122 unset($choices[UU_PWRESET_WEAK]); 00123 } 00124 $mform->addElement('select', 'uuforcepasswordchange', get_string('forcepasswordchange', 'core'), $choices); 00125 00126 00127 $mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'tool_uploaduser')); 00128 $mform->setDefault('uuallowrenames', 0); 00129 $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDNEW); 00130 $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDINC); 00131 00132 $mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'tool_uploaduser')); 00133 $mform->setDefault('uuallowdeletes', 0); 00134 $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDNEW); 00135 $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDINC); 00136 00137 $mform->addElement('selectyesno', 'uuallowsuspends', get_string('allowsuspends', 'tool_uploaduser')); 00138 $mform->setDefault('uuallowsuspends', 1); 00139 $mform->disabledIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDNEW); 00140 $mform->disabledIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDINC); 00141 00142 $mform->addElement('selectyesno', 'uunoemailduplicates', get_string('uunoemailduplicates', 'tool_uploaduser')); 00143 $mform->setDefault('uunoemailduplicates', 1); 00144 00145 $mform->addElement('selectyesno', 'uustandardusernames', get_string('uustandardusernames', 'tool_uploaduser')); 00146 $mform->setDefault('uustandardusernames', 1); 00147 00148 $choices = array(UU_BULK_NONE => get_string('no'), 00149 UU_BULK_NEW => get_string('uubulknew', 'tool_uploaduser'), 00150 UU_BULK_UPDATED => get_string('uubulkupdated', 'tool_uploaduser'), 00151 UU_BULK_ALL => get_string('uubulkall', 'tool_uploaduser')); 00152 $mform->addElement('select', 'uubulk', get_string('uubulk', 'tool_uploaduser'), $choices); 00153 $mform->setDefault('uubulk', 0); 00154 00155 // roles selection 00156 $showroles = false; 00157 foreach ($columns as $column) { 00158 if (preg_match('/^type\d+$/', $column)) { 00159 $showroles = true; 00160 break; 00161 } 00162 } 00163 if ($showroles) { 00164 $mform->addElement('header', 'rolesheader', get_string('roles')); 00165 00166 $choices = uu_allowed_roles(true); 00167 00168 $mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'tool_uploaduser'), $choices); 00169 if ($studentroles = get_archetype_roles('student')) { 00170 foreach ($studentroles as $role) { 00171 if (isset($choices[$role->id])) { 00172 $mform->setDefault('uulegacy1', $role->id); 00173 break; 00174 } 00175 } 00176 unset($studentroles); 00177 } 00178 00179 $mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'tool_uploaduser'), $choices); 00180 if ($editteacherroles = get_archetype_roles('editingteacher')) { 00181 foreach ($editteacherroles as $role) { 00182 if (isset($choices[$role->id])) { 00183 $mform->setDefault('uulegacy2', $role->id); 00184 break; 00185 } 00186 } 00187 unset($editteacherroles); 00188 } 00189 00190 $mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'tool_uploaduser'), $choices); 00191 if ($teacherroles = get_archetype_roles('teacher')) { 00192 foreach ($teacherroles as $role) { 00193 if (isset($choices[$role->id])) { 00194 $mform->setDefault('uulegacy3', $role->id); 00195 break; 00196 } 00197 } 00198 unset($teacherroles); 00199 } 00200 } 00201 00202 // default values 00203 $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'tool_uploaduser')); 00204 00205 $mform->addElement('text', 'username', get_string('uuusernametemplate', 'tool_uploaduser'), 'size="20"'); 00206 $mform->addRule('username', get_string('requiredtemplate', 'tool_uploaduser'), 'required', null, 'client'); 00207 $mform->disabledIf('username', 'uutype', 'eq', UU_USER_ADD_UPDATE); 00208 $mform->disabledIf('username', 'uutype', 'eq', UU_USER_UPDATE); 00209 00210 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"'); 00211 $mform->disabledIf('email', 'uutype', 'eq', UU_USER_ADD_UPDATE); 00212 $mform->disabledIf('email', 'uutype', 'eq', UU_USER_UPDATE); 00213 00214 // only enabled and known to work plugins 00215 $choices = uu_supported_auths(); 00216 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices); 00217 $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default 00218 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth'); 00219 $mform->setAdvanced('auth'); 00220 00221 $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse')); 00222 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices); 00223 $mform->setDefault('maildisplay', 2); 00224 00225 $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat')); 00226 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices); 00227 $mform->setDefault('mailformat', 1); 00228 $mform->setAdvanced('mailformat'); 00229 00230 $choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects')); 00231 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices); 00232 $mform->setDefault('maildigest', 0); 00233 $mform->setAdvanced('maildigest'); 00234 00235 $choices = array(1 => get_string('autosubscribeyes'), 0 => get_string('autosubscribeno')); 00236 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices); 00237 $mform->setDefault('autosubscribe', 1); 00238 00239 $editors = editors_get_enabled(); 00240 if (count($editors) > 1) { 00241 $choices = array(); 00242 $choices['0'] = get_string('texteditor'); 00243 $choices['1'] = get_string('htmleditor'); 00244 $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices); 00245 $mform->setDefault('htmleditor', 1); 00246 } else { 00247 $mform->addElement('hidden', 'htmleditor'); 00248 $mform->setDefault('htmleditor', 1); 00249 $mform->setType('htmleditor', PARAM_INT); 00250 } 00251 00252 if (empty($CFG->enableajax)) { 00253 $mform->addElement('static', 'ajax', get_string('ajaxuse'), get_string('ajaxno')); 00254 } else { 00255 $choices = array( 0 => get_string('ajaxno'), 1 => get_string('ajaxyes')); 00256 $mform->addElement('select', 'ajax', get_string('ajaxuse'), $choices); 00257 $mform->setDefault('ajax', 1); 00258 } 00259 $mform->setAdvanced('ajax'); 00260 00261 $mform->addElement('text', 'city', get_string('city'), 'maxlength="100" size="25"'); 00262 $mform->setType('city', PARAM_MULTILANG); 00263 if (empty($CFG->defaultcity)) { 00264 $mform->setDefault('city', $templateuser->city); 00265 } else { 00266 $mform->setDefault('city', $CFG->defaultcity); 00267 } 00268 $mform->addRule('city', get_string('required'), 'required'); 00269 00270 $mform->addElement('select', 'country', get_string('selectacountry'), get_string_manager()->get_list_of_countries()); 00271 if (empty($CFG->country)) { 00272 $mform->setDefault('country', $templateuser->country); 00273 } else { 00274 $mform->setDefault('country', $CFG->country); 00275 } 00276 $mform->setAdvanced('country'); 00277 00278 $choices = get_list_of_timezones(); 00279 $choices['99'] = get_string('serverlocaltime'); 00280 $mform->addElement('select', 'timezone', get_string('timezone'), $choices); 00281 $mform->setDefault('timezone', $templateuser->timezone); 00282 $mform->setAdvanced('timezone'); 00283 00284 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations()); 00285 $mform->setDefault('lang', $templateuser->lang); 00286 $mform->setAdvanced('lang'); 00287 00288 $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false); 00289 $mform->addElement('editor', 'description', get_string('userdescription'), null, $editoroptions); 00290 $mform->setType('description', PARAM_CLEANHTML); 00291 $mform->addHelpButton('description', 'userdescription'); 00292 $mform->setAdvanced('description'); 00293 00294 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"'); 00295 $mform->setAdvanced('url'); 00296 00297 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="64" size="25"'); 00298 $mform->setType('idnumber', PARAM_NOTAGS); 00299 00300 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"'); 00301 $mform->setType('institution', PARAM_MULTILANG); 00302 $mform->setDefault('institution', $templateuser->institution); 00303 00304 $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"'); 00305 $mform->setType('department', PARAM_MULTILANG); 00306 $mform->setDefault('department', $templateuser->department); 00307 00308 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"'); 00309 $mform->setType('phone1', PARAM_NOTAGS); 00310 $mform->setAdvanced('phone1'); 00311 00312 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"'); 00313 $mform->setType('phone2', PARAM_NOTAGS); 00314 $mform->setAdvanced('phone2'); 00315 00316 $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"'); 00317 $mform->setType('address', PARAM_MULTILANG); 00318 $mform->setAdvanced('address'); 00319 00320 // Next the profile defaults 00321 profile_definition($mform); 00322 00323 // hidden fields 00324 $mform->addElement('hidden', 'iid'); 00325 $mform->setType('iid', PARAM_INT); 00326 00327 $mform->addElement('hidden', 'previewrows'); 00328 $mform->setType('previewrows', PARAM_INT); 00329 00330 $this->add_action_buttons(true, get_string('uploadusers', 'tool_uploaduser')); 00331 00332 $this->set_data($data); 00333 } 00334 00338 function definition_after_data() { 00339 $mform = $this->_form; 00340 $columns = $this->_customdata['columns']; 00341 00342 foreach ($columns as $column) { 00343 if ($mform->elementExists($column)) { 00344 $mform->removeElement($column); 00345 } 00346 } 00347 00348 if (!in_array('password', $columns)) { 00349 // password resetting makes sense only if password specified in csv file 00350 if ($mform->elementExists('uuforcepasswordchange')) { 00351 $mform->removeElement('uuforcepasswordchange'); 00352 } 00353 } 00354 } 00355 00359 function validation($data, $files) { 00360 $errors = parent::validation($data, $files); 00361 $columns = $this->_customdata['columns']; 00362 $optype = $data['uutype']; 00363 00364 // detect if password column needed in file 00365 if (!in_array('password', $columns)) { 00366 switch ($optype) { 00367 case UU_USER_UPDATE: 00368 if (!empty($data['uupasswordold'])) { 00369 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password'); 00370 } 00371 break; 00372 00373 case UU_USER_ADD_UPDATE: 00374 if (empty($data['uupasswordnew'])) { 00375 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password'); 00376 } 00377 if (!empty($data['uupasswordold'])) { 00378 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password'); 00379 } 00380 break; 00381 00382 case UU_USER_ADDNEW: 00383 if (empty($data['uupasswordnew'])) { 00384 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password'); 00385 } 00386 break; 00387 case UU_USER_ADDINC: 00388 if (empty($data['uupasswordnew'])) { 00389 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password'); 00390 } 00391 break; 00392 } 00393 } 00394 00395 // look for other required data 00396 if ($optype != UU_USER_UPDATE) { 00397 if (!in_array('firstname', $columns)) { 00398 $errors['uutype'] = get_string('missingfield', 'error', 'firstname'); 00399 } 00400 00401 if (!in_array('lastname', $columns)) { 00402 if (isset($errors['uutype'])) { 00403 $errors['uutype'] = ''; 00404 } else { 00405 $errors['uutype'] = ' '; 00406 } 00407 $errors['uutype'] .= get_string('missingfield', 'error', 'lastname'); 00408 } 00409 00410 if (!in_array('email', $columns) and empty($data['email'])) { 00411 $errors['email'] = get_string('requiredtemplate', 'tool_uploaduser'); 00412 } 00413 } 00414 00415 return $errors; 00416 } 00417 00423 function get_data() { 00424 $data = parent::get_data(); 00425 00426 if ($data !== null and isset($data->description)) { 00427 $data->descriptionformat = $data->description['format']; 00428 $data->description = $data->description['text']; 00429 } 00430 00431 return $data; 00432 } 00433 }