public ActionResult StartQuiz(int id)
        {
            Quiz quiz = db.Quizzes.Find(id);

            if (quiz == null)
            {
                return(RedirectToAction("Index"));
            }

            UserProfile student = db.UserProfiles.Find(WebSecurity.CurrentUserId);

            if (quiz.Grade != student.Grade)
            {
                TempData["Message"]      = "Quiz is not in your grade";
                TempData["MessageClass"] = "error";
                return(RedirectToAction("Index"));
            }

            if (quiz.Exam && getOtherEnrollmentsForQuizAndUser(student, quiz).Count() > 0)
            {
                TempData["Message"]      = "Quiz is an exam in which you already have participated";
                TempData["MessageClass"] = "error";
                return(RedirectToAction("Index"));
            }

            Enrollment enrollment = new Enrollment(quiz, student);

            db.Enrollments.Add(enrollment);
            db.SaveChanges();

            return(RedirectToAction("TakeQuiz", new { eid = enrollment.Id, index = 0 }));
        }
Esempio n. 2
0
        public ActionResult Create(
            [Bind(Exclude = ("StateID"))]
            Quiz quiz)
        {
            if (ModelState.IsValid)
            {
                var teacher = db.UserProfiles.Find(WebSecurity.CurrentUserId);
                quiz.Author = teacher;
                db.Quizzes.Add(quiz);
                db.SaveChanges();
                TempData["Message"]      = "Quiz succesfully created, you can now start adding questions";
                TempData["MessageClass"] = "success";
                return(RedirectToAction("Details", new { id = quiz.Id }));
            }

            PopulateStateDropdownList(quiz.StateID);
            return(View(quiz));
        }
Esempio n. 3
0
        public ActionResult Create(
            [Bind(Exclude = "Date")]
            Exercise exercise)
        {
            if (ModelState.IsValid)
            {
                var teacher = db.UserProfiles.Find(WebSecurity.CurrentUserId);
                exercise.Author = teacher;

                db.Exercises.Add(exercise);
                db.SaveChanges();
                TempData["Message"]      = "Exercise succesfully created";
                TempData["MessageClass"] = "success";
                return(RedirectToAction("Index"));
            }
            PopulateCategoryDropdownList(exercise.CategoryID);
            return(View(exercise));
        }
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider       = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return(RedirectToAction("Manage"));
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (var db = new QuizAppDb())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.UserProfiles.Add(new UserProfile {
                            UserName = model.UserName
                        });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return(RedirectToLocal(returnUrl));
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl           = returnUrl;
            return(View(model));
        }