diff --git a/application/config/database.php b/application/config/database.php index de9475a..e8ab51e 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' => 'root', - 'password' => '', + 'username' => 'jostheta', + 'password' => 'Pa$$w0rd', 'database' => 'gforms', 'dbdriver' => 'mysqli', 'dbprefix' => '', diff --git a/application/config/routes.php b/application/config/routes.php index b8f50cb..3e27766 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -6,6 +6,9 @@ $route['create'] = 'Forms/create'; $route['my_forms'] = 'Forms/my_forms'; $route['my_drafts'] = 'Forms/my_drafts'; $route['my_drafts'] = 'Forms/my_drafts/$1'; +$route['forms/delete/(:num)'] = 'forms/delete_form/$1'; +$route['forms/respond/(:num)'] = 'forms/respond/$1'; + //Routes of the pages controller diff --git a/application/controllers/Forms.php b/application/controllers/Forms.php index cb65315..0dfdc05 100644 --- a/application/controllers/Forms.php +++ b/application/controllers/Forms.php @@ -63,6 +63,110 @@ class Forms extends CI_Controller $this->load->view('forms/view_form', $data); $this->load->view('templates/footer', $data); } + + public function delete_form($form_id) { + $this->load->model('Form_Model'); + if ($this->Form_Model->delete_form($form_id)) { + $this->session->set_flashdata('message', 'Form deleted successfully.'); + } else { + $this->session->set_flashdata('error', 'There was a problem deleting the form.'); + } + redirect('forms/my_drafts'); + } + + public function update_form() { + $form_id = $this->input->post('form_id'); + $title = $this->input->post('title'); + $description = $this->input->post('description'); + $questions = json_decode($this->input->post('questions'), true); + + // Load the model + $this->load->model('Form_Model'); + + // Update form details + $form_data = array( + 'title' => $title, + 'description' => $description, + ); + $this->Form_Model->update_form($form_id, $form_data); + + // Update or add questions + foreach ($questions as $question) { + $question_id = isset($question['question_id']) ? $question['question_id'] : null; + $question_data = array( + 'form_id' => $form_id, + 'question_text' => $question['question_text'], + 'question_type' => $question['question_type'], + ); + if ($question_id) { + // Update existing question + $this->Form_Model->update_question($question_id, $question_data); + } else { + // Add new question + $question_id = $this->Form_Model->add_question($question_data); + } + + // Update or add options for each question + if (isset($question['options']) && is_array($question['options'])) { + foreach ($question['options'] as $option) { + $option_id = isset($option['option_id']) ? $option['option_id'] : null; + $option_data = array( + 'question_id' => $question_id, + 'option_text' => $option['option_text'], + ); + if ($option_id) { + // Update existing option + $this->Form_Model->update_option($option_id, $option_data); + + } else { + // Add new option + $this->Form_Model->add_option($option_data); + + } + + } + } + } + + // Return success response or redirect + echo json_encode(array('success' => true)); + } + + public function preview($form_id){ + $data['form'] = $this->Form_model->get_form_by_id($form_id); + $data['questions'] = $this->Form_model->get_questions_by_form_id($form_id); + foreach ($data['questions'] as &$question) { + $question->options = $this->Form_model->get_options_by_question_id($question->question_id); + } + $this->load->view('templates/header'); + $this->load->view('forms/preview', $data); + $this->load->view('templates/footer'); + } + + public function publish_form() { + $form_id = $this->input->post('form_id'); + + // Update is_published to 1 + $this->Form_model->update_form($form_id, ['is_published' => 1]); + + // Generate a unique link + $response_link = base_url("forms/respond/" . $form_id); + + // Send back the response link + echo json_encode(['response_link' => $response_link]); + } + + public function respond($form_id){ + $data['form'] = $this->Form_model->get_form_by_id($form_id); + $data['questions'] = $this->Form_model->get_questions_by_form_id($form_id); + foreach ($data['questions'] as &$question) { + $question->options = $this->Form_model->get_options_by_question_id($question->question_id); + } + $this->load->view('templates/header'); + $this->load->view('forms/respond_form'); + $this->load->view('templates/footer'); + } + } diff --git a/application/models/Form_model.php b/application/models/Form_model.php index 5530e22..c3ff3b1 100644 --- a/application/models/Form_model.php +++ b/application/models/Form_model.php @@ -56,4 +56,57 @@ class Form_model extends CI_Model { return $query->result(); } + public function delete_form($form_id) { + // Begin transaction + $this->db->trans_start(); + // Delete options related to the form using a join + $this->db->query("DELETE o FROM options o + JOIN questions q ON o.question_id = q.question_id + WHERE q.form_id = ?", array($form_id)); + + // Delete questions related to the form + $this->db->where('form_id', $form_id); + $this->db->delete('questions'); + + // Delete the form itself + $this->db->where('form_id', $form_id); + $this->db->delete('forms'); + + // Complete transaction + $this->db->trans_complete(); + + // Check transaction status + return $this->db->trans_status(); + } + + public function update_form($form_id, $data) { + $this->db->where('form_id', $form_id); + $this->db->update('forms', $data); + } + + // Update question details + public function update_question($question_id, $data) { + $this->db->where('question_id', $question_id); + $this->db->update('questions', $data); + } + + // Add new question + public function add_question($data) { + $this->db->insert('questions', $data); + return $this->db->insert_id(); + } + + // Update option details + public function update_option($option_id, $data) { + $this->db->where('option_id', $option_id); + $this->db->update('options', $data); + } + + // Add new option + public function add_option($data) { + $this->db->insert('options', $data); + return $this->db->insert_id(); + } + + } diff --git a/application/views/forms/create.php b/application/views/forms/create.php index e287059..f42901f 100644 --- a/application/views/forms/create.php +++ b/application/views/forms/create.php @@ -62,9 +62,9 @@
diff --git a/application/views/forms/mydrafts.php b/application/views/forms/mydrafts.php index e3b1ee6..17e7116 100644 --- a/application/views/forms/mydrafts.php +++ b/application/views/forms/mydrafts.php @@ -6,6 +6,7 @@= htmlspecialchars($form->description, ENT_QUOTES, 'UTF-8') ?>
+ +No questions found for this form.
+ += htmlspecialchars($form->description, ENT_QUOTES, 'UTF-8') ?>
+ + +