quiztech_theme/template-manage-assessments.php

92 lines
No EOL
3.9 KiB
PHP

<?php
/**
* Template Name: Manage Assessments
*
* This template is used for the page where Quiz Managers can manage Assessments.
*
* @package Quiztech
*/
// Ensure the user is logged in and has the appropriate capability.
// 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;
// Alternatively, display an error message:
// 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">
<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">
<?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(); ?> -->
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();