Included form validations to publish form

This commit is contained in:
jostheta 2024-07-19 08:40:38 +05:30
parent a259008a9a
commit 2ed6574915
5 changed files with 87 additions and 10 deletions

View File

@ -146,6 +146,35 @@ class Forms extends CI_Controller
} }
public function publish_form($form_id) { 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 // Generate a unique link
$response_link = base_url("forms/respond/" . $form_id); $response_link = base_url("forms/respond/" . $form_id);
@ -159,6 +188,10 @@ class Forms extends CI_Controller
redirect('forms/list_user_published_forms'); redirect('forms/list_user_published_forms');
} }
public function respond($form_id) { public function respond($form_id) {
// Check if user is logged in // Check if user is logged in
if (!$this->session->userdata('user_id')) { if (!$this->session->userdata('user_id')) {

View File

@ -14,7 +14,11 @@
<?php foreach ($forms as $form) : ?> <?php foreach ($forms as $form) : ?>
<?php if ($form->is_published == 0) : ?> <?php if ($form->is_published == 0) : ?>
<tr> <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><?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> <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> </tr>

View File

@ -2,28 +2,34 @@
<br> <br>
<div class="section"> <div class="section">
<div class="form_container-response"> <div class="form_container-response">
<div class="form_container_top"> <div class="form_container_top">
<div class = "form_container_top_title"><?= htmlspecialchars($form->title, 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 class="form_container_top_desc"><?= htmlspecialchars($form->description, ENT_QUOTES, 'UTF-8') ?></div>
</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"> <div id="questions-container">
<?php if (!empty($questions)) : ?> <?php if (!empty($questions)) : ?>
<?php foreach ($questions as $index => $question) : ?> <?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="question-box_header">
<div class="response-questions" style="color:black;"><?= htmlspecialchars($question->question_text, ENT_QUOTES, 'UTF-8') ?></div> <div class="response-questions" style="color:black;"><?= htmlspecialchars($question->question_text, ENT_QUOTES, 'UTF-8') ?></div>
</div> </div>
<br> <br>
<?php if ($question->question_type == 'paragraph') : ?> <?php if ($question->question_type == 'paragraph') : ?>
<div class="question-box_short-answer"> <div class="question-box_short-answer">
<textarea class="response-text-area" placeholder="Your Answer"></textarea> <textarea class="response-text-area" placeholder="Your Answer"></textarea>
</div> </div>
<?php else : ?> <?php else : ?>
<div id="options-container"> <div id="options-container">
<?php if (!empty($question->options)) : ?> <?php if (!empty($question->options)) : ?>
<?php foreach ($question->options as $optionIndex => $option) : ?> <?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') : ?> <?php if ($question->question_type == 'multiple-choice') : ?>
&nbsp;<input type="radio" id="option-<?= $optionIndex ?>" name="question-<?= $index ?>"> &nbsp;<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> <label style="padding-top:12px;" for="option-<?= $optionIndex ?>"><?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?></label>
@ -34,6 +40,8 @@
</div> </div>
<br> <br>
<?php endforeach; ?> <?php endforeach; ?>
<?php else: ?>
<p>No options found for this question.</p>
<?php endif; ?> <?php endif; ?>
</div> </div>
<?php endif; ?> <?php endif; ?>
@ -44,7 +52,7 @@
<p>No questions found for this form.</p> <p>No questions found for this form.</p>
<?php endif; ?> <?php endif; ?>
</div> </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> </div>
</div> </div>

View File

@ -295,6 +295,7 @@ tr:nth-child(even) {
padding: 10px; padding: 10px;
box-shadow: rgba(0, 0, 0, 0.1) 0px 10px 15px -3px, rgba(0, 0, 0, 0.05) 0px 4px 6px -2px; 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; border-radius: 5px;
overflow-y: auto;
} }
.submit-button{ .submit-button{
@ -316,6 +317,11 @@ tr:nth-child(even) {
background-color: white; background-color: white;
} }
.question-box.active {
border-left: 6px solid #1a73e8;
}

View File

@ -16,6 +16,9 @@ $(document).ready(function() {
// Append the cloned question to the form container // Append the cloned question to the form container
$('#question-template').parent().append(newQuestion); $('#question-template').parent().append(newQuestion);
// Scroll to the newly added question and set it as active
setActiveQuestion(newQuestion);
}); });
// Add new option to a question // Add new option to a question
@ -93,6 +96,26 @@ $(document).ready(function() {
questionBox.attr('data-question-type', selectedType); questionBox.attr('data-question-type', selectedType);
}).trigger('change'); }).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
$('#submit-form').click(function() { $('#submit-form').click(function() {
var formData = { var formData = {
@ -136,6 +159,9 @@ $(document).ready(function() {
} }
}); });
}); });
});
$(document).ready(function() { $(document).ready(function() {
$('#update-form').click(function() { $('#update-form').click(function() {
@ -221,4 +247,4 @@ $(document).ready(function() {
}); });
});