add social battery sorting engine
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Controller;
|
||||
use App\Entity\Task;
|
||||
use App\Form\TaskType;
|
||||
use App\Repository\TaskRepository;
|
||||
use App\Service\TaskRecommendationService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
@@ -15,13 +16,30 @@ use Symfony\Component\Routing\Attribute\Route;
|
||||
final class TaskController extends AbstractController
|
||||
{
|
||||
#[Route(name: 'app_task_index', methods: ['GET'])]
|
||||
public function index(TaskRepository $taskRepository): Response
|
||||
public function index(Request $request, TaskRepository $taskRepository, TaskRecommendationService $taskRecommendationService): Response
|
||||
{
|
||||
$batteryLevel = $taskRecommendationService->normaliseBatteryLevel($request->getSession()->get('social_battery', 3));
|
||||
|
||||
return $this->render('task/index.html.twig', [
|
||||
'tasks' => $taskRepository->findAll(),
|
||||
'tasks' => $taskRecommendationService->sort($taskRepository->findAll(), $batteryLevel),
|
||||
'battery_level' => $batteryLevel,
|
||||
'battery_budget' => $taskRecommendationService->budgetFor($batteryLevel),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/social-battery', name: 'app_task_social_battery', methods: ['POST'])]
|
||||
public function socialBattery(Request $request, TaskRepository $taskRepository, TaskRecommendationService $taskRecommendationService): Response
|
||||
{
|
||||
if (!$this->isCsrfTokenValid('social_battery', $request->getPayload()->getString('_token'))) {
|
||||
throw $this->createAccessDeniedException('Invalid CSRF token.');
|
||||
}
|
||||
|
||||
$batteryLevel = $taskRecommendationService->normaliseBatteryLevel($request->getPayload()->getInt('social_battery'));
|
||||
$request->getSession()->set('social_battery', $batteryLevel);
|
||||
|
||||
return $this->renderTaskRecommendations($taskRepository, $taskRecommendationService, $batteryLevel);
|
||||
}
|
||||
|
||||
#[Route('/new', name: 'app_task_new', methods: ['GET', 'POST'])]
|
||||
public function new(Request $request, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
@@ -69,7 +87,7 @@ final class TaskController extends AbstractController
|
||||
}
|
||||
|
||||
#[Route('/{id}/completion', name: 'app_task_toggle_completion', methods: ['POST'])]
|
||||
public function toggleCompletion(Request $request, Task $task, EntityManagerInterface $entityManager): Response
|
||||
public function toggleCompletion(Request $request, Task $task, EntityManagerInterface $entityManager, TaskRepository $taskRepository, TaskRecommendationService $taskRecommendationService): Response
|
||||
{
|
||||
if (!$this->isCsrfTokenValid('completion'.$task->getId(), $request->getPayload()->getString('_token'))) {
|
||||
throw $this->createAccessDeniedException('Invalid CSRF token.');
|
||||
@@ -81,9 +99,9 @@ final class TaskController extends AbstractController
|
||||
->setUpdatedAt(new \DateTimeImmutable());
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->render('task/_task_item.html.twig', [
|
||||
'task' => $task,
|
||||
]);
|
||||
$batteryLevel = $taskRecommendationService->normaliseBatteryLevel($request->getSession()->get('social_battery', 3));
|
||||
|
||||
return $this->renderTaskList($taskRepository, $taskRecommendationService, $batteryLevel);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'app_task_delete', methods: ['POST'])]
|
||||
@@ -96,4 +114,20 @@ final class TaskController extends AbstractController
|
||||
|
||||
return $this->redirectToRoute('app_task_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
private function renderTaskList(TaskRepository $taskRepository, TaskRecommendationService $taskRecommendationService, int $batteryLevel): Response
|
||||
{
|
||||
return $this->render('task/_task_list.html.twig', [
|
||||
'tasks' => $taskRecommendationService->sort($taskRepository->findAll(), $batteryLevel),
|
||||
]);
|
||||
}
|
||||
|
||||
private function renderTaskRecommendations(TaskRepository $taskRepository, TaskRecommendationService $taskRecommendationService, int $batteryLevel): Response
|
||||
{
|
||||
return $this->render('task/_task_recommendations.html.twig', [
|
||||
'tasks' => $taskRecommendationService->sort($taskRepository->findAll(), $batteryLevel),
|
||||
'battery_level' => $batteryLevel,
|
||||
'battery_budget' => $taskRecommendationService->budgetFor($batteryLevel),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Task;
|
||||
|
||||
/**
|
||||
* Central home for recommendation rules. Add future signals such as priority,
|
||||
* category, or recurring tasks here without changing the controller or view.
|
||||
*/
|
||||
final class TaskRecommendationService
|
||||
{
|
||||
/** @var array<int, int> */
|
||||
private const ENERGY_BUDGET_MINUTES = [
|
||||
1 => 15,
|
||||
2 => 30,
|
||||
3 => 60,
|
||||
4 => 90,
|
||||
5 => 180,
|
||||
];
|
||||
|
||||
public function normaliseBatteryLevel(int $batteryLevel): int
|
||||
{
|
||||
return max(1, min(5, $batteryLevel));
|
||||
}
|
||||
|
||||
public function budgetFor(int $batteryLevel): int
|
||||
{
|
||||
return self::ENERGY_BUDGET_MINUTES[$this->normaliseBatteryLevel($batteryLevel)];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Task[] $tasks
|
||||
*
|
||||
* @return Task[] Active tasks that fit the selected energy budget are first.
|
||||
*/
|
||||
public function sort(array $tasks, int $batteryLevel): array
|
||||
{
|
||||
$batteryLevel = $this->normaliseBatteryLevel($batteryLevel);
|
||||
$budget = $this->budgetFor($batteryLevel);
|
||||
|
||||
usort($tasks, function (Task $left, Task $right) use ($batteryLevel, $budget): int {
|
||||
return $this->sortKey($left, $batteryLevel, $budget) <=> $this->sortKey($right, $batteryLevel, $budget);
|
||||
});
|
||||
|
||||
return $tasks;
|
||||
}
|
||||
|
||||
/** @return array<int, int|string> */
|
||||
private function sortKey(Task $task, int $batteryLevel, int $budget): array
|
||||
{
|
||||
$duration = $task->getEstimatedDurationMinutes();
|
||||
$hasDuration = null !== $duration;
|
||||
$duration ??= PHP_INT_MAX;
|
||||
$fitsBudget = $hasDuration && $duration <= $budget;
|
||||
|
||||
// Low-energy days start with the smallest task. With more energy,
|
||||
// prefer a task that makes good use of the available time budget.
|
||||
$durationScore = $batteryLevel <= 2 ? $duration : abs($budget - $duration);
|
||||
|
||||
return [
|
||||
$task->isActive() ? 0 : 1,
|
||||
$hasDuration ? 0 : 1,
|
||||
$fitsBudget ? 0 : 1,
|
||||
$durationScore,
|
||||
$duration,
|
||||
strtolower($task->getTitle() ?? ''),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user