def parse_and_save_activity(self,
                                course,
                                section,
                                activity_node,
                                new_course,
                                is_baseline=False):
        """
        Parses an Activity XML and saves it to the DB
        :param section: section the activity belongs to
        :param act: a XML DOM element containing a single activity
        :param new_course: boolean indicating if it is a new course or existed
                previously
        :param is_baseline: is the activity part of the baseline?
        :return: None
        """

        title = {}
        for t in activity_node.findall('title'):
            title[t.get('lang')] = t.text
        title = json.dumps(title) if title else None

        description = {}
        for t in activity_node.findall('description'):
            description[t.get('lang')] = t.text
        description = json.dumps(description) if description else None

        content, activity_type = get_activity_content(activity_node)

        image = None
        for i in activity_node.findall("image"):
            image = i.get('filename')

        digest = activity_node.get("digest")
        try:
            activity = Activity.objects.get(
                digest=digest, section__course__shortname=course.shortname)
        except Activity.DoesNotExist:
            activity = Activity()

        activity.section = section
        activity.title = title
        activity.type = activity_type
        activity.order = activity_node.get("order")
        activity.digest = digest
        activity.baseline = is_baseline
        activity.image = image
        activity.content = content
        activity.description = description

        if (activity_type == "quiz") or (activity_type == "feedback"):
            updated_json = parse_and_save_quiz(user=None,
                                               activity=activity,
                                               act_xml=activity_node)
            # we need to update the JSON contents both in the XML and in the
            # activity data
            activity_node.find("content").text = \
                "<![CDATA[ " + updated_json + "]]>"
            activity.content = updated_json

        activity.save()
def activity_create(section, act, baseline=False):
    temp_title = {}
    for t in act.getElementsByTagName("title"):
        temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
    title = json.dumps(temp_title)
    
    if act.getAttribute("type") == "page":
        temp_content = {}
        for t in act.getElementsByTagName("location"):
            temp_content[t.getAttribute('lang')] = t.firstChild.nodeValue
        content = json.dumps(temp_content)
    elif act.getAttribute("type") == "quiz":
        for c in act.getElementsByTagName("content"):
            content = c.firstChild.nodeValue
    elif act.getAttribute("type") == "feedback":
        for c in act.getElementsByTagName("content"):
            content = c.firstChild.nodeValue
    elif act.getAttribute("type") == "resource":
        for c in act.getElementsByTagName("location"):
            content = c.firstChild.nodeValue
    elif act.getAttribute("type") == "url":
        temp_content = {}
        for t in act.getElementsByTagName("location"):
            temp_content[t.getAttribute('lang')] = t.firstChild.nodeValue
        content = json.dumps(temp_content)
    else:
        content = None
    
    image = None
    if act.getElementsByTagName("image"):
        for i in act.getElementsByTagName("image"):
            image = i.getAttribute('filename') 
        
    
    if act.getElementsByTagName("description"):
        description = {}
        for d in act.getElementsByTagName("description"):
            description[t.getAttribute('lang')] = d.firstChild.nodeValue
        description = json.dumps(description)
    else:
        description = None
          
    activity = Activity()
    activity.section = section
    activity.order = act.getAttribute("order")
    activity.title = title
    activity.type = act.getAttribute("type")
    activity.digest = act.getAttribute("digest")
    activity.baseline = baseline
    activity.image = image
    activity.content = content
    activity.description = description
    activity.save()           
Exemple #3
0
def activity_create(section, act, baseline=False):
    temp_title = {}
    for t in act.getElementsByTagName("title"):
        temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
    title = json.dumps(temp_title)

    if act.getAttribute("type") == "page":
        temp_content = {}
        for t in act.getElementsByTagName("location"):
            temp_content[t.getAttribute('lang')] = t.firstChild.nodeValue
        content = json.dumps(temp_content)
    elif act.getAttribute("type") == "quiz":
        for c in act.getElementsByTagName("content"):
            content = c.firstChild.nodeValue
    elif act.getAttribute("type") == "feedback":
        for c in act.getElementsByTagName("content"):
            content = c.firstChild.nodeValue
    elif act.getAttribute("type") == "resource":
        for c in act.getElementsByTagName("location"):
            content = c.firstChild.nodeValue
    elif act.getAttribute("type") == "url":
        temp_content = {}
        for t in act.getElementsByTagName("location"):
            temp_content[t.getAttribute('lang')] = t.firstChild.nodeValue
        content = json.dumps(temp_content)
    else:
        content = None

    image = None
    if act.getElementsByTagName("image"):
        for i in act.getElementsByTagName("image"):
            image = i.getAttribute('filename')

    if act.getElementsByTagName("description"):
        description = {}
        for d in act.getElementsByTagName("description"):
            description[t.getAttribute('lang')] = d.firstChild.nodeValue
        description = json.dumps(description)
    else:
        description = None

    activity = Activity()
    activity.section = section
    activity.order = act.getAttribute("order")
    activity.title = title
    activity.type = act.getAttribute("type")
    activity.digest = act.getAttribute("digest")
    activity.baseline = baseline
    activity.image = image
    activity.content = content
    activity.description = description
    activity.save()
def parse_and_save_activity(req, user, section, act, new_course, process_quiz_locally, is_baseline=False):
    """
    Parses an Activity XML and saves it to the DB
    :param section: section the activity belongs to
    :param act: a XML DOM element containing a single activity
    :param new_course: boolean indicating if it is a new course or existed previously
    :param process_quiz_locally: should the quiz be created based on the JSON contents?
    :param is_baseline: is the activity part of the baseline?
    :return: None
    """
    temp_title = {}
    for t in act.getElementsByTagName("title"):
        temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
    title = json.dumps(temp_title)

    content = ""
    act_type = act.getAttribute("type")
    if act_type == "page":
        temp_content = {}
        for t in act.getElementsByTagName("location"):
            if t.firstChild and t.getAttribute('lang'):
                temp_content[t.getAttribute('lang')] = t.firstChild.nodeValue
        content = json.dumps(temp_content)
    elif act_type == "quiz" or act_type == "feedback":
        for c in act.getElementsByTagName("content"):
            content = c.firstChild.nodeValue
    elif act_type == "resource":
        for c in act.getElementsByTagName("location"):
            content = c.firstChild.nodeValue
    elif act_type == "url":
        temp_content = {}
        for t in act.getElementsByTagName("location"):
            if t.firstChild and t.getAttribute('lang'):
                temp_content[t.getAttribute('lang')] = t.firstChild.nodeValue
        content = json.dumps(temp_content)
    else:
        content = None

    image = None
    if act.getElementsByTagName("image"):
        for i in act.getElementsByTagName("image"):
            image = i.getAttribute('filename')

    if act.getElementsByTagName("description"):
        description = {}
        for d in act.getElementsByTagName("description"):
            if d.firstChild and d.getAttribute('lang'):
                description[d.getAttribute('lang')] = d.firstChild.nodeValue
        description = json.dumps(description)
    else:
        description = None

    digest = act.getAttribute("digest")
    existed = False
    try:
        activity = Activity.objects.get(digest=digest)
        existed = True
    except Activity.DoesNotExist:
        activity = Activity()

    activity.section = section
    activity.title = title
    activity.type = act_type
    activity.order = act.getAttribute("order")
    activity.digest = act.getAttribute("digest")
    activity.baseline = is_baseline
    activity.image = image
    activity.content = content
    activity.description = description

    if not existed and not new_course:
        messages.warning(req, _('Activity "%(act)s"(%(digest)s) did not exist previously.') % {'act': activity, 'digest': activity.digest})
    '''
    If we also want to show the activities that previously existed, uncomment this block
    else:
        messages.info(req, _('Activity "%(act)s"(%(digest)s) previously existed. Updated with new information') % {'act': activity, 'digest':activity.digest})
    '''

    if (act_type == "quiz") and process_quiz_locally:
        updated_json = parse_and_save_quiz(req, user, activity, act)
        # we need to update the JSON contents both in the XML and in the activity data
        act.getElementsByTagName("content")[0].firstChild.nodeValue = updated_json
        activity.content = updated_json

    activity.save()

    # save gamification events
    if act.getElementsByTagName('gamification')[:1]:
        events = parse_gamification_events(act.getElementsByTagName('gamification')[0])
        # remove anything existing for this course
        ActivityGamificationEvent.objects.filter(activity=activity).delete()
        # add new
        for event in events:
            e = ActivityGamificationEvent(user=user, activity=activity, event=event['name'], points=event['points'])
            e.save()
Exemple #5
0
def parse_and_save_activity(request,
                            user,
                            course,
                            section,
                            activity_node,
                            is_new_course,
                            is_baseline=False):
    """
    Parses an Activity XML and saves it to the DB
    :param section: section the activity belongs to
    :param act: a XML DOM element containing a single activity
    :param is_new_course: boolean indicating if it is a new course or existed
            previously
    :param is_baseline: is the activity part of the baseline?
    :return: None
    """

    title = {}
    for t in activity_node.findall('title'):
        title[t.get('lang')] = t.text
    title = json.dumps(title) if title else None

    description = {}
    for t in activity_node.findall('description'):
        description[t.get('lang')] = t.text
    description = json.dumps(description) if description else None

    content, activity_type = get_activity_content(activity_node)

    image = None
    for i in activity_node.findall("image"):
        image = i.get('filename')

    digest = activity_node.get("digest")
    existed = False
    try:
        activity = Activity.objects.get(
            digest=digest, section__course__shortname=course.shortname)
        existed = True
    except Activity.DoesNotExist:
        activity = Activity()

    activity.section = section
    activity.title = title
    activity.type = activity_type
    activity.order = activity_node.get("order")
    activity.digest = digest
    activity.baseline = is_baseline
    activity.image = image
    activity.content = content
    activity.description = description

    if not existed and not is_new_course:
        msg_text = _(u'Activity "%(act)s"(%(digest)s) did not exist \
                     previously.') % {
            'act': activity.title,
            'digest': activity.digest
        }
        messages.warning(request, msg_text)
        CoursePublishingLog(course=course,
                            user=user,
                            action="activity_added",
                            data=msg_text).save()
    else:
        msg_text = _(u'Activity "%(act)s"(%(digest)s) previously existed. \
                    Updated with new information'                                                 ) \
                    % {'act': activity.title,
                       'digest': activity.digest}
        '''
        If we also want to show the activities that previously existed,
        uncomment this next line
        messages.info(req, msg_text)
        '''
        CoursePublishingLog(course=course,
                            user=user,
                            action="activity_updated",
                            data=msg_text).save()

    if (activity_type == "quiz") or (activity_type == "feedback"):
        updated_json = parse_and_save_quiz(user, activity)
        # we need to update the JSON contents both in the XML and in the
        # activity data
        activity_node.find("content").text = \
            "<![CDATA[ " + updated_json + "]]>"
        activity.content = updated_json

    activity.save()

    # save gamification events
    gamification = activity_node.find('gamification')
    events = parse_gamification_events(gamification)
    for event in events:
        e, created = ActivityGamificationEvent.objects.get_or_create(
            activity=activity,
            event=event['name'],
            defaults={
                'points': event['points'],
                'user': request.user
            })

        if created:
            msg_text = _(u'Gamification for "%(event)s" at activity \
                        "%(act)s"(%(digest)s) added'                                                    ) \
                      % {'event': e.event,
                         'act': activity.title,
                         'digest': activity.digest}
            messages.info(request, msg_text)
            CoursePublishingLog(course=course,
                                user=user,
                                action="activity_gamification_added",
                                data=msg_text).save()
def parse_and_save_activity(section, act, is_baseline=False):
    """
    Parses an Activity XML and saves it to the DB
    :param section: section the activity belongs to
    :param act: a XML DOM element containing a single activity
    :param is_baseline: is the activity part of the baseline?
    :return: None
    """
    temp_title = {}
    for t in act.getElementsByTagName("title"):
        temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
    title = json.dumps(temp_title)

    content = ""
    if act.getAttribute("type") == "page":
        temp_content = {}
        for t in act.getElementsByTagName("location"):
            if t.firstChild and t.getAttribute('lang'):
                temp_content[t.getAttribute('lang')] = t.firstChild.nodeValue
        content = json.dumps(temp_content)
    elif act.getAttribute("type") == "quiz":
        for c in act.getElementsByTagName("content"):
            content = c.firstChild.nodeValue
    elif act.getAttribute("type") == "feedback":
        for c in act.getElementsByTagName("content"):
            content = c.firstChild.nodeValue
    elif act.getAttribute("type") == "resource":
        for c in act.getElementsByTagName("location"):
            content = c.firstChild.nodeValue
    elif act.getAttribute("type") == "url":
        temp_content = {}
        for t in act.getElementsByTagName("location"):
            if t.firstChild and t.getAttribute('lang'):
                temp_content[t.getAttribute('lang')] = t.firstChild.nodeValue
        content = json.dumps(temp_content)
    else:
        content = None

    image = None
    if act.getElementsByTagName("image"):
        for i in act.getElementsByTagName("image"):
            image = i.getAttribute('filename')

    if act.getElementsByTagName("description"):
        description = {}
        for d in act.getElementsByTagName("description"):
            if d.firstChild and d.getAttribute('lang'):
                description[d.getAttribute('lang')] = d.firstChild.nodeValue
        description = json.dumps(description)
    else:
        description = None

    activity = Activity()
    activity.section = section
    activity.order = act.getAttribute("order")
    activity.title = title
    activity.type = act.getAttribute("type")
    activity.digest = act.getAttribute("digest")
    activity.baseline = is_baseline
    activity.image = image
    activity.content = content
    activity.description = description
    activity.save()
def parse_and_save_activity(req,
                            user,
                            section,
                            act,
                            new_course,
                            process_quiz_locally,
                            is_baseline=False):
    """
    Parses an Activity XML and saves it to the DB
    :param section: section the activity belongs to
    :param act: a XML DOM element containing a single activity
    :param new_course: boolean indicating if it is a new course or existed previously
    :param process_quiz_locally: should the quiz be created based on the JSON contents?
    :param is_baseline: is the activity part of the baseline?
    :return: None
    """

    title = {}
    for t in act.findall('title'):
        title[t.get('lang')] = t.text
    title = json.dumps(title) if title else None

    description = {}
    for t in act.findall('description'):
        description[t.get('lang')] = t.text
    description = json.dumps(description) if description else None

    content = ""
    act_type = act.get("type")
    if act_type == "page" or act_type == "url":
        temp_content = {}
        for t in act.findall("location"):
            if t.text:
                temp_content[t.get('lang')] = t.text
        content = json.dumps(temp_content)
    elif act_type == "quiz" or act_type == "feedback":
        for c in act.findall("content"):
            content = c.text
    elif act_type == "resource":
        for c in act.findall("location"):
            content = c.text
    else:
        content = None

    image = None
    for i in act.findall("image"):
        image = i.get('filename')

    digest = act.get("digest")
    existed = False
    try:
        activity = Activity.objects.get(digest=digest)
        existed = True
    except Activity.DoesNotExist:
        activity = Activity()

    activity.section = section
    activity.title = title
    activity.type = act_type
    activity.order = act.get("order")
    activity.digest = digest
    activity.baseline = is_baseline
    activity.image = image
    activity.content = content
    activity.description = description

    if not existed and not new_course:
        messages.warning(
            req,
            _('Activity "%(act)s"(%(digest)s) did not exist previously.') % {
                'act': activity,
                'digest': activity.digest
            })
    '''
    If we also want to show the activities that previously existed, uncomment this block
    else:
        messages.info(req, _('Activity "%(act)s"(%(digest)s) previously existed. Updated with new information') % {'act': activity, 'digest':activity.digest})
    '''

    if (act_type == "quiz") and process_quiz_locally:
        updated_json = parse_and_save_quiz(req, user, activity, act)
        # we need to update the JSON contents both in the XML and in the activity data
        act.find("content")[0].text = updated_json
        activity.content = updated_json

    activity.save()

    # save gamification events
    gamification = act.find('gamification')
    events = parse_gamification_events(gamification)
    for event in events:
        e, created = ActivityGamificationEvent.objects.get_or_create(
            activity=activity,
            event=event['name'],
            defaults={
                'points': event['points'],
                'user': req.user
            })

        if created:
            messages.info(
                req,
                _('Gamification for "%(event)s" at activity "%(act)s" added') %
                {
                    'event': e.event,
                    'act': activity
                })
def parse_and_save_activity(section, act, is_baseline=False):
    """
    Parses an Activity XML and saves it to the DB
    :param section: section the activity belongs to
    :param act: a XML DOM element containing a single activity
    :param is_baseline: is the activity part of the baseline?
    :return: None
    """
    temp_title = {}
    for t in act.getElementsByTagName("title"):
        temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
    title = json.dumps(temp_title)

    content = ""
    if act.getAttribute("type") == "page":
        temp_content = {}
        for t in act.getElementsByTagName("location"):
            if t.firstChild and t.getAttribute('lang'):
                temp_content[t.getAttribute('lang')] = t.firstChild.nodeValue
        content = json.dumps(temp_content)
    elif act.getAttribute("type") == "quiz":
        for c in act.getElementsByTagName("content"):
            content = c.firstChild.nodeValue
    elif act.getAttribute("type") == "feedback":
        for c in act.getElementsByTagName("content"):
            content = c.firstChild.nodeValue
    elif act.getAttribute("type") == "resource":
        for c in act.getElementsByTagName("location"):
            content = c.firstChild.nodeValue
    elif act.getAttribute("type") == "url":
        temp_content = {}
        for t in act.getElementsByTagName("location"):
            if t.firstChild and t.getAttribute('lang'):
                temp_content[t.getAttribute('lang')] = t.firstChild.nodeValue
        content = json.dumps(temp_content)
    else:
        content = None
    
    image = None
    if act.getElementsByTagName("image"):
        for i in act.getElementsByTagName("image"):
            image = i.getAttribute('filename') 

    if act.getElementsByTagName("description"):
        description = {}
        for d in act.getElementsByTagName("description"):
            if d.firstChild and d.getAttribute('lang'):
                description[d.getAttribute('lang')] = d.firstChild.nodeValue
        description = json.dumps(description)
    else:
        description = None
          
    activity = Activity()
    activity.section = section
    activity.order = act.getAttribute("order")
    activity.title = title
    activity.type = act.getAttribute("type")
    activity.digest = act.getAttribute("digest")
    activity.baseline = is_baseline
    activity.image = image
    activity.content = content
    activity.description = description
    activity.save()