editadvanced_form.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 for editing 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_editadvanced_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_editadvanced_form extends moodleform {
  34. /**
  35. * Define the form.
  36. */
  37. public function definition() {
  38. global $USER, $CFG, $COURSE;
  39. $mform = $this->_form;
  40. $editoroptions = null;
  41. $filemanageroptions = null;
  42. if (!is_array($this->_customdata)) {
  43. throw new coding_exception('invalid custom data for user_edit_form');
  44. }
  45. $editoroptions = $this->_customdata['editoroptions'];
  46. $filemanageroptions = $this->_customdata['filemanageroptions'];
  47. $user = $this->_customdata['user'];
  48. $userid = $user->id;
  49. // Accessibility: "Required" is bad legend text.
  50. $strgeneral = get_string('general');
  51. $strrequired = get_string('required');
  52. // Add some extra hidden fields.
  53. $mform->addElement('hidden', 'id');
  54. $mform->setType('id', core_user::get_property_type('id'));
  55. $mform->addElement('hidden', 'course', $COURSE->id);
  56. $mform->setType('course', PARAM_INT);
  57. // Print the required moodle fields first.
  58. $mform->addElement('header', 'moodle', $strgeneral);
  59. $auths = core_component::get_plugin_list('auth');
  60. $enabled = get_string('pluginenabled', 'core_plugin');
  61. $disabled = get_string('plugindisabled', 'core_plugin');
  62. $authoptions = array($enabled => array(), $disabled => array());
  63. $cannotchangepass = array();
  64. $cannotchangeusername = array();
  65. foreach ($auths as $auth => $unused) {
  66. $authinst = get_auth_plugin($auth);
  67. if (!$authinst->is_internal()) {
  68. $cannotchangeusername[] = $auth;
  69. }
  70. $passwordurl = $authinst->change_password_url();
  71. if (!($authinst->can_change_password() && empty($passwordurl))) {
  72. if ($userid < 1 and $authinst->is_internal()) {
  73. // This is unlikely but we can not create account without password
  74. // when plugin uses passwords, we need to set it initially at least.
  75. } else {
  76. $cannotchangepass[] = $auth;
  77. }
  78. }
  79. if (is_enabled_auth($auth)) {
  80. $authoptions[$enabled][$auth] = get_string('pluginname', "auth_{$auth}");
  81. } else {
  82. $authoptions[$disabled][$auth] = get_string('pluginname', "auth_{$auth}");
  83. }
  84. }
  85. $mform->addElement('text', 'username', get_string('username'), 'size="20"');
  86. $mform->addHelpButton('username', 'username', 'auth');
  87. $mform->setType('username', core_user::get_property_type('username'));
  88. if ($userid !== -1) {
  89. $mform->disabledIf('username', 'auth', 'in', $cannotchangeusername);
  90. }
  91. $mform->addElement('selectgroups', 'auth', get_string('chooseauthmethod', 'auth'), $authoptions);
  92. $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
  93. $mform->addElement('advcheckbox', 'suspended', get_string('suspended', 'auth'));
  94. $mform->addHelpButton('suspended', 'suspended', 'auth');
  95. $mform->addElement('checkbox', 'createpassword', get_string('createpassword', 'auth'));
  96. $mform->disabledIf('createpassword', 'auth', 'in', $cannotchangepass);
  97. if (!empty($CFG->passwordpolicy)) {
  98. $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
  99. }
  100. $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
  101. $mform->addHelpButton('newpassword', 'newpassword');
  102. $mform->setType('newpassword', core_user::get_property_type('password'));
  103. $mform->disabledIf('newpassword', 'createpassword', 'checked');
  104. $mform->disabledIf('newpassword', 'auth', 'in', $cannotchangepass);
  105. $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
  106. $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
  107. $mform->disabledIf('preference_auth_forcepasswordchange', 'createpassword', 'checked');
  108. // Shared fields.
  109. useredit_shared_definition($mform, $editoroptions, $filemanageroptions, $user);
  110. // Next the customisable profile fields.
  111. profile_definition($mform, $userid);
  112. if ($userid == -1) {
  113. $btnstring = get_string('createuser');
  114. } else {
  115. $btnstring = get_string('updatemyprofile');
  116. }
  117. $this->add_action_buttons(false, $btnstring);
  118. $this->set_data($user);
  119. }
  120. /**
  121. * Extend the form definition after data has been parsed.
  122. */
  123. public function definition_after_data() {
  124. global $USER, $CFG, $DB, $OUTPUT;
  125. $mform = $this->_form;
  126. // Trim required name fields.
  127. foreach (useredit_get_required_name_fields() as $field) {
  128. $mform->applyFilter($field, 'trim');
  129. }
  130. if ($userid = $mform->getElementValue('id')) {
  131. $user = $DB->get_record('user', array('id' => $userid));
  132. } else {
  133. $user = false;
  134. }
  135. // User can not change own auth method.
  136. if ($userid == $USER->id) {
  137. $mform->hardFreeze('auth');
  138. $mform->hardFreeze('preference_auth_forcepasswordchange');
  139. }
  140. // Admin must choose some password and supply correct email.
  141. if (!empty($USER->newadminuser)) {
  142. $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
  143. if ($mform->elementExists('suspended')) {
  144. $mform->removeElement('suspended');
  145. }
  146. }
  147. // Require password for new users.
  148. if ($userid > 0) {
  149. if ($mform->elementExists('createpassword')) {
  150. $mform->removeElement('createpassword');
  151. }
  152. }
  153. if ($user and is_mnet_remote_user($user)) {
  154. // Only local accounts can be suspended.
  155. if ($mform->elementExists('suspended')) {
  156. $mform->removeElement('suspended');
  157. }
  158. }
  159. if ($user and ($user->id == $USER->id or is_siteadmin($user))) {
  160. // Prevent self and admin mess ups.
  161. if ($mform->elementExists('suspended')) {
  162. $mform->hardFreeze('suspended');
  163. }
  164. }
  165. // Print picture.
  166. if (empty($USER->newadminuser)) {
  167. if ($user) {
  168. $context = context_user::instance($user->id, MUST_EXIST);
  169. $fs = get_file_storage();
  170. $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
  171. if (!empty($user->picture) && $hasuploadedpicture) {
  172. $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size' => 64));
  173. } else {
  174. $imagevalue = get_string('none');
  175. }
  176. } else {
  177. $imagevalue = get_string('none');
  178. }
  179. $imageelement = $mform->getElement('currentpicture');
  180. $imageelement->setValue($imagevalue);
  181. if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
  182. $mform->removeElement('deletepicture');
  183. }
  184. }
  185. // Next the customisable profile fields.
  186. profile_definition_after_data($mform, $userid);
  187. }
  188. /**
  189. * Validate the form data.
  190. * @param array $usernew
  191. * @param array $files
  192. * @return array|bool
  193. */
  194. public function validation($usernew, $files) {
  195. global $CFG, $DB;
  196. $usernew = (object)$usernew;
  197. $usernew->username = trim($usernew->username);
  198. $user = $DB->get_record('user', array('id' => $usernew->id));
  199. $err = array();
  200. if (!$user and !empty($usernew->createpassword)) {
  201. if ($usernew->suspended) {
  202. // Show some error because we can not mail suspended users.
  203. $err['suspended'] = get_string('error');
  204. }
  205. } else {
  206. if (!empty($usernew->newpassword)) {
  207. $errmsg = ''; // Prevent eclipse warning.
  208. if (!check_password_policy($usernew->newpassword, $errmsg)) {
  209. $err['newpassword'] = $errmsg;
  210. }
  211. } else if (!$user) {
  212. $auth = get_auth_plugin($usernew->auth);
  213. if ($auth->is_internal()) {
  214. // Internal accounts require password!
  215. $err['newpassword'] = get_string('required');
  216. }
  217. }
  218. }
  219. if (empty($usernew->username)) {
  220. // Might be only whitespace.
  221. $err['username'] = get_string('required');
  222. } else if (!$user or $user->username !== $usernew->username) {
  223. // Check new username does not exist.
  224. if ($DB->record_exists('user', array('username' => $usernew->username, 'mnethostid' => $CFG->mnet_localhost_id))) {
  225. $err['username'] = get_string('usernameexists');
  226. }
  227. // Check allowed characters.
  228. if ($usernew->username !== core_text::strtolower($usernew->username)) {
  229. $err['username'] = get_string('usernamelowercase');
  230. } else {
  231. if ($usernew->username !== core_user::clean_field($usernew->username, 'username')) {
  232. $err['username'] = get_string('invalidusername');
  233. }
  234. }
  235. }
  236. if (!$user or (isset($usernew->email) && $user->email !== $usernew->email)) {
  237. if (!validate_email($usernew->email)) {
  238. $err['email'] = get_string('invalidemail');
  239. } else if (empty($CFG->allowaccountssameemail)
  240. and $DB->record_exists('user', array('email' => $usernew->email, 'mnethostid' => $CFG->mnet_localhost_id))) {
  241. $err['email'] = get_string('emailexists');
  242. }
  243. }
  244. // Next the customisable profile fields.
  245. $err += profile_validation($usernew, $files);
  246. if (count($err) == 0) {
  247. return true;
  248. } else {
  249. return $err;
  250. }
  251. }
  252. }