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

/user/edit_form.php

https://github.com/mackensen/moodle
PHP | 245 lines | 144 code | 40 blank | 61 comment | 41 complexity | d4c45236223aa66718e3dfce65de4e63 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Form to edit a users profile
  18. *
  19. * @copyright 1999 Martin Dougiamas http://dougiamas.com
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package core_user
  22. */
  23. if (!defined('MOODLE_INTERNAL')) {
  24. die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
  25. }
  26. require_once($CFG->dirroot.'/lib/formslib.php');
  27. /**
  28. * Class user_edit_form.
  29. *
  30. * @copyright 1999 Martin Dougiamas http://dougiamas.com
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class user_edit_form extends moodleform {
  34. /**
  35. * Define the form.
  36. */
  37. public function definition () {
  38. global $CFG, $COURSE, $USER;
  39. $mform = $this->_form;
  40. $editoroptions = null;
  41. $filemanageroptions = null;
  42. $usernotfullysetup = user_not_fully_set_up($USER);
  43. if (!is_array($this->_customdata)) {
  44. throw new coding_exception('invalid custom data for user_edit_form');
  45. }
  46. $editoroptions = $this->_customdata['editoroptions'];
  47. $filemanageroptions = $this->_customdata['filemanageroptions'];
  48. $user = $this->_customdata['user'];
  49. $userid = $user->id;
  50. if (empty($user->country)) {
  51. // We must unset the value here so $CFG->country can be used as default one.
  52. unset($user->country);
  53. }
  54. // Accessibility: "Required" is bad legend text.
  55. $strgeneral = get_string('general');
  56. $strrequired = get_string('required');
  57. // Add some extra hidden fields.
  58. $mform->addElement('hidden', 'id');
  59. $mform->setType('id', PARAM_INT);
  60. $mform->addElement('hidden', 'course', $COURSE->id);
  61. $mform->setType('course', PARAM_INT);
  62. // Print the required moodle fields first.
  63. $mform->addElement('header', 'moodle', $strgeneral);
  64. // Shared fields.
  65. useredit_shared_definition($mform, $editoroptions, $filemanageroptions, $user);
  66. // Extra settigs.
  67. if (!empty($CFG->disableuserimages) || $usernotfullysetup) {
  68. $mform->removeElement('deletepicture');
  69. $mform->removeElement('imagefile');
  70. $mform->removeElement('imagealt');
  71. }
  72. // If the user isn't fully set up, let them know that they will be able to change
  73. // their profile picture once their profile is complete.
  74. if ($usernotfullysetup) {
  75. $userpicturewarning = $mform->createElement('warning', 'userpicturewarning', 'notifymessage', get_string('newpictureusernotsetup'));
  76. $enabledusernamefields = useredit_get_enabled_name_fields();
  77. if ($mform->elementExists('moodle_additional_names')) {
  78. $mform->insertElementBefore($userpicturewarning, 'moodle_additional_names');
  79. } else if ($mform->elementExists('moodle_interests')) {
  80. $mform->insertElementBefore($userpicturewarning, 'moodle_interests');
  81. } else {
  82. $mform->insertElementBefore($userpicturewarning, 'moodle_optional');
  83. }
  84. // This is expected to exist when the form is submitted.
  85. $imagefile = $mform->createElement('hidden', 'imagefile');
  86. $mform->insertElementBefore($imagefile, 'userpicturewarning');
  87. }
  88. // Next the customisable profile fields.
  89. profile_definition($mform, $userid);
  90. $this->add_action_buttons(true, get_string('updatemyprofile'));
  91. $this->set_data($user);
  92. }
  93. /**
  94. * Extend the form definition after the data has been parsed.
  95. */
  96. public function definition_after_data() {
  97. global $CFG, $DB, $OUTPUT;
  98. $mform = $this->_form;
  99. $userid = $mform->getElementValue('id');
  100. // Trim required name fields.
  101. foreach (useredit_get_required_name_fields() as $field) {
  102. $mform->applyFilter($field, 'trim');
  103. }
  104. if ($user = $DB->get_record('user', array('id' => $userid))) {
  105. // Remove description.
  106. if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid' => $userid))) {
  107. $mform->removeElement('description_editor');
  108. }
  109. // Print picture.
  110. $context = context_user::instance($user->id, MUST_EXIST);
  111. $fs = get_file_storage();
  112. $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
  113. if (!empty($user->picture) && $hasuploadedpicture) {
  114. $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size' => 64));
  115. } else {
  116. $imagevalue = get_string('none');
  117. }
  118. $imageelement = $mform->getElement('currentpicture');
  119. $imageelement->setValue($imagevalue);
  120. if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) {
  121. $mform->removeElement('deletepicture');
  122. }
  123. // Disable fields that are locked by auth plugins.
  124. $fields = get_user_fieldnames();
  125. $authplugin = get_auth_plugin($user->auth);
  126. $customfields = $authplugin->get_custom_user_profile_fields();
  127. $customfieldsdata = profile_user_record($userid, false);
  128. $fields = array_merge($fields, $customfields);
  129. foreach ($fields as $field) {
  130. if ($field === 'description') {
  131. // Hard coded hack for description field. See MDL-37704 for details.
  132. $formfield = 'description_editor';
  133. } else {
  134. $formfield = $field;
  135. }
  136. if (!$mform->elementExists($formfield)) {
  137. continue;
  138. }
  139. // Get the original value for the field.
  140. if (in_array($field, $customfields)) {
  141. $key = str_replace('profile_field_', '', $field);
  142. $value = isset($customfieldsdata->{$key}) ? $customfieldsdata->{$key} : '';
  143. } else {
  144. $value = $user->{$field};
  145. }
  146. $configvariable = 'field_lock_' . $field;
  147. if (isset($authplugin->config->{$configvariable})) {
  148. if ($authplugin->config->{$configvariable} === 'locked') {
  149. $mform->hardFreeze($formfield);
  150. $mform->setConstant($formfield, $value);
  151. } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $value != '') {
  152. $mform->hardFreeze($formfield);
  153. $mform->setConstant($formfield, $value);
  154. }
  155. }
  156. }
  157. // Next the customisable profile fields.
  158. profile_definition_after_data($mform, $user->id);
  159. } else {
  160. profile_definition_after_data($mform, 0);
  161. }
  162. }
  163. /**
  164. * Validate incoming form data.
  165. * @param array $usernew
  166. * @param array $files
  167. * @return array
  168. */
  169. public function validation($usernew, $files) {
  170. global $CFG, $DB;
  171. $errors = parent::validation($usernew, $files);
  172. $usernew = (object)$usernew;
  173. $user = $DB->get_record('user', array('id' => $usernew->id));
  174. // Validate email.
  175. if (!isset($usernew->email)) {
  176. // Mail not confirmed yet.
  177. } else if (!validate_email($usernew->email)) {
  178. $errors['email'] = get_string('invalidemail');
  179. } else if (($usernew->email !== $user->email) && empty($CFG->allowaccountssameemail)) {
  180. // Make a case-insensitive query for the given email address.
  181. $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
  182. $params = array(
  183. 'email' => $usernew->email,
  184. 'mnethostid' => $CFG->mnet_localhost_id,
  185. 'userid' => $usernew->id
  186. );
  187. // If there are other user(s) that already have the same email, show an error.
  188. if ($DB->record_exists_select('user', $select, $params)) {
  189. $errors['email'] = get_string('emailexists');
  190. }
  191. }
  192. if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
  193. $errors['email'] = get_string('toomanybounces');
  194. }
  195. if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', context_system::instance())) {
  196. $errorstr = email_is_not_allowed($usernew->email);
  197. if ($errorstr !== false) {
  198. $errors['email'] = $errorstr;
  199. }
  200. }
  201. // Next the customisable profile fields.
  202. $errors += profile_validation($usernew, $files);
  203. return $errors;
  204. }
  205. }