From 96d4a55ce3fc5644b082c474bf86c7c68fd591a1 Mon Sep 17 00:00:00 2001 From: jostheta Date: Mon, 15 Jul 2024 17:52:02 +0530 Subject: [PATCH] added delete edit and response form basic functionality --- application/config/database.php | 4 +- application/config/routes.php | 3 + application/controllers/Forms.php | 104 +++++++++++++++++++++++ application/models/Form_model.php | 53 ++++++++++++ application/views/forms/create.php | 4 +- application/views/forms/mydrafts.php | 4 +- application/views/forms/myforms.php | 5 +- application/views/forms/preview.php | 48 +++++++++++ application/views/forms/respond_form.php | 51 +++++++++++ application/views/forms/view.php | 60 +++++++++++++ application/views/forms/view_form.php | 8 +- assets/css/style.css | 11 ++- assets/js/script.js | 83 ++++++++++++++++++ 13 files changed, 427 insertions(+), 11 deletions(-) create mode 100644 application/views/forms/preview.php create mode 100644 application/views/forms/respond_form.php create mode 100644 application/views/forms/view.php 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 @@ Draft Title Created At + @@ -13,7 +14,8 @@ title, ENT_QUOTES, 'UTF-8') ?> - created_at)); ?> + created_at)); ?> + Delete diff --git a/application/views/forms/myforms.php b/application/views/forms/myforms.php index 85e10f3..7714ca1 100644 --- a/application/views/forms/myforms.php +++ b/application/views/forms/myforms.php @@ -5,14 +5,17 @@ Form Title + Published Created At + Response Links - title, ENT_QUOTES, 'UTF-8'); ?> + title, ENT_QUOTES, 'UTF-8') ?> + is_published == 0)?'No':'Yes'?> created_at)); ?> diff --git a/application/views/forms/preview.php b/application/views/forms/preview.php new file mode 100644 index 0000000..ce85605 --- /dev/null +++ b/application/views/forms/preview.php @@ -0,0 +1,48 @@ +
+
+
+
+

title, ENT_QUOTES, 'UTF-8') ?>

+

description, ENT_QUOTES, 'UTF-8') ?>

+ +
+ + $question) : ?> +
+
+

question_text, ENT_QUOTES, 'UTF-8') ?>

+
+
+ question_type == 'paragraph') : ?> +
+ +
+ +
+ options)) : ?> + options as $optionIndex => $option) : ?> +
+ question_type == 'multiple-choice') : ?> + + + question_type == 'checkbox') : ?> + + + +
+
+ + +
+ +
+
+ + +

No questions found for this form.

+ +
+ +
+
+
diff --git a/application/views/forms/respond_form.php b/application/views/forms/respond_form.php new file mode 100644 index 0000000..cd90122 --- /dev/null +++ b/application/views/forms/respond_form.php @@ -0,0 +1,51 @@ +
+
+
+
+

title, ENT_QUOTES, 'UTF-8') ?>

+

description, ENT_QUOTES, 'UTF-8') ?>

+ +
+ +
+ + $question) : ?> +
+
+

question_text, ENT_QUOTES, 'UTF-8') ?>

+
+
+ question_type == 'paragraph') : ?> +
+ +
+ +
+ options)) : ?> + options as $optionIndex => $option) : ?> +
+ question_type == 'multiple-choice') : ?> + + + question_type == 'checkbox') : ?> + + + +
+
+ + +
+ +
+
+ + +

No questions found for this form.

+ +
+ +
+
+
+
diff --git a/application/views/forms/view.php b/application/views/forms/view.php new file mode 100644 index 0000000..23e3b6c --- /dev/null +++ b/application/views/forms/view.php @@ -0,0 +1,60 @@ +
+
+
+
+
+ + +
+
+ + +
+
+ + add an image +
+ +
+
+
+   + + + +
+
+ +
+
Paragraph
+
+ +
+ +
+ option + + +
+
+ +
+
+ +
+
+ + +
+
+ +
+
+
diff --git a/application/views/forms/view_form.php b/application/views/forms/view_form.php index 99bc049..0f3f452 100644 --- a/application/views/forms/view_form.php +++ b/application/views/forms/view_form.php @@ -8,12 +8,12 @@
$question) : ?> -
+
add an image
- @@ -33,7 +33,7 @@
options)) : ?> options as $optionIndex => $option) : ?> -
+
option <?= $question->question_type ?> 0) : ?> @@ -64,7 +64,7 @@ - +
\ No newline at end of file diff --git a/assets/css/style.css b/assets/css/style.css index 239b3ac..9ab5b9f 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -1,12 +1,20 @@ /* all the styling below*/ + @font-face { + font-family: 'Roboto'; + src: url('../fonts/Roboto/Roboto-Regular.ttf') format('truetype'); + /* Add more src lines for other weights and styles (e.g., Bold, Italic) */ + font-weight: normal; /* Adjust weight as needed */ + font-style: normal; /* Adjust style as needed */ +} + .page_layout{ background-color: #f0ebf8; height: 100%; padding-bottom: 30px; width: 100%; - + font-family: 'Roboto'; display: flex; flex-direction: column; height: 100vh; /* Adjust 60px according to the height of your .form_header */ @@ -71,6 +79,7 @@ border-radius: 8px; margin-top: 10px; margin-bottom: 10px; + font-family: 'Roboto'; } diff --git a/assets/js/script.js b/assets/js/script.js index 784a55e..1a44b17 100644 --- a/assets/js/script.js +++ b/assets/js/script.js @@ -136,4 +136,87 @@ $(document).ready(function() { } }); }); + + $(document).ready(function() { + $('#update-form').click(function() { + var form_id = $(this).data('form_id'); + var title = $('#form-title').val(); + var description = $('#form-desc').val(); + var questions = []; + + $('.question-box:visible').each(function() { + var question_id = $(this).data('question_id'); + var question_text = $(this).find('.question-box_header_question').val(); + var question_type = $(this).find('#question-type').val(); + var options = []; + + $(this).find('.question-box_option-block').each(function() { + var option_id = $(this).data('option_id'); + var option_text = $(this).find('input').val(); + options.push({ + option_id: option_id, + option_text: option_text + }); + }); + + questions.push({ + question_id: question_id, + question_text: question_text, + question_type: question_type, + options: options + }); + }); + + var formData = { + form_id: form_id, + title: title, + description: description, + questions: JSON.stringify(questions) + }; + + console.log(formData); + + $.ajax({ + url: base_url + 'forms/update_form', + type: 'POST', + data: formData, + dataType: 'json', + success: function(response) { + console.log('Form updated successfully:', response); + window.location.href = base_url + 'my_drafts'; + // Handle success response + }, + error: function(xhr, status, error) { + console.error('Error updating form:', error); + console.log(error); + // Handle error + } + }); + }); + }); + + $(document).ready(function() { + $('#publish-form').click(function() { + var form_id = $(this).data('form_id'); + + $.ajax({ + url: base_url + 'forms/publish_form', + type: 'POST', + data: { form_id: form_id }, + dataType: 'json', + success: function(response) { + alert('Form published successfully! Share this link: ' + response.response_link); + // Optionally, redirect to a page or show the link in the UI + // window.location.href = response.response_link; + }, + error: function(xhr, status, error) { + console.error('Error publishing form:', error); + console.log(xhr.responseText); + // Handle error + } + }); + }); + }); + + });