added delete edit and response form basic functionality

This commit is contained in:
jostheta 2024-07-15 17:52:02 +05:30
parent 0eac111198
commit 96d4a55ce3
13 changed files with 427 additions and 11 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -62,9 +62,9 @@
<div class="sidebar">
<button id="add-question">
<img src="<?= base_url() ?>assets/images/add.png" width="20px" height="20px" alt="add button">
<button id="submit-form" style="color: #fff; background-color: #1a73e8; font-weight: 500; padding: 10px; border: none;">Submit</button>
<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

@ -6,6 +6,7 @@
<tr>
<th>Draft Title</th>
<th>Created At</th>
<th> </th>
</tr>
</thead>
<tbody>
@ -13,7 +14,8 @@
<?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>
<td><?php echo date('Y-m-d H:i:s', strtotime($form->created_at)); ?></td>
<td><a href="<?= base_url()?>forms/delete/<?=$form->form_id?>" onclick="return confirm('Are you sure you want to delete this form?');">Delete</a></td>
</tr>
<?php endforeach; ?>
<?php else : ?>

View File

@ -5,14 +5,17 @@
<thead>
<tr>
<th>Form Title</th>
<th>Published</th>
<th>Created At</th>
<th>Response Links</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><a href="<?= base_url() ?>forms/preview/<?=$form->form_id?> "><?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?></a></td>
<td><?= ($form->is_published == 0)?'No':'Yes'?></td>
<td><?php echo date('Y-m-d H:i:s', strtotime($form->created_at)); ?></td>
</tr>
<?php endforeach; ?>

View File

@ -0,0 +1,48 @@
<div class="page_layout">
<br>
<div class="section">
<div class="form-container">
<h1><?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?></h1>
<p><?= htmlspecialchars($form->description, ENT_QUOTES, 'UTF-8') ?></p>
<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" data-question_id="<?=htmlspecialchars($question->question_id, ENT_QUOTES, 'UTF-8')?>">
<div class="question-box_header">
<h3><?= htmlspecialchars($question->question_text, ENT_QUOTES, 'UTF-8') ?></h3>
</div>
<br>
<?php if ($question->question_type == 'paragraph') : ?>
<div class="question-box_short-answer">
<textarea placeholder="Paragraph"></textarea>
</div>
<?php else : ?>
<div id="options-container">
<?php if (!empty($question->options)) : ?>
<?php foreach ($question->options as $optionIndex => $option) : ?>
<div class="question-box_option-block" id="option-template" data-option_id="<?=htmlspecialchars($option->option_id, ENT_QUOTES, 'UTF-8') ?>" >
<?php if ($question->question_type == 'multiple-choice') : ?>
<input type="radio" id="option-<?= $optionIndex ?>" name="question-<?= $index ?>">
<label for="option-<?= $optionIndex ?>"><?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?></label>
<?php elseif ($question->question_type == 'checkbox') : ?>
<input type="checkbox" id="option-<?= $optionIndex ?>" name="question-<?= $index ?>[]">
<label for="option-<?= $optionIndex ?>"><?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?></label>
<?php endif; ?>
</div>
<br>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<br>
<?php endforeach; ?>
<?php else : ?>
<p>No questions found for this form.</p>
<?php endif; ?>
</div>
<button id="publish-form" data-form_id="<?=$form->form_id;?>" style="color: #fff; background-color: #1a73e8; font-weight: 500; padding: 10px; border: none;">Publish</button>
</div>
</div>
</div>

View File

@ -0,0 +1,51 @@
<div class="page_layout">
<br>
<div class="section">
<div class="form-container">
<h1><?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?></h1>
<p><?= htmlspecialchars($form->description, ENT_QUOTES, 'UTF-8') ?></p>
<form id="response-form" action="<?= base_url('forms/submit_response') ?>" method="post">
<input type="hidden" name="form_id" value="<?= $form->form_id ?>">
<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') ?>">
<div class="question-box_header">
<h3><?= htmlspecialchars($question->question_text, ENT_QUOTES, 'UTF-8') ?></h3>
</div>
<br>
<?php if ($question->question_type == 'paragraph') : ?>
<div class="question-box_short-answer">
<textarea name="responses[<?= $question->question_id ?>]" placeholder="Paragraph"></textarea>
</div>
<?php else : ?>
<div id="options-container">
<?php if (!empty($question->options)) : ?>
<?php foreach ($question->options as $optionIndex => $option) : ?>
<div class="question-box_option-block" id="option-template" data-option_id="<?= htmlspecialchars($option->option_id, ENT_QUOTES, 'UTF-8') ?>" >
<?php if ($question->question_type == 'multiple-choice') : ?>
<input type="radio" id="option-<?= $optionIndex ?>" name="responses[<?= $question->question_id ?>]" value="<?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?>">
<label for="option-<?= $optionIndex ?>"><?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?></label>
<?php elseif ($question->question_type == 'checkbox') : ?>
<input type="checkbox" id="option-<?= $optionIndex ?>" name="responses[<?= $question->question_id ?>][]" value="<?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?>">
<label for="option-<?= $optionIndex ?>"><?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?></label>
<?php endif; ?>
</div>
<br>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<br>
<?php endforeach; ?>
<?php else : ?>
<p>No questions found for this form.</p>
<?php endif; ?>
</div>
<button type="submit" style="color: #fff; background-color: #1a73e8; font-weight: 500; padding: 10px; border: none;">Submit</button>
</form>
</div>
</div>
</div>

View File

@ -0,0 +1,60 @@
<div class="page_layout">
<br>
<div class="section">
<div class="form_container">
<div class="form_container_top">
<input type="text" id="form-title" class="form_container_top_title" style="color: black;" placeholder="Untitled Form" value="<?= $formData['form']['title']; ?>">
<input type="text" id="form-desc" class="form_container_top_desc" style="color: black;" placeholder="Form Description" value="<?= $formData['form']['description']; ?>">
</div>
<br>
<?php foreach ($formData['questions'] as $question): ?>
<div class="question-box">
<div class="question-box_header">
<input type="text" class="question-box_header_question" style="color: black;" placeholder="Question" value="<?= $question['question_text']; ?>">
<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="<?= $question['question_type'] == 'paragraph' ? '' : 'display: none;'; ?>">
<div class="question-box_short-answer_placeholder">Paragraph</div>
</div>
<div id="options-container" style="<?= $question['question_type'] != 'paragraph' ? '' : 'display: none;'; ?>">
<?php foreach ($question['options'] as $option): ?>
<div class="question-box_option-block">
<img src="<?= base_url() ?>assets/images/<?= $question['question_type'] == 'multiple-choice' ? 'circle' : 'square'; ?>.png" alt="option" width="16px" height="16px">
<input type="text" class="question-box_option-block_option-text" value="<?= $option['option_text']; ?>">
<button class="question-box_option-block_option-close"><img src="<?= base_url() ?>assets/images/close.png" alt="close option"></button>
</div>
<br>
<?php endforeach; ?>
<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; ?>
</div>
</div>
</div>

View File

@ -8,12 +8,12 @@
<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" data-question-type="<?= htmlspecialchars($question->question_type, ENT_QUOTES, 'UTF-8') ?>" id="question-template" data-question_id="<?=htmlspecialchars($question->question_id, ENT_QUOTES, 'UTF-8')?>">
<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">
<select id="question-type" 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>
@ -33,7 +33,7 @@
<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">
<div class="question-box_option-block" id="option-template" data-option_id="<?=htmlspecialchars($option->option_id, ENT_QUOTES, 'UTF-8') ?>" >
<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) : ?>
@ -64,7 +64,7 @@
<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>
<button id="update-form" data-form_id="<?=$form->form_id;?>" style="color: #fff; background-color: #1a73e8; font-weight: 500; padding: 10px; border: none;">Update</button>
</div>
</div>
</div>

View File

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

View File

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