google_forms/application/models/Form_model.php

93 lines
2.8 KiB
PHP
Raw Normal View History

2024-07-12 11:50:41 +00:00
<?php
2024-07-26 12:41:46 +00:00
defined('BASEPATH') or exit('No direct script access allowed');
2024-07-12 11:50:41 +00:00
2024-07-26 12:41:46 +00:00
class Form_model extends CI_Model
{
2024-07-12 11:50:41 +00:00
2024-07-29 09:17:16 +00:00
// Function to get form details by ID
public function get_form_by_id($form_id)
2024-07-26 12:41:46 +00:00
{
$this->load->database();
2024-07-29 09:17:16 +00:00
$this->db->where('id', $form_id);
$query = $this->db->get('forms');
return $query->row();
}
// Get the total number of forms
public function get_total_forms($user_id) {
// Ensure user_id is passed as a parameter and used in the query
$this->db->where('user_id', $user_id);
return $this->db->count_all_results('forms'); // Use count_all_results to ensure WHERE conditions are applied
}
// Function to count published forms
public function get_published_forms($user_id) {
// Ensure user_id and is_published are passed as parameters and used in the query
$this->db->where('user_id', $user_id);
$this->db->where('is_published', 1);
return $this->db->count_all_results('forms'); // Use count_all_results to ensure WHERE conditions are applied
2024-07-26 12:41:46 +00:00
}
2024-07-29 09:17:16 +00:00
// Function to get all forms
public function get_all_forms($user_id)
2024-07-26 12:41:46 +00:00
{
2024-07-29 09:17:16 +00:00
$this->db->where('user_id', $user_id);
$this->db->order_by('created_at', 'DESC');
$query = $this->db->get('forms');
return $query->result();
2024-07-26 12:41:46 +00:00
}
2024-07-29 09:17:16 +00:00
2024-07-26 12:41:46 +00:00
public function save_form($form_data)
{
2024-07-12 11:50:41 +00:00
$this->db->trans_start();
foreach ($form_data as $section) {
$question_data = array(
'form_id' => $section['form_id'],
'text' => $section['text'],
'type' => $section['type'],
'required' => $section['required'],
'created_at' => date('Y-m-d H:i:s')
);
$this->db->insert('questions', $question_data);
$question_id = $this->db->insert_id();
foreach ($section['options'] as $option_text) {
$option_data = array(
'question_id' => $question_id,
2024-07-22 09:49:37 +00:00
'option_text' => $option_text,
2024-07-12 11:50:41 +00:00
'created_at' => date('Y-m-d H:i:s')
);
$this->db->insert('options', $option_data);
}
}
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) {
return false;
} else {
return true;
}
}
2024-07-26 12:41:46 +00:00
public function __construct()
{
2024-07-23 12:54:27 +00:00
$this->load->database();
}
2024-07-26 12:41:46 +00:00
public function get_form_title($form_id)
{
2024-07-23 12:54:27 +00:00
$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();
2024-07-26 12:41:46 +00:00
2024-07-23 12:54:27 +00:00
if ($query->num_rows() > 0) {
return $query->row()->title;
} else {
return null;
}
}
2024-07-26 12:41:46 +00:00
}