Is there an action hook for when a student successfully completes a course

I need to send email notifications to the instructors and students when a course is successfully completed. How would you suggest I do this, I am happy to write my own function, I just need to know how to hook into your plugin.

Thanks in advance.

  • Michelle Shull
    • DEV MAN’s Apprentice

    Hi, Graham!

    Student completion is defined in wp-content/plugins/coursepress/includes/classes/class.student.completion.php, where you'll see how we determine the course progress for each student, based on the course ID and student ID. I think that file should get you on the right track.

    Thanks for your question!

  • Graham
    • Site Builder, Child of Zeus

    Hi, I have found this thread https://premium.wpmudev.org/forums/topic/coursepress-redirect-to-a-different-page-when-course-completed and I have implemented the code in my functions.php file but it does not work. No page redirect on completed course. Here is the exact code I am using.

    add_action('coursepress_set_course_completed' , 'my_custom_redirect' , 10 , 2) ; 
    
    function my_custom_redirect ( $student_id , $course_id ) {
    $location = "http://itaperth.com.au/test-course-complete/" ;
    wp_safe_redirect( $location, 200 ) ;
    }

    Would really like some help with this.

  • Graham
    • Site Builder, Child of Zeus

    Hi Michelle, thank you for your reply. As I stated above "...I have implemented the code in my functions.php file but it does not work. " so I have already dropped into my functions.php file but it does not do anything.

    Regards

  • Graham
    • Site Builder, Child of Zeus

    Hi Michelle

    Thank you for your reply. I think the suggestion of using the mu plugin to achieve this is over the top. All I want to know is why the hooked function, I posted on this thread, is not working when another WPMU Dev staff member (Jude) told another member it would?

    All I want to do is hook into the course completed action so that I can send emails. I am using the redirect just for testing purposes and once I get the hook working I will alter the function code to send the emails I want.

    Regards
    Graham J OConnor

  • Michelle Shull
    • DEV MAN’s Apprentice

    Hi Graham!

    That's fine if you'd prefer not to use an mu plugin. I sent Jude a note to see if he's got some insight into the code snippet above, and I'm going to invite our second level support team in to have a look, as well. They're our code gurus, they may be able to help you craft the exact function you need to send an email upon course completion.

    Thanks, Graham.

  • Graham
    • Site Builder, Child of Zeus

    Hi Michelle, thanks for your reply.

    All I really need is a function that hooks into the completed course action hook for testing purposes then once I know that the hook is working I will write my own function to send out emails, until your developers release a version of Coursepress Pro that does this.

    Best Regards
    Graham J OConnor

  • Jude
    • DEV MAN

    Hey Graham

    Just chipping in here, the architecture of the plugin has changed considerably since I made that post.

    We now have a class that specifically tracks completion called class.student.completion.php.

    You will need to use the static is_course_complete($course_ID, $user_ID) function from the Student Completion Class to check if a course is completed now.

    Cheers
    Judr

  • Jose
    • Bruno Diaz

    Hi there Graham,

    In addition to what Jude said above, I want to clarify that even while the action 'coursepress_set_course_completed' is still present in the code, it is not being triggered anymore.

    The workaround suggested using is_course_complete() is correct and it would work.

    Nevertheless, you still need a hook where to run this check. (it wouldn't be a good approach to check this on every page load for all the courses and students in your site, of course).

    So, I coded this little Must-use plugin that will trigger an action when the course progress is updated.

    if(! class_exists('course_completion_notification') ):
    
        class course_completion_notification{
    
            function __construct(){
                add_action( 'updated_user_meta', array($this, 'on_updated_user_meta'), 20, 4);
            }
    
            function on_updated_user_meta($meta_id, $object_id, $meta_key, $_meta_value){
                $course_progress_pattern = '/_course_(\d+)_progress/';
                if( preg_match( $course_progress_pattern, $meta_key, $matches )){
                    $student_ID = $object_id;
                    $course_ID = $matches[1];
                    do_action( 'coursepress_course_progress_update', $course_ID, $student_ID, $_meta_value);
                }
            }
    
         }
        new course_completion_notification;
    endif;

    I'm attaching a zipped version of the plugin here, so you only need to drop the plugin into the folder ../wp-content/mu-plugins/ (create the folder if it doesn't exists).

    Now, with this plugin running in your site, you can use the following code in your plugin or theme's functions.php:

    function on_course_progress_update( $course_ID, $student_ID, $progress_data ){
        if(!empty($progress_data['course_progress'])){
            if( 100 == $progress_data['course_progress']){
                //Send notification.
            } else {
                //In case you want to do something when progress updates.
            }
        }
    }
    
    add_action( 'coursepress_course_progress_update', 'on_course_progress_update', 10, 3 );

    *You should note that the progress update might run more than once on each page load. So you should make sure that your logic handles this and checks that only one notification is sent.

    Please let me know if this works fine for you.

    Cheers,
    Jose

  • David
    • WPMU DEV Initiate

    Although this is an old topic, I have recently been looking for the same functionality: to send emails to teacher(s) and student upon course completion.

    I came across this post in my research and used the coursepress_set_course_completed hook, however, I discovered that if I viewed the Courses page (where the list of courses are) when logged in as a student, the coursepress_set_course_completed hook would trigger. I only need to catch the course completion once, so having this trigger when viewing a page was an undesired outcome.

    I did some research into the coursepress_set_course_completed hook and came across the add_course_completed_meta function which is run by this hook. Within this function lies another hook coursepress_student_course_completed. This hook has the following comment: Will only fire once when a course is marked as complete, should not trigger again.

    Bingo! Exactly what I was looking for. Now when a course is completed by a student, the coursepress_student_course_completed is triggered and my custom function can run to send out the required emails.

    Hope this helps anyone else looking for similar functionality.

    (I also added this info to: https://premium.wpmudev.org/forums/topic/coursepress-redirect-to-a-different-page-when-course-completed

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.