diff --git a/application/controllers/Forms.php b/application/controllers/Forms.php index 05af47e..e5e01d0 100644 --- a/application/controllers/Forms.php +++ b/application/controllers/Forms.php @@ -27,5 +27,29 @@ class Forms extends CI_Controller // redirect('home/preview_forms'); } + public function response_preview($form_id) + { + // Load the model that handles the form data + $this->load->model('preview_model'); + + // Fetch the form details + $form = $this->preview_model->get_form($form_id); + + // Fetch the questions for the form + $questions = $this->preview_model->get_questions($form_id); + + // Fetch the options for each question + foreach ($questions as &$question) { + $question->options = $this->preview_model->get_options($question->id); + } + + // Pass the data to the view + $data['form'] = $form; + $data['questions'] = $questions; + + $this->load->view('response_submit', $data); + + } + } diff --git a/application/controllers/Home.php b/application/controllers/Home.php index 8815208..7958f0b 100644 --- a/application/controllers/Home.php +++ b/application/controllers/Home.php @@ -4,13 +4,6 @@ defined('BASEPATH') or exit('No direct script access allowed'); class Home extends CI_Controller { - public function index() - { - $this->load->view('templates/header'); - $this->load->view('templates/footer'); - - } - public function index1() { $this->load->view('templates/header'); diff --git a/application/controllers/Publish_controller.php b/application/controllers/Publish_controller.php index e99a72b..bd01d86 100644 --- a/application/controllers/Publish_controller.php +++ b/application/controllers/Publish_controller.php @@ -6,7 +6,7 @@ class Publish_controller extends CI_Controller { // Method to publish a form public function publish_form($form_id) { // Generate a unique link - $response_link = base_url("forms/preview/" . $form_id); + $response_link = base_url("forms/response_preview/" . $form_id); $this->load->model('Publish_model'); // Update is_published to 1 and set the response link $this->Publish_model->update_form($form_id, [ diff --git a/application/controllers/Response_submit.php b/application/controllers/Response_submit.php new file mode 100644 index 0000000..80b6ac2 --- /dev/null +++ b/application/controllers/Response_submit.php @@ -0,0 +1,72 @@ +load->model('Response_model'); + + $data['form'] = $this->Response_model->get_form($form_id); + $questions = $this->Response_model->get_questions($form_id); + + foreach ($questions as $question) { + $question->options = $this->Response_model->get_options($question->id); + } + + $data['questions'] = $questions; + + // Redirect to the view_responses function in the Response_submit controller + redirect('Response_submit/view_responses/' . $form_id); + } + + public function view_responses($form_id) { + $this->load->model('Response_model'); + + $data['form'] = $this->Response_model->get_form($form_id); + $data['responses'] = $this->Response_model->get_responses_by_form($form_id); + + $this->load->view('templates/header'); + $this->load->view('responses_list', $data); + $this->load->view('templates/footer'); + + } + + public function submit_form() { + $this->load->model('Response_model'); + $responses = $this->input->post('responses'); + $user_id = $this->session->userdata('user_id'); // Assuming user_id is stored in session + + foreach ($responses as $response) { + $answered_text = ''; + + if (isset($response['options'])) { + if (is_array($response['options'])) { + $answered_text = implode(', ', $response['options']); + } else { + $answered_text = $response['options']; + } + } else if (isset($response['answered_text'])) { + $answered_text = $response['answered_text']; + } + + // Find the max response_id for the given form_id + $this->db->select_max('response_id'); + $this->db->where('form_id', $response['form_id']); + $max_response_id = $this->db->get('responses')->row()->response_id; + + // Increment the response_id by 1 + $new_response_id = $max_response_id + 1; + + $data = [ + 'form_id' => $response['form_id'], + 'user_id' => $user_id, + 'question_id' => $response['question_id'], + 'answered_text' => $answered_text, + 'response_id' => $new_response_id, + 'submitted_at' => date('Y-m-d H:i:s') + ]; + + $this->Response_model->insert_response($data); + } + + redirect('Response_submit/view_responses/' . $response['form_id']); + } +} diff --git a/application/models/Response_model.php b/application/models/Response_model.php new file mode 100644 index 0000000..5766487 --- /dev/null +++ b/application/models/Response_model.php @@ -0,0 +1,36 @@ +db->insert('responses', $data); + return $this->db->insert_id(); + } + + public function get_form($form_id) { + $this->db->where('id', $form_id); + $query = $this->db->get('forms'); + return $query->row(); + } + + public function get_questions($form_id) { + $this->db->where('form_id', $form_id); + $query = $this->db->get('questions'); + return $query->result(); + } + + public function get_options($question_id) { + $this->db->where('question_id', $question_id); + $query = $this->db->get('options'); + return $query->result(); + } + + public function get_responses_by_form($form_id) { + $this->db->select('responses.response_id, MAX(responses.submitted_at) as submitted_at, users.username'); + $this->db->from('responses'); + $this->db->join('users', 'responses.user_id = users.id'); + $this->db->where('responses.form_id', $form_id); + $this->db->group_by('responses.response_id, users.username'); + $query = $this->db->get(); + return $query->result(); + } +} diff --git a/application/views/Frontend/header.php b/application/views/Frontend/header.php index 1a6ccb9..81b8f59 100644 --- a/application/views/Frontend/header.php +++ b/application/views/Frontend/header.php @@ -27,7 +27,7 @@
  • About
  • Published Forms
  • -
  • Responses
  • +
  • Responses
  • diff --git a/assets/css/styles.css b/assets/css/styles.css index f8c908b..9adafaf 100644 --- a/assets/css/styles.css +++ b/assets/css/styles.css @@ -213,3 +213,6 @@ input:checked + .slider { input:checked + .slider:before { transform: translateX(14px); } +.body_header_bg{ + background-color: rgb(240,235,248); +} \ No newline at end of file diff --git a/assets/js/scripts.js b/assets/js/scripts.js index 8751be3..2044b2a 100644 --- a/assets/js/scripts.js +++ b/assets/js/scripts.js @@ -1,8 +1,7 @@ $(document).ready(function() { let index = 1; let activeSection = null; - - // Add option function +// Add option function function addOption(type, container) { let optionIndex = container.children().length + 1; let optionHtml; @@ -24,8 +23,7 @@ $(document).ready(function() { } container.append(optionHtml); } - - // Form section function +//Form section function function createFormSection() { let newSection = `
    @@ -65,11 +63,11 @@ $(document).ready(function() { left: position.left - buttonWidth - 47 + 'px', top: position.top + activeSection.height() / 2 - buttonHeight / 2 + 'px' }); - } + } + } - - // Event handler is triggered - // creates a new form section;sets it as active;repositions the add section button +//Event handler is triggered +// creates a new form section;sets it as active;respositions the add section button $('#add-section-btn').on('click', function() { createFormSection(); $('.form-section').removeClass('active'); @@ -77,8 +75,7 @@ $(document).ready(function() { activeSection.addClass('active'); positionAddSectionButton(); }); - - // It updates the options container based on the selected type, adding the necessary input fields or buttons. +// It updates the options container based on the selected type, adding the necessary input fields or buttons. $(document).on('change', '.custom-select', function() { let type = $(this).val(); let container = $(this).closest('.form-section').find('.options-container'); @@ -95,17 +92,15 @@ $(document).ready(function() { $(this).closest('.form-section').append(''); } }); - - // add option event handler - // adds a new option to the options container and updates the option numbers +// add option event handler +// adds a new option to the options container and updates the option numbers $(document).on('click', '.add-option-btn', function() { let type = $(this).closest('.form-section').find('.custom-select').val(); let container = $(this).closest('.form-section').find('.options-container'); addOption(type, container); - // refreshOptionNumbers(container); - }); - // removes the section;updates the active section;repositions add section button + }); +// removes the section;updates the active section;repositions add section button $(document).on('click', '.delete-section-icon', function() { let section = $(this).closest('.form-section'); let prevSection = section.prev('.form-section'); @@ -116,8 +111,9 @@ $(document).ready(function() { } if (prevSection.length > 0) { prevSection.find('.delete-section-icon').appendTo(prevSection.find('.form-section')); - activeSection = prevSection; - } else if (nextSection.length > 0) { + activeSection = prevSection;row + } + else if (nextSection.length > 0) { nextSection.find('.delete-section-icon').appendTo(nextSection.find('.form-header')); activeSection = nextSection; } @@ -125,17 +121,21 @@ $(document).ready(function() { positionAddSectionButton(); }); - // delete option +// delele option $(document).on('click', '.delete-option-icon', function() { let option = $(this).closest('.option'); let container = option.closest('.options-container'); option.remove(); -K }); - // Event handler for required toggle button + }); + // Event handler for required toggle button $(document).on('click', '.required-toggle', function() { $(this).closest('.form-section').toggleClass('required'); }); + $(document).on('click', '.required-toggle', function() { + $(this).closest('.form-section').toggleClass('required'); + }); + // Preview button functionality $('#preview-btn').on('click', function() {