From 2ed65749151503f5067b15396bbf5cebcbf2c389 Mon Sep 17 00:00:00 2001 From: jostheta Date: Fri, 19 Jul 2024 08:40:38 +0530 Subject: [PATCH] Included form validations to publish form --- application/controllers/Forms.php | 33 ++++++++++++++++++++++++++++ application/views/forms/mydrafts.php | 6 ++++- application/views/forms/preview.php | 24 +++++++++++++------- assets/css/style.css | 6 +++++ assets/js/script.js | 28 ++++++++++++++++++++++- 5 files changed, 87 insertions(+), 10 deletions(-) diff --git a/application/controllers/Forms.php b/application/controllers/Forms.php index 1010d12..1e7c8b9 100644 --- a/application/controllers/Forms.php +++ b/application/controllers/Forms.php @@ -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); @@ -158,6 +187,10 @@ class Forms extends CI_Controller // Redirect to the list_user_forms function redirect('forms/list_user_published_forms'); } + + + + public function respond($form_id) { // Check if user is logged in diff --git a/application/views/forms/mydrafts.php b/application/views/forms/mydrafts.php index e13ce47..941734a 100644 --- a/application/views/forms/mydrafts.php +++ b/application/views/forms/mydrafts.php @@ -14,7 +14,11 @@ is_published == 0) : ?> - title, ENT_QUOTES, 'UTF-8') ?> + + + title ? $form->title : $form->form_id, ENT_QUOTES, 'UTF-8') ?> + + created_at)); ?> Delete diff --git a/application/views/forms/preview.php b/application/views/forms/preview.php index 2242706..1b0afb9 100644 --- a/application/views/forms/preview.php +++ b/application/views/forms/preview.php @@ -2,28 +2,34 @@
-
-
title, ENT_QUOTES, 'UTF-8') ?>
-
description, ENT_QUOTES, 'UTF-8') ?>
-
+
+
title, ENT_QUOTES, 'UTF-8') ?>
+
description, ENT_QUOTES, 'UTF-8') ?>
+
+ + session->flashdata('error')): ?> +
+ session->flashdata('error') ?> +
+
$question) : ?> -
+
question_text, ENT_QUOTES, 'UTF-8') ?>

question_type == 'paragraph') : ?>
- +
options)) : ?> options as $optionIndex => $option) : ?> -
+
question_type == 'multiple-choice') : ?>   @@ -34,6 +40,8 @@

+ +

No options found for this question.

@@ -44,7 +52,7 @@

No questions found for this form.

- Publish + Publish
diff --git a/assets/css/style.css b/assets/css/style.css index 933d599..48958ee 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -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; +} + + diff --git a/assets/js/script.js b/assets/js/script.js index efab058..b795228 100644 --- a/assets/js/script.js +++ b/assets/js/script.js @@ -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() { }); -}); +