PageRenderTime 61ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/user/editadvanced_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 237 lines | 178 code | 39 blank | 20 comment | 57 complexity | 944a8a15aee6dfdaab07d8024dcc8d77 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_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 = core_component::get_plugin_list('auth');
  39. $enabled = get_string('pluginenabled', 'core_plugin');
  40. $disabled = get_string('plugindisabled', 'core_plugin');
  41. $auth_options = array($enabled=>array(), $disabled=>array());
  42. foreach ($auths as $auth => $unused) {
  43. if (is_enabled_auth($auth)) {
  44. $auth_options[$enabled][$auth] = get_string('pluginname', "auth_{$auth}");
  45. } else {
  46. $auth_options[$disabled][$auth] = get_string('pluginname', "auth_{$auth}");
  47. }
  48. }
  49. $mform->addElement('selectgroups', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
  50. $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
  51. $mform->addElement('advcheckbox', 'suspended', get_string('suspended','auth'));
  52. $mform->addHelpButton('suspended', 'suspended', 'auth');
  53. $mform->addElement('checkbox', 'createpassword', get_string('createpassword','auth'));
  54. if (!empty($CFG->passwordpolicy)){
  55. $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
  56. }
  57. $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
  58. $mform->addHelpButton('newpassword', 'newpassword');
  59. $mform->setType('newpassword', PARAM_RAW);
  60. $mform->disabledIf('newpassword', 'createpassword', 'checked');
  61. $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
  62. $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
  63. $mform->disabledIf('preference_auth_forcepasswordchange', 'createpassword', 'checked');
  64. /// shared fields
  65. useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
  66. /// Next the customisable profile fields
  67. profile_definition($mform, $userid);
  68. if ($userid == -1) {
  69. $btnstring = get_string('createuser');
  70. } else {
  71. $btnstring = get_string('updatemyprofile');
  72. }
  73. $this->add_action_buttons(false, $btnstring);
  74. }
  75. function definition_after_data() {
  76. global $USER, $CFG, $DB, $OUTPUT;
  77. $mform =& $this->_form;
  78. if ($userid = $mform->getElementValue('id')) {
  79. $user = $DB->get_record('user', array('id'=>$userid));
  80. } else {
  81. $user = false;
  82. }
  83. // if language does not exist, use site default lang
  84. if ($langsel = $mform->getElementValue('lang')) {
  85. $lang = reset($langsel);
  86. // check lang exists
  87. if (!get_string_manager()->translation_exists($lang, false)) {
  88. $lang_el =& $mform->getElement('lang');
  89. $lang_el->setValue($CFG->lang);
  90. }
  91. }
  92. // user can not change own auth method
  93. if ($userid == $USER->id) {
  94. $mform->hardFreeze('auth');
  95. $mform->hardFreeze('preference_auth_forcepasswordchange');
  96. }
  97. // admin must choose some password and supply correct email
  98. if (!empty($USER->newadminuser)) {
  99. $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
  100. if ($mform->elementExists('suspended')) {
  101. $mform->removeElement('suspended');
  102. }
  103. }
  104. // require password for new users
  105. if ($userid > 0) {
  106. if ($mform->elementExists('createpassword')) {
  107. $mform->removeElement('createpassword');
  108. }
  109. }
  110. if ($user and is_mnet_remote_user($user)) {
  111. // only local accounts can be suspended
  112. if ($mform->elementExists('suspended')) {
  113. $mform->removeElement('suspended');
  114. }
  115. }
  116. if ($user and ($user->id == $USER->id or is_siteadmin($user))) {
  117. // prevent self and admin mess ups
  118. if ($mform->elementExists('suspended')) {
  119. $mform->hardFreeze('suspended');
  120. }
  121. }
  122. // print picture
  123. if (empty($USER->newadminuser)) {
  124. if ($user) {
  125. $context = context_user::instance($user->id, MUST_EXIST);
  126. $fs = get_file_storage();
  127. $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
  128. if (!empty($user->picture) && $hasuploadedpicture) {
  129. $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
  130. } else {
  131. $imagevalue = get_string('none');
  132. }
  133. } else {
  134. $imagevalue = get_string('none');
  135. }
  136. $imageelement = $mform->getElement('currentpicture');
  137. $imageelement->setValue($imagevalue);
  138. if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
  139. $mform->removeElement('deletepicture');
  140. }
  141. }
  142. /// Next the customisable profile fields
  143. profile_definition_after_data($mform, $userid);
  144. }
  145. function validation($usernew, $files) {
  146. global $CFG, $DB;
  147. $usernew = (object)$usernew;
  148. $usernew->username = trim($usernew->username);
  149. $user = $DB->get_record('user', array('id'=>$usernew->id));
  150. $err = array();
  151. if (!$user and !empty($usernew->createpassword)) {
  152. if ($usernew->suspended) {
  153. // Show some error because we can not mail suspended users.
  154. $err['suspended'] = get_string('error');
  155. }
  156. } else {
  157. if (!empty($usernew->newpassword)) {
  158. $errmsg = ''; // Prevent eclipse warning.
  159. if (!check_password_policy($usernew->newpassword, $errmsg)) {
  160. $err['newpassword'] = $errmsg;
  161. }
  162. } else if (!$user) {
  163. $err['newpassword'] = get_string('required');
  164. }
  165. }
  166. if (empty($usernew->username)) {
  167. //might be only whitespace
  168. $err['username'] = get_string('required');
  169. } else if (!$user or $user->username !== $usernew->username) {
  170. //check new username does not exist
  171. if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
  172. $err['username'] = get_string('usernameexists');
  173. }
  174. //check allowed characters
  175. if ($usernew->username !== core_text::strtolower($usernew->username)) {
  176. $err['username'] = get_string('usernamelowercase');
  177. } else {
  178. if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) {
  179. $err['username'] = get_string('invalidusername');
  180. }
  181. }
  182. }
  183. if (!$user or $user->email !== $usernew->email) {
  184. if (!validate_email($usernew->email)) {
  185. $err['email'] = get_string('invalidemail');
  186. } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
  187. $err['email'] = get_string('emailexists');
  188. }
  189. }
  190. /// Next the customisable profile fields
  191. $err += profile_validation($usernew, $files);
  192. if (count($err) == 0){
  193. return true;
  194. } else {
  195. return $err;
  196. }
  197. }
  198. }