PageRenderTime 144ms CodeModel.GetById 41ms RepoModel.GetById 16ms app.codeStats 0ms

/admin/tool/uploaduser/user_form.php

http://github.com/moodle/moodle
PHP | 437 lines | 309 code | 71 blank | 57 comment | 42 complexity | 8af03484b68aa13107edc2280ec7abf4 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Bulk user upload forms
  18. *
  19. * @package tool
  20. * @subpackage uploaduser
  21. * @copyright 2007 Dan Poltawski
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. require_once $CFG->libdir.'/formslib.php';
  26. require_once($CFG->dirroot . '/user/editlib.php');
  27. /**
  28. * Upload a file CVS file with user information.
  29. *
  30. * @copyright 2007 Petr Skoda {@link http://skodak.org}
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class admin_uploaduser_form1 extends moodleform {
  34. function definition () {
  35. $mform = $this->_form;
  36. $mform->addElement('header', 'settingsheader', get_string('upload'));
  37. $url = new moodle_url('example.csv');
  38. $link = html_writer::link($url, 'example.csv');
  39. $mform->addElement('static', 'examplecsv', get_string('examplecsv', 'tool_uploaduser'), $link);
  40. $mform->addHelpButton('examplecsv', 'examplecsv', 'tool_uploaduser');
  41. $mform->addElement('filepicker', 'userfile', get_string('file'));
  42. $mform->addRule('userfile', null, 'required');
  43. $choices = csv_import_reader::get_delimiter_list();
  44. $mform->addElement('select', 'delimiter_name', get_string('csvdelimiter', 'tool_uploaduser'), $choices);
  45. if (array_key_exists('cfg', $choices)) {
  46. $mform->setDefault('delimiter_name', 'cfg');
  47. } else if (get_string('listsep', 'langconfig') == ';') {
  48. $mform->setDefault('delimiter_name', 'semicolon');
  49. } else {
  50. $mform->setDefault('delimiter_name', 'comma');
  51. }
  52. $choices = core_text::get_encodings();
  53. $mform->addElement('select', 'encoding', get_string('encoding', 'tool_uploaduser'), $choices);
  54. $mform->setDefault('encoding', 'UTF-8');
  55. $choices = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
  56. $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'tool_uploaduser'), $choices);
  57. $mform->setType('previewrows', PARAM_INT);
  58. $this->add_action_buttons(false, get_string('uploadusers', 'tool_uploaduser'));
  59. }
  60. }
  61. /**
  62. * Specify user upload details
  63. *
  64. * @copyright 2007 Petr Skoda {@link http://skodak.org}
  65. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  66. */
  67. class admin_uploaduser_form2 extends moodleform {
  68. function definition () {
  69. global $CFG, $USER;
  70. $mform = $this->_form;
  71. $columns = $this->_customdata['columns'];
  72. $data = $this->_customdata['data'];
  73. // I am the template user, why should it be the administrator? we have roles now, other ppl may use this script ;-)
  74. $templateuser = $USER;
  75. // upload settings and file
  76. $mform->addElement('header', 'settingsheader', get_string('settings'));
  77. $choices = array(UU_USER_ADDNEW => get_string('uuoptype_addnew', 'tool_uploaduser'),
  78. UU_USER_ADDINC => get_string('uuoptype_addinc', 'tool_uploaduser'),
  79. UU_USER_ADD_UPDATE => get_string('uuoptype_addupdate', 'tool_uploaduser'),
  80. UU_USER_UPDATE => get_string('uuoptype_update', 'tool_uploaduser'));
  81. $mform->addElement('select', 'uutype', get_string('uuoptype', 'tool_uploaduser'), $choices);
  82. $choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth'));
  83. $mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'tool_uploaduser'), $choices);
  84. $mform->setDefault('uupasswordnew', 1);
  85. $mform->hideIf('uupasswordnew', 'uutype', 'eq', UU_USER_UPDATE);
  86. $choices = array(UU_UPDATE_NOCHANGES => get_string('nochanges', 'tool_uploaduser'),
  87. UU_UPDATE_FILEOVERRIDE => get_string('uuupdatefromfile', 'tool_uploaduser'),
  88. UU_UPDATE_ALLOVERRIDE => get_string('uuupdateall', 'tool_uploaduser'),
  89. UU_UPDATE_MISSING => get_string('uuupdatemissing', 'tool_uploaduser'));
  90. $mform->addElement('select', 'uuupdatetype', get_string('uuupdatetype', 'tool_uploaduser'), $choices);
  91. $mform->setDefault('uuupdatetype', UU_UPDATE_NOCHANGES);
  92. $mform->hideIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDNEW);
  93. $mform->hideIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDINC);
  94. $choices = array(0 => get_string('nochanges', 'tool_uploaduser'), 1 => get_string('update'));
  95. $mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'tool_uploaduser'), $choices);
  96. $mform->setDefault('uupasswordold', 0);
  97. $mform->hideIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDNEW);
  98. $mform->hideIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDINC);
  99. $mform->hideIf('uupasswordold', 'uuupdatetype', 'eq', 0);
  100. $mform->hideIf('uupasswordold', 'uuupdatetype', 'eq', 3);
  101. $choices = array(UU_PWRESET_WEAK => get_string('usersweakpassword', 'tool_uploaduser'),
  102. UU_PWRESET_NONE => get_string('none'),
  103. UU_PWRESET_ALL => get_string('all'));
  104. if (empty($CFG->passwordpolicy)) {
  105. unset($choices[UU_PWRESET_WEAK]);
  106. }
  107. $mform->addElement('select', 'uuforcepasswordchange', get_string('forcepasswordchange', 'core'), $choices);
  108. $mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'tool_uploaduser'));
  109. $mform->setDefault('uuallowrenames', 0);
  110. $mform->hideIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDNEW);
  111. $mform->hideIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDINC);
  112. $mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'tool_uploaduser'));
  113. $mform->setDefault('uuallowdeletes', 0);
  114. $mform->hideIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDNEW);
  115. $mform->hideIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDINC);
  116. $mform->addElement('selectyesno', 'uuallowsuspends', get_string('allowsuspends', 'tool_uploaduser'));
  117. $mform->setDefault('uuallowsuspends', 1);
  118. $mform->hideIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDNEW);
  119. $mform->hideIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDINC);
  120. if (!empty($CFG->allowaccountssameemail)) {
  121. $mform->addElement('selectyesno', 'uunoemailduplicates', get_string('uunoemailduplicates', 'tool_uploaduser'));
  122. $mform->setDefault('uunoemailduplicates', 1);
  123. } else {
  124. $mform->addElement('hidden', 'uunoemailduplicates', 1);
  125. }
  126. $mform->setType('uunoemailduplicates', PARAM_BOOL);
  127. $mform->addElement('selectyesno', 'uustandardusernames', get_string('uustandardusernames', 'tool_uploaduser'));
  128. $mform->setDefault('uustandardusernames', 1);
  129. $choices = array(UU_BULK_NONE => get_string('no'),
  130. UU_BULK_NEW => get_string('uubulknew', 'tool_uploaduser'),
  131. UU_BULK_UPDATED => get_string('uubulkupdated', 'tool_uploaduser'),
  132. UU_BULK_ALL => get_string('uubulkall', 'tool_uploaduser'));
  133. $mform->addElement('select', 'uubulk', get_string('uubulk', 'tool_uploaduser'), $choices);
  134. $mform->setDefault('uubulk', 0);
  135. // roles selection
  136. $showroles = false;
  137. foreach ($columns as $column) {
  138. if (preg_match('/^type\d+$/', $column)) {
  139. $showroles = true;
  140. break;
  141. }
  142. }
  143. if ($showroles) {
  144. $mform->addElement('header', 'rolesheader', get_string('roles'));
  145. $choices = uu_allowed_roles(true);
  146. $mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'tool_uploaduser'), $choices);
  147. if ($studentroles = get_archetype_roles('student')) {
  148. foreach ($studentroles as $role) {
  149. if (isset($choices[$role->id])) {
  150. $mform->setDefault('uulegacy1', $role->id);
  151. break;
  152. }
  153. }
  154. unset($studentroles);
  155. }
  156. $mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'tool_uploaduser'), $choices);
  157. if ($editteacherroles = get_archetype_roles('editingteacher')) {
  158. foreach ($editteacherroles as $role) {
  159. if (isset($choices[$role->id])) {
  160. $mform->setDefault('uulegacy2', $role->id);
  161. break;
  162. }
  163. }
  164. unset($editteacherroles);
  165. }
  166. $mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'tool_uploaduser'), $choices);
  167. if ($teacherroles = get_archetype_roles('teacher')) {
  168. foreach ($teacherroles as $role) {
  169. if (isset($choices[$role->id])) {
  170. $mform->setDefault('uulegacy3', $role->id);
  171. break;
  172. }
  173. }
  174. unset($teacherroles);
  175. }
  176. }
  177. // default values
  178. $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'tool_uploaduser'));
  179. $mform->addElement('text', 'username', get_string('uuusernametemplate', 'tool_uploaduser'), 'size="20"');
  180. $mform->setType('username', PARAM_RAW); // No cleaning here. The process verifies it later.
  181. $mform->addRule('username', get_string('requiredtemplate', 'tool_uploaduser'), 'required', null, 'client');
  182. $mform->hideIf('username', 'uutype', 'eq', UU_USER_ADD_UPDATE);
  183. $mform->hideIf('username', 'uutype', 'eq', UU_USER_UPDATE);
  184. $mform->setForceLtr('username');
  185. $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
  186. $mform->setType('email', PARAM_RAW); // No cleaning here. The process verifies it later.
  187. $mform->hideIf('email', 'uutype', 'eq', UU_USER_ADD_UPDATE);
  188. $mform->hideIf('email', 'uutype', 'eq', UU_USER_UPDATE);
  189. $mform->setForceLtr('email');
  190. // only enabled and known to work plugins
  191. $choices = uu_supported_auths();
  192. $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices);
  193. $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default
  194. $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
  195. $mform->setAdvanced('auth');
  196. $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse'));
  197. $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
  198. $mform->setDefault('maildisplay', core_user::get_property_default('maildisplay'));
  199. $mform->addHelpButton('maildisplay', 'emaildisplay');
  200. $choices = array(0 => get_string('emailenable'), 1 => get_string('emaildisable'));
  201. $mform->addElement('select', 'emailstop', get_string('emailstop'), $choices);
  202. $mform->setDefault('emailstop', core_user::get_property_default('emailstop'));
  203. $mform->setAdvanced('emailstop');
  204. $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat'));
  205. $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
  206. $mform->setDefault('mailformat', core_user::get_property_default('mailformat'));
  207. $mform->setAdvanced('mailformat');
  208. $choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects'));
  209. $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
  210. $mform->setDefault('maildigest', core_user::get_property_default('maildigest'));
  211. $mform->setAdvanced('maildigest');
  212. $choices = array(1 => get_string('autosubscribeyes'), 0 => get_string('autosubscribeno'));
  213. $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
  214. $mform->setDefault('autosubscribe', core_user::get_property_default('autosubscribe'));
  215. $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="25"');
  216. $mform->setType('city', PARAM_TEXT);
  217. if (empty($CFG->defaultcity)) {
  218. $mform->setDefault('city', $templateuser->city);
  219. } else {
  220. $mform->setDefault('city', core_user::get_property_default('city'));
  221. }
  222. $choices = get_string_manager()->get_list_of_countries();
  223. $choices = array(''=>get_string('selectacountry').'...') + $choices;
  224. $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
  225. if (empty($CFG->country)) {
  226. $mform->setDefault('country', $templateuser->country);
  227. } else {
  228. $mform->setDefault('country', core_user::get_property_default('country'));
  229. }
  230. $mform->setAdvanced('country');
  231. $choices = core_date::get_list_of_timezones($templateuser->timezone, true);
  232. $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
  233. $mform->setDefault('timezone', $templateuser->timezone);
  234. $mform->setAdvanced('timezone');
  235. $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
  236. $mform->setDefault('lang', $templateuser->lang);
  237. $mform->setAdvanced('lang');
  238. $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false);
  239. $mform->addElement('editor', 'description', get_string('userdescription'), null, $editoroptions);
  240. $mform->setType('description', PARAM_CLEANHTML);
  241. $mform->addHelpButton('description', 'userdescription');
  242. $mform->setAdvanced('description');
  243. $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
  244. $mform->setType('url', PARAM_URL);
  245. $mform->setAdvanced('url');
  246. $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
  247. $mform->setType('idnumber', PARAM_NOTAGS);
  248. $mform->setForceLtr('idnumber');
  249. $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"');
  250. $mform->setType('institution', PARAM_TEXT);
  251. $mform->setDefault('institution', $templateuser->institution);
  252. $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"');
  253. $mform->setType('department', PARAM_TEXT);
  254. $mform->setDefault('department', $templateuser->department);
  255. $mform->addElement('text', 'phone1', get_string('phone1'), 'maxlength="20" size="25"');
  256. $mform->setType('phone1', PARAM_NOTAGS);
  257. $mform->setAdvanced('phone1');
  258. $mform->setForceLtr('phone1');
  259. $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
  260. $mform->setType('phone2', PARAM_NOTAGS);
  261. $mform->setAdvanced('phone2');
  262. $mform->setForceLtr('phone2');
  263. $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"');
  264. $mform->setType('address', PARAM_TEXT);
  265. $mform->setAdvanced('address');
  266. // Next the profile defaults
  267. profile_definition($mform);
  268. // hidden fields
  269. $mform->addElement('hidden', 'iid');
  270. $mform->setType('iid', PARAM_INT);
  271. $mform->addElement('hidden', 'previewrows');
  272. $mform->setType('previewrows', PARAM_INT);
  273. $this->add_action_buttons(true, get_string('uploadusers', 'tool_uploaduser'));
  274. $this->set_data($data);
  275. }
  276. /**
  277. * Form tweaks that depend on current data.
  278. */
  279. function definition_after_data() {
  280. $mform = $this->_form;
  281. $columns = $this->_customdata['columns'];
  282. foreach ($columns as $column) {
  283. if ($mform->elementExists($column)) {
  284. $mform->removeElement($column);
  285. }
  286. }
  287. if (!in_array('password', $columns)) {
  288. // password resetting makes sense only if password specified in csv file
  289. if ($mform->elementExists('uuforcepasswordchange')) {
  290. $mform->removeElement('uuforcepasswordchange');
  291. }
  292. }
  293. }
  294. /**
  295. * Server side validation.
  296. */
  297. function validation($data, $files) {
  298. $errors = parent::validation($data, $files);
  299. $columns = $this->_customdata['columns'];
  300. $optype = $data['uutype'];
  301. $updatetype = $data['uuupdatetype'];
  302. // detect if password column needed in file
  303. if (!in_array('password', $columns)) {
  304. switch ($optype) {
  305. case UU_USER_UPDATE:
  306. if (!empty($data['uupasswordold'])) {
  307. $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
  308. }
  309. break;
  310. case UU_USER_ADD_UPDATE:
  311. if (empty($data['uupasswordnew'])) {
  312. $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
  313. }
  314. if (!empty($data['uupasswordold'])) {
  315. $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
  316. }
  317. break;
  318. case UU_USER_ADDNEW:
  319. if (empty($data['uupasswordnew'])) {
  320. $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
  321. }
  322. break;
  323. case UU_USER_ADDINC:
  324. if (empty($data['uupasswordnew'])) {
  325. $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
  326. }
  327. break;
  328. }
  329. }
  330. // If the 'Existing user details' value is set we need to ensure that the
  331. // 'Upload type' is not set to something invalid.
  332. if (!empty($updatetype) && ($optype == UU_USER_ADDNEW || $optype == UU_USER_ADDINC)) {
  333. $errors['uuupdatetype'] = get_string('invalidupdatetype', 'tool_uploaduser');
  334. }
  335. // look for other required data
  336. if ($optype != UU_USER_UPDATE) {
  337. $requiredusernames = useredit_get_required_name_fields();
  338. $missing = array();
  339. foreach ($requiredusernames as $requiredusername) {
  340. if (!in_array($requiredusername, $columns)) {
  341. $missing[] = get_string('missingfield', 'error', $requiredusername);;
  342. }
  343. }
  344. if ($missing) {
  345. $errors['uutype'] = implode('<br />', $missing);
  346. }
  347. if (!in_array('email', $columns) and empty($data['email'])) {
  348. $errors['email'] = get_string('requiredtemplate', 'tool_uploaduser');
  349. }
  350. }
  351. return $errors;
  352. }
  353. /**
  354. * Used to reformat the data from the editor component
  355. *
  356. * @return stdClass
  357. */
  358. function get_data() {
  359. $data = parent::get_data();
  360. if ($data !== null and isset($data->description)) {
  361. $data->descriptionformat = $data->description['format'];
  362. $data->description = $data->description['text'];
  363. }
  364. return $data;
  365. }
  366. }