Courses

Class for all courses related functions

Description

Source

File: bp-integrations/learndash/buddypress/Courses.php

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
class Courses
{
    /**
     * Constructor
     *
     * @since BuddyBoss 1.0.0
     */
    public function __construct()
    {
        add_action('bp_ld_sync/init', [$this, 'init']);
    }
 
    /**
     * Add actions once integration is ready
     *
     * @since BuddyBoss 1.0.0
     */
    public function init()
    {
        add_action('bp_ld_sync/courses_loop/after_title', [$this, 'showUserProgress'], 10);
        add_action('bp_ld_sync/courses_loop/after_title', [$this, 'showGroupProgress'], 20);
        add_action('bp_ld_sync/courses_loop/after_title', [$this, 'showCourseButton'], 30);
    }
 
    /**
     * Output user course progress html
     *
     * @since BuddyBoss 1.0.0
     */
    public function showUserProgress()
    {
        if (! groups_is_user_member(bp_loggedin_user_id(), groups_get_current_group()->id)) {
            return;
        }
 
        if (groups_is_user_admin(bp_loggedin_user_id(), groups_get_current_group()->id)) {
            return;
        }
 
        $label = __('Your Progress', 'buddyboss');
        $progress = $this->getUserCourseProgress();
        require bp_locate_template('groups/single/courses-progress.php', false, false);
    }
 
    /**
     * Output group course progress html
     *
     * @since BuddyBoss 1.0.0
     */
    public function showGroupProgress()
    {
        $label = __('Group Progress', 'buddyboss');
        $progress = $this->getGroupCourseProgress();
        require bp_locate_template('groups/single/courses-progress.php', false, false);
    }
 
    /**
     * Output course link button html
     *
     * @since BuddyBoss 1.0.0
     */
    public function showCourseButton()
    {
        if (! is_user_logged_in()) {
            return;
        }
 
        $label  = $this->getUserCourseViewButtonLabel(bp_loggedin_user_id(), get_the_ID());
        require bp_locate_template('groups/single/courses-view-button.php', false, false);
    }
 
    /**
     * Returns the courses belongs to the group
     *
     * @since BuddyBoss 1.0.0
     */
    public function getGroupCourses() {
        $group_id  = bp_ld_sync( 'buddypress' )->helpers->getLearndashGroupId( groups_get_current_group()->id );
        $courseIds = learndash_group_enrolled_courses( $group_id );
 
        /**
         * Filter to update course lists
         */
        return array_map( 'get_post', apply_filters( 'bp_ld_learndash_group_enrolled_courses', $courseIds, $group_id ) );
    }
 
    /**
     * Get user's course progress
     *
     * @since BuddyBoss 1.0.0
     */
    public function getUserCourseProgress($courseId = null, $userId = null)
    {
        if (! $courseId) {
            $courseId = get_the_ID();
        }
 
        if (! $userId) {
            $userId = bp_loggedin_user_id();
        }
 
        $totalSteps = learndash_get_course_steps_count($courseId);
 
        if ($totalSteps == 0) {
            return 0;
        }
 
        $userSteps  = learndash_course_get_completed_steps($userId, $courseId);
 
        return round($userSteps / $totalSteps * 100);
    }
 
    /**
     * Get gorup's course progress
     *
     * @since BuddyBoss 1.0.0
     */
    public function getGroupCourseProgress($courseId = null)
    {
        if (! $courseId) {
            $courseId = get_the_ID();
        }
 
        $members    = groups_get_group_members();
        $totalSteps = learndash_get_course_steps_count($courseId);
 
        if ($members['count'] == 0) {
            return 0;
        }
 
        if ($totalSteps == 0) {
            return 0;
        }
 
        $totalSteps  = learndash_get_course_steps_count($courseId);
        $memberSteps = array_sum(array_map(function($member) use ($courseId) {
            return learndash_course_get_completed_steps($member->id, $courseId);
        }, $members['members']));
 
        return round($memberSteps / ($members['count'] * $totalSteps) * 100);
    }
 
    /**
     * Returns dynamic label text based on user's course progress
     *
     * @since BuddyBoss 1.0.0
     */
    public function getUserCourseViewButtonLabel($userId, $courseId)
    {
        $label  = __('Start Course', 'buddyboss');
        $status = learndash_course_status($courseId, $userId, true);
 
        if ('in-progress' === $status) {
            $label = __( 'Continue', 'buddyboss' );
        }
 
        if ('completed' === $status) {
            $label = __( 'View Course', 'buddyboss' );
        }
 
        return $label;
    }
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Methods

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.