From b32e2c821e26c2847b2c827d35855782e224f1f4 Mon Sep 17 00:00:00 2001 From: jostheta Date: Tue, 23 Jul 2024 08:33:15 +0530 Subject: [PATCH] added required and dropdown --- application/config/database.php | 4 +- application/controllers/Forms.php | 34 ++++++--- application/models/Form_model.php | 6 +- application/views/forms/create.php | 2 + application/views/forms/preview.php | 67 +++++++++++------ application/views/forms/respond_form.php | 90 ++++++++++++++++------- application/views/forms/view_form.php | 6 +- assets/css/style.css | 32 ++++++++ assets/images/down-arrow.png | Bin 0 -> 289 bytes assets/js/edit.js | 14 +++- assets/js/script.js | 66 +++++++++++------ 11 files changed, 235 insertions(+), 86 deletions(-) create mode 100644 assets/images/down-arrow.png diff --git a/application/config/database.php b/application/config/database.php index e8ab51e..de9475a 100644 --- a/application/config/database.php +++ b/application/config/database.php @@ -76,8 +76,8 @@ $query_builder = TRUE; $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', - 'username' => 'jostheta', - 'password' => 'Pa$$w0rd', + 'username' => 'root', + 'password' => '', 'database' => 'gforms', 'dbdriver' => 'mysqli', 'dbprefix' => '', diff --git a/application/controllers/Forms.php b/application/controllers/Forms.php index a4f4037..f6580e2 100644 --- a/application/controllers/Forms.php +++ b/application/controllers/Forms.php @@ -220,20 +220,36 @@ class Forms extends CI_Controller public function submit_response() { $this->load->model('Form_model'); - + $form_id = $this->input->post('form_id'); $responses = $this->input->post('responses'); - - if ($this->Form_model->save_responses($form_id, $responses)) { - $this->output - ->set_content_type('application/json') - ->set_output(json_encode(['success' => true])); + $questions = $this->Form_model->get_questions_by_form_id($form_id); // Assuming you have a method to get questions by form_id + + $errors = []; + + foreach ($questions as $question) { + if ($question->is_required && empty($responses[$question->question_id])) { + $errors[$question->question_id] = 'This is a required question'; + } + } + + if (!empty($errors)) { + $this->session->set_flashdata('errors', $errors); + $this->session->set_flashdata('responses', $responses); // Persisting responses + redirect('forms/respond_form/' . $form_id); // Redirect back to the form } else { - $this->output - ->set_content_type('application/json') - ->set_output(json_encode(['success' => false])); + if ($this->Form_model->save_responses($form_id, $responses)) { + $this->output + ->set_content_type('application/json') + ->set_output(json_encode(['success' => true])); + } else { + $this->output + ->set_content_type('application/json') + ->set_output(json_encode(['success' => false])); + } } } + // List all forms of the current logged-in user public function list_user_forms() { diff --git a/application/models/Form_model.php b/application/models/Form_model.php index f6d0dc3..9a32dcd 100644 --- a/application/models/Form_model.php +++ b/application/models/Form_model.php @@ -21,7 +21,8 @@ class Form_model extends CI_Model { $this->db->insert('questions', [ 'form_id' => $formId, 'question_text' => $question['question'], - 'question_type' => $question['type'] + 'question_type' => $question['type'], + 'is_required' => $question['required'] ]); $questionId = $this->db->insert_id(); @@ -239,7 +240,8 @@ class Form_model extends CI_Model { $this->db->insert('questions', [ 'form_id' => $form_id, 'question_text' => $question['question_text'], - 'question_type' => $question['question_type'] + 'question_type' => $question['question_type'], + 'is_required' => $question['required'] ]); $question_id = $this->db->insert_id(); diff --git a/application/views/forms/create.php b/application/views/forms/create.php index 15cafd4..a413d76 100644 --- a/application/views/forms/create.php +++ b/application/views/forms/create.php @@ -20,6 +20,7 @@ + @@ -57,6 +58,7 @@
diff --git a/application/views/forms/preview.php b/application/views/forms/preview.php index 1b0afb9..05b8895 100644 --- a/application/views/forms/preview.php +++ b/application/views/forms/preview.php @@ -4,8 +4,9 @@
title, ENT_QUOTES, 'UTF-8') ?>
-
description, ENT_QUOTES, 'UTF-8') ?>
-
+
description, ENT_QUOTES, 'UTF-8') ?> +
+
session->flashdata('error')): ?>
@@ -14,45 +15,65 @@
- - $question) : ?> -
+ + $question): ?> +
-
question_text, ENT_QUOTES, 'UTF-8') ?>
+
+ question_text, ENT_QUOTES, 'UTF-8') ?>
+ is_required ? '*' : '' ?>

- question_type == 'paragraph') : ?> + question_type == 'paragraph'): ?>
- +
- options)) : ?> - options as $optionIndex => $option) : ?> -
- question_type == 'multiple-choice') : ?> -   - - question_type == 'checkbox') : ?> -   - - -
-
- + options)): ?> + question_type == 'dropdown'): ?> + + + options as $optionIndex => $option): ?> +
+ question_type == 'multiple-choice'): ?> +   + + question_type == 'checkbox'): ?> +   + + +
+
+ +

No options found for this question.

+

- +

No questions found for this form.

Publish
- + \ No newline at end of file diff --git a/application/views/forms/respond_form.php b/application/views/forms/respond_form.php index e00e752..fb828f7 100644 --- a/application/views/forms/respond_form.php +++ b/application/views/forms/respond_form.php @@ -3,50 +3,90 @@
-
title, ENT_QUOTES, 'UTF-8') ?>
-
description, ENT_QUOTES, 'UTF-8') ?>
+
title, ENT_QUOTES, 'UTF-8') ?>
+
description, ENT_QUOTES, 'UTF-8') ?> +
- - $question) : ?> -
+ + session->flashdata('errors'); + $responses = $this->session->flashdata('responses'); + ?> + $question): ?> +
-
question_text, ENT_QUOTES, 'UTF-8') ?>
+
+ question_text, ENT_QUOTES, 'UTF-8') ?> + is_required ? '*' : '' ?> +
+ question_id])): ?> +
+ question_id], ENT_QUOTES, 'UTF-8') ?> +
+

- question_type == 'paragraph') : ?> + question_type == 'paragraph'): ?>
- + +
- +
- options)) : ?> - options as $optionIndex => $option) : ?> -
- question_type == 'multiple-choice') : ?> -   - - question_type == 'checkbox') : ?> -   - - -
-
- + options)): ?> + question_type == 'dropdown'): ?> + + + options as $optionIndex => $option): ?> +
+ question_type == 'multiple-choice'): ?> +  question_id]) && $responses[$question->question_id] == $option->option_text ? 'checked' : '' ?>> + + question_type == 'checkbox'): ?> +  question_id]) && in_array($option->option_text, $responses[$question->question_id]) ? 'checked' : '' ?>> + + +
+
+ +

- +

No questions found for this form.

- +
-
+ \ No newline at end of file diff --git a/application/views/forms/view_form.php b/application/views/forms/view_form.php index e40f879..26d2d1d 100644 --- a/application/views/forms/view_form.php +++ b/application/views/forms/view_form.php @@ -18,6 +18,7 @@ @@ -36,7 +37,7 @@ options)) : ?> options as $optionIndex => $option) : ?>
- option <?= $question->question_type ?> + option <?= $question->question_type ?> 0) : ?> @@ -53,6 +54,9 @@

diff --git a/assets/css/style.css b/assets/css/style.css index c4ebe5f..2592467 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -336,7 +336,39 @@ tr:nth-child(even) { } +.checkbox-inline input[type="checkbox"] { + position: relative; + appearance: none; + width: 40px; + height: 20px; + background: #ccc; + outline: none; + cursor: pointer; + border-radius: 20px; + transition: background 0.3s; + margin-left:15px; +} +.checkbox-inline input[type="checkbox"]:checked { + background: #2196F3; +} + +.checkbox-inline input[type="checkbox"]::before { + content: ''; + position: absolute; + width: 16px; + height: 16px; + border-radius: 50%; + top: 2px; + left: 2px; + background: #fff; + transition: transform 0.3s; +} + +.checkbox-inline input[type="checkbox"]:checked::before { + transform: translateX(20px); + +} diff --git a/assets/images/down-arrow.png b/assets/images/down-arrow.png new file mode 100644 index 0000000000000000000000000000000000000000..88e0ed9a6d59ac3a14ffeb832fbcfc5d2cd99c47 GIT binary patch literal 289 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`EX7WqAsj$Z!;#Vf2?- zS`cPD?!gbF86-S}K_KrbYA%Uq&rRwSad2=Ee+3Gn~*mfQ^ebRnCf# 1 && newOption.find('.question-box_option-block_option-close').length === 0) { @@ -106,7 +110,14 @@ $(document).ready(function() { images.attr('alt', 'Square for Checkbox'); optionsContainer.show(); shortAnswerContainer.hide(); - } else if (selectedType === 'paragraph') { + } + else if (selectedType === 'dropdown') { + images.attr('src', base_url + 'assets/images/down-arrow.png'); + images.attr('alt', 'down-arrow for dropdown'); + optionsContainer.show(); + shortAnswerContainer.hide(); + } + else if (selectedType === 'paragraph') { images.attr('src', ''); images.attr('alt', ''); optionsContainer.hide(); @@ -129,6 +140,7 @@ $(document).ready(function() { var questionData = { question_text: questionBox.find('.question-box_header_question').val(), question_type: questionBox.find('.question-box_header_question-type_select').val(), + required: questionBox.find('.required-checkbox').is(':checked') ? 1 : 0, options: [] }; diff --git a/assets/js/script.js b/assets/js/script.js index 2717629..1610b35 100644 --- a/assets/js/script.js +++ b/assets/js/script.js @@ -37,6 +37,9 @@ $(document).ready(function() { } else if (currentQuestionType === 'checkbox') { newOption.find('img').attr('src', base_url+'assets/images/square.png'); } + else if (currentQuestionType === 'dropdown') { + newOption.find('img').attr('src', base_url+'assets/images/down-arrow.png'); + } if (optionCount > 1) { newOption.append(''); @@ -86,7 +89,14 @@ $(document).ready(function() { image.attr('alt', 'Square for Checkbox'); optionsContainer.show(); shortAnswerContainer.hide(); - } else if (selectedType === 'paragraph') { + } + 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') { image.attr('src', ''); image.attr('alt', ''); optionsContainer.hide(); @@ -136,6 +146,7 @@ $(document).ready(function() { var questionData = { question: questionBox.find('.question-box_header_question').val(), type: questionBox.find('#question-type').val(), + required: questionBox.find('.required-checkbox').is(':checked') ? 1 : 0, options: [] }; @@ -229,29 +240,38 @@ $(document).ready(function() { }); $(document).ready(function() { - $('#response-form').on('submit', function(e) { - e.preventDefault(); - - $.ajax({ - url: $(this).attr('action'), - type: $(this).attr('method'), - data: $(this).serialize(), - 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'; - } else { + // 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(); + + $.ajax({ + url: $(this).attr('action'), + type: $(this).attr('method'), + data: $(this).serialize(), + 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'; + } else { + alert('An error occurred. Please try again.'); + } + }, + error: function() { alert('An error occurred. Please try again.'); } - }, - error: function() { - alert('An error occurred. Please try again.'); - } + }); }); }); - }); - - - + }); \ No newline at end of file