Contents

Core::bp_get_courses_progress( $user_id,  $sort_order = 'desc' )

Description

Source

File: bp-integrations/learndash/core/Core.php

453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
public function bp_get_courses_progress ( $user_id, $sort_order = 'desc' ) {
    $course_completion_percentage = array();
 
    if ( ! $course_completion_percentage = wp_cache_get ( $user_id, 'ld_courses_progress' ) ) {
        $course_progress = get_user_meta( $user_id, '_sfwd-course_progress', true );
 
        if ( ! empty( $course_progress ) ) {
 
            foreach ( $course_progress as $course_id => $coursep ) {
                // We take default progress value as 1 % rather than 0%
                $course_completion_percentage[ $course_id ] = 1;//
 
                if ( $coursep['total'] == 0 ) {
                    continue;
                }
 
                $course_steps_count = learndash_get_course_steps_count( $course_id );
                $course_steps_completed = learndash_course_get_completed_steps( $user_id, $course_id, $coursep );
 
                $completed_on = get_user_meta( $user_id, 'course_completed_' . $course_id, true );
                if ( !empty( $completed_on ) ) {
 
                    $coursep['completed'] = $course_steps_count;
                    $coursep['total'] = $course_steps_count;
 
                } else {
                    $coursep['total'] = $course_steps_count;
                    $coursep['completed'] = $course_steps_completed;
 
                    if ( $coursep['completed'] > $coursep['total'] )
                        $coursep['completed'] = $coursep['total'];
                }
 
                // cannot divide by 0
                if ( $coursep['total'] == 0 ) {
                    $course_completion_percentage[ $course_id ] = 0;
                } else {
                    $course_completion_percentage[ $course_id ] = ceil( ( $coursep['completed'] * 100 ) / $coursep['total'] );
                }
            }
        }
 
        //Avoid running the queries multiple times if user's course progress is empty
        $course_completion_percentage = !empty( $course_completion_percentage ) ? $course_completion_percentage : 'empty';
 
        wp_cache_set( $user_id, $course_completion_percentage, 'ld_courses_progress' );
    }
 
    $course_completion_percentage = 'empty' !== $course_completion_percentage ? $course_completion_percentage : array();
 
    if ( !empty( $course_completion_percentage ) ) {
        // Sort.
        if ( 'asc' == $sort_order ) {
            asort( $course_completion_percentage );
        } else {
            arsort( $course_completion_percentage );
        }
    }
 
    return $course_completion_percentage;
}

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.