diff --git a/application/config/routes.php b/application/config/routes.php index b9589e7..b3586a2 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -11,6 +11,8 @@ $route['forms/respond/(:num)'] = 'forms/respond/$1'; $route['responses'] = 'Forms/list_user_forms'; $route['edit_form/(:num)'] = 'Forms/edit_form/$1'; +$route['responses/index/(:num)'] = 'Responses/index/$1'; + //Routes of the pages controller diff --git a/application/controllers/Responses.php b/application/controllers/Responses.php new file mode 100644 index 0000000..8b56f9f --- /dev/null +++ b/application/controllers/Responses.php @@ -0,0 +1,53 @@ +load->model('Response_model'); + } + + public function index($form_id) { + $responses = $this->Response_model->get_responses($form_id); + + $data = []; + foreach ($responses as $response) { + $question_id = $response['question_id']; + $question_text = $response['question_text']; + $question_type = $response['question_type']; + $answer_text = $response['answer_text']; + + if (!isset($data[$question_id])) { + $data[$question_id] = [ + 'question_text' => $question_text, + 'question_type' => $question_type, + 'answers' => [] + ]; + if ($question_type == 'multiple-choice' || $question_type == 'checkbox') { + $options = $this->Response_model->get_options($question_id); + foreach ($options as $option) { + $data[$question_id]['options'][$option['option_text']] = 0; + } + } + } + + if ($question_type == 'multiple-choice' || $question_type == 'checkbox') { + if (isset($data[$question_id]['options'][$answer_text])) { + $data[$question_id]['options'][$answer_text]++; + } else { + $data[$question_id]['options'][$answer_text] = 1; // Initialize the count for this option + } + } else { + $data[$question_id]['answers'][] = $answer_text; + } + } + + // // Debug statement + // echo '
'; + // print_r($data); + // echo ''; + + $this->load->view('responses/view', ['data' => $data]); + } +} + + diff --git a/application/models/Response_model.php b/application/models/Response_model.php new file mode 100644 index 0000000..4c2f047 --- /dev/null +++ b/application/models/Response_model.php @@ -0,0 +1,20 @@ +db->select('q.question_id, q.question_text, q.question_type, ra.answer_text'); + $this->db->from('Questions q'); + $this->db->join('Response_Answers ra', 'q.question_id = ra.question_id'); + $this->db->join('Responses r', 'ra.response_id = r.response_id'); + $this->db->where('q.form_id', $form_id); + $query = $this->db->get(); + return $query->result_array(); + } + + public function get_options($question_id) { + $this->db->select('option_id, option_text'); + $this->db->from('Options'); + $this->db->where('question_id', $question_id); + $query = $this->db->get(); + return $query->result_array(); + } +} diff --git a/application/views/forms/form_responses.php b/application/views/forms/form_responses.php index 4b5d232..3693ad0 100644 --- a/application/views/forms/form_responses.php +++ b/application/views/forms/form_responses.php @@ -1,6 +1,8 @@