Exemple #1
0
 def create_activity(self, title, content=''):
     new_section = self.create_section('{"en": "My section"}')
     new_activity = Activity(title=title,
                             content=content,
                             section=new_section,
                             order=1)
     new_activity.save()
     return new_activity
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 #3
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()
Exemple #5
0
    def parse_and_save_activity(self,
                                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")
        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)
            # 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
    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 #7
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
    """

    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 handle_uploaded_file(f, extract_path, request):
    zipfilepath = settings.COURSE_UPLOAD_DIR + f.name
    
    with open(zipfilepath, 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)
            
    zip = zipfile.ZipFile(zipfilepath)
    zip.extractall(path=extract_path)      
    
    mod_name = ''
    for dir in os.listdir(extract_path)[:1]:
        mod_name = dir
       
    # check there is at least a sub dir 
    if mod_name == '':
        messages.info(request,_("Invalid course zip file"))
        return False
    
    # check that the 
    if not os.path.isfile(extract_path + mod_name + "/module.xml"):
        messages.info(request,_("Zip file does not contain a module.xml file"))
        return False
      
    # parse the module.xml file
    doc = xml.dom.minidom.parse(extract_path + mod_name + "/module.xml") 
    for meta in doc.getElementsByTagName("meta")[:1]:
        versionid = 0
        for v in meta.getElementsByTagName("versionid")[:1]:
            versionid = int(v.firstChild.nodeValue)
        temp_title = {}
        for t in meta.childNodes:
            if t.nodeName == "title":
                temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
        title = json.dumps(temp_title)
        shortname = ''
        for sn in meta.getElementsByTagName("shortname")[:1]:
            shortname = sn.firstChild.nodeValue
    
    old_course_filename = None
    # Find if course already exists
    try: 
        course = Course.objects.get(shortname = shortname)
        old_course_filename = course.filename
         
        # check that the current user is allowed to wipe out the other course
        if course.user != request.user:
            messages.info(request,_("Sorry, only the original owner may update this course"))
            return False
        
        # check if course version is older
        if course.version > versionid:
            messages.info(request,_("A newer version of this course already exists"))
            return False
        # wipe out the old sections/activities/media
        oldsections = Section.objects.filter(course = course)
        oldsections.delete()
        oldmedia = Media.objects.filter(course = course)
        oldmedia.delete()
        
        course.shortname = shortname
        course.title = title
        course.version = versionid
        course.user = request.user
        course.filename = f.name
        course.lastupdated_date = datetime.datetime.now()
        course.save()
    except Course.DoesNotExist:
        course = Course()
        course.shortname = shortname
        course.title = title
        course.version = versionid
        course.user = request.user
        course.filename = f.name
        course.save()
        
    # add all the sections
    for structure in doc.getElementsByTagName("structure")[:1]:
        
        if structure.getElementsByTagName("section").length == 0:
            messages.info(request,_("There don't appear to be any activities in this upload file."))
            course.delete()
            return False
        
        for s in structure.getElementsByTagName("section"):
            temp_title = {}
            for t in s.childNodes:
                if t.nodeName == 'title':
                    temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
            title = json.dumps(temp_title)
            section = Section()
            section.course = course
            section.title = title
            section.order = s.getAttribute("order")
            section.save()
            
            # add all the activities
            for activities in s.getElementsByTagName("activities")[:1]:
                for a in activities.getElementsByTagName("activity"):
                    temp_title = {}
                    for t in a.getElementsByTagName("title"):
                        temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
                    title = json.dumps(temp_title)
                    activity = Activity()
                    activity.section = section
                    activity.order = a.getAttribute("order")
                    activity.title = title
                    activity.type = a.getAttribute("type")
                    activity.digest = a.getAttribute("digest")
                    activity.save()
                    
    # add all the media
    for files in doc.lastChild.lastChild.childNodes:
        if files.nodeName == 'file':
            media = Media()
            media.course = course
            media.filename = files.getAttribute("filename")
            media.download_url = files.getAttribute("download_url")
            media.digest = files.getAttribute("digest")
            media.save()
    
    if old_course_filename is not None and course.version != versionid:
        os.remove(settings.COURSE_UPLOAD_DIR + old_course_filename)
    return course       
              
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()           
Exemple #11
0
def handle_uploaded_file(f, extract_path, request):
    zipfilepath = settings.COURSE_UPLOAD_DIR + f.name

    with open(zipfilepath, 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

    zip = zipfile.ZipFile(zipfilepath)
    zip.extractall(path=extract_path)

    mod_name = ''
    for dir in os.listdir(extract_path)[:1]:
        mod_name = dir

    # check there is at least a sub dir
    if mod_name == '':
        messages.info(request, _("Invalid course zip file"))
        return False

    # check that the
    if not os.path.isfile(extract_path + mod_name + "/module.xml"):
        messages.info(request,
                      _("Zip file does not contain a module.xml file"))
        return False

    # parse the module.xml file
    doc = xml.dom.minidom.parse(extract_path + mod_name + "/module.xml")
    for meta in doc.getElementsByTagName("meta")[:1]:
        versionid = 0
        for v in meta.getElementsByTagName("versionid")[:1]:
            versionid = int(v.firstChild.nodeValue)
        temp_title = {}
        for t in meta.childNodes:
            if t.nodeName == "title":
                temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
        title = json.dumps(temp_title)
        shortname = ''
        for sn in meta.getElementsByTagName("shortname")[:1]:
            shortname = sn.firstChild.nodeValue

    old_course_filename = None
    # Find if course already exists
    try:
        course = Course.objects.get(shortname=shortname)
        old_course_filename = course.filename

        # check that the current user is allowed to wipe out the other course
        if course.user != request.user:
            messages.info(
                request,
                _("Sorry, only the original owner may update this course"))
            return False

        # check if course version is older
        if course.version > versionid:
            messages.info(request,
                          _("A newer version of this course already exists"))
            return False
        # wipe out the old sections/activities/media
        oldsections = Section.objects.filter(course=course)
        oldsections.delete()
        oldmedia = Media.objects.filter(course=course)
        oldmedia.delete()

        course.shortname = shortname
        course.title = title
        course.version = versionid
        course.user = request.user
        course.filename = f.name
        course.lastupdated_date = datetime.datetime.now()
        course.save()
    except Course.DoesNotExist:
        course = Course()
        course.shortname = shortname
        course.title = title
        course.version = versionid
        course.user = request.user
        course.filename = f.name
        course.save()

    # add all the sections
    for structure in doc.getElementsByTagName("structure")[:1]:

        if structure.getElementsByTagName("section").length == 0:
            messages.info(
                request,
                _("There don't appear to be any activities in this upload file."
                  ))
            course.delete()
            return False

        for s in structure.getElementsByTagName("section"):
            temp_title = {}
            for t in s.childNodes:
                if t.nodeName == 'title':
                    temp_title[t.getAttribute('lang')] = t.firstChild.nodeValue
            title = json.dumps(temp_title)
            section = Section()
            section.course = course
            section.title = title
            section.order = s.getAttribute("order")
            section.save()

            # add all the activities
            for activities in s.getElementsByTagName("activities")[:1]:
                for a in activities.getElementsByTagName("activity"):
                    temp_title = {}
                    for t in a.getElementsByTagName("title"):
                        temp_title[t.getAttribute(
                            'lang')] = t.firstChild.nodeValue
                    title = json.dumps(temp_title)
                    activity = Activity()
                    activity.section = section
                    activity.order = a.getAttribute("order")
                    activity.title = title
                    activity.type = a.getAttribute("type")
                    activity.digest = a.getAttribute("digest")
                    activity.save()

    # add all the media
    for files in doc.lastChild.lastChild.childNodes:
        if files.nodeName == 'file':
            media = Media()
            media.course = course
            media.filename = files.getAttribute("filename")
            media.download_url = files.getAttribute("download_url")
            media.digest = files.getAttribute("digest")
            media.save()

    if old_course_filename is not None and course.version != versionid:
        os.remove(settings.COURSE_UPLOAD_DIR + old_course_filename)
    return course