/** * Calculates the statistics * The returned array will contain various statistics about the available boards, columns * and tickets of the incoming user. * * @param \DigitalKanban\BaseBundle\Entity\User $userLoggedIn * @return array */ protected function getStatisticsOfUser(User $userLoggedIn) { $boardCount = $columnCount = $ticketCount = $userCount = 0; $countedUser = array(); $userLoggedInId = $userLoggedIn->getId(); $boards = $userLoggedIn->getBoards(); $boardCount = count($boards); foreach ($boards as $board) { $columns = $board->getColumns(); $columnCount += count($columns); // We must save the counted users, because if we only sum the user // it is possible to get a higher number than users exists, because // one user could have more than one board assigned $users = $board->getUsers(); foreach ($users as $user) { $userId = $user->getId(); // If the user is not counted yet AND the user is not the user which is logged in if (in_array($userId, $countedUser) === FALSE && $userLoggedInId !== $userId) { $countedUser[] = $userId; $userCount++; } } foreach ($columns as $column) { $tickets = $column->getIssues(); $ticketCount += count($tickets); } } $statistics = array('boards' => $this->formatNumberForStatistics($boardCount), 'columns' => $this->formatNumberForStatistics($columnCount), 'columnsPerBoardAVG' => $this->formatNumberForStatistics($boardCount > 0 ? $columnCount / $boardCount : 0), 'tickets' => $this->formatNumberForStatistics($ticketCount), 'ticketsPerBoardAVG' => $this->formatNumberForStatistics($boardCount > 0 ? $ticketCount / $boardCount : 0), 'ticketsPerColumnAVG' => $this->formatNumberForStatistics($columnCount > 0 ? $ticketCount / $columnCount : 0), 'users' => $this->formatNumberForStatistics($userCount), 'usersPerBoardAVG' => $this->formatNumberForStatistics($boardCount > 0 ? $userCount / $boardCount : 0)); return $statistics; }
/** * Checks if the incoming $user has access to the incoming $columnId * to edit this board. * * @param User $user * @param integer $columnId * @param $entityManager * @return bool */ public function isUserAllowedToEditThisColumn(User $user, $columnId, $entityManager) { $returnVal = FALSE; // An admin is allowed to edit every board if ($user->isAdmin() === TRUE) { return TRUE; } // Ask the database if the user has access to this board $query = $entityManager->createQuery('SELECT user FROM DigitalKanbanBaseBundle:User user INNER JOIN user.boards board INNER JOIN board.columns boardcolumn WHERE user.id = :userId AND boardcolumn.id = :columnId')->setParameters(array('userId' => $user->getId(), 'columnId' => $columnId)); try { // If there is no result, an exception will be thrown // and this means, the user has no access to this board $query->getSingleResult(); $returnVal = TRUE; } catch (\Doctrine\ORM\NoResultException $eNoResult) { // Nothing to do here, because $returnVal is set to FALSE already } catch (Exception $e) { // Nothing to do here, because $returnVal is set to FALSE already } return $returnVal; }