2019-01-13 07:05:54 +01:00
|
|
|
class Task < ApplicationRecord
|
2009-01-06 11:49:19 +01:00
|
|
|
has_many :assignments, :dependent => :destroy
|
|
|
|
has_many :users, :through => :assignments
|
2020-08-01 02:49:15 +02:00
|
|
|
belongs_to :workgroup, optional: true
|
|
|
|
belongs_to :periodic_task_group, optional: true
|
2020-10-10 20:39:40 +02:00
|
|
|
belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id', optional: true
|
2009-01-15 12:14:01 +01:00
|
|
|
|
2014-02-20 15:04:53 +01:00
|
|
|
scope :non_group, -> { where(workgroup_id: nil, done: false) }
|
|
|
|
scope :done, -> { where(done: true) }
|
|
|
|
scope :undone, -> { where(done: false) }
|
2012-11-12 09:03:23 +01:00
|
|
|
|
|
|
|
attr_accessor :current_user_id
|
|
|
|
|
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
|
2013-06-24 11:53:52 +02:00
|
|
|
validates :done, exclusion: { in: [true] }, if: :periodic?, on: :create
|
2016-02-24 20:16:06 +01:00
|
|
|
validates_presence_of :due_date, if: :periodic?
|
2009-08-01 13:41:22 +02:00
|
|
|
|
2013-06-24 10:36:14 +02:00
|
|
|
before_save :exclude_from_periodic_task_group, if: :changed?, unless: :new_record?
|
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)
|
2021-03-01 15:27:26 +01:00
|
|
|
user.tasks.undone.where(assignments: { accepted: false })
|
2012-11-12 09:03:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Find all accepted tasks, which aren't done
|
|
|
|
def self.accepted_tasks_for(user)
|
2021-03-01 15:27:26 +01:00
|
|
|
user.tasks.undone.where(assignments: { accepted: true })
|
2012-11-12 09:03:23 +01:00
|
|
|
end
|
|
|
|
|
2014-11-23 01:22:50 +01:00
|
|
|
# find all tasks in the period (or another number of days)
|
|
|
|
def self.next_assigned_tasks_for(user, number = FoodsoftConfig[:tasks_period_days].to_i)
|
2021-03-01 15:27:26 +01:00
|
|
|
user.tasks.undone.where(assignments: { accepted: true })
|
|
|
|
.where(["tasks.due_date >= ? AND tasks.due_date <= ?", Time.now, number.days.from_now])
|
2012-11-12 09:03:23 +01:00
|
|
|
end
|
|
|
|
|
2012-12-16 19:03:04 +01:00
|
|
|
# count tasks with not enough responsible people
|
2012-11-12 09:03:23 +01:00
|
|
|
# tasks for groups the user is not a member are ignored
|
|
|
|
def self.unassigned_tasks_for(user)
|
2012-12-16 19:03:04 +01:00
|
|
|
undone.includes(:assignments, workgroup: :memberships).select do |task|
|
|
|
|
!task.enough_users_assigned? and
|
2021-03-01 15:27:26 +01:00
|
|
|
(!task.workgroup or task.workgroup.memberships.detect { |m| m.user_id == user.id })
|
2012-11-12 09:03:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 01:01:12 +02:00
|
|
|
def self.next_unassigned_tasks_for(user, max = 2)
|
|
|
|
periodic_task_group_count = {}
|
|
|
|
self.unassigned_tasks_for(user).reject do |item|
|
2017-10-29 00:06:05 +02:00
|
|
|
next false unless item.periodic_task_group
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2017-10-20 01:01:12 +02:00
|
|
|
count = periodic_task_group_count[item.periodic_task_group] || 0
|
|
|
|
periodic_task_group_count[item.periodic_task_group] = count + 1
|
|
|
|
count >= max
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-21 13:48:48 +02:00
|
|
|
def periodic?
|
2012-11-28 10:13:54 +01:00
|
|
|
not periodic_task_group.nil?
|
|
|
|
end
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
def is_assigned?(user)
|
2021-03-01 15:27:26 +01:00
|
|
|
self.assignments.detect { |ass| ass.user_id == user.id }
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2017-10-20 01:01:12 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
def is_accepted?(user)
|
2021-03-01 15:27:26 +01:00
|
|
|
self.assignments.detect { |ass| ass.user_id == user.id && ass.accepted }
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2017-10-20 01:01:12 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
def enough_users_assigned?
|
2012-12-16 19:03:04 +01:00
|
|
|
assignments.to_a.count(&:accepted) >= required_users ? true : false
|
|
|
|
end
|
|
|
|
|
|
|
|
def still_required_users
|
|
|
|
required_users - assignments.to_a.count(&:accepted)
|
2009-01-06 11:49:19 +01:00
|
|
|
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)
|
2013-04-08 01:30:31 +02:00
|
|
|
list = ids.split(",").map(&:to_i)
|
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) }
|
2019-01-13 07:05:54 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
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
|
2021-03-01 15:27:26 +01:00
|
|
|
if id == current_user_id.to_i
|
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
|
2019-01-13 07:05:54 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
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
|
|
|
|
2012-12-16 19:03:04 +01:00
|
|
|
def update_ordergroup_stats(user_ids = self.user_ids)
|
2021-03-01 15:27:26 +01:00
|
|
|
Ordergroup.joins(:users).where(users: { id: user_ids }).each(&:update_stats!)
|
2009-08-01 13:41:22 +02:00
|
|
|
end
|
2013-06-21 20:54:24 +02:00
|
|
|
|
|
|
|
def exclude_from_periodic_task_group
|
2013-06-24 10:36:14 +02:00
|
|
|
self.periodic_task_group = nil
|
|
|
|
true
|
2013-06-21 20:54:24 +02:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|