暫無描述

insertQuestionnaire.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <?php
  2. require_once 'config.php';
  3. require_once 'dbh.inc.php';
  4. require_once 'checkLogin.php';
  5. include_once '../../funciones.php'; // para console_log()
  6. console_log($_POST);
  7. // http_response_code(400);
  8. // echo json_encode(array("error" => "Currently under development. \n".var_export($_POST, true)));
  9. // exit();
  10. // EXAMPLE INPUT...
  11. // array(39) { ["id_experience"]=> string(1) "1" ["q_title"]=> string(1) "t" ["description"]=> string(1) "d" ["referencia"]=> string(1) "r" ["q_categories"]=> string(4) "a, b" ["q_subcategories"]=> string(10) "c, d, e, f" ["q_premise1"]=> string(2) "p1" ["q_type1"]=> string(1) "2" ["min_val1"]=> string(0) "" ["min_text1"]=> string(0) "" ["max_val1"]=> string(0) "" ["max_text1"]=> string(0) "" ["q_category1"]=> string(1) "a" ["q_subcategory1"]=> string(1) "c" ["q_premise2"]=> string(2) "p2" ["q_type2"]=> string(1) "1" ["min_val2"]=> string(1) "1" ["min_text2"]=> string(5) "mint2" ["max_val2"]=> string(1) "5" ["max_text2"]=> string(5) "maxt2" ["q_category2"]=> string(1) "a" ["q_subcategory2"]=> string(1) "d" ["q_premise3"]=> string(2) "p3" ["q_type3"]=> string(1) "1" ["min_val3"]=> string(1) "1" ["min_text3"]=> string(5) "mint3" ["max_val3"]=> string(2) "10" ["max_text3"]=> string(5) "maxt3" ["q_category3"]=> string(1) "b" ["q_subcategory3"]=> string(1) "e" ["q_premise4"]=> string(2) "p4" ["q_type4"]=> string(1) "1" ["min_val4"]=> string(1) "5" ["min_text4"]=> string(5) "mint4" ["max_val4"]=> string(1) "7" ["max_text4"]=> string(5) "maxt4" ["q_category4"]=> string(1) "b" ["q_subcategory4"]=> string(1) "f" ["createQuestionnaire"]=> string(0) "" }
  12. if(isset($_POST['createQuestionnaire'])) {
  13. /*** FETCH THE BASIC INFO OF THE NEW QUESTIONNAIRE ***/
  14. $experienceID = mysqli_real_escape_string($connection, trim($_POST['id_experience']));
  15. $questionnaireTitle = mysqli_real_escape_string($connection, trim($_POST['q_title']));
  16. $questionnaireDescription = mysqli_real_escape_string($connection, trim($_POST['description']));
  17. $questionnaireReference = mysqli_real_escape_string($connection, trim($_POST['referencia']));
  18. $questionnaireCategories = explode(',', mysqli_real_escape_string($connection, trim($_POST['q_categories'])));
  19. $questionnaireSubcategories = explode(',', mysqli_real_escape_string($connection, trim($_POST['q_subcategories'])));
  20. /*** (START) FIRST BLOCK OF INPUT VALIDATION ***/
  21. // Check that experienceID is not an empty string
  22. // And that experienceID is in the database
  23. if($experienceID === "") {
  24. http_response_code(400);
  25. echo json_encode(array("error" => "Please specify experience ID."));
  26. exit();
  27. } else if(mysqli_query($connection, "SELECT * FROM experience WHERE id = '$experienceID';")->num_rows !== 1) {
  28. http_response_code(400);
  29. echo json_encode(array("error" => "Given experience ID ($experienceID) not in database."));
  30. exit();
  31. }
  32. // Check that questionnaireTitle is not an empty string
  33. // And that it doesn't exceed 100 characters (database limit)
  34. if($questionnaireTitle === "") {
  35. http_response_code(400);
  36. echo json_encode(array("error" => "Please specify questionnaire title."));
  37. exit();
  38. } else if(mb_strlen($questionnaireTitle) > 100) {
  39. http_response_code(400);
  40. echo json_encode(array("error" => "Questionnaire title too long (max. is 100 characters)."));
  41. exit();
  42. }
  43. // Check that questionnaireDescription is not an empty string
  44. if($questionnaireDescription === "") {
  45. http_response_code(400);
  46. echo json_encode(array("error" => "Please specify questionnaire description."));
  47. exit();
  48. } else if(mb_strlen($questionnaireDescription) > 200) {
  49. http_response_code(400);
  50. echo json_encode(array("error" => "Questionnaire description too long (max. is 200 characters)."));
  51. exit();
  52. }
  53. // Check that questionnaireReference is not an empty string
  54. if($questionnaireReference === "") {
  55. http_response_code(400);
  56. echo json_encode(array("error" => "Please specify questionnaire reference."));
  57. exit();
  58. } else if(mb_strlen($questionnaireReference) > 60) {
  59. http_response_code(400);
  60. echo json_encode(array("error" => "Questionnaire reference too long (max. is 60 characters)."));
  61. exit();
  62. }
  63. // Check that questionnaireCategories is not an empty string (explode returns FALSE if empty string)
  64. // And that user has at least one category
  65. // WARNING: " , , ,, " goes through
  66. if($questionnaireCategories === FALSE || count($questionnaireCategories) === 0) {
  67. http_response_code(400);
  68. echo json_encode(array("error" => "Please specify questionnaire categories (go back with 'Previous' button)."));
  69. exit();
  70. }
  71. // Check that questionnaireSubcategories is not an empty string (explode returns FALSE if empty string)
  72. // And that user have at least one subcategory
  73. // WARNING: " , , ,, " goes through
  74. if($questionnaireSubcategories === FALSE || count($questionnaireSubcategories) === 0) {
  75. http_response_code(400);
  76. echo json_encode(array("error" => "Please specify questionnaire subcategories (go back with 'Previous' button)."));
  77. exit();
  78. }
  79. /*** (END) FIRST BLOCK OF INPUT VALIDATION ***/
  80. /*** CLEAN UP CATEGORIES STRING ***/
  81. // Validate (make sure every category is not an empty string)
  82. // REMINDER: Here we can deal with " , , ,, "
  83. for($i = 0; $i < count($questionnaireCategories); $i++) {
  84. $questionnaireCategories[$i] = trim($questionnaireCategories[$i]);
  85. // Check that none of the categories are empty strings
  86. if($questionnaireCategories[$i] === "") {
  87. http_response_code(400);
  88. echo json_encode(array("error" => "Please specify a valid category (category #".($i+1).")."));
  89. exit();
  90. } else if(mb_strlen($questionnaireCategories[$i]) > 60) {
  91. http_response_code(400);
  92. echo json_encode(array("error" => "Category '".$questionnaireCategories[$i]."' too long (max. is 60 characters)."));
  93. exit();
  94. }
  95. }
  96. /*** CLEAN UP SUBCATEGORIES STRING ***/
  97. // Validate (make sure every subcategory is not an empty string)
  98. // REMINDER: Here we can deal with " , , ,, "
  99. for($i = 0; $i < count($questionnaireSubcategories); $i++) {
  100. $questionnaireSubcategories[$i] = trim($questionnaireSubcategories[$i]);
  101. // Check that none of the subcategories are empty strings
  102. if($questionnaireSubcategories[$i] === "") {
  103. http_response_code(400);
  104. echo json_encode(array("error" => "Please specify a valid subcategory (subcategory #".($i+1).")."));
  105. exit();
  106. } else if(mb_strlen($questionnaireSubcategories[$i]) > 60) {
  107. http_response_code(400);
  108. echo json_encode(array("error" => "Subcategory '".$questionnaireSubcategories[$i]."' too long (max. is 60 characters)."));
  109. exit();
  110. }
  111. }
  112. // Check that the number of subcategories is greater or equal than that of categories
  113. if(count($questionnaireSubcategories) < count($questionnaireCategories)) {
  114. http_response_code(400);
  115. echo json_encode(array("error" => "The number of subcategories must be greater or equal than the number of categories (go back with 'Previous' button)."));
  116. exit();
  117. }
  118. /*** INITIALIZE OTHER VARIABLES ***/
  119. $questionPremises = array();
  120. $questionTypes = array();
  121. $questionMinVal = array();
  122. $questionMinText = array();
  123. $questionMaxVal = array();
  124. $questionMaxText = array();
  125. $questionCategories = array();
  126. $questionSubcategories = array();
  127. $numberOfQuestions = 0;
  128. /*** STORE THE QUESTIONS' DATA ***/
  129. /*** FINAL BLOCK OF INPUT VALIDATION ***/
  130. // WARNING: $start IS VERY SENSITIVE TO NUMBER OF INPUTS GIVEN //
  131. // REMINDER: THE -1 IS FOR THE createQuestionnaire SUBMIT BUTTON //
  132. $start = 6;
  133. for($i = $start; $i < count($_POST) - 1 - $start; $i += 8) {
  134. $questionPremises[] = mysqli_real_escape_string($connection, trim(array_values($_POST)[$i]));
  135. $questionTypes[] = mysqli_real_escape_string($connection, trim(array_values($_POST)[$i+1]));
  136. $questionMinVal[] = mysqli_real_escape_string($connection, trim(array_values($_POST)[$i+2]));
  137. $questionMinText[] = mysqli_real_escape_string($connection, trim(array_values($_POST)[$i+3]));
  138. $questionMaxVal[] = mysqli_real_escape_string($connection, trim(array_values($_POST)[$i+4]));
  139. $questionMaxText[] = mysqli_real_escape_string($connection, trim(array_values($_POST)[$i+5]));
  140. $questionCategories[] = mysqli_real_escape_string($connection, trim(array_values($_POST)[$i+6]));
  141. $questionSubcategories[] = mysqli_real_escape_string($connection, trim(array_values($_POST)[$i+7]));
  142. $numberOfQuestions++;
  143. // Check that none of the premises are empty strings
  144. if($questionPremises[$numberOfQuestions - 1] === "") {
  145. http_response_code(400);
  146. echo json_encode(array("error" => "Please specify a valid premise (in Question #$numberOfQuestions)."));
  147. exit();
  148. } else if(mb_strlen($questionPremises[$numberOfQuestions - 1]) > 600) {
  149. http_response_code(400);
  150. echo json_encode(array("error" => "Premise too long (max. is 600 characters) (in Question #$numberOfQuestions)."));
  151. exit();
  152. }
  153. // Check that none of the assigned categories are empty strings
  154. if($questionCategories[$numberOfQuestions - 1] === "") {
  155. http_response_code(400);
  156. echo json_encode(array("error" => "Please specify a valid category (in Question #$numberOfQuestions)."));
  157. exit();
  158. } else if(mb_strlen($questionCategories[$numberOfQuestions - 1]) > 60) {
  159. http_response_code(400);
  160. echo json_encode(array("error" => "Category '".$questionCategories[$numberOfQuestions - 1]."' too long (max. is 60 characters) (in Question #$numberOfQuestions)."));
  161. exit();
  162. }
  163. // Check that none of the assigned subcategories are empty strings
  164. if($questionSubcategories[$numberOfQuestions - 1] === "") {
  165. http_response_code(400);
  166. echo json_encode(array("error" => "Please specify a valid subcategory (in Question #$numberOfQuestions)."));
  167. exit();
  168. } else if(mb_strlen($questionSubcategories[$numberOfQuestions - 1]) > 60) {
  169. http_response_code(400);
  170. echo json_encode(array("error" => "Subcategory '".$questionSubcategories[$numberOfQuestions - 1]."' too long (max. is 60 characters) (in Question #$numberOfQuestions)."));
  171. exit();
  172. }
  173. // Check that none of the types are empty strings
  174. // If the question type is scaled (e.g. value of "1"), make sure all the min/max settings are valid
  175. // If the question type is open, we don't have to check for min/max settings
  176. // If the question type is neither (invalid; nor "1" nor "2"), let user know (user probably tampered with client-side)
  177. if($questionTypes[$numberOfQuestions - 1] === "") {
  178. http_response_code(400);
  179. echo json_encode(array("error" => "Please specify a type (1 for scaled, 2 for open) (in Question #$numberOfQuestions)."));
  180. exit();
  181. } else if($questionTypes[$numberOfQuestions - 1] === "1") {
  182. // Check if minVal is equal to 1 (we decided it should be like that)
  183. if($questionMinVal[$numberOfQuestions - 1] !== "1") {
  184. http_response_code(400);
  185. echo json_encode(array("error" => "Minimum value has to be 1 (in Question #$numberOfQuestions)."));
  186. exit();
  187. }
  188. // Check if minText is not an empty string
  189. if($questionMinText[$numberOfQuestions - 1] === "") {
  190. http_response_code(400);
  191. echo json_encode(array("error" => "Please specify a valid minimum text (in Question #$numberOfQuestions)."));
  192. exit();
  193. } else if(mb_strlen($questionMinText[$numberOfQuestions - 1]) > 40) {
  194. http_response_code(400);
  195. echo json_encode(array("error" => "Minimum text '".$questionMinText[$numberOfQuestions - 1]."' too long (max. is 40 characters) (in Question #$numberOfQuestions)."));
  196. exit();
  197. }
  198. // Check if maxVal is greater or equal to 2 (we decided it should be like that)
  199. // REMINDERS:
  200. // is_numeric() ensures the string is a number
  201. // intval() returns truncates "starting numeric-like" numbers (e.g. 1234asdf is 1234)
  202. // intval() returns 0 if it detects "normal string" (e.g. asdf1234 is 0)
  203. if(!is_numeric($questionMaxVal[$numberOfQuestions - 1])) {
  204. http_response_code(400);
  205. echo json_encode(array("error" => "Maximum value has to be numeric (in Question #$numberOfQuestions)."));
  206. exit();
  207. } else if(intval($questionMaxVal[$numberOfQuestions - 1]) < 2) {
  208. http_response_code(400);
  209. echo json_encode(array("error" => "Maximum value has to be greater or equal to 2 (in Question #$numberOfQuestions)."));
  210. exit();
  211. }
  212. // Check if maxText is not an empty string
  213. if($questionMaxText[$numberOfQuestions - 1] === "") {
  214. http_response_code(400);
  215. echo json_encode(array("error" => "Please specify a valid maximum text (in Question #$numberOfQuestions)."));
  216. exit();
  217. } else if(mb_strlen($questionMaxText[$numberOfQuestions - 1]) > 40) {
  218. http_response_code(400);
  219. echo json_encode(array("error" => "Maximum text '".$questionMaxText[$numberOfQuestions - 1]."' too long (max. is 40 characters) (in Question #$numberOfQuestions)."));
  220. exit();
  221. }
  222. // Check that maxText and minText are different strings
  223. if(mb_strtolower($questionMaxText[$numberOfQuestions - 1]) === mb_strtolower($questionMinText[$numberOfQuestions - 1])) {
  224. http_response_code(400);
  225. echo json_encode(array("error" => "Labels must be different (in Question #$numberOfQuestions)."));
  226. exit();
  227. }
  228. } else if($questionTypes[$numberOfQuestions - 1] !== "2") {
  229. http_response_code(400);
  230. echo json_encode(array("error" => "Please specify a valid type (1 for scaled, 2 for open) (in Question #$numberOfQuestions)."));
  231. exit();
  232. }
  233. }
  234. // Check that all the categories are used
  235. for($i = 0; $i < count($questionnaireCategories); $i++) {
  236. if(!in_array($questionnaireCategories[$i], $questionCategories)) {
  237. http_response_code(400);
  238. echo json_encode(array("error" => "Category '" . $questionnaireCategories[$i] . "' is unused."));
  239. exit();
  240. }
  241. }
  242. // Check that all the subcategories are used
  243. for($i = 0; $i < count($questionnaireSubcategories); $i++) {
  244. if(!in_array($questionnaireSubcategories[$i], $questionSubcategories)) {
  245. http_response_code(400);
  246. echo json_encode(array("error" => "Subcategory '" . $questionnaireSubcategories[$i] . "' is unused."));
  247. exit();
  248. }
  249. }
  250. /*** (END) FINAL BLOCK OF INPUT VALIDATION ***/
  251. /*** START OF DB QUERIES ***/
  252. // exit();
  253. // while(TRUE) {
  254. // exit();
  255. // }
  256. // exit();
  257. /*** IF IT GOT THROUGH VALIDATION, IT'S TOO LATE ***/
  258. /*** CREATE QUESTIONNAIRE, RETRIEVE ITS ID (TO USE IT IN TABLES experience_questionnair AND questionnair_question) AND HOOK TO CURRENT EXPERIENCE ***/
  259. $queryQuestionnaire = "INSERT INTO questionnair (`q_title`, `description`) VALUES ('$questionnaireTitle', '$questionnaireDescription');";
  260. if(!mysqli_query($connection, $queryQuestionnaire)) {
  261. http_response_code(400);
  262. die(json_encode(array("error" => "Error: Couldn't create questionnaire<br>".mysqli_error($connection)."<br>Query: $queryQuestionnaire")));
  263. }
  264. $questionnaireID = mysqli_insert_id($connection) or die(json_encode(array("error" => "Error: Couldn't fetch questionnaire id after creating it<br>".mysqli_error($connection))));
  265. $queryHookQuestionnaireToExperience = "INSERT INTO experience_questionnair (`id_experience`, `id_questionnair`) VALUES ('$experienceID', '$questionnaireID');";
  266. if(!mysqli_query($connection, $queryHookQuestionnaireToExperience)) {
  267. http_response_code(400);
  268. die(json_encode(array("error" => "Error: Couldn't hook questionnaire to experience<br>".mysqli_error($connection)."<br>Query: $queryHookQuestionnaireToExperience")));
  269. }
  270. /*** CREATE REFERENCE AND RETRIEVE ITS ID (TO USE IT IN TABLE question) ***/
  271. $queryReference = "INSERT INTO reference (`referencia`) VALUES ('$questionnaireReference');";
  272. if(!mysqli_query($connection, $queryReference)) {
  273. http_response_code(400);
  274. die(json_encode(array("error" => "Error: Couldn't create questionnaire reference<br>".mysqli_error($connection)."<br>Query: $queryReference")));
  275. }
  276. $referenceID = mysqli_insert_id($connection) or die(json_encode(array("error" => "Error: Couldn't fetch reference id after creating it<br>".mysqli_error($connection))));
  277. /*** CREATE CATEGORIES ONE BY ONE AND RETRIEVE THEIR RESPECTIVE IDs (TO USE IN TABLES subcategory AND question) ***/
  278. for($i = 0; $i < count($questionnaireCategories); $i++) {
  279. $queryCategory = "INSERT INTO category (`categoria`) VALUES ('".$questionnaireCategories[$i]."');";
  280. if(!mysqli_query($connection, $queryCategory)) {
  281. http_response_code(400);
  282. die(json_encode(array("error" => "Error: Couldn't create category '".$questionnaireCategories[$i]."'<br>".mysqli_error($connection)."<br>Query: $queryCategory")));
  283. }
  284. $categoryIDs[$questionnaireCategories[$i]] = mysqli_insert_id($connection) or die(json_encode(array("error" => "Error: Couldn't fetch category id ('".$questionnaireCategories[$i]."') after creating it<br>".mysqli_error($connection))));
  285. }
  286. console_log($categoryIDs);
  287. /*** CREATE ASSOCIATIVE ARRAY THAT GOES FROM SUBCATEGORY NAME TO ITS CORRESPONDING CATEGORY'S ID ***/
  288. // WARNING: ESTO ES ASUMIENDO QUE LA RELACIÓN SUBCATEGORY A CATEGORY ES UNA FUNCIÓN (una subcategoría corresponde solamente a una categoría)
  289. for($i = 0; $i < count($questionnaireCategories); $i++) {
  290. for($j = 0; $j < count($questionnaireSubcategories); $j++) {
  291. for($k = 0; $k < $numberOfQuestions; $k++) {
  292. if($questionSubcategories[$k] == $questionnaireSubcategories[$j] && $questionCategories[$k] == $questionnaireCategories[$i]) {
  293. $linkSubcategoryToCategoryID[$questionnaireSubcategories[$j]] = $categoryIDs[$questionnaireCategories[$i]];
  294. }
  295. }
  296. }
  297. }
  298. console_log($linkSubcategoryToCategoryID);
  299. /*** CREATE SUBCATEGORIES ONE BY ONE AND RETRIEVE THEIR RESPECTIVE IDs (TO USE IN TABLE question) ***/
  300. // NOTE: $subcategoryIDs and $questionnaireSubcategories compartirán el mismo índice
  301. for($i = 0; $i < count($questionnaireSubcategories); $i++) {
  302. $querySubcategory = "INSERT INTO subcategory (`subcategoria`, `id_categoria`) VALUES ('".$questionnaireSubcategories[$i]."', '".$linkSubcategoryToCategoryID[$questionnaireSubcategories[$i]]."');";
  303. if(!mysqli_query($connection, $querySubcategory)) {
  304. http_response_code(400);
  305. die(json_encode(array("error" => "Error: Couldn't create subcategory '".$questionnaireSubcategories[$i]."', which is linked to category ID ".$linkSubcategoryToCategoryID[$questionnaireSubcategories[$i]]."<br>".mysqli_error($connection)."<br>Query: $querySubcategory<br>Dump: \n".var_export($linkSubcategoryToCategoryID, true))));
  306. }
  307. $subcategoryIDs[$questionnaireSubcategories[$i]] = mysqli_insert_id($connection) or die(json_encode(array("error" => "Error: Couldn't fetch subcategory id after creating it ('".$questionnaireSubcategories[$i]."', which is linked to category ID ".$linkSubcategoryToCategoryID[$questionnaireSubcategories[$i]].")<br>".mysqli_error($connection))));
  308. }
  309. // DO WE HAVE TO DO SOMETHING FOR OPEN ANSWER CASE? I SEE THERE'S NO OPEN ANSWER QUESTION THAT IS STORED IN THE TABLE question
  310. /*** CREATE QUESTIONS ONE BY ONE, RETRIEVE THEIR RESPECTIVE IDs (TO USE IN TABLES question_type AND questionnair_question), ENTER THE MIN/MAX VALUES/TEXTS, AND HOOK QUESTION TO QUESTIONNAIR ***/
  311. for($i = 0; $i < $numberOfQuestions; $i++) {
  312. $queryQuestion = "INSERT INTO question (`premise`, `id_category`, `id_subcategory`, `id_type`, `id_referencia`) VALUES ('".$questionPremises[$i]."', '".$categoryIDs[$questionCategories[$i]]."', '".$subcategoryIDs[$questionSubcategories[$i]]."', '".$questionTypes[$i]."', '$referenceID');";
  313. if(!mysqli_query($connection, $queryQuestion)) {
  314. http_response_code(400);
  315. die(json_encode(array("error" => "Error: Couldn't insert question '".$questionPremises[$i]."'<br>".mysqli_error($connection)."<br>Query: $queryQuestion")));
  316. }
  317. $questionID = mysqli_insert_id($connection) or die(json_encode(array("error" => "Error: Couldn't fetch question ID (from '".$questionPremises[$i]."')<br>".mysqli_error($connection))));
  318. // IF QUESTION IS SCALED, INSERT MIN/MAX VALUES/TEXT
  319. if($questionTypes[$i] == '1') {
  320. $queryMinVal = "INSERT INTO question_type (`id_type`, `id_question`, `label`, `value`) VALUES ('".$questionTypes[$i]."', '$questionID', 'min_val', '".$questionMinVal[$i]."');";
  321. if(!mysqli_query($connection, $queryMinVal)) {
  322. http_response_code(400);
  323. die(json_encode(array("error" => "Error: Couldn't insert question minimum value (from '".$questionPremises[$i]."')<br>".mysqli_error($connection)."<br>Query: $queryMinVal")));
  324. }
  325. $queryMinText = "INSERT INTO question_type (`id_type`, `id_question`, `label`, `value`) VALUES ('".$questionTypes[$i]."', '$questionID', 'min_texto', '".$questionMinText[$i]."');";
  326. if(!mysqli_query($connection, $queryMinText)) {
  327. http_response_code(400);
  328. die(json_encode(array("error" => "Error: Couldn't insert question minimum text (from '".$questionPremises[$i]."')<br>".mysqli_error($connection)."<br>Query: $queryMinText")));
  329. }
  330. $queryMaxVal = "INSERT INTO question_type (`id_type`, `id_question`, `label`, `value`) VALUES ('".$questionTypes[$i]."', '$questionID', 'max_val', '".$questionMaxVal[$i]."');";
  331. if(!mysqli_query($connection, $queryMaxVal)) {
  332. http_response_code(400);
  333. die(json_encode(array("error" => "Error: Couldn't insert question maximum value (from '".$questionPremises[$i]."')<br>".mysqli_error($connection)."<br>Query: $queryMaxVal")));
  334. }
  335. $queryMaxText = "INSERT INTO question_type (`id_type`, `id_question`, `label`, `value`) VALUES ('".$questionTypes[$i]."', '$questionID', 'max_texto', '".$questionMaxText[$i]."');";
  336. if(!mysqli_query($connection, $queryMaxText)) {
  337. http_response_code(400);
  338. die(json_encode(array("error" => "Error: Couldn't insert question maximum text (from '".$questionPremises[$i]."')<br>".mysqli_error($connection)."<br>Query: $queryMaxText")));
  339. }
  340. }
  341. $queryHookQuestionToQuestionnaire = "INSERT INTO questionnair_question (`id_questionnair`, `id_question`) VALUES ('$questionnaireID', '$questionID');";
  342. if(!mysqli_query($connection, $queryHookQuestionToQuestionnaire)) {
  343. http_response_code(400);
  344. die("Error: Couldn't hook question to questionnaire (Question: '".$questionPremises[$i]."')<br>".mysqli_error($connection)."<br>Query: $queryHookQuestionToQuestionnaire");
  345. }
  346. }
  347. }
  348. ?>