public/wordpress/wp-content/plugins/wp-courseware/includes/legacy/pages/page-gradebook.php
/**
 * Handle sending out emails to users when they have completed the course and we're sending them their final grade.
 *
 * @since 1.0.0
 *
 * @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.
 */
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.
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 ) {
	set_time_limit( 0 );
	global $wpdb, $wpcwdb;
	$wpdb->show_errors();
	$usersNeedGrades_SQL = $wpdb->prepare( "
		SELECT *
		FROM $wpcwdb->user_courses uc
		LEFT JOIN $wpdb->users u ON u.ID = uc.user_id
		WHERE uc.course_id = %d
		  AND u.ID IS NOT NULL
		  AND uc.course_progress = 100
		  AND uc.course_final_grade_sent != 'sent'
		", $courseDetails->course_id );
	$usersNeedGrades = $wpdb->get_results( apply_filters( "wpcw_back_query_filter_gradebook_users_final_grades_email", $usersNeedGrades_SQL, $courseDetails ) );
	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 );
	$headers = false;
	if ( $courseDetails->course_from_email ) {
		$headers = sprintf( 'From: %s <%s>' . "\r\n", $courseDetails->course_from_name, $courseDetails->course_from_email );
	}
	printf( '<div id="wpcw_gradebook_email_progress">' );
	printf( '<h3>%s <b>%d %s</b>...</h3>', __( 'Sending final grade emails to', 'wp-courseware' ), $totalUserCount, _n( 'user', 'users', $totalUserCount, 'wp-courseware' ) );
	$quizIDList       = array();
	$quizIDListForSQL = false;
	$quizzesForCourse = WPCW_quizzes_getAllQuizzesForCourse( $courseDetails->course_id );
	if ( $quizzesForCourse ) {
		foreach ( $quizzesForCourse as $singleQuiz ) {
			$quizIDList[ $singleQuiz->quiz_id ] = $singleQuiz;
		}
		$quizIDListForSQL = '(' . implode( ',', array_keys( $quizIDList ) ) . ')';
	}
	$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' ) );
		$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 );
		$quizGradeMessage = "\n";
		if ( ! empty( $quizIDList ) ) {
			$quizResults = WPCW_quizzes_getQuizResultsForUser( $aSingleUser->ID, $quizIDListForSQL );
			$quizScoresSoFar       = 0;
			$quizScoresSoFar_count = 0;
			foreach ( $quizIDList as $aQuizID => $singleQuiz ) {
				if ( isset( $quizResults[ $aQuizID ] ) ) {
					$theResults            = $quizResults[ $aQuizID ];
					$theResults->quiz_data = maybe_unserialize( $theResults->quiz_data );
					if ( $theResults->quiz_needs_marking == 0 ) {
						$score           = number_format( $theResults->quiz_grade );
						$quizScoresSoFar += $score;
						$quizScoresSoFar_count++;
						$quizGradeMessage .= sprintf( "%s #%d - %s\n%s: %s%%\n\n", __( 'Quiz', 'wp-courseware' ), $quizScoresSoFar_count, $singleQuiz->quiz_title, __( 'Grade', 'wp-courseware' ), $score );
					}
				} 
			}
		} 
		$cumulativeGrade = ( $quizScoresSoFar_count > 0 ? number_format( ( $quizScoresSoFar / $quizScoresSoFar_count ), 1 ) . '%' : __( '-', 'wp-courseware' ) );
		$messageBody = str_ireplace( '{QUIZ_SUMMARY}', trim( $quizGradeMessage ), $messageBody );
		$messageBody = str_ireplace( '{CUMULATIVE_GRADE}', $cumulativeGrade, $messageBody );
		$targetEmail = $aSingleUser->user_email;
		if ( ! wp_mail( $targetEmail, $messageSubject, $messageBody, $headers ) ) {
			error_log( 'WPCW_email_sendEmail() - email did not send.' );
		}
		$wpdb->query( $wpdb->prepare( "
		    	UPDATE $wpcwdb->user_courses
		    	   SET course_final_grade_sent = 'sent'
		    	WHERE user_id = %d
		    	  AND course_id = %d
		    ", $aSingleUser->ID, $courseDetails->course_id ) );
		flush();
		$userCount++;
	}
	printf( '<h3>%s</h3>', __( 'All done.', 'wp-courseware' ) );
	printf( '</div>' );
}
function WPCW_showPage_GradeBook_handleFinalGradesEmail ( $ courseDetails , $ page ) { set_time_limit ( 0 ) ; global $ wpdb , $ wpcwdb ; $ wpdb -> show_errors ( ) ; $ usersNeedGrades_SQL = $ wpdb -> prepare ( " 
		SELECT *
		FROM  $ wpcwdb -> user_courses  uc
		LEFT JOIN  $ wpdb -> users  u ON u.ID = uc.user_id
		WHERE uc.course_id = %d
		  AND u.ID IS NOT NULL
		  AND uc.course_progress = 100
		  AND uc.course_final_grade_sent != 'sent'
		 " , $ courseDetails -> course_id ) ; $ usersNeedGrades = $ wpdb -> get_results ( apply_filters ( " wpcw_back_query_filter_gradebook_users_final_grades_email " , $ usersNeedGrades_SQL , $ courseDetails ) ) ; 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 ) ; $ headers = false ; if ( $ courseDetails -> course_from_email ) { $ headers = sprintf ( 'From: %s <%s>' . " \r \n " , $ courseDetails -> course_from_name , $ courseDetails -> course_from_email ) ; } printf ( '<div id="wpcw_gradebook_email_progress">' ) ; printf ( '<h3>%s <b>%d %s</b>...</h3>' , __ ( 'Sending final grade emails to' , 'wp-courseware' ) , $ totalUserCount , _n ( 'user' , 'users' , $ totalUserCount , 'wp-courseware' ) ) ; $ quizIDList = array ( ) ; $ quizIDListForSQL = false ; $ quizzesForCourse = WPCW_quizzes_getAllQuizzesForCourse ( $ courseDetails -> course_id ) ; if ( $ quizzesForCourse ) { foreach ( $ quizzesForCourse as $ singleQuiz ) { $ quizIDList [ $ singleQuiz -> quiz_id ] = $ singleQuiz ; } $ quizIDListForSQL = '(' . implode ( ',' , array_keys ( $ quizIDList ) ) . ')' ; } $ 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' ) ) ; $ 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 ) ; $ quizGradeMessage = " \n " ; if ( ! empty ( $ quizIDList ) ) { $ quizResults = WPCW_quizzes_getQuizResultsForUser ( $ aSingleUser -> ID , $ quizIDListForSQL ) ; $ quizScoresSoFar = 0 ; $ quizScoresSoFar_count = 0 ; foreach ( $ quizIDList as $ aQuizID => $ singleQuiz ) { if ( isset ( $ quizResults [ $ aQuizID ] ) ) { $ theResults = $ quizResults [ $ aQuizID ] ; $ theResults -> quiz_data = maybe_unserialize ( $ theResults -> quiz_data ) ; if ( $ theResults -> quiz_needs_marking == 0 ) { $ score = number_format ( $ theResults -> quiz_grade ) ; $ quizScoresSoFar += $ score ; $ quizScoresSoFar_count ++ ; $ quizGradeMessage .= sprintf ( " %s #%d - %s \n %s: %s%% \n \n " , __ ( 'Quiz' , 'wp-courseware' ) , $ quizScoresSoFar_count , $ singleQuiz -> quiz_title , __ ( 'Grade' , 'wp-courseware' ) , $ score ) ; } } } } $ cumulativeGrade = ( $ quizScoresSoFar_count > 0 ? number_format ( ( $ quizScoresSoFar / $ quizScoresSoFar_count ) , 1 ) . '%' : __ ( '-' , 'wp-courseware' ) ) ; $ messageBody = str_ireplace ( '{QUIZ_SUMMARY}' , trim ( $ quizGradeMessage ) , $ messageBody ) ; $ messageBody = str_ireplace ( '{CUMULATIVE_GRADE}' , $ cumulativeGrade , $ messageBody ) ; $ targetEmail = $ aSingleUser -> user_email ; if ( ! wp_mail ( $ targetEmail , $ messageSubject , $ messageBody , $ headers ) ) { error_log ( 'WPCW_email_sendEmail() - email did not send.' ) ; } $ wpdb -> query ( $ wpdb -> prepare ( " 
		    	UPDATE  $ wpcwdb -> user_courses 
		    	   SET course_final_grade_sent = 'sent'
		    	WHERE user_id = %d
		    	  AND course_id = %d
		     " , $ aSingleUser -> ID , $ courseDetails -> course_id ) ) ; flush ( ) ; $ userCount ++ ; } printf ( '<h3>%s</h3>' , __ ( 'All done.' , 'wp-courseware' ) ) ; printf ( '</div>' ) ; }
Handle sending out emails to users when they have completed the course and we're sending them their final grade.
Handle sending out emails to users when they have completed the course and we ' re sending them their final grade .
// This could take a long time, hence setting time limit to unlimited. // Get users to email final grades to // Allow the list of users to email to be customised. // Abort if there's nothing to do, showing a useful error message to the user. // ### Email Template - Construct the from part of the email // Start the status pane to wrap the updates. // Little summary of how many users there are. // Get all the quizzes for this course // Create a simple list of IDs to use in SQL queries // Convert list of IDs into an SQL list // Run through each user, and generate their details. // Work out what tags we have to replace in the body and subject and replace // the generic ones. // Generate the data for all of the quizzes, and add it to the email. // Only add quiz summary if we have one! // Get quiz results for this user // Track cumulative data // ### Now render results for each quiz // Got progress data, process the result // Extract results and unserialise the data array. // We've got something that needs grading. // Calculate score, and use for cumulative. // Add to string with the quiz name and each grade. // end of quiz result check. // end of check for quizzes for course // Calculate the cumulative grade // Now replace the cumulative grades. // Set up the target email address // Send the actual email // Update the user record to mark as being sent // Tell the user we're complete.