Module: Moodle2CC::Moodle2Converter::QuestionnaireConverter

Includes:
ConverterHelper
Included in:
AssessmentConverter
Defined in:
lib/moodle2cc/moodle2converter/questionnaire_converter.rb

Constant Summary collapse

QUESTION_TYPE_ID_MAP =
{
    1 =>  'multiple_choice_question', # yes/no question
    2 =>  'essay_question', # text box question
    3 =>  'essay_question', # essay box question
    4 =>  'multiple_choice_question', # radio buttons question
    5 =>  'multiple_answers_question', # check boxes question
    6 =>  'multiple_choice_question', # dropdown box question
    8 =>  'multiple_dropdowns_question', # rate 1..5 question
    9 =>  'essay_question', # date question
    10 =>  'numerical_question', # numeric question
    100 =>  'text_only_question', # label question
}

Constants included from ConverterHelper

ConverterHelper::ACTIVITY_LOOKUP, ConverterHelper::ASSESSMENT_SUFFIX, ConverterHelper::ASSIGNMENT_SUFFIX, ConverterHelper::CHAPTER_SUFFIX, ConverterHelper::CHOICE_ASSESSMENT_SUFFIX, ConverterHelper::COURSE_SUFFIX, ConverterHelper::DISCUSSION_SUFFIX, ConverterHelper::EXTERNAL_URL_SUFFIX, ConverterHelper::FEEDBACK_ASSESSMENT_SUFFIX, ConverterHelper::FILE_SUFFIX, ConverterHelper::FOLDER_SUFFIX, ConverterHelper::GLOSSARY_SUFFIX, ConverterHelper::INTRO_SUFFIX, ConverterHelper::LTI_SUFFIX, ConverterHelper::MAX_TITLE_LENGTH, ConverterHelper::MODULE_SUFFIX, ConverterHelper::PAGE_SUFFIX, ConverterHelper::QUESTIONNAIRE_ASSESSMENT_SUFFIX, ConverterHelper::QUESTION_BANK_SUFFIX, ConverterHelper::SUMMARY_PAGE_SUFFIX

Instance Method Summary collapse

Methods included from ConverterHelper

#activity_content_type, #generate_unique_identifier, #generate_unique_identifier_for, #generate_unique_identifier_for_activity, #generate_unique_resource_path, #get_unique_identifier_for_activity, #truncate_text, #workflow_state

Instance Method Details

#convert_questionnaire(moodle_questionnaire) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/moodle2cc/moodle2converter/questionnaire_converter.rb', line 5

def convert_questionnaire(moodle_questionnaire)
  canvas_assessment = Moodle2CC::CanvasCC::Models::Assessment.new
  canvas_assessment.identifier = generate_unique_identifier_for_activity(moodle_questionnaire)
  canvas_assessment.title = truncate_text(moodle_questionnaire.name)
  canvas_assessment.description = moodle_questionnaire.intro
  canvas_assessment.workflow_state = workflow_state(moodle_questionnaire.visible)

  canvas_assessment.lock_at = Time.at(Integer(moodle_questionnaire.close_date)) if moodle_questionnaire.close_date
  canvas_assessment.unlock_at = Time.at(Integer(moodle_questionnaire.open_date)) if moodle_questionnaire.open_date

  canvas_assessment.scoring_policy = 'keep_latest'
  canvas_assessment.quiz_type = 'survey'

  canvas_assessment.items = []
  moodle_questionnaire.questions.each do |question|
    next if question.deleted
    if canvas_question = convert_questionnaire_question(question)
      canvas_assessment.items << canvas_question
    end
  end

  canvas_assessment
end

#convert_questionnaire_question(moodle_question) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/moodle2cc/moodle2converter/questionnaire_converter.rb', line 42

def convert_questionnaire_question(moodle_question)
  return unless canvas_type = QUESTION_TYPE_ID_MAP[moodle_question.type_id.to_i]

  canvas_question = Moodle2CC::CanvasCC::Models::Question.create(canvas_type)
  canvas_question.identifier = generate_unique_identifier_for(moodle_question.id, "_questionnaire_question")
  canvas_question.title = truncate_text(moodle_question.name)
  canvas_question.material = moodle_question.content
  canvas_question.answers = []

  if moodle_question.type_id.to_i == 1
    # yes/no question
    moodle_question.choices = [{:id => 'yes', :content => "Yes"}, {:id => 'no', :content => "No"}]
  end

  if canvas_type == 'multiple_dropdowns_question'
    # rating scale question
    convert_rating_question(moodle_question, canvas_question)
  else
    moodle_question.choices.each do |choice|
      answer = Moodle2CC::CanvasCC::Models::Answer.new
      answer.id = choice[:id]
      answer.answer_text = choice[:content]
      canvas_question.answers << answer
    end
  end

  canvas_question
end

#convert_rating_question(moodle_question, canvas_question) ⇒ Object

For l..x rating questions



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/moodle2cc/moodle2converter/questionnaire_converter.rb', line 72

def convert_rating_question(moodle_question, canvas_question)
  choices = create_rating_choices(moodle_question)
  canvas_question.responses = []

  moodle_question.choices.each_with_index do |answer, answer_idx|
    response = {:id => "response#{answer_idx + 1}", :choices => []}

    # add dropdown to the question text
    canvas_question.material = canvas_question.material.to_s + "<p>#{answer[:content]} [#{response[:id]}]</p>"

    choices.each_with_index do |choice, choice_idx|
      response[:choices] << {:id => "#{moodle_question.id}_choice_#{answer_idx}_#{choice_idx}", :text => choice}
    end

    canvas_question.responses << response
  end
end

#create_rating_choices(moodle_question) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/moodle2cc/moodle2converter/questionnaire_converter.rb', line 90

def create_rating_choices(moodle_question)
  scale = (moodle_question.length || 5).to_i
  choices = (1..scale).map(&:to_s)
  if moodle_question.precise.to_i == 1
    choices << "N/A"
  end
  choices
end