class CustomTestSolver extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
`;
// Auto-uppercase for test code
this.shadowRoot.getElementById('solve-code').addEventListener('input', (e) => {
e.target.value = e.target.value.toUpperCase();
});
// Submit test functionality
this.shadowRoot.getElementById('submit-test').addEventListener('click', async () => {
const code = this.shadowRoot.getElementById('solve-code').value.trim().toUpperCase();
let rawAns = this.shadowRoot.getElementById('solve-answers').value.trim().toLowerCase().replace(/\s/g, '');
if (!code || !rawAns) {
tg.showAlert("Kod va javoblarni kiriting!");
return;
}
// Show loading
const submitBtn = this.shadowRoot.getElementById('submit-test');
const originalText = submitBtn.textContent;
submitBtn.textContent = "TEKSHIRILMOQDA...";
submitBtn.disabled = true;
try {
const { data: test, error: testError } = await sb.from('tests')
.select('*')
.eq('test_code', code)
.single();
if (testError || !test) {
tg.showAlert("Bunday kodli test topilmadi!");
return;
}
// Validate answer format
if (test.test_type === 'simple' && !/^[a-dA-D]+$/.test(rawAns)) {
tg.showAlert("Javoblar faqat a,b,c,d harflaridan iborat bo'lishi kerak!");
return;
}
let correct = 0;
let total = 0;
if (test.test_type === 'simple') {
total = test.answer_keys.length;
for(let i=0; i📊 NATIJA: ${currentUser.full_name}\nTest: ${test.title}\nBall: ${correct}/${total}\nRasch: ${logit}`;
// Send to teacher
sendBotMsg(test.teacher_tg_id,
`🔔 YANGI TEST TOPSHIRILDI!\n\n` +
msg + `\n\nO'quvchi: @${user.username || 'foydalanuvchi'}`);
// Send to student
sendBotMsg(user.id.toString(),
`✅ SIZNING NATIJANGIZ!\n\n` +
msg + `\n\nDo'stlarga ulashish`);
// Save result to database
await sb.from('results').insert([{
student_tg_id: user.id.toString(),
teacher_tg_id: test.teacher_tg_id,
test_id: test.id,
score: correct,
total: total,
logit: logit,
answers: rawAns
}]);
} catch (err) {
tg.showAlert("Xatolik yuz berdi: " + err.message);
} finally {
submitBtn.textContent = originalText;
submitBtn.disabled = false;
}
});
}
}
customElements.define('custom-test-solver', CustomTestSolver);