Last active
November 21, 2016 16:24
-
-
Save joelworsham/4fee7399cc0fd574be24f645cd75df56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Modifies WP Pro Quiz so I can get the dang answers in the results. | |
* | |
* @since 0.1.0 | |
* @package ME10 | |
*/ | |
// Don't load directly | |
if ( ! defined( 'ABSPATH' ) ) { | |
die; | |
} | |
// Only fire on admin AJAX quiz complete | |
if ( defined( 'DOING_AJAX' ) && | |
DOING_AJAX && | |
isset( $_POST['data'] ) && | |
isset( $_POST['data']['quizId'] ) && | |
isset( $_POST['data']['results'] ) | |
) { | |
add_filter( 'init', 'me10_wpproquiz_email_results' ); | |
} | |
function me10_wpproquiz_email_results() { | |
$quiz_ID = $_POST['data']['quizId']; | |
$results = $_POST['data']['results']; | |
$quizMapper = new WpProQuiz_Model_QuizMapper(); | |
$formMapper = new WpProQuiz_Model_FormMapper(); | |
$questionMapper = new WpProQuiz_Model_QuestionMapper(); | |
$quiz = $quizMapper->fetch( $quiz_ID ); | |
if ( $quiz === null || $quiz->getId() <= 0 ) { | |
return json_encode( array() ); | |
} | |
$forms = $formMapper->fetch( $quiz->getId() ); | |
$forms_map = array(); | |
foreach ( $forms as $form ) { | |
$forms_map[ $form->getFieldname() ] = $form->getFormId(); | |
} | |
// Get form fields | |
$instructor_email = $_POST['data']['forms'][ $forms_map['Instructor Email'] ]; | |
$student_email_s = $_POST['data']['forms'][ $forms_map['Your Email'] ]; | |
$student_name = $_POST['data']['forms'][ $forms_map['Your Name'] ]; | |
if ( ! $instructor_email || ! $student_name ) { | |
return; | |
} | |
$to = "$instructor_email,$student_email_s"; | |
$subject = 'Quiz Completed'; | |
ob_start(); ?> | |
<h1>Quiz "<?php echo $quiz->getName(); ?>" has been completed by <?php echo esc_attr( $student_name ); ?></h1> | |
<h2><strong>Score: </strong> <?php echo $results['comp']['correctQuestions']; ?> out | |
of <?php echo count( $results ) - 1; ?></h2> | |
<h3>Answers:</h3> | |
<?php foreach ( $results as $result_ID => $result ) : | |
if ( $result_ID == 'comp' ) { | |
continue; | |
} | |
$question = $questionMapper->fetch( $result_ID ); | |
$answer_data = $question->getAnswerData(); | |
$answered = $answer_data[ array_search( '1', $result['data'] ) ]; | |
$correct = false; | |
foreach ( $answer_data as $answer ) { | |
if ( $answer->isCorrect() ) { | |
$correct = $answer; | |
break; | |
} | |
} | |
$was_correct = $answered->getAnswer() === $correct->getAnswer(); | |
?> | |
<table | |
style="padding: 0.5em; border: 1px solid #aaa; background: <?php echo $was_correct ? '#BEFFC2' : '#EEC0AF'; ?>; text-align: left;margin-bottom: 1em; width: 100%; max-width: 600px;"> | |
<tbody> | |
<tr valign="top"> | |
<th scope="row" style="white-space: nowrap;">Question:</th> | |
<td><?php echo $question->getQuestion(); ?></td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row" style="white-space: nowrap;">Correct Answer:</th> | |
<td><?php echo $correct->getAnswer(); ?></td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row" style="white-space: nowrap;">Student Answered:</th> | |
<td><?php echo $answered->getAnswer(); ?></td> | |
</tr> | |
<tr valign="top"> | |
<th scope="row" style="white-space: nowrap;"><?php echo $was_correct ? 'Correct' : 'Incorrect'; ?>:</th> | |
<td><?php echo $question->getCorrectMsg(); ?></td> | |
</tr> | |
</tbody> | |
</table> | |
<?php endforeach; ?> | |
<?php $message = ob_get_clean(); | |
add_filter( 'wp_mail_from', 'me10_email_from', 1000 ); | |
add_filter( 'wp_mail_from_name', 'me10_email_from_name', 1000 ); | |
add_filter( 'wp_mail_content_type', 'me10_html_email', 1000 ); | |
wp_mail( $to, $subject, $message ); | |
remove_filter( 'wp_mail_from', 'me10_html_email', 1000 ); | |
remove_filter( 'wp_mail_from_name', 'me10_html_email', 1000 ); | |
remove_filter( 'wp_mail_content_type', 'me10_html_email', 1000 ); | |
} | |
function me10_html_email() { | |
return 'text/html'; | |
} | |
function me10_email_from() { | |
return '[email protected]'; | |
} | |
function me10_email_from_name() { | |
return 'Media Ethics'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment