73 lines
4.4 KiB
XML
73 lines
4.4 KiB
XML
|
|
<?xml version="1.0" encoding="utf-8"?>
|
||
|
|
<odoo>
|
||
|
|
<template id="survey_answer_limit_assets" inherit_id="survey.survey_page">
|
||
|
|
<xpath expr="//head" position="inside">
|
||
|
|
<script type="text/javascript">
|
||
|
|
document.addEventListener('DOMContentLoaded', function() {
|
||
|
|
console.log('🔍 Survey validation script loaded');
|
||
|
|
|
||
|
|
// Find all multiple choice questions with max_answers
|
||
|
|
const questionContainers = document.querySelectorAll('.o_survey_question');
|
||
|
|
|
||
|
|
questionContainers.forEach(container => {
|
||
|
|
// Look for max_answers in data attribute or hidden field
|
||
|
|
const maxAnswersEl = container.querySelector('[data-max-answers]');
|
||
|
|
const maxAnswers = maxAnswersEl ? parseInt(maxAnswersEl.dataset.maxAnswers) : 0;
|
||
|
|
|
||
|
|
if (!maxAnswers) return;
|
||
|
|
|
||
|
|
console.log('✅ Found question with max_answers:', maxAnswers);
|
||
|
|
|
||
|
|
const checkboxes = container.querySelectorAll('input[type="checkbox"]');
|
||
|
|
const questionId = container.dataset.questionId || 'unknown';
|
||
|
|
|
||
|
|
// Add visual hint
|
||
|
|
const hint = document.createElement('small');
|
||
|
|
hint.className = 'text-muted d-block mt-2';
|
||
|
|
hint.textContent = `(សូមជ្រើសរើសត្រឹមតែ ${maxAnswers} ជម្រើសប៉ុណ្ណោះ)`;
|
||
|
|
const answersContainer = container.querySelector('.o_survey_question_answers');
|
||
|
|
if (answersContainer) {
|
||
|
|
answersContainer.appendChild(hint);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Checkbox change handler
|
||
|
|
checkboxes.forEach(checkbox => {
|
||
|
|
checkbox.addEventListener('change', function() {
|
||
|
|
const checked = container.querySelectorAll('input[type="checkbox"]:checked');
|
||
|
|
|
||
|
|
if (checked.length > maxAnswers) {
|
||
|
|
this.checked = false;
|
||
|
|
alert(`⚠️ ចំនួនជម្រើសលើសពីកំណត់!\nសូមជ្រើសរើសត្រឹមតែ ${maxAnswers} ជម្រើសប៉ុណ្ណោះ។`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Disable unchecked when limit reached
|
||
|
|
checkboxes.forEach(cb => {
|
||
|
|
if (!cb.checked) {
|
||
|
|
cb.disabled = (checked.length >= maxAnswers);
|
||
|
|
} else {
|
||
|
|
cb.disabled = false;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
// Submit/Next button validation
|
||
|
|
const submitBtn = container.closest('form').querySelector('button[type="submit"][value="next"], #next_page, .btn-primary');
|
||
|
|
if (submitBtn) {
|
||
|
|
submitBtn.addEventListener('click', function(e) {
|
||
|
|
const checked = container.querySelectorAll('input[type="checkbox"]:checked');
|
||
|
|
|
||
|
|
if (checked.length > maxAnswers) {
|
||
|
|
e.preventDefault();
|
||
|
|
e.stopPropagation();
|
||
|
|
alert(`❌ ការជ្រើសរើសរបស់អ្នកលើសពីកំណត់!\nសូមជ្រើសរើសត្រឹមតែ ${maxAnswers} ជម្រើសប៉ុណ្ណោះ。\n\nចំនួនដែលបានជ្រើសរើស: ${checked.length}`);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
</script>
|
||
|
|
</xpath>
|
||
|
|
</template>
|
||
|
|
</odoo>
|