| Current Path : /var/www/cesa.co.za/cesanet/ |
| Current File : /var/www/cesa.co.za/cesanet/saveexam.php |
<?php
include("inc_functions.php");
$sRedirServer = '/cesanet/'; //"http" . ($_SERVER["HTTPS"] == "on" ? "s" : "") . "://" . $_SERVER["HTTP_HOST"] . "/cesanet/";
if (!isset($_SESSION["StudentID"]) || empty($_SESSION["StudentID"])) {
header("Location: " . $sRedirServer . "login.php");
}
$conni_orange = $conni;
$conn = $conni;
$Questionnaire_ID = intval($_POST["qid"]);
if (isset($_POST["StudentNumber"])) {
$sql = "UPDATE CCEExamTiming SET StudentNumber='" . filter_input(INPUT_POST, "StudentNumber", FILTER_SANITIZE_STRING) . "' WHERE CCEStudentID=" . intval($_SESSION["StudentID"]) . " AND Questionnaire_ID=" . $Questionnaire_ID;
mysqli_query($conn, $sql);
die();
}
if (isset($_POST["IDNumber"])) {
$sql = "UPDATE CCEExamTiming SET IDNumber='" . filter_input(INPUT_POST, "IDNumber", FILTER_SANITIZE_STRING) . "' WHERE CCEStudentID=" . intval($_SESSION["StudentID"]) . " AND Questionnaire_ID=" . $Questionnaire_ID;
mysqli_query($conn, $sql);
die();
}
$GroupID = 0;
$iRespondeeID = $_SESSION["StudentID"];
$iInterim = intval($_REQUEST["i"]);
$sql = "SELECT CCEExams.*, CCEExamTiming.CCEExamTimingID, CCEExamTiming.TimeStarted, CCEExamTiming.NewEndTime
FROM CCEExams LEFT JOIN CCEExamTiming ON (CCEExams.Questionnaire_ID=CCEExamTiming.Questionnaire_ID AND CCEExamTiming.CCEStudentID=" . $_SESSION["StudentID"] . ")
WHERE CCEExams.Questionnaire_ID='" . $Questionnaire_ID . "' ORDER BY `CCEExamTiming`.`TimeStarted` DESC";
$rec = mysqli_query($conn, $sql);
if ($rec && $row = mysqli_fetch_assoc($rec)) {
extract($row);
if (!empty($NewEndTime))
$CloseDate = $NewEndTime;
if ($CCEExamTimingID <= 0) {
$CCEExamTimingID = 0;
}
}
mysqli_free_result($rec);
$sql = "SELECT CCEExamQuestions.IsMulti, CCEExamQuestions.QuestionTitle, CCEExamQuestions.Answer_Type, CCEExamAnswers.* "
. "FROM CCEExamAnswers JOIN CCEExamQuestions USING (Question_ID) "
. "WHERE CCEExamQuestions.Question_ID = '" . intval($_REQUEST["q"]) . "' OR CCEExamQuestions.ParentQuestionID = '" . intval($_REQUEST["q"]) . "' ";
//echo $sql . '<br />';
//exit;
$rec = mysqli_query($conn, $sql);
$bSaved = false;
while ($rec && $row = mysqli_fetch_assoc($rec)) {
if ($row["Answer_Type"] == 'a') {
// This is a free text answer
if (isset($_POST["Question_" . $row["Question_ID"] . "_" . $row["Answer_ID"]])) {
$sTextAnswer = $_POST["Question_" . $row["Question_ID"] . "_" . $row["Answer_ID"]];
} else {
$sTextAnswer = $_POST["Question_" . $row["Question_ID"]];
}
if (empty($sTextAnswer)) {
$sql = "UPDATE CCEExamStudentAnswers SET InterimSave=1 WHERE Question_ID=" . $row["Question_ID"] . " AND Respondee_ID=" . $iRespondeeID;
mysqli_query($conn, $sql);
if (!isset($_REQUEST["noredirect"])) {
$_SESSION["ExamError"] = "Nothing to save, please write your answer in the block provided.";
header("Location: " . $sRedirServer . "exam.php?qid=" . $Questionnaire_ID . "&q=" . intval($_REQUEST["q"]));
}
die();
} else {
$sql = "REPLACE INTO CCEExamStudentAnswers (GroupID, Questionnaire_ID, Question_ID, Answer_ID, Answer_Text, Respondee_ID, InterimSave, ExamTimingID)
VALUES (" . $GroupID . ", " . $Questionnaire_ID . ", " . $row["Question_ID"] . ", " . $row["Answer_ID"] . ", '" . addslashes($sTextAnswer) . "', " . $iRespondeeID . ", " . $iInterim . ", " . $CCEExamTimingID . ")";
// echo $sql . '<br />';
// exit;
mysqli_query($conn, $sql);
$bSaved = true;
//echo 'saved it';
//exit;
}
} elseif ($row["IsMulti"] == 1) {
$sql = "REPLACE INTO CCEExamStudentAnswers (GroupID, Questionnaire_ID, Question_ID, Answer_ID, Answer_Text, Respondee_ID, InterimSave, ExamTimingID) VALUES "
. "(" . $GroupID . ", " . $Questionnaire_ID . ", " . $row["Question_ID"] . ", " . $row["Answer_ID"] . ", '" . implode(",", $_POST["Question_" . $row["Question_ID"]]) . "', " . $iRespondeeID . ", " . $iInterim . ", " . $CCEExamTimingID . ")";
mysqli_query($conn, $sql);
$bSaved = true;
} else {
if ($_POST["Question_" . $row["Question_ID"]] == $row["Answer_ID"]) {
if (isset($_POST["Question_" . $row["Question_ID"] . "_" . $row["Answer_ID"]]) && !empty($_POST["Question_" . $row["Question_ID"] . "_" . $row["Answer_ID"]])) {
$sTextAnswer = $_POST["Question_" . $row["Question_ID"] . "_" . $row["Answer_ID"]];
} else {
$sTextAnswer = $row["Answer_ID"];
}
$sql = "REPLACE INTO CCEExamStudentAnswers (GroupID, Questionnaire_ID, Question_ID, Answer_ID, Respondee_ID, Answer_Text, InterimSave, ExamTimingID) VALUES "
. "(" . $GroupID . ", " . $Questionnaire_ID . ", " . $row["Question_ID"] . ", " . $_POST["Question_" . $row["Question_ID"]] . ", " . $iRespondeeID . ", '" . addslashes($sTextAnswer) . "', " . $iInterim . ", " . $CCEExamTimingID . ")";
// echo $sql;
mysqli_query($conn, $sql);
$bSaved = true;
}
}
if ($bSaved) {
$_SESSION["ExamAlert"] = "Question '" . $row["QuestionTitle"] . "' saved.";
$_SESSION["saved"] = intval($_REQUEST["q"]);
}
// echo $_SESSION["ExamAlert"];
// exit;
} //while
//echo 'after while';
//exit;
if (!isset($_REQUEST["noredirect"])) {
if ($bSaved) {
header("Location: " . $sRedirServer . "exam.php?qid=" . $Questionnaire_ID);
} else {
header("Location: " . $sRedirServer . "exam.php?qid=" . $Questionnaire_ID . "&q=" . intval($_REQUEST["q"]));
}
}