PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/user/editlib.php

https://github.com/lsuits/moodle
PHP | 486 lines | 297 code | 68 blank | 121 comment | 48 complexity | b715f2a103c26c03582bb7a3c01e0ef5 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, BSD-3-Clause, Apache-2.0
  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. * This file contains function used when editing a users profile and preferences.
  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. /**
  24. * Cancels the requirement for a user to update their email address.
  25. *
  26. * @param int $userid
  27. */
  28. function cancel_email_update($userid) {
  29. unset_user_preference('newemail', $userid);
  30. unset_user_preference('newemailkey', $userid);
  31. unset_user_preference('newemailattemptsleft', $userid);
  32. }
  33. /**
  34. * Loads the given users preferences into the given user object.
  35. *
  36. * @param stdClass $user The user object, modified by reference.
  37. * @param bool $reload
  38. */
  39. function useredit_load_preferences(&$user, $reload=true) {
  40. global $USER;
  41. if (!empty($user->id)) {
  42. if ($reload and $USER->id == $user->id) {
  43. // Reload preferences in case it was changed in other session.
  44. unset($USER->preference);
  45. }
  46. if ($preferences = get_user_preferences(null, null, $user->id)) {
  47. foreach ($preferences as $name => $value) {
  48. $user->{'preference_'.$name} = $value;
  49. }
  50. }
  51. }
  52. }
  53. /**
  54. * Updates the user preferences for teh given user.
  55. *
  56. * @param stdClass|array $usernew
  57. */
  58. function useredit_update_user_preference($usernew) {
  59. $ua = (array)$usernew;
  60. foreach ($ua as $key => $value) {
  61. if (strpos($key, 'preference_') === 0) {
  62. $name = substr($key, strlen('preference_'));
  63. set_user_preference($name, $value, $usernew->id);
  64. }
  65. }
  66. }
  67. /**
  68. * Updates the provided users profile picture based upon the expected fields returned from the edit or edit_advanced forms.
  69. *
  70. * @global moodle_database $DB
  71. * @param stdClass $usernew An object that contains some information about the user being updated
  72. * @param moodleform $userform The form that was submitted to edit the form
  73. * @param array $filemanageroptions
  74. * @return bool True if the user was updated, false if it stayed the same.
  75. */
  76. function useredit_update_picture(stdClass $usernew, moodleform $userform, $filemanageroptions = array()) {
  77. global $CFG, $DB;
  78. require_once("$CFG->libdir/gdlib.php");
  79. $context = context_user::instance($usernew->id, MUST_EXIST);
  80. $user = $DB->get_record('user', array('id' => $usernew->id), 'id, picture', MUST_EXIST);
  81. $newpicture = $user->picture;
  82. // Get file_storage to process files.
  83. $fs = get_file_storage();
  84. if (!empty($usernew->deletepicture)) {
  85. // The user has chosen to delete the selected users picture.
  86. $fs->delete_area_files($context->id, 'user', 'icon'); // Drop all images in area.
  87. $newpicture = 0;
  88. } else {
  89. // Save newly uploaded file, this will avoid context mismatch for newly created users.
  90. file_save_draft_area_files($usernew->imagefile, $context->id, 'user', 'newicon', 0, $filemanageroptions);
  91. if (($iconfiles = $fs->get_area_files($context->id, 'user', 'newicon')) && count($iconfiles) == 2) {
  92. // Get file which was uploaded in draft area.
  93. foreach ($iconfiles as $file) {
  94. if (!$file->is_directory()) {
  95. break;
  96. }
  97. }
  98. // Copy file to temporary location and the send it for processing icon.
  99. if ($iconfile = $file->copy_content_to_temp()) {
  100. // There is a new image that has been uploaded.
  101. // Process the new image and set the user to make use of it.
  102. // NOTE: Uploaded images always take over Gravatar.
  103. $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile);
  104. // Delete temporary file.
  105. @unlink($iconfile);
  106. // Remove uploaded file.
  107. $fs->delete_area_files($context->id, 'user', 'newicon');
  108. } else {
  109. // Something went wrong while creating temp file.
  110. // Remove uploaded file.
  111. $fs->delete_area_files($context->id, 'user', 'newicon');
  112. return false;
  113. }
  114. }
  115. }
  116. if ($newpicture != $user->picture) {
  117. $DB->set_field('user', 'picture', $newpicture, array('id' => $user->id));
  118. return true;
  119. } else {
  120. return false;
  121. }
  122. }
  123. /**
  124. * Updates the user email bounce + send counts when the user is edited.
  125. *
  126. * @param stdClass $user The current user object.
  127. * @param stdClass $usernew The updated user object.
  128. */
  129. function useredit_update_bounces($user, $usernew) {
  130. if (!isset($usernew->email)) {
  131. // Locked field.
  132. return;
  133. }
  134. if (!isset($user->email) || $user->email !== $usernew->email) {
  135. set_bounce_count($usernew, true);
  136. set_send_count($usernew, true);
  137. }
  138. }
  139. /**
  140. * Updates the forums a user is tracking when the user is edited.
  141. *
  142. * @param stdClass $user The original user object.
  143. * @param stdClass $usernew The updated user object.
  144. */
  145. function useredit_update_trackforums($user, $usernew) {
  146. global $CFG;
  147. if (!isset($usernew->trackforums)) {
  148. // Locked field.
  149. return;
  150. }
  151. if ((!isset($user->trackforums) || ($usernew->trackforums != $user->trackforums)) and !$usernew->trackforums) {
  152. require_once($CFG->dirroot.'/mod/forum/lib.php');
  153. forum_tp_delete_read_records($usernew->id);
  154. }
  155. }
  156. /**
  157. * Updates a users interests.
  158. *
  159. * @param stdClass $user
  160. * @param array $interests
  161. */
  162. function useredit_update_interests($user, $interests) {
  163. tag_set('user', $user->id, $interests, 'core', context_user::instance($user->id)->id);
  164. }
  165. /**
  166. * Powerful function that is used by edit and editadvanced to add common form elements/rules/etc.
  167. *
  168. * @param moodleform $mform
  169. * @param array|null $editoroptions
  170. * @param array|null $filemanageroptions
  171. */
  172. function useredit_shared_definition(&$mform, $editoroptions = null, $filemanageroptions = null) {
  173. global $CFG, $USER, $DB;
  174. $user = $DB->get_record('user', array('id' => $USER->id));
  175. useredit_load_preferences($user, false);
  176. $strrequired = get_string('required');
  177. // Add the necessary names.
  178. foreach (useredit_get_required_name_fields() as $fullname) {
  179. $mform->addElement('text', $fullname, get_string($fullname), 'maxlength="100" size="30"');
  180. $mform->addRule($fullname, $strrequired, 'required', null, 'client');
  181. $mform->setType($fullname, PARAM_NOTAGS);
  182. }
  183. $enabledusernamefields = useredit_get_enabled_name_fields();
  184. // Add the enabled additional name fields.
  185. foreach ($enabledusernamefields as $addname) {
  186. $mform->addElement('text', $addname, get_string($addname), 'maxlength="100" size="30"');
  187. $mform->setType($addname, PARAM_NOTAGS);
  188. }
  189. // Do not show email field if change confirmation is pending.
  190. if (!empty($CFG->emailchangeconfirmation) and !empty($user->preference_newemail)) {
  191. $notice = get_string('emailchangepending', 'auth', $user);
  192. $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
  193. . get_string('emailchangecancel', 'auth') . '</a>';
  194. $mform->addElement('static', 'emailpending', get_string('email'), $notice);
  195. } else {
  196. $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
  197. $mform->addRule('email', $strrequired, 'required', null, 'client');
  198. $mform->setType('email', PARAM_EMAIL);
  199. }
  200. $choices = array();
  201. $choices['0'] = get_string('emaildisplayno');
  202. $choices['1'] = get_string('emaildisplayyes');
  203. $choices['2'] = get_string('emaildisplaycourse');
  204. $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
  205. $mform->setDefault('maildisplay', 2);
  206. $choices = array();
  207. $choices['0'] = get_string('textformat');
  208. $choices['1'] = get_string('htmlformat');
  209. $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
  210. $mform->setDefault('mailformat', 1);
  211. if (!empty($CFG->allowusermailcharset)) {
  212. $choices = array();
  213. $charsets = get_list_of_charsets();
  214. if (!empty($CFG->sitemailcharset)) {
  215. $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
  216. } else {
  217. $choices['0'] = get_string('site').' (UTF-8)';
  218. }
  219. $choices = array_merge($choices, $charsets);
  220. $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
  221. }
  222. $choices = array();
  223. $choices['0'] = get_string('emaildigestoff');
  224. $choices['1'] = get_string('emaildigestcomplete');
  225. $choices['2'] = get_string('emaildigestsubjects');
  226. $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
  227. $mform->setDefault('maildigest', 0);
  228. $mform->addHelpButton('maildigest', 'emaildigest');
  229. $choices = array();
  230. $choices['1'] = get_string('autosubscribeyes');
  231. $choices['0'] = get_string('autosubscribeno');
  232. $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
  233. $mform->setDefault('autosubscribe', 1);
  234. if (!empty($CFG->forum_trackreadposts)) {
  235. $choices = array();
  236. $choices['0'] = get_string('trackforumsno');
  237. $choices['1'] = get_string('trackforumsyes');
  238. $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
  239. $mform->setDefault('trackforums', 0);
  240. }
  241. $editors = editors_get_enabled();
  242. if (count($editors) > 1) {
  243. $choices = array('' => get_string('defaulteditor'));
  244. $firsteditor = '';
  245. foreach (array_keys($editors) as $editor) {
  246. if (!$firsteditor) {
  247. $firsteditor = $editor;
  248. }
  249. $choices[$editor] = get_string('pluginname', 'editor_' . $editor);
  250. }
  251. $mform->addElement('select', 'preference_htmleditor', get_string('textediting'), $choices);
  252. $mform->setDefault('preference_htmleditor', '');
  253. } else {
  254. // Empty string means use the first chosen text editor.
  255. $mform->addElement('hidden', 'preference_htmleditor');
  256. $mform->setDefault('preference_htmleditor', '');
  257. $mform->setType('preference_htmleditor', PARAM_PLUGIN);
  258. }
  259. $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
  260. $mform->setType('city', PARAM_TEXT);
  261. if (!empty($CFG->defaultcity)) {
  262. $mform->setDefault('city', $CFG->defaultcity);
  263. }
  264. $choices = get_string_manager()->get_list_of_countries();
  265. $choices = array('' => get_string('selectacountry') . '...') + $choices;
  266. $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
  267. if (!empty($CFG->country)) {
  268. $mform->setDefault('country', $CFG->country);
  269. }
  270. $choices = get_list_of_timezones();
  271. $choices['99'] = get_string('serverlocaltime');
  272. if ($CFG->forcetimezone != 99) {
  273. $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
  274. } else {
  275. $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
  276. $mform->setDefault('timezone', '99');
  277. }
  278. $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
  279. $mform->setDefault('lang', $CFG->lang);
  280. // Multi-Calendar Support - see MDL-18375.
  281. $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
  282. // We do not want to show this option unless there is more than one calendar type to display.
  283. if (count($calendartypes) > 1) {
  284. $mform->addElement('select', 'calendartype', get_string('preferredcalendar', 'calendar'), $calendartypes);
  285. $mform->setDefault('calendartype', $CFG->calendartype);
  286. }
  287. if (!empty($CFG->allowuserthemes)) {
  288. $choices = array();
  289. $choices[''] = get_string('default');
  290. $themes = get_list_of_themes();
  291. foreach ($themes as $key => $theme) {
  292. if (empty($theme->hidefromselector)) {
  293. $choices[$key] = get_string('pluginname', 'theme_'.$theme->name);
  294. }
  295. }
  296. $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
  297. }
  298. $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
  299. $mform->setType('description_editor', PARAM_CLEANHTML);
  300. $mform->addHelpButton('description_editor', 'userdescription');
  301. if (empty($USER->newadminuser)) {
  302. $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
  303. if (!empty($CFG->enablegravatar)) {
  304. $mform->addElement('html', html_writer::tag('p', get_string('gravatarenabled')));
  305. }
  306. $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
  307. $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
  308. $mform->setDefault('deletepicture', 0);
  309. $mform->addElement('filemanager', 'imagefile', get_string('newpicture'), '', $filemanageroptions);
  310. $mform->addHelpButton('imagefile', 'newpicture');
  311. $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
  312. $mform->setType('imagealt', PARAM_TEXT);
  313. }
  314. // Display user name fields that are not currenlty enabled here if there are any.
  315. $disabledusernamefields = useredit_get_disabled_name_fields($enabledusernamefields);
  316. if (count($disabledusernamefields) > 0) {
  317. $mform->addElement('header', 'moodle_additional_names', get_string('additionalnames'));
  318. foreach ($disabledusernamefields as $allname) {
  319. $mform->addElement('text', $allname, get_string($allname), 'maxlength="100" size="30"');
  320. $mform->setType($allname, PARAM_NOTAGS);
  321. }
  322. }
  323. if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
  324. $mform->addElement('header', 'moodle_interests', get_string('interests'));
  325. $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
  326. $mform->addHelpButton('interests', 'interestslist');
  327. }
  328. // Moodle optional fields.
  329. $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
  330. $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
  331. $mform->setType('url', PARAM_URL);
  332. $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
  333. $mform->setType('icq', PARAM_NOTAGS);
  334. $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
  335. $mform->setType('skype', PARAM_NOTAGS);
  336. $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
  337. $mform->setType('aim', PARAM_NOTAGS);
  338. $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
  339. $mform->setType('yahoo', PARAM_NOTAGS);
  340. $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
  341. $mform->setType('msn', PARAM_NOTAGS);
  342. $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
  343. $mform->setType('idnumber', PARAM_NOTAGS);
  344. $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"');
  345. $mform->setType('institution', PARAM_TEXT);
  346. $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"');
  347. $mform->setType('department', PARAM_TEXT);
  348. $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
  349. $mform->setType('phone1', PARAM_NOTAGS);
  350. $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
  351. $mform->setType('phone2', PARAM_NOTAGS);
  352. $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"');
  353. $mform->setType('address', PARAM_TEXT);
  354. }
  355. /**
  356. * Return required user name fields for forms.
  357. *
  358. * @return array required user name fields in order according to settings.
  359. */
  360. function useredit_get_required_name_fields() {
  361. global $CFG;
  362. // Get the name display format.
  363. $nameformat = $CFG->fullnamedisplay;
  364. // Names that are required fields on user forms.
  365. $necessarynames = array('firstname', 'lastname');
  366. $languageformat = get_string('fullnamedisplay');
  367. // Check that the language string and the $nameformat contain the necessary names.
  368. foreach ($necessarynames as $necessaryname) {
  369. $pattern = "/$necessaryname\b/";
  370. if (!preg_match($pattern, $languageformat)) {
  371. // If the language string has been altered then fall back on the below order.
  372. $languageformat = 'firstname lastname';
  373. }
  374. if (!preg_match($pattern, $nameformat)) {
  375. // If the nameformat doesn't contain the necessary name fields then use the languageformat.
  376. $nameformat = $languageformat;
  377. }
  378. }
  379. // Order all of the name fields in the postion they are written in the fullnamedisplay setting.
  380. $necessarynames = order_in_string($necessarynames, $nameformat);
  381. return $necessarynames;
  382. }
  383. /**
  384. * Gets enabled (from fullnameformate setting) user name fields in appropriate order.
  385. *
  386. * @return array Enabled user name fields.
  387. */
  388. function useredit_get_enabled_name_fields() {
  389. global $CFG;
  390. // Get all of the other name fields which are not ranked as necessary.
  391. $additionalusernamefields = array_diff(get_all_user_name_fields(), array('firstname', 'lastname'));
  392. // Find out which additional name fields are actually being used from the fullnamedisplay setting.
  393. $enabledadditionalusernames = array();
  394. foreach ($additionalusernamefields as $enabledname) {
  395. if (strpos($CFG->fullnamedisplay, $enabledname) !== false) {
  396. $enabledadditionalusernames[] = $enabledname;
  397. }
  398. }
  399. // Order all of the name fields in the postion they are written in the fullnamedisplay setting.
  400. $enabledadditionalusernames = order_in_string($enabledadditionalusernames, $CFG->fullnamedisplay);
  401. return $enabledadditionalusernames;
  402. }
  403. /**
  404. * Gets user name fields not enabled from the setting fullnamedisplay.
  405. *
  406. * @param array $enabledadditionalusernames Current enabled additional user name fields.
  407. * @return array Disabled user name fields.
  408. */
  409. function useredit_get_disabled_name_fields($enabledadditionalusernames = null) {
  410. // If we don't have enabled additional user name information then go and fetch it (try to avoid).
  411. if (!isset($enabledadditionalusernames)) {
  412. $enabledadditionalusernames = useredit_get_enabled_name_fields();
  413. }
  414. // These are the additional fields that are not currently enabled.
  415. $nonusednamefields = array_diff(get_all_user_name_fields(),
  416. array_merge(array('firstname', 'lastname'), $enabledadditionalusernames));
  417. return $nonusednamefields;
  418. }