Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions db/seeds/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,45 @@ public function run(): void
],
];

$forcedPlannings = [
6 => [
'debut' => new \DateTime('2020-09-15 10:00:00')->format('U'),
'fin' => new \DateTime('2020-09-15 10:40:00')->format('U'),
],
];

// Une édition passée avec un planning complet (7 talks publiés, une vidéo sur
// deux, des horaires dans un autre ordre que les identifiants) et un talk dont
// la publication est à venir, pour les liens entre talks de la page talk.
$passedEventTalks = [
7 => ['titre' => 'Clôture : le futur de PHP', 'youtube_id' => 'videoTalk07', 'debut' => '16:00'],
8 => ['titre' => 'Keynote d\'ouverture', 'youtube_id' => null, 'debut' => '09:00'],
9 => ['titre' => 'Tester sans se ruiner', 'youtube_id' => 'videoTalk09', 'debut' => '11:00'],
10 => ['titre' => 'Migrer une application legacy', 'youtube_id' => null, 'debut' => '12:00'],
11 => ['titre' => 'Observabilité avec OpenTelemetry', 'youtube_id' => 'videoTalk11', 'debut' => '14:00'],
12 => ['titre' => 'Le typage statique en pratique', 'youtube_id' => null, 'debut' => '15:00'],
13 => ['titre' => 'Un talk pas encore publié', 'youtube_id' => 'videoTalk13', 'debut' => '17:00', 'date_publication' => new \DateTime()->modify('+5 days')->format('Y-m-d H:i:s')],
];
foreach ($passedEventTalks as $sessionId => $passedEventTalk) {
$sessions[] = [
'session_id' => $sessionId,
'id_forum' => Event::ID_PASSED,
'date_soumission' => '2020-05-01',
'titre' => $passedEventTalk['titre'],
'abstract' => 'Résumé du talk « ' . $passedEventTalk['titre'] . ' ».',
'genre' => 1,
'skill' => 0,
'plannifie' => 1,
'youtube_id' => $passedEventTalk['youtube_id'],
'language_code' => 'fr',
'date_publication' => $passedEventTalk['date_publication'] ?? null,
];
$forcedPlannings[$sessionId] = [
'debut' => new \DateTime('2020-09-15 ' . $passedEventTalk['debut'])->format('U'),
'fin' => new \DateTime('2020-09-15 ' . $passedEventTalk['debut'])->modify('+40 minutes')->format('U'),
];
}

$sessionsByIds = [];
foreach ($sessions as $session) {
$sessionsByIds[$session['session_id']] = $session;
Expand All @@ -158,6 +197,11 @@ public function run(): void
}

$conferenciers[2]['conferencier_id'] = 2;
foreach ($conferenciers as $index => $conferencier) {
if (isset($passedEventTalks[$conferencier['session_id']])) {
$conferenciers[$index]['conferencier_id'] = 3;
}
}

$table = $this->table('afup_conferenciers_sessions');
$table->truncate();
Expand All @@ -179,14 +223,6 @@ public function run(): void
->save();


$forcedPlannings = [
6 => [
'debut' => new \DateTime('2020-09-15 10:00:00')->format('U'),
'fin' => new \DateTime('2020-09-15 10:40:00')->format('U'),
],
];


$i = 1;
$plannings = [];
foreach ($sessions as $session) {
Expand Down
15 changes: 14 additions & 1 deletion sources/AppBundle/Controller/Website/Talks/ShowAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use AppBundle\Event\Model\Repository\SpeakerRepository;
use AppBundle\Event\Model\Repository\TalkRepository;
use AppBundle\Event\Talk\VideoObjectJsonLd;
use AppBundle\Event\Talk\RelatedTalkRepository;
use AppBundle\Event\Talk\RelatedTalks;
use AppBundle\Joindin\JoindinComments;
use AppBundle\Subtitles\Parser;
use AppBundle\Twig\ViewRenderer;
Expand All @@ -26,6 +28,7 @@ public function __construct(
private readonly PlanningRepository $planningRepository,
private readonly EventRepository $eventRepository,
private readonly VideoObjectJsonLd $videoObjectJsonLd,
private readonly RelatedTalkRepository $relatedTalkRepository,
) {}

public function __invoke(int $id, string $slug): Response
Expand All @@ -45,8 +48,17 @@ public function __invoke(int $id, string $slug): Response
return $this->redirectToRoute('talks_show', ['id' => $id, 'slug' => $talk->getSlug()], Response::HTTP_MOVED_PERMANENTLY);
}

$speakers = $this->speakerRepository->getSpeakersByTalk($talk);
$speakers = iterator_to_array($this->speakerRepository->getSpeakersByTalk($talk), false);
$event = $this->eventRepository->get($planning->eventId);
if ($event === null) {
throw $this->createNotFoundException();
}
$relatedTalks = RelatedTalks::build(
$talk,
$this->relatedTalkRepository->getPublishedByEventInPlanningOrder($event),
$speakers,
$this->relatedTalkRepository->getPublishedBySpeakers($speakers, $talk),
);
$comments = $this->joindinComments->getCommentsFromTalk($talk);

$parser = new Parser();
Expand All @@ -59,6 +71,7 @@ public function __invoke(int $id, string $slug): Response
'comments' => $comments,
'transcript' => $parsedContent,
'video_object' => $this->videoObjectJsonLd->getData($talk, $planning->start, $parsedContent, $this->generateUrl('talks_show', ['id' => $talk->getId(), 'slug' => $talk->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL)),
'related_talks' => $relatedTalks,
]);
}
}
168 changes: 168 additions & 0 deletions sources/AppBundle/Event/Talk/RelatedTalkRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

declare(strict_types=1);

namespace AppBundle\Event\Talk;

use AppBundle\Event\Model\Event;
use AppBundle\Event\Model\Repository\EventRepository;
use AppBundle\Event\Model\Repository\SpeakerRepository;
use AppBundle\Event\Model\Repository\TalkRepository;
use AppBundle\Event\Model\Speaker;
use AppBundle\Event\Model\Talk;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Webmozart\Assert\Assert;

/**
* Talks publiés liés à la page d'un talk (cf. RelatedTalks).
*
* Un talk publié est planifié, visible dans l'historique (ni clinique, ni présentation des
* speakers, cf. Talk::isDisplayedOnHistory()), a un créneau (sans créneau, sa page répond 404)
* et sa date de publication est passée.
*
* Les requêtes renvoient des identifiants ; les talks, éditions et speakers sont ensuite chargés
* par leurs repositories.
*/
final readonly class RelatedTalkRepository
{
/**
* Créneau le plus tôt de chaque talk : un talk planifié sur plusieurs créneaux n'est renvoyé qu'une fois.
*/
private const string PLANNING = '(SELECT id_session, MIN(debut) AS debut FROM afup_forum_planning GROUP BY id_session)';

private const string PUBLISHED = 'talk.plannifie = 1
AND talk.genre NOT IN (:hiddenTypes)
AND (talk.date_publication < NOW() OR talk.date_publication IS NULL)';

public function __construct(
private Connection $connection,
private TalkRepository $talkRepository,
private EventRepository $eventRepository,
private SpeakerRepository $speakerRepository,
) {}

/**
* Talks publiés d'une édition, dans l'ordre du planning.
*
* @return list<Talk>
*/
public function getPublishedByEventInPlanningOrder(Event $event): array
{
$talkIds = $this->connection->executeQuery(
'SELECT talk.session_id
FROM afup_sessions AS talk
INNER JOIN ' . self::PLANNING . ' AS planning ON planning.id_session = talk.session_id
WHERE talk.id_forum = :event AND ' . self::PUBLISHED . '
ORDER BY planning.debut ASC, talk.session_id ASC',
['event' => $event->getId(), 'hiddenTypes' => self::hiddenTypes()],
['hiddenTypes' => ArrayParameterType::INTEGER],
)->fetchFirstColumn();
Assert::allIntegerish($talkIds);

return array_values($this->loadTalks(array_map(intval(...), $talkIds)));
}

/**
* Talks publiés, toutes éditions confondues, des personnes qui ont présenté ces speakers,
* du plus récent au plus ancien.
*
* Un speaker étant une ligne par édition, la même personne est retrouvée par son
* compte GitHub ou, à défaut, par son prénom et son nom.
*
* @param list<Speaker> $speakers
*
* @return list<array{talk: Talk, event: Event, speakers: list<Speaker>}> les speakers de chaque talk qui correspondent à l'un de ceux demandés
*/
public function getPublishedBySpeakers(array $speakers, Talk $excludedTalk): array
{
$conditions = [];
$params = ['excluded' => $excludedTalk->getId(), 'hiddenTypes' => self::hiddenTypes()];
foreach ($speakers as $index => $speaker) {
$conditions[] = sprintf('(LOWER(TRIM(speaker.prenom)) = :firstname%1$d AND LOWER(TRIM(speaker.nom)) = :lastname%1$d)', $index);
$params['firstname' . $index] = mb_strtolower(trim((string) $speaker->getFirstname()));
$params['lastname' . $index] = mb_strtolower(trim((string) $speaker->getLastname()));

if ($speaker->getUser()) {
$conditions[] = sprintf('speaker.user_github = :github%d', $index);
$params['github' . $index] = $speaker->getUser();
}
}

if ($conditions === []) {
return [];
}

$rows = $this->connection->executeQuery(
sprintf('SELECT talk.session_id AS talk_id, talk.id_forum AS event_id, speaker.conferencier_id AS speaker_id
FROM afup_sessions AS talk
INNER JOIN ' . self::PLANNING . ' AS planning ON planning.id_session = talk.session_id
INNER JOIN afup_conferenciers_sessions acs ON acs.session_id = talk.session_id
INNER JOIN afup_conferenciers speaker ON speaker.conferencier_id = acs.conferencier_id
WHERE talk.session_id != :excluded AND ' . self::PUBLISHED . '
AND (%s)
ORDER BY planning.debut DESC, talk.session_id DESC, speaker.conferencier_id ASC', implode(' OR ', $conditions)),
$params,
['hiddenTypes' => ArrayParameterType::INTEGER],
)->fetchAllAssociative();

// Par talk, dans l'ordre de la requête : l'édition et les speakers trouvés.
$matches = [];
foreach ($rows as $row) {
Assert::integerish($row['talk_id']);
Assert::integerish($row['event_id']);
Assert::integerish($row['speaker_id']);
$matches[(int) $row['talk_id']]['event'] = (int) $row['event_id'];
$matches[(int) $row['talk_id']]['speakers'][] = (int) $row['speaker_id'];
}

$talks = $this->loadTalks(array_keys($matches));
$events = [];
$talksWithSpeakers = [];
foreach ($talks as $talkId => $talk) {
$eventId = $matches[$talkId]['event'];
$event = $events[$eventId] ??= $this->eventRepository->get($eventId);
$talkSpeakers = array_values(array_filter(array_map($this->speakerRepository->get(...), $matches[$talkId]['speakers'])));

if ($event instanceof Event && $talkSpeakers !== []) {
$talksWithSpeakers[] = ['talk' => $talk, 'event' => $event, 'speakers' => $talkSpeakers];
}
}

return $talksWithSpeakers;
}

/**
* @param list<int> $talkIds
*
* @return array<int, Talk> les talks indexés par identifiant, dans l'ordre des identifiants donnés
*/
private function loadTalks(array $talkIds): array
{
if ($talkIds === []) {
return [];
}

$talksById = [];
foreach ($this->talkRepository->findList($talkIds) as $talk) {
$talksById[$talk->getId() ?? 0] = $talk;
}

$talks = [];
foreach ($talkIds as $talkId) {
if (isset($talksById[$talkId])) {
$talks[$talkId] = $talksById[$talkId];
}
}

return $talks;
}

/**
* @return list<int>
*/
private static function hiddenTypes(): array
{
return [Talk::TYPE_CLINIC, Talk::TYPE_SPEAKER_INTRODUCTIONS];
}
}
90 changes: 90 additions & 0 deletions sources/AppBundle/Event/Talk/RelatedTalks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace AppBundle\Event\Talk;

use AppBundle\Event\Model\Event;
use AppBundle\Event\Model\Speaker;
use AppBundle\Event\Model\Talk;

/**
* Liens vers d'autres talks affichés sur la page d'un talk.
*/
final readonly class RelatedTalks
{
public const int SAME_EVENT_LIMIT = 6;

/**
* @param list<Talk> $sameEvent
* @param list<array{speaker: Speaker, talks: list<array{talk: Talk, event: Event}>}> $bySpeaker
*/
public function __construct(
public ?Talk $previous,
public ?Talk $next,
public array $sameEvent,
public array $bySpeaker,
) {}

/**
* @param list<Talk> $eventTalks talks publiés de l'édition, dans l'ordre du planning
* @param list<Speaker> $speakers speakers du talk
* @param list<array{talk: Talk, event: Event, speakers: list<Speaker>}> $speakersTalks talks publiés des speakers, du plus récent au plus ancien
*/
public static function build(Talk $talk, array $eventTalks, array $speakers, array $speakersTalks): self
{
$position = array_find_key($eventTalks, fn(Talk $eventTalk): bool => $eventTalk->getId() === $talk->getId());

if ($position === null) {
$previous = $next = null;
$others = $eventTalks;
} else {
$previous = $eventTalks[$position - 1] ?? null;
$next = $eventTalks[$position + 1] ?? null;
// On part du talk suivant et on boucle sur le début du planning : chaque talk
// de l'édition reçoit ainsi des liens, pas seulement les premiers du planning.
$others = [...array_slice($eventTalks, $position + 1), ...array_slice($eventTalks, 0, $position)];
}

// Tri stable : les vidéos d'abord, l'ordre du planning ensuite.
usort($others, fn(Talk $a, Talk $b): int => $b->hasYoutubeId() <=> $a->hasYoutubeId());

$bySpeaker = [];
foreach ($speakers as $speaker) {
$talks = [];
foreach ($speakersTalks as $speakerTalk) {
foreach ($speakerTalk['speakers'] as $candidate) {
if (self::isSamePerson($speaker, $candidate)) {
$talks[] = ['talk' => $speakerTalk['talk'], 'event' => $speakerTalk['event']];
break;
}
}
}

if ($talks !== []) {
$bySpeaker[] = ['speaker' => $speaker, 'talks' => $talks];
}
}

return new self($previous, $next, array_slice($others, 0, self::SAME_EVENT_LIMIT), $bySpeaker);
}

/**
* Même règle que RelatedTalkRepository::getPublishedBySpeakers() : même compte GitHub,
* ou même prénom et même nom.
*/
private static function isSamePerson(Speaker $speaker, Speaker $candidate): bool
{
if ($speaker->getUser() && $speaker->getUser() === $candidate->getUser()) {
return true;
}

return self::normalize($speaker->getFirstname()) === self::normalize($candidate->getFirstname())
&& self::normalize($speaker->getLastname()) === self::normalize($candidate->getLastname());
}

private static function normalize(?string $name): string
{
return mb_strtolower(trim((string) $name));
}
}
Loading
Loading