public/wordpress/wp-content/plugins/wp-courseware/includes/legacy/admin/functions.php
/**
 * Function that handles the export of the survey responses for a specified survey.
 *
 * @since 1.0.0
 */
Function that handles the export of the survey responses for a specified survey.
Function that handles the export of the survey responses for a specified survey .
function WPCW_data_export_quizSurveyData() {
	global $wpcwdb, $wpdb;
	$quizID       = absint( trim( WPCW_arrays_getValue( $_GET, 'quiz_id' ) ) );
	$quizDetails  = WPCW_quizzes_getQuizDetails( $quizID, true, false, false );
	$current_user = wp_get_current_user();
	if ( ! $quizDetails ) {
		wp_die( __( 'Sorry, could not find that survey to export the response data.', 'wp-courseware' ) );
	}
	if ( 'survey' != $quizDetails->quiz_type ) {
		wp_die( __( 'Sorry, but the selected item is not a survey, it\'s a quiz.', 'wp-courseware' ) );
	}
	if ( ! user_can( $current_user, 'view_wpcw_courses' ) ) {
		wp_die( __( 'You do not have sufficient permissions to export this survey data.', 'wp-courseware' ) );
	}
	if ( ! user_can( $current_user, 'manage_wpcw_settings' ) && $current_user->ID != $quizDetails->quiz_author ) {
		wp_die( __( 'You do not have sufficient permissions to export this survey data.', 'wp-courseware' ) );
	}
	if ( WPCW_quizzes_doesQuizContainRandomQuestions( $quizDetails ) ) {
		$quizDetails->questions = WPCW_quizzes_randomQuestions_fullyExpand( $quizDetails );
	}
	$csvFileName = WPCW_urls_createCleanURL( 'survey-' . $quizDetails->quiz_id . '-' . $quizDetails->quiz_title ) . '.csv';
	WPCW_data_export_sendHeaders_CSV( $csvFileName );
	$headings = array(
		__( 'Trainee WP ID', 'wp-courseware' ),
		__( 'Trainee Name', 'wp-courseware' ),
		__( 'Trainee Email Address', 'wp-courseware' ),
	);
	$questionListForColumns = array();
	if ( ! empty( $quizDetails->questions ) ) {
		foreach ( $quizDetails->questions as $questionID => $questionDetails ) {
			$questionListForColumns[ $questionID ] = $questionDetails->question_question;
			$headings[] = $questionDetails->question_question;
		}
	}
	$out = fopen( 'php://output', 'w' );
	fputcsv( $out, $headings );
	$answers = $wpdb->get_results( $wpdb->prepare( "
		SELECT *
		FROM $wpcwdb->user_progress_quiz
		WHERE quiz_id = %d
	", $quizDetails->quiz_id ) );
	if ( ! empty( $answers ) ) {
		foreach ( $answers as $answerDetails ) {
			$resultData = array();
			$resultData[] = $answerDetails->user_id;
			$userDetails = get_userdata( $answerDetails->user_id );
			if ( $userDetails ) {
				$resultData[] = $userDetails->display_name;
				$resultData[] = $userDetails->user_email;
			} 
			else {
				$resultData[] = __( 'User no longer on system.', 'wp-courseware' );
				$resultData[] = __( 'N/A', 'wp-courseware' );
			}
			$theirResponses = maybe_unserialize( $answerDetails->quiz_data );
			if ( ! empty( $questionListForColumns ) ) {
				foreach ( $questionListForColumns as $questionID => $questionTitle ) {
					if ( isset( $theirResponses[ $questionID ] ) && isset( $theirResponses[ $questionID ]['their_answer'] ) ) {
						if ( is_array( $theirResponses[ $questionID ]['their_answer'] ) ) {
							$resultData[] = implode( ",", $theirResponses[ $questionID ]['their_answer'] );
						} else {
							$resultData[] = $theirResponses[ $questionID ]['their_answer'];
						}
					} 
					else {
						$resultData[] = __( 'No answer for this question.', 'wp-courseware' );
					}
				}
			}
			fputcsv( $out, $resultData );
		}
	}
	fclose( $out );
	die();
}
function WPCW_data_export_quizSurveyData ( ) { global $ wpcwdb , $ wpdb ; $ quizID = absint ( trim ( WPCW_arrays_getValue ( $ _GET , 'quiz_id' ) ) ) ; $ quizDetails = WPCW_quizzes_getQuizDetails ( $ quizID , true , false , false ) ; $ current_user = wp_get_current_user ( ) ; if ( ! $ quizDetails ) { wp_die ( __ ( 'Sorry, could not find that survey to export the response data.' , 'wp-courseware' ) ) ; } if ( 'survey' != $ quizDetails -> quiz_type ) { wp_die ( __ ( 'Sorry, but the selected item is not a survey, it\'s a quiz.' , 'wp-courseware' ) ) ; } if ( ! user_can ( $ current_user , 'view_wpcw_courses' ) ) { wp_die ( __ ( 'You do not have sufficient permissions to export this survey data.' , 'wp-courseware' ) ) ; } if ( ! user_can ( $ current_user , 'manage_wpcw_settings' ) && $ current_user -> ID != $ quizDetails -> quiz_author ) { wp_die ( __ ( 'You do not have sufficient permissions to export this survey data.' , 'wp-courseware' ) ) ; } if ( WPCW_quizzes_doesQuizContainRandomQuestions ( $ quizDetails ) ) { $ quizDetails -> questions = WPCW_quizzes_randomQuestions_fullyExpand ( $ quizDetails ) ; } $ csvFileName = WPCW_urls_createCleanURL ( 'survey-' . $ quizDetails -> quiz_id . '-' . $ quizDetails -> quiz_title ) . '.csv' ; WPCW_data_export_sendHeaders_CSV ( $ csvFileName ) ; $ headings = array ( __ ( 'Trainee WP ID' , 'wp-courseware' ) , __ ( 'Trainee Name' , 'wp-courseware' ) , __ ( 'Trainee Email Address' , 'wp-courseware' ) , ) ; $ questionListForColumns = array ( ) ; if ( ! empty ( $ quizDetails -> questions ) ) { foreach ( $ quizDetails -> questions as $ questionID => $ questionDetails ) { $ questionListForColumns [ $ questionID ] = $ questionDetails -> question_question ; $ headings [ ] = $ questionDetails -> question_question ; } } $ out = fopen ( 'php://output' , 'w' ) ; fputcsv ( $ out , $ headings ) ; $ answers = $ wpdb -> get_results ( $ wpdb -> prepare ( " 
		SELECT *
		FROM  $ wpcwdb -> user_progress_quiz 
		WHERE quiz_id = %d
	 " , $ quizDetails -> quiz_id ) ) ; if ( ! empty ( $ answers ) ) { foreach ( $ answers as $ answerDetails ) { $ resultData = array ( ) ; $ resultData [ ] = $ answerDetails -> user_id ; $ userDetails = get_userdata ( $ answerDetails -> user_id ) ; if ( $ userDetails ) { $ resultData [ ] = $ userDetails -> display_name ; $ resultData [ ] = $ userDetails -> user_email ; } else { $ resultData [ ] = __ ( 'User no longer on system.' , 'wp-courseware' ) ; $ resultData [ ] = __ ( 'N/A' , 'wp-courseware' ) ; } $ theirResponses = maybe_unserialize ( $ answerDetails -> quiz_data ) ; if ( ! empty ( $ questionListForColumns ) ) { foreach ( $ questionListForColumns as $ questionID => $ questionTitle ) { if ( isset ( $ theirResponses [ $ questionID ] ) && isset ( $ theirResponses [ $ questionID ] [ 'their_answer' ] ) ) { if ( is_array ( $ theirResponses [ $ questionID ] [ 'their_answer' ] ) ) { $ resultData [ ] = implode ( " , " , $ theirResponses [ $ questionID ] [ 'their_answer' ] ) ; } else { $ resultData [ ] = $ theirResponses [ $ questionID ] [ 'their_answer' ] ; } } else { $ resultData [ ] = __ ( 'No answer for this question.' , 'wp-courseware' ) ; } } } fputcsv ( $ out , $ resultData ) ; } } fclose ( $ out ) ; die ( ) ; }
Function that handles the export of the survey responses for a specified survey.
Function that handles the export of the survey responses for a specified survey .
// Globals // Vars // Check that we can find the survey. // Ensure it's a survey // If user is not allowed to view courses, then redirect to home page // Check permissions // Does this survey contain random questions? If so, then we need to get the full question data // of all possible questions // Create a URL-safe version of the filename. // The headings // Extract the questions to use as headings. // See if we have any questions in the list. // Add this question to the headings. // Start CSV // Push out the question headings. // The responses to the questions // Process eacy response from the user, extracting their details too. // We've definitely got the ID // See if we can get the name and email address. // User has been deleted. // Extract their responses into an array // Go through answers logically now // Put something in the column, even if there is no answer.