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 @@