PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/moodle/admin/uploaduser_form.php

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