PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/tool/uploaduser/user_form.php

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