//function to return querystring values, or default
function getQString($key, $default = ""){
return isset($_GET[$key]) && $_GET[$key] != "" ? $_GET[$key] : $default;
}
include('config.php');
//database connection established in config file, now enable arabic letters
mysql_query("SET NAMES 'cp1256'");
//mysql_query("SET CHARACTER SET 'cp1256'");
//process querystring data
$user = getQString("user", "none");
$POI = getQString("POI", "none");
$loadString = getQString("load", "none");
//setup error message to show on page if something fails
$errorMessage = "";
if($user != "none" && $POI != "none"){
//check if user is a student
$query = "SELECT * FROM mdl_user INNER JOIN mdl_role_assignments ON mdl_user.id=mdl_role_assignments.userid WHERE mdl_role_assignments.roleid=5 AND mdl_user.username='" . mysql_real_escape_string($user) . "'";
$resultUser = mysql_query($query);
if(mysql_num_rows($resultUser) > 0){ //if user is indeed a student
//get database user ID of the student
$userID = mysql_result($resultUser, 0, "id");
mysql_data_seek($resultUser,0);
/* Set default values for user position data.
* These variables will be overwritten below if database contains the data for the user.
* Otherwise, a new record will be created in database with these default values.
*/
$userBook = 1; // the "user..." variables indicate the maximum available book/act/scene to this user
$userAct = 1;
$userScene = 1;
$currentBook = 1; // the "current..." variables indicate which book/act/scene is being reviewed by the user
$currentAct = 1;
$currentScene = 1;
$currentGender = "m";
//if load data was passed in via querystring, check if the desired book/act/scene is available to this user
$loadValid = true;
if($loadString != "none"){
$loadArray = explode("/", $loadString);
if($loadArray[0] > $userBook){
$loadValid = false;
}
else if($loadArray[0] == $userBook){
if($loadArray[1] > $userAct){
$loadValid = false;
}
else if($loadArray[1] == $userAct){
if($loadArray[2] > $userScene){
$loadValid = false;
}
}
}
}
else{
$loadValid = false;
}
//if desired book/act/scene is available, save current position data in database (for next time)
if($loadValid){
$currentBook = $loadArray[0];
$currentAct = $loadArray[1];
$currentScene = $loadArray[2];
$currentGender = $loadArray[3];
mysql_query("SET NAMES 'utf8'");
$query = "UPDATE mdl_arabixia_user_position SET current_book='" . mysql_real_escape_string($currentBook) . "', current_act='" . mysql_real_escape_string($currentAct) . "', current_scene='" . mysql_real_escape_string($currentScene) . "', current_gender='" . mysql_real_escape_string($currentGender) . "' WHERE userid=" . mysql_real_escape_string($userID);
mysql_query($query);
mysql_query("SET NAMES 'cp1256'");
}
else if($loadString != "none"){
//user has tried to load a quiz they are not upto yet
$errorMessage .= "You need to complete more storylines first. Please choose Book:$userBook Act:$userAct Scene:$userScene to continue as normal";
if(!($userBook==1 && $userAct==1 && $userScene==1)){
$errorMessage .= ", or choose a storyline preceding it to review";
}
$errorMessage .= ".<br/><br/>";
}
//get latest user position data from db, because if user did not provide the load data via querystring, we need to overwrite the default values set above, in order to resume the same quiz session from last time
if($loadValid || $loadString == "none"){
$query = "SELECT * FROM mdl_arabixia_user_position WHERE userid=" . mysql_real_escape_string($userID);
$resultUserPosition = mysql_query($query);
if(mysql_num_rows($resultUserPosition) > 0){
while($row = mysql_fetch_array($resultUserPosition, MYSQL_ASSOC)){
$userBook = $row['book'];
$userAct = $row['act'];
$userScene = $row['scene'];
$currentBook = $row['current_book'];
$currentAct = $row['current_act'];
$currentScene = $row['current_scene'];
$currentGender = $row['current_gender'];
}
unset($row);
}
else{ //...but if no position record exists for this user, create a new record with the default values
mysql_query("SET NAMES 'utf8'");
$query = "INSERT INTO mdl_arabixia_user_position (userid, book, current_book, act, current_act, scene, current_scene, current_gender) VALUES ('" . mysql_real_escape_string($userID) . "', '1', '1', '1', '1', '1', '1', '" . mysql_real_escape_string($currentGender) . "')";
mysql_query($query);
mysql_query("SET NAMES 'cp1256'");
}
//create gender selection radio buttons (with the correct one checked) to be displayed in HTML form
$genderButtons = "<div style=\"float: left; margin-left: 25px;\">";
$genderButtons .= "<label for=\"genderInputMasculine\">Masculine Context</label><input type=\"radio\" name=\"genderInput\" id=\"genderInputMasculine\" value=\"m\"";
if($currentGender == "m"){
$genderButtons .= " checked ";
}
$genderButtons .= "/> <label for=\"genderInputFeminine\">Feminine Context</label><input type=\"radio\" name=\"genderInput\" id=\"genderInputFeminine\" value=\"f\"";
if($currentGender == "f"){
$genderButtons .= " checked ";
}
$genderButtons .= "/> </div>";
//get the quiz id of the current scene
$currentQuizID = -1;
$query = "SELECT * FROM mdl_quiz WHERE name LIKE 'BAS/" . mysql_real_escape_string($currentBook) . "/" . mysql_real_escape_string($currentAct) . "/" . mysql_real_escape_string($currentScene) . "/" . mysql_real_escape_string($currentGender) . "/%'";
$resultCurrentQuiz = mysql_query($query);
if(mysql_num_rows($resultCurrentQuiz) > 0){
$currentQuizID = mysql_result($resultCurrentQuiz, 0, "id");
mysql_data_seek($resultCurrentQuiz,0);
//now check if this quiz is available at this POI
$query = "SELECT quiz_id FROM mdl_arabixia_poi_quiz WHERE poi_num=" . mysql_real_escape_string($POI) . " AND quiz_id=" . $currentQuizID;
$resultPOIQuiz = mysql_query($query);
if(mysql_num_rows($resultPOIQuiz) > 0){
//to display the quiz, extract question id numbers and quiz intro first
$quizIntro;
$questionIDs;
while($row = mysql_fetch_array($resultCurrentQuiz, MYSQL_ASSOC)){
$questionIDs = explode(",", $row['questions']);
$quizIntro = $row['intro'];
}
unset($row);
//now get the actual questions
$questionCount = count($questionIDs) - 1; //a question with id 0 is appended by default in moodle db, ignore it
if($questionCount > 0){
//set the query to be used
$query = "SELECT * FROM mdl_question WHERE id=";
for($i=0; $i<$questionCount; $i++){
$query .= mysql_real_escape_string($questionIDs[$i]);
if($questionCount - $i >= 2){
$query .= " OR id=";
}
}
$resultQuestions = mysql_query($query);
//set up question-and-answer panels
$dynamicPanels;
$idCounter = 0;
if(mysql_num_rows($resultQuestions) > 0){
while($row = mysql_fetch_array($resultQuestions, MYSQL_ASSOC)){
//panel
$dynamicPanels .= "<div id=\"dynamicPanel_" . $idCounter . "\" class=\"dynamicPanel\">";
//question
$dynamicPanels .= "<div class=\"dynamicPanelQuestion\">" . $row['questiontext'] . "</div>";
//answer
$dynamicPanels .= "<div class=\"dynamicPanelAnswer\">";
if($row['qtype']=="shortanswer"){
//input box
$dynamicPanels .= "<div class=\"input-box\"><div class=\"input-box-top\"></div><div class=\"input-box-middle\">";
$dynamicPanels .= "<textarea id=\"text_" . $idCounter . "\" name=\"text_" . $idCounter . "\" class=\"text\" wrap=\"soft\" onfocus=\"VirtualKeyboard.attachInput(this)\"></textarea>";
$dynamicPanels .= "</div><div class=\"input-box-bottom\"></div></div>";
}
$dynamicPanels .= "</div>"; //end of answer
$dynamicPanels .= "</div>"; //end of panel
$idCounter++;
}
unset($row);
}
}
else{
$errorMessage .= "Storyline BAS/" . $currentBook . "/" . $currentAct . "/" . $currentScene . "/" . $currentGender . " has no questions.<br/><br/>";
}
}
else{
//this quiz is not available at this POI, tell the student to come back later
$errorMessage .= "Your current storyline is for another location on the island.<br/><br/>";
}
}
else{
//this quiz is not in the system, show error message
$errorMessage .= "Storyline BAS/" . $currentBook . "/" . $currentAct . "/" . $currentScene . "/" . $currentGender . " appears to be unavailable.<br/><br/>";
}
}
}
else{
$errorMessage .= "It appears you are not registered as a student on the system.<br/><br/>";
}
}
else{
$errorMessage .= "The dashboard did not successfully pass your name and/or location details to the quiz engine.<br/><br/>";
}
//close db connection and exit
mysql_close($conn);