Class: ActiveLrs::Xapi::ActivityDefinition

Inherits:
Object
  • Object
show all
Includes:
LocalizationHelper
Defined in:
lib/active_lrs/xapi/activity_definition.rb

Overview

Represents an xAPI ActivityDefinition object.

ActivityDefinition provides metadata for an Activity. It describes the nature, type, and content of the activity, including human-readable labels, interaction type, scoring, and other extensions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LocalizationHelper

#get_localized_value

Constructor Details

#initialize(attributes = {}) ⇒ void

Initializes a new ActivityDefinition instance.

Options Hash (attributes):

  • "name" (Hash{String => String})

    Language map for the activity’s name.

  • "description" (Hash{String => String})

    Language map for description.

  • "type" (String)

    IRI of the activity type.

  • "moreInfo" (String)

    Documentation URL of the activity.

  • "interactionType" (String)

    Interaction type string.

  • "correctResponsesPattern" (Array<String>)

    Correct response patterns.

  • "choices" (Array<Hash>)

    Array of InteractionComponent hashes.

  • "scale" (Array<Hash>)

    Array of InteractionComponent hashes.

  • "source" (Array<Hash>)

    Array of InteractionComponent hashes.

  • "target" (Array<Hash>)

    Array of InteractionComponent hashes.

  • "steps" (Array<Hash>)

    Array of InteractionComponent hashes.

  • "extensions" (Hash)

    Key/value pairs for extensions.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/active_lrs/xapi/activity_definition.rb', line 72

def initialize(attributes = {})
  self.name = attributes["name"] if attributes["name"]
  self.description = attributes["description"] if attributes["description"]
  self.type = attributes["type"] if attributes["type"]
  self.more_info = attributes["moreInfo"] if attributes["moreInfo"]
  self.interaction_type = attributes["interactionType"] if attributes["interactionType"]
  self.correct_responses_pattern = attributes["correctResponsesPattern"] if attributes["correctResponsesPattern"]
  self.choices = attributes["choices"]&.map { |choice| InteractionComponent.new(choice) }
  self.scale = attributes["scale"]&.map { |scale| InteractionComponent.new(scale) }
  self.source = attributes["source"]&.map { |source| InteractionComponent.new(source) }
  self.target = attributes["target"]&.map { |target| InteractionComponent.new(target) }
  self.steps = attributes["steps"]&.map { |step| InteractionComponent.new(step) }
  self.extensions = attributes["extensions"] if attributes["extensions"]
end

Instance Attribute Details

#choicesArray<InteractionComponent>?



38
39
40
# File 'lib/active_lrs/xapi/activity_definition.rb', line 38

def choices
  @choices
end

#correct_responses_patternArray<String>?



35
36
37
# File 'lib/active_lrs/xapi/activity_definition.rb', line 35

def correct_responses_pattern
  @correct_responses_pattern
end

#descriptionHash{String => String}?



20
21
22
# File 'lib/active_lrs/xapi/activity_definition.rb', line 20

def description
  @description
end

#extensionsHash{String => Object}?



53
54
55
# File 'lib/active_lrs/xapi/activity_definition.rb', line 53

def extensions
  @extensions
end

#interaction_typeString?



32
33
34
# File 'lib/active_lrs/xapi/activity_definition.rb', line 32

def interaction_type
  @interaction_type
end

#more_infoString?

human-readable information about the Activity. This may include instructions, descriptions, or a way to launch the activity.



28
29
30
# File 'lib/active_lrs/xapi/activity_definition.rb', line 28

def more_info
  @more_info
end

#nameHash{String => String}?



17
18
19
# File 'lib/active_lrs/xapi/activity_definition.rb', line 17

def name
  @name
end

#scaleArray<InteractionComponent>?



41
42
43
# File 'lib/active_lrs/xapi/activity_definition.rb', line 41

def scale
  @scale
end

#sourceArray<InteractionComponent>?



44
45
46
# File 'lib/active_lrs/xapi/activity_definition.rb', line 44

def source
  @source
end

#stepsArray<InteractionComponent>?



50
51
52
# File 'lib/active_lrs/xapi/activity_definition.rb', line 50

def steps
  @steps
end

#targetArray<InteractionComponent>?



47
48
49
# File 'lib/active_lrs/xapi/activity_definition.rb', line 47

def target
  @target
end

#typeString?



23
24
25
# File 'lib/active_lrs/xapi/activity_definition.rb', line 23

def type
  @type
end

Instance Method Details

#localize_description(locale: nil) ⇒ String

Returns the localized description of the activity.



159
160
161
# File 'lib/active_lrs/xapi/activity_definition.rb', line 159

def localize_description(locale: nil)
  get_localized_value(description, locale)
end

#localize_name(locale: nil) ⇒ String

Returns the localized name of the activity.



151
152
153
# File 'lib/active_lrs/xapi/activity_definition.rb', line 151

def localize_name(locale: nil)
  get_localized_value(name, locale)
end

#to_hHash{String => Object}

Converts the ActivityDefinition into a hash suitable for inclusion in an xAPI Activity object.

Examples:

activity_def = ActiveLrs::Xapi::ActivityDefinition.new(
  "name" => { "en-US" => "Example Course" },
  "description" => { "en-US" => "An introductory example." },
  "type" => "http://adlnet.gov/expapi/activities/course",
  "interactionType" => "choice",
  "correctResponsesPattern" => ["golf", "tennis"],
  "choices" => [
    { "id" => "golf", "description" => { "en-US" => "Golf" } },
    { "id" => "tennis", "description" => { "en-US" => "Tennis" } }
  ],
  "moreInfo" => "http://example.com/course"
)
activity_def.to_h
# => {
#   "name" => { "en-US" => "Example Course" },
#   "description" => { "en-US" => "An introductory example." },
#   "type" => "http://adlnet.gov/expapi/activities/course",
#   "interactionType" => "choice",
#   "correctResponsesPattern" => ["golf", "tennis"],
#   "choices" => [
#     { "id" => "golf", "description" => { "en-US" => "Golf" } },
#     { "id" => "tennis", "description" => { "en-US" => "Tennis" } }
#   ],
#   "moreInfo" => "http://example.com/course"
# }


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/active_lrs/xapi/activity_definition.rb', line 118

def to_h
  node = {}
  node["name"] = name if name
  node["description"] = description if description
  node["type"] = type.to_s if type
  node["moreInfo"] = more_info.to_s if more_info
  node["extensions"] = extensions if extensions
  if interaction_type
    node["interactionType"] = interaction_type.to_s
    case interaction_type
    when "choice", "sequencing"
      node["choices"] = choices.map { |element| element.to_h } if choices && choices.any?
    when "likert"
      node["scale"] = scale.map { |element| element.to_h } if scale && scale.any?
    when "matching"
      node["source"] = source.map { |element| element.to_h } if source && source.any?
      node["target"] = target.map { |element| element.to_h } if target && target.any?
    when "performance"
      node["steps"] = steps.map { |element| element.to_h } if steps && steps.any?
    end
  end

  if correct_responses_pattern&.any?
    node["correctResponsesPattern"] = correct_responses_pattern.map { |element| element }
  end

  node
end