string(1) "1" ["toExperienceID"]=> string(1) "11" ["duplicateQuestionnaire"]=> string(1) "" }
if(isset($_POST['duplicateQuestionnaire'])) {
/*** FETCH INPUT ***/
// WARNING: SHOULD VERIFY IF THESE ACTUALLY EXIST
$fromQuestionnaireID = mysqli_real_escape_string($connection, $_POST['fromQuestionnaireID']);
$toExperienceID = mysqli_real_escape_string($connection, $_POST['toExperienceID']);
/*** FETCH THE QUESTIONNAIRE INFO ***/
$queryOldQuestionnaire = "SELECT * FROM questionnair WHERE id = $fromQuestionnaireID";
$resultOldQuestionnaire = mysqli_query($connection, $queryOldQuestionnaire) or die("Error: Couldn't fetch questionnaire info from given ID
".mysqli_error($connection));
$rowOldQuestionnaire = mysqli_fetch_assoc($resultOldQuestionnaire);
/*** DUPLICATE QUESTIONNAIRE, RETRIEVE ITS ID (TO USE IT IN TABLES experience_questionnair AND questionnair_question) AND HOOK TO CURRENT EXPERIENCE ***/
$queryQuestionnaire = "INSERT INTO questionnair (`q_title`, `description`) VALUES ('".$rowOldQuestionnaire['q_title']."', '".$rowOldQuestionnaire['description']."');";
if(!mysqli_query($connection, $queryQuestionnaire)) {
echo $queryQuestionnaire . "
";
die("Error: Couldn't create questionnaire
".mysqli_error($connection));
}
$questionnaireID = mysqli_insert_id($connection) or die("Error: Couldn't fetch questionnaire id after creating it
".mysqli_error($connection));
$queryHookQuestionnaireToExperience = "INSERT INTO experience_questionnair (`id_experience`, `id_questionnair`) VALUES ('".$toExperienceID."', '".$questionnaireID."');";
if(!mysqli_query($connection, $queryHookQuestionnaireToExperience)) {
echo $queryHookQuestionnaireToExperience . "
";
die("Error: Couldn't hook questionnaire to experience
".mysqli_error($connection));
}
// WE DON'T NEED TO INSERT A NEW REFERENCE, WE JUST NEED TO DUPLICATE THE QUESTIONS (WHICH HAVE THE REFERENCE ID ALREADY)
// WE DON'T NEED TO CREATE THE NEW CATEGORIES, WE JUST NEED TO DUPLICATE THE QUESTIONS (WHICH HAVE THE CATEGORY ID ALREADY)
// WE DON'T NEED TO CREATE THE NEW SUBCATEGORIES, WE JUST NEED TO DUPLICATE THE QUESTIONS (WHICH HAVE THE SUBCATEGORY ID ALREADY)
$queryForQuestions = "SELECT id_question FROM questionnair_question WHERE id_questionnair = $fromQuestionnaireID;";
$resultForQuestions = mysqli_query($connection, $queryForQuestions);
/*** 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 ***/
while($rowForQuestions = mysqli_fetch_assoc($resultForQuestions)) {
// FETCH THE INFO FOR A GIVEN QUESTION
$queryOldQuestion = "SELECT * FROM question WHERE id = " . $rowForQuestions['id_question'];
$resultOldQuestion = mysqli_query($connection, $queryOldQuestion);
$rowOldQuestion = mysqli_fetch_assoc($resultOldQuestion);
// DUPLICATE QUESTION
$queryQuestion = "INSERT INTO question (`premise`, `id_category`, `id_subcategory`, `id_type`, `id_referencia`) VALUES ('".mysqli_real_escape_string($connection, $rowOldQuestion['premise'])."', '".$rowOldQuestion['id_category']."', '".$rowOldQuestion['id_subcategory']."', '".$rowOldQuestion['id_type']."', '".$rowOldQuestion['id_referencia']."');";
if(!mysqli_query($connection, $queryQuestion)) {
echo $queryQuestion . "
";
die("Error: Couldn't duplicate question with id ".$rowForQuestions['id_question']."
".mysqli_error($connection));
}
// FETCH DUPLICATE QUESTION ID
$questionID = mysqli_insert_id($connection) or die("Error: Couldn't fetch new question id (from old question id ".$rowForQuestions['id_question'].")
".mysqli_error($connection));
// IF QUESTION IS SCALED, DUPLICATE MIN/MAX VALUES/TEXT
if($rowOldQuestion['id_type'] == '1') {
// FETCH THE QUESTION TYPES FOR A GIVEN QUESTION
$queryOldQuestionTypes = "SELECT * FROM question_type WHERE id_question = " . $rowForQuestions['id_question'];
$resultOldQuestionTypes = mysqli_query($connection, $queryOldQuestionTypes);
// LOOP THROUGH ALL QUESTION TYPES
while($rowOldQuestionTypes = mysqli_fetch_assoc($resultOldQuestionTypes)) {
$queryMinVal = "INSERT INTO question_type (`id_type`, `id_question`, `label`, `value`) VALUES ('".$rowOldQuestionTypes['id_type']."', '".$questionID."', '".$rowOldQuestionTypes['label']."', '".$rowOldQuestionTypes['value']."');";
if(!mysqli_query($connection, $queryMinVal)) {
echo $queryMinVal . "
";
die("Error: Couldn't duplicate question ".$rowOldQuestionTypes['label']." (from old question with id ".$rowForQuestions['id_question'].")
".mysqli_error($connection));
}
}
}
$queryHookQuestionToQuestionnaire = "INSERT INTO questionnair_question (`id_questionnair`, `id_question`) VALUES ('".$questionnaireID."', '".$questionID."');";
if(!mysqli_query($connection, $queryHookQuestionToQuestionnaire)) {
echo $queryHookQuestionToQuestionnaire . "
";
die("Error: Couldn't hook question to questionnaire (Question ID: ".$questionID.")
".mysqli_error($connection));
}
}
header('Location: ../questionnaires.php');
exit();
}
?>