Example #1
0
def save_media_points(request, form, course, media):
    MediaGamificationEvent.objects.filter(media=media).delete()
    for x in form.cleaned_data:
        media_game_event = MediaGamificationEvent()
        media_game_event.user = request.user
        media_game_event.media = media
        media_game_event.event = x
        media_game_event.points = form.cleaned_data[x]
        media_game_event.save()

    writer = GamificationXMLWriter(course)
    new_version = writer.update_gamification(request.user)
Example #2
0
def save_activity_points(request, form, course, activity):
    # update the gamification tables
    ActivityGamificationEvent.objects.filter(activity=activity).delete()
    for x in form.cleaned_data:
        activity_game_event = ActivityGamificationEvent()
        activity_game_event.user = request.user
        activity_game_event.activity = activity
        activity_game_event.event = x
        activity_game_event.points = form.cleaned_data[x]
        activity_game_event.save()

    writer = GamificationXMLWriter(course)
    new_version = writer.update_gamification(request.user)
Example #3
0
def save_course_points(request, form, course):
    # update the gamification tables
    CourseGamificationEvent.objects.filter(course=course).delete()
    for x in form.cleaned_data:
        course_game_event = CourseGamificationEvent()
        course_game_event.user = request.user
        course_game_event.course = course
        course_game_event.event = x
        course_game_event.points = form.cleaned_data[x]
        course_game_event.save()

    writer = GamificationXMLWriter(course)
    new_version = writer.update_gamification(request.user)
Example #4
0
def process_edit_gamification_post(request, course, events_formset):
    formset = events_formset(request.POST, request.FILES, prefix='events')
    if formset.is_valid():

        updated = False
        for form in formset:
            process_gamification_formset(request, formset, form)
            updated = True

        if updated:
            writer = GamificationXMLWriter(course)
            new_version = writer.update_gamification(request.user)
            messages.success(
                request,
                'Course XML updated. New version: {}'.format(new_version))
Example #5
0
def edit_course_gamification(request, course_id):
    if not can_edit_course(request, course_id):
        raise exceptions.PermissionDenied

    course = get_object_or_404(Course, pk=course_id)

    EventsFormset = formset_factory(GamificationEventForm,
                                    extra=0,
                                    can_delete=True)
    if request.method == 'POST':
        formset = EventsFormset(request.POST, request.FILES, prefix='events')
        if formset.is_valid():

            updated = False
            for form in formset:
                # extract name from each form and save
                event = form.cleaned_data.get('event')
                level = form.cleaned_data.get('level')
                points = form.cleaned_data.get('points')
                reference = form.cleaned_data.get('reference')
                defaults = {'points': points, 'user': request.user}

                to_delete = formset.can_delete and formset._should_delete_form(
                    form)

                updated = True
                if level == 'course':
                    if to_delete:
                        CourseGamificationEvent.objects.filter(
                            course_id=reference, event=event).delete()
                    else:
                        CourseGamificationEvent.objects.update_or_create(
                            course_id=reference,
                            event=event,
                            defaults=defaults)
                elif level == 'activity':
                    if to_delete:
                        ActivityGamificationEvent.objects.filter(
                            activity_id=reference, event=event).delete()
                    else:
                        ActivityGamificationEvent.objects.update_or_create(
                            activity_id=reference,
                            event=event,
                            defaults=defaults)
                elif level == 'media':
                    if to_delete:
                        MediaGamificationEvent.objects.filter(
                            media_id=reference, event=event).delete()
                    else:
                        MediaGamificationEvent.objects.update_or_create(
                            media_id=reference, event=event, defaults=defaults)

            if updated:
                writer = GamificationXMLWriter(course)
                new_version = writer.update_gamification(request.user)
                messages.success(
                    request,
                    'Course XML updated. New version: {}'.format(new_version))
        else:
            print formset.errors
    else:
        formset = EventsFormset(prefix='events')

    activities = Activity.objects.filter(
        section__course=course).select_related('section').prefetch_related(
            'gamification_events')
    media = Media.objects.filter(
        course=course).prefetch_related('gamification_events')

    default_points = {
        'course':
        DefaultGamificationEvent.objects.exclude(
            level=DefaultGamificationEvent.GLOBAL),
        'activity':
        DefaultGamificationEvent.objects.filter(
            level=DefaultGamificationEvent.ACTIVITY),
        'quiz':
        DefaultGamificationEvent.objects.filter(
            level=DefaultGamificationEvent.QUIZ),
        'media':
        DefaultGamificationEvent.objects.filter(
            level=DefaultGamificationEvent.MEDIA),
    }

    course_events = CourseGamificationEvent.objects.filter(course=course)
    course.events = {}
    for event in course_events:
        course.events[event.event] = event.points

    for activity in activities:
        activity.events = {}
        for event in activity.gamification_events.all():
            activity.events[event.event] = event.points

    for m in media:
        m.events = {}
        for event in m.gamification_events.all():
            m.events[event.event] = event.points

    return render(
        request, 'oppia/gamification/edit.html', {
            'default_points': default_points,
            'course': course,
            'events_formset': formset,
            'course_events': course_events,
            'activities': activities,
            'media': media
        })
Example #6
0
def process_course(extract_path, f, mod_name, request, user):
    xml_path = os.path.join(extract_path, mod_name, "module.xml")
    # check that the module.xml file exists
    if not os.path.isfile(xml_path):
        msg_text = _(u"Zip file does not contain a module.xml file")
        messages.info(request, msg_text, extra_tags="danger")
        CoursePublishingLog(user=user, action="no_module_xml",
                            data=msg_text).save()
        return False, 400, False

    # parse the module.xml file
    doc = ET.parse(xml_path)
    meta_info = parse_course_meta(doc)

    is_new_course = False
    oldsections = []
    old_course_filename = None

    # Find if course already exists
    try:
        course = Course.objects.get(shortname=meta_info['shortname'])
        course_manager = CoursePermissions.objects.filter(
            user=user, course=course, role=CoursePermissions.MANAGER).count()
        # check that the current user is allowed to wipe out the other course
        if course.user != user and course_manager == 0:
            msg_text = \
                _(u"Sorry, you do not have permissions to update this course.")
            messages.info(request, msg_text)
            CoursePublishingLog(course=course,
                                new_version=meta_info['versionid'],
                                old_version=course.version,
                                user=user,
                                action="permissions_error",
                                data=msg_text).save()
            return False, 401, is_new_course
        # check if course version is older
        if course.version > meta_info['versionid']:
            msg_text = _(u"A newer version of this course already exists")
            messages.info(request, msg_text)
            CoursePublishingLog(course=course,
                                new_version=meta_info['versionid'],
                                old_version=course.version,
                                user=user,
                                action="newer_version_exists",
                                data=msg_text).save()
            return False, 400, is_new_course

        # obtain the old sections
        oldsections = list(
            Section.objects.filter(course=course).values_list('pk', flat=True))
        # wipe out old media
        oldmedia = Media.objects.filter(course=course)
        oldmedia.delete()

        old_course_filename = course.filename
        course.lastupdated_date = timezone.now()

    except Course.DoesNotExist:
        course = Course()
        course.status = CourseStatus.DRAFT
        is_new_course = True

    old_course_version = course.version

    course.shortname = meta_info['shortname']
    course.title = meta_info['title']
    course.description = meta_info['description']
    course.version = meta_info['versionid']
    course.priority = int(meta_info['priority'])
    course.user = user
    course.filename = f.name
    course.save()

    if not parse_course_contents(request, doc, course, user, is_new_course):
        return False, 500, is_new_course
    clean_old_course(request, user, oldsections, old_course_filename, course)

    # save gamification events
    if 'gamification' in meta_info:
        events = parse_gamification_events(meta_info['gamification'])
        for event in events:
            # Only add events if the didn't exist previously
            e, created = CourseGamificationEvent.objects.get_or_create(
                course=course,
                event=event['name'],
                defaults={
                    'points': event['points'],
                    'user': user
                })

            if created:
                msg_text = \
                    _(u'Gamification for "%(event)s" at course level added') \
                    % {'event': e.event}
                messages.info(request, msg_text)
                CoursePublishingLog(course=course,
                                    new_version=meta_info['versionid'],
                                    old_version=old_course_version,
                                    user=user,
                                    action="gamification_added",
                                    data=msg_text).save()

    tmp_path = replace_zip_contents(xml_path, doc, mod_name, extract_path)
    # Extract the final file into the courses area for preview
    zipfilepath = os.path.join(settings.COURSE_UPLOAD_DIR, f.name)
    shutil.copy(tmp_path + ".zip", zipfilepath)

    course_preview_path = os.path.join(settings.MEDIA_ROOT, "courses")
    ZipFile(zipfilepath).extractall(path=course_preview_path)

    writer = GamificationXMLWriter(course)
    writer.update_gamification(request.user)

    return course, 200, is_new_course
Example #7
0
def process_course(extract_path, f, mod_name, request, user):
    xml_path = os.path.join(extract_path, mod_name, "module.xml")
    # check that the module.xml file exists
    if not os.path.isfile(xml_path):
        messages.info(request,
                      _("Zip file does not contain a module.xml file"),
                      extra_tags="danger")
        return False, 400

    # parse the module.xml file
    doc = ET.parse(xml_path)
    meta_info = parse_course_meta(doc)

    new_course = False
    oldsections = []
    old_course_filename = None

    # Find if course already exists
    try:
        course = Course.objects.get(shortname=meta_info['shortname'])
        # check that the current user is allowed to wipe out the other course
        if course.user != user:
            messages.info(
                request,
                _("Sorry, only the original owner may update this course"))
            return False, 401
        # check if course version is older
        if course.version > meta_info['versionid']:
            messages.info(request,
                          _("A newer version of this course already exists"))
            return False, 400

        # obtain the old sections
        oldsections = list(
            Section.objects.filter(course=course).values_list('pk', flat=True))
        # wipe out old media
        oldmedia = Media.objects.filter(course=course)
        oldmedia.delete()

        old_course_filename = course.filename
        course.lastupdated_date = timezone.now()

    except Course.DoesNotExist:
        course = Course()
        course.is_draft = True
        new_course = True

    course.shortname = meta_info['shortname']
    course.title = meta_info['title']
    course.description = meta_info['description']
    course.version = meta_info['versionid']
    course.user = user
    course.filename = f.name
    course.save()

    # save gamification events
    if 'gamification' in meta_info:
        events = parse_gamification_events(meta_info['gamification'])
        for event in events:
            # Only add events if the didn't exist previously
            e, created = CourseGamificationEvent.objects.get_or_create(
                course=course,
                event=event['name'],
                defaults={
                    'points': event['points'],
                    'user': user
                })

            if created:
                messages.info(
                    request,
                    _('Gamification for "%(event)s" at course level added') %
                    {'event': e.event})

    process_quizzes_locally = False
    if 'exportversion' in meta_info and meta_info[
            'exportversion'] >= settings.OPPIA_EXPORT_LOCAL_MINVERSION:
        process_quizzes_locally = True

    parse_course_contents(request, doc, course, user, new_course,
                          process_quizzes_locally)
    clean_old_course(request, oldsections, old_course_filename, course)

    tmp_path = replace_zip_contents(xml_path, doc, mod_name, extract_path)
    # Extract the final file into the courses area for preview
    zipfilepath = os.path.join(settings.COURSE_UPLOAD_DIR, f.name)
    shutil.copy(tmp_path + ".zip", zipfilepath)

    course_preview_path = os.path.join(settings.MEDIA_ROOT, "courses")
    ZipFile(zipfilepath).extractall(path=course_preview_path)

    writer = GamificationXMLWriter(course)
    writer.update_gamification(request.user)

    return course, 200