Ejemplo n.º 1
0
def edit_course_gamification(request, course_id):
    if not can_edit_course(request, course_id):
        raise PermissionDenied

    course = get_object_or_404(Course, pk=course_id)

    events_formset = formset_factory(GamificationEventForm,
                                     extra=0,
                                     can_delete=True)
    if request.method == 'POST':
        formset = events_formset(request.POST, request.FILES, prefix='events')
        process_edit_gamification_post(request, course, events_formset)
    else:
        formset = events_formset(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, 'gamification/edit.html', {
            'default_points': default_points,
            'course': course,
            'events_formset': formset,
            'course_events': course_events,
            'activities': activities,
            'media': media
        })
Ejemplo n.º 2
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        dashboard_accessed.send(sender=None, request=self.request, data=None)

        course_list = context['page_obj'].object_list
        course_stats = UserCourseSummary.objects\
            .filter(course__in=list(course_list))\
            .aggregated_stats('total_downloads')

        for course in course_list:
            access_detail = can_view_course_detail(self.request, course.id)
            course.can_edit = can_edit_course(self.request, course.id)
            course.access_detail = access_detail is not None
            for stats in course_stats:
                if stats['course'] == course.id:
                    course.distinct_downloads = stats['distinct']
                    course.total_downloads = stats['total']
                    # remove the element to optimize next searches
                    course_stats.remove(stats)

        context['page_ordering'] = self.get_ordering()
        context['tag_list'] = Tag.objects.all() \
            .exclude(coursetag=None) \
            .order_by('name')
        context['current_tag'] = self.get_current_tag()
        context['course_filter'] = self.get_filter()

        return context
Ejemplo n.º 3
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        course_list = context['page_obj'].object_list
        course_stats = UserCourseSummary.objects\
            .filter(course__in=list(course_list))\
            .aggregated_stats('total_downloads')

        for course in course_list:
            try:
                access_detail = can_view_course_detail(self.request, course.id)
                course.access_detail = access_detail is not None
            except PermissionDenied:
                course.access_detail = None
            course.can_edit = can_edit_course(self.request, course.id)
            course.can_edit_gamification = can_edit_course_gamification(
                self.request, course.id)
            for stats in course_stats:
                if stats['course'] == course.id:
                    course.distinct_downloads = stats['distinct']
                    course.total_downloads = stats['total']
                    # remove the element to optimize next searches
                    course_stats.remove(stats)

        context['page_ordering'] = self.get_ordering()
        context['category_list'] = Category.objects.all().exclude(
            coursecategory=None).order_by('name')
        context['current_category'] = self.get_current_category()
        context['course_filter'] = self.get_filter()

        return context
Ejemplo n.º 4
0
    def get(self, request, course_id):
        if (not can_edit_course(request, course_id)):
            raise PermissionDenied

        course = get_object_or_404(Course, pk=course_id)

        return render(request, 'course/export.html', {'course': course})
Ejemplo n.º 5
0
    def get(self, request, course_id):
        if (not can_edit_course(request, course_id)):
            raise PermissionDenied

        course = Course.objects.get(pk=course_id)

        return render(request, 'course/structure.html', {'course': course})
Ejemplo n.º 6
0
def render_courses_list(request, courses, params=None):

    if params is None:
        params = {}

    course_filter = request.GET.get('visibility', '')
    if course_filter == 'draft':
        courses = courses.filter(is_draft=True)
    elif course_filter == 'archived':
        courses = courses.filter(is_archived=True)

    tag_list = Tag.objects.all().exclude(coursetag=None).order_by('name')
    paginator = Paginator(courses, 25)  # Show 25 per page
    # Make sure page request is an int. If not, deliver first page.
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    course_stats = list(
        UserCourseSummary.objects.filter(
            course__in=courses).values('course').annotate(
                distinct=Count('user'), total=Sum('total_downloads')))

    try:
        courses = paginator.page(page)
    except (EmptyPage, InvalidPage):
        courses = paginator.page(paginator.num_pages)

    for course in courses:
        access_detail = can_view_course_detail(request, course.id)
        course.can_edit = can_edit_course(request, course.id)
        course.access_detail = access_detail is not None

        for stats in course_stats:
            if stats['course'] == course.id:
                course.distinct_downloads = stats['distinct']
                course.total_downloads = stats['total']
                # remove the element to optimize next searches
                course_stats.remove(stats)

    params['page'] = courses
    params['tag_list'] = tag_list
    params['course_filter'] = course_filter

    return render(request, 'course/list.html', params)
Ejemplo n.º 7
0
def upload_step2(request, course_id, editing=False):

    if (editing and not can_edit_course(request, course_id)):
        raise PermissionDenied

    course = Course.objects.get(pk=course_id)

    if request.method == 'POST':
        form = UploadCourseStep2Form(request.POST, request.FILES)
        if form.is_valid() and course:
            # add the tags

            update_course_tags(form, course, request.user)
            redirect = 'oppia:course' if editing else 'oppia:upload_success'
            CoursePublishingLog(
                course=course,
                new_version=course.version,
                user=request.user,
                action="upload_course_published",
                data=_(u'Course published via file upload')).save()
            return HttpResponseRedirect(reverse(redirect))
    else:
        form = UploadCourseStep2Form(initial={
            'tags': course.get_tags(),
            'is_draft': course.is_draft,
        })

    page_title = _(u'Upload Course - step 2') \
        if not editing else _(u'Edit course')
    return render(
        request, 'course/form.html', {
            'form': form,
            'course_title': course.title,
            'editing': editing,
            'title': page_title
        })
Ejemplo n.º 8
0
 def test_func(self):
     return can_edit_course(self.request, self.kwargs[self.pk_url_kwarg])