feat: Add Linked Assessment field to Manage Jobs UI

This commit is contained in:
Ruben Ramirez 2025-04-04 06:20:18 -05:00
parent 243c1481be
commit 509288f087
3 changed files with 81 additions and 22 deletions

View file

@ -10,8 +10,10 @@ function log_to_file($message, $data = false){
// Only proceed if a message is provided
if ($message) {
// Check if the custom log file constant is defined
if (defined('CUSTOM_DEBUG_LOG') && CUSTOM_DEBUG_LOG) {
$log_File = CUSTOM_DEBUG_LOG;
// Linter fix: Check only if defined, assume non-empty if defined.
if (defined('CUSTOM_DEBUG_LOG')) {
// Linter fix: Use constant() function to avoid undefined constant error
$log_File = constant('CUSTOM_DEBUG_LOG');
$date = new DateTime();
$date = $date->format("Y/m/d h:i:s");
@ -156,9 +158,10 @@ function quiztech_ajax_get_job() {
// 6. Send Response
wp_send_json_success( [
'id' => $post->ID,
'title' => $post->post_title,
'description' => $post->post_content,
'id' => $post->ID,
'title' => $post->post_title,
'description' => $post->post_content,
'assessment_id' => get_post_meta( $post->ID, '_quiztech_associated_assessment_id', true ), // Fetch the linked assessment ID
] );
}
add_action( 'wp_ajax_quiztech_get_job', 'quiztech_ajax_get_job' );
@ -226,24 +229,42 @@ function quiztech_ajax_save_job() {
// 6. Send Response
if ( is_wp_error( $result ) ) {
wp_send_json_error( [ 'message' => $result->get_error_message() ], 500 );
} elseif ( $result === 0 ) {
// wp_update_post returns 0 if nothing changed, which isn't an error here.
// Treat it as success but maybe with a different message? For now, same success message.
wp_send_json_success( [
'message' => esc_html__( 'Job details saved (no changes detected).', 'quiztech' ),
'job_id' => $job_id // Return the existing ID
] );
} elseif ( $result === 0 && $is_update ) { // Check if it was an update with no changes
// wp_update_post returns 0 if nothing changed.
// We still need to potentially update the meta field even if core fields didn't change.
$new_or_updated_post_id = $job_id;
} else {
// Success (Insert or Update)
$success_message = $is_update
? esc_html__( 'Job updated successfully.', 'quiztech' )
: esc_html__( 'Job created successfully.', 'quiztech' );
wp_send_json_success( [
'message' => $success_message,
'job_id' => $result // $result is the post ID on success
] );
// Success (Insert or Update with changes)
$new_or_updated_post_id = $result; // $result is the post ID on success
}
// --- Update Meta Fields ---
if ( $new_or_updated_post_id > 0 ) {
// Save Linked Assessment ID
if ( isset( $_POST['quiztech_associated_assessment_id'] ) ) {
$assessment_id = sanitize_text_field( wp_unslash( $_POST['quiztech_associated_assessment_id'] ) );
$assessment_id = '' === $assessment_id ? '' : absint( $assessment_id );
update_post_meta( $new_or_updated_post_id, '_quiztech_associated_assessment_id', $assessment_id );
} else {
// If not set in POST, maybe delete it? Or assume it wasn't part of the form?
// For now, let's assume if it's not sent, we don't change it.
// delete_post_meta($new_or_updated_post_id, '_quiztech_associated_assessment_id');
}
// Add other meta field updates here if needed
}
// --- End Meta Fields Update ---
// Send success response
$success_message = $is_update
? ( $result === 0 ? esc_html__( 'Job details saved (no changes detected).', 'quiztech' ) : esc_html__( 'Job updated successfully.', 'quiztech' ) )
: esc_html__( 'Job created successfully.', 'quiztech' );
wp_send_json_success( [
'message' => $success_message,
'job_id' => $new_or_updated_post_id
] );
}
add_action( 'wp_ajax_quiztech_save_job', 'quiztech_ajax_save_job' ); // Use new action hook

View file

@ -18,6 +18,7 @@ jQuery(document).ready(function($) {
function resetAndPrepareForm(mode = 'add') {
formElement[0].reset(); // Reset native form fields
jobIdField.val(''); // Clear hidden ID
formElement.find('#quiztech_associated_assessment_id').val(''); // Reset assessment dropdown
statusDiv.empty().removeClass('error success'); // Clear status messages
spinner.css('visibility', 'hidden');
@ -80,6 +81,8 @@ jQuery(document).ready(function($) {
jobIdField.val(data.id);
jobTitleField.val(data.title);
jobDescriptionField.val(data.description);
// Populate the linked assessment dropdown
formElement.find('#quiztech_associated_assessment_id').val(data.assessment_id || ''); // Set to empty string if null/undefined
formWrapper.slideDown(); // Show the form
// Scroll to form for better UX
@ -114,7 +117,8 @@ jQuery(document).ready(function($) {
nonce: quiztechThemeData.save_job_nonce, // Use new nonce
job_id: jobIdField.val(), // Include job_id (will be empty for new jobs)
job_title: jobTitleField.val(),
job_description: jobDescriptionField.val()
job_description: jobDescriptionField.val(),
quiztech_associated_assessment_id: formElement.find('#quiztech_associated_assessment_id').val() // Add assessment ID
};
$.post(quiztechThemeData.ajax_url, formData)

View file

@ -115,6 +115,40 @@ get_header(); ?>
<textarea id="job_description" name="job_description" rows="10" style="width: 100%;"></textarea>
</p>
<p>
<label for="quiztech_associated_assessment_id"><?php esc_html_e( 'Linked Assessment:', 'quiztech' ); ?></label><br />
<?php
// Query published assessments
$assessments_query = new WP_Query( [
'post_type' => 'assessment',
'posts_per_page' => -1,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
] );
echo '<select name="quiztech_associated_assessment_id" id="quiztech_associated_assessment_id" style="width: 100%;">';
echo '<option value="">' . esc_html__( '-- None --', 'quiztech' ) . '</option>';
if ( $assessments_query->have_posts() ) {
while ( $assessments_query->have_posts() ) {
$assessments_query->the_post();
$assessment_id = get_the_ID();
$assessment_title = get_the_title();
// Note: The 'selected' attribute will be handled by JavaScript when editing
echo '<option value="' . esc_attr( $assessment_id ) . '">' . esc_html( $assessment_title ) . '</option>';
}
wp_reset_postdata(); // Restore original post data
}
echo '</select>';
if ( ! $assessments_query->have_posts() ) {
echo '<br><small>' . esc_html__( 'No assessments found. Please create an assessment first.', 'quiztech' ) . '</small>';
}
?>
</p>
<p>
<input type="submit" name="submit_new_job" class="button button-primary" value="<?php esc_attr_e( 'Save Job', 'quiztech' ); ?>" />
<button type="button" class="button cancel-add-job"><?php esc_html_e( 'Cancel', 'quiztech' ); ?></button>