Instantly share code, notes, and snippets.

Embed
What would you like to do?
[CoursePress] - Responses in Certificate
<?php
/**
* Plugin Name: [CoursePress] - Responses in Certificate
* Plugin URI: https://premium.wpmudev.org/
* Description: Include Questions and Responses in Certificates
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
add_filter( 'coursepress_basic_certificate_vars', function( $vars ){
if(
//is_admin() ||
! isset( $_REQUEST['action'] ) || 'certificate' != $_REQUEST['action'] ||
! isset( $_REQUEST['course_id'] ) || ! isset( $_REQUEST['student_id'] )
){
return;
}
$replacement = '';
$course_id = intval( $_REQUEST['course_id'] );
$student_id = intval( $_REQUEST['student_id'] );
$units = CoursePress_Data_Course::get_units_with_modules( $course_id );
// We will need completion data for input-text and input-textarea responses
$text_responses = array();
$completion_data = CoursePress_Data_Student::get_completion_data( $student_id, $course_id );
//Responses of text or textarea modules
foreach( $completion_data['units'] as $unit_id => $unit_data ){
if( isset( $unit_data['responses'] ) && ! empty( $unit_data['responses'] ) ){
foreach( $unit_data['responses'] as $module_id => $response_data ){
$text_responses[ $module_id ] = $response_data[0]['response'];
}
}
}
foreach( $units as $unit_id => $unit_data ){
$pages = $unit_data['pages'];
foreach ( $pages as $page_num => $page_data ) {
$modules = $page_data['modules'];
if( ! empty( $modules ) ){
$replacement .= '<ul>';
foreach( $modules as $module_id => $module ){
$response = '';
$attributes = CoursePress_Data_Module::attributes( $module_id );
switch ( $attributes['module_type'] ) {
case 'input-checkbox':
$answers = $attributes['answers'];
$answers_selected = $attributes['answers_selected'];
if( ! empty( $answers_selected ) ){
foreach( $answers_selected as $selected ){
if( isset( $answers[$selected] ) ){
$response .= $answers[$selected];
}
}
}
if( isset( $answers[$selected] ) ){
$response = $answers[$selected];
}
break;
case 'input-select':
case 'input-radio':
$selected = $attributes['answers_selected'];
if( isset( $answers[$selected] ) ){
$response = $answers[$selected];
}
break;
case 'input-text':
case 'input-textarea':
$response = isset( $text_responses[ $module_id ] ) ? $text_responses[ $module_id ] : '';
break;
}
$replacement .= "<li>{$module->post_title} <p>{$response}</p></li>";
}
$replacement .= '</ul>';
}
}
}
if( '' != $replacement ){
$vars['STUDENT_REPLIES'] = $replacement;
}
return $vars;
}, 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment