diff --git a/application/config/config.php b/application/config/config.php index 8dfc488..f168a7d 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://localhost/google_forms'; +$config['base_url'] = 'http://192.168.2.110/google_forms'; /* |-------------------------------------------------------------------------- diff --git a/application/config/routes.php b/application/config/routes.php index fc85526..559bf30 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -10,7 +10,6 @@ $route['default_controller'] = 'Form_controller/index_forms'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE; $route['start'] = 'Form_controller/index_forms'; -// $route['new_form'] = 'home/create_form'; $route['title_desc'] = 'homepage/title'; $route['forms/delete/(:any)'] = 'Form_controller/delete/$1'; $route['home'] = 'Form_controller/index_forms'; @@ -26,4 +25,3 @@ $route['response_preview/(:num)'] = 'forms/response_preview/$1'; $route['title'] = 'homepage/title'; -// $route['designform'] = 'homepage/design_form'; diff --git a/application/controllers/Form_controller.php b/application/controllers/Form_controller.php index 8123384..b902a19 100644 --- a/application/controllers/Form_controller.php +++ b/application/controllers/Form_controller.php @@ -1,11 +1,12 @@ load->model('Frontend_model'); + $this->load->model('Frontend_model'); // Check if the user is logged in if (!$this->session->userdata('logged_in')) { // If not logged in, redirect to login page @@ -30,92 +31,91 @@ class Form_controller extends CI_Controller { $this->load->view('templates/footer'); } - public function delete($id) - { - if (!$this->session->userdata('logged_in')) { + public function delete($id) + { + if (!$this->session->userdata('logged_in')) { // If not logged in, redirect to login page redirect('users/login'); } -$this->load->model('Frontend_model'); -$this->Frontend_model->deleteForm($id); -$this->session->set_flashdata('status','Form data deleted successfully'); -redirect('home'); - } - public function __construct() { + $this->load->model('Frontend_model'); + $this->Frontend_model->deleteForm($id); + $this->session->set_flashdata('status', 'Form data deleted successfully'); + redirect('home'); + } + public function __construct() + { parent::__construct(); $this->load->model('Updation_model'); } // Load the form for editing - public function edit_form($form_id) { + public function edit_form($form_id) + { $data['form'] = $this->Updation_model->get_form($form_id); $data['questions'] = $this->Updation_model->get_questions($form_id); $data['options'] = $this->Updation_model->get_options(); - // $this->load->view('templates/header'); + // $this->load->view('templates/header'); $this->load->view('edit_form_view', $data); - // $this->load->view('templates/footer'); + // $this->load->view('templates/footer'); } // Save the edited form - public function update_form() { + public function update_form() + { $formData = $this->input->post('formData'); - + if (!$formData) { echo json_encode(['status' => 'error', 'message' => 'Form data is missing']); return; } - + $form_id = $formData['form_id']; $title = $formData['title']; $description = $formData['description']; $questions = $formData['questions']; - + $this->load->model('Updation_model'); $updateStatus = $this->Updation_model->update_form_data($form_id, $title, $description, $questions); - + if ($updateStatus) { echo json_encode(['status' => 'success', 'message' => 'Form updated successfully']); } else { echo json_encode(['status' => 'error', 'message' => 'Failed to update form data']); } } - - - - - - - public function index_forms_draft($form_id = null) { - $this->load->model('Frontend_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'); - } - - // Retrieve form title from the forms table using form_id - $form_title = 'Untitled Form'; // Default title - if ($form_id) { - $form = $this->Frontend_model->getFormById($form_id); - if ($form) { - $form_title = $form['title']; - } - } - - // Get the user_id from session - $user_id = $this->session->userdata('user_id'); - - // Load views and data if user is logged in - $this->load->view('templates/header'); - - // Get the forms created by the user - $data = $this->Frontend_model->getforms_draft($user_id); - $this->load->view('Tables/draft', ['forms' => $data, 'form_title' => $form_title]); - - $this->load->view('templates/footer'); - } - + + public function index_forms_draft($form_id = null) + { + $this->load->model('Frontend_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'); + } + + // Retrieve form title from the forms table using form_id + $form_title = 'Untitled Form'; // Default title + if ($form_id) { + $form = $this->Frontend_model->getFormById($form_id); + if ($form) { + $form_title = $form['title']; + } + } + + // Get the user_id from session + $user_id = $this->session->userdata('user_id'); + + // Load views and data if user is logged in + $this->load->view('templates/header'); + + // Get the forms created by the user + $data = $this->Frontend_model->getforms_draft($user_id); + $this->load->view('Tables/draft', ['forms' => $data, 'form_title' => $form_title]); + + $this->load->view('templates/footer'); + } + } \ No newline at end of file diff --git a/application/models/Frontend_model.php b/application/models/Frontend_model.php index 883a198..19ba99d 100644 --- a/application/models/Frontend_model.php +++ b/application/models/Frontend_model.php @@ -10,12 +10,12 @@ class Frontend_model extends CI_Model { // Ensure user_id is set if (!$user_id) { - return []; // Return an empty array if user_id is not available + return []; } // Filter forms by user_id and order by created_at in descending order - $this->db->where('user_id', $user_id); // Assuming 'user_id' is the column name in the 'forms' table - $this->db->order_by('created_at', 'DESC'); // Order by created_at column, most recent first + $this->db->where('user_id', $user_id); + $this->db->order_by('created_at', 'DESC'); $query = $this->db->get('forms'); return $query->result(); // Return the result as an array of objects @@ -30,9 +30,9 @@ class Frontend_model extends CI_Model { return $query->row_array(); } public function getforms_draft($user_id) { - $this->db->where('is_published', 0); // Filter by unpublished forms - $this->db->where('user_id', $user_id); // Filter by user_id - $this->db->order_by('created_at', 'DESC'); // Sort by creation date, newest first + $this->db->where('is_published', 0); + $this->db->where('user_id', $user_id); + $this->db->order_by('created_at', 'DESC'); $query = $this->db->get('forms'); return $query->result(); } diff --git a/application/models/New_form_model.php b/application/models/New_form_model.php index 2bf6235..1af773f 100644 --- a/application/models/New_form_model.php +++ b/application/models/New_form_model.php @@ -25,7 +25,7 @@ class New_form_model extends CI_Model { if (!empty($option)) { // Avoid inserting empty options $optionData = [ 'question_id' => $questionId, - 'option_text' => $option // Ensure column name matches database schema + 'option_text' => $option ]; // Insert option into options table $this->db->insert('options', $optionData); diff --git a/application/models/Response_model.php b/application/models/Response_model.php index 46aa549..22ff9e3 100644 --- a/application/models/Response_model.php +++ b/application/models/Response_model.php @@ -1,39 +1,45 @@ db->insert('responses', $data); return $this->db->insert_id(); } - public function insert_response_answer($data) { + public function insert_response_answer($data) + { $this->db->insert('response_answers', $data); } - public function get_form($form_id) { + public function get_form($form_id) + { $this->db->where('id', $form_id); $query = $this->db->get('forms'); return $query->row(); } -// 888888888888888888888 - public function get_questions($form_id) { + + 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) { + public function get_options($question_id) + { $this->db->where('question_id', $question_id); $query = $this->db->get('options'); return $query->result(); } -// 888888888888888888888 - - public function get_responses_by_form($form_id) { + + public function get_responses_by_form($form_id) + { $this->db->select('responses.id as response_id, responses.submitted_at, users.username'); $this->db->from('responses'); $this->db->join('users', 'responses.user_id = users.id'); @@ -48,16 +54,17 @@ class Response_model extends CI_Model { return $responses; } - public function get_answers_by_response_id($response_id) { + public function get_answers_by_response_id($response_id) + { $this->db->select('response_answers.question_id, response_answers.answered_text'); $this->db->from('response_answers'); $this->db->where('response_answers.response_id', $response_id); $query = $this->db->get(); return $query->result(); } -// 888888888888888888888 - public function get_responses($form_id) { + public function get_responses($form_id) + { $this->db->where('form_id', $form_id); $query = $this->db->get('responses'); return $query->result(); @@ -65,7 +72,8 @@ class Response_model extends CI_Model { // Method to get response details - public function get_response($response_id) { + public function get_response($response_id) + { $this->db->select('responses.*, users.email'); $this->db->from('responses'); $this->db->join('users', 'responses.user_id = users.id'); // Assuming 'user_id' is the foreign key in 'responses' @@ -73,10 +81,11 @@ class Response_model extends CI_Model { $query = $this->db->get(); return $query->row(); } - + // Method to get questions and answers for a response - public function get_questions_and_answers($response_id) { + public function get_questions_and_answers($response_id) + { $this->db->select('questions.id AS question_id, questions.text AS question_text, response_answers.answered_text, users.email'); $this->db->from('questions'); $this->db->join('response_answers', 'questions.id = response_answers.question_id'); @@ -86,9 +95,10 @@ class Response_model extends CI_Model { $query = $this->db->get(); return $query->result(); } - - public function get_form_by_response($response_id) { + + public function get_form_by_response($response_id) + { $this->db->select('forms.title, forms.description'); $this->db->from('forms'); $this->db->join('responses', 'forms.id = responses.form_id'); diff --git a/application/models/Updation_model.php b/application/models/Updation_model.php index 320d441..610cad0 100644 --- a/application/models/Updation_model.php +++ b/application/models/Updation_model.php @@ -1,34 +1,39 @@ db->where('id', $form_id); $query = $this->db->get('forms'); return $query->row_array(); } - public function get_questions($form_id) { + public function get_questions($form_id) + { $this->db->where('form_id', $form_id); $this->db->order_by('id', 'ASC'); $query = $this->db->get('questions'); return $query->result_array(); } - public function get_options() { + public function get_options() + { $query = $this->db->get('options'); return $query->result_array(); } - public function update_form_data($form_id, $title, $description, $questions) { + public function update_form_data($form_id, $title, $description, $questions) + { $this->db->trans_start(); - + // Update form title and description $this->db->where('id', $form_id); $this->db->update('forms', ['title' => $title, 'description' => $description]); - + // Update questions $this->db->where('form_id', $form_id); $this->db->delete('questions'); - + foreach ($questions as $question) { $question_data = [ 'form_id' => $form_id, @@ -38,7 +43,7 @@ class Updation_model extends CI_Model { ]; $this->db->insert('questions', $question_data); $question_id = $this->db->insert_id(); - + if (isset($question['options'])) { foreach ($question['options'] as $option_text) { $option_data = [ @@ -49,25 +54,26 @@ class Updation_model extends CI_Model { } } } - + $this->db->trans_complete(); - + return $this->db->trans_status(); } - - - private function update_question_options($question_id, $options) { + + + private function update_question_options($question_id, $options) + { // Fetch existing options for this question $existing_options = $this->db->where('question_id', $question_id)->get('options')->result_array(); $existing_option_texts = array_column($existing_options, 'option_text'); - + // Insert or update options foreach ($options as $option_text) { if (in_array($option_text, $existing_option_texts)) { // Option already exists, no need to insert continue; } - + // Insert new option $option_data = [ 'question_id' => $question_id, @@ -75,7 +81,7 @@ class Updation_model extends CI_Model { ]; $this->db->insert('options', $option_data); } - + // Delete options that are no longer present $options_to_delete = array_diff($existing_option_texts, $options); if (!empty($options_to_delete)) { @@ -84,8 +90,8 @@ class Updation_model extends CI_Model { $this->db->delete('options'); } } - - - + + + } -?> +?> \ No newline at end of file diff --git a/application/views/Tables/draft.php b/application/views/Tables/draft.php index 19daee0..0a6222a 100644 --- a/application/views/Tables/draft.php +++ b/application/views/Tables/draft.php @@ -50,7 +50,7 @@ Drafts class="btn btn-danger btn-sm" style=" background-color: rgb(103, 58, 183); border-color: rgb(103, 58, 183); color: white;">Delete