app/models/uploaded_exercise.rb
# -------------------------------------------------------------
# Public: Creates the exercises with corresponding choices
#
# Returns true if the spreadsheet was processed successfully, otherwise false.
#

Creates the exercises with corresponding choices

Returns true if the spreadsheet was processed successfully, otherwise false.
Creates the exercises with corresponding choices Returns true if the spreadsheet was processed successfully otherwise false .
def save
    @csv[0..-1].each_with_index do |row, i|
      if @csv.first.count != NUMBER_OF_COLUMNS
        false
      end
      exercise = Exercise.new
      type = row[COLUMN_TYPE]
      if type.eql?"MC"
        exercise.question_type = Exercise.type_mc
        exercise.mcq_allow_multiple = false
        exercise.mcq_is_scrambled = false
        ans = row[COLUMN_ANSWER].upcase.ord - 'A'.ord 
        (COLUMN_CHOICE_A..COLUMN_CHOICE_J).each do |index|
          choice = Choice.new
          choice.answer = row[index]
          choice_num = index - COLUMN_CHOICE_A
          choice.position = choice_num + 1
          if ans == choice_num
            choice.value = 100
          else
            choice.value = 0
          end
          Tag.tag_this_with(exercise, "imported", Tag.misc)
          unless choice.answer.nil?
            exercise.choices << choice
            choice.save
          end
        end
      end
      exercise.count_attempts = 0
      exercise.count_correct = 0
      exercise.name = "Imported " + index
      exercise.is_public = true
      exercise.priority = 0
      exercise.experience = 100
      exercise.difficulty = 100
      exercise.discrimination = 0
      exercise.question = row[COLUMN_PROMPT]
      if exercise.save
        @exercises_created << exercise
        true
      else
        false
      end
    end
  end
def save @csv [ 0 .. - 1 ] . each_with_index do | row , i | if @csv . first . count != NUMBER_OF_COLUMNS false end exercise = Exercise . new type = row [ COLUMN_TYPE ] if type . eql? "MC" exercise . question_type = Exercise . type_mc exercise . mcq_allow_multiple = false exercise . mcq_is_scrambled = false ans = row [ COLUMN_ANSWER ] . upcase . ord - 'A' . ord ( COLUMN_CHOICE_A .. COLUMN_CHOICE_J ) . each do | index | choice = Choice . new choice . answer = row [ index ] choice_num = index - COLUMN_CHOICE_A choice . position = choice_num + 1 if ans == choice_num choice . value = 100 else choice . value = 0 end Tag . tag_this_with ( exercise , "imported" , Tag . misc ) unless choice . answer . nil? exercise . choices << choice choice . save end end end exercise . count_attempts = 0 exercise . count_correct = 0 exercise . name = "Imported " + index exercise . is_public = true exercise . priority = 0 exercise . experience = 100 exercise . difficulty = 100 exercise . discrimination = 0 exercise . question = row [ COLUMN_PROMPT ] if exercise . save @exercises_created << exercise true else false end end end
Public: Creates the exercises with corresponding choices
Public : Creates the exercises with corresponding choices
# Process each row as an exercise
 # Make sure all columns are present
 #TODO determine if scrambled questions and/or if multiple selections are allowed
 #A=0, B=1, ...
 #TODO tag exercises intelligently, not just hardcoded "imported"
 #TODO get (exercise name, is_public, priority, experience, difficulty) not hardcoded
