Skip to content
This repository has been archived by the owner on Oct 4, 2022. It is now read-only.

Commit

Permalink
Adding code to allow experimenter to add participants to an already c…
Browse files Browse the repository at this point in the history
…ompleted experiment session. Fixes #85
  • Loading branch information
tgpatel committed Apr 1, 2015
1 parent 222b9a5 commit 389a4bb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,24 @@
</div>
</form>
</div>
<div class="alert alert-warning">
<span id="add-participant-error" class="alert-danger"></span>
<form class="form-inline" id="addParticipant">
{% csrf_token %}
<div class="form-group">
<label for="participantEmail">Add Participant with Email: </label>
<input type="text" id="participantEmail" name="participantEmail" class="form-control input-sm" />
<button type="submit" class="btn btn-default input-sm">Add Participant</button>
</div>
</form>
</div>
{% for dict in formset.errors %}
{% for error in dict.values %}
<div class="alert alert-error">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}

<h3>Registered Participants</h3>
<form method="POST" id="participant-info" class="form-inline">
{% csrf_token %}
Expand Down Expand Up @@ -101,6 +111,25 @@ <h3>Registered Participants</h3>
}
});

$("#addParticipant").submit(function(event) {
$.post("{% url 'subjectpool:add_participant' session_detail.pk %}", $(event.target).serialize(), function(data) {
console.log(data.success);
if(data.success) {
// reload page to show the added participant
location.reload();
} else {
$("#add-participant-error").text("ERROR: " + data.error);
}
}).fail(function(data) {
if(data.status == 404) {
$("#add-participant-error").text("ERROR: Participant not found in the system. Please contact the administrator");
} else {
$("#add-participant-error").text("ERROR: Something went wrong. Please try again, If issue persists please contact the administrator");
}
});
event.preventDefault();
});

/* API method to get paging information */
$.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings ) {
return {
Expand Down
3 changes: 2 additions & 1 deletion vcweb/core/subjectpool/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
manage_participant_attendance, send_invitations, get_invitations_count,
invite_email_preview, experiment_session_signup,
submit_experiment_session_signup, cancel_experiment_session_signup,
download_experiment_session)
download_experiment_session, add_participant)

urlpatterns = [
url(r'^$', experimenter_index, name='experimenter_index'),
url(r'^session/manage/(?P<pk>\-?\d+)$', manage_experiment_session, name='manage_experiment_session'),
url(r'^session/events$', get_session_events, name='session_events'),
url(r'^session/detail/event/(?P<pk>\d+)$', manage_participant_attendance, name='session_event_detail'),
url(r'^session/(?P<pk>\d+)/participant/add/$', add_participant, name='add_participant'),
url(r'^session/invite$', send_invitations, name='send_invites'),
url(r'^session/invite/count$', get_invitations_count, name='get_invitations_count'),
url(r'^session/email-preview$', invite_email_preview, name='invite_email_preview'),
Expand Down
35 changes: 35 additions & 0 deletions vcweb/core/subjectpool/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,41 @@ def manage_participant_attendance(request, pk=None):
{'session_detail': es, 'formset': formset})


@group_required(PermissionGroup.experimenter)
@ownership_required(ExperimentSession)
@require_POST
def add_participant(request, pk=None):
user_email = request.POST.get('participantEmail')
participant = get_object_or_404(Participant.objects.select_related('user'), user__email=user_email)
invitations = Invitation.objects.filter(participant=participant, experiment_session__pk=pk)
logger.debug("Found %s invitations for participant %s while adding him to experiment session %s", len(invitations), participant, pk)

es = get_object_or_404(ExperimentSession, pk=pk)

# First check the experiment session should be over
if es.scheduled_end_date < datetime.now():
logger.debug("Experimeter %s tried adding participant %s to experiment session %s that isn't still over",
request.user, participant, pk)
return JsonResponse({'success': False, 'error': "Can't add a participant to yet not finished experiment session"})

# second check the participant must have recevied invitations
if len(invitations) == 0:
logger.debug("Experimeter %s tried adding participant %s to experiment session %s, who hasn't recevied invitation for the same",
request.user, participant, pk)
return JsonResponse({'success': False, 'error': "Can't add a participant who hasn't received invitation"})

signups = ParticipantSignup.objects.filter(invitation__in=invitations).count()

# third and final check, the participant must not have already signedup for the experiment session
if signups > 0:
logger.debug("Experimeter %s tried adding participant %s to experiment session %s, who is already signed up for the same",
request.user, participant, pk)
return JsonResponse({'success': False, 'error': 'Participant is already signed up of the experiment session'})
else:
ParticipantSignup(invitation=invitations[0], attendance=ParticipantSignup.ATTENDANCE.participated).save()
return JsonResponse({'success': True})


@group_required(PermissionGroup.participant)
@require_POST
def cancel_experiment_session_signup(request):
Expand Down

0 comments on commit 389a4bb

Please sign in to comment.