diff --git a/js/quiztech-assessment-builder.js b/js/quiztech-assessment-builder.js index b3c1061..9f1b31e 100644 --- a/js/quiztech-assessment-builder.js +++ b/js/quiztech-assessment-builder.js @@ -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 = ` +
' + 'No questions added yet.' + '
'); // 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