bp_learndash_get_users_certificates( string $user_id = '' )

Function to get list of certificates the user has

Description

Parameters

$user_id

(Optional)

Default value: ''

Return

(array|bool)

Source

File: bp-integrations/learndash/bp-learndash-functions.php

function bp_learndash_get_users_certificates( $user_id = '' ) {
	if ( empty( $user_id ) ) {
		return false;
	}

	/**
	 * Course Certificate
	 **/
	$user_courses = ld_get_mycourses( $user_id, array() );
	$certificates = array();
	foreach ( $user_courses as $course_id ) {

		$certificateLink = learndash_get_course_certificate_link( $course_id, $user_id );
		$filename        = "Certificate.pdf";
		$course_title    = get_the_title( $course_id );
		$certificate_id  = learndash_get_setting( $course_id, 'certificate' );
		$image           = '';

		if ( ! empty( $certificate_id ) ) {
			$certificate_data = get_post( $certificate_id );
			$filename         = sanitize_file_name( $course_title ) . "-" . sanitize_file_name( $certificate_data->post_title ) . ".pdf";
			$image            = wp_get_attachment_url( get_post_thumbnail_id( $certificate_id ) );
		}

		$date = get_user_meta( $user_id, 'course_completed_' . $course_id, true );

		if ( ! empty( $certificateLink ) ) {
			$certificate           = new \stdClass();
			$certificate->ID       = $course_id;
			$certificate->link     = $certificateLink;
			$certificate->title    = get_the_title( $course_id );
			$certificate->filename = $filename;
			$certificate->date     = date_i18n( "Y-m-d h:i:s", $date );
			$certificate->time     = $date;
			$certificate->type     = 'course';
			$certificates[]        = $certificate;
		}
	}

	/**
	 * Quiz Certificate
	 **/
	$quizzes  = get_user_meta( $user_id, '_sfwd-quizzes', true );
	$quiz_ids = empty( $quizzes ) ? array() : wp_list_pluck( $quizzes, 'quiz' );
	if ( ! empty( $quiz_ids ) ) {
		$quiz_total_query_args = array(
			'post_type' => 'sfwd-quiz',
			'fields'    => 'ids',
			'orderby'   => 'title', //$atts['quiz_orderby'],
			'order'     => 'ASC', //$atts['quiz_order'],
			'nopaging'  => true,
			'post__in'  => $quiz_ids
		);
		$quiz_query            = new \WP_Query( $quiz_total_query_args );
		$quizzes_tmp           = array();
		foreach ( $quiz_query->posts as $post_idx => $quiz_id ) {
			foreach ( $quizzes as $quiz_idx => $quiz_attempt ) {
				if ( $quiz_attempt['quiz'] == $quiz_id ) {
					$quiz_key                 = $quiz_attempt['time'] . '-' . $quiz_attempt['quiz'];
					$quizzes_tmp[ $quiz_key ] = $quiz_attempt;
					unset( $quizzes[ $quiz_idx ] );
				}
			}
		}
		$quizzes = $quizzes_tmp;
		krsort( $quizzes );
		if ( ! empty( $quizzes ) ) {
			foreach ( $quizzes as $quizdata ) {
				if ( ! in_array( $quizdata['quiz'], wp_list_pluck( $certificates, 'ID' ) ) ) {
					$quiz_settings         = learndash_get_setting( $quizdata['quiz'] );
					$certificate_post_id   = intval( $quiz_settings['certificate'] );
					$certificate_post_data = get_post( $certificate_post_id );
					$certificate_data      = learndash_certificate_details( $quizdata['quiz'], $user_id );
					if ( ! empty( $certificate_data['certificateLink'] ) && $certificate_data['certificate_threshold'] <= $quizdata['percentage'] / 100 ) {
						$filename              = sanitize_file_name( get_the_title( $quizdata['quiz'] ) ) . "-" . sanitize_file_name( get_the_title( $certificate_post_id ) ) . ".pdf";
						$certificate           = new \stdClass();
						$certificate->ID       = $quizdata['quiz'];
						$certificate->link     = $certificate_data['certificateLink'];
						$certificate->title    = get_the_title( $quizdata['quiz'] );
						$certificate->filename = $filename;
						$certificate->date     = date_i18n( "Y-m-d h:i:s", $quizdata['time'] );
						$certificate->time     = $quizdata['time'];
						$certificate->type     = 'quiz';
						$certificates[]        = $certificate;
					}
				}

			}
		}
	}

	usort( $certificates, function ( $a, $b ) {
		return strcmp( $b->time, $a->time );
	} );

	return $certificates;
}

Changelog

Changelog
Version Description
BuddyBoss 1.2.0 Introduced.

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.