';
}).join('');
@@ -310,11 +319,15 @@
var answers = [];
currentContent.questions.forEach(function(q) {
- var selected = document.querySelector('input[name="lh-q-' + q.id + '"]:checked');
- answers.push({
- questionId: q.id,
- optionId: selected ? parseInt(selected.value) : null
- });
+ if (q.question_type === 'multi') {
+ var checkedEls = document.querySelectorAll('input[name="lh-qm-' + q.id + '"]:checked');
+ var optionIds = [];
+ checkedEls.forEach(function(el) { optionIds.push(parseInt(el.value)); });
+ answers.push({ questionId: q.id, optionIds: optionIds });
+ } else {
+ var selected = document.querySelector('input[name="lh-q-' + q.id + '"]:checked');
+ answers.push({ questionId: q.id, optionId: selected ? parseInt(selected.value) : null });
+ }
});
showLoading('Submitting quiz...');
@@ -373,12 +386,20 @@
data.results.forEach(function(r) {
var qDiv = document.querySelector('.lh-quiz-q[data-qid="' + r.questionId + '"]');
if (!qDiv) return;
+ var isMulti = r.questionType === 'multi';
qDiv.querySelectorAll('.lh-quiz-option').forEach(function(label) {
var input = label.querySelector('input');
if (!input) return;
var optId = parseInt(input.value);
- if (optId === r.correctOptionId) label.classList.add('lh-opt-correct');
- if (optId === r.selectedOptionId && !r.isCorrect) label.classList.add('lh-opt-wrong');
+ if (isMulti) {
+ var correctIds = r.correctOptionIds || [];
+ var selectedIds = r.selectedOptionIds || [];
+ if (correctIds.indexOf(optId) !== -1) label.classList.add('lh-opt-correct');
+ if (selectedIds.indexOf(optId) !== -1 && correctIds.indexOf(optId) === -1) label.classList.add('lh-opt-wrong');
+ } else {
+ if (optId === r.correctOptionId) label.classList.add('lh-opt-correct');
+ if (optId === r.selectedOptionId && !r.isCorrect) label.classList.add('lh-opt-wrong');
+ }
input.disabled = true;
});
});
@@ -560,30 +581,131 @@
renderCmsContentList(filtered);
}
- // Editor toolbar — insert HTML tags at cursor in textarea
- function insertTag(tag) {
- var ta = document.getElementById('lh-cms-edit-body');
- if (!ta) return;
- var start = ta.selectionStart, end = ta.selectionEnd;
- var sel = ta.value.substring(start, end);
- var insert = '';
- switch(tag) {
- case 'b': case 'i': case 'u': case 'code': case 'h2': case 'h3':
- insert = '<' + tag + '>' + (sel || 'text') + '' + tag + '>'; break;
- case 'a':
- var url = prompt('Enter URL:');
- if (!url) return;
- insert = '' + (sel || 'link text') + ''; break;
- case 'ul': insert = '
\n
' + (sel || 'item') + '
\n \n
'; break;
- case 'ol': insert = '\n
' + (sel || 'item') + '
\n \n'; break;
- case 'blockquote': insert = '
' + (sel || 'quote') + '
'; break;
- case 'table': insert = '
\n
Header 1
Header 2
\n
' + (sel || 'data') + '
\n
'; break;
- case 'hr': insert = ''; break;
- default: return;
+ // ── Quill body editor ─────────────────────────────────────
+ function initBodyEditor() {
+ var container = document.getElementById('lh-body-editor');
+ if (!container || _bodyQuill) return;
+ _bodyQuill = new Quill(container, {
+ theme: 'snow',
+ modules: {
+ toolbar: [
+ [{ header: [2, 3, false] }],
+ ['bold', 'italic', 'underline', 'strike'],
+ [{ list: 'ordered' }, { list: 'bullet' }, { indent: '-1' }, { indent: '+1' }],
+ ['blockquote', 'code-block'],
+ ['link'],
+ [{ color: [] }],
+ ['clean']
+ ]
+ }
+ });
+ }
+
+ // Mini Quill for question text or option text
+ function makeMinQuill(container, existingHtml, isOption) {
+ var toolbar = isOption
+ ? [['bold', 'italic'], ['link']]
+ : [['bold', 'italic'], [{ list: 'ordered' }, { list: 'bullet' }], ['link'], ['clean']];
+ var q = new Quill(container, {
+ theme: 'snow',
+ modules: { toolbar: toolbar }
+ });
+ if (existingHtml) q.clipboard.dangerouslyPasteHTML(existingHtml);
+ return q;
+ }
+
+ // Toggle rich text on a question text area or option text area
+ function toggleRichText(btn) {
+ var isActive = btn.classList.contains('active');
+ var target = btn.dataset.target; // 'question' or 'option'
+ var block = btn.closest(target === 'question' ? '.lh-question-block' : '.lh-option-row');
+ if (!block) return;
+
+ if (target === 'question') {
+ var ta = block.querySelector('.lh-q-text');
+ var wrap = block.querySelector('.lh-q-richtext-wrap');
+
+ if (!isActive) {
+ // Enable: hide textarea, show quill
+ var currentVal = ta ? ta.value : '';
+ btn.classList.add('active');
+ btn.textContent = 'Plain Text';
+ if (ta) ta.style.display = 'none';
+ if (!wrap) {
+ wrap = document.createElement('div');
+ wrap.className = 'lh-q-richtext-wrap';
+ ta.parentNode.insertBefore(wrap, ta.nextSibling);
+ }
+ wrap.style.display = '';
+ if (!block._questionQuill) {
+ block._questionQuill = makeMinQuill(wrap, currentVal, false);
+ } else {
+ wrap.style.display = '';
+ }
+ } else {
+ // Disable: get HTML from quill, restore textarea
+ btn.classList.remove('active');
+ btn.textContent = 'Rich Text';
+ if (block._questionQuill) {
+ var html = block._questionQuill.root.innerHTML;
+ if (html === '
') html = '';
+ if (ta) { ta.value = html; ta.style.display = ''; }
+ }
+ if (wrap) wrap.style.display = 'none';
+ }
+ } else {
+ // option
+ var row = block;
+ var optInput = row.querySelector('.lh-opt-text');
+ var optWrap = row.querySelector('.lh-opt-richtext-wrap');
+
+ if (!isActive) {
+ var curVal = optInput ? optInput.value : '';
+ btn.classList.add('active');
+ btn.textContent = 'Plain';
+ if (optInput) optInput.style.display = 'none';
+ if (!optWrap) {
+ optWrap = document.createElement('div');
+ optWrap.className = 'lh-opt-richtext-wrap';
+ optInput.parentNode.insertBefore(optWrap, optInput.nextSibling);
+ }
+ optWrap.style.display = '';
+ if (!row._optQuill) {
+ row._optQuill = makeMinQuill(optWrap, curVal, true);
+ }
+ } else {
+ btn.classList.remove('active');
+ btn.textContent = 'Rich';
+ if (row._optQuill) {
+ var optHtml = row._optQuill.root.innerHTML;
+ if (optHtml === '
') optHtml = '';
+ if (optInput) { optInput.value = optHtml; optInput.style.display = ''; }
+ }
+ if (optWrap) optWrap.style.display = 'none';
+ }
}
- ta.value = ta.value.substring(0, start) + insert + ta.value.substring(end);
- ta.focus();
- ta.selectionStart = ta.selectionEnd = start + insert.length;
+ }
+
+ // Read question text whether in rich text mode or plain
+ function getQText(block) {
+ if (block._questionQuill && block.querySelector('.lh-q-richtext-wrap') &&
+ block.querySelector('.lh-q-richtext-wrap').style.display !== 'none') {
+ var html = block._questionQuill.root.innerHTML;
+ return html === '
' ? '' : html;
+ }
+ var ta = block.querySelector('.lh-q-text');
+ return ta ? ta.value.trim() : '';
+ }
+
+ // Read option text whether in rich text mode or plain
+ function getOptText(row) {
+ if (row._optQuill && row.querySelector('.lh-opt-richtext-wrap') &&
+ row.querySelector('.lh-opt-richtext-wrap').style.display !== 'none') {
+ var html = row._optQuill.root.innerHTML;
+ return html === '
' ? '' : html;
+ }
+ var inp = row.querySelector('.lh-opt-text');
+ return inp ? inp.value.trim() : '';
}
function openEditor(contentId, contentType) {
@@ -597,12 +719,13 @@
document.getElementById('lh-cms-edit-id').value = '';
document.getElementById('lh-cms-edit-title').value = '';
document.getElementById('lh-cms-edit-subject').value = '';
- document.getElementById('lh-cms-edit-body').value = '';
document.getElementById('lh-cms-edit-category').value = '';
document.getElementById('lh-cms-edit-type').value = contentType || 'article';
document.getElementById('lh-cms-edit-published').value = 'false';
document.getElementById('lh-cms-questions').innerHTML = '';
document.getElementById('btn-lh-delete-content').classList.add('hidden');
+ initBodyEditor();
+ if (_bodyQuill) _bodyQuill.setContents([{ insert: '\n' }]);
// For quiz type, add a starter question
if (contentType === 'quiz' && !contentId) {
@@ -620,7 +743,7 @@
document.getElementById('lh-cms-edit-id').value = c.id;
document.getElementById('lh-cms-edit-title').value = c.title;
document.getElementById('lh-cms-edit-subject').value = c.subject || '';
- document.getElementById('lh-cms-edit-body').value = c.body || '';
+ if (_bodyQuill) _bodyQuill.clipboard.dangerouslyPasteHTML(c.body || '');
document.getElementById('lh-cms-edit-category').value = c.category_id || '';
document.getElementById('lh-cms-edit-type').value = c.content_type || 'article';
document.getElementById('lh-cms-edit-published').value = c.published ? 'true' : 'false';
@@ -648,7 +771,7 @@
var id = document.getElementById('lh-cms-edit-id').value;
var title = document.getElementById('lh-cms-edit-title').value.trim();
var subject = document.getElementById('lh-cms-edit-subject').value.trim();
- var body = document.getElementById('lh-cms-edit-body').value;
+ var body = _bodyQuill ? (_bodyQuill.root.innerHTML === '
' ? '' : _bodyQuill.root.innerHTML) : '';
var category_id = document.getElementById('lh-cms-edit-category').value || null;
var content_type = document.getElementById('lh-cms-edit-type').value;
var published = document.getElementById('lh-cms-edit-published').value === 'true';
@@ -661,7 +784,7 @@
var qBlocks = document.querySelectorAll('#lh-cms-questions .lh-question-block');
for (var qi = 0; qi < qBlocks.length; qi++) {
var qBlock = qBlocks[qi];
- var qText = qBlock.querySelector('.lh-q-text').value.trim();
+ var qText = getQText(qBlock);
if (!qText) continue;
var qType = qBlock.querySelector('.lh-q-type').value;
var qExpl = qBlock.querySelector('.lh-q-explanation').value.trim();
@@ -669,7 +792,7 @@
var options = [];
qBlock.querySelectorAll('.lh-option-row').forEach(function(row) {
- var optText = row.querySelector('.lh-opt-text').value.trim();
+ var optText = getOptText(row);
if (!optText) return;
options.push({
option_text: optText,
@@ -792,8 +915,10 @@
'Q' + qNum + '' +
'' +
+ '' +
'