コード例 #1
0
        // GET: Tickets/Create
        public ActionResult Create() {
            TicketsModel ticketsModel = new TicketsModel();
            if (ticketsModel.AssignedToUserId == null) {
                ViewBag.AssignedToUserId = "Not Assigned Yet";
            }
            else {
                ViewBag.AssignedToUserId = new SelectList(db.Users, "Id", "DisplayName");
            }
            ViewBag.OwnerUserId = new SelectList(db.Users, "Id", "DisplayName");

            ViewBag.ProjectId = new SelectList(db.ProjectsData, "Id", "Name");
            ViewBag.TicketPriorityId = new SelectList(db.TicketPrioritiesData, "Id", "Name");
            ViewBag.TicketStatusId = new SelectList(db.TicketStatusesData, "Id", "Name");
            ViewBag.TicketTypeId = new SelectList(db.TicketTypesData, "Id", "Name");
            ViewBag.TicketAttachmentId = new SelectList(db.AttachmentData, "Id", "FileUrl");
            return View(ticketsModel);
        }
コード例 #2
0
        public ActionResult Edit(TicketsModel ticketsModel) {

            if (ModelState.IsValid) {

                var userId = User.Identity.GetUserId();
                var user = db.Users.Find(userId);
                var dateChanged = System.DateTimeOffset.Now;
                var oldTicket = db.TicketsData.AsNoTracking().FirstOrDefault(t => t.Id == ticketsModel.Id);
                var assignedUser = db.Users.Find(ticketsModel.AssignedToUserId);

                //Title Notification
                if (oldTicket.Title != ticketsModel.Title) {
                    TicketHistoriesModel histTitle = new TicketHistoriesModel {
                        TicketId = ticketsModel.Id,
                        Property = "Title",
                        OldValue = oldTicket.Title,
                        NewValue = ticketsModel.Title,
                        ChangedDate = dateChanged,
                        UserId = userId
                    };
                    db.TicketHistoriesData.Add(histTitle);
                }

                //Ticket Title Description
                if (oldTicket.Description != ticketsModel.Description) {
                    TicketHistoriesModel histDescription = new TicketHistoriesModel {
                        TicketId = ticketsModel.Id,
                        Property = "Description",
                        OldValue = oldTicket.Description,
                        NewValue = ticketsModel.Description,
                        ChangedDate = dateChanged,
                        UserId = userId
                    };
                    db.TicketHistoriesData.Add(histDescription);
                }

                //Ticket Assigned To Notification
                if (oldTicket.AssignedToUserId != ticketsModel.AssignedToUserId) {
                    TicketHistoriesModel histAssignedTo = new TicketHistoriesModel {
                        TicketId = ticketsModel.Id,
                        Property = "Assigned To",
                        OldValue = db.Users.Find(oldTicket.AssignedToUserId).DisplayName,
                        NewValue = db.Users.Find(ticketsModel.AssignedToUserId).DisplayName,
                        ChangedDate = dateChanged,
                        UserId = userId
                    };
                    db.TicketHistoriesData.Add(histAssignedTo);

                    //Send Notification for Assigned To Change
                    TicketNotificationsModel ticketNotification = new TicketNotificationsModel {
                        TicketId = ticketsModel.Id,
                        UserId = User.Identity.GetUserId(),
                        Sender = user.Email,
                        Recipient = assignedUser.Email,
                        NotificationSubject = "Ticket \"" + ticketsModel.Title + "\" has been changed.",
                        NotificationMessage = "The Ticket <b>" + ticketsModel.Title + "</b> was reassigned to " + user.DisplayName + ".<br /><br />" +
                            "Previous Assignee: " + histAssignedTo.OldValue + "<br />" +
                            "New Assignee: " + histAssignedTo.NewValue + ".<br /><br />" +
                            "This change was made by " + user.DisplayName + ".",
                        SentDate = DateTimeOffset.Now
                    };
                    db.TicketNotificationsData.Add(ticketNotification);
                    assignedUser.SendNotification(ticketNotification.NotificationSubject, ticketNotification.NotificationMessage);
                }

                //Ticket Type Notification
                if (oldTicket.TicketTypeId != ticketsModel.TicketTypeId) {
                    TicketHistoriesModel histTicketType = new TicketHistoriesModel {
                        TicketId = ticketsModel.Id,
                        Property = "Ticket Type",
                        OldValue = db.TicketTypesData.Find(oldTicket.TicketTypeId).Name,
                        NewValue = db.TicketTypesData.Find(ticketsModel.TicketTypeId).Name,
                        ChangedDate = dateChanged,
                        UserId = userId
                    };
                    db.TicketHistoriesData.Add(histTicketType);
                }

                //Ticket Priority Notification
                if (oldTicket.TicketPriorityId != ticketsModel.TicketPriorityId) {

                    TicketHistoriesModel histPriority = new TicketHistoriesModel {
                        TicketId = ticketsModel.Id,
                        Property = "Ticket Priority",
                        OldValue = db.TicketPrioritiesData.Find(oldTicket.TicketPriorityId).Name,
                        NewValue = db.TicketPrioritiesData.Find(ticketsModel.TicketPriorityId).Name,
                        ChangedDate = dateChanged,
                        UserId = userId
                    };
                    db.TicketHistoriesData.Add(histPriority);

                    //Send Notification for Priority Change
                    TicketNotificationsModel ticketNotification = new TicketNotificationsModel {
                        TicketId = ticketsModel.Id,
                        UserId = User.Identity.GetUserId(),
                        Sender = user.Email,
                        Recipient = assignedUser.Email,
                        NotificationSubject = "The Priority for the Ticket titled \"" + ticketsModel.Title + "\" has been changed",
                        NotificationMessage = "The Ticket titled <b>" + ticketsModel.Title + "</b> has had its Priority changed. <br /><br />" +
                            "Previous Priority: " + histPriority.OldValue + "<br />" +
                            "New Priority: " + histPriority.NewValue + ".<br /><br />" +
                            "The change was made by " + user.DisplayName + ".",
                        SentDate = DateTimeOffset.Now
                    };
                    db.TicketNotificationsData.Add(ticketNotification);
                    assignedUser.SendNotification(ticketNotification.NotificationSubject, ticketNotification.NotificationMessage);
                }

                //Ticket Status Notification
                if (oldTicket.TicketStatusId != ticketsModel.TicketStatusId) {
                    TicketHistoriesModel histStatus = new TicketHistoriesModel {
                        TicketId = ticketsModel.Id,
                        Property = "Ticket Status",
                        OldValue = db.TicketStatusesData.Find(oldTicket.TicketStatusId).Name,
                        NewValue = db.TicketStatusesData.Find(ticketsModel.TicketStatusId).Name,
                        ChangedDate = dateChanged,
                        UserId = userId
                    };
                    db.TicketHistoriesData.Add(histStatus);

                    //Send Notification for Priority Change
                    TicketNotificationsModel ticketNotification = new TicketNotificationsModel {
                        TicketId = ticketsModel.Id,
                        UserId = User.Identity.GetUserId(),
                        Sender = user.Email,
                        Recipient = assignedUser.Email,
                        NotificationSubject = "The Status for the Ticket titled \"" + ticketsModel.Title + "\" has been changed",
                        NotificationMessage = "The Ticket titled <b>" + ticketsModel.Title + "</b> has had its Status changed. <br /><br />" +
                                    "Previous Status: " + histStatus.OldValue + "<br />" +
                                    "New Status: " + histStatus.NewValue + ".<br /><br />" +
                                    "The change was made by " + user.DisplayName + ".",
                        SentDate = DateTimeOffset.Now
                    };
                    db.TicketNotificationsData.Add(ticketNotification);
                    assignedUser.SendNotification(ticketNotification.NotificationSubject, ticketNotification.NotificationMessage);

                    if ((db.Entry(ticketsModel).Property(p => p.AssignedToUserId).IsModified == true) && (assignedUser.Roles.Any(r => r.RoleId == "c8d38989-258c-484b-9606-6f733c273059"))) { 
                        assignedUser.SendNotification(
                            "You have been assigned a Ticket",
                            "The Ticket titled <b>" + ticketsModel.Title + "</b> has been assigned to you. <br /><br />" +
                            "The change was made by " + user.DisplayName + "."
                            );
                    }
                }

                ticketsModel.UpdatedDate = DateTimeOffset.Now;

                db.TicketsData.Attach(ticketsModel);
                db.Entry(ticketsModel).Property(p => p.AssignedToUserId).IsModified = true;
                db.Entry(ticketsModel).Property(p => p.Description).IsModified = true;
                db.Entry(ticketsModel).Property(p => p.Title).IsModified = true;
                db.Entry(ticketsModel).Property(p => p.TicketTypeId).IsModified = true;
                db.Entry(ticketsModel).Property(p => p.TicketStatusId).IsModified = true;
                db.Entry(ticketsModel).Property(p => p.TicketPriorityId).IsModified = true;

                if ((ticketsModel.TicketStatusId == 4) && (ticketsModel.TicketPriorityId != 1004)) {
                    ticketsModel.TicketPriorityId = 1004;
                }

                db.Entry(ticketsModel).Property(p => p.UpdatedDate).IsModified = true;

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.OwnerUserId = new SelectList(db.Users, "Id", "DisplayName", ticketsModel.OwnerUserId);
            ViewBag.AssignedToUserId = new SelectList(db.Users, "Id", "DisplayName", ticketsModel.AssignedToUserId);
            ViewBag.ProjectId = new SelectList(db.ProjectsData, "Id", "Name", ticketsModel.ProjectId);
            ViewBag.TicketPriorityId = new SelectList(db.TicketPrioritiesData, "Id", "Name", ticketsModel.TicketPriorityId);
            ViewBag.TicketStatusId = new SelectList(db.TicketStatusesData, "Id", "Name", ticketsModel.TicketStatusId);
            ViewBag.TicketTypeId = new SelectList(db.TicketTypesData, "Id", "Name", ticketsModel.TicketTypeId);

            return View(ticketsModel);
        }