Class: Ruql::Canvas

Inherits:
Object
  • Object
show all
Defined in:
lib/ruql/canvas/canvas.rb,
lib/ruql/canvas/version.rb

Defined Under Namespace

Classes: CanvasApiError

Constant Summary collapse

VERSION =
"1.0.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(quiz, options = {}) ⇒ Canvas

Returns a new instance of Canvas.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruql/canvas/canvas.rb', line 17

def initialize(quiz,options={})
  @quiz = quiz
  @logger = quiz.logger
  @gem_root = Gem.loaded_specs['ruql-canvas'].full_gem_path rescue '.'
  @default_quiz_options_file = File.join(@gem_root, 'templates', 'quiz.yml')
  # read YAML options
  yaml_options = options.delete('--canvas-config') || raise(Ruql::OptionsError.new("Canvas config file must be provided"))
  yaml = YAML.load_file(yaml_options)
  logger.warn "Doing dry run without making any changes" if
    (@dry_run = yaml['dry_run'] || options['--canvas-dry-run'])
  @canvas_options = yaml['canvas']
  set_canvas_options(@canvas_options)

  @quiz_options = YAML.load_file(@default_quiz_options_file)['quiz'].merge(yaml['quiz'] || {})
  @quiz_id = @quiz_options['quiz_id'] # may be nil if new quiz is to be created.
  logger.info "Using quiz options:\n#{@quiz_options.inspect}"

  verify_valid_quiz!
  
  @current_group_id = nil
  @response = nil
  @group_count = 0
  @qcount = 0

  @output = ''
  
end

Instance Attribute Details

#default_quiz_options_fileObject (readonly)

Returns the value of attribute default_quiz_options_file.



12
13
14
# File 'lib/ruql/canvas/canvas.rb', line 12

def default_quiz_options_file
  @default_quiz_options_file
end

#dry_runObject (readonly)

Returns the value of attribute dry_run.



14
15
16
# File 'lib/ruql/canvas/canvas.rb', line 14

def dry_run
  @dry_run
end

#loggerObject (readonly)

Returns the value of attribute logger.



13
14
15
# File 'lib/ruql/canvas/canvas.rb', line 13

def logger
  @logger
end

#outputObject (readonly)

Returns the value of attribute output.



15
16
17
# File 'lib/ruql/canvas/canvas.rb', line 15

def output
  @output
end

#quizObject (readonly)

Returns the value of attribute quiz.



11
12
13
# File 'lib/ruql/canvas/canvas.rb', line 11

def quiz
  @quiz
end

Class Method Details

.allowed_optionsObject



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
70
71
# File 'lib/ruql/canvas/canvas.rb', line 45

def self.allowed_options
  opts = [
    ['--canvas-config', GetoptLong::REQUIRED_ARGUMENT],
    ['--canvas-dry-run', GetoptLong::NO_ARGUMENT],
  ]
  help = <<eos
The Canvas renderer adds the given file as a quiz in Canvas, with these options:
  --canvas-dry-run  - don't actually change anything, but do all non-state-changing API calls.
           You can also set dry_run: true in the YAML file instead.  The presence of either
           mechanism will turn this into a dry run.
  --canvas-config=file.yml - A Yaml file that must contain at least the following:
dry_run: true   #  don't actually change anything, but check API connection and parsability
canvas:
  api_base: https://bcourses.berkeley.edu/api/v1 # base URL for Canvas LMS API
  auth_token: 012345    #  Bearer auth token for Canvas API
  course_id: 99999      #  Course ID in Canvas to which a NEW quiz should be added
  # If quiz_id given, replace that quiz's questions and time limit; if not, create new
  quiz_id: 99999
  #  How quiz time limit is set, eg 20-point quiz = 40 minutes. Default is 1
  minutes_per_point: 2  
  # Fixed extra time in addition to minutes/point. Default 5. Total time rounded up to multiple of 5.
  extra_minutes: 5
quiz:
  # various options that control quiz; defaults are in #{@default_quiz_options_file}
eos
  return [help, opts]
end

Instance Method Details

#render_quizObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ruql/canvas/canvas.rb', line 87

def render_quiz
  if @quiz_id
    truncate_quiz!
    @output = "Existing quiz #{@quiz_id}"
  else
    create_quiz!
    @output = "New quiz #{@quiz_id}"
  end
  emit_ungrouped_questions
  emit_grouped_questions
  @output << " now has #{@qcount} questions in #{@group_count} pool(s)"
end

#verify_valid_quiz!Object

We must set up the following before adding questions:

1) create quiz with given options (unless quiz ID is given)
2) for each question in quiz, sorted so that same-pool questions are together:
   - if part of a new pool, start new QuizGroup
   - add it to the quiz, specifying its group

docs: canvas.instructure.com/doc/api/quizzes,quiz_question_groups,quiz_questions.html



80
81
82
83
84
85
# File 'lib/ruql/canvas/canvas.rb', line 80

def verify_valid_quiz!
  unless quiz.questions.all? { |q| MultipleChoice === q || SelectMultiple === q }
    raise Ruql::QuizContentError.new(
      "Canvas renderer currently only supports Multiple Choice and Select All That Apply questions")
  end
end