<commit_before>include CsvHelper
class AssignmentsController < ApplicationController
  before_filter      :authorize_only_for_admin,
                     :except => [:deletegroup,
                                 :delete_rejected,
                                 :disinvite_member,
                                 :invite_member,
                                 :creategroup,
                                 :join_group,
                                 :decline_invitation,
                                 :index,
                                 :student_interface,
                                 :update_collected_submissions,
                                 :render_test_result]

  before_filter      :authorize_for_student,
                     :only => [:student_interface,
                              :deletegroup,
                              :delete_rejected,
                              :disinvite_member,
                              :invite_member,
                              :creategroup,
                              :join_group,
                              :decline_invitation]

  before_filter      :authorize_for_user,
                     :only => [:index, :render_test_result]

  auto_complete_for  :assignment,
                     :name

  # Copy of API::AssignmentController without the id and order changed
  # to put first the 4 required fields
  DEFAULT_FIELDS = [:short_identifier, :description, :repository_folder,
                    :due_date, :message, :group_min, :group_max, :tokens_per_day,
                    :allow_web_submits, :student_form_groups, :remark_due_date,
                    :remark_message, :assign_graders_to_criteria, :enable_test,
                    :allow_remarks, :display_grader_names_to_students,
                    :group_name_autogenerated, :marking_scheme_type, :is_hidden]

  # Publicly accessible actions ---------------------------------------

  #=== Description
  # Action called via Rails' remote_function from the test_result_window partial
  # Prepares test result and updates content in window.
  def render_test_result
    @assignment = Assignment.find(params[:aid])
    @test_result = TestResult.find(params[:test_result_id])

    # Students can use this action only, when marks have been released
    if current_user.student? &&
        (@test_result.submission.grouping.membership_status(current_user).nil? ||
        @test_result.submission.get_latest_result.released_to_students == false)
      render :partial => 'shared/handle_error',
       :locals => {:error => I18n.t('test_result.error.no_access', :test_result_id => @test_result.id)}
      return
    end

    render :template => 'assignments/render_test_result', :layout => 'plain'
  end

  def student_interface
    @assignment = Assignment.find(params[:id])


    # >>>>>>>>>
    if @assignment.is_hidden
      render :file => "public/404.html", :status => 404
      return
    end


    @student = current_user
    @grouping = @student.accepted_grouping_for(@assignment.id)

    if @student.section &&
       !@student.section.section_due_date_for(@assignment.id).nil?
      @due_date =
        @student.section.section_due_date_for(@assignment.id).due_date
    end
    if @due_date.nil?
      @due_date = @assignment.due_date
    end
    if @student.has_pending_groupings_for?(@assignment.id)
      @pending_grouping = @student.pending_groupings_for(@assignment.id)
    end
    if @grouping.nil?
      if @assignment.group_max == 1
        begin
          # fix for issue #627
          # currently create_group_for_working_alone_student only returns false
          # when saving a grouping throws an exception
          unless @student.create_group_for_working_alone_student(@assignment.id)
            # if create_group_for_working_alone_student returned false then the student
            # must have an ( empty ) existing grouping that he is not a member of.
            # we must delete this grouping for the transaction to succeed.
            Grouping.find_by_group_id_and_assignment_id( Group.find_by_group_name(@student.user_name), @assignment.id).destroy
          end
        rescue RuntimeError => @error
          render 'shared/generic_error', :layout => 'error'
          return
        end
        redirect_to :action => 'student_interface', :id => @assignment.id
      else
        render :student_interface
      end
    else
      # We look for the information on this group...
      # The members
      @studentmemberships =  @grouping.student_memberships
      # The group name
      @group = @grouping.group
      # The inviter
      @inviter = @grouping.inviter

      # Look up submission information
      repo = @grouping.group.repo
      @revision  = repo.get_latest_revision
      @revision_number = @revision.revision_number

      # For running tests
      if params[:collect]
        @result = manually_collect_and_prepare_test(@grouping, @revision.revision_number)
      else
        @result = automatically_collect_and_prepare_test(@grouping, @revision.revision_number)
      end
      #submission = @grouping.submissions.find_by_submission_version_used(true)
      if @result
        @test_result_files = @result.submission.test_results
      else
        @test_result_files = nil
      end
      @token = Token.find_by_grouping_id(@grouping.id)
      if @token
        @token.reassign_tokens_if_new_day()
      end
      @last_modified_date = @grouping.assignment_folder_last_modified_date
      @num_submitted_files = @grouping.number_of_submitted_files
      @num_missing_assignment_files = @grouping.missing_assignment_files.length
      repo.close
    end
  end

  # Displays "Manage Assignments" page for creating and editing
  # assignment information
  def index
    

    #@assignments = Assignment.all(:order => :id)


    @grade_entry_forms = GradeEntryForm.all(:order => :id)
    @default_fields = DEFAULT_FIELDS
    if current_user.student?

      @assignments = Assignment.find(:all, :conditions =>
                                             { :is_hidden => false },
                                            :order => :id)
      #get the section of current user
      @section = current_user.section
      # get results for assignments for the current user
      @a_id_results = Hash.new()
      @assignments.each do |a|
        if current_user.has_accepted_grouping_for?(a)
          grouping = current_user.accepted_grouping_for(a)
          if grouping.has_submission?
            submission = grouping.current_submission_used
            if submission.has_remark? && submission.get_remark_result.released_to_students
              @a_id_results[a.id] = submission.get_remark_result
            elsif submission.has_result? && submission.get_original_result.released_to_students
              @a_id_results[a.id] = submission.get_original_result
            end
          end
        end
      end

      # Get the grades for grade entry forms for the current user
      @g_id_entries = Hash.new()
      @grade_entry_forms.each do |g|
        grade_entry_student = g.grade_entry_students.find_by_user_id(
                                    current_user.id )
        if !grade_entry_student.nil? &&
             grade_entry_student.released_to_student
          @g_id_entries[g.id] = grade_entry_student
        end
      end<commit_msg>Removing blank spaces and lines<commit_after>include CsvHelper
class AssignmentsController < ApplicationController
  before_filter      :authorize_only_for_admin,
                     :except => [:deletegroup,
                                 :delete_rejected,
                                 :disinvite_member,
                                 :invite_member,
                                 :creategroup,
                                 :join_group,
                                 :decline_invitation,
                                 :index,
                                 :student_interface,
                                 :update_collected_submissions,
                                 :render_test_result]

  before_filter      :authorize_for_student,
                     :only => [:student_interface,
                              :deletegroup,
                              :delete_rejected,
                              :disinvite_member,
                              :invite_member,
                              :creategroup,
                              :join_group,
                              :decline_invitation]

  before_filter      :authorize_for_user,
                     :only => [:index, :render_test_result]

  auto_complete_for  :assignment,
                     :name

  # Copy of API::AssignmentController without the id and order changed
  # to put first the 4 required fields
  DEFAULT_FIELDS = [:short_identifier, :description, :repository_folder,
                    :due_date, :message, :group_min, :group_max, :tokens_per_day,
                    :allow_web_submits, :student_form_groups, :remark_due_date,
                    :remark_message, :assign_graders_to_criteria, :enable_test,
                    :allow_remarks, :display_grader_names_to_students,
                    :group_name_autogenerated, :marking_scheme_type, :is_hidden]

  # Publicly accessible actions ---------------------------------------

  #=== Description
  # Action called via Rails' remote_function from the test_result_window partial
  # Prepares test result and updates content in window.
  def render_test_result
    @assignment = Assignment.find(params[:aid])
    @test_result = TestResult.find(params[:test_result_id])

    # Students can use this action only, when marks have been released
    if current_user.student? &&
        (@test_result.submission.grouping.membership_status(current_user).nil? ||
        @test_result.submission.get_latest_result.released_to_students == false)
      render :partial => 'shared/handle_error',
       :locals => {:error => I18n.t('test_result.error.no_access', :test_result_id => @test_result.id)}
      return
    end

    render :template => 'assignments/render_test_result', :layout => 'plain'
  end

  def student_interface
    @assignment = Assignment.find(params[:id])
    
    if @assignment.is_hidden
      render :file => "public/404.html", :status => 404
      return
    end

    @student = current_user
    @grouping = @student.accepted_grouping_for(@assignment.id)

    if @student.section &&
       !@student.section.section_due_date_for(@assignment.id).nil?
      @due_date =
        @student.section.section_due_date_for(@assignment.id).due_date
    end
    if @due_date.nil?
      @due_date = @assignment.due_date
    end
    if @student.has_pending_groupings_for?(@assignment.id)
      @pending_grouping = @student.pending_groupings_for(@assignment.id)
    end
    if @grouping.nil?
      if @assignment.group_max == 1
        begin
          # fix for issue #627
          # currently create_group_for_working_alone_student only returns false
          # when saving a grouping throws an exception
          unless @student.create_group_for_working_alone_student(@assignment.id)
            # if create_group_for_working_alone_student returned false then the student
            # must have an ( empty ) existing grouping that he is not a member of.
            # we must delete this grouping for the transaction to succeed.
            Grouping.find_by_group_id_and_assignment_id( Group.find_by_group_name(@student.user_name), @assignment.id).destroy
          end
        rescue RuntimeError => @error
          render 'shared/generic_error', :layout => 'error'
          return
        end
        redirect_to :action => 'student_interface', :id => @assignment.id
      else
        render :student_interface
      end
    else
      # We look for the information on this group...
      # The members
      @studentmemberships =  @grouping.student_memberships
      # The group name
      @group = @grouping.group
      # The inviter
      @inviter = @grouping.inviter

      # Look up submission information
      repo = @grouping.group.repo
      @revision  = repo.get_latest_revision
      @revision_number = @revision.revision_number

      # For running tests
      if params[:collect]
        @result = manually_collect_and_prepare_test(@grouping, @revision.revision_number)
      else
        @result = automatically_collect_and_prepare_test(@grouping, @revision.revision_number)
      end
      #submission = @grouping.submissions.find_by_submission_version_used(true)
      if @result
        @test_result_files = @result.submission.test_results
      else
        @test_result_files = nil
      end
      @token = Token.find_by_grouping_id(@grouping.id)
      if @token
        @token.reassign_tokens_if_new_day()
      end
      @last_modified_date = @grouping.assignment_folder_last_modified_date
      @num_submitted_files = @grouping.number_of_submitted_files
      @num_missing_assignment_files = @grouping.missing_assignment_files.length
      repo.close
    end
  end

  # Displays "Manage Assignments" page for creating and editing
  # assignment information
  def index
    #@assignments = Assignment.all(:order => :id)
    @grade_entry_forms = GradeEntryForm.all(:order => :id)
    @default_fields = DEFAULT_FIELDS
    if current_user.student?
      @assignments = Assignment.find(:all, :conditions =>
                                             { :is_hidden => false },
                                            :order => :id)
      #get the section of current user
      @section = current_user.section
      # get results for assignments for the current user
      @a_id_results = Hash.new()
      @assignments.each do |a|
        if current_user.has_accepted_grouping_for?(a)
          grouping = current_user.accepted_grouping_for(a)
          if grouping.has_submission?
            submission = grouping.current_submission_used
            if submission.has_remark? && submission.get_remark_result.released_to_students
              @a_id_results[a.id] = submission.get_remark_result
            elsif submission.has_result? && submission.get_original_result.released_to_students
              @a_id_results[a.id] = submission.get_original_result
            end
          end
        end
      end

      # Get the grades for grade entry forms for the current user
      @g_id_entries = Hash.new()
      @grade_entry_forms.each do |g|
        grade_entry_student = g.grade_entry_students.find_by_user_id(
                                    current_user.id )
        if !grade_entry_student.nil? &&
             grade_entry_student.released_to_student
          @g_id_entries[g.id] = grade_entry_student
        end
      end
{'fraction_non_alphanumeric': 0.08121896469627916, 'fraction_numerical': 0.0017685871709407523, 'mean_word_length': 2.1121930567315834, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6947748', 'n_tokens_mistral': 4110, 'n_tokens_neox': 3999, 'n_words': 823}