<commit_before>require 'fastercsv'

class AnnotationCategoriesController < ApplicationController
  include AnnotationCategoriesHelper

  before_filter      :authorize_only_for_admin

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

  def get_annotations
    @annotation_category = AnnotationCategory.find(params[:assignment_id])
    @annotation_texts = @annotation_category.annotation_texts
  end

  def add_annotation_category
    @assignment = Assignment.find(params[:assignment_id])
    if request.post?
      # Attempt to add Annotation Category
      @annotation_category = AnnotationCategory.new
      @annotation_category.update_attributes(params[:annotation_category])
      @annotation_category.assignment = @assignment
      if !@annotation_category.save
        render :action => 'new_annotation_category_error'
        return
      end
      render :action => 'insert_new_annotation_category'
      return
    end
  end

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

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

  def update_annotation
    @annotation_text = AnnotationText.find(params[:id])
    @annotation_text.update_attributes(params[:annotation_text])
    @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(params[:annotation_text])
      @annotation_text.annotation_category = @annotation_category
      if !@annotation_text.save
        render :action => 'new_annotation_text_error'
        return
      end
      render :action => 'insert_new_annotation_text'
      return
    end
  end

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

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

  def download
    @assignment = Assignment.find(params[:id])
    @annotation_categories = @assignment.annotation_categories
    case params[:format]
    when 'csv'
      send_data convert_to_csv(@annotation_categories),
                :filename => "#{@assignment.short_identifier}_annotations.csv",<commit_msg>Fix: Other errors corrected<commit_after>require 'fastercsv'

class AnnotationCategoriesController < ApplicationController
  include AnnotationCategoriesHelper

  before_filter      :authorize_only_for_admin

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

  def get_annotations
    @annotation_category = AnnotationCategory.find(params[:assignment_id])
    @annotation_texts = @annotation_category.annotation_texts
  end

  def add_annotation_category
    @assignment = Assignment.find(params[:assignment_id])
    if request.post?
      # Attempt to add Annotation Category
      @annotation_category = AnnotationCategory.new
      @annotation_category.update_attributes(params[:annotation_category])
      @annotation_category.assignment = @assignment
      if !@annotation_category.save
        render :action => 'new_annotation_category_error'
        return
      end
      render :action => 'insert_new_annotation_category'
      return
    end
  end

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

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

  def update_annotation
    @annotation_text = AnnotationText.find(params[:id])
    @annotation_text.update_attributes(params[:annotation_text])
    @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(params[:annotation_text])
      @annotation_text.annotation_category = @annotation_category
      if !@annotation_text.save
        render :action => 'new_annotation_text_error'
        return
      end
      render :action => 'insert_new_annotation_text'
      return
    end
  end

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

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

  def download
    @assignment = Assignment.find(params[:assignment_id])
    @annotation_categories = @assignment.annotation_categories
    case params[:format]
    when 'csv'
      send_data convert_to_csv(@annotation_categories),
                :filename => "#{@assignment.short_identifier}_annotations.csv",
{'fraction_non_alphanumeric': 0.08151676131159553, 'fraction_numerical': 0.0007327349331379373, 'mean_word_length': 4.25, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '3515706', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1469, 'n_words': 262}