added a myforms and mydrafts page

This commit is contained in:
josephkurian 2024-07-15 02:34:04 +05:30
parent c920987b44
commit 8d23e2a233
12 changed files with 223 additions and 18 deletions

View File

@ -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' => '',

View File

@ -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

View File

@ -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);
}
}

View File

@ -1,5 +1,8 @@
<?php
class Form_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function save_form_data($formData) {
@ -32,4 +35,25 @@ 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();
}
}

View File

@ -0,0 +1,26 @@
<div style="margin: 0 10%;">
<h1>My Drafts</h1>
<table>
<thead>
<tr>
<th>Draft Title</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
<?php if (!empty($forms)) : ?>
<?php foreach ($forms as $form) : ?>
<tr>
<td><a href="<?= base_url() ?>forms/view_form/<?=$form->form_id?> "><?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?></a></td>
<td><?php echo date('Y-m-d H:i:s', strtotime($form->created_at)); ?></td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<tr>
<td colspan="2">No Drafts found.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>

View File

@ -0,0 +1,26 @@
<div style="margin: 0 10%;">
<h1>My Forms</h1>
<table>
<thead>
<tr>
<th>Form Title</th>
<th>Created At</th>
</tr>
</thead>
<tbody>
<?php if (!empty($forms)) : ?>
<?php foreach ($forms as $form) : ?>
<tr>
<td><?php echo htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8'); ?></td>
<td><?php echo date('Y-m-d H:i:s', strtotime($form->created_at)); ?></td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<tr>
<td colspan="2">No forms found.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>

View File

@ -0,0 +1,70 @@
<div class="page_layout">
<br>
<div class="section">
<div class="form-container">
<input type="text" id="form-title" value="<?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?>" class="form_container_top_title" style="color: black;" placeholder="Untitled Form">
<input type="text" id="form-desc" value="<?= htmlspecialchars($form->description, ENT_QUOTES, 'UTF-8') ?>" class="form_container_top_desc" style="color: black;" placeholder="Form Description">
<div id="questions-container">
<?php if (!empty($questions)) : ?>
<?php foreach ($questions as $index => $question) : ?>
<div class="question-box" data-question-type="<?= htmlspecialchars($question->question_type, ENT_QUOTES, 'UTF-8') ?>" id="question-template">
<div class="question-box_header">
<input type="text" value="<?= htmlspecialchars($question->question_text, ENT_QUOTES, 'UTF-8') ?>" class="question-box_header_question" style="color: black;" placeholder="Question <?= $index + 1 ?>">
<img src="<?= base_url() ?>assets/images/image.png" alt="add an image" height="20px" width="20px">
<div class="question-box_header_question-type">
<select class="question-box_header_question-type_select">
<option value="multiple-choice" <?= $question->question_type == 'multiple-choice' ? 'selected' : '' ?>>Multiple choice</option>
<option value="checkbox" <?= $question->question_type == 'checkbox' ? 'selected' : '' ?>>Checkbox</option>
<option value="paragraph" <?= $question->question_type == 'paragraph' ? 'selected' : '' ?>>Paragraph</option>
</select>
</div>
</div>
<div class="question-box_header-style">
&nbsp;
<button><img src="<?= base_url() ?>assets/images/bold.png" width="14px" height="14px"></button>
<button><img src="<?= base_url() ?>assets/images/italics.png" width="14px" height="14px"></button>
<button><img src="<?= base_url() ?>assets/images/underline.png" width="16px" height="16px"></button>
</div>
<br>
<div class="question-box_short-answer" style="display: <?= $question->question_type == 'paragraph' ? 'block' : 'none' ?>;">
<div class="question-box_short-answer_placeholder">Paragraph</div>
</div>
<div id="options-container" style="display: <?= $question->question_type == 'paragraph' ? 'none' : 'block' ?>;">
<?php if (!empty($question->options)) : ?>
<?php foreach ($question->options as $optionIndex => $option) : ?>
<div class="question-box_option-block" id="option-template">
<img id="question-type-image"src="<?= base_url() ?>assets/images/<?= $question->question_type == 'multiple-choice' ? 'circle' : 'square' ?>.png" alt="option <?= $question->question_type ?>" width="16px" height="16px">
<input type="text" value="<?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?>" class="question-box_option-block_option-text" placeholder="Option <?= $optionIndex + 1 ?>">
<?php if ($optionIndex > 0) : ?>
<button class="question-box_option-block_option-close"><img src="<?= base_url() ?>assets/images/close.png" alt="close option"></button>
<?php endif; ?>
</div>
<br>
<?php endforeach; ?>
<?php endif; ?>
<div id="new-options"></div>
<div class="question-box_add-option">
<button id="add-option" style="color:#1a73e8;font-weight: 500;">Add Option</button>
</div>
</div>
<div class="question-box_footer">
<button class="duplicate-question"><img src="<?= base_url() ?>assets/images/duplicate.png" width="24px" height="24px"></button>
<button class="delete-question"><img src="<?= base_url() ?>assets/images/trash.png" alt="delete question"></button>
</div>
</div>
<br>
<?php endforeach; ?>
<?php else : ?>
<p>No questions found for this form.</p>
<?php endif; ?>
</div>
<div class="sidebar">
<button id="add-question">
<img src="<?= base_url() ?>assets/images/add.png" width="20px" height="20px" alt="add button">
</button>
<button id="submit-form" style="color: #fff; background-color: #1a73e8; font-weight: 500; padding: 10px; border: none;">Submit</button>
</div>
</div>
</div>

View File

@ -1,2 +1,4 @@
<div style="margin: 0 10%;">
<h2><?= $title ?></h2>
<p>This is Gforms version 1.0</p>
</div>

View File

@ -1,2 +1,4 @@
<div style="margin: 0 10%;">
<h2><?= $title ?></h2>
<p>Welcome to the Gforms Application</p>
</div>

View File

@ -18,8 +18,8 @@
<li><a href = "<?= base_url(); ?>home">Home</a></li>
<li><a href = "<?= base_url(); ?>about">About</a></li>
<?php if($this->session->userdata('logged_in')) : ?>
<li><a href="<?= base_url(); ?>posts">My Forms</a></li>
<li><a href="<?= base_url(); ?>categories">My Drafts</a></li>
<li><a href="<?= base_url(); ?>my_forms">My Forms</a></li>
<li><a href="<?= base_url(); ?>my_drafts">My Drafts</a></li>
<?php endif; ?>
</ul>
<ul class = "nav navbar-nav navbar-right">
@ -28,7 +28,7 @@
<li><a href="<?php echo base_url(); ?>users/register">Register</a></li>
<?php endif; ?>
<?php if($this->session->userdata('logged_in')) : ?>
<li><a href="<?php echo base_url(); ?>forms/create">Create Form</a></li>
<li><a href="<?php echo base_url(); ?>create">Create Form</a></li>
<li><a href="<?php echo base_url(); ?>users/logout">Logout</a></li>
<?php endif; ?>
</ul>

View File

@ -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;
}

View File

@ -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);