send questions & basic results in email
This commit is contained in:
parent
b3cd808265
commit
802b7c179f
@ -14,4 +14,7 @@ MESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=0 #Leave this alone unless
|
|||||||
MAILER_DSN=smtp://jv-com.mail.protection.outlook.com:25
|
MAILER_DSN=smtp://jv-com.mail.protection.outlook.com:25
|
||||||
#Anonymous Complaints Email Settings
|
#Anonymous Complaints Email Settings
|
||||||
COMPLAINT_RECIPIENT_ADDRESS=chris@jv.com
|
COMPLAINT_RECIPIENT_ADDRESS=chris@jv.com
|
||||||
COMPLAINT_RECIPIENT_NAME="Chris Weiser"
|
COMPLAINT_RECIPIENT_NAME="Chris Weiser"
|
||||||
|
#Safety Quiz Result Recipients
|
||||||
|
#Enter full email addresses, separated by commas
|
||||||
|
RESULT_INBOXES=""
|
||||||
@ -2,6 +2,8 @@
|
|||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
use App\Form\SafetyQuizType;
|
use App\Form\SafetyQuizType;
|
||||||
|
use Egulias\EmailValidator\Warning\Warning;
|
||||||
|
use Error;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Annotation\Route;
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
@ -9,6 +11,12 @@ use Symfony\Component\Yaml\Yaml;
|
|||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||||
use Psr\Log\LoggerInterface;
|
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;
|
use function PHPUnit\Framework\containsIdentical;
|
||||||
|
|
||||||
@ -21,7 +29,7 @@ class SafetyController extends AbstractController
|
|||||||
return $this->render('Training/Safety/safetyHome.html.twig',['content' => $contentFile]);
|
return $this->render('Training/Safety/safetyHome.html.twig',['content' => $contentFile]);
|
||||||
}
|
}
|
||||||
#[Route('/training/safety/quiz')]
|
#[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 = $this->createForm(SafetyQuizType::class);
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
@ -40,6 +48,7 @@ class SafetyController extends AbstractController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
$log->Debug($testerName . ' quiz answers correct: ' . $correctAnswers);
|
$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,
|
return $this->render('Training/Safety/quizResults.html.twig', ['testerName'=>$testerName,
|
||||||
'correctAnswers'=>$correctAnswers, 'totalAnswers'=>count($data)]);
|
'correctAnswers'=>$correctAnswers, 'totalAnswers'=>count($data)]);
|
||||||
}
|
}
|
||||||
@ -104,4 +113,72 @@ class SafetyController extends AbstractController
|
|||||||
|
|
||||||
return new Response($header . $navButtons . $contents);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,7 @@
|
|||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
<h1>A Quiz-taker has submitted the J.V. Safety Quiz <h1>
|
<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>
|
<table>
|
||||||
<th>
|
<th>
|
||||||
<td>#</td>
|
<td>#</td>
|
||||||
@ -18,16 +18,16 @@
|
|||||||
<td>Submitted Answer</td>
|
<td>Submitted Answer</td>
|
||||||
<td>Correct Answer</td>
|
<td>Correct Answer</td>
|
||||||
</th>
|
</th>
|
||||||
{% for question in submission %}
|
{% for key,question in submission %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{question}}</td>
|
<td>{{key}}</td>
|
||||||
<td>{{question.Text}}</td>
|
<td>{{question["Text"]}}</td>
|
||||||
<td class="{{ (question['submittedAnswer']==question['answer'])? 'correct': 'incorrect' }}">
|
{% if question['submittedAnswer'] != null %}
|
||||||
{% if submittedAnswer != null %}
|
<td class="{{ (question['submittedAnswer']==true)? 'correct': 'incorrect' }}">
|
||||||
{{question['submittedAnswer']}}
|
{{question['submittedAnswer']}}
|
||||||
{% endif %}
|
|
||||||
</td>
|
</td>
|
||||||
<td>{{quesiton['answer']}}</td>
|
{% endif %}
|
||||||
|
<td>{{question['answer']['Value']}}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user