protected void Page_Load(object sender, EventArgs e) { QuizResultDAO qrDAO = new QuizResultDAO(); QuizQuestionDAO qqDAO = new QuizQuestionDAO(); int quizResultID = Convert.ToInt32(Request.QueryString["id"]); QuizResult currentQuizResult = qrDAO.getQuizResultByID(quizResultID); Quiz currentQuiz = currentQuizResult.getQuiz(); User currentUser = (User)Session["currentUser"]; List <QuizQuestion> allQuestions = qqDAO.getAllQuizQuestionByQuizID(currentQuiz.getQuizID()); Boolean superuser = false; foreach (string s in currentUser.getRoles()) { if (s.Equals("superuser")) { superuser = true; } } if (currentUser == null) { Response.Redirect("Login.aspx"); } else if (currentQuizResult.getUser().getUserID() != currentUser.getUserID() && !superuser) { Response.Redirect("errorPage.aspx"); } else { lblBreadcrumbCourseName.Text = currentQuiz.getMainCourse().getCourseName(); lblScore.Text = currentQuizResult.getScore() + "/" + allQuestions.Count; double percentage = (Convert.ToDouble(currentQuizResult.getScore()) / allQuestions.Count) * 100; lblPercent.Text = Math.Round(percentage, 2).ToString() + "%"; lblAttemptNo.Text = currentQuizResult.getAttempt().ToString(); lblQuizDate.Text = currentQuizResult.getDateSubmitted().ToString("dd/MM/yyyy"); string grade = currentQuizResult.getGrade(); if (grade.Equals("pass")) { lblStatusPass.Visible = true; lblStatusFail.Visible = false; } else { lblStatusPass.Visible = false; lblStatusFail.Visible = true; } } }
protected void Timer1_Tick(object sender, EventArgs e) { TimeSpan timeLeft = new TimeSpan(); if (Session["timeLeft"] != null) { timeLeft = (DateTime)Session["timeLeft"] - DateTime.Now; if (timeLeft.Hours <= 0 && timeLeft.Minutes <= 0 && timeLeft.Seconds <= 0) { lblTimer.Text = "Times up!"; lblTimerDisplay.Text = "Times up!"; panelQuiz.Visible = false; panelStartQuiz.Visible = false; panelTimesUp.Visible = true; //insert attempt int quizID = Convert.ToInt32(Request.QueryString["id"]); User currentUser = (User)Session["currentUser"]; QuizResultDAO qrDAO = new QuizResultDAO(); QuizResultHistoryDAO qrhDAO = new QuizResultHistoryDAO(); List <QuizResultHistory> userAnswers = (List <QuizResultHistory>)Session["userAnswers"]; QuizQuestion currentQuestion = (QuizQuestion)Session["previousQuestion"]; int attempt = qrDAO.getAttemptForQuiz(quizID, currentUser.getUserID()); attempt++; //to calculate score int userScore = calculateScore(userAnswers); //to get all other details needed to insert to QuizResult string grade = "fail"; if (checkIfUserPass(quizID, userScore)) { grade = "pass"; } DateTime currentDate = DateTime.Now.Date; //insert quizResultHistory foreach (QuizResultHistory qrh in userAnswers) { qrhDAO.createQuizResultHistory(qrh); } //insert QuizResult int quizResultID = qrDAO.createQuizResult(currentUser.getUserID(), quizID, userScore, grade, currentDate, attempt); Session["timesUpResultID"] = quizResultID; //check if user can reattempt quiz QuizResult currentResult = qrDAO.getQuizResultByID(quizResultID); Quiz currentQuiz = currentResult.getQuiz(); if (currentQuiz.getMultipleAttempts().Equals("n")) { int numOfAttempts = qrDAO.getNumberOfAttempts(currentUser.getUserID(), currentQuiz.getQuizID()); if (numOfAttempts >= currentQuiz.getNumberOfAttempts()) { btnRestartQuiz.Visible = false; } } } else { //lblTimer.Text = timeLeft.Seconds.ToString(); lblTimer.Text = timeLeft.ToString(@"hh\:mm\:ss"); } } }