quiztech_theme/template-manage-questions.php

218 lines
No EOL
12 KiB
PHP

<?php
/**
* Template Name: Manage Questions
*
* This template is used for the page where Quiz Managers can manage Questions in the library.
*
* @package Quiztech
*/
// Ensure the user is logged in and has the appropriate capability.
// Capability required to manage questions.
$required_capability = 'edit_questions'; // Corresponds to 'question' CPT capability_type
if ( ! is_user_logged_in() || ! current_user_can( $required_capability ) ) {
// Redirect to login page or show an error message.
wp_safe_redirect( wp_login_url( get_permalink() ) );
exit;
// Alternatively, display an error message:
// wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'quiztech' ), 403 );
}
// Define available question types (should ideally come from a central config or helper)
$question_types = array(
'multiple-choice' => __( 'Multiple Choice', 'quiztech' ),
'true-false' => __( 'True/False', 'quiztech' ),
'short-answer' => __( 'Short Answer', 'quiztech' ),
// Add other types as needed
);
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content">
<p><button id="quiztech-add-new-question-btn" class="button"><?php esc_html_e( 'Add New Question', 'quiztech' ); ?></button></p>
<div id="quiztech-add-edit-question-form" style="display: none; border: 1px solid #ccc; padding: 15px; margin-bottom: 20px;">
<h3><?php esc_html_e( 'Add/Edit Question', 'quiztech' ); ?></h3>
<form id="question-form">
<?php wp_nonce_field( 'quiztech_save_question_action', 'quiztech_question_nonce' ); ?>
<input type="hidden" id="quiztech-question-id" name="question_id" value="">
<p>
<label for="quiztech-question-title"><?php esc_html_e( 'Question Title (for reference):', 'quiztech' ); ?></label><br>
<input type="text" id="quiztech-question-title" name="question_title" class="widefat" required>
</p>
<p>
<label for="quiztech-question-content"><?php esc_html_e( 'Question Text:', 'quiztech' ); ?></label><br>
<textarea id="quiztech-question-content" name="question_content" class="widefat" rows="4" required></textarea>
</p>
<p>
<label for="quiztech-question-type"><?php esc_html_e( 'Question Type:', 'quiztech' ); ?></label><br>
<select id="quiztech-question-type" name="question_type" required>
<option value=""><?php esc_html_e( '-- Select Type --', 'quiztech' ); ?></option>
<?php foreach ( $question_types as $value => $label ) : ?>
<option value="<?php echo esc_attr( $value ); ?>"><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
</p>
<p id="quiztech-question-options-wrapper" style="display: none;">
<label for="quiztech-question-options"><?php esc_html_e( 'Options (one per line, mark correct with *):', 'quiztech' ); ?></label><br>
<textarea id="quiztech-question-options" name="question_options" class="widefat" rows="5"></textarea>
<small><?php esc_html_e('Example: Option A*'); ?></small>
</p>
<p>
<label for="quiztech-question-category"><?php esc_html_e( 'Category:', 'quiztech' ); ?></label><br>
<?php
wp_dropdown_categories( array(
'taxonomy' => 'quiztech_category',
'name' => 'question_category',
'id' => 'quiztech-question-category',
'show_option_none'=> __( '-- Select Category --', 'quiztech' ),
'hide_empty' => 0,
'hierarchical' => 1,
) );
?>
</p>
<p>
<button type="submit" class="button button-primary"><?php esc_html_e( 'Save Question', 'quiztech' ); ?></button>
<button type="button" id="quiztech-cancel-edit-question-btn" class="button" style="display: none;"><?php esc_html_e( 'Cancel', 'quiztech' ); ?></button>
</p>
</form>
</div>
<h2><?php esc_html_e( 'Question Library', 'quiztech' ); ?></h2>
<?php
$args = array(
'post_type' => 'question',
'post_status' => 'publish', // Or 'any'
'posts_per_page' => 20, // Add pagination later
'orderby' => 'title',
'order' => 'ASC',
);
$questions_query = new WP_Query( $args );
?>
<?php if ( $questions_query->have_posts() ) : ?>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th><?php esc_html_e( 'Title', 'quiztech' ); ?></th>
<th><?php esc_html_e( 'Type', 'quiztech' ); ?></th>
<th><?php esc_html_e( 'Category', 'quiztech' ); ?></th>
<th><?php esc_html_e( 'Actions', 'quiztech' ); ?></th>
</tr>
</thead>
<tbody>
<?php while ( $questions_query->have_posts() ) : $questions_query->the_post(); ?>
<?php
$question_id = get_the_ID();
$q_type_value = get_post_meta( $question_id, '_quiztech_question_type', true );
$q_type_label = isset( $question_types[ $q_type_value ] ) ? $question_types[ $q_type_value ] : esc_html( $q_type_value );
$terms = get_the_terms( $question_id, 'quiztech_category' );
$category_name = ! empty( $terms ) && ! is_wp_error( $terms ) ? esc_html( $terms[0]->name ) : __( 'N/A', 'quiztech' );
?>
<tr>
<td><?php the_title(); ?></td>
<td><?php echo esc_html( $q_type_label ); ?></td>
<td><?php echo esc_html( $category_name ); ?></td>
<td>
<button class="button quiztech-edit-question-btn" data-question-id="<?php echo esc_attr( $question_id ); ?>"><?php esc_html_e( 'Edit', 'quiztech' ); ?></button>
<button class="button quiztech-delete-question-btn" data-question-id="<?php echo esc_attr( $question_id ); ?>"><?php esc_html_e( 'Delete', 'quiztech' ); ?></button>
<?php // Add nonces and JS handlers later ?>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<?php wp_reset_postdata(); // Restore original Post Data ?>
<?php // Add pagination links here later ?>
<?php else : ?>
<p><?php esc_html_e( 'No questions found in the library.', 'quiztech' ); ?></p>
<?php endif; ?>
<script>
// Basic JS to toggle form visibility and handle options field display
jQuery(document).ready(function($) {
$('#quiztech-add-new-question-btn').on('click', function() {
$('#quiztech-add-edit-question-form').slideToggle();
$('#question-form')[0].reset(); // Clear form
$('#quiztech-question-id').val(''); // Clear hidden ID
$('#quiztech-cancel-edit-question-btn').hide();
$('#quiztech-add-edit-question-form h3').text('<?php echo esc_js( __( 'Add New Question', 'quiztech' ) ); ?>');
});
$('#quiztech-cancel-edit-question-btn').on('click', function() {
$('#quiztech-add-edit-question-form').slideUp();
$('#question-form')[0].reset();
$('#quiztech-question-id').val('');
$(this).hide();
});
$('#quiztech-question-type').on('change', function() {
const type = $(this).val();
if (type === 'multiple-choice' || type === 'true-false') {
$('#quiztech-question-options-wrapper').show();
} else {
$('#quiztech-question-options-wrapper').hide();
}
}).trigger('change'); // Trigger on load in case of editing
// Placeholder for Edit button click (to populate form - needs AJAX)
$('.quiztech-edit-question-btn').on('click', function() {
const questionId = $(this).data('question-id');
alert('Edit functionality for question ID ' + questionId + ' needs AJAX implementation.');
// TODO: AJAX call to get question data
// TODO: Populate form fields
// TODO: Show form, change title, show cancel button
// $('#quiztech-question-id').val(questionId);
// $('#quiztech-add-edit-question-form h3').text('<?php echo esc_js( __( 'Edit Question', 'quiztech' ) ); ?>');
// $('#quiztech-cancel-edit-question-btn').show();
// $('#quiztech-add-edit-question-form').slideDown();
});
// Placeholder for Delete button click (needs AJAX)
$('.quiztech-delete-question-btn').on('click', function() {
const questionId = $(this).data('question-id');
if (confirm('<?php echo esc_js( __( 'Are you sure you want to delete this question?', 'quiztech' ) ); ?>')) {
alert('Delete functionality for question ID ' + questionId + ' needs AJAX implementation.');
// TODO: AJAX call to delete question
// TODO: Remove row from table on success
}
});
// Placeholder for Form submission (needs AJAX)
$('#question-form').on('submit', function(e) {
e.preventDefault();
alert('Form submission needs AJAX implementation to save/update the question.');
// TODO: Gather form data
// TODO: AJAX call to save/update question
// TODO: Update table or reload on success
// TODO: Hide form
});
});
</script>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();