<commit_before>class ExamTemplatesController < ApplicationController

  before_filter      :authorize_only_for_admin

  layout 'assignment_content'

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

  # Creates a new instance of the exam template.
  def create
    assignment = Assignment.find(params[:assignment_id])
    # retrieving exam template file
    new_uploaded_io = params[:create_template][:file_io]
    # getting filename
    filename = params[:create_template][:filename]
    # error checking when new_uploaded_io is not pdf, nil, or when filename is not given
    if filename.nil? || new_uploaded_io.nil? || new_uploaded_io.content_type != 'application/pdf'
      flash_message(:error, t('exam_templates.create.failure'))
      redirect_to action: 'index'
      return
    end
    new_file_content = new_uploaded_io.read
    # getting number of pages
    pdf = CombinePDF.parse new_file_content
    num_pages = pdf.pages.length
    # if user didn't include extension of new template (which is pdf), then add '.pdf' to filename
    if File.extname(filename) == ''
      filename = filename + '.pdf'
    end
    # creating corresponding directory and file
    assignment_name = Assignment.find(assignment.id).short_identifier
    template_path = File.join(
      MarkusConfigurator.markus_exam_template_dir,
      assignment_name
    )
    FileUtils.mkdir template_path unless Dir.exists? template_path
    File.open(File.join(template_path, filename), 'wb') do |f|
      f.write new_file_content
    end
    # instantiates new exam template
    new_template = ExamTemplate.new(
      filename: filename,
      num_pages: num_pages,
      assignment: assignment
    )
    # sending flash message if saved
    if new_template.save<commit_msg>remove unneccessary comments and repetition of code<commit_after>class ExamTemplatesController < ApplicationController

  before_filter      :authorize_only_for_admin

  layout 'assignment_content'

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

  # Creates a new instance of the exam template.
  def create
    assignment = Assignment.find(params[:assignment_id])
    new_uploaded_io = params[:create_template][:file_io]
    filename = params[:create_template][:filename]
    # error checking when new_uploaded_io is not pdf, nil, or when filename is not given
    if filename.nil? || new_uploaded_io.nil? || new_uploaded_io.content_type != 'application/pdf'
      flash_message(:error, t('exam_templates.create.failure'))
      redirect_to action: 'index'
      return
    end
    new_file_content = new_uploaded_io.read
    pdf = CombinePDF.parse new_file_content
    num_pages = pdf.pages.length
    # if user didn't include extension of new template (which is pdf), then add '.pdf' to filename
    if File.extname(filename) == ''
      filename = filename + '.pdf'
    end
    # creating corresponding directory and file
    assignment_name = assignment.short_identifier
    template_path = File.join(
      MarkusConfigurator.markus_exam_template_dir,
      assignment_name
    )
    FileUtils.mkdir template_path unless Dir.exists? template_path
    File.open(File.join(template_path, filename), 'wb') do |f|
      f.write new_file_content
    end
    # instantiates new exam template
    new_template = ExamTemplate.new(
      filename: filename,
      num_pages: num_pages,
      assignment: assignment
    )
    # sending flash message if saved
    if new_template.save
{'fraction_non_alphanumeric': 0.07778087538332869, 'fraction_numerical': 0.0, 'mean_word_length': 3.6840731070496084, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1074462', 'n_tokens_mistral': 1047, 'n_tokens_neox': 1036, 'n_words': 271}