Action uncanny-learndash-groups

learndash_essay_all_quiz_data_updated

This action is documented in includes/quiz/ld-quiz-essays.php */ Fires when LearnDash quiz essay scoring data has been updated for a specific quiz and question.

add_action( 'learndash_essay_all_quiz_data_updated', $callback, 10, 2 );

Description

Fires after essay quiz data has been updated for a specific quiz, question, and essay post. Developers can use this hook to perform custom actions or integrations that depend on the updated essay scoring information, such as logging changes or triggering additional notifications.


Usage

add_action( 'learndash_essay_all_quiz_data_updated', 'your_function_name', 10, 2 );

Parameters

$quiz_id (mixed)
- **$question_id** `mixed`
$updated_scoring_data (mixed)
- **$essay_post** `mixed`

Examples

/**
 * Example function to hook into learndash_essay_all_quiz_data_updated.
 * This function demonstrates how to process updated essay quiz data,
 * for example, to log changes or trigger further actions.
 *
 * @param int   $quiz_id              The ID of the quiz.
 * @param int   $question_id          The ID of the essay question.
 * @param array $updated_scoring_data An array containing the updated scoring data for the essay.
 * @param WP_Post $essay_post         The WordPress post object representing the essay.
 */
function my_learndash_essay_update_handler( $quiz_id, $question_id, $updated_scoring_data, $essay_post ) {
    // Check if the $quiz_id, $question_id, and $essay_post are valid.
    if ( ! is_numeric( $quiz_id ) || ! is_numeric( $question_id ) || ! $essay_post instanceof WP_Post ) {
        // Log an error or return early if invalid data is received.
        error_log( 'learndash_essay_all_quiz_data_updated received invalid data.' );
        return;
    }

    // Log the update for debugging purposes.
    $log_message = sprintf(
        'Learndash Essay Quiz Updated: Quiz ID: %d, Question ID: %d. Updated scoring data: %s',
        absint( $quiz_id ),
        absint( $question_id ),
        print_r( $updated_scoring_data, true ) // Use print_r to represent the array structure
    );
    error_log( $log_message );

    // Example: If the essay is marked as graded and has a score.
    if ( isset( $updated_scoring_data['graded'] ) && true === $updated_scoring_data['graded'] && isset( $updated_scoring_data['points'] ) ) {
        $score = floatval( $updated_scoring_data['points'] );
        $user_id = get_post_meta( $essay_post->ID, 'user_id', true ); // Assuming user ID is stored in essay post meta.

        // Example: Send a notification to the user that their essay has been graded.
        if ( $user_id && $score > 0 ) {
            $user = get_user_by( 'id', $user_id );
            if ( $user ) {
                $quiz_title = get_the_title( $quiz_id );
                $subject = sprintf( __( 'Your essay for "%s" has been graded!', 'your-text-domain' ), $quiz_title );
                $message = sprintf(
                    __( 'Dear %s, your essay for the quiz "%s" has been graded. Your score is %d.', 'your-text-domain' ),
                    $user->display_name,
                    $quiz_title,
                    $score
                );

                // In a real-world scenario, you might use wp_mail() or a more sophisticated notification system.
                // For demonstration, we'll just log it.
                error_log( "Notification sent to user {$user_id} for quiz {$quiz_id}: {$subject}" );
            }
        }
    }

    // You could also:
    // - Update other post meta associated with the quiz or user.
    // - Trigger a custom report generation.
    // - Interact with third-party services.
}
add_action( 'learndash_essay_all_quiz_data_updated', 'my_learndash_essay_update_handler', 10, 4 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/classes/reports/group-essays.php:1367
src/classes/reports/group-essays.php:1742

);

					/** This filter is documented in includes/quiz/ld-quiz-essays.php */
					$updated_scoring_data = apply_filters( 'learndash_updated_essay_scoring', $updated_scoring_data );
					learndash_update_quiz_data( $quiz_id, $question_id, $updated_scoring_data, $essay_post );

					/** This action is documented in includes/quiz/ld-quiz-essays.php */
					do_action( 'learndash_essay_all_quiz_data_updated', $quiz_id, $question_id, $updated_scoring_data, $essay_post );
				}
			}
		}
	}

	/**
	 *


Scroll to Top