2024-07-19 10:46:18 +00:00
|
|
|
<?php
|
2024-07-24 13:12:00 +00:00
|
|
|
class Updation_model extends CI_Model
|
|
|
|
{
|
2024-07-19 10:46:18 +00:00
|
|
|
|
2024-07-24 13:12:00 +00:00
|
|
|
public function get_form($form_id)
|
|
|
|
{
|
2024-07-19 10:46:18 +00:00
|
|
|
$this->db->where('id', $form_id);
|
|
|
|
$query = $this->db->get('forms');
|
|
|
|
return $query->row_array();
|
|
|
|
}
|
|
|
|
|
2024-07-24 13:12:00 +00:00
|
|
|
public function get_questions($form_id)
|
|
|
|
{
|
2024-07-19 10:46:18 +00:00
|
|
|
$this->db->where('form_id', $form_id);
|
|
|
|
$this->db->order_by('id', 'ASC');
|
|
|
|
$query = $this->db->get('questions');
|
|
|
|
return $query->result_array();
|
|
|
|
}
|
|
|
|
|
2024-07-24 13:12:00 +00:00
|
|
|
public function get_options()
|
|
|
|
{
|
2024-07-19 10:46:18 +00:00
|
|
|
$query = $this->db->get('options');
|
|
|
|
return $query->result_array();
|
|
|
|
}
|
2024-07-24 13:12:00 +00:00
|
|
|
public function update_form_data($form_id, $title, $description, $questions)
|
|
|
|
{
|
2024-07-22 09:49:37 +00:00
|
|
|
$this->db->trans_start();
|
2024-07-24 13:12:00 +00:00
|
|
|
|
2024-07-22 09:49:37 +00:00
|
|
|
// Update form title and description
|
2024-07-19 10:46:18 +00:00
|
|
|
$this->db->where('id', $form_id);
|
|
|
|
$this->db->update('forms', ['title' => $title, 'description' => $description]);
|
2024-07-24 13:12:00 +00:00
|
|
|
|
2024-07-22 09:49:37 +00:00
|
|
|
// Update questions
|
2024-07-19 10:46:18 +00:00
|
|
|
$this->db->where('form_id', $form_id);
|
|
|
|
$this->db->delete('questions');
|
2024-07-24 13:12:00 +00:00
|
|
|
|
2024-07-19 10:46:18 +00:00
|
|
|
foreach ($questions as $question) {
|
2024-07-22 09:49:37 +00:00
|
|
|
$question_data = [
|
2024-07-19 10:46:18 +00:00
|
|
|
'form_id' => $form_id,
|
2024-07-22 09:49:37 +00:00
|
|
|
'text' => $question['text'],
|
|
|
|
'type' => $question['type'],
|
|
|
|
'is_required' => $question['required'] // Correctly capture the required value
|
|
|
|
];
|
|
|
|
$this->db->insert('questions', $question_data);
|
2024-07-19 10:46:18 +00:00
|
|
|
$question_id = $this->db->insert_id();
|
2024-07-24 13:12:00 +00:00
|
|
|
|
2024-07-19 10:46:18 +00:00
|
|
|
if (isset($question['options'])) {
|
2024-07-22 09:49:37 +00:00
|
|
|
foreach ($question['options'] as $option_text) {
|
|
|
|
$option_data = [
|
2024-07-19 10:46:18 +00:00
|
|
|
'question_id' => $question_id,
|
2024-07-22 09:49:37 +00:00
|
|
|
'option_text' => $option_text
|
|
|
|
];
|
|
|
|
$this->db->insert('options', $option_data);
|
2024-07-19 10:46:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-24 13:12:00 +00:00
|
|
|
|
2024-07-22 09:49:37 +00:00
|
|
|
$this->db->trans_complete();
|
2024-07-24 13:12:00 +00:00
|
|
|
|
2024-07-22 09:49:37 +00:00
|
|
|
return $this->db->trans_status();
|
|
|
|
}
|
2024-07-24 13:12:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
private function update_question_options($question_id, $options)
|
|
|
|
{
|
2024-07-22 09:49:37 +00:00
|
|
|
// Fetch existing options for this question
|
|
|
|
$existing_options = $this->db->where('question_id', $question_id)->get('options')->result_array();
|
|
|
|
$existing_option_texts = array_column($existing_options, 'option_text');
|
2024-07-24 13:12:00 +00:00
|
|
|
|
2024-07-22 09:49:37 +00:00
|
|
|
// Insert or update options
|
|
|
|
foreach ($options as $option_text) {
|
|
|
|
if (in_array($option_text, $existing_option_texts)) {
|
|
|
|
// Option already exists, no need to insert
|
|
|
|
continue;
|
|
|
|
}
|
2024-07-24 13:12:00 +00:00
|
|
|
|
2024-07-22 09:49:37 +00:00
|
|
|
// Insert new option
|
|
|
|
$option_data = [
|
|
|
|
'question_id' => $question_id,
|
|
|
|
'option_text' => $option_text
|
|
|
|
];
|
|
|
|
$this->db->insert('options', $option_data);
|
|
|
|
}
|
2024-07-24 13:12:00 +00:00
|
|
|
|
2024-07-22 09:49:37 +00:00
|
|
|
// Delete options that are no longer present
|
|
|
|
$options_to_delete = array_diff($existing_option_texts, $options);
|
|
|
|
if (!empty($options_to_delete)) {
|
|
|
|
$this->db->where('question_id', $question_id);
|
|
|
|
$this->db->where_in('option_text', $options_to_delete);
|
|
|
|
$this->db->delete('options');
|
|
|
|
}
|
2024-07-19 10:46:18 +00:00
|
|
|
}
|
2024-07-24 13:12:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-19 10:46:18 +00:00
|
|
|
}
|
2024-07-24 13:12:00 +00:00
|
|
|
?>
|