Included form validations to publish form
This commit is contained in:
parent
a259008a9a
commit
2ed6574915
|
@ -146,6 +146,35 @@ class Forms extends CI_Controller
|
|||
}
|
||||
|
||||
public function publish_form($form_id) {
|
||||
// Load form and questions data
|
||||
$form = $this->Form_model->get_form_by_id($form_id);
|
||||
$questions = $this->Form_model->get_questions_by_form_id($form_id);
|
||||
|
||||
// Validation checks
|
||||
if (empty($form->title)) {
|
||||
$this->session->set_flashdata('error', 'Form title cannot be empty.');
|
||||
redirect('forms/preview/' . $form_id);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($questions as $question) {
|
||||
if (empty($question->question_text)) {
|
||||
$this->session->set_flashdata('error', 'All questions must have text.');
|
||||
redirect('forms/preview/' . $form_id);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if question type is multiple-choice or checkbox
|
||||
if (in_array($question->question_type, ['multiple-choice', 'checkbox'])) {
|
||||
$options = $this->Form_model->get_options_by_question_id($question->question_id);
|
||||
if (empty($options)) {
|
||||
$this->session->set_flashdata('error', 'Questions of type multiple-choice or checkbox must have at least one option.');
|
||||
redirect('forms/preview/' . $form_id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a unique link
|
||||
$response_link = base_url("forms/respond/" . $form_id);
|
||||
|
||||
|
@ -159,6 +188,10 @@ class Forms extends CI_Controller
|
|||
redirect('forms/list_user_published_forms');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public function respond($form_id) {
|
||||
// Check if user is logged in
|
||||
if (!$this->session->userdata('user_id')) {
|
||||
|
|
|
@ -14,7 +14,11 @@
|
|||
<?php foreach ($forms as $form) : ?>
|
||||
<?php if ($form->is_published == 0) : ?>
|
||||
<tr>
|
||||
<td><a href="<?= base_url() ?>forms/view_form/<?= $form->form_id ?>"><?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?></a></td>
|
||||
<td>
|
||||
<a href="<?= base_url() ?>forms/preview/<?=$form->form_id?>">
|
||||
<?= htmlspecialchars($form->title ? $form->title : $form->form_id, ENT_QUOTES, 'UTF-8') ?>
|
||||
</a>
|
||||
</td>
|
||||
<td><?php echo date('Y-m-d H:i:s', strtotime($form->created_at)); ?></td>
|
||||
<td><a href="<?= base_url() ?>forms/delete/<?= $form->form_id ?>" onclick="return confirm('Are you sure you want to delete this form?');">Delete</a></td>
|
||||
</tr>
|
||||
|
|
|
@ -3,14 +3,20 @@
|
|||
<div class="section">
|
||||
<div class="form_container-response">
|
||||
<div class="form_container_top">
|
||||
<div class = "form_container_top_title"><?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?></div>
|
||||
<div class = "form_container_top_desc"><?= htmlspecialchars($form->description, ENT_QUOTES, 'UTF-8') ?></div>
|
||||
<div class="form_container_top_title"><?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?></div>
|
||||
<div class="form_container_top_desc"><?= htmlspecialchars($form->description, ENT_QUOTES, 'UTF-8') ?></div>
|
||||
</div>
|
||||
|
||||
<?php if ($this->session->flashdata('error')): ?>
|
||||
<div class="error-message" style="color: red;">
|
||||
<?= $this->session->flashdata('error') ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div id="questions-container">
|
||||
<?php if (!empty($questions)) : ?>
|
||||
<?php foreach ($questions as $index => $question) : ?>
|
||||
<div class="question-box" data-question-type="<?= htmlspecialchars($question->question_type, ENT_QUOTES, 'UTF-8') ?>" id="question-template" data-question_id="<?=htmlspecialchars($question->question_id, ENT_QUOTES, 'UTF-8')?>">
|
||||
<div class="question-box" data-question-type="<?= htmlspecialchars($question->question_type, ENT_QUOTES, 'UTF-8') ?>" id="question-template" data-question_id="<?= htmlspecialchars($question->question_id, ENT_QUOTES, 'UTF-8') ?>">
|
||||
<div class="question-box_header">
|
||||
<div class="response-questions" style="color:black;"><?= htmlspecialchars($question->question_text, ENT_QUOTES, 'UTF-8') ?></div>
|
||||
</div>
|
||||
|
@ -23,7 +29,7 @@
|
|||
<div id="options-container">
|
||||
<?php if (!empty($question->options)) : ?>
|
||||
<?php foreach ($question->options as $optionIndex => $option) : ?>
|
||||
<div class="question-box_option-block" id="option-template" data-option_id="<?=htmlspecialchars($option->option_id, ENT_QUOTES, 'UTF-8') ?>" >
|
||||
<div class="question-box_option-block" id="option-template" data-option_id="<?= htmlspecialchars($option->option_id, ENT_QUOTES, 'UTF-8') ?>">
|
||||
<?php if ($question->question_type == 'multiple-choice') : ?>
|
||||
<input type="radio" id="option-<?= $optionIndex ?>" name="question-<?= $index ?>">
|
||||
<label style="padding-top:12px;" for="option-<?= $optionIndex ?>"><?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?></label>
|
||||
|
@ -34,6 +40,8 @@
|
|||
</div>
|
||||
<br>
|
||||
<?php endforeach; ?>
|
||||
<?php else: ?>
|
||||
<p>No options found for this question.</p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
@ -44,7 +52,7 @@
|
|||
<p>No questions found for this form.</p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<a class = "publish-button"href="<?= base_url() ?>forms/publish_form/<?=$form->form_id?> ">Publish</a>
|
||||
<a class="publish-button" href="<?= base_url() ?>forms/publish_form/<?= $form->form_id ?>">Publish</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -295,6 +295,7 @@ tr:nth-child(even) {
|
|||
padding: 10px;
|
||||
box-shadow: rgba(0, 0, 0, 0.1) 0px 10px 15px -3px, rgba(0, 0, 0, 0.05) 0px 4px 6px -2px;
|
||||
border-radius: 5px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.submit-button{
|
||||
|
@ -316,6 +317,11 @@ tr:nth-child(even) {
|
|||
background-color: white;
|
||||
}
|
||||
|
||||
.question-box.active {
|
||||
border-left: 6px solid #1a73e8;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -16,6 +16,9 @@ $(document).ready(function() {
|
|||
|
||||
// Append the cloned question to the form container
|
||||
$('#question-template').parent().append(newQuestion);
|
||||
|
||||
// Scroll to the newly added question and set it as active
|
||||
setActiveQuestion(newQuestion);
|
||||
});
|
||||
|
||||
// Add new option to a question
|
||||
|
@ -93,6 +96,26 @@ $(document).ready(function() {
|
|||
questionBox.attr('data-question-type', selectedType);
|
||||
}).trigger('change');
|
||||
|
||||
// Function to set the active question and scroll the sidebar
|
||||
function setActiveQuestion(questionBox) {
|
||||
// Remove active class from all question boxes
|
||||
$('.question-box').removeClass('active');
|
||||
|
||||
// Add active class to the clicked question box
|
||||
questionBox.addClass('active');
|
||||
|
||||
// Scroll sidebar to the active question
|
||||
var offset = questionBox.offset().top - $('.sidebar').offset().top;
|
||||
$('.sidebar').animate({
|
||||
scrollTop: offset + $('.sidebar').scrollTop()
|
||||
}, 500);
|
||||
}
|
||||
|
||||
// Add click event listener to all question boxes to set active question
|
||||
$(document).on('click', '.question-box', function() {
|
||||
setActiveQuestion($(this));
|
||||
});
|
||||
|
||||
// Submit form
|
||||
$('#submit-form').click(function() {
|
||||
var formData = {
|
||||
|
@ -136,6 +159,9 @@ $(document).ready(function() {
|
|||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
$(document).ready(function() {
|
||||
$('#update-form').click(function() {
|
||||
|
@ -221,4 +247,4 @@ $(document).ready(function() {
|
|||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue