PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/tool/uploaduser/user_form.php

https://github.com/dongsheng/moodle
PHP | 466 lines | 321 code | 73 blank | 72 comment | 42 complexity | e571cf09407fe4993bd1d048dd5a0dd8 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, GPL-3.0, Apache-2.0, LGPL-2.1
  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. * Returns list of elements and their default values, to be used in CLI
  62. *
  63. * @return array
  64. */
  65. public function get_form_for_cli() {
  66. $elements = array_filter($this->_form->_elements, function($element) {
  67. return !in_array($element->getName(), ['buttonar', 'userfile', 'previewrows']);
  68. });
  69. return [$elements, $this->_form->_defaultValues];
  70. }
  71. }
  72. /**
  73. * Specify user upload details
  74. *
  75. * @copyright 2007 Petr Skoda {@link http://skodak.org}
  76. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  77. */
  78. class admin_uploaduser_form2 extends moodleform {
  79. function definition () {
  80. global $CFG, $USER;
  81. $mform = $this->_form;
  82. $columns = $this->_customdata['columns'];
  83. $data = $this->_customdata['data'];
  84. // I am the template user, why should it be the administrator? we have roles now, other ppl may use this script ;-)
  85. $templateuser = $USER;
  86. // upload settings and file
  87. $mform->addElement('header', 'settingsheader', get_string('settings'));
  88. $choices = array(UU_USER_ADDNEW => get_string('uuoptype_addnew', 'tool_uploaduser'),
  89. UU_USER_ADDINC => get_string('uuoptype_addinc', 'tool_uploaduser'),
  90. UU_USER_ADD_UPDATE => get_string('uuoptype_addupdate', 'tool_uploaduser'),
  91. UU_USER_UPDATE => get_string('uuoptype_update', 'tool_uploaduser'));
  92. $mform->addElement('select', 'uutype', get_string('uuoptype', 'tool_uploaduser'), $choices);
  93. $choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth'));
  94. $mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'tool_uploaduser'), $choices);
  95. $mform->setDefault('uupasswordnew', 1);
  96. $mform->hideIf('uupasswordnew', 'uutype', 'eq', UU_USER_UPDATE);
  97. $choices = array(UU_UPDATE_NOCHANGES => get_string('nochanges', 'tool_uploaduser'),
  98. UU_UPDATE_FILEOVERRIDE => get_string('uuupdatefromfile', 'tool_uploaduser'),
  99. UU_UPDATE_ALLOVERRIDE => get_string('uuupdateall', 'tool_uploaduser'),
  100. UU_UPDATE_MISSING => get_string('uuupdatemissing', 'tool_uploaduser'));
  101. $mform->addElement('select', 'uuupdatetype', get_string('uuupdatetype', 'tool_uploaduser'), $choices);
  102. $mform->setDefault('uuupdatetype', UU_UPDATE_NOCHANGES);
  103. $mform->hideIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDNEW);
  104. $mform->hideIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDINC);
  105. $choices = array(0 => get_string('nochanges', 'tool_uploaduser'), 1 => get_string('update'));
  106. $mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'tool_uploaduser'), $choices);
  107. $mform->setDefault('uupasswordold', 0);
  108. $mform->hideIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDNEW);
  109. $mform->hideIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDINC);
  110. $mform->hideIf('uupasswordold', 'uuupdatetype', 'eq', 0);
  111. $mform->hideIf('uupasswordold', 'uuupdatetype', 'eq', 3);
  112. $choices = array(UU_PWRESET_WEAK => get_string('usersweakpassword', 'tool_uploaduser'),
  113. UU_PWRESET_NONE => get_string('none'),
  114. UU_PWRESET_ALL => get_string('all'));
  115. if (empty($CFG->passwordpolicy)) {
  116. unset($choices[UU_PWRESET_WEAK]);
  117. }
  118. $mform->addElement('select', 'uuforcepasswordchange', get_string('forcepasswordchange', 'core'), $choices);
  119. $mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'tool_uploaduser'));
  120. $mform->setDefault('uuallowrenames', 0);
  121. $mform->hideIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDNEW);
  122. $mform->hideIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDINC);
  123. $mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'tool_uploaduser'));
  124. $mform->setDefault('uuallowdeletes', 0);
  125. $mform->hideIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDNEW);
  126. $mform->hideIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDINC);
  127. $mform->addElement('selectyesno', 'uuallowsuspends', get_string('allowsuspends', 'tool_uploaduser'));
  128. $mform->setDefault('uuallowsuspends', 1);
  129. $mform->hideIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDNEW);
  130. $mform->hideIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDINC);
  131. if (!empty($CFG->allowaccountssameemail)) {
  132. $mform->addElement('selectyesno', 'uunoemailduplicates', get_string('uunoemailduplicates', 'tool_uploaduser'));
  133. $mform->setDefault('uunoemailduplicates', 1);
  134. } else {
  135. $mform->addElement('hidden', 'uunoemailduplicates', 1);
  136. }
  137. $mform->setType('uunoemailduplicates', PARAM_BOOL);
  138. $mform->addElement('selectyesno', 'uustandardusernames', get_string('uustandardusernames', 'tool_uploaduser'));
  139. $mform->setDefault('uustandardusernames', 1);
  140. $choices = array(UU_BULK_NONE => get_string('no'),
  141. UU_BULK_NEW => get_string('uubulknew', 'tool_uploaduser'),
  142. UU_BULK_UPDATED => get_string('uubulkupdated', 'tool_uploaduser'),
  143. UU_BULK_ALL => get_string('uubulkall', 'tool_uploaduser'));
  144. $mform->addElement('select', 'uubulk', get_string('uubulk', 'tool_uploaduser'), $choices);
  145. $mform->setDefault('uubulk', 0);
  146. // roles selection
  147. $showroles = false;
  148. foreach ($columns as $column) {
  149. if (preg_match('/^type\d+$/', $column)) {
  150. $showroles = true;
  151. break;
  152. }
  153. }
  154. if ($showroles) {
  155. $mform->addElement('header', 'rolesheader', get_string('roles'));
  156. $choices = uu_allowed_roles(true);
  157. $mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'tool_uploaduser'), $choices);
  158. if ($studentroles = get_archetype_roles('student')) {
  159. foreach ($studentroles as $role) {
  160. if (isset($choices[$role->id])) {
  161. $mform->setDefault('uulegacy1', $role->id);
  162. break;
  163. }
  164. }
  165. unset($studentroles);
  166. }
  167. $mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'tool_uploaduser'), $choices);
  168. if ($editteacherroles = get_archetype_roles('editingteacher')) {
  169. foreach ($editteacherroles as $role) {
  170. if (isset($choices[$role->id])) {
  171. $mform->setDefault('uulegacy2', $role->id);
  172. break;
  173. }
  174. }
  175. unset($editteacherroles);
  176. }
  177. $mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'tool_uploaduser'), $choices);
  178. if ($teacherroles = get_archetype_roles('teacher')) {
  179. foreach ($teacherroles as $role) {
  180. if (isset($choices[$role->id])) {
  181. $mform->setDefault('uulegacy3', $role->id);
  182. break;
  183. }
  184. }
  185. unset($teacherroles);
  186. }
  187. }
  188. // default values
  189. $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'tool_uploaduser'));
  190. $mform->addElement('text', 'username', get_string('uuusernametemplate', 'tool_uploaduser'), 'size="20"');
  191. $mform->setType('username', PARAM_RAW); // No cleaning here. The process verifies it later.
  192. $mform->addRule('username', get_string('requiredtemplate', 'tool_uploaduser'), 'required', null, 'client');
  193. $mform->hideIf('username', 'uutype', 'eq', UU_USER_ADD_UPDATE);
  194. $mform->hideIf('username', 'uutype', 'eq', UU_USER_UPDATE);
  195. $mform->setForceLtr('username');
  196. $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
  197. $mform->setType('email', PARAM_RAW); // No cleaning here. The process verifies it later.
  198. $mform->hideIf('email', 'uutype', 'eq', UU_USER_ADD_UPDATE);
  199. $mform->hideIf('email', 'uutype', 'eq', UU_USER_UPDATE);
  200. $mform->setForceLtr('email');
  201. // only enabled and known to work plugins
  202. $choices = uu_supported_auths();
  203. $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices);
  204. $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default
  205. $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
  206. $mform->setAdvanced('auth');
  207. $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse'));
  208. $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
  209. $mform->setDefault('maildisplay', core_user::get_property_default('maildisplay'));
  210. $mform->addHelpButton('maildisplay', 'emaildisplay');
  211. $choices = array(0 => get_string('emailenable'), 1 => get_string('emaildisable'));
  212. $mform->addElement('select', 'emailstop', get_string('emailstop'), $choices);
  213. $mform->setDefault('emailstop', core_user::get_property_default('emailstop'));
  214. $mform->setAdvanced('emailstop');
  215. $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat'));
  216. $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
  217. $mform->setDefault('mailformat', core_user::get_property_default('mailformat'));
  218. $mform->setAdvanced('mailformat');
  219. $choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects'));
  220. $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
  221. $mform->setDefault('maildigest', core_user::get_property_default('maildigest'));
  222. $mform->setAdvanced('maildigest');
  223. $choices = array(1 => get_string('autosubscribeyes'), 0 => get_string('autosubscribeno'));
  224. $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
  225. $mform->setDefault('autosubscribe', core_user::get_property_default('autosubscribe'));
  226. $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="25"');
  227. $mform->setType('city', PARAM_TEXT);
  228. if (empty($CFG->defaultcity)) {
  229. $mform->setDefault('city', $templateuser->city);
  230. } else {
  231. $mform->setDefault('city', core_user::get_property_default('city'));
  232. }
  233. $choices = get_string_manager()->get_list_of_countries();
  234. $choices = array(''=>get_string('selectacountry').'...') + $choices;
  235. $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
  236. if (empty($CFG->country)) {
  237. $mform->setDefault('country', $templateuser->country);
  238. } else {
  239. $mform->setDefault('country', core_user::get_property_default('country'));
  240. }
  241. $mform->setAdvanced('country');
  242. $choices = core_date::get_list_of_timezones($templateuser->timezone, true);
  243. $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
  244. $mform->setDefault('timezone', $templateuser->timezone);
  245. $mform->setAdvanced('timezone');
  246. $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
  247. $mform->setDefault('lang', $templateuser->lang);
  248. $mform->setAdvanced('lang');
  249. $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false);
  250. $mform->addElement('editor', 'description', get_string('userdescription'), null, $editoroptions);
  251. $mform->setType('description', PARAM_CLEANHTML);
  252. $mform->addHelpButton('description', 'userdescription');
  253. $mform->setAdvanced('description');
  254. $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
  255. $mform->setType('idnumber', core_user::get_property_type('idnumber'));
  256. $mform->setForceLtr('idnumber');
  257. $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"');
  258. $mform->setType('institution', PARAM_TEXT);
  259. $mform->setDefault('institution', $templateuser->institution);
  260. $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"');
  261. $mform->setType('department', PARAM_TEXT);
  262. $mform->setDefault('department', $templateuser->department);
  263. $mform->addElement('text', 'phone1', get_string('phone1'), 'maxlength="20" size="25"');
  264. $mform->setType('phone1', PARAM_NOTAGS);
  265. $mform->setAdvanced('phone1');
  266. $mform->setForceLtr('phone1');
  267. $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
  268. $mform->setType('phone2', PARAM_NOTAGS);
  269. $mform->setAdvanced('phone2');
  270. $mform->setForceLtr('phone2');
  271. $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"');
  272. $mform->setType('address', PARAM_TEXT);
  273. $mform->setAdvanced('address');
  274. // Next the profile defaults
  275. profile_definition($mform);
  276. // hidden fields
  277. $mform->addElement('hidden', 'iid');
  278. $mform->setType('iid', PARAM_INT);
  279. $mform->addElement('hidden', 'previewrows');
  280. $mform->setType('previewrows', PARAM_INT);
  281. $this->add_action_buttons(true, get_string('uploadusers', 'tool_uploaduser'));
  282. $this->set_data($data);
  283. }
  284. /**
  285. * Form tweaks that depend on current data.
  286. */
  287. function definition_after_data() {
  288. $mform = $this->_form;
  289. $columns = $this->_customdata['columns'];
  290. foreach ($columns as $column) {
  291. if ($mform->elementExists($column)) {
  292. $mform->removeElement($column);
  293. }
  294. }
  295. if (!in_array('password', $columns)) {
  296. // password resetting makes sense only if password specified in csv file
  297. if ($mform->elementExists('uuforcepasswordchange')) {
  298. $mform->removeElement('uuforcepasswordchange');
  299. }
  300. }
  301. }
  302. /**
  303. * Server side validation.
  304. */
  305. function validation($data, $files) {
  306. $errors = parent::validation($data, $files);
  307. $columns = $this->_customdata['columns'];
  308. $optype = $data['uutype'];
  309. $updatetype = $data['uuupdatetype'];
  310. // detect if password column needed in file
  311. if (!in_array('password', $columns)) {
  312. switch ($optype) {
  313. case UU_USER_UPDATE:
  314. if (!empty($data['uupasswordold'])) {
  315. $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
  316. }
  317. break;
  318. case UU_USER_ADD_UPDATE:
  319. if (empty($data['uupasswordnew'])) {
  320. $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
  321. }
  322. if (!empty($data['uupasswordold'])) {
  323. $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
  324. }
  325. break;
  326. case UU_USER_ADDNEW:
  327. if (empty($data['uupasswordnew'])) {
  328. $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
  329. }
  330. break;
  331. case UU_USER_ADDINC:
  332. if (empty($data['uupasswordnew'])) {
  333. $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
  334. }
  335. break;
  336. }
  337. }
  338. // If the 'Existing user details' value is set we need to ensure that the
  339. // 'Upload type' is not set to something invalid.
  340. if (!empty($updatetype) && ($optype == UU_USER_ADDNEW || $optype == UU_USER_ADDINC)) {
  341. $errors['uuupdatetype'] = get_string('invalidupdatetype', 'tool_uploaduser');
  342. }
  343. // look for other required data
  344. if ($optype != UU_USER_UPDATE) {
  345. $requiredusernames = useredit_get_required_name_fields();
  346. $missing = array();
  347. foreach ($requiredusernames as $requiredusername) {
  348. if (!in_array($requiredusername, $columns)) {
  349. $missing[] = get_string('missingfield', 'error', $requiredusername);;
  350. }
  351. }
  352. if ($missing) {
  353. $errors['uutype'] = implode('<br />', $missing);
  354. }
  355. if (!in_array('email', $columns) and empty($data['email'])) {
  356. $errors['email'] = get_string('requiredtemplate', 'tool_uploaduser');
  357. }
  358. }
  359. return $errors;
  360. }
  361. /**
  362. * Used to reformat the data from the editor component
  363. *
  364. * @return stdClass
  365. */
  366. function get_data() {
  367. $data = parent::get_data();
  368. if ($data !== null and isset($data->description)) {
  369. $data->descriptionformat = $data->description['format'];
  370. $data->description = $data->description['text'];
  371. }
  372. return $data;
  373. }
  374. /**
  375. * Returns list of elements and their default values, to be used in CLI
  376. *
  377. * @return array
  378. */
  379. public function get_form_for_cli() {
  380. $elements = array_filter($this->_form->_elements, function($element) {
  381. return !in_array($element->getName(), ['buttonar', 'uubulk']);
  382. });
  383. return [$elements, $this->_form->_defaultValues];
  384. }
  385. /**
  386. * Returns validation errors (used in CLI)
  387. *
  388. * @return array
  389. */
  390. public function get_validation_errors(): array {
  391. return $this->_form->_errors;
  392. }
  393. }