Exemplo n.º 1
0
/**
 * Send an email out using a template.
 * 
 * @param Object $unitParentData The parent data for a unit.
 * @param Object $userDetails The details of the user who's done the completing.
 * @param String $targetEmail The email address of the recipient.
 * @param String $subjectTemplate The content of the subject template, before substitutions.
 * @param String $bodyTemplate The content of the email body template, before substitutions. 
 */
function WPCW_email_sendEmail($unitParentData, $userDetails, $targetEmail, $subjectTemplate, $bodyTemplate)
{
    // TODO ZZZ - DEBUG Tool - Email logging
    //error_log(sprintf('Sending Email - WPCW_email_sendEmail(): %s (%s)',  $targetEmail, $subjectTemplate));
    // Replace content in email body first
    $tagList_Body = WPCW_email_getTagList($bodyTemplate);
    $bodyTemplate = WPCW_email_replaceTags_generic($unitParentData, $userDetails, $tagList_Body, $bodyTemplate);
    // Then do subject line
    $tagList_Subject = WPCW_email_getTagList($subjectTemplate);
    $subjectTemplate = WPCW_email_replaceTags_generic($unitParentData, $userDetails, $tagList_Subject, $subjectTemplate);
    //error_log(sprintf('Sending Email - WPCW_email_sendEmail(): %s (%s)',  $messageSubject, $messageBody));
    // Construct the from part of the email
    $headers = false;
    if ($unitParentData->course_from_email) {
        $headers = sprintf('From: %s <%s>' . "\r\n", $unitParentData->course_from_name, $unitParentData->course_from_email);
    }
    // Actually send the email
    if (!wp_mail($targetEmail, $subjectTemplate, $bodyTemplate, $headers)) {
        error_log('WPCW_email_sendEmail() - email did not send.');
    }
}
Exemplo n.º 2
0
/**
 * Handle sending out emails to users when they have completed the course and we're sending them their final grade.
 * 
 * @param Object $courseDetails The details of the course that we're sending details out for.
 * @param PageBuilder $page The page that's rendering the page structure.
 */
function WPCW_showPage_GradeBook_handleFinalGradesEmail($courseDetails, $page)
{
    // This could take a long time, hence setting time limit to unlimited.
    set_time_limit(0);
    global $wpdb, $wpcwdb;
    $wpdb->show_errors();
    // Get users to email final grades to
    $usersNeedGrades_SQL = $wpdb->prepare("\n\t\tSELECT * \n\t\tFROM {$wpcwdb->user_courses} uc\t\t\t\t\t\t\t\t\t\n\t\tLEFT JOIN {$wpdb->users} u ON u.ID = uc.user_id\n\t\tWHERE uc.course_id = %d\n\t\t  AND u.ID IS NOT NULL\n\t\t  AND uc.course_progress = 100\n\t\t  AND uc.course_final_grade_sent != 'sent'\n\t\t", $courseDetails->course_id);
    // Allow the list of users to email to be customised.
    $usersNeedGrades = $wpdb->get_results(apply_filters("wpcw_back_query_filter_gradebook_users_final_grades_email", $usersNeedGrades_SQL, $courseDetails));
    // Abort if there's nothing to do, showing a useful error message to the user.
    if (empty($usersNeedGrades)) {
        $page->showMessage(__('There are currently no users that are eligible to receive their final grade.', 'wp_courseware') . ' ' . __('No emails have been sent.', 'wp_courseware'), true);
        return;
    }
    $totalUserCount = count($usersNeedGrades);
    //WPCW_debug_showArray($courseDetails);
    // ### Email Template - Construct the from part of the email
    $headers = false;
    if ($courseDetails->course_from_email) {
        $headers = sprintf('From: %s <%s>' . "\r\n", $courseDetails->course_from_name, $courseDetails->course_from_email);
    }
    // Start the status pane to wrap the updates.
    printf('<div id="wpcw_gradebook_email_progress">');
    // Little summary of how many users there are.
    printf('<h3>%s <b>%d %s</b>...</h3>', __('Sending final grade emails to', 'wp_courseware'), $totalUserCount, _n('user', 'users', $totalUserCount, 'wp_courseware'));
    // Get all the quizzes for this course
    $quizIDList = array();
    $quizIDListForSQL = false;
    $quizzesForCourse = WPCW_quizzes_getAllQuizzesForCourse($courseDetails->course_id);
    // Create a simple list of IDs to use in SQL queries
    if ($quizzesForCourse) {
        foreach ($quizzesForCourse as $singleQuiz) {
            $quizIDList[$singleQuiz->quiz_id] = $singleQuiz;
        }
        // Convert list of IDs into an SQL list
        $quizIDListForSQL = '(' . implode(',', array_keys($quizIDList)) . ')';
    }
    // Run through each user, and generate their details.
    $userCount = 1;
    foreach ($usersNeedGrades as $aSingleUser) {
        printf('<p>%s (%s) - <b>%d%% %s</b></p>', $aSingleUser->display_name, $aSingleUser->user_email, number_format($userCount / $totalUserCount * 100, 1), __('complete', 'wp_courseware'));
        // Work out what tags we have to replace in the body and subject and replace
        // the generic ones.
        $messageBody = $courseDetails->email_complete_course_grade_summary_body;
        $tagList_Body = WPCW_email_getTagList($messageBody);
        $messageBody = WPCW_email_replaceTags_generic($courseDetails, $aSingleUser, $tagList_Body, $messageBody);
        $messageSubject = $courseDetails->email_complete_course_grade_summary_subject;
        $tagList_Subject = WPCW_email_getTagList($messageSubject);
        $messageSubject = WPCW_email_replaceTags_generic($courseDetails, $aSingleUser, $tagList_Subject, $messageSubject);
        // Generate the data for all of the quizzes, and add it to the email.
        $quizGradeMessage = "\n";
        // Only add quiz summary if we have one!
        if (!empty($quizIDList)) {
            // Get quiz results for this user
            $quizResults = WPCW_quizzes_getQuizResultsForUser($aSingleUser->ID, $quizIDListForSQL);
            // Track cumulative data
            $quizScoresSoFar = 0;
            $quizScoresSoFar_count = 0;
            // ### Now render results for each quiz
            foreach ($quizIDList as $aQuizID => $singleQuiz) {
                // Got progress data, process the result
                if (isset($quizResults[$aQuizID])) {
                    // Extract results and unserialise the data array.
                    $theResults = $quizResults[$aQuizID];
                    $theResults->quiz_data = maybe_unserialize($theResults->quiz_data);
                    // We've got something that needs grading.
                    if ($theResults->quiz_needs_marking == 0) {
                        // Calculate score, and use for cumulative.
                        $score = number_format($theResults->quiz_grade);
                        $quizScoresSoFar += $score;
                        $quizScoresSoFar_count++;
                        // Add to string with the quiz name and each grade.
                        $quizGradeMessage .= sprintf("%s #%d - %s\n%s: %s%%\n\n", __('Quiz', 'wp_courseware'), $quizScoresSoFar_count, $singleQuiz->quiz_title, __('Grade', 'wp_courseware'), $score);
                    }
                }
                // end of quiz result check.
            }
        }
        // end of check for quizzes for course
        // Calculate the cumulative grade
        $cumulativeGrade = $quizScoresSoFar_count > 0 ? number_format($quizScoresSoFar / $quizScoresSoFar_count, 1) . '%' : __('n/a', 'wp_courseware');
        // Now replace the cumulative grades.
        $messageBody = str_ireplace('{QUIZ_SUMMARY}', trim($quizGradeMessage), $messageBody);
        $messageBody = str_ireplace('{CUMULATIVE_GRADE}', $cumulativeGrade, $messageBody);
        // Set up the target email address
        $targetEmail = $aSingleUser->user_email;
        // Send the actual email
        if (!wp_mail($targetEmail, $messageSubject, $messageBody, $headers)) {
            error_log('WPCW_email_sendEmail() - email did not send.');
        }
        // Update the user record to mark as being sent
        $wpdb->query($wpdb->prepare("\n\t\t    \tUPDATE {$wpcwdb->user_courses}\n\t\t    \t   SET course_final_grade_sent = 'sent'\n\t\t    \tWHERE user_id = %d\n\t\t    \t  AND course_id = %d\n\t\t    ", $aSingleUser->ID, $courseDetails->course_id));
        flush();
        $userCount++;
    }
    // Tell the user we're complete.
    printf('<h3>%s</h3>', __('All done.', 'wp_courseware'));
    printf('</div>');
}