diff --git a/application/config/routes.php b/application/config/routes.php index bb24084..6c37616 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -3,12 +3,16 @@ defined('BASEPATH') OR exit('No direct script access allowed'); $route['forms'] = 'home/index4'; +$route['responses/list/(:num)'] = 'Response_submit/list_responses/$1'; +$route['responses/view/(:num)'] = 'Response_submit/viewresponse/$1'; + $route['default_controller'] = 'Form_controller/index_forms'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE; $route['start'] = 'home/index'; $route['new_form'] = 'home/create_form'; $route['title_desc'] = 'home/title'; -$route['forms_home'] = 'Form_controller/index_forms'; +$route['default_page'] = 'Form_controller/index_forms'; $route['forms/delete/(:any)'] = 'Form_controller/delete/$1'; +// $route['froms_home'] = 'Form_controller/index_forms'; diff --git a/application/controllers/Form.php b/application/controllers/Form.php index 1b70743..5077544 100644 --- a/application/controllers/Form.php +++ b/application/controllers/Form.php @@ -9,6 +9,10 @@ class Form extends CI_Controller { } public function submit() { + if (!$this->session->userdata('logged_in')) { + // If not logged in, redirect to login page + redirect('users/login'); + } $form_data = json_decode($this->input->raw_input_stream, true); if ($this->Form_model->save_form($form_data)) { diff --git a/application/controllers/Form_controller.php b/application/controllers/Form_controller.php index f90b88b..05124c3 100644 --- a/application/controllers/Form_controller.php +++ b/application/controllers/Form_controller.php @@ -3,26 +3,68 @@ defined('BASEPATH') OR exit('No direct script access allowed'); class Form_controller extends CI_Controller { - public function index_forms() - { - $this->load->view('Frontend/header'); + public function index_forms($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'); + } - $this->load->model('Frontend_model'); + // 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']; + } + } - $data= $this->Frontend_model->getforms(); - $this->load->view('Tables/list_forms',['forms'=> $data]); + // 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('Frontend/footer'); - - } + $this->load->view('templates/footer'); + } 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('forms_home'); +redirect('default_page'); } + public function __construct() { + parent::__construct(); + $this->load->model('Updation_model'); + } + // Load the form for editing + 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('edit_form_view', $data); + } + + // Save the edited form + public function update_form() { + $form_id = $this->input->post('form_id'); + $title = $this->input->post('title'); + $description = $this->input->post('description'); + $questions = $this->input->post('questions'); + + $this->Updation_model->update_form($form_id, $title, $description); + $this->Updation_model->update_questions($form_id, $questions); + + echo json_encode(['status' => 'success']); + } } \ No newline at end of file diff --git a/application/controllers/Forms.php b/application/controllers/Forms.php index e5e01d0..8008669 100644 --- a/application/controllers/Forms.php +++ b/application/controllers/Forms.php @@ -5,6 +5,10 @@ class Forms extends CI_Controller { public function preview($form_id) { + if (!$this->session->userdata('logged_in')) { + // If not logged in, redirect to login page + redirect('users/login'); + } // Load the model that handles the form data $this->load->model('preview_model'); @@ -23,12 +27,21 @@ class Forms extends CI_Controller $data['form'] = $form; $data['questions'] = $questions; + $this->load->view('templates/header'); + $this->load->view('form_preview', $data); - // redirect('home/preview_forms'); - + $this->load->view('templates/footer'); + } + public function response_preview($form_id) { + if (!$this->session->userdata('logged_in')) { + // If not logged in, redirect to login page + redirect('users/login'); + } + + // Load the model that handles the form data $this->load->model('preview_model'); @@ -50,6 +63,36 @@ class Forms extends CI_Controller $this->load->view('response_submit', $data); } + public function preview_back($form_id) + { + if (!$this->session->userdata('logged_in')) { + // If not logged in, redirect to login page + redirect('users/login'); + } + // Load the model that handles the form data + $this->load->model('preview_model'); + + // Fetch the form details + $form = $this->preview_model->get_form($form_id); + + // Fetch the questions for the form + $questions = $this->preview_model->get_questions($form_id); + + // Fetch the options for each question + foreach ($questions as &$question) { + $question->options = $this->preview_model->get_options($question->id); + } + + // Pass the data to the view + $data['form'] = $form; + $data['questions'] = $questions; + + $this->load->view('templates/header'); + + $this->load->view('form_preview_back', $data); + $this->load->view('templates/footer'); + + } } diff --git a/application/controllers/Home.php b/application/controllers/Home.php index 7958f0b..42a82c8 100644 --- a/application/controllers/Home.php +++ b/application/controllers/Home.php @@ -4,53 +4,32 @@ defined('BASEPATH') or exit('No direct script access allowed'); class Home extends CI_Controller { - public function index1() + // index2-default + public function default_page() { - $this->load->view('templates/header'); - $this->load->view('pages/about'); - $this->load->view('templates/footer'); - - - } - public function index2() - { - // $this->load->view('templates/header'); $this->load->view('Frontend/header'); $this->load->view('Form_controller/index_forms'); - // $this->load->view('templates/footer'); $this->load->view('Frontend/footer'); + } + public function design_form() + { + $this->load->view('templates/forms_ui'); } - public function index3() + public function title() { $this->load->view('templates/header'); - $this->load->view('pages/homepage'); + $this->load->view('templates/form_title'); $this->load->view('templates/footer'); -} -public function design_form(){ - $this->load->view('templates/forms_ui'); -} -public function title(){ - $this->load->view('templates/header'); - $this->load->view('templates/form_title'); - $this->load->view('templates/footer'); + } + public function ui_forms() + { + $this->load->view('templates/forms_ui'); + } -} -public function index4() -{ - $this->load->view('templates/forms_ui'); -} -// public function preview_forms($data) - -// { $this->load->view('templates/header'); - -// $this->load->view('form_preview',$data); -// $this->load->view('templates/footer'); - -// } } \ No newline at end of file diff --git a/application/controllers/New_form.php b/application/controllers/New_form.php index c4e0cb3..f2dab84 100644 --- a/application/controllers/New_form.php +++ b/application/controllers/New_form.php @@ -3,6 +3,10 @@ class New_form extends CI_Controller { public function create_form() { + if (!$this->session->userdata('logged_in')) { + // If not logged in, redirect to login page + redirect('users/login'); + } $data['title'] = 'Form Details'; $this->form_validation->set_rules('title', 'Title', 'required'); $this->form_validation->set_rules('description', 'Description', 'required'); diff --git a/application/controllers/New_form_controller.php b/application/controllers/New_form_controller.php index fea61d5..d8d7495 100644 --- a/application/controllers/New_form_controller.php +++ b/application/controllers/New_form_controller.php @@ -4,21 +4,28 @@ defined('BASEPATH') OR exit('No direct script access allowed'); class New_form_controller extends CI_Controller { public function submit_form() { - // no need to decode the data + if (!$this->session->userdata('logged_in')) { + // If not logged in, redirect to login page + redirect('users/login'); + } + // Decode the formData from the POST request $formData = $this->input->post('formData'); - - $formId = $this->session->userdata('form_id'); + // Check if form_id is set in session + $formId = $this->session->userdata('form_id'); if ($formId) { - // Save questions and options associated with the form_id + // Load the model and save form data $this->load->model('new_form_model'); - $this->new_form_model->save_form_data($formId, $formData); + $saveStatus = $this->new_form_model->save_form_data($formId, $formData); - echo json_encode(['status' => 'success', 'message' => 'Form data submitted successfully']); + if ($saveStatus) { + echo json_encode(['status' => 'success', 'message' => 'Form data submitted successfully']); + } else { + echo json_encode(['status' => 'error', 'message' => 'Failed to save form data']); + } } else { - echo json_encode(['status' => 'error', 'message' => 'Failed to submit form data']); + echo json_encode(['status' => 'error', 'message' => 'Form ID not found in session']); } } - } -?> \ No newline at end of file +?> diff --git a/application/controllers/Publish_controller.php b/application/controllers/Publish_controller.php index bd01d86..cc1a880 100644 --- a/application/controllers/Publish_controller.php +++ b/application/controllers/Publish_controller.php @@ -6,6 +6,10 @@ class Publish_controller extends CI_Controller { // Method to publish a form public function publish_form($form_id) { // Generate a unique link + if (!$this->session->userdata('logged_in')) { + // If not logged in, redirect to login page + redirect('users/login'); + } $response_link = base_url("forms/response_preview/" . $form_id); $this->load->model('Publish_model'); // Update is_published to 1 and set the response link @@ -20,12 +24,16 @@ $this->load->model('Publish_model'); // Method to list published forms of a user public function list_user_published_forms() { + if (!$this->session->userdata('logged_in')) { + // If not logged in, redirect to login page + redirect('users/login'); + } $user_id = $this->session->userdata('user_id'); $this->load->model('Publish_model'); $data['forms'] = $this->Publish_model->get_published_forms_by_user($user_id); - $this->load->view('Frontend/header'); + $this->load->view('templates/header'); $this->load->view('publish_view', $data); - $this->load->view('Frontend/footer'); + $this->load->view('templates/footer'); } } diff --git a/application/controllers/Response_submit.php b/application/controllers/Response_submit.php index 80b6ac2..54a32db 100644 --- a/application/controllers/Response_submit.php +++ b/application/controllers/Response_submit.php @@ -69,4 +69,28 @@ class Response_submit extends CI_Controller { redirect('Response_submit/view_responses/' . $response['form_id']); } + + + // 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'] = $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'); + } } diff --git a/application/controllers/Users.php b/application/controllers/Users.php index 9620894..2dd567f 100644 --- a/application/controllers/Users.php +++ b/application/controllers/Users.php @@ -71,7 +71,7 @@ class Users extends CI_Controller // Set message $this->session->set_flashdata('user_loggedin', 'You are now logged in'); - redirect('forms_home'); + redirect('default_page'); } else { // Set message $this->session->set_flashdata('login_failed', 'Login is invalid'); diff --git a/application/models/Frontend_model.php b/application/models/Frontend_model.php index 00da6d1..c9edce3 100644 --- a/application/models/Frontend_model.php +++ b/application/models/Frontend_model.php @@ -3,13 +3,28 @@ defined('BASEPATH') OR exit('No direct script access allowed'); class Frontend_model extends CI_Model { - public function getforms(){ - $query = $this->db->get("forms"); - return $query->result(); + public function getforms() + { + // Get the user_id from session + $user_id = $this->session->userdata('user_id'); + + // Ensure user_id is set + if (!$user_id) { + return []; // Return an empty array if user_id is not available + } + + // Filter forms by user_id + $this->db->where('user_id', $user_id); // Assuming 'user_id' is the column name in the 'forms' table + $query = $this->db->get('forms'); + + return $query->result(); // Return the result as an array of objects } - public function deleteForm($id){ return $this->db->delete('forms', ['id' => $id]); } - + public function getFormById($form_id) + { + $query = $this->db->get_where('forms', ['id' => $form_id]); + return $query->row_array(); + } } \ No newline at end of file diff --git a/application/models/New_form_model.php b/application/models/New_form_model.php index c6f0ef1..c494f68 100644 --- a/application/models/New_form_model.php +++ b/application/models/New_form_model.php @@ -1,35 +1,38 @@ $formId, - 'text' => $question['text'], - 'type' => $question['type'], - 'required' => ($question['required'] == 'true') ? 0 : 1 + 'form_id' => $formId, + 'text' => $question['text'], + 'type' => $question['type'], + 'required' => ($question['required'] == 'true') ? 1 : 0 ]; - + $this->db->insert('questions', $questionData); $questionId = $this->db->insert_id(); // Get the inserted question_id - - foreach ($question['options'] as $option) { - $optionData = [ 'question_id' => $questionId, - 'option_text' => $option]; - - // Insert option into options table - $this->db->insert('options', $optionData); + + // Handle options for multiple-choice, checkboxes, and dropdown questions + if ($question['type'] === 'multiple-choice' || $question['type'] === 'checkboxes' || $question['type'] === 'dropdown') { + foreach ($question['options'] as $option) { + $optionData = [ + 'question_id' => $questionId, + 'option_text' => $option + ]; + // Insert option into options table + $this->db->insert('options', $optionData); + } } } - + return true; // Return true indicating success } - - - } ?> diff --git a/application/models/Publish_model.php b/application/models/Publish_model.php index 8365fac..b256d14 100644 --- a/application/models/Publish_model.php +++ b/application/models/Publish_model.php @@ -10,7 +10,7 @@ public function update_form($form_id, $data) { // Method to retrieve published forms by user public function get_published_forms_by_user($user_id) { $this->db->where('user_id', $user_id); - $this->db->where('is_published', 1); // Ensure only published forms are retrieved + $this->db->where('is_published', 1); $query = $this->db->get('forms'); return $query->result(); } diff --git a/application/models/Response_model.php b/application/models/Response_model.php index 5766487..bb4c93f 100644 --- a/application/models/Response_model.php +++ b/application/models/Response_model.php @@ -32,5 +32,35 @@ class Response_model extends CI_Model { $this->db->group_by('responses.response_id, users.username'); $query = $this->db->get(); return $query->result(); + } public function get_responses($form_id) { + $this->db->where('form_id', $form_id); + $query = $this->db->get('responses'); + return $query->result(); + } + + + // Method to get response details + public function get_response($response_id) { + $this->db->where('response_id', $response_id); + $query = $this->db->get('responses'); + return $query->row(); + } + + // Method to get questions and answers for a response + public function get_questions_and_answers($response_id) { + $this->db->select('questions.id AS question_id, questions.text AS question_text, responses.answered_text'); + $this->db->from('questions'); + $this->db->join('responses', 'questions.id = responses.question_id'); + $this->db->where('responses.response_id', $response_id); + $query = $this->db->get(); + return $query->result(); + } + 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'); + $this->db->where('responses.response_id', $response_id); + $query = $this->db->get(); + return $query->row(); } } diff --git a/application/models/Updation_model.php b/application/models/Updation_model.php new file mode 100644 index 0000000..f004cb4 --- /dev/null +++ b/application/models/Updation_model.php @@ -0,0 +1,51 @@ +db->where('id', $form_id); + $query = $this->db->get('forms'); + return $query->row_array(); + } + + 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() { + $query = $this->db->get('options'); + return $query->result_array(); + } + + public function update_form($form_id, $title, $description) { + $this->db->where('id', $form_id); + $this->db->update('forms', ['title' => $title, 'description' => $description]); + } + + public function update_questions($form_id, $questions) { + // First, delete existing questions + $this->db->where('form_id', $form_id); + $this->db->delete('questions'); + + // Insert new questions + foreach ($questions as $question) { + $this->db->insert('questions', [ + 'form_id' => $form_id, + 'text' => $question['text'] + ]); + $question_id = $this->db->insert_id(); + + if (isset($question['options'])) { + foreach ($question['options'] as $option) { + $this->db->insert('options', [ + 'question_id' => $question_id, + 'option_text' => $option + ]); + } + } + } + } +} +?> diff --git a/application/views/Frontend/header.php b/application/views/Frontend/header.php index 81b8f59..d8be42d 100644 --- a/application/views/Frontend/header.php +++ b/application/views/Frontend/header.php @@ -25,9 +25,8 @@