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( $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

@ -37,7 +37,7 @@ class Forms extends CI_Controller
public function my_forms() { public function my_forms() {
$this->load->model('Form_model'); $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('templates/header');
$this->load->view('forms/myforms', $data); $this->load->view('forms/myforms', $data);
$this->load->view('templates/footer'); $this->load->view('templates/footer');
@ -47,7 +47,7 @@ class Forms extends CI_Controller
public function my_drafts() { public function my_drafts() {
$this->load->model('Form_model'); $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('templates/header');
$this->load->view('forms/mydrafts', $data); $this->load->view('forms/mydrafts', $data);
$this->load->view('templates/footer'); $this->load->view('templates/footer');
@ -160,16 +160,31 @@ class Forms extends CI_Controller
} }
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['form'] = $this->Form_model->get_form_by_id($form_id);
$data['questions'] = $this->Form_model->get_questions_by_form_id($form_id); $data['questions'] = $this->Form_model->get_questions_by_form_id($form_id);
foreach ($data['questions'] as &$question) { foreach ($data['questions'] as &$question) {
$question->options = $this->Form_model->get_options_by_question_id($question->question_id); $question->options = $this->Form_model->get_options_by_question_id($question->question_id);
} }
// Load the views
$this->load->view('templates/header'); $this->load->view('templates/header');
$this->load->view('forms/respond_form', $data); $this->load->view('forms/respond_form', $data);
$this->load->view('templates/footer'); $this->load->view('templates/footer');
} }
public function submit_response() { public function submit_response() {
$this->load->model('Form_model'); $this->load->model('Form_model');

View File

@ -103,4 +103,55 @@
return false; 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,11 +36,14 @@ 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'); $query = $this->db->get('forms');
return $query->result(); return $query->result();
} }
public function get_form_by_id($form_id) { public function get_form_by_id($form_id) {
$query = $this->db->get_where('forms', array('form_id' => $form_id)); $query = $this->db->get_where('forms', array('form_id' => $form_id));
return $query->row(); return $query->row();

View File

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

View File

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

View File

@ -16,7 +16,6 @@
<div id = "navbar"> <div id = "navbar">
<ul class = "nav navbar-nav"> <ul class = "nav navbar-nav">
<li><a href = "<?= base_url(); ?>home">Home</a></li> <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')) : ?> <?php if($this->session->userdata('logged_in')) : ?>
<li><a href="<?= base_url(); ?>my_forms">My Forms</a></li> <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(); ?>my_drafts">My Drafts</a></li>