Updated assessment-builder

This commit is contained in:
Ruben Ramirez 2025-04-04 09:42:44 -05:00
parent 22161c876f
commit dc49593fac

View file

@ -12,7 +12,7 @@ jQuery(document).ready(function($) {
// --- State ---
let currentAssessmentQuestions = []; // Array to hold IDs of questions in the right pane
let currentAssessmentId = null; // Store the ID if editing an existing assessment (TODO: Load this if editing)
let currentAssessmentId = null; // Store the ID if editing an existing assessment
// --- Functions ---
@ -202,6 +202,63 @@ jQuery(document).ready(function($) {
}
/**
* Fetches data for an existing assessment and populates the builder.
* @param {number} assessmentId The ID of the assessment to load.
*/
function loadExistingAssessment(assessmentId) {
console.log('Loading assessment:', assessmentId);
// Show some loading indicator maybe?
$saveStatus.text('Loading assessment data...').removeClass('success error'); // TODO: Localize
$.post(quiztechBuilderData.ajax_url, {
action: 'quiztech_fetch_assessment_data',
// nonce: quiztechBuilderData.fetch_nonce, // TODO: Implement nonce verification in PHP
assessment_id: assessmentId
})
.done(function(response) {
if (response.success && response.data) {
currentAssessmentId = assessmentId; // Set the ID in state
$assessmentTitleInput.val(response.data.title || '');
// Clear placeholder and render questions
$currentList.empty();
if (response.data.questions && response.data.questions.length > 0) {
response.data.questions.forEach(function(q) {
const itemHtml = `
<div class="selected-question-item" data-question-id="${q.id}" data-cost="${q.cost}">
<span>
<strong>${q.title || 'Untitled'}</strong> (Type: ${q.type || 'N/A'}) [Cost: ${q.cost || 0}] <!-- TODO: Localize -->
</span>
<button class="remove-question-from-assessment" data-question-id="${q.id}">Remove</button> <!-- TODO: Localize -->
</div>
`;
$currentList.append(itemHtml);
});
} else {
// Add placeholder if no questions were returned
$currentList.html('<p>' + 'No questions added yet.' + '</p>'); // TODO: Localize
}
// Update state and UI based on loaded data
updateStateFromDOM();
updateTotalCost();
updateLibraryButtons(); // Disable 'Add' buttons for loaded questions
$saveStatus.text('Assessment loaded.').addClass('success'); // TODO: Localize
setTimeout(() => $saveStatus.empty().removeClass('success'), 3000); // Clear status after a delay
} else {
$saveStatus.text(response.data.message || 'Error loading assessment.').addClass('error'); // TODO: Localize
// Maybe redirect or show a more prominent error?
}
})
.fail(function() {
$saveStatus.text('Error loading assessment.').addClass('error'); // TODO: Localize
});
}
// --- Event Handlers ---
// Add question from library to current assessment (Handles manual click - less relevant with drag/drop but keep for now)
@ -302,6 +359,15 @@ jQuery(document).ready(function($) {
}
}).disableSelection(); // Prevent text selection while dragging
// --- Check for Edit Mode on Load ---
const urlParams = new URLSearchParams(window.location.search);
const editAssessmentId = urlParams.get('assessment_id');
if (editAssessmentId && !isNaN(parseInt(editAssessmentId))) {
loadExistingAssessment(parseInt(editAssessmentId));
}
// --- Initial Load ---
fetchLibraryQuestions(); // Load initial questions into the library (will also init draggable)
// renderCurrentAssessment(); // Don't render initially, sortable handles it