From 8d23e2a233c9a6c12e1b0d9b4fd1071660746767 Mon Sep 17 00:00:00 2001 From: josephkurian Date: Mon, 15 Jul 2024 02:34:04 +0530 Subject: [PATCH] added a myforms and mydrafts page --- application/config/database.php | 4 +- application/config/routes.php | 6 ++- application/controllers/Forms.php | 37 ++++++++++++++ application/models/Form_model.php | 38 +++++++++++--- application/views/forms/mydrafts.php | 26 ++++++++++ application/views/forms/myforms.php | 26 ++++++++++ application/views/forms/view_form.php | 70 ++++++++++++++++++++++++++ application/views/pages/about.php | 6 ++- application/views/pages/home.php | 6 ++- application/views/templates/header.php | 6 +-- assets/css/style.css | 15 ++++++ assets/js/script.js | 1 + 12 files changed, 223 insertions(+), 18 deletions(-) create mode 100644 application/views/forms/mydrafts.php create mode 100644 application/views/forms/myforms.php create mode 100644 application/views/forms/view_form.php diff --git a/application/config/database.php b/application/config/database.php index e8ab51e..de9475a 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' => 'jostheta', - 'password' => 'Pa$$w0rd', + 'username' => 'root', + 'password' => '', 'database' => 'gforms', 'dbdriver' => 'mysqli', 'dbprefix' => '', diff --git a/application/config/routes.php b/application/config/routes.php index e4defca..b8f50cb 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -2,8 +2,10 @@ defined('BASEPATH') OR exit('No direct script access allowed'); //Routes of the forms controller -$route['forms/create'] = 'Forms/create'; - +$route['create'] = 'Forms/create'; +$route['my_forms'] = 'Forms/my_forms'; +$route['my_drafts'] = 'Forms/my_drafts'; +$route['my_drafts'] = 'Forms/my_drafts/$1'; //Routes of the pages controller diff --git a/application/controllers/Forms.php b/application/controllers/Forms.php index 7dedebe..cb65315 100644 --- a/application/controllers/Forms.php +++ b/application/controllers/Forms.php @@ -2,6 +2,11 @@ class Forms extends CI_Controller { + public function __construct() { + parent::__construct(); + $this->load->model('Form_model'); + } + public function create(){ //check login if(!$this->session->userdata('logged_in')){ @@ -13,6 +18,7 @@ class Forms extends CI_Controller $this->load->view('templates/footer'); } + public function submit_form() { $formData = $this->input->post('formData'); $decodedData = json_decode($formData, true); @@ -26,6 +32,37 @@ class Forms extends CI_Controller echo json_encode(['status' => 'success', 'message' => 'Form data submitted successfully']); } + + public function my_forms() { + + $this->load->model('Form_model'); + $data['forms'] = $this->Form_model->get_all_forms(); + $this->load->view('templates/header'); + $this->load->view('forms/myforms', $data); + $this->load->view('templates/footer'); + + } + + public function my_drafts() { + + $this->load->model('Form_model'); + $data['forms'] = $this->Form_model->get_all_forms(); + $this->load->view('templates/header'); + $this->load->view('forms/mydrafts', $data); + $this->load->view('templates/footer'); + + } + + public function view_form($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', $data); + $this->load->view('forms/view_form', $data); + $this->load->view('templates/footer', $data); + } } diff --git a/application/models/Form_model.php b/application/models/Form_model.php index 2ce65ac..5530e22 100644 --- a/application/models/Form_model.php +++ b/application/models/Form_model.php @@ -1,20 +1,23 @@ session->userdata('user_id'); + $user_id = $this->session->userdata('user_id'); // Save the form data to the database - $this->db->insert('forms', [ + $this->db->insert('forms', [ 'title' => $formData['title'], 'description' => $formData['description'], 'user_id' => $user_id - ]); + ]); - $formId = $this->db->insert_id(); + $formId = $this->db->insert_id(); - foreach ($formData['questions'] as $question) { + foreach ($formData['questions'] as $question) { $this->db->insert('questions', [ 'form_id' => $formId, 'question_text' => $question['question'], @@ -30,6 +33,27 @@ public function save_form_data($formData) { ]); } } + } +} + + public function get_all_forms() { + $query = $this->db->get('forms'); + return $query->result(); } -} + + public function get_form_by_id($form_id) { + $query = $this->db->get_where('forms', array('form_id' => $form_id)); + return $query->row(); + } + + public function get_questions_by_form_id($form_id) { + $query = $this->db->get_where('questions', array('form_id' => $form_id)); + return $query->result(); + } + + public function get_options_by_question_id($question_id) { + $query = $this->db->get_where('options', array('question_id' => $question_id)); + return $query->result(); + } + } diff --git a/application/views/forms/mydrafts.php b/application/views/forms/mydrafts.php new file mode 100644 index 0000000..e3b1ee6 --- /dev/null +++ b/application/views/forms/mydrafts.php @@ -0,0 +1,26 @@ +
+

My Drafts

+ + + + + + + + + + + + + + + + + + + + + + +
Draft TitleCreated At
title, ENT_QUOTES, 'UTF-8') ?>created_at)); ?>
No Drafts found.
+
\ No newline at end of file diff --git a/application/views/forms/myforms.php b/application/views/forms/myforms.php new file mode 100644 index 0000000..85e10f3 --- /dev/null +++ b/application/views/forms/myforms.php @@ -0,0 +1,26 @@ +
+

My Forms

+ + + + + + + + + + + + + + + + + + + + + + +
Form TitleCreated At
title, ENT_QUOTES, 'UTF-8'); ?>created_at)); ?>
No forms found.
+
\ No newline at end of file diff --git a/application/views/forms/view_form.php b/application/views/forms/view_form.php new file mode 100644 index 0000000..99bc049 --- /dev/null +++ b/application/views/forms/view_form.php @@ -0,0 +1,70 @@ +
+
+
+
+ + + +
+ + $question) : ?> +
+
+ + add an image +
+ +
+
+
+   + + + +
+
+
+
Paragraph
+
+
+ options)) : ?> + options as $optionIndex => $option) : ?> +
+ option <?= $question->question_type ?> + + 0) : ?> + + +
+
+ + +
+
+ +
+
+ +
+
+ + +

No questions found for this form.

+ +
+ + +
+
\ No newline at end of file diff --git a/application/views/pages/about.php b/application/views/pages/about.php index 815c842..c585018 100644 --- a/application/views/pages/about.php +++ b/application/views/pages/about.php @@ -1,2 +1,4 @@ -

-

This is Gforms version 1.0

\ No newline at end of file +
+

+

This is Gforms version 1.0

+
\ No newline at end of file diff --git a/application/views/pages/home.php b/application/views/pages/home.php index 6e1cf4b..58dbaad 100644 --- a/application/views/pages/home.php +++ b/application/views/pages/home.php @@ -1,2 +1,4 @@ -

-

Welcome to the Gforms Application

\ No newline at end of file +
+

+

Welcome to the Gforms Application

+
\ No newline at end of file diff --git a/application/views/templates/header.php b/application/views/templates/header.php index cceb4c0..148e4a7 100644 --- a/application/views/templates/header.php +++ b/application/views/templates/header.php @@ -18,8 +18,8 @@
  • Home
  • About
  • session->userdata('logged_in')) : ?> -
  • My Forms
  • -
  • My Drafts
  • +
  • My Forms
  • +
  • My Drafts
  • diff --git a/assets/css/style.css b/assets/css/style.css index 1477f9f..239b3ac 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -189,6 +189,21 @@ font-style: italic; } +table { + width: 100%; + border-collapse: collapse; +} +th, td { + padding: 8px 12px; + border: 1px solid #ddd; +} +th { + background-color: #f4f4f4; +} +tr:nth-child(even) { + background-color: #f9f9f9; +} + diff --git a/assets/js/script.js b/assets/js/script.js index 3f2ed49..784a55e 100644 --- a/assets/js/script.js +++ b/assets/js/script.js @@ -129,6 +129,7 @@ $(document).ready(function() { data: { formData: JSON.stringify(formData) }, success: function(response) { console.log('Form data submitted successfully:', response); + window.location.href = base_url + 'my_drafts'; }, error: function(xhr, status, error) { console.error('Error submitting form data:', error);