diff --git a/application/config/config.php b/application/config/config.php index f168a7d..8dfc488 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -23,7 +23,7 @@ defined('BASEPATH') OR exit('No direct script access allowed'); | a PHP script and you can easily do that on your own. | */ -$config['base_url'] = 'http://192.168.2.110/google_forms'; +$config['base_url'] = 'http://localhost/google_forms'; /* |-------------------------------------------------------------------------- diff --git a/application/controllers/Form_controller.php b/application/controllers/Form_controller.php index e5952cb..fc5c28f 100644 --- a/application/controllers/Form_controller.php +++ b/application/controllers/Form_controller.php @@ -3,26 +3,19 @@ defined('BASEPATH') or exit('No direct script access allowed'); class Form_controller extends CI_Controller { - - public function index() { - // Fetch the data - $this->load->model('Form_model'); - $this->load->model('Response_model'); - $data['total_forms'] = $this->Form_model->get_total_forms(); - $data['published_forms'] = $this->Form_model->get_published_forms(); - $data['total_responses'] = $this->Response_model->get_total_responses(); -// var_dump($data); - // Load the view and pass the data - $this->load->view('list_forms', $data); - } + public function index_forms($form_id = null) { $this->load->model('Frontend_model'); + $this->load->model('Form_model'); + $this->load->model('Response_model'); + // Check if the user is logged in if (!$this->session->userdata('logged_in')) { // If not logged in, redirect to login page redirect('users/login'); } + $user_id = $this->session->userdata('user_id'); // Retrieve form title from the forms table using form_id $form_title = 'Untitled Form'; // Default title @@ -32,15 +25,20 @@ class Form_controller extends CI_Controller $form_title = $form['title']; } } - + + // Fetch data from models + $data['total_forms'] = $this->Form_model->get_total_forms($user_id); + $data['published_forms'] = $this->Form_model->get_published_forms($user_id); + $data['total_responses'] = $this->Response_model->get_total_responses($user_id); + $data['forms'] = $this->Form_model->get_all_forms($user_id); + $data['form_title'] = $form_title; + // Load views and data if user is logged in $this->load->view('templates/header'); - - $data = $this->Frontend_model->getforms(); - $this->load->view('Tables/list_forms', ['forms' => $data, 'form_title' => $form_title]); - + $this->load->view('Tables/list_forms', $data); $this->load->view('templates/footer'); } + public function delete($id) { diff --git a/application/controllers/Publish_controller.php b/application/controllers/Publish_controller.php index 3587573..31370b2 100644 --- a/application/controllers/Publish_controller.php +++ b/application/controllers/Publish_controller.php @@ -34,7 +34,7 @@ class Publish_controller extends CI_Controller { $this->load->view('templates/header'); $this->load->view('publish_view', $data); - $this->load->view('templates/footer'); + // $this->load->view('templates/footer'); } diff --git a/application/controllers/Response_submit.php b/application/controllers/Response_submit.php index 6032f5b..a98f813 100644 --- a/application/controllers/Response_submit.php +++ b/application/controllers/Response_submit.php @@ -25,7 +25,7 @@ class Response_submit extends CI_Controller { $this->load->view('templates/header'); $this->load->view('responses_list', $data); - $this->load->view('templates/footer'); + // $this->load->view('templates/footer'); } @@ -93,5 +93,37 @@ class Response_submit extends CI_Controller { $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'); + } + + // Fetch form details + $form = $this->Form_model->get_form_by_id($form_id); + if (!$form) { + show_404(); + } + + // Fetch summary data + $summary_data = $this->Response_model->get_summary_data($form_id); + + $data['form'] = $form; + $data['summary_data'] = $summary_data; + + $this->load->view('templates/header'); + $this->load->view('Forms/summary', $data); + $this->load->view('templates/footer'); +} + + + + + + } diff --git a/application/models/Form_model.php b/application/models/Form_model.php index 0dea148..df48707 100644 --- a/application/models/Form_model.php +++ b/application/models/Form_model.php @@ -4,22 +4,38 @@ defined('BASEPATH') or exit('No direct script access allowed'); class Form_model extends CI_Model { + // Function to get form details by ID + public function get_form_by_id($form_id) + { + $this->load->database(); + $this->db->where('id', $form_id); + $query = $this->db->get('forms'); + return $query->row(); + } // Get the total number of forms - public function get_total_forms() - { - $this->load->database(); - - return $this->db->count_all('forms'); + public function get_total_forms($user_id) { + // Ensure user_id is passed as a parameter and used in the query + $this->db->where('user_id', $user_id); + return $this->db->count_all_results('forms'); // Use count_all_results to ensure WHERE conditions are applied } - - // Get the number of published forms - public function get_published_forms() - { - $this->load->database(); - + + // Function to count published forms + public function get_published_forms($user_id) { + // Ensure user_id and is_published are passed as parameters and used in the query + $this->db->where('user_id', $user_id); $this->db->where('is_published', 1); - return $this->db->count_all_results('forms'); + return $this->db->count_all_results('forms'); // Use count_all_results to ensure WHERE conditions are applied } + + // Function to get all forms + public function get_all_forms($user_id) + { + $this->db->where('user_id', $user_id); + $this->db->order_by('created_at', 'DESC'); + $query = $this->db->get('forms'); + return $query->result(); + } + public function save_form($form_data) { $this->db->trans_start(); diff --git a/application/models/Response_model.php b/application/models/Response_model.php index 75d64d0..1a37424 100644 --- a/application/models/Response_model.php +++ b/application/models/Response_model.php @@ -6,10 +6,16 @@ class Response_model extends CI_Model } // Get the total number of responses - public function get_total_responses() { - return $this->db->count_all('responses'); + public function get_total_responses($user_id) { + // Join the responses table with the forms table + $this->db->select('responses.id'); + $this->db->from('responses'); + $this->db->join('forms', 'responses.form_id = forms.id'); + // Filter by user_id in the forms table + $this->db->where('forms.user_id', $user_id); + return $this->db->count_all_results(); } - + public function insert_response($data) { $this->db->insert('responses', $data); @@ -113,5 +119,54 @@ class Response_model extends CI_Model $query = $this->db->get(); return $query->row(); } + + + // Function to get summary data for a form + public function get_summary_data($form_id) + { + $this->load->database(); + + $summary_data = array(); + + // Get questions for the form + $this->db->select('id,text,type'); + $this->db->where('form_id', $form_id); + $questions = $this->db->get('questions')->result_array(); + + // Get responses count + $this->db->where('form_id', $form_id); + $responses = $this->db->get('responses')->result_array(); + + if (count($responses) > 0) { + $response_ids = array_column($responses, 'id'); + + // Get response answers + $this->db->where_in('response_id', $response_ids); + $response_answers = $this->db->get('response_answers')->result_array(); + + // Process response answers + foreach ($questions as $question) { + $question_id = $question['id']; + $summary_data[$question_id] = [ + 'text' => $question['text'], + 'type' => $question['type'], + 'answers' => [] + ]; + } + + foreach ($response_answers as $answer) { + $question_id = $answer['question_id']; + $answered_text = $answer['answered_text']; + + if (!isset($summary_data[$question_id]['answers'][$answered_text])) { + $summary_data[$question_id]['answers'][$answered_text] = 1; + } else { + $summary_data[$question_id]['answers'][$answered_text]++; + } + } + } + + return $summary_data; + } } diff --git a/application/views/Forms/summary.php b/application/views/Forms/summary.php new file mode 100644 index 0000000..75faa79 --- /dev/null +++ b/application/views/Forms/summary.php @@ -0,0 +1,100 @@ + + +
+ + +