Toggle learning contents

This commit is contained in:
Daniel Egger 2022-06-14 16:41:21 +02:00
parent ad7d63a8e9
commit a27fa35fe3
6 changed files with 35 additions and 18 deletions

View File

@ -6,8 +6,10 @@ import ItCheckbox from '@/components/ui/ItCheckbox.vue';
const props = defineProps(['learningSequence', 'completionData'])
const contentCompleted = (learningContent) => {
if (props.completionData?.json_data?.completed_learning_contents) {
return learningContent.translation_key in props.completionData.json_data.completed_learning_contents;
if (props.completionData?.completed_learning_contents) {
return props.completionData.completed_learning_contents.findIndex((e) => {
return e.learning_content_key === learningContent.translation_key && e.completed;
}) > 0;
}
return false;
@ -41,7 +43,7 @@ const contentCompleted = (learningContent) => {
>
<ItCheckbox
:modelValue="contentCompleted(learningContent)"
@click="$emit('toggleLearningContentCheckbox', learningContent)"
@click="$emit('toggleLearningContentCheckbox', learningContent, !contentCompleted(learningContent))"
>
{{ learningContent.contents[0].type }}: {{ learningContent.title }}
</ItCheckbox>

View File

@ -17,12 +17,13 @@ export default {
}
},
methods: {
toggleLearningContentCheckbox(learningContent) {
log.debug('toggleLearningContentCheckbox', learningContent);
toggleLearningContentCheckbox(learningContent, completed=true) {
log.debug('toggleLearningContentCheckbox', learningContent, completed);
console.log(learningContent);
itPost('/api/completion/complete_learning_content/', {
learning_content_key: learningContent.translation_key,
completed: completed,
}).then((data) => {
this.completionData = data;
});

View File

@ -12,4 +12,7 @@ class UserCircleCompletionSerializer(serializers.ModelSerializer):
class LearningContentCompletionSerializer(serializers.ModelSerializer):
class Meta:
model = LearningContentCompletion
fields = ['id', 'created_at', 'updated_at', 'user', 'learning_content_key', 'circle_key', 'json_data']
fields = [
'id', 'created_at', 'updated_at', 'user', 'learning_content_key', 'circle_key',
'completed', 'json_data',
]

View File

@ -35,7 +35,7 @@ class CompletionApiTestCase(APITestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response_json['circle_key'], circle_key)
self.assertEqual(
response_json['json_data']['completed_learning_contents'][learning_content_key]['learning_content_key'],
response_json['completed_learning_contents'][learning_content_key]['learning_content_key'],
learning_content_key
)
@ -47,7 +47,7 @@ class CompletionApiTestCase(APITestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response_json['circle_key'], circle_key)
self.assertEqual(
response_json['json_data']['completed_learning_contents'][learning_content_key]['learning_content_key'],
response_json['completed_learning_contents'][learning_content_key]['learning_content_key'],
learning_content_key
)

View File

@ -1,11 +1,9 @@
from datetime import datetime
import structlog
from rest_framework.decorators import api_view
from rest_framework.response import Response
from vbv_lernwelt.completion.models import LearningContentCompletion, UserCircleCompletion
from vbv_lernwelt.completion.serializers import UserCircleCompletionSerializer
from vbv_lernwelt.completion.serializers import UserCircleCompletionSerializer, LearningContentCompletionSerializer
from vbv_lernwelt.learnpath.models import LearningContent
logger = structlog.get_logger(__name__)
@ -18,30 +16,44 @@ def request_user_circle_completion(request, circle_key):
circle_key=circle_key,
)
response_data = {}
if ucc.count() > 0:
return Response(status=200, data=UserCircleCompletionSerializer(ucc.first()).data)
else:
return Response(status=200, data={})
response_data = UserCircleCompletionSerializer(ucc.first()).data
response_data['completed_learning_contents'] = LearningContentCompletionSerializer(
LearningContentCompletion.objects.filter(circle_key=circle_key, user=request.user),
many=True
).data
return Response(status=200, data=response_data)
@api_view(['POST'])
def complete_learning_content(request):
learning_content_key = request.data.get('learning_content_key')
completed = request.data.get('completed', True)
learning_content = LearningContent.objects.get(translation_key=learning_content_key)
circle_key = learning_content.get_parent().translation_key
LearningContentCompletion.objects.get_or_create(
lcc, created = LearningContentCompletion.objects.get_or_create(
user=request.user,
learning_content_key=learning_content_key,
circle_key=circle_key,
)
lcc.completed = completed
lcc.save()
ucc, created = UserCircleCompletion.objects.get_or_create(
user=request.user,
circle_key=circle_key,
)
ucc.save()
response_data = UserCircleCompletionSerializer(ucc).data
response_data['completed_learning_contents'] = LearningContentCompletionSerializer(
LearningContentCompletion.objects.filter(circle_key=circle_key, user=request.user),
many=True
).data
logger.debug(
'learning content completed',
@ -51,4 +63,4 @@ def complete_learning_content(request):
user_id=request.user.id,
)
return Response(status=200, data=UserCircleCompletionSerializer(ucc).data)
return Response(status=200, data=response_data)

View File

@ -66,6 +66,5 @@ svg {
hover:bg-sky-400 hover:border-sky-400
disabled:opacity-50 disabled:cursor-not-allowed
}
}