let questions = null; // Auto-detect API base URL const getApiBase = () => { const protocol = window.location.protocol; const hostname = window.location.hostname; // If running on localhost, assume backend is on port 3001 if (hostname === 'localhost' || hostname === '127.0.0.1') { return `${protocol}//${hostname}:3001`; } // For production, assume backend is on same origin return "https://fizika-backend.schmelczer.dev" }; const API_BASE = getApiBase(); const loadQuestions = async ( isSearch, categories, sourceScheme, questionCount ) => { if (questions === null) { try { const response = await fetch(`${API_BASE}/api/fizika`); if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); } questions = await response.json(); console.log('Questions loaded from backend API'); } catch (error) { console.warn('Failed to load questions from API, falling back to local file:', error); try { const fallbackResponse = await fetch("fizika.json"); if (!fallbackResponse.ok) { throw new Error(`Local file not available: ${fallbackResponse.status}`); } questions = await fallbackResponse.json(); console.log('Questions loaded from local fallback file'); } catch (fallbackError) { console.error('Both API and local file failed:', fallbackError); throw new Error('Unable to load quiz data from either backend API or local file'); } } } let currentQuestions = questions.slice(); if (isSearch) { currentQuestions = currentQuestions.filter((q) => q.source.match(sourceScheme) ); } else { shuffleArray(currentQuestions); currentQuestions = currentQuestions.filter((q) => categories.includes(q.type) ); } resultHtml = ""; currentQuestions = currentQuestions.slice(0, questionCount); currentQuestions.forEach( ({ id, source, description, a, b, c, d, correct, image }, i) => { resultHtml += `

${i + 1}.

${source}

${description}
${image ? `
` : ""}



${d ? `
` : "" }
`; } ); resultHtml += currentQuestions.length === 0 ? '
Nem található a keresésnek megfelelő feladat!
' : ``; return resultHtml; }; function shuffleArray(array) { for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; } } // Initialize year dropdown with dynamic years from question data const initializeYearDropdown = async () => { try { // Load questions if not already loaded if (questions === null) { try { const response = await fetch(`${API_BASE}/api/fizika`); if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); } questions = await response.json(); console.log('Questions loaded for year dropdown initialization'); } catch (error) { console.warn('Failed to load questions from API, falling back to local file:', error); try { const fallbackResponse = await fetch("fizika.json"); if (!fallbackResponse.ok) { throw new Error(`Local file not available: ${fallbackResponse.status}`); } questions = await fallbackResponse.json(); console.log('Questions loaded from local fallback file for year dropdown'); } catch (fallbackError) { console.error('Both API and local file failed:', fallbackError); return; // Don't update dropdown if data unavailable } } } // Extract unique years from question sources const yearSet = new Set(); questions.forEach(q => { const yearMatch = q.source.match(/^(\d{4})\//); if (yearMatch) { yearSet.add(parseInt(yearMatch[1])); } }); // Convert to sorted array (newest first) const uniqueYears = Array.from(yearSet).sort((a, b) => b - a); // Get existing dropdown const yearDropdown = document.getElementById('evszam'); if (!yearDropdown) return; // Preserve the "Összes év" option and add dynamic years const allYearsOption = ''; const yearOptions = uniqueYears.map(year => `` ).join(''); yearDropdown.innerHTML = allYearsOption + yearOptions; console.log('Year dropdown initialized with years:', uniqueYears); } catch (error) { console.error('Failed to initialize year dropdown:', error); } }; // Initialize month dropdown dynamically based on selected year const initializeMonthDropdown = (selectedYear) => { if (!questions) return; const monthDropdown = document.getElementById('honap'); if (!monthDropdown) return; // Always include "Összes feladatsora" option let monthOptions = ''; if (selectedYear === 'all/') { // Show all possible month patterns const monthSet = new Set(); questions.forEach(q => { const sourceMatch = q.source.match(/^(\d{4})\/(.+?)\//); if (sourceMatch) { monthSet.add(sourceMatch[2]); } }); // Convert to sorted array and create options const uniqueMonths = Array.from(monthSet).sort(); uniqueMonths.forEach(month => { monthOptions += ``; }); } else { // Extract year from selected value (e.g., "2024/" -> "2024") const year = selectedYear.replace('/', ''); // Get unique months for this specific year const monthSet = new Set(); questions.forEach(q => { const sourceMatch = q.source.match(`^${year}\/(.+?)\/`); if (sourceMatch) { monthSet.add(sourceMatch[1]); } }); const uniqueMonths = Array.from(monthSet).sort(); // Special handling for known year patterns if (year === '2006') { // Preserve existing 2006 logic but add any new months found monthOptions += ''; monthOptions += ''; monthOptions += ''; // Add any dynamic months not covered by the standard ones uniqueMonths.forEach(month => { if (!['1', '2', '3'].includes(month)) { monthOptions += ``; } }); } else if (year === '2016') { // Preserve existing 2016 logic but add any new months found monthOptions += ''; monthOptions += ''; monthOptions += ''; monthOptions += ''; monthOptions += ''; // Add any dynamic months not covered uniqueMonths.forEach(month => { if (!['1', '2', 'm1', 'm2', 'm3'].includes(month)) { monthOptions += ``; } }); } else if (year === '2017') { // Preserve existing 2017 logic but add any new months found monthOptions += ''; monthOptions += ''; // Add any dynamic months uniqueMonths.forEach(month => { if (!['1', '2'].includes(month)) { monthOptions += ``; } }); } else { // For other years, use standard logic plus dynamic months const hasStandard1 = uniqueMonths.includes('1'); const hasStandard2 = uniqueMonths.includes('2'); if (hasStandard1) { monthOptions += ''; } if (hasStandard2) { monthOptions += ''; } // Add any non-standard months uniqueMonths.forEach(month => { if (!['1', '2'].includes(month)) { monthOptions += ``; } }); } } monthDropdown.innerHTML = monthOptions; console.log(`Month dropdown initialized for year: ${selectedYear}`); }; // Helper function to get readable month labels const getMonthLabel = (monthValue) => { // Handle known patterns const knownLabels = { '1': 'Május-Június', '2': 'Október-November', '3': 'Harmadik időszak', 'm1': '1. Mintafeladatsor', 'm2': '2. Mintafeladatsor', 'm3': '3. Mintafeladatsor' }; return knownLabels[monthValue] || monthValue; };