123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
-
- require_once 'config.php';
- require_once 'dbh.inc.php';
- require_once 'checkLogin.php';
-
-
-
-
-
-
-
- if(isset($_POST['insertQuestionToQuestionnaire'])) {
-
-
-
- $questionnaireID = mysqli_real_escape_string($connection, trim($_POST['questionnaireID']));
- $premise = mysqli_real_escape_string($connection, trim($_POST['q_premise']));
- $typeID = mysqli_real_escape_string($connection, trim($_POST['q_type']));
- $minVal = mysqli_real_escape_string($connection, trim($_POST['min_val']));
- $minText = mysqli_real_escape_string($connection, trim($_POST['min_text']));
- $maxVal = mysqli_real_escape_string($connection, trim($_POST['max_val']));
- $maxText = mysqli_real_escape_string($connection, trim($_POST['max_text']));
- $categoryID = mysqli_real_escape_string($connection, trim($_POST['q_category']));
- $subcategoryID = mysqli_real_escape_string($connection, trim($_POST['q_subcategory']));
-
-
-
-
-
- if($questionnaireID === "") {
- http_response_code(400);
- echo json_encode(array("error" => "Please specify questionnaire ID."));
- exit();
- } else if(mysqli_query($connection, "SELECT * FROM questionnair WHERE id = '$questionnaireID';")->num_rows !== 1) {
- http_response_code(400);
- echo json_encode(array("error" => "Given questionnaire ID ($questionnaireID) not in database."));
- exit();
- }
-
-
-
-
-
- if($premise === "") {
- http_response_code(400);
- echo json_encode(array("error" => "Question premise can't be empty."));
- exit();
- } else if(mb_strlen($premise) > 600) {
- http_response_code(400);
- echo json_encode(array("error" => "Question premise too long (max. is 600 characters)."));
- exit();
- }
-
-
-
-
-
- if($typeID === "" || ($typeID != "1" && $typeID != "2")) {
-
- http_response_code(400);
- echo json_encode(array("error" => "Please specify a type (1 for scaled, 2 for open)."));
- exit();
-
- } else if($typeID === "1") {
-
-
-
- if($minVal !== "1") {
- http_response_code(400);
- echo json_encode(array("error" => "Minimum value has to be 1."));
- exit();
- }
-
-
-
- if($minText === "") {
- http_response_code(400);
- echo json_encode(array("error" => "Please specify a valid minimum text."));
- exit();
- } else if(mb_strlen($minText) > 40) {
- http_response_code(400);
- echo json_encode(array("error" => "Minimum text '$minText' too long (max. is 40 characters)."));
- exit();
- }
-
-
-
-
-
-
-
- if(!is_numeric($maxVal)) {
- http_response_code(400);
- echo json_encode(array("error" => "Maximum value has to be numeric."));
- exit();
- } else if(intval($maxVal) < 2) {
- http_response_code(400);
- echo json_encode(array("error" => "Maximum value has to be greater or equal to 2."));
- exit();
- }
-
-
-
- if($maxText === "") {
- http_response_code(400);
- echo json_encode(array("error" => "Please specify a valid maximum text."));
- exit();
- } else if(mb_strlen($maxText) > 40) {
- http_response_code(400);
- echo json_encode(array("error" => "Maximum text '$maxText' too long (max. is 40 characters)."));
- exit();
- }
-
-
-
- if(mb_strtolower($maxText) === mb_strtolower($minText)) {
- http_response_code(400);
- echo json_encode(array("error" => "Labels must be different."));
- exit();
- }
-
-
- }
-
-
-
-
-
-
- if($categoryID === "") {
- http_response_code(400);
- echo json_encode(array("error" => "Please specify category ID."));
- exit();
- } else if(mysqli_query($connection, "SELECT * FROM category WHERE id = '$categoryID';")->num_rows !== 1) {
- http_response_code(400);
- echo json_encode(array("error" => "Given category ID ($categoryID) not in database."));
- exit();
- }
-
-
-
-
-
- if($subcategoryID === "") {
- http_response_code(400);
- echo json_encode(array("error" => "Please specify subcategory ID."));
- exit();
- } else if(mysqli_query($connection, "SELECT * FROM subcategory WHERE id = '$subcategoryID';")->num_rows !== 1) {
- http_response_code(400);
- echo json_encode(array("error" => "Given subcategory ID ($subcategoryID) not in database."));
- exit();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $query = "INSERT INTO question (`premise`, `id_category`, `id_subcategory`, `id_type`) VALUES ('$premise', '$categoryID', '$subcategoryID', '$typeID');";
- $result = mysqli_query($connection, $query) or die("Error: ".mysqli_error($connection));
-
-
- $questionID = mysqli_insert_id($connection) or die("Error: ".mysqli_error($connection));
-
-
-
- if($typeID === "1") {
-
- $queryMinVal = "INSERT INTO question_type (`id_type`, `id_question`, `label`, `value`) VALUES ('$typeID', '$questionID', 'min_val', '$minVal');";
- $result = mysqli_query($connection, $queryMinVal) or die("Error: ".mysqli_error($connection));
-
- $queryMinText = "INSERT INTO question_type (`id_type`, `id_question`, `label`, `value`) VALUES ('$typeID', '$questionID', 'min_texto', '$minText');";
- $result = mysqli_query($connection, $queryMinText) or die("Error: ".mysqli_error($connection));
-
-
- $queryMaxVal = "INSERT INTO question_type (`id_type`, `id_question`, `label`, `value`) VALUES ('$typeID', '$questionID', 'max_val', '$maxVal');";
- $result = mysqli_query($connection, $queryMaxVal) or die("Error: ".mysqli_error($connection));
-
- $queryMaxText = "INSERT INTO question_type (`id_type`, `id_question`, `label`, `value`) VALUES ('$typeID', '$questionID', 'max_texto', '$maxText');";
- $result = mysqli_query($connection, $queryMaxText) or die("Error: ".mysqli_error($connection));
-
- }
-
-
-
- $queryHookQuestionToQuestionnaire = "INSERT INTO questionnair_question (`id_questionnair`, `id_question`) VALUES ('$questionnaireID', '$questionID');";
- $result = mysqli_query($connection, $queryHookQuestionToQuestionnaire) or die("Error: ".mysqli_error($connection));
-
-
-
-
-
-
- }
-
|