send questions & basic results in email

This commit is contained in:
Audrey Jensen 2023-09-19 16:24:19 +00:00
parent b3cd808265
commit 802b7c179f
3 changed files with 90 additions and 10 deletions

View File

@ -15,3 +15,6 @@ MAILER_DSN=smtp://jv-com.mail.protection.outlook.com:25
#Anonymous Complaints Email Settings
COMPLAINT_RECIPIENT_ADDRESS=chris@jv.com
COMPLAINT_RECIPIENT_NAME="Chris Weiser"
#Safety Quiz Result Recipients
#Enter full email addresses, separated by commas
RESULT_INBOXES=""

View File

@ -2,6 +2,8 @@
namespace App\Controller;
use App\Form\SafetyQuizType;
use Egulias\EmailValidator\Warning\Warning;
use Error;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -9,6 +11,12 @@ use Symfony\Component\Yaml\Yaml;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Psr\Log\LoggerInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Mime\Address;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\DependencyInjection\Exception\EnvParameterException;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use function PHPUnit\Framework\containsIdentical;
@ -21,7 +29,7 @@ class SafetyController extends AbstractController
return $this->render('Training/Safety/safetyHome.html.twig',['content' => $contentFile]);
}
#[Route('/training/safety/quiz')]
public function Quiz(Request $request, LoggerInterface $log)
public function Quiz(Request $request, LoggerInterface $log, MailerInterface $mailer)
{
$form = $this->createForm(SafetyQuizType::class);
$form->handleRequest($request);
@ -40,6 +48,7 @@ class SafetyController extends AbstractController
}
}
$log->Debug($testerName . ' quiz answers correct: ' . $correctAnswers);
$this->sendQuizResults($correctAnswers, count($data), $form, $mailer, $log);
return $this->render('Training/Safety/quizResults.html.twig', ['testerName'=>$testerName,
'correctAnswers'=>$correctAnswers, 'totalAnswers'=>count($data)]);
}
@ -104,4 +113,72 @@ class SafetyController extends AbstractController
return new Response($header . $navButtons . $contents);
}
//Send an email with the quiz results
public function sendQuizResults(int $answersCorrect, int $totalQuestions, FormInterface $submittedQuiz, MailerInterface $mailer, LoggerInterface $log)
{
//Fetch array of mail inboxes to send messages to.
//If env var doesn't exist then fail gracefully
$recipients = []; //X: keep in method's scope
try
{
$recipients = explode(",", $_SERVER['RESULT_INBOXES']);
}
catch (EnvParameterException $e)
{
$log->warning("safety Quiz Submission: Environment Variable RESULT_INBOXES not configured. Quiz results will not be emailed.");
return;
}
//If no inboxes configured, cancel
if(count($recipients) <= 0)
{
$log->warning("Safety Quiz Submission: No email recipients configured");
return;
}
//Prepare payload data
$submittedData = $submittedQuiz->getData();
$submission = [];
$quiz = Yaml::parseFile('../config/SafetyQuiz.yaml', 2, 2, Yaml::PARSE_OBJECT_FOR_MAP)['Questions'];
$questionKeys = array_keys($quiz);
foreach($questionKeys as $key)
{
$questionData = [];
$questionData += ['key'=>$key, 'text'=>$quiz[$key]['Text']];
$answer = null;
foreach($quiz[$key]['Choices'] as $choice)
{
if($choice['Value'])
{
$answer = $choice;
}
}
$questionData += ['answer'=>$answer, 'submittedAnswer'=>$submittedData[$key]];
$submission += $questionData;
$quiz[$key] += ['submittedAnswer'=>$submittedData[$key], 'answer'=>$answer];
}
//var_dump($submission);
//Build & send the email
$email = (new TemplatedEmail())
->from(new Address('noreply@jv.com', 'Internal'))
->to(new Address("ajensen@jv.com"))
->subject('Safety Quiz Submission')
->htmlTemplate('Emails/safetyQuizResults.html.twig')
->context([
'submission' => $quiz,
'answersCorrect' => $answersCorrect,
'totalQuestions' => $totalQuestions,
'submitterName' => $submittedData['Name']])
;
try
{
$log->info("Sending Quiz Results...");
$mailer->send($email);
}
catch (TransportExceptionInterface $e)
{
$log->error("Failed to email quiz results");
$log->error($e);
}
}
}

View File

@ -10,7 +10,7 @@
}
</style>
<h1>A Quiz-taker has submitted the J.V. Safety Quiz <h1>
<h2> {{ data.getData['Name']}} has completed the quiz, scoring {{answersCorrect}}/{{totalAnswers}} correct.</h2>
<h2> {{ submitterName }} has completed the quiz, scoring {{answersCorrect}}/{{totalQuestions}} correct.</h2>
<table>
<th>
<td>#</td>
@ -18,16 +18,16 @@
<td>Submitted Answer</td>
<td>Correct Answer</td>
</th>
{% for question in submission %}
{% for key,question in submission %}
<tr>
<td>{{question}}</td>
<td>{{question.Text}}</td>
<td class="{{ (question['submittedAnswer']==question['answer'])? 'correct': 'incorrect' }}">
{% if submittedAnswer != null %}
<td>{{key}}</td>
<td>{{question["Text"]}}</td>
{% if question['submittedAnswer'] != null %}
<td class="{{ (question['submittedAnswer']==true)? 'correct': 'incorrect' }}">
{{question['submittedAnswer']}}
{% endif %}
</td>
<td>{{quesiton['answer']}}</td>
{% endif %}
<td>{{question['answer']['Value']}}</td>
</tr>
{% endfor %}
</table>