PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/user/editlib.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 331 lines | 247 code | 58 blank | 26 comment | 44 complexity | b85b6e3cecddb778343a219303a1248a MD5 | raw file
  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 = context_user::instance($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. $mform->setType('email', PARAM_EMAIL);
  139. }
  140. $choices = array();
  141. $choices['0'] = get_string('emaildisplayno');
  142. $choices['1'] = get_string('emaildisplayyes');
  143. $choices['2'] = get_string('emaildisplaycourse');
  144. $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
  145. $mform->setDefault('maildisplay', 2);
  146. $choices = array();
  147. $choices['0'] = get_string('textformat');
  148. $choices['1'] = get_string('htmlformat');
  149. $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
  150. $mform->setDefault('mailformat', 1);
  151. if (!empty($CFG->allowusermailcharset)) {
  152. $choices = array();
  153. $charsets = get_list_of_charsets();
  154. if (!empty($CFG->sitemailcharset)) {
  155. $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
  156. } else {
  157. $choices['0'] = get_string('site').' (UTF-8)';
  158. }
  159. $choices = array_merge($choices, $charsets);
  160. $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
  161. }
  162. $choices = array();
  163. $choices['0'] = get_string('emaildigestoff');
  164. $choices['1'] = get_string('emaildigestcomplete');
  165. $choices['2'] = get_string('emaildigestsubjects');
  166. $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
  167. $mform->setDefault('maildigest', 0);
  168. $choices = array();
  169. $choices['1'] = get_string('autosubscribeyes');
  170. $choices['0'] = get_string('autosubscribeno');
  171. $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
  172. $mform->setDefault('autosubscribe', 1);
  173. if (!empty($CFG->forum_trackreadposts)) {
  174. $choices = array();
  175. $choices['0'] = get_string('trackforumsno');
  176. $choices['1'] = get_string('trackforumsyes');
  177. $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
  178. $mform->setDefault('trackforums', 0);
  179. }
  180. $editors = editors_get_enabled();
  181. if (count($editors) > 1) {
  182. $choices = array();
  183. $choices['0'] = get_string('texteditor');
  184. $choices['1'] = get_string('htmleditor');
  185. $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
  186. $mform->setDefault('htmleditor', 1);
  187. } else {
  188. $mform->addElement('hidden', 'htmleditor');
  189. $mform->setDefault('htmleditor', 1);
  190. $mform->setType('htmleditor', PARAM_INT);
  191. }
  192. $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
  193. $mform->setType('city', PARAM_TEXT);
  194. $mform->addRule('city', $strrequired, 'required', null, 'client');
  195. if (!empty($CFG->defaultcity)) {
  196. $mform->setDefault('city', $CFG->defaultcity);
  197. }
  198. $choices = get_string_manager()->get_list_of_countries();
  199. $choices= array(''=>get_string('selectacountry').'...') + $choices;
  200. $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
  201. $mform->addRule('country', $strrequired, 'required', null, 'client');
  202. if (!empty($CFG->country)) {
  203. $mform->setDefault('country', $CFG->country);
  204. }
  205. $choices = get_list_of_timezones();
  206. $choices['99'] = get_string('serverlocaltime');
  207. if ($CFG->forcetimezone != 99) {
  208. $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
  209. } else {
  210. $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
  211. $mform->setDefault('timezone', '99');
  212. }
  213. $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
  214. $mform->setDefault('lang', $CFG->lang);
  215. if (!empty($CFG->allowuserthemes)) {
  216. $choices = array();
  217. $choices[''] = get_string('default');
  218. $themes = get_list_of_themes();
  219. foreach ($themes as $key=>$theme) {
  220. if (empty($theme->hidefromselector)) {
  221. $choices[$key] = get_string('pluginname', 'theme_'.$theme->name);
  222. }
  223. }
  224. $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
  225. }
  226. $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
  227. $mform->setType('description_editor', PARAM_CLEANHTML);
  228. $mform->addHelpButton('description_editor', 'userdescription');
  229. if (empty($USER->newadminuser)) {
  230. $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
  231. if (!empty($CFG->enablegravatar)) {
  232. $mform->addElement('html', html_writer::tag('p', get_string('gravatarenabled')));
  233. }
  234. $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
  235. $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
  236. $mform->setDefault('deletepicture', 0);
  237. $mform->addElement('filemanager', 'imagefile', get_string('newpicture'), '', $filemanageroptions);
  238. $mform->addHelpButton('imagefile', 'newpicture');
  239. $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
  240. $mform->setType('imagealt', PARAM_TEXT);
  241. }
  242. if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
  243. $mform->addElement('header', 'moodle_interests', get_string('interests'));
  244. $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
  245. $mform->addHelpButton('interests', 'interestslist');
  246. }
  247. /// Moodle optional fields
  248. $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
  249. $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
  250. $mform->setType('url', PARAM_URL);
  251. $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
  252. $mform->setType('icq', PARAM_NOTAGS);
  253. $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
  254. $mform->setType('skype', PARAM_NOTAGS);
  255. $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
  256. $mform->setType('aim', PARAM_NOTAGS);
  257. $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
  258. $mform->setType('yahoo', PARAM_NOTAGS);
  259. $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
  260. $mform->setType('msn', PARAM_NOTAGS);
  261. $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
  262. $mform->setType('idnumber', PARAM_NOTAGS);
  263. $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
  264. $mform->setType('institution', PARAM_TEXT);
  265. $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
  266. $mform->setType('department', PARAM_TEXT);
  267. $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
  268. $mform->setType('phone1', PARAM_NOTAGS);
  269. $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
  270. $mform->setType('phone2', PARAM_NOTAGS);
  271. $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
  272. $mform->setType('address', PARAM_TEXT);
  273. }