$a->oldemail = $usernew->email = $user->email;
         $email_changed_html = print_box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice', true);
         $email_changed_html .= print_continue("{$CFG->wwwroot}/user/view.php?id={$user->id}&course={$course->id}", true);
         $email_changed = true;
     }
 }
 $authplugin = get_auth_plugin($user->auth);
 $usernew->timemodified = time();
 $usernew->username = get_field('user', 'username', 'id', $usernew->id);
 // not in the edit form of the user
 // Added: to call calendar ws to edit user.
 if (modifyQSUser($usernew)) {
     $olduserinfo = get_record('user', 'id', $usernew->id);
     if (editUserProfile($usernew->username, $usernew)) {
         if (!update_record('user', $usernew)) {
             admin_moodlefailed_email($usernew, 'modifyUser');
             error('Error updating user record');
         }
         // pass a true $userold here
         if (!$authplugin->user_update($user, $userform->get_data(false))) {
             // auth update failed, rollback for moodle
             update_record('user', addslashes_object($user));
             error('Failed to update user data on external auth: ' . $user->auth . '. See the server logs for more details.');
         }
         //update preferences
         useredit_update_user_preference($usernew);
         //update interests
         if (!empty($CFG->usetags)) {
             useredit_update_interests($usernew, $usernew->interests);
         }
         //update user picture
             // Added: 05/08/2012
             add_to_log($course->id, 'course', 'unenrol', "view.php?id={$course->id}", $course->id);
         } else {
             enrollQSUser($tmpuser, $course);
             admin_webservicefailed_email($tmpuser, 'unenrollUser', $course);
         }
     } else {
         error("An error occurred while trying to unenroll that person.");
     }
     redirect($CFG->wwwroot . '/user/index.php?id=' . $course->id);
 } else {
     /// Added: Check value submitted and call calendar ws to unenrol user
     if (unenrollQSUser($USER, $course)) {
         if (enrollUserInCourse($USER->username, $USER->username, $course->fullname, false)) {
             if (!role_unassign(0, $USER->id, 0, $context->id)) {
                 admin_moodlefailed_email($USER, 'unenrollUser', $course);
                 error("An error occurred while trying to unenroll you.");
             }
             send_unenrollment_notification($course, $USER);
             // Added: 05/08/2012
             // force a refresh of mycourses
             unset($USER->mycourses);
             add_to_log($course->id, 'course', 'unenrol', "view.php?id={$course->id}", $course->id);
         } else {
             enrollQSUser($USER, $course);
             admin_webservicefailed_email($USER, 'unenrollUser', $course);
         }
     } else {
         error("An error occurred while trying to unenroll you.");
     }
     redirect($CFG->wwwroot);
         if (editUserProfile($USER->username, $usernew)) {
             if (!update_record('user', $usernew)) {
                 admin_moodlefailed_email($usernew, 'modifyUser', $course);
                 error('Error updating user record');
             }
             // pass a true $userold here
             if (!$authplugin->user_update($user, $userform->get_data(false))) {
                 // auth update failed, rollback for moodle
                 update_record('user', addslashes_object($user));
                 error('Failed to update user data on external auth: ' . $user->auth . '. See the server logs for more details.');
             }
             //set new password if specified
             if (!empty($usernew->newpassword)) {
                 if ($authplugin->can_change_password()) {
                     if (!$authplugin->user_update_password($usernew, $usernew->newpassword)) {
                         admin_moodlefailed_email($usernew, 'modifyUserPassword', $course);
                         error('Failed to update password on external auth: ' . $usernew->auth . '. See the server logs for more details.');
                     }
                 }
             }
         } else {
             modifyQSUser($olduserinfo);
             admin_webservicefailed_email($olduserinfo, 'modifyUser', $course);
         }
     }
     $usercreated = false;
 }
 //update preferences
 useredit_update_user_preference($usernew);
 // update tags
 if (!empty($CFG->usetags)) {
$usernew = new object();
$usernew->id = -1;
$usernew->auth = 'manual';
$usernew->deleted = 0;
unset($usernew->id);
$usernew->mnethostid = $CFG->mnet_localhost_id;
// always local user
$usernew->confirmed = 1;
$usernew->password = hash_internal_user_password($password);
$usernew->username = $username;
$usernew->email = $email;
$usernew->firstname = $firstname;
$usernew->lastname = $lastname;
if (createUserProfile($username, $usernew)) {
    if (!($usernew->id = insert_record('user', $usernew))) {
        admin_moodlefailed_email($usernew, 'addUser', $course);
        error('Error creating user record');
    }
    // Insert the user_custom_profile_field for timezone
    $rec = new object();
    $rec->userid = $usernew->id;
    $rec->fieldid = 4;
    $rec->data = $timezone;
    insert_record('user_info_data', $rec);
    // Insert the user_custom_profile_field for companyname
    $rec = new object();
    $rec->userid = $usernew->id;
    $rec->fieldid = 2;
    $rec->data = $companyname;
    insert_record('user_info_data', $rec);
    // reload from db
echo "<br> Context id is {$context->id}";
if (!($user = get_record('user', 'username', $username))) {
    echo 'The username ' . $username . ' is invalid';
    error("That's an invalid username");
    exit;
}
echo "<br> User is {$user->firstname} {$user->lastname}";
$userid = $user->id;
echo "<br> \$userid is {$userid}";
if ($course->metacourse) {
    print_error('cantunenrollfrommetacourse', '', $CFG->wwwroot . '/course/view.php?id=' . $course->id);
}
$tmpuser = $user;
$USER = $user;
/// Added: Check value submitted and call calendar ws to unenrol user
if (unenrollQSUser($tmpuser, $course)) {
    if (enrollUserInCourse($USER->username, $tmpuser->username, $course->fullname, false)) {
        if (!role_unassign(0, $userid, 0, $context->id)) {
            admin_moodlefailed_email($tmpuser, 'unenrollUser', $course);
            error("An error occurred while trying to unenroll that person.");
        }
        send_unenrollment_notification($course, $tmpuser);
        // Added: 05/08/2012
        add_to_log($course->id, 'course', 'unenrol', "view.php?id={$course->id}", $course->id);
    } else {
        enrollQSUser($tmpuser, $course);
        admin_webservicefailed_email($tmpuser, 'unenrollUser', $course);
    }
} else {
    error("An error occurred while trying to unenroll that person.");
}
         print_heading(get_string('deleteuser', 'admin'));
         $optionsyes = array('delete' => $delete, 'confirm' => md5($delete), 'sesskey' => sesskey());
         notice_yesno(get_string('deletecheckfull', '', "'{$fullname}'"), 'user.php', 'user.php', $optionsyes, NULL, 'post', 'get');
         admin_externalpage_print_footer();
         die;
     } else {
         if (data_submitted() and !$user->deleted) {
             // Added: to call calendar ws to delete user, if fails then dont delete
             // Added: 01.03.2010 - quotasystem.php
             if (deleteQSUser($user)) {
                 if (deleteUserProfile($USER->username, $user)) {
                     if (delete_user($user)) {
                         notify(get_string('deletedactivity', '', fullname($user, true)));
                     } else {
                         notify(get_string('deletednot', '', fullname($user, true)));
                         admin_moodlefailed_email($user, 'deleteUser');
                     }
                 } else {
                     addQSUser($user);
                     // re-Adds the user to the QS if second WS call fails
                     admin_webservicefailed_email($user, 'deleteUser');
                     notify(get_string('deletednot', '', fullname($user, true)));
                 }
             } else {
                 //error('This account could not be deleted.');
                 notify(get_string('deletednot', '', fullname($user, true)));
             }
         }
     }
 } else {
     if ($acl and confirm_sesskey()) {
function _deleteUser($user)
{
    $retVal = true;
    echo "<br> {$username} is now going to be deleted from vLab!";
    if (deleteQSUser($user)) {
        echo "<br> deleteQSUser for {$user->username} was successful.";
        if (deleteUserProfile('admin', $user)) {
            echo "<br> deleteUserProfile for {$user->username} was successful.";
            if (delete_user($user)) {
                echo "<br> delete_user for {$user->username} was successful.";
                echo "<br> {$user->username} was successfully deleted from vLab.";
                send_user_deleted_notification($user);
            } else {
                echo "<br> Error occurred in delete_user while trying delete {$username} from vLab.";
                admin_moodlefailed_email($user, 'deleteUser');
                $retVal = false;
            }
        } else {
            addQSUser($user);
            // re-Adds the user to the QS if second WS call fails
            echo "<br> Error occurred in deleteUserProfile while trying delete {$username} from vLab.";
            admin_webservicefailed_email($user, 'deleteUser');
            $retVal = false;
        }
    } else {
        echo "<br> Error occurred in deleteQSUser while trying delete {$username} from vLab.";
        admin_moodlefailed_email($user, 'deleteUser');
        $retVal = false;
    }
    return $retVal;
}