PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/user/edit_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 162 lines | 113 code | 33 blank | 16 comment | 33 complexity | bd26d18d067122d9a403f7c082b1fa85 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  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->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. $context = context_user::instance($user->id, MUST_EXIST);
  67. $fs = get_file_storage();
  68. $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
  69. if (!empty($user->picture) && $hasuploadedpicture) {
  70. $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
  71. } else {
  72. $imagevalue = get_string('none');
  73. }
  74. $imageelement = $mform->getElement('currentpicture');
  75. $imageelement->setValue($imagevalue);
  76. if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) {
  77. $mform->removeElement('deletepicture');
  78. }
  79. /// disable fields that are locked by auth plugins
  80. $fields = get_user_fieldnames();
  81. $authplugin = get_auth_plugin($user->auth);
  82. foreach ($fields as $field) {
  83. if (!$mform->elementExists($field)) {
  84. continue;
  85. }
  86. $configvariable = 'field_lock_' . $field;
  87. if (isset($authplugin->config->{$configvariable})) {
  88. if ($authplugin->config->{$configvariable} === 'locked') {
  89. $mform->hardFreeze($field);
  90. $mform->setConstant($field, $user->$field);
  91. } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $user->$field != '') {
  92. $mform->hardFreeze($field);
  93. $mform->setConstant($field, $user->$field);
  94. }
  95. }
  96. }
  97. /// Next the customisable profile fields
  98. profile_definition_after_data($mform, $user->id);
  99. } else {
  100. profile_definition_after_data($mform, 0);
  101. }
  102. }
  103. function validation($usernew, $files) {
  104. global $CFG, $DB;
  105. $errors = parent::validation($usernew, $files);
  106. $usernew = (object)$usernew;
  107. $user = $DB->get_record('user', array('id'=>$usernew->id));
  108. // validate email
  109. if (!isset($usernew->email)) {
  110. // mail not confirmed yet
  111. } else if (!validate_email($usernew->email)) {
  112. $errors['email'] = get_string('invalidemail');
  113. } else if (($usernew->email !== $user->email) and $DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
  114. $errors['email'] = get_string('emailexists');
  115. }
  116. if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
  117. $errors['email'] = get_string('toomanybounces');
  118. }
  119. if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', context_system::instance())) {
  120. $errorstr = email_is_not_allowed($usernew->email);
  121. if ($errorstr !== false) {
  122. $errors['email'] = $errorstr;
  123. }
  124. }
  125. /// Next the customisable profile fields
  126. $errors += profile_validation($usernew, $files);
  127. return $errors;
  128. }
  129. }