PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/user/editlib.php

https://github.com/mijiacang/moodle
PHP | 299 lines | 235 code | 59 blank | 5 comment | 37 complexity | 3e069b8f1a700c3ce9d91467f2f7535f MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, BSD-3-Clause, AGPL-3.0, MPL-2.0-no-copyleft-exception, Apache-2.0
  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. function useredit_update_picture(&$usernew, $userform) {
  31. global $CFG, $DB;
  32. require_once("$CFG->libdir/gdlib.php");
  33. $fs = get_file_storage();
  34. $context = get_context_instance(CONTEXT_USER, $usernew->id, MUST_EXIST);
  35. if (isset($usernew->deletepicture) and $usernew->deletepicture) {
  36. $fs->delete_area_files($context->id, 'user', 'icon'); // drop all areas
  37. $DB->set_field('user', 'picture', 0, array('id'=>$usernew->id));
  38. } else if ($iconfile = $userform->save_temp_file('imagefile')) {
  39. if (process_new_icon($context, 'user', 'icon', 0, $iconfile)) {
  40. $DB->set_field('user', 'picture', 1, array('id'=>$usernew->id));
  41. }
  42. @unlink($iconfile);
  43. }
  44. }
  45. function useredit_update_bounces($user, $usernew) {
  46. if (!isset($usernew->email)) {
  47. //locked field
  48. return;
  49. }
  50. if (!isset($user->email) || $user->email !== $usernew->email) {
  51. set_bounce_count($usernew,true);
  52. set_send_count($usernew,true);
  53. }
  54. }
  55. function useredit_update_trackforums($user, $usernew) {
  56. global $CFG;
  57. if (!isset($usernew->trackforums)) {
  58. //locked field
  59. return;
  60. }
  61. if ((!isset($user->trackforums) || ($usernew->trackforums != $user->trackforums)) and !$usernew->trackforums) {
  62. require_once($CFG->dirroot.'/mod/forum/lib.php');
  63. forum_tp_delete_read_records($usernew->id);
  64. }
  65. }
  66. function useredit_update_interests($user, $interests) {
  67. tag_set('user', $user->id, $interests);
  68. }
  69. function useredit_shared_definition(&$mform, $editoroptions = null) {
  70. global $CFG, $USER, $DB;
  71. $user = $DB->get_record('user', array('id' => $USER->id));
  72. useredit_load_preferences($user, false);
  73. $strrequired = get_string('required');
  74. $nameordercheck = new stdClass();
  75. $nameordercheck->firstname = 'a';
  76. $nameordercheck->lastname = 'b';
  77. if (fullname($nameordercheck) == 'b a' ) { // See MDL-4325
  78. $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
  79. $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
  80. } else {
  81. $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
  82. $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
  83. }
  84. $mform->addRule('firstname', $strrequired, 'required', null, 'client');
  85. $mform->setType('firstname', PARAM_NOTAGS);
  86. $mform->addRule('lastname', $strrequired, 'required', null, 'client');
  87. $mform->setType('lastname', PARAM_NOTAGS);
  88. // Do not show email field if change confirmation is pending
  89. if (!empty($CFG->emailchangeconfirmation) and !empty($user->preference_newemail)) {
  90. $notice = get_string('auth_emailchangepending', 'auth_email', $user);
  91. $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
  92. . get_string('auth_emailchangecancel', 'auth_email') . '</a>';
  93. $mform->addElement('static', 'emailpending', get_string('email'), $notice);
  94. } else {
  95. $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
  96. $mform->addRule('email', $strrequired, 'required', null, 'client');
  97. }
  98. $choices = array();
  99. $choices['0'] = get_string('emaildisplayno');
  100. $choices['1'] = get_string('emaildisplayyes');
  101. $choices['2'] = get_string('emaildisplaycourse');
  102. $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
  103. $mform->setDefault('maildisplay', 2);
  104. $choices = array();
  105. $choices['0'] = get_string('textformat');
  106. $choices['1'] = get_string('htmlformat');
  107. $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
  108. $mform->setDefault('mailformat', 1);
  109. if (!empty($CFG->allowusermailcharset)) {
  110. $choices = array();
  111. $charsets = get_list_of_charsets();
  112. if (!empty($CFG->sitemailcharset)) {
  113. $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
  114. } else {
  115. $choices['0'] = get_string('site').' (UTF-8)';
  116. }
  117. $choices = array_merge($choices, $charsets);
  118. $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
  119. }
  120. $choices = array();
  121. $choices['0'] = get_string('emaildigestoff');
  122. $choices['1'] = get_string('emaildigestcomplete');
  123. $choices['2'] = get_string('emaildigestsubjects');
  124. $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
  125. $mform->setDefault('maildigest', 0);
  126. $choices = array();
  127. $choices['1'] = get_string('autosubscribeyes');
  128. $choices['0'] = get_string('autosubscribeno');
  129. $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
  130. $mform->setDefault('autosubscribe', 1);
  131. if (!empty($CFG->forum_trackreadposts)) {
  132. $choices = array();
  133. $choices['0'] = get_string('trackforumsno');
  134. $choices['1'] = get_string('trackforumsyes');
  135. $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
  136. $mform->setDefault('trackforums', 0);
  137. }
  138. $editors = editors_get_enabled();
  139. if (count($editors) > 1) {
  140. $choices = array();
  141. $choices['0'] = get_string('texteditor');
  142. $choices['1'] = get_string('htmleditor');
  143. $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
  144. $mform->setDefault('htmleditor', 1);
  145. } else {
  146. $mform->addElement('hidden', 'htmleditor');
  147. $mform->setDefault('htmleditor', 1);
  148. $mform->setType('htmleditor', PARAM_INT);
  149. }
  150. if (empty($CFG->enableajax)) {
  151. $mform->addElement('static', 'ajaxdisabled', get_string('ajaxuse'), get_string('ajaxno'));
  152. } else {
  153. $choices = array();
  154. $choices['0'] = get_string('ajaxno');
  155. $choices['1'] = get_string('ajaxyes');
  156. $mform->addElement('select', 'ajax', get_string('ajaxuse'), $choices);
  157. $mform->setDefault('ajax', 0);
  158. }
  159. $choices = array();
  160. $choices['0'] = get_string('screenreaderno');
  161. $choices['1'] = get_string('screenreaderyes');
  162. $mform->addElement('select', 'screenreader', get_string('screenreaderuse'), $choices);
  163. $mform->setDefault('screenreader', 0);
  164. $mform->addHelpButton('screenreader', 'screenreaderuse');
  165. $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
  166. $mform->setType('city', PARAM_MULTILANG);
  167. $mform->addRule('city', $strrequired, 'required', null, 'client');
  168. $choices = get_string_manager()->get_list_of_countries();
  169. $choices= array(''=>get_string('selectacountry').'...') + $choices;
  170. $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
  171. $mform->addRule('country', $strrequired, 'required', null, 'client');
  172. if (!empty($CFG->country)) {
  173. $mform->setDefault('country', $CFG->country);
  174. }
  175. $choices = get_list_of_timezones();
  176. $choices['99'] = get_string('serverlocaltime');
  177. if ($CFG->forcetimezone != 99) {
  178. $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
  179. } else {
  180. $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
  181. $mform->setDefault('timezone', '99');
  182. }
  183. $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
  184. $mform->setDefault('lang', $CFG->lang);
  185. if (!empty($CFG->allowuserthemes)) {
  186. $choices = array();
  187. $choices[''] = get_string('default');
  188. $themes = get_list_of_themes();
  189. foreach ($themes as $key=>$theme) {
  190. if (empty($theme->hidefromselector)) {
  191. $choices[$key] = $theme->name;
  192. }
  193. }
  194. $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
  195. }
  196. $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
  197. $mform->setType('description_editor', PARAM_CLEANHTML);
  198. $mform->addHelpButton('description_editor', 'userdescription');
  199. if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
  200. $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
  201. $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
  202. $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
  203. $mform->setDefault('deletepicture', 0);
  204. $mform->addElement('filepicker', 'imagefile', get_string('newpicture'), '', array('maxbytes'=>get_max_upload_file_size($CFG->maxbytes)));
  205. $mform->addHelpButton('imagefile', 'newpicture');
  206. $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
  207. $mform->setType('imagealt', PARAM_MULTILANG);
  208. }
  209. if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
  210. $mform->addElement('header', 'moodle_interests', get_string('interests'));
  211. $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
  212. $mform->addHelpButton('interests', 'interestslist');
  213. }
  214. /// Moodle optional fields
  215. $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
  216. $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
  217. $mform->setType('url', PARAM_URL);
  218. $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
  219. $mform->setType('icq', PARAM_NOTAGS);
  220. $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
  221. $mform->setType('skype', PARAM_NOTAGS);
  222. $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
  223. $mform->setType('aim', PARAM_NOTAGS);
  224. $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
  225. $mform->setType('yahoo', PARAM_NOTAGS);
  226. $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
  227. $mform->setType('msn', PARAM_NOTAGS);
  228. $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
  229. $mform->setType('idnumber', PARAM_NOTAGS);
  230. $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
  231. $mform->setType('institution', PARAM_MULTILANG);
  232. $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
  233. $mform->setType('department', PARAM_MULTILANG);
  234. $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
  235. $mform->setType('phone1', PARAM_NOTAGS);
  236. $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
  237. $mform->setType('phone2', PARAM_NOTAGS);
  238. $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
  239. $mform->setType('address', PARAM_MULTILANG);
  240. }