feat: Calculer automatiquement les moyennes après chaque saisie de notes
Les enseignants ont besoin de moyennes à jour immédiatement après la publication ou modification des notes, sans attendre un batch nocturne. Le système recalcule via Domain Events synchrones : statistiques d'évaluation (min/max/moyenne/médiane), moyennes matières pondérées (normalisation /20), et moyenne générale par élève. Les résultats sont stockés dans des tables dénormalisées avec cache Redis (TTL 5 min). Trois endpoints API exposent les données avec contrôle d'accès par rôle. Une commande console permet le backfill des données historiques au déploiement.
This commit is contained in:
@@ -39,6 +39,11 @@ framework:
|
||||
adapter: cache.adapter.filesystem
|
||||
default_lifetime: 604800 # 7 jours
|
||||
|
||||
# Pool dédié aux moyennes élèves et statistiques (5 min TTL)
|
||||
student_averages.cache:
|
||||
adapter: cache.adapter.filesystem
|
||||
default_lifetime: 300 # 5 minutes
|
||||
|
||||
# Pool dédié au cache des requêtes paginées (1h TTL, tag-aware)
|
||||
paginated_queries.cache:
|
||||
adapter: cache.adapter.filesystem
|
||||
@@ -79,6 +84,10 @@ when@test:
|
||||
adapter: cache.adapter.redis
|
||||
provider: '%env(REDIS_URL)%'
|
||||
default_lifetime: 604800
|
||||
student_averages.cache:
|
||||
adapter: cache.adapter.redis
|
||||
provider: '%env(REDIS_URL)%'
|
||||
default_lifetime: 300
|
||||
paginated_queries.cache:
|
||||
adapter: cache.adapter.redis_tag_aware
|
||||
provider: '%env(REDIS_URL)%'
|
||||
@@ -120,6 +129,10 @@ when@prod:
|
||||
adapter: cache.adapter.redis
|
||||
provider: '%env(REDIS_URL)%'
|
||||
default_lifetime: 604800 # 7 jours
|
||||
student_averages.cache:
|
||||
adapter: cache.adapter.redis
|
||||
provider: '%env(REDIS_URL)%'
|
||||
default_lifetime: 300 # 5 minutes
|
||||
paginated_queries.cache:
|
||||
adapter: cache.adapter.redis_tag_aware
|
||||
provider: '%env(REDIS_URL)%'
|
||||
|
||||
@@ -265,9 +265,44 @@ services:
|
||||
App\Scolarite\Domain\Repository\GradeRepository:
|
||||
alias: App\Scolarite\Infrastructure\Persistence\Doctrine\DoctrineGradeRepository
|
||||
|
||||
App\Scolarite\Domain\Repository\AppreciationTemplateRepository:
|
||||
alias: App\Scolarite\Infrastructure\Persistence\Doctrine\DoctrineAppreciationTemplateRepository
|
||||
|
||||
App\Scolarite\Application\Port\EvaluationGradesChecker:
|
||||
alias: App\Scolarite\Infrastructure\Service\NoGradesEvaluationGradesChecker
|
||||
|
||||
# Averages (Story 6.3 - Calcul automatique des moyennes)
|
||||
App\Scolarite\Domain\Service\AverageCalculator:
|
||||
autowire: true
|
||||
|
||||
App\Scolarite\Application\Service\RecalculerMoyennesService:
|
||||
autowire: true
|
||||
|
||||
App\Scolarite\Application\Port\PeriodFinder:
|
||||
alias: App\Scolarite\Infrastructure\Service\DoctrinePeriodFinder
|
||||
|
||||
App\Scolarite\Infrastructure\Persistence\Doctrine\DoctrineEvaluationStatisticsRepository:
|
||||
autowire: true
|
||||
|
||||
App\Scolarite\Infrastructure\Cache\CachingEvaluationStatisticsRepository:
|
||||
arguments:
|
||||
$inner: '@App\Scolarite\Infrastructure\Persistence\Doctrine\DoctrineEvaluationStatisticsRepository'
|
||||
$cache: '@student_averages.cache'
|
||||
|
||||
App\Scolarite\Domain\Repository\EvaluationStatisticsRepository:
|
||||
alias: App\Scolarite\Infrastructure\Cache\CachingEvaluationStatisticsRepository
|
||||
|
||||
App\Scolarite\Infrastructure\Persistence\Doctrine\DoctrineStudentAverageRepository:
|
||||
autowire: true
|
||||
|
||||
App\Scolarite\Infrastructure\Cache\CachingStudentAverageRepository:
|
||||
arguments:
|
||||
$inner: '@App\Scolarite\Infrastructure\Persistence\Doctrine\DoctrineStudentAverageRepository'
|
||||
$cache: '@student_averages.cache'
|
||||
|
||||
App\Scolarite\Domain\Repository\StudentAverageRepository:
|
||||
alias: App\Scolarite\Infrastructure\Cache\CachingStudentAverageRepository
|
||||
|
||||
# Super Admin Repositories (Story 2.10 - Multi-établissements)
|
||||
App\SuperAdmin\Domain\Repository\SuperAdminRepository:
|
||||
alias: App\SuperAdmin\Infrastructure\Persistence\Doctrine\DoctrineSuperAdminRepository
|
||||
|
||||
84
backend/migrations/Version20260329082334.php
Normal file
84
backend/migrations/Version20260329082334.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
final class Version20260329082334 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Création des tables dénormalisées pour les moyennes élèves et statistiques évaluations';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE TABLE student_averages (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
tenant_id UUID NOT NULL,
|
||||
student_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
subject_id UUID NOT NULL REFERENCES subjects(id) ON DELETE CASCADE,
|
||||
period_id UUID NOT NULL REFERENCES academic_periods(id) ON DELETE CASCADE,
|
||||
average DECIMAL(4,2),
|
||||
grade_count INT NOT NULL DEFAULT 0,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
UNIQUE (student_id, subject_id, period_id)
|
||||
)
|
||||
SQL);
|
||||
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE INDEX idx_student_averages_tenant ON student_averages(tenant_id)
|
||||
SQL);
|
||||
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE INDEX idx_student_averages_student ON student_averages(student_id)
|
||||
SQL);
|
||||
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE INDEX idx_student_averages_subject ON student_averages(subject_id)
|
||||
SQL);
|
||||
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE TABLE student_general_averages (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
tenant_id UUID NOT NULL,
|
||||
student_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
period_id UUID NOT NULL REFERENCES academic_periods(id) ON DELETE CASCADE,
|
||||
average DECIMAL(4,2),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
UNIQUE (student_id, period_id)
|
||||
)
|
||||
SQL);
|
||||
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE INDEX idx_student_general_averages_tenant ON student_general_averages(tenant_id)
|
||||
SQL);
|
||||
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE INDEX idx_student_general_averages_student ON student_general_averages(student_id)
|
||||
SQL);
|
||||
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE TABLE evaluation_statistics (
|
||||
evaluation_id UUID PRIMARY KEY REFERENCES evaluations(id) ON DELETE CASCADE,
|
||||
average DECIMAL(5,2),
|
||||
min_grade DECIMAL(5,2),
|
||||
max_grade DECIMAL(5,2),
|
||||
median_grade DECIMAL(5,2),
|
||||
graded_count INT NOT NULL DEFAULT 0,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
)
|
||||
SQL);
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$this->addSql('DROP TABLE IF EXISTS evaluation_statistics');
|
||||
$this->addSql('DROP TABLE IF EXISTS student_general_averages');
|
||||
$this->addSql('DROP TABLE IF EXISTS student_averages');
|
||||
}
|
||||
}
|
||||
56
backend/migrations/Version20260331154510.php
Normal file
56
backend/migrations/Version20260331154510.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
final class Version20260331154510 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Ajout des appréciations sur les notes et table des modèles d\'appréciations enseignant';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql(<<<'SQL'
|
||||
ALTER TABLE grades ADD COLUMN appreciation TEXT
|
||||
SQL);
|
||||
|
||||
$this->addSql(<<<'SQL'
|
||||
ALTER TABLE grades ADD COLUMN appreciation_updated_at TIMESTAMPTZ
|
||||
SQL);
|
||||
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE TABLE appreciation_templates (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
tenant_id UUID NOT NULL,
|
||||
teacher_id UUID NOT NULL REFERENCES users(id),
|
||||
title VARCHAR(100) NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
category VARCHAR(50),
|
||||
usage_count INT NOT NULL DEFAULT 0,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
)
|
||||
SQL);
|
||||
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE INDEX idx_templates_teacher ON appreciation_templates(teacher_id)
|
||||
SQL);
|
||||
|
||||
$this->addSql(<<<'SQL'
|
||||
CREATE INDEX idx_templates_tenant ON appreciation_templates(tenant_id)
|
||||
SQL);
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$this->addSql('DROP TABLE IF EXISTS appreciation_templates');
|
||||
$this->addSql('ALTER TABLE grades DROP COLUMN IF EXISTS appreciation_updated_at');
|
||||
$this->addSql('ALTER TABLE grades DROP COLUMN IF EXISTS appreciation');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\CreateAppreciationTemplate;
|
||||
|
||||
final readonly class CreateAppreciationTemplateCommand
|
||||
{
|
||||
public function __construct(
|
||||
public string $tenantId,
|
||||
public string $teacherId,
|
||||
public string $title,
|
||||
public string $content,
|
||||
public ?string $category,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\CreateAppreciationTemplate;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Exception\CategorieAppreciationInvalideException;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationCategory;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplate;
|
||||
use App\Scolarite\Domain\Repository\AppreciationTemplateRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
#[AsMessageHandler(bus: 'command.bus')]
|
||||
final readonly class CreateAppreciationTemplateHandler
|
||||
{
|
||||
public function __construct(
|
||||
private AppreciationTemplateRepository $templateRepository,
|
||||
private Clock $clock,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(CreateAppreciationTemplateCommand $command): AppreciationTemplate
|
||||
{
|
||||
$category = null;
|
||||
if ($command->category !== null) {
|
||||
$category = AppreciationCategory::tryFrom($command->category);
|
||||
if ($category === null) {
|
||||
throw CategorieAppreciationInvalideException::pourValeur($command->category);
|
||||
}
|
||||
}
|
||||
|
||||
$template = AppreciationTemplate::creer(
|
||||
tenantId: TenantId::fromString($command->tenantId),
|
||||
teacherId: UserId::fromString($command->teacherId),
|
||||
title: $command->title,
|
||||
content: $command->content,
|
||||
category: $category,
|
||||
now: $this->clock->now(),
|
||||
);
|
||||
|
||||
$this->templateRepository->save($template);
|
||||
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\DeleteAppreciationTemplate;
|
||||
|
||||
final readonly class DeleteAppreciationTemplateCommand
|
||||
{
|
||||
public function __construct(
|
||||
public string $tenantId,
|
||||
public string $teacherId,
|
||||
public string $templateId,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\DeleteAppreciationTemplate;
|
||||
|
||||
use App\Scolarite\Domain\Exception\AppreciationTemplateNonTrouveeException;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDuModeleException;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplateId;
|
||||
use App\Scolarite\Domain\Repository\AppreciationTemplateRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
#[AsMessageHandler(bus: 'command.bus')]
|
||||
final readonly class DeleteAppreciationTemplateHandler
|
||||
{
|
||||
public function __construct(
|
||||
private AppreciationTemplateRepository $templateRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(DeleteAppreciationTemplateCommand $command): void
|
||||
{
|
||||
$tenantId = TenantId::fromString($command->tenantId);
|
||||
$templateId = AppreciationTemplateId::fromString($command->templateId);
|
||||
|
||||
$template = $this->templateRepository->findById($templateId, $tenantId);
|
||||
|
||||
if ($template === null) {
|
||||
throw AppreciationTemplateNonTrouveeException::withId($templateId);
|
||||
}
|
||||
|
||||
if ((string) $template->teacherId !== $command->teacherId) {
|
||||
throw NonProprietaireDuModeleException::withId($templateId);
|
||||
}
|
||||
|
||||
$this->templateRepository->delete($templateId, $tenantId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\SaveAppreciation;
|
||||
|
||||
final readonly class SaveAppreciationCommand
|
||||
{
|
||||
public function __construct(
|
||||
public string $tenantId,
|
||||
public string $gradeId,
|
||||
public string $teacherId,
|
||||
public ?string $appreciation,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\SaveAppreciation;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDeLEvaluationException;
|
||||
use App\Scolarite\Domain\Model\Grade\Grade;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeId;
|
||||
use App\Scolarite\Domain\Repository\EvaluationRepository;
|
||||
use App\Scolarite\Domain\Repository\GradeRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
#[AsMessageHandler(bus: 'command.bus')]
|
||||
final readonly class SaveAppreciationHandler
|
||||
{
|
||||
public function __construct(
|
||||
private EvaluationRepository $evaluationRepository,
|
||||
private GradeRepository $gradeRepository,
|
||||
private Clock $clock,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(SaveAppreciationCommand $command): Grade
|
||||
{
|
||||
$tenantId = TenantId::fromString($command->tenantId);
|
||||
$gradeId = GradeId::fromString($command->gradeId);
|
||||
$teacherId = UserId::fromString($command->teacherId);
|
||||
|
||||
$grade = $this->gradeRepository->get($gradeId, $tenantId);
|
||||
|
||||
$evaluation = $this->evaluationRepository->get($grade->evaluationId, $tenantId);
|
||||
|
||||
if ((string) $evaluation->teacherId !== (string) $teacherId) {
|
||||
throw NonProprietaireDeLEvaluationException::withId($grade->evaluationId);
|
||||
}
|
||||
|
||||
$grade->saisirAppreciation($command->appreciation, $this->clock->now());
|
||||
$this->gradeRepository->saveAppreciation($grade);
|
||||
|
||||
return $grade;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\UpdateAppreciationTemplate;
|
||||
|
||||
final readonly class UpdateAppreciationTemplateCommand
|
||||
{
|
||||
public function __construct(
|
||||
public string $tenantId,
|
||||
public string $teacherId,
|
||||
public string $templateId,
|
||||
public string $title,
|
||||
public string $content,
|
||||
public ?string $category,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Command\UpdateAppreciationTemplate;
|
||||
|
||||
use App\Scolarite\Domain\Exception\AppreciationTemplateNonTrouveeException;
|
||||
use App\Scolarite\Domain\Exception\CategorieAppreciationInvalideException;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDuModeleException;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationCategory;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplate;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplateId;
|
||||
use App\Scolarite\Domain\Repository\AppreciationTemplateRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
#[AsMessageHandler(bus: 'command.bus')]
|
||||
final readonly class UpdateAppreciationTemplateHandler
|
||||
{
|
||||
public function __construct(
|
||||
private AppreciationTemplateRepository $templateRepository,
|
||||
private Clock $clock,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(UpdateAppreciationTemplateCommand $command): AppreciationTemplate
|
||||
{
|
||||
$tenantId = TenantId::fromString($command->tenantId);
|
||||
$templateId = AppreciationTemplateId::fromString($command->templateId);
|
||||
|
||||
$template = $this->templateRepository->findById($templateId, $tenantId);
|
||||
|
||||
if ($template === null) {
|
||||
throw AppreciationTemplateNonTrouveeException::withId($templateId);
|
||||
}
|
||||
|
||||
if ((string) $template->teacherId !== $command->teacherId) {
|
||||
throw NonProprietaireDuModeleException::withId($templateId);
|
||||
}
|
||||
|
||||
$category = null;
|
||||
if ($command->category !== null) {
|
||||
$category = AppreciationCategory::tryFrom($command->category);
|
||||
if ($category === null) {
|
||||
throw CategorieAppreciationInvalideException::pourValeur($command->category);
|
||||
}
|
||||
}
|
||||
|
||||
$template->modifier(
|
||||
title: $command->title,
|
||||
content: $command->content,
|
||||
category: $category,
|
||||
now: $this->clock->now(),
|
||||
);
|
||||
|
||||
$this->templateRepository->save($template);
|
||||
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
13
backend/src/Scolarite/Application/Port/PeriodFinder.php
Normal file
13
backend/src/Scolarite/Application/Port/PeriodFinder.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Port;
|
||||
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
|
||||
interface PeriodFinder
|
||||
{
|
||||
public function findForDate(DateTimeImmutable $date, TenantId $tenantId): ?PeriodInfo;
|
||||
}
|
||||
17
backend/src/Scolarite/Application/Port/PeriodInfo.php
Normal file
17
backend/src/Scolarite/Application/Port/PeriodInfo.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Port;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
final readonly class PeriodInfo
|
||||
{
|
||||
public function __construct(
|
||||
public string $periodId,
|
||||
public DateTimeImmutable $startDate,
|
||||
public DateTimeImmutable $endDate,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Service;
|
||||
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassId;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Port\PeriodFinder;
|
||||
use App\Scolarite\Application\Port\PeriodInfo;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Evaluation;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Domain\Model\Grade\Grade;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeStatus;
|
||||
use App\Scolarite\Domain\Repository\EvaluationRepository;
|
||||
use App\Scolarite\Domain\Repository\EvaluationStatisticsRepository;
|
||||
use App\Scolarite\Domain\Repository\GradeRepository;
|
||||
use App\Scolarite\Domain\Repository\StudentAverageRepository;
|
||||
use App\Scolarite\Domain\Service\AverageCalculator;
|
||||
use App\Scolarite\Domain\Service\GradeEntry;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
|
||||
use function array_map;
|
||||
use function count;
|
||||
|
||||
final readonly class RecalculerMoyennesService
|
||||
{
|
||||
public function __construct(
|
||||
private EvaluationRepository $evaluationRepository,
|
||||
private GradeRepository $gradeRepository,
|
||||
private EvaluationStatisticsRepository $evaluationStatisticsRepository,
|
||||
private StudentAverageRepository $studentAverageRepository,
|
||||
private PeriodFinder $periodFinder,
|
||||
private AverageCalculator $calculator,
|
||||
) {
|
||||
}
|
||||
|
||||
public function recalculerStatistiquesEvaluation(EvaluationId $evaluationId, TenantId $tenantId): void
|
||||
{
|
||||
$grades = $this->gradeRepository->findByEvaluation($evaluationId, $tenantId);
|
||||
|
||||
$gradedValues = [];
|
||||
|
||||
foreach ($grades as $g) {
|
||||
if ($g->status === GradeStatus::GRADED && $g->value !== null) {
|
||||
$gradedValues[] = $g->value->value;
|
||||
}
|
||||
}
|
||||
|
||||
$stats = $this->calculator->calculateClassStatistics($gradedValues);
|
||||
$this->evaluationStatisticsRepository->save($evaluationId, $stats);
|
||||
}
|
||||
|
||||
public function recalculerMoyenneEleve(
|
||||
UserId $studentId,
|
||||
SubjectId $subjectId,
|
||||
ClassId $classId,
|
||||
PeriodInfo $period,
|
||||
TenantId $tenantId,
|
||||
): void {
|
||||
$evaluations = $this->evaluationRepository->findWithPublishedGradesBySubjectAndClassInDateRange(
|
||||
$subjectId,
|
||||
$classId,
|
||||
$period->startDate,
|
||||
$period->endDate,
|
||||
$tenantId,
|
||||
);
|
||||
|
||||
$evalIds = array_map(static fn (Evaluation $e) => $e->id, $evaluations);
|
||||
$allGradesByEval = $this->gradeRepository->findByEvaluations($evalIds, $tenantId);
|
||||
|
||||
$entries = [];
|
||||
|
||||
foreach ($evaluations as $eval) {
|
||||
/** @var list<Grade> $evalGrades */
|
||||
$evalGrades = $allGradesByEval[(string) $eval->id] ?? [];
|
||||
|
||||
foreach ($evalGrades as $grade) {
|
||||
if ($grade->studentId->equals($studentId)
|
||||
&& $grade->status === GradeStatus::GRADED
|
||||
&& $grade->value !== null
|
||||
) {
|
||||
$entries[] = new GradeEntry(
|
||||
value: $grade->value->value,
|
||||
gradeScale: $eval->gradeScale,
|
||||
coefficient: $eval->coefficient,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$subjectAverage = $this->calculator->calculateSubjectAverage($entries);
|
||||
|
||||
if ($subjectAverage !== null) {
|
||||
$this->studentAverageRepository->saveSubjectAverage(
|
||||
$tenantId,
|
||||
$studentId,
|
||||
$subjectId,
|
||||
$period->periodId,
|
||||
$subjectAverage,
|
||||
count($entries),
|
||||
);
|
||||
} else {
|
||||
$this->studentAverageRepository->deleteSubjectAverage(
|
||||
$studentId,
|
||||
$subjectId,
|
||||
$period->periodId,
|
||||
$tenantId,
|
||||
);
|
||||
}
|
||||
|
||||
// Moyenne générale
|
||||
$allSubjectAverages = $this->studentAverageRepository->findSubjectAveragesForStudent(
|
||||
$studentId,
|
||||
$period->periodId,
|
||||
$tenantId,
|
||||
);
|
||||
|
||||
$generalAverage = $this->calculator->calculateGeneralAverage($allSubjectAverages);
|
||||
|
||||
if ($generalAverage !== null) {
|
||||
$this->studentAverageRepository->saveGeneralAverage(
|
||||
$tenantId,
|
||||
$studentId,
|
||||
$period->periodId,
|
||||
$generalAverage,
|
||||
);
|
||||
} else {
|
||||
$this->studentAverageRepository->deleteGeneralAverage(
|
||||
$studentId,
|
||||
$period->periodId,
|
||||
$tenantId,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function recalculerTousElevesPourEvaluation(EvaluationId $evaluationId, TenantId $tenantId): void
|
||||
{
|
||||
$evaluation = $this->evaluationRepository->findById($evaluationId, $tenantId);
|
||||
|
||||
if ($evaluation === null || !$evaluation->notesPubliees()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$period = $this->periodFinder->findForDate($evaluation->evaluationDate, $tenantId);
|
||||
|
||||
if ($period === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$grades = $this->gradeRepository->findByEvaluation($evaluationId, $tenantId);
|
||||
$studentIds = [];
|
||||
|
||||
foreach ($grades as $g) {
|
||||
$studentIds[(string) $g->studentId] = $g->studentId;
|
||||
}
|
||||
|
||||
foreach ($studentIds as $studentId) {
|
||||
$this->recalculerMoyenneEleve(
|
||||
$studentId,
|
||||
$evaluation->subjectId,
|
||||
$evaluation->classId,
|
||||
$period,
|
||||
$tenantId,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Exception;
|
||||
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplateId;
|
||||
use DomainException;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
final class AppreciationTemplateNonTrouveeException extends DomainException
|
||||
{
|
||||
public static function withId(AppreciationTemplateId $id): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Le modèle d\'appréciation avec l\'ID "%s" n\'a pas été trouvé.',
|
||||
$id,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Exception;
|
||||
|
||||
use DomainException;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
final class AppreciationTropLongueException extends DomainException
|
||||
{
|
||||
public static function depasseLimite(int $length, int $maxLength): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'L\'appréciation dépasse la limite de %d caractères (%d donnés).',
|
||||
$maxLength,
|
||||
$length,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Exception;
|
||||
|
||||
use DomainException;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
final class CategorieAppreciationInvalideException extends DomainException
|
||||
{
|
||||
public static function pourValeur(string $value): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'La catégorie d\'appréciation "%s" n\'est pas valide. Valeurs acceptées : positive, neutral, improvement.',
|
||||
$value,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -18,4 +18,13 @@ final class GradeNotFoundException extends DomainException
|
||||
$id,
|
||||
));
|
||||
}
|
||||
|
||||
public static function forStudent(string $studentId, string $evaluationId): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Aucune note trouvée pour l\'élève "%s" dans l\'évaluation "%s".',
|
||||
$studentId,
|
||||
$evaluationId,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Exception;
|
||||
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplateId;
|
||||
use DomainException;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
final class NonProprietaireDuModeleException extends DomainException
|
||||
{
|
||||
public static function withId(AppreciationTemplateId $id): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Vous n\'êtes pas le propriétaire du modèle d\'appréciation "%s".',
|
||||
$id,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Model\Evaluation;
|
||||
|
||||
final readonly class ClassStatistics
|
||||
{
|
||||
public function __construct(
|
||||
public ?float $average,
|
||||
public ?float $min,
|
||||
public ?float $max,
|
||||
public ?float $median,
|
||||
public int $gradedCount,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Model\Grade;
|
||||
|
||||
enum AppreciationCategory: string
|
||||
{
|
||||
case POSITIVE = 'positive';
|
||||
case NEUTRAL = 'neutral';
|
||||
case IMPROVEMENT = 'improvement';
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Model\Grade;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
|
||||
final class AppreciationTemplate
|
||||
{
|
||||
public private(set) DateTimeImmutable $updatedAt;
|
||||
public private(set) int $usageCount;
|
||||
|
||||
private function __construct(
|
||||
public private(set) AppreciationTemplateId $id,
|
||||
public private(set) TenantId $tenantId,
|
||||
public private(set) UserId $teacherId,
|
||||
public private(set) string $title,
|
||||
public private(set) string $content,
|
||||
public private(set) ?AppreciationCategory $category,
|
||||
public private(set) DateTimeImmutable $createdAt,
|
||||
) {
|
||||
$this->updatedAt = $createdAt;
|
||||
$this->usageCount = 0;
|
||||
}
|
||||
|
||||
public static function creer(
|
||||
TenantId $tenantId,
|
||||
UserId $teacherId,
|
||||
string $title,
|
||||
string $content,
|
||||
?AppreciationCategory $category,
|
||||
DateTimeImmutable $now,
|
||||
): self {
|
||||
return new self(
|
||||
id: AppreciationTemplateId::generate(),
|
||||
tenantId: $tenantId,
|
||||
teacherId: $teacherId,
|
||||
title: $title,
|
||||
content: $content,
|
||||
category: $category,
|
||||
createdAt: $now,
|
||||
);
|
||||
}
|
||||
|
||||
public function modifier(
|
||||
string $title,
|
||||
string $content,
|
||||
?AppreciationCategory $category,
|
||||
DateTimeImmutable $now,
|
||||
): void {
|
||||
$this->title = $title;
|
||||
$this->content = $content;
|
||||
$this->category = $category;
|
||||
$this->updatedAt = $now;
|
||||
}
|
||||
|
||||
public function incrementerUtilisation(): void
|
||||
{
|
||||
++$this->usageCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal Pour usage Infrastructure uniquement
|
||||
*/
|
||||
public static function reconstitute(
|
||||
AppreciationTemplateId $id,
|
||||
TenantId $tenantId,
|
||||
UserId $teacherId,
|
||||
string $title,
|
||||
string $content,
|
||||
?AppreciationCategory $category,
|
||||
int $usageCount,
|
||||
DateTimeImmutable $createdAt,
|
||||
DateTimeImmutable $updatedAt,
|
||||
): self {
|
||||
$template = new self(
|
||||
id: $id,
|
||||
tenantId: $tenantId,
|
||||
teacherId: $teacherId,
|
||||
title: $title,
|
||||
content: $content,
|
||||
category: $category,
|
||||
createdAt: $createdAt,
|
||||
);
|
||||
|
||||
$template->updatedAt = $updatedAt;
|
||||
$template->usageCount = $usageCount;
|
||||
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Model\Grade;
|
||||
|
||||
use App\Shared\Domain\EntityId;
|
||||
|
||||
final readonly class AppreciationTemplateId extends EntityId
|
||||
{
|
||||
}
|
||||
@@ -7,6 +7,7 @@ namespace App\Scolarite\Domain\Model\Grade;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Event\NoteModifiee;
|
||||
use App\Scolarite\Domain\Event\NoteSaisie;
|
||||
use App\Scolarite\Domain\Exception\AppreciationTropLongueException;
|
||||
use App\Scolarite\Domain\Exception\NoteRequiseException;
|
||||
use App\Scolarite\Domain\Exception\ValeurNoteInvalideException;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
@@ -15,9 +16,15 @@ use App\Shared\Domain\AggregateRoot;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
|
||||
use function mb_strlen;
|
||||
|
||||
final class Grade extends AggregateRoot
|
||||
{
|
||||
public const int MAX_APPRECIATION_LENGTH = 500;
|
||||
|
||||
public private(set) DateTimeImmutable $updatedAt;
|
||||
public private(set) ?string $appreciation;
|
||||
public private(set) ?DateTimeImmutable $appreciationUpdatedAt;
|
||||
|
||||
private function __construct(
|
||||
public private(set) GradeId $id,
|
||||
@@ -30,6 +37,8 @@ final class Grade extends AggregateRoot
|
||||
public private(set) DateTimeImmutable $createdAt,
|
||||
) {
|
||||
$this->updatedAt = $createdAt;
|
||||
$this->appreciation = null;
|
||||
$this->appreciationUpdatedAt = null;
|
||||
}
|
||||
|
||||
public static function saisir(
|
||||
@@ -96,6 +105,17 @@ final class Grade extends AggregateRoot
|
||||
));
|
||||
}
|
||||
|
||||
public function saisirAppreciation(?string $appreciation, DateTimeImmutable $now): void
|
||||
{
|
||||
if ($appreciation !== null && mb_strlen($appreciation) > self::MAX_APPRECIATION_LENGTH) {
|
||||
throw AppreciationTropLongueException::depasseLimite(mb_strlen($appreciation), self::MAX_APPRECIATION_LENGTH);
|
||||
}
|
||||
|
||||
$this->appreciation = $appreciation !== null && $appreciation !== '' ? $appreciation : null;
|
||||
$this->appreciationUpdatedAt = $now;
|
||||
$this->updatedAt = $now;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal Pour usage Infrastructure uniquement
|
||||
*/
|
||||
@@ -109,6 +129,8 @@ final class Grade extends AggregateRoot
|
||||
UserId $createdBy,
|
||||
DateTimeImmutable $createdAt,
|
||||
DateTimeImmutable $updatedAt,
|
||||
?string $appreciation = null,
|
||||
?DateTimeImmutable $appreciationUpdatedAt = null,
|
||||
): self {
|
||||
$grade = new self(
|
||||
id: $id,
|
||||
@@ -122,6 +144,8 @@ final class Grade extends AggregateRoot
|
||||
);
|
||||
|
||||
$grade->updatedAt = $updatedAt;
|
||||
$grade->appreciation = $appreciation;
|
||||
$grade->appreciationUpdatedAt = $appreciationUpdatedAt;
|
||||
|
||||
return $grade;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Repository;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplate;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplateId;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
|
||||
interface AppreciationTemplateRepository
|
||||
{
|
||||
public function save(AppreciationTemplate $template): void;
|
||||
|
||||
public function findById(AppreciationTemplateId $id, TenantId $tenantId): ?AppreciationTemplate;
|
||||
|
||||
/** @return array<AppreciationTemplate> */
|
||||
public function findByTeacher(UserId $teacherId, TenantId $tenantId): array;
|
||||
|
||||
public function delete(AppreciationTemplateId $id, TenantId $tenantId): void;
|
||||
}
|
||||
@@ -5,11 +5,13 @@ declare(strict_types=1);
|
||||
namespace App\Scolarite\Domain\Repository;
|
||||
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassId;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Exception\EvaluationNotFoundException;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Evaluation;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
|
||||
interface EvaluationRepository
|
||||
{
|
||||
@@ -28,4 +30,16 @@ interface EvaluationRepository
|
||||
|
||||
/** @return array<Evaluation> */
|
||||
public function findByClass(ClassId $classId, TenantId $tenantId): array;
|
||||
|
||||
/** @return array<Evaluation> Toutes les évaluations publiées (notes visibles) */
|
||||
public function findAllWithPublishedGrades(TenantId $tenantId): array;
|
||||
|
||||
/** @return array<Evaluation> Évaluations dont les notes sont publiées */
|
||||
public function findWithPublishedGradesBySubjectAndClassInDateRange(
|
||||
SubjectId $subjectId,
|
||||
ClassId $classId,
|
||||
DateTimeImmutable $startDate,
|
||||
DateTimeImmutable $endDate,
|
||||
TenantId $tenantId,
|
||||
): array;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Repository;
|
||||
|
||||
use App\Scolarite\Domain\Model\Evaluation\ClassStatistics;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
|
||||
interface EvaluationStatisticsRepository
|
||||
{
|
||||
public function save(EvaluationId $evaluationId, ClassStatistics $statistics): void;
|
||||
|
||||
public function findByEvaluation(EvaluationId $evaluationId): ?ClassStatistics;
|
||||
|
||||
public function delete(EvaluationId $evaluationId): void;
|
||||
}
|
||||
@@ -14,6 +14,8 @@ interface GradeRepository
|
||||
{
|
||||
public function save(Grade $grade): void;
|
||||
|
||||
public function saveAppreciation(Grade $grade): void;
|
||||
|
||||
/** @throws GradeNotFoundException */
|
||||
public function get(GradeId $id, TenantId $tenantId): Grade;
|
||||
|
||||
@@ -22,5 +24,12 @@ interface GradeRepository
|
||||
/** @return array<Grade> */
|
||||
public function findByEvaluation(EvaluationId $evaluationId, TenantId $tenantId): array;
|
||||
|
||||
/**
|
||||
* @param array<EvaluationId> $evaluationIds
|
||||
*
|
||||
* @return array<string, list<Grade>> Clé = evaluationId (string)
|
||||
*/
|
||||
public function findByEvaluations(array $evaluationIds, TenantId $tenantId): array;
|
||||
|
||||
public function hasGradesForEvaluation(EvaluationId $evaluationId, TenantId $tenantId): bool;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Repository;
|
||||
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
|
||||
interface StudentAverageRepository
|
||||
{
|
||||
public function saveSubjectAverage(
|
||||
TenantId $tenantId,
|
||||
UserId $studentId,
|
||||
SubjectId $subjectId,
|
||||
string $periodId,
|
||||
float $average,
|
||||
int $gradeCount,
|
||||
): void;
|
||||
|
||||
public function saveGeneralAverage(
|
||||
TenantId $tenantId,
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
float $average,
|
||||
): void;
|
||||
|
||||
/** @return list<float> Moyennes matières d'un élève pour une période */
|
||||
public function findSubjectAveragesForStudent(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): array;
|
||||
|
||||
/** @return list<array{subjectId: string, subjectName: string|null, average: float, gradeCount: int}> */
|
||||
public function findDetailedSubjectAveragesForStudent(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): array;
|
||||
|
||||
public function findGeneralAverageForStudent(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): ?float;
|
||||
|
||||
public function deleteSubjectAverage(
|
||||
UserId $studentId,
|
||||
SubjectId $subjectId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): void;
|
||||
|
||||
public function deleteGeneralAverage(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): void;
|
||||
}
|
||||
102
backend/src/Scolarite/Domain/Service/AverageCalculator.php
Normal file
102
backend/src/Scolarite/Domain/Service/AverageCalculator.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Service;
|
||||
|
||||
use App\Scolarite\Domain\Model\Evaluation\ClassStatistics;
|
||||
|
||||
use function array_sum;
|
||||
use function count;
|
||||
use function intdiv;
|
||||
use function round;
|
||||
use function sort;
|
||||
|
||||
final class AverageCalculator
|
||||
{
|
||||
/**
|
||||
* Moyenne matière pondérée par les coefficients, normalisée sur /20.
|
||||
* Les notes absent/dispensé doivent être exclues en amont.
|
||||
*
|
||||
* Formule : Σ(note_sur_20 × coef) / Σ(coef)
|
||||
*
|
||||
* @param list<GradeEntry> $grades
|
||||
*/
|
||||
public function calculateSubjectAverage(array $grades): ?float
|
||||
{
|
||||
if ($grades === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$sumWeighted = 0.0;
|
||||
$sumCoef = 0.0;
|
||||
|
||||
foreach ($grades as $entry) {
|
||||
$normalizedValue = $entry->gradeScale->convertTo20($entry->value);
|
||||
$sumWeighted += $normalizedValue * $entry->coefficient->value;
|
||||
$sumCoef += $entry->coefficient->value;
|
||||
}
|
||||
|
||||
return round($sumWeighted / $sumCoef, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Moyenne générale : moyenne arithmétique des moyennes matières.
|
||||
* Les matières sans note sont exclues en amont.
|
||||
*
|
||||
* @param list<float> $subjectAverages
|
||||
*/
|
||||
public function calculateGeneralAverage(array $subjectAverages): ?float
|
||||
{
|
||||
if ($subjectAverages === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return round(array_sum($subjectAverages) / count($subjectAverages), 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Statistiques de classe pour une évaluation : min, max, moyenne, médiane.
|
||||
* Les absents et dispensés doivent être exclus en amont.
|
||||
*
|
||||
* @param list<float> $values
|
||||
*/
|
||||
public function calculateClassStatistics(array $values): ClassStatistics
|
||||
{
|
||||
if ($values === []) {
|
||||
return new ClassStatistics(
|
||||
average: null,
|
||||
min: null,
|
||||
max: null,
|
||||
median: null,
|
||||
gradedCount: 0,
|
||||
);
|
||||
}
|
||||
|
||||
sort($values);
|
||||
$count = count($values);
|
||||
|
||||
return new ClassStatistics(
|
||||
average: round(array_sum($values) / $count, 2),
|
||||
min: $values[0],
|
||||
max: $values[$count - 1],
|
||||
median: $this->calculateMedian($values),
|
||||
gradedCount: $count,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<float> $sortedValues Valeurs déjà triées par ordre croissant
|
||||
*/
|
||||
private function calculateMedian(array $sortedValues): float
|
||||
{
|
||||
$count = count($sortedValues);
|
||||
$middle = intdiv($count, 2);
|
||||
|
||||
if ($count % 2 === 0) {
|
||||
return round(($sortedValues[$middle - 1] + $sortedValues[$middle]) / 2, 2);
|
||||
}
|
||||
|
||||
return $sortedValues[$middle];
|
||||
}
|
||||
}
|
||||
18
backend/src/Scolarite/Domain/Service/GradeEntry.php
Normal file
18
backend/src/Scolarite/Domain/Service/GradeEntry.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Domain\Service;
|
||||
|
||||
use App\Scolarite\Domain\Model\Evaluation\Coefficient;
|
||||
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
|
||||
|
||||
final readonly class GradeEntry
|
||||
{
|
||||
public function __construct(
|
||||
public float $value,
|
||||
public GradeScale $gradeScale,
|
||||
public Coefficient $coefficient,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
use App\Scolarite\Application\Command\CreateAppreciationTemplate\CreateAppreciationTemplateCommand;
|
||||
use App\Scolarite\Application\Command\CreateAppreciationTemplate\CreateAppreciationTemplateHandler;
|
||||
use App\Scolarite\Domain\Exception\CategorieAppreciationInvalideException;
|
||||
use App\Scolarite\Infrastructure\Api\Resource\AppreciationTemplateResource;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use Override;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
|
||||
/**
|
||||
* @implements ProcessorInterface<AppreciationTemplateResource, AppreciationTemplateResource>
|
||||
*/
|
||||
final readonly class CreateAppreciationTemplateProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private CreateAppreciationTemplateHandler $handler,
|
||||
private TenantContext $tenantContext,
|
||||
private Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AppreciationTemplateResource $data
|
||||
*/
|
||||
#[Override]
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): AppreciationTemplateResource
|
||||
{
|
||||
if (!$this->tenantContext->hasTenant()) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (!$user instanceof SecurityUser) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
|
||||
}
|
||||
|
||||
try {
|
||||
$command = new CreateAppreciationTemplateCommand(
|
||||
tenantId: (string) $this->tenantContext->getCurrentTenantId(),
|
||||
teacherId: $user->userId(),
|
||||
title: $data->title ?? '',
|
||||
content: $data->content ?? '',
|
||||
category: $data->category,
|
||||
);
|
||||
|
||||
$template = ($this->handler)($command);
|
||||
|
||||
return AppreciationTemplateResource::fromDomain($template);
|
||||
} catch (CategorieAppreciationInvalideException $e) {
|
||||
throw new BadRequestHttpException($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
use App\Scolarite\Application\Command\DeleteAppreciationTemplate\DeleteAppreciationTemplateCommand;
|
||||
use App\Scolarite\Application\Command\DeleteAppreciationTemplate\DeleteAppreciationTemplateHandler;
|
||||
use App\Scolarite\Domain\Exception\AppreciationTemplateNonTrouveeException;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDuModeleException;
|
||||
use App\Scolarite\Infrastructure\Api\Resource\AppreciationTemplateResource;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use Override;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
|
||||
/**
|
||||
* @implements ProcessorInterface<AppreciationTemplateResource, null>
|
||||
*/
|
||||
final readonly class DeleteAppreciationTemplateProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private DeleteAppreciationTemplateHandler $handler,
|
||||
private TenantContext $tenantContext,
|
||||
private Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AppreciationTemplateResource $data
|
||||
*/
|
||||
#[Override]
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): null
|
||||
{
|
||||
if (!$this->tenantContext->hasTenant()) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (!$user instanceof SecurityUser) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
|
||||
}
|
||||
|
||||
/** @var string $templateId */
|
||||
$templateId = $uriVariables['id'] ?? '';
|
||||
|
||||
try {
|
||||
$command = new DeleteAppreciationTemplateCommand(
|
||||
tenantId: (string) $this->tenantContext->getCurrentTenantId(),
|
||||
teacherId: $user->userId(),
|
||||
templateId: $templateId,
|
||||
);
|
||||
|
||||
($this->handler)($command);
|
||||
|
||||
return null;
|
||||
} catch (AppreciationTemplateNonTrouveeException $e) {
|
||||
throw new NotFoundHttpException($e->getMessage());
|
||||
} catch (NonProprietaireDuModeleException $e) {
|
||||
throw new AccessDeniedHttpException($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
use App\Scolarite\Application\Command\SaveAppreciation\SaveAppreciationCommand;
|
||||
use App\Scolarite\Application\Command\SaveAppreciation\SaveAppreciationHandler;
|
||||
use App\Scolarite\Domain\Exception\AppreciationTropLongueException;
|
||||
use App\Scolarite\Domain\Exception\GradeNotFoundException;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDeLEvaluationException;
|
||||
use App\Scolarite\Infrastructure\Api\Resource\GradeResource;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use Override;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
|
||||
/**
|
||||
* @implements ProcessorInterface<GradeResource, GradeResource>
|
||||
*/
|
||||
final readonly class SaveAppreciationProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private SaveAppreciationHandler $handler,
|
||||
private TenantContext $tenantContext,
|
||||
private Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param GradeResource $data
|
||||
*/
|
||||
#[Override]
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): GradeResource
|
||||
{
|
||||
if (!$this->tenantContext->hasTenant()) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (!$user instanceof SecurityUser) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
|
||||
}
|
||||
|
||||
/** @var string $gradeId */
|
||||
$gradeId = $uriVariables['id'] ?? '';
|
||||
|
||||
try {
|
||||
$command = new SaveAppreciationCommand(
|
||||
tenantId: (string) $this->tenantContext->getCurrentTenantId(),
|
||||
gradeId: $gradeId,
|
||||
teacherId: $user->userId(),
|
||||
appreciation: $data->appreciation,
|
||||
);
|
||||
|
||||
$grade = ($this->handler)($command);
|
||||
|
||||
return GradeResource::fromDomain($grade);
|
||||
} catch (NonProprietaireDeLEvaluationException $e) {
|
||||
throw new AccessDeniedHttpException($e->getMessage());
|
||||
} catch (GradeNotFoundException) {
|
||||
throw new NotFoundHttpException('Aucune note trouvée. Veuillez d\'abord saisir une note pour cet élève avant d\'ajouter une appréciation.');
|
||||
} catch (AppreciationTropLongueException $e) {
|
||||
throw new BadRequestHttpException($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
use App\Scolarite\Application\Command\UpdateAppreciationTemplate\UpdateAppreciationTemplateCommand;
|
||||
use App\Scolarite\Application\Command\UpdateAppreciationTemplate\UpdateAppreciationTemplateHandler;
|
||||
use App\Scolarite\Domain\Exception\AppreciationTemplateNonTrouveeException;
|
||||
use App\Scolarite\Domain\Exception\CategorieAppreciationInvalideException;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDuModeleException;
|
||||
use App\Scolarite\Infrastructure\Api\Resource\AppreciationTemplateResource;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use Override;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
|
||||
/**
|
||||
* @implements ProcessorInterface<AppreciationTemplateResource, AppreciationTemplateResource>
|
||||
*/
|
||||
final readonly class UpdateAppreciationTemplateProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private UpdateAppreciationTemplateHandler $handler,
|
||||
private TenantContext $tenantContext,
|
||||
private Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AppreciationTemplateResource $data
|
||||
*/
|
||||
#[Override]
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): AppreciationTemplateResource
|
||||
{
|
||||
if (!$this->tenantContext->hasTenant()) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (!$user instanceof SecurityUser) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
|
||||
}
|
||||
|
||||
/** @var string $templateId */
|
||||
$templateId = $uriVariables['id'] ?? '';
|
||||
|
||||
try {
|
||||
$command = new UpdateAppreciationTemplateCommand(
|
||||
tenantId: (string) $this->tenantContext->getCurrentTenantId(),
|
||||
teacherId: $user->userId(),
|
||||
templateId: $templateId,
|
||||
title: $data->title ?? '',
|
||||
content: $data->content ?? '',
|
||||
category: $data->category,
|
||||
);
|
||||
|
||||
$template = ($this->handler)($command);
|
||||
|
||||
return AppreciationTemplateResource::fromDomain($template);
|
||||
} catch (AppreciationTemplateNonTrouveeException $e) {
|
||||
throw new NotFoundHttpException($e->getMessage());
|
||||
} catch (NonProprietaireDuModeleException $e) {
|
||||
throw new AccessDeniedHttpException($e->getMessage());
|
||||
} catch (CategorieAppreciationInvalideException $e) {
|
||||
throw new BadRequestHttpException($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Provider;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
use App\Scolarite\Domain\Repository\AppreciationTemplateRepository;
|
||||
use App\Scolarite\Infrastructure\Api\Resource\AppreciationTemplateResource;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
|
||||
use function array_map;
|
||||
|
||||
use Override;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
|
||||
/**
|
||||
* @implements ProviderInterface<AppreciationTemplateResource>
|
||||
*/
|
||||
final readonly class AppreciationTemplateCollectionProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private AppreciationTemplateRepository $templateRepository,
|
||||
private TenantContext $tenantContext,
|
||||
private Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
/** @return array<AppreciationTemplateResource> */
|
||||
#[Override]
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
|
||||
{
|
||||
if (!$this->tenantContext->hasTenant()) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (!$user instanceof SecurityUser) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
|
||||
}
|
||||
|
||||
$tenantId = $this->tenantContext->getCurrentTenantId();
|
||||
$teacherId = UserId::fromString($user->userId());
|
||||
|
||||
$templates = $this->templateRepository->findByTeacher($teacherId, $tenantId);
|
||||
|
||||
return array_map(
|
||||
static fn ($template) => AppreciationTemplateResource::fromDomain($template),
|
||||
$templates,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Provider;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Administration\Domain\Model\User\Role;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationStatus;
|
||||
use App\Scolarite\Infrastructure\Api\Resource\ClassStatisticsResource;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use Doctrine\DBAL\Connection;
|
||||
|
||||
use function in_array;
|
||||
|
||||
use Override;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
|
||||
/**
|
||||
* @implements ProviderInterface<ClassStatisticsResource>
|
||||
*/
|
||||
final readonly class ClassStatisticsProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private Connection $connection,
|
||||
private TenantContext $tenantContext,
|
||||
private Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ClassStatisticsResource
|
||||
{
|
||||
if (!$this->tenantContext->hasTenant()) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (!$user instanceof SecurityUser) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
|
||||
}
|
||||
|
||||
$isStaff = $this->hasAnyRole($user->getRoles(), [
|
||||
Role::ADMIN->value,
|
||||
Role::PROF->value,
|
||||
Role::VIE_SCOLAIRE->value,
|
||||
]);
|
||||
|
||||
if (!$isStaff) {
|
||||
throw new AccessDeniedHttpException('Accès réservé au personnel éducatif.');
|
||||
}
|
||||
|
||||
/** @var string $classId */
|
||||
$classId = $uriVariables['classId'];
|
||||
$tenantId = (string) $this->tenantContext->getCurrentTenantId();
|
||||
|
||||
$resource = new ClassStatisticsResource();
|
||||
$resource->classId = $classId;
|
||||
|
||||
$rows = $this->connection->fetchAllAssociative(
|
||||
'SELECT e.id as evaluation_id, e.title, es.average, es.min_grade, es.max_grade, es.median_grade, es.graded_count
|
||||
FROM evaluations e
|
||||
LEFT JOIN evaluation_statistics es ON es.evaluation_id = e.id
|
||||
WHERE e.class_id = :class_id
|
||||
AND e.tenant_id = :tenant_id
|
||||
AND e.status != :deleted
|
||||
AND e.grades_published_at IS NOT NULL
|
||||
ORDER BY e.evaluation_date DESC',
|
||||
[
|
||||
'class_id' => $classId,
|
||||
'tenant_id' => $tenantId,
|
||||
'deleted' => EvaluationStatus::DELETED->value,
|
||||
],
|
||||
);
|
||||
|
||||
foreach ($rows as $row) {
|
||||
/** @var string $evaluationId */
|
||||
$evaluationId = $row['evaluation_id'];
|
||||
/** @var string $title */
|
||||
$title = $row['title'];
|
||||
/** @var string|float|null $average */
|
||||
$average = $row['average'];
|
||||
/** @var string|float|null $minGrade */
|
||||
$minGrade = $row['min_grade'];
|
||||
/** @var string|float|null $maxGrade */
|
||||
$maxGrade = $row['max_grade'];
|
||||
/** @var string|float|null $medianGrade */
|
||||
$medianGrade = $row['median_grade'];
|
||||
/** @var string|int|null $gradedCount */
|
||||
$gradedCount = $row['graded_count'];
|
||||
|
||||
$resource->evaluations[] = [
|
||||
'evaluationId' => $evaluationId,
|
||||
'title' => $title,
|
||||
'average' => $average !== null ? (float) $average : null,
|
||||
'min' => $minGrade !== null ? (float) $minGrade : null,
|
||||
'max' => $maxGrade !== null ? (float) $maxGrade : null,
|
||||
'median' => $medianGrade !== null ? (float) $medianGrade : null,
|
||||
'gradedCount' => $gradedCount !== null ? (int) $gradedCount : 0,
|
||||
];
|
||||
}
|
||||
|
||||
return $resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $userRoles
|
||||
* @param list<string> $allowedRoles
|
||||
*/
|
||||
private function hasAnyRole(array $userRoles, array $allowedRoles): bool
|
||||
{
|
||||
foreach ($userRoles as $role) {
|
||||
if (in_array($role, $allowedRoles, true)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Provider;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Domain\Repository\EvaluationRepository;
|
||||
use App\Scolarite\Domain\Repository\EvaluationStatisticsRepository;
|
||||
use App\Scolarite\Infrastructure\Api\Resource\EvaluationStatisticsResource;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use InvalidArgumentException;
|
||||
use Override;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
|
||||
/**
|
||||
* @implements ProviderInterface<EvaluationStatisticsResource>
|
||||
*/
|
||||
final readonly class EvaluationStatisticsProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private EvaluationRepository $evaluationRepository,
|
||||
private EvaluationStatisticsRepository $evaluationStatisticsRepository,
|
||||
private TenantContext $tenantContext,
|
||||
private Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): EvaluationStatisticsResource
|
||||
{
|
||||
if (!$this->tenantContext->hasTenant()) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (!$user instanceof SecurityUser) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
|
||||
}
|
||||
|
||||
/** @var string $evaluationId */
|
||||
$evaluationId = $uriVariables['evaluationId'];
|
||||
|
||||
try {
|
||||
$evalId = EvaluationId::fromString($evaluationId);
|
||||
} catch (InvalidArgumentException) {
|
||||
throw new BadRequestHttpException('Identifiant d\'évaluation invalide.');
|
||||
}
|
||||
|
||||
$tenantId = $this->tenantContext->getCurrentTenantId();
|
||||
|
||||
$evaluation = $this->evaluationRepository->findById($evalId, $tenantId);
|
||||
|
||||
if ($evaluation === null) {
|
||||
throw new NotFoundHttpException('Évaluation non trouvée.');
|
||||
}
|
||||
|
||||
if ((string) $evaluation->teacherId !== $user->userId()) {
|
||||
throw new AccessDeniedHttpException('Vous n\'êtes pas le propriétaire de cette évaluation.');
|
||||
}
|
||||
|
||||
$statistics = $this->evaluationStatisticsRepository->findByEvaluation($evalId);
|
||||
|
||||
if ($statistics === null) {
|
||||
throw new NotFoundHttpException('Statistiques non disponibles pour cette évaluation.');
|
||||
}
|
||||
|
||||
return EvaluationStatisticsResource::fromStatistics($evaluationId, $statistics);
|
||||
}
|
||||
}
|
||||
@@ -76,7 +76,8 @@ final readonly class GradeCollectionProvider implements ProviderInterface
|
||||
// Return all students in the class, with LEFT JOIN to grades
|
||||
$rows = $this->connection->fetchAllAssociative(
|
||||
'SELECT u.id AS student_id, u.first_name, u.last_name,
|
||||
g.id AS grade_id, g.evaluation_id, g.value, g.status AS grade_status
|
||||
g.id AS grade_id, g.evaluation_id, g.value, g.status AS grade_status,
|
||||
g.appreciation
|
||||
FROM class_assignments ca
|
||||
JOIN users u ON u.id = ca.user_id
|
||||
LEFT JOIN grades g ON g.student_id = u.id AND g.evaluation_id = :evaluation_id AND g.tenant_id = :tenant_id
|
||||
@@ -96,7 +97,7 @@ final readonly class GradeCollectionProvider implements ProviderInterface
|
||||
$studentId = $row['student_id'];
|
||||
/** @var string|null $gradeId */
|
||||
$gradeId = $row['grade_id'] ?? null;
|
||||
$resource->id = $gradeId ?? $studentId;
|
||||
$resource->id = $gradeId ?? 'pending-' . $studentId;
|
||||
$resource->evaluationId = $evaluationIdStr;
|
||||
$resource->studentId = $studentId;
|
||||
/** @var string|null $firstName */
|
||||
@@ -112,6 +113,9 @@ final readonly class GradeCollectionProvider implements ProviderInterface
|
||||
/** @var string|null $gradeStatus */
|
||||
$gradeStatus = $row['grade_status'] ?? null;
|
||||
$resource->status = $gradeStatus;
|
||||
/** @var string|null $appreciation */
|
||||
$appreciation = $row['appreciation'] ?? null;
|
||||
$resource->appreciation = $appreciation;
|
||||
|
||||
return $resource;
|
||||
}, $rows);
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Provider;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Administration\Domain\Model\User\Role;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
use App\Scolarite\Domain\Repository\StudentAverageRepository;
|
||||
use App\Scolarite\Infrastructure\Api\Resource\StudentAveragesResource;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
|
||||
use function in_array;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Override;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
|
||||
/**
|
||||
* @implements ProviderInterface<StudentAveragesResource>
|
||||
*/
|
||||
final readonly class StudentAveragesProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private StudentAverageRepository $studentAverageRepository,
|
||||
private TenantContext $tenantContext,
|
||||
private Security $security,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): StudentAveragesResource
|
||||
{
|
||||
if (!$this->tenantContext->hasTenant()) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.');
|
||||
}
|
||||
|
||||
$user = $this->security->getUser();
|
||||
|
||||
if (!$user instanceof SecurityUser) {
|
||||
throw new UnauthorizedHttpException('Bearer', 'Authentification requise.');
|
||||
}
|
||||
|
||||
/** @var string $studentId */
|
||||
$studentId = $uriVariables['studentId'];
|
||||
|
||||
try {
|
||||
$userId = UserId::fromString($studentId);
|
||||
} catch (InvalidArgumentException) {
|
||||
throw new BadRequestHttpException('Identifiant d\'élève invalide.');
|
||||
}
|
||||
|
||||
// L'élève peut voir ses propres moyennes, les enseignants et admins aussi
|
||||
$isOwner = $user->userId() === $studentId;
|
||||
$isStaff = $this->hasAnyRole($user->getRoles(), [
|
||||
Role::ADMIN->value,
|
||||
Role::PROF->value,
|
||||
Role::VIE_SCOLAIRE->value,
|
||||
]);
|
||||
|
||||
if (!$isOwner && !$isStaff) {
|
||||
throw new AccessDeniedHttpException('Accès non autorisé aux moyennes de cet élève.');
|
||||
}
|
||||
|
||||
$tenantId = $this->tenantContext->getCurrentTenantId();
|
||||
|
||||
/** @var array<string, mixed> $filters */
|
||||
$filters = $context['filters'] ?? [];
|
||||
/** @var string|null $periodId */
|
||||
$periodId = $filters['periodId'] ?? null;
|
||||
|
||||
$resource = new StudentAveragesResource();
|
||||
$resource->studentId = $studentId;
|
||||
$resource->periodId = $periodId;
|
||||
|
||||
if ($periodId === null) {
|
||||
return $resource;
|
||||
}
|
||||
|
||||
$resource->subjectAverages = $this->studentAverageRepository->findDetailedSubjectAveragesForStudent(
|
||||
$userId,
|
||||
$periodId,
|
||||
$tenantId,
|
||||
);
|
||||
|
||||
$resource->generalAverage = $this->studentAverageRepository->findGeneralAverageForStudent(
|
||||
$userId,
|
||||
$periodId,
|
||||
$tenantId,
|
||||
);
|
||||
|
||||
return $resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $userRoles
|
||||
* @param list<string> $allowedRoles
|
||||
*/
|
||||
private function hasAnyRole(array $userRoles, array $allowedRoles): bool
|
||||
{
|
||||
foreach ($userRoles as $role) {
|
||||
if (in_array($role, $allowedRoles, true)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Resource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiProperty;
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Delete;
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use ApiPlatform\Metadata\Put;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplate;
|
||||
use App\Scolarite\Infrastructure\Api\Processor\CreateAppreciationTemplateProcessor;
|
||||
use App\Scolarite\Infrastructure\Api\Processor\DeleteAppreciationTemplateProcessor;
|
||||
use App\Scolarite\Infrastructure\Api\Processor\UpdateAppreciationTemplateProcessor;
|
||||
use App\Scolarite\Infrastructure\Api\Provider\AppreciationTemplateCollectionProvider;
|
||||
use DateTimeImmutable;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[ApiResource(
|
||||
shortName: 'AppreciationTemplate',
|
||||
operations: [
|
||||
new GetCollection(
|
||||
uriTemplate: '/me/appreciation-templates',
|
||||
provider: AppreciationTemplateCollectionProvider::class,
|
||||
name: 'get_appreciation_templates',
|
||||
),
|
||||
new Post(
|
||||
uriTemplate: '/me/appreciation-templates',
|
||||
read: false,
|
||||
processor: CreateAppreciationTemplateProcessor::class,
|
||||
validationContext: ['groups' => ['Default', 'create']],
|
||||
name: 'create_appreciation_template',
|
||||
),
|
||||
new Put(
|
||||
uriTemplate: '/me/appreciation-templates/{id}',
|
||||
read: false,
|
||||
processor: UpdateAppreciationTemplateProcessor::class,
|
||||
validationContext: ['groups' => ['Default', 'update']],
|
||||
name: 'update_appreciation_template',
|
||||
),
|
||||
new Delete(
|
||||
uriTemplate: '/me/appreciation-templates/{id}',
|
||||
read: false,
|
||||
processor: DeleteAppreciationTemplateProcessor::class,
|
||||
name: 'delete_appreciation_template',
|
||||
),
|
||||
],
|
||||
)]
|
||||
final class AppreciationTemplateResource
|
||||
{
|
||||
#[ApiProperty(identifier: true)]
|
||||
public ?string $id = null;
|
||||
|
||||
#[Assert\NotBlank(groups: ['create', 'update'])]
|
||||
#[Assert\Length(max: 100, groups: ['create', 'update'])]
|
||||
public ?string $title = null;
|
||||
|
||||
#[Assert\NotBlank(groups: ['create', 'update'])]
|
||||
#[Assert\Length(max: 500, groups: ['create', 'update'])]
|
||||
public ?string $content = null;
|
||||
|
||||
public ?string $category = null;
|
||||
|
||||
public ?int $usageCount = null;
|
||||
|
||||
public ?DateTimeImmutable $createdAt = null;
|
||||
|
||||
public ?DateTimeImmutable $updatedAt = null;
|
||||
|
||||
public static function fromDomain(AppreciationTemplate $template): self
|
||||
{
|
||||
$resource = new self();
|
||||
$resource->id = (string) $template->id;
|
||||
$resource->title = $template->title;
|
||||
$resource->content = $template->content;
|
||||
$resource->category = $template->category?->value;
|
||||
$resource->usageCount = $template->usageCount;
|
||||
$resource->createdAt = $template->createdAt;
|
||||
$resource->updatedAt = $template->updatedAt;
|
||||
|
||||
return $resource;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Resource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiProperty;
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use App\Scolarite\Infrastructure\Api\Provider\ClassStatisticsProvider;
|
||||
|
||||
#[ApiResource(
|
||||
shortName: 'ClassStatistics',
|
||||
operations: [
|
||||
new Get(
|
||||
uriTemplate: '/classes/{classId}/statistics',
|
||||
provider: ClassStatisticsProvider::class,
|
||||
name: 'get_class_statistics',
|
||||
),
|
||||
],
|
||||
)]
|
||||
final class ClassStatisticsResource
|
||||
{
|
||||
#[ApiProperty(identifier: true)]
|
||||
public ?string $classId = null;
|
||||
|
||||
public ?string $periodId = null;
|
||||
|
||||
/** @var list<array{evaluationId: string, title: string, average: float|null, min: float|null, max: float|null, median: float|null, gradedCount: int}> */
|
||||
public array $evaluations = [];
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Resource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiProperty;
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use App\Scolarite\Domain\Model\Evaluation\ClassStatistics;
|
||||
use App\Scolarite\Infrastructure\Api\Provider\EvaluationStatisticsProvider;
|
||||
|
||||
#[ApiResource(
|
||||
shortName: 'EvaluationStatistics',
|
||||
operations: [
|
||||
new Get(
|
||||
uriTemplate: '/evaluations/{evaluationId}/statistics',
|
||||
provider: EvaluationStatisticsProvider::class,
|
||||
name: 'get_evaluation_statistics',
|
||||
),
|
||||
],
|
||||
)]
|
||||
final class EvaluationStatisticsResource
|
||||
{
|
||||
#[ApiProperty(identifier: true)]
|
||||
public ?string $evaluationId = null;
|
||||
|
||||
public ?float $average = null;
|
||||
|
||||
public ?float $min = null;
|
||||
|
||||
public ?float $max = null;
|
||||
|
||||
public ?float $median = null;
|
||||
|
||||
public int $gradedCount = 0;
|
||||
|
||||
public static function fromStatistics(string $evaluationId, ClassStatistics $statistics): self
|
||||
{
|
||||
$resource = new self();
|
||||
$resource->evaluationId = $evaluationId;
|
||||
$resource->average = $statistics->average;
|
||||
$resource->min = $statistics->min;
|
||||
$resource->max = $statistics->max;
|
||||
$resource->median = $statistics->median;
|
||||
$resource->gradedCount = $statistics->gradedCount;
|
||||
|
||||
return $resource;
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ use ApiPlatform\Metadata\Post;
|
||||
use ApiPlatform\Metadata\Put;
|
||||
use App\Scolarite\Domain\Model\Grade\Grade;
|
||||
use App\Scolarite\Infrastructure\Api\Processor\PublishGradesProcessor;
|
||||
use App\Scolarite\Infrastructure\Api\Processor\SaveAppreciationProcessor;
|
||||
use App\Scolarite\Infrastructure\Api\Processor\SaveGradesProcessor;
|
||||
use App\Scolarite\Infrastructure\Api\Provider\GradeCollectionProvider;
|
||||
use DateTimeImmutable;
|
||||
@@ -32,6 +33,12 @@ use DateTimeImmutable;
|
||||
processor: SaveGradesProcessor::class,
|
||||
name: 'save_evaluation_grades',
|
||||
),
|
||||
new Put(
|
||||
uriTemplate: '/grades/{id}/appreciation',
|
||||
read: false,
|
||||
processor: SaveAppreciationProcessor::class,
|
||||
name: 'save_grade_appreciation',
|
||||
),
|
||||
new Post(
|
||||
uriTemplate: '/evaluations/{evaluationId}/publish',
|
||||
uriVariables: ['evaluationId'],
|
||||
@@ -56,6 +63,8 @@ final class GradeResource
|
||||
|
||||
public ?string $status = null;
|
||||
|
||||
public ?string $appreciation = null;
|
||||
|
||||
public ?DateTimeImmutable $createdAt = null;
|
||||
|
||||
public ?DateTimeImmutable $updatedAt = null;
|
||||
@@ -76,6 +85,7 @@ final class GradeResource
|
||||
$resource->studentName = $studentName;
|
||||
$resource->value = $grade->value?->value;
|
||||
$resource->status = $grade->status->value;
|
||||
$resource->appreciation = $grade->appreciation;
|
||||
$resource->createdAt = $grade->createdAt;
|
||||
$resource->updatedAt = $grade->updatedAt;
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Api\Resource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiProperty;
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use App\Scolarite\Infrastructure\Api\Provider\StudentAveragesProvider;
|
||||
|
||||
#[ApiResource(
|
||||
shortName: 'StudentAverages',
|
||||
operations: [
|
||||
new Get(
|
||||
uriTemplate: '/students/{studentId}/averages',
|
||||
provider: StudentAveragesProvider::class,
|
||||
name: 'get_student_averages',
|
||||
),
|
||||
],
|
||||
)]
|
||||
final class StudentAveragesResource
|
||||
{
|
||||
#[ApiProperty(identifier: true)]
|
||||
public ?string $studentId = null;
|
||||
|
||||
public ?string $periodId = null;
|
||||
|
||||
/** @var list<array{subjectId: string, subjectName: string|null, average: float, gradeCount: int}> */
|
||||
public array $subjectAverages = [];
|
||||
|
||||
public ?float $generalAverage = null;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Cache;
|
||||
|
||||
use App\Scolarite\Domain\Model\Evaluation\ClassStatistics;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Domain\Repository\EvaluationStatisticsRepository;
|
||||
use Override;
|
||||
use Psr\Cache\CacheItemPoolInterface;
|
||||
|
||||
final readonly class CachingEvaluationStatisticsRepository implements EvaluationStatisticsRepository
|
||||
{
|
||||
public function __construct(
|
||||
private EvaluationStatisticsRepository $inner,
|
||||
private CacheItemPoolInterface $cache,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function save(EvaluationId $evaluationId, ClassStatistics $statistics): void
|
||||
{
|
||||
$this->inner->save($evaluationId, $statistics);
|
||||
$this->cache->deleteItem($this->cacheKey($evaluationId));
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findByEvaluation(EvaluationId $evaluationId): ?ClassStatistics
|
||||
{
|
||||
$key = $this->cacheKey($evaluationId);
|
||||
$item = $this->cache->getItem($key);
|
||||
|
||||
if ($item->isHit()) {
|
||||
/** @var ClassStatistics|null $cached */
|
||||
$cached = $item->get();
|
||||
|
||||
return $cached;
|
||||
}
|
||||
|
||||
$stats = $this->inner->findByEvaluation($evaluationId);
|
||||
|
||||
$item->set($stats);
|
||||
$this->cache->save($item);
|
||||
|
||||
return $stats;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function delete(EvaluationId $evaluationId): void
|
||||
{
|
||||
$this->inner->delete($evaluationId);
|
||||
$this->cache->deleteItem($this->cacheKey($evaluationId));
|
||||
}
|
||||
|
||||
private function cacheKey(EvaluationId $evaluationId): string
|
||||
{
|
||||
return 'eval_stats_' . $evaluationId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Cache;
|
||||
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Repository\StudentAverageRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use Override;
|
||||
use Psr\Cache\CacheItemPoolInterface;
|
||||
|
||||
final readonly class CachingStudentAverageRepository implements StudentAverageRepository
|
||||
{
|
||||
public function __construct(
|
||||
private StudentAverageRepository $inner,
|
||||
private CacheItemPoolInterface $cache,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function saveSubjectAverage(
|
||||
TenantId $tenantId,
|
||||
UserId $studentId,
|
||||
SubjectId $subjectId,
|
||||
string $periodId,
|
||||
float $average,
|
||||
int $gradeCount,
|
||||
): void {
|
||||
$this->inner->saveSubjectAverage($tenantId, $studentId, $subjectId, $periodId, $average, $gradeCount);
|
||||
|
||||
// Invalider le cache des moyennes matières de l'élève
|
||||
$this->cache->deleteItem($this->subjectAveragesKey($studentId, $periodId, $tenantId));
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function saveGeneralAverage(
|
||||
TenantId $tenantId,
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
float $average,
|
||||
): void {
|
||||
$this->inner->saveGeneralAverage($tenantId, $studentId, $periodId, $average);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findSubjectAveragesForStudent(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): array {
|
||||
$key = $this->subjectAveragesKey($studentId, $periodId, $tenantId);
|
||||
$item = $this->cache->getItem($key);
|
||||
|
||||
if ($item->isHit()) {
|
||||
/** @var list<float> $cached */
|
||||
$cached = $item->get();
|
||||
|
||||
return $cached;
|
||||
}
|
||||
|
||||
$averages = $this->inner->findSubjectAveragesForStudent($studentId, $periodId, $tenantId);
|
||||
|
||||
$item->set($averages);
|
||||
$this->cache->save($item);
|
||||
|
||||
return $averages;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findDetailedSubjectAveragesForStudent(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): array {
|
||||
return $this->inner->findDetailedSubjectAveragesForStudent($studentId, $periodId, $tenantId);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findGeneralAverageForStudent(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): ?float {
|
||||
return $this->inner->findGeneralAverageForStudent($studentId, $periodId, $tenantId);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function deleteSubjectAverage(
|
||||
UserId $studentId,
|
||||
SubjectId $subjectId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): void {
|
||||
$this->inner->deleteSubjectAverage($studentId, $subjectId, $periodId, $tenantId);
|
||||
$this->cache->deleteItem($this->subjectAveragesKey($studentId, $periodId, $tenantId));
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function deleteGeneralAverage(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): void {
|
||||
$this->inner->deleteGeneralAverage($studentId, $periodId, $tenantId);
|
||||
}
|
||||
|
||||
private function subjectAveragesKey(UserId $studentId, string $periodId, TenantId $tenantId): string
|
||||
{
|
||||
return 'avg_' . $tenantId . '_' . $studentId . '_' . $periodId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Console;
|
||||
|
||||
use App\Scolarite\Application\Service\RecalculerMoyennesService;
|
||||
use App\Scolarite\Domain\Repository\EvaluationRepository;
|
||||
use App\Shared\Infrastructure\Tenant\TenantConfig;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use App\Shared\Infrastructure\Tenant\TenantDatabaseSwitcher;
|
||||
use App\Shared\Infrastructure\Tenant\TenantId;
|
||||
use App\Shared\Infrastructure\Tenant\TenantRegistry;
|
||||
|
||||
use function count;
|
||||
|
||||
use Override;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Recalcule toutes les projections de moyennes à partir des données existantes.
|
||||
*
|
||||
* Usage:
|
||||
* php bin/console app:recalculer-moyennes # Tous les tenants
|
||||
* php bin/console app:recalculer-moyennes --tenant=UUID # Un seul tenant
|
||||
*/
|
||||
#[AsCommand(
|
||||
name: 'app:recalculer-moyennes',
|
||||
description: 'Recalcule les statistiques évaluations et moyennes élèves depuis les notes publiées',
|
||||
)]
|
||||
final class RecalculerToutesMoyennesCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EvaluationRepository $evaluationRepository,
|
||||
private readonly TenantRegistry $tenantRegistry,
|
||||
private readonly TenantContext $tenantContext,
|
||||
private readonly TenantDatabaseSwitcher $databaseSwitcher,
|
||||
private readonly RecalculerMoyennesService $service,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
#[Override]
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->addOption('tenant', 't', InputOption::VALUE_REQUIRED, 'Limiter à un tenant spécifique (UUID)');
|
||||
}
|
||||
|
||||
#[Override]
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$io->title('Recalcul des moyennes et statistiques');
|
||||
|
||||
/** @var string|null $tenantOption */
|
||||
$tenantOption = $input->getOption('tenant');
|
||||
|
||||
if ($tenantOption !== null) {
|
||||
$configs = [$this->tenantRegistry->getConfig(TenantId::fromString($tenantOption))];
|
||||
} else {
|
||||
$configs = $this->tenantRegistry->getAllConfigs();
|
||||
}
|
||||
|
||||
$totalEvals = 0;
|
||||
$totalErrors = 0;
|
||||
|
||||
foreach ($configs as $config) {
|
||||
[$processed, $errors] = $this->processTenant($config, $io);
|
||||
$totalEvals += $processed;
|
||||
$totalErrors += $errors;
|
||||
}
|
||||
|
||||
if ($totalErrors > 0) {
|
||||
$io->warning(sprintf(
|
||||
'%d évaluation(s) traitée(s), %d erreur(s).',
|
||||
$totalEvals,
|
||||
$totalErrors,
|
||||
));
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$io->success(sprintf('%d évaluation(s) traitée(s) avec succès.', $totalEvals));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{int, int} [processed, errors]
|
||||
*/
|
||||
private function processTenant(TenantConfig $config, SymfonyStyle $io): array
|
||||
{
|
||||
$this->tenantContext->setCurrentTenant($config);
|
||||
$this->databaseSwitcher->useTenantDatabase($config->databaseUrl);
|
||||
|
||||
$tenantId = \App\Shared\Domain\Tenant\TenantId::fromString((string) $config->tenantId);
|
||||
|
||||
$evaluations = $this->evaluationRepository->findAllWithPublishedGrades($tenantId);
|
||||
|
||||
if ($evaluations === []) {
|
||||
$io->text(sprintf(' Tenant %s : aucune évaluation publiée.', $config->subdomain));
|
||||
|
||||
return [0, 0];
|
||||
}
|
||||
|
||||
$io->text(sprintf(' Tenant %s : %d évaluation(s) publiée(s)', $config->subdomain, count($evaluations)));
|
||||
|
||||
$processed = 0;
|
||||
$errors = 0;
|
||||
|
||||
foreach ($evaluations as $evaluation) {
|
||||
try {
|
||||
$this->service->recalculerStatistiquesEvaluation($evaluation->id, $tenantId);
|
||||
$this->service->recalculerTousElevesPourEvaluation($evaluation->id, $tenantId);
|
||||
++$processed;
|
||||
} catch (Throwable $e) {
|
||||
$io->error(sprintf(' Erreur évaluation %s : %s', $evaluation->id, $e->getMessage()));
|
||||
++$errors;
|
||||
}
|
||||
}
|
||||
|
||||
$io->text(sprintf(' → %d traitée(s), %d erreur(s)', $processed, $errors));
|
||||
|
||||
return [$processed, $errors];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\EventHandler;
|
||||
|
||||
use App\Scolarite\Application\Service\RecalculerMoyennesService;
|
||||
use App\Scolarite\Domain\Event\EvaluationModifiee;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
#[AsMessageHandler(bus: 'event.bus')]
|
||||
final readonly class RecalculerMoyennesOnEvaluationModifieeHandler
|
||||
{
|
||||
public function __construct(
|
||||
private TenantContext $tenantContext,
|
||||
private RecalculerMoyennesService $service,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(EvaluationModifiee $event): void
|
||||
{
|
||||
$tenantId = $this->tenantContext->getCurrentTenantId();
|
||||
|
||||
$this->service->recalculerStatistiquesEvaluation($event->evaluationId, $tenantId);
|
||||
$this->service->recalculerTousElevesPourEvaluation($event->evaluationId, $tenantId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\EventHandler;
|
||||
|
||||
use App\Scolarite\Application\Port\PeriodFinder;
|
||||
use App\Scolarite\Application\Service\RecalculerMoyennesService;
|
||||
use App\Scolarite\Domain\Event\EvaluationSupprimee;
|
||||
use App\Scolarite\Domain\Repository\EvaluationRepository;
|
||||
use App\Scolarite\Domain\Repository\EvaluationStatisticsRepository;
|
||||
use App\Scolarite\Domain\Repository\GradeRepository;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
#[AsMessageHandler(bus: 'event.bus')]
|
||||
final readonly class RecalculerMoyennesOnEvaluationSupprimeeHandler
|
||||
{
|
||||
public function __construct(
|
||||
private TenantContext $tenantContext,
|
||||
private EvaluationRepository $evaluationRepository,
|
||||
private GradeRepository $gradeRepository,
|
||||
private EvaluationStatisticsRepository $evaluationStatisticsRepository,
|
||||
private PeriodFinder $periodFinder,
|
||||
private RecalculerMoyennesService $service,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(EvaluationSupprimee $event): void
|
||||
{
|
||||
$tenantId = $this->tenantContext->getCurrentTenantId();
|
||||
$evaluationId = $event->evaluationId;
|
||||
|
||||
// Charger l'évaluation (encore accessible même en DELETED)
|
||||
$evaluation = $this->evaluationRepository->findById($evaluationId, $tenantId);
|
||||
|
||||
if ($evaluation === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Supprimer les stats de l'évaluation
|
||||
$this->evaluationStatisticsRepository->delete($evaluationId);
|
||||
|
||||
// Si les notes étaient publiées, recalculer les moyennes des élèves affectés
|
||||
if ($evaluation->gradesPublishedAt === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$period = $this->periodFinder->findForDate($evaluation->evaluationDate, $tenantId);
|
||||
|
||||
if ($period === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$grades = $this->gradeRepository->findByEvaluation($evaluationId, $tenantId);
|
||||
$studentIds = [];
|
||||
|
||||
foreach ($grades as $g) {
|
||||
$studentIds[(string) $g->studentId] = $g->studentId;
|
||||
}
|
||||
|
||||
// Recalculer : l'évaluation DELETED sera exclue par findWithPublishedGrades...
|
||||
foreach ($studentIds as $studentId) {
|
||||
$this->service->recalculerMoyenneEleve(
|
||||
$studentId,
|
||||
$evaluation->subjectId,
|
||||
$evaluation->classId,
|
||||
$period,
|
||||
$tenantId,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\EventHandler;
|
||||
|
||||
use App\Scolarite\Application\Port\PeriodFinder;
|
||||
use App\Scolarite\Application\Service\RecalculerMoyennesService;
|
||||
use App\Scolarite\Domain\Event\NoteModifiee;
|
||||
use App\Scolarite\Domain\Event\NoteSaisie;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Domain\Repository\EvaluationRepository;
|
||||
use App\Scolarite\Domain\Repository\GradeRepository;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
#[AsMessageHandler(bus: 'event.bus')]
|
||||
final readonly class RecalculerMoyennesOnNoteModifieeHandler
|
||||
{
|
||||
public function __construct(
|
||||
private TenantContext $tenantContext,
|
||||
private EvaluationRepository $evaluationRepository,
|
||||
private GradeRepository $gradeRepository,
|
||||
private PeriodFinder $periodFinder,
|
||||
private RecalculerMoyennesService $service,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(NoteSaisie|NoteModifiee $event): void
|
||||
{
|
||||
$tenantId = $this->tenantContext->getCurrentTenantId();
|
||||
$evaluationId = EvaluationId::fromString($event->evaluationId);
|
||||
|
||||
$evaluation = $this->evaluationRepository->findById($evaluationId, $tenantId);
|
||||
|
||||
if ($evaluation === null || !$evaluation->notesPubliees()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->service->recalculerStatistiquesEvaluation($evaluationId, $tenantId);
|
||||
|
||||
$gradeId = $event instanceof NoteSaisie ? $event->gradeId : $event->gradeId;
|
||||
$grade = $this->gradeRepository->findById($gradeId, $tenantId);
|
||||
|
||||
if ($grade === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$period = $this->periodFinder->findForDate($evaluation->evaluationDate, $tenantId);
|
||||
|
||||
if ($period === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->service->recalculerMoyenneEleve(
|
||||
$grade->studentId,
|
||||
$evaluation->subjectId,
|
||||
$evaluation->classId,
|
||||
$period,
|
||||
$tenantId,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\EventHandler;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Port\PeriodFinder;
|
||||
use App\Scolarite\Application\Service\RecalculerMoyennesService;
|
||||
use App\Scolarite\Domain\Event\NotesPubliees;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeStatus;
|
||||
use App\Scolarite\Domain\Repository\EvaluationRepository;
|
||||
use App\Scolarite\Domain\Repository\GradeRepository;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
|
||||
use function array_filter;
|
||||
use function array_map;
|
||||
use function array_unique;
|
||||
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
|
||||
#[AsMessageHandler(bus: 'event.bus')]
|
||||
final readonly class RecalculerMoyennesOnNotesPublieesHandler
|
||||
{
|
||||
public function __construct(
|
||||
private TenantContext $tenantContext,
|
||||
private EvaluationRepository $evaluationRepository,
|
||||
private GradeRepository $gradeRepository,
|
||||
private PeriodFinder $periodFinder,
|
||||
private RecalculerMoyennesService $service,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(NotesPubliees $event): void
|
||||
{
|
||||
$tenantId = $this->tenantContext->getCurrentTenantId();
|
||||
$evaluationId = $event->evaluationId;
|
||||
|
||||
$evaluation = $this->evaluationRepository->findById($evaluationId, $tenantId);
|
||||
|
||||
if ($evaluation === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->service->recalculerStatistiquesEvaluation($evaluationId, $tenantId);
|
||||
|
||||
$period = $this->periodFinder->findForDate($evaluation->evaluationDate, $tenantId);
|
||||
|
||||
if ($period === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$grades = $this->gradeRepository->findByEvaluation($evaluationId, $tenantId);
|
||||
|
||||
$studentIds = array_unique(array_map(
|
||||
static fn ($g) => (string) $g->studentId,
|
||||
array_filter($grades, static fn ($g) => $g->status === GradeStatus::GRADED),
|
||||
));
|
||||
|
||||
foreach ($studentIds as $studentIdStr) {
|
||||
$this->service->recalculerMoyenneEleve(
|
||||
UserId::fromString($studentIdStr),
|
||||
$evaluation->subjectId,
|
||||
$evaluation->classId,
|
||||
$period,
|
||||
$tenantId,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Persistence\Doctrine;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationCategory;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplate;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplateId;
|
||||
use App\Scolarite\Domain\Repository\AppreciationTemplateRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
|
||||
use function array_map;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Override;
|
||||
|
||||
final readonly class DoctrineAppreciationTemplateRepository implements AppreciationTemplateRepository
|
||||
{
|
||||
public function __construct(
|
||||
private Connection $connection,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function save(AppreciationTemplate $template): void
|
||||
{
|
||||
$this->connection->executeStatement(
|
||||
'INSERT INTO appreciation_templates (id, tenant_id, teacher_id, title, content, category, usage_count, created_at, updated_at)
|
||||
VALUES (:id, :tenant_id, :teacher_id, :title, :content, :category, :usage_count, :created_at, :updated_at)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
title = EXCLUDED.title,
|
||||
content = EXCLUDED.content,
|
||||
category = EXCLUDED.category,
|
||||
usage_count = EXCLUDED.usage_count,
|
||||
updated_at = EXCLUDED.updated_at',
|
||||
[
|
||||
'id' => (string) $template->id,
|
||||
'tenant_id' => (string) $template->tenantId,
|
||||
'teacher_id' => (string) $template->teacherId,
|
||||
'title' => $template->title,
|
||||
'content' => $template->content,
|
||||
'category' => $template->category?->value,
|
||||
'usage_count' => $template->usageCount,
|
||||
'created_at' => $template->createdAt->format(DateTimeImmutable::ATOM),
|
||||
'updated_at' => $template->updatedAt->format(DateTimeImmutable::ATOM),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findById(AppreciationTemplateId $id, TenantId $tenantId): ?AppreciationTemplate
|
||||
{
|
||||
$row = $this->connection->fetchAssociative(
|
||||
'SELECT * FROM appreciation_templates WHERE id = :id AND tenant_id = :tenant_id',
|
||||
['id' => (string) $id, 'tenant_id' => (string) $tenantId],
|
||||
);
|
||||
|
||||
if ($row === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->hydrate($row);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findByTeacher(UserId $teacherId, TenantId $tenantId): array
|
||||
{
|
||||
$rows = $this->connection->fetchAllAssociative(
|
||||
'SELECT * FROM appreciation_templates
|
||||
WHERE teacher_id = :teacher_id AND tenant_id = :tenant_id
|
||||
ORDER BY usage_count DESC, title ASC',
|
||||
[
|
||||
'teacher_id' => (string) $teacherId,
|
||||
'tenant_id' => (string) $tenantId,
|
||||
],
|
||||
);
|
||||
|
||||
return array_map($this->hydrate(...), $rows);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function delete(AppreciationTemplateId $id, TenantId $tenantId): void
|
||||
{
|
||||
$this->connection->executeStatement(
|
||||
'DELETE FROM appreciation_templates WHERE id = :id AND tenant_id = :tenant_id',
|
||||
['id' => (string) $id, 'tenant_id' => (string) $tenantId],
|
||||
);
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $row */
|
||||
private function hydrate(array $row): AppreciationTemplate
|
||||
{
|
||||
/** @var string $id */
|
||||
$id = $row['id'];
|
||||
/** @var string $tenantId */
|
||||
$tenantId = $row['tenant_id'];
|
||||
/** @var string $teacherId */
|
||||
$teacherId = $row['teacher_id'];
|
||||
/** @var string $title */
|
||||
$title = $row['title'];
|
||||
/** @var string $content */
|
||||
$content = $row['content'];
|
||||
/** @var string|null $category */
|
||||
$category = $row['category'] ?? null;
|
||||
/** @var int|string $usageCount */
|
||||
$usageCount = $row['usage_count'];
|
||||
/** @var string $createdAt */
|
||||
$createdAt = $row['created_at'];
|
||||
/** @var string $updatedAt */
|
||||
$updatedAt = $row['updated_at'];
|
||||
|
||||
return AppreciationTemplate::reconstitute(
|
||||
id: AppreciationTemplateId::fromString($id),
|
||||
tenantId: TenantId::fromString($tenantId),
|
||||
teacherId: UserId::fromString($teacherId),
|
||||
title: $title,
|
||||
content: $content,
|
||||
category: $category !== null ? AppreciationCategory::from($category) : null,
|
||||
usageCount: (int) $usageCount,
|
||||
createdAt: new DateTimeImmutable($createdAt),
|
||||
updatedAt: new DateTimeImmutable($updatedAt),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -158,6 +158,54 @@ final readonly class DoctrineEvaluationRepository implements EvaluationRepositor
|
||||
return array_map($this->hydrate(...), $rows);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findAllWithPublishedGrades(TenantId $tenantId): array
|
||||
{
|
||||
$rows = $this->connection->fetchAllAssociative(
|
||||
'SELECT * FROM evaluations
|
||||
WHERE tenant_id = :tenant_id
|
||||
AND status != :deleted
|
||||
AND grades_published_at IS NOT NULL
|
||||
ORDER BY evaluation_date ASC',
|
||||
[
|
||||
'tenant_id' => (string) $tenantId,
|
||||
'deleted' => EvaluationStatus::DELETED->value,
|
||||
],
|
||||
);
|
||||
|
||||
return array_map($this->hydrate(...), $rows);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findWithPublishedGradesBySubjectAndClassInDateRange(
|
||||
SubjectId $subjectId,
|
||||
ClassId $classId,
|
||||
DateTimeImmutable $startDate,
|
||||
DateTimeImmutable $endDate,
|
||||
TenantId $tenantId,
|
||||
): array {
|
||||
$rows = $this->connection->fetchAllAssociative(
|
||||
'SELECT * FROM evaluations
|
||||
WHERE subject_id = :subject_id
|
||||
AND class_id = :class_id
|
||||
AND tenant_id = :tenant_id
|
||||
AND status != :deleted
|
||||
AND grades_published_at IS NOT NULL
|
||||
AND evaluation_date BETWEEN :start_date AND :end_date
|
||||
ORDER BY evaluation_date ASC',
|
||||
[
|
||||
'subject_id' => (string) $subjectId,
|
||||
'class_id' => (string) $classId,
|
||||
'tenant_id' => (string) $tenantId,
|
||||
'deleted' => EvaluationStatus::DELETED->value,
|
||||
'start_date' => $startDate->format('Y-m-d'),
|
||||
'end_date' => $endDate->format('Y-m-d'),
|
||||
],
|
||||
);
|
||||
|
||||
return array_map($this->hydrate(...), $rows);
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $row */
|
||||
private function hydrate(array $row): Evaluation
|
||||
{
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Persistence\Doctrine;
|
||||
|
||||
use App\Scolarite\Domain\Model\Evaluation\ClassStatistics;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Domain\Repository\EvaluationStatisticsRepository;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Override;
|
||||
|
||||
final readonly class DoctrineEvaluationStatisticsRepository implements EvaluationStatisticsRepository
|
||||
{
|
||||
public function __construct(
|
||||
private Connection $connection,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function save(EvaluationId $evaluationId, ClassStatistics $statistics): void
|
||||
{
|
||||
$this->connection->executeStatement(
|
||||
'INSERT INTO evaluation_statistics (evaluation_id, average, min_grade, max_grade, median_grade, graded_count, updated_at)
|
||||
VALUES (:evaluation_id, :average, :min_grade, :max_grade, :median_grade, :graded_count, NOW())
|
||||
ON CONFLICT (evaluation_id) DO UPDATE SET
|
||||
average = EXCLUDED.average,
|
||||
min_grade = EXCLUDED.min_grade,
|
||||
max_grade = EXCLUDED.max_grade,
|
||||
median_grade = EXCLUDED.median_grade,
|
||||
graded_count = EXCLUDED.graded_count,
|
||||
updated_at = NOW()',
|
||||
[
|
||||
'evaluation_id' => (string) $evaluationId,
|
||||
'average' => $statistics->average,
|
||||
'min_grade' => $statistics->min,
|
||||
'max_grade' => $statistics->max,
|
||||
'median_grade' => $statistics->median,
|
||||
'graded_count' => $statistics->gradedCount,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findByEvaluation(EvaluationId $evaluationId): ?ClassStatistics
|
||||
{
|
||||
$row = $this->connection->fetchAssociative(
|
||||
'SELECT * FROM evaluation_statistics WHERE evaluation_id = :evaluation_id',
|
||||
['evaluation_id' => (string) $evaluationId],
|
||||
);
|
||||
|
||||
if ($row === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var string|float|null $average */
|
||||
$average = $row['average'];
|
||||
/** @var string|float|null $minGrade */
|
||||
$minGrade = $row['min_grade'];
|
||||
/** @var string|float|null $maxGrade */
|
||||
$maxGrade = $row['max_grade'];
|
||||
/** @var string|float|null $medianGrade */
|
||||
$medianGrade = $row['median_grade'];
|
||||
/** @var string|int $gradedCount */
|
||||
$gradedCount = $row['graded_count'];
|
||||
|
||||
return new ClassStatistics(
|
||||
average: $average !== null ? (float) $average : null,
|
||||
min: $minGrade !== null ? (float) $minGrade : null,
|
||||
max: $maxGrade !== null ? (float) $maxGrade : null,
|
||||
median: $medianGrade !== null ? (float) $medianGrade : null,
|
||||
gradedCount: (int) $gradedCount,
|
||||
);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function delete(EvaluationId $evaluationId): void
|
||||
{
|
||||
$this->connection->executeStatement(
|
||||
'DELETE FROM evaluation_statistics WHERE evaluation_id = :evaluation_id',
|
||||
['evaluation_id' => (string) $evaluationId],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,9 @@ use function array_map;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\DBAL\Connection;
|
||||
|
||||
use function implode;
|
||||
|
||||
use Override;
|
||||
|
||||
final readonly class DoctrineGradeRepository implements GradeRepository
|
||||
@@ -31,8 +34,8 @@ final readonly class DoctrineGradeRepository implements GradeRepository
|
||||
public function save(Grade $grade): void
|
||||
{
|
||||
$this->connection->executeStatement(
|
||||
'INSERT INTO grades (id, tenant_id, evaluation_id, student_id, value, status, created_by, created_at, updated_at)
|
||||
VALUES (:id, :tenant_id, :evaluation_id, :student_id, :value, :status, :created_by, :created_at, :updated_at)
|
||||
'INSERT INTO grades (id, tenant_id, evaluation_id, student_id, value, status, created_by, created_at, updated_at, appreciation, appreciation_updated_at)
|
||||
VALUES (:id, :tenant_id, :evaluation_id, :student_id, :value, :status, :created_by, :created_at, :updated_at, :appreciation, :appreciation_updated_at)
|
||||
ON CONFLICT (evaluation_id, student_id) DO UPDATE SET
|
||||
value = EXCLUDED.value,
|
||||
status = EXCLUDED.status,
|
||||
@@ -47,6 +50,24 @@ final readonly class DoctrineGradeRepository implements GradeRepository
|
||||
'created_by' => (string) $grade->createdBy,
|
||||
'created_at' => $grade->createdAt->format(DateTimeImmutable::ATOM),
|
||||
'updated_at' => $grade->updatedAt->format(DateTimeImmutable::ATOM),
|
||||
'appreciation' => $grade->appreciation,
|
||||
'appreciation_updated_at' => $grade->appreciationUpdatedAt?->format(DateTimeImmutable::ATOM),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function saveAppreciation(Grade $grade): void
|
||||
{
|
||||
$this->connection->executeStatement(
|
||||
'UPDATE grades SET appreciation = :appreciation, appreciation_updated_at = :appreciation_updated_at, updated_at = :updated_at
|
||||
WHERE id = :id AND tenant_id = :tenant_id',
|
||||
[
|
||||
'id' => (string) $grade->id,
|
||||
'tenant_id' => (string) $grade->tenantId,
|
||||
'appreciation' => $grade->appreciation,
|
||||
'appreciation_updated_at' => $grade->appreciationUpdatedAt?->format(DateTimeImmutable::ATOM),
|
||||
'updated_at' => $grade->updatedAt->format(DateTimeImmutable::ATOM),
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -95,6 +116,43 @@ final readonly class DoctrineGradeRepository implements GradeRepository
|
||||
return array_map($this->hydrate(...), $rows);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findByEvaluations(array $evaluationIds, TenantId $tenantId): array
|
||||
{
|
||||
if ($evaluationIds === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$placeholders = [];
|
||||
$params = ['tenant_id' => (string) $tenantId];
|
||||
|
||||
foreach ($evaluationIds as $i => $evalId) {
|
||||
$key = 'eval_' . $i;
|
||||
$placeholders[] = ':' . $key;
|
||||
$params[$key] = (string) $evalId;
|
||||
}
|
||||
|
||||
$in = implode(', ', $placeholders);
|
||||
|
||||
$rows = $this->connection->fetchAllAssociative(
|
||||
"SELECT * FROM grades
|
||||
WHERE evaluation_id IN ({$in})
|
||||
AND tenant_id = :tenant_id
|
||||
ORDER BY created_at ASC",
|
||||
$params,
|
||||
);
|
||||
|
||||
$grouped = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
/** @var string $evalId */
|
||||
$evalId = $row['evaluation_id'];
|
||||
$grouped[$evalId][] = $this->hydrate($row);
|
||||
}
|
||||
|
||||
return $grouped;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function hasGradesForEvaluation(EvaluationId $evaluationId, TenantId $tenantId): bool
|
||||
{
|
||||
@@ -133,6 +191,10 @@ final readonly class DoctrineGradeRepository implements GradeRepository
|
||||
$createdAt = $row['created_at'];
|
||||
/** @var string $updatedAt */
|
||||
$updatedAt = $row['updated_at'];
|
||||
/** @var string|null $appreciation */
|
||||
$appreciation = $row['appreciation'] ?? null;
|
||||
/** @var string|null $appreciationUpdatedAt */
|
||||
$appreciationUpdatedAt = $row['appreciation_updated_at'] ?? null;
|
||||
|
||||
return Grade::reconstitute(
|
||||
id: GradeId::fromString($id),
|
||||
@@ -144,6 +206,8 @@ final readonly class DoctrineGradeRepository implements GradeRepository
|
||||
createdBy: UserId::fromString($createdBy),
|
||||
createdAt: new DateTimeImmutable($createdAt),
|
||||
updatedAt: new DateTimeImmutable($updatedAt),
|
||||
appreciation: $appreciation,
|
||||
appreciationUpdatedAt: $appreciationUpdatedAt !== null ? new DateTimeImmutable($appreciationUpdatedAt) : null,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Persistence\Doctrine;
|
||||
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Repository\StudentAverageRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Override;
|
||||
|
||||
final readonly class DoctrineStudentAverageRepository implements StudentAverageRepository
|
||||
{
|
||||
public function __construct(
|
||||
private Connection $connection,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function saveSubjectAverage(
|
||||
TenantId $tenantId,
|
||||
UserId $studentId,
|
||||
SubjectId $subjectId,
|
||||
string $periodId,
|
||||
float $average,
|
||||
int $gradeCount,
|
||||
): void {
|
||||
$this->connection->executeStatement(
|
||||
'INSERT INTO student_averages (id, tenant_id, student_id, subject_id, period_id, average, grade_count, updated_at)
|
||||
VALUES (gen_random_uuid(), :tenant_id, :student_id, :subject_id, :period_id, :average, :grade_count, NOW())
|
||||
ON CONFLICT (student_id, subject_id, period_id) DO UPDATE SET
|
||||
average = EXCLUDED.average,
|
||||
grade_count = EXCLUDED.grade_count,
|
||||
updated_at = NOW()',
|
||||
[
|
||||
'tenant_id' => (string) $tenantId,
|
||||
'student_id' => (string) $studentId,
|
||||
'subject_id' => (string) $subjectId,
|
||||
'period_id' => $periodId,
|
||||
'average' => $average,
|
||||
'grade_count' => $gradeCount,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function saveGeneralAverage(
|
||||
TenantId $tenantId,
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
float $average,
|
||||
): void {
|
||||
$this->connection->executeStatement(
|
||||
'INSERT INTO student_general_averages (id, tenant_id, student_id, period_id, average, updated_at)
|
||||
VALUES (gen_random_uuid(), :tenant_id, :student_id, :period_id, :average, NOW())
|
||||
ON CONFLICT (student_id, period_id) DO UPDATE SET
|
||||
average = EXCLUDED.average,
|
||||
updated_at = NOW()',
|
||||
[
|
||||
'tenant_id' => (string) $tenantId,
|
||||
'student_id' => (string) $studentId,
|
||||
'period_id' => $periodId,
|
||||
'average' => $average,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findSubjectAveragesForStudent(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): array {
|
||||
$rows = $this->connection->fetchAllAssociative(
|
||||
'SELECT average FROM student_averages
|
||||
WHERE student_id = :student_id
|
||||
AND period_id = :period_id
|
||||
AND tenant_id = :tenant_id',
|
||||
[
|
||||
'student_id' => (string) $studentId,
|
||||
'period_id' => $periodId,
|
||||
'tenant_id' => (string) $tenantId,
|
||||
],
|
||||
);
|
||||
|
||||
return array_map(
|
||||
/** @param array<string, mixed> $row */
|
||||
static function (array $row): float {
|
||||
/** @var string|float $avg */
|
||||
$avg = $row['average'];
|
||||
|
||||
return (float) $avg;
|
||||
},
|
||||
$rows,
|
||||
);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findDetailedSubjectAveragesForStudent(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): array {
|
||||
$rows = $this->connection->fetchAllAssociative(
|
||||
'SELECT sa.subject_id, sa.average, sa.grade_count, s.name as subject_name
|
||||
FROM student_averages sa
|
||||
LEFT JOIN subjects s ON s.id = sa.subject_id
|
||||
WHERE sa.student_id = :student_id
|
||||
AND sa.period_id = :period_id
|
||||
AND sa.tenant_id = :tenant_id
|
||||
ORDER BY s.name ASC',
|
||||
[
|
||||
'student_id' => (string) $studentId,
|
||||
'period_id' => $periodId,
|
||||
'tenant_id' => (string) $tenantId,
|
||||
],
|
||||
);
|
||||
|
||||
return array_map(
|
||||
/** @param array<string, mixed> $row */
|
||||
static function (array $row): array {
|
||||
/** @var string $subjectId */
|
||||
$subjectId = $row['subject_id'];
|
||||
/** @var string|null $subjectName */
|
||||
$subjectName = $row['subject_name'];
|
||||
/** @var string|float $average */
|
||||
$average = $row['average'];
|
||||
/** @var string|int $gradeCount */
|
||||
$gradeCount = $row['grade_count'];
|
||||
|
||||
return [
|
||||
'subjectId' => $subjectId,
|
||||
'subjectName' => $subjectName,
|
||||
'average' => (float) $average,
|
||||
'gradeCount' => (int) $gradeCount,
|
||||
];
|
||||
},
|
||||
$rows,
|
||||
);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findGeneralAverageForStudent(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): ?float {
|
||||
$row = $this->connection->fetchAssociative(
|
||||
'SELECT average FROM student_general_averages
|
||||
WHERE student_id = :student_id
|
||||
AND period_id = :period_id
|
||||
AND tenant_id = :tenant_id',
|
||||
[
|
||||
'student_id' => (string) $studentId,
|
||||
'period_id' => $periodId,
|
||||
'tenant_id' => (string) $tenantId,
|
||||
],
|
||||
);
|
||||
|
||||
if ($row === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var string|float $average */
|
||||
$average = $row['average'];
|
||||
|
||||
return (float) $average;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function deleteSubjectAverage(
|
||||
UserId $studentId,
|
||||
SubjectId $subjectId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): void {
|
||||
$this->connection->executeStatement(
|
||||
'DELETE FROM student_averages
|
||||
WHERE student_id = :student_id
|
||||
AND subject_id = :subject_id
|
||||
AND period_id = :period_id
|
||||
AND tenant_id = :tenant_id',
|
||||
[
|
||||
'student_id' => (string) $studentId,
|
||||
'subject_id' => (string) $subjectId,
|
||||
'period_id' => $periodId,
|
||||
'tenant_id' => (string) $tenantId,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function deleteGeneralAverage(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): void {
|
||||
$this->connection->executeStatement(
|
||||
'DELETE FROM student_general_averages
|
||||
WHERE student_id = :student_id
|
||||
AND period_id = :period_id
|
||||
AND tenant_id = :tenant_id',
|
||||
[
|
||||
'student_id' => (string) $studentId,
|
||||
'period_id' => $periodId,
|
||||
'tenant_id' => (string) $tenantId,
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Persistence\InMemory;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplate;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplateId;
|
||||
use App\Scolarite\Domain\Repository\AppreciationTemplateRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
|
||||
use function array_filter;
|
||||
use function array_values;
|
||||
|
||||
use Override;
|
||||
|
||||
final class InMemoryAppreciationTemplateRepository implements AppreciationTemplateRepository
|
||||
{
|
||||
/** @var array<string, AppreciationTemplate> */
|
||||
private array $byId = [];
|
||||
|
||||
#[Override]
|
||||
public function save(AppreciationTemplate $template): void
|
||||
{
|
||||
$this->byId[(string) $template->id] = $template;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findById(AppreciationTemplateId $id, TenantId $tenantId): ?AppreciationTemplate
|
||||
{
|
||||
$template = $this->byId[(string) $id] ?? null;
|
||||
|
||||
if ($template === null || (string) $template->tenantId !== (string) $tenantId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $template;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findByTeacher(UserId $teacherId, TenantId $tenantId): array
|
||||
{
|
||||
return array_values(array_filter(
|
||||
$this->byId,
|
||||
static fn (AppreciationTemplate $t): bool => (string) $t->teacherId === (string) $teacherId
|
||||
&& (string) $t->tenantId === (string) $tenantId,
|
||||
));
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function delete(AppreciationTemplateId $id, TenantId $tenantId): void
|
||||
{
|
||||
$template = $this->byId[(string) $id] ?? null;
|
||||
|
||||
if ($template !== null && (string) $template->tenantId === (string) $tenantId) {
|
||||
unset($this->byId[(string) $id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Scolarite\Infrastructure\Persistence\InMemory;
|
||||
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassId;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Exception\EvaluationNotFoundException;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Evaluation;
|
||||
@@ -16,6 +17,7 @@ use App\Shared\Domain\Tenant\TenantId;
|
||||
use function array_filter;
|
||||
use function array_values;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Override;
|
||||
|
||||
final class InMemoryEvaluationRepository implements EvaluationRepository
|
||||
@@ -86,4 +88,35 @@ final class InMemoryEvaluationRepository implements EvaluationRepository
|
||||
&& $e->status !== EvaluationStatus::DELETED,
|
||||
));
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findAllWithPublishedGrades(TenantId $tenantId): array
|
||||
{
|
||||
return array_values(array_filter(
|
||||
$this->byId,
|
||||
static fn (Evaluation $e): bool => $e->tenantId->equals($tenantId)
|
||||
&& $e->status !== EvaluationStatus::DELETED
|
||||
&& $e->notesPubliees(),
|
||||
));
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findWithPublishedGradesBySubjectAndClassInDateRange(
|
||||
SubjectId $subjectId,
|
||||
ClassId $classId,
|
||||
DateTimeImmutable $startDate,
|
||||
DateTimeImmutable $endDate,
|
||||
TenantId $tenantId,
|
||||
): array {
|
||||
return array_values(array_filter(
|
||||
$this->byId,
|
||||
static fn (Evaluation $e): bool => $e->subjectId->equals($subjectId)
|
||||
&& $e->classId->equals($classId)
|
||||
&& $e->tenantId->equals($tenantId)
|
||||
&& $e->status !== EvaluationStatus::DELETED
|
||||
&& $e->notesPubliees()
|
||||
&& $e->evaluationDate >= $startDate
|
||||
&& $e->evaluationDate <= $endDate,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Persistence\InMemory;
|
||||
|
||||
use App\Scolarite\Domain\Model\Evaluation\ClassStatistics;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Domain\Repository\EvaluationStatisticsRepository;
|
||||
use Override;
|
||||
|
||||
final class InMemoryEvaluationStatisticsRepository implements EvaluationStatisticsRepository
|
||||
{
|
||||
/** @var array<string, ClassStatistics> */
|
||||
private array $byEvaluationId = [];
|
||||
|
||||
#[Override]
|
||||
public function save(EvaluationId $evaluationId, ClassStatistics $statistics): void
|
||||
{
|
||||
$this->byEvaluationId[(string) $evaluationId] = $statistics;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findByEvaluation(EvaluationId $evaluationId): ?ClassStatistics
|
||||
{
|
||||
return $this->byEvaluationId[(string) $evaluationId] ?? null;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function delete(EvaluationId $evaluationId): void
|
||||
{
|
||||
unset($this->byEvaluationId[(string) $evaluationId]);
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,9 @@ use App\Scolarite\Domain\Repository\GradeRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
|
||||
use function array_filter;
|
||||
use function array_map;
|
||||
use function array_values;
|
||||
use function in_array;
|
||||
|
||||
use Override;
|
||||
|
||||
@@ -27,6 +29,12 @@ final class InMemoryGradeRepository implements GradeRepository
|
||||
$this->byId[(string) $grade->id] = $grade;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function saveAppreciation(Grade $grade): void
|
||||
{
|
||||
$this->byId[(string) $grade->id] = $grade;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function get(GradeId $id, TenantId $tenantId): Grade
|
||||
{
|
||||
@@ -61,6 +69,24 @@ final class InMemoryGradeRepository implements GradeRepository
|
||||
));
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findByEvaluations(array $evaluationIds, TenantId $tenantId): array
|
||||
{
|
||||
$idStrings = array_map(static fn (EvaluationId $id): string => (string) $id, $evaluationIds);
|
||||
|
||||
$grouped = [];
|
||||
|
||||
foreach ($this->byId as $grade) {
|
||||
$evalIdStr = (string) $grade->evaluationId;
|
||||
|
||||
if ($grade->tenantId->equals($tenantId) && in_array($evalIdStr, $idStrings, true)) {
|
||||
$grouped[$evalIdStr][] = $grade;
|
||||
}
|
||||
}
|
||||
|
||||
return $grouped;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function hasGradesForEvaluation(EvaluationId $evaluationId, TenantId $tenantId): bool
|
||||
{
|
||||
|
||||
@@ -0,0 +1,232 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Persistence\InMemory;
|
||||
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Repository\StudentAverageRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
|
||||
use function array_filter;
|
||||
use function array_map;
|
||||
use function array_values;
|
||||
|
||||
use Override;
|
||||
|
||||
final class InMemoryStudentAverageRepository implements StudentAverageRepository
|
||||
{
|
||||
/** @var list<array{tenantId: string, studentId: string, subjectId: string, periodId: string, average: float, gradeCount: int}> */
|
||||
private array $subjectAverages = [];
|
||||
|
||||
/** @var list<array{tenantId: string, studentId: string, periodId: string, average: float}> */
|
||||
private array $generalAverages = [];
|
||||
|
||||
#[Override]
|
||||
public function saveSubjectAverage(
|
||||
TenantId $tenantId,
|
||||
UserId $studentId,
|
||||
SubjectId $subjectId,
|
||||
string $periodId,
|
||||
float $average,
|
||||
int $gradeCount,
|
||||
): void {
|
||||
$tenantStr = (string) $tenantId;
|
||||
$studentStr = (string) $studentId;
|
||||
$subjectStr = (string) $subjectId;
|
||||
|
||||
foreach ($this->subjectAverages as $i => $entry) {
|
||||
if ($entry['tenantId'] === $tenantStr
|
||||
&& $entry['studentId'] === $studentStr
|
||||
&& $entry['subjectId'] === $subjectStr
|
||||
&& $entry['periodId'] === $periodId
|
||||
) {
|
||||
$this->subjectAverages[$i] = [
|
||||
'tenantId' => $tenantStr,
|
||||
'studentId' => $studentStr,
|
||||
'subjectId' => $subjectStr,
|
||||
'periodId' => $periodId,
|
||||
'average' => $average,
|
||||
'gradeCount' => $gradeCount,
|
||||
];
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$this->subjectAverages[] = [
|
||||
'tenantId' => $tenantStr,
|
||||
'studentId' => $studentStr,
|
||||
'subjectId' => $subjectStr,
|
||||
'periodId' => $periodId,
|
||||
'average' => $average,
|
||||
'gradeCount' => $gradeCount,
|
||||
];
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function saveGeneralAverage(
|
||||
TenantId $tenantId,
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
float $average,
|
||||
): void {
|
||||
$tenantStr = (string) $tenantId;
|
||||
$studentStr = (string) $studentId;
|
||||
|
||||
foreach ($this->generalAverages as $i => $entry) {
|
||||
if ($entry['tenantId'] === $tenantStr
|
||||
&& $entry['studentId'] === $studentStr
|
||||
&& $entry['periodId'] === $periodId
|
||||
) {
|
||||
$this->generalAverages[$i] = [
|
||||
'tenantId' => $tenantStr,
|
||||
'studentId' => $studentStr,
|
||||
'periodId' => $periodId,
|
||||
'average' => $average,
|
||||
];
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$this->generalAverages[] = [
|
||||
'tenantId' => $tenantStr,
|
||||
'studentId' => $studentStr,
|
||||
'periodId' => $periodId,
|
||||
'average' => $average,
|
||||
];
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findSubjectAveragesForStudent(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): array {
|
||||
$studentStr = (string) $studentId;
|
||||
$tenantStr = (string) $tenantId;
|
||||
|
||||
$matching = array_filter(
|
||||
$this->subjectAverages,
|
||||
static fn (array $e): bool => $e['studentId'] === $studentStr
|
||||
&& $e['periodId'] === $periodId
|
||||
&& $e['tenantId'] === $tenantStr,
|
||||
);
|
||||
|
||||
return array_values(array_map(
|
||||
static fn (array $e): float => $e['average'],
|
||||
$matching,
|
||||
));
|
||||
}
|
||||
|
||||
/** @return array{average: float, gradeCount: int}|null */
|
||||
public function findSubjectAverage(
|
||||
UserId $studentId,
|
||||
SubjectId $subjectId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): ?array {
|
||||
$studentStr = (string) $studentId;
|
||||
$subjectStr = (string) $subjectId;
|
||||
$tenantStr = (string) $tenantId;
|
||||
|
||||
foreach ($this->subjectAverages as $entry) {
|
||||
if ($entry['studentId'] === $studentStr
|
||||
&& $entry['subjectId'] === $subjectStr
|
||||
&& $entry['periodId'] === $periodId
|
||||
&& $entry['tenantId'] === $tenantStr
|
||||
) {
|
||||
return ['average' => $entry['average'], 'gradeCount' => $entry['gradeCount']];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findDetailedSubjectAveragesForStudent(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): array {
|
||||
$studentStr = (string) $studentId;
|
||||
$tenantStr = (string) $tenantId;
|
||||
|
||||
$matching = array_filter(
|
||||
$this->subjectAverages,
|
||||
static fn (array $e): bool => $e['studentId'] === $studentStr
|
||||
&& $e['periodId'] === $periodId
|
||||
&& $e['tenantId'] === $tenantStr,
|
||||
);
|
||||
|
||||
return array_values(array_map(
|
||||
static fn (array $e): array => [
|
||||
'subjectId' => $e['subjectId'],
|
||||
'subjectName' => null,
|
||||
'average' => $e['average'],
|
||||
'gradeCount' => $e['gradeCount'],
|
||||
],
|
||||
$matching,
|
||||
));
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findGeneralAverageForStudent(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): ?float {
|
||||
$studentStr = (string) $studentId;
|
||||
$tenantStr = (string) $tenantId;
|
||||
|
||||
foreach ($this->generalAverages as $entry) {
|
||||
if ($entry['studentId'] === $studentStr
|
||||
&& $entry['periodId'] === $periodId
|
||||
&& $entry['tenantId'] === $tenantStr
|
||||
) {
|
||||
return $entry['average'];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function deleteSubjectAverage(
|
||||
UserId $studentId,
|
||||
SubjectId $subjectId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): void {
|
||||
$studentStr = (string) $studentId;
|
||||
$subjectStr = (string) $subjectId;
|
||||
$tenantStr = (string) $tenantId;
|
||||
|
||||
$this->subjectAverages = array_values(array_filter(
|
||||
$this->subjectAverages,
|
||||
static fn (array $e): bool => !($e['studentId'] === $studentStr
|
||||
&& $e['subjectId'] === $subjectStr
|
||||
&& $e['periodId'] === $periodId
|
||||
&& $e['tenantId'] === $tenantStr),
|
||||
));
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function deleteGeneralAverage(
|
||||
UserId $studentId,
|
||||
string $periodId,
|
||||
TenantId $tenantId,
|
||||
): void {
|
||||
$studentStr = (string) $studentId;
|
||||
$tenantStr = (string) $tenantId;
|
||||
|
||||
$this->generalAverages = array_values(array_filter(
|
||||
$this->generalAverages,
|
||||
static fn (array $e): bool => !($e['studentId'] === $studentStr
|
||||
&& $e['periodId'] === $periodId
|
||||
&& $e['tenantId'] === $tenantStr),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Infrastructure\Service;
|
||||
|
||||
use App\Scolarite\Application\Port\PeriodFinder;
|
||||
use App\Scolarite\Application\Port\PeriodInfo;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Override;
|
||||
|
||||
final readonly class DoctrinePeriodFinder implements PeriodFinder
|
||||
{
|
||||
public function __construct(
|
||||
private Connection $connection,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findForDate(DateTimeImmutable $date, TenantId $tenantId): ?PeriodInfo
|
||||
{
|
||||
$row = $this->connection->fetchAssociative(
|
||||
'SELECT id, start_date, end_date
|
||||
FROM academic_periods
|
||||
WHERE tenant_id = :tenant_id
|
||||
AND start_date <= :date
|
||||
AND end_date >= :date
|
||||
ORDER BY start_date DESC
|
||||
LIMIT 1',
|
||||
[
|
||||
'tenant_id' => (string) $tenantId,
|
||||
'date' => $date->format('Y-m-d'),
|
||||
],
|
||||
);
|
||||
|
||||
if ($row === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var string $id */
|
||||
$id = $row['id'];
|
||||
/** @var string $startDate */
|
||||
$startDate = $row['start_date'];
|
||||
/** @var string $endDate */
|
||||
$endDate = $row['end_date'];
|
||||
|
||||
return new PeriodInfo(
|
||||
periodId: $id,
|
||||
startDate: new DateTimeImmutable($startDate),
|
||||
endDate: new DateTimeImmutable($endDate),
|
||||
);
|
||||
}
|
||||
}
|
||||
394
backend/tests/Functional/Scolarite/Api/MoyennesEndpointsTest.php
Normal file
394
backend/tests/Functional/Scolarite/Api/MoyennesEndpointsTest.php
Normal file
@@ -0,0 +1,394 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Functional\Scolarite\Api;
|
||||
|
||||
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassId;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Infrastructure\Security\SecurityUser;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Coefficient;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Evaluation;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
|
||||
use App\Scolarite\Domain\Model\Grade\Grade;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeStatus;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeValue;
|
||||
use App\Scolarite\Domain\Repository\EvaluationRepository;
|
||||
use App\Scolarite\Domain\Repository\EvaluationStatisticsRepository;
|
||||
use App\Scolarite\Domain\Repository\GradeRepository;
|
||||
use App\Scolarite\Domain\Repository\StudentAverageRepository;
|
||||
use App\Scolarite\Domain\Service\AverageCalculator;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
|
||||
final class MoyennesEndpointsTest extends ApiTestCase
|
||||
{
|
||||
protected static ?bool $alwaysBootKernel = true;
|
||||
|
||||
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
|
||||
private const string TEACHER_ID = '44444444-4444-4444-4444-444444444444';
|
||||
private const string OTHER_TEACHER_ID = '44444444-4444-4444-4444-444444444445';
|
||||
private const string STUDENT_ID = '22222222-2222-2222-2222-222222222222';
|
||||
private const string CLASS_ID = '55555555-5555-5555-5555-555555555555';
|
||||
private const string SUBJECT_ID = '66666666-6666-6666-6666-666666666666';
|
||||
private const string PERIOD_ID = '11111111-1111-1111-1111-111111111111';
|
||||
private const string BASE_URL = 'http://ecole-alpha.classeo.local/api';
|
||||
|
||||
private ?EvaluationId $evaluationId = null;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->seedFixtures();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
/** @var Connection $connection */
|
||||
$connection = static::getContainer()->get(Connection::class);
|
||||
$connection->executeStatement('DELETE FROM evaluation_statistics WHERE evaluation_id IN (SELECT id FROM evaluations WHERE tenant_id = :tid)', ['tid' => self::TENANT_ID]);
|
||||
$connection->executeStatement('DELETE FROM student_general_averages WHERE tenant_id = :tid', ['tid' => self::TENANT_ID]);
|
||||
$connection->executeStatement('DELETE FROM student_averages WHERE tenant_id = :tid', ['tid' => self::TENANT_ID]);
|
||||
$connection->executeStatement('DELETE FROM grades WHERE tenant_id = :tid', ['tid' => self::TENANT_ID]);
|
||||
$connection->executeStatement('DELETE FROM evaluations WHERE tenant_id = :tid', ['tid' => self::TENANT_ID]);
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /evaluations/{id}/statistics — Auth
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getEvaluationStatisticsReturns401WithoutAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(401);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getEvaluationStatisticsReturns403ForNonOwner(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::OTHER_TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(403);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /evaluations/{id}/statistics — Happy path
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getEvaluationStatisticsReturnsStatsForOwner(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/evaluations/' . $this->evaluationId . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
self::assertJsonContains([
|
||||
'evaluationId' => (string) $this->evaluationId,
|
||||
'gradedCount' => 2,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getEvaluationStatisticsReturns404ForUnknownEvaluation(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$unknownId = (string) EvaluationId::generate();
|
||||
$client->request('GET', self::BASE_URL . '/evaluations/' . $unknownId . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(404);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /students/{id}/averages — Auth
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getStudentAveragesReturns401WithoutAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', self::BASE_URL . '/students/' . self::STUDENT_ID . '/averages?periodId=' . self::PERIOD_ID, [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(401);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getStudentAveragesReturns403ForUnrelatedParent(): void
|
||||
{
|
||||
$parentId = '88888888-8888-8888-8888-888888888888';
|
||||
$client = $this->createAuthenticatedClient($parentId, ['ROLE_PARENT']);
|
||||
$client->request('GET', self::BASE_URL . '/students/' . self::STUDENT_ID . '/averages?periodId=' . self::PERIOD_ID, [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(403);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /students/{id}/averages — Happy path
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getStudentAveragesReturnsDataForStaff(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/students/' . self::STUDENT_ID . '/averages?periodId=' . self::PERIOD_ID, [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
self::assertJsonContains([
|
||||
'studentId' => self::STUDENT_ID,
|
||||
'periodId' => self::PERIOD_ID,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getStudentAveragesReturnsOwnDataForStudent(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::STUDENT_ID, ['ROLE_ELEVE']);
|
||||
$client->request('GET', self::BASE_URL . '/students/' . self::STUDENT_ID . '/averages?periodId=' . self::PERIOD_ID, [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
self::assertJsonContains([
|
||||
'studentId' => self::STUDENT_ID,
|
||||
]);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /classes/{id}/statistics — Auth
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getClassStatisticsReturns401WithoutAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', self::BASE_URL . '/classes/' . self::CLASS_ID . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(401);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getClassStatisticsReturns403ForStudent(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::STUDENT_ID, ['ROLE_ELEVE']);
|
||||
$client->request('GET', self::BASE_URL . '/classes/' . self::CLASS_ID . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(403);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getClassStatisticsReturns403ForParent(): void
|
||||
{
|
||||
$parentId = '88888888-8888-8888-8888-888888888888';
|
||||
$client = $this->createAuthenticatedClient($parentId, ['ROLE_PARENT']);
|
||||
$client->request('GET', self::BASE_URL . '/classes/' . self::CLASS_ID . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(403);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// GET /classes/{id}/statistics — Happy path
|
||||
// =========================================================================
|
||||
|
||||
#[Test]
|
||||
public function getClassStatisticsReturnsDataForTeacher(): void
|
||||
{
|
||||
$client = $this->createAuthenticatedClient(self::TEACHER_ID, ['ROLE_PROF']);
|
||||
$client->request('GET', self::BASE_URL . '/classes/' . self::CLASS_ID . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
self::assertJsonContains([
|
||||
'classId' => self::CLASS_ID,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function getClassStatisticsReturnsDataForAdmin(): void
|
||||
{
|
||||
$adminId = '99999999-9999-9999-9999-999999999999';
|
||||
$client = $this->createAuthenticatedClient($adminId, ['ROLE_ADMIN']);
|
||||
$client->request('GET', self::BASE_URL . '/classes/' . self::CLASS_ID . '/statistics', [
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
]);
|
||||
|
||||
self::assertResponseIsSuccessful();
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Helpers
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @param list<string> $roles
|
||||
*/
|
||||
private function createAuthenticatedClient(string $userId, array $roles): \ApiPlatform\Symfony\Bundle\Test\Client
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
$user = new SecurityUser(
|
||||
userId: UserId::fromString($userId),
|
||||
email: 'test@classeo.local',
|
||||
hashedPassword: '',
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
roles: $roles,
|
||||
);
|
||||
|
||||
$client->loginUser($user, 'api');
|
||||
|
||||
return $client;
|
||||
}
|
||||
|
||||
private function seedFixtures(): void
|
||||
{
|
||||
$container = static::getContainer();
|
||||
/** @var Connection $connection */
|
||||
$connection = $container->get(Connection::class);
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$now = new DateTimeImmutable();
|
||||
|
||||
$schoolId = '550e8400-e29b-41d4-a716-ff6655440001';
|
||||
$academicYearId = '550e8400-e29b-41d4-a716-ff6655440002';
|
||||
|
||||
// Seed parent tables
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO users (id, tenant_id, email, hashed_password, first_name, last_name, roles, statut, created_at, updated_at)
|
||||
VALUES (:id, :tid, 'teacher-moy@test.local', '', 'Test', 'Teacher', '[\"ROLE_PROF\"]', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::TEACHER_ID, 'tid' => self::TENANT_ID],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO users (id, tenant_id, email, hashed_password, first_name, last_name, roles, statut, created_at, updated_at)
|
||||
VALUES (:id, :tid, 'student-moy@test.local', '', 'Test', 'Student', '[\"ROLE_ELEVE\"]', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::STUDENT_ID, 'tid' => self::TENANT_ID],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO users (id, tenant_id, email, hashed_password, first_name, last_name, roles, statut, created_at, updated_at)
|
||||
VALUES (:id, :tid, 'student2-moy@test.local', '', 'Test', 'Student2', '[\"ROLE_ELEVE\"]', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => '33333333-3333-3333-3333-333333333333', 'tid' => self::TENANT_ID],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO school_classes (id, tenant_id, school_id, academic_year_id, name, status, created_at, updated_at)
|
||||
VALUES (:id, :tid, :sid, :ayid, 'Test-Moy-Class', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::CLASS_ID, 'tid' => self::TENANT_ID, 'sid' => $schoolId, 'ayid' => $academicYearId],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO subjects (id, tenant_id, school_id, name, code, status, created_at, updated_at)
|
||||
VALUES (:id, :tid, :sid, 'Test-Moy-Subject', 'TMOY', 'active', NOW(), NOW())
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::SUBJECT_ID, 'tid' => self::TENANT_ID, 'sid' => $schoolId],
|
||||
);
|
||||
$connection->executeStatement(
|
||||
"INSERT INTO academic_periods (id, tenant_id, academic_year_id, period_type, sequence, label, start_date, end_date)
|
||||
VALUES (:id, :tid, :ayid, 'trimester', 2, 'Trimestre 2', '2026-01-01', '2026-03-31')
|
||||
ON CONFLICT (id) DO NOTHING",
|
||||
['id' => self::PERIOD_ID, 'tid' => self::TENANT_ID, 'ayid' => $academicYearId],
|
||||
);
|
||||
|
||||
// Créer une évaluation publiée avec 2 notes
|
||||
$evaluation = Evaluation::creer(
|
||||
tenantId: $tenantId,
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString(self::SUBJECT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'DS Mathématiques',
|
||||
description: null,
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
gradeScale: new GradeScale(20),
|
||||
coefficient: new Coefficient(1.0),
|
||||
now: $now,
|
||||
);
|
||||
$evaluation->publierNotes($now);
|
||||
$evaluation->pullDomainEvents();
|
||||
|
||||
/** @var EvaluationRepository $evalRepo */
|
||||
$evalRepo = $container->get(EvaluationRepository::class);
|
||||
$evalRepo->save($evaluation);
|
||||
|
||||
$this->evaluationId = $evaluation->id;
|
||||
|
||||
/** @var GradeRepository $gradeRepo */
|
||||
$gradeRepo = $container->get(GradeRepository::class);
|
||||
|
||||
$student2Id = '33333333-3333-3333-3333-333333333333';
|
||||
|
||||
foreach ([
|
||||
[self::STUDENT_ID, 16.0],
|
||||
[$student2Id, 12.0],
|
||||
] as [$studentId, $value]) {
|
||||
$grade = Grade::saisir(
|
||||
tenantId: $tenantId,
|
||||
evaluationId: $evaluation->id,
|
||||
studentId: UserId::fromString($studentId),
|
||||
value: new GradeValue($value),
|
||||
status: GradeStatus::GRADED,
|
||||
gradeScale: new GradeScale(20),
|
||||
createdBy: UserId::fromString(self::TEACHER_ID),
|
||||
now: $now,
|
||||
);
|
||||
$grade->pullDomainEvents();
|
||||
$gradeRepo->save($grade);
|
||||
}
|
||||
|
||||
// Calculer et sauvegarder les statistiques
|
||||
/** @var AverageCalculator $calculator */
|
||||
$calculator = $container->get(AverageCalculator::class);
|
||||
|
||||
$stats = $calculator->calculateClassStatistics([16.0, 12.0]);
|
||||
|
||||
/** @var EvaluationStatisticsRepository $statsRepo */
|
||||
$statsRepo = $container->get(EvaluationStatisticsRepository::class);
|
||||
$statsRepo->save($evaluation->id, $stats);
|
||||
|
||||
// Sauvegarder une moyenne élève
|
||||
/** @var StudentAverageRepository $avgRepo */
|
||||
$avgRepo = $container->get(StudentAverageRepository::class);
|
||||
$avgRepo->saveSubjectAverage(
|
||||
$tenantId,
|
||||
UserId::fromString(self::STUDENT_ID),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
16.0,
|
||||
1,
|
||||
);
|
||||
$avgRepo->saveGeneralAverage(
|
||||
$tenantId,
|
||||
UserId::fromString(self::STUDENT_ID),
|
||||
self::PERIOD_ID,
|
||||
16.0,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Application\Command\CreateAppreciationTemplate;
|
||||
|
||||
use App\Scolarite\Application\Command\CreateAppreciationTemplate\CreateAppreciationTemplateCommand;
|
||||
use App\Scolarite\Application\Command\CreateAppreciationTemplate\CreateAppreciationTemplateHandler;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationCategory;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryAppreciationTemplateRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class CreateAppreciationTemplateHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
|
||||
private InMemoryAppreciationTemplateRepository $templateRepository;
|
||||
private Clock $clock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->templateRepository = new InMemoryAppreciationTemplateRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itCreatesTemplate(): void
|
||||
{
|
||||
$handler = new CreateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$template = $handler(new CreateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
title: 'Très bon travail',
|
||||
content: 'Très bon travail, continuez ainsi !',
|
||||
category: 'positive',
|
||||
));
|
||||
|
||||
self::assertSame('Très bon travail', $template->title);
|
||||
self::assertSame('Très bon travail, continuez ainsi !', $template->content);
|
||||
self::assertSame(AppreciationCategory::POSITIVE, $template->category);
|
||||
self::assertSame(0, $template->usageCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itPersistsTemplate(): void
|
||||
{
|
||||
$handler = new CreateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$template = $handler(new CreateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
title: 'Test',
|
||||
content: 'Contenu test',
|
||||
category: null,
|
||||
));
|
||||
|
||||
$found = $this->templateRepository->findById(
|
||||
$template->id,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNotNull($found);
|
||||
self::assertSame('Test', $found->title);
|
||||
self::assertNull($found->category);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itCreatesTemplateWithNullCategory(): void
|
||||
{
|
||||
$handler = new CreateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$template = $handler(new CreateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
title: 'Sans catégorie',
|
||||
content: 'Contenu',
|
||||
category: null,
|
||||
));
|
||||
|
||||
self::assertNull($template->category);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Application\Command\DeleteAppreciationTemplate;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Command\DeleteAppreciationTemplate\DeleteAppreciationTemplateCommand;
|
||||
use App\Scolarite\Application\Command\DeleteAppreciationTemplate\DeleteAppreciationTemplateHandler;
|
||||
use App\Scolarite\Domain\Exception\AppreciationTemplateNonTrouveeException;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDuModeleException;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationCategory;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplate;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryAppreciationTemplateRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class DeleteAppreciationTemplateHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
|
||||
private InMemoryAppreciationTemplateRepository $templateRepository;
|
||||
private Clock $clock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->templateRepository = new InMemoryAppreciationTemplateRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itDeletesTemplate(): void
|
||||
{
|
||||
$template = $this->seedTemplate();
|
||||
$handler = new DeleteAppreciationTemplateHandler($this->templateRepository);
|
||||
|
||||
$handler(new DeleteAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
templateId: (string) $template->id,
|
||||
));
|
||||
|
||||
$found = $this->templateRepository->findById(
|
||||
$template->id,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNull($found);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenTemplateNotFound(): void
|
||||
{
|
||||
$handler = new DeleteAppreciationTemplateHandler($this->templateRepository);
|
||||
|
||||
$this->expectException(AppreciationTemplateNonTrouveeException::class);
|
||||
|
||||
$handler(new DeleteAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
templateId: '550e8400-e29b-41d4-a716-446655440099',
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenTeacherNotOwner(): void
|
||||
{
|
||||
$template = $this->seedTemplate();
|
||||
$handler = new DeleteAppreciationTemplateHandler($this->templateRepository);
|
||||
|
||||
$this->expectException(NonProprietaireDuModeleException::class);
|
||||
|
||||
$handler(new DeleteAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: '550e8400-e29b-41d4-a716-446655440099',
|
||||
templateId: (string) $template->id,
|
||||
));
|
||||
}
|
||||
|
||||
private function seedTemplate(): AppreciationTemplate
|
||||
{
|
||||
$template = AppreciationTemplate::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Template test',
|
||||
content: 'Contenu test',
|
||||
category: AppreciationCategory::POSITIVE,
|
||||
now: $this->clock->now(),
|
||||
);
|
||||
|
||||
$this->templateRepository->save($template);
|
||||
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Application\Command\SaveAppreciation;
|
||||
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassId;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Command\SaveAppreciation\SaveAppreciationCommand;
|
||||
use App\Scolarite\Application\Command\SaveAppreciation\SaveAppreciationHandler;
|
||||
use App\Scolarite\Domain\Exception\AppreciationTropLongueException;
|
||||
use App\Scolarite\Domain\Exception\GradeNotFoundException;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDeLEvaluationException;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Coefficient;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Evaluation;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationStatus;
|
||||
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
|
||||
use App\Scolarite\Domain\Model\Grade\Grade;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeStatus;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeValue;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryGradeRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use function str_repeat;
|
||||
|
||||
final class SaveAppreciationHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string EVALUATION_ID = '550e8400-e29b-41d4-a716-446655440040';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
private const string STUDENT_ID = '550e8400-e29b-41d4-a716-446655440050';
|
||||
|
||||
private InMemoryEvaluationRepository $evaluationRepository;
|
||||
private InMemoryGradeRepository $gradeRepository;
|
||||
private Clock $clock;
|
||||
private string $gradeId;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->evaluationRepository = new InMemoryEvaluationRepository();
|
||||
$this->gradeRepository = new InMemoryGradeRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
}
|
||||
};
|
||||
|
||||
$this->seedEvaluationAndGrade();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itSavesAppreciation(): void
|
||||
{
|
||||
$handler = $this->createHandler();
|
||||
|
||||
$grade = $handler(new SaveAppreciationCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
gradeId: $this->gradeId,
|
||||
teacherId: self::TEACHER_ID,
|
||||
appreciation: 'Très bon travail',
|
||||
));
|
||||
|
||||
self::assertSame('Très bon travail', $grade->appreciation);
|
||||
self::assertNotNull($grade->appreciationUpdatedAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itClearsAppreciation(): void
|
||||
{
|
||||
$handler = $this->createHandler();
|
||||
|
||||
$handler(new SaveAppreciationCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
gradeId: $this->gradeId,
|
||||
teacherId: self::TEACHER_ID,
|
||||
appreciation: 'Bon travail',
|
||||
));
|
||||
|
||||
$grade = $handler(new SaveAppreciationCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
gradeId: $this->gradeId,
|
||||
teacherId: self::TEACHER_ID,
|
||||
appreciation: null,
|
||||
));
|
||||
|
||||
self::assertNull($grade->appreciation);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenTeacherNotOwner(): void
|
||||
{
|
||||
$handler = $this->createHandler();
|
||||
|
||||
$this->expectException(NonProprietaireDeLEvaluationException::class);
|
||||
|
||||
$handler(new SaveAppreciationCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
gradeId: $this->gradeId,
|
||||
teacherId: '550e8400-e29b-41d4-a716-446655440099',
|
||||
appreciation: 'Test',
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenGradeNotFound(): void
|
||||
{
|
||||
$handler = $this->createHandler();
|
||||
|
||||
$this->expectException(GradeNotFoundException::class);
|
||||
|
||||
$handler(new SaveAppreciationCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
gradeId: '550e8400-e29b-41d4-a716-446655440099',
|
||||
teacherId: self::TEACHER_ID,
|
||||
appreciation: 'Test',
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenAppreciationTooLong(): void
|
||||
{
|
||||
$handler = $this->createHandler();
|
||||
|
||||
$this->expectException(AppreciationTropLongueException::class);
|
||||
|
||||
$handler(new SaveAppreciationCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
gradeId: $this->gradeId,
|
||||
teacherId: self::TEACHER_ID,
|
||||
appreciation: str_repeat('a', 501),
|
||||
));
|
||||
}
|
||||
|
||||
private function createHandler(): SaveAppreciationHandler
|
||||
{
|
||||
return new SaveAppreciationHandler(
|
||||
$this->evaluationRepository,
|
||||
$this->gradeRepository,
|
||||
$this->clock,
|
||||
);
|
||||
}
|
||||
|
||||
private function seedEvaluationAndGrade(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
|
||||
$evaluation = Evaluation::reconstitute(
|
||||
id: EvaluationId::fromString(self::EVALUATION_ID),
|
||||
tenantId: $tenantId,
|
||||
classId: ClassId::fromString('550e8400-e29b-41d4-a716-446655440020'),
|
||||
subjectId: SubjectId::fromString('550e8400-e29b-41d4-a716-446655440030'),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Contrôle',
|
||||
description: null,
|
||||
evaluationDate: new DateTimeImmutable('2026-04-15'),
|
||||
gradeScale: new GradeScale(20),
|
||||
coefficient: new Coefficient(1.0),
|
||||
status: EvaluationStatus::PUBLISHED,
|
||||
createdAt: new DateTimeImmutable('2026-03-12 10:00:00'),
|
||||
updatedAt: new DateTimeImmutable('2026-03-12 10:00:00'),
|
||||
);
|
||||
|
||||
$this->evaluationRepository->save($evaluation);
|
||||
|
||||
$grade = Grade::saisir(
|
||||
tenantId: $tenantId,
|
||||
evaluationId: EvaluationId::fromString(self::EVALUATION_ID),
|
||||
studentId: UserId::fromString(self::STUDENT_ID),
|
||||
value: new GradeValue(15.5),
|
||||
status: GradeStatus::GRADED,
|
||||
gradeScale: new GradeScale(20),
|
||||
createdBy: UserId::fromString(self::TEACHER_ID),
|
||||
now: new DateTimeImmutable('2026-03-27 10:00:00'),
|
||||
);
|
||||
|
||||
$this->gradeRepository->save($grade);
|
||||
$this->gradeId = (string) $grade->id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Application\Command\UpdateAppreciationTemplate;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Command\UpdateAppreciationTemplate\UpdateAppreciationTemplateCommand;
|
||||
use App\Scolarite\Application\Command\UpdateAppreciationTemplate\UpdateAppreciationTemplateHandler;
|
||||
use App\Scolarite\Domain\Exception\AppreciationTemplateNonTrouveeException;
|
||||
use App\Scolarite\Domain\Exception\CategorieAppreciationInvalideException;
|
||||
use App\Scolarite\Domain\Exception\NonProprietaireDuModeleException;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationCategory;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplate;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryAppreciationTemplateRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class UpdateAppreciationTemplateHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
|
||||
private InMemoryAppreciationTemplateRepository $templateRepository;
|
||||
private Clock $clock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->templateRepository = new InMemoryAppreciationTemplateRepository();
|
||||
$this->clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itUpdatesTemplate(): void
|
||||
{
|
||||
$template = $this->seedTemplate();
|
||||
$handler = new UpdateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$updated = $handler(new UpdateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
templateId: (string) $template->id,
|
||||
title: 'Titre modifié',
|
||||
content: 'Contenu modifié',
|
||||
category: 'improvement',
|
||||
));
|
||||
|
||||
self::assertSame('Titre modifié', $updated->title);
|
||||
self::assertSame('Contenu modifié', $updated->content);
|
||||
self::assertSame(AppreciationCategory::IMPROVEMENT, $updated->category);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itPersistsUpdatedTemplate(): void
|
||||
{
|
||||
$template = $this->seedTemplate();
|
||||
$handler = new UpdateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$handler(new UpdateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
templateId: (string) $template->id,
|
||||
title: 'Persisté',
|
||||
content: 'Contenu persisté',
|
||||
category: null,
|
||||
));
|
||||
|
||||
$found = $this->templateRepository->findById(
|
||||
$template->id,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNotNull($found);
|
||||
self::assertSame('Persisté', $found->title);
|
||||
self::assertNull($found->category);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenTemplateNotFound(): void
|
||||
{
|
||||
$handler = new UpdateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$this->expectException(AppreciationTemplateNonTrouveeException::class);
|
||||
|
||||
$handler(new UpdateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
templateId: '550e8400-e29b-41d4-a716-446655440099',
|
||||
title: 'Test',
|
||||
content: 'Contenu',
|
||||
category: null,
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenTeacherNotOwner(): void
|
||||
{
|
||||
$template = $this->seedTemplate();
|
||||
$handler = new UpdateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$this->expectException(NonProprietaireDuModeleException::class);
|
||||
|
||||
$handler(new UpdateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: '550e8400-e29b-41d4-a716-446655440099',
|
||||
templateId: (string) $template->id,
|
||||
title: 'Hijack',
|
||||
content: 'Contenu',
|
||||
category: null,
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenCategoryInvalid(): void
|
||||
{
|
||||
$template = $this->seedTemplate();
|
||||
$handler = new UpdateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$this->expectException(CategorieAppreciationInvalideException::class);
|
||||
|
||||
$handler(new UpdateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
templateId: (string) $template->id,
|
||||
title: 'Test',
|
||||
content: 'Contenu',
|
||||
category: 'invalid_category',
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itUpdatesTemplateWithNullCategory(): void
|
||||
{
|
||||
$template = $this->seedTemplate();
|
||||
$handler = new UpdateAppreciationTemplateHandler($this->templateRepository, $this->clock);
|
||||
|
||||
$updated = $handler(new UpdateAppreciationTemplateCommand(
|
||||
tenantId: self::TENANT_ID,
|
||||
teacherId: self::TEACHER_ID,
|
||||
templateId: (string) $template->id,
|
||||
title: 'Sans catégorie',
|
||||
content: 'Contenu mis à jour',
|
||||
category: null,
|
||||
));
|
||||
|
||||
self::assertNull($updated->category);
|
||||
self::assertSame('Sans catégorie', $updated->title);
|
||||
}
|
||||
|
||||
private function seedTemplate(): AppreciationTemplate
|
||||
{
|
||||
$template = AppreciationTemplate::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Template original',
|
||||
content: 'Contenu original',
|
||||
category: AppreciationCategory::POSITIVE,
|
||||
now: $this->clock->now(),
|
||||
);
|
||||
|
||||
$this->templateRepository->save($template);
|
||||
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Domain\Model\Grade;
|
||||
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationCategory;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplate;
|
||||
use App\Scolarite\Domain\Model\Grade\AppreciationTemplateId;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class AppreciationTemplateTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
private const string TEACHER_ID = '550e8400-e29b-41d4-a716-446655440010';
|
||||
|
||||
#[Test]
|
||||
public function creerSetsAllProperties(): void
|
||||
{
|
||||
$now = new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
|
||||
$template = AppreciationTemplate::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Très bon travail',
|
||||
content: 'Très bon travail, continuez ainsi !',
|
||||
category: AppreciationCategory::POSITIVE,
|
||||
now: $now,
|
||||
);
|
||||
|
||||
self::assertSame('Très bon travail', $template->title);
|
||||
self::assertSame('Très bon travail, continuez ainsi !', $template->content);
|
||||
self::assertSame(AppreciationCategory::POSITIVE, $template->category);
|
||||
self::assertSame(0, $template->usageCount);
|
||||
self::assertEquals($now, $template->createdAt);
|
||||
self::assertEquals($now, $template->updatedAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function creerAcceptsNullCategory(): void
|
||||
{
|
||||
$template = $this->createTemplate(category: null);
|
||||
|
||||
self::assertNull($template->category);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function modifierUpdatesProperties(): void
|
||||
{
|
||||
$template = $this->createTemplate();
|
||||
$modifiedAt = new DateTimeImmutable('2026-03-31 14:00:00');
|
||||
|
||||
$template->modifier(
|
||||
title: 'Nouveau titre',
|
||||
content: 'Nouveau contenu',
|
||||
category: AppreciationCategory::IMPROVEMENT,
|
||||
now: $modifiedAt,
|
||||
);
|
||||
|
||||
self::assertSame('Nouveau titre', $template->title);
|
||||
self::assertSame('Nouveau contenu', $template->content);
|
||||
self::assertSame(AppreciationCategory::IMPROVEMENT, $template->category);
|
||||
self::assertEquals($modifiedAt, $template->updatedAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function incrementerUtilisationIncreasesCount(): void
|
||||
{
|
||||
$template = $this->createTemplate();
|
||||
|
||||
$template->incrementerUtilisation();
|
||||
self::assertSame(1, $template->usageCount);
|
||||
|
||||
$template->incrementerUtilisation();
|
||||
self::assertSame(2, $template->usageCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function reconstituteRestoresAllProperties(): void
|
||||
{
|
||||
$id = AppreciationTemplateId::generate();
|
||||
$createdAt = new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
$updatedAt = new DateTimeImmutable('2026-03-31 14:00:00');
|
||||
|
||||
$template = AppreciationTemplate::reconstitute(
|
||||
id: $id,
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Titre',
|
||||
content: 'Contenu',
|
||||
category: AppreciationCategory::NEUTRAL,
|
||||
usageCount: 5,
|
||||
createdAt: $createdAt,
|
||||
updatedAt: $updatedAt,
|
||||
);
|
||||
|
||||
self::assertTrue($template->id->equals($id));
|
||||
self::assertSame('Titre', $template->title);
|
||||
self::assertSame('Contenu', $template->content);
|
||||
self::assertSame(AppreciationCategory::NEUTRAL, $template->category);
|
||||
self::assertSame(5, $template->usageCount);
|
||||
self::assertEquals($createdAt, $template->createdAt);
|
||||
self::assertEquals($updatedAt, $template->updatedAt);
|
||||
}
|
||||
|
||||
private function createTemplate(?AppreciationCategory $category = AppreciationCategory::POSITIVE): AppreciationTemplate
|
||||
{
|
||||
return AppreciationTemplate::creer(
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Très bon travail',
|
||||
content: 'Très bon travail, continuez ainsi !',
|
||||
category: $category,
|
||||
now: new DateTimeImmutable('2026-03-31 10:00:00'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ namespace App\Tests\Unit\Scolarite\Domain\Model\Grade;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Domain\Event\NoteModifiee;
|
||||
use App\Scolarite\Domain\Event\NoteSaisie;
|
||||
use App\Scolarite\Domain\Exception\AppreciationTropLongueException;
|
||||
use App\Scolarite\Domain\Exception\NoteRequiseException;
|
||||
use App\Scolarite\Domain\Exception\ValeurNoteInvalideException;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
@@ -20,6 +21,8 @@ use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use function str_repeat;
|
||||
|
||||
final class GradeTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
@@ -269,6 +272,96 @@ final class GradeTest extends TestCase
|
||||
self::assertEmpty($grade->pullDomainEvents());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function saisirAppreciationSetsAppreciation(): void
|
||||
{
|
||||
$grade = $this->createGrade();
|
||||
$now = new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
|
||||
$grade->saisirAppreciation('Très bon travail', $now);
|
||||
|
||||
self::assertSame('Très bon travail', $grade->appreciation);
|
||||
self::assertEquals($now, $grade->appreciationUpdatedAt);
|
||||
self::assertEquals($now, $grade->updatedAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function saisirAppreciationAcceptsNull(): void
|
||||
{
|
||||
$grade = $this->createGrade();
|
||||
$grade->saisirAppreciation('Temporaire', new DateTimeImmutable('2026-03-31 09:00:00'));
|
||||
|
||||
$grade->saisirAppreciation(null, new DateTimeImmutable('2026-03-31 10:00:00'));
|
||||
|
||||
self::assertNull($grade->appreciation);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function saisirAppreciationAcceptsEmptyString(): void
|
||||
{
|
||||
$grade = $this->createGrade();
|
||||
$grade->saisirAppreciation('Temporaire', new DateTimeImmutable('2026-03-31 09:00:00'));
|
||||
|
||||
$grade->saisirAppreciation('', new DateTimeImmutable('2026-03-31 10:00:00'));
|
||||
|
||||
self::assertNull($grade->appreciation);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function saisirAppreciationThrowsWhenTooLong(): void
|
||||
{
|
||||
$grade = $this->createGrade();
|
||||
|
||||
$this->expectException(AppreciationTropLongueException::class);
|
||||
|
||||
$grade->saisirAppreciation(str_repeat('a', 501), new DateTimeImmutable('2026-03-31 10:00:00'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function saisirAppreciationAcceptsMaxLength(): void
|
||||
{
|
||||
$grade = $this->createGrade();
|
||||
|
||||
$grade->saisirAppreciation(str_repeat('a', 500), new DateTimeImmutable('2026-03-31 10:00:00'));
|
||||
|
||||
self::assertSame(500, mb_strlen($grade->appreciation ?? ''));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function newGradeHasNullAppreciation(): void
|
||||
{
|
||||
$grade = $this->createGrade();
|
||||
|
||||
self::assertNull($grade->appreciation);
|
||||
self::assertNull($grade->appreciationUpdatedAt);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function reconstituteRestoresAppreciation(): void
|
||||
{
|
||||
$gradeId = GradeId::generate();
|
||||
$createdAt = new DateTimeImmutable('2026-03-27 10:00:00');
|
||||
$updatedAt = new DateTimeImmutable('2026-03-27 14:00:00');
|
||||
$appreciationUpdatedAt = new DateTimeImmutable('2026-03-31 10:00:00');
|
||||
|
||||
$grade = Grade::reconstitute(
|
||||
id: $gradeId,
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
evaluationId: EvaluationId::fromString(self::EVALUATION_ID),
|
||||
studentId: UserId::fromString(self::STUDENT_ID),
|
||||
value: new GradeValue(15.5),
|
||||
status: GradeStatus::GRADED,
|
||||
createdBy: UserId::fromString(self::TEACHER_ID),
|
||||
createdAt: $createdAt,
|
||||
updatedAt: $updatedAt,
|
||||
appreciation: 'Bon travail',
|
||||
appreciationUpdatedAt: $appreciationUpdatedAt,
|
||||
);
|
||||
|
||||
self::assertSame('Bon travail', $grade->appreciation);
|
||||
self::assertEquals($appreciationUpdatedAt, $grade->appreciationUpdatedAt);
|
||||
}
|
||||
|
||||
private function createGrade(): Grade
|
||||
{
|
||||
return Grade::saisir(
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Domain\Service;
|
||||
|
||||
use App\Scolarite\Domain\Model\Evaluation\Coefficient;
|
||||
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
|
||||
use App\Scolarite\Domain\Service\AverageCalculator;
|
||||
use App\Scolarite\Domain\Service\GradeEntry;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class AverageCalculatorTest extends TestCase
|
||||
{
|
||||
private AverageCalculator $calculator;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->calculator = new AverageCalculator();
|
||||
}
|
||||
|
||||
// --- Subject Average ---
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageReturnsNullWhenNoGrades(): void
|
||||
{
|
||||
self::assertNull($this->calculator->calculateSubjectAverage([]));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageWithSingleGrade(): void
|
||||
{
|
||||
$grades = [
|
||||
new GradeEntry(
|
||||
value: 15.0,
|
||||
gradeScale: new GradeScale(20),
|
||||
coefficient: new Coefficient(1.0),
|
||||
),
|
||||
];
|
||||
|
||||
self::assertSame(15.0, $this->calculator->calculateSubjectAverage($grades));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageWithEqualCoefficients(): void
|
||||
{
|
||||
$grades = [
|
||||
new GradeEntry(value: 12.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
new GradeEntry(value: 16.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
new GradeEntry(value: 8.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
];
|
||||
|
||||
// (12 + 16 + 8) / 3 = 12.0
|
||||
self::assertSame(12.0, $this->calculator->calculateSubjectAverage($grades));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageWithDifferentCoefficients(): void
|
||||
{
|
||||
$grades = [
|
||||
new GradeEntry(value: 14.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(2.0)),
|
||||
new GradeEntry(value: 8.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
];
|
||||
|
||||
// (14×2 + 8×1) / (2+1) = 36/3 = 12.0
|
||||
self::assertSame(12.0, $this->calculator->calculateSubjectAverage($grades));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageNormalizesToScale20(): void
|
||||
{
|
||||
$grades = [
|
||||
new GradeEntry(value: 8.0, gradeScale: new GradeScale(10), coefficient: new Coefficient(1.0)),
|
||||
];
|
||||
|
||||
// 8/10 × 20 = 16.0
|
||||
self::assertSame(16.0, $this->calculator->calculateSubjectAverage($grades));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageWithMixedScales(): void
|
||||
{
|
||||
$grades = [
|
||||
new GradeEntry(value: 15.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
new GradeEntry(value: 40.0, gradeScale: new GradeScale(100), coefficient: new Coefficient(1.0)),
|
||||
];
|
||||
|
||||
// 15/20×20=15 et 40/100×20=8 → (15+8)/2 = 11.5
|
||||
self::assertSame(11.5, $this->calculator->calculateSubjectAverage($grades));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageWithMixedScalesAndCoefficients(): void
|
||||
{
|
||||
$grades = [
|
||||
new GradeEntry(value: 16.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(3.0)),
|
||||
new GradeEntry(value: 7.0, gradeScale: new GradeScale(10), coefficient: new Coefficient(2.0)),
|
||||
];
|
||||
|
||||
// 16/20×20=16 (coef 3), 7/10×20=14 (coef 2)
|
||||
// (16×3 + 14×2) / (3+2) = (48+28)/5 = 76/5 = 15.2
|
||||
self::assertSame(15.2, $this->calculator->calculateSubjectAverage($grades));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function subjectAverageRoundsToTwoDecimals(): void
|
||||
{
|
||||
$grades = [
|
||||
new GradeEntry(value: 13.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
new GradeEntry(value: 7.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
new GradeEntry(value: 11.0, gradeScale: new GradeScale(20), coefficient: new Coefficient(1.0)),
|
||||
];
|
||||
|
||||
// (13+7+11)/3 = 31/3 = 10.333... → 10.33
|
||||
self::assertSame(10.33, $this->calculator->calculateSubjectAverage($grades));
|
||||
}
|
||||
|
||||
// --- General Average ---
|
||||
|
||||
#[Test]
|
||||
public function generalAverageReturnsNullWhenNoSubjects(): void
|
||||
{
|
||||
self::assertNull($this->calculator->calculateGeneralAverage([]));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function generalAverageWithSingleSubject(): void
|
||||
{
|
||||
self::assertSame(14.5, $this->calculator->calculateGeneralAverage([14.5]));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function generalAverageIsArithmeticMean(): void
|
||||
{
|
||||
// (12.0 + 15.0 + 9.0) / 3 = 12.0
|
||||
self::assertSame(12.0, $this->calculator->calculateGeneralAverage([12.0, 15.0, 9.0]));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function generalAverageRoundsToTwoDecimals(): void
|
||||
{
|
||||
// (14.0 + 13.0 + 11.0) / 3 = 38/3 = 12.666... → 12.67
|
||||
self::assertSame(12.67, $this->calculator->calculateGeneralAverage([14.0, 13.0, 11.0]));
|
||||
}
|
||||
|
||||
// --- Class Statistics ---
|
||||
|
||||
#[Test]
|
||||
public function classStatisticsReturnsEmptyWhenNoGrades(): void
|
||||
{
|
||||
$stats = $this->calculator->calculateClassStatistics([]);
|
||||
|
||||
self::assertNull($stats->average);
|
||||
self::assertNull($stats->min);
|
||||
self::assertNull($stats->max);
|
||||
self::assertNull($stats->median);
|
||||
self::assertSame(0, $stats->gradedCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function classStatisticsWithSingleGrade(): void
|
||||
{
|
||||
$stats = $this->calculator->calculateClassStatistics([15.0]);
|
||||
|
||||
self::assertSame(15.0, $stats->average);
|
||||
self::assertSame(15.0, $stats->min);
|
||||
self::assertSame(15.0, $stats->max);
|
||||
self::assertSame(15.0, $stats->median);
|
||||
self::assertSame(1, $stats->gradedCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function classStatisticsWithOddNumberOfGrades(): void
|
||||
{
|
||||
$stats = $this->calculator->calculateClassStatistics([8.0, 15.0, 12.0]);
|
||||
|
||||
// Sorted: 8, 12, 15
|
||||
self::assertSame(11.67, $stats->average); // 35/3
|
||||
self::assertSame(8.0, $stats->min);
|
||||
self::assertSame(15.0, $stats->max);
|
||||
self::assertSame(12.0, $stats->median); // middle element
|
||||
self::assertSame(3, $stats->gradedCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function classStatisticsWithEvenNumberOfGrades(): void
|
||||
{
|
||||
$stats = $this->calculator->calculateClassStatistics([7.0, 12.0, 14.0, 18.0]);
|
||||
|
||||
// Sorted: 7, 12, 14, 18
|
||||
self::assertSame(12.75, $stats->average); // 51/4
|
||||
self::assertSame(7.0, $stats->min);
|
||||
self::assertSame(18.0, $stats->max);
|
||||
self::assertSame(13.0, $stats->median); // (12+14)/2
|
||||
self::assertSame(4, $stats->gradedCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function classStatisticsSortsInputValues(): void
|
||||
{
|
||||
// Input not sorted
|
||||
$stats = $this->calculator->calculateClassStatistics([18.0, 7.0, 14.0, 12.0]);
|
||||
|
||||
self::assertSame(7.0, $stats->min);
|
||||
self::assertSame(18.0, $stats->max);
|
||||
self::assertSame(13.0, $stats->median); // (12+14)/2
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function classStatisticsWithIdenticalGrades(): void
|
||||
{
|
||||
$stats = $this->calculator->calculateClassStatistics([10.0, 10.0, 10.0]);
|
||||
|
||||
self::assertSame(10.0, $stats->average);
|
||||
self::assertSame(10.0, $stats->min);
|
||||
self::assertSame(10.0, $stats->max);
|
||||
self::assertSame(10.0, $stats->median);
|
||||
self::assertSame(3, $stats->gradedCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function classStatisticsWithTwoGrades(): void
|
||||
{
|
||||
$stats = $this->calculator->calculateClassStatistics([6.0, 16.0]);
|
||||
|
||||
self::assertSame(11.0, $stats->average);
|
||||
self::assertSame(6.0, $stats->min);
|
||||
self::assertSame(16.0, $stats->max);
|
||||
self::assertSame(11.0, $stats->median); // (6+16)/2
|
||||
self::assertSame(2, $stats->gradedCount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Infrastructure\Cache;
|
||||
|
||||
use App\Scolarite\Domain\Model\Evaluation\ClassStatistics;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Infrastructure\Cache\CachingEvaluationStatisticsRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationStatisticsRepository;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
||||
|
||||
final class CachingEvaluationStatisticsRepositoryTest extends TestCase
|
||||
{
|
||||
private InMemoryEvaluationStatisticsRepository $inner;
|
||||
private CachingEvaluationStatisticsRepository $cached;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->inner = new InMemoryEvaluationStatisticsRepository();
|
||||
$this->cached = new CachingEvaluationStatisticsRepository(
|
||||
inner: $this->inner,
|
||||
cache: new ArrayAdapter(),
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itCachesStatisticsOnRead(): void
|
||||
{
|
||||
$evaluationId = EvaluationId::generate();
|
||||
$stats = new ClassStatistics(average: 14.5, min: 8.0, max: 19.0, median: 15.0, gradedCount: 5);
|
||||
|
||||
$this->inner->save($evaluationId, $stats);
|
||||
|
||||
// Premier appel : va au inner
|
||||
$result1 = $this->cached->findByEvaluation($evaluationId);
|
||||
self::assertNotNull($result1);
|
||||
self::assertSame(14.5, $result1->average);
|
||||
|
||||
// Deuxième appel : devrait venir du cache (même résultat)
|
||||
$result2 = $this->cached->findByEvaluation($evaluationId);
|
||||
self::assertNotNull($result2);
|
||||
self::assertSame(14.5, $result2->average);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itInvalidatesCacheOnSave(): void
|
||||
{
|
||||
$evaluationId = EvaluationId::generate();
|
||||
$stats1 = new ClassStatistics(average: 14.0, min: 8.0, max: 19.0, median: 14.0, gradedCount: 3);
|
||||
|
||||
// Sauvegarder et lire pour remplir le cache
|
||||
$this->cached->save($evaluationId, $stats1);
|
||||
$this->cached->findByEvaluation($evaluationId);
|
||||
|
||||
// Mettre à jour
|
||||
$stats2 = new ClassStatistics(average: 16.0, min: 10.0, max: 20.0, median: 16.0, gradedCount: 4);
|
||||
$this->cached->save($evaluationId, $stats2);
|
||||
|
||||
$result = $this->cached->findByEvaluation($evaluationId);
|
||||
self::assertNotNull($result);
|
||||
self::assertSame(16.0, $result->average);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itReturnsNullForUnknownEvaluation(): void
|
||||
{
|
||||
$result = $this->cached->findByEvaluation(EvaluationId::generate());
|
||||
self::assertNull($result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itInvalidatesCacheOnDelete(): void
|
||||
{
|
||||
$evaluationId = EvaluationId::generate();
|
||||
$stats = new ClassStatistics(average: 14.5, min: 8.0, max: 19.0, median: 15.0, gradedCount: 5);
|
||||
|
||||
$this->cached->save($evaluationId, $stats);
|
||||
|
||||
// Remplir le cache
|
||||
$this->cached->findByEvaluation($evaluationId);
|
||||
|
||||
// Supprimer
|
||||
$this->cached->delete($evaluationId);
|
||||
|
||||
// Le cache ne doit plus retourner l'ancienne valeur
|
||||
self::assertNull($this->cached->findByEvaluation($evaluationId));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itCachesNullResultForUnknownEvaluation(): void
|
||||
{
|
||||
$evaluationId = EvaluationId::generate();
|
||||
|
||||
// Premier appel : null → mis en cache
|
||||
self::assertNull($this->cached->findByEvaluation($evaluationId));
|
||||
|
||||
// Sauvegarder directement dans inner (sans passer par le cache)
|
||||
$this->inner->save($evaluationId, new ClassStatistics(
|
||||
average: 12.0,
|
||||
min: 10.0,
|
||||
max: 14.0,
|
||||
median: 12.0,
|
||||
gradedCount: 2,
|
||||
));
|
||||
|
||||
// Le cache retourne encore null (valeur cachée)
|
||||
$result = $this->cached->findByEvaluation($evaluationId);
|
||||
self::assertNull($result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Infrastructure\Cache;
|
||||
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Infrastructure\Cache\CachingStudentAverageRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryStudentAverageRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
||||
|
||||
final class CachingStudentAverageRepositoryTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
|
||||
private const string STUDENT_ID = '22222222-2222-2222-2222-222222222222';
|
||||
private const string SUBJECT_ID = '66666666-6666-6666-6666-666666666666';
|
||||
private const string PERIOD_ID = '11111111-1111-1111-1111-111111111111';
|
||||
|
||||
private InMemoryStudentAverageRepository $inner;
|
||||
private CachingStudentAverageRepository $cached;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->inner = new InMemoryStudentAverageRepository();
|
||||
$this->cached = new CachingStudentAverageRepository(
|
||||
inner: $this->inner,
|
||||
cache: new ArrayAdapter(),
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itCachesSubjectAveragesOnRead(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$studentId = UserId::fromString(self::STUDENT_ID);
|
||||
|
||||
$this->inner->saveSubjectAverage(
|
||||
$tenantId,
|
||||
$studentId,
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
15.0,
|
||||
3,
|
||||
);
|
||||
|
||||
$result1 = $this->cached->findSubjectAveragesForStudent($studentId, self::PERIOD_ID, $tenantId);
|
||||
$result2 = $this->cached->findSubjectAveragesForStudent($studentId, self::PERIOD_ID, $tenantId);
|
||||
|
||||
self::assertSame([15.0], $result1);
|
||||
self::assertSame([15.0], $result2);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itInvalidatesCacheOnSaveSubjectAverage(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$studentId = UserId::fromString(self::STUDENT_ID);
|
||||
$subjectId = SubjectId::fromString(self::SUBJECT_ID);
|
||||
|
||||
$this->cached->saveSubjectAverage($tenantId, $studentId, $subjectId, self::PERIOD_ID, 14.0, 2);
|
||||
|
||||
// Remplir le cache
|
||||
$this->cached->findSubjectAveragesForStudent($studentId, self::PERIOD_ID, $tenantId);
|
||||
|
||||
// Mettre à jour → doit invalider le cache
|
||||
$this->cached->saveSubjectAverage($tenantId, $studentId, $subjectId, self::PERIOD_ID, 16.0, 3);
|
||||
|
||||
$result = $this->cached->findSubjectAveragesForStudent($studentId, self::PERIOD_ID, $tenantId);
|
||||
|
||||
self::assertSame([16.0], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itReturnsEmptyArrayWhenNoAverages(): void
|
||||
{
|
||||
$result = $this->cached->findSubjectAveragesForStudent(
|
||||
UserId::fromString(self::STUDENT_ID),
|
||||
self::PERIOD_ID,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertSame([], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itDelegatesToInnerForGeneralAverage(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$studentId = UserId::fromString(self::STUDENT_ID);
|
||||
|
||||
$this->cached->saveGeneralAverage($tenantId, $studentId, self::PERIOD_ID, 13.5);
|
||||
|
||||
$result = $this->cached->findGeneralAverageForStudent($studentId, self::PERIOD_ID, $tenantId);
|
||||
|
||||
self::assertSame(13.5, $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itInvalidatesCacheOnDeleteSubjectAverage(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$studentId = UserId::fromString(self::STUDENT_ID);
|
||||
$subjectId = SubjectId::fromString(self::SUBJECT_ID);
|
||||
|
||||
$this->cached->saveSubjectAverage($tenantId, $studentId, $subjectId, self::PERIOD_ID, 14.0, 2);
|
||||
|
||||
// Remplir le cache
|
||||
$this->cached->findSubjectAveragesForStudent($studentId, self::PERIOD_ID, $tenantId);
|
||||
|
||||
// Supprimer → doit invalider le cache
|
||||
$this->cached->deleteSubjectAverage($studentId, $subjectId, self::PERIOD_ID, $tenantId);
|
||||
|
||||
$result = $this->cached->findSubjectAveragesForStudent($studentId, self::PERIOD_ID, $tenantId);
|
||||
|
||||
self::assertSame([], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itCachesMultipleSubjectAverages(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$studentId = UserId::fromString(self::STUDENT_ID);
|
||||
$subject2Id = '77777777-7777-7777-7777-777777777777';
|
||||
|
||||
$this->cached->saveSubjectAverage(
|
||||
$tenantId,
|
||||
$studentId,
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
15.0,
|
||||
3,
|
||||
);
|
||||
$this->cached->saveSubjectAverage(
|
||||
$tenantId,
|
||||
$studentId,
|
||||
SubjectId::fromString($subject2Id),
|
||||
self::PERIOD_ID,
|
||||
12.0,
|
||||
2,
|
||||
);
|
||||
|
||||
$result = $this->cached->findSubjectAveragesForStudent($studentId, self::PERIOD_ID, $tenantId);
|
||||
|
||||
self::assertCount(2, $result);
|
||||
self::assertContains(15.0, $result);
|
||||
self::assertContains(12.0, $result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Infrastructure\Console;
|
||||
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassId;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Port\PeriodFinder;
|
||||
use App\Scolarite\Application\Port\PeriodInfo;
|
||||
use App\Scolarite\Application\Service\RecalculerMoyennesService;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Coefficient;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Evaluation;
|
||||
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
|
||||
use App\Scolarite\Domain\Model\Grade\Grade;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeStatus;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeValue;
|
||||
use App\Scolarite\Domain\Service\AverageCalculator;
|
||||
use App\Scolarite\Infrastructure\Console\RecalculerToutesMoyennesCommand;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationStatisticsRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryGradeRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryStudentAverageRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use App\Shared\Infrastructure\Tenant\TenantConfig;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use App\Shared\Infrastructure\Tenant\TenantDatabaseSwitcher;
|
||||
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
|
||||
use App\Shared\Infrastructure\Tenant\TenantRegistry;
|
||||
use DateTimeImmutable;
|
||||
use Override;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
final class RecalculerToutesMoyennesCommandTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
|
||||
public const string PERIOD_ID = '11111111-1111-1111-1111-111111111111';
|
||||
private const string STUDENT_ID = '22222222-2222-2222-2222-222222222222';
|
||||
private const string TEACHER_ID = '44444444-4444-4444-4444-444444444444';
|
||||
private const string CLASS_ID = '55555555-5555-5555-5555-555555555555';
|
||||
private const string SUBJECT_ID = '66666666-6666-6666-6666-666666666666';
|
||||
|
||||
private InMemoryEvaluationRepository $evaluationRepo;
|
||||
private InMemoryGradeRepository $gradeRepo;
|
||||
private InMemoryEvaluationStatisticsRepository $evalStatsRepo;
|
||||
private InMemoryStudentAverageRepository $studentAvgRepo;
|
||||
private TenantConfig $tenantConfig;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->evaluationRepo = new InMemoryEvaluationRepository();
|
||||
$this->gradeRepo = new InMemoryGradeRepository();
|
||||
$this->evalStatsRepo = new InMemoryEvaluationStatisticsRepository();
|
||||
$this->studentAvgRepo = new InMemoryStudentAverageRepository();
|
||||
|
||||
$this->tenantConfig = new TenantConfig(
|
||||
tenantId: InfraTenantId::fromString(self::TENANT_ID),
|
||||
subdomain: 'ecole-test',
|
||||
databaseUrl: 'postgresql://test',
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itBackfillsStatisticsAndAveragesForPublishedEvaluations(): void
|
||||
{
|
||||
$this->seedPublishedEvaluationWithGrades();
|
||||
|
||||
$tester = $this->executeCommand();
|
||||
|
||||
self::assertSame(0, $tester->getStatusCode(), $tester->getDisplay());
|
||||
self::assertStringContainsString('1 évaluation(s) publiée(s)', $tester->getDisplay());
|
||||
self::assertStringContainsString('1 évaluation(s) traitée(s) avec succès', $tester->getDisplay());
|
||||
|
||||
// Vérifier que les stats évaluation sont créées
|
||||
$evaluations = $this->evaluationRepo->findAllWithPublishedGrades(TenantId::fromString(self::TENANT_ID));
|
||||
$stats = $this->evalStatsRepo->findByEvaluation($evaluations[0]->id);
|
||||
|
||||
self::assertNotNull($stats);
|
||||
self::assertSame(2, $stats->gradedCount);
|
||||
|
||||
// Vérifier que la moyenne matière est créée
|
||||
$subjectAvg = $this->studentAvgRepo->findSubjectAverage(
|
||||
UserId::fromString(self::STUDENT_ID),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNotNull($subjectAvg);
|
||||
self::assertSame(14.0, $subjectAvg['average']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itReportsSuccessWhenNoPublishedEvaluations(): void
|
||||
{
|
||||
$tester = $this->executeCommand();
|
||||
|
||||
self::assertSame(0, $tester->getStatusCode());
|
||||
self::assertStringContainsString('aucune évaluation publiée', $tester->getDisplay());
|
||||
self::assertStringContainsString('0 évaluation(s) traitée(s) avec succès', $tester->getDisplay());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itIgnoresUnpublishedEvaluations(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$now = new DateTimeImmutable();
|
||||
|
||||
// Évaluation NON publiée
|
||||
$evaluation = Evaluation::creer(
|
||||
tenantId: $tenantId,
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString(self::SUBJECT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Non publiée',
|
||||
description: null,
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
gradeScale: new GradeScale(20),
|
||||
coefficient: new Coefficient(1.0),
|
||||
now: $now,
|
||||
);
|
||||
$evaluation->pullDomainEvents();
|
||||
$this->evaluationRepo->save($evaluation);
|
||||
|
||||
$tester = $this->executeCommand();
|
||||
|
||||
self::assertSame(0, $tester->getStatusCode());
|
||||
self::assertStringContainsString('aucune évaluation publiée', $tester->getDisplay());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itProcessesMultipleEvaluations(): void
|
||||
{
|
||||
$this->seedPublishedEvaluationWithGrades();
|
||||
$this->seedPublishedEvaluationWithGrades(coefficient: 2.0);
|
||||
|
||||
$tester = $this->executeCommand();
|
||||
|
||||
self::assertSame(0, $tester->getStatusCode());
|
||||
self::assertStringContainsString('2 évaluation(s) publiée(s)', $tester->getDisplay());
|
||||
self::assertStringContainsString('2 évaluation(s) traitée(s) avec succès', $tester->getDisplay());
|
||||
}
|
||||
|
||||
private function seedPublishedEvaluationWithGrades(float $coefficient = 1.0): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$now = new DateTimeImmutable();
|
||||
|
||||
$evaluation = Evaluation::creer(
|
||||
tenantId: $tenantId,
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString(self::SUBJECT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Contrôle',
|
||||
description: null,
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
gradeScale: new GradeScale(20),
|
||||
coefficient: new Coefficient($coefficient),
|
||||
now: $now,
|
||||
);
|
||||
$evaluation->publierNotes($now);
|
||||
$evaluation->pullDomainEvents();
|
||||
$this->evaluationRepo->save($evaluation);
|
||||
|
||||
$grade1 = Grade::saisir(
|
||||
tenantId: $tenantId,
|
||||
evaluationId: $evaluation->id,
|
||||
studentId: UserId::fromString(self::STUDENT_ID),
|
||||
value: new GradeValue(14.0),
|
||||
status: GradeStatus::GRADED,
|
||||
gradeScale: new GradeScale(20),
|
||||
createdBy: UserId::fromString(self::TEACHER_ID),
|
||||
now: $now,
|
||||
);
|
||||
$grade1->pullDomainEvents();
|
||||
$this->gradeRepo->save($grade1);
|
||||
|
||||
$grade2 = Grade::saisir(
|
||||
tenantId: $tenantId,
|
||||
evaluationId: $evaluation->id,
|
||||
studentId: UserId::fromString('33333333-3333-3333-3333-333333333333'),
|
||||
value: new GradeValue(10.0),
|
||||
status: GradeStatus::GRADED,
|
||||
gradeScale: new GradeScale(20),
|
||||
createdBy: UserId::fromString(self::TEACHER_ID),
|
||||
now: $now,
|
||||
);
|
||||
$grade2->pullDomainEvents();
|
||||
$this->gradeRepo->save($grade2);
|
||||
}
|
||||
|
||||
private function executeCommand(): CommandTester
|
||||
{
|
||||
$tenantContext = new TenantContext();
|
||||
|
||||
$periodFinder = new class implements PeriodFinder {
|
||||
public function findForDate(DateTimeImmutable $date, TenantId $tenantId): ?PeriodInfo
|
||||
{
|
||||
return new PeriodInfo(
|
||||
periodId: RecalculerToutesMoyennesCommandTest::PERIOD_ID,
|
||||
startDate: new DateTimeImmutable('2026-01-01'),
|
||||
endDate: new DateTimeImmutable('2026-03-31'),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
$service = new RecalculerMoyennesService(
|
||||
evaluationRepository: $this->evaluationRepo,
|
||||
gradeRepository: $this->gradeRepo,
|
||||
evaluationStatisticsRepository: $this->evalStatsRepo,
|
||||
studentAverageRepository: $this->studentAvgRepo,
|
||||
periodFinder: $periodFinder,
|
||||
calculator: new AverageCalculator(),
|
||||
);
|
||||
|
||||
$tenantRegistry = new class($this->tenantConfig) implements TenantRegistry {
|
||||
public function __construct(private readonly TenantConfig $config)
|
||||
{
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function getConfig(InfraTenantId $tenantId): TenantConfig
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function getBySubdomain(string $subdomain): TenantConfig
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function exists(string $subdomain): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function getAllConfigs(): array
|
||||
{
|
||||
return [$this->config];
|
||||
}
|
||||
};
|
||||
|
||||
$databaseSwitcher = new class implements TenantDatabaseSwitcher {
|
||||
#[Override]
|
||||
public function useTenantDatabase(string $databaseUrl): void
|
||||
{
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function useDefaultDatabase(): void
|
||||
{
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function currentDatabaseUrl(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
$command = new RecalculerToutesMoyennesCommand(
|
||||
evaluationRepository: $this->evaluationRepo,
|
||||
tenantRegistry: $tenantRegistry,
|
||||
tenantContext: $tenantContext,
|
||||
databaseSwitcher: $databaseSwitcher,
|
||||
service: $service,
|
||||
);
|
||||
|
||||
$tester = new CommandTester($command);
|
||||
$tester->execute([]);
|
||||
|
||||
return $tester;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,302 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Infrastructure\EventHandler;
|
||||
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassId;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Port\PeriodFinder;
|
||||
use App\Scolarite\Application\Port\PeriodInfo;
|
||||
use App\Scolarite\Application\Service\RecalculerMoyennesService;
|
||||
use App\Scolarite\Domain\Event\EvaluationModifiee;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Coefficient;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Evaluation;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
|
||||
use App\Scolarite\Domain\Model\Grade\Grade;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeStatus;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeValue;
|
||||
use App\Scolarite\Domain\Service\AverageCalculator;
|
||||
use App\Scolarite\Infrastructure\EventHandler\RecalculerMoyennesOnEvaluationModifieeHandler;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationStatisticsRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryGradeRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryStudentAverageRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use App\Shared\Infrastructure\Tenant\TenantConfig;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RecalculerMoyennesOnEvaluationModifieeHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
|
||||
public const string PERIOD_ID = '11111111-1111-1111-1111-111111111111';
|
||||
private const string STUDENT_1 = '22222222-2222-2222-2222-222222222222';
|
||||
private const string STUDENT_2 = '33333333-3333-3333-3333-333333333333';
|
||||
private const string TEACHER_ID = '44444444-4444-4444-4444-444444444444';
|
||||
private const string CLASS_ID = '55555555-5555-5555-5555-555555555555';
|
||||
private const string SUBJECT_ID = '66666666-6666-6666-6666-666666666666';
|
||||
|
||||
private InMemoryEvaluationRepository $evaluationRepo;
|
||||
private InMemoryGradeRepository $gradeRepo;
|
||||
private InMemoryEvaluationStatisticsRepository $evalStatsRepo;
|
||||
private InMemoryStudentAverageRepository $studentAvgRepo;
|
||||
private RecalculerMoyennesOnEvaluationModifieeHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->evaluationRepo = new InMemoryEvaluationRepository();
|
||||
$this->gradeRepo = new InMemoryGradeRepository();
|
||||
$this->evalStatsRepo = new InMemoryEvaluationStatisticsRepository();
|
||||
$this->studentAvgRepo = new InMemoryStudentAverageRepository();
|
||||
|
||||
$tenantContext = new TenantContext();
|
||||
$tenantContext->setCurrentTenant(new TenantConfig(
|
||||
tenantId: InfraTenantId::fromString(self::TENANT_ID),
|
||||
subdomain: 'test',
|
||||
databaseUrl: 'postgresql://test',
|
||||
));
|
||||
|
||||
$periodFinder = new class implements PeriodFinder {
|
||||
public function findForDate(DateTimeImmutable $date, TenantId $tenantId): ?PeriodInfo
|
||||
{
|
||||
return new PeriodInfo(
|
||||
periodId: RecalculerMoyennesOnEvaluationModifieeHandlerTest::PERIOD_ID,
|
||||
startDate: new DateTimeImmutable('2026-01-01'),
|
||||
endDate: new DateTimeImmutable('2026-03-31'),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
$service = new RecalculerMoyennesService(
|
||||
evaluationRepository: $this->evaluationRepo,
|
||||
gradeRepository: $this->gradeRepo,
|
||||
evaluationStatisticsRepository: $this->evalStatsRepo,
|
||||
studentAverageRepository: $this->studentAvgRepo,
|
||||
periodFinder: $periodFinder,
|
||||
calculator: new AverageCalculator(),
|
||||
);
|
||||
|
||||
$this->handler = new RecalculerMoyennesOnEvaluationModifieeHandler(
|
||||
tenantContext: $tenantContext,
|
||||
service: $service,
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itRecalculatesStatisticsWhenEvaluationModified(): void
|
||||
{
|
||||
$evaluationId = $this->seedPublishedEvaluationWithGrades(
|
||||
grades: [
|
||||
[self::STUDENT_1, 14.0, GradeStatus::GRADED],
|
||||
[self::STUDENT_2, 8.0, GradeStatus::GRADED],
|
||||
],
|
||||
);
|
||||
|
||||
($this->handler)(new EvaluationModifiee(
|
||||
evaluationId: $evaluationId,
|
||||
title: 'Titre modifié',
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
$stats = $this->evalStatsRepo->findByEvaluation($evaluationId);
|
||||
|
||||
self::assertNotNull($stats);
|
||||
self::assertSame(11.0, $stats->average);
|
||||
self::assertSame(8.0, $stats->min);
|
||||
self::assertSame(14.0, $stats->max);
|
||||
self::assertSame(2, $stats->gradedCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itRecalculatesStudentAveragesWhenEvaluationModified(): void
|
||||
{
|
||||
$evaluationId = $this->seedPublishedEvaluationWithGrades(
|
||||
grades: [
|
||||
[self::STUDENT_1, 16.0, GradeStatus::GRADED],
|
||||
[self::STUDENT_2, 12.0, GradeStatus::GRADED],
|
||||
],
|
||||
);
|
||||
|
||||
($this->handler)(new EvaluationModifiee(
|
||||
evaluationId: $evaluationId,
|
||||
title: 'Titre modifié',
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
$student1Avg = $this->studentAvgRepo->findSubjectAverage(
|
||||
UserId::fromString(self::STUDENT_1),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNotNull($student1Avg);
|
||||
self::assertSame(16.0, $student1Avg['average']);
|
||||
|
||||
$student2Avg = $this->studentAvgRepo->findSubjectAverage(
|
||||
UserId::fromString(self::STUDENT_2),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNotNull($student2Avg);
|
||||
self::assertSame(12.0, $student2Avg['average']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itRecalculatesGeneralAverageForAllStudents(): void
|
||||
{
|
||||
$evaluationId = $this->seedPublishedEvaluationWithGrades(
|
||||
grades: [
|
||||
[self::STUDENT_1, 14.0, GradeStatus::GRADED],
|
||||
],
|
||||
);
|
||||
|
||||
($this->handler)(new EvaluationModifiee(
|
||||
evaluationId: $evaluationId,
|
||||
title: 'Titre modifié',
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
$generalAvg = $this->studentAvgRepo->findGeneralAverageForStudent(
|
||||
UserId::fromString(self::STUDENT_1),
|
||||
self::PERIOD_ID,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertSame(14.0, $generalAvg);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itRecalculatesStatsButNotStudentAveragesWhenNotPublished(): void
|
||||
{
|
||||
$evaluationId = $this->seedUnpublishedEvaluationWithGrades(
|
||||
grades: [
|
||||
[self::STUDENT_1, 14.0, GradeStatus::GRADED],
|
||||
],
|
||||
);
|
||||
|
||||
($this->handler)(new EvaluationModifiee(
|
||||
evaluationId: $evaluationId,
|
||||
title: 'Titre modifié',
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
// Les stats sont calculées (le handler ne filtre pas sur publication)
|
||||
$stats = $this->evalStatsRepo->findByEvaluation($evaluationId);
|
||||
self::assertNotNull($stats);
|
||||
self::assertSame(14.0, $stats->average);
|
||||
|
||||
// Mais pas de recalcul des moyennes élèves (recalculerTousEleves filtre)
|
||||
self::assertNull($this->studentAvgRepo->findSubjectAverage(
|
||||
UserId::fromString(self::STUDENT_1),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itExcludesAbsentStudentsFromStatistics(): void
|
||||
{
|
||||
$evaluationId = $this->seedPublishedEvaluationWithGrades(
|
||||
grades: [
|
||||
[self::STUDENT_1, 18.0, GradeStatus::GRADED],
|
||||
[self::STUDENT_2, null, GradeStatus::ABSENT],
|
||||
],
|
||||
);
|
||||
|
||||
($this->handler)(new EvaluationModifiee(
|
||||
evaluationId: $evaluationId,
|
||||
title: 'Titre modifié',
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
$stats = $this->evalStatsRepo->findByEvaluation($evaluationId);
|
||||
|
||||
self::assertNotNull($stats);
|
||||
self::assertSame(18.0, $stats->average);
|
||||
self::assertSame(1, $stats->gradedCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{0: string, 1: float|null, 2: GradeStatus}> $grades
|
||||
*/
|
||||
private function seedPublishedEvaluationWithGrades(
|
||||
array $grades,
|
||||
float $coefficient = 1.0,
|
||||
): EvaluationId {
|
||||
return $this->seedEvaluationWithGrades($grades, $coefficient, published: true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{0: string, 1: float|null, 2: GradeStatus}> $grades
|
||||
*/
|
||||
private function seedUnpublishedEvaluationWithGrades(
|
||||
array $grades,
|
||||
float $coefficient = 1.0,
|
||||
): EvaluationId {
|
||||
return $this->seedEvaluationWithGrades($grades, $coefficient, published: false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{0: string, 1: float|null, 2: GradeStatus}> $grades
|
||||
*/
|
||||
private function seedEvaluationWithGrades(
|
||||
array $grades,
|
||||
float $coefficient = 1.0,
|
||||
bool $published = true,
|
||||
): EvaluationId {
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$now = new DateTimeImmutable();
|
||||
|
||||
$evaluation = Evaluation::creer(
|
||||
tenantId: $tenantId,
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString(self::SUBJECT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Test Evaluation',
|
||||
description: null,
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
gradeScale: new GradeScale(20),
|
||||
coefficient: new Coefficient($coefficient),
|
||||
now: $now,
|
||||
);
|
||||
|
||||
if ($published) {
|
||||
$evaluation->publierNotes($now);
|
||||
}
|
||||
|
||||
$evaluation->pullDomainEvents();
|
||||
$this->evaluationRepo->save($evaluation);
|
||||
|
||||
foreach ($grades as [$studentId, $value, $status]) {
|
||||
$grade = Grade::saisir(
|
||||
tenantId: $tenantId,
|
||||
evaluationId: $evaluation->id,
|
||||
studentId: UserId::fromString($studentId),
|
||||
value: $value !== null ? new GradeValue($value) : null,
|
||||
status: $status,
|
||||
gradeScale: new GradeScale(20),
|
||||
createdBy: UserId::fromString(self::TEACHER_ID),
|
||||
now: $now,
|
||||
);
|
||||
$grade->pullDomainEvents();
|
||||
$this->gradeRepo->save($grade);
|
||||
}
|
||||
|
||||
return $evaluation->id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,342 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Infrastructure\EventHandler;
|
||||
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassId;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Port\PeriodFinder;
|
||||
use App\Scolarite\Application\Port\PeriodInfo;
|
||||
use App\Scolarite\Application\Service\RecalculerMoyennesService;
|
||||
use App\Scolarite\Domain\Event\EvaluationSupprimee;
|
||||
use App\Scolarite\Domain\Model\Evaluation\ClassStatistics;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Coefficient;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Evaluation;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
|
||||
use App\Scolarite\Domain\Model\Grade\Grade;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeStatus;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeValue;
|
||||
use App\Scolarite\Domain\Service\AverageCalculator;
|
||||
use App\Scolarite\Infrastructure\EventHandler\RecalculerMoyennesOnEvaluationSupprimeeHandler;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationStatisticsRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryGradeRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryStudentAverageRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use App\Shared\Infrastructure\Tenant\TenantConfig;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RecalculerMoyennesOnEvaluationSupprimeeHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
|
||||
public const string PERIOD_ID = '11111111-1111-1111-1111-111111111111';
|
||||
private const string STUDENT_1 = '22222222-2222-2222-2222-222222222222';
|
||||
private const string STUDENT_2 = '33333333-3333-3333-3333-333333333333';
|
||||
private const string TEACHER_ID = '44444444-4444-4444-4444-444444444444';
|
||||
private const string CLASS_ID = '55555555-5555-5555-5555-555555555555';
|
||||
private const string SUBJECT_ID = '66666666-6666-6666-6666-666666666666';
|
||||
|
||||
private InMemoryEvaluationRepository $evaluationRepo;
|
||||
private InMemoryGradeRepository $gradeRepo;
|
||||
private InMemoryEvaluationStatisticsRepository $evalStatsRepo;
|
||||
private InMemoryStudentAverageRepository $studentAvgRepo;
|
||||
private RecalculerMoyennesOnEvaluationSupprimeeHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->evaluationRepo = new InMemoryEvaluationRepository();
|
||||
$this->gradeRepo = new InMemoryGradeRepository();
|
||||
$this->evalStatsRepo = new InMemoryEvaluationStatisticsRepository();
|
||||
$this->studentAvgRepo = new InMemoryStudentAverageRepository();
|
||||
|
||||
$tenantContext = new TenantContext();
|
||||
$tenantContext->setCurrentTenant(new TenantConfig(
|
||||
tenantId: InfraTenantId::fromString(self::TENANT_ID),
|
||||
subdomain: 'test',
|
||||
databaseUrl: 'postgresql://test',
|
||||
));
|
||||
|
||||
$periodFinder = new class implements PeriodFinder {
|
||||
public function findForDate(DateTimeImmutable $date, TenantId $tenantId): ?PeriodInfo
|
||||
{
|
||||
return new PeriodInfo(
|
||||
periodId: RecalculerMoyennesOnEvaluationSupprimeeHandlerTest::PERIOD_ID,
|
||||
startDate: new DateTimeImmutable('2026-01-01'),
|
||||
endDate: new DateTimeImmutable('2026-03-31'),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
$service = new RecalculerMoyennesService(
|
||||
evaluationRepository: $this->evaluationRepo,
|
||||
gradeRepository: $this->gradeRepo,
|
||||
evaluationStatisticsRepository: $this->evalStatsRepo,
|
||||
studentAverageRepository: $this->studentAvgRepo,
|
||||
periodFinder: $periodFinder,
|
||||
calculator: new AverageCalculator(),
|
||||
);
|
||||
|
||||
$this->handler = new RecalculerMoyennesOnEvaluationSupprimeeHandler(
|
||||
tenantContext: $tenantContext,
|
||||
evaluationRepository: $this->evaluationRepo,
|
||||
gradeRepository: $this->gradeRepo,
|
||||
evaluationStatisticsRepository: $this->evalStatsRepo,
|
||||
periodFinder: $periodFinder,
|
||||
service: $service,
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itDeletesEvaluationStatisticsOnDeletion(): void
|
||||
{
|
||||
$evaluationId = $this->seedPublishedEvaluationWithGrades(
|
||||
grades: [
|
||||
[self::STUDENT_1, 14.0, GradeStatus::GRADED],
|
||||
[self::STUDENT_2, 10.0, GradeStatus::GRADED],
|
||||
],
|
||||
);
|
||||
|
||||
// Pré-remplir les stats
|
||||
$this->evalStatsRepo->save($evaluationId, new ClassStatistics(
|
||||
average: 12.0,
|
||||
min: 10.0,
|
||||
max: 14.0,
|
||||
median: 12.0,
|
||||
gradedCount: 2,
|
||||
));
|
||||
|
||||
self::assertNotNull($this->evalStatsRepo->findByEvaluation($evaluationId));
|
||||
|
||||
($this->handler)(new EvaluationSupprimee(
|
||||
evaluationId: $evaluationId,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
self::assertNull($this->evalStatsRepo->findByEvaluation($evaluationId));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itRecalculatesStudentAveragesAfterDeletion(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
|
||||
// Première évaluation (sera supprimée)
|
||||
$evalToDelete = $this->seedPublishedEvaluationWithGrades(
|
||||
grades: [
|
||||
[self::STUDENT_1, 10.0, GradeStatus::GRADED],
|
||||
],
|
||||
);
|
||||
|
||||
// Deuxième évaluation (reste)
|
||||
$evalRemaining = $this->seedPublishedEvaluationWithGrades(
|
||||
grades: [
|
||||
[self::STUDENT_1, 18.0, GradeStatus::GRADED],
|
||||
],
|
||||
);
|
||||
|
||||
// Pré-remplir les moyennes (comme si les deux évaluations comptaient)
|
||||
$this->studentAvgRepo->saveSubjectAverage(
|
||||
$tenantId,
|
||||
UserId::fromString(self::STUDENT_1),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
14.0, // (10+18)/2
|
||||
2,
|
||||
);
|
||||
|
||||
// Supprimer la première évaluation (status DELETED mais encore accessible)
|
||||
$evaluation = $this->evaluationRepo->findById($evalToDelete, $tenantId);
|
||||
$evaluation->supprimer(new DateTimeImmutable());
|
||||
$evaluation->pullDomainEvents();
|
||||
$this->evaluationRepo->save($evaluation);
|
||||
|
||||
($this->handler)(new EvaluationSupprimee(
|
||||
evaluationId: $evalToDelete,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
// La moyenne doit être recalculée sans l'évaluation supprimée
|
||||
$subjectAvg = $this->studentAvgRepo->findSubjectAverage(
|
||||
UserId::fromString(self::STUDENT_1),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
$tenantId,
|
||||
);
|
||||
|
||||
self::assertNotNull($subjectAvg);
|
||||
self::assertSame(18.0, $subjectAvg['average']);
|
||||
self::assertSame(1, $subjectAvg['gradeCount']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itDoesNothingWhenEvaluationNotFound(): void
|
||||
{
|
||||
$unknownId = EvaluationId::generate();
|
||||
|
||||
($this->handler)(new EvaluationSupprimee(
|
||||
evaluationId: $unknownId,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
self::assertNull($this->evalStatsRepo->findByEvaluation($unknownId));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itOnlyDeletesStatsWhenGradesWereNotPublished(): void
|
||||
{
|
||||
$evaluationId = $this->seedUnpublishedEvaluationWithGrades(
|
||||
grades: [
|
||||
[self::STUDENT_1, 14.0, GradeStatus::GRADED],
|
||||
],
|
||||
);
|
||||
|
||||
// Pré-remplir des stats (cas hypothétique)
|
||||
$this->evalStatsRepo->save($evaluationId, new ClassStatistics(
|
||||
average: 14.0,
|
||||
min: 14.0,
|
||||
max: 14.0,
|
||||
median: 14.0,
|
||||
gradedCount: 1,
|
||||
));
|
||||
|
||||
($this->handler)(new EvaluationSupprimee(
|
||||
evaluationId: $evaluationId,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
// Stats supprimées
|
||||
self::assertNull($this->evalStatsRepo->findByEvaluation($evaluationId));
|
||||
|
||||
// Pas de recalcul de moyennes élèves (notes non publiées)
|
||||
self::assertNull($this->studentAvgRepo->findSubjectAverage(
|
||||
UserId::fromString(self::STUDENT_1),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itRecalculatesGeneralAverageAfterDeletion(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
|
||||
$evaluationId = $this->seedPublishedEvaluationWithGrades(
|
||||
grades: [
|
||||
[self::STUDENT_1, 14.0, GradeStatus::GRADED],
|
||||
],
|
||||
);
|
||||
|
||||
// Pré-remplir
|
||||
$this->studentAvgRepo->saveSubjectAverage(
|
||||
$tenantId,
|
||||
UserId::fromString(self::STUDENT_1),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
14.0,
|
||||
1,
|
||||
);
|
||||
$this->studentAvgRepo->saveGeneralAverage(
|
||||
$tenantId,
|
||||
UserId::fromString(self::STUDENT_1),
|
||||
self::PERIOD_ID,
|
||||
14.0,
|
||||
);
|
||||
|
||||
// Supprimer l'évaluation
|
||||
$evaluation = $this->evaluationRepo->findById($evaluationId, $tenantId);
|
||||
$evaluation->supprimer(new DateTimeImmutable());
|
||||
$evaluation->pullDomainEvents();
|
||||
$this->evaluationRepo->save($evaluation);
|
||||
|
||||
($this->handler)(new EvaluationSupprimee(
|
||||
evaluationId: $evaluationId,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
// Plus aucune note publiée → moyennes supprimées
|
||||
$generalAvg = $this->studentAvgRepo->findGeneralAverageForStudent(
|
||||
UserId::fromString(self::STUDENT_1),
|
||||
self::PERIOD_ID,
|
||||
$tenantId,
|
||||
);
|
||||
|
||||
self::assertNull($generalAvg);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{0: string, 1: float|null, 2: GradeStatus}> $grades
|
||||
*/
|
||||
private function seedPublishedEvaluationWithGrades(
|
||||
array $grades,
|
||||
float $coefficient = 1.0,
|
||||
): EvaluationId {
|
||||
return $this->seedEvaluationWithGrades($grades, $coefficient, published: true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{0: string, 1: float|null, 2: GradeStatus}> $grades
|
||||
*/
|
||||
private function seedUnpublishedEvaluationWithGrades(
|
||||
array $grades,
|
||||
float $coefficient = 1.0,
|
||||
): EvaluationId {
|
||||
return $this->seedEvaluationWithGrades($grades, $coefficient, published: false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{0: string, 1: float|null, 2: GradeStatus}> $grades
|
||||
*/
|
||||
private function seedEvaluationWithGrades(
|
||||
array $grades,
|
||||
float $coefficient = 1.0,
|
||||
bool $published = true,
|
||||
): EvaluationId {
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$now = new DateTimeImmutable();
|
||||
|
||||
$evaluation = Evaluation::creer(
|
||||
tenantId: $tenantId,
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString(self::SUBJECT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Test Evaluation',
|
||||
description: null,
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
gradeScale: new GradeScale(20),
|
||||
coefficient: new Coefficient($coefficient),
|
||||
now: $now,
|
||||
);
|
||||
|
||||
if ($published) {
|
||||
$evaluation->publierNotes($now);
|
||||
}
|
||||
|
||||
$evaluation->pullDomainEvents();
|
||||
$this->evaluationRepo->save($evaluation);
|
||||
|
||||
foreach ($grades as [$studentId, $value, $status]) {
|
||||
$grade = Grade::saisir(
|
||||
tenantId: $tenantId,
|
||||
evaluationId: $evaluation->id,
|
||||
studentId: UserId::fromString($studentId),
|
||||
value: $value !== null ? new GradeValue($value) : null,
|
||||
status: $status,
|
||||
gradeScale: new GradeScale(20),
|
||||
createdBy: UserId::fromString(self::TEACHER_ID),
|
||||
now: $now,
|
||||
);
|
||||
$grade->pullDomainEvents();
|
||||
$this->gradeRepo->save($grade);
|
||||
}
|
||||
|
||||
return $evaluation->id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Infrastructure\EventHandler;
|
||||
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassId;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Port\PeriodFinder;
|
||||
use App\Scolarite\Application\Port\PeriodInfo;
|
||||
use App\Scolarite\Application\Service\RecalculerMoyennesService;
|
||||
use App\Scolarite\Domain\Event\NoteModifiee;
|
||||
use App\Scolarite\Domain\Event\NoteSaisie;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Coefficient;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Evaluation;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
|
||||
use App\Scolarite\Domain\Model\Grade\Grade;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeId;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeStatus;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeValue;
|
||||
use App\Scolarite\Domain\Service\AverageCalculator;
|
||||
use App\Scolarite\Infrastructure\EventHandler\RecalculerMoyennesOnNoteModifieeHandler;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationStatisticsRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryGradeRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryStudentAverageRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use App\Shared\Infrastructure\Tenant\TenantConfig;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RecalculerMoyennesOnNoteModifieeHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
|
||||
public const string PERIOD_ID = '11111111-1111-1111-1111-111111111111';
|
||||
private const string STUDENT_ID = '22222222-2222-2222-2222-222222222222';
|
||||
private const string TEACHER_ID = '44444444-4444-4444-4444-444444444444';
|
||||
private const string CLASS_ID = '55555555-5555-5555-5555-555555555555';
|
||||
private const string SUBJECT_ID = '66666666-6666-6666-6666-666666666666';
|
||||
|
||||
private InMemoryEvaluationRepository $evaluationRepo;
|
||||
private InMemoryGradeRepository $gradeRepo;
|
||||
private InMemoryEvaluationStatisticsRepository $evalStatsRepo;
|
||||
private InMemoryStudentAverageRepository $studentAvgRepo;
|
||||
private RecalculerMoyennesOnNoteModifieeHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->evaluationRepo = new InMemoryEvaluationRepository();
|
||||
$this->gradeRepo = new InMemoryGradeRepository();
|
||||
$this->evalStatsRepo = new InMemoryEvaluationStatisticsRepository();
|
||||
$this->studentAvgRepo = new InMemoryStudentAverageRepository();
|
||||
|
||||
$tenantContext = new TenantContext();
|
||||
$tenantContext->setCurrentTenant(new TenantConfig(
|
||||
tenantId: InfraTenantId::fromString(self::TENANT_ID),
|
||||
subdomain: 'test',
|
||||
databaseUrl: 'postgresql://test',
|
||||
));
|
||||
|
||||
$periodFinder = new class implements PeriodFinder {
|
||||
public function findForDate(DateTimeImmutable $date, TenantId $tenantId): ?PeriodInfo
|
||||
{
|
||||
return new PeriodInfo(
|
||||
periodId: RecalculerMoyennesOnNoteModifieeHandlerTest::PERIOD_ID,
|
||||
startDate: new DateTimeImmutable('2026-01-01'),
|
||||
endDate: new DateTimeImmutable('2026-03-31'),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
$service = new RecalculerMoyennesService(
|
||||
evaluationRepository: $this->evaluationRepo,
|
||||
gradeRepository: $this->gradeRepo,
|
||||
evaluationStatisticsRepository: $this->evalStatsRepo,
|
||||
studentAverageRepository: $this->studentAvgRepo,
|
||||
periodFinder: $periodFinder,
|
||||
calculator: new AverageCalculator(),
|
||||
);
|
||||
|
||||
$this->handler = new RecalculerMoyennesOnNoteModifieeHandler(
|
||||
tenantContext: $tenantContext,
|
||||
evaluationRepository: $this->evaluationRepo,
|
||||
gradeRepository: $this->gradeRepo,
|
||||
periodFinder: $periodFinder,
|
||||
service: $service,
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itRecalculatesStatisticsWhenGradeModifiedAfterPublication(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$now = new DateTimeImmutable();
|
||||
|
||||
$evaluation = $this->seedPublishedEvaluation();
|
||||
|
||||
// Deux notes initiales
|
||||
$grade1 = $this->seedGrade($evaluation->id, self::STUDENT_ID, 14.0, GradeStatus::GRADED);
|
||||
$this->seedGrade($evaluation->id, '77777777-7777-7777-7777-777777777777', 10.0, GradeStatus::GRADED);
|
||||
|
||||
// Simuler modification de la note
|
||||
$grade1->modifier(
|
||||
value: new GradeValue(18.0),
|
||||
status: GradeStatus::GRADED,
|
||||
gradeScale: new GradeScale(20),
|
||||
modifiedBy: UserId::fromString(self::TEACHER_ID),
|
||||
now: $now,
|
||||
);
|
||||
$grade1->pullDomainEvents();
|
||||
$this->gradeRepo->save($grade1);
|
||||
|
||||
$event = new NoteModifiee(
|
||||
gradeId: $grade1->id,
|
||||
evaluationId: (string) $evaluation->id,
|
||||
oldValue: 14.0,
|
||||
newValue: 18.0,
|
||||
oldStatus: 'graded',
|
||||
newStatus: 'graded',
|
||||
modifiedBy: self::TEACHER_ID,
|
||||
occurredOn: $now,
|
||||
);
|
||||
|
||||
($this->handler)($event);
|
||||
|
||||
// Statistiques recalculées
|
||||
$stats = $this->evalStatsRepo->findByEvaluation($evaluation->id);
|
||||
self::assertNotNull($stats);
|
||||
self::assertSame(14.0, $stats->average); // (18+10)/2
|
||||
self::assertSame(10.0, $stats->min);
|
||||
self::assertSame(18.0, $stats->max);
|
||||
|
||||
// Moyenne matière recalculée pour l'élève
|
||||
$subjectAvg = $this->studentAvgRepo->findSubjectAverage(
|
||||
UserId::fromString(self::STUDENT_ID),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
$tenantId,
|
||||
);
|
||||
|
||||
self::assertNotNull($subjectAvg);
|
||||
self::assertSame(18.0, $subjectAvg['average']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itDoesNothingWhenGradesNotYetPublished(): void
|
||||
{
|
||||
$now = new DateTimeImmutable();
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
|
||||
// Évaluation NON publiée
|
||||
$evaluation = Evaluation::creer(
|
||||
tenantId: $tenantId,
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString(self::SUBJECT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Test',
|
||||
description: null,
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
gradeScale: new GradeScale(20),
|
||||
coefficient: new Coefficient(1.0),
|
||||
now: $now,
|
||||
);
|
||||
$evaluation->pullDomainEvents();
|
||||
$this->evaluationRepo->save($evaluation);
|
||||
|
||||
$grade = $this->seedGrade($evaluation->id, self::STUDENT_ID, 14.0, GradeStatus::GRADED);
|
||||
|
||||
$event = new NoteModifiee(
|
||||
gradeId: $grade->id,
|
||||
evaluationId: (string) $evaluation->id,
|
||||
oldValue: 10.0,
|
||||
newValue: 14.0,
|
||||
oldStatus: 'graded',
|
||||
newStatus: 'graded',
|
||||
modifiedBy: self::TEACHER_ID,
|
||||
occurredOn: $now,
|
||||
);
|
||||
|
||||
($this->handler)($event);
|
||||
|
||||
self::assertNull($this->evalStatsRepo->findByEvaluation($evaluation->id));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itRecalculatesOnNoteSaisieWhenAlreadyPublished(): void
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$now = new DateTimeImmutable();
|
||||
|
||||
$evaluation = $this->seedPublishedEvaluation();
|
||||
|
||||
$grade = $this->seedGrade($evaluation->id, self::STUDENT_ID, 16.0, GradeStatus::GRADED);
|
||||
|
||||
$event = new NoteSaisie(
|
||||
gradeId: $grade->id,
|
||||
evaluationId: (string) $evaluation->id,
|
||||
studentId: self::STUDENT_ID,
|
||||
value: 16.0,
|
||||
status: 'graded',
|
||||
createdBy: self::TEACHER_ID,
|
||||
occurredOn: $now,
|
||||
);
|
||||
|
||||
($this->handler)($event);
|
||||
|
||||
$stats = $this->evalStatsRepo->findByEvaluation($evaluation->id);
|
||||
self::assertNotNull($stats);
|
||||
self::assertSame(16.0, $stats->average);
|
||||
|
||||
$subjectAvg = $this->studentAvgRepo->findSubjectAverage(
|
||||
UserId::fromString(self::STUDENT_ID),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
$tenantId,
|
||||
);
|
||||
|
||||
self::assertNotNull($subjectAvg);
|
||||
self::assertSame(16.0, $subjectAvg['average']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itDoesNothingWhenGradeNotFound(): void
|
||||
{
|
||||
$now = new DateTimeImmutable();
|
||||
$evaluation = $this->seedPublishedEvaluation();
|
||||
|
||||
$event = new NoteModifiee(
|
||||
gradeId: GradeId::generate(),
|
||||
evaluationId: (string) $evaluation->id,
|
||||
oldValue: 10.0,
|
||||
newValue: 14.0,
|
||||
oldStatus: 'graded',
|
||||
newStatus: 'graded',
|
||||
modifiedBy: self::TEACHER_ID,
|
||||
occurredOn: $now,
|
||||
);
|
||||
|
||||
($this->handler)($event);
|
||||
|
||||
// Les stats sont recalculées (car l'évaluation est publiée),
|
||||
// mais pas de moyenne élève (grade introuvable)
|
||||
$subjectAvg = $this->studentAvgRepo->findSubjectAverage(
|
||||
UserId::fromString(self::STUDENT_ID),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNull($subjectAvg);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itDoesNothingWhenEvaluationNotFound(): void
|
||||
{
|
||||
$now = new DateTimeImmutable();
|
||||
$unknownEvalId = EvaluationId::generate();
|
||||
|
||||
$event = new NoteModifiee(
|
||||
gradeId: GradeId::generate(),
|
||||
evaluationId: (string) $unknownEvalId,
|
||||
oldValue: 10.0,
|
||||
newValue: 14.0,
|
||||
oldStatus: 'graded',
|
||||
newStatus: 'graded',
|
||||
modifiedBy: self::TEACHER_ID,
|
||||
occurredOn: $now,
|
||||
);
|
||||
|
||||
($this->handler)($event);
|
||||
|
||||
self::assertNull($this->evalStatsRepo->findByEvaluation($unknownEvalId));
|
||||
}
|
||||
|
||||
private function seedPublishedEvaluation(): Evaluation
|
||||
{
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$now = new DateTimeImmutable();
|
||||
|
||||
$evaluation = Evaluation::creer(
|
||||
tenantId: $tenantId,
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString(self::SUBJECT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Test Evaluation',
|
||||
description: null,
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
gradeScale: new GradeScale(20),
|
||||
coefficient: new Coefficient(1.0),
|
||||
now: $now,
|
||||
);
|
||||
$evaluation->publierNotes($now);
|
||||
$evaluation->pullDomainEvents();
|
||||
$this->evaluationRepo->save($evaluation);
|
||||
|
||||
return $evaluation;
|
||||
}
|
||||
|
||||
private function seedGrade(
|
||||
EvaluationId $evaluationId,
|
||||
string $studentId,
|
||||
?float $value,
|
||||
GradeStatus $status,
|
||||
): Grade {
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$now = new DateTimeImmutable();
|
||||
|
||||
$grade = Grade::saisir(
|
||||
tenantId: $tenantId,
|
||||
evaluationId: $evaluationId,
|
||||
studentId: UserId::fromString($studentId),
|
||||
value: $value !== null ? new GradeValue($value) : null,
|
||||
status: $status,
|
||||
gradeScale: new GradeScale(20),
|
||||
createdBy: UserId::fromString(self::TEACHER_ID),
|
||||
now: $now,
|
||||
);
|
||||
$grade->pullDomainEvents();
|
||||
$this->gradeRepo->save($grade);
|
||||
|
||||
return $grade;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Scolarite\Infrastructure\EventHandler;
|
||||
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassId;
|
||||
use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Port\PeriodFinder;
|
||||
use App\Scolarite\Application\Port\PeriodInfo;
|
||||
use App\Scolarite\Application\Service\RecalculerMoyennesService;
|
||||
use App\Scolarite\Domain\Event\NotesPubliees;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Coefficient;
|
||||
use App\Scolarite\Domain\Model\Evaluation\Evaluation;
|
||||
use App\Scolarite\Domain\Model\Evaluation\EvaluationId;
|
||||
use App\Scolarite\Domain\Model\Evaluation\GradeScale;
|
||||
use App\Scolarite\Domain\Model\Grade\Grade;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeStatus;
|
||||
use App\Scolarite\Domain\Model\Grade\GradeValue;
|
||||
use App\Scolarite\Domain\Service\AverageCalculator;
|
||||
use App\Scolarite\Infrastructure\EventHandler\RecalculerMoyennesOnNotesPublieesHandler;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryEvaluationStatisticsRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryGradeRepository;
|
||||
use App\Scolarite\Infrastructure\Persistence\InMemory\InMemoryStudentAverageRepository;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use App\Shared\Infrastructure\Tenant\TenantConfig;
|
||||
use App\Shared\Infrastructure\Tenant\TenantContext;
|
||||
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RecalculerMoyennesOnNotesPublieesHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
|
||||
public const string PERIOD_ID = '11111111-1111-1111-1111-111111111111';
|
||||
private const string STUDENT_1 = '22222222-2222-2222-2222-222222222222';
|
||||
private const string STUDENT_2 = '33333333-3333-3333-3333-333333333333';
|
||||
private const string TEACHER_ID = '44444444-4444-4444-4444-444444444444';
|
||||
private const string CLASS_ID = '55555555-5555-5555-5555-555555555555';
|
||||
private const string SUBJECT_ID = '66666666-6666-6666-6666-666666666666';
|
||||
|
||||
private InMemoryEvaluationRepository $evaluationRepo;
|
||||
private InMemoryGradeRepository $gradeRepo;
|
||||
private InMemoryEvaluationStatisticsRepository $evalStatsRepo;
|
||||
private InMemoryStudentAverageRepository $studentAvgRepo;
|
||||
private RecalculerMoyennesOnNotesPublieesHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->evaluationRepo = new InMemoryEvaluationRepository();
|
||||
$this->gradeRepo = new InMemoryGradeRepository();
|
||||
$this->evalStatsRepo = new InMemoryEvaluationStatisticsRepository();
|
||||
$this->studentAvgRepo = new InMemoryStudentAverageRepository();
|
||||
|
||||
$tenantContext = new TenantContext();
|
||||
$tenantContext->setCurrentTenant(new TenantConfig(
|
||||
tenantId: InfraTenantId::fromString(self::TENANT_ID),
|
||||
subdomain: 'test',
|
||||
databaseUrl: 'postgresql://test',
|
||||
));
|
||||
|
||||
$periodFinder = new class implements PeriodFinder {
|
||||
public function findForDate(DateTimeImmutable $date, TenantId $tenantId): ?PeriodInfo
|
||||
{
|
||||
return new PeriodInfo(
|
||||
periodId: RecalculerMoyennesOnNotesPublieesHandlerTest::PERIOD_ID,
|
||||
startDate: new DateTimeImmutable('2026-01-01'),
|
||||
endDate: new DateTimeImmutable('2026-03-31'),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
$service = new RecalculerMoyennesService(
|
||||
evaluationRepository: $this->evaluationRepo,
|
||||
gradeRepository: $this->gradeRepo,
|
||||
evaluationStatisticsRepository: $this->evalStatsRepo,
|
||||
studentAverageRepository: $this->studentAvgRepo,
|
||||
periodFinder: $periodFinder,
|
||||
calculator: new AverageCalculator(),
|
||||
);
|
||||
|
||||
$this->handler = new RecalculerMoyennesOnNotesPublieesHandler(
|
||||
tenantContext: $tenantContext,
|
||||
evaluationRepository: $this->evaluationRepo,
|
||||
gradeRepository: $this->gradeRepo,
|
||||
periodFinder: $periodFinder,
|
||||
service: $service,
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itCalculatesEvaluationStatisticsOnPublication(): void
|
||||
{
|
||||
$evaluationId = $this->seedEvaluationWithGrades(
|
||||
grades: [
|
||||
[self::STUDENT_1, 14.0, GradeStatus::GRADED],
|
||||
[self::STUDENT_2, 8.0, GradeStatus::GRADED],
|
||||
],
|
||||
);
|
||||
|
||||
($this->handler)(new NotesPubliees(
|
||||
evaluationId: $evaluationId,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
$stats = $this->evalStatsRepo->findByEvaluation($evaluationId);
|
||||
|
||||
self::assertNotNull($stats);
|
||||
self::assertSame(11.0, $stats->average);
|
||||
self::assertSame(8.0, $stats->min);
|
||||
self::assertSame(14.0, $stats->max);
|
||||
self::assertSame(11.0, $stats->median);
|
||||
self::assertSame(2, $stats->gradedCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itExcludesAbsentAndDispensedFromStatistics(): void
|
||||
{
|
||||
$evaluationId = $this->seedEvaluationWithGrades(
|
||||
grades: [
|
||||
[self::STUDENT_1, 16.0, GradeStatus::GRADED],
|
||||
[self::STUDENT_2, null, GradeStatus::ABSENT],
|
||||
],
|
||||
);
|
||||
|
||||
($this->handler)(new NotesPubliees(
|
||||
evaluationId: $evaluationId,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
$stats = $this->evalStatsRepo->findByEvaluation($evaluationId);
|
||||
|
||||
self::assertNotNull($stats);
|
||||
self::assertSame(16.0, $stats->average);
|
||||
self::assertSame(1, $stats->gradedCount);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itCalculatesSubjectAverageForEachStudent(): void
|
||||
{
|
||||
$evaluationId = $this->seedEvaluationWithGrades(
|
||||
grades: [
|
||||
[self::STUDENT_1, 15.0, GradeStatus::GRADED],
|
||||
[self::STUDENT_2, 10.0, GradeStatus::GRADED],
|
||||
],
|
||||
);
|
||||
|
||||
($this->handler)(new NotesPubliees(
|
||||
evaluationId: $evaluationId,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
$student1Avg = $this->studentAvgRepo->findSubjectAverage(
|
||||
UserId::fromString(self::STUDENT_1),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNotNull($student1Avg);
|
||||
self::assertSame(15.0, $student1Avg['average']);
|
||||
self::assertSame(1, $student1Avg['gradeCount']);
|
||||
|
||||
$student2Avg = $this->studentAvgRepo->findSubjectAverage(
|
||||
UserId::fromString(self::STUDENT_2),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNotNull($student2Avg);
|
||||
self::assertSame(10.0, $student2Avg['average']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itCalculatesWeightedSubjectAverageAcrossMultipleEvaluations(): void
|
||||
{
|
||||
// Première évaluation publiée (coef 2)
|
||||
$eval1Id = $this->seedEvaluationWithGrades(
|
||||
grades: [[self::STUDENT_1, 16.0, GradeStatus::GRADED]],
|
||||
coefficient: 2.0,
|
||||
published: true,
|
||||
);
|
||||
|
||||
// Publier la première évaluation d'abord
|
||||
($this->handler)(new NotesPubliees(
|
||||
evaluationId: $eval1Id,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
// Deuxième évaluation publiée (coef 1)
|
||||
$eval2Id = $this->seedEvaluationWithGrades(
|
||||
grades: [[self::STUDENT_1, 10.0, GradeStatus::GRADED]],
|
||||
coefficient: 1.0,
|
||||
published: true,
|
||||
);
|
||||
|
||||
($this->handler)(new NotesPubliees(
|
||||
evaluationId: $eval2Id,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
$student1Avg = $this->studentAvgRepo->findSubjectAverage(
|
||||
UserId::fromString(self::STUDENT_1),
|
||||
SubjectId::fromString(self::SUBJECT_ID),
|
||||
self::PERIOD_ID,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertNotNull($student1Avg);
|
||||
// (16×2 + 10×1) / (2+1) = 42/3 = 14.0
|
||||
self::assertSame(14.0, $student1Avg['average']);
|
||||
self::assertSame(2, $student1Avg['gradeCount']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itCalculatesGeneralAverage(): void
|
||||
{
|
||||
$evaluationId = $this->seedEvaluationWithGrades(
|
||||
grades: [[self::STUDENT_1, 14.0, GradeStatus::GRADED]],
|
||||
);
|
||||
|
||||
($this->handler)(new NotesPubliees(
|
||||
evaluationId: $evaluationId,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
$generalAvg = $this->studentAvgRepo->findGeneralAverageForStudent(
|
||||
UserId::fromString(self::STUDENT_1),
|
||||
self::PERIOD_ID,
|
||||
TenantId::fromString(self::TENANT_ID),
|
||||
);
|
||||
|
||||
self::assertSame(14.0, $generalAvg);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itDoesNothingWhenEvaluationNotFound(): void
|
||||
{
|
||||
$unknownId = EvaluationId::generate();
|
||||
|
||||
($this->handler)(new NotesPubliees(
|
||||
evaluationId: $unknownId,
|
||||
occurredOn: new DateTimeImmutable(),
|
||||
));
|
||||
|
||||
self::assertNull($this->evalStatsRepo->findByEvaluation($unknownId));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{0: string, 1: float|null, 2: GradeStatus}> $grades
|
||||
*/
|
||||
private function seedEvaluationWithGrades(
|
||||
array $grades,
|
||||
float $coefficient = 1.0,
|
||||
bool $published = true,
|
||||
): EvaluationId {
|
||||
$tenantId = TenantId::fromString(self::TENANT_ID);
|
||||
$now = new DateTimeImmutable();
|
||||
|
||||
$evaluation = Evaluation::creer(
|
||||
tenantId: $tenantId,
|
||||
classId: ClassId::fromString(self::CLASS_ID),
|
||||
subjectId: SubjectId::fromString(self::SUBJECT_ID),
|
||||
teacherId: UserId::fromString(self::TEACHER_ID),
|
||||
title: 'Test Evaluation',
|
||||
description: null,
|
||||
evaluationDate: new DateTimeImmutable('2026-02-15'),
|
||||
gradeScale: new GradeScale(20),
|
||||
coefficient: new Coefficient($coefficient),
|
||||
now: $now,
|
||||
);
|
||||
|
||||
if ($published) {
|
||||
$evaluation->publierNotes($now);
|
||||
}
|
||||
|
||||
$evaluation->pullDomainEvents();
|
||||
$this->evaluationRepo->save($evaluation);
|
||||
|
||||
foreach ($grades as [$studentId, $value, $status]) {
|
||||
$grade = Grade::saisir(
|
||||
tenantId: $tenantId,
|
||||
evaluationId: $evaluation->id,
|
||||
studentId: UserId::fromString($studentId),
|
||||
value: $value !== null ? new GradeValue($value) : null,
|
||||
status: $status,
|
||||
gradeScale: new GradeScale(20),
|
||||
createdBy: UserId::fromString(self::TEACHER_ID),
|
||||
now: $now,
|
||||
);
|
||||
$grade->pullDomainEvents();
|
||||
$this->gradeRepo->save($grade);
|
||||
}
|
||||
|
||||
return $evaluation->id;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user