public/wordpress/wp-content/plugins/wp-courseware/includes/legacy/admin/functions.php
/**
 * Generates a verbose output of the gradebook data for a specific course.
 *
 * @since 1.0.0
 */
Generates a verbose output of the gradebook data for a specific course.
Generates a verbose output of the gradebook data for a specific course .
function WPCW_data_export_gradebookData() {
	global $wpcwdb, $wpdb;
	$current_user = wp_get_current_user();
	if ( ! user_can( $current_user, 'view_wpcw_courses' ) ) {
		wp_die( __( 'You do not have sufficient permissions to access this page.', 'wp-courseware' ) );
	}
	$courseDetails = false;
	if ( isset( $_GET['course_id'] ) && $courseID = $_GET['course_id'] ) {
		$courseDetails = WPCW_courses_getCourseDetails( $courseID );
	}
	if ( ! $courseDetails ) {
		wp_die( __( 'Sorry, but that course could not be found.', 'wp-courseware' ) );
	}
	if ( ! user_can( $current_user, 'manage_wpcw_settings' ) && $current_user->ID != $courseDetails->course_author ) {
		wp_die( __( 'You do not have sufficient permissions to access this course gradebook data.', 'wp-courseware' ) );
	}
	$quizzesForCourse = WPCW_quizzes_getAllQuizzesForCourse( $courseDetails->course_id );
	if ( ! $quizzesForCourse ) {
		header( 'Content-Type: text/plain' );
		_e( 'There are no quizzes for this course, therefore no grade information to show.', 'wp-courseware' );
		return;
	}
		$usingCertificates = ( 'use_certs' == $courseDetails->course_opt_use_certificate );
	$quizIDList = array();
	foreach ( $quizzesForCourse as $singleQuiz ) {
		$quizIDList[] = $singleQuiz->quiz_id;
	}
	$quizIDListForSQL = '(' . implode( ',', $quizIDList ) . ')';
	$csvFilename = sanitize_title( $courseDetails->course_title ) . "-gradebook-" . date( "Y-m-d" ) . ".csv";
	WPCW_data_export_sendHeaders_CSV( $csvFilename );
	$out = fopen( 'php://output', 'w' );
	$headings = array(
		__( 'Name', 'wp-courseware' ),
		__( 'Username', 'wp-courseware' ),
		__( 'Email Address', 'wp-courseware' ),
		__( 'Course Progress', 'wp-courseware' ),
		__( 'Cumulative Grade', 'wp-courseware' ),
		__( 'Has Grade Been Sent?', 'wp-courseware' ),
		__( 'Course Completion Date', 'wp-courseware' ),
	);
	if ( $usingCertificates ) {
		$headings[] = __( 'Is Certificate Available?', 'wp-courseware' );
	}
	foreach ( $quizzesForCourse as $singleQuiz ) {
		$headings[] = sprintf( '%s (quiz_%d)', $singleQuiz->quiz_title, $singleQuiz->quiz_id );
	}
	fputcsv( $out, $headings );
	$SQL = $wpdb->prepare( "
		SELECT *
		FROM $wpcwdb->user_courses uc
		LEFT JOIN $wpdb->users u ON u.ID = uc.user_id
		WHERE uc.course_id = %d
		  AND u.ID IS NOT NULL
		", $courseDetails->course_id );
	$userData = $wpdb->get_results( $SQL );
	if ( ! $userData ) {
		fclose( $out );
		return;
	}
	foreach ( $userData as $userObj ) {
		$quizResults = WPCW_quizzes_getQuizResultsForUser( $userObj->ID, $quizIDListForSQL );
		$quizScoresSoFar       = 0;
		$quizScoresSoFar_count = 0;
		$thisUsersQuizData = array();
		foreach ( $quizIDList as $aQuizID ) {
			if ( isset( $quizResults[ $aQuizID ] ) ) {
				$theResults            = $quizResults[ $aQuizID ];
				$theResults->quiz_data = maybe_unserialize( $theResults->quiz_data );
				if ( $theResults->quiz_needs_marking > 0 ) {
					$thisUsersQuizData[ 'quiz_' . $aQuizID ] = __( 'Manual Grade Required', 'wp-courseware' );
				} 
				elseif ( 'quiz_fail_no_retakes' == $theResults->quiz_next_step_type ) {
					$thisUsersQuizData[ 'quiz_' . $aQuizID ] = __( 'Quiz Retakes Exhausted', 'wp-courseware' );
				} 
				elseif ( 'incomplete' == $theResults->quiz_paging_status ) {
					$thisUsersQuizData[ 'quiz_' . $aQuizID ] = __( 'In Progress', 'wp-courseware' );
				} else { 
					$score           = number_format( $theResults->quiz_grade );
					$quizScoresSoFar += $score;
					$thisUsersQuizData[ 'quiz_' . $aQuizID ] = $score . '%';
					$quizScoresSoFar_count ++;
				}
			} else { 
				$thisUsersQuizData[ 'quiz_' . $aQuizID ] = __( 'Not Taken', 'wp-courseware' );
			}
		}
		$dataToOutput = array();
		$dataToOutput['name']          = $userObj->display_name;
		$dataToOutput['username']      = $userObj->user_login;
		$dataToOutput['email_address'] = $userObj->user_email;
		$dataToOutput['course_progress']     = $userObj->course_progress . '%';
		$dataToOutput['cumulative_grade']    = ( $quizScoresSoFar_count > 0 ? number_format( ( $quizScoresSoFar / $quizScoresSoFar_count ), 1 ) . '%' : __( '-', 'wp-courseware' ) );
		$dataToOutput['has_grade_been_sent'] = ( 'sent' == $userObj->course_final_grade_sent ? __( 'Yes', 'wp-courseware' ) : __( 'No', 'wp-courseware' ) );
		$dataToOutput['completion_date'] = __( 'N/A', 'wp-courseware' );
		$userProgress                    = new WPCW_UserProgress( $courseDetails->course_id, $userObj->ID );
		$courseCompDate                  = $userProgress->courseCompletedDate();
				if ( $courseCompDate ) {
			$date_localFormat                = get_option( 'date_format' );
			$date_str                        = date_i18n( $date_localFormat, strtotime( $courseCompDate ) );
			$dataToOutput['completion_date'] = $date_str;
		}
		if ( $usingCertificates ) {
			$dataToOutput['is_certificate_available'] = __( 'No', 'wp-courseware' );
			if ( WPCW_certificate_getCertificateDetails( $userObj->ID, $courseDetails->course_id, false ) ) {
				$dataToOutput['is_certificate_available'] = __( 'Yes', 'wp-courseware' );
			}
		}
		$dataToOutput += $thisUsersQuizData;
		fputcsv( $out, $dataToOutput );
	}
	fclose( $out );
	die();
}
function WPCW_data_export_gradebookData ( ) { global $ wpcwdb , $ wpdb ; $ current_user = wp_get_current_user ( ) ; if ( ! user_can ( $ current_user , 'view_wpcw_courses' ) ) { wp_die ( __ ( 'You do not have sufficient permissions to access this page.' , 'wp-courseware' ) ) ; } $ courseDetails = false ; if ( isset ( $ _GET [ 'course_id' ] ) && $ courseID = $ _GET [ 'course_id' ] ) { $ courseDetails = WPCW_courses_getCourseDetails ( $ courseID ) ; } if ( ! $ courseDetails ) { wp_die ( __ ( 'Sorry, but that course could not be found.' , 'wp-courseware' ) ) ; } if ( ! user_can ( $ current_user , 'manage_wpcw_settings' ) && $ current_user -> ID != $ courseDetails -> course_author ) { wp_die ( __ ( 'You do not have sufficient permissions to access this course gradebook data.' , 'wp-courseware' ) ) ; } $ quizzesForCourse = WPCW_quizzes_getAllQuizzesForCourse ( $ courseDetails -> course_id ) ; if ( ! $ quizzesForCourse ) { header ( 'Content-Type: text/plain' ) ; _e ( 'There are no quizzes for this course, therefore no grade information to show.' , 'wp-courseware' ) ; return ; } $ usingCertificates = ( 'use_certs' == $ courseDetails -> course_opt_use_certificate ) ; $ quizIDList = array ( ) ; foreach ( $ quizzesForCourse as $ singleQuiz ) { $ quizIDList [ ] = $ singleQuiz -> quiz_id ; } $ quizIDListForSQL = '(' . implode ( ',' , $ quizIDList ) . ')' ; $ csvFilename = sanitize_title ( $ courseDetails -> course_title ) . " -gradebook- " . date ( " Y-m-d " ) . " .csv " ; WPCW_data_export_sendHeaders_CSV ( $ csvFilename ) ; $ out = fopen ( 'php://output' , 'w' ) ; $ headings = array ( __ ( 'Name' , 'wp-courseware' ) , __ ( 'Username' , 'wp-courseware' ) , __ ( 'Email Address' , 'wp-courseware' ) , __ ( 'Course Progress' , 'wp-courseware' ) , __ ( 'Cumulative Grade' , 'wp-courseware' ) , __ ( 'Has Grade Been Sent?' , 'wp-courseware' ) , __ ( 'Course Completion Date' , 'wp-courseware' ) , ) ; if ( $ usingCertificates ) { $ headings [ ] = __ ( 'Is Certificate Available?' , 'wp-courseware' ) ; } foreach ( $ quizzesForCourse as $ singleQuiz ) { $ headings [ ] = sprintf ( '%s (quiz_%d)' , $ singleQuiz -> quiz_title , $ singleQuiz -> quiz_id ) ; } fputcsv ( $ out , $ headings ) ; $ SQL = $ wpdb -> prepare ( " 
		SELECT *
		FROM  $ wpcwdb -> user_courses  uc
		LEFT JOIN  $ wpdb -> users  u ON u.ID = uc.user_id
		WHERE uc.course_id = %d
		  AND u.ID IS NOT NULL
		 " , $ courseDetails -> course_id ) ; $ userData = $ wpdb -> get_results ( $ SQL ) ; if ( ! $ userData ) { fclose ( $ out ) ; return ; } foreach ( $ userData as $ userObj ) { $ quizResults = WPCW_quizzes_getQuizResultsForUser ( $ userObj -> ID , $ quizIDListForSQL ) ; $ quizScoresSoFar = 0 ; $ quizScoresSoFar_count = 0 ; $ thisUsersQuizData = array ( ) ; foreach ( $ quizIDList as $ aQuizID ) { if ( isset ( $ quizResults [ $ aQuizID ] ) ) { $ theResults = $ quizResults [ $ aQuizID ] ; $ theResults -> quiz_data = maybe_unserialize ( $ theResults -> quiz_data ) ; if ( $ theResults -> quiz_needs_marking > 0 ) { $ thisUsersQuizData [ 'quiz_' . $ aQuizID ] = __ ( 'Manual Grade Required' , 'wp-courseware' ) ; } elseif ( 'quiz_fail_no_retakes' == $ theResults -> quiz_next_step_type ) { $ thisUsersQuizData [ 'quiz_' . $ aQuizID ] = __ ( 'Quiz Retakes Exhausted' , 'wp-courseware' ) ; } elseif ( 'incomplete' == $ theResults -> quiz_paging_status ) { $ thisUsersQuizData [ 'quiz_' . $ aQuizID ] = __ ( 'In Progress' , 'wp-courseware' ) ; } else { $ score = number_format ( $ theResults -> quiz_grade ) ; $ quizScoresSoFar += $ score ; $ thisUsersQuizData [ 'quiz_' . $ aQuizID ] = $ score . '%' ; $ quizScoresSoFar_count ++ ; } } else { $ thisUsersQuizData [ 'quiz_' . $ aQuizID ] = __ ( 'Not Taken' , 'wp-courseware' ) ; } } $ dataToOutput = array ( ) ; $ dataToOutput [ 'name' ] = $ userObj -> display_name ; $ dataToOutput [ 'username' ] = $ userObj -> user_login ; $ dataToOutput [ 'email_address' ] = $ userObj -> user_email ; $ dataToOutput [ 'course_progress' ] = $ userObj -> course_progress . '%' ; $ dataToOutput [ 'cumulative_grade' ] = ( $ quizScoresSoFar_count > 0 ? number_format ( ( $ quizScoresSoFar / $ quizScoresSoFar_count ) , 1 ) . '%' : __ ( '-' , 'wp-courseware' ) ) ; $ dataToOutput [ 'has_grade_been_sent' ] = ( 'sent' == $ userObj -> course_final_grade_sent ? __ ( 'Yes' , 'wp-courseware' ) : __ ( 'No' , 'wp-courseware' ) ) ; $ dataToOutput [ 'completion_date' ] = __ ( 'N/A' , 'wp-courseware' ) ; $ userProgress = new WPCW_UserProgress ( $ courseDetails -> course_id , $ userObj -> ID ) ; $ courseCompDate = $ userProgress -> courseCompletedDate ( ) ; if ( $ courseCompDate ) { $ date_localFormat = get_option ( 'date_format' ) ; $ date_str = date_i18n ( $ date_localFormat , strtotime ( $ courseCompDate ) ) ; $ dataToOutput [ 'completion_date' ] = $ date_str ; } if ( $ usingCertificates ) { $ dataToOutput [ 'is_certificate_available' ] = __ ( 'No' , 'wp-courseware' ) ; if ( WPCW_certificate_getCertificateDetails ( $ userObj -> ID , $ courseDetails -> course_id , false ) ) { $ dataToOutput [ 'is_certificate_available' ] = __ ( 'Yes' , 'wp-courseware' ) ; } } $ dataToOutput += $ thisUsersQuizData ; fputcsv ( $ out , $ dataToOutput ) ; } fclose ( $ out ) ; die ( ) ; }
Generates a verbose output of the gradebook data for a specific course.
Generates a verbose output of the gradebook data for a specific course .
// Globals // Current User // If user is not allowed to view courses, then redirect to home page // 1 - See if the course exists first. // Course does not exist, simply output an error using plain text. // Check permissions on course // 2 - Need a list of all quizzes for this course, excluding surveys. // Handle situation when there are no quizzes. // Do we want certificates?
 // Create a simple list of IDs to use in SQL queries // Convert list of IDs into an SQL list // Course does exist, so now we really output the data // Start CSV // 3 - The headings for the CSV data // Check if we're using certificates or not. // 4 - Add the headings for the quiz titles. // 6 - Render the headings // 7 - Select all users that exist for this course // 8 - Render the specific user details. // Track cumulative data // Track the quiz scores in order // Now render results for each quiz // Got progress data, process the result // Extract results and unserialise the data array. // We've got something that needs grading. // User is blocked - they've failed and are blocked // Quiz not yet complete... // No quizzes need marking, so show the scores as usual. // Calculate score, and use for cumulative. // No progress data - quiz not completed yet // These must be in the order of the columns specified above for it all to match up. // Progress Details // Course completion date // Do we have a course completion date?
 // Make the date look pretty // Show if there's a certificate that can be downloaded. // Output the quiz summary here..