public ActionResult CreateProblem(string id) { ProblemsModel model = new ProblemsModel(); try { Common common = new Common(); UnitOfWork uow = new UnitOfWork(); List <TeacherSubject> subjects = uow.TeacherRepository.GetSubjects(id); model.Subjects = new SelectList(subjects, "SubjectID", "SubjectName"); model.Problems = uow.UserRepository.GetProblemsByStudentId(Session["UserId"].ToString()); model.SessionTypes = GetSessionTypess(); model.DurationHourList = new SelectList(common.GetDurationHours()); model.DurationMinuteList = new SelectList(common.GetMinutes()); model.TeacherID = id; if (!string.IsNullOrEmpty(id)) { User selectedTeacher = uow.Users.GetByID(id); ViewBag.TeacherName = selectedTeacher.FirstName + " " + selectedTeacher.LastName; } return(View(model)); } catch (Exception) { ModelState.AddModelError("error", Resources.Resources.MsgErrorTryAgain); return(View(model)); } }
public ActionResult Problems() { UnitOfWork uow = new UnitOfWork(); ProblemsModel model = new ProblemsModel(); model.Problems = uow.UserRepository.GetProblemsByStudentId(Session["UserId"].ToString()); return(View(model)); }
public ActionResult Problems(ProblemsModel model, HttpPostedFileBase file) { UnitOfWork uow = new UnitOfWork(); model.Subjects = new SelectList(uow.Subjects.Get(), "SubjectID", "SubjectName"); model.SessionTypes = GetSessionTypess(); if (!ModelState.IsValid) { model.Problems = uow.UserRepository.GetProblemsByStudentId(Session["UserId"].ToString()); ModelState.AddModelError("error", Resources.Resources.InvalidInfo); return(View(model)); } string fileName = null; if (file != null) { fileName = Guid.NewGuid().ToString() + Path.GetFileName(file.FileName); string path = Path.Combine(Server.MapPath("~/Content/images/"), fileName); file.SaveAs(path); //byte[] bytes; //using (BinaryReader br = new BinaryReader(file.InputStream)) //{ // bytes = br.ReadBytes(file.ContentLength); //} //fileName = bytes; } StudentProblem problem = new StudentProblem { ProblemID = Guid.NewGuid().ToString(), StudentID = Session["UserId"].ToString(), SubjectID = model.Subject, CreationDate = DateTime.Now, Description = model.ProblemDescription, HoursNeeded = model.HoursNeeded, Type = model.Type, FileName = fileName, ExpireDate = DateTime.ParseExact(model.DeadLine, "dd/MM/yyyy", CultureInfo.InvariantCulture) // need to add datetime datepicker }; uow.StudentProblems.Insert(problem); uow.Save(); model.Problems = uow.UserRepository.GetProblemsByStudentId(Session["UserId"].ToString()); ModelState.AddModelError("success", Resources.Resources.MsgProblemSubmitedSuccessfully); return(View(model)); }
public ActionResult CreateProblem(ProblemsModel model, List <HttpPostedFileBase> files) { Common common = new Common(); UnitOfWork uow = new UnitOfWork(); List <TeacherSubject> subjects = uow.TeacherRepository.GetSubjects(model.TeacherID); model.Subjects = new SelectList(uow.Subjects.Get(), "SubjectID", "SubjectName"); model.SessionTypes = GetSessionTypess(); model.DurationHourList = new SelectList(common.GetDurationHours()); model.DurationMinuteList = new SelectList(common.GetMinutes()); if (Convert.ToInt32(model.DurationHour) < 1 && Convert.ToInt32(model.DurationMinutes) < 30) { ModelState.AddModelError("classtime-error", Resources.Resources.MsgClassDurationError); return(View(model)); } else if (!ModelState.IsValid) { if (!string.IsNullOrEmpty(model.TeacherID)) { User selectedTeacher = uow.Users.GetByID(model.TeacherID); ViewBag.TeacherName = selectedTeacher.FirstName + " " + selectedTeacher.LastName; } //ModelState.AddModelError("error", Resources.Resources.InvalidInfo); return(View(model)); } string userId = Session["UserId"].ToString(); if (Common.UserHasCredits(userId, model.HoursNeeded)) { decimal classDuration = Math.Round(Convert.ToInt16(model.DurationHour) + (Convert.ToInt16(model.DurationMinutes) / 60m), 2); StudentProblem problem = new StudentProblem { ProblemID = Guid.NewGuid().ToString(), StudentID = Session["UserId"].ToString(), SubjectID = model.Subject, CreationDate = DateTime.Now, Description = model.ProblemDescription, HoursNeeded = classDuration, Type = model.Type, //FileName = fileName, TeacherID = model.TeacherID, Status = model.TeacherID != null ? (int)ProblemStatus.TeacherSelected : (int)ProblemStatus.Created, ExpireDate = DateTime.ParseExact(model.DeadLine, "dd/MM/yyyy", CultureInfo.InvariantCulture) // need to add datetime datepicker }; uow.StudentProblems.Insert(problem); //save problem files(s) in uploads folder if (files != null && files.Count > 0) { foreach (var file in files) { if (file != null) { string fileName = null; //fileName = Guid.NewGuid().ToString() + Path.GetFileName(file.FileName); fileName = Path.GetFileName(file.FileName); string path = Path.Combine(Server.MapPath("~/Uploads/QuestionFiles/"), fileName); if (System.IO.File.Exists(path)) { string[] fileNameSplit = fileName.Split('.'); if (fileNameSplit.Count() > 1) { fileName = fileName.Split('.')[0] + "_2." + fileName.Split('.')[1]; } else { fileName = fileName + "_2"; } path = Path.Combine(Server.MapPath("~/Uploads/QuestionFiles/"), fileName); } file.SaveAs(path); //save problem files(s) in database StudentProblemFile problemFile = new StudentProblemFile { ProblemID = problem.ProblemID, FileName = fileName, CreationDate = DateTime.Now, UserID = userId }; uow.StudentProblemFiles.Insert(problemFile); } } } uow.Save(); //add notification if teacher is selected if (!string.IsNullOrEmpty(model.TeacherID)) { Common.AddNotification(Session["UserName"] + " asked you a question", "", Session["UserId"].ToString(), model.TeacherID, "/tutor/writeproposal?q=" + problem.ProblemID, (int)NotificationType.Question); } //end ModelState.AddModelError("success", Resources.Resources.MsgProblemSubmitedSuccessfully); return(View(model)); } else { model.SessionTypes = GetSessionTypess(); model.DurationMinuteList = new SelectList(common.GetMinutes()); model.DurationHourList = new SelectList(common.GetDurationHours()); model.Subjects = new SelectList(uow.Subjects.Get(), "SubjectID", "SubjectName"); ModelState.AddModelError("error", Resources.Resources.MsgNoBalance); return(View(model)); } }