2009-01-06 11:49:19 +01:00
|
|
|
class Task < ActiveRecord::Base
|
|
|
|
has_many :assignments, :dependent => :destroy
|
|
|
|
has_many :users, :through => :assignments
|
2009-01-15 12:14:01 +01:00
|
|
|
belongs_to :workgroup
|
|
|
|
|
2012-11-12 09:03:23 +01:00
|
|
|
scope :non_group, where(workgroup_id: nil, done: false)
|
|
|
|
scope :done, where(done: true)
|
|
|
|
scope :undone, where(done: false)
|
|
|
|
|
|
|
|
attr_accessor :current_user_id
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# form will send user in string. responsibilities will added later
|
|
|
|
attr_protected :users
|
2011-05-14 19:02:52 +02:00
|
|
|
|
|
|
|
validates :name, :presence => true, :length => { :minimum => 3 }
|
|
|
|
validates :required_users, :presence => true
|
|
|
|
validates_numericality_of :duration, :required_users, :only_integer => true, :greater_than => 0
|
2012-08-24 11:11:40 +02:00
|
|
|
validates_length_of :description, maximum: 250
|
2009-08-01 13:41:22 +02:00
|
|
|
|
|
|
|
after_save :update_ordergroup_stats
|
2012-11-12 09:03:23 +01:00
|
|
|
|
|
|
|
# Find all tasks, for which the current user should be responsible
|
|
|
|
# but which aren't accepted yet
|
|
|
|
def self.unaccepted_tasks_for(user)
|
|
|
|
user.tasks.undone.where(assignments: {accepted: false})
|
|
|
|
end
|
|
|
|
|
|
|
|
# Find all accepted tasks, which aren't done
|
|
|
|
def self.accepted_tasks_for(user)
|
|
|
|
user.tasks.undone.where(assignments: {accepted: true})
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# find all tasks in the next week (or another number of days)
|
|
|
|
def self.next_assigned_tasks_for(user, number = 7)
|
|
|
|
user.tasks.undone.where(assignments: {accepted: true}).
|
|
|
|
where(["tasks.due_date >= ? AND tasks.due_date <= ?", Time.now, number.days.from_now])
|
|
|
|
end
|
|
|
|
|
|
|
|
# count tasks with no responsible person
|
|
|
|
# tasks for groups the user is not a member are ignored
|
|
|
|
def self.unassigned_tasks_for(user)
|
|
|
|
Task.undone.where(assigned: false).includes(:workgroup).all.select do |task|
|
|
|
|
!task.workgroup or user.member_of?(task.workgroup)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
def is_assigned?(user)
|
|
|
|
self.assignments.detect {|ass| ass.user_id == user.id }
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_accepted?(user)
|
|
|
|
self.assignments.detect {|ass| ass.user_id == user.id && ass.accepted }
|
|
|
|
end
|
|
|
|
|
|
|
|
def enough_users_assigned?
|
|
|
|
assignments.find_all_by_accepted(true).size >= required_users ? true : false
|
|
|
|
end
|
2011-05-14 19:41:46 +02:00
|
|
|
|
|
|
|
# Get users from comma seperated ids
|
2009-01-06 11:49:19 +01:00
|
|
|
# and makes the users responsible for the task
|
2012-11-12 21:31:04 +01:00
|
|
|
# TODO: check for maximal number of users
|
2011-05-14 19:41:46 +02:00
|
|
|
def user_list=(ids)
|
|
|
|
list = ids.split(",")
|
2012-12-14 18:10:46 +01:00
|
|
|
new_users = (list - users.collect(&:id)).uniq
|
2011-05-14 19:41:46 +02:00
|
|
|
old_users = users.reject { |user| list.include?(user.id) }
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2012-11-12 09:03:23 +01:00
|
|
|
logger.debug "[debug] New users: #{new_users}"
|
2009-01-06 11:49:19 +01:00
|
|
|
logger.debug "Old users: #{old_users}"
|
|
|
|
|
|
|
|
self.class.transaction do
|
|
|
|
# delete old assignments
|
|
|
|
if old_users.any?
|
2012-11-12 09:03:23 +01:00
|
|
|
assignments.where(user_id: old_users.map(&:id)).each(&:destroy)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
# create new assignments
|
2011-05-14 19:41:46 +02:00
|
|
|
new_users.each do |id|
|
|
|
|
user = User.find(id)
|
2009-01-06 11:49:19 +01:00
|
|
|
if user.blank?
|
|
|
|
errors.add(:user_list)
|
|
|
|
else
|
2012-11-12 09:03:23 +01:00
|
|
|
if id == current_user_id
|
2009-01-06 11:49:19 +01:00
|
|
|
# current_user will accept, when he puts himself to the list of users
|
|
|
|
self.assignments.build :user => user, :accepted => true
|
|
|
|
else
|
|
|
|
# normal assignement
|
|
|
|
self.assignments.build :user => user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_list
|
2011-05-14 19:41:46 +02:00
|
|
|
@user_list ||= users.collect(&:id).join(", ")
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-08-01 13:41:22 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_ordergroup_stats
|
|
|
|
if done
|
2011-05-07 21:54:00 +02:00
|
|
|
users.each { |u| u.ordergroup.update_stats! if u.ordergroup }
|
2009-08-01 13:41:22 +02:00
|
|
|
end
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2011-05-07 20:50:39 +02:00
|
|
|
|