From 9fa0f3abea0ec1e5d617e0adae5543a16eca5ff6 Mon Sep 17 00:00:00 2001 From: jostheta Date: Tue, 16 Jul 2024 08:30:23 +0530 Subject: [PATCH] added responses --- application/config/database.php | 4 +- application/config/routes.php | 1 + application/controllers/Forms.php | 67 +++++++++++++++++++-- application/models/Form_model.php | 70 ++++++++++++++++++++++ application/views/forms/form_responses.php | 25 ++++++++ application/views/forms/myforms.php | 7 +++ application/views/forms/preview.php | 2 +- application/views/forms/respond_form.php | 2 +- application/views/forms/responses.php | 0 application/views/forms/user_forms.php | 25 ++++++++ application/views/forms/view_response.php | 59 ++++++++++++++++++ application/views/templates/header.php | 1 + assets/js/script.js | 30 +++++----- 13 files changed, 269 insertions(+), 24 deletions(-) create mode 100644 application/views/forms/form_responses.php create mode 100644 application/views/forms/responses.php create mode 100644 application/views/forms/user_forms.php create mode 100644 application/views/forms/view_response.php 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/config/routes.php b/application/config/routes.php index 3e27766..46c544d 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -8,6 +8,7 @@ $route['my_drafts'] = 'Forms/my_drafts'; $route['my_drafts'] = 'Forms/my_drafts/$1'; $route['forms/delete/(:num)'] = 'forms/delete_form/$1'; $route['forms/respond/(:num)'] = 'forms/respond/$1'; +$route['responses'] = 'Forms/list_user_forms'; diff --git a/application/controllers/Forms.php b/application/controllers/Forms.php index 0dfdc05..47b1700 100644 --- a/application/controllers/Forms.php +++ b/application/controllers/Forms.php @@ -5,6 +5,7 @@ class Forms extends CI_Controller public function __construct() { parent::__construct(); $this->load->model('Form_model'); + $this->load->library('session'); } public function create(){ @@ -143,17 +144,24 @@ class Forms extends CI_Controller $this->load->view('templates/footer'); } - public function publish_form() { - $form_id = $this->input->post('form_id'); + public function publish_form($form_id) { // Update is_published to 1 $this->Form_model->update_form($form_id, ['is_published' => 1]); // Generate a unique link $response_link = base_url("forms/respond/" . $form_id); - - // Send back the response link - echo json_encode(['response_link' => $response_link]); + + // Prepare data for the view + $data = []; + $data['response_link'] = $response_link; + $data['forms'] = $this->Form_model->get_all_forms(); + + $this->load->view('templates/header'); + $this->load->view('forms/myforms',$data); + $this->load->view('templates/footer'); + + } public function respond($form_id){ @@ -163,7 +171,54 @@ class Forms extends CI_Controller $question->options = $this->Form_model->get_options_by_question_id($question->question_id); } $this->load->view('templates/header'); - $this->load->view('forms/respond_form'); + $this->load->view('forms/respond_form',$data); + $this->load->view('templates/footer'); + } + + 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])); + } 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() { + $user_id = $this->session->userdata('user_id'); + $data['forms'] = $this->Form_model->get_forms_by_user($user_id); + + $this->load->view('templates/header'); + $this->load->view('forms/user_forms', $data); + $this->load->view('templates/footer'); + } + + // List all responses for a particular form + public function list_form_responses($form_id) { + $data['responses'] = $this->Form_model->get_responses_by_form($form_id); + $data['form'] = $this->Form_model->get_form($form_id); + + $this->load->view('templates/header'); + $this->load->view('forms/form_responses', $data); + $this->load->view('templates/footer'); + } + + // View a specific response + public function view_response($response_id) { + $data['response'] = $this->Form_model->get_response($response_id); + $data['form'] = $this->Form_model->get_form($data['response']->form_id); + + $this->load->view('templates/header'); + $this->load->view('forms/view_response', $data); $this->load->view('templates/footer'); } diff --git a/application/models/Form_model.php b/application/models/Form_model.php index c3ff3b1..ab8710e 100644 --- a/application/models/Form_model.php +++ b/application/models/Form_model.php @@ -107,6 +107,76 @@ class Form_model extends CI_Model { $this->db->insert('options', $data); return $this->db->insert_id(); } + + public function save_responses($form_id, $responses) { + $this->db->trans_start(); + + // Insert response record + $response_data = [ + 'form_id' => $form_id, + 'user_id' => $this->session->userdata('user_id'), // Set user_id if applicable + 'created_at' => date('Y-m-d H:i:s'), + ]; + $this->db->insert('responses', $response_data); + $response_id = $this->db->insert_id(); + + // Insert each answer + foreach ($responses as $question_id => $answer) { + if (is_array($answer)) { + foreach ($answer as $answer_text) { + $answer_data = [ + 'response_id' => $response_id, + 'question_id' => $question_id, + 'answer_text' => $answer_text, + 'created_at' => date('Y-m-d H:i:s'), + ]; + $this->db->insert('response_answers', $answer_data); + } + } else { + $answer_data = [ + 'response_id' => $response_id, + 'question_id' => $question_id, + 'answer_text' => $answer, + 'created_at' => date('Y-m-d H:i:s'), + ]; + $this->db->insert('response_answers', $answer_data); + } + } + + $this->db->trans_complete(); + + return $this->db->trans_status(); + } + + public function get_forms_by_user($user_id) { + $this->db->where('user_id', $user_id); + $query = $this->db->get('forms'); + return $query->result(); + } + + public function get_responses_by_form($form_id) { + $this->db->where('form_id', $form_id); + $query = $this->db->get('responses'); + return $query->result(); + } + + public function get_response($response_id) { + $this->db->where('response_id', $response_id); + $query = $this->db->get('responses'); + $response = $query->row(); + + $this->db->where('response_id', $response_id); + $query = $this->db->get('response_answers'); + $response->answers = $query->result(); + + return $response; + } + + public function get_form($form_id) { + $this->db->where('form_id', $form_id); + $query = $this->db->get('forms'); + return $query->row(); + } } diff --git a/application/views/forms/form_responses.php b/application/views/forms/form_responses.php new file mode 100644 index 0000000..e9de128 --- /dev/null +++ b/application/views/forms/form_responses.php @@ -0,0 +1,25 @@ +
+

Responses for: title, ENT_QUOTES, 'UTF-8') ?>

+ + + + + + + + + + + + + + + + + + + + + +
Response IDSubmitted At
response_id ?>created_at)) ?>
No responses found.
+
diff --git a/application/views/forms/myforms.php b/application/views/forms/myforms.php index 7714ca1..12f059f 100644 --- a/application/views/forms/myforms.php +++ b/application/views/forms/myforms.php @@ -17,6 +17,13 @@ title, ENT_QUOTES, 'UTF-8') ?> is_published == 0)?'No':'Yes'?> created_at)); ?> + + is_published == 1): ?> + Response link + + Not Published + + diff --git a/application/views/forms/preview.php b/application/views/forms/preview.php index ce85605..ff30626 100644 --- a/application/views/forms/preview.php +++ b/application/views/forms/preview.php @@ -42,7 +42,7 @@

No questions found for this form.

- + Publish diff --git a/application/views/forms/respond_form.php b/application/views/forms/respond_form.php index cd90122..a70c359 100644 --- a/application/views/forms/respond_form.php +++ b/application/views/forms/respond_form.php @@ -44,7 +44,7 @@

No questions found for this form.

- + diff --git a/application/views/forms/responses.php b/application/views/forms/responses.php new file mode 100644 index 0000000..e69de29 diff --git a/application/views/forms/user_forms.php b/application/views/forms/user_forms.php new file mode 100644 index 0000000..e956634 --- /dev/null +++ b/application/views/forms/user_forms.php @@ -0,0 +1,25 @@ +
+

Your Forms

+ + + + + + + + + + + + + + + + + + + + + +
Form TitleCreated At
title, ENT_QUOTES, 'UTF-8') ?>created_at)) ?>
No forms found.
+
diff --git a/application/views/forms/view_response.php b/application/views/forms/view_response.php new file mode 100644 index 0000000..e95281b --- /dev/null +++ b/application/views/forms/view_response.php @@ -0,0 +1,59 @@ +
+
+
+
+

title, ENT_QUOTES, 'UTF-8') ?>

+

description, ENT_QUOTES, 'UTF-8') ?>

+ +

Response ID: response_id ?>

+

Submitted At: created_at)) ?>

+ +
+ + $question) : ?> +
+
+

question_text, ENT_QUOTES, 'UTF-8') ?>

+
+
+ answers as $answer) { + if ($answer->question_id == $question->question_id) { + $answer_text = htmlspecialchars($answer->answer_text, ENT_QUOTES, 'UTF-8'); + break; + } + } + ?> + question_type == 'paragraph') : ?> +
+ +
+ +
+ options)) : ?> + options as $optionIndex => $option) : ?> +
+ question_type == 'multiple-choice') : ?> + option_text, ENT_QUOTES, 'UTF-8')) ? 'checked' : '' ?> readonly> + + question_type == 'checkbox') : ?> + option_text, ENT_QUOTES, 'UTF-8')) !== false) ? 'checked' : '' ?> readonly> + + +
+
+ + +
+ +
+
+ + +

No questions found for this form.

+ +
+
+
+
diff --git a/application/views/templates/header.php b/application/views/templates/header.php index 148e4a7..7f0175c 100644 --- a/application/views/templates/header.php +++ b/application/views/templates/header.php @@ -20,6 +20,7 @@ session->userdata('logged_in')) : ?>
  • My Forms
  • My Drafts
  • +
  • Responses