diff --git a/assets/js/analytics-results.js b/assets/js/analytics-results.js index 4ca7a27..fd13633 100644 --- a/assets/js/analytics-results.js +++ b/assets/js/analytics-results.js @@ -5,9 +5,95 @@ $(document).ready(function () { graphProjects(); graphTasks(); graphUsers(); + + showProjects(true); + showTasks(false); + showUsers(false); + showRecords(false); + + $('#showRecords').on('click', function () { + showRecords(true); + showProjects(false); + showTasks(false); + showUsers(false); + }) + + $('#showProjects').on('click', function () { + showRecords(false); + showProjects(true); + showTasks(false); + showUsers(false); + }) + + $('#showTasks').on('click', function () { + showRecords(false); + showProjects(false); + showTasks(true); + showUsers(false); + }) + + $('#showUsers').on('click', function () { + showRecords(false); + showProjects(false); + showTasks(false); + showUsers(true); + }) }) -function graphProjects() { +function showProjects(show) +{ + let $projects = $('#analytics-projects'); + let $projectsTab = $('#projects'); + if (show) { + $projects.show(); + $projectsTab.show(); + } else { + $projects.hide(); + $projectsTab.hide(); + } +} + +function showTasks(show) +{ + let $tasks = $('#analytics-tasks'); + let $tasksTab = $('#tasks'); + if (show) { + $tasks.show(); + $tasksTab.show(); + } else { + $tasks.hide(); + $tasksTab.hide(); + } +} + +function showUsers(show) +{ + let $users = $('#analytics-users'); + let $usersTab = $('#users'); + if (show) { + $users.show(); + $usersTab.show(); + } else { + $users.hide(); + $usersTab.hide(); + } +} + +function showRecords(show) +{ + let $records = $('#analytics-all-records'); + let $recordsTab = $('#all-records'); + if (show) { + $records.show(); + $recordsTab.show(); + } else { + $records.hide(); + $recordsTab.hide(); + } +} + +function graphProjects() +{ let $projects = $('#projects'); let projects = $projects.attr('data-projects'); let colorTasks = []; @@ -31,27 +117,27 @@ function graphProjects() { colorUsers.push(colorGenerator()); }) - let ctxPt = document.getElementById('project-task-' + iProject).getContext("2d"); - new Chart(ctxPt, { - type: 'pie', - data: { - labels: labelTasks, - datasets: [{ - label: labelTasks, - data: dataTasks, - backgroundColor: colorTasks, - borderColor: colorTasks, - borderWidth: 1 - }] - }, - options: { - title: { - text: project.name, - display: true, - position: top, + let ctxPt = document.getElementById('project-task-' + iProject).getContext("2d"); + new Chart(ctxPt, { + type: 'pie', + data: { + labels: labelTasks, + datasets: [{ + label: labelTasks, + data: dataTasks, + backgroundColor: colorTasks, + borderColor: colorTasks, + borderWidth: 1 + }] + }, + options: { + title: { + text: project.name, + display: true, + position: top, + } } - } - }); + }); let ctxPu = document.getElementById('project-user-' + iProject).getContext("2d"); new Chart(ctxPu, { diff --git a/src/Controller/AnalyticsController.php b/src/Controller/AnalyticsController.php index 4f56a34..b3dd3b9 100644 --- a/src/Controller/AnalyticsController.php +++ b/src/Controller/AnalyticsController.php @@ -13,6 +13,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\Routing\Annotation\Route; @@ -38,7 +39,7 @@ public function __construct(SessionInterface $session, EntityManagerInterface $e * @Route(path="/analytics", name="analytics", methods={"GET", "POST"}) * @IsGranted("ROLE_USER") */ - public function index(Request $request, AnalyticsServices $analyticsServices) + public function index(Request $request, AnalyticsServices $analyticsServices): Response { $company = $this->companySession; $form = $this->createForm(AnalyticsType::class, null, [ diff --git a/src/Service/AnalyticsServices.php b/src/Service/AnalyticsServices.php index 2b9d022..7f950c7 100644 --- a/src/Service/AnalyticsServices.php +++ b/src/Service/AnalyticsServices.php @@ -54,6 +54,8 @@ public function analyzePerProjects(array $projects, array $tasks, array $users, $stats[$project->getId()]['tasks'][$task->getId()]['total'] = $this->timeToHours($time) + $stats[$project->getId()]['tasks'][$task->getId()]['total']; } } + + $stats = $this->cleanSubStats($stats, $project->getId(), 'tasks', $task->getId()); } // Per projects and users @@ -66,10 +68,15 @@ public function analyzePerProjects(array $projects, array $tasks, array $users, $stats[$project->getId()]['users'][$user->getId()]['total'] = $this->timeToHours($time) + $stats[$project->getId()]['users'][$user->getId()]['total']; } } + + $stats = $this->cleanSubStats($stats, $project->getId(), 'users', $user->getId()); } + + $stats = $this->cleanStats($stats, $project->getId(), 'tasks'); } return $stats; + } public function analyzePerTasks(array $projects, array $tasks, array $users, array $times): array @@ -95,6 +102,7 @@ public function analyzePerTasks(array $projects, array $tasks, array $users, arr $stats[$task->getId()]['projects'][$project->getId()]['total'] = $this->timeToHours($time) + $stats[$task->getId()]['projects'][$project->getId()]['total']; } } + $stats = $this->cleanSubStats($stats, $task->getId(), 'projects', $project->getId()); } /** @var User $user */ @@ -106,7 +114,11 @@ public function analyzePerTasks(array $projects, array $tasks, array $users, arr $stats[$task->getId()]['users'][$user->getId()]['total'] = $this->timeToHours($time) + $stats[$task->getId()]['users'][$user->getId()]['total']; } } + + $stats = $this->cleanSubStats($stats, $task->getId(), 'users', $user->getId()); } + + $stats = $this->cleanStats($stats, $task->getId(), 'projects'); } return $stats; @@ -135,6 +147,8 @@ public function analyzePerUsers(array $projects, array $tasks, array $users, arr $stats[$user->getId()]['projects'][$project->getId()]['total'] = $this->timeToHours($time) + $stats[$user->getId()]['projects'][$project->getId()]['total']; } } + + $stats = $this->cleanSubStats($stats, $user->getId(), 'projects', $project->getId()); } /** @var Task $task */ @@ -146,7 +160,11 @@ public function analyzePerUsers(array $projects, array $tasks, array $users, arr $stats[$user->getId()]['tasks'][$task->getId()]['total'] = $this->timeToHours($time) + $stats[$user->getId()]['tasks'][$task->getId()]['total']; } } + + $stats = $this->cleanSubStats($stats, $user->getId(), 'tasks', $task->getId()); } + + $stats = $this->cleanStats($stats, $user->getId(), 'projects'); } return $stats; @@ -154,19 +172,9 @@ public function analyzePerUsers(array $projects, array $tasks, array $users, arr public function timeToHours(Time $time): float { - $diff = $time->getEndTime()->diff($time->getStartTime()); - $hours1 = $hours2 = $hours3 = 0; - if ($diff->format('%a') > 0) { - $hours1 = $diff->format('%a') * 24; - } - if ($diff->format('%h') > 0) { - $hours2 = $diff->format('%h'); - } - if ($diff->format('%m') > 0) { - $hours3 = round($diff->format('%m') / 60, 2); - } + $diff = $time->getEndTime()->getTimestamp() - $time->getStartTime()->getTimestamp(); - return $hours1 + $hours2 + $hours3; + return $diff / ( 60 * 60 ); } private function getProjects(array $projects) : array @@ -202,4 +210,23 @@ private function getUsers(array $users) : array return $users; } + private function cleanStats($stats, int $perId, string $category): array + { + if (0 === count($stats[$perId][$category])) { + unset($stats[$perId]); + } + + return $stats; + } + + private function cleanSubStats(array $stats, int $perId, string $subName, int $subId): array + { + // Delete tasks with no times + if (0 === $stats[$perId][$subName][$subId]['total']) { + unset($stats[$perId][$subName][$subId]); + } + + return $stats; + } + } \ No newline at end of file diff --git a/templates/page/analytics/results.html.twig b/templates/page/analytics/results.html.twig index 2ce0968..c9eaaef 100644 --- a/templates/page/analytics/results.html.twig +++ b/templates/page/analytics/results.html.twig @@ -8,10 +8,10 @@ {% block page_content %}
- {{ 'Projects'|trans }} - {{ 'Tasks'|trans }} - {{ 'Users'|trans }} - {{ 'All records'|trans }} + {{ 'Projects'|trans }} + {{ 'Tasks'|trans }} + {{ 'Users'|trans }} + {{ 'All records'|trans }}