2024-07-11 13:04:55 +00:00
|
|
|
$(document).ready(function() {
|
|
|
|
console.log('jQuery is ready');
|
|
|
|
let questionCount = 0;
|
|
|
|
|
2024-07-24 04:59:15 +00:00
|
|
|
// Make the question container sortable
|
|
|
|
$('.form_container').sortable({
|
|
|
|
items: '.question-box:visible',
|
|
|
|
handle: '.question-box_header',
|
|
|
|
placeholder: 'sortable-placeholder',
|
|
|
|
update: function(event, ui) {
|
|
|
|
// Handle the order update here if needed
|
|
|
|
updateQuestionOrder();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-07-12 09:55:29 +00:00
|
|
|
// Add new question
|
2024-07-11 13:04:55 +00:00
|
|
|
$('#add-question').click(function() {
|
|
|
|
questionCount++;
|
2024-07-12 06:21:27 +00:00
|
|
|
|
2024-07-11 13:04:55 +00:00
|
|
|
// Clone the question template
|
|
|
|
var newQuestion = $('#question-template').clone();
|
|
|
|
newQuestion.removeAttr('id');
|
2024-07-12 09:55:29 +00:00
|
|
|
newQuestion.attr('data-question-type', 'multiple-choice'); // Set default question type
|
2024-07-24 07:35:03 +00:00
|
|
|
newQuestion.find('.question-box_header_question').attr('placeholder', 'Question ' );
|
2024-07-11 13:04:55 +00:00
|
|
|
newQuestion.find('.question-box_option-block_option-text').attr('placeholder', 'Option 1');
|
|
|
|
newQuestion.show(); // Ensure the cloned template is visible
|
|
|
|
|
|
|
|
// Append the cloned question to the form container
|
2024-07-12 06:21:27 +00:00
|
|
|
$('#question-template').parent().append(newQuestion);
|
2024-07-19 03:10:38 +00:00
|
|
|
|
|
|
|
// Scroll to the newly added question and set it as active
|
|
|
|
setActiveQuestion(newQuestion);
|
2024-07-24 07:35:03 +00:00
|
|
|
|
|
|
|
//update sidebar
|
|
|
|
updateSidebarMarginTop();
|
|
|
|
|
|
|
|
|
2024-07-11 13:04:55 +00:00
|
|
|
});
|
|
|
|
|
2024-07-12 09:55:29 +00:00
|
|
|
// Add new option to a question
|
2024-07-11 13:04:55 +00:00
|
|
|
$(document).on('click', '#add-option', function() {
|
|
|
|
const questionBox = $(this).closest('.question-box');
|
|
|
|
const currentQuestionType = questionBox.attr('data-question-type');
|
|
|
|
let optionCount = questionBox.find('.question-box_option-block').length + 1;
|
|
|
|
|
|
|
|
var newOption = $('#option-template').clone();
|
|
|
|
newOption.removeAttr('id');
|
2024-07-12 06:21:27 +00:00
|
|
|
newOption.find('input').val('');
|
|
|
|
newOption.find('input').attr('placeholder', 'Option ' + optionCount);
|
2024-07-11 13:04:55 +00:00
|
|
|
|
|
|
|
if (currentQuestionType === 'multiple-choice') {
|
|
|
|
newOption.find('img').attr('src', base_url+'assets/images/circle.png');
|
|
|
|
} else if (currentQuestionType === 'checkbox') {
|
|
|
|
newOption.find('img').attr('src', base_url+'assets/images/square.png');
|
|
|
|
}
|
2024-07-23 03:03:15 +00:00
|
|
|
else if (currentQuestionType === 'dropdown') {
|
|
|
|
newOption.find('img').attr('src', base_url+'assets/images/down-arrow.png');
|
|
|
|
}
|
2024-07-11 13:04:55 +00:00
|
|
|
|
|
|
|
if (optionCount > 1) {
|
|
|
|
newOption.append('<button class="question-box_option-block_option-close"><img src="'+base_url+'assets/images/close.png" alt="close option"></button>');
|
|
|
|
}
|
|
|
|
|
|
|
|
questionBox.find('#new-options').append(newOption).append('<br>');
|
|
|
|
});
|
|
|
|
|
2024-07-12 09:55:29 +00:00
|
|
|
// Remove an option from a question
|
2024-07-11 13:04:55 +00:00
|
|
|
$(document).on('click', '.question-box_option-block_option-close', function() {
|
2024-07-12 06:21:27 +00:00
|
|
|
$(this).closest('.question-box_option-block').next('br').remove();
|
|
|
|
$(this).closest('.question-box_option-block').remove();
|
2024-07-11 13:04:55 +00:00
|
|
|
});
|
|
|
|
|
2024-07-12 09:55:29 +00:00
|
|
|
// Delete a question
|
2024-07-11 13:04:55 +00:00
|
|
|
$(document).on('click', '.delete-question', function() {
|
2024-07-24 07:35:03 +00:00
|
|
|
questionCount = questionCount - 1;
|
|
|
|
var questionBox = $(this).closest('.question-box');
|
|
|
|
var prevQuestionBox = questionBox.prev('.question-box');
|
|
|
|
var nextQuestionBox = questionBox.next('.question-box');
|
|
|
|
|
|
|
|
questionBox.next('br').remove(); // Remove the adjacent <br> element
|
|
|
|
questionBox.remove(); // Remove the question box
|
|
|
|
|
|
|
|
if (nextQuestionBox.length) {
|
|
|
|
setActiveQuestion(nextQuestionBox);
|
|
|
|
} else if (prevQuestionBox.length) {
|
|
|
|
setActiveQuestion(prevQuestionBox);
|
|
|
|
} else {
|
|
|
|
// No questions left, align sidebar with form container top
|
|
|
|
setActiveQuestion($([])); // Pass an empty jQuery object
|
|
|
|
}
|
|
|
|
|
|
|
|
//update sidebar
|
|
|
|
updateSidebarMarginTop();
|
|
|
|
|
2024-07-11 13:04:55 +00:00
|
|
|
});
|
|
|
|
|
2024-07-12 09:55:29 +00:00
|
|
|
// Duplicate a question
|
2024-07-11 13:04:55 +00:00
|
|
|
$(document).on('click', '.duplicate-question', function() {
|
2024-07-12 06:21:27 +00:00
|
|
|
const originalQuestion = $(this).closest('.question-box');
|
2024-07-11 13:04:55 +00:00
|
|
|
const duplicateQuestion = originalQuestion.clone();
|
|
|
|
|
|
|
|
duplicateQuestion.removeAttr('id');
|
|
|
|
duplicateQuestion.show();
|
|
|
|
|
|
|
|
originalQuestion.after(duplicateQuestion).after('<br>');
|
|
|
|
});
|
|
|
|
|
2024-07-12 09:55:29 +00:00
|
|
|
// Handle question type change
|
2024-07-12 06:21:27 +00:00
|
|
|
$(document).on('change', '#question-type', function() {
|
2024-07-11 13:04:55 +00:00
|
|
|
const selectedType = $(this).val();
|
2024-07-12 06:21:27 +00:00
|
|
|
const questionBox = $(this).closest('.question-box');
|
|
|
|
const image = questionBox.find('#question-type-image');
|
|
|
|
const optionsContainer = questionBox.find('#options-container');
|
|
|
|
const shortAnswerContainer = questionBox.find('.question-box_short-answer');
|
2024-07-11 13:04:55 +00:00
|
|
|
|
|
|
|
if (selectedType === 'multiple-choice') {
|
|
|
|
image.attr('src', base_url+'assets/images/circle.png');
|
|
|
|
image.attr('alt', 'Circle for Multiple Choice');
|
|
|
|
optionsContainer.show();
|
|
|
|
shortAnswerContainer.hide();
|
|
|
|
} else if (selectedType === 'checkbox') {
|
|
|
|
image.attr('src', base_url+'assets/images/square.png');
|
|
|
|
image.attr('alt', 'Square for Checkbox');
|
|
|
|
optionsContainer.show();
|
|
|
|
shortAnswerContainer.hide();
|
2024-07-23 03:03:15 +00:00
|
|
|
}
|
|
|
|
else if (selectedType === 'dropdown') {
|
|
|
|
image.attr('src', base_url+'assets/images/down-arrow.png');
|
|
|
|
image.attr('alt', 'down arrow for dropdown');
|
|
|
|
optionsContainer.show();
|
|
|
|
shortAnswerContainer.hide();
|
|
|
|
}
|
|
|
|
else if (selectedType === 'paragraph') {
|
2024-07-11 13:04:55 +00:00
|
|
|
image.attr('src', '');
|
|
|
|
image.attr('alt', '');
|
|
|
|
optionsContainer.hide();
|
|
|
|
shortAnswerContainer.show();
|
|
|
|
}
|
|
|
|
|
2024-07-12 06:21:27 +00:00
|
|
|
questionBox.attr('data-question-type', selectedType);
|
|
|
|
}).trigger('change');
|
2024-07-11 13:04:55 +00:00
|
|
|
|
2024-07-19 03:10:38 +00:00
|
|
|
function setActiveQuestion(questionBox) {
|
|
|
|
// Remove active class from all question boxes
|
|
|
|
$('.question-box').removeClass('active');
|
2024-07-24 04:59:15 +00:00
|
|
|
|
2024-07-19 03:10:38 +00:00
|
|
|
// Add active class to the clicked question box
|
2024-07-24 07:35:03 +00:00
|
|
|
questionBox.addClass('active');
|
2024-07-19 03:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add click event listener to all question boxes to set active question
|
|
|
|
$(document).on('click', '.question-box', function() {
|
2024-07-24 07:35:03 +00:00
|
|
|
setActiveQuestion($(this));
|
|
|
|
//update sidebar
|
|
|
|
updateSidebarMarginTop();
|
2024-07-19 03:10:38 +00:00
|
|
|
});
|
|
|
|
|
2024-07-24 07:35:03 +00:00
|
|
|
//update Sidebar Margin
|
|
|
|
function updateSidebarMarginTop() {
|
|
|
|
var sidebar = $('.sidebar');
|
|
|
|
var questionCount = $('.question-box').length;
|
|
|
|
var referencePoint = $('.reference-point');
|
|
|
|
var activeQuestionBox = $('.question-box.active');
|
|
|
|
|
|
|
|
if (questionCount === 0) {
|
|
|
|
sidebar.css('margin-top', '0px');
|
|
|
|
} else if (questionCount === 1) {
|
|
|
|
sidebar.css('margin-top', '185px');
|
|
|
|
} else {
|
|
|
|
if (activeQuestionBox.length) {
|
|
|
|
// Calculate the offset of the active question from the reference point
|
|
|
|
var referenceOffsetTop = referencePoint.offset().top;
|
|
|
|
var activeBoxOffsetTop = activeQuestionBox.offset().top;
|
|
|
|
var offsetTop = activeBoxOffsetTop - referenceOffsetTop;
|
|
|
|
|
|
|
|
sidebar.css('margin-top', offsetTop + 'px');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-12 09:55:29 +00:00
|
|
|
// Submit form
|
2024-07-12 06:21:27 +00:00
|
|
|
$('#submit-form').click(function() {
|
|
|
|
var formData = {
|
|
|
|
title: $('#form-title').val(),
|
|
|
|
description: $('#form-desc').val(),
|
|
|
|
questions: []
|
|
|
|
};
|
2024-07-11 13:04:55 +00:00
|
|
|
|
|
|
|
$('.question-box:visible').each(function() {
|
2024-07-12 06:21:27 +00:00
|
|
|
var questionBox = $(this);
|
|
|
|
var questionData = {
|
|
|
|
question: questionBox.find('.question-box_header_question').val(),
|
2024-07-12 09:55:29 +00:00
|
|
|
type: questionBox.find('#question-type').val(),
|
2024-07-23 03:03:15 +00:00
|
|
|
required: questionBox.find('.required-checkbox').is(':checked') ? 1 : 0,
|
2024-07-12 06:21:27 +00:00
|
|
|
options: []
|
|
|
|
};
|
|
|
|
|
|
|
|
if (questionData.type !== 'paragraph') {
|
2024-07-12 09:55:29 +00:00
|
|
|
questionBox.find('.question-box_option-block').each(function() {
|
|
|
|
var optionText = $(this).find('.question-box_option-block_option-text').val();
|
2024-07-12 06:21:27 +00:00
|
|
|
if (optionText) {
|
|
|
|
questionData.options.push(optionText);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-07-11 13:04:55 +00:00
|
|
|
|
2024-07-12 06:21:27 +00:00
|
|
|
formData.questions.push(questionData);
|
|
|
|
});
|
2024-07-11 13:04:55 +00:00
|
|
|
|
2024-07-12 09:55:29 +00:00
|
|
|
console.log(formData);
|
|
|
|
|
2024-07-12 06:21:27 +00:00
|
|
|
$.ajax({
|
|
|
|
url: base_url+'forms/submit_form',
|
|
|
|
type: 'POST',
|
|
|
|
data: { formData: JSON.stringify(formData) },
|
|
|
|
success: function(response) {
|
|
|
|
console.log('Form data submitted successfully:', response);
|
2024-07-14 21:04:04 +00:00
|
|
|
window.location.href = base_url + 'my_drafts';
|
2024-07-12 06:21:27 +00:00
|
|
|
},
|
|
|
|
error: function(xhr, status, error) {
|
|
|
|
console.error('Error submitting form data:', error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2024-07-24 04:59:15 +00:00
|
|
|
|
|
|
|
// Function to update question order
|
|
|
|
function updateQuestionOrder() {
|
|
|
|
// Here you can handle the update event if needed, e.g., save the new order to the database
|
|
|
|
console.log('Question order updated');
|
|
|
|
}
|
2024-07-19 03:10:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2024-07-15 12:22:02 +00:00
|
|
|
|
|
|
|
$(document).ready(function() {
|
|
|
|
$('#update-form').click(function() {
|
|
|
|
var form_id = $(this).data('form_id');
|
|
|
|
var title = $('#form-title').val();
|
|
|
|
var description = $('#form-desc').val();
|
|
|
|
var questions = [];
|
|
|
|
|
|
|
|
$('.question-box:visible').each(function() {
|
|
|
|
var question_id = $(this).data('question_id');
|
|
|
|
var question_text = $(this).find('.question-box_header_question').val();
|
|
|
|
var question_type = $(this).find('#question-type').val();
|
|
|
|
var options = [];
|
|
|
|
|
|
|
|
$(this).find('.question-box_option-block').each(function() {
|
|
|
|
var option_id = $(this).data('option_id');
|
|
|
|
var option_text = $(this).find('input').val();
|
|
|
|
options.push({
|
|
|
|
option_id: option_id,
|
|
|
|
option_text: option_text
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
questions.push({
|
|
|
|
question_id: question_id,
|
|
|
|
question_text: question_text,
|
|
|
|
question_type: question_type,
|
|
|
|
options: options
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
var formData = {
|
|
|
|
form_id: form_id,
|
|
|
|
title: title,
|
|
|
|
description: description,
|
|
|
|
questions: JSON.stringify(questions)
|
|
|
|
};
|
|
|
|
|
|
|
|
console.log(formData);
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: base_url + 'forms/update_form',
|
|
|
|
type: 'POST',
|
|
|
|
data: formData,
|
|
|
|
dataType: 'json',
|
|
|
|
success: function(response) {
|
|
|
|
console.log('Form updated successfully:', response);
|
|
|
|
window.location.href = base_url + 'my_drafts';
|
|
|
|
// Handle success response
|
|
|
|
},
|
|
|
|
error: function(xhr, status, error) {
|
|
|
|
console.error('Error updating form:', error);
|
|
|
|
console.log(error);
|
|
|
|
// Handle error
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).ready(function() {
|
2024-07-23 03:03:15 +00:00
|
|
|
// Handle dropdowns with initial "Choose" option
|
|
|
|
$('select[data-initial-value="choose"]').on('change', function() {
|
|
|
|
var $this = $(this);
|
|
|
|
if ($this.val() === "") {
|
|
|
|
$this.addClass('default-value');
|
|
|
|
} else {
|
|
|
|
$this.removeClass('default-value');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).ready(function() {
|
|
|
|
$('#response-form').on('submit', function(e) {
|
|
|
|
e.preventDefault();
|
2024-07-23 05:01:02 +00:00
|
|
|
var form = $(this);
|
|
|
|
|
2024-07-23 03:03:15 +00:00
|
|
|
$.ajax({
|
2024-07-23 05:01:02 +00:00
|
|
|
url: form.attr('action'),
|
|
|
|
type: form.attr('method'),
|
|
|
|
data: form.serialize(),
|
2024-07-23 03:03:15 +00:00
|
|
|
dataType: 'json',
|
|
|
|
success: function(data) {
|
|
|
|
if (data.success) {
|
|
|
|
alert('Response submitted successfully!');
|
|
|
|
// Optionally, you can clear the form or redirect the user
|
|
|
|
window.location.href = base_url + 'my_forms';
|
2024-07-23 05:01:02 +00:00
|
|
|
} else if (data.errors) {
|
|
|
|
// Clear previous error messages
|
|
|
|
$('.error-message').remove();
|
|
|
|
|
|
|
|
// Display validation errors
|
|
|
|
$.each(data.errors, function(question_id, error_message) {
|
|
|
|
var questionBox = $('div[data-question-id="' + question_id + '"]');
|
|
|
|
questionBox.append('<div class="error-message" style="color:red;">' + error_message + '</div>');
|
|
|
|
});
|
2024-07-23 03:03:15 +00:00
|
|
|
} else {
|
|
|
|
alert('An error occurred. Please try again.');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function() {
|
2024-07-16 03:00:23 +00:00
|
|
|
alert('An error occurred. Please try again.');
|
|
|
|
}
|
2024-07-23 03:03:15 +00:00
|
|
|
});
|
2024-07-15 12:22:02 +00:00
|
|
|
});
|
|
|
|
});
|
2024-07-23 05:01:02 +00:00
|
|
|
|
2024-07-23 03:03:15 +00:00
|
|
|
});
|