· 8 years ago · Mar 14, 2018, 08:00 PM
1<script>
2 var mudadiv = 0;
3 var NovaPergunta = 0;
4 var mudaradio = 0;
5 var mudatxt = 0;
6 var con = 0;
7 var conti = 0;
8 var arrPerguntas = [];
9
10 function Gravar() {
11 /*
12
13 *arrPerguntas {
14 * 0 -> 3,
15 * 1 -> 1,
16 * 2 -> 5
17 *}
18 *para cada um dos elementos de arrPerguntas
19 * indice = id pergunta
20 * para de cont = 1 ate cont <= arrPerguntas[indice]
21 * document.getElementById("textodaresposta"+indicec+"-"+cont+"").value
22 *
23 *
24 *
25 **/
26 var perguntasAbertas = [];
27
28 for (i = 0; i < conti; i++) {
29 perguntasAbertas.push(document.getElementById("perguntaA" + i + "").value);
30 }
31
32 var perguntasFechadas = [];
33
34 for (l = 0; l < NovaPergunta; l++) {
35 perguntasFechadas.push(document.getElementById("perguntaF" + l + "").value);
36
37 }
38 var respostas2 = new Array();
39 for (l = 0; l < arrPerguntas.length; l++) {
40 respostas2[l] = new Array();
41 for (i=1; i <= arrPerguntas[l]; i++){
42 respostas2[l].push(document.getElementById('textodaresposta' + l + '-' + i +'').value);
43 }
44 }
45 $.ajax({
46 type: "post",
47 url: "RecebendoDados.php",
48 dataType: "text",
49 async: true,
50
51 data: {
52 quest: document.getElementById("quest").value,
53 descr: document.getElementById("descr").value,
54 categ: document.getElementById("categ").value,
55 autor: document.getElementById("autor").value,
56 perguntasAbertas: perguntasAbertas,
57 perguntasFechadas: perguntasFechadas,
58 arrPerguntas: arrPerguntas,
59 respostas2: respostas2
60 },
61 success: function (resp) {
62 if (resp === "false") {
63 alert("ERROOO");
64 }
65 else {
66 alert("OK");
67 }
68 },
69 error: function () {
70
71
72 alert('ERRO DO SISTEMA Nº22.nENTRE EM CONTATO COM O ADMINISTRADOR DO SISTEMA.');
73 }
74 });
75 }
76</script>
77
78function __construct() {
79
80 $Conexao = new Conexao();
81 $this->con = $Conexao->connection();
82 }
83
84
85$ques = isset($_POST["quest"]) ? $_POST["nome"] : "Nome do questionario não informado";
86$descr = isset($_POST["descr"]) ? $_POST["descr"] : "Descrição não informado";
87$categ = isset($_POST["categ"]) ? $_POST["categ"] : "Categoria do questionario não informado";
88$autor = isset($_POST["autor"]) ? $_POST["autor"] : "Autor não informado";
89$perAbertas = isset($_POST["perguntasAbertas"]) ? $_POST["perguntasAbertas"] : "Nenhuma pergunta informada";
90$perFechadas = isset($_POST["perguntasFechadas"]) ? $_POST["perguntasFechadas"] : "Nenhuma pergunta informada";
91$qntPerguntas = isset($_POST["arrPerguntas"]) ? $_POST["arrPerguntas"] : "Nenhuma resposta informada";
92$respostas2 = isset($_POST["respostas2"]) ? $_POST["respostas2"] : "Nenhuma resposta informada";
93
94
95
96
97 function insertgeral($nome_questionario, $descricao, $categoria, $autor, $texto_pergunta, $texto_resposta){
98 $this->con->beginTransaction();
99 try{$stmt = "
100 INSERT INTO
101 questionario (myid, nome_questionario, descricao, categoria, data, data_update, autor)
102 VALUES (NULL, :nome_questionario, :descricao, :categoria, :data, :data2, :autor)";
103 $stmt = $this->con->prepare($stmt);
104 $stmt->bindValue(":nome_questionario", $nome_questionario);
105 $stmt->bindValue(":descricao", $descricao);
106 $stmt->bindValue(":categoria", $categoria);
107 $stmt->bindValue(":data", date('Y-m-d H:i:s'));
108 $stmt->bindValue(":data2", date('Y-m-d H:i:s'));
109 $stmt->bindValue(":autor", $autor);
110 $stmt->execute();
111 $id_Questionario = $this->con->lastInsertId();
112 //insert questionario
113foreach ($perFechadas as $perguntaF){
114 $stmt = "";
115 $stmt .= "
116 INSERT INTO perguntas (myid, id_questionario, texto_pergunta)
117 VALUES (null, :id_questionario, :texto_pergunta)";
118 $stmt = $this->con->prepare($stmt);
119 $stmt->bindValue(":id_questionario", $idQuestionario);
120 $stmt->bindValue(":texto_pergunta", $perguntaF);
121 $stmt->execute();
122
123}
124//insert perguntas
125foreach ($perAbertas as $perguntasA){
126
127$stmt = "";
128$stmt .= "
129 INSERT INTO perguntas (myid, id_questionario, texto_pergunta)
130 VALUES (null, :id_questionario, :texto_pergunta)";
131 $stmt = $this->con->prepare($stmt);
132 $stmt->bindValue(":id_questionario", $id_Questionario);
133 $stmt->bindValue(":texto_pergunta", $perguntasA);
134 $stmt->execute();
135 $id_Perguntas = $this->con->lastInsertId();
136
137//receb o id de query_tab_3 => $idPerguntA
138 for ($cont=0; $cont <= $qntPerguntas[i]; $cont++){
139 $stmt = "";
140 $stmt .= "
141 INSERT INTO
142 respostas (myid, id_perguntas, texto_resposta, correct)
143 VALUES (null, :id_perguntas, :texto_resposta, NULL)";
144 $stmt = $this->con->prepare($stmt);
145 $stmt->bindValue(":id_perguntas", $id_Perguntas);// ou ".$idPerguntA."
146 $stmt->bindValue(":texto_resposta", ".$respostas2[i][$cont].");
147 $stmt->execute();
148
149 }
150 $i++;
151
152 }
153 //ultimo execute usar o commit
154 $this->con->commit();
155 return $this->processResults($stmt);
156 }
157
158 catch (Exception $e){
159 $log = fopen('log.txt', 'a');
160 fwrite($log, "ERRO EM 'pesRastreioDAO -> instPesRast' -=- DIA ".date("d/m/Y")." -=- HORA ".date("H:m:s")."rn".$e->getMessage()."rn");
161 fclose($log);
162 $this->con->rollBack();
163 return false;
164 }
165 }
166
167BANCO DE DADOS MYSQL`DROP TABLE IF EXISTS `perguntas`;
168CREATE TABLE IF NOT EXISTS `perguntas` (
169 `myid` int(8) NOT NULL AUTO_INCREMENT,
170 `id_questionario` int(8) NOT NULL,
171 `texto_pergunta` varchar(300) NOT NULL,
172 PRIMARY KEY (`myid`),
173 KEY `fk_questionario_pergunta` (`id_questionario`)
174) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
175
176-- --------------------------------------------------------
177
178--
179-- Estrutura da tabela `questionario`
180--
181
182DROP TABLE IF EXISTS `questionario`;
183CREATE TABLE IF NOT EXISTS `questionario` (
184 `myid` int(8) NOT NULL AUTO_INCREMENT,
185 `nome_questionario` varchar(300) DEFAULT NULL,
186 `descricao` varchar(300) DEFAULT NULL,
187 `categoria` varchar(300) DEFAULT NULL,
188 `data` datetime DEFAULT NULL,
189 `data_update` datetime DEFAULT NULL,
190 `autor` varchar(300) DEFAULT NULL,
191 PRIMARY KEY (`myid`)
192) ENGINE=InnoDB AUTO_INCREMENT=92 DEFAULT CHARSET=utf8;
193
194-- --------------------------------------------------------
195
196--
197-- Estrutura da tabela `respostas`
198--
199
200DROP TABLE IF EXISTS `respostas`;
201CREATE TABLE IF NOT EXISTS `respostas` (
202 `myid` int(8) NOT NULL AUTO_INCREMENT,
203 `texto_resposta` varchar(150) DEFAULT NULL,
204 `id_perguntas` int(150) NOT NULL,
205 `correct` varchar(300) DEFAULT NULL,
206 PRIMARY KEY (`myid`),
207 KEY `fk_respostas_perguntas` (`id_perguntas`)
208) ENGINE=InnoDB DEFAULT CHARSET=utf8;
209
210--
211-- Constraints for dumped tables
212--
213
214--
215-- Limitadores para a tabela `perguntas`
216--
217ALTER TABLE `perguntas`
218 ADD CONSTRAINT `fk_questionario_pergunta` FOREIGN KEY (`id_questionario`) REFERENCES `questionario` (`myid`);
219
220--
221-- Limitadores para a tabela `respostas`
222--
223ALTER TABLE `respostas`
224 ADD CONSTRAINT `fk_respostas_perguntas` FOREIGN KEY (`id_perguntas`) REFERENCES `perguntas` (`myid`);
225COMMIT;`