Commit bdf8fa6b7bafcf776529b03af62ef0c17bc0c840

Authored by fbormann
1 parent 663bcb2b

solved empty tag for single and multiple topics options

Showing 2 changed files with 64 additions and 30 deletions   Show diff stats
reports/forms.py
... ... @@ -29,7 +29,6 @@ class ResourceAndTagForm(forms.Form):
29 29 initial = kwargs['initial']
30 30 self.fields['resource'].choices = [(classes.__name__.lower(), classes.__name__.lower()) for classes in initial['class_name']]
31 31 self.fields['tag'].choices = [(tag.id, tag.name) for tag in initial['tag']]
32   -
33 32  
34 33  
35 34 class CreateInteractionReportForm(forms.Form):
... ...
reports/views.py
... ... @@ -23,6 +23,7 @@ import pandas as pd
23 23 import math
24 24 from io import BytesIO
25 25 import os
  26 +import copy
26 27  
27 28 class ReportView(LoginRequiredMixin, generic.FormView):
28 29 template_name = "reports/create.html"
... ... @@ -46,19 +47,7 @@ class ReportView(LoginRequiredMixin, generic.FormView):
46 47 context = super(ReportView, self).get_context_data(**kwargs)
47 48 subject = Subject.objects.get(id=self.request.GET['subject_id'])
48 49  
49   - context['subject'] = subject
50   -
51   - topics = subject.topic_subject.all()
52   - #get all resources associated with topics
53   - tags = []
54   - for topic in topics:
55   - resources_set = topic.resource_topic.all()
56   - for resource in resources_set:
57   - for tag in resource.tags.all():
58   - tags.append(tag)
59   -
60   -
61   - classes = Resource.__subclasses__()
  50 + context['subject'] = subject
62 51  
63 52 #set formset
64 53 resourceTagFormSet = formset_factory(ResourceAndTagForm, formset=BaseResourceAndTagFormset)
... ... @@ -104,6 +93,10 @@ class ReportView(LoginRequiredMixin, generic.FormView):
104 93 for tag in resource.tags.all():
105 94 tags.append(tag)
106 95  
  96 +
  97 + t = Tag(name=" ")
  98 + t.id = -1 #so I know he choose empyt one
  99 + tags.append(t)
107 100 classes = Resource.__subclasses__()
108 101 amount_of_forms = self.request.POST['form-TOTAL_FORMS']
109 102 initial_datum = {'class_name': classes , 'tag': tags}
... ... @@ -211,7 +204,7 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
211 204 #I use this so the system can gather data up to end_date 11h59 p.m.
212 205 end_date = end_date + timedelta(days=1)
213 206  
214   -
  207 + self.used_tags = copy.deepcopy(tags_id) #so I can check whether we are dealing with multiple or single tags (empty option)
215 208 #For each student in the subject
216 209 for student in students:
217 210 data[student.id] = []
... ... @@ -303,8 +296,8 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
303 296 distinct_days += 1
304 297  
305 298 interactions[_('Number of distinct days the user access the subject. ')] = distinct_days
306   - interactions[_("Class")] = ""
307   - interactions[_("Performance")] = ""
  299 + interactions[_("Class")] = _("Undefined")
  300 + interactions[_("Performance")] = _("Undefined")
308 301 for value in interactions.values():
309 302 data[student.id].append(value)
310 303  
... ... @@ -316,13 +309,47 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
316 309 def get_resources_and_tags_data(self, resources_types, tags, student, subject, topics, init_date, end_date):
317 310 data = OrderedDict()
318 311  
  312 + new_tags = [] #tags will be replaced by this variable
319 313 for i in range(len(resources_types)):
320   -
  314 +
  315 + if tags[i] == "-1": #it means I should select all of tags available for this kind of resource
  316 + new_tags = set()
  317 + if not isinstance(topics,Topic):
  318 + topics = subject.topic_subject.all()
  319 + for topic in topics:
  320 + resource_set = Resource.objects.select_related(resources_types[i].lower()).filter(topic = topic)
  321 +
  322 + for resource in resource_set:
  323 + if resource._my_subclass == resources_types[i].lower():
  324 + for tag in resource.tags.all():
  325 + if tag.name != "":
  326 + new_tags.add(tag)
  327 + else:
  328 + topics = topics
  329 + resource_set = Resource.objects.select_related(resources_types[i].lower()).filter(topic = topics)
  330 +
  331 + for resource in resource_set:
  332 + if resource._my_subclass == resources_types[i].lower():
  333 + for tag in resource.tags.all():
  334 + if tag.name != "":
  335 + new_tags.add(tag)
  336 + data = {}
  337 +
  338 +
  339 + new_tags = [tag.id for tag in new_tags]
  340 + tags[i] = new_tags
  341 + for i in range(len(resources_types)):
  342 + original_tags = copy.deepcopy(self.used_tags) #effectiving copy
321 343 if isinstance(topics,Topic):
322   - resources = Resource.objects.select_related(resources_types[i].lower()).filter(tags__in = tags, topic=topics)
  344 + if type(tags[i]) == type(list()):
  345 + resources = Resource.objects.select_related(resources_types[i].lower()).filter(tags__in = tags[i], topic=topics)
  346 + else:
  347 + resources = Resource.objects.select_related(resources_types[i].lower()).filter(tags__in = [tags[i]], topic=topics)
323 348 else:
324   - resources = Resource.objects.select_related(resources_types[i].lower()).filter(tags__in = tags, topic__in=topics)
325   -
  349 + if type(tags[i]) == type(list()):
  350 + resources = Resource.objects.select_related(resources_types[i].lower()).filter(tags__in = tags[i], topic__in=topics)
  351 + else:
  352 + resources = Resource.objects.select_related(resources_types[i].lower()).filter(tags__in = [tags[i]], topic__in=topics)
326 353 distinct_resources = 0
327 354 total_count = 0
328 355  
... ... @@ -425,15 +452,20 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
425 452 mapping['webconference'] = str(_('Web Conference'))
426 453 mapping['ytvideo'] = str(_('YouTube Video'))
427 454 mapping['webpage'] = str(_('WebPage'))
428   - data[str(_("number of visualizations of ")) + mapping[str(resources_types[i])] + str(_(" with tag ")) + Tag.objects.get(id=int(tags[i])).name] = total_count
429   - data[str(_("number of visualizations of distintic ")) + mapping[str(resources_types[i])] + str(_(" with tag ")) + Tag.objects.get(id=int(tags[i])).name] = distinct_resources
430   - data[str(_("distintic days ")) + mapping[str(resources_types[i])] + str(_(" with tag ")) + Tag.objects.get(id=int(tags[i])).name] = distinct_days
431   -
432   - if resources_types[i].lower() in ["ytvideo", "webconference"]:
433   - data[str(_("hours viewed of ")) + str(resources_types[i]) + str(_(" with tag ")) + Tag.objects.get(id=int(tags[i])).name] = hours_viewed
  455 + if original_tags[i] != "-1":
  456 + data[str(_("number of visualizations of ")) + mapping[str(resources_types[i])] + str(_(" with tag ")) + Tag.objects.get(id=int(tags[i])).name] = total_count
  457 + data[str(_("number of visualizations of distintic ")) + mapping[str(resources_types[i])] + str(_(" with tag ")) + Tag.objects.get(id=int(tags[i])).name] = distinct_resources
  458 + data[str(_("distintic days ")) + mapping[str(resources_types[i])] + str(_(" with tag ")) + Tag.objects.get(id=int(tags[i])).name] = distinct_days
  459 +
  460 + if resources_types[i].lower() in ["ytvideo", "webconference"]:
  461 + data[str(_("hours viewed of ")) + str(resources_types[i]) + str(_(" with tag ")) + Tag.objects.get(id=int(tags[i])).name] = hours_viewed
  462 + else:
  463 + data[str(_("number of visualizations of ")) + mapping[str(resources_types[i])] ] = total_count
  464 + data[str(_("number of visualizations of distintic ")) + mapping[str(resources_types[i])] ] = distinct_resources
  465 + data[str(_("distintic days ")) + mapping[str(resources_types[i])]] = distinct_days
434 466  
435   - """data["distinct" + str(resources[i]) + " with tag " + Tag.objects.get(id=int(tags[i])).name] = Log.objects.filter(action="view", resource=resources[i].lower(),
436   - user_id = student.id, context__contains = {'subject_id': subject.id}).distinct().count()"""
  467 + if resources_types[i].lower() in ["ytvideo", "webconference"]:
  468 + data[str(_("hours viewed of ")) + str(resources_types[i]) ] = hours_viewed
437 469  
438 470 return data
439 471  
... ... @@ -504,7 +536,10 @@ def get_tags(request):
504 536  
505 537 #adding empty tag for the purpose of giving the user this option for adicional behavior
506 538 tags = list(tags)
507   - tags.append(Tag(name=" "))
  539 + #creating empty tag
  540 + t = Tag(name=" ")
  541 + t.id = -1 #so I know he choose empyt one
  542 + tags.append(t)
508 543 data['tags'] = [ {'id':tag.id, 'name':tag.name} for tag in tags]
509 544 return JsonResponse(data)
510 545  
... ...