def insertsocialrelationships(request): html_response = HttpResponse() # Rebuild Relationships SocialRelationship.objects.filter().delete() sql = """ SELECT clatoolkit_learningrecord.username, clatoolkit_learningrecord.verb, obj, clatoolkit_learningrecord.platform, clatoolkit_learningrecord.datetimestamp, clatoolkit_learningrecord.message, clatoolkit_learningrecord.course_code, clatoolkit_learningrecord.verb, clatoolkit_learningrecord.platformid FROM clatoolkit_learningrecord, json_array_elements(clatoolkit_learningrecord.xapi->'context'->'contextActivities'->'other') obj """ cursor = connection.cursor() cursor.execute(sql) result = cursor.fetchall() for row in result: #print row dict = row[2] tag = dict["definition"]["name"]["en-US"] if tag.startswith('@'): # hastags are also returned in query and need to be filtered out socialrelationship = SocialRelationship() socialrelationship.fromusername = row[0] #get_username_fromsmid(row[0], row[3]) socialrelationship.tousername = get_username_fromsmid(tag[1:], row[3]) #remove @symbol socialrelationship.message = row[5] socialrelationship.datetimestamp = row[4] socialrelationship.platform = row[3] socialrelationship.verb = "mentioned" socialrelationship.platformid = row[8] socialrelationship.course_code = row[6] socialrelationship.save() print row[3] #get all statements with platformparentid sql = """ SELECT clatoolkit_learningrecord.username, clatoolkit_learningrecord.parentusername, clatoolkit_learningrecord.verb, clatoolkit_learningrecord.platform, clatoolkit_learningrecord.platformid, clatoolkit_learningrecord.message, clatoolkit_learningrecord.datetimestamp, clatoolkit_learningrecord.course_code FROM clatoolkit_learningrecord WHERE COALESCE(clatoolkit_learningrecord.platformparentid, '') != '' """ cursor = connection.cursor() cursor.execute(sql) result = cursor.fetchall() for row in result: socialrelationship = SocialRelationship() socialrelationship.fromusername = row[0] #get_username_fromsmid(row[0], row[3]) socialrelationship.tousername = row[1] #get_username_fromsmid(row[1], row[3]) socialrelationship.message = row[5] socialrelationship.datetimestamp = row[6] socialrelationship.platform = row[3] socialrelationship.verb = row[2] socialrelationship.platformid = row[4] socialrelationship.course_code = row[7] socialrelationship.save() html_response.write('Social Relationships Updated.') return html_response
def insert_comment(user, post_id, comment_id, comment_message, comment_created_time, unit, platform, platform_url, parent_user=None, parent_user_external=None, other_contexts=[]): if check_ifnotinlocallrs(unit, platform, comment_id): lrs_client = LRS_Auth(provider_id=unit.get_lrs_id()) account_name = user.userprofile.get_username_for_platform(platform) statement_id = get_uuid4() # lrs = LearningRecord(statement_id=statement_id, unit=unit, verb=xapi_settings.VERB_COMMENTED, platform=platform, # user=user, platformid=comment_id, platformparentid=post_id, parent_user=parent_user, # parent_user_external=parent_user_external, message=comment_message, # datetimestamp=comment_created_time) lrs = LearningRecord(statement_id=statement_id, unit=unit, verb=xapi_settings.VERB_COMMENTED, platform=platform, user=user, platformid=comment_id, platformparentid=post_id, parent_user=parent_user, datetimestamp=comment_created_time) lrs.save() sr = SocialRelationship(verb=xapi_settings.VERB_COMMENTED, from_user=user, to_user=parent_user, to_external_user=parent_user_external, platform=platform, message=comment_message, datetimestamp=comment_created_time, unit=unit, platformid=comment_id) sr.save() stm = socialmedia_builder(statement_id=statement_id, verb=xapi_settings.VERB_COMMENTED, platform=platform, account_name=account_name, account_homepage=platform_url, object_type=xapi_settings.OBJECT_NOTE, object_id=comment_id, message=comment_message, parent_id=post_id, parent_object_type=xapi_settings.OBJECT_NOTE, timestamp=comment_created_time, unit=unit, other_contexts=other_contexts) jsn = stm.to_json() status, content = lrs_client.transfer_statement(user.id, statement=jsn)
def insert_like(user, object_id, message, unit, platform, platform_url, parent_id, parent_object_type, created_time=None, parent_user=None, parent_user_external=None): verb = xapi_settings.VERB_LIKED if check_ifnotinlocallrs(unit, platform, object_id, user, verb): lrs_client = LRS_Auth(provider_id=unit.get_lrs_id()) account_name = user.userprofile.get_username_for_platform(platform) statement_id = get_uuid4() # lrs = LearningRecord(statement_id=statement_id, unit=unit, verb=verb, platform=platform, user=user, # platformid=object_id, message=message, platformparentid=object_id, parent_user=parent_user, # parent_user_external=parent_user_external, datetimestamp=created_time) lrs = LearningRecord(statement_id=statement_id, unit=unit, verb=verb, platform=platform, user=user, platformid=object_id, platformparentid=object_id, parent_user=parent_user, datetimestamp=created_time) lrs.save() sr = SocialRelationship(verb=verb, from_user=user, to_user=parent_user, to_external_user=parent_user_external, platform=platform, message=message, datetimestamp=created_time, unit=unit, platformid=object_id) sr.save() # Send xAPI to LRS stm = socialmedia_builder(statement_id=statement_id, verb=verb, platform=platform, account_name=account_name, account_homepage=platform_url, object_type=xapi_settings.OBJECT_NOTE, object_id=object_id, message=message, parent_id=parent_id, parent_object_type=parent_object_type, timestamp=created_time, unit=unit) jsn = stm.to_json() status, content = lrs_client.transfer_statement(user.id, statement=jsn)
def insert_post(user, post_id, message, created_time, unit, platform, platform_url, tags=()): # verb = 'created' verb = xapi_settings.VERB_CREATED #TODO: update for lrs connection as it happens if check_ifnotinlocallrs(unit, platform, post_id, user, verb): #Setup statment builder with param and build lrs using defualt rs lrs_client = LRS_Auth(provider_id=unit.get_lrs_id()) account_name = user.userprofile.get_username_for_platform(platform) statement_id = get_uuid4() # #lrs.xapi = the "transaction" uuid # lrs = LearningRecord(statement_id=statement_id, unit=unit, verb=verb, platform=platform, user=user, # platformid=post_id, message=message, datetimestamp=created_time) lrs = LearningRecord(statement_id=statement_id, unit=unit, verb=verb, platform=platform, user=user, platformid=post_id, datetimestamp=created_time) lrs.save() #Transfer xapi to lrs of cache for later stm = socialmedia_builder(statement_id=statement_id, verb=verb, platform=platform, account_name=account_name, account_homepage=platform_url, object_type=xapi_settings.OBJECT_NOTE, object_id=post_id, message=message, timestamp=created_time, unit=unit, tags=tags) jsn = stm.to_json() # print 'sending xapi... ' # print jsn status, content = lrs_client.transfer_statement(user.id, statement=jsn) # print 'in insert_post(): Response status/code from LRS: %s/%s' % (status, content) for tag in tags: if tag[0] == "@": # If the user exists, use their user object else reference them as an external user if username_exists(tag[1:], unit, platform): to_user = get_user_from_screen_name(tag[1:], platform) external_user = None else: to_user = None external_user = tag[1:] sr = SocialRelationship(verb=xapi_settings.VERB_MENTIONED, from_user=user, to_user=to_user, to_external_user=external_user, platform=platform, message=message, datetimestamp=created_time, unit=unit, platformid=post_id) sr.save()
def insert_share(user, post_id, share_id, comment_message, comment_created_time, unit, platform, platform_url, tags=(), parent_user=None, parent_external_user=None): if check_ifnotinlocallrs(unit, platform, share_id): # Setup statement builder parameters and lrs using default lrs. TODO: Institutions required in xapi maybe? lrs_client = LRS_Auth(provider_id=unit.get_lrs_id()) account_name = user.userprofile.get_username_for_platform(platform) _parent_user = parent_user if parent_user else parent_external_user statement_id = get_uuid4() # #lrs.xapi = the "transaction" uuid # lrs = LearningRecord(statement_id=statement_id, unit=unit, verb=xapi_settings.VERB_SHARED, platform=platform, user=user, # platformid=share_id, platformparentid=post_id, parent_user=parent_user, # parent_user_external=parent_external_user, message=comment_message, # datetimestamp=comment_created_time) lrs = LearningRecord(statement_id=statement_id, unit=unit, verb=xapi_settings.VERB_SHARED, platform=platform, user=user, platformid=share_id, platformparentid=post_id, parent_user=parent_user, datetimestamp=comment_created_time) lrs.save() #Send xapi to lrs or cache for later stm = socialmedia_builder(statement_id=statement_id, verb=xapi_settings.VERB_SHARED, platform=platform, account_name=account_name, account_homepage=platform_url, object_type=xapi_settings.OBJECT_NOTE, object_id=share_id, message=comment_message, parent_id=post_id, parent_object_type=xapi_settings.OBJECT_NOTE, timestamp=comment_created_time, unit=unit, tags=tags) jsn = stm.to_json() #Transfer xapi to lrs TODO: Handle caching for failed sends # print 'Sending xapi..' status, content = lrs_client.transfer_statement(user.id, statement=jsn) # print 'Tried to send xapi to lrs: status %s, response: %s' % (status,content) sr = SocialRelationship(verb=xapi_settings.VERB_SHARED, from_user=user, to_user=parent_user, to_external_user=parent_external_user, platform=platform, message=comment_message, datetimestamp=comment_created_time, unit=unit, platformid=share_id) sr.save()