PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/user/edit_form.php

https://bitbucket.org/andrewdavidson/sl-clone
PHP | 164 lines | 115 code | 33 blank | 16 comment | 34 complexity | 8971bd518dce2ef464c9f3f5fbf33965 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_edit_form extends moodleform {
  7. // Define the form
  8. function definition () {
  9. global $CFG, $COURSE, $USER;
  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. /// shared fields
  36. useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
  37. /// extra settigs
  38. if (!empty($CFG->gdversion) and !empty($CFG->disableuserimages)) {
  39. $mform->removeElement('deletepicture');
  40. $mform->removeElement('imagefile');
  41. $mform->removeElement('imagealt');
  42. }
  43. /// Next the customisable profile fields
  44. profile_definition($mform, $userid);
  45. $this->add_action_buttons(false, get_string('updatemyprofile'));
  46. }
  47. function definition_after_data() {
  48. global $CFG, $DB, $OUTPUT;
  49. $mform =& $this->_form;
  50. $userid = $mform->getElementValue('id');
  51. // if language does not exist, use site default lang
  52. if ($langsel = $mform->getElementValue('lang')) {
  53. $lang = reset($langsel);
  54. // check lang exists
  55. if (!get_string_manager()->translation_exists($lang, false)) {
  56. $lang_el =& $mform->getElement('lang');
  57. $lang_el->setValue($CFG->lang);
  58. }
  59. }
  60. if ($user = $DB->get_record('user', array('id'=>$userid))) {
  61. // remove description
  62. if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$userid))) {
  63. $mform->removeElement('description_editor');
  64. }
  65. // print picture
  66. if (!empty($CFG->gdversion)) {
  67. $context = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST);
  68. $fs = get_file_storage();
  69. $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
  70. if (!empty($user->picture) && $hasuploadedpicture) {
  71. $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
  72. } else {
  73. $imagevalue = get_string('none');
  74. }
  75. $imageelement = $mform->getElement('currentpicture');
  76. $imageelement->setValue($imagevalue);
  77. if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) {
  78. $mform->removeElement('deletepicture');
  79. }
  80. }
  81. /// disable fields that are locked by auth plugins
  82. $fields = get_user_fieldnames();
  83. $authplugin = get_auth_plugin($user->auth);
  84. foreach ($fields as $field) {
  85. if (!$mform->elementExists($field)) {
  86. continue;
  87. }
  88. $configvariable = 'field_lock_' . $field;
  89. if (isset($authplugin->config->{$configvariable})) {
  90. if ($authplugin->config->{$configvariable} === 'locked') {
  91. $mform->hardFreeze($field);
  92. $mform->setConstant($field, $user->$field);
  93. } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $user->$field != '') {
  94. $mform->hardFreeze($field);
  95. $mform->setConstant($field, $user->$field);
  96. }
  97. }
  98. }
  99. /// Next the customisable profile fields
  100. profile_definition_after_data($mform, $user->id);
  101. } else {
  102. profile_definition_after_data($mform, 0);
  103. }
  104. }
  105. function validation($usernew, $files) {
  106. global $CFG, $DB;
  107. $errors = parent::validation($usernew, $files);
  108. $usernew = (object)$usernew;
  109. $user = $DB->get_record('user', array('id'=>$usernew->id));
  110. // validate email
  111. if (!isset($usernew->email)) {
  112. // mail not confirmed yet
  113. } else if (!validate_email($usernew->email)) {
  114. $errors['email'] = get_string('invalidemail');
  115. } else if (($usernew->email !== $user->email) and $DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
  116. $errors['email'] = get_string('emailexists');
  117. }
  118. if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
  119. $errors['email'] = get_string('toomanybounces');
  120. }
  121. if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
  122. $errorstr = email_is_not_allowed($usernew->email);
  123. if ($errorstr !== false) {
  124. $errors['email'] = $errorstr;
  125. }
  126. }
  127. /// Next the customisable profile fields
  128. $errors += profile_validation($usernew, $files);
  129. return $errors;
  130. }
  131. }