public TaskTypeResult ProcessFormCollection(int taskID, int userID, FormCollection collection, HttpRequestBase request)
        {
            DidacheDb db = new DidacheDb();

            Task task = db.Tasks.Find(taskID);
            UserTaskData data = db.UserTasks.SingleOrDefault(d => d.TaskID == taskID && d.UserID == userID);

            // CREATE POST
            InteractionThread thread = new InteractionThread();
            thread.UserID = userID;
            thread.TotalReplies = 0;
            thread.Subject = "Assignment: " + task.Name;
            thread.TaskID = taskID;
            thread.ThreadDate = DateTime.Now;
            db.InteractionThreads.Add(thread);
            db.SaveChanges();

            InteractionPost post = new InteractionPost();
            post.IsApproved = true;
            post.PostContent = request["usercomment"];
            post.PostContentFormatted = Interactions.FormatPost(request["usercomment"]);
            post.PostDate = DateTime.Now;
            post.ReplyToPostID = 0;
            post.ThreadID = thread.ThreadID;
            post.UserID = userID;
            post.Subject = "RE: Assignment: " + task.Name;
            post.TaskID = taskID;
            db.InteractionPosts.Add(post);
            db.SaveChanges();

            return new TaskTypeResult() { Success = true, UrlHash = "thread-" + thread.ThreadID };
        }
        public TaskTypeResult ProcessFormCollection(int taskID, int userID, FormCollection collection, HttpRequestBase request)
        {
            DidacheDb db = new DidacheDb();

            Task task = db.Tasks.Find(taskID);
            UserTaskData data = db.UserTasks.SingleOrDefault(d=> d.TaskID == taskID && d.UserID == userID);

            // find the related task to send this post to
            int interactionTaskID = task.RelatedTaskID;

            // nasty lookup
            if (interactionTaskID == 0) {
                List<Task> possibleMatches = db.Tasks
                                                    .Where(t => t.UnitID == task.UnitID && t.TaskTypeName == "CommentOnClassmatesFile")
                                                    .OrderBy(t => t.SortOrder)
                                                    .ToList();

                if (possibleMatches.Count > 0) {
                    interactionTaskID = possibleMatches[0].TaskID;
                }

            }

            // CREATE POST
            InteractionThread thread = new InteractionThread();
            thread.UserID = userID;
            thread.TotalReplies = 0;
            thread.Subject = "Assignment: " + task.Name;
            thread.TaskID = interactionTaskID;
            thread.ThreadDate = DateTime.Now;
            db.InteractionThreads.Add(thread);
            db.SaveChanges();

            InteractionPost post = new InteractionPost();
            post.IsApproved = true;
            post.PostContent = request["usercomment"];
            post.PostContentFormatted = Interactions.FormatPost(request["usercomment"]);
            post.PostDate = DateTime.Now;
            post.ReplyToPostID = 0;
            post.ThreadID = thread.ThreadID;
            post.UserID = userID;
            post.Subject = "RE: Assignment: " + task.Name;
            post.TaskID = interactionTaskID;
            db.InteractionPosts.Add(post);
            db.SaveChanges();

            // facilty and facilitators
            if (data != null) {
                data.PostID = post.PostID;
                data.StudentSubmitDate = DateTime.Now;
                data.TaskCompletionStatus = TaskCompletionStatus.Completed;

                db.SaveChanges();
            }

            return new TaskTypeResult() { Success = true, UrlHash = "thread-" + thread.ThreadID };
        }
        public TaskTypeResult ProcessFormCollection(int taskID, int userID, FormCollection collection, HttpRequestBase request)
        {
            DidacheDb db = new DidacheDb();

            Task task = db.Tasks.Find(taskID);
            UserTaskData data = db.UserTasks.SingleOrDefault(d=> d.TaskID == taskID && d.UserID == userID);

            StudentFile studentFile = null;

            // save file
            if (request.Files.Count > 0) {
                HttpPostedFileBase file = request.Files[0];

                /* What if this is a GTA?
                 * - Create a fake task for looking up and saving information
                 */
                UserTaskData dataForSaving = data;
                if (dataForSaving == null) {
                    dataForSaving = new UserTaskData() {
                        UserID = userID,
                        TaskID = taskID
                    };
                }

                studentFile = CourseFiles.SaveStudentFile(userID, dataForSaving, file);
            }

            if (studentFile == null) {
                return new TaskTypeResult() { Success = false, Message = "No file" };
            }
            else {

                // save this file, even if somethign messes up with the forums
                if (data != null) {
                    data.StudentFileID = studentFile.FileID;
                    db.SaveChanges();
                }

                int interactionTaskID = task.RelatedTaskID;

                // nasty lookup
                if (interactionTaskID == 0) {
                    List<Task> possibleMatches = db.Tasks
                                                        .Where(t => t.UnitID == task.UnitID && t.TaskTypeName == "CommentOnClassmatesFile")
                                                        .OrderBy(t => t.SortOrder)
                                                        .ToList();

                    if (possibleMatches.Count > 0) {
                        interactionTaskID = possibleMatches[0].TaskID;
                    }

                }

                //if (interactionTaskID == 0) {
                //	return new TaskTypeResult() { Success = false, Message = "No related task" };
                //}

                InteractionThread thread = null;
                InteractionPost post = null;
                bool isNewPost = true;

                // check for existing thread/post
                if (data != null && data.PostID > 0) {
                    post = db.InteractionPosts.SingleOrDefault(p => p.PostID == data.PostID);
                    if (post != null) {
                        thread = db.InteractionThreads.SingleOrDefault(t => t.ThreadID == post.ThreadID);

                        if (thread != null) {
                            isNewPost = false;
                        }
                    }
                }

                // CREATE POST
                if (isNewPost) {
                    thread = new InteractionThread();
                }
                thread.UserID = userID;
                thread.TotalReplies = 0;
                thread.IsDeleted = false;
                thread.Subject = "Assignment: " + task.Name;
                thread.TaskID = interactionTaskID;
                thread.ThreadDate = DateTime.Now;
                if (isNewPost) {
                    db.InteractionThreads.Add(thread);
                }
                db.SaveChanges();

                if (isNewPost) {
                    post = new InteractionPost();
                }
                post.IsApproved = true;
                post.IsDeleted = false;
                post.PostContent = request["usercomment"];
                post.PostContentFormatted = Interactions.FormatPost(request["usercomment"]);
                post.PostDate = DateTime.Now;
                post.ReplyToPostID = 0;
                post.ThreadID = thread.ThreadID;
                post.UserID = userID;
                post.Subject = "RE: Assignment: " + task.Name;
                post.TaskID = interactionTaskID;
                post.FileID = studentFile.FileID;
                if (isNewPost) {
                    db.InteractionPosts.Add(post);
                }
                db.SaveChanges();

                if (data != null) {
                    data.PostID = post.PostID;
                    data.StudentSubmitDate = DateTime.Now;
                    data.TaskCompletionStatus = TaskCompletionStatus.Completed;

                    db.SaveChanges();
                }

                return new TaskTypeResult() { Success = true, UrlHash = "thread-" + thread.ThreadID };
            }
        }