def create_Performance_statement(row): division, num_expediente, idp, cod_asignatura, desc_assignatura, af, nf, supera_s_n, seguiment_ac_s_n, tipus_examen, qe, qualificacio_teorica, nota_prova_validacio, cod_estudios, desc_estudios, codi_aula, any_academic = row statement = Statement({ 'actor': Agent({ 'account': AgentAccount({ 'name': idp, }), }), 'verb': Verb({ 'id': 'http://la.uoc.edu/verb/performance', 'display': LanguageMap({'en-US': 'Enrolment'}), }), 'object': Activity({ 'id': 'http://la.uoc.edu/object/expedient/code/%s' % num_expediente, 'definition': ActivityDefinition({ 'extensions': Extensions({ 'edu:uoc:la:expedient': { 'code': num_expediente, 'division': division, }, 'edu:uoc:la:subject': { 'code': cod_asignatura, 'description': desc_assignatura, 'evaluation': { 'af': af, 'nf': nf, 'ac': { 'follows': seguiment_ac_s_n == 'Si', 'pass': supera_s_n == 'Si', }, 'examType': tipus_examen, 'qe': qe, 'qt': qualificacio_teorica, 'validationTest': { 'qualification': nota_prova_validacio } } }, 'edu:uoc:la:semester': { 'code': any_academic, }, 'edu:uoc:la:study': { 'code': cod_estudios, 'description': desc_estudios, }, 'edu:uoc:la:classroom': { 'code': codi_aula, }, }) }) }), 'result': Result({ }), 'timestamp': datetime.datetime.utcnow(), 'context': Context({ 'registration': uuid.uuid4(), }) }) return json.loads(statement.to_json())
def test_ToJSON(self): statement = Statement( **{ 'id': '016699c6-d600-48a7-96ab-86187498f16f', 'actor': { 'name': 'test' }, 'verb': { 'id': 'test' }, 'object': { 'object_type': 'Agent', 'name': 'test' }, 'authority': { 'name': 'test' }, 'context': { 'registration': '016699c6-d600-48a7-96ab-86187498f16f' }, 'attachments': [{ 'usage_type': 'test' }] }) self.assertEqual( json.loads(statement.to_json()), json.loads( '{"verb": {"id": "test"}, ' '"attachments": [{"usageType": "test"}], ' '"object": {"name": "test", "objectType": "Agent"}, ' '"actor": {"name": "test", "objectType": "Agent"}, ' '"version": "1.0.1", ' '"authority": {"name": "test", "objectType": "Agent"}, ' '"context": {"registration": "016699c6-d600-48a7-96ab-86187498f16f"}, ' '"id": "016699c6-d600-48a7-96ab-86187498f16f"}'))
def create_ass_matr_statement(row): cod_asignatura, any_academic, userid, idp = row statement = Statement({ 'actor': Agent({ 'account': AgentAccount({ 'name': idp, }), }), 'verb': Verb({ 'id': 'http://la.uoc.edu/verb/subject/enrolment', 'display': LanguageMap({'en-US': 'Enrolment'}), }), 'object': Activity({ 'id': 'http://la.uoc.edu/object/subject/code/%s' % cod_asignatura, 'definition': ActivityDefinition({ 'extensions': Extensions({ 'edu:uoc:la:subject': { 'code': cod_asignatura }, 'edu:uoc:la:semester': { 'code': any_academic, } }) }) }), 'result': Result({ }), 'timestamp': datetime.datetime.utcnow(), 'context': Context({ 'registration': uuid.uuid4(), }) }) return json.loads(statement.to_json())
def process_event(self, event_edx): course_id = event_edx['context'].get('course_id', None) if course_id is None or course_id == '': try: # We need to do this because we receive a string instead than a dictionary event = json.loads(event_edx['event']) course_id = event['POST'].get('course_id', None)[0] except: pass # No event data, just skip if course_id != '' and course_id in self.course_ids: try: # Sometimes we receive time as python datetime, sometimes as string... timepart = event_edx['time'] timestamp = xutils.make_datetime_for_tincan(timepart) actor = None user_id = 0 try: user_id = event_edx['context'].get('user_id', 0) except: user_id = json.loads(event_edx['context']).get('user_id', 0) try: actor = self.get_actor(user_id) except (ValueError, UserSocialAuth.DoesNotExist) as e: # Only logged ECO user need to be tracked return verb, obj = self.to_xapi(event_edx, course_id) context = self.get_context(course_id, user_id) d = {"time_with_millis": timepart} context.extensions = Extensions(d) # verb = None means to not record the action if verb: statement = Statement( actor=actor, verb=verb, object=obj, context=context, timestamp=timestamp ) tldat = TrackingLog( dtcreated=timestamp, user_id=user_id, course_id=course_id, statement=statement.to_json(), original_event=json.dumps(event_edx, cls=DateTimeJSONEncoder) ) # We don't need to add duplication event test, so we save directly tldat.save() except Exception as e: # pylint: disable=broad-except log.exception(e) else: if course_id != '': # print 'Course not activated', course_id # Uncomment for debug pass
def create_Matricula_statement(row): division, num_expediente, any_academico, inv_estado_expediente, idp, anula_matricula, motiu_anulacio, cod_plan, desc_plan, cod_estudios, desc_estudios, tipo_educacion, oficial_propi, cod_area, desc_area = row statement = Statement({ 'actor': Agent({ 'account': AgentAccount({ 'name': idp, }), }), 'verb': Verb({ 'id': 'http://la.uoc.edu/verb/enrolment', 'display': LanguageMap({'en-US': 'Enrolment'}), }), 'object': Activity({ 'id': 'http://la.uoc.edu/object/expedient/code/%s' % num_expediente, 'definition': ActivityDefinition({ 'extensions': Extensions({ 'edu:uoc:la:expedient': { 'code': num_expediente, 'division': division, 'status': inv_estado_expediente, }, 'edu:uoc:la:semester': { 'code': any_academico, }, 'edu:uoc:la:plan': { 'code': cod_plan, 'description': desc_plan, }, 'edu:uoc:la:study': { 'code': cod_estudios, 'description': desc_estudios, 'type': tipo_educacion, 'official': oficial_propi, }, 'edu:uoc:la:area': { 'code': cod_area, 'description': desc_area, }, 'edu:uoc:la:enrolment': { 'cancelation': { 'date': anula_matricula, 'reason': motiu_anulacio, }, } }) }) }), 'result': Result({ 'success': not anula_matricula, }), 'timestamp': datetime.datetime.utcnow(), 'context': Context({ 'registration': uuid.uuid4(), }) }) return json.loads(statement.to_json())
def test_send_method_with_successfull_flow(self, mocked_logger, mocked_get_transformer): transformed_event = Statement() mocked_transformer = MagicMock() mocked_transformer.transform.return_value = transformed_event mocked_get_transformer.return_value = mocked_transformer self.processor(self.sample_event) self.assertIn(call(transformed_event.to_json()), mocked_logger.mock_calls)
def create_login_logout_statement(row): print(row) if len(row) == 5: userid, login, logout, ip, lastaction = row else: return json.loads("{}") try: logintime = datetime.datetime.strptime(login, "%d%m%Y%H%M%S").isoformat() logouttime = datetime.datetime.strptime(logout, "%d%m%Y%H%M%S").isoformat() lastactiontime = datetime.datetime.strptime(lastaction, "%d%m%Y%H%M%S").isoformat() except ValueError: logintime = '' logouttime = '' lastactiontime = '' statement = Statement({ 'actor': Agent({ 'account': AgentAccount({ 'name': userid, }), }), 'verb': Verb({ 'id': 'http://la.uoc.edu/verb/login', 'display': LanguageMap({'en-US': 'Login'}), }), 'object': Activity({ 'id': 'http://la.uoc.edu/object/login', 'definition': ActivityDefinition({ 'extensions': Extensions({ 'edu:uoc:la:campus': { 'userid': userid, }, 'edu:uoc:la:login': { 'login': logintime, 'logout': logouttime, 'lastactiontime': lastactiontime, }, }) }) }), 'result': Result({ }), 'timestamp': datetime.datetime.utcnow(), 'context': Context({ 'registration': uuid.uuid4(), }) }) return json.loads(statement.to_json())
def test_ToJSON(self): statement = Statement( **{'id': '016699c6-d600-48a7-96ab-86187498f16f', 'actor': {'name': 'test'}, 'verb': {'id': 'test'}, 'object': {'object_type': 'Agent', 'name': 'test'}, 'authority': {'name': 'test'}, 'context': {'registration': '016699c6-d600-48a7-96ab-86187498f16f'}, 'attachments': [{'usage_type': 'test'}]}) self.assertEqual(statement.to_json(), '{"verb": {"id": "test"}, ' '"attachments": [{"usageType": "test"}], ' '"object": {"name": "test", "objectType": "Agent"}, ' '"actor": {"name": "test", "objectType": "Agent"}, ' '"version": "1.0.1", ' '"authority": {"name": "test", "objectType": "Agent"}, ' '"context": {"registration": "016699c6-d600-48a7-96ab-86187498f16f"}, ' '"id": "016699c6-d600-48a7-96ab-86187498f16f"}')
def create_AEP_statement(row): cod_element, estat_solicitud, any_acad_valida, desc_estat, any_academico, idp, num_expedient, asigna_clase, estat, num_creditos, cod_plan = row statement = Statement({ 'actor': Agent({ 'account': AgentAccount({ 'name': idp, }), }), 'verb': Verb({ 'id': 'http://la.uoc.edu/verb/aeprequest', 'display': LanguageMap({'en-US': 'AEP Request'}), }), 'object': Activity({ 'id': 'http://la.uoc.edu/object/subject/code/%s' % cod_element, 'definition': ActivityDefinition({ 'extensions': Extensions({ 'edu:uoc:la:subject': { 'code': cod_element, 'credits': num_creditos, }, 'edu:uoc:la:plan': { 'code': cod_plan, }, 'edu:uoc:la:semester': { 'code': any_academico, 'validated': any_acad_valida, }, 'edu:uoc:la:expedient': { 'code': num_expedient, }, 'edu:uoc:la:aep': { 'status': estat, 'classification': asigna_clase } }) }) }), 'result': Result({ 'success': estat == 'A', }), 'timestamp': datetime.datetime.utcnow(), 'context': Context({ 'registration': uuid.uuid4(), }) }) return json.loads(statement.to_json())
def test_ToJSONEmpty(self): statement = Statement() self.assertEqual(json.loads(statement.to_json()), json.loads('{"version": "1.0.3"}'))
def test_ToJSONEmpty(self): statement = Statement() self.assertEqual(statement.to_json(), '{}')
def test_ToJSONEmpty(self): statement = Statement() self.assertEqual(statement.to_json(), '{"version": "1.0.1"}')
statement = Statement( actor=actor, verb=obj.getVerb(), result=obj.getResult(), object=obj.getObject(), context=context, timestamp=timestamp ) # Commit the statement response = lrs.save_statement(statement) if not response.success: # TODO : Format proper error here responseObject = json.loads(response.data) logging.error("Failed to insert statement for logID: '%s' for eventname = '%s'"%(row['id'], row['eventname'])) print statement.to_json() print str(responseObject['message'][0]) sys.exit(5) except moodleObjects.moodleCritical as e: # Critical Errors cause the program to die e.addLogID(row['id']) logging.critical(e) # print statement.to_json() sys.exit(3) except moodleObjects.moodleError as e: # Errors can cause the program to die e.addLogID(row['id']) logging.error(e) if config.STRICT: sys.exit(2)
statement = Statement(actor=actor, verb=obj.getVerb(), result=obj.getResult(), object=obj.getObject(), context=context, timestamp=timestamp) # Commit the statement response = lrs.save_statement(statement) if not response.success: # TODO : Format proper error here responseObject = json.loads(response.data) logging.error( "Failed to insert statement for logID: '%s' for eventname = '%s'" % (row['id'], row['eventname'])) print statement.to_json() print str(responseObject['message'][0]) sys.exit(5) except moodleObjects.moodleCritical as e: # Critical Errors cause the program to die e.addLogID(row['id']) logging.critical(e) # print statement.to_json() sys.exit(3) except moodleObjects.moodleError as e: # Errors can cause the program to die e.addLogID(row['id']) logging.error(e) if config.STRICT: sys.exit(2)
def test_ToJSONEmpty(self): statement = Statement() self.assertEqual(statement.to_json(), '{"version": "' + lrs_properties.version + '"}')