google_forms/application/controllers/Response_submit.php

173 lines
5.6 KiB
PHP
Raw Permalink Normal View History

2024-07-17 12:21:51 +00:00
<?php
2024-08-09 12:04:48 +00:00
class Response_submit extends CI_Controller
{
public function view($form_id)
{
2024-07-17 12:21:51 +00:00
$this->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
2024-07-22 12:39:47 +00:00
redirect('responses/' . $form_id);
2024-07-17 12:21:51 +00:00
}
2024-08-09 12:04:48 +00:00
public function view_responses($form_id)
{
2024-07-17 12:21:51 +00:00
$this->load->model('Response_model');
2024-08-09 12:04:48 +00:00
2024-07-17 12:21:51 +00:00
$data['form'] = $this->Response_model->get_form($form_id);
$data['responses'] = $this->Response_model->get_responses_by_form($form_id);
2024-08-09 12:04:48 +00:00
2024-07-17 12:21:51 +00:00
$this->load->view('templates/header');
$this->load->view('responses_list', $data);
2024-07-29 09:17:16 +00:00
// $this->load->view('templates/footer');
2024-07-17 12:21:51 +00:00
}
2024-08-09 12:04:48 +00:00
public function submit_form()
{
2024-07-17 12:21:51 +00:00
$this->load->model('Response_model');
$responses = $this->input->post('responses');
$user_id = $this->session->userdata('user_id'); // Assuming user_id is stored in session
2024-07-22 09:49:37 +00:00
$form_id = $this->input->post('form_id'); // Assuming form_id is passed
2024-08-09 12:04:48 +00:00
2024-07-22 09:49:37 +00:00
// Insert into responses table
$response_id = $this->Response_model->insert_response([
'form_id' => $form_id,
'user_id' => $user_id,
'submitted_at' => date('Y-m-d H:i:s')
]);
2024-08-09 12:04:48 +00:00
2024-07-22 09:49:37 +00:00
// Insert each answer into response_answers table
2024-07-17 12:21:51 +00:00
foreach ($responses as $response) {
$answered_text = '';
2024-08-09 12:04:48 +00:00
2024-07-17 12:21:51 +00:00
if (isset($response['options'])) {
if (is_array($response['options'])) {
$answered_text = implode(', ', $response['options']);
} else {
$answered_text = $response['options'];
}
2024-08-09 12:04:48 +00:00
} elseif (isset($response['answered_text'])) {
2024-07-17 12:21:51 +00:00
$answered_text = $response['answered_text'];
}
2024-08-09 12:04:48 +00:00
2024-07-17 12:21:51 +00:00
$data = [
2024-07-22 09:49:37 +00:00
'response_id' => $response_id,
2024-07-17 12:21:51 +00:00
'question_id' => $response['question_id'],
'answered_text' => $answered_text,
'submitted_at' => date('Y-m-d H:i:s')
];
2024-08-09 12:04:48 +00:00
2024-07-22 09:49:37 +00:00
$this->Response_model->insert_response_answer($data);
2024-07-17 12:21:51 +00:00
}
2024-08-09 12:04:48 +00:00
2024-07-22 12:39:47 +00:00
redirect('responses/' . $form_id);
2024-07-17 12:21:51 +00:00
}
2024-08-09 12:04:48 +00:00
// Method to list responses for a form
public function list_responses($form_id)
{
$this->load->model('Response_model');
$data['form'] = $this->Response_model->get_form($form_id);
$data['responses'] = $this->Response_model->get_responses($form_id);
$this->load->view('Frontend/header');
$this->load->view('responses_list_view', $data);
$this->load->view('Frontend/footer');
}
// Method to view questions and answers for a specific response
public function viewresponse($response_id)
{
$this->load->model('Response_model');
$data['response'] = $this->Response_model->get_response($response_id);
$data['form'] = $this->Response_model->get_form_by_response($response_id); // Get form details
$data['questions_and_answers'] = $this->Response_model->get_questions_and_answers($response_id);
$this->load->view('templates/header');
$this->load->view('response_details_view', $data);
$this->load->view('templates/footer');
}
public function summary($form_id)
{
$this->load->model('Form_model');
$this->load->model('Response_model');
// Check if the user is logged in
if (!$this->session->userdata('logged_in')) {
redirect('users/login');
2024-07-19 10:46:18 +00:00
}
2024-08-09 12:04:48 +00:00
// Fetch form details
$form = $this->Form_model->get_form_by_id($form_id);
if (!$form) {
show_404();
2024-07-19 10:46:18 +00:00
}
2024-07-29 09:17:16 +00:00
2024-08-09 12:04:48 +00:00
// Fetch summary data
$summary_data = $this->Response_model->get_summary_data($form_id);
$data['form'] = $form;
$data['summary_data'] = $summary_data;
2024-07-29 09:17:16 +00:00
2024-08-09 12:04:48 +00:00
$this->load->view('Forms/summary', $data);
2024-07-29 09:17:16 +00:00
}
2024-08-09 12:04:48 +00:00
public function __construct()
{
parent::__construct();
$this->load->model('response_model');
}
2024-07-29 09:17:16 +00:00
2024-08-09 12:04:48 +00:00
public function index()
{
$data['responses'] = $this->response_model->get_responses_with_details();
$this->load->view('templates/header');
$this->load->view('allresponse_details_view', $data);
$this->load->view('templates/footer');
}
public function response_summary()
{
$data['forms'] = $this->Response_model->get_forms();
$this->load->view('templates/header');
$this->load->view('responses/response_summary', $data);
$this->load->view('templates/footer');
}
2024-07-29 09:17:16 +00:00
2024-08-09 12:04:48 +00:00
public function response_summary_by_form($form_id)
{
$responses = $this->Response_model->get_response_summary_by_form($form_id);
$data = $this->prepare_data($responses);
$data['form_id'] = $form_id;
2024-07-29 09:17:16 +00:00
2024-08-09 12:04:48 +00:00
$this->load->view('templates/header');
$this->load->view('responses/summary', $data);
$this->load->view('templates/footer');
}
2024-07-29 09:17:16 +00:00
2024-08-09 12:04:48 +00:00
private function prepare_data($responses)
{
$data = [];
foreach ($responses as $response) {
$question_text = $response->question_text;
if (!isset($data[$question_text])) {
$data[$question_text] = ['type' => $response->question_type, 'answers' => []];
}
$data[$question_text]['answers'][] = $response->answer_text;
}
return $data;
}
2024-07-17 12:21:51 +00:00
}