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

/admin/uploaduser_form.php

https://github.com/mavrik/testmoodleongit
PHP | 379 lines | 289 code | 70 blank | 20 comment | 35 complexity | 67f5adb9ea6ce52509920bd4545115cc MD5 | raw file
  1. <?php
  2. if (!defined('MOODLE_INTERNAL')) {
  3. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  4. }
  5. require_once $CFG->libdir.'/formslib.php';
  6. class admin_uploaduser_form1 extends moodleform {
  7. function definition (){
  8. global $CFG, $USER;
  9. $mform =& $this->_form;
  10. $mform->addElement('header', 'settingsheader', get_string('upload'));
  11. $mform->addElement('filepicker', 'userfile', get_string('file'));
  12. $mform->addRule('userfile', null, 'required');
  13. $choices = csv_import_reader::get_delimiter_list();
  14. $mform->addElement('select', 'delimiter_name', get_string('csvdelimiter', 'admin'), $choices);
  15. if (array_key_exists('cfg', $choices)) {
  16. $mform->setDefault('delimiter_name', 'cfg');
  17. } else if (get_string('listsep', 'langconfig') == ';') {
  18. $mform->setDefault('delimiter_name', 'semicolon');
  19. } else {
  20. $mform->setDefault('delimiter_name', 'comma');
  21. }
  22. $textlib = textlib_get_instance();
  23. $choices = $textlib->get_encodings();
  24. $mform->addElement('select', 'encoding', get_string('encoding', 'admin'), $choices);
  25. $mform->setDefault('encoding', 'UTF-8');
  26. $choices = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
  27. $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'admin'), $choices);
  28. $mform->setType('previewrows', PARAM_INT);
  29. $choices = array(UU_ADDNEW => get_string('uuoptype_addnew', 'admin'),
  30. UU_ADDINC => get_string('uuoptype_addinc', 'admin'),
  31. UU_ADD_UPDATE => get_string('uuoptype_addupdate', 'admin'),
  32. UU_UPDATE => get_string('uuoptype_update', 'admin'));
  33. $mform->addElement('select', 'uutype', get_string('uuoptype', 'admin'), $choices);
  34. $this->add_action_buttons(false, get_string('uploadusers', 'admin'));
  35. }
  36. }
  37. class admin_uploaduser_form2 extends moodleform {
  38. function definition (){
  39. global $CFG, $USER;
  40. $mform =& $this->_form;
  41. $columns =& $this->_customdata;
  42. // I am the template user, why should it be the administrator? we have roles now, other ppl may use this script ;-)
  43. $templateuser = $USER;
  44. // upload settings and file
  45. $mform->addElement('header', 'settingsheader', get_string('settings'));
  46. $mform->addElement('static', 'uutypelabel', get_string('uuoptype', 'admin') );
  47. $choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth'));
  48. $mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'admin'), $choices);
  49. $mform->setDefault('uupasswordnew', 1);
  50. $mform->disabledIf('uupasswordnew', 'uutype', 'eq', UU_UPDATE);
  51. $choices = array(0 => get_string('nochanges', 'admin'),
  52. 1 => get_string('uuupdatefromfile', 'admin'),
  53. 2 => get_string('uuupdateall', 'admin'),
  54. 3 => get_string('uuupdatemissing', 'admin'));
  55. $mform->addElement('select', 'uuupdatetype', get_string('uuupdatetype', 'admin'), $choices);
  56. $mform->setDefault('uuupdatetype', 0);
  57. $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_ADDNEW);
  58. $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_ADDINC);
  59. $choices = array(0 => get_string('nochanges', 'admin'), 1 => get_string('update'));
  60. $mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'admin'), $choices);
  61. $mform->setDefault('uupasswordold', 0);
  62. $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_ADDNEW);
  63. $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_ADDINC);
  64. $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 0);
  65. $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 3);
  66. $mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'admin'));
  67. $mform->setDefault('uuallowrenames', 0);
  68. $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_ADDNEW);
  69. $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_ADDINC);
  70. $mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'admin'));
  71. $mform->setDefault('uuallowdeletes', 0);
  72. $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_ADDNEW);
  73. $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_ADDINC);
  74. $mform->addElement('selectyesno', 'uunoemailduplicates', get_string('uunoemailduplicates', 'admin'));
  75. $mform->setDefault('uunoemailduplicates', 1);
  76. $choices = array(0 => get_string('no'),
  77. 1 => get_string('uubulknew', 'admin'),
  78. 2 => get_string('uubulkupdated', 'admin'),
  79. 3 => get_string('uubulkall', 'admin'));
  80. $mform->addElement('select', 'uubulk', get_string('uubulk', 'admin'), $choices);
  81. $mform->setDefault('uubulk', 0);
  82. // roles selection
  83. $showroles = false;
  84. foreach ($columns as $column) {
  85. if (preg_match('/^type\d+$/', $column)) {
  86. $showroles = true;
  87. break;
  88. }
  89. }
  90. if ($showroles) {
  91. $mform->addElement('header', 'rolesheader', get_string('roles'));
  92. $choices = uu_allowed_roles(true);
  93. $mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'admin'), $choices);
  94. if ($studentroles = get_archetype_roles('student')) {
  95. foreach ($studentroles as $role) {
  96. if (isset($choices[$role->id])) {
  97. $mform->setDefault('uulegacy1', $role->id);
  98. break;
  99. }
  100. }
  101. unset($studentroles);
  102. }
  103. $mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'admin'), $choices);
  104. if ($editteacherroles = get_archetype_roles('editingteacher')) {
  105. foreach ($editteacherroles as $role) {
  106. if (isset($choices[$role->id])) {
  107. $mform->setDefault('uulegacy2', $role->id);
  108. break;
  109. }
  110. }
  111. unset($editteacherroles);
  112. }
  113. $mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'admin'), $choices);
  114. if ($teacherroles = get_archetype_roles('teacher')) {
  115. foreach ($teacherroles as $role) {
  116. if (isset($choices[$role->id])) {
  117. $mform->setDefault('uulegacy3', $role->id);
  118. break;
  119. }
  120. }
  121. unset($teacherroles);
  122. }
  123. }
  124. // default values
  125. $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'admin'));
  126. $mform->addElement('text', 'username', get_string('username'), 'size="20"');
  127. $mform->addRule('username', get_string('requiredtemplate', 'admin'), 'required', null, 'client');
  128. // only enabled and known to work plugins
  129. $choices = uu_allowed_auths();
  130. $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices);
  131. $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default
  132. $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
  133. $mform->setAdvanced('auth');
  134. $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
  135. $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse'));
  136. $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
  137. $mform->setDefault('maildisplay', 2);
  138. $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat'));
  139. $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
  140. $mform->setDefault('mailformat', 1);
  141. $mform->setAdvanced('mailformat');
  142. $choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects'));
  143. $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
  144. $mform->setDefault('maildigest', 0);
  145. $mform->setAdvanced('maildigest');
  146. $choices = array(0 => get_string('autosubscribeyes'), 1 => get_string('autosubscribeno'));
  147. $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
  148. $mform->setDefault('autosubscribe', 1);
  149. $editors = editors_get_enabled();
  150. if (count($editors) > 1) {
  151. $choices = array();
  152. $choices['0'] = get_string('texteditor');
  153. $choices['1'] = get_string('htmleditor');
  154. $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
  155. $mform->setDefault('htmleditor', 1);
  156. } else {
  157. $mform->addElement('hidden', 'htmleditor');
  158. $mform->setDefault('htmleditor', 1);
  159. $mform->setType('htmleditor', PARAM_INT);
  160. }
  161. if (empty($CFG->enableajax)) {
  162. $mform->addElement('static', 'ajax', get_string('ajaxuse'), get_string('ajaxno'));
  163. } else {
  164. $choices = array( 0 => get_string('ajaxno'), 1 => get_string('ajaxyes'));
  165. $mform->addElement('select', 'ajax', get_string('ajaxuse'), $choices);
  166. $mform->setDefault('ajax', 1);
  167. }
  168. $mform->setAdvanced('ajax');
  169. $mform->addElement('text', 'city', get_string('city'), 'maxlength="100" size="25"');
  170. $mform->setType('city', PARAM_MULTILANG);
  171. $mform->setDefault('city', $templateuser->city);
  172. $mform->addElement('select', 'country', get_string('selectacountry'), get_string_manager()->get_list_of_countries());
  173. $mform->setDefault('country', $templateuser->country);
  174. $mform->setAdvanced('country');
  175. $choices = get_list_of_timezones();
  176. $choices['99'] = get_string('serverlocaltime');
  177. $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
  178. $mform->setDefault('timezone', $templateuser->timezone);
  179. $mform->setAdvanced('timezone');
  180. $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
  181. $mform->setDefault('lang', $templateuser->lang);
  182. $mform->setAdvanced('lang');
  183. $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false);
  184. $mform->addElement('editor', 'description', get_string('userdescription'), null, $editoroptions);
  185. $mform->setType('description', PARAM_CLEANHTML);
  186. $mform->addHelpButton('description', 'userdescription');
  187. $mform->setAdvanced('description');
  188. $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
  189. $mform->setAdvanced('url');
  190. $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="64" size="25"');
  191. $mform->setType('idnumber', PARAM_NOTAGS);
  192. $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
  193. $mform->setType('institution', PARAM_MULTILANG);
  194. $mform->setDefault('institution', $templateuser->institution);
  195. $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
  196. $mform->setType('department', PARAM_MULTILANG);
  197. $mform->setDefault('department', $templateuser->department);
  198. $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
  199. $mform->setType('phone1', PARAM_NOTAGS);
  200. $mform->setAdvanced('phone1');
  201. $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
  202. $mform->setType('phone2', PARAM_NOTAGS);
  203. $mform->setAdvanced('phone2');
  204. $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
  205. $mform->setType('address', PARAM_MULTILANG);
  206. $mform->setAdvanced('address');
  207. // Next the profile defaults
  208. profile_definition($mform);
  209. // hidden fields
  210. $mform->addElement('hidden', 'iid');
  211. $mform->setType('iid', PARAM_INT);
  212. $mform->addElement('hidden', 'previewrows');
  213. $mform->setType('previewrows', PARAM_INT);
  214. $mform->addElement('hidden', 'readcount');
  215. $mform->setType('readcount', PARAM_INT);
  216. $mform->addElement('hidden', 'uutype');
  217. $mform->setType('uutype', PARAM_INT);
  218. $this->add_action_buttons(true, get_string('uploadusers', 'admin'));
  219. }
  220. /**
  221. * Form tweaks that depend on current data.
  222. */
  223. function definition_after_data() {
  224. $mform =& $this->_form;
  225. $columns =& $this->_customdata;
  226. foreach ($columns as $column) {
  227. if ($mform->elementExists($column)) {
  228. $mform->removeElement($column);
  229. }
  230. }
  231. }
  232. /**
  233. * Server side validation.
  234. */
  235. function validation($data, $files) {
  236. $errors = parent::validation($data, $files);
  237. $columns =& $this->_customdata;
  238. $optype = $data['uutype'];
  239. // detect if password column needed in file
  240. if (!in_array('password', $columns)) {
  241. switch ($optype) {
  242. case UU_UPDATE:
  243. if (!empty($data['uupasswordold'])) {
  244. $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
  245. }
  246. break;
  247. case UU_ADD_UPDATE:
  248. if (empty($data['uupasswordnew'])) {
  249. $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
  250. }
  251. if (!empty($data['uupasswordold'])) {
  252. $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
  253. }
  254. break;
  255. case UU_ADDNEW:
  256. if (empty($data['uupasswordnew'])) {
  257. $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
  258. }
  259. break;
  260. case UU_ADDINC:
  261. if (empty($data['uupasswordnew'])) {
  262. $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
  263. }
  264. break;
  265. }
  266. }
  267. // look for other required data
  268. if ($optype != UU_UPDATE) {
  269. if (!in_array('firstname', $columns)) {
  270. $errors['uutype'] = get_string('missingfield', 'error', 'firstname');
  271. }
  272. if (!in_array('lastname', $columns)) {
  273. if (isset($errors['uutype'])) {
  274. $errors['uutype'] = '';
  275. } else {
  276. $errors['uutype'] = ' ';
  277. }
  278. $errors['uutype'] .= get_string('missingfield', 'error', 'lastname');
  279. }
  280. if (!in_array('email', $columns) and empty($data['email'])) {
  281. $errors['email'] = get_string('requiredtemplate', 'admin');
  282. }
  283. if (!in_array('city', $columns) and empty($data['city'])) {
  284. $errors['city'] = get_string('required');
  285. }
  286. }
  287. return $errors;
  288. }
  289. /**
  290. * Used to reformat the data from the editor component
  291. *
  292. * @return stdClass
  293. */
  294. function get_data() {
  295. $data = parent::get_data();
  296. if ($data !== null) {
  297. $data->descriptionformat = $data->description['format'];
  298. $data->description = $data->description['text'];
  299. }
  300. return $data;
  301. }
  302. }
  303. class admin_uploaduser_form3 extends moodleform {
  304. function definition (){
  305. global $CFG, $USER;
  306. $mform =& $this->_form;
  307. $this->add_action_buttons(false, get_string('uploadnewfile'));
  308. }
  309. }