Exemplo n.º 1
0
        public ActionResult CheckProblemPost()
        {
            var userId = this.User.Identity.GetUserId();
            var function1 = Request.Form["Function.Function1"];
            var function2 = Request.Form["Function.Function2"];

            var newProblem = new Problem(userId,
                function1,
                function2 == "" ? null : function2,
                ProblemType.Coursework,
                ProblemStatus.Active);

            this.Data.Problems.Add(newProblem);
            this.Data.SaveChanges();

            int[] mistakes = new int[4];

            var solutionModel = new ProblemSolutionViewModel();

            for (int i = 0; i < 4; i++)
            {
                var mistakeIndex = Request.Form["Mistakes[" + i + "].IndexOfX"];
                var mistakeProduct = Request.Form["Mistakes[" + i + "].MistakenProduct"];
                var mistakeValue = Request.Form["Mistakes[" + i + "].MistakenValue"];
                string mistakeDisplay;

                if (i < 2)
                {
                    mistakeDisplay = FunctionParser.GetDisplayX(mistakeProduct + mistakeIndex);
                }
                else
                {
                    mistakeDisplay = FunctionParser.GetDisplayProduct(mistakeProduct);
                }

                var mistake = new NDKS.Models.Mistake(newProblem.Id, mistakeProduct,
                    Int32.Parse(mistakeValue),
                    mistakeIndex == "" ? null : (int?) Int32.Parse(mistakeIndex),
                    mistakeDisplay);

                solutionModel.Mistakes.Add(mistake);

                this.Data.Mistakes.Add(mistake);
                this.Data.SaveChanges();

                mistakes[i] = mistake.Id;
            }
            this.Data.SaveChanges();

            solutionModel.Solution = this.GetProblemSolution(newProblem);
            int problemId = newProblem.Id;

            for (int i = 0; i < mistakes.Length; i++)
            {
                var mistake = this.Data.Mistakes.Find(mistakes[i]);
                this.Data.Mistakes.Delete(mistake);
            }

            this.Data.Problems.Delete(problemId);

            this.Data.SaveChanges();

            solutionModel.Functions = FunctionParser.GetDisplayFunction(new Function(function1, function2 == "" ? null : function2));

            return View(solutionModel);
        }
Exemplo n.º 2
0
        private void AddProblem(System.Collections.Specialized.NameValueCollection form,
            ProblemType type, string userId)
        {
            var function1 = form["Function.Function1"];
            var function2 = form["Function.Function2"];

            var newProblem = new Problem(userId,
                function1,
                function2 == "" ? null : function2,
                type,
                ProblemStatus.Active);

            this.Data.Problems.Add(newProblem);
            this.Data.SaveChanges();

            Common.Mistake[] mistakes = new Common.Mistake[4];

            for (int i = 0; i < 4; i++)
            {
                var mistakeIndex = form["Mistakes[" + i + "].IndexOfX"];
                var mistakeProduct = form["Mistakes[" + i + "].MistakenProduct"];
                var mistakeValue = form["Mistakes[" + i + "].MistakenValue"];
                string mistakeDisplay;

                if (i < 2)
                {
                    mistakeDisplay = FunctionParser.GetDisplayX(mistakeProduct + mistakeIndex);
                }
                else
                {
                    mistakeDisplay = FunctionParser.GetDisplayProduct(mistakeProduct);
                }

                var mistake = new NDKS.Models.Mistake(newProblem.Id, mistakeProduct,
                    Int32.Parse(mistakeValue),
                    mistakeIndex == "" ? null : (int?) Int32.Parse(mistakeIndex),
                    mistakeDisplay);

                this.Data.Mistakes.Add(mistake);
            }
            this.Data.SaveChanges();
        }
Exemplo n.º 3
0
        public ActionResult Exercise()
        {
            string loggedUserId = User.Identity.GetUserId();

            var existingProblem = this.Data.Problems
                .All().FirstOrDefault(p => p.UserId == loggedUserId &&
                    p.ProblemType == ProblemType.Train &&
                    p.ProblemStatus == ProblemStatus.Active);

            if (existingProblem != null)
            {
                var func = new Function(existingProblem.Function, existingProblem.Function2);

                ViewBag.func = FunctionParser.GetDisplayFunction(func);

                var existingMistakes = this.Data.Mistakes
                    .All()
                    .Where(m => m.ProblemId == existingProblem.Id).ToList();

                Common.Mistake[] existingMistakesForSolution = new Common.Mistake[4];

                for (int i = 0; i < existingMistakes.Count(); i++)
                {
                    existingMistakesForSolution[i] = new Common.Mistake(
                        existingMistakes.ElementAt(i).MistakenProduct,
                        existingMistakes.ElementAt(i).MistakenValue,
                        existingMistakes.ElementAt(i).IndexOfX,
                        existingMistakes.ElementAt(i).DisplayProduct);
                }

                for (int i = 0; i < existingMistakesForSolution.Length; i++)
                {
                    ViewBag.func += "</br> T" + (i + 1) + " => " + existingMistakesForSolution[i].DisplayProduct + " = " +
                                    existingMistakesForSolution[i].MistakenValue;
                }

                var existingSolution = FunctionParser.Solve(func, existingMistakesForSolution);
                var viewModel = new Models.Solution(existingSolution, existingProblem.Id);
                return View(viewModel);
            }

            function = FunctionParser.Generate();
            Common.Mistake[] mistakes = FunctionParser.GenerateMistakes(function);

            ViewBag.func = FunctionParser.GetDisplayFunction(function);

            for (int i = 0; i < mistakes.Length; i++)
            {
                ViewBag.func += "</br> T" + (i + 1) + " => " + mistakes[i].DisplayProduct + " = " +
                                mistakes[i].MistakenValue;
            }

            var solution = FunctionParser.Solve(function, mistakes);

            ViewBag.table1 = solution.FirstTable;

            if (solution.SecondTable != null)
            {
                ViewBag.table2 = solution.SecondTable;
            }

            Problem problem = new Problem(
                loggedUserId,
                this.function.Function1,
                this.function.Function2,
                ProblemType.Train,
                ProblemStatus.Active
                );

            this.Data.Problems.Add(problem);
            this.Data.SaveChanges();

            for (int i = 0; i < mistakes.Length; i++)
            {
                NDKS.Models.Mistake newMistake =
                    new NDKS.Models.Mistake(
                        problem.Id,
                        mistakes[i].MistakenProduct,
                        mistakes[i].MistakenValue,
                        mistakes[i].IndexOfX,
                        mistakes[i].DisplayProduct);

                this.Data.Mistakes.Add(newMistake);
            }

            this.Data.SaveChanges();

            var solutionModel = new Models.Solution(solution, problem.Id);

            return View(solutionModel);
        }