feat: Implement basic Assessment and Question Management UI templates

This commit is contained in:
Ruben Ramirez 2025-04-03 22:26:54 -05:00
parent 2ea343b65f
commit 6942e7de77
2 changed files with 236 additions and 10 deletions

View file

@ -8,9 +8,10 @@
*/
// Ensure the user is logged in and has the appropriate capability.
// Using 'manage_options' as a placeholder capability for Quiz Managers.
// Replace with a more specific capability like 'manage_quiztech_assessments' if defined.
if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
// Capability required to manage assessments.
$required_capability = 'edit_assessments'; // Corresponds to 'assessment' 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;
@ -18,6 +19,10 @@ if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
// wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'quiztech' ), 403 );
}
// Get the URL for the Assessment Builder page
$builder_page = get_page_by_path( 'assessment-builder' ); // Assuming 'assessment-builder' is the slug
$builder_url = $builder_page ? get_permalink( $builder_page->ID ) : '';
get_header(); ?>
<div id="primary" class="content-area">
@ -29,8 +34,53 @@ get_header(); ?>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php esc_html_e( 'Placeholder for Assessment Management interface (list, add, edit - likely the Assessment Builder).', 'quiztech' ); ?></p>
<!-- Add Assessment listing table, 'Create New Assessment' button/link to builder here -->
<?php if ( $builder_url ) : ?>
<p><a href="<?php echo esc_url( $builder_url ); ?>" class="button"><?php esc_html_e( 'Create New Assessment', 'quiztech' ); ?></a></p>
<?php else : ?>
<p><?php esc_html_e( 'Assessment Builder page not found. Please ensure a page with the slug "assessment-builder" exists and uses the correct template.', 'quiztech' ); ?></p>
<?php endif; ?>
<h2><?php esc_html_e( 'Existing Assessments', 'quiztech' ); ?></h2>
<?php
$args = array(
'post_type' => 'assessment',
'post_status' => 'publish', // Or 'any' if you want drafts etc.
'posts_per_page' => -1, // Show all
'orderby' => 'title',
'order' => 'ASC',
);
$assessments_query = new WP_Query( $args );
?>
<?php if ( $assessments_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( 'Actions', 'quiztech' ); ?></th>
</tr>
</thead>
<tbody>
<?php while ( $assessments_query->have_posts() ) : $assessments_query->the_post(); ?>
<tr>
<td><?php the_title(); ?></td>
<td>
<?php if ( $builder_url ) : ?>
<a href="<?php echo esc_url( add_query_arg( 'assessment_id', get_the_ID(), $builder_url ) ); ?>"><?php esc_html_e( 'Edit', 'quiztech' ); ?></a>
<?php endif; ?>
<?php // Add delete link/action later if needed ?>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<?php wp_reset_postdata(); // Restore original Post Data ?>
<?php else : ?>
<p><?php esc_html_e( 'No assessments found.', 'quiztech' ); ?></p>
<?php endif; ?>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->

View file

@ -8,9 +8,10 @@
*/
// Ensure the user is logged in and has the appropriate capability.
// Using 'manage_options' as a placeholder capability for Quiz Managers.
// Replace with a more specific capability like 'manage_quiztech_questions' if defined.
if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
// 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;
@ -18,6 +19,14 @@ if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
// 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">
@ -29,8 +38,175 @@ get_header(); ?>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php esc_html_e( 'Placeholder for Question Management interface (list, add, edit).', 'quiztech' ); ?></p>
<!-- Add Question listing table, 'Add New Question' button/form here -->
<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(); ?> -->