Update meta field name for questions in Assessment Builder; changed default credit value
This commit is contained in:
parent
9eaedf9674
commit
55d975845e
1 changed files with 88 additions and 5 deletions
|
|
@ -22,6 +22,10 @@ class AssessmentBuilderAjaxHandler {
|
||||||
// AJAX action for logged-in users to save/update an assessment
|
// AJAX action for logged-in users to save/update an assessment
|
||||||
add_action( 'wp_ajax_quiztech_save_assessment', [ $this, 'save_assessment' ] );
|
add_action( 'wp_ajax_quiztech_save_assessment', [ $this, 'save_assessment' ] );
|
||||||
|
|
||||||
|
|
||||||
|
// AJAX action for logged-in users to fetch data for an existing assessment
|
||||||
|
add_action( 'wp_ajax_quiztech_fetch_assessment_data', [ $this, 'fetch_assessment_data' ] );
|
||||||
|
|
||||||
// TODO: Add AJAX action for creating a new question directly from the builder?
|
// TODO: Add AJAX action for creating a new question directly from the builder?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,7 +40,7 @@ class AssessmentBuilderAjaxHandler {
|
||||||
// check_ajax_referer( 'quiztech_assessment_builder_nonce', 'nonce' );
|
// check_ajax_referer( 'quiztech_assessment_builder_nonce', 'nonce' );
|
||||||
|
|
||||||
// 2. Check Capabilities
|
// 2. Check Capabilities
|
||||||
if ( ! current_user_can( 'manage_options' ) ) { // TODO: Use specific capability
|
if ( ! current_user_can( 'read' ) ) { // TODO: Use specific capability
|
||||||
wp_send_json_error( [ 'message' => esc_html__( 'Insufficient permissions.', 'quiztech' ) ], 403 );
|
wp_send_json_error( [ 'message' => esc_html__( 'Insufficient permissions.', 'quiztech' ) ], 403 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -65,8 +69,8 @@ class AssessmentBuilderAjaxHandler {
|
||||||
$questions[] = [
|
$questions[] = [
|
||||||
'id' => $question_id,
|
'id' => $question_id,
|
||||||
'title' => get_the_title(),
|
'title' => get_the_title(),
|
||||||
'type' => get_post_meta( $question_id, '_quiztech_question_type', true ) ?: 'N/A', // Placeholder
|
'type' => get_post_meta( $question_id, '_quiztech_question_type', true ) ?: 'N/A',
|
||||||
'cost' => get_post_meta( $question_id, '_quiztech_question_credits', true ) ?: 0, // Placeholder
|
'cost' => get_post_meta( $question_id, '_quiztech_credit_cost', true ) ?: 3, // Correct meta key
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
wp_reset_postdata();
|
wp_reset_postdata();
|
||||||
|
|
@ -138,7 +142,7 @@ class AssessmentBuilderAjaxHandler {
|
||||||
$saved_assessment_id = $result;
|
$saved_assessment_id = $result;
|
||||||
// Update the list of associated question IDs in post meta
|
// Update the list of associated question IDs in post meta
|
||||||
// Use a simple comma-separated list or serialized array. Serialized is more flexible.
|
// Use a simple comma-separated list or serialized array. Serialized is more flexible.
|
||||||
update_post_meta( $saved_assessment_id, '_quiztech_assessment_questions', $question_ids ); // Using array directly
|
update_post_meta( $saved_assessment_id, '_quiztech_linked_question_ids', $question_ids ); // Correct meta key
|
||||||
|
|
||||||
// TODO: Calculate and save total credit cost? Or calculate dynamically?
|
// TODO: Calculate and save total credit cost? Or calculate dynamically?
|
||||||
|
|
||||||
|
|
@ -148,4 +152,83 @@ class AssessmentBuilderAjaxHandler {
|
||||||
] );
|
] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AJAX handler to fetch data for an existing assessment.
|
||||||
|
*
|
||||||
|
* Expects assessment_id via $_POST.
|
||||||
|
* Returns JSON containing assessment title and question details or an error.
|
||||||
|
*/
|
||||||
|
public function fetch_assessment_data() {
|
||||||
|
// 1. Verify Nonce (TODO: Add nonce check)
|
||||||
|
// check_ajax_referer( 'quiztech_assessment_builder_nonce', 'nonce' );
|
||||||
|
|
||||||
|
// 2. Check Capabilities
|
||||||
|
if ( ! current_user_can( 'manage_options' ) ) { // TODO: Use specific capability
|
||||||
|
wp_send_json_error( [ 'message' => esc_html__( 'Insufficient permissions.', 'quiztech' ) ], 403 );
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Sanitize Input
|
||||||
|
$assessment_id = isset( $_POST['assessment_id'] ) ? absint( $_POST['assessment_id'] ) : 0;
|
||||||
|
|
||||||
|
if ( $assessment_id <= 0 ) {
|
||||||
|
wp_send_json_error( [ 'message' => esc_html__( 'Invalid Assessment ID.', 'quiztech' ) ], 400 );
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Get Assessment Post
|
||||||
|
$assessment_post = get_post( $assessment_id );
|
||||||
|
|
||||||
|
// 5. Validate Post
|
||||||
|
if ( ! $assessment_post || $assessment_post->post_type !== 'assessment' ) {
|
||||||
|
wp_send_json_error( [ 'message' => esc_html__( 'Assessment not found.', 'quiztech' ) ], 404 );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Optional: Check ownership or specific edit capability
|
||||||
|
// if ( $assessment_post->post_author != get_current_user_id() && ! current_user_can( 'edit_others_assessments' ) ) {
|
||||||
|
// wp_send_json_error( [ 'message' => esc_html__( 'Permission denied.', 'quiztech' ) ], 403 );
|
||||||
|
// }
|
||||||
|
|
||||||
|
// 6. Get Meta Data
|
||||||
|
$assessment_title = $assessment_post->post_title;
|
||||||
|
$linked_question_ids = get_post_meta( $assessment_id, '_quiztech_linked_question_ids', true );
|
||||||
|
$linked_question_ids = is_array( $linked_question_ids ) ? $linked_question_ids : [];
|
||||||
|
|
||||||
|
$questions_data = [];
|
||||||
|
if ( ! empty( $linked_question_ids ) ) {
|
||||||
|
// Ensure IDs are integers
|
||||||
|
$linked_question_ids = array_map( 'absint', $linked_question_ids );
|
||||||
|
|
||||||
|
// Query the details for these specific questions
|
||||||
|
$args = [
|
||||||
|
'post_type' => 'question',
|
||||||
|
'post_status' => 'publish',
|
||||||
|
'post__in' => $linked_question_ids,
|
||||||
|
'orderby' => 'post__in', // Maintain the order from the meta field
|
||||||
|
'posts_per_page' => -1, // Get all linked questions
|
||||||
|
];
|
||||||
|
$query = new \WP_Query( $args );
|
||||||
|
|
||||||
|
if ( $query->have_posts() ) {
|
||||||
|
while ( $query->have_posts() ) {
|
||||||
|
$query->the_post();
|
||||||
|
$question_id = get_the_ID();
|
||||||
|
$questions_data[] = [
|
||||||
|
'id' => $question_id,
|
||||||
|
'title' => get_the_title(),
|
||||||
|
'type' => get_post_meta( $question_id, '_quiztech_question_type', true ) ?: 'N/A',
|
||||||
|
'cost' => get_post_meta( $question_id, '_quiztech_credit_cost', true ) ?: 0,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
wp_reset_postdata();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7. Send Response
|
||||||
|
wp_send_json_success( [
|
||||||
|
'title' => $assessment_title,
|
||||||
|
'questions' => $questions_data,
|
||||||
|
] );
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Reference in a new issue