/// <summary> /// Handles the CreatedUser event of the RegisterUser control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void RegisterUser_CreatedUser(object sender, EventArgs e) { FormsAuthentication.SetAuthCookie(RegisterUser.UserName, createPersistentCookie: false); var firstNameTextBox = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("FirstName") as TextBox; var lastNameTextBox = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("LastName") as TextBox; var userNameTextBox = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("UserName") as TextBox; var userName = userNameTextBox.Text; System.Web.Security.Roles.AddUserToRole(userName, "Student"); using (var context = new Entities()) { var user = (from item in context.Users where item.UserName == userName select item).FirstOrDefault(); user.FirstName = firstNameTextBox.Text; user.LastName = lastNameTextBox.Text; context.SaveChanges(); } Response.Redirect("~/"); }
/// <summary> /// Deletes the category. /// </summary> /// <param name="id">The id.</param> public void DeleteCategory(int id) { using (var context = new Entities()) { var categoryToBeDeleted = (from item in context.QuestionCategories where item.Id == id select item).FirstOrDefault(); var questions = from item in context.Questions where item.CategoryId == id select item; foreach (var question in questions) { question.CategoryId = null; } var childCategories = from item in context.QuestionCategories where item.ParentCategoryId == id select item; foreach (var category in childCategories) { category.ParentCategoryId = categoryToBeDeleted.ParentCategoryId; } context.QuestionCategories.Remove(categoryToBeDeleted); context.SaveChanges(); } }
/// <summary> /// Adds the specified question. /// </summary> /// <param name="question">The question.</param> public static void Add(Question question) { using (var context = new Entities()) { context.Questions.Add(question); context.SaveChanges(); } }
/// <summary> /// Gets all. /// </summary> /// <returns>All the categories.</returns> public static IEnumerable<QuestionCategory> GetAll() { using (var context = new Entities()) { return (from item in context.QuestionCategories select item).ToList(); } }
/// <summary> /// Adds the specified category. /// </summary> /// <param name="category">The category.</param> public static void Add(QuestionCategory category) { using (var context = new Entities()) { context.QuestionCategories.Add(category); context.SaveChanges(); } }
/// <summary> /// Gets the by id. /// </summary> /// <param name="id">The id.</param> /// <returns>Found question.</returns> public static Question GetById(int id) { using (var context = new Entities()) { return (from item in context.Questions.Include("Author").Include("QuestionCategory").Include("QuestionAnswers") where item.Id == id select item).FirstOrDefault(); } }
/// <summary> /// Finds the specified questions. /// </summary> /// <param name="query">The query.</param> /// <returns>Found questions.</returns> public static IEnumerable<Question> Find(Expression<Func<Question, bool>> query) { using (var context = new Entities()) { return (from question in context.Questions.Include("Author").Include("QuestionCategory").Include("QuestionAnswers") .Where(query) select question).ToList(); } }
/// <summary> /// Gets the by id. /// </summary> /// <param name="id">The id.</param> /// <returns>Found question category.</returns> public static QuestionCategory GetById(int id) { using (var context = new Entities()) { return (from item in context.QuestionCategories where item.Id == id select item).FirstOrDefault(); } }
/// <summary> /// Finds the specified categories. /// </summary> /// <param name="query">The query.</param> /// <returns>Found categories.</returns> public static IEnumerable<QuestionCategory> Find(Expression<Func<QuestionCategory, bool>> query) { using (var context = new Entities()) { return (from question in context.QuestionCategories .Where(query) select question).ToList(); } }
/// <summary> /// Marks question as deleted. /// </summary> /// <param name="id">The id.</param> public static void MarkAsDeleted(int id) { using (var context = new Entities()) { var question = (from item in context.Questions where item.Id == id select item).FirstOrDefault(); question.IsDeleted = true; context.SaveChanges(); } }
/// <summary> /// Gets the students. /// </summary> /// <returns>List of students.</returns> public static IEnumerable<Student> GetStudents() { var students = new List<Student>(); var studentAccountNames = System.Web.Security.Roles.GetUsersInRole(Student); using (var context = new Entities()) { foreach (var accountname in studentAccountNames) { var foundStudent = (from item in context.Users where item.UserName == accountname select item).FirstOrDefault(); students.Add(new Student(string.Format("{0} {1}", foundStudent.FirstName, foundStudent.LastName), foundStudent.UserId)); } } return students; }
/// <summary> /// Saves the changes. /// </summary> /// <param name="modifiedQuestion">The modified question.</param> public void SaveChanges(Question modifiedQuestion) { using (var context = new Entities()) { var selectedQuestion = (from item in context.Questions.Include("QuestionCategory").Include("QuestionAnswers") where item.Id == modifiedQuestion.Id select item).FirstOrDefault(); selectedQuestion.Content = modifiedQuestion.Content; selectedQuestion.ModificationDate = DateTime.Now; selectedQuestion.IsVisible = modifiedQuestion.IsVisible; for (int i = 0; i < selectedQuestion.QuestionAnswers.Count; ++i) { selectedQuestion.QuestionAnswers.ElementAt(i).Content = modifiedQuestion.QuestionAnswers.ElementAt(i).Content; selectedQuestion.QuestionAnswers.ElementAt(i).Fraction = modifiedQuestion.QuestionAnswers.ElementAt(i).Fraction; } context.SaveChanges(); } }