File: //home/timetracker.panomity.com/tm.old/Controller/ReportController.php
<?php
/*
* This file is part of the TaskManagementBundle for Kimai 2.
* All rights reserved by Kevin Papst (www.kevinpapst.de).
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace KimaiPlugin\TaskManagementBundle\Controller;
use App\Controller\AbstractController;
use Exception;
use KimaiPlugin\TaskManagementBundle\Entity\Task;
use KimaiPlugin\TaskManagementBundle\Repository\Query\TaskQuery;
use KimaiPlugin\TaskManagementBundle\Repository\TaskRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route(path="/reporting")
* @Security("is_granted('view_reporting')")
*/
final class ReportController extends AbstractController
{
/**
* @Route(path="/assigned-tasks", name="report_tasks_assigned", methods={"GET","POST"})
*
* @param Request $request
* @param TaskRepository $repository
* @return Response
* @throws Exception
*/
public function assignedTasks(Request $request, TaskRepository $repository): Response
{
$currentUser = $this->getUser();
$query = new TaskQuery();
$query->setCurrentUser($currentUser);
if (!$this->isGranted('view_other_timesheet')) {
$query->setUser($currentUser);
}
$query->setStatus([Task::STATUS_PENDING, Task::STATUS_IN_PROGRESS]);
$tasks = $repository->getTasksForQuery($query);
$hasEstimation = false;
$template = [
'total' => 0,
'estimation' => 0,
'duration' => 0,
'no_estimation' => 0,
'with_estimation' => 0,
'started' => 0,
'not_started' => 0,
'overdue' => 0,
];
$data = [];
$total = $template;
$total['estimation_duration'] = 0;
foreach ($tasks as $task) {
$user = $task->getUser();
// we only calculate data for tasks with an assigned user
if ($user === null) {
continue;
}
if (!\array_key_exists($user->getId(), $data)) {
$data[$user->getId()] = $template;
$data[$user->getId()]['user'] = $user;
}
$data[$user->getId()]['total']++;
$total['total']++;
$duration = $task->getDuration();
if ($duration === null) {
$data[$user->getId()]['not_started']++;
$total['not_started']++;
} else {
$data[$user->getId()]['started']++;
$data[$user->getId()]['duration'] += $duration;
$total['started']++;
$total['duration'] += $duration;
}
$estimation = $task->getEstimation();
if ($estimation === null) {
$data[$user->getId()]['no_estimation']++;
$total['no_estimation']++;
} else {
$data[$user->getId()]['with_estimation']++;
$data[$user->getId()]['estimation'] += $estimation;
$total['with_estimation']++;
$total['estimation'] += $estimation;
if ($duration !== null) {
$total['estimation_duration'] += $duration;
}
$hasEstimation = true;
}
if ($task->isOverdue()) {
$data[$user->getId()]['overdue']++;
$total['overdue']++;
}
}
return $this->render('@TaskManagement/report-assigned-tasks.html.twig', [
'data' => $data,
'hasEstimation' => $hasEstimation,
'totals' => $total,
]);
}
/**
* @Route(path="/team-tasks", name="report_tasks_teams", methods={"GET","POST"})
*
* @param Request $request
* @param TaskRepository $repository
* @return Response
* @throws Exception
*/
public function teamTasks(Request $request, TaskRepository $repository): Response
{
$currentUser = $this->getUser();
$query = new TaskQuery();
$query->setCurrentUser($currentUser);
if (!$this->isGranted('view_other_timesheet')) {
$query->setTeams($currentUser->getTeams());
}
$query->setStatus([Task::STATUS_PENDING, Task::STATUS_IN_PROGRESS]);
$tasks = $repository->getTasksForQuery($query);
$hasEstimation = false;
$template = [
'total' => 0,
'estimation' => 0,
'duration' => 0,
'no_estimation' => 0,
'with_estimation' => 0,
'started' => 0,
'not_started' => 0,
'overdue' => 0,
];
$data = [];
$total = $template;
$total['estimation_duration'] = 0;
foreach ($tasks as $task) {
$team = $task->getTeam();
// we only calculate data for tasks with an assigned team
if ($team === null) {
continue;
}
if (!\array_key_exists($team->getId(), $data)) {
$data[$team->getId()] = $template;
$data[$team->getId()]['team'] = $team;
}
$data[$team->getId()]['total']++;
$total['total']++;
$duration = $task->getDuration();
if ($duration === null) {
$data[$team->getId()]['not_started']++;
$total['not_started']++;
} else {
$data[$team->getId()]['started']++;
$data[$team->getId()]['duration'] += $duration;
$total['started']++;
$total['duration'] += $duration;
}
$estimation = $task->getEstimation();
if ($estimation === null) {
$data[$team->getId()]['no_estimation']++;
$total['no_estimation']++;
} else {
$data[$team->getId()]['with_estimation']++;
$data[$team->getId()]['estimation'] += $estimation;
$total['with_estimation']++;
$total['estimation'] += $estimation;
if ($duration !== null) {
$total['estimation_duration'] += $duration;
}
$hasEstimation = true;
}
if ($task->isOverdue()) {
$data[$team->getId()]['overdue']++;
$total['overdue']++;
}
}
return $this->render('@TaskManagement/report-team-tasks.html.twig', [
'data' => $data,
'hasEstimation' => $hasEstimation,
'totals' => $total,
]);
}
}