Your IP : 216.73.217.79


Current Path : /var/www/cesa.co.za/
Upload File :
Current File : /var/www/cesa.co.za/test.php

<?php
session_start();
?>
<?php include($_SERVER['DOCUMENT_ROOT'] . "/php/inc_db.php"); ?>
<?php
$conni_orange = $conni;

$Recall = 0;
$RespondeeID = 0;
if (isset($_GET["rid"]) && isset($_GET["chk"])) {
	if (md5($_GET["rid"]) == $_GET["chk"]) {
		$sql = "SELECT * FROM AnonRespondees WHERE Anon_RespondeeID=" . intval($_GET["rid"]);
		$rec = mysqli_query($conni_orange, $sql);
		if ($row = mysqli_fetch_assoc($rec)) {
			$Recall = 1;
			$RespondeeID = intval($_GET["rid"]);
			$GroupID = $row["GroupID"];
			$Questionnaire_ID = $row["Questionnaire_ID"];
			$arrPrevious = array();

			if (empty($Questionnaire_ID)) {
				echo "Invalid link, please reload the test from the original link you were given.";
				die();
			}

			$sql = "SELECT Questions.`Answer_Type`, Anon_User_Answers.* FROM Anon_User_Answers JOIN Questions USING (Question_ID) "
				. "WHERE Respondee_ID=" . $RespondeeID;
			$rec = mysqli_query($conni_orange, $sql);
			while ($row = mysqli_fetch_assoc($rec)) {
				if (empty($row["Answer_Text"]) && ($row["Answer_Type"] == "m")) {
					$arrPrevious["Question_" . $row["Question_ID"] . "_" . $row["Answer_ID"]] = $row["Answer_ID"];
				} else {
					$arrPrevious["Question_" . $row["Question_ID"] . "_" . $row["Answer_ID"]] = $row["Answer_Text"];
				}
			}
		} else {
			echo "Invalid link";
			die();
		}
	} else {
		echo "Unauthorised link";
		die();
	}
} else {
	$GroupID = intval($_GET["id"]);
	$Questionnaire_ID = intval($_GET["qid"]);
}
//var_dump($arrPrevious);
$sql = "SELECT * FROM Questionnaires WHERE Questionnaire_ID='" . $Questionnaire_ID . "'";
$rec = mysqli_query($conni_orange, $sql);
if ($row = mysqli_fetch_assoc($rec)) {
	extract($row);
}
mysqli_free_result($rec);

?>
<html>

<head>
	<title>Questionnaire</title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
	<style type="text/css">
		@media (max-width: 600px) {
			font-size:1.5rem;

			.big-checkbox .form-check-input {
				/* Double-sized Checkboxes */
				-ms-transform: scale(2);
				/* IE */
				-moz-transform: scale(2);
				/* FF */
				-webkit-transform: scale(2);
				/* Safari and Chrome */
				-o-transform: scale(2);
				/* Opera */
				padding: 10px !important;
			}

			.big-checkbox .form-check-label {
				padding-left: 10px;
			}
		}
	</style>
	<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
	<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
	<script type="text/javascript">
		<!--
		function CheckForm(objForm) {
			bNoErrors = true;
			if (document.getElementById('go').value == '2') return true;
			for (i = 0; i < objForm.elements.length; i++) {
				objElem = objForm.elements[i];
				bRequired = true;
				bElemSet = true;
				if (objElemOpt = objForm.elements[objElem.name + '_Optional']) {
					if (objElemOpt.value == 1) bRequired = false;
				}
				if (bRequired) {
					if ((objElem.type == "radio") || (objElem.type == "checkbox")) {
						objRadioElems = objForm.elements[objElem.name];
						if (objRadioElems.length && (objRadioElems.length > 1)) {
							bAnyRadioChosen = false;
							for (z = 0; z < objRadioElems.length; z++) {
								if (objRadioElems[z].checked) bAnyRadioChosen = true;
							}
							if (!bAnyRadioChosen) bElemSet = false;
						}
					} else {
						if (objElem.value == "") bElemSet = false;
					}
					if (!bElemSet) bNoErrors = false;
					if (document.getElementById(objElem.name + '_cell')) {
						if (!bElemSet) {
							document.getElementById(objElem.name + '_cell').style.backgroundColor = 'red';
							document.getElementById(objElem.name + '_cell').style.color = 'white';
						} else {
							document.getElementById(objElem.name + '_cell').style.backgroundColor = 'white';
							document.getElementById(objElem.name + '_cell').style.color = 'black';
						}
					}
				}
			}
			if (!bNoErrors) alert("Please answer all the questions.");
			$('.form-control').each(function() {
				bOutOfBounds = false;
				if (this.attr("max") !== undefined) {
					if (parseFloat(this.val()) > parseInt(this.attr("max"))) bOutOfBounds = true;
				}
				if (this.attr("min") !== undefined) {
					if (parseFloat(this.val()) < parseInt(this.attr("min"))) bOutOfBounds = true;
				}
				if (bOutOfBounds) {
					if (bNoErrors) alert("Some answers are invalid.");
					bNoErrors = false;
					document.getElementById(this.name() + '_cell').style.backgroundColor = 'red';
					document.getElementById(this.name() + '_cell').style.color = 'white';
				} else {
					document.getElementById(this.name() + '_cell').style.backgroundColor = 'white';
					document.getElementById(this.name() + '_cell').style.color = 'black';
				}
			});
			return bNoErrors;
		}

		function docascadeSelect(parent, child) {
			var childOptions = child.find('option:not(.static)');
			child.data('options', childOptions);

			parent.change(function() {
				childOptions.remove();
				child
					.append(child.data('options').filter('.sub_' + this.value))
					.change();
			})

			childOptions.not('.static, .sub_' + parent.val()).remove();
		}

		function clearForms() {
			$(':input').not(':button, :submit, :reset, :hidden, :checkbox, :radio').val('');
			$(':checkbox, :radio').prop('checked', false);
			$(':checkbox, :radio').removeAttr('checked');
		}
		//
		-->
		function
		InterimSave(objForm)
		{
		$(':checkbox,
		:
		radio
		').prop('
		required
		',
		false);
		$(':checkbox,
		:
		radio
		').removeAttr('
		required
		');
		document.getElementById('go').value
		=
		'2';
		objForm.submit();
		}
	</script>
</head>

<body class="bg-dark">
	<?php
	$CoverNote = "";

	$sql = "SELECT * FROM Sections WHERE Questionnaire_ID='" . $Questionnaire_ID . "' ORDER BY Display_Order, Section_ID";
	$recS = mysqli_query($conni_orange, $sql);
	$iNumSteps = mysqli_num_rows($recS);
	?>
	<div class="container bg-light">
		<div class="row">
			<div class="col-12">
				<h1>
					<?php echo htmlspecialchars($Questionnaire_Name); ?>
				</h1>
				<?php

				if (!empty($CloseDate) && (strtotime($CloseDate) < time())) {
					echo "<p><b>This questionnaire is now closed.</b></p>";
					die();
				}
				?>
			</div>
		</div>
		<?php
		if (!empty($CoverNote) && (!isset($_REQUEST["aftercover"]) || empty($_REQUEST["aftercover"]))) {
		?>
			<div class="row">
				<div class="col-12">
					<p><b>Before beginning your questionnaire, please read through the following
							introduction. Then click the link at the bottom of this page to move on to the
							questionnaire.</b></p>
					<?php echo $CoverNote; ?>
					<p><b><a href="test.php?id=<?php echo $_REQUEST["id"]; ?>&qid=<?php echo $_REQUEST["qid"]; ?>&aftercover=1">
								CONTINUE TO THE QUESTIONNAIRE NOW</a></b></p>
				</div>
			</div>
		<?php
		} else {
		?>
			<form name="form1" method="post" action="savetest.php" onSubmit="return CheckForm(this)" enctype="multipart/form-data">
				<input type="hidden" name="go" id="go" value="1">
				<input type="hidden" name="id" value="<?php echo $GroupID; ?>">
				<input type="hidden" name="qid" value="<?php echo $Questionnaire_ID; ?>">
				<input type="hidden" name="rid" value="<?php echo $RespondeeID; ?>">
				<input type="hidden" name="yourname_Optional" value="0">
				<input type="hidden" name="email_Optional" value="0">
				<input type="hidden" name="subscribe_Optional" value="1">
				<div class="row">
					<div class="col-12">
						<?php
						if (!empty($Introduction)) echo $Introduction;
						?>
						<p>Please answer all the questions.</p>
						<input type="hidden" name="yourname" value="Anonymous">
						<input type="hidden" name="email" value="Unknown">
					</div>
				</div>
				<?php
				$iStep = 1;
				$arrQuestionIDs = array();
				while ($rowS = mysqli_fetch_assoc($recS)) {
					extract($rowS);
				?>
					<div class="card">
						<?php
						if ($ShowHeading == 1) {
						?>
							<div class="card-header"><b><?php echo htmlspecialchars($Section_Name); ?></b><br><?php echo $Section_Text; ?></div>
						<?php
						}
						?>
						<div class="card-body">
							<?php
							$sql = "SELECT * FROM (SELECT * FROM Questions WHERE Questionnaire_ID='" . $Questionnaire_ID . "' AND Section_ID='" . $Section_ID . "'";
							if ($Recall == 1) $sql .= " AND Question_ID IN (SELECT Question_ID FROM Anon_User_Answers WHERE Respondee_ID=" . intval($_GET["rid"]) . ")";
							$sql .= " ORDER BY ";
							if ($SelectRandom == 1) $sql .= " RAND(), ";
							$sql .= "Display_Order, Question_ID";
							if (!empty($NumToSelect)) $sql .= " LIMIT 0," . $NumToSelect;
							$sql .= ") Tmp ORDER BY Display_Order";
							$recQ = mysqli_query($conni_orange, $sql);
							while ($rowQ = mysqli_fetch_assoc($recQ)) {
								extract($rowQ);
								$arrQuestionIDs[] = $rowQ["Question_ID"];
							?>
								<div class="row border-bottom my-3">
									<div class="col-lg-6"><?php echo $Question; ?>
										<input type="hidden" name="Question_<?php echo $Question_ID; ?>_Optional" id="Question_<?php echo $Question_ID; ?>_Optional" value="<?php echo $IsOptional; ?>">
									</div>
									<div class="col-lg-6" id="Question_<?php echo $Question_ID; ?>_cell">
										<?php
										if ($Answer_Type == 's') echo "<div class='form-group'><select name='Question_" . $Question_ID . "' class='form-control " . $Question_ID . "Select'" . ($IsMulti == 1 ? " multiple" : "") . ($IsOptional ? "" : " required") . ">";
										$sql = "SELECT * FROM Answers WHERE Question_ID='" . $Question_ID . "' ORDER BY Display_Order, Answer_ID";
										$recA = mysqli_query($conni_orange, $sql);
										while ($rowA = mysqli_fetch_assoc($recA)) {
											extract($rowA);
											if (($Answer_Type != 'm') && ($Answer_Type != 's')) {
										?>
												<div class="form-group" id="Question_<?php echo $Question_ID; ?>_<?php echo $Answer_ID; ?>_cell" style="padding:2px">
													<input type="hidden" name="Question_<?php echo $Question_ID; ?>_<?php echo $Answer_ID; ?>_Optional" id="Question_<?php echo $Question_ID; ?>_<?php echo $Answer_ID; ?>_Optional" value="<?php echo $IsOptional; ?>">
													<?php
													if ($Answer_Type == 'a') {
													?>
														<textarea name="Question_<?php echo $Question_ID; ?>_<?php echo $Answer_ID; ?>" class="form-control" cols="30" rows="4" id="Question_<?php echo $Question_ID; ?>_<?php echo $Answer_ID; ?>" <?php if (!$IsOptional) echo " required"; ?>><?php if ($Recall == 1) echo $arrPrevious["Question_" . $Question_ID . "_" . $Answer_ID]; ?></textarea>
														<br>
													<?php
													} elseif ($Answer_Type == 'f') {
													?>
														<input name="Question_<?php echo $Question_ID; ?>_<?php echo $Answer_ID; ?>" class="form-control-file" type="file" id="Question_<?php echo $Question_ID; ?>_<?php echo $Answer_ID; ?>" <?php if (!$IsOptional) echo " required"; ?>>
													<?php
													} else {
													?>
														<input name="Question_<?php echo $Question_ID; ?>_<?php echo $Answer_ID; ?>" class="form-control" type="<?php echo ($RequireNumber == 1 ? "number" : "text"); ?>" <?php if ($RequireNumber == 1) echo ' step="0.01"'; ?> id="Question_<?php echo $Question_ID; ?>_<?php echo $Answer_ID; ?>" value="<?php if ($Recall == 1) echo $arrPrevious["Question_" . $Question_ID . "_" . $Answer_ID]; ?>" size="40" <?php if (!$IsOptional) echo " required"; ?><?php if (!empty($MinNum)) echo " min='" . $MinNum . "'"; ?><?php if (!empty($MaxNum)) echo " max='" . $MaxNum . "'"; ?>>
													<?php
													}
													if (!empty($TextAfter)) echo "<small class='form-text text-muted'>" . $TextAfter . "</small>";
													?>
												</div>
												<?php
											} else {
												if ($Answer_Type == 'm') {
												?>
													<div class="form-check my-1 big-checkbox">
														<?php
														if ($IsMulti == 1) {
														?>
															<input type="checkbox" class="form-check-input" value="<?php echo $Answer_ID; ?>" name="Question_<?php echo $Question_ID; ?>[]" <?php if (($Recall == 1) && ($arrPrevious["Question_" . $Question_ID . "_" . $Answer_ID] == 1)) echo " checked"; ?>> <label class="form-check-label"><?php echo strip_tags($Answer, "<br><br /><b><i><u><a><sup><sub>"); ?></label>
														<?php
														} else {
														?>
															<input type="radio" class="form-check-input" value="<?php echo $Answer_ID; ?>" name="Question_<?php echo $Question_ID; ?>" <?php if (!$IsOptional) echo " required"; ?><?php if (($Recall == 1) && ($arrPrevious["Question_" . $Question_ID . "_" . $Answer_ID] == $Answer_ID)) echo " checked"; ?>> <label class="form-check-label"><?php echo strip_tags($Answer, "<br><br /><b><i><u><a><sup><sub>"); ?></label>
														<?php
														}
														if (!empty($TextAfter)) echo "<small class='form-text text-muted'>" . $TextAfter . "</small>";
														?>
													</div>
												<?php
												} elseif ($Answer_Type == 's') {
												?>
													<option value="<?php echo $Answer_ID; ?>" class="sub_<?php echo $ParentAnswerID; ?>" <?php if (($Recall == 1) && ($arrPrevious["Question_" . $Question_ID . "_" . $Answer_ID] == 1)) echo " selected"; ?>><?php echo strip_tags($Answer); ?></option>
										<?php
												}
											}
										}
										if ($Answer_Type == 's') echo "</select></div>";
										if (!empty($ParentQuestionID)) {
											echo "<script>docascadeSelect($('." . $ParentQuestionID . "Select'),$('." . $Question_ID . "Select'));</script>";
										}
										mysqli_free_result($recA);
										?>
									</div>
								</div>
							<?php
							}
							mysqli_free_result($recQ);
							?>
						</div>
					</div>
				<?php
					$iStep++;
				}
				mysqli_free_result($recS);
				?>
				<div class="row">
					<div class="col-12 d-flex justify-content-center">
						<p><b>Please click the Submit button
								below to save your answers once you have <u>completed</u> the test.</b></p>
						<p><b>Click Save and Continue Later to save your progress and return later to continue completing the test.</b></p>
					</div>
				</div>
				<div class="row">
					<div class="col-12 pb-3 d-flex justify-content-center">
						<input type="hidden" name="QuestionIDs" value="<?php echo implode(",", $arrQuestionIDs); ?>">
						<input name="button" type="submit" class="btn btn-success btn-lg" id="button" value="Submit">
						<br>
						<input class="button" type="button" value="Save and Continue Later" onclick="InterimSave(this.form)">
					</div>
				</div>
			</form>
	</div>
<?php
		}
?>
</body>

</html>