added responses
This commit is contained in:
parent
96d4a55ce3
commit
9fa0f3abea
|
@ -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' => '',
|
||||
|
|
|
@ -8,6 +8,7 @@ $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';
|
||||
$route['responses'] = 'Forms/list_user_forms';
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ class Forms extends CI_Controller
|
|||
public function __construct() {
|
||||
parent::__construct();
|
||||
$this->load->model('Form_model');
|
||||
$this->load->library('session');
|
||||
}
|
||||
|
||||
public function create(){
|
||||
|
@ -143,17 +144,24 @@ class Forms extends CI_Controller
|
|||
$this->load->view('templates/footer');
|
||||
}
|
||||
|
||||
public function publish_form() {
|
||||
$form_id = $this->input->post('form_id');
|
||||
public function publish_form($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]);
|
||||
|
||||
// Prepare data for the view
|
||||
$data = [];
|
||||
$data['response_link'] = $response_link;
|
||||
$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 respond($form_id){
|
||||
|
@ -163,7 +171,54 @@ class Forms extends CI_Controller
|
|||
$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('forms/respond_form',$data);
|
||||
$this->load->view('templates/footer');
|
||||
}
|
||||
|
||||
public function submit_response() {
|
||||
$this->load->model('Form_model');
|
||||
|
||||
$form_id = $this->input->post('form_id');
|
||||
$responses = $this->input->post('responses');
|
||||
|
||||
if ($this->Form_model->save_responses($form_id, $responses)) {
|
||||
$this->output
|
||||
->set_content_type('application/json')
|
||||
->set_output(json_encode(['success' => true]));
|
||||
} else {
|
||||
$this->output
|
||||
->set_content_type('application/json')
|
||||
->set_output(json_encode(['success' => false]));
|
||||
}
|
||||
}
|
||||
|
||||
// List all forms of the current logged-in user
|
||||
public function list_user_forms() {
|
||||
$user_id = $this->session->userdata('user_id');
|
||||
$data['forms'] = $this->Form_model->get_forms_by_user($user_id);
|
||||
|
||||
$this->load->view('templates/header');
|
||||
$this->load->view('forms/user_forms', $data);
|
||||
$this->load->view('templates/footer');
|
||||
}
|
||||
|
||||
// List all responses for a particular form
|
||||
public function list_form_responses($form_id) {
|
||||
$data['responses'] = $this->Form_model->get_responses_by_form($form_id);
|
||||
$data['form'] = $this->Form_model->get_form($form_id);
|
||||
|
||||
$this->load->view('templates/header');
|
||||
$this->load->view('forms/form_responses', $data);
|
||||
$this->load->view('templates/footer');
|
||||
}
|
||||
|
||||
// View a specific response
|
||||
public function view_response($response_id) {
|
||||
$data['response'] = $this->Form_model->get_response($response_id);
|
||||
$data['form'] = $this->Form_model->get_form($data['response']->form_id);
|
||||
|
||||
$this->load->view('templates/header');
|
||||
$this->load->view('forms/view_response', $data);
|
||||
$this->load->view('templates/footer');
|
||||
}
|
||||
|
||||
|
|
|
@ -107,6 +107,76 @@ class Form_model extends CI_Model {
|
|||
$this->db->insert('options', $data);
|
||||
return $this->db->insert_id();
|
||||
}
|
||||
|
||||
public function save_responses($form_id, $responses) {
|
||||
$this->db->trans_start();
|
||||
|
||||
// Insert response record
|
||||
$response_data = [
|
||||
'form_id' => $form_id,
|
||||
'user_id' => $this->session->userdata('user_id'), // Set user_id if applicable
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
$this->db->insert('responses', $response_data);
|
||||
$response_id = $this->db->insert_id();
|
||||
|
||||
// Insert each answer
|
||||
foreach ($responses as $question_id => $answer) {
|
||||
if (is_array($answer)) {
|
||||
foreach ($answer as $answer_text) {
|
||||
$answer_data = [
|
||||
'response_id' => $response_id,
|
||||
'question_id' => $question_id,
|
||||
'answer_text' => $answer_text,
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
$this->db->insert('response_answers', $answer_data);
|
||||
}
|
||||
} else {
|
||||
$answer_data = [
|
||||
'response_id' => $response_id,
|
||||
'question_id' => $question_id,
|
||||
'answer_text' => $answer,
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
$this->db->insert('response_answers', $answer_data);
|
||||
}
|
||||
}
|
||||
|
||||
$this->db->trans_complete();
|
||||
|
||||
return $this->db->trans_status();
|
||||
}
|
||||
|
||||
public function get_forms_by_user($user_id) {
|
||||
$this->db->where('user_id', $user_id);
|
||||
$query = $this->db->get('forms');
|
||||
return $query->result();
|
||||
}
|
||||
|
||||
public function get_responses_by_form($form_id) {
|
||||
$this->db->where('form_id', $form_id);
|
||||
$query = $this->db->get('responses');
|
||||
return $query->result();
|
||||
}
|
||||
|
||||
public function get_response($response_id) {
|
||||
$this->db->where('response_id', $response_id);
|
||||
$query = $this->db->get('responses');
|
||||
$response = $query->row();
|
||||
|
||||
$this->db->where('response_id', $response_id);
|
||||
$query = $this->db->get('response_answers');
|
||||
$response->answers = $query->result();
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function get_form($form_id) {
|
||||
$this->db->where('form_id', $form_id);
|
||||
$query = $this->db->get('forms');
|
||||
return $query->row();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
<div class="page_layout">
|
||||
<h1>Responses for: <?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?></h1>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Response ID</th>
|
||||
<th>Submitted At</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php if (!empty($responses)) : ?>
|
||||
<?php foreach ($responses as $response) : ?>
|
||||
<tr>
|
||||
<td><a href="<?= base_url('forms/view_response/' . $response->response_id) ?>"><?= $response->response_id ?></a></td>
|
||||
<td><?= date('Y-m-d H:i:s', strtotime($response->created_at)) ?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<?php else : ?>
|
||||
<tr>
|
||||
<td colspan="2">No responses found.</td>
|
||||
</tr>
|
||||
<?php endif; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
|
@ -17,6 +17,13 @@
|
|||
<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>
|
||||
<td>
|
||||
<?php if (isset($response_link) && $form->is_published == 1): ?>
|
||||
<a href="<?= $response_link ?>">Response link</a>
|
||||
<?php else: ?>
|
||||
Not Published
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<?php else : ?>
|
||||
|
|
|
@ -42,7 +42,7 @@
|
|||
<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>
|
||||
<a href="<?= base_url() ?>forms/publish_form/<?=$form->form_id?> ">Publish</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -44,7 +44,7 @@
|
|||
<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>
|
||||
<button class="response-submit" type="submit" style="color: #fff; background-color: #1a73e8; font-weight: 500; padding: 10px; border: none;">Submit</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
<div class="page_layout">
|
||||
<h1>Your 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><a href="<?= base_url('forms/list_form_responses/' . $form->form_id) ?>"><?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?></a></td>
|
||||
<td><?= 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>
|
|
@ -0,0 +1,59 @@
|
|||
<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>
|
||||
|
||||
<h2>Response ID: <?= $response->response_id ?></h2>
|
||||
<h3>Submitted At: <?= date('Y-m-d H:i:s', strtotime($response->created_at)) ?></h3>
|
||||
|
||||
<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
|
||||
$answer_text = '';
|
||||
foreach ($response->answers as $answer) {
|
||||
if ($answer->question_id == $question->question_id) {
|
||||
$answer_text = htmlspecialchars($answer->answer_text, ENT_QUOTES, 'UTF-8');
|
||||
break;
|
||||
}
|
||||
}
|
||||
?>
|
||||
<?php if ($question->question_type == 'paragraph') : ?>
|
||||
<div class="question-box_short-answer">
|
||||
<textarea name="responses[<?= $question->question_id ?>]" placeholder="Paragraph" readonly><?= $answer_text ?></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') ?>" <?= ($answer_text == htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8')) ? 'checked' : '' ?> readonly>
|
||||
<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') ?>" <?= (strpos($answer_text, htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8')) !== false) ? 'checked' : '' ?> readonly>
|
||||
<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>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
|
@ -20,6 +20,7 @@
|
|||
<?php if($this->session->userdata('logged_in')) : ?>
|
||||
<li><a href="<?= base_url(); ?>my_forms">My Forms</a></li>
|
||||
<li><a href="<?= base_url(); ?>my_drafts">My Drafts</a></li>
|
||||
<li><a href="<?=base_url(); ?>responses">Responses</a></li>
|
||||
<?php endif; ?>
|
||||
</ul>
|
||||
<ul class = "nav navbar-nav navbar-right">
|
||||
|
|
|
@ -196,27 +196,29 @@ $(document).ready(function() {
|
|||
});
|
||||
|
||||
$(document).ready(function() {
|
||||
$('#publish-form').click(function() {
|
||||
var form_id = $(this).data('form_id');
|
||||
$('#response-form').on('submit', function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
$.ajax({
|
||||
url: base_url + 'forms/publish_form',
|
||||
type: 'POST',
|
||||
data: { form_id: form_id },
|
||||
url: $(this).attr('action'),
|
||||
type: $(this).attr('method'),
|
||||
data: $(this).serialize(),
|
||||
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;
|
||||
success: function(data) {
|
||||
if (data.success) {
|
||||
alert('Response submitted successfully!');
|
||||
// Optionally, you can clear the form or redirect the user
|
||||
window.location.href = base_url + 'my_forms';
|
||||
} else {
|
||||
alert('An error occurred. Please try again.');
|
||||
}
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.error('Error publishing form:', error);
|
||||
console.log(xhr.responseText);
|
||||
// Handle error
|
||||
error: function() {
|
||||
alert('An error occurred. Please try again.');
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue