PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/tool/uploaduser/user_form.php

https://bitbucket.org/ngmares/moodle
PHP | 423 lines | 295 code | 73 blank | 55 comment | 39 complexity | eab90924b74383de416422d53f3d098c MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, 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. /**
  27. * Upload a file CVS file with user information.
  28. *
  29. * @copyright 2007 Petr Skoda {@link http://skodak.org}
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class admin_uploaduser_form1 extends moodleform {
  33. function definition () {
  34. $mform = $this->_form;
  35. $mform->addElement('header', 'settingsheader', get_string('upload'));
  36. $mform->addElement('filepicker', 'userfile', get_string('file'));
  37. $mform->addRule('userfile', null, 'required');
  38. $choices = csv_import_reader::get_delimiter_list();
  39. $mform->addElement('select', 'delimiter_name', get_string('csvdelimiter', 'tool_uploaduser'), $choices);
  40. if (array_key_exists('cfg', $choices)) {
  41. $mform->setDefault('delimiter_name', 'cfg');
  42. } else if (get_string('listsep', 'langconfig') == ';') {
  43. $mform->setDefault('delimiter_name', 'semicolon');
  44. } else {
  45. $mform->setDefault('delimiter_name', 'comma');
  46. }
  47. $choices = textlib::get_encodings();
  48. $mform->addElement('select', 'encoding', get_string('encoding', 'tool_uploaduser'), $choices);
  49. $mform->setDefault('encoding', 'UTF-8');
  50. $choices = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
  51. $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'tool_uploaduser'), $choices);
  52. $mform->setType('previewrows', PARAM_INT);
  53. $this->add_action_buttons(false, get_string('uploadusers', 'tool_uploaduser'));
  54. }
  55. }
  56. /**
  57. * Specify user upload details
  58. *
  59. * @copyright 2007 Petr Skoda {@link http://skodak.org}
  60. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  61. */
  62. class admin_uploaduser_form2 extends moodleform {
  63. function definition () {
  64. global $CFG, $USER;
  65. $mform = $this->_form;
  66. $columns = $this->_customdata['columns'];
  67. $data = $this->_customdata['data'];
  68. // I am the template user, why should it be the administrator? we have roles now, other ppl may use this script ;-)
  69. $templateuser = $USER;
  70. // upload settings and file
  71. $mform->addElement('header', 'settingsheader', get_string('settings'));
  72. $choices = array(UU_USER_ADDNEW => get_string('uuoptype_addnew', 'tool_uploaduser'),
  73. UU_USER_ADDINC => get_string('uuoptype_addinc', 'tool_uploaduser'),
  74. UU_USER_ADD_UPDATE => get_string('uuoptype_addupdate', 'tool_uploaduser'),
  75. UU_USER_UPDATE => get_string('uuoptype_update', 'tool_uploaduser'));
  76. $mform->addElement('select', 'uutype', get_string('uuoptype', 'tool_uploaduser'), $choices);
  77. $choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth'));
  78. $mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'tool_uploaduser'), $choices);
  79. $mform->setDefault('uupasswordnew', 1);
  80. $mform->disabledIf('uupasswordnew', 'uutype', 'eq', UU_USER_UPDATE);
  81. $choices = array(UU_UPDATE_NOCHANGES => get_string('nochanges', 'tool_uploaduser'),
  82. UU_UPDATE_FILEOVERRIDE => get_string('uuupdatefromfile', 'tool_uploaduser'),
  83. UU_UPDATE_ALLOVERRIDE => get_string('uuupdateall', 'tool_uploaduser'),
  84. UU_UPDATE_MISSING => get_string('uuupdatemissing', 'tool_uploaduser'));
  85. $mform->addElement('select', 'uuupdatetype', get_string('uuupdatetype', 'tool_uploaduser'), $choices);
  86. $mform->setDefault('uuupdatetype', UU_UPDATE_NOCHANGES);
  87. $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDNEW);
  88. $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDINC);
  89. $choices = array(0 => get_string('nochanges', 'tool_uploaduser'), 1 => get_string('update'));
  90. $mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'tool_uploaduser'), $choices);
  91. $mform->setDefault('uupasswordold', 0);
  92. $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDNEW);
  93. $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDINC);
  94. $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 0);
  95. $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 3);
  96. $choices = array(UU_PWRESET_WEAK => get_string('usersweakpassword', 'tool_uploaduser'),
  97. UU_PWRESET_NONE => get_string('none'),
  98. UU_PWRESET_ALL => get_string('all'));
  99. if (empty($CFG->passwordpolicy)) {
  100. unset($choices[UU_PWRESET_WEAK]);
  101. }
  102. $mform->addElement('select', 'uuforcepasswordchange', get_string('forcepasswordchange', 'core'), $choices);
  103. $mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'tool_uploaduser'));
  104. $mform->setDefault('uuallowrenames', 0);
  105. $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDNEW);
  106. $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDINC);
  107. $mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'tool_uploaduser'));
  108. $mform->setDefault('uuallowdeletes', 0);
  109. $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDNEW);
  110. $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDINC);
  111. $mform->addElement('selectyesno', 'uuallowsuspends', get_string('allowsuspends', 'tool_uploaduser'));
  112. $mform->setDefault('uuallowsuspends', 1);
  113. $mform->disabledIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDNEW);
  114. $mform->disabledIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDINC);
  115. $mform->addElement('selectyesno', 'uunoemailduplicates', get_string('uunoemailduplicates', 'tool_uploaduser'));
  116. $mform->setDefault('uunoemailduplicates', 1);
  117. $mform->addElement('selectyesno', 'uustandardusernames', get_string('uustandardusernames', 'tool_uploaduser'));
  118. $mform->setDefault('uustandardusernames', 1);
  119. $choices = array(UU_BULK_NONE => get_string('no'),
  120. UU_BULK_NEW => get_string('uubulknew', 'tool_uploaduser'),
  121. UU_BULK_UPDATED => get_string('uubulkupdated', 'tool_uploaduser'),
  122. UU_BULK_ALL => get_string('uubulkall', 'tool_uploaduser'));
  123. $mform->addElement('select', 'uubulk', get_string('uubulk', 'tool_uploaduser'), $choices);
  124. $mform->setDefault('uubulk', 0);
  125. // roles selection
  126. $showroles = false;
  127. foreach ($columns as $column) {
  128. if (preg_match('/^type\d+$/', $column)) {
  129. $showroles = true;
  130. break;
  131. }
  132. }
  133. if ($showroles) {
  134. $mform->addElement('header', 'rolesheader', get_string('roles'));
  135. $choices = uu_allowed_roles(true);
  136. $mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'tool_uploaduser'), $choices);
  137. if ($studentroles = get_archetype_roles('student')) {
  138. foreach ($studentroles as $role) {
  139. if (isset($choices[$role->id])) {
  140. $mform->setDefault('uulegacy1', $role->id);
  141. break;
  142. }
  143. }
  144. unset($studentroles);
  145. }
  146. $mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'tool_uploaduser'), $choices);
  147. if ($editteacherroles = get_archetype_roles('editingteacher')) {
  148. foreach ($editteacherroles as $role) {
  149. if (isset($choices[$role->id])) {
  150. $mform->setDefault('uulegacy2', $role->id);
  151. break;
  152. }
  153. }
  154. unset($editteacherroles);
  155. }
  156. $mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'tool_uploaduser'), $choices);
  157. if ($teacherroles = get_archetype_roles('teacher')) {
  158. foreach ($teacherroles as $role) {
  159. if (isset($choices[$role->id])) {
  160. $mform->setDefault('uulegacy3', $role->id);
  161. break;
  162. }
  163. }
  164. unset($teacherroles);
  165. }
  166. }
  167. // default values
  168. $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'tool_uploaduser'));
  169. $mform->addElement('text', 'username', get_string('uuusernametemplate', 'tool_uploaduser'), 'size="20"');
  170. $mform->addRule('username', get_string('requiredtemplate', 'tool_uploaduser'), 'required', null, 'client');
  171. $mform->disabledIf('username', 'uutype', 'eq', UU_USER_ADD_UPDATE);
  172. $mform->disabledIf('username', 'uutype', 'eq', UU_USER_UPDATE);
  173. $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
  174. $mform->disabledIf('email', 'uutype', 'eq', UU_USER_ADD_UPDATE);
  175. $mform->disabledIf('email', 'uutype', 'eq', UU_USER_UPDATE);
  176. // only enabled and known to work plugins
  177. $choices = uu_supported_auths();
  178. $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices);
  179. $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default
  180. $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
  181. $mform->setAdvanced('auth');
  182. $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse'));
  183. $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
  184. $mform->setDefault('maildisplay', 2);
  185. $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat'));
  186. $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
  187. $mform->setDefault('mailformat', 1);
  188. $mform->setAdvanced('mailformat');
  189. $choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects'));
  190. $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
  191. $mform->setDefault('maildigest', 0);
  192. $mform->setAdvanced('maildigest');
  193. $choices = array(1 => get_string('autosubscribeyes'), 0 => get_string('autosubscribeno'));
  194. $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
  195. $mform->setDefault('autosubscribe', 1);
  196. $editors = editors_get_enabled();
  197. if (count($editors) > 1) {
  198. $choices = array();
  199. $choices['0'] = get_string('texteditor');
  200. $choices['1'] = get_string('htmleditor');
  201. $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
  202. $mform->setDefault('htmleditor', 1);
  203. } else {
  204. $mform->addElement('hidden', 'htmleditor');
  205. $mform->setDefault('htmleditor', 1);
  206. $mform->setType('htmleditor', PARAM_INT);
  207. }
  208. $mform->addElement('text', 'city', get_string('city'), 'maxlength="100" size="25"');
  209. $mform->setType('city', PARAM_MULTILANG);
  210. if (empty($CFG->defaultcity)) {
  211. $mform->setDefault('city', $templateuser->city);
  212. } else {
  213. $mform->setDefault('city', $CFG->defaultcity);
  214. }
  215. $mform->addRule('city', get_string('required'), 'required');
  216. $mform->addElement('select', 'country', get_string('selectacountry'), get_string_manager()->get_list_of_countries());
  217. if (empty($CFG->country)) {
  218. $mform->setDefault('country', $templateuser->country);
  219. } else {
  220. $mform->setDefault('country', $CFG->country);
  221. }
  222. $mform->setAdvanced('country');
  223. $choices = get_list_of_timezones();
  224. $choices['99'] = get_string('serverlocaltime');
  225. $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
  226. $mform->setDefault('timezone', $templateuser->timezone);
  227. $mform->setAdvanced('timezone');
  228. $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
  229. $mform->setDefault('lang', $templateuser->lang);
  230. $mform->setAdvanced('lang');
  231. $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false);
  232. $mform->addElement('editor', 'description', get_string('userdescription'), null, $editoroptions);
  233. $mform->setType('description', PARAM_CLEANHTML);
  234. $mform->addHelpButton('description', 'userdescription');
  235. $mform->setAdvanced('description');
  236. $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
  237. $mform->setAdvanced('url');
  238. $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="64" size="25"');
  239. $mform->setType('idnumber', PARAM_NOTAGS);
  240. $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
  241. $mform->setType('institution', PARAM_MULTILANG);
  242. $mform->setDefault('institution', $templateuser->institution);
  243. $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
  244. $mform->setType('department', PARAM_MULTILANG);
  245. $mform->setDefault('department', $templateuser->department);
  246. $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
  247. $mform->setType('phone1', PARAM_NOTAGS);
  248. $mform->setAdvanced('phone1');
  249. $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
  250. $mform->setType('phone2', PARAM_NOTAGS);
  251. $mform->setAdvanced('phone2');
  252. $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
  253. $mform->setType('address', PARAM_MULTILANG);
  254. $mform->setAdvanced('address');
  255. // Next the profile defaults
  256. profile_definition($mform);
  257. // hidden fields
  258. $mform->addElement('hidden', 'iid');
  259. $mform->setType('iid', PARAM_INT);
  260. $mform->addElement('hidden', 'previewrows');
  261. $mform->setType('previewrows', PARAM_INT);
  262. $this->add_action_buttons(true, get_string('uploadusers', 'tool_uploaduser'));
  263. $this->set_data($data);
  264. }
  265. /**
  266. * Form tweaks that depend on current data.
  267. */
  268. function definition_after_data() {
  269. $mform = $this->_form;
  270. $columns = $this->_customdata['columns'];
  271. foreach ($columns as $column) {
  272. if ($mform->elementExists($column)) {
  273. $mform->removeElement($column);
  274. }
  275. }
  276. if (!in_array('password', $columns)) {
  277. // password resetting makes sense only if password specified in csv file
  278. if ($mform->elementExists('uuforcepasswordchange')) {
  279. $mform->removeElement('uuforcepasswordchange');
  280. }
  281. }
  282. }
  283. /**
  284. * Server side validation.
  285. */
  286. function validation($data, $files) {
  287. $errors = parent::validation($data, $files);
  288. $columns = $this->_customdata['columns'];
  289. $optype = $data['uutype'];
  290. // detect if password column needed in file
  291. if (!in_array('password', $columns)) {
  292. switch ($optype) {
  293. case UU_USER_UPDATE:
  294. if (!empty($data['uupasswordold'])) {
  295. $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
  296. }
  297. break;
  298. case UU_USER_ADD_UPDATE:
  299. if (empty($data['uupasswordnew'])) {
  300. $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
  301. }
  302. if (!empty($data['uupasswordold'])) {
  303. $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
  304. }
  305. break;
  306. case UU_USER_ADDNEW:
  307. if (empty($data['uupasswordnew'])) {
  308. $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
  309. }
  310. break;
  311. case UU_USER_ADDINC:
  312. if (empty($data['uupasswordnew'])) {
  313. $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
  314. }
  315. break;
  316. }
  317. }
  318. // look for other required data
  319. if ($optype != UU_USER_UPDATE) {
  320. if (!in_array('firstname', $columns)) {
  321. $errors['uutype'] = get_string('missingfield', 'error', 'firstname');
  322. }
  323. if (!in_array('lastname', $columns)) {
  324. if (isset($errors['uutype'])) {
  325. $errors['uutype'] = '';
  326. } else {
  327. $errors['uutype'] = ' ';
  328. }
  329. $errors['uutype'] .= get_string('missingfield', 'error', 'lastname');
  330. }
  331. if (!in_array('email', $columns) and empty($data['email'])) {
  332. $errors['email'] = get_string('requiredtemplate', 'tool_uploaduser');
  333. }
  334. }
  335. return $errors;
  336. }
  337. /**
  338. * Used to reformat the data from the editor component
  339. *
  340. * @return stdClass
  341. */
  342. function get_data() {
  343. $data = parent::get_data();
  344. if ($data !== null and isset($data->description)) {
  345. $data->descriptionformat = $data->description['format'];
  346. $data->description = $data->description['text'];
  347. }
  348. return $data;
  349. }
  350. }