Coursepress: I want to allow my students to speak privately with the instructor via Chat plugin

I want to allow my students to speak privately with the instructor via Chat plugin. I am using the plugin Friends and I paste this code on theme functions.php

add_action('coursepress_student_enrolled', 'link_tutor_student_friend', 10, 2);
function link_tutor_student_friend($ID, $course_id) {
$instructors = get_post_meta($course_id, 'instructors', true);
foreach ($instructors as $instructor) {
if ($instructor != get_current_user_id()) {
friends_add($instructor, get_current_user_id(), 1);
}
}
}
How do I get this to work?

  • Lindeni Mahlalela

    Hello Dídac,

    I hope you are doing great today, thank you for contacting us and thank you for your patience as we were working on this.

    I have double checked the source code of CoursePress and have noticed that the meta key for storing the instructors for the course has changed from 'instructors' to 'cp_instructors', so the code you have used will work with old version of CoursePress that was using the 'instructors' meta key to store the instructors in the course meta.

    So, you should change the line that retrieves the instructors from:
    $instructors = get_post_meta($post->ID, 'instructors', true);

    To:
    $instructors = get_post_meta($post->ID, 'cp_instructors', true);

    I have also noticed that the action hook used already supplies the $student_id ($ID in the code above) which can be used instead of 'get_current_user_id()' function. I say this because sometimes you may want to add a student to a course from your dashboard, in which case 'get_current_user_id()' will return your user id (logged in user id) and the function will fail. So instead we can use the $student_id instead of the logged in user id.

    So, in the code we can change the line from:
    friends_add($instructor, get_current_user_id(), 1);

    To:
    friends_add($instructor, $ID, 1); //id supplied in callback function

    So rewriting the all the code we have:

    add_action('coursepress_student_enrolled', 'link_tutor_student_friend', 10, 2);
    function link_tutor_student_friend($student_id, $course_id)
    {
        $instructors = get_post_meta($course_id, 'cp_instructors', true);
        foreach ($instructors as $instructor) {
            if ($instructor != get_current_user_id()) {
    			error_log("Instructor ID: ". $instructor);
                friends_add($instructor, $student_id, 1); //student_id made available by the action hook
            }
        }
    }

    This snippet should add all students subscribed to the course as the instructor's friends in Friends plugin. Ideally, you should test this when logged in as a student, enroll to a course and see what happens. If you are the course instructor and try to enroll, it won't add you as a friend since you are already the instructor.

    I hope this helps. Please let me know if this helps or if you have any further questions regarding this.

    Have a nice day.
    Mahlamusa

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.