Class: ActiveLrs::Xapi::Context

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

Overview

Represents an xAPI Context object.

A Context object provides additional information about the circumstances in which a Statement occurred. This may include registration identifiers, instructor, team, contextual activities, platform, language, revision, and extensions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ void

Initializes a new Context instance with optional attributes.

Parameters:

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

    a hash of context attributes

Options Hash (attributes):

  • "registration" (String)

    UUID string identifying a registration.

  • "instructor" (Hash)

    A hash representing an Agent or Group.

  • "team" (Hash)

    A hash representing a Group of Agents.

  • "contextActivities" (Hash)

    A hash of ContextActivity arrays.

  • "revision" (String)

    Revision/version of the Activity.

  • "platform" (String)

    System/platform identifier.

  • "language" (String)

    Language code per RFC 5646.

  • "statement" (Hash)

    A StatementRef hash (keys: “objectType” => “StatementRef”, “id” => “<UUID>”).

  • "extensions" (Hash)

    Arbitrary key/value pairs for extended context.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_lrs/xapi/context.rb', line 58

def initialize(attributes = {})
  self.registration = attributes["registration"] if attributes["registration"]
  self.instructor = attributes["instructor"]["member"] ? Xapi::Group.new(attributes["instructor"]) : Xapi::Agent.new(attributes["instructor"]) if attributes["instructor"]
  self.team = Xapi::Group.new(attributes["team"]) if attributes["team"]
  self.context_activities = Xapi::ContextActivities.new(attributes["contextActivities"]) if attributes["contextActivities"]
  self.revision = attributes["revision"] if attributes["revision"]
  self.platform = attributes["platform"] if attributes["platform"]
  self.language = attributes["language"] if attributes["language"]
  self.statement = Xapi::StatementRef.new(attributes["statement"]) if attributes["statement"]
  self.extensions = attributes["extensions"] if attributes["extensions"]
end

Instance Attribute Details

#context_activitiesContextActivity?

Returns A set of related Activities providing context (e.g., parent course, grouping, category, or other).

Returns:

  • (ContextActivity, nil)

    A set of related Activities providing context (e.g., parent course, grouping, category, or other).



26
27
28
# File 'lib/active_lrs/xapi/context.rb', line 26

def context_activities
  @context_activities
end

#extensionsHash{String => Object}?

Returns A map of custom key/value pairs providing additional context data.

Returns:

  • (Hash{String => Object}, nil)

    A map of custom key/value pairs providing additional context data.



42
43
44
# File 'lib/active_lrs/xapi/context.rb', line 42

def extensions
  @extensions
end

#instructorAgent, ...

Returns An Agent or Group representing the instructor.

Returns:

  • (Agent, Group, nil)

    An Agent or Group representing the instructor.



19
20
21
# File 'lib/active_lrs/xapi/context.rb', line 19

def instructor
  @instructor
end

#languageString?

Returns The language code (RFC 5646) in which the experience occurred.

Returns:

  • (String, nil)

    The language code (RFC 5646) in which the experience occurred.



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

def language
  @language
end

#platformString?

Returns A description of the system or platform used in the experience.

Returns:

  • (String, nil)

    A description of the system or platform used in the experience.



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

def platform
  @platform
end

#registrationString?

Returns A UUID identifying a registration (a series of Statements).

Returns:

  • (String, nil)

    A UUID identifying a registration (a series of Statements).



16
17
18
# File 'lib/active_lrs/xapi/context.rb', line 16

def registration
  @registration
end

#revisionString?

Returns Revision/version information associated with the Activity.

Returns:

  • (String, nil)

    Revision/version information associated with the Activity.



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

def revision
  @revision
end

#statementStatementRef?

Returns A reference to another Statement. Serialized as: { “objectType” => “StatementRef”, “id” => “<UUID>” }.

Returns:

  • (StatementRef, nil)

    A reference to another Statement. Serialized as: { “objectType” => “StatementRef”, “id” => “<UUID>” }.



39
40
41
# File 'lib/active_lrs/xapi/context.rb', line 39

def statement
  @statement
end

#teamGroup?

Returns A Group of Agents that participated in the Statement.

Returns:

  • (Group, nil)

    A Group of Agents that participated in the Statement.



22
23
24
# File 'lib/active_lrs/xapi/context.rb', line 22

def team
  @team
end

Instance Method Details

#to_hHash{String => Object}

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

Examples:

context = ActiveLrs::Xapi::Context.new(
  "registration" => "550e8400-e29b-41d4-a716-446655440000",
  "instructor" => { "mbox" => "mailto:[email protected]" },
  "statement" => { "objectType" => "StatementRef", "id" => "8f87ccde-bb56-4c2e-ab83-44982ef22df0" }
)
context.to_h
# => {
#   "registration" => "550e8400-e29b-41d4-a716-446655440000",
#   "instructor" => { "objectType" => "Agent", "mbox" => "mailto:[email protected]" },
#   "statement" => { "objectType" => "StatementRef", "id" => "8f87ccde-..." }
# }

Returns:

  • (Hash{String => Object})

    a hash including only the present context attributes



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_lrs/xapi/context.rb', line 86

def to_h
  node = {}
  node["registration"] = registration if registration
  node["instructor"] = instructor.to_h if instructor
  node["team"] = team.to_h if team
  node["contextActivities"] = context_activities.to_h if context_activities
  node["revision"] = revision if revision
  node["platform"] = platform if platform
  node["language"] = language if language
  node["statement"] = statement.to_h if statement
  node["extensions"] = extensions if extensions
  node
end