added redirect login

This commit is contained in:
jostheta 2024-07-17 16:05:29 +05:30
parent 8ff1e34c65
commit 0ab5a64f42
7 changed files with 114 additions and 19 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

@ -37,7 +37,7 @@ class Forms extends CI_Controller
public function my_forms() {
$this->load->model('Form_model');
$data['forms'] = $this->Form_model->get_all_forms();
$data['forms'] = $this->Form_model->get_all_user_forms();
$this->load->view('templates/header');
$this->load->view('forms/myforms', $data);
$this->load->view('templates/footer');
@ -47,7 +47,7 @@ class Forms extends CI_Controller
public function my_drafts() {
$this->load->model('Form_model');
$data['forms'] = $this->Form_model->get_all_forms();
$data['forms'] = $this->Form_model->get_all_user_forms();
$this->load->view('templates/header');
$this->load->view('forms/mydrafts', $data);
$this->load->view('templates/footer');
@ -159,16 +159,31 @@ class Forms extends CI_Controller
redirect('forms/list_user_published_forms');
}
public function respond($form_id){
public function respond($form_id) {
// Check if user is logged in
if (!$this->session->userdata('user_id')) {
// Set flash message
$this->session->set_flashdata('error', 'Please login to respond to the form.');
// Redirect to login page with form ID
redirect('users/login_redirect/' . $form_id);
}
// Load form, questions, and options data if user is logged in
$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);
}
// Load the views
$this->load->view('templates/header');
$this->load->view('forms/respond_form',$data);
$this->load->view('forms/respond_form', $data);
$this->load->view('templates/footer');
}
public function submit_response() {
$this->load->model('Form_model');

View File

@ -103,4 +103,55 @@
return false;
}
}
// Log in user with redirection
// Log in user with redirection
public function login_redirect($form_id = null) {
$data['title'] = 'Sign In';
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/header');
$this->load->view('users/login', $data);
$this->load->view('templates/footer');
} else {
// Get username
$username = $this->input->post('username');
// Get and encrypt the password
$password = md5($this->input->post('password'));
// Login user
$user_id = $this->user_model->login($username, $password);
if ($user_id) {
// Create session
$user_data = array(
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
);
$this->session->set_userdata($user_data);
// Set message
$this->session->set_flashdata('user_loggedin', 'You are now logged in');
// Redirect to the form response page if form ID is provided
if ($form_id) {
redirect('forms/respond/' . $form_id);
} else {
redirect('pages');
}
} else {
// Set message
$this->session->set_flashdata('login_failed', 'Login is invalid');
redirect('users/login');
}
}
}
}

View File

@ -36,10 +36,13 @@ class Form_model extends CI_Model {
}
}
public function get_all_forms() {
public function get_all_user_forms() {
$user_id = $this->session->userdata('user_id');
$this->db->where('user_id', $user_id);
$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));

View File

@ -2,9 +2,9 @@
class Response_model extends CI_Model {
public function get_responses($form_id) {
$this->db->select('q.question_id, q.question_text, q.question_type, ra.answer_text');
$this->db->from('Questions q');
$this->db->join('Response_Answers ra', 'q.question_id = ra.question_id');
$this->db->join('Responses r', 'ra.response_id = r.response_id');
$this->db->from('questions q');
$this->db->join('response_answers ra', 'q.question_id = ra.question_id');
$this->db->join('responses r', 'ra.response_id = r.response_id');
$this->db->where('q.form_id', $form_id);
$query = $this->db->get();
return $query->result_array();
@ -12,7 +12,7 @@ class Response_model extends CI_Model {
public function get_options($question_id) {
$this->db->select('option_id, option_text');
$this->db->from('Options');
$this->db->from('options');
$this->db->where('question_id', $question_id);
$query = $this->db->get();
return $query->result_array();

View File

@ -1,30 +1,57 @@
<!-- application/views/responses/view.php -->
<!DOCTYPE html>
<html>
<head>
<title>Form Responses</title>
<link rel= "stylesheet" href = "<?= base_url() ?>assets/css/style.css">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
align-items: flex-start; /* Change align-items to flex-start to avoid centering */
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
font-family: Roboto, sans-serif;
background-color: #f0ebf8;
overflow-y: auto; /* Ensure vertical scrolling is enabled */
}
.container {
max-width: 600px;
width: 90%;
max-width: 700px;
padding: 20px;
background: #f9f9f9;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
margin: 20px 0; /* Add margin to the top and bottom to provide space around the container */
}
.question-box {
margin-bottom: 20px;
padding: 15px;
background: #ffffff;
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.question-title {
font-size: 18px;
margin-bottom: 10px;
color: #333;
}
.chart-container {
margin-top: 30px;
margin-bottom: 20px;
width:300px;
margin-left:200px;
}
.paragraph-answer {
margin-bottom: 10px;
padding: 10px;
background: #f5f5f5;
border-radius: 4px;
}
.paragraph-answer p {
margin: 0;
}
</style>
</head>
@ -32,8 +59,8 @@
<div class="container">
<h1 style="text-align: center;">Form Responses</h1>
<?php foreach ($data as $question_id => $question): ?>
<div>
<h2><?php echo $question['question_text']; ?></h2>
<div class="question-box">
<h2 class="question-title"><?php echo $question['question_text']; ?></h2>
<?php if ($question['question_type'] == 'multiple-choice'): ?>
<?php if (!empty($question['options'])): ?>
<div class="chart-container">

View File

@ -16,7 +16,6 @@
<div id = "navbar">
<ul class = "nav navbar-nav">
<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(); ?>my_forms">My Forms</a></li>
<li><a href="<?= base_url(); ?>my_drafts">My Drafts</a></li>