Exemplo n.º 1
0
        /// <summary>
        /// If you want to edit a workset it has to be reloaded into the page
        /// this is what this function does
        /// it also then generates the models for that work.
        /// </summary>
        /// <param name="setID"></param>
        /// <returns></returns>
        public IActionResult Build(int setID)
        {
            // See
            if (setID == 0)
            {
                return(Unauthorized());
            }
            if (UserHelper.OwnsWorkset(UserHelper.GetUserId(HttpContext.Session), setID) ||
                (UserHelper.UserInRole(UserHelper.GetUserId(HttpContext.Session), UserHelper.ROLE_ADMIN) && UserHelper.InSameOrganisation(UserHelper.GetUserId(HttpContext.Session), setID)))
            {
                // Gets the workset information from the database
                var workset = DatabaseConnector.GetWorkset(setID);
                if (workset == null)
                {
                    return(NotFound());
                }

                //Sets up the array of work components
                List <WorkPartial> currentwork = new List <WorkPartial>();

                // Get the work already in this sheet from the database
                var current = DatabaseConnector.GetWhere <Work>($"WorkSetID={setID}");

                // Iterates through every piece of work currently avaliable.
                foreach (var piece in current)
                {
                    // Gets the information about the question.
                    var workPartial = new WorkPartial()
                    {
                        TypeName = DatabaseConnector.Get <QuestionTypes>().FirstOrDefault(x => x.TypeID == piece.QuestionType).Type_Name,
                        TypeID   = piece.QuestionType,
                        Seed     = piece.Seed,
                        Answer   = ""
                    };

                    // If the work is supposed to have a set answer do this
                    if (workset.ExamStyle)
                    {
                        workPartial.Answer = Interpreter.GenerateQuestion(AppContext.BaseDirectory + @"wwwroot\lib\Python\" + DatabaseConnector.Get <QuestionTypes>().SingleOrDefault(x => x.TypeID == piece.QuestionType).Class, piece.Seed).GetAnswer().ToString();
                    }
                    currentwork.Add(workPartial);
                }

                // Create the build model from all of the given information and pass it to the page.
                var model = new BuildViewModel()
                {
                    WorkSetID   = setID,
                    createdWork = new CreatedWork()
                    {
                        SelectFromList = DatabaseConnector.GetWorkset(setID).ExamStyle,
                        CatagoryTypes  = GetAllQuestions()
                    },
                    Work = currentwork.ToArray()
                };

                return(View(model));
            }
            return(Unauthorized());
        }
Exemplo n.º 2
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());
        }