<commit_before>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_overall_remark_comment,
                         :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,
                       :update_overall_remark_comment]
  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_id = params[:id]
    @result = Result.find(result_id)
    @assignment = @result.submission.assignment
    @submission = @result.submission

    @old_result = nil
    if @submission.remark_submitted?
      @old_result = Result.all(conditions: ['submission_id = ?', @submission.id],
                               order: ['id ASC'])[0]
    end

    @annotation_categories = @assignment.annotation_categories
    @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

        unless oldmark.nil?
          # Updates the current mark to reflect the previous mark
          mark.mark = oldmark.mark<commit_msg>Fixed the issue with viewing remarks as a grader.<commit_after>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_overall_remark_comment,
                         :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,
                       :update_overall_remark_comment]
  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_id = params[:id]
    @result = Result.find(result_id)
    @assignment = @result.submission.assignment
    @submission = @result.submission

    @old_result = nil
    if @submission.remark_submitted?
      @old_result = Result.all(conditions: ['submission_id = ?', @submission.id],
                               order: ['id ASC'])[0]
    end

    @annotation_categories = @assignment.annotation_categories
    @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

        unless oldmark.nil? or @result.released_to_students
          # Updates the current mark to reflect the previous mark
          mark.mark = oldmark.mark
{'fraction_non_alphanumeric': 0.09198572355613238, 'fraction_numerical': 0.0016223231667748216, 'mean_word_length': 2.752282410225198, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3852700', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1851, 'n_words': 329}