Beispiel #1
0
        public void TestParse()
        {
            ExamModel model = new ExamModel();
            ExamEntity exam = model.praseFromDoc("doc/chapter1.docx");
            Assert.IsNotNull(exam);
            Assert.AreEqual(20, exam.time);
            Assert.AreEqual(2, exam.tNumber);
            Assert.AreEqual(2, exam.tScore);
            Assert.AreEqual(1, exam.sNumber);
            Assert.AreEqual(3, exam.sScore);
            Assert.AreEqual(0, exam.mNumber);
            Assert.AreEqual(4, exam.mScore);

            Assert.AreEqual(3, exam.tf.Count);
            Assert.AreEqual(1, exam.sc.Count);
            Assert.AreEqual(0, exam.mc.Count);

            QuestionEntity tf0 = exam.tf[0] as QuestionEntity;
            QuestionEntity tf1 = exam.tf[1] as QuestionEntity;
            QuestionEntity tf2 = exam.tf[2] as QuestionEntity;

            
            Assert.AreEqual(tf0.ans, "F");
            Assert.AreEqual(tf1.ans, "T");
            Assert.AreEqual(tf2.ans, "T");
            System.Console.WriteLine (tf0.statement);
            System.Console.WriteLine (tf1.statement);
            System.Console.WriteLine (tf2.statement);

            Assert.IsNull(tf0.image);
            Assert.IsNotNull(tf1.image);
            Assert.IsNull(tf2.image);
            

            


            QuestionEntity sc0 = exam.sc[0] as QuestionEntity;
            Assert.AreEqual(sc0.ans, "A");
            System.Console.WriteLine(sc0.statement);
            foreach(String choice in sc0.choices)
            {
                System.Console.WriteLine(choice);
            }

        }
Beispiel #2
0
        public ActionResult AddExam()
        {
            if (loginStatus() == false)
            {
                return Redirect("Login");
            }
            Dictionary<string,String> ret = new Dictionary<string,string>();
            HttpPostedFileBase file =  Request.Files.Get("doc");
            if (file != null)
            {
                if (checkFileExt(file.FileName))
                {
                    ExamModel examModel= new ExamModel();
                    QuestionModel questionModel = new QuestionModel();
                    var docName = "~/upload/tmp.docx";
                    var examname = "";
                    file.SaveAs(Server.MapPath(docName));
                    try
                    {
                        ExamEntity examEntity = examModel.praseFromDoc(Server.MapPath(docName));
                        if (file.FileName.LastIndexOf('\\') != -1)
                        {
                            examname = file.FileName.Substring(file.FileName.LastIndexOf('\\') + 1, file.FileName.Length - 1 - file.FileName.LastIndexOf('\\'));
                        }
                        else
                        {
                            examname = file.FileName;
                        }
                        int examId = examModel.addExam(examEntity.time, examEntity.sNumber, examEntity.mNumber, examEntity.tNumber, examEntity.sScore, examEntity.mScore, examEntity.tScore, 0, examname.Substring(0, examname.Length - 5));
                        int type = -1;
                        int count = 1;
                        foreach (QuestionEntity q in examEntity.sc)
                        {
                            
                            switch (q.type)
                            {
                                case "SC":
                                    type = 0;
                                    break;
                                case "MC":
                                    type = 1;
                                    break;
                                case "TF":
                                    type = 2;
                                    break;
                            }
                             var imageDir = "";
                             var imageName = "";
                             if (q.image != null)
                             {
                                 imageName = string.Format("/{0}.jpg", count);
                                 imageDir = string.Format("~/upload/{0}/sc", examId, count);
                                 if(!Directory.Exists(Server.MapPath(imageDir)))
                                 {
                                     Directory.CreateDirectory(Server.MapPath(imageDir));
                                 }
                                
                                 q.image.Save(Server.MapPath(imageDir+imageName));
                                 count = count + 1;
                             }
                           
                            var choiceJson = new JavaScriptSerializer().Serialize(q.choices);
                            questionModel.addQuesiton(type, q.ans, choiceJson, imageDir+imageName, q.statement, examId);
                        }
                        count = 1;
                        foreach (QuestionEntity q in examEntity.mc)
                        {

                            switch (q.type)
                            {
                                case "SC":
                                    type = 0;
                                    break;
                                case "MC":
                                    type = 1;
                                    break;
                                case "TF":
                                    type = 2;
                                    break;
                            }
                             var imageDir = "";
                             var imageName = "";
                             if (q.image != null)
                             {
                                 imageName = string.Format("/{0}.jpg",count);
                                 imageDir = string.Format("~/upload/{0}/mc", examId, count);
                                 if(!Directory.Exists(Server.MapPath(imageDir)))
                                 {
                                     Directory.CreateDirectory(Server.MapPath(imageDir));
                                 }
                                
                                 q.image.Save(Server.MapPath(imageDir+imageName));
                                 count = count + 1;
                             }
                            
                            var choiceJson = new JavaScriptSerializer().Serialize(q.choices);
                            questionModel.addQuesiton(type, q.ans, choiceJson, imageDir+imageName, q.statement, examId);
                        }
                        count = 1;
                        foreach (QuestionEntity q in examEntity.tf)
                        {

                            switch (q.type)
                            {
                                case "SC":
                                    type = 0;
                                    break;
                                case "MC":
                                    type = 1;
                                    break;
                                case "TF":
                                    type = 2;
                                    break;
                            }
                             var imageDir = "";
                             var imageName = "";
                             if (q.image != null)
                             {
                                 imageName = string.Format("/{0}.jpg", count);
                                 imageDir = string.Format("~/upload/{0}/tf", examId, count);
                                 if(!Directory.Exists(Server.MapPath(imageDir)))
                                 {
                                     Directory.CreateDirectory(Server.MapPath(imageDir));
                                 }
                                
                                 q.image.Save(Server.MapPath(imageDir+imageName));
                                 count = count + 1;
                             }
                            
                            var choiceJson = new JavaScriptSerializer().Serialize(q.choices);
                            questionModel.addQuesiton(type, q.ans, choiceJson, imageDir+imageName, q.statement, examId);
                        }
                    

                    }
                    catch(Exception e)
                    {
                        System.IO.File.Delete(Server.MapPath(docName));
                        ret.Add("status", "failed");
                        ret.Add("error", "parse doc error");
                        return Json(ret);
                    }
                    System.IO.File.Delete(Server.MapPath(docName));
                    ret.Add("status", "success");
                    ret.Add("filename", examname);
                    return Json(ret);
                }
                else
                {
                    
                    ret.Add("status", "failed");
                    ret.Add("error", "file type not allowed");
                    return Json(ret);
                }
            }
            else
            {
              
                ret.Add("status", "failed");
                ret.Add("error", "no files");
                return Json(ret);
            }
           
        }