Send email notification when a student finishes a course

Would it be possible to send an email notification to the course docent or facilitator when a student finishes a course?

I’d like to be able to send this notification to the docent or facilitator of a course after a student finishes it even if this docent or facilitator is another user different from the site admin.

On this ticket a member asked for this, but this is from 2015 so I’m not sure if this will work with the current version of the plugin: https://premium.wpmudev.org/forums/topic/coursepress-course-completion-notifications-for-the-teacher

  • Panos

    Hey there Julius Huitema ,

    I don't see there is a specific hook which we can use for this. The following snippet seems to be doing the trick :

    add_filter( 'the_content', function( $content ){
    
    	global $wp, $post;
    	if ( ! empty( $wp->query_vars['course_completion'] ) ) {
    
    		$send_to 			= array();
    		$email_subject 		= 'A student has completed a course!';
    		$email_content 		= '';
    		$course_title 		= $post->post_title;
    		$student 			= get_user_by( 'ID', get_current_user_id() );
    		$instructors 		= (array) CoursePress_Data_Course::get_setting( $post->ID, 'instructors', array() );
    		$assessment_link 	= add_query_arg(
    								array(
    									'post_type' => 'course',
    									'page' => 'coursepress_assessments',
    									'view' => 'profile',
    									'course_id' => $post->ID,
    									'student_id' => $student->data->ID,
    								),
    								admin_url( 'edit.php' )
    							);
    
    		foreach ( $instructors as $instructor_id ) {
    
    			$instructor = get_user_by( 'ID', $instructor_id );
    			if ( $instructor instanceof WP_User ) {
    				$send_to[] = $instructor->data->user_email;
    			}
    		}
    
    		$email_content = "Student {$student->data->display_name} has completed course {$course_title}\r\n";
    		$email_content .= "You can view replies and grade them from here {$assessment_link}\r\n";
    
    		wp_mail( $send_to, $email_subject, $email_content );
    	}
    
    	return $content;
    } );

    Copy the above little snippet in a mu-plugin
    You will probably want to do some changes in the subject and the content. Please let me know if you need any help there.

    Hope this helps :slight_smile:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.