Commit ae48710d5961176a266db8a77e2d2ce44be384a1
1 parent
2822dbd5
Exists in
master
and in
2 other branches
create method to capture most used tags in a category and modified tag chart to …
…replace tag chart if it exists
Showing
3 changed files
with
38 additions
and
4 deletions
Show diff stats
analytics/static/analytics/js/charts.js
@@ -352,7 +352,12 @@ var charts = { | @@ -352,7 +352,12 @@ var charts = { | ||
352 | 352 | ||
353 | 353 | ||
354 | var container_div = d3.select("#most-used-tags-body"); | 354 | var container_div = d3.select("#most-used-tags-body"); |
355 | + if($('#most_used_tag_chart').length > 0){ | ||
356 | + $('#most_used_tag_chart').remove(); | ||
357 | + } | ||
358 | + | ||
355 | var svg = container_div.append("svg").attr("width", "100%").attr("height", height) | 359 | var svg = container_div.append("svg").attr("width", "100%").attr("height", height) |
360 | + .attr("id", "most_used_tag_chart") | ||
356 | .style("margin","auto") | 361 | .style("margin","auto") |
357 | .style("display","block") | 362 | .style("display","block") |
358 | .style("background","#ddf8e7") | 363 | .style("background","#ddf8e7") |
@@ -505,8 +510,6 @@ var charts = { | @@ -505,8 +510,6 @@ var charts = { | ||
505 | 510 | ||
506 | 511 | ||
507 | $(document).ready(function(){ | 512 | $(document).ready(function(){ |
508 | - charts.most_used_tags('/analytics/most_used_tags'); | ||
509 | - //charts.build_resource('/topics/count_resources/'); | ||
510 | - charts.build_bubble_user('/analytics/most_active_users/'); | 513 | + |
511 | //charts.most_accessed_subjects('/subjects/most_accessed_subjects'); | 514 | //charts.most_accessed_subjects('/subjects/most_accessed_subjects'); |
512 | }); | 515 | }); |
analytics/urls.py
@@ -12,4 +12,5 @@ urlpatterns = [ | @@ -12,4 +12,5 @@ urlpatterns = [ | ||
12 | url(r'^most_active_users/$', views.most_active_users, name= "most_active_users"), | 12 | url(r'^most_active_users/$', views.most_active_users, name= "most_active_users"), |
13 | url(r'^amount_active_users_per_day/$', views.most_active_users_in_a_month, name="most_active_users_in_a_month"), | 13 | url(r'^amount_active_users_per_day/$', views.most_active_users_in_a_month, name="most_active_users_in_a_month"), |
14 | url(r'^get_days_of_the_week_log/$', views.get_days_of_the_week_log, name="get_days_of_the_week_log"), | 14 | url(r'^get_days_of_the_week_log/$', views.get_days_of_the_week_log, name="get_days_of_the_week_log"), |
15 | + url(r'^get_category_tags/$', views.category_tags, name='get_category_tags'), | ||
15 | ] | 16 | ] |
16 | \ No newline at end of file | 17 | \ No newline at end of file |
analytics/views.py
@@ -5,7 +5,7 @@ from django.db.models import Count | @@ -5,7 +5,7 @@ from django.db.models import Count | ||
5 | from django.core.urlresolvers import reverse_lazy | 5 | from django.core.urlresolvers import reverse_lazy |
6 | 6 | ||
7 | from subjects.models import Tag, Subject | 7 | from subjects.models import Tag, Subject |
8 | -from topics.models import Resource | 8 | +from topics.models import Resource, Topic |
9 | from users.models import User | 9 | from users.models import User |
10 | from django.http import HttpResponse, JsonResponse | 10 | from django.http import HttpResponse, JsonResponse |
11 | from log.models import Log | 11 | from log.models import Log |
@@ -193,3 +193,33 @@ def get_days_of_the_week(date): | @@ -193,3 +193,33 @@ def get_days_of_the_week(date): | ||
193 | return days_set | 193 | return days_set |
194 | 194 | ||
195 | 195 | ||
196 | + | ||
197 | +def category_tags(request): | ||
198 | + category_id = request.GET['category_id'] | ||
199 | + data = most_tags_inside_category(category_id) | ||
200 | + data = sorted(data.values(), key = lambda x: x['count'], reverse=True ) | ||
201 | + data = data[:15] #get top 15 tags | ||
202 | + return JsonResponse(data, safe=False) | ||
203 | + | ||
204 | +def most_tags_inside_category(category_id): | ||
205 | + tags = Tag.objects.all() | ||
206 | + data = {} | ||
207 | + #grab all references to that tag | ||
208 | + for tag in tags: | ||
209 | + subjects_count = Subject.objects.filter(tags = tag, category__id = category_id).count() | ||
210 | + if subjects_count > 0: | ||
211 | + data[tag.name] = {'name': tag.name} | ||
212 | + data[tag.name]['count'] = subjects_count | ||
213 | + | ||
214 | + subjects = Subject.objects.filter(category__id = category_id) | ||
215 | + topics = Topic.objects.filter(subject__in= subjects) | ||
216 | + if topics.count() > 0 : | ||
217 | + resources_count = Resource.objects.filter(tags = tag, topic__in = topics).count() | ||
218 | + | ||
219 | + if resources_count > 0: | ||
220 | + if data.get(tag.name): | ||
221 | + data[tag.name]['count'] = data[tag.name]['count'] + resources_count | ||
222 | + else: | ||
223 | + data[tag.name] = {'name': tag.name} | ||
224 | + data[tag.name]['count'] = resources_count | ||
225 | + return data | ||
196 | \ No newline at end of file | 226 | \ No newline at end of file |