<commit_before>require 'zip'
class ResultsController < ApplicationController
  include TagsHelper
  before_filter :authorize_only_for_admin,
                except: [:codeviewer, :edit, :update_mark, :view_marks,
                         :create, :add_extra_mark, :next_grouping,
                         :update_overall_comment, :remove_extra_mark,
                         :update_marking_state, :download, :download_zip,
                         :note_message,
                         :update_remark_request, :cancel_remark_request]
  before_filter :authorize_for_ta_and_admin,
                only: [:edit, :update_mark, :create, :add_extra_mark,
                       :next_grouping, :update_overall_comment,
                       :remove_extra_mark, :update_marking_state, :note_message]
  before_filter :authorize_for_user,
                only: [:codeviewer, :download, :download_zip]
  before_filter :authorize_for_student,
                only: [:view_marks, :update_remark_request,
                       :cancel_remark_request]
  after_filter  :update_remark_request_count,
                only: [:update_remark_request, :cancel_remark_request,
                       :set_released_to_students]

  def note_message
    @result = Result.find(params[:id])
    if params[:success]
      flash[:note_success] = I18n.t('notes.success')
    else
      flash[:fail_notice] = I18n.t('notes.error')
    end
  end

  def edit
    @result = Result.find(params[:id])
    @assignment = @result.submission.assignment
    @submission = @result.submission

    if @submission.remark_submitted?
      @old_result = @submission.get_original_result
    else
      @old_result = nil
    end

    @grouping = @result.submission.grouping
    @not_associated_tags = get_tags_not_associated_with_grouping(@grouping.id)
    @group = @grouping.group
    @files = @submission.submission_files.sort do |a, b|
      File.join(a.path, a.filename) <=> File.join(b.path, b.filename)
    end
    @test_result_files = @submission.test_results
    @first_file = @files.first
    @extra_marks_points = @result.extra_marks.points
    @extra_marks_percentage = @result.extra_marks.percentage
    @marks_map = Hash.new
    @old_marks_map = Hash.new
    @mark_criteria = @assignment.get_criteria
    @assignment.get_criteria.each do |criterion|
      mark = criterion.marks.find_or_create_by(result_id: @result.id)
      @marks_map[criterion.id] = mark

      # Loading up previous results for the case of a remark
      if @old_result
        oldmark = criterion.marks.find_or_create_by(result_id: @old_result.id)
        oldmark.save(validate: false)
        @old_marks_map[criterion.id] = oldmark
      end

      mark.save(validate: false)
      @result.update_total_mark
    end

    groupings = Grouping.get_groupings_for_assignment(@assignment,
                                                      current_user)
    # We sort by group name by default
    groupings = groupings.sort do |a, b|
      a.group.group_name <=> b.group.group_name
    end

    current_grouping_index = groupings.index(@grouping)
    if current_grouping_index.nil?
      @next_grouping = groupings.first
      @previous_grouping = groupings.last
    else
      unless groupings[current_grouping_index + 1].nil?
        @next_grouping = groupings[current_grouping_index + 1]
      end
      if (current_grouping_index - 1) >= 0
        @previous_grouping = groupings[current_grouping_index - 1]
      end
    end

    m_logger = MarkusLogger.instance
    m_logger.log("User '#{current_user.user_name}' viewed submission (id: #{@submission.id})" +
                 "of assignment '#{@assignment.short_identifier}' for group '" +
                 "#{@group.group_name}'")

    # Sets up the tags for the tag pane.
    # Creates a variable for all the tags not used
    # and all the tags that are used by the assignment.
    @all_tags = Tag.all
    @grouping_tags = get_tags_for_grouping(@grouping.id)
    @not_grouping_tags = get_tags_not_associated_with_grouping(@grouping.id)

    # Gets the top tags and their usage.
    @top_tags = get_top_tags
    @top_tags_num = Hash.new
    @top_tags.each do |current|
      @top_tags_num[current.id] = get_num_groupings_for_tag(current.id)
    end

    # Respond to AJAX request.
    respond_to do |format|
      format.html
      format.json do
        @request_type = params[:type]

        # Checks the operation requested.
        if @request_type.eql? 'add'
          create_grouping_tag_association_from_existing_tag(
              params[:grouping_id],
              params[:tag_id])
        else
          delete_grouping_tag_association(params[:tag_id],
                                          Grouping.find(params[:grouping_id]))
        end

        # Renders nothing.
        render nothing: true
      end
    end
  end

  ##  Tag Methods  ##

  def add_tag
    create_grouping_tag_association_from_existing_tag(params[:grouping_id],
                                                      params[:tag_id])
    respond_to do |format|
      format.html do
        redirect_to :back
      end
    end
  end

  def remove_tag
    delete_grouping_tag_association(params[:tag_id],
                                    Grouping.find(params[:grouping_id]))
    respond_to do |format|
      format.html do
        redirect_to :back
      end
    end
  end

  def next_grouping
    grouping = Grouping.find(params[:id])
    if grouping.has_submission? && grouping.is_collected?
        redirect_to action: 'edit',
                    id: grouping.current_submission_used.get_latest_result.id
    else
      redirect_to controller: 'submissions',
                  action: 'collect_and_begin_grading',
                  id: grouping.assignment.id,
                  grouping_id: grouping.id
    end
  end

  def set_released_to_students
    @result = Result.find(params[:id])
    released_to_students = (params[:value] == 'true')
    if params[:old_id]
      @old_result = Result.find(params[:old_id])
      @old_result.released_to_students = released_to_students
      @old_result.save
    end
    @result.released_to_students = released_to_students
    @result.save
    m_logger = MarkusLogger.instance
    assignment = @result.submission.assignment
    if params[:value] == 'true'
      m_logger.log("Marks released for assignment '#{assignment.short_identifier}', ID: '" +
                   "#{assignment.id}' (for 1 group).")
    else
      m_logger.log("Marks unreleased for assignment '#{assignment.short_identifier}', ID: '" +
                   "#{assignment.id}' (for 1 group).")
    end
  end

  #Updates the marking state
  def update_marking_state
    @result = Result.find(params[:id])<commit_msg>disable automatic collection for Next/Previous submission buttons<commit_after>require 'zip'
class ResultsController < ApplicationController
  include TagsHelper
  before_filter :authorize_only_for_admin,
                except: [:codeviewer, :edit, :update_mark, :view_marks,
                         :create, :add_extra_mark, :next_grouping,
                         :update_overall_comment, :remove_extra_mark,
                         :update_marking_state, :download, :download_zip,
                         :note_message,
                         :update_remark_request, :cancel_remark_request]
  before_filter :authorize_for_ta_and_admin,
                only: [:edit, :update_mark, :create, :add_extra_mark,
                       :next_grouping, :update_overall_comment,
                       :remove_extra_mark, :update_marking_state, :note_message]
  before_filter :authorize_for_user,
                only: [:codeviewer, :download, :download_zip]
  before_filter :authorize_for_student,
                only: [:view_marks, :update_remark_request,
                       :cancel_remark_request]
  after_filter  :update_remark_request_count,
                only: [:update_remark_request, :cancel_remark_request,
                       :set_released_to_students]

  def note_message
    @result = Result.find(params[:id])
    if params[:success]
      flash[:note_success] = I18n.t('notes.success')
    else
      flash[:fail_notice] = I18n.t('notes.error')
    end
  end

  def edit
    @result = Result.find(params[:id])
    @assignment = @result.submission.assignment
    @submission = @result.submission

    if @submission.remark_submitted?
      @old_result = @submission.get_original_result
    else
      @old_result = nil
    end

    @grouping = @result.submission.grouping
    @not_associated_tags = get_tags_not_associated_with_grouping(@grouping.id)
    @group = @grouping.group
    @files = @submission.submission_files.sort do |a, b|
      File.join(a.path, a.filename) <=> File.join(b.path, b.filename)
    end
    @test_result_files = @submission.test_results
    @first_file = @files.first
    @extra_marks_points = @result.extra_marks.points
    @extra_marks_percentage = @result.extra_marks.percentage
    @marks_map = Hash.new
    @old_marks_map = Hash.new
    @mark_criteria = @assignment.get_criteria
    @assignment.get_criteria.each do |criterion|
      mark = criterion.marks.find_or_create_by(result_id: @result.id)
      @marks_map[criterion.id] = mark

      # Loading up previous results for the case of a remark
      if @old_result
        oldmark = criterion.marks.find_or_create_by(result_id: @old_result.id)
        oldmark.save(validate: false)
        @old_marks_map[criterion.id] = oldmark
      end

      mark.save(validate: false)
      @result.update_total_mark
    end

    groupings = Grouping.get_groupings_for_assignment(@assignment,
                                                      current_user)
    # We sort by group name by default
    groupings = groupings.sort do |a, b|
      a.group.group_name <=> b.group.group_name
    end

    current_grouping_index = groupings.index(@grouping)
    if current_grouping_index.nil?
      @next_grouping = groupings.first
      @previous_grouping = groupings.last
    else
      unless groupings[current_grouping_index + 1].nil?
        @next_grouping = groupings[current_grouping_index + 1]
      end
      if (current_grouping_index - 1) >= 0
        @previous_grouping = groupings[current_grouping_index - 1]
      end
    end

    m_logger = MarkusLogger.instance
    m_logger.log("User '#{current_user.user_name}' viewed submission (id: #{@submission.id})" +
                 "of assignment '#{@assignment.short_identifier}' for group '" +
                 "#{@group.group_name}'")

    # Sets up the tags for the tag pane.
    # Creates a variable for all the tags not used
    # and all the tags that are used by the assignment.
    @all_tags = Tag.all
    @grouping_tags = get_tags_for_grouping(@grouping.id)
    @not_grouping_tags = get_tags_not_associated_with_grouping(@grouping.id)

    # Gets the top tags and their usage.
    @top_tags = get_top_tags
    @top_tags_num = Hash.new
    @top_tags.each do |current|
      @top_tags_num[current.id] = get_num_groupings_for_tag(current.id)
    end

    # Respond to AJAX request.
    respond_to do |format|
      format.html
      format.json do
        @request_type = params[:type]

        # Checks the operation requested.
        if @request_type.eql? 'add'
          create_grouping_tag_association_from_existing_tag(
              params[:grouping_id],
              params[:tag_id])
        else
          delete_grouping_tag_association(params[:tag_id],
                                          Grouping.find(params[:grouping_id]))
        end

        # Renders nothing.
        render nothing: true
      end
    end
  end

  ##  Tag Methods  ##

  def add_tag
    create_grouping_tag_association_from_existing_tag(params[:grouping_id],
                                                      params[:tag_id])
    respond_to do |format|
      format.html do
        redirect_to :back
      end
    end
  end

  def remove_tag
    delete_grouping_tag_association(params[:tag_id],
                                    Grouping.find(params[:grouping_id]))
    respond_to do |format|
      format.html do
        redirect_to :back
      end
    end
  end

  def next_grouping
    grouping = Grouping.find(params[:id])
    if grouping.has_submission? && grouping.is_collected?
        redirect_to action: 'edit',
                    id: grouping.current_submission_used.get_latest_result.id
    else
      redirect_to controller: 'submissions',
                  action: 'browse'
    end
  end

  def set_released_to_students
    @result = Result.find(params[:id])
    released_to_students = (params[:value] == 'true')
    if params[:old_id]
      @old_result = Result.find(params[:old_id])
      @old_result.released_to_students = released_to_students
      @old_result.save
    end
    @result.released_to_students = released_to_students
    @result.save
    m_logger = MarkusLogger.instance
    assignment = @result.submission.assignment
    if params[:value] == 'true'
      m_logger.log("Marks released for assignment '#{assignment.short_identifier}', ID: '" +
                   "#{assignment.id}' (for 1 group).")
    else
      m_logger.log("Marks unreleased for assignment '#{assignment.short_identifier}', ID: '" +
                   "#{assignment.id}' (for 1 group).")
    end
  end

  #Updates the marking state
  def update_marking_state
    @result = Result.find(params[:id])
{'fraction_non_alphanumeric': 0.09097029999251889, 'fraction_numerical': 0.0016458442432857037, 'mean_word_length': 2.507740750459197, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1196691', 'n_tokens_mistral': 4165, 'n_tokens_neox': 4033, 'n_words': 795}