Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Register the quiz fields in the WPF settings
- function wpf_add_learndash_quiz_fields( $meta_fields ) {
- $args = array(
- 'post_type' => 'sfwd-quiz',
- 'nopaging' => true,
- );
- $quizzes = get_posts( $args );
- foreach ( $quizzes as $quiz ) {
- $meta_fields[ 'quiz_' . $quiz->ID . '_pass' ] = array(
- 'label' => $quiz->post_title . ' - Pass',
- 'type' => 'checkbox',
- 'group' => 'learndash_progress',
- );
- $meta_fields[ 'quiz_' . $quiz->ID . '_pass_date' ] = array(
- 'label' => $quiz->post_title . ' - Pass Date',
- 'type' => 'date',
- 'group' => 'learndash_progress',
- );
- $meta_fields[ 'quiz_' . $quiz->ID . '_fail' ] = array(
- 'label' => $quiz->post_title . ' - Fail',
- 'type' => 'checkbox',
- 'group' => 'learndash_progress',
- );
- $meta_fields[ 'quiz_' . $quiz->ID . '_fail_date' ] = array(
- 'label' => $quiz->post_title . ' - Fail Date',
- 'type' => 'date',
- 'group' => 'learndash_progress',
- );
- }
- return $meta_fields;
- }
- add_filter( 'wpf_meta_fields', 'wpf_add_learndash_quiz_fields' );
- // Sync the pass / fail data to the custom fields
- function wpf_quiz_completed( $data, $user ) {
- if ( isset( $data['quiz']->ID ) ) {
- $quiz_id = $data['quiz']->ID;
- } else {
- // For grading in the admin
- $quiz_id = $data['quiz'];
- }
- if ( true == $data['pass'] ) {
- $update_data = array(
- 'quiz_' . $quiz_id . '_pass' => true,
- 'quiz_' . $quiz_id . '_pass_date' => time(),
- 'quiz_' . $quiz_id . '_fail' => false,
- );
- } elseif ( false == $data['pass'] ) {
- $update_data = array(
- 'quiz_' . $quiz_id . '_pass' => false,
- 'quiz_' . $quiz_id . '_fail' => true,
- 'quiz_' . $quiz_id . '_fail_date' => time(),
- );
- }
- wp_fusion()->user->push_user_meta( $user->ID, $update_data );
- }
- add_action( 'learndash_quiz_completed', 'wpf_quiz_completed', 5, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement