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

/user/editadvanced_form.php

https://bitbucket.org/andrewdavidson/sl-clone
PHP | 209 lines | 154 code | 36 blank | 19 comment | 47 complexity | 0af446d4d08afc1a333f122989bfa83b MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, Apache-2.0, GPL-3.0, BSD-3-Clause, LGPL-2.1
  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->dirroot.'/lib/formslib.php');
  6. class user_editadvanced_form extends moodleform {
  7. // Define the form
  8. function definition() {
  9. global $USER, $CFG, $COURSE;
  10. $mform =& $this->_form;
  11. $editoroptions = null;
  12. $filemanageroptions = null;
  13. $userid = $USER->id;
  14. if (is_array($this->_customdata)) {
  15. if (array_key_exists('editoroptions', $this->_customdata)) {
  16. $editoroptions = $this->_customdata['editoroptions'];
  17. }
  18. if (array_key_exists('filemanageroptions', $this->_customdata)) {
  19. $filemanageroptions = $this->_customdata['filemanageroptions'];
  20. }
  21. if (array_key_exists('userid', $this->_customdata)) {
  22. $userid = $this->_customdata['userid'];
  23. }
  24. }
  25. //Accessibility: "Required" is bad legend text.
  26. $strgeneral = get_string('general');
  27. $strrequired = get_string('required');
  28. /// Add some extra hidden fields
  29. $mform->addElement('hidden', 'id');
  30. $mform->setType('id', PARAM_INT);
  31. $mform->addElement('hidden', 'course', $COURSE->id);
  32. $mform->setType('course', PARAM_INT);
  33. /// Print the required moodle fields first
  34. $mform->addElement('header', 'moodle', $strgeneral);
  35. $mform->addElement('text', 'username', get_string('username'), 'size="20"');
  36. $mform->addRule('username', $strrequired, 'required', null, 'client');
  37. $mform->setType('username', PARAM_RAW);
  38. $auths = get_plugin_list('auth');
  39. $auth_options = array();
  40. foreach ($auths as $auth => $unused) {
  41. $auth_options[$auth] = get_string('pluginname', "auth_{$auth}");
  42. }
  43. $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
  44. $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
  45. $mform->addElement('advcheckbox', 'suspended', get_string('suspended','auth'));
  46. $mform->addHelpButton('suspended', 'suspended', 'auth');
  47. if (!empty($CFG->passwordpolicy)){
  48. $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
  49. }
  50. $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
  51. $mform->addHelpButton('newpassword', 'newpassword');
  52. $mform->setType('newpassword', PARAM_RAW);
  53. $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
  54. $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
  55. /// shared fields
  56. useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
  57. /// Next the customisable profile fields
  58. profile_definition($mform, $userid);
  59. $this->add_action_buttons(false, get_string('updatemyprofile'));
  60. }
  61. function definition_after_data() {
  62. global $USER, $CFG, $DB, $OUTPUT;
  63. $mform =& $this->_form;
  64. if ($userid = $mform->getElementValue('id')) {
  65. $user = $DB->get_record('user', array('id'=>$userid));
  66. } else {
  67. $user = false;
  68. }
  69. // if language does not exist, use site default lang
  70. if ($langsel = $mform->getElementValue('lang')) {
  71. $lang = reset($langsel);
  72. // check lang exists
  73. if (!get_string_manager()->translation_exists($lang, false)) {
  74. $lang_el =& $mform->getElement('lang');
  75. $lang_el->setValue($CFG->lang);
  76. }
  77. }
  78. // user can not change own auth method
  79. if ($userid == $USER->id) {
  80. $mform->hardFreeze('auth');
  81. $mform->hardFreeze('preference_auth_forcepasswordchange');
  82. }
  83. // admin must choose some password and supply correct email
  84. if (!empty($USER->newadminuser)) {
  85. $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
  86. if ($mform->elementExists('suspended')) {
  87. $mform->removeElement('suspended');
  88. }
  89. }
  90. // require password for new users
  91. if ($userid == -1) {
  92. $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
  93. }
  94. if ($user and is_mnet_remote_user($user)) {
  95. // only local accounts can be suspended
  96. if ($mform->elementExists('suspended')) {
  97. $mform->removeElement('suspended');
  98. }
  99. }
  100. if ($user and ($user->id == $USER->id or is_siteadmin($user))) {
  101. // prevent self and admin mess ups
  102. if ($mform->elementExists('suspended')) {
  103. $mform->hardFreeze('suspended');
  104. }
  105. }
  106. // print picture
  107. if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
  108. if ($user) {
  109. $context = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST);
  110. $fs = get_file_storage();
  111. $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
  112. if (!empty($user->picture) && $hasuploadedpicture) {
  113. $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
  114. } else {
  115. $imagevalue = get_string('none');
  116. }
  117. } else {
  118. $imagevalue = get_string('none');
  119. }
  120. $imageelement = $mform->getElement('currentpicture');
  121. $imageelement->setValue($imagevalue);
  122. if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
  123. $mform->removeElement('deletepicture');
  124. }
  125. }
  126. /// Next the customisable profile fields
  127. profile_definition_after_data($mform, $userid);
  128. }
  129. function validation($usernew, $files) {
  130. global $CFG, $DB;
  131. $usernew = (object)$usernew;
  132. $usernew->username = trim($usernew->username);
  133. $user = $DB->get_record('user', array('id'=>$usernew->id));
  134. $err = array();
  135. if (!empty($usernew->newpassword)) {
  136. $errmsg = '';//prevent eclipse warning
  137. if (!check_password_policy($usernew->newpassword, $errmsg)) {
  138. $err['newpassword'] = $errmsg;
  139. }
  140. }
  141. if (empty($usernew->username)) {
  142. //might be only whitespace
  143. $err['username'] = get_string('required');
  144. } else if (!$user or $user->username !== $usernew->username) {
  145. //check new username does not exist
  146. if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
  147. $err['username'] = get_string('usernameexists');
  148. }
  149. //check allowed characters
  150. if ($usernew->username !== textlib::strtolower($usernew->username)) {
  151. $err['username'] = get_string('usernamelowercase');
  152. } else {
  153. if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) {
  154. $err['username'] = get_string('invalidusername');
  155. }
  156. }
  157. }
  158. if (!$user or $user->email !== $usernew->email) {
  159. if (!validate_email($usernew->email)) {
  160. $err['email'] = get_string('invalidemail');
  161. } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
  162. $err['email'] = get_string('emailexists');
  163. }
  164. }
  165. /// Next the customisable profile fields
  166. $err += profile_validation($usernew, $files);
  167. if (count($err) == 0){
  168. return true;
  169. } else {
  170. return $err;
  171. }
  172. }
  173. }