public static Document FindAssignment(User user, Activity activity, ApplicationDbContext db, HttpServerUtilityBase server) { string folder = server.MapPath("~/documents/ovning/" + activity.Id + "/" + user.Id + "/"); string name = "Inlämning för " + user.FirstName + " " + user.LastName; return FindDocument(folder, name, db); }
public ActionResult CreateActivity(CreateActivityViewModel model, int? id) { Module module = db.Modules.FirstOrDefault(c => c.Id == model.ModuleId); ViewBag.AtEarliest = (module != null ? (DateTime.Today.AddDays(1) > module.StartDate ? DateTime.Today.AddDays(1) : module.StartDate) : DateTime.Today.AddDays(1)); ViewBag.AtLatest = (module != null ? (DateTime.Today.AddDays(2) < module.EndDate ? module.EndDate : DateTime.Today.AddDays(2)) : DateTime.Today.AddDays(2)); Menu(Home: true); SetBreadcrumbs( one: new MenyItem { Link = "~/Teacher/", Text = "Se alla kurser" }, three: new MenyItem { Link = "~/Teacher/Module/" + id, Text = "Tillbaka till modul" }); if (!ModelState.IsValid) { model.ModuleId = (id == null ? 0 : id); model.Type = null; // FetchAllModules(); //Anrop till metoden FetchAllModules. return View(model); } model.StartDate = new DateTime(model.StartDate.Year, model.StartDate.Month, model.StartDate.Day, 0, 0, 0); model.EndDate = new DateTime(model.EndDate.Year, model.EndDate.Month, model.EndDate.Day, 23, 59, 0); if (model.Deadline != null) { model.Deadline = new DateTime(((DateTime)model.Deadline).Year, ((DateTime)model.Deadline).Month, ((DateTime)model.Deadline).Day, 23, 59, 0); } bool hasError = false; if (model.StartDate != null && model.StartDate < DateTime.Today.AddDays(1)) { ModelState.AddModelError("StartDate", "Startdatum kan tyvärr ej starta innan morgondagen, pga. planeringstid"); hasError = true; } if (model.EndDate != null && model.StartDate != null && model.EndDate < model.StartDate) { ModelState.AddModelError("EndDate", "Slutdatumet kan ej vara innan startdatumet"); hasError = true; } if (model.EndDate != null && model.EndDate < DateTime.Today.AddDays(1)) { ModelState.AddModelError("EndDate", "Slutdatum kan ej vara innan morgondagen"); hasError = true; } if (model.Deadline != null && model.StartDate != null && model.Deadline < model.StartDate) { ModelState.AddModelError("Deadline", "Deadline för övningsuppgift kan ej vara innan startdatumet"); hasError = true; } if (model.Deadline != null && model.EndDate != null && model.Deadline > model.EndDate) { ModelState.AddModelError("Deadline", "Deadline för övningsuppgift kan ej vara efter slutdatumet"); hasError = true; } if (model.Deadline != null && model.Deadline < DateTime.Today.AddDays(1)) { ModelState.AddModelError("Deadline", "Deadline för övningsuppgift kan ej vara innan morgondagen"); hasError = true; } if (module == null) { ModelState.AddModelError("", "Modulen kan ej hittas"); hasError = true; } else { if (model.StartDate != null && model.StartDate < module.StartDate) { ModelState.AddModelError("StartDate", "Startdatum kan ej starta innan modulen"); hasError = true; } if (model.EndDate != null && model.EndDate > module.EndDate) { ModelState.AddModelError("EndDate", "Slutdatum kan ej sluta efter modulen"); hasError = true; } if (model.Deadline != null && model.Deadline < module.StartDate) { ModelState.AddModelError("Deadline", "Deadline för övningsuppgift kan ej vara innan modulens startdatum"); hasError = true; } if (model.Deadline != null && model.Deadline > module.EndDate) { ModelState.AddModelError("Deadline", "Deadline för övningsuppgift kan ej vara efter modulens slutdatumet"); hasError = true; } } if (hasError) { model.ModuleId = (id == null ? 0 : id); ; model.Type = null; // FetchAllModules(); return View(model); } Activity activity = new Activity { Name = model.Name, Description = (model.Description != null ? model.Description : ""), StartDate = model.StartDate, EndDate = model.EndDate, ModuleId = model.ModuleId, Deadline = model.Deadline, Type = model.Type }; db.Activities.Add(activity); db.SaveChanges(); return Redirect("~/Teacher/Activity/" + activity.Id); //Skickar vidare till vy med information om den aktivitet som vi just har skapat. }