diff --git a/application/.htaccess b/application/.htaccess
deleted file mode 100644
index 6c63ed4..0000000
--- a/application/.htaccess
+++ /dev/null
@@ -1,6 +0,0 @@
-
- Require all denied
-
-
- Deny from all
-
\ No newline at end of file
diff --git a/application/config/database.php b/application/config/database.php
index b0164e6..7da6d6e 100644
--- a/application/config/database.php
+++ b/application/config/database.php
@@ -76,8 +76,8 @@ $query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
- 'username' => 'admin',
- 'password' => 'Password',
+ 'username' => 'torun',
+ 'password' => 'thug@NIT20',
'database' => 'google_forms',
'dbdriver' => 'mysqli',
'dbprefix' => '',
diff --git a/application/config/routes.php b/application/config/routes.php
index 6c37616..c8b9593 100644
--- a/application/config/routes.php
+++ b/application/config/routes.php
@@ -9,7 +9,7 @@ $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['start'] = 'Form_controller/index_forms';
$route['new_form'] = 'home/create_form';
$route['title_desc'] = 'home/title';
$route['default_page'] = 'Form_controller/index_forms';
diff --git a/application/controllers/Form_controller.php b/application/controllers/Form_controller.php
index 4b8149b..2f0c987 100644
--- a/application/controllers/Form_controller.php
+++ b/application/controllers/Form_controller.php
@@ -60,16 +60,33 @@ redirect('default_page');
// 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']);
+ $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');
diff --git a/application/controllers/Forms.php b/application/controllers/Forms.php
index 8008669..d916740 100644
--- a/application/controllers/Forms.php
+++ b/application/controllers/Forms.php
@@ -34,65 +34,60 @@ class Forms extends CI_Controller
}
- public function response_preview($form_id)
- {
+ 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');
+
+ // 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('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 including 'is_required'
+ $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);
+ }
-
- // 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('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('templates/header');
$this->load->view('form_preview_back', $data);
$this->load->view('templates/footer');
-
}
-
-
+
}
diff --git a/application/controllers/New_form_controller.php b/application/controllers/New_form_controller.php
index 1364c51..e42fea1 100644
--- a/application/controllers/New_form_controller.php
+++ b/application/controllers/New_form_controller.php
@@ -6,8 +6,10 @@ class New_form_controller extends CI_Controller {
public function submit_form() {
if (!$this->session->userdata('logged_in')) {
// If not logged in, redirect to login page
- redirect('users/login');
+ echo json_encode(['status' => 'error', 'message' => 'User not logged in']);
+ return;
}
+
// Decode the formData from the POST request
$formData = $this->input->post('formData');
@@ -20,7 +22,6 @@ class New_form_controller extends CI_Controller {
if ($saveStatus) {
echo json_encode(['status' => 'success', 'message' => 'Form data submitted successfully']);
-
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to save form data']);
}
diff --git a/application/controllers/Publish_controller.php b/application/controllers/Publish_controller.php
index 3c58533..a04a305 100644
--- a/application/controllers/Publish_controller.php
+++ b/application/controllers/Publish_controller.php
@@ -31,11 +31,12 @@ class Publish_controller extends CI_Controller {
$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('templates/header');
$this->load->view('publish_view', $data);
$this->load->view('templates/footer');
}
+
// Method to unpublish a form
public function unpublish_form($form_id) {
diff --git a/application/controllers/Response_submit.php b/application/controllers/Response_submit.php
index 54a32db..a23d016 100644
--- a/application/controllers/Response_submit.php
+++ b/application/controllers/Response_submit.php
@@ -19,24 +19,33 @@ class Response_submit extends CI_Controller {
public function view_responses($form_id) {
$this->load->model('Response_model');
-
+
$data['form'] = $this->Response_model->get_form($form_id);
$data['responses'] = $this->Response_model->get_responses_by_form($form_id);
-
+
$this->load->view('templates/header');
$this->load->view('responses_list', $data);
$this->load->view('templates/footer');
-
}
+
public function submit_form() {
$this->load->model('Response_model');
$responses = $this->input->post('responses');
$user_id = $this->session->userdata('user_id'); // Assuming user_id is stored in session
-
+ $form_id = $this->input->post('form_id'); // Assuming form_id is passed
+
+ // 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')
+ ]);
+
+ // Insert each answer into response_answers table
foreach ($responses as $response) {
$answered_text = '';
-
+
if (isset($response['options'])) {
if (is_array($response['options'])) {
$answered_text = implode(', ', $response['options']);
@@ -46,28 +55,18 @@ class Response_submit extends CI_Controller {
} else if (isset($response['answered_text'])) {
$answered_text = $response['answered_text'];
}
-
- // Find the max response_id for the given form_id
- $this->db->select_max('response_id');
- $this->db->where('form_id', $response['form_id']);
- $max_response_id = $this->db->get('responses')->row()->response_id;
-
- // Increment the response_id by 1
- $new_response_id = $max_response_id + 1;
-
+
$data = [
- 'form_id' => $response['form_id'],
- 'user_id' => $user_id,
+ 'response_id' => $response_id,
'question_id' => $response['question_id'],
'answered_text' => $answered_text,
- 'response_id' => $new_response_id,
'submitted_at' => date('Y-m-d H:i:s')
];
-
- $this->Response_model->insert_response($data);
+
+ $this->Response_model->insert_response_answer($data);
}
-
- redirect('Response_submit/view_responses/' . $response['form_id']);
+
+ redirect('Response_submit/view_responses/' . $form_id);
}
@@ -87,10 +86,12 @@ class Response_submit extends CI_Controller {
$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);
+ $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');
}
+
+
}
diff --git a/application/models/Form_model.php b/application/models/Form_model.php
index c3c4e5a..aad772a 100644
--- a/application/models/Form_model.php
+++ b/application/models/Form_model.php
@@ -21,7 +21,7 @@ class Form_model extends CI_Model {
foreach ($section['options'] as $option_text) {
$option_data = array(
'question_id' => $question_id,
- 'text' => $option_text,
+ 'option_text' => $option_text,
'created_at' => date('Y-m-d H:i:s')
);
diff --git a/application/models/Frontend_model.php b/application/models/Frontend_model.php
index be11bb7..883a198 100644
--- a/application/models/Frontend_model.php
+++ b/application/models/Frontend_model.php
@@ -7,30 +7,32 @@ class Frontend_model extends CI_Model {
{
// 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
+
+ // 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
$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)
+ public function getFormById($form_id)
{
$query = $this->db->get_where('forms', ['id' => $form_id]);
return $query->row_array();
}
- public function getforms_draft($user_id) {
+ 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_on', 'DESC'); // Sort by creation date, newest first
+ $this->db->order_by('created_at', 'DESC'); // Sort by creation date, newest first
$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 c494f68..2bf6235 100644
--- a/application/models/New_form_model.php
+++ b/application/models/New_form_model.php
@@ -2,37 +2,41 @@
class New_form_model extends CI_Model {
public function save_form_data($formId, $formData) {
- if (!$formId) {
- return false; // Handle error if formId is not valid
+ if (!$formId || !isset($formData['questions'])) {
+ return false; // Handle error if formId is not valid or questions are missing
}
-
+
$questions_array = $formData['questions'];
-
- foreach ($questions_array as $question) {
+
+ foreach ($questions_array as $question) {
$questionData = [
'form_id' => $formId,
'text' => $question['text'],
'type' => $question['type'],
- 'required' => ($question['required'] == 'true') ? 1 : 0
+ 'is_required' => isset($question['is_required']) && $question['is_required'] == 'true' ? 1 : 0
];
-
+
$this->db->insert('questions', $questionData);
$questionId = $this->db->insert_id(); // Get the inserted question_id
-
+
// Handle options for multiple-choice, checkboxes, and dropdown questions
- if ($question['type'] === 'multiple-choice' || $question['type'] === 'checkboxes' || $question['type'] === 'dropdown') {
+ if (in_array($question['type'], ['multiple-choice', 'checkboxes', 'dropdown'])) {
foreach ($question['options'] as $option) {
- $optionData = [
- 'question_id' => $questionId,
- 'option_text' => $option
- ];
- // Insert option into options table
- $this->db->insert('options', $optionData);
+ if (!empty($option)) { // Avoid inserting empty options
+ $optionData = [
+ 'question_id' => $questionId,
+ 'option_text' => $option // Ensure column name matches database schema
+ ];
+ // Insert option into options table
+ $this->db->insert('options', $optionData);
+ }
}
}
}
-
+
return true; // Return true indicating success
}
+
+
}
?>
diff --git a/application/models/Preview_model.php b/application/models/Preview_model.php
index 3959b2e..4e2783d 100644
--- a/application/models/Preview_model.php
+++ b/application/models/Preview_model.php
@@ -1,19 +1,22 @@
db->where('id', $form_id);
+ $query = $this->db->get('forms');
+ return $query->row();
+ }
- public function get_form($form_id)
- {
- return $this->db->get_where('forms', array('id' => $form_id))->row();
+ public function get_questions($form_id) {
+ $this->db->where('form_id', $form_id);
+ $query = $this->db->get('questions');
+ return $query->result(); // Ensure this returns objects with the 'is_required' field
}
-
- public function get_questions($form_id)
- {
- return $this->db->get_where('questions', array('form_id' => $form_id))->result();
- }
-
- public function get_options($question_id)
- {
- return $this->db->get_where('options', array('question_id' => $question_id))->result();
+
+ public function get_options($question_id) {
+ $this->db->where('question_id', $question_id);
+ $query = $this->db->get('options');
+ return $query->result(); // Ensure this returns the options related to the question
}
}
\ No newline at end of file
diff --git a/application/models/Publish_model.php b/application/models/Publish_model.php
index b256d14..c3384c9 100644
--- a/application/models/Publish_model.php
+++ b/application/models/Publish_model.php
@@ -10,8 +10,10 @@ 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);
+ $this->db->where('is_published', 1);
+ $this->db->order_by('id', 'DESC'); // Order by id column, most recent first
$query = $this->db->get('forms');
return $query->result();
}
+
}
diff --git a/application/models/Response_model.php b/application/models/Response_model.php
index bb4c93f..46aa549 100644
--- a/application/models/Response_model.php
+++ b/application/models/Response_model.php
@@ -1,17 +1,25 @@
db->insert('responses', $data);
return $this->db->insert_id();
}
+ public function insert_response_answer($data) {
+ $this->db->insert('response_answers', $data);
+ }
+
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) {
$this->db->where('form_id', $form_id);
$query = $this->db->get('questions');
@@ -23,16 +31,33 @@ class Response_model extends CI_Model {
$query = $this->db->get('options');
return $query->result();
}
-
+// 888888888888888888888
+
public function get_responses_by_form($form_id) {
- $this->db->select('responses.response_id, MAX(responses.submitted_at) as submitted_at, users.username');
+ $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');
$this->db->where('responses.form_id', $form_id);
- $this->db->group_by('responses.response_id, users.username');
+ $query = $this->db->get();
+ $responses = $query->result();
+
+ foreach ($responses as &$response) {
+ $response->answers = $this->get_answers_by_response_id($response->response_id);
+ }
+
+ return $responses;
+ }
+
+ 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();
- } public function get_responses($form_id) {
+ }
+// 888888888888888888888
+
+ public function get_responses($form_id) {
$this->db->where('form_id', $form_id);
$query = $this->db->get('responses');
return $query->result();
@@ -41,25 +66,33 @@ class Response_model extends CI_Model {
// Method to get response details
public function get_response($response_id) {
- $this->db->where('response_id', $response_id);
- $query = $this->db->get('responses');
+ $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'
+ $this->db->where('responses.id', $response_id);
+ $query = $this->db->get();
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->select('questions.id AS question_id, questions.text AS question_text, response_answers.answered_text, users.email');
$this->db->from('questions');
- $this->db->join('responses', 'questions.id = responses.question_id');
- $this->db->where('responses.response_id', $response_id);
+ $this->db->join('response_answers', 'questions.id = response_answers.question_id');
+ $this->db->join('responses', 'response_answers.response_id = responses.id');
+ $this->db->join('users', 'responses.user_id = users.id'); // Join to get the user's email
+ $this->db->where('response_answers.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);
+ $this->db->where('responses.id', $response_id);
$query = $this->db->get();
return $query->row();
}
diff --git a/application/models/Updation_model.php b/application/models/Updation_model.php
index f004cb4..320d441 100644
--- a/application/models/Updation_model.php
+++ b/application/models/Updation_model.php
@@ -18,34 +18,74 @@ class Updation_model extends CI_Model {
$query = $this->db->get('options');
return $query->result_array();
}
-
- public function update_form($form_id, $title, $description) {
+ 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]);
- }
-
- public function update_questions($form_id, $questions) {
- // First, delete existing questions
+
+ // Update questions
$this->db->where('form_id', $form_id);
$this->db->delete('questions');
-
- // Insert new questions
+
foreach ($questions as $question) {
- $this->db->insert('questions', [
+ $question_data = [
'form_id' => $form_id,
- 'text' => $question['text']
- ]);
+ 'text' => $question['text'],
+ 'type' => $question['type'],
+ 'is_required' => $question['required'] // Correctly capture the required value
+ ];
+ $this->db->insert('questions', $question_data);
$question_id = $this->db->insert_id();
-
+
if (isset($question['options'])) {
- foreach ($question['options'] as $option) {
- $this->db->insert('options', [
+ foreach ($question['options'] as $option_text) {
+ $option_data = [
'question_id' => $question_id,
- 'option_text' => $option
- ]);
+ 'option_text' => $option_text
+ ];
+ $this->db->insert('options', $option_data);
}
}
}
+
+ $this->db->trans_complete();
+
+ return $this->db->trans_status();
}
+
+
+ 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,
+ 'option_text' => $option_text
+ ];
+ $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)) {
+ $this->db->where('question_id', $question_id);
+ $this->db->where_in('option_text', $options_to_delete);
+ $this->db->delete('options');
+ }
+ }
+
+
+
}
?>
diff --git a/application/views/Frontend/footer.php b/application/views/Frontend/footer.php
deleted file mode 100644
index 12db258..0000000
--- a/application/views/Frontend/footer.php
+++ /dev/null
@@ -1,3 +0,0 @@
-
-