From e2e93b2d7802bbd2b93a83e382ed070d4635c6ea Mon Sep 17 00:00:00 2001 From: Cristhian Garcia Date: Thu, 24 Aug 2023 12:49:08 -0500 Subject: [PATCH] fix: use result to store completion status --- dumped.json | 51 +++++++++++++++++++ ...x.completion.block_completion.changed.json | 2 +- .../tests/transformers_test_mixin.py | 7 +++ .../event_transformers/completion_events.py | 27 +++++++--- ...x.completion.block_completion.changed.json | 17 +++++-- expected.json | 51 +++++++++++++++++++ 6 files changed, 144 insertions(+), 11 deletions(-) create mode 100644 dumped.json create mode 100644 expected.json diff --git a/dumped.json b/dumped.json new file mode 100644 index 00000000..c332d0a4 --- /dev/null +++ b/dumped.json @@ -0,0 +1,51 @@ +{ + "id": "32e08e30-f8ae-4ce2-94a8-c2bfe38a70cb", + "result": { + "completion": true, + "extensions": { + "https://w3id.org/xapi/cmi5/result/extensions/progress": 100.0 + } + }, + "version": "1.0.3", + "actor": { + "objectType": "Agent", + "account": { + "name": "32e08e30-f8ae-4ce2-94a8-c2bfe38a70cb", + "homePage": "http://localhost:18000" + } + }, + "verb": { + "id": "http://adlnet.gov/expapi/verbs/progressed", + "display": { + "en": "progressed" + } + }, + "object": { + "id": "http://localhost:18000/xblock/block-v1:edX+DemoX+Demo_Course+type@problem+block@7c54b16c8ed34f9f8772015178c7a175", + "objectType": "Activity", + "definition": { + "type": "http://id.tincanapi.com/activitytype/resource" + } + }, + "timestamp": "2023-08-22T20:16:25.500832+00:00", + "context": { + "contextActivities": { + "parent": [ + { + "id": "http://localhost:18000/course/course-v1:edX+DemoX+Demo_Course", + "objectType": "Activity", + "definition": { + "name": { + "en-US": "Demonstration Course" + }, + "type": "http://adlnet.gov/expapi/activities/course" + } + } + ] + }, + "extensions": { + "https://w3id.org/xapi/openedx/extension/transformer-version": "event-routing-backends@1.1.1", + "https://w3id.org/xapi/openedx/extensions/session-id": "056aca2a1c6b76742b283e73d3424453" + } + } +} \ No newline at end of file diff --git a/event_routing_backends/processors/tests/fixtures/current/edx.completion.block_completion.changed.json b/event_routing_backends/processors/tests/fixtures/current/edx.completion.block_completion.changed.json index 38524a7b..ea994689 100644 --- a/event_routing_backends/processors/tests/fixtures/current/edx.completion.block_completion.changed.json +++ b/event_routing_backends/processors/tests/fixtures/current/edx.completion.block_completion.changed.json @@ -5,7 +5,7 @@ "user_id": 4, "course_id": "course-v1:edX+DemoX+Demo_Course", "context_key": "course-v1:edX+DemoX+Demo_Course", - "block_key": "block-v1:edX+DemoX+Demo_Course+type@problem+block@7c54b16c8ed34f9f8772015178c7a175", + "block_id": "block-v1:edX+DemoX+Demo_Course+type@problem+block@7c54b16c8ed34f9f8772015178c7a175", "block_type": "problem", "completion": 1.0, "is_new": false diff --git a/event_routing_backends/processors/tests/transformers_test_mixin.py b/event_routing_backends/processors/tests/transformers_test_mixin.py index eeb0320a..0bd1a853 100644 --- a/event_routing_backends/processors/tests/transformers_test_mixin.py +++ b/event_routing_backends/processors/tests/transformers_test_mixin.py @@ -110,4 +110,11 @@ def test_event_transformer(self, event_filename, mocked_uuid): actual_transformed_event = self.registry.get_transformer(original_event).transform() + if expected_event_file_path.endswith('edx.completion.block_completion.changed.json'): + with open("dumped.json", "w") as f: + f.write(json.dumps(actual_transformed_event.as_version(), indent=4)) + + with open("expected.json", "w") as f: + f.write(json.dumps(expected_event, indent=4)) + self.compare_events(actual_transformed_event, expected_event) diff --git a/event_routing_backends/processors/xapi/event_transformers/completion_events.py b/event_routing_backends/processors/xapi/event_transformers/completion_events.py index ad08c931..6e2945fe 100644 --- a/event_routing_backends/processors/xapi/event_transformers/completion_events.py +++ b/event_routing_backends/processors/xapi/event_transformers/completion_events.py @@ -1,23 +1,26 @@ """ Transformers for forum related events. """ -from tincan import Activity, ActivityDefinition, Extensions, LanguageMap, Verb +from tincan import Activity, ActivityDefinition, Extensions, LanguageMap, Result, Verb from event_routing_backends.processors.xapi import constants from event_routing_backends.processors.xapi.registry import XApiTransformersRegistry from event_routing_backends.processors.xapi.transformer import XApiTransformer -@XApiTransformersRegistry.register('edx.completion.block_completion.changed') +@XApiTransformersRegistry.register("edx.completion.block_completion.changed") class CompletionCreatedTransformer(XApiTransformer): """ Transformers for event generated when an student completion is created or updated. """ + verb = Verb( id=constants.XAPI_VERB_PROGRESSED, display=LanguageMap({constants.EN: constants.PROGRESSED}), ) + additional_fields = ('result', ) + def get_object(self): """ Get object for xAPI transformed event related to a thread. @@ -25,12 +28,24 @@ def get_object(self): Returns: `Activity` """ + print(f'\n\n\n BLOCK ID: {self.get_object_iri("xblock", self.get_data("data.block_id"))}\n\n\n') return Activity( - id=self.get_object_iri('xblock', self.get_data('data.block_key')), + id=self.get_object_iri("xblock", self.get_data("data.block_id")), definition=ActivityDefinition( type=constants.XAPI_ACTIVITY_RESOURCE, - extensions=Extensions({ - constants.XAPI_ACTIVITY_PROGRESS: self.get_data('data.completion')*100 - }) + ), + ) + + def get_result(self): + """ + Get result for xAPI transformed event related to a thread. + + Returns: + `Result` + """ + return Result( + completion=self.get_data("data.completion") == 1.0, + extensions=Extensions( + {constants.XAPI_ACTIVITY_PROGRESS: self.get_data("data.completion")*100} ), ) diff --git a/event_routing_backends/processors/xapi/tests/fixtures/expected/edx.completion.block_completion.changed.json b/event_routing_backends/processors/xapi/tests/fixtures/expected/edx.completion.block_completion.changed.json index c0bb72d4..4ea719af 100644 --- a/event_routing_backends/processors/xapi/tests/fixtures/expected/edx.completion.block_completion.changed.json +++ b/event_routing_backends/processors/xapi/tests/fixtures/expected/edx.completion.block_completion.changed.json @@ -1,17 +1,26 @@ { "id": "32e08e30-f8ae-4ce2-94a8-c2bfe38a70cb", + "result": { + "completion": true, + "extensions": { "https://w3id.org/xapi/cmi5/result/extensions/progress": 100.0 } + }, "version": "1.0.3", "actor": { "objectType": "Agent", - "account": { "name": "32e08e30-f8ae-4ce2-94a8-c2bfe38a70cb", "homePage": "http://localhost:18000" } + "account": { + "name": "32e08e30-f8ae-4ce2-94a8-c2bfe38a70cb", + "homePage": "http://localhost:18000" + } }, - "verb": { "id": "http://adlnet.gov/expapi/verbs/progressed", "display": { "en": "progressed" } }, + "verb": { + "id": "http://adlnet.gov/expapi/verbs/progressed", + "display": { "en": "progressed" } +}, "object": { "id": "http://localhost:18000/xblock/block-v1:edX+DemoX+Demo_Course+type@problem+block@7c54b16c8ed34f9f8772015178c7a175", "objectType": "Activity", "definition": { - "type": "http://id.tincanapi.com/activitytype/resource", - "extensions": { "https://w3id.org/xapi/cmi5/result/extensions/progress": 100.0 } + "type": "http://id.tincanapi.com/activitytype/resource" } }, "timestamp": "2023-08-22T20:16:25.500832+00:00", diff --git a/expected.json b/expected.json new file mode 100644 index 00000000..c332d0a4 --- /dev/null +++ b/expected.json @@ -0,0 +1,51 @@ +{ + "id": "32e08e30-f8ae-4ce2-94a8-c2bfe38a70cb", + "result": { + "completion": true, + "extensions": { + "https://w3id.org/xapi/cmi5/result/extensions/progress": 100.0 + } + }, + "version": "1.0.3", + "actor": { + "objectType": "Agent", + "account": { + "name": "32e08e30-f8ae-4ce2-94a8-c2bfe38a70cb", + "homePage": "http://localhost:18000" + } + }, + "verb": { + "id": "http://adlnet.gov/expapi/verbs/progressed", + "display": { + "en": "progressed" + } + }, + "object": { + "id": "http://localhost:18000/xblock/block-v1:edX+DemoX+Demo_Course+type@problem+block@7c54b16c8ed34f9f8772015178c7a175", + "objectType": "Activity", + "definition": { + "type": "http://id.tincanapi.com/activitytype/resource" + } + }, + "timestamp": "2023-08-22T20:16:25.500832+00:00", + "context": { + "contextActivities": { + "parent": [ + { + "id": "http://localhost:18000/course/course-v1:edX+DemoX+Demo_Course", + "objectType": "Activity", + "definition": { + "name": { + "en-US": "Demonstration Course" + }, + "type": "http://adlnet.gov/expapi/activities/course" + } + } + ] + }, + "extensions": { + "https://w3id.org/xapi/openedx/extension/transformer-version": "event-routing-backends@1.1.1", + "https://w3id.org/xapi/openedx/extensions/session-id": "056aca2a1c6b76742b283e73d3424453" + } + } +} \ No newline at end of file