forked from audrey/intranet
105 lines
4.5 KiB
PHP
105 lines
4.5 KiB
PHP
<?php
|
|
namespace App\Controller;
|
|
|
|
use App\Form\SafetyQuizType;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\Yaml\Yaml;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use function PHPUnit\Framework\containsIdentical;
|
|
|
|
class SafetyController extends AbstractController
|
|
{
|
|
#[Route('/training/safety', name: 'safetyHome')]
|
|
public function Index() : Response
|
|
{
|
|
$contentFile = Yaml::parseFile('../config/safetyLinks.yaml', 2, 2, Yaml::PARSE_OBJECT_FOR_MAP);
|
|
return $this->render('Training/Safety/safetyHome.html.twig',['content' => $contentFile]);
|
|
}
|
|
#[Route('/training/safety/quiz')]
|
|
public function Quiz(Request $request, LoggerInterface $log)
|
|
{
|
|
$form = $this->createForm(SafetyQuizType::class);
|
|
$form->handleRequest($request);
|
|
if($form->isSubmitted() && $form->isValid())
|
|
{
|
|
//TODO: Process quiz submissions. Calculate score & return a printable results page
|
|
$data = $form->getData();
|
|
$testerName = $data['Name'];
|
|
unset($data['Name']);
|
|
$correctAnswers = 0;
|
|
foreach($data as $question)
|
|
{
|
|
if($question)
|
|
{
|
|
$correctAnswers++;
|
|
}
|
|
}
|
|
$log->Debug($testerName . ' quiz answers correct: ' . $correctAnswers);
|
|
return $this->render('Training/Safety/quizResults.html.twig', ['testerName'=>$testerName,
|
|
'correctAnswers'=>$correctAnswers, 'totalAnswers'=>count($data)]);
|
|
}
|
|
return $this->render('Training/Safety/SafetyQuiz.html.twig',['quiz'=>$form]);
|
|
}
|
|
#[Route('/Training/Safety/Topics/{topic?}')]
|
|
public function Topics(?string $topic, LoggerInterface $log) : Response
|
|
{
|
|
//We want to show the safety homepage if this is blank
|
|
if($topic == null)
|
|
{
|
|
return $this->redirectToRoute('safetyHome');
|
|
}
|
|
|
|
//We need to get the previous and next topics for nav buttons
|
|
//FEATURING: Regret for not having mapped this YAML file to a data model that would have saved myself a lot of trouble and spaghetti
|
|
$contentFile = Yaml::parseFile('../config/safetyLinks.yaml', 2, 2, Yaml::PARSE_OBJECT_FOR_MAP);
|
|
$previousTopic = $nextTopic = ""; //declare ahead of time for scoping
|
|
|
|
$arrayKeys = array_keys($contentFile['Cards']);
|
|
foreach($arrayKeys as $key)
|
|
{
|
|
$card = $contentFile['Cards'][$key];
|
|
for($i = 0; $i < count($card); $i++)
|
|
{
|
|
$entry = $card[$i];
|
|
$cardIndex = array_search($key, $arrayKeys);
|
|
if($entry['url'] == $topic)
|
|
{
|
|
//Assign Previous Topic
|
|
if($i > 0)
|
|
{
|
|
$previousTopic = $contentFile['Cards'][$arrayKeys[$cardIndex]][$i-1];
|
|
}
|
|
else if($cardIndex > 0) //there's a previous card, use the last entry in it
|
|
{
|
|
//NOTE: Should be fine to use end, at this point we shouldn't care about the array's internal pointer, we're done iterating
|
|
$previousTopic = end($contentFile['Cards'][$arrayKeys[$cardIndex-1]]);
|
|
}
|
|
|
|
//Assign Next Topic
|
|
if($i < count($card)-1) //Next topic should be within the same card
|
|
{
|
|
$nextTopic = $contentFile['Cards'][$arrayKeys[$cardIndex]][$i+1];
|
|
}
|
|
else if($cardIndex < count($contentFile['Cards'])-1) //Next topic is not in same card, or does not exist
|
|
{
|
|
$nextTopic = $contentFile['Cards'][$arrayKeys[$cardIndex+1]][0]; //Set to first entry in next card
|
|
}
|
|
break 2; //Got what we needed, break out of both for loops
|
|
}
|
|
}
|
|
}
|
|
|
|
$topic = strtolower($topic);
|
|
//TODO: Insert back button
|
|
$navButtonData = ['previous'=> $previousTopic, 'next' => $nextTopic];
|
|
$contents = $this->render('Training/Safety/Topics/'.$topic.".html.twig");
|
|
$navButtons = $this->render('Training/Safety/navButtons.html.twig',$navButtonData);
|
|
return new Response($contents . $navButtons);
|
|
}
|
|
}
|