Beispiel #1
0
def contentsView(request):
    if 'contentId' in request.GET:
        try:
            owner = h5p_contents.objects.get(
                content_id=h5p_get_content_id(request))
        except:
            raise Http404
        h5p_load(request)
        content = include_h5p(request)
        score = None

        if "html" not in content:
            html = '<div>Sorry, preview of H5P content is not yet available.</div>'
            return render(request, 'h5p/content.html', {'html': html})
        else:
            if request.user.is_authenticated:
                h5p_set_started(request.user, h5p_get_content_id(request))
                score = get_user_score(h5p_get_content_id(request),
                                       request.user)

                return render(
                    request, 'h5p/content.html', {
                        'html': content['html'],
                        'data': content['data'],
                        'owner': owner.author,
                        'score': score[0]
                    })
            return render(
                request, 'h5p/content.html', {
                    'html': content['html'],
                    'data': content['data'],
                    'owner': owner.author
                })

    return HttpResponseRedirect('/h5p/listContents')
Beispiel #2
0
def embedView(request):
    if 'contentId' in request.GET:
        h5p_load(request)
        embed = h5p_embed(request)
        score = None
        if request.user.is_authenticated:
            h5p_set_started(request.user, h5p_get_content_id(request))
            score = get_user_score(request.GET['contentId'], request.user)[0]
        return render(request, 'h5p/embed.html', {
            'embed': embed,
            'score': score
        })

    return HttpResponseForbidden()
Beispiel #3
0
def ajax(request):
    if request.method == 'POST':
        if 'content-user-data' in request.GET:
            data = handleContentUserData(request)
            return HttpResponse(data, content_type='application/json')

        elif 'setFinished' in request.GET:
            data = h5p_set_finished(request)
            return HttpResponse(data, content_type='application/json')

    if 'content-user-data' in request.GET:
        data = handleContentUserData(request)
        return HttpResponse(data, content_type='application/json')

    elif 'user-scores' in request.GET:
        score = get_user_score(request.GET['user-scores'], None, True)
        return HttpResponse(score, content_type='application/json')
    return HttpResponseRedirect('/h5p/create')
Beispiel #4
0
    def get_context_data(self, **kwargs):
        ctx = super(ContentDetailView, self).get_context_data(**kwargs)

        self.request.GET = self.request.GET.copy()
        self.request.GET["contentId"] = self.kwargs.get("content_id")
        h5p_load(self.request)
        content = include_h5p(self.request)
        h5p_set_started(self.request.user, self.kwargs.get("content_id"))
        score = get_user_score(self.kwargs.get("content_id"),
                               self.request.user)

        if "html" not in content:
            ctx["html"] = "<div>Sorry, preview of H5P content is not yet available.</div>"
        else:
            ctx["html"] = content["html"]

        ctx["html"] = content["html"]
        ctx["data"] = content["data"]
        ctx["score"] = score[0] if score is not None else None
        ctx["content_id"] = self.kwargs.get("content_id")

        return ctx
Beispiel #5
0
def scoreView(request, contentId):
    try:
        content = h5p_contents.objects.get(content_id=contentId)
    except:
        raise Http404
    if request.user.is_authenticated:
        if request.method == 'POST' and (request.user.username
                                         == content.author
                                         or request.user.is_superuser):
            userData = h5p_content_user_data.objects.filter(
                content_main_id=content.content_id)
            if userData:
                userData.delete()
            userPoints = h5p_points.objects.filter(
                content_id=content.content_id)
            if userPoints:
                userPoints.delete()

            return HttpResponseRedirect('/h5p/score/%s' % content.content_id,
                                        {'status': "Scores has been reset !"})

        if 'user' in request.GET and (request.user.username == content.author
                                      or request.user.is_superuser):
            user = User.objects.get(username=request.GET['user'])
            userData = h5p_content_user_data.objects.filter(
                user_id=user.id, content_main_id=content.content_id)
            if userData:
                userData.delete()
            userPoints = h5p_points.objects.filter(
                uid=user.id, content_id=content.content_id)
            if userPoints:
                userPoints.delete()

            return HttpResponseRedirect(
                '/h5p/score/%s' % content.content_id,
                {'status': "%s's score has been reset !" % user.username})

        if 'download' in request.GET and request.user.is_superuser:
            if request.GET['download'] == 'all':
                scores = export_score()
                scores = ContentFile(scores)
                response = HttpResponse(scores, 'text/plain')
                response['Content-Length'] = scores.size
                response[
                    'Content-Disposition'] = 'attachment; filename="h5pp_users_score.txt"'
            else:
                scores = export_score(request.GET['download'])
                scores = ContentFile(scores)
                response = HttpResponse(scores, 'text/plain')
                response['Content-Length'] = scores.size
                response[
                    'Content-Disposition'] = 'attachment; filename="content_%s_users_score.txt"' % request.GET[
                        'download']

            return response

        listScore = dict()
        if request.user.username == content.author or request.user.is_superuser:
            listScore['owner'] = True

        listScore['data'] = get_user_score(content.content_id)
        if listScore['data'] and listScore['data'].count() > 0:
            return render(request, 'h5p/score.html', {
                'listScore': listScore,
                'content': content
            })

        return render(request, 'h5p/score.html', {
            'status': 'No score available yet.',
            'content': content
        })

    return HttpResponseRedirect('/h5p/login/?next=/h5p/score/' + contentId +
                                '/')