diff --git a/application/config/database.php b/application/config/database.php index de9475a..e8ab51e 100644 --- a/application/config/database.php +++ b/application/config/database.php @@ -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' => '', diff --git a/application/controllers/Forms.php b/application/controllers/Forms.php index a493d4d..1010d12 100644 --- a/application/controllers/Forms.php +++ b/application/controllers/Forms.php @@ -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'); diff --git a/application/controllers/Users.php b/application/controllers/Users.php index 62d36a4..764ac9d 100644 --- a/application/controllers/Users.php +++ b/application/controllers/Users.php @@ -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'); + } + } +} + + } \ No newline at end of file diff --git a/application/models/Form_model.php b/application/models/Form_model.php index 1fd85d9..0612777 100644 --- a/application/models/Form_model.php +++ b/application/models/Form_model.php @@ -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)); diff --git a/application/models/Response_model.php b/application/models/Response_model.php index 4c2f047..bf61719 100644 --- a/application/models/Response_model.php +++ b/application/models/Response_model.php @@ -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(); diff --git a/application/views/responses/view.php b/application/views/responses/view.php index 1d9c946..97f2bf7 100644 --- a/application/views/responses/view.php +++ b/application/views/responses/view.php @@ -1,30 +1,57 @@ -