Tutor LMS: Email the Instructor When a Quiz Is Submitted
Emails the course author each time a student finishes a Tutor LMS quiz, with the score, so instructors can follow up or grade open questions quickly.
Opens WPIDE on
Code
if ( ! function_exists( 'tutor_utils' ) ) {
return;
}
add_action( 'tutor_quiz/attempt_ended', function ( $attempt_id, $course_id = 0, $user_id = 0 ) {
$attempt = tutor_utils()->get_attempt( $attempt_id );
if ( ! $attempt ) {
return;
}
$course_id = $course_id ? (int) $course_id : (int) ( $attempt->course_id ?? 0 );
$student = get_userdata( $user_id ? (int) $user_id : (int) ( $attempt->user_id ?? 0 ) );
$author = get_userdata( (int) get_post_field( 'post_author', $course_id ) );
if ( ! $student || ! $author ) {
return;
}
$total = (float) ( $attempt->total_marks ?? 0 );
$score = $total > 0 ? round( (float) $attempt->earned_marks / $total * 100 ) : 0;
wp_mail(
$author->user_email,
sprintf( 'Quiz submitted: %s', get_the_title( (int) $attempt->quiz_id ) ),
sprintf( "%s finished \"%s\" in \"%s\" with a score of %d%%.\n\nReview attempts: %s", $student->display_name, get_the_title( (int) $attempt->quiz_id ), get_the_title( $course_id ), $score, admin_url( 'admin.php?page=tutor_quiz_attempts' ) )
);
}, 10, 3 );
Notes
The email goes to the author of the course the quiz belongs to.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.