<commit_before>require_dependency "discovery/application_controller"

module Discovery
  class QuestionsController < ApplicationController
    before_filter :set_question, only: [:show, :edit, :update, :destroy]
    before_filter :current_question, :go_to_next_question, only: [:index]
    before_filter :next_question, :previous_question, only: [:show]
    before_filter :authenticate_user!, only: [:show, :edit, :update, :destroy]

    # GET /questions
    def index
      @questions = Discovery::Question.all
    end

    def show
      if answer_update = current_user.answers.find_by_question(Discovery::Question.find(params[:id]))
        @answer = answer_update
      else
        @answer = Discovery::Answer.new
        @answer.question = @question.id
        @answer.user_id = current_user.id
        if @question.question_type == "self-guided"
          if @question.personality_type == "Extravert/Introvert"
            @answer.user.ei = @answer.value
          elsif @question.personality_type == "Sensory/Intuitive"
            @answer.user.si = @answer.value
          elsif @question.personality_type == "Thinking/Feeling"
            @answer.user.tf = @answer.value
          elsif @question.personality_type == "Judging/Perceiving"
            @answer.user.jp = @answer.value
          end
        end
      end
      
      
    end

    # GET /questions/new
    def new
      @question = Discovery::Question.new 
    end

    # GET /questions/1/edit
    def edit
    end

    # POST /questions
    def create
      @question = Discovery::Question.new(params[:question])

      if @question.save
        redirect_to @question, notice: 'Question was successfully created.'
      else
        render action: 'new'
      end
    end

    # PATCH/PUT /questions/1
    def update
      if @question.update_attributes(params[:question])
        redirect_to @question, notice: 'Question was successfully updated.'
      else
        render action: 'edit'
      end
    end

    # DELETE /questions/1
    def destroy
      @question.destroy
      redirect_to questions_url, notice: 'Question was successfully destroyed.'
    end

    private
      # If you've answered some questions let's find out what question you're on.
      def current_question 
        if current_user
          @n = 1
          while current_user.answers.where(id: @n).present?
            @n += 1
          end
          if current_user.answers.last   # if a user has any answers 
            @last_answered = current_user.answers.last
            @last_question = Discovery::Question.find(@last_answered.question)
          end
        end
      end
      # Once we've found out what question you're on let's send you to the next question
      def go_to_next_question
        if current_user && current_user.answers.last
          # Self-Guided Routing
          if @question.question_type == "self-guided"
            @go_to_next_question = @last_question.id + 1
          # Normal Personality Quiz Routing
          elsif @question.question_type == "quiz"
            @go_to_next_question = @last_question.id + 10
            if @go_to_next_question > 70
              @go_to_next_question = @go_to_next_question - 69
            end
          end
        end
      end

      # Go to next question while taking personality quiz
      def next_question
        if @question.question_type == "quiz"
          @next_question = @question.id + 10
          if @next_question > 70
            @next_question = @next_question - 69<commit_msg>updated questions controller for discovery index page<commit_after>require_dependency "discovery/application_controller"

module Discovery
  class QuestionsController < ApplicationController
    before_filter :set_question, only: [:show, :edit, :update, :destroy]
    before_filter :current_question, :go_to_next_question, only: [:index]
    before_filter :next_question, :previous_question, only: [:show]
    before_filter :authenticate_user!, only: [:show, :edit, :update, :destroy]

    # GET /questions
    def index
      @questions = Discovery::Question.all
    end

    def show
      if answer_update = current_user.answers.find_by_question(Discovery::Question.find(params[:id]))
        @answer = answer_update
      else
        @answer = Discovery::Answer.new
        @answer.question = @question.id
        @answer.user_id = current_user.id
        if @question.question_type == "self-guided"
          if @question.personality_type == "Extravert/Introvert"
            @answer.user.ei = @answer.value
          elsif @question.personality_type == "Sensory/Intuitive"
            @answer.user.si = @answer.value
          elsif @question.personality_type == "Thinking/Feeling"
            @answer.user.tf = @answer.value
          elsif @question.personality_type == "Judging/Perceiving"
            @answer.user.jp = @answer.value
          end
        end
      end
      
      
    end

    # GET /questions/new
    def new
      @question = Discovery::Question.new 
    end

    # GET /questions/1/edit
    def edit
    end

    # POST /questions
    def create
      @question = Discovery::Question.new(params[:question])

      if @question.save
        redirect_to @question, notice: 'Question was successfully created.'
      else
        render action: 'new'
      end
    end

    # PATCH/PUT /questions/1
    def update
      if @question.update_attributes(params[:question])
        redirect_to @question, notice: 'Question was successfully updated.'
      else
        render action: 'edit'
      end
    end

    # DELETE /questions/1
    def destroy
      @question.destroy
      redirect_to questions_url, notice: 'Question was successfully destroyed.'
    end

    private
      # If you've answered some questions let's find out what question you're on.
      def current_question 
        if current_user
          @n = 1
          while current_user.answers.where(id: @n).present?
            @n += 1
          end
          if current_user.answers.last   # if a user has any answers 
            @last_answered = current_user.answers.last
            @last_question = Discovery::Question.find(@last_answered.question)
          end
        end
      end
      # Once we've found out what question you're on let's send you to the next question
      def go_to_next_question
        if current_user && current_user.answers.last
          # Self-Guided Routing
          if current_question.question_type == "self-guided"
            @go_to_next_question = @last_question.id + 1
            # if @go_to_next_question == 75
            #   redirect_to results_path
            # end
          # Normal Personality Quiz Routing
          elsif current_question.question_type == "quiz"
            @go_to_next_question = @last_question.id + 10
            if @go_to_next_question > 70
              @go_to_next_question = @go_to_next_question - 69
            end
          end
        end
      end

      # Go to next question while taking personality quiz
      def next_question
        if @question.question_type == "quiz"
          @next_question = @question.id + 10
          if @next_question > 70
            @next_question = @next_question - 69
{'fraction_non_alphanumeric': 0.08380740946302206, 'fraction_numerical': 0.005272651588733176, 'mean_word_length': 2.2279444693237798, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '5797', 'n_tokens_mistral': 2125, 'n_tokens_neox': 1982, 'n_words': 519}