コード例 #1
0
        //load file for events
        string myfunc(string path, int evntid, string CourseName)
        {
            int                totaladded = 0;
            string             mpath      = HostingEnvironment.MapPath(path);
            StreamReader       sre        = new StreamReader(mpath);
            string             line;
            List <Question>    questions   = new List <Question>();
            List <OptionClass> optionclass = new List <OptionClass>();
            OptionClass        opclsobj    = new OptionClass();
            List <Option>      options     = new List <Option>();
            string             err         = "";

            string[] tags = { "@question", "@answer", "@option" }; int index = 0, lc = 0, count = 0;
            Question ques = new Question();
            Option   op   = new Option();

            while ((line = sre.ReadLine()) != null)
            {
                if (index == 2)
                {
                    int at = -1;
                    at = line.IndexOf(tags[index]);
                    if (at >= 0)
                    {
                        string[] arr = line.Split(new char[] { ':' });
                        op.OptionText = arr[1].Trim();
                        if (String.IsNullOrWhiteSpace(op.OptionText))
                        {
                            return(err = "Option error, at line " + lc);
                        }
                        if (op.OptionText == "NA")
                        {
                            return("Option/Answer can not be NA, at line " + lc);
                        }
                        options.Add(op);
                        op    = new Option();
                        count = 3;
                    }
                    at = line.IndexOf(tags[0]);
                    if (at >= 0)
                    {
                        index            = 0;
                        opclsobj.options = options;
                        options          = new List <Option>();
                        optionclass.Add(opclsobj);
                        opclsobj = new OptionClass();
                    }
                }

                string[] arr2 = line.Split(new char[] { ':' });
                if (arr2.Length > 1)
                {
                    string tmptag = arr2[0].Trim();
                    if (!(String.IsNullOrWhiteSpace(tmptag)))
                    {
                        if (tmptag != tags[index])
                        {
                            return(tags[index] + " expected, at line " + lc);
                        }
                    }
                }


                if (index == 0)
                {
                    int at = -1;
                    at = line.IndexOf(tags[index]);
                    if (at >= 0)
                    {
                        string[] arr = line.Split(new char[] { ':' });
                        ques.QuestionText = arr[1].Trim();
                        if (String.IsNullOrWhiteSpace(ques.QuestionText))
                        {
                            return(err = "Question error, at line " + lc);
                        }
                        count = 1;
                        index++;
                    }
                }
                if (index == 1)
                {
                    int at = -1;
                    at = line.IndexOf(tags[index]);
                    if (at >= 0)
                    {
                        string[] arr = line.Split(new char[] { ':' });
                        ques.Answer = arr[1].Trim();
                        if (String.IsNullOrWhiteSpace(ques.Answer))
                        {
                            return(err = "Answer error, at line " + lc);
                        }
                        ques.CourseName      = CourseName;
                        ques.TeacherUserName = Thread.CurrentPrincipal.Identity.Name;
                        questions.Add(ques);
                        ques  = new Question();
                        count = 2;
                        index++;
                    }
                }



                lc++;
            }
            sre.Close();
            if (count < 3)
            {
                return("Incomplete file");
            }
            else
            {
                opclsobj.options = options;
                options          = new List <Option>();
                optionclass.Add(opclsobj);
                opclsobj = new OptionClass();
                Question      questoadd = new Question();
                List <Option> optoadd   = new List <Option>();
                Course        cc        = new Course();
                Exam          ex        = new Exam();
                int           qid       = -1;
                for (int k = 0; k < questions.Count; k++)
                {
                    if (!quesrepo.CheckValidQuestion(questions[k], optionclass[k].options))
                    {
                        return("Failed!Incorrects options for Question no. " + (k + 1));
                    }
                }
                for (int i = 0; i < questions.Count; i++)
                {
                    questoadd = questions[i];
                    optoadd   = optionclass[i].options;

                    qid = quesrepo.CheckIdenticalQuestion(questoadd, optoadd);
                    if (qid == (-1))
                    {
                        quesrepo.Insert(questoadd);
                        int quesid = quesrepo.GetMaxId();

                        foreach (Option oop in optoadd)
                        {
                            oop.QuestionId = quesid;
                            oprepo.Insert(oop);
                        }
                        ex.CourseName = questoadd.CourseName;
                        ex.EvntId     = evntid;
                        ex.QuestionId = quesid;
                        exrepo.Insert(ex);
                        totaladded++;
                    }
                    else
                    {
                        questoadd.QuestionId = qid;
                        if (!exrepo.CheckIdenticalQuestion(questoadd, evntid))
                        {
                            ex.EvntId     = evntid;
                            ex.QuestionId = qid;
                            ex.CourseName = questoadd.CourseName;
                            exrepo.Insert(ex);
                            ex = new Exam();
                            totaladded++;
                        }
                    }
                }

                return("Questions Added : " + totaladded.ToString());
            }
        }