[CoursePress Pro] CoursePress Pro Shortcode for Course Progress and Score

Is there a shortcode that I can use to display a users progress and or current score for a given course? I haven't been able to find anything as of yet.

  • Ash

    Hello Ryan

    There is no shortcode, neither this can be easily done. But, I have sent your issue to our second level support team. Please note that, they deal with more complex issues, so there might be little delay. We appreciate your patience on this. Also remember, it totally depends on how long it takes, if it takes too much time then SLS team might refuse. Let's see :slight_smile:

    Have a nice day!

    Cheers,
    Ash

  • Konstantinos Xenos

    Hi Ryan ,

    I've made a simple mu-plugin for you as a start because I will need a little bit more information for the Units addition that you like.

    To add a mu-plugin, navigate to your wp-content directory and create a folder named "mu-plugins" if it doesn't exist, after that create a file inside the mu-plugins folder named "my_custom_cp_shortcodes.php" and paste inside the following code.

    This shortcode will simply return a number for you i.e. 100 if the Course is completed fully. To use this you simply add a shortcode like this: [my_course_completion course_id=20], where course_id=20 is the ID of the course you want to gather the progress from. It will automatically get the student ID for each logged in user.

    <?php
    
    add_shortcode( 'my_course_completion', 'my_cp_completion' );
    function my_cp_completion( $atts ) {
    	// This is the default 'fallback' message if you don't insert a course_id
    	$content = 'Please enter a course_id';
    
    	$a = shortcode_atts( array( 'course_id' => '' ), $atts );
    
    	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 = $progress['completion']['progress'];
    		} else {
    			// This is the message shown if there's no progress on the current Course ID for the logged in member
    			$content = 'No progress found.';
    		}
    	}
    
    	return $content;
    }

    You can tell me if you need further help on the above or refining to fit your needs and I'll help you out.

    Now for the Units part, would you like to have a 'full' table like it's shown in the Workbook for example to see if that's something that could be easily created in a shortcode, or do you want to add a specific Unit ID as well yourself in each shortcode ( this means that I'll extend the above given shortcode and you'll have to use more shortcodes within a page ).

    Tell me your thoughts so I can understand what you'd like to achieve exactly and see what I could do for you.

    Regards,
    Konstantinos

  • Konstantinos Xenos

    Hi Ryan ,

    I've update the previous mu-plugin and you get the new one from here by clicking the Download button on the top right corner, extracting the .zip and again follow the previous process to add it as a mu-plugin on your installation : https://gist.github.com/wpmudev-sls/7b003196e9c7fa66e404cdbf41487720

    When you use the shortcode [my_course_completion course_id=50] it will gather some information about the given Course ( name, total progress, unit names and unit progress ) and it will return a table as seen in the following image

    Tell me if this is closer to what you're looking for and if you'd like any further tweaks.

    Regards,
    Konstantinos

    -----

    Code preview ( and if you want to copy / paste directly instead ):

    <?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;
    }
  • Ryan

    Thank you! However, I guess I misinterpreted your concept. The progress is nice, I can definitely use that, bit what I really am looking for is the current score. For example, the way my site is configured now, the user completes a unit assessment but has no idea what their score is. Can the score be displayed instead of the progress? Also, this doesn't seem like it would work as a sidebar widget as I would have to change the course ID for each course, correct?

  • Konstantinos Xenos

    Hey Ryan , let's make a fresh try now that I had a better scope.

    You can "add" this code into the existing mu-plugin that you've created if you like ( see the end of the post for the code ).

    With this you can add a text widget with the [my_course_current_grades] shortcode, and whenever a student is viewing a course it will automatically show a table with the current Grades that they have aquired i.e.

    This might need some refinement again as each course is somewhat different so I'm not sure how yours are set up and if this will play out of the box, but it most probably will.

    Tell me if we have to add anything extra to this !

    Regards,
    Konstantinos

    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;
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.