Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
Merge 1ed6ab6 into a61493f
Browse files Browse the repository at this point in the history
  • Loading branch information
msinovcic committed Jul 15, 2015
2 parents a61493f + 1ed6ab6 commit 6f4dbfe
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 22 deletions.
2 changes: 0 additions & 2 deletions oneplus/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ def get_timeframe_range(value):
# last week
start = today - timedelta(days=dw, weeks=1)
end = start + timedelta(6 - dw)


elif value == "2":
# this month
start = today.replace(day=1)
Expand Down
25 changes: 15 additions & 10 deletions oneplus/learn_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,19 +323,19 @@ def post():
_learnerstate.active_question.module
)

if _total_correct >= 15:
if _total_correct == 15:
_participant.award_scenario(
"15_CORRECT",
_learnerstate.active_question.module
)

if _total_correct >= 30:
if _total_correct == 30:
_participant.award_scenario(
"30_CORRECT",
_learnerstate.active_question.module
)

if _total_correct >= 100:
if _total_correct % 100 == 0:
_participant.award_scenario(
"100_CORRECT",
_learnerstate.active_question.module
Expand All @@ -347,10 +347,14 @@ def post():

if last_3.count() == 3 \
and len([i for i in last_3 if i.correct]) == 3:
_participant.award_scenario(
"3_CORRECT_RUNNING",
_learnerstate.active_question.module
)
last_3 = ParticipantQuestionAnswer.objects.filter(
participant=_participant
).order_by("answerdate").reverse()[3:6]
if last_3.count() == 0 or (last_3.count() == 3 and len([i for i in last_3 if i.correct]) == 3):
_participant.award_scenario(
"3_CORRECT_RUNNING",
_learnerstate.active_question.module
)

last_5 = ParticipantQuestionAnswer.objects.filter(
participant=_participant
Expand Down Expand Up @@ -1264,6 +1268,7 @@ def right(request, state, user):
_golden_egg.airtime), fail_silently=False)
if _golden_egg.badge:
golden_egg["message"] = "You've won this week's Golden Egg and a badge"

ParticipantBadgeTemplateRel(participant=_participant, badgetemplate=_golden_egg.badge.badge,
scenario=_golden_egg.badge, awarddate=datetime.now()).save()
if _golden_egg.badge.point and _golden_egg.badge.point.value:
Expand Down Expand Up @@ -1704,13 +1709,13 @@ def event_end_page(request, state, user):
module
)

if "spot test" in _event.name.lower():
if _event.type == 1:
module = CourseModuleRel.objects.filter(course=_event.course).first()
_participant.award_scenario(
"SPOT_TEST",
module
)
_num_spot_tests = EventParticipantRel.objects.filter(event__name__icontains="spot test",
_num_spot_tests = EventParticipantRel.objects.filter(event__type=1,
participant=_participant).count()
if _num_spot_tests > 0 and _num_spot_tests % 5 == 0:
module = CourseModuleRel.objects.filter(course=_event.course).first()
Expand All @@ -1719,7 +1724,7 @@ def event_end_page(request, state, user):
module
)

if "exam" in _event.name.lower():
if _event.type == 2:
module = CourseModuleRel.objects.filter(course=_event.course).first()
_participant.award_scenario(
"EXAM",
Expand Down
61 changes: 55 additions & 6 deletions oneplus/prog_views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import division
from datetime import datetime, timedelta
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.db.models import Count, Sum
from auth.models import Learner
from core.models import Participant, ParticipantQuestionAnswer, Class, ParticipantBadgeTemplateRel
from gamification.models import GamificationScenario
from oneplus.views import oneplus_state_required, oneplus_login_required, COUNTRYWIDE
from oneplus.auth_views import resolve_http_method
from django.contrib.auth.decorators import user_passes_test


@oneplus_state_required
Expand Down Expand Up @@ -271,7 +272,6 @@ def post():
= list([{"area": COUNTRYWIDE}]) \
+ list(Learner.objects.values("area").distinct().all())


return render(
request,
"prog/leader.html",
Expand Down Expand Up @@ -348,11 +348,10 @@ def badges(request, state, user):

# Link achieved badges
for x in _badges:
if ParticipantBadgeTemplateRel.objects.filter(
participant=_participant,
badgetemplate=x
).exists():
rel = ParticipantBadgeTemplateRel.objects.filter(participant=_participant, badgetemplate=x)
if rel.exists():
x.achieved = True
x.count = rel.first().awardcount

def get():
return render(request, "prog/badges.html", {
Expand All @@ -375,3 +374,53 @@ def post():
)

return resolve_http_method(request, [get, post])


@user_passes_test(lambda u: u.is_staff)
def award_badge(request, learner_id, scenario_id):
try:
participant = Participant.objects.get(learner__id=learner_id, is_active=True)
except Participant.DoesNotExist:
return redirect("/admin/auth/learner")

try:
scenario = GamificationScenario.objects.get(id=scenario_id)
except GamificationScenario.DoesNotExist:
return redirect("/admin/auth/learner")

awarding_scenario = ParticipantBadgeTemplateRel.objects.filter(participant=participant,
participant__is_active=True,
scenario=scenario).first()

allowed = True
if awarding_scenario:
if awarding_scenario.scenario.award_type == 1:
allowed = False

def get():
return render(request,
"admin/auth/badge_award.html",
{
"allowed": allowed,
"participant": participant,
"scenario": scenario
})

def post():
if "award_yes" in request.POST.keys():
if awarding_scenario:
if allowed:
awarding_scenario.awardcount += 1
awarding_scenario.save()
else:
return redirect("/admin/auth/learner/%s" % participant.learner.id)
else:
ParticipantBadgeTemplateRel.objects.create(participant=participant,
badgetemplate=scenario.badge,
scenario=scenario,
awarddate=datetime.now(),
awardcount=1)

return redirect("/admin/auth/learner/%s" % participant.learner.id)

return resolve_http_method(request, [get, post])
13 changes: 13 additions & 0 deletions oneplus/static/css/oneplus.css
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,19 @@ li.down_red {
margin-right: 1em;
}

.badge-counter{
border-radius: 100%;
height: 25px;
width: 25px !important;
text-align: center;
line-height: 25px;
border: 1px solid black;
position: relative;
left: 50px;
top: 20px;
margin-top: 0px !important;
}


/*
.comment > button{
Expand Down
23 changes: 23 additions & 0 deletions oneplus/templates/admin/auth/badge_award.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% extends "admin/base.html" %}
{% block content %}
{% if allowed %}
<header id="grp-content-title">
<h1>Are you sure?</h1>
</header>
<div id="grp-content-container">
<div class="grp-group">
<h2>Are you sure you want to award {{ scenario.name }} badge to {{ participant.learner.first_name }} {{ participant.learner.last_name }} ({{ participant.learner.mobile }})?</h2>
</div>
<form name="award_badge" action="" method="post">
{% csrf_token %}
<input class="grp-button grp-default" type="submit" name="award_yes" value="Yes">
<a class="grp-button grp-cancel-link" href="/admin/auth/learner/{{ participant.learner.id }}">No</a>
</form>
</div>
{% else %}
<header id="grp-content-title">
<h1>{{ scenario.name }} badge cannot be awarded multiple times.</h1>
</header>
<a class="grp-button grp-default" href="/admin/auth/learner/{{ participant.learner.id }}">Back</a>
{% endif %}
{% endblock %}
16 changes: 16 additions & 0 deletions oneplus/templates/admin/auth/badge_fieldset.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="grp-module aligned ">
<h2>Badges</h2>
<table style="width:100%;">
{% for scenario_row in scenario_list %}
<tr>
{% for scenario in scenario_row %}
<td>
<a href="/award_badge/{{ object_id }}/{{ scenario.scenario.id }}">
<img width="10" height="10" src="/static/admin/img/icon_addlink.gif"> {{ scenario.scenario.name }} ({{ scenario.scenario_count }})
</a>
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
42 changes: 42 additions & 0 deletions oneplus/templates/admin/auth/change_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,45 @@
<a href="{% add_preserved_filters history_url %}" class="historylink">{% trans "History" %}</a>
</li>
{% endblock %}

{% block content %}<div id="content-main">
<form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.model_name }}_form">{% csrf_token %}{% block form_top %}{% endblock %}
<div>
{% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %}
{% if save_on_top %}{% block submit_buttons_top %}{% submit_row %}{% endblock %}{% endif %}
{% if errors %}
<p class="errornote">
{% if errors|length == 1 %}{% trans "Please correct the error below." %}{% else %}{% trans "Please correct the errors below." %}{% endif %}
</p>
{{ adminform.form.non_field_errors }}
{% endif %}
{{ osm_data }}
{% block field_sets %}
{% for fieldset in adminform %}
{% include "admin/includes/fieldset.html" %}
{% endfor %}
{% include "admin/auth/badge_fieldset.html" %}
{% endblock %}

{% block after_field_sets %}{% endblock %}

{% block inline_field_sets %}
{% for inline_admin_formset in inline_admin_formsets %}
{% include inline_admin_formset.opts.template %}
{% endfor %}
{% endblock %}

{% block after_related_objects %}{% endblock %}

{% block submit_buttons_bottom %}{% submit_row %}{% endblock %}

{% if adminform.first_field and add %}
<script type="text/javascript">document.getElementById("{{ adminform.first_field.id_for_label }}").focus();</script>
{% endif %}

{# JavaScript for prepopulated fields #}
{% prepopulated_fields_js %}

</div>
</form></div>
{% endblock %}
6 changes: 5 additions & 1 deletion oneplus/templates/prog/badges.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

<div class="content">
{% for badge in badges %}
<div class="badgecontainer {% if not badge.achieved %}disabled{% endif %}"><li class="badge" style="background-image: url('{{ badge.image.url }}')"></li><div class="text center-font"><p>{{ badge.name }}</p></div></div>
<div class="badgecontainer">
<div class="badge-counter {% if not badge.achieved %}disabled{% endif %}">{{ badge.count }}</div>
<li class="badge" style="background-image: url('{{ badge.image.url }}')"></li>
<div class="text center-font"><p>{{ badge.name }}</p></div>
</div>
{% endfor %}
<div class="clear"></div>
</div>
Expand Down

0 comments on commit 6f4dbfe

Please sign in to comment.