Created
February 24, 2018 22:03
-
-
Save wpmudev-sls/b7f49ecb14471d6cd14869bc80a0717f to your computer and use it in GitHub Desktop.
[CoursePress] - Responses in Certificate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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