public ActionResult FileUpload(int id)
        {
            // get groupID from form data JSON
            int courseID = id;
            int groupID = 0;
            string data = Request.Form["groupid"];
            Int32.TryParse(data, out groupID);

            // prep objects
            Guid uniqueID = Guid.NewGuid();
            string originalFilename = "";
            string originalExtension = "";
            string title = "";
            int fileID = 0;
            HttpPostedFileBase file = null;

            CourseFileAssociation cfa = null;
            CourseFile courseFile = null;

            // save file
            if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0) {
                file = Request.Files[0];

                originalFilename = Path.GetFileName(file.FileName);
                originalExtension = Path.GetExtension(file.FileName);
                title = originalFilename.Replace(originalExtension, ""); ;

                string filePath = Path.Combine(Settings.CourseFilesLocation, uniqueID.ToString() + originalExtension);
                file.SaveAs(filePath);

                courseFile = new CourseFile();
                courseFile.UserID = Didache.Users.GetLoggedInUser().UserID;
                courseFile.UniqueID = uniqueID;
                courseFile.ContentType = file.ContentType;
                courseFile.Length = file.ContentLength;
                courseFile.Filename = originalFilename;
                courseFile.UploadedDate = DateTime.Now;
                courseFile.Title = title;
                courseFile.Description = "";

                db.CourseFiles.Add(courseFile);
                db.SaveChanges();

                // create association
                cfa = new CourseFileAssociation();
                cfa.FileID = courseFile.FileID;
                cfa.GroupID = groupID;
                cfa.SortOrder = 999;
                cfa.DateAdded = DateTime.Now;

                db.CourseFileAssociations.Add(cfa);
                db.SaveChanges();

            }

            /*
            return Json(serializer.Serialize(new {
                success = (file!= null),
                cfa = cfa
            }));
            */

            return Json(serializer.Serialize(cfa));

            // do processing
            object returnObject = new {
                success = (file != null),
                fileid = fileID,
                filelength = (file != null) ? file.ContentLength : 0,
                filename = originalFilename,
                title = title,
                user = Didache.Users.GetLoggedInUser().FullName
            };

            return Json(returnObject);
        }
        public ActionResult UpdateFileSorting(int id)
        {
            var didacheDb = new DidacheDb();

            string data = HttpUtility.UrlDecode( Request.Form.ToString() );

            //dynamic newValue = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Dynamic.>(data);
            var reader = new JsonFx.Json.JsonReader();
            dynamic output = reader.Read(data);

            // get groups
            List<CourseFileGroup> groups = db.CourseFileGroups.Where(cfg => cfg.CourseID == id).ToList();
            List<int> groupIDs = groups.Select(cfg => cfg.GroupID).ToList();
            // get files
            List<CourseFileAssociation> courseFiles = db.CourseFileAssociations.Where(cfa => groupIDs.Contains(cfa.GroupID)).ToList();

            foreach (var groupInfo in output) {
                CourseFileGroup fileGroup = groups.SingleOrDefault(cfg => cfg.GroupID == groupInfo.groupid);
                fileGroup.SortOrder = groupInfo.sortorder;

                foreach (var fileInfo in groupInfo.files) {
                    CourseFileAssociation currentCourseFile = courseFiles.SingleOrDefault(cfa => cfa.FileID == fileInfo.fileid);

                    // has the group changed?
                    if (currentCourseFile.GroupID != fileGroup.GroupID) {

                        // create a new one with the new group
                        CourseFileAssociation newCourseFile = new CourseFileAssociation() {
                            FileID = currentCourseFile.FileID,
                            GroupID = fileGroup.GroupID,
                            DateAdded = currentCourseFile.DateAdded,
                            SortOrder = fileInfo.sortorder,
                            IsActive = true
                        };

                        db.CourseFileAssociations.Remove(currentCourseFile);
                        db.CourseFileAssociations.Add(newCourseFile);

                    }
                    else {
                        // update current one
                        currentCourseFile.SortOrder = fileInfo.sortorder;
                    }

                }
            }
            db.SaveChanges();

            /*
            foreach (var groupInfo in output) {

                // get and update the group
                CourseFileGroup fileGroup = didacheDb.CourseFileGroups.Find(groupInfo.groupid);
                fileGroup.SortOrder = groupInfo.sortorder;
                //fileGroup.Name = groupInfo.name;

                // TEST 2: get all existing, update, remove missing

                // get exsiting files
                List<CourseFileAssociation> courseFiles = didacheDb.CourseFileAssociations.Where(cfa => cfa.GroupID == fileGroup.GroupID).ToList();

                foreach (var fileInfo in groupInfo.files) {
                    // find the file
                    CourseFileAssociation cfa = courseFiles.Find(c => c.FileID == fileInfo.fileid);
                    if (cfa != null) {
                        // update
                        cfa.SortOrder = fileInfo.sortorder;
                        // add to change list
                        //changedFiles.Add(cfa);
                        courseFiles.Remove(cfa);
                    } else {
                        cfa = new CourseFileAssociation();
                        cfa.GroupID = fileGroup.GroupID;
                        cfa.FileID = fileInfo.fileid;
                        cfa.SortOrder = fileInfo.sortorder;
                        didacheDb.CourseFileAssociations.Add(cfa);
                    }
                }

                // remove all remaining files
                foreach (CourseFileAssociation notUpdated in courseFiles) {
                    didacheDb.CourseFileAssociations.Remove(notUpdated);
                }

            }
            */

            didacheDb.SaveChanges();

            // need to deserialize this and update all the groups and files

            return Json(new { success= true});
        }
Esempio n. 3
0
        public static Course CloneCourse(int courseID, int sessionID, DateTime startDate, string courseCode, string section)
        {
            DidacheDb db = new DidacheDb();

            Course oldCourse = db.Courses
                                                .Include("Units.Tasks")
                                                .Include("CourseFileGroups.CourseFileAssociations")
                                                .SingleOrDefault(c => c.CourseID == courseID);

            int daysToShift = (startDate - oldCourse.StartDate).Days;

            Course newCourse = new Course() {
                SessionID = sessionID,
                CampusID = oldCourse.CampusID,
                IsActive = oldCourse.IsActive,
                CourseCode = !String.IsNullOrWhiteSpace(courseCode) ? courseCode : oldCourse.CourseCode,
                Name = oldCourse.Name,
                Section = !String.IsNullOrWhiteSpace(section) ? section : oldCourse.Section,
                StartDate = oldCourse.StartDate.AddDays(daysToShift),
                EndDate = oldCourse.EndDate.AddDays(daysToShift),
                Description = oldCourse.Description
            };

            db.Courses.Add(newCourse);
            db.SaveChanges();

            /*
            } catch (DbEntityValidationException dbEx) {
                string errors = "";

                foreach (var validationErrors in dbEx.EntityValidationErrors) {
                    foreach (var validationError in validationErrors.ValidationErrors) {
                        //System.Web.HttpContext.Current.Trace.Warn("Property: {0} Error: {1}", validationError.PropertyName, dbEx);
                        errors += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + "; ";
                    }
                }

                throw new Exception(errors);
            }
            */

            foreach (Unit oldUnit in oldCourse.Units) {
                Unit newUnit = new Unit() {
                    CourseID = newCourse.CourseID,
                    IsActive = oldUnit.IsActive,
                    SortOrder = oldUnit.SortOrder,
                    Name = oldUnit.Name,
                    StartDate = oldUnit.StartDate.AddDays(daysToShift),
                    EndDate = oldUnit.EndDate.AddDays(daysToShift),
                    Instructions = oldUnit.Instructions
                };

                db.Units.Add(newUnit);
                db.SaveChanges();

                Dictionary<int, int> taskMap = new Dictionary<int, int>();
                List<Task> newTasks = new List<Task>();

                foreach (Task oldTask in oldUnit.Tasks) {
                    Task newTask = new Task() {
                        CourseID = newUnit.CourseID,
                        UnitID = newUnit.UnitID,
                        IsActive = oldTask.IsActive,
                        SortOrder = oldTask.SortOrder,
                        Name = oldTask.Name,
                        DueDate = null,
                        FileTypesAllowed = oldTask.FileTypesAllowed,
                        InstructionsAvailableDate = null,
                        IsSkippable = oldTask.IsSkippable,
                        Priority = oldTask.Priority,
                        RelatedTaskID = oldTask.RelatedTaskID,
                        SubmissionAvailableDate = null,
                        TaskID = oldTask.TaskID,
                        TaskTypeName = oldTask.TaskTypeName,
                        Instructions = oldTask.Instructions,
                        CustomAttributes = oldTask.CustomAttributes
                    };

                    if (oldTask.DueDate.HasValue)
                        newTask.DueDate = oldTask.DueDate.Value.AddDays(daysToShift);

                    if (oldTask.SubmissionAvailableDate.HasValue)
                        newTask.SubmissionAvailableDate = oldTask.SubmissionAvailableDate.Value.AddDays(daysToShift);

                    if (oldTask.InstructionsAvailableDate.HasValue)
                        newTask.InstructionsAvailableDate = oldTask.InstructionsAvailableDate.Value.AddDays(daysToShift);

                    db.Tasks.Add(newTask);

                    db.SaveChanges();

                    // store to remap the tasks below
                    newTasks.Add(newTask);
                    taskMap.Add(oldTask.TaskID, newTask.TaskID);
                }

                // go back and remap the related tasks
                List<int> newTaskIds = taskMap.Values.ToList();

                foreach(Task newTask in newTasks) {
                    if (newTask.RelatedTaskID > 0 && taskMap.ContainsKey(newTask.RelatedTaskID)) {
                        newTask.RelatedTaskID = taskMap[newTask.RelatedTaskID];
                    }
                }

                db.SaveChanges();
            }

            // FILES
            foreach (CourseFileGroup oldGroup in oldCourse.CourseFileGroups) {
                CourseFileGroup newGroup = new CourseFileGroup() {
                    CourseID = newCourse.CourseID,
                    Name = oldGroup.Name,
                    SortOrder = oldGroup.SortOrder
                };

                db.CourseFileGroups.Add(newGroup);
                db.SaveChanges();

                foreach (CourseFileAssociation oldFile in oldGroup.CourseFileAssociations) {
                    CourseFileAssociation newFile = new CourseFileAssociation() {
                        GroupID = newGroup.GroupID,
                        FileID = oldFile.FileID,
                        DateAdded = newCourse.StartDate,
                        IsActive = oldFile.IsActive,
                        SortOrder = oldFile.SortOrder
                    };

                    db.CourseFileAssociations.Add(newFile);
                }

                db.SaveChanges();

            }

            return newCourse;
        }