Пример #1
0
        /// <summary>
        /// Gets all of the possible questions
        /// and question types that could be asked to the
        /// user by collecting and setting up all that are on the database.
        /// </summary>
        /// <returns></returns>
        public CatagoryTypes[] GetAllQuestions()
        {
            // Gets the list of all the catagory types and iterates through them
            List <CatagoryTypes> catagoryTypes = new List <CatagoryTypes>();

            CatagoryType[] catTypes = DatabaseConnector.Get <CatagoryType>();
            foreach (var CatType in catTypes)
            {
                // gets the list of all the catagories for that type
                List <LengthCatagory> lengthCatagories = new List <LengthCatagory>();
                var catagories = DatabaseConnector.GetWhere <Catagory>($"CatagoryType={CatType.CatTypeID}");
                foreach (var Cat in catagories)
                {
                    // Get all of the question types for that catagory
                    List <TypedWork> works = new List <TypedWork>();
                    var qtypes             = DatabaseConnector.GetWhere <QuestionTypes>($"Catagory={Cat.CatagoryID}").ToArray();
                    int seed = 0;
                    foreach (var item in qtypes)
                    {
                        // Gets all of the work for that item
                        WorkPartial[] possibleWork = new WorkPartial[5];
                        for (int i = 0; i < 5; i++)
                        {
                            // Generates a random piece of work for that question type to show the user.
                            seed = StoredQuestion.GenerateSeed();
                            var work = Interpreter.GenerateQuestion(AppContext.BaseDirectory + @"wwwroot\lib\Python\" + item.Class, 0);

                            possibleWork[i] = new WorkPartial()
                            {
                                Seed   = seed,
                                Answer = work.GetAnswer().ToString()
                            };
                        }
                        // Adds all of the work to list of question types
                        works.Add(new TypedWork()
                        {
                            WorkType     = item.Type_Name,
                            TypeID       = item.TypeID,
                            PossibleWork = possibleWork.ToArray()
                        });
                    }
                    // adds that catagory and the question types in it to the list of catagorys
                    lengthCatagories.Add(new LengthCatagory()
                    {
                        catagoryName = Cat.CatagoryName,
                        WorkTypes    = works.ToArray()
                    });
                }
                // adds the catagory type and its list of catagories to the returning list
                catagoryTypes.Add(new CatagoryTypes()
                {
                    CatTypeName = CatType.CatTypeName,
                    Catagories  = lengthCatagories.ToArray()
                });
            }
            // return the list of catagory types.
            return(catagoryTypes.ToArray());
        }
Пример #2
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());
        }