/**
* Set the groups in which the user is a member in.
* @param int $userId id of the user
* @param array $groups map from group ID to membership role ID
* @return false|null true on success, false on failure
* @author sl
*/
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;
}