CodeIgniter_Gforms/application/models/Form_model.php

60 lines
1.7 KiB
PHP
Raw Normal View History

2024-07-12 09:55:29 +00:00
<?php
2024-07-12 09:53:54 +00:00
class Form_model extends CI_Model {
2024-07-14 21:04:04 +00:00
public function __construct() {
parent::__construct();
}
2024-07-12 09:53:54 +00:00
2024-07-14 21:04:04 +00:00
public function save_form_data($formData) {
2024-07-12 09:55:29 +00:00
2024-07-14 21:04:04 +00:00
$user_id = $this->session->userdata('user_id');
2024-07-12 09:55:29 +00:00
2024-07-12 09:53:54 +00:00
// Save the form data to the database
2024-07-14 21:04:04 +00:00
$this->db->insert('forms', [
2024-07-12 09:53:54 +00:00
'title' => $formData['title'],
2024-07-12 09:55:29 +00:00
'description' => $formData['description'],
'user_id' => $user_id
2024-07-14 21:04:04 +00:00
]);
2024-07-12 09:53:54 +00:00
2024-07-14 21:04:04 +00:00
$formId = $this->db->insert_id();
2024-07-12 09:53:54 +00:00
2024-07-14 21:04:04 +00:00
foreach ($formData['questions'] as $question) {
2024-07-12 09:53:54 +00:00
$this->db->insert('questions', [
'form_id' => $formId,
'question_text' => $question['question'],
'question_type' => $question['type']
]);
$questionId = $this->db->insert_id();
if ($question['type'] !== 'paragraph') {
foreach ($question['options'] as $option) {
$this->db->insert('options', [
'question_id' => $questionId,
'option_text' => $option
]);
}
}
2024-07-14 21:04:04 +00:00
}
2024-07-12 09:53:54 +00:00
}
2024-07-14 21:04:04 +00:00
public function get_all_forms() {
$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));
return $query->row();
}
public function get_questions_by_form_id($form_id) {
$query = $this->db->get_where('questions', array('form_id' => $form_id));
return $query->result();
}
public function get_options_by_question_id($question_id) {
$query = $this->db->get_where('options', array('question_id' => $question_id));
return $query->result();
}
2024-07-12 09:53:54 +00:00
}