Beispiel #1
0
        /// <summary>
        /// Return the index page for viewing the workset
        /// however the index is the page used to complete a workset
        /// so this file also sets up all the questions and
        /// questionset information and it then saves this.
        /// </summary>
        /// <param name="WorkSetID"></param>
        /// <returns></returns>
        public IActionResult Index(int WorkSetID)
        {
            int uid = UserHelper.GetUserId(HttpContext.Session);

            // Checks whether the user can access and therefore complete the workset
            // If they can set it up.
            if (UserHelper.UserHasBasicAccess(uid, WorkSetID))
            {
                var set = DatabaseConnector.GetWorkset(WorkSetID);

                if (set == null)
                {
                    return(RedirectToAction("Index", "Work"));
                }

                var work = DatabaseConnector.GetWhere <Work>($"WorkSetID={set.WorksetID}");

                // Shuffle the work if it is supposed to be in a random order.
                if (set.RandomOrdering)
                {
                    work.Shuffle();
                }

                // Create the question set.
                QuestionSets qSet = new QuestionSets()
                {
                    UserID     = uid,
                    WorkSetID  = WorkSetID,
                    Date_Asked = DateTime.Today
                };

                // Save to the datbase and get the ID of the question set
                qSet.QuestionSetID = DatabaseConnector.AddQuestionSet(qSet);

                List <QuestionViewModel> Qusts = new List <QuestionViewModel>();

                // Go through each piece of required work and update and save it.
                foreach (var piece in work)
                {
                    // Generate the seed for the question
                    var seed = StoredQuestion.GenerateSeed(piece.Seed);

                    // Create the question and save it to the database.
                    Questions qust = new Questions()
                    {
                        QuestionSetID = qSet.QuestionSetID,
                        Question_Type = piece.QuestionType,
                        Difficulty    = (int)piece.Difficulty,
                        Seed          = seed,
                        AnswerCorrect = 0
                    };
                    qust.QuestionID = DatabaseConnector.AddQuestion(qust);

                    // Gets the question informataion from the databse (the python file) and passes it into the pythond interpreter.
                    var curQuest = Interpreter.GenerateQuestion(AppContext.BaseDirectory + @"wwwroot\lib\Python\"
                                                                + DatabaseConnector.Get <QuestionTypes>().SingleOrDefault(x => x.TypeID == qust.Question_Type).Class, seed);

                    // From the scriptOuput get the names of the boxes and the hints for them
                    var boxes = curQuest.Boxes().Split(',');
                    var Hints = new List <string>();
                    Hints.AddRange(curQuest.Hints.Split(','));

                    //If we don't int for each box add some blank ones.
                    for (int i = Hints.Count - 1; i < boxes.Length; i++)
                    {
                        Hints.Add("");
                    }

                    // Create the question view model for the question set
                    var questionSet = new QuestionViewModel()
                    {
                        questionID = qust.QuestionID,
                        question   = curQuest,
                        answer     = new string[boxes.Length],
                        correct    = new int[boxes.Length],
                        Hints      = Hints.ToArray(),
                        Boxes      = boxes
                    };
                    //Add it to the list of questions.
                    Qusts.Add(questionSet);

                    // Add the answer into the Http session to get the
                    HttpContext.Session.Set("Q" + qust.QuestionID, Encoding.UTF8.GetBytes(questionSet.question.GetAnswer().ToString()));
                }
                // Return the entire question set
                return(View(new QuestionSetViewModel()
                {
                    QuestionSetID = qSet.QuestionSetID,
                    PerQuestion = (set.SetType == 1),
                    questions = Qusts.ToArray()
                }));
            }
            return(Unauthorized());
        }