<commit_before># Handle tasks for a Company / User
# Author:: <NAME> (mailto:<EMAIL>)
#
class TasksController < ApplicationController
 cache_sweeper :cache_sweeper, :only => [:create, :update, :destroy, :ajax_hide, :ajax_restore,
    :ajax_check, :ajax_uncheck, :start_work_ajax, :stop_work, :swap_work_ajax, :save_log, :update_log,
    :cancel_work_ajax, :destroy_log ]

  def new
    @projects = User.find(session[:user].id).projects.find(:all, :order => 'name', :conditions => ["completed_at IS NULL"]).collect {|c| [ "#{c.name} / #{c.customer.name}", c.id ] if session[:user].can?(c, 'create')  }.compact unless session[:user].projects.nil?

    if @projects.nil? || @projects.empty?
      flash['notice'] = _("You need to create a project to hold your tasks, or get access to create tasks in an existing project...")
      redirect_to :controller => 'projects', :action => 'new'
    else
      @task = Task.new
      @task.duration = 0
      @tags = Tag.top_counts({ :company_id => session[:user].company_id, :project_ids => current_project_ids, :filter_hidden => session[:filter_hidden], :filter_milestone => session[:filter_milestone]})
    end
  end

  def list
    # Subscribe to the juggernaut channel for Task updates
    session[:channels] += ["tasks_#{session[:user].company_id}"]

    @tags = {}
    @tags.default = 0
    @tags_total = 0
    if session[:filter_project].to_i == 0
      project_ids = current_project_ids
    else
      project_ids = session[:filter_project]
    end


    filter = ""

    if session[:filter_user].to_i > 0
      task_ids = User.find(session[:filter_user].to_i).tasks.collect { |t| t.id }.join(',')
      if task_ids == ''
        filter = "tasks.id IN (0) AND "
      else
        filter = "tasks.id IN (#{task_ids}) AND "
      end
    elsif session[:filter_user].to_i < 0
      not_task_ids = Task.find(:all, :select => "tasks.*", :joins => "LEFT OUTER JOIN task_owners t_o ON tasks.id = t_o.task_id", :readonly => false, :conditions => ["tasks.company_id = ? AND t_o.id IS NULL", session[:user].company_id]).collect { |t| t.id }.join(',')
      if not_task_ids == ''
        filter = "tasks.id = 0 AND "
      else
        filter = "tasks.id IN (#{not_task_ids}) AND " if not_task_ids != ""
      end
    end

    if session[:filter_milestone].to_i > 0
      filter << "tasks.milestone_id = #{session[:filter_milestone]} AND "
    elsif session[:filter_milestone].to_i < 0
      filter << "(tasks.milestone_id IS NULL OR tasks.milestone_id = 0) AND "
    end

    unless session[:filter_status].to_i == -1 || session[:filter_status].to_i == -2
      if session[:filter_status].to_i == 0
        filter << "(tasks.status = 0 OR tasks.status = 1) AND "
      else
        filter << "tasks.status = #{session[:filter_status].to_i} AND "
      end
    end

    if session[:filter_status].to_i == -2
      filter << "tasks.hidden = 1 AND "
    else
      filter << "tasks.hidden = 0 AND "
    end

    unless session[:filter_type].to_i == -1
      filter << "tasks.type_id = #{session[:filter_type].to_i} AND "
    end

    unless session[:filter_customer].to_i == 0
      filter << "projects.customer_id = #{session[:filter_customer]} AND "
    end

    filter << "(tasks.milestone_id NOT IN (#{completed_milestone_ids}) OR tasks.milestone_id IS NULL) AND "


    if params[:tag] && params[:tag].length > 0
      # Looking for tasks based on tags
      @selected_tags = params[:tag].downcase.split(',').collect{|t| t.strip}
      @tasks = Task.tagged_with(@selected_tags, { :company_id => session[:user].company_id, :project_ids => project_ids, :filter_hidden => session[:filter_hidden], :filter_user => session[:filter_user], :filter_milestone => session[:filter_milestone], :filter_status => session[:filter_status], :filter_customer => session[:filter_customer]})
    else
      # Looking for tasks based on filters
      @selected_tags = []
      @tasks = Task.find(:all, :conditions => [filter + "tasks.company_id = #{session[:user].company_id} AND tasks.project_id IN (#{project_ids})"],  :order => " tasks.completed_at IS NOT NULL, tasks.completed_at desc, tasks.priority + tasks.severity_id desc, CASE WHEN (tasks.due_at IS NULL AND milestones.due_at IS NULL) THEN 1 ELSE 0 END, CASE WHEN (tasks.due_at IS NULL AND tasks.milestone_id IS NOT NULL) THEN milestones.due_at ELSE tasks.due_at END", :include => [:tags, :work_logs, :milestone, :project])
    end

    # Most popular tags, currently unlimited.
    @all_tags = Tag.top_counts({ :company_id => session[:user].company_id, :project_ids => project_ids, :filter_hidden => session[:filter_hidden], :filter_customer => session[:filter_customer]})

    if session[:group_by].to_i == 1 # tags
      @tag_names = @all_tags.collect{|i,j| i}
      @groups = Task.tag_groups(session[:user].company_id, @tag_names, @tasks)
    elsif session[:group_by].to_i == 2 # Clients
      items = Customer.find(:all, :conditions => ["company_id = ?", session[:user].company_id], :order => "name").collect(&:name).sort
      @groups = Task.group_by(@tasks, items) { |t,i| t.project.customer.name == i }
    elsif session[:group_by].to_i == 3 # Projects
      items = User.find(session[:user].id).projects.collect(&:name).sort
      @groups = Task.group_by(@tasks, items) { |t,i| t.project.name == i }
    elsif session[:group_by].to_i == 4 # Milestones
      items = Milestone.find(:all, :conditions => ["company_id = ? AND project_id IN (#{current_project_ids})", session[:user].company_id], :order => "due_at, name").collect(&:name)
      @groups = Task.group_by(@tasks, items) { |t,i| t.milestone.name == i if t.milestone }
    elsif session[:group_by].to_i == 5 # Users
      items = session[:user].company.users.collect(&:name).sort
      @groups = Task.group_by(@tasks, items) { |t,i| t.users.collect(&:name).include? i }
    elsif session[:group_by].to_i == 6 # Task Type
      items = Task.issue_types.sort
      @groups = Task.group_by(@tasks, items) { |t,i| t.issue_type == i }
    elsif session[:group_by].to_i == 7 # Status
      items = Task.status_types
      @groups = Task.group_by(@tasks, items) { |t,i| t.status_type == i }
    elsif session[:group_by].to_i == 8 # Severity
      items = Task.severity_types.sort.collect{ |v| v[1] }.reverse
      @groups = Task.group_by(@tasks, items) { |t,i| t.severity_type == i }
    elsif session[:group_by].to_i == 9 # Priority
      items = Task.priority_types.sort.collect{ |v| v[1] }.reverse
      @groups = Task.group_by(@tasks, items) { |t,i| t.priority_type == i }
    else
      @groups = [@tasks]
    end



  end<commit_msg>[ClockingIT @ Group by Client / Project instead of just Project]<commit_after># Handle tasks for a Company / User
# Author:: <NAME> (mailto:<EMAIL>.com)
#
class TasksController < ApplicationController
 cache_sweeper :cache_sweeper, :only => [:create, :update, :destroy, :ajax_hide, :ajax_restore,
    :ajax_check, :ajax_uncheck, :start_work_ajax, :stop_work, :swap_work_ajax, :save_log, :update_log,
    :cancel_work_ajax, :destroy_log ]

  def new
    @projects = User.find(session[:user].id).projects.find(:all, :order => 'name', :conditions => ["completed_at IS NULL"]).collect {|c| [ "#{c.name} / #{c.customer.name}", c.id ] if session[:user].can?(c, 'create')  }.compact unless session[:user].projects.nil?

    if @projects.nil? || @projects.empty?
      flash['notice'] = _("You need to create a project to hold your tasks, or get access to create tasks in an existing project...")
      redirect_to :controller => 'projects', :action => 'new'
    else
      @task = Task.new
      @task.duration = 0
      @tags = Tag.top_counts({ :company_id => session[:user].company_id, :project_ids => current_project_ids, :filter_hidden => session[:filter_hidden], :filter_milestone => session[:filter_milestone]})
    end
  end

  def list
    # Subscribe to the juggernaut channel for Task updates
    session[:channels] += ["tasks_#{session[:user].company_id}"]

    @tags = {}
    @tags.default = 0
    @tags_total = 0
    if session[:filter_project].to_i == 0
      project_ids = current_project_ids
    else
      project_ids = session[:filter_project]
    end


    filter = ""

    if session[:filter_user].to_i > 0
      task_ids = User.find(session[:filter_user].to_i).tasks.collect { |t| t.id }.join(',')
      if task_ids == ''
        filter = "tasks.id IN (0) AND "
      else
        filter = "tasks.id IN (#{task_ids}) AND "
      end
    elsif session[:filter_user].to_i < 0
      not_task_ids = Task.find(:all, :select => "tasks.*", :joins => "LEFT OUTER JOIN task_owners t_o ON tasks.id = t_o.task_id", :readonly => false, :conditions => ["tasks.company_id = ? AND t_o.id IS NULL", session[:user].company_id]).collect { |t| t.id }.join(',')
      if not_task_ids == ''
        filter = "tasks.id = 0 AND "
      else
        filter = "tasks.id IN (#{not_task_ids}) AND " if not_task_ids != ""
      end
    end

    if session[:filter_milestone].to_i > 0
      filter << "tasks.milestone_id = #{session[:filter_milestone]} AND "
    elsif session[:filter_milestone].to_i < 0
      filter << "(tasks.milestone_id IS NULL OR tasks.milestone_id = 0) AND "
    end

    unless session[:filter_status].to_i == -1 || session[:filter_status].to_i == -2
      if session[:filter_status].to_i == 0
        filter << "(tasks.status = 0 OR tasks.status = 1) AND "
      else
        filter << "tasks.status = #{session[:filter_status].to_i} AND "
      end
    end

    if session[:filter_status].to_i == -2
      filter << "tasks.hidden = 1 AND "
    else
      filter << "tasks.hidden = 0 AND "
    end

    unless session[:filter_type].to_i == -1
      filter << "tasks.type_id = #{session[:filter_type].to_i} AND "
    end

    unless session[:filter_customer].to_i == 0
      filter << "projects.customer_id = #{session[:filter_customer]} AND "
    end

    filter << "(tasks.milestone_id NOT IN (#{completed_milestone_ids}) OR tasks.milestone_id IS NULL) AND "


    if params[:tag] && params[:tag].length > 0
      # Looking for tasks based on tags
      @selected_tags = params[:tag].downcase.split(',').collect{|t| t.strip}
      @tasks = Task.tagged_with(@selected_tags, { :company_id => session[:user].company_id, :project_ids => project_ids, :filter_hidden => session[:filter_hidden], :filter_user => session[:filter_user], :filter_milestone => session[:filter_milestone], :filter_status => session[:filter_status], :filter_customer => session[:filter_customer]})
    else
      # Looking for tasks based on filters
      @selected_tags = []
      @tasks = Task.find(:all, :conditions => [filter + "tasks.company_id = #{session[:user].company_id} AND tasks.project_id IN (#{project_ids})"],  :order => " tasks.completed_at IS NOT NULL, tasks.completed_at desc, tasks.priority + tasks.severity_id desc, CASE WHEN (tasks.due_at IS NULL AND milestones.due_at IS NULL) THEN 1 ELSE 0 END, CASE WHEN (tasks.due_at IS NULL AND tasks.milestone_id IS NOT NULL) THEN milestones.due_at ELSE tasks.due_at END", :include => [:tags, :work_logs, :milestone, :project])
    end

    # Most popular tags, currently unlimited.
    @all_tags = Tag.top_counts({ :company_id => session[:user].company_id, :project_ids => project_ids, :filter_hidden => session[:filter_hidden], :filter_customer => session[:filter_customer]})

    if session[:group_by].to_i == 1 # tags
      @tag_names = @all_tags.collect{|i,j| i}
      @groups = Task.tag_groups(session[:user].company_id, @tag_names, @tasks)
    elsif session[:group_by].to_i == 2 # Clients
      items = Customer.find(:all, :conditions => ["company_id = ?", session[:user].company_id], :order => "name").collect(&:name).sort
      @groups = Task.group_by(@tasks, items) { |t,i| t.project.customer.name == i }
    elsif session[:group_by].to_i == 3 # Projects
      items = User.find(session[:user].id).projects.collect(&:full_name).sort
      @groups = Task.group_by(@tasks, items) { |t,i| t.project.full_name == i }
    elsif session[:group_by].to_i == 4 # Milestones
      items = Milestone.find(:all, :conditions => ["company_id = ? AND project_id IN (#{current_project_ids})", session[:user].company_id], :order => "due_at, name").collect(&:name)
      @groups = Task.group_by(@tasks, items) { |t,i| t.milestone.name == i if t.milestone }
    elsif session[:group_by].to_i == 5 # Users
      items = session[:user].company.users.collect(&:name).sort
      @groups = Task.group_by(@tasks, items) { |t,i| t.users.collect(&:name).include? i }
    elsif session[:group_by].to_i == 6 # Task Type
      items = Task.issue_types.sort
      @groups = Task.group_by(@tasks, items) { |t,i| t.issue_type == i }
    elsif session[:group_by].to_i == 7 # Status
      items = Task.status_types
      @groups = Task.group_by(@tasks, items) { |t,i| t.status_type == i }
    elsif session[:group_by].to_i == 8 # Severity
      items = Task.severity_types.sort.collect{ |v| v[1] }.reverse
      @groups = Task.group_by(@tasks, items) { |t,i| t.severity_type == i }
    elsif session[:group_by].to_i == 9 # Priority
      items = Task.priority_types.sort.collect{ |v| v[1] }.reverse
      @groups = Task.group_by(@tasks, items) { |t,i| t.priority_type == i }
    else
      @groups = [@tasks]
    end



  end
{'fraction_non_alphanumeric': 0.15697102655268932, 'fraction_numerical': 0.005295408124669037, 'mean_word_length': 3.604667363288053, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '910926', 'n_tokens_mistral': 4468, 'n_tokens_neox': 4312, 'n_words': 1031}