Exemplo n.º 1
0
/**
 * Create user
 *
 * @param object $user stdclass or User object for the usr table
 * @param array  $profile profile field/values to set
 * @param string $institution Institution the user should joined to
 * @param stdclass $remoteauth authinstance record for a remote authinstance
 * @param string $remotename username on the remote site
 * @return integer id of the new user
 */
function create_user($user, $profile = array(), $institution = null, $remoteauth = null, $remotename = null)
{
    db_begin();
    if ($user instanceof User) {
        $user->create();
        $user->quota_init();
        $user->commit();
        $user = $user->to_stdclass();
    } else {
        $user->ctime = db_format_timestamp(time());
        if (empty($user->quota)) {
            $user->quota = get_config_plugin('artefact', 'file', 'defaultquota');
        }
        $user->id = insert_record('usr', $user, 'id', true);
    }
    // Bypass access check for 'copynewuser' institution/site views, because this user may not be logged in yet
    $user->newuser = true;
    if (isset($user->email) && $user->email != '') {
        set_profile_field($user->id, 'email', $user->email);
    }
    if (isset($user->firstname) && $user->firstname != '') {
        set_profile_field($user->id, 'firstname', $user->firstname);
    }
    if (isset($user->lastname) && $user->lastname != '') {
        set_profile_field($user->id, 'lastname', $user->lastname);
    }
    foreach ($profile as $k => $v) {
        if (in_array($k, array('firstname', 'lastname', 'email'))) {
            continue;
        }
        set_profile_field($user->id, $k, $v);
    }
    if (!empty($institution) && $institution != 'mahara') {
        if (is_string($institution)) {
            $institution = new Institution($institution);
        }
        if ($institution->name != 'mahara') {
            $institution->addUserAsMember($user);
            // uses $user->newuser
        }
    }
    if (!empty($remoteauth) && $remoteauth->authname != 'internal') {
        if (isset($remotename) && strlen($remotename) > 0) {
            $un = $remotename;
        } else {
            $un = $user->username;
        }
        delete_records('auth_remote_user', 'authinstance', $user->authinstance, 'remoteusername', $un);
        insert_record('auth_remote_user', (object) array('authinstance' => $user->authinstance, 'remoteusername' => $un, 'localusr' => $user->id));
    }
    // Copy site views to the new user's profile
    $checkviewaccess = !$user->newuser;
    $userobj = new User();
    $userobj->find_by_id($user->id);
    $userobj->copy_views(get_column('view', 'id', 'institution', 'mahara', 'copynewuser', 1), $checkviewaccess);
    handle_event('createuser', $user);
    db_commit();
    return $user->id;
}
 public function addUserAsMember($user)
 {
     global $USER;
     if ($this->isFull()) {
         throw new SystemException('Trying to add a user to an institution that already has a full quota of members');
     }
     if (is_numeric($user)) {
         $user = get_record('usr', 'id', $user);
     }
     if ($user instanceof User) {
         $lang = $user->get_account_preference('lang');
         if (empty($lang) || $lang == 'default') {
             $lang = get_config('lang');
         }
     } else {
         // stdclass object
         $lang = get_user_language($user->id);
     }
     $userinst = new StdClass();
     $userinst->institution = $this->name;
     $studentid = get_field('usr_institution_request', 'studentid', 'usr', $user->id, 'institution', $this->name);
     if (!empty($studentid)) {
         $userinst->studentid = $studentid;
     } else {
         if (!empty($user->studentid)) {
             $userinst->studentid = $user->studentid;
         }
     }
     $userinst->usr = $user->id;
     $now = time();
     $userinst->ctime = db_format_timestamp($now);
     $defaultexpiry = $this->defaultmembershipperiod;
     if (!empty($defaultexpiry)) {
         $userinst->expiry = db_format_timestamp($now + $defaultexpiry);
     }
     $message = (object) array('users' => array($user->id), 'subject' => get_string_from_language($lang, 'institutionmemberconfirmsubject'), 'message' => get_string_from_language($lang, 'institutionmemberconfirmmessage', 'mahara', $this->displayname));
     db_begin();
     if (!get_config('usersallowedmultipleinstitutions')) {
         delete_records('usr_institution', 'usr', $user->id);
         delete_records('usr_institution_request', 'usr', $user->id);
     }
     insert_record('usr_institution', $userinst);
     delete_records('usr_institution_request', 'usr', $userinst->usr, 'institution', $this->name);
     // Copy institution views to the user's portfolio
     $checkviewaccess = empty($user->newuser) && !$USER->get('admin');
     $userobj = new User();
     $userobj->find_by_id($user->id);
     $userobj->copy_views(get_column('view', 'id', 'institution', $this->name, 'copynewuser', 1), $checkviewaccess);
     require_once 'activity.php';
     activity_occurred('maharamessage', $message);
     handle_event('updateuser', $userinst->usr);
     db_commit();
 }