2024-08-09 12:04:48 +00:00
|
|
|
$(document).ready(function () {
|
2024-07-22 12:39:47 +00:00
|
|
|
var base_url = '<?php echo base_url(); ?>';
|
|
|
|
|
|
|
|
// Add section button functionality
|
2024-08-09 12:04:48 +00:00
|
|
|
$('#add-section-btn').on('click', function () {
|
2024-07-22 12:39:47 +00:00
|
|
|
var sectionHtml = `
|
|
|
|
<div class="form-section" data-type="">
|
|
|
|
<div class="header-row">
|
|
|
|
<textarea class="form-control untitled-question" placeholder="Untitled Question" rows="1"></textarea>
|
|
|
|
<select class="custom-select">
|
|
|
|
<option value="short-answer">Short Answer</option>
|
|
|
|
<option value="paragraph">Paragraph</option>
|
|
|
|
<option value="multiple-choice">Multiple Choice</option>
|
|
|
|
<option value="checkboxes">Checkboxes</option>
|
|
|
|
<option value="dropdown">Dropdown</option>
|
|
|
|
</select>
|
|
|
|
<label class="toggle-switch">
|
|
|
|
<input type="checkbox" class="required-toggle">
|
|
|
|
<span class="slider"></span>
|
|
|
|
</label>
|
|
|
|
<span class="delete-section-icon"><i class="fas fa-trash-alt"></i></span>
|
|
|
|
</div>
|
|
|
|
<div class="options-container"></div>
|
2024-08-09 12:04:48 +00:00
|
|
|
<button class="btn btn-secondary add-option-btn" style="display: none;">Add Option</button></div>
|
|
|
|
`; $('#form-container').append(sectionHtml);
|
2024-07-22 12:39:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Add option button functionality
|
2024-08-09 12:04:48 +00:00
|
|
|
$(document).on('click', '.add-option-btn', function () {
|
2024-07-22 12:39:47 +00:00
|
|
|
var optionHtml = `
|
|
|
|
<div class="option">
|
|
|
|
<input type="text" class="form-control option-label" placeholder="Option">
|
|
|
|
<span class="delete-option-icon">×</span>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
$(this).siblings('.options-container').append(optionHtml);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Delete option functionality
|
2024-08-09 12:04:48 +00:00
|
|
|
$(document).on('click', '.delete-option-icon', function () {
|
2024-07-22 12:39:47 +00:00
|
|
|
$(this).parent().remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Delete section functionality
|
2024-08-09 12:04:48 +00:00
|
|
|
$(document).on('click', '.delete-section-icon', function () {
|
2024-07-22 12:39:47 +00:00
|
|
|
$(this).closest('.form-section').remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Show/Hide "Add Option" button based on question type
|
2024-08-09 12:04:48 +00:00
|
|
|
$(document).on('change', '.custom-select', function () {
|
2024-07-22 12:39:47 +00:00
|
|
|
var type = $(this).val();
|
|
|
|
var $section = $(this).closest('.form-section');
|
|
|
|
if (type === 'multiple-choice' || type === 'checkboxes' || type === 'dropdown') {
|
|
|
|
$section.find('.add-option-btn').show();
|
|
|
|
} else {
|
|
|
|
$section.find('.add-option-btn').hide();
|
|
|
|
}
|
|
|
|
}).trigger('change'); // Trigger change to apply to existing sections
|
|
|
|
|
|
|
|
// Submit button functionality
|
2024-08-09 12:04:48 +00:00
|
|
|
$('#submit-btn').on('click', function () {
|
2024-07-22 12:39:47 +00:00
|
|
|
var formData = collectFormData();
|
2024-08-09 12:04:48 +00:00
|
|
|
formData['form_id'] = <? php echo $form['id']; ?>;
|
2024-07-22 12:39:47 +00:00
|
|
|
|
|
|
|
let validation = validateFormData(formData);
|
|
|
|
if (!validation.isValid) {
|
|
|
|
alert(validation.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: base_url + 'Form_controller/update_form',
|
|
|
|
type: 'POST',
|
|
|
|
data: { formData: formData },
|
|
|
|
dataType: 'JSON',
|
2024-08-09 12:04:48 +00:00
|
|
|
success: function (response) {
|
2024-07-22 12:39:47 +00:00
|
|
|
if (response.status === 'success') {
|
|
|
|
alert('Form updated successfully!');
|
|
|
|
window.location.href = base_url + 'Form_controller/index_forms_draft';
|
|
|
|
} else {
|
|
|
|
alert(response.message);
|
|
|
|
}
|
|
|
|
},
|
2024-08-09 12:04:48 +00:00
|
|
|
error: function (error) {
|
2024-07-22 12:39:47 +00:00
|
|
|
alert('Error updating form!');
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Collect form data function
|
|
|
|
function collectFormData() {
|
|
|
|
var formData = {
|
|
|
|
title: $('#form-title').val(),
|
|
|
|
description: $('#form-description').val(),
|
|
|
|
questions: []
|
|
|
|
};
|
|
|
|
|
2024-08-09 12:04:48 +00:00
|
|
|
$('.form-section').each(function () {
|
2024-07-22 12:39:47 +00:00
|
|
|
var questionData = {
|
|
|
|
id: $(this).data('index'),
|
|
|
|
text: $(this).find('.untitled-question').val(),
|
|
|
|
type: $(this).find('.custom-select').val(),
|
|
|
|
required: $(this).find('.required-toggle').is(':checked') ? 1 : 0, // Correctly capture the required value
|
|
|
|
options: []
|
|
|
|
};
|
|
|
|
|
2024-08-09 12:04:48 +00:00
|
|
|
$(this).find('.option-label').each(function () {
|
2024-07-22 12:39:47 +00:00
|
|
|
questionData.options.push($(this).val());
|
|
|
|
});
|
|
|
|
|
|
|
|
formData.questions.push(questionData);
|
|
|
|
});
|
|
|
|
|
|
|
|
return formData;
|
|
|
|
}
|
|
|
|
function validateFormData(formData) {
|
|
|
|
for (let question of formData.questions) {
|
|
|
|
if (!question.text.trim()) {
|
|
|
|
return { isValid: false, message: 'All questions must have text.' };
|
|
|
|
}
|
|
|
|
if ((question.type === 'multiple-choice' || question.type === 'checkboxes' || question.type === 'dropdown') && question.options.length === 0) {
|
|
|
|
return { isValid: false, message: 'All options-based questions must have at least one option.' };
|
|
|
|
}
|
|
|
|
for (let option of question.options) {
|
|
|
|
if (!option.trim()) {
|
|
|
|
return { isValid: false, message: 'All options must have text.' };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { isValid: true };
|
|
|
|
}
|
|
|
|
});
|