learndash_data_reports_headers

apply_filters( 'learndash_data_reports_headers',  array $data_headers )

Filters data reports headers.


Description #


Parameters #

$data_headers

(array) An array of data report header details.


Source #

File: includes/admin/classes-data-reports-actions/class-learndash-admin-data-reports-user-courses.php


Examples #

 <?php
/**
 * Example usage for learndash_data_reports_headers filter.
 */
add_filter(
	'learndash_data_reports_headers',
	function( $data_headers, $data_slug ) {

		if ( $data_slug == 'user-courses' ) {

			// Example on how to change the output column header label used.
			// if ( isset( $data_headers['customer_id'] ) ) {
			// $data_headers['customer_id']['label'] = 'Customer ID';
			// }
			// if ( isset( $data_headers['course_title'] ) ) {
			// $data_headers['course_title']['label'] = 'Course Title';
			// }

			// As an example assume you want to add two new custom columns called 'customer_id' and 'payroll_id. These fields are already stored as part of the WP user meta.
			// Below note on the 'display' parameter we are assigning the same callback function. This is NOT a requirement. You can assigne different callback functions as needed.
			if ( ! isset( $data_headers['customer_id'] ) ) {
				$data_headers['customer_id'] = array(
					'label'   => 'customer_id',
					'default' => '',
					'display' => 'ld_custom_reporting_column',
				);
			}

			if ( ! isset( $data_headers['payroll_id'] ) ) {
				$data_headers['payroll_id'] = array(
					'label'   => 'payroll_id',
					'default' => '',
					'display' => 'ld_custom_reporting_column',
				);
			}
		} elseif ( $data_slug == 'user-quizzes' ) {
				// Example on how to change the output column header label used.
				// if ( isset( $data_headers['quiz_time_spent'] ) ) {
				// $data_headers['quiz_time_spent']['label'] = 'Test Time Used';
				// }
				// if ( isset( $data_headers['quiz_points_total'] ) ) {
				// $data_headers['quiz_points_total']['label'] = 'Test Points Total';
				// }
		}

		// Always return $data_headers
		return $data_headers;
	},
	50,
	2
);


/**
 * This is an exmaple of a custom reporting column. The function is setup via the 'learndash_data_reports_headers' filter to add the custom report header.
 *
 * @param $column_value The current value of the header. Default is empty ''.
 * @param $column_key stirng This is the unique column key your assigned when adding the report header via the 'learndash_data_reports_headers' hook.
 * @param $report_item object This is the activity object. This contains some reference information like course_id, course title, lesson_id, lesson_title, etc.
 * @param report_user object This is the user object associated with the                                                                            $report_item. For example the user id is $report_user->ID
 *
 * @return $column_value string
 */
function ld_custom_reporting_column( $column_value = '', $column_key, $report_item, $report_user ) {

	switch ( $column_key ) {
		case 'customer_id':
			if ( $report_user instanceof WP_User ) {
				$column_value = get_user_meta( $report_user->ID, 'customer_id', true );
			}
			break;

		case 'payroll_id':
			if ( $report_user instanceof WP_User ) {
				$column_value = get_user_meta( $report_user->ID, 'payroll_id', true );
			}
			break;

	}

	// always return $column_value
	return $column_value;
}

add_filter(
	'learndash_data_reports_headers',
	function( $data_headers, $data_slug ) {

		if ( $data_slug == 'user-courses' ) {

			// Example on how to change the output column header label used.
			if ( isset( $data_headers['customer_id'] ) ) {
				$data_headers['customer_id']['label'] = 'Customer ID';
			}
			if ( isset( $data_headers['course_title'] ) ) {
				$data_headers['course_title']['label'] = 'Course Title';
			}
		} elseif ( $data_slug == 'user-quizzes' ) {
			if ( isset( $data_headers['quiz_time_spent'] ) ) {
				$data_headers['quiz_time_spent']['label'] = 'Test Time Used';
			}
			if ( isset( $data_headers['quiz_points_total'] ) ) {
				$data_headers['quiz_points_total']['label'] = 'Test Points Total';
			}
		}

		// Always return $data_headers
		return $data_headers;
	},
	50,
	2
);

add_filter(
	'learndash_data_reports_headers',
	function( $data_headers, $data_slug ) {

		if ( 'user-quizzes' === $data_slug ) {
			if ( ! isset( $data_headers['quiz_category'] ) ) {
				$data_headers['quiz_category'] = array(
					'label'   => 'quiz_category',
					'default' => '',
					'display' => 'ld_custom_quiz_reporting_column',
				);
			}

			if ( ! isset( $data_headers['quiz_topic'] ) ) {
				$data_headers['quiz_topic'] = array(
					'label'   => 'quiz_topic',
					'default' => '',
					'display' => 'ld_custom_quiz_reporting_column',
				);
			}
		}

		// Always return $data_headers.
		return $data_headers;
	},
	50,
	2
);


function ld_custom_quiz_reporting_column( $column_value = '', $column_key, $report_item, $report_user ) {

	switch ( $column_key ) {
		case 'quiz_category':
			$quiz_terms = get_the_terms( $report_item->post_id, 'ld_quiz_category' );
			if ( ! empty( $quiz_terms ) ) {
				foreach ( $quiz_terms as $quiz_term ) {
					if ( ! empty( $column_value ) ) {
						$column_value .= ', ';
					}
					$column_value .= $quiz_term->name;
				}
			}
			break;

		case 'quiz_topic':
			// Uncomment the line below to return specific parent topic.
			// $parent_post_id = learndash_course_get_single_parent_step( $report_item->activity_course_id, $report_item->post_id, 'sfwd-topic' );

			// Or this line will return any parent topic, lesson, course or none
			$parent_post_id = learndash_course_get_single_parent_step( $report_item->activity_course_id, $report_item->post_id );
			if ( ! empty( $parent_post_id ) ) {
				$column_value = $parent_post_id;
			}
			break;

	}

	// always return $column_value
	return $column_value;
}

add_filter(
	'learndash_data_reports_headers',
	function( $data_headers = array(), $data_slug = '' ) {

		if ( $data_slug == 'user-quizzes' ) {
			if ( isset( $data_headers['user_name'] ) ) {

				// For the Courses we want to replace the default 'name' column with two columns for 'first name' and 'last name'.
				// So best option is to loop over the array until we get to the 'name' key. During the looping we transfer other
				// columns to a new array.
				$data_headers_new = array();

				foreach ( $data_headers as $data_headers_key => $data_headers_meta ) {
					if ( $data_headers_key == 'user_name' ) {
						// If we found the 'name' key then we DON'T add it to the $data_headers_new
						$data_headers_new['first_name'] = array(
							'label'   => 'first_name',
							'default' => '',
							'display' => 'ld_custom_quiz_reporting_column',
						);

						$data_headers_new['last_name'] = array(
							'label'   => 'last_name',
							'default' => '',
							'display' => 'ld_custom_quiz_reporting_column',
						);
					} else {
						$data_headers_new[ $data_headers_key ] = $data_headers_meta;
					}
				}

				// Finally, after looping we copy out new data headers to the variable to be returned.
				$data_headers = $data_headers_new;
			}

			if ( ! isset( $data_headers['FIO'] ) ) {
				$data_headers['FIO'] = array(
					'label'   => 'FIO',
					'default' => '',
					'display' => 'ld_custom_quiz_reporting_column',
				);
			}

			if ( ! isset( $data_headers['course_id'] ) ) {
				$data_headers['course_id'] = array(
					'label'   => 'course_id',
					'default' => '',
					'display' => 'ld_custom_quiz_reporting_column',
				);
			}

			if ( ! isset( $data_headers['course_title'] ) ) {
				// change the 'display' element to call our function.
				$data_headers['course_title'] = array(
					'label'   => 'course_title',
					'default' => '',
					'display' => 'ld_custom_quiz_reporting_column',
				);
			}
		}

		// always return $data_headers
		return $data_headers;
	},
	10,
	2
);


function ld_custom_quiz_reporting_column( $column_value = '', $column_key, $report_item, $report_user ) {

	switch ( $column_key ) {

		case 'first_name':
			if ( $report_user instanceof WP_User ) {
				$column_value = get_user_meta( $report_user->ID, 'first_name', true );
			}
			break;

		case 'last_name':
			if ( $report_user instanceof WP_User ) {
				$column_value = get_user_meta( $report_user->ID, 'last_name', true );
			}
			break;

		case 'FIO':
			// I'm not sure if 'FIO' is a user meta field ot something else.
			// if ( $report_user instanceof WP_User ) {
			// $column_value = get_user_meta( $report_user->ID, 'last_name', true );
			// }
			break;

		case 'course_id':
			// Check to ensure we have a valid quiz post_id
			if ( ( property_exists( $report_item, 'post_id' ) ) && ( ! empty( $report_item->post_id ) ) ) {
				$course_id = learndash_get_course_id( $report_item->post_id );
				if ( ! empty( $course_id ) ) {
					$column_value = $course_id;
				}
			}
			break;

		case 'course_title':
			if ( ( property_exists( $report_item, 'post_id' ) ) && ( ! empty( $report_item->post_id ) ) ) {
				$course_id = learndash_get_course_id( $report_item->post_id );
				if ( ! empty( $course_id ) ) {
					$column_value = get_the_title( $course_id );
				}
			}
			break;

	}

	// always return $column_value
	return $column_value;
}

add_filter(
	'learndash_data_reports_headers',
	function( $data_headers, $data_slug ) {
		if ( isset( $data_headers['user_name'] ) ) {
			$data_headers['user_name'] = array(
				'label'   => 'name',
				'default' => '',
				'display' => 'ld_custom_reporting_column',
			);
		}

		// Always return $data_headers
		return $data_headers;
	},
	50,
	2
);

function ld_custom_reporting_column( $column_value = '', $column_key, $report_item, $report_user ) {
	if ( $column_key == 'user_name' ) {
		switch ( $column_key ) {
			case 'user_name':
				if ( $report_user instanceof WP_User ) {
					// Check if the display name is the same as the email address
					if ( $report_user->user_email == $report_user->display_name ) {
						$column_value = get_user_meta( $report_user->ID, 'first_name', true ) . ' ' . get_user_meta( $report_user->ID, 'last_name', true );
					} else {
						// Otherwise return the display name
						$column_value = $report_user->display_name;
					}
				}
				break;

		}
	}

	// always return $column_value
	return $column_value;
}