예제 #1
0
/**
 * Given a frontend object with user progress data, replace the tags with quiz-related information
 * based on the tags that have been found in the email.
 * 
 * @param WPCW_UnitFrontend $feObj The frontend object with details.
 * @param Array $tagList The list of tags found in the template.
 * @param String $emailData The data to replace the strings with.
 * @param String $additionalData Any additional data triggered from the trainer.
 * 
 * @return The modified email data ready for sending.
 */
function WPCW_email_replaceTags_quizData($feObj, $tagList, $emailData, $additionalData = false)
{
    if (!$feObj || empty($tagList)) {
        return $emailData;
    }
    $quizDetails = $feObj->fetch_getUnitQuizDetails();
    $progressData = $feObj->fetch_getQuizProgressDetails();
    // Replace each tag for quiz-related data.
    foreach ($tagList as $tagToReplace) {
        switch ($tagToReplace) {
            case '{QUIZ_TITLE}':
                $emailData = str_replace('{QUIZ_TITLE}', $quizDetails->quiz_title, $emailData);
                break;
            case '{QUIZ_GRADE}':
                $emailData = str_replace('{QUIZ_GRADE}', $progressData->quiz_grade . '%', $emailData);
                break;
            case '{QUIZ_ATTEMPTS}':
                $emailData = str_replace('{QUIZ_ATTEMPTS}', $progressData->attempt_count, $emailData);
                break;
            case '{QUIZ_TIME}':
                $timeToShare = __('n/a', 'wp_courseware');
                if ($progressData->quiz_completion_time_seconds > 0) {
                    $timeToShare = WPCW_time_convertSecondsToHumanLabel($progressData->quiz_completion_time_seconds);
                }
                $emailData = str_replace('{QUIZ_TIME}', $timeToShare, $emailData);
                break;
            case '{QUIZ_RESULT_DETAIL}':
                $emailData = str_replace('{QUIZ_RESULT_DETAIL}', $additionalData, $emailData);
                break;
            case '{QUIZ_GRADES_BY_TAG}':
                // Use existing frontend code to get the list of messages relating to tags.
                $msgList = $feObj->fetch_quizzes_questionResultsByTag();
                $msgSummary = false;
                if (!empty($msgList)) {
                    foreach ($msgList as $tagDetails) {
                        // Got open questions
                        if ($tagDetails['question_open_count'] > 0) {
                            $msgSummary .= sprintf("%s: %s\n", $tagDetails['tag_details']->question_tag_name, sprintf(__('Your grade is %d%%', 'wp_courseware'), $tagDetails['score_total']));
                        } else {
                            $msgSummary .= sprintf("%s: %s\n", $tagDetails['tag_details']->question_tag_name, sprintf(__('%d out of %d correct (%d%%)', 'wp_courseware'), $tagDetails['score_correct_questions'], $tagDetails['question_count'], $tagDetails['score_total']));
                        }
                        // end of question type check
                    }
                }
                $emailData = str_replace('{QUIZ_GRADES_BY_TAG}', $msgSummary, $emailData);
                break;
            case '{CUSTOM_FEEDBACK}':
                // Use existing frontend code to get the list of custom feedback messages.
                $customFeedback = false;
                $msgList = $feObj->fetch_customFeedbackMessage_calculateMessages();
                if (!empty($msgList)) {
                    $customFeedback = apply_filters('wpcw_email_feedback_separator_top', "\n\n------\n\n");
                    foreach ($msgList as $singleMsg) {
                        // Separate each custom feedback message slightly
                        $customFeedback .= $singleMsg . apply_filters('wpcw_email_feedback_separator', "\n\n------\n\n");
                    }
                }
                $emailData = str_replace('{CUSTOM_FEEDBACK}', $customFeedback, $emailData);
                break;
        }
    }
    return $emailData;
}