I have being trying to create a shortcode to retrieve the information from the csld_events and output in a certificate, but it turnout it is returning every event related to the course_id in the csld_events.
In this situation I could not find a way to only select the events related to the current user, i thought maybe i should try to retrieve it also with the group-id that the course and the student is registered, but until the moment i could not find any way to relate the three of them and be able to retrieve from csld_events the event with course id(object_id
) and group id(ld_group
).
In the wp_posts
table i could find the posts related to group but nothing that would relate them to the course or the users in that group.
In the wp_learndash_user_activity
i could find the activity_status
with group-progress as meta-value with the post_id
which is also the group ID but nothing that would relate it to the course.
Did anybody go through something like this in wordpress + learndash and have a solution for it?
<?php
// Function to retrieve the dates of a course for a specific user
function get_course_dates( $user_id, $course_id ) {
// Retrieve the dates of the course
global $wpdb;
$table_name = $wpdb->prefix . 'csld_events';
$query = $wpdb->prepare( "SELECT start, end FROM $table_name WHERE object_id = %d", $course_id );
$course_dates = $wpdb->get_results( $query, ARRAY_A );
// Return the course dates
return $course_dates;
}
// Register the shortcode to show the course dates
add_shortcode( 'course_dates', 'course_dates_shortcode' );
function course_dates_shortcode( $atts ) {
// Extract the attributes
$atts = shortcode_atts( array(
'user_id' => 0,
'course_id' => 0
), $atts, 'course_dates' );
// If the user ID is not specified, use the current user
$user_id = $atts['user_id'];
if ( !$user_id ) {
$user_id = get_current_user_id();
}
// Retrieve the course dates for the user
$course_dates = get_course_dates( $user_id, $atts['course_id'] );
// Build the output for the shortcode
$output = '';
if ( !empty( $course_dates ) ) {
$output .= '<ul>';
foreach ( $course_dates as $date ) {
$start = date_create($date['start']);
$end = date_create($date['end']);
$output .= '<p>Start: ' . date_format($start,'d/m/Y') . '<br>Eind: ' . date_format($end,'d/m/Y') . '</p>';
}
$output .= '</p>';
}
// Return the output of the shortcode
return $output;
}
?>