Online AI-Powered Teaching Assistant

Course Discussion Forum

with AI-Powered Teaching Assistant

${answer}

`; submitAiQuestionBtn.disabled = false; }, 1500); // Simulate API call delay }; askAiBtn.addEventListener('click', openAIModal); closeAiModalBtn.addEventListener('click', closeAIModal); aiModalOverlay.addEventListener('click', (e) => { if (e.target === aiModalOverlay) closeAIModal(); }); submitAiQuestionBtn.addEventListener('click', handleAIQuestion); aiQuestionInput.addEventListener('keyup', (e) => { if (e.key === 'Enter') handleAIQuestion(); }); // --- Core Forum Render Functions (largely unchanged) --- const renderAllDiscussions = () => { discussionThreads.sort((a, b) => b.pinned - a.pinned); mainContent.innerHTML = `
${discussionThreads.map(thread => `

${thread.title}

Posted by ${thread.author} • ${thread.timestamp} • ${thread.replies} replies

${thread.pinned ? `PINNED` : ''}
`).join('')}
`; }; const renderNewPostForm = () => { mainContent.innerHTML = `
`; }; const renderThreadView = (threadId) => { const thread = discussionThreads.find(t => t.id === threadId); if (!thread) return; mainContent.innerHTML = `
${isTeachingAssistant ? ` ` : ''}

${thread.title}

Posted by ${thread.author} on ${thread.timestamp}

${thread.content}

Replies (${thread.replies})

${thread.replyList.map(reply => `

${reply.content}

By ${reply.author} • ${reply.timestamp}

${isTeachingAssistant ? ` ` : (thread.bestAnswer === reply.id ? `BEST ANSWER` : '')}
`).join('')}

Add Your Reply

`; }; // --- Event Handlers & Tab Switching --- const switchToTab = (tabId) => { tabButtons.forEach(btn => btn.classList.toggle('active', btn.dataset.tab === tabId)); if (tabId === 'all-discussions') renderAllDiscussions(); else if (tabId === 'new-post') renderNewPostForm(); }; tabButtons.forEach(btn => btn.addEventListener('click', () => switchToTab(btn.dataset.tab))); mainContent.addEventListener('click', (e) => { const target = e.target; const threadCard = target.closest('[data-thread-id]'); if (threadCard && !target.closest('button')) { renderThreadView(parseInt(threadCard.dataset.threadId)); } else if (target.id === 'back-to-discussions') { switchToTab('all-discussions'); } else if (isTeachingAssistant && target.id === 'pin-thread-btn') { const thread = discussionThreads.find(t => t.id === parseInt(target.dataset.threadId)); thread.pinned = !thread.pinned; renderThreadView(thread.id); } else if (isTeachingAssistant && target.classList.contains('mark-best-btn')) { const thread = discussionThreads.find(t => t.id === parseInt(target.dataset.threadId)); const replyId = parseInt(target.dataset.replyId); thread.bestAnswer = thread.bestAnswer === replyId ? null : replyId; renderThreadView(thread.id); } else if (target.id === 'submit-post-btn') { const title = document.getElementById('post-title').value.trim(); const content = document.getElementById('post-content').value.trim(); if (title && content) { discussionThreads.unshift({ id: Date.now(), title, content, author: "Current User", timestamp: "Just now", replies: 0, replyList: [], pinned: false, bestAnswer: null }); switchToTab('all-discussions'); } else { alert("Please fill out all fields."); } } else if (target.id === 'submit-reply-btn') { const thread = discussionThreads.find(t => t.id === parseInt(target.dataset.threadId)); const replyContent = document.getElementById('reply-content').value.trim(); if (replyContent) { thread.replyList.push({ id: Date.now(), author: "Current User", timestamp: "Just now", content: replyContent }); thread.replies++; renderThreadView(thread.id); } } else if (target.id === 'download-thread-pdf') { generatePdf(parseInt(target.dataset.threadId)); } }); // --- PDF Generation --- const generatePdf = (threadId) => { const thread = discussionThreads.find(t => t.id === threadId); if (!thread) return; const buildPdfHtml = () => { const date = new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }); const repliesHtml = thread.replyList.map(reply => { const isBest = thread.bestAnswer === reply.id; return `
${isBest ? '

BEST ANSWER

' : ''}

${reply.content}

${reply.author}, ${reply.timestamp}

`; }).join(''); const aiSectionHtml = lastAIInteraction ? `

AI Assistant Interaction

Your Question: ${lastAIInteraction.question}

AI Answer: ${lastAIInteraction.answer}

` : ''; return `
${thread.pinned ? '

PINNED DISCUSSION

' : ''}

${thread.title}

Posted by ${thread.author} • Report generated on ${date}

Original Post

${thread.content}

Replies

${repliesHtml || '

No replies yet.

'}
${aiSectionHtml}
`; }; pdfRenderContainer.innerHTML = buildPdfHtml(); html2canvas(pdfRenderContainer, { scale: 2, useCORS: true }) .then(canvas => { const { jsPDF } = window.jspdf; const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: 'a4' }); const imgData = canvas.toDataURL('image/png'); const pdfWidth = pdf.internal.pageSize.getWidth(); const ratio = canvas.width / canvas.height; const pdfHeight = pdfWidth / ratio; pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight); pdf.save(`Discussion_${thread.title.replace(/\s+/g, '_')}.pdf`); }); }; // --- Initial Load --- switchToTab('all-discussions'); });
Scroll to Top