public ActionResult Upload(Problem problem, HttpPostedFileBase filebase) { if (filebase != null) { if (filebase.ContentLength > 0) { //TODO: đọc từ file cấu hình problem.LecturerID = User.Identity.Name; problem.Name = Path.GetFileNameWithoutExtension(filebase.FileName); problem.MemoryLimit = 1000; problem.TimeLimit = 1000; if (problem.ContestID == 0) { problem.ContestID = null; } db.Problems.AddObject(problem); try { Unzipfile(problem, filebase.InputStream); if (problem.ClassID != null) { foreach (String s in problem.ClassID) { foreach (Class c in db.Classes) { if (c.ID == int.Parse(s)) { problem.Classes.Add(c); problem.SubjectID = c.SubjectID; } } } } db.SaveChanges(); if ((problem.ContestID == null || problem.ContestID == 0) && problem.Classes.Count() == 0) return RedirectToAction("Browse", "Problem", new { ID = problem.SubjectID }); if (problem.Classes.Count() > 0) return RedirectToAction("ClassDetailOfLecturer", "Class", new { ID = problem.Classes.Last().ID }); else return RedirectToAction("CreateContestCont", "Contest", new { ContestID = problem.ContestID });//TODO:Sua EXAM } catch (Exception ex) { //throw (ex); LogUtility.WriteLog(ex); ViewBag.ErrorMessage = "Có lỗi trong khi upload đề bài"; } } } //TODO: redirect show problem ViewBag.SubjectID = new SelectList(ProblemRep.GetListSubjectByLecturerID(User.Identity.Name), "ID", "Name", problem.SubjectID); //ViewBag.Message = "Thông tin không đúng"; return View(problem); }
public string Unzipfile(Problem problem, Stream inputStream) { Dictionary<string, TestCas> dTestcase = new Dictionary<string, TestCas>(); ZipInputStream s = new ZipInputStream(inputStream); ZipEntry ZEntry; string sFileName = ""; String configXML = null; while ((ZEntry = s.GetNextEntry()) != null) { string directoryName = ""; directoryName = Path.GetDirectoryName(ZEntry.Name); if (directoryName != "" && directoryName != null && !dTestcase.ContainsKey(directoryName)) { TestCas tcTemp = new TestCas(); tcTemp.MaDB = problem.ID; dTestcase.Add(directoryName, tcTemp); db.TestCases.AddObject(tcTemp); problem.TestCases.Add(tcTemp); } sFileName = Path.GetFileName(ZEntry.Name); if (sFileName == String.Empty) continue; #region Doc file entry int size = 2048; using (MemoryStream memStream = new MemoryStream(size)) { byte[] data = new byte[size]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { memStream.Write(data, 0, size); } else { break; } } memStream.Position = 0; switch (Path.GetExtension(sFileName).ToLower()) { case ".inp": using (StreamReader docFile = new System.IO.StreamReader(memStream)) { TestCas tc = dTestcase[directoryName]; tc.Input = docFile.ReadToEnd(); docFile.Close(); } break; case ".out": using (StreamReader docFile = new System.IO.StreamReader(memStream)) { TestCas tc = dTestcase[directoryName]; tc.Output = docFile.ReadToEnd(); docFile.Close(); } break; case ".xml": //Doc file cau hinh if (Path.GetFileNameWithoutExtension(sFileName).ToLower() == problem.Name) using (StreamReader docFile = new System.IO.StreamReader(memStream)) { configXML = docFile.ReadToEnd(); docFile.Close(); } break; case ".doc": case ".docx": case ".pdf": byte[] problemfile = memStream.ToArray(); SPKTOnline.Models.File file = new SPKTOnline.Models.File(); file.Content = problemfile; file.Type = Path.GetExtension(sFileName); file.Name = sFileName; file.DownloadCount = 0; db.Files.AddObject(file); problem.File = file; break; default: break; } } #endregion } //TODO: Gan du lieu Problem tu cau hinh xml double phanTramDiem = Math.Round(100.0 / problem.TestCases.Count, 2); foreach (TestCas tc in dTestcase.Values) { tc.Diem = phanTramDiem; } s.Close(); return sFileName; }
public ActionResult Upload(int? ContestID, int ClassID = 0) { //khi tạo problem từ contest thì ta có ContestID!=0 và ClassID!=0, if ContestID!=0 và ClassID==0 thì cho ClassID=Contest.ClassID //khi tạo problem từ Class thì contestID =0, ClassID !=0 //Khi tạo problem từ Bên ngoài. thì ContestID=null, ClassID=0 Problem pro = new Problem(); if (ClassID != 0) { if (ContestID == 0) { Class cl = db.Classes.FirstOrDefault(c => c.ID == ClassID); ViewBag.ClassID = new MultiSelectList(db.Classes.Where(c => c.LecturerID == User.Identity.Name), "ID", "ID", new String[] { "Mã lớp: " + ClassID.ToString()+" Môn: "+cl.Subject.Name+" Nhóm: "+cl.Group+" "+cl.Term}); pro.Classes.Add(db.Classes.FirstOrDefault(c => c.ID == ClassID)); pro.SubjectID = db.Classes.FirstOrDefault(c => c.ID == ClassID).SubjectID; } else { pro.Classes.Add(db.Classes.FirstOrDefault(c => c.ID == ClassID)); pro.SubjectID = db.Classes.FirstOrDefault(c => c.ID == ClassID).SubjectID; } pro.ContestID = ContestID; } else { ViewBag.ClassID = new MultiSelectList(db.Classes.Where(c => c.LecturerID == User.Identity.Name), "ID", "Name"); ViewBag.SubjectID = new SelectList(ProblemRep.GetListSubjectByLecturerID(User.Identity.Name), "ID", "Name", ProblemRep.GetListSubjectByLecturerID(User.Identity.Name).First()); } ViewBag.DifficultyID = new SelectList(db.Difficulties, "DifficultyID", "Name",db.Difficulties.First()); ViewBag.ComparerID = new SelectList(db.Comparers, "ID", "Name", db.Comparers.First()); pro.AvailableTime = DateTime.Now; pro.Score = 10; return View(pro); //} }
/// <summary> /// Deprecated Method for adding a new object to the Problems EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToProblems(Problem problem) { base.AddObject("Problems", problem); }
/// <summary> /// Create a new Problem object. /// </summary> /// <param name="id">Initial value of the ID property.</param> /// <param name="lecturerID">Initial value of the LecturerID property.</param> /// <param name="memoryLimit">Initial value of the MemoryLimit property.</param> /// <param name="timeLimit">Initial value of the TimeLimit property.</param> /// <param name="comparerID">Initial value of the ComparerID property.</param> /// <param name="difficultyID">Initial value of the DifficultyID property.</param> /// <param name="isHiden">Initial value of the IsHiden property.</param> /// <param name="score">Initial value of the Score property.</param> /// <param name="subjectID">Initial value of the SubjectID property.</param> /// <param name="isDeleted">Initial value of the IsDeleted property.</param> public static Problem CreateProblem(global::System.Int32 id, global::System.String lecturerID, global::System.Int32 memoryLimit, global::System.Int32 timeLimit, global::System.Int32 comparerID, global::System.Int32 difficultyID, global::System.Boolean isHiden, global::System.Double score, global::System.String subjectID, global::System.Boolean isDeleted) { Problem problem = new Problem(); problem.ID = id; problem.LecturerID = lecturerID; problem.MemoryLimit = memoryLimit; problem.TimeLimit = timeLimit; problem.ComparerID = comparerID; problem.DifficultyID = difficultyID; problem.IsHiden = isHiden; problem.Score = score; problem.SubjectID = subjectID; problem.IsDeleted = isDeleted; return problem; }
public ActionResult Details(int ID) { Session["CurrentUrl"] = Request.Url.ToString(); if (ID > 0) { Problem p = new Problem(); foreach (Problem pr in db.Problems) { if (pr.ID == ID) p = pr; } return View(p); } else return View("Index", "Home"); }
public ActionResult CreateProblem(AddProblemModels problemModel) { if (Request.IsAuthenticated) { if (checkRole.IsLecturer(HttpContext.User.Identity.Name)) { Problem problem = new Problem(); problem.Name = problemModel.Name; problem.Content = problemModel.Content; problem.IsHiden = problemModel.IsHiden; problem.DifficultyID = problemModel.DifficultyID; problem.LecturerID = HttpContext.User.Identity.Name; problem.SubjectID = problemModel.SubjectID; problem.ComparerID = problemModel.ComparerID; problem.MemoryLimit = problemModel.MemoryLimit; problem.TimeLimit = problemModel.TimeLimit; problem.SubjectID = problemModel.SubjectID; problem.Score = problem.Score; if (problemModel.ContestID > 0) { problem.ContestID = problemModel.ContestID; } else problem.ContestID = null; db.Problems.AddObject(problem); db.SaveChanges(); #region AddClass //String[] kqClass = problemModel.ClassID; //if (kqClass.Count() > 0) //{ // foreach (String s in kqClass) // { // foreach (Class c in db.Classes) // { // if (c.ID == int.Parse(s)) // { // problem.Classes.Add(c); // } // } // } //} #endregion db.SaveChanges(); ViewBag.DifficultyID = new SelectList(db.Difficulties, "DifficultyID", "Name", problemModel.DifficultyID); return RedirectToAction("CreateTestCase", "TestCase", new { ProblemID = problem.ID }); } else return RedirectToAction("Index", "Home"); } else return RedirectToAction("Logon", "Home"); }