2024-07-15 12:45:25 +00:00
|
|
|
<?php
|
|
|
|
class New_form_model extends CI_Model {
|
|
|
|
|
2024-07-19 10:46:18 +00:00
|
|
|
public function save_form_data($formId, $formData) {
|
2024-07-22 09:49:37 +00:00
|
|
|
if (!$formId || !isset($formData['questions'])) {
|
|
|
|
return false; // Handle error if formId is not valid or questions are missing
|
2024-07-15 12:45:25 +00:00
|
|
|
}
|
2024-07-22 09:49:37 +00:00
|
|
|
|
2024-07-16 12:29:59 +00:00
|
|
|
$questions_array = $formData['questions'];
|
2024-07-22 09:49:37 +00:00
|
|
|
|
|
|
|
foreach ($questions_array as $question) {
|
2024-07-15 12:45:25 +00:00
|
|
|
$questionData = [
|
2024-07-19 10:46:18 +00:00
|
|
|
'form_id' => $formId,
|
|
|
|
'text' => $question['text'],
|
|
|
|
'type' => $question['type'],
|
2024-07-22 09:49:37 +00:00
|
|
|
'is_required' => isset($question['is_required']) && $question['is_required'] == 'true' ? 1 : 0
|
2024-07-15 12:45:25 +00:00
|
|
|
];
|
2024-07-22 09:49:37 +00:00
|
|
|
|
2024-07-15 12:45:25 +00:00
|
|
|
$this->db->insert('questions', $questionData);
|
|
|
|
$questionId = $this->db->insert_id(); // Get the inserted question_id
|
2024-07-22 09:49:37 +00:00
|
|
|
|
2024-07-19 10:46:18 +00:00
|
|
|
// Handle options for multiple-choice, checkboxes, and dropdown questions
|
2024-07-22 09:49:37 +00:00
|
|
|
if (in_array($question['type'], ['multiple-choice', 'checkboxes', 'dropdown'])) {
|
2024-07-19 10:46:18 +00:00
|
|
|
foreach ($question['options'] as $option) {
|
2024-07-22 09:49:37 +00:00
|
|
|
if (!empty($option)) { // Avoid inserting empty options
|
|
|
|
$optionData = [
|
|
|
|
'question_id' => $questionId,
|
2024-07-24 13:12:00 +00:00
|
|
|
'option_text' => $option
|
2024-07-22 09:49:37 +00:00
|
|
|
];
|
|
|
|
// Insert option into options table
|
|
|
|
$this->db->insert('options', $optionData);
|
|
|
|
}
|
2024-07-19 10:46:18 +00:00
|
|
|
}
|
2024-07-15 12:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-22 09:49:37 +00:00
|
|
|
|
2024-07-15 12:45:25 +00:00
|
|
|
return true; // Return true indicating success
|
|
|
|
}
|
2024-07-22 09:49:37 +00:00
|
|
|
|
|
|
|
|
2024-07-15 12:45:25 +00:00
|
|
|
}
|
|
|
|
?>
|