Class: ActiveLrs::Xapi::Result

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

Overview

Represents an xAPI Result object.

The Result object captures the outcome of an Activity. It may include scoring, completion status, success, learner response, duration, and additional extensions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ void

Initializes a new Result instance.

Parameters:

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

    Hash of attributes for the Result object.

Options Hash (attributes):

  • "score" (Hash)

    A hash representing a Score object.

  • "success" (Boolean)

    Indicates whether the Activity was successful.

  • "completion" (Boolean)

    Indicates whether the Activity was completed.

  • "response" (String)

    Learner’s response.

  • "duration" (String)

    Duration in ISO 8601 format.

  • "extensions" (Hash)

    Optional key/value extensions.



45
46
47
48
49
50
51
52
# File 'lib/active_lrs/xapi/result.rb', line 45

def initialize(attributes = {})
  self.score = Xapi::Score.new(attributes["score"]) if attributes["score"]
  self.success = attributes["success"] unless attributes["success"].nil?
  self.completion = attributes["completion"] unless attributes["completion"].nil?
  self.duration = attributes["duration"] if attributes["duration"]
  self.response = attributes["response"] if attributes["response"]
  self.extensions = attributes["extensions"] if attributes["extensions"]
end

Instance Attribute Details

#completionBoolean?

Returns True if the Activity was completed (optional).

Returns:

  • (Boolean, nil)

    True if the Activity was completed (optional).



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

def completion
  @completion
end

#durationString?

Returns ISO 8601 duration string representing the time spent (optional).

Returns:

  • (String, nil)

    ISO 8601 duration string representing the time spent (optional).



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

def duration
  @duration
end

#extensionsHash{String => Object}?

Returns Additional metadata not covered by standard properties.

Returns:

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

    Additional metadata not covered by standard properties.



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

def extensions
  @extensions
end

#responseString?

Returns Learner’s response, e.g., entered text or choice (optional).

Returns:

  • (String, nil)

    Learner’s response, e.g., entered text or choice (optional).



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

def response
  @response
end

#scoreScore?

Returns The score achieved for the Activity (optional).

Returns:

  • (Score, nil)

    The score achieved for the Activity (optional).



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

def score
  @score
end

#successBoolean?

Returns True if the Activity was successfully completed (optional).

Returns:

  • (Boolean, nil)

    True if the Activity was successfully completed (optional).



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

def success
  @success
end

Instance Method Details

#to_hHash{String => Object}

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

Examples:

result = ActiveLrs::Xapi::Result.new(
  "score" => { "scaled" => 0.8, "raw" => 80 },
  "success" => true,
  "completion" => true,
  "response" => "42",
  "duration" => "PT1H30M",
  "extensions" => { "http://example.com/extension" => "value" }
)
result.to_h
# => {
#   "score" => { "scaled" => 0.8, "raw" => 80 },
#   "success" => true,
#   "completion" => true,
#   "response" => "42",
#   "duration" => "PT1H30M",
#   "extensions" => { "http://example.com/extension" => "value" }
# }

Returns:

  • (Hash{String => Object})

    A hash representation of the Result object.



76
77
78
79
80
81
82
83
84
85
# File 'lib/active_lrs/xapi/result.rb', line 76

def to_h
  node = {}
  node["score"] = score.to_h if score
  node["success"] = success unless success.nil?
  node["completion"] = completion unless completion.nil?
  node["response"] = response if response
  node["duration"] = format_duration(duration) if duration
  node["extensions"] = extensions if extensions
  node
end