Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/user/edit.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00026 require_once('../config.php');
00027 require_once($CFG->libdir.'/gdlib.php');
00028 require_once($CFG->dirroot.'/user/edit_form.php');
00029 require_once($CFG->dirroot.'/user/editlib.php');
00030 require_once($CFG->dirroot.'/user/profile/lib.php');
00031 
00032 //HTTPS is required in this page when $CFG->loginhttps enabled
00033 $PAGE->https_required();
00034 
00035 $userid = optional_param('id', $USER->id, PARAM_INT);    // user id
00036 $course = optional_param('course', SITEID, PARAM_INT);   // course id (defaults to Site)
00037 $cancelemailchange = optional_param('cancelemailchange', 0, PARAM_INT);   // course id (defaults to Site)
00038 
00039 $PAGE->set_url('/user/edit.php', array('course'=>$course, 'id'=>$userid));
00040 
00041 if (!$course = $DB->get_record('course', array('id'=>$course))) {
00042     print_error('invalidcourseid');
00043 }
00044 
00045 if ($course->id != SITEID) {
00046     require_login($course);
00047 } else if (!isloggedin()) {
00048     if (empty($SESSION->wantsurl)) {
00049         $SESSION->wantsurl = $CFG->httpswwwroot.'/user/edit.php';
00050     }
00051     redirect(get_login_url());
00052 } else {
00053     $PAGE->set_context(get_system_context());
00054     $PAGE->set_pagelayout('standard');
00055 }
00056 
00057 // Guest can not edit
00058 if (isguestuser()) {
00059     print_error('guestnoeditprofile');
00060 }
00061 
00062 // The user profile we are editing
00063 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
00064     print_error('invaliduserid');
00065 }
00066 
00067 // Guest can not be edited
00068 if (isguestuser($user)) {
00069     print_error('guestnoeditprofile');
00070 }
00071 
00072 // User interests separated by commas
00073 if (!empty($CFG->usetags)) {
00074     require_once($CFG->dirroot.'/tag/lib.php');
00075     $user->interests = tag_get_tags_array('user', $user->id);
00076 }
00077 
00078 // remote users cannot be edited
00079 if (is_mnet_remote_user($user)) {
00080     if (user_not_fully_set_up($user)) {
00081         $hostwwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id'=>$user->mnethostid));
00082         print_error('usernotfullysetup', 'mnet', '', $hostwwwroot);
00083     }
00084     redirect($CFG->wwwroot . "/user/view.php?course={$course->id}");
00085 }
00086 
00087 // load the appropriate auth plugin
00088 $userauth = get_auth_plugin($user->auth);
00089 
00090 if (!$userauth->can_edit_profile()) {
00091     print_error('noprofileedit', 'auth');
00092 }
00093 
00094 if ($editurl = $userauth->edit_profile_url()) {
00095     // this internal script not used
00096     redirect($editurl);
00097 }
00098 
00099 if ($course->id == SITEID) {
00100     $coursecontext = get_context_instance(CONTEXT_SYSTEM);   // SYSTEM context
00101 } else {
00102     $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);   // Course context
00103 }
00104 $systemcontext   = get_context_instance(CONTEXT_SYSTEM);
00105 $personalcontext = get_context_instance(CONTEXT_USER, $user->id);
00106 
00107 // check access control
00108 if ($user->id == $USER->id) {
00109     //editing own profile - require_login() MUST NOT be used here, it would result in infinite loop!
00110     if (!has_capability('moodle/user:editownprofile', $systemcontext)) {
00111         print_error('cannotedityourprofile');
00112     }
00113 
00114 } else {
00115     // teachers, parents, etc.
00116     require_capability('moodle/user:editprofile', $personalcontext);
00117     // no editing of guest user account
00118     if (isguestuser($user->id)) {
00119         print_error('guestnoeditprofileother');
00120     }
00121     // no editing of primary admin!
00122     if (is_siteadmin($user) and !is_siteadmin($USER)) {  // Only admins may edit other admins
00123         print_error('useradmineditadmin');
00124     }
00125 }
00126 
00127 if ($user->deleted) {
00128     echo $OUTPUT->header();
00129     echo $OUTPUT->heading(get_string('userdeleted'));
00130     echo $OUTPUT->footer();
00131     die;
00132 }
00133 
00134 // Process email change cancellation
00135 if ($cancelemailchange) {
00136     cancel_email_update($user->id);
00137 }
00138 
00139 //load user preferences
00140 useredit_load_preferences($user);
00141 
00142 //Load custom profile fields data
00143 profile_load_data($user);
00144 
00145 
00146 // Prepare the editor and create form
00147 $editoroptions = array(
00148     'maxfiles'   => EDITOR_UNLIMITED_FILES,
00149     'maxbytes'   => $CFG->maxbytes,
00150     'trusttext'  => false,
00151     'forcehttps' => false,
00152     'context'    => $personalcontext
00153 );
00154 
00155 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
00156 $userform = new user_edit_form(null, array('editoroptions'=>$editoroptions));
00157 if (empty($user->country)) {
00158     // MDL-16308 - we must unset the value here so $CFG->country can be used as default one
00159     unset($user->country);
00160 }
00161 $userform->set_data($user);
00162 
00163 $email_changed = false;
00164 
00165 if ($usernew = $userform->get_data()) {
00166 
00167     add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
00168 
00169     $email_changed_html = '';
00170 
00171     if ($CFG->emailchangeconfirmation) {
00172         // Handle change of email carefully for non-trusted users
00173         if (isset($usernew->email) and $user->email != $usernew->email && !has_capability('moodle/user:update', $systemcontext)) {
00174             $a = new stdClass();
00175             $a->newemail = $usernew->preference_newemail = $usernew->email;
00176             $usernew->preference_newemailkey = random_string(20);
00177             $usernew->preference_newemailattemptsleft = 3;
00178             $a->oldemail = $usernew->email = $user->email;
00179 
00180             $email_changed_html = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice');
00181             $email_changed_html .= $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id");
00182             $email_changed = true;
00183         }
00184     }
00185 
00186     $authplugin = get_auth_plugin($user->auth);
00187 
00188     $usernew->timemodified = time();
00189 
00190     // description editor element may not exist!
00191     if (isset($usernew->description_editor)) {
00192         $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
00193     }
00194 
00195     $DB->update_record('user', $usernew);
00196 
00197     // pass a true $userold here
00198     if (! $authplugin->user_update($user, $usernew)) {
00199         // auth update failed, rollback for moodle
00200         $DB->update_record('user', $user);
00201         print_error('cannotupdateprofile');
00202     }
00203 
00204     //update preferences
00205     useredit_update_user_preference($usernew);
00206 
00207     //update interests
00208     if (!empty($CFG->usetags)) {
00209         useredit_update_interests($usernew, $usernew->interests);
00210     }
00211 
00212     //update user picture
00213     if (!empty($CFG->gdversion) and empty($CFG->disableuserimages)) {
00214         useredit_update_picture($usernew, $userform);
00215     }
00216 
00217     // update mail bounces
00218     useredit_update_bounces($user, $usernew);
00219 
00221     useredit_update_trackforums($user, $usernew);
00222 
00223     // save custom profile fields data
00224     profile_save_data($usernew);
00225 
00226     // If email was changed, send confirmation email now
00227     if ($email_changed && $CFG->emailchangeconfirmation) {
00228         $temp_user = fullclone($user);
00229         $temp_user->email = $usernew->preference_newemail;
00230 
00231         $a = new stdClass();
00232         $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
00233         $a->site = format_string($SITE->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID)));
00234         $a->fullname = fullname($user, true);
00235 
00236         $emailupdatemessage = get_string('emailupdatemessage', 'auth', $a);
00237         $emailupdatetitle = get_string('emailupdatetitle', 'auth', $a);
00238 
00239         //email confirmation directly rather than using messaging so they will definitely get an email
00240         if (!$mail_results = email_to_user($temp_user, get_admin(), $emailupdatetitle, $emailupdatemessage)) {
00241             die("could not send email!");
00242         }
00243     }
00244 
00245     // reload from db
00246     $usernew = $DB->get_record('user', array('id'=>$user->id));
00247     events_trigger('user_updated', $usernew);
00248 
00249     if ($USER->id == $user->id) {
00250         // Override old $USER session variable if needed
00251         foreach ((array)$usernew as $variable => $value) {
00252             $USER->$variable = $value;
00253         }
00254         // preload custom fields
00255         profile_load_custom_fields($USER);
00256     }
00257 
00258     if (is_siteadmin() and empty($SITE->shortname)) {
00259         // fresh cli install - we need to finish site settings
00260         redirect(new moodle_url('/admin/index.php'));
00261     }
00262 
00263     if (!$email_changed || !$CFG->emailchangeconfirmation) {
00264         redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
00265     }
00266 }
00267 
00268 // make sure we really are on the https page when https login required
00269 $PAGE->verify_https_required();
00270 
00271 
00273 $streditmyprofile = get_string('editmyprofile');
00274 $strparticipants  = get_string('participants');
00275 $userfullname     = fullname($user, true);
00276 
00277 $PAGE->set_title("$course->shortname: $streditmyprofile");
00278 $PAGE->set_heading($course->fullname);
00279 
00280 echo $OUTPUT->header();
00281 
00282 if ($email_changed) {
00283     echo $email_changed_html;
00284 } else {
00286     $userform->display();
00287 }
00288 
00290 echo $OUTPUT->footer();
00291 
 All Data Structures Namespaces Files Functions Variables Enumerations