/// <summary> /// Function that uses the compiler, teacher uploads .zip that has /// code to create new milestone for the students to solve. /// Optionally input file can also be uploaded. /// </summary> public MilestoneViewModel CompileMilestoneUpload(MilestoneViewModel model, HttpPostedFileBase ZipFile, HttpPostedFileBase TxtFile) { var TheUser = Ident.GetUser(model.UserModel.Name); SubmissionViewModel SVM = new SubmissionViewModel(); if (TxtFile != null) { // Reads the textfile into the model. string result = new StreamReader(TxtFile.InputStream).ReadToEnd(); SVM.Input = result; model.Input = result; } if (ZipFile != null) { // The directory the milestone .zip file will be saved in. string UserDirectory = UserDataDir + "\\" + TheUser.Id + "\\" + model.AssignmentID + "\\" + model.ID; // creates the directory. if (!Directory.Exists(UserDirectory)) { Directory.CreateDirectory(UserDirectory); } var fileName = Path.GetFileName(ZipFile.FileName); var path = UserDirectory + "\\" + fileName; // If the teacher uploads the same .zip file again for a specific milestone // then it will be replaced. try { if (File.Exists(path)) { File.Delete(path); } ZipFile.SaveAs(Path.GetFullPath(path)); } catch { // user probably pressed upload twice, nothing to worry about. } SVM.UserName = model.UserModel.Name; SVM.AssignmentID = model.AssignmentID; SVM.MilestoneID = model.ID; SVM.FilePath = fileName; CompilerService CS = new CompilerService(); // compiler service called. var compiled = CS.Compile(SVM); model.Output = compiled.Output; model.DrMemory = compiled.DrMemory; } return(model); }
/// <summary> /// A function that users the compiler when the student uploads assignment, /// very similar to the milestone compile function that the teacher uses. /// </summary> public SubmissionViewModel StudentCompile(MilestoneViewModel model, HttpPostedFileBase ZipFile) { var TheUser = Ident.GetUser(model.UserModel.Name); int Count = 1; var Compiled = new SubmissionViewModel(); var ToCompile = new SubmissionViewModel(); if (model.Output == null) { throw new Exception(); } if (ZipFile != null) { string UserDirectory = UserDataDir + "\\" + TheUser.Id + "\\" + model.AssignmentID + "\\" + model.ID + "\\" + Count; // with each submission we create a new folder with increasing number in the name while (Directory.Exists(UserDirectory)) { Count++; UserDirectory = UserDataDir + "\\" + TheUser.Id + "\\" + model.AssignmentID + "\\" + model.ID + "\\" + Count; } Directory.CreateDirectory(UserDirectory); // we splice a couple of filepaths together to get the userdirectory and // zip directory so the compiler knows where to work. var fileName = Path.GetFileName(ZipFile.FileName); var path = UserDirectory + "\\" + fileName; string NewPath = UserDirectory + "\\" + model.UserModel.Name + "-" + model.ID + ".zip"; try { if (File.Exists(path)) { File.Delete(path); } // saves the zip in user storage ZipFile.SaveAs(Path.GetFullPath(path)); System.IO.File.Move(path, NewPath); fileName = model.UserModel.Name + "-" + model.ID + ".zip"; } catch { // user probably pressed upload twice, nothing to worry about. } ToCompile.UserName = model.UserModel.Name; ToCompile.AssignmentID = model.AssignmentID; ToCompile.MilestoneID = model.ID; ToCompile.FilePath = Count + "\\" + fileName; ToCompile.Input = model.Input; CompilerService CS = new CompilerService(); // compiler called. Compiled = CS.Compile(ToCompile); ToCompile.FilePath = "\\userdata\\" + TheUser.Id + "\\" + model.AssignmentID + "\\" + model.ID + "\\" + Count + "\\" + fileName; ToCompile.Error = Compiled.Error; ToCompile.Output = Compiled.Output; ToCompile.DrMemory = Compiled.DrMemory; ToCompile.Status = Compiled.Status; ToCompile.AssignmentName = AS.GetAssignmentByID(model.AssignmentID).Name; ToCompile.CourseID = model.CourseID; if (ToCompile.Output == null) { ToCompile.Output = "No output obtained"; } if (ToCompile.DrMemory == null) { ToCompile.DrMemory = "No memory file found"; } } return(ToCompile); }