PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/user/editlib.php

https://bitbucket.org/ngmares/moodle
PHP | 337 lines | 252 code | 59 blank | 26 comment | 44 complexity | e0e1d525290d9697ccecfd07e8efebe4 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. function cancel_email_update($userid) {
  3. unset_user_preference('newemail', $userid);
  4. unset_user_preference('newemailkey', $userid);
  5. unset_user_preference('newemailattemptsleft', $userid);
  6. }
  7. function useredit_load_preferences(&$user, $reload=true) {
  8. global $USER;
  9. if (!empty($user->id)) {
  10. if ($reload and $USER->id == $user->id) {
  11. // reload preferences in case it was changed in other session
  12. unset($USER->preference);
  13. }
  14. if ($preferences = get_user_preferences(null, null, $user->id)) {
  15. foreach($preferences as $name=>$value) {
  16. $user->{'preference_'.$name} = $value;
  17. }
  18. }
  19. }
  20. }
  21. function useredit_update_user_preference($usernew) {
  22. $ua = (array)$usernew;
  23. foreach($ua as $key=>$value) {
  24. if (strpos($key, 'preference_') === 0) {
  25. $name = substr($key, strlen('preference_'));
  26. set_user_preference($name, $value, $usernew->id);
  27. }
  28. }
  29. }
  30. /**
  31. * Updates the provided users profile picture based upon the expected fields
  32. * returned from the edit or edit_advanced forms.
  33. *
  34. * @global moodle_database $DB
  35. * @param stdClass $usernew An object that contains some information about the user being updated
  36. * @param moodleform $userform The form that was submitted to edit the form
  37. * @return bool True if the user was updated, false if it stayed the same.
  38. */
  39. function useredit_update_picture(stdClass $usernew, moodleform $userform, $filemanageroptions = array()) {
  40. global $CFG, $DB;
  41. require_once("$CFG->libdir/gdlib.php");
  42. $context = get_context_instance(CONTEXT_USER, $usernew->id, MUST_EXIST);
  43. $user = $DB->get_record('user', array('id'=>$usernew->id), 'id, picture', MUST_EXIST);
  44. $newpicture = $user->picture;
  45. // Get file_storage to process files.
  46. $fs = get_file_storage();
  47. if (!empty($usernew->deletepicture)) {
  48. // The user has chosen to delete the selected users picture
  49. $fs->delete_area_files($context->id, 'user', 'icon'); // drop all images in area
  50. $newpicture = 0;
  51. } else {
  52. // Save newly uploaded file, this will avoid context mismatch for newly created users.
  53. file_save_draft_area_files($usernew->imagefile, $context->id, 'user', 'newicon', 0, $filemanageroptions);
  54. if (($iconfiles = $fs->get_area_files($context->id, 'user', 'newicon')) && count($iconfiles) == 2) {
  55. // Get file which was uploaded in draft area
  56. foreach ($iconfiles as $file) {
  57. if (!$file->is_directory()) {
  58. break;
  59. }
  60. }
  61. // Copy file to temporary location and the send it for processing icon
  62. if ($iconfile = $file->copy_content_to_temp()) {
  63. // There is a new image that has been uploaded
  64. // Process the new image and set the user to make use of it.
  65. // NOTE: Uploaded images always take over Gravatar
  66. $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile);
  67. // Delete temporary file
  68. @unlink($iconfile);
  69. // Remove uploaded file.
  70. $fs->delete_area_files($context->id, 'user', 'newicon');
  71. } else {
  72. // Something went wrong while creating temp file.
  73. // Remove uploaded file.
  74. $fs->delete_area_files($context->id, 'user', 'newicon');
  75. return false;
  76. }
  77. }
  78. }
  79. if ($newpicture != $user->picture) {
  80. $DB->set_field('user', 'picture', $newpicture, array('id' => $user->id));
  81. return true;
  82. } else {
  83. return false;
  84. }
  85. }
  86. function useredit_update_bounces($user, $usernew) {
  87. if (!isset($usernew->email)) {
  88. //locked field
  89. return;
  90. }
  91. if (!isset($user->email) || $user->email !== $usernew->email) {
  92. set_bounce_count($usernew,true);
  93. set_send_count($usernew,true);
  94. }
  95. }
  96. function useredit_update_trackforums($user, $usernew) {
  97. global $CFG;
  98. if (!isset($usernew->trackforums)) {
  99. //locked field
  100. return;
  101. }
  102. if ((!isset($user->trackforums) || ($usernew->trackforums != $user->trackforums)) and !$usernew->trackforums) {
  103. require_once($CFG->dirroot.'/mod/forum/lib.php');
  104. forum_tp_delete_read_records($usernew->id);
  105. }
  106. }
  107. function useredit_update_interests($user, $interests) {
  108. tag_set('user', $user->id, $interests);
  109. }
  110. function useredit_shared_definition(&$mform, $editoroptions = null, $filemanageroptions = null) {
  111. global $CFG, $USER, $DB;
  112. $user = $DB->get_record('user', array('id' => $USER->id));
  113. useredit_load_preferences($user, false);
  114. $strrequired = get_string('required');
  115. $nameordercheck = new stdClass();
  116. $nameordercheck->firstname = 'a';
  117. $nameordercheck->lastname = 'b';
  118. if (fullname($nameordercheck) == 'b a' ) { // See MDL-4325
  119. $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
  120. $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
  121. } else {
  122. $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
  123. $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
  124. }
  125. $mform->addRule('firstname', $strrequired, 'required', null, 'client');
  126. $mform->setType('firstname', PARAM_NOTAGS);
  127. $mform->addRule('lastname', $strrequired, 'required', null, 'client');
  128. $mform->setType('lastname', PARAM_NOTAGS);
  129. // Do not show email field if change confirmation is pending
  130. if (!empty($CFG->emailchangeconfirmation) and !empty($user->preference_newemail)) {
  131. $notice = get_string('emailchangepending', 'auth', $user);
  132. $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
  133. . get_string('emailchangecancel', 'auth') . '</a>';
  134. $mform->addElement('static', 'emailpending', get_string('email'), $notice);
  135. } else {
  136. $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
  137. $mform->addRule('email', $strrequired, 'required', null, 'client');
  138. }
  139. $choices = array();
  140. $choices['0'] = get_string('emaildisplayno');
  141. $choices['1'] = get_string('emaildisplayyes');
  142. $choices['2'] = get_string('emaildisplaycourse');
  143. $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
  144. $mform->setDefault('maildisplay', 2);
  145. $choices = array();
  146. $choices['0'] = get_string('textformat');
  147. $choices['1'] = get_string('htmlformat');
  148. $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
  149. $mform->setDefault('mailformat', 1);
  150. if (!empty($CFG->allowusermailcharset)) {
  151. $choices = array();
  152. $charsets = get_list_of_charsets();
  153. if (!empty($CFG->sitemailcharset)) {
  154. $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
  155. } else {
  156. $choices['0'] = get_string('site').' (UTF-8)';
  157. }
  158. $choices = array_merge($choices, $charsets);
  159. $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
  160. }
  161. $choices = array();
  162. $choices['0'] = get_string('emaildigestoff');
  163. $choices['1'] = get_string('emaildigestcomplete');
  164. $choices['2'] = get_string('emaildigestsubjects');
  165. $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
  166. $mform->setDefault('maildigest', 0);
  167. $choices = array();
  168. $choices['1'] = get_string('autosubscribeyes');
  169. $choices['0'] = get_string('autosubscribeno');
  170. $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
  171. $mform->setDefault('autosubscribe', 1);
  172. if (!empty($CFG->forum_trackreadposts)) {
  173. $choices = array();
  174. $choices['0'] = get_string('trackforumsno');
  175. $choices['1'] = get_string('trackforumsyes');
  176. $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
  177. $mform->setDefault('trackforums', 0);
  178. }
  179. $editors = editors_get_enabled();
  180. if (count($editors) > 1) {
  181. $choices = array();
  182. $choices['0'] = get_string('texteditor');
  183. $choices['1'] = get_string('htmleditor');
  184. $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
  185. $mform->setDefault('htmleditor', 1);
  186. } else {
  187. $mform->addElement('hidden', 'htmleditor');
  188. $mform->setDefault('htmleditor', 1);
  189. $mform->setType('htmleditor', PARAM_INT);
  190. }
  191. $choices = array();
  192. $choices['0'] = get_string('screenreaderno');
  193. $choices['1'] = get_string('screenreaderyes');
  194. $mform->addElement('select', 'screenreader', get_string('screenreaderuse'), $choices);
  195. $mform->setDefault('screenreader', 0);
  196. $mform->addHelpButton('screenreader', 'screenreaderuse');
  197. $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
  198. $mform->setType('city', PARAM_MULTILANG);
  199. $mform->addRule('city', $strrequired, 'required', null, 'client');
  200. if (!empty($CFG->defaultcity)) {
  201. $mform->setDefault('city', $CFG->defaultcity);
  202. }
  203. $choices = get_string_manager()->get_list_of_countries();
  204. $choices= array(''=>get_string('selectacountry').'...') + $choices;
  205. $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
  206. $mform->addRule('country', $strrequired, 'required', null, 'client');
  207. if (!empty($CFG->country)) {
  208. $mform->setDefault('country', $CFG->country);
  209. }
  210. $choices = get_list_of_timezones();
  211. $choices['99'] = get_string('serverlocaltime');
  212. if ($CFG->forcetimezone != 99) {
  213. $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
  214. } else {
  215. $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
  216. $mform->setDefault('timezone', '99');
  217. }
  218. $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
  219. $mform->setDefault('lang', $CFG->lang);
  220. if (!empty($CFG->allowuserthemes)) {
  221. $choices = array();
  222. $choices[''] = get_string('default');
  223. $themes = get_list_of_themes();
  224. foreach ($themes as $key=>$theme) {
  225. if (empty($theme->hidefromselector)) {
  226. $choices[$key] = get_string('pluginname', 'theme_'.$theme->name);
  227. }
  228. }
  229. $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
  230. }
  231. $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
  232. $mform->setType('description_editor', PARAM_CLEANHTML);
  233. $mform->addHelpButton('description_editor', 'userdescription');
  234. if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
  235. $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
  236. if (!empty($CFG->enablegravatar)) {
  237. $mform->addElement('html', html_writer::tag('p', get_string('gravatarenabled')));
  238. }
  239. $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
  240. $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
  241. $mform->setDefault('deletepicture', 0);
  242. $mform->addElement('filemanager', 'imagefile', get_string('newpicture'), '', $filemanageroptions);
  243. $mform->addHelpButton('imagefile', 'newpicture');
  244. $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
  245. $mform->setType('imagealt', PARAM_MULTILANG);
  246. }
  247. if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
  248. $mform->addElement('header', 'moodle_interests', get_string('interests'));
  249. $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
  250. $mform->addHelpButton('interests', 'interestslist');
  251. }
  252. /// Moodle optional fields
  253. $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
  254. $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
  255. $mform->setType('url', PARAM_URL);
  256. $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
  257. $mform->setType('icq', PARAM_NOTAGS);
  258. $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
  259. $mform->setType('skype', PARAM_NOTAGS);
  260. $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
  261. $mform->setType('aim', PARAM_NOTAGS);
  262. $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
  263. $mform->setType('yahoo', PARAM_NOTAGS);
  264. $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
  265. $mform->setType('msn', PARAM_NOTAGS);
  266. $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
  267. $mform->setType('idnumber', PARAM_NOTAGS);
  268. $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
  269. $mform->setType('institution', PARAM_MULTILANG);
  270. $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
  271. $mform->setType('department', PARAM_MULTILANG);
  272. $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
  273. $mform->setType('phone1', PARAM_NOTAGS);
  274. $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
  275. $mform->setType('phone2', PARAM_NOTAGS);
  276. $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
  277. $mform->setType('address', PARAM_MULTILANG);
  278. }