Example #1
0
        public static string FormatEmail(string text, Course course, Unit unit, Task task, User fromUser, User toUser, UserTaskData userTaskData = null, InteractionPost interactionPost = null, ForumPost forumPost = null, UserPost userPost = null, UserPostComment userPostComment = null, DiscussionGroup discussionGroup = null)
        {
            if (course != null) {
                text = text.Replace("{course-code}", course.CourseCode + course.Section);
                text = text.Replace("{course-name}", course.Name);
            }

            if (fromUser != null) {
                text = text.Replace("{fromuser-secureformattedname}", fromUser.SecureFormattedName);
                text = text.Replace("{fromuser-secureshortname}", fromUser.SecureShortName);
            }

            if (toUser != null) {
                text = text.Replace("{touser-secureformattedname}", toUser.SecureFormattedName);
                text = text.Replace("{touser-secureshortname}", toUser.SecureShortName);
            }

            if (task != null) {
                text = text.Replace("{task-name}", task.Name);
            }

            if (userTaskData != null) {
                text = text.Replace("{usertaskdata-numericgrade}", userTaskData.NumericGrade.HasValue ? userTaskData.NumericGrade.Value.ToString() : "(none)");
                text = text.Replace("{usertaskdata-studentcomments}", userTaskData.StudentComments);
                text = text.Replace("{usertaskdata-gradercomments}", userTaskData.GraderComments);
                text = text.Replace("{usertaskdata-gradedfile-url}", userTaskData.GradedFile != null ? "https://online.dts.edu" + userTaskData.GradedFile.FileUrl : "");
                text = text.Replace("{usertaskdata-studentfile-url}", userTaskData.StudentFile != null ? "https://online.dts.edu" + userTaskData.StudentFile.FileUrl : "");
            }

            if (interactionPost != null) {
                text = text.Replace("{interactionpost-postcontent}", interactionPost.PostContent);
                text = text.Replace("{interactionpost-posturl}", "https://online.dts.edu" + interactionPost.PostUrl);
            }

            // NOTE: using unformated text for now?!
            if (userPost != null) {
                text = text.Replace("{userpost-text}", userPost.Text);
                text = text.Replace("{userpost-posturl}", "https://online.dts.edu" + userPost.PostUrl);
            }

            if (discussionGroup != null) {
                text = text.Replace("{discussiongroup-name}", discussionGroup.Name);
                text = text.Replace("{discussiongroup-groupurl}", "https://online.dts.edu" + discussionGroup.GroupUrl);
            }

            if (userPostComment != null) {
                text = text.Replace("{postcomment-text}", userPostComment.Text);
            }

            return text;
        }
Example #2
0
        public ActionResult CreateUserPost(string text, string postType, bool notifyCourse, int? id)
        {
            User user = Users.GetLoggedInUser();
            UserPostType userPostType = (UserPostType)Enum.Parse(typeof(UserPostType), postType);

            int courseID = (userPostType == UserPostType.Course ? id.Value : 0);
            int groupID = (userPostType == UserPostType.CourseGroup ? id.Value : 0);
            int discussionGroupID = (userPostType == UserPostType.DiscussionGroup ? id.Value : 0);
            int userGroupID = 0;

            if (groupID > 0) {
                try {
                    courseID = db.CourseUserGroups.Find(groupID).CourseID;
                } catch { }
            }

            UserPost p = new UserPost() {
                UserID = user.UserID,
                PostDate = DateTime.Now,
                IsDeleted = false,
                IsPinned = false,
                CourseGroupID = groupID,
                UserGroupID = userGroupID,

                CourseID = courseID,
                DiscussionGroupID = discussionGroupID,

                FileID = 0,
                UserPostType = userPostType,
                Text = text,
                TextFormatted = Markdown.Transform(text)
            };

            db.UserPosts.Add(p);
            db.SaveChanges();

            // get the data back out
            p = db.UserPosts
                        .Include("User")
                        .Include("PostComments.User")
                        .Include("Course")
                        .Include("DiscussionGroup")
                        .Single(up => up.PostID == p.PostID);

            // user action
            db.UserActions.Add(new UserAction() {
                SourceUserID = user.UserID,
                ActionDate = DateTime.Now,
                UserActionType = UserActionType.MakeNewPost,
                PostID = p.PostID,
                Text = p.TextFormatted,
                GroupID = 0,
                MessageID = 0,
                PostCommentID = 0,
                TargetUserID = 0,
                DiscussionGroupID = discussionGroupID
            });
            db.SaveChanges();

            // send emails
            if (notifyCourse && (userPostType == UserPostType.CourseGroup || userPostType == UserPostType.Course)) {

                List<User> usersToBeNotified = null;
                string title = "";

                if (userPostType == UserPostType.Course) {
                    usersToBeNotified = Courses.GetUsersInCourse(courseID)
                                            .Select(cu => cu.User)
                                            .Distinct()
                                            .ToList();

                    Course course = db.Courses.Find(courseID);

                    title = course.CourseCode + course.Section + " Announcement";

                } else if (userPostType == UserPostType.CourseGroup) {
                    usersToBeNotified = Courses.GetUsersInCourse(courseID)
                                            .Where(cu => cu.GroupID == groupID)
                                            .Select(cu => cu.User)
                                            .Distinct()
                                            .ToList();

                    Course course = db.Courses.Find(courseID);
                    CourseUserGroup group = db.CourseUserGroups.Find(groupID);

                    title = course.CourseCode + course.Section + " " + group.Name + " Announcement";
                }
                //Course course

                if (usersToBeNotified != null) {
                    foreach (User userToBeNotified in usersToBeNotified) {
                        Emails.EnqueueEmail(
                            "*****@*****.**",
                            userToBeNotified.Email,
                            title,
                            Emails.FormatEmail(Didache.Resources.emails.userpost_announcement, null, null, null, user, userToBeNotified, null, null, null, p, null),
                            false);
                    }
                }

            }

            // send to group subscribers
            if (userPostType == UserPostType.DiscussionGroup) {

                DiscussionGroup group = db.DiscussionGroups.Find(discussionGroupID);

                // get subscribers
                List<User> subscribers = db.DiscussionGroupMembers
                                                .Include("User")
                                                .Where(m => m.GroupID == discussionGroupID)
                                                .Select(m => m.User)
                                                .ToList();

                foreach (User subscriber in subscribers) {
                    Emails.EnqueueEmail(
                        "*****@*****.**",
                        subscriber.Email,
                        group.Name  + " Message",
                        Emails.FormatEmail(Didache.Resources.emails.discussiongroup_message, null, null, null, user, subscriber, null, null, null, p, null, group),
                        false);
                }

            }

            return Json(new {
                PostID = p.PostID,
                PostDate = p.PostDate,
                PostDateFormatted = p.PostDate.ToString("MMM d"),
                //Text = Regex.Replace(p.TextFormatted, "</?(div|font|span|style|script|img).*?>", ""),
                Text = p.TextFormatted,
                UserPostTypeFormatted = p.UserPostTypeFormatted,
                UserPostTypeUrl = p.UserPostTypeUrl,
                User = new {
                    SecureFormattedName = p.User.SecureFormattedName,
                    ProfileDisplayUrl = p.User.ProfileDisplayUrl,
                    ProfileImageUrl = p.User.ProfileImageUrl
                },
                PostComments = p.PostComments.Select(c => new {
                    PostCommentID = c.PostCommentID,
                    CommentDate = c.CommentDate,
                    CommentDateFormatted = c.CommentDate.ToString("MMM d, hh:mm tt"),
                    //Text = Regex.Replace(c.TextFormatted, "</?(div|font|span|style|script|img).*?>", ""),
                    Text = p.TextFormatted,
                    User = new {
                        SecureFormattedName = c.User.SecureFormattedName,
                        ProfileDisplayUrl = c.User.ProfileDisplayUrl,
                        ProfileImageUrl = c.User.ProfileImageUrl
                    }
                })
            });
        }