diff --git a/application/controllers/Form_controller.php b/application/controllers/Form_controller.php index 295ac8f..e5952cb 100644 --- a/application/controllers/Form_controller.php +++ b/application/controllers/Form_controller.php @@ -3,7 +3,18 @@ defined('BASEPATH') or exit('No direct script access allowed'); class Form_controller extends CI_Controller { - + + public function index() { + // Fetch the data + $this->load->model('Form_model'); + $this->load->model('Response_model'); + $data['total_forms'] = $this->Form_model->get_total_forms(); + $data['published_forms'] = $this->Form_model->get_published_forms(); + $data['total_responses'] = $this->Response_model->get_total_responses(); +// var_dump($data); + // Load the view and pass the data + $this->load->view('list_forms', $data); + } public function index_forms($form_id = null) { $this->load->model('Frontend_model'); diff --git a/application/controllers/Forms.php b/application/controllers/Forms.php index 4624c24..32223f0 100644 --- a/application/controllers/Forms.php +++ b/application/controllers/Forms.php @@ -34,33 +34,42 @@ class Forms extends CI_Controller } - public function response_preview($form_id) - { - if (!$this->session->userdata('logged_in')) { - // If not logged in, redirect to login page - redirect('users/login/'.$form_id); - } - - // Load the model that handles the form data - $this->load->model('preview_model'); - - // Fetch the form details - $form = $this->preview_model->get_form($form_id); - - // Fetch the questions for the form - $questions = $this->preview_model->get_questions($form_id); - - // Fetch the options for each question - foreach ($questions as &$question) { - $question->options = $this->preview_model->get_options($question->id); - } - - // Pass the data to the view - $data['form'] = $form; - $data['questions'] = $questions; - - $this->load->view('response_submit', $data); + public function response_preview($form_id) + { + if (!$this->session->userdata('logged_in')) { + // If not logged in, redirect to login page with form_id as a parameter + redirect('users/login/' . $form_id); } + + // Load the model that handles the form data + $this->load->model('preview_model'); + + // Fetch the form details + $form = $this->preview_model->get_form($form_id); + + // Check if the form is responsive + if ($form->is_responsive == 0) { + // Pass a message to the view + $data['message'] = "This form is not accepting responses."; + $this->load->view('response_submit', $data); + return; + } + + // Fetch the questions for the form + $questions = $this->preview_model->get_questions($form_id); + + // Fetch the options for each question + foreach ($questions as &$question) { + $question->options = $this->preview_model->get_options($question->id); + } + + // Pass the data to the view + $data['form'] = $form; + $data['questions'] = $questions; + + $this->load->view('response_submit', $data); + } + public function preview_back($form_id) { if (!$this->session->userdata('logged_in')) { // If not logged in, redirect to login page diff --git a/application/controllers/Publish_controller.php b/application/controllers/Publish_controller.php index 43ab23d..3587573 100644 --- a/application/controllers/Publish_controller.php +++ b/application/controllers/Publish_controller.php @@ -46,9 +46,36 @@ class Publish_controller extends CI_Controller { } $this->load->model('Publish_model'); // Update is_published to 0 - $this->Publish_model->update_form($form_id, ['is_published' => 0]); + $this->Publish_model->update_form($form_id, ['is_responsive' => 1]); - // Redirect to the list_user_published_forms function - redirect('published_forms'); + + // redirect('published_forms'); } + public function toggle_responsive($form_id) { + if (!$this->session->userdata('logged_in')) { + redirect('users/login'); + } + + $is_responsive = $this->input->post('is_responsive', true); + + if ($is_responsive === null) { + log_message('error', 'is_responsive is null'); + echo json_encode(['success' => false, 'message' => 'Invalid data received']); + return; + } + + log_message('info', 'is_responsive received: ' . $is_responsive); + + $this->load->model('Publish_model'); + $update_result = $this->Publish_model->update_form($form_id, ['is_responsive' => $is_responsive]); + + if ($update_result) { + echo json_encode(['success' => true]); + } else { + log_message('error', 'Failed to update is_responsive'); + echo json_encode(['success' => false, 'message' => 'Failed to update']); + } + } + + } diff --git a/application/controllers/Users.php b/application/controllers/Users.php index f1f0e51..384d52e 100644 --- a/application/controllers/Users.php +++ b/application/controllers/Users.php @@ -29,26 +29,20 @@ class Users extends CI_Controller } -/** - * function to login into google forms - * @param null - * @return mixed(data return type) - * @author torun - */ + public function login($form_id = null) { $data['title'] = 'Sign In'; $data['form_id'] = $form_id; - + $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 @@ -56,7 +50,7 @@ class Users extends CI_Controller $this->load->model('user_model'); // Login user $person_id = $this->user_model->login($username, $password); - + if ($person_id) { // Create session $user_data = array( @@ -64,24 +58,22 @@ class Users extends CI_Controller 'username' => $username, 'logged_in' => true ); - + $this->session->set_userdata($user_data); - $person_id = $this->session->userdata('user_id'); - - + // Set message $this->session->set_flashdata('user_loggedin', 'You are now logged in'); - + if ($form_id) { - redirect('forms/response_preview/'.$form_id); + redirect('forms/response_preview/' . $form_id); } else { - redirect('home'); } - + redirect('home'); + } } else { // Set message $this->session->set_flashdata('login_failed', 'Login is invalid'); - - redirect('users/login'); + + redirect('users/login/' . $form_id); } } } diff --git a/application/models/Form_model.php b/application/models/Form_model.php index b3969ac..0dea148 100644 --- a/application/models/Form_model.php +++ b/application/models/Form_model.php @@ -1,9 +1,27 @@ load->database(); + + return $this->db->count_all('forms'); + } + + // Get the number of published forms + public function get_published_forms() + { + $this->load->database(); + + $this->db->where('is_published', 1); + return $this->db->count_all_results('forms'); + } + public function save_form($form_data) + { $this->db->trans_start(); foreach ($form_data as $section) { @@ -37,20 +55,22 @@ class Form_model extends CI_Model { return true; } } - public function __construct() { + public function __construct() + { $this->load->database(); } - public function get_form_title($form_id) { + public function get_form_title($form_id) + { $this->db->select('title'); // Assuming the title column in the forms table is called 'title' $this->db->from('forms'); $this->db->where('id', $form_id); $query = $this->db->get(); - + if ($query->num_rows() > 0) { return $query->row()->title; } else { return null; } } -} \ No newline at end of file +} diff --git a/application/models/Publish_model.php b/application/models/Publish_model.php index c3384c9..20dfd7c 100644 --- a/application/models/Publish_model.php +++ b/application/models/Publish_model.php @@ -16,4 +16,4 @@ public function get_published_forms_by_user($user_id) { return $query->result(); } -} +} \ No newline at end of file diff --git a/application/models/Response_model.php b/application/models/Response_model.php index 22ff9e3..75d64d0 100644 --- a/application/models/Response_model.php +++ b/application/models/Response_model.php @@ -1,7 +1,14 @@ load->database(); + } + + // Get the total number of responses + public function get_total_responses() { + return $this->db->count_all('responses'); + } public function insert_response($data) { @@ -107,3 +114,4 @@ class Response_model extends CI_Model return $query->row(); } } + diff --git a/application/views/Tables/draft.php b/application/views/Tables/draft.php index 0a6222a..140b011 100644 --- a/application/views/Tables/draft.php +++ b/application/views/Tables/draft.php @@ -1,65 +1,96 @@ - -
-
-
-
-
- session->flashdata('status')): ?> -
- session->flashdata('status'); ?> -
- -

-Drafts -

-
-
- - - - - - - - - - - - - - - - - - - - - - + + + + + + + Form List + + + + + +
+
+
+
+
+ session->flashdata('status')): ?> +
+ session->flashdata('status'); ?> +
+ +

Drafts

+
+
+
DraftsTitleCreated OnEditDeletePreview
- title; ?> - created_at; ?> - Edit - - Delete - - - - -
+ + + + + + + + - - -
DraftsTitleCreated OnEditDeletePreview
+ + + + + + title; ?> + created_at)); ?> + + Edit + + + Delete + + + + + + + + + + +
- \ No newline at end of file + + + diff --git a/application/views/Tables/list_forms.php b/application/views/Tables/list_forms.php index 2b1da3d..140b011 100644 --- a/application/views/Tables/list_forms.php +++ b/application/views/Tables/list_forms.php @@ -7,82 +7,90 @@ Form List + - -
-
-
-
-
- session->flashdata('status')): ?> -
- session->flashdata('status'); ?> -
- -

- List of Forms -

-
-
- - - - - - - - - - - - - - - - - - - - - +
+
+
+
+
+ session->flashdata('status')): ?> +
+ session->flashdata('status'); ?> +
+ +

Drafts

+
+
+
FormsTitleDescriptionCreated OnStatusResponses
- title; ?> - description; ?>created_at; ?>is_published ? 'Published' : 'Draft'); ?> - - - -
+ + + + + + + + - - -
DraftsTitleCreated OnEditDeletePreview
+ + + + + + title; ?> + created_at)); ?> + + Edit + + + Delete + + + + + + + + + + +
- + + const dateA = new Date(dateTextA); + const dateB = new Date(dateTextB); + + if (isNaN(dateA.getTime())) { + return 1; + } + if (isNaN(dateB.getTime())) { + return -1; + } + + return dateB - dateA; + }); + + rows.forEach(row => tbody.appendChild(row)); + }); + diff --git a/application/views/edit_form_view.php b/application/views/edit_form_view.php index 49ebb3e..cca0cdb 100644 --- a/application/views/edit_form_view.php +++ b/application/views/edit_form_view.php @@ -8,6 +8,8 @@ + + @@ -20,28 +22,8 @@