Kimai_Database_Mysql::setGroupMemberships PHP Method

setGroupMemberships() public method

Set the groups in which the user is a member in.
Author: sl
public setGroupMemberships ( integer $userId, array $groups = null ) : false | null
$userId integer id of the user
$groups array map from group ID to membership role ID
return false | null true on success, false on failure
    public function setGroupMemberships($userId, array $groups = null)
    {
        $table = $this->getGroupsUsersTable();
        if (!$this->conn->TransactionBegin()) {
            $this->logLastError('setGroupMemberships');
            return false;
        }
        $data['userID'] = MySQL::SQLValue($userId, MySQL::SQLVALUE_NUMBER);
        $result = $this->conn->DeleteRows($table, $data);
        if (!$result) {
            $this->logLastError('setGroupMemberships');
            if (!$this->conn->TransactionRollback()) {
                $this->logLastError('setGroupMemberships_rollback');
            }
            return false;
        }
        foreach ($groups as $group => $role) {
            $data['groupID'] = MySQL::SQLValue($group, MySQL::SQLVALUE_NUMBER);
            $data['membershipRoleID'] = MySQL::SQLValue($role, MySQL::SQLVALUE_NUMBER);
            $result = $this->conn->InsertRow($table, $data);
            if ($result === false) {
                $this->logLastError('setGroupMemberships');
                if (!$this->conn->TransactionRollback()) {
                    $this->logLastError('setGroupMemberships_rollback');
                }
                return false;
            }
        }
        if (!$this->conn->TransactionEnd()) {
            $this->logLastError('setGroupMemberships');
            return false;
        }
        return true;
    }
Kimai_Database_Mysql