Skip to content

Commit

Permalink
Merge pull request #92 from loguilhem/features/hide-stats-with-no-res…
Browse files Browse the repository at this point in the history
…ults

Features/hide stats with no results
  • Loading branch information
loguilhem authored Dec 4, 2021
2 parents ec03b38 + 2371ec4 commit 1d8a146
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 38 deletions.
128 changes: 107 additions & 21 deletions assets/js/analytics-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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, {
Expand Down
3 changes: 2 additions & 1 deletion src/Controller/AnalyticsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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, [
Expand Down
51 changes: 39 additions & 12 deletions src/Service/AnalyticsServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 */
Expand All @@ -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;
Expand Down Expand Up @@ -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 */
Expand All @@ -146,27 +160,21 @@ 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;
}

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
Expand Down Expand Up @@ -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;
}

}
8 changes: 4 additions & 4 deletions templates/page/analytics/results.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

{% block page_content %}
<div class="btn-group" id="analytics-submenu">
<a class="btn btn-secondary" href="#analytics-projects">{{ 'Projects'|trans }}</a>
<a class="btn btn-secondary" href="#analytics-tasks">{{ 'Tasks'|trans }}</a>
<a class="btn btn-secondary" href="#analytics-users">{{ 'Users'|trans }}</a>
<a class="btn btn-secondary" href="#all-records">{{ 'All records'|trans }}</a>
<a id="showProjects" class="btn btn-secondary">{{ 'Projects'|trans }}</a>
<a id="showTasks" class="btn btn-secondary">{{ 'Tasks'|trans }}</a>
<a id="showUsers" class="btn btn-secondary">{{ 'Users'|trans }}</a>
<a id="showRecords" class="btn btn-secondary">{{ 'All records'|trans }}</a>
</div>

<div class="row" id="analytics-projects">
Expand Down

0 comments on commit 1d8a146

Please sign in to comment.