<commit_before>class SpentTimeController < ApplicationController

  helper :timelog
  include TimelogHelper
  helper :spent_time
  include SpentTimeHelper

  # Show the initial form.
  # * If user has permissions to see spent time for every project
  # the users combobox is filled with all the users.
  # * If user has permissions to see other members' spent times of the projects he works in,
  # the users combobox is filled with their co-workers
  # * If the user only has permissions to see his own report, the users' combobox is filled with the user himself.
  def index
    @user = User.current
    if (authorized_for?(:view_every_project_spent_time))
      @users = User.find(:all, :conditions => ["status = 1"])
    elsif (authorized_for?(:view_others_spent_time))
      projects = User.current.projects
      @users = []
      projects.each { |project| @users.concat(project.users) }
      @users.uniq!
    else
      @users = [@user]
    end
    @users.sort! { |a, b| a.name <=> b.name }
    @assigned_issues = []
    @same_user = true
  end

  # Show the report of spent time between to dates for an user
  def report
    @user = User.current
    make_time_entry_report(params[:from], params[:to], params[:user])
    another_user = User.find(params[:user])
    @same_user = (@user.id == another_user.id)
    respond_to do |format|
      format.html { render :partial => 'report'}
    end
  end

  # Delete a time entry
  def destroy_entry
    @time_entry = TimeEntry.find(params[:id])
    render_404 and return unless @time_entry
    render_403 and return unless @time_entry.editable_by?(User.current)
    @time_entry.destroy

    @user = User.current
    @from = params[:from].to_s.to_date
    @to = params[:to].to_s.to_date
    make_time_entry_report(params[:from], params[:to], @user)
    respond_to do |format|
      format.js
    end
  rescue ::ActionController::RedirectBackError
    redirect_to :action => 'index'
  end

  # Create a new time entry
  def create_entry
    @user = User.current

    @time_entry_date = params[:time_entry_spent_on].to_s.to_date
    params[:time_entry][:spent_on] = @time_entry_date
    @from = params[:from].to_s.to_date
    @to = params[:to].to_s.to_date

    # Save the new record
    begin
      @issue = Issue.find(params[:issue_id])
      @project = @issue.project
      @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => @user)
    rescue
      @project = Project.find(params[:project_id]);
      @time_entry ||= TimeEntry.new(:project => @project, :user => @user)
    end

    @time_entry.attributes = params[:time_entry]
    render_403 and return if @time_entry && !@time_entry.editable_by?(@user)
    @time_entry.user = @user
    @time_entry.save!

    respond_to do |format|
      if @time_entry_date > @to
        @to = @time_entry_date
      elsif @time_entry_date < @from
        @from = @time_entry_date
      end
      make_time_entry_report(@from, @to, @user)
      format.js
    end
  end

  # Update the project's issues when another project is selected
  def update_project_issues
    find_assigned_issues_by_project(params[:project_id])
    render :update do |page|
      page.replace_html "fields_for_new_entry_form", :partial => "fields_for_new_entry_form"
    end
  end

  # Update the users' combobox after changing the project
  def update_users_select
    if Project.exists?(params[:value])
      project = Project.find(params[:value])
      @users = project.users
    else
      @users = User.find(:all, :conditions => ["status = 1"])<commit_msg>Fix update issues when changing project selection<commit_after>class SpentTimeController < ApplicationController

  helper :timelog
  include TimelogHelper
  helper :spent_time
  include SpentTimeHelper

  # Show the initial form.
  # * If user has permissions to see spent time for every project
  # the users combobox is filled with all the users.
  # * If user has permissions to see other members' spent times of the projects he works in,
  # the users combobox is filled with their co-workers
  # * If the user only has permissions to see his own report, the users' combobox is filled with the user himself.
  def index
    @user = User.current
    if (authorized_for?(:view_every_project_spent_time))
      @users = User.find(:all, :conditions => ["status = 1"])
    elsif (authorized_for?(:view_others_spent_time))
      projects = User.current.projects
      @users = []
      projects.each { |project| @users.concat(project.users) }
      @users.uniq!
    else
      @users = [@user]
    end
    @users.sort! { |a, b| a.name <=> b.name }
    @assigned_issues = []
    @same_user = true
  end

  # Show the report of spent time between to dates for an user
  def report
    @user = User.current
    make_time_entry_report(params[:from], params[:to], params[:user])
    another_user = User.find(params[:user])
    @same_user = (@user.id == another_user.id)
    respond_to do |format|
      format.html { render :partial => 'report'}
    end
  end

  # Delete a time entry
  def destroy_entry
    @time_entry = TimeEntry.find(params[:id])
    render_404 and return unless @time_entry
    render_403 and return unless @time_entry.editable_by?(User.current)
    @time_entry.destroy

    @user = User.current
    @from = params[:from].to_s.to_date
    @to = params[:to].to_s.to_date
    make_time_entry_report(params[:from], params[:to], @user)
    respond_to do |format|
      format.js
    end
  rescue ::ActionController::RedirectBackError
    redirect_to :action => 'index'
  end

  # Create a new time entry
  def create_entry
    @user = User.current

    @time_entry_date = params[:time_entry_spent_on].to_s.to_date
    params[:time_entry][:spent_on] = @time_entry_date
    @from = params[:from].to_s.to_date
    @to = params[:to].to_s.to_date

    # Save the new record
    begin
      @issue = Issue.find(params[:issue_id])
      @project = @issue.project
      @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => @user)
    rescue
      @project = Project.find(params[:project_id]);
      @time_entry ||= TimeEntry.new(:project => @project, :user => @user)
    end

    @time_entry.attributes = params[:time_entry]
    render_403 and return if @time_entry && !@time_entry.editable_by?(@user)
    @time_entry.user = @user
    @time_entry.save!

    respond_to do |format|
      if @time_entry_date > @to
        @to = @time_entry_date
      elsif @time_entry_date < @from
        @from = @time_entry_date
      end
      make_time_entry_report(@from, @to, @user)
      format.js
    end
  end

  # Update the project's issues when another project is selected
  def update_project_issues
    @to = params[:to].to_date
    @from = params[:from].to_date
    find_assigned_issues_by_project(params[:project_id])
    render :update do |page|
      page.replace_html "fields_for_new_entry_form", :partial => "fields_for_new_entry_form"
    end
  end

  # Update the users' combobox after changing the project
  def update_users_select
    if Project.exists?(params[:value])
      project = Project.find(params[:value])
      @users = project.users
    else
      @users = User.find(:all, :conditions => ["status = 1"])
{'fraction_non_alphanumeric': 0.1094140516523188, 'fraction_numerical': 0.0030547070258261596, 'mean_word_length': 3.347012673506337, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, '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': '809766', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2274, 'n_words': 611}