Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active November 15, 2018 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/7b003196e9c7fa66e404cdbf41487720 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/7b003196e9c7fa66e404cdbf41487720 to your computer and use it in GitHub Desktop.
CoursePress Progress Shortcode
<?php
/**
* Plugin Name: CoursePress Progress Shortcode
* Plugin URI: https://premium.wpmudev.org/
* Description: Use the [my_course_completion course_id=50] shortcode to display a table to the logged in user with the course & unit progress.
* Version: 1.0.0
* Author: Konstantinos Xenos @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
add_shortcode( 'my_course_completion', 'my_cp_completion' );
function my_cp_completion( $atts ) {
$a = shortcode_atts( array( 'course_id' => '' ), $atts );
$content = '<table>';
if ( ! empty( $a['course_id'] ) && intval( $a['course_id'] ) && is_user_logged_in() ) {
$student_id = get_current_user_id();
$progress = CoursePress_Data_Student::get_completion_data( $student_id, intval( $a['course_id'] ) );
if ( ! empty( $progress['completion']['progress'] ) ) {
$content .= '<tr><td colspan="2"><strong>' . __( 'Course:' ) . ' ' . get_the_title( $a['course_id'] ) . '</strong></td></tr>';
$content .= '<tr><td colspan="2">' . __( 'Total Progress:' ) . ' ' . $progress['completion']['progress'] . ' %</td></tr>';
$content .= '<tr><td colspan="2"><strong>' . __( 'Units:' ) . '</strong></td></tr>';
$units = CoursePress_Data_Course::get_unit_ids( $a['course_id'], array( 'publish' ), false );
foreach ( $units as $unit_id ) {
$content .= '<tr>';
$content .= '<td>' . get_the_title( $unit_id ) . '</td>';
$content .= '<td>' . CoursePress_Data_Student::get_unit_progress( $student_id, $a['course_id'], $unit_id ) . ' %</td>';
$content .= '</tr>';
}
} else {
$content .= '<tr><td>' . __( 'No progress found.' ) . '</td></tr>';
}
} else {
$content .= '<tr><td>' . __( 'Please enter Course ID.' ) . '</td></tr>';
}
$content .= '</table>';
return $content;
}
add_shortcode( 'my_course_current_grades', 'my_cp_current_grades' );
function my_cp_current_grades() {
if ( CoursePress_Data_Course::is_course( get_the_ID() ) ) {
$course_id = get_the_ID();
} elseif ( CoursePress_Data_Course::is_course( wp_get_post_parent_id( get_the_ID() ) ) ) {
$course_id = wp_get_post_parent_id( get_the_ID() );
}
$student_id = get_current_user_id();
if ( ! empty( $course_id ) ) {
$student_progress = CoursePress_Data_Student::get_completion_data( $student_id, $course_id );
$units = CoursePress_Data_Course::get_unit_ids( $course_id, array( 'publish' ), false );
$content = '<table>';
$content .= '<tr><td colspan="2">' . __( 'Your current scores.' ) . '</td></tr>';
foreach ( $units as $unit_id ) {
$content .= '<tr>';
$content .= '<td colspan="2">' . get_the_title( $unit_id ) . '</td>';
$modules_ids = CoursePress_Data_Module::get_modules_ids_by_unit( $unit_id );
sort( $modules_ids );
foreach ( $modules_ids as $module_id ) {
$grade = CoursePress_Data_Student::get_grade( $student_id, $course_id, $unit_id, $module_id, false, false, $student_progress );
$content .= '<tr>';
$content .= '<td>' . get_the_title( $module_id ) . '</td>';
if ( ! empty( $grade['grade'] ) ) {
$content .= '<td>' . $grade['grade'] . '</td>';
} else {
$content .= '<td>0</td>';
}
$content .= '</tr>';
}
$content .= '</tr>';
}
$content .= '</table>';
return $content;
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment