<commit_before>class Assessment::ScribingQuestionsController < Assessment::QuestionsController

  def create
    assign_params
    extract_tags
    @question.creator = current_user
    qa = @assessment.question_assessments.new
    qa.question = @question.question
    qa.position = @assessment.questions.count

    if params[:assessment_scribing_question][:document]
      file_upload = FileUpload.create({creator: current_user,
                                       owner: @question,
                                       file: params[:assessment_scribing_question][:document]
                                       })
      file_save_success = file_upload.save
    else
      file_save_success = true
    end
    
    respond_to do |format|
      if @question.save && qa.save && file_save_success
        format.html { redirect_to url_for([@course, @assessment.as_assessment]),
                      notice: 'Question has been added.' }
        format.json { render json: @question, status: :created, location: @question }
      else
        format.html { render action: 'new' }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @question.update_attributes(params[:assessment_scribing_question])
        format.html { redirect_to url_for([@course, @assessment.as_assessment]),
                                  notice: 'Question has been updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end


  protected<commit_msg>Convert PDF files to PNG<commit_after>class Assessment::ScribingQuestionsController < Assessment::QuestionsController

  def create
    assign_params
    extract_tags
    @question.creator = current_user
    qa = @assessment.question_assessments.new
    qa.question = @question.question
    qa.position = @assessment.questions.count
    uploaded_file_object = params[:assessment_scribing_question][:document]

    if uploaded_file_object && uploaded_file_object.content_type == 'application/pdf'   #special handling for PDF files
      # write file out to filesystem
      File.open(uploaded_file_object.original_filename, 'wb') do |file|
        file.write(uploaded_file_object.read)
      end
      basename = uploaded_file_object.original_filename[0..-5]

      #TODO: consider and fix security implications
      # Puts together and runs the PDF to PNG conversion command
      convert_cmd = "/usr/bin/pdftoppm -png -r 300 #{uploaded_file_object.original_filename} "
      convert_cmd += "#{basename}"
      `#{convert_cmd}`

      file_save_success = true

      # cleanup PDF file
      File.delete(uploaded_file_object.original_filename)
      
    elsif uploaded_file_object
      file_upload = FileUpload.create({creator: current_user,
                                       owner: @question,
                                       file: uploaded_file_object
                                       })
      file_save_success = file_upload.save
    else
      file_save_success = true
    end
    
    respond_to do |format|
      if @question.save && qa.save && file_save_success
        format.html { redirect_to url_for([@course, @assessment.as_assessment]),
                      notice: "Question has been added." }
        format.json { render json: @question, status: :created, location: @question }
      else
        format.html { render action: 'new' }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @question.update_attributes(params[:assessment_scribing_question])
        format.html { redirect_to url_for([@course, @assessment.as_assessment]),
                                  notice: 'Question has been updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end


  protected
{'fraction_non_alphanumeric': 0.08401344215074412, 'fraction_numerical': 0.001200192030724916, 'mean_word_length': 2.4016326530612244, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5710259', 'n_tokens_mistral': 1162, 'n_tokens_neox': 1113, 'n_words': 262}