private PartialViewResult ShowTasks(int projectId)
        {
            List<TaskListElement> model = new List<TaskListElement>();

            int userId = int.Parse(User.Identity.GetProjectUserId());
            var manager = new TaskManager();

            int deletedId = manager.GetDeletedStateId();

            ViewData["projectId"] = projectId;
            ViewData["deletedId"] = deletedId;
            ViewData["isLeader"] = new ProjectUserManager().IsLeader(userId, projectId);
            ViewData["isDone"] = new ProjectUserManager().GetProject(projectId).Done;

            var tasks = manager.GetTasksForProject(projectId);

            foreach (var t in tasks)
            {
                var name = manager.GetTaskStateName(t.Id);

                var isCommented = manager.IsCommented(t.Id);

                var workHours = manager.GetAllWorkTimeForTask(t.Id)
                    .Sum(w => w.EndTime.Subtract(w.StartTime).TotalSeconds);

                var developers = manager.GetUsersForTask(t.Id);

                List<string> devs = new List<string>();
                foreach (var v in developers)
                {
                    devs.Add(v.UserName);
                }

                TaskListElement element = new TaskListElement(t, name, devs, (int)(workHours / 3600), isCommented);
                model.Add(element);
            }

            return PartialView("_Tasks", model);
        }
        private TaskDetailsModel CreateTaskDetailsModel(int userId, int taskId)
        {
            var manager = new TaskManager();

            var project = manager.GetProjectForTask(taskId);
            ViewData["isLeader"] = new ProjectUserManager().IsLeader(userId, project.Id);

            var task = manager.GetTask(taskId);
            var stateName = manager.GetTaskStateName(taskId);
            var users = manager.GetUsersForTask(taskId);
            var workTimes = manager.GetAllWorkTimeForTask(taskId);
            var comments = manager.GetComments(taskId);
            var canComment = task.State == manager.GetActiveStateId();
            var addableDevelopers = manager.GetAddableOrRemovableDevelopers(
                taskId, project.Id, addable: true);
            var removableDevelopers = manager.GetAddableOrRemovableDevelopers(
                taskId, project.Id, addable: false);
            var unacceptedDevelopers = manager.GetUnacceptedDevelopers(taskId);
            var unacceptedTaskStateChanges = manager.GetUnacceptedTaskStateChanges(taskId);
            var acceptedTaskStateChanges = manager.GetAcceptedTaskStateChanges(taskId);

            int deletedId = manager.GetDeletedStateId();
            ViewData["deletedId"] = deletedId;

            int doneId = manager.GetDoneStateId();
            ViewData["doneId"] = doneId;

            int activeId = manager.GetActiveStateId();
            ViewData["activeId"] = activeId;

            int newId = manager.GetNewStateId();
            ViewData["newId"] = newId;

            ViewData["isUserOnTask"] = users.Any(u => u.Id == userId);
            ViewData["isUserApplyUnaccepted"] = unacceptedDevelopers.Any(u => u.Id == userId);
            Boolean maxDevelopers = task.MaxDevelopers == null ? false : task.MaxDevelopers <= users.Count;
            ViewData["userHasWorktime"] = workTimes.Any(w => w.ProjectUserId == userId);

            List<string> devs = new List<string>();
            foreach (var u in users)
            {
                devs.Add(u.UserName);
            }

            List<CommentViewModel> commentViewModels = new List<CommentViewModel>();
            foreach (var comment in comments)
            {
                var commentingUser = new ProjectUserManager().GetUser(comment.ProjectUserId);
                commentViewModels.Add(
                    new CommentViewModel(comment.Content, comment.Timestamp, commentingUser.UserName.Trim()));
            }

            Dictionary<string, int> userHours = new Dictionary<string, int>();
            foreach (var d in devs)
            {
                userHours.Add(d, 0);
            }

            foreach (var workTime in workTimes)
            {
                var userName = users.First(u => u.Id == workTime.ProjectUserId).UserName;
                int elapsed = (int)(workTime.EndTime.Subtract(workTime.StartTime).TotalSeconds / 3600);

                int currentHours = userHours[userName];
                userHours[userName] = currentHours + elapsed;
            }

            bool projectDone = new ProjectUserManager().IsDone(task.ProjectId);

            return new TaskDetailsModel(task, stateName, devs, userHours, commentViewModels, canComment,
                addableDevelopers, removableDevelopers, unacceptedDevelopers, unacceptedTaskStateChanges,
                acceptedTaskStateChanges, projectDone, maxDevelopers);
        }
        public ActionResult Finish(int Id)
        {
            int userId = int.Parse(User.Identity.GetProjectUserId());

            var project = new ProjectUserManager().GetProject(Id);

            var manager = new TaskManager();
            var tasks = new TaskManager().GetTasksForProject(Id);

            if (tasks.Count == 0 ||
                tasks.All(t => t.State == manager.GetDeletedStateId() || t.State == manager.GetDoneStateId()))
            {
                new ProjectUserManager().FinishProject(Id);
            }
            else
            {
                TempData["errorMessage"] = "Can't finish project, there are still tasks in progress!";
            }

            return Redirect(Request.UrlReferrer.ToString());
        }