PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/moodle/login/signup_form.php

https://bitbucket.org/geek745/moodle-db2
PHP | 159 lines | 118 code | 38 blank | 3 comment | 25 complexity | 11f59706e6065a98a6b89fbc239415d8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0
  1. <?php // $Id: signup_form.php,v 1.35.2.6 2008/07/23 05:21:21 nicolasconnault Exp $
  2. require_once($CFG->libdir.'/formslib.php');
  3. require_once($CFG->dirroot.'/user/profile/lib.php');
  4. class login_signup_form extends moodleform {
  5. function definition() {
  6. global $USER, $CFG;
  7. $mform =& $this->_form;
  8. $mform->addElement('header', '', get_string('createuserandpass'), '');
  9. $mform->addElement('text', 'username', get_string('username'), 'maxlength="100" size="12"');
  10. $mform->setType('username', PARAM_NOTAGS);
  11. $mform->addRule('username', get_string('missingusername'), 'required', null, 'server');
  12. $mform->addElement('passwordunmask', 'password', get_string('password'), 'maxlength="32" size="12"');
  13. $mform->setType('password', PARAM_RAW);
  14. $mform->addRule('password', get_string('missingpassword'), 'required', null, 'server');
  15. $mform->addElement('header', '', get_string('supplyinfo'),'');
  16. $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
  17. $mform->setType('email', PARAM_NOTAGS);
  18. $mform->addRule('email', get_string('missingemail'), 'required', null, 'server');
  19. $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
  20. $mform->setType('email2', PARAM_NOTAGS);
  21. $mform->addRule('email2', get_string('missingemail'), 'required', null, 'server');
  22. $nameordercheck = new object();
  23. $nameordercheck->firstname = 'a';
  24. $nameordercheck->lastname = 'b';
  25. if (fullname($nameordercheck) == 'b a' ) { // See MDL-4325
  26. $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
  27. $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
  28. } else {
  29. $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
  30. $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
  31. }
  32. $mform->setType('firstname', PARAM_TEXT);
  33. $mform->addRule('firstname', get_string('missingfirstname'), 'required', null, 'server');
  34. $mform->setType('lastname', PARAM_TEXT);
  35. $mform->addRule('lastname', get_string('missinglastname'), 'required', null, 'server');
  36. $mform->addElement('text', 'city', get_string('city'), 'maxlength="20" size="20"');
  37. $mform->setType('city', PARAM_TEXT);
  38. $mform->addRule('city', get_string('missingcity'), 'required', null, 'server');
  39. $country = get_list_of_countries();
  40. $default_country[''] = get_string('selectacountry');
  41. $country = array_merge($default_country, $country);
  42. $mform->addElement('select', 'country', get_string('country'), $country);
  43. $mform->addRule('country', get_string('missingcountry'), 'required', null, 'server');
  44. if( !empty($CFG->country) ){
  45. $mform->setDefault('country', $CFG->country);
  46. }else{
  47. $mform->setDefault('country', '');
  48. }
  49. if (signup_captcha_enabled()) {
  50. $mform->addElement('recaptcha', 'recaptcha_element', get_string('recaptcha', 'auth'), array('https' => $CFG->loginhttps));
  51. $mform->setHelpButton('recaptcha_element', array('recaptcha', get_string('recaptcha', 'auth')));
  52. }
  53. profile_signup_fields($mform);
  54. if (!empty($CFG->sitepolicy)) {
  55. $mform->addElement('header', '', get_string('policyagreement'), '');
  56. $mform->addElement('static', 'policylink', '', '<a href="'.$CFG->sitepolicy.'" onclick="this.target=\'_blank\'">'.get_String('policyagreementclick').'</a>');
  57. $mform->addElement('checkbox', 'policyagreed', get_string('policyaccept'));
  58. $mform->addRule('policyagreed', get_string('policyagree'), 'required', null, 'server');
  59. }
  60. // buttons
  61. $this->add_action_buttons(true, get_string('createaccount'));
  62. }
  63. function definition_after_data(){
  64. $mform =& $this->_form;
  65. $mform->applyFilter('username', 'moodle_strtolower');
  66. $mform->applyFilter('username', 'trim');
  67. }
  68. function validation($data, $files) {
  69. global $CFG;
  70. $errors = parent::validation($data, $files);
  71. $authplugin = get_auth_plugin($CFG->registerauth);
  72. if (record_exists('user', 'username', $data['username'], 'mnethostid', $CFG->mnet_localhost_id)) {
  73. $errors['username'] = get_string('usernameexists');
  74. } else {
  75. if (empty($CFG->extendedusernamechars)) {
  76. $string = eregi_replace("[^(-\.[:alnum:])]", '', $data['username']);
  77. if (strcmp($data['username'], $string)) {
  78. $errors['username'] = get_string('alphanumerical');
  79. }
  80. }
  81. }
  82. //check if user exists in external db
  83. //TODO: maybe we should check all enabled plugins instead
  84. if ($authplugin->user_exists($data['username'])) {
  85. $errors['username'] = get_string('usernameexists');
  86. }
  87. if (! validate_email($data['email'])) {
  88. $errors['email'] = get_string('invalidemail');
  89. } else if (record_exists('user', 'email', $data['email'])) {
  90. $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
  91. }
  92. if (empty($data['email2'])) {
  93. $errors['email2'] = get_string('missingemail');
  94. } else if ($data['email2'] != $data['email']) {
  95. $errors['email2'] = get_string('invalidemail');
  96. }
  97. if (!isset($errors['email'])) {
  98. if ($err = email_is_not_allowed($data['email'])) {
  99. $errors['email'] = $err;
  100. }
  101. }
  102. $errmsg = '';
  103. if (!check_password_policy($data['password'], $errmsg)) {
  104. $errors['password'] = $errmsg;
  105. }
  106. if (signup_captcha_enabled()) {
  107. $recaptcha_element = $this->_form->getElement('recaptcha_element');
  108. if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
  109. $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
  110. $response_field = $this->_form->_submitValues['recaptcha_response_field'];
  111. if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
  112. $errors['recaptcha'] = $result;
  113. }
  114. } else {
  115. $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
  116. }
  117. }
  118. return $errors;
  119. }
  120. }
  121. ?>