public void CreateQuiz(DatabaseEntities connection) { var quiz = new Quiz(); var transaction = connection.Database.BeginTransaction(); try { quiz.teacherId = this.teacherId; quiz.title = this.title; quiz.description = this.description; quiz.totalMarks = this.totalMarks; quiz.passingMarks = this.passingMarks; quiz.visibility = this.visibility; connection.Quiz.Add(quiz); connection.SaveChanges(); foreach (var question in questions) { var newQuestion = question.ToQuestion(); newQuestion.quizId = quiz.id; newQuestion.teacherId = quiz.teacherId; connection.Question.Add(newQuestion); connection.SaveChanges(); if (newQuestion.type == "Multiple Choice" || newQuestion.type == "Checkboxes") { foreach (var option in question.options) { var newOption = option.ToQuestionOption(); newOption.questionId = newQuestion.id; newOption.quizId = quiz.id; newOption.teacherId = quiz.teacherId; connection.QuestionOption.Add(newOption); connection.SaveChanges(); } } } if (this.visibility == "Public") { foreach (string bl in this.blackList) { var blackEmail = new Blacklist(); blackEmail.email = bl; blackEmail.quizId = quiz.id; blackEmail.teacherId = quiz.teacherId; connection.Blacklist.Add(blackEmail); connection.SaveChanges(); } } else { foreach (string wl in this.whiteList) { var whiteEmail = new Whitelist(); whiteEmail.email = wl; whiteEmail.quizId = quiz.id; whiteEmail.teacherId = quiz.teacherId; connection.Whitelist.Add(whiteEmail); connection.SaveChanges(); } } transaction.Commit(); } catch (Exception err) { transaction.Rollback(); throw err; } }
/* handle the submission of form */ protected void signUpSubmit_ServerClick(object sender, EventArgs e) { var Sender = (Button)sender; signupMessages.Visible = false; signupErrors.Visible = false; String loginLink = "<span><a href='/login.aspx'>Login</a></span>"; DatabaseEntities db = new DatabaseEntities(); var user = db.EndUser.FirstOrDefault(eu => eu.email == email.Text); if (user == null) { user = new EndUser(); } /* user already exist */ if ((user.email == email.Text && Sender.CommandName != "update") || (user.email == email.Text && Sender.CommandName == "update" && email.Text != Sender.CommandArgument)) { String _message = "This email is already registered with us. Please choose another!"; signupErrors.InnerHtml = _message; signupErrors.Visible = true; return; } /* user password is invalid - in case of update */ if (Sender.CommandName == "update") { var originalUser = db.EndUser.First(eu => eu.email == Sender.CommandArgument); if (oldPassword.Value != originalUser.password) { signupErrors.InnerText = "Invalid old password!"; signupErrors.Visible = true; return; } } /* check image type - if file uploaded */ if (isProfilePictureSet.Value == "true" && profilePictureFileUpload.HasFile) { if (profilePictureFileUpload.PostedFile.ContentType != "image/png") { signupErrors.InnerText = "Only png files are supported as profile picture!"; signupErrors.Visible = true; return; } } try { user.email = email.Text; user.password = password.Value; user.firstName = firstName.Text; user.secondName = secondName.Text; user.countryCode = country.SelectedValue; user.city = city.Text; user.active = true; user.profilePicturePath = profilePicture.Src; user.gender = "Female"; if (male.Checked) { user.gender = "Male"; } if (unspecified.Checked) { user.gender = "Unspecified"; } if (teacher.Checked) { user.type = "teacher"; } else { user.type = "student"; } if (Sender.CommandName != "update") { db.EndUser.Add(user); } db.SaveChanges(); /* set profile picture - if user added */ if (isProfilePictureSet.Value == "true") { if (profilePictureFileUpload.HasFile) { var fileInfo = new FileInfo(profilePictureFileUpload.PostedFile.FileName); var path = "/resources/images/profile_pictures/custom/" + user.id + fileInfo.Extension; profilePictureFileUpload.PostedFile.SaveAs(Server.MapPath("~" + path)); user.profilePicturePath = path; } db.SaveChanges(); } if (teacher.Checked) { var _teacher = db.Teacher.FirstOrDefault(t => t.userId == user.id); if (_teacher == null) { _teacher = new Teacher(); } _teacher.speciality = speciality.Text; _teacher.userId = user.id; if (Sender.CommandName != "update") { db.Teacher.Add(_teacher); } db.SaveChanges(); } else { var _student = db.Student.FirstOrDefault(s => s.userId == user.id); if (_student == null) { _student = new Student(); } _student.userId = user.id; if (Sender.CommandName != "update") { db.Student.Add(_student); } db.SaveChanges(); } } catch (Exception err) { var _message = "Something went wrong!"; signupErrors.InnerText = _message; signupErrors.Visible = true; return; } if (Sender.CommandName == "update") { Session["userId"] = user.id.ToString(); Session["userType"] = user.type; Session["firstName"] = user.firstName; Session["profilePicture"] = user.profilePicturePath; Response.Cookies["login"]["userId"] = user.profilePicturePath; Response.Cookies["login"]["firstName"] = user.profilePicturePath; Response.Cookies["login"]["userType"] = user.profilePicturePath; Response.Cookies["login"]["profilePicture"] = user.profilePicturePath; Response.Redirect("/profile/profile.aspx?updated=true"); } var message = "Your account has been successfully created!"; message += "Please " + loginLink + " to continue."; signupMessages.Visible = true; signupMessages.InnerHtml = message; }
public void UpdateQuiz(DatabaseEntities connection) { if (this.id < 0) { throw new Exception("Quiz.id must be non-negtive to perform update."); } var quiz = connection.Quiz.Where(q => q.id == this.id && q.teacherId == this.teacherId).Single(); var transaction = connection.Database.BeginTransaction(); try { /* remove existing data */ this.Purge(connection); /* add new data */ foreach (var question in questions) { var newQuestion = question.ToQuestion(); newQuestion.quizId = quiz.id; newQuestion.teacherId = quiz.teacherId; connection.Question.Add(newQuestion); connection.SaveChanges(); if (newQuestion.type == "Multiple Choice" || newQuestion.type == "Checkboxes") { foreach (var option in question.options) { var newOption = option.ToQuestionOption(); newOption.questionId = newQuestion.id; newOption.quizId = quiz.id; newOption.teacherId = quiz.teacherId; connection.QuestionOption.Add(newOption); connection.SaveChanges(); } } } if (this.visibility == "Public") { foreach (string bl in this.blackList) { var blackEmail = new Blacklist(); blackEmail.email = bl; blackEmail.quizId = quiz.id; blackEmail.teacherId = quiz.teacherId; connection.Blacklist.Add(blackEmail); connection.SaveChanges(); } } else { foreach (string wl in this.whiteList) { var whiteEmail = new Whitelist(); whiteEmail.email = wl; whiteEmail.quizId = quiz.id; whiteEmail.teacherId = quiz.teacherId; connection.Whitelist.Add(whiteEmail); connection.SaveChanges(); } } quiz.title = this.title; quiz.description = this.description; quiz.totalMarks = this.totalMarks; quiz.passingMarks = this.passingMarks; quiz.visibility = this.visibility; connection.SaveChanges(); transaction.Commit(); } catch (Exception err) { transaction.Rollback(); throw new Exception("Error: please see inner exception for details.", err); } }
protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { return; } if (string.IsNullOrEmpty(Request.QueryString["userId"])) { /* if session/cookies exist - Redirect */ try { if (String.IsNullOrEmpty((String)Session["userId"]) || String.IsNullOrEmpty((String)Session["firstName"]) || String.IsNullOrEmpty((String)Session["userType"])) { String userId = Request.Cookies["login"]["userId"]; String userFirstName = Request.Cookies["login"]["firstName"]; String userType = Request.Cookies["login"]["userType"]; try { Int32.Parse(userId); } catch { throw new Exception(); } Session.Abandon(); Session["userId"] = userId; Session["firstName"] = userFirstName; Session["userType"] = userType; Session.Timeout = 60; } if ((String)Session["userType"] == "teacher") { Response.Redirect("/teacher_quizzes/all_quizzes.aspx"); } else { Response.Redirect("/student_quizzes/open_quizzes.aspx"); } } catch (Exception error) { if (!(error is Exception) || !(error is System.NullReferenceException)) { throw error; } } } /* load countries from database */ DatabaseEntities db = new DatabaseEntities(); var countries = db.getCountries(); country.DataSource = countries; country.DataTextField = "name"; country.DataValueField = "code"; country.DataBind(); /* change user type based on Query String */ if (string.IsNullOrEmpty(Request.QueryString["userId"]) && Request.QueryString["userType"] != null) { if (Request.QueryString["userType"] != "teacher") { student.Checked = true; specialityInputGroup.Visible = false; specialityRFV.Enabled = false; profilePictureFileUpload.Attributes["src"] = "/resources/images/profile_pictures/default/student_male.png"; } } /* load update mode, if request is to edit profile */ if (Request.QueryString["userId"] != null) { try { var userId = Int32.Parse(Request.QueryString["userId"]); var profile = db.EndUser.First(i => i.id == userId); firstName.Text = profile.firstName; secondName.Text = profile.secondName; email.Text = profile.email; country.SelectedValue = profile.countryCode; city.Text = profile.city; profilePicture.Src = profile.profilePicturePath; /* check if the image is default or custom */ var pattern = @"\/resources\/images\/profile_pictures\/custom\/.*\.png"; if (Regex.IsMatch(profilePicture.Src, pattern)) { isProfilePictureSet.Value = "true"; } switch (profile.gender) { case "Male": male.Checked = true; break; case "Female": female.Checked = true; break; case "Unspecified": unspecified.Checked = true; break; default: break; } if (profile.type == "teacher") { var _teacher = db.Teacher.First(t => t.userId == profile.id); speciality.Text = _teacher.speciality; student.Enabled = false; student.Checked = false; teacher.Checked = true; } else { teacher.Enabled = false; teacher.Checked = false; student.Checked = true; specialityInputGroup.Visible = false; specialityRFV.Enabled = false; } gotoLogin.Visible = false; oldPasswordFG.Visible = true; oldPasswordRFV.Enabled = true; signupLabel.InnerText = "Update Profile"; signupSubmit.Text = "Update Profile"; signupSubmit.CommandName = "update"; signupSubmit.CommandArgument = profile.email; } catch (Exception error) { signupErrors.Visible = true; signupErrors.InnerText = "Sorry, error occured while loading profile!"; SignupForm.Visible = false; } } }