google_forms/application/models/Response_model.php

173 lines
5.5 KiB
PHP
Raw Normal View History

2024-07-17 12:21:51 +00:00
<?php
2024-07-24 13:12:00 +00:00
class Response_model extends CI_Model
{
2024-07-26 12:41:46 +00:00
public function __construct() {
$this->load->database();
}
// Get the total number of responses
2024-07-29 09:17:16 +00:00
public function get_total_responses($user_id) {
// Join the responses table with the forms table
$this->db->select('responses.id');
$this->db->from('responses');
$this->db->join('forms', 'responses.form_id = forms.id');
// Filter by user_id in the forms table
$this->db->where('forms.user_id', $user_id);
return $this->db->count_all_results();
2024-07-26 12:41:46 +00:00
}
2024-07-29 09:17:16 +00:00
2024-07-24 13:12:00 +00:00
public function insert_response($data)
{
2024-07-17 12:21:51 +00:00
$this->db->insert('responses', $data);
return $this->db->insert_id();
}
2024-07-24 13:12:00 +00:00
public function insert_response_answer($data)
{
2024-07-22 09:49:37 +00:00
$this->db->insert('response_answers', $data);
}
2024-07-24 13:12:00 +00:00
public function get_form($form_id)
{
2024-07-17 12:21:51 +00:00
$this->db->where('id', $form_id);
$query = $this->db->get('forms');
return $query->row();
}
2024-07-22 09:49:37 +00:00
2024-07-24 13:12:00 +00:00
public function get_questions($form_id)
{
2024-07-17 12:21:51 +00:00
$this->db->where('form_id', $form_id);
$query = $this->db->get('questions');
return $query->result();
}
2024-07-24 13:12:00 +00:00
public function get_options($question_id)
{
2024-07-17 12:21:51 +00:00
$this->db->where('question_id', $question_id);
$query = $this->db->get('options');
return $query->result();
}
2024-07-24 13:12:00 +00:00
public function get_responses_by_form($form_id)
{
2024-07-22 09:49:37 +00:00
$this->db->select('responses.id as response_id, responses.submitted_at, users.username');
2024-07-17 12:21:51 +00:00
$this->db->from('responses');
$this->db->join('users', 'responses.user_id = users.id');
$this->db->where('responses.form_id', $form_id);
2024-07-22 09:49:37 +00:00
$query = $this->db->get();
$responses = $query->result();
foreach ($responses as &$response) {
$response->answers = $this->get_answers_by_response_id($response->response_id);
}
return $responses;
}
2024-07-24 13:12:00 +00:00
public function get_answers_by_response_id($response_id)
{
2024-07-22 09:49:37 +00:00
$this->db->select('response_answers.question_id, response_answers.answered_text');
$this->db->from('response_answers');
$this->db->where('response_answers.response_id', $response_id);
2024-07-17 12:21:51 +00:00
$query = $this->db->get();
return $query->result();
2024-07-22 09:49:37 +00:00
}
2024-07-24 13:12:00 +00:00
public function get_responses($form_id)
{
2024-07-19 10:46:18 +00:00
$this->db->where('form_id', $form_id);
$query = $this->db->get('responses');
return $query->result();
}
// Method to get response details
2024-07-24 13:12:00 +00:00
public function get_response($response_id)
{
2024-07-22 09:49:37 +00:00
$this->db->select('responses.*, users.email');
$this->db->from('responses');
$this->db->join('users', 'responses.user_id = users.id'); // Assuming 'user_id' is the foreign key in 'responses'
$this->db->where('responses.id', $response_id);
$query = $this->db->get();
2024-07-19 10:46:18 +00:00
return $query->row();
}
2024-07-24 13:12:00 +00:00
2024-07-19 10:46:18 +00:00
// Method to get questions and answers for a response
2024-07-24 13:12:00 +00:00
public function get_questions_and_answers($response_id)
{
2024-07-22 09:49:37 +00:00
$this->db->select('questions.id AS question_id, questions.text AS question_text, response_answers.answered_text, users.email');
2024-07-19 10:46:18 +00:00
$this->db->from('questions');
2024-07-22 09:49:37 +00:00
$this->db->join('response_answers', 'questions.id = response_answers.question_id');
$this->db->join('responses', 'response_answers.response_id = responses.id');
$this->db->join('users', 'responses.user_id = users.id'); // Join to get the user's email
$this->db->where('response_answers.response_id', $response_id);
2024-07-19 10:46:18 +00:00
$query = $this->db->get();
return $query->result();
}
2024-07-22 09:49:37 +00:00
2024-07-24 13:12:00 +00:00
public function get_form_by_response($response_id)
{
2024-07-19 10:46:18 +00:00
$this->db->select('forms.title, forms.description');
$this->db->from('forms');
$this->db->join('responses', 'forms.id = responses.form_id');
2024-07-22 09:49:37 +00:00
$this->db->where('responses.id', $response_id);
2024-07-19 10:46:18 +00:00
$query = $this->db->get();
return $query->row();
2024-07-17 12:21:51 +00:00
}
2024-07-29 09:17:16 +00:00
// Function to get summary data for a form
public function get_summary_data($form_id)
{
$this->load->database();
$summary_data = array();
// Get questions for the form
$this->db->select('id,text,type');
$this->db->where('form_id', $form_id);
$questions = $this->db->get('questions')->result_array();
// Get responses count
$this->db->where('form_id', $form_id);
$responses = $this->db->get('responses')->result_array();
if (count($responses) > 0) {
$response_ids = array_column($responses, 'id');
// Get response answers
$this->db->where_in('response_id', $response_ids);
$response_answers = $this->db->get('response_answers')->result_array();
// Process response answers
foreach ($questions as $question) {
$question_id = $question['id'];
$summary_data[$question_id] = [
'text' => $question['text'],
'type' => $question['type'],
'answers' => []
];
}
foreach ($response_answers as $answer) {
$question_id = $answer['question_id'];
$answered_text = $answer['answered_text'];
if (!isset($summary_data[$question_id]['answers'][$answered_text])) {
$summary_data[$question_id]['answers'][$answered_text] = 1;
} else {
$summary_data[$question_id]['answers'][$answered_text]++;
}
}
}
return $summary_data;
}
2024-07-17 12:21:51 +00:00
}
2024-07-26 12:41:46 +00:00