added response preview fixed issue with response links

This commit is contained in:
jostheta 2024-07-16 11:58:22 +05:30
parent 9fa0f3abea
commit 8572bbf644
7 changed files with 64 additions and 41 deletions

View File

@ -76,8 +76,8 @@ $query_builder = TRUE;
$db['default'] = array( $db['default'] = array(
'dsn' => '', 'dsn' => '',
'hostname' => 'localhost', 'hostname' => 'localhost',
'username' => 'root', 'username' => 'jostheta',
'password' => '', 'password' => 'Pa$$w0rd',
'database' => 'gforms', 'database' => 'gforms',
'dbdriver' => 'mysqli', 'dbdriver' => 'mysqli',
'dbprefix' => '', 'dbprefix' => '',

View File

@ -145,23 +145,17 @@ class Forms extends CI_Controller
} }
public function publish_form($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 // Generate a unique link
$response_link = base_url("forms/respond/" . $form_id); $response_link = base_url("forms/respond/" . $form_id);
// Prepare data for the view // Update is_published to 1 and set the response link
$data = []; $this->Form_model->update_form($form_id, [
$data['response_link'] = $response_link; 'is_published' => 1,
$data['forms'] = $this->Form_model->get_all_forms(); 'response_link' => $response_link
]);
$this->load->view('templates/header');
$this->load->view('forms/myforms',$data);
$this->load->view('templates/footer');
// Redirect to the list_user_forms function
redirect('forms/list_user_published_forms');
} }
public function respond($form_id){ public function respond($form_id){
@ -214,14 +208,40 @@ class Forms extends CI_Controller
// View a specific response // View a specific response
public function view_response($response_id) { public function view_response($response_id) {
// Get the response details
$data['response'] = $this->Form_model->get_response($response_id); $data['response'] = $this->Form_model->get_response($response_id);
$data['form'] = $this->Form_model->get_form($data['response']->form_id); if (empty($data['response'])) {
show_404();
}
$this->load->view('templates/header'); // Get the form details using the form ID from the response
$this->load->view('forms/view_response', $data); $form_id = $data['response']->form_id;
$this->load->view('templates/footer'); $data['form'] = $this->Form_model->get_form($form_id);
// Get the questions and their options for the form
$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);
} }
// Load the views
$this->load->view('templates/header');
$this->load->view('forms/view_response', $data);
$this->load->view('templates/footer');
}
public function list_user_published_forms() {
$user_id = $this->session->userdata('user_id');
$data['forms'] = $this->Form_model->get_published_forms_by_user($user_id);
$this->load->view('templates/header');
$this->load->view('forms/user_forms', $data);
$this->load->view('templates/footer');
}
} }

View File

@ -178,5 +178,13 @@ class Form_model extends CI_Model {
return $query->row(); return $query->row();
} }
public function get_published_forms_by_user($user_id) {
$this->db->where('user_id', $user_id);
$this->db->where('is_published', 1); // Ensure only published forms are retrieved
$query = $this->db->get('forms');
return $query->result();
}
} }

View File

@ -26,3 +26,4 @@
</tbody> </tbody>
</table> </table>
</div> </div>

View File

@ -7,7 +7,6 @@
<th>Form Title</th> <th>Form Title</th>
<th>Published</th> <th>Published</th>
<th>Created At</th> <th>Created At</th>
<th>Response Links</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -17,13 +16,6 @@
<td><a href="<?= base_url() ?>forms/preview/<?=$form->form_id?> "><?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?></a></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><?= ($form->is_published == 0)?'No':'Yes'?></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>
<?php if (isset($response_link) && $form->is_published == 1): ?>
<a href="<?= $response_link ?>">Response link</a>
<?php else: ?>
Not Published
<?php endif; ?>
</td>
</tr> </tr>
<?php endforeach; ?> <?php endforeach; ?>
<?php else : ?> <?php else : ?>

View File

@ -1,23 +1,26 @@
<div class="page_layout"> <div style="margin: 0 10%;">
<h1>Your Forms</h1> <h1>Published Forms</h1>
<table> <table>
<thead> <thead>
<tr> <tr>
<th>Form Title</th> <th>Form Title</th>
<th>Created At</th> <th>Created At</th>
<th>Response Links</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?php if (!empty($forms)) : ?> <?php if (!empty($forms)) : ?>
<?php foreach ($forms as $form) : ?> <?php foreach ($forms as $form) : ?>
<tr> <tr>
<td><a href="<?= base_url('forms/list_form_responses/' . $form->form_id) ?>"><?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?></a></td> <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> <td><?= date('Y-m-d H:i:s', strtotime($form->created_at)) ?></td>
<td><a href="<?= $form->response_link ?>"><?= $form->response_link ?></a></td>
</tr> </tr>
<?php endforeach; ?> <?php endforeach; ?>
<?php else : ?> <?php else : ?>
<tr> <tr>
<td colspan="2">No forms found.</td> <td colspan="3">No forms found.</td>
</tr> </tr>
<?php endif; ?> <?php endif; ?>
</tbody> </tbody>

View File

@ -17,17 +17,16 @@
</div> </div>
<br> <br>
<?php <?php
$answer_text = ''; $answer_texts = [];
foreach ($response->answers as $answer) { foreach ($response->answers as $answer) {
if ($answer->question_id == $question->question_id) { if ($answer->question_id == $question->question_id) {
$answer_text = htmlspecialchars($answer->answer_text, ENT_QUOTES, 'UTF-8'); $answer_texts[] = htmlspecialchars($answer->answer_text, ENT_QUOTES, 'UTF-8');
break;
} }
} }
?> ?>
<?php if ($question->question_type == 'paragraph') : ?> <?php if ($question->question_type == 'paragraph') : ?>
<div class="question-box_short-answer"> <div class="question-box_short-answer">
<textarea name="responses[<?= $question->question_id ?>]" placeholder="Paragraph" readonly><?= $answer_text ?></textarea> <textarea name="responses[<?= $question->question_id ?>]" placeholder="Paragraph" readonly><?= implode("\n", $answer_texts) ?></textarea>
</div> </div>
<?php else : ?> <?php else : ?>
<div id="options-container"> <div id="options-container">
@ -35,10 +34,10 @@
<?php foreach ($question->options as $optionIndex => $option) : ?> <?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') ?>" > <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') : ?> <?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> <input type="radio" id="option-<?= $optionIndex ?>" name="responses[<?= $question->question_id ?>]" value="<?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?>" <?= (in_array(htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8'), $answer_texts)) ? 'checked' : '' ?> readonly>
<label for="option-<?= $optionIndex ?>"><?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?></label> <label for="option-<?= $optionIndex ?>"><?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?></label>
<?php elseif ($question->question_type == 'checkbox') : ?> <?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> <input type="checkbox" id="option-<?= $optionIndex ?>" name="responses[<?= $question->question_id ?>][]" value="<?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?>" <?= (in_array(htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8'), $answer_texts)) ? 'checked' : '' ?> readonly>
<label for="option-<?= $optionIndex ?>"><?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?></label> <label for="option-<?= $optionIndex ?>"><?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?></label>
<?php endif; ?> <?php endif; ?>
</div> </div>