def insert_file(usr_dict, file_id, message, from_uid, from_name, committed_time, course_code, parent_id, platform, platform_id, platform_parentid, verb, commit_username=None, tags=[]):
    if check_ifnotinlocallrs(course_code, platform, file_id):
        object = "File"
        parentObj = "Collection"

        stm = socialmedia_builder(
            verb=verb, platform=platform, account_name=from_uid, 
            account_homepage=platform_id, object_type=object, object_id=file_id, 
            message=message, tags=tags, parent_object_type=parentObj, parent_id=parent_id, 
            timestamp=committed_time, account_email=usr_dict['email'], 
            user_name=from_name, course_code=course_code)

        jsn = ast.literal_eval(stm.to_json())
        stm_json = pretty_print_json(jsn)
        lrs = LearningRecord(
            xapi=stm_json, course_code=course_code, verb=verb, 
            platform=platform, username=get_username_fromsmid(from_uid, platform), 
            platformid=platform_id, platformparentid=platform_parentid, 
            parentusername=get_username_fromsmid(commit_username, platform), 
            message=message, datetimestamp=committed_time)
        lrs.save()
        socialrelationship = SocialRelationship(
            verb = verb, 
            fromusername=get_username_fromsmid(from_uid, platform), 
            tousername=get_username_fromsmid(commit_username, platform), 
            platform=platform, message=message, datetimestamp=committed_time, 
            course_code=course_code, platformid=file_id)
        socialrelationship.save()
Exemple #2
0
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)
Exemple #3
0
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_comment(
    usr_dict,
    post_id,
    comment_id,
    comment_message,
    comment_from_uid,
    comment_from_name,
    comment_created_time,
    course_code,
    platform,
    platform_url,
    shared_username=None,
    shared_displayname=None,
):
    if check_ifnotinlocallrs(course_code, platform, comment_id):
        if shared_displayname is not None:
            stm = socialmedia_builder(
                verb="commented",
                platform=platform,
                account_name=comment_from_uid,
                account_homepage=platform_url,
                object_type="Note",
                object_id=comment_id,
                message=comment_message,
                parent_id=post_id,
                parent_object_type="Note",
                timestamp=comment_created_time,
                account_email=usr_dict["email"],
                user_name=comment_from_name,
                course_code=course_code,
            )
            jsn = ast.literal_eval(stm.to_json())
            stm_json = pretty_print_json(jsn)
            lrs = LearningRecord(
                xapi=stm_json,
                course_code=course_code,
                verb="commented",
                platform=platform,
                username=get_username_fromsmid(comment_from_uid, platform),
                platformid=comment_id,
                platformparentid=post_id,
                parentusername=get_username_fromsmid(shared_username, platform),
                parentdisplayname=shared_displayname,
                message=comment_message,
                datetimestamp=comment_created_time,
            )
            lrs.save()
            socialrelationship = SocialRelationship(
                verb="commented",
                fromusername=get_username_fromsmid(comment_from_uid, platform),
                tousername=get_username_fromsmid(shared_username, platform),
                platform=platform,
                message=comment_message,
                datetimestamp=comment_created_time,
                course_code=course_code,
                platformid=comment_id,
            )
            socialrelationship.save()
def insert_share(usr_dict, post_id, share_id, comment_message, comment_from_uid, comment_from_name, comment_created_time, course_code, platform, platform_url, tags=[], shared_username=None):
    if check_ifnotinlocallrs(course_code, platform, share_id):
        stm = socialmedia_builder(verb='shared', platform=platform, account_name=comment_from_uid, account_homepage=platform_url, object_type='Note', object_id=share_id, message=comment_message, parent_id=post_id, parent_object_type='Note', timestamp=comment_created_time, account_email=usr_dict['email'], user_name=comment_from_name, course_code=course_code, tags=tags )
        jsn = ast.literal_eval(stm.to_json())
        stm_json = pretty_print_json(jsn)
        lrs = LearningRecord(xapi=stm_json, course_code=course_code, verb='shared', platform=platform, username=get_username_fromsmid(comment_from_uid, platform), platformid=share_id, platformparentid=post_id, parentusername=get_username_fromsmid(shared_username,platform), message=comment_message, datetimestamp=comment_created_time)
        lrs.save()
        socialrelationship = SocialRelationship(verb = "shared", fromusername=get_username_fromsmid(comment_from_uid,platform), tousername=get_username_fromsmid(shared_username,platform), platform=platform, message=comment_message, datetimestamp=comment_created_time, course_code=course_code, platformid=share_id)
        socialrelationship.save()
def insert_blogpost(usr_dict, post_id,message,from_name,from_uid, created_time, course_code, platform, platform_url, tags=[]):
    if check_ifnotinlocallrs(course_code, platform, post_id):
        stm = socialmedia_builder(verb='created', platform=platform, account_name=from_uid, account_homepage=platform_url, object_type='Article', object_id=post_id, message=message, timestamp=created_time, account_email=usr_dict['email'], user_name=from_name, course_code=course_code, tags=tags)
        jsn = ast.literal_eval(stm.to_json())
        stm_json = pretty_print_json(jsn)
        lrs = LearningRecord(xapi=stm_json, course_code=course_code, verb='created', platform=platform, username=get_username_fromsmid(from_uid, platform), platformid=post_id, message=message, datetimestamp=created_time)
        lrs.save()
        for tag in tags:
            if tag[0]=="@":
                socialrelationship = SocialRelationship(verb = "mentioned", fromusername=get_username_fromsmid(from_uid,platform), tousername=get_username_fromsmid(tag[1:],platform), platform=platform, message=message, datetimestamp=created_time, course_code=course_code, platformid=post_id)
                socialrelationship.save()
Exemple #7
0
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
Exemple #8
0
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()
Exemple #9
0
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()
Exemple #10
0
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