<commit_before>require 'encoding'

class AnnotationCategoriesController < ApplicationController
  include AnnotationCategoriesHelper

  before_filter      :authorize_only_for_admin

  def index
    @assignment = Assignment.find(params[:assignment_id])
    @annotation_categories = @assignment.annotation_categories(order: 'position')
    render layout: 'assignment_content'
  end

  def new
    @assignment = Assignment.find(params[:assignment_id])
  end

  def create
    @assignment = Assignment.find(params[:assignment_id])
    @annotation_category = @assignment.annotation_categories
                                      .new(annotation_category_params)
    if @annotation_category.save
      render :insert_new_annotation_category
    else
      render :new_annotation_category_error
    end
  end

  def show
    @assignment = Assignment.find(params[:assignment_id])
    @annotation_category = AnnotationCategory.find(params[:id])
  end

  def destroy
    @annotation_category = AnnotationCategory.find(params[:id])
    @annotation_category.destroy
  end

  def update
    @assignment = Assignment.find(params[:assignment_id])
    @annotation_category = AnnotationCategory.find(params[:id])

    @annotation_category.update_attributes(annotation_category_params)
    if @annotation_category.save
      flash.now[:success] = I18n.t('annotations.update.annotation_category_success')
    else
      flash.now[:error] = @annotation_category.errors.full_messages
    end
  end

  def update_annotation
    @annotation_text = AnnotationText.find(params[:id])
    @annotation_text.update_attributes(annotation_text_params)
    @annotation_text.last_editor_id = current_user.id
    @annotation_text.save
  end

  def add_annotation_text
    @annotation_category = AnnotationCategory.find(params[:id])
    if request.post?
      # Attempt to add Annotation Text
      @annotation_text = AnnotationText.new
      @annotation_text.update_attributes(annotation_text_params)
      @annotation_text.annotation_category = @annotation_category
      @annotation_text.creator_id = current_user.id
      @annotation_text.last_editor_id = current_user.id
      unless @annotation_text.save
        render :new_annotation_text_error
        return
      end
      @assignment = Assignment.find(params[:assignment_id])
      render :insert_new_annotation_text
    end
  end

  def delete_annotation_text
    @assignment = Assignment.find(params[:assignment_id])
    @annotation_text = AnnotationText.find(params[:id])
    @annotation_text.destroy
  end

  def find_annotation_text
    string = params[:string]
    annotation_texts = AnnotationText.where("content LIKE ?", "#{string}%")
    if annotation_texts.size == 1
      render json: "#{annotation_texts.first.content}".html_safe
    else
      render json: ''.html_safe
    end
  end

  # This method handles the drag/drop Annotations sorting
  def update_positions
    unless request.post?
      render nothing: true
      return
    end

    @assignment = Assignment.find(params[:assignment_id])
    @annotation_categories = @assignment.annotation_categories
    position = 0

    params[:annotation_category].each do |id|
      if id != ''
        position += 1
        AnnotationCategory.update(id, position: position)
      end
    end
  end
<commit_msg>issue-2561: Only show texts for current assignment<commit_after>require 'encoding'

class AnnotationCategoriesController < ApplicationController
  include AnnotationCategoriesHelper

  before_filter      :authorize_only_for_admin

  def index
    @assignment = Assignment.find(params[:assignment_id])
    @annotation_categories = @assignment.annotation_categories(order: 'position')
    render layout: 'assignment_content'
  end

  def new
    @assignment = Assignment.find(params[:assignment_id])
  end

  def create
    @assignment = Assignment.find(params[:assignment_id])
    @annotation_category = @assignment.annotation_categories
                                      .new(annotation_category_params)
    if @annotation_category.save
      render :insert_new_annotation_category
    else
      render :new_annotation_category_error
    end
  end

  def show
    @assignment = Assignment.find(params[:assignment_id])
    @annotation_category = AnnotationCategory.find(params[:id])
  end

  def destroy
    @annotation_category = AnnotationCategory.find(params[:id])
    @annotation_category.destroy
  end

  def update
    @assignment = Assignment.find(params[:assignment_id])
    @annotation_category = AnnotationCategory.find(params[:id])

    @annotation_category.update_attributes(annotation_category_params)
    if @annotation_category.save
      flash.now[:success] = I18n.t('annotations.update.annotation_category_success')
    else
      flash.now[:error] = @annotation_category.errors.full_messages
    end
  end

  def update_annotation
    @annotation_text = AnnotationText.find(params[:id])
    @annotation_text.update_attributes(annotation_text_params)
    @annotation_text.last_editor_id = current_user.id
    @annotation_text.save
  end

  def add_annotation_text
    @annotation_category = AnnotationCategory.find(params[:id])
    if request.post?
      # Attempt to add Annotation Text
      @annotation_text = AnnotationText.new
      @annotation_text.update_attributes(annotation_text_params)
      @annotation_text.annotation_category = @annotation_category
      @annotation_text.creator_id = current_user.id
      @annotation_text.last_editor_id = current_user.id
      unless @annotation_text.save
        render :new_annotation_text_error
        return
      end
      @assignment = Assignment.find(params[:assignment_id])
      render :insert_new_annotation_text
    end
  end

  def delete_annotation_text
    @assignment = Assignment.find(params[:assignment_id])
    @annotation_text = AnnotationText.find(params[:id])
    @annotation_text.destroy
  end

  def find_annotation_text
    @assignment = Assignment.find(params[:assignment_id])
    string = params[:string]
    annotation_texts = AnnotationText.where("content LIKE ?", "#{string}%")
    texts_for_current_assignment = annotation_texts.find_all{|a| a.annotations.first.result.submission.grouping.assignment == @assignment}
    if texts_for_current_assignment.size == 1
      render json: "#{texts_for_current_assignment.first.content}".html_safe
    else
      render json: ''.html_safe
    end
  end

  # This method handles the drag/drop Annotations sorting
  def update_positions
    unless request.post?
      render nothing: true
      return
    end

    @assignment = Assignment.find(params[:assignment_id])
    @annotation_categories = @assignment.annotation_categories
    position = 0

    params[:annotation_category].each do |id|
      if id != ''
        position += 1
        AnnotationCategory.update(id, position: position)
      end
    end
  end

{'fraction_non_alphanumeric': 0.08206997084548105, 'fraction_numerical': 0.0020408163265306124, 'mean_word_length': 3.8763326226012795, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '973607', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1919, 'n_words': 360}