Class: ActiveLrs::Xapi::ContextActivities

Inherits:
Object
  • Object
show all
Defined in:
lib/active_lrs/xapi/context_activities.rb

Overview

Represents an xAPI ContextActivities object.

ContextActivities provide contextual information about the Activity related to a Statement. They allow grouping, categorization, or relationships to other Activities.

Each property is an array of Activity objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ void

Initializes a new ContextActivity instance with optional context activity attributes.

Parameters:

  • attributes (Hash) (defaults to: {})

    a hash of context activity attributes

Options Hash (attributes):

  • "parent" (Array<Hash>)

    Array of Activity hashes describing parent context.

  • "grouping" (Array<Hash>)

    Array of Activity hashes describing grouping context.

  • "category" (Array<Hash>)

    Array of Activity hashes describing categorization context.

  • "other" (Array<Hash>)

    Array of Activity hashes describing other context.



40
41
42
43
44
45
# File 'lib/active_lrs/xapi/context_activities.rb', line 40

def initialize(attributes = {})
  self.parent = Array(attributes["parent"]).map { |element| Activity.new(element) } if attributes["parent"]
  self.grouping = Array(attributes["grouping"]).map { |element| Activity.new(element) } if attributes["grouping"]
  self.other = Array(attributes["other"]).map { |element| Activity.new(element) } if attributes["other"]
  self.category = Array(attributes["category"]).map { |element| Activity.new(element) } if attributes["category"]
end

Instance Attribute Details

#categoryArray<Activity>?

Returns A categorization tag indicating a profile or context (e.g., “SCORM profile”, “performance review”).

Returns:

  • (Array<Activity>, nil)

    A categorization tag indicating a profile or context (e.g., “SCORM profile”, “performance review”).



25
26
27
# File 'lib/active_lrs/xapi/context_activities.rb', line 25

def category
  @category
end

#groupingArray<Activity>?

Returns Activities used to group Statements for reporting purposes (e.g., “Unit 1” grouping multiple lessons).

Returns:

  • (Array<Activity>, nil)

    Activities used to group Statements for reporting purposes (e.g., “Unit 1” grouping multiple lessons).



21
22
23
# File 'lib/active_lrs/xapi/context_activities.rb', line 21

def grouping
  @grouping
end

#otherArray<Activity>?

Returns Any other contextually relevant Activities not covered by parent, grouping, or category.

Returns:

  • (Array<Activity>, nil)

    Any other contextually relevant Activities not covered by parent, grouping, or category.



29
30
31
# File 'lib/active_lrs/xapi/context_activities.rb', line 29

def other
  @other
end

#parentArray<Activity>?

Returns A parent Activity with a more general scope than the Statement’s object Activity (e.g., “Course” as parent of “Lesson”).

Returns:

  • (Array<Activity>, nil)

    A parent Activity with a more general scope than the Statement’s object Activity (e.g., “Course” as parent of “Lesson”).



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

def parent
  @parent
end

Instance Method Details

#to_hHash{String => Array<Hash>}

Converts the ContextActivity into a hash suitable for inclusion in an xAPI Statement.

Examples:

context_activities = ActiveLrs::Xapi::ContextActivity.new(
  "parent" => [{ "id" => "http://example.com/course/1" }],
  "grouping" => [{ "id" => "http://example.com/unit/1" }]
)
context_activities.to_h
# => {
#   "parent" => [{ "id" => "http://example.com/course/1", "objectType" => "Activity" }],
#   "grouping" => [{ "id" => "http://example.com/unit/1", "objectType" => "Activity" }]
# }

Returns:

  • (Hash{String => Array<Hash>})

    a hash including only the present context activities



61
62
63
64
65
66
67
68
# File 'lib/active_lrs/xapi/context_activities.rb', line 61

def to_h
  node = {}
  node["parent"] = parent.map { |element| element.to_h } if parent && parent.any?
  node["grouping"] = grouping.map { |element| element.to_h } if grouping && grouping.any?
  node["other"] = other.map { |element| element.to_h } if other && other.any?
  node["category"] = category.map { |element| element.to_h } if category && category.any?
  node
end