Commit b77b721e by Bridger Maxwell

Moved the certificate request and survey to their own pages, instead of being…

Moved the certificate request and survey to their own pages, instead of being modals on the Profile page.
parent 07ebc236
import settings
from django.contrib.auth.models import User
from django.db import models
......@@ -32,4 +34,46 @@ class GeneratedCertificate(models.Model):
# enabled should only be true if the student has earned a grade in the course
# The student must have a grade and request a certificate for enabled to be True
enabled = models.BooleanField(default=False)
\ No newline at end of file
enabled = models.BooleanField(default=False)
def certificate_state_for_student(student, grade):
'''
This returns a tuple of (state, download_url). The state is one of the
following:
unavailable - A student is not elligible for a certificate.
requestable - A student is elligible to request a certificate
generating - A student has requested a certificate, but it is not generated yet.
downloadable - The certificate has been requested and is available for download.
In all states except "available", download_url is None
'''
if grade:
#TODO: Remove the following after debugging
if settings.DEBUG_SURVEY:
return ("requestable", None)
try:
generated_certificate = GeneratedCertificate.objects.get(user = student)
if generated_certificate.enabled:
if generated_certificate.download_url:
return ("downloadable", generated_certificate.download_url)
else:
return ("generating", None)
else:
# If enabled=False, it may have been pre-generated but not yet requested
# Our output will be the same as if the GeneratedCertificate did not exist
pass
except GeneratedCertificate.DoesNotExist:
pass
return ("requestable", None)
else:
# No grade, no certificate. No exceptions
return ("unavailable", None)
\ No newline at end of file
import json
import logging
import settings
import uuid
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.http import Http404, HttpResponse
from mitxmako.shortcuts import render_to_response
import courseware.grades as grades
from certificates.models import GeneratedCertificate
from django.shortcuts import redirect
from django.core.validators import validate_email
from django.core.exceptions import ValidationError
import courseware.grades as grades
from certificates.models import GeneratedCertificate, certificate_state_for_student
from mitxmako.shortcuts import render_to_response
from student.models import UserProfile
from student.survey_questions import exit_survey_list_for_student
from student.views import student_took_survey, record_exit_survey
log = logging.getLogger("mitx.certificates")
@login_required
def certificate_request(request):
''' Attempt to send a certificate. '''
if request.method != "POST" or not settings.END_COURSE_ENABLED:
if not settings.END_COURSE_ENABLED:
raise Http404
if request.method == "POST":
honor_code_verify = request.POST.get('cert_request_honor_code_verify', 'false')
name_verify = request.POST.get('cert_request_name_verify', 'false')
id_verify = request.POST.get('cert_request_id_verify', 'false')
error = ''
honor_code_verify = request.POST.get('cert_request_honor_code_verify', 'false')
name_verify = request.POST.get('cert_request_name_verify', 'false')
id_verify = request.POST.get('cert_request_id_verify', 'false')
error = ''
if honor_code_verify != 'true':
error += 'Please verify that you have followed the honor code to receive a certificate. '
survey_response = record_exit_survey(request, internal_request=True)
if not survey_response['success']:
error += survey_response['error']
if honor_code_verify != 'true':
error += 'Please verify that you have followed the honor code to receive a certificate. '
if name_verify != 'true':
error += 'Please verify that your name is correct to receive a certificate. '
if name_verify != 'true':
error += 'Please verify that your name is correct to receive a certificate. '
if id_verify != 'true':
error += 'Please certify that you understand the unique ID on the certificate. '
if id_verify != 'true':
error += 'Please certify that you understand the unique ID on the certificate. '
grade = None
if len(error) == 0:
student_gradesheet = grades.grade_sheet(request.user)
grade = None
if len(error) == 0:
student_gradesheet = grades.grade_sheet(request.user)
grade = student_gradesheet['grade']
grade = student_gradesheet['grade']
if not grade:
error += 'You have not earned a grade in this course. '
if not grade:
error += 'You have not earned a grade in this course. '
if len(error) == 0:
generate_certificate(request.user, grade)
if len(error) == 0:
generate_certificate(request.user, grade)
# TODO: Send the certificate email
return HttpResponse(json.dumps({'success':True}))
# TODO: Send the certificate email
return HttpResponse(json.dumps({'success':True}))
else:
return HttpResponse(json.dumps({'success':False,
'error': error }))
else:
return HttpResponse(json.dumps({'success':False,
'error': error }))
#This is a request for the page with the form
grade_sheet = grades.grade_sheet(request.user)
certificate_state, certificate_download_url = certificate_state_for_student(request.user, grade_sheet['grade'])
if certificate_state != "requestable":
return redirect("/profile")
user_info = UserProfile.objects.get(user=request.user)
took_survey = student_took_survey(user_info)
if settings.DEBUG_SURVEY:
took_survey = False
survey_list = []
if not took_survey:
survey_list = exit_survey_list_for_student(request.user)
context = {'certificate_state' : certificate_state,
'took_survey' : took_survey,
'survey_list' : survey_list,
'name' : user_info.name }
return render_to_response('cert_request.html', context)
# This method should only be called if the user has a grade and has requested a certificate
......
......@@ -22,14 +22,11 @@ from lxml import etree
from courseware import course_settings
from module_render import render_module, modx_dispatch
from certificates.models import GeneratedCertificate
from certificates.models import GeneratedCertificate, certificate_state_for_student
from models import StudentModule
from student.models import UserProfile
from student.views import student_took_survey
if settings.END_COURSE_ENABLED:
from student.survey_questions import exit_survey_list_for_student
import courseware.content_parser as content_parser
import courseware.modules.capa_module
......@@ -94,34 +91,12 @@ def profile(request, student_id = None):
took_survey = student_took_survey(user_info)
if settings.DEBUG_SURVEY:
took_survey = False
survey_list = []
if not took_survey:
survey_list = exit_survey_list_for_student(student)
# certificate_requested determines if the student has requested a certificate
certificate_requested = False
# certificate_download_url determines if the certificate has been generated
certificate_download_url = None
if grade_sheet['grade']:
try:
generated_certificate = GeneratedCertificate.objects.get(user = student)
#If enabled=False, it may have been pre-generated but not yet requested
if generated_certificate.enabled:
certificate_requested = True
certificate_download_url = generated_certificate.download_url
except GeneratedCertificate.DoesNotExist:
#They haven't submited the request form
certificate_requested = False
if settings.DEBUG_SURVEY:
certificate_requested = False
certificate_state, certificate_download_url = certificate_state_for_student(student, grade_sheet['grade'])
context.update({'certificate_requested' : certificate_requested,
context.update({'certificate_state' : certificate_state,
'certificate_download_url' : certificate_download_url,
'took_survey' : took_survey,
'survey_list' : survey_list})
'took_survey' : took_survey})
return render_to_response('profile.html', context)
......
......@@ -22,6 +22,7 @@ from mako import exceptions
from django_future.csrf import ensure_csrf_cookie
from student.survey_questions import exit_survey_list_for_student
from models import Registration, UserProfile, PendingNameChange, PendingEmailChange
log = logging.getLogger("mitx.student")
......@@ -461,27 +462,57 @@ def accept_name_change(request):
@login_required
def record_exit_survey(request):
if request.method != "POST" or not settings.END_COURSE_ENABLED:
raise Http404
if 'survey_results' not in request.POST:
return HttpResponse(json.dumps({'success':False, 'error':'No survey responses were found.'}))
def record_exit_survey(request, internal_request = False):
if not settings.END_COURSE_ENABLED:
raise Http404
response = json.loads(request.POST['survey_results'])
if request.method == "POST":
# If internal_request = True, this is a survey that was submitted with another form
# We will record it and return the results as a dictionary, instead of a json HttpResponse
def returnResults(return_data):
if internal_request:
return return_data
else:
return HttpResponse(json.dumps(return_data))
up = UserProfile.objects.get(user=request.user)
if 'survey_results' not in request.POST:
if internal_request:
return returnResults({'success':True})
else:
return returnResults({'success':False, 'error':'There was a problem receiving your survey response.'})
meta = up.get_meta()
if '6002x_exit_response' in meta:
# Once we got a response, we don't show them the survey form again, so this is a really odd case anyway
return HttpResponse(json.dumps({'success':False, 'error':'You have already submitted a survey.'}))
response = json.loads(request.POST['survey_results'])
up = UserProfile.objects.get(user=request.user)
meta = up.get_meta()
if '6002x_exit_response' in meta:
# Once we got a response, we don't show them the survey form again, so this is a really odd case anyway
log.warning("Received an extra survey response. " + request.POST['survey_results'])
return returnResults({'success':True})
else:
meta['6002x_exit_response'] = response
up.set_meta(meta)
up.save()
return returnResults({'success':True})
else:
meta['6002x_exit_response'] = response
up.set_meta(meta)
up.save()
return HttpResponse(json.dumps({'success':True}))
user_info = UserProfile.objects.get(user=request.user)
took_survey = student_took_survey(user_info)
if settings.DEBUG_SURVEY:
took_survey = False
survey_list = []
if not took_survey:
survey_list = exit_survey_list_for_student(request.user)
context = {'took_survey' : took_survey,
'survey_list' : survey_list}
return render_to_response('exit_survey.html', context)
def student_took_survey(userprofile):
meta = userprofile.get_meta()
......
$(function() {
//This is used to scroll error divs to the user's attention. It can be removed when we switch to the branch with jQuery.ScrollTo
jQuery.fn.scrollMinimal = function(smooth) {
var cTop = this.offset().top;
var cHeight = this.outerHeight(true);
var windowTop = $(window).scrollTop();
var visibleHeight = $(window).height();
if (cTop < windowTop) {
$('body').animate({
'scrollTop': cTop
}, 'slow', 'swing');
} else if (cTop + cHeight > windowTop + visibleHeight) {
$('body').animate({
'scrollTop': cTop - visibleHeight + cHeight
}, 'slow', 'swing');
}
};
var get_survey_data = function() {
var values = {};
//First we set the value of every input to null. This assures that even
//if a checkbox isn't checked or a question isn't answered, its name is
//still in the dictionary so we know the question was on the form.
var inputs = $("#survey_fieldset :input");
inputs.each(function(index, element) {
values[element.getAttribute("name")] = null;
});
//Grab the values from the survey_fieldset
var serializedArray = inputs.serializeArray();
for (var i = 0; i < serializedArray.length; i++) {
var key = serializedArray[i]['name'];
var value = serializedArray[i]['value'];
if (key in values && values[key] != null) {
values[key].push(value);
} else {
values[key] = [value];
}
}
return JSON.stringify(values);
};
$("#cert_request_form").submit(function() {
var values = {
'cert_request_honor_code_verify': $("#cert_request_honor_code_verify").is(':checked'),
'cert_request_name_verify': $("#cert_request_name_verify").is(':checked'),
'cert_request_id_verify': $("#cert_request_id_verify").is(':checked'),
//Notice that if the survey is present, it's data is in here! That is important
'survey_results': get_survey_data(),
};
postJSON('/certificate_request', values, function(data) {
if (data.success) {
//Now we move the survey_survey_holder back to a safer container, and submit the survey
if ($("#survey_fieldset").length > 0) {
$("#survey_survey_holder").prepend($("#survey_fieldset").remove());
$("#survey_form").submit();
}
$("#cert_request").html("<h1>Certificate Request Received</h1><p>Thank you! A certificate is being generated. You will be notified when it is ready to download.</p>");
$(".cert_request_link").text("Certificate is being generated...");
} else {
$("#cert_request_error").html(data.error).scrollMinimal();
}
});
log_event("certificate_request", values);
return false;
});
$("#survey_form").submit(function() {
var values = { 'survey_results': get_survey_data() };
postJSON('/exit_survey', values, function(data) {
if (data.success) {
$("#survey").html("<h1>Survey Response Recorded</h1><p>Thank you for filling out the survey!</p>");
//Make sure that the survey fieldset is removed
$("#survey_fieldset").remove();
//Remove the links to take the survey
$(".survey_offer").hide();
} else {
$("#survey_error").html(data.error).scrollMinimal();
}
});
log_event("exit_survey_submission", values);
return false;
});
});
<%inherit file="main.html" />
<%namespace name="survey_fields" file="survey_fields.html"/>
<%block name="title"><title>Certificate Request - MITx 6.002x</title></%block>
<%block name="headextra">
<script type="text/javascript" src="${ settings.LIB_URL }certificate_survey.js"></script>
</%block>
<%include file="navigation.html" args="active_page=''" />
<div id="cert_request">
<p>You can request a certificate which verifies that you passed this course. When the certificate has been generated, you will receive an email notification that it can be downloaded from your Profile page.</p>
<p>The certificate will indicate that <strong>${name}</strong> has successfully completed the Fall 2012 offering of <strong>Circuits and Electronics 6.002x</strong>. Please ensure that this information is correct.</p>
<form id="cert_request_form">
<fieldset id="cert_request_fieldset">
<div id="cert_request_error"> </div>
<ul>
<li class="verify">
<input type="checkbox" id="cert_request_honor_code_verify"/>
<label> I certify that I have not violated the Honor Code for this course. </label>
</li>
<li class="verify">
<input type="checkbox" id="cert_request_name_verify"/>
<label> The name shown above is correct. </label>
</li>
<li class="verify">
<input type="checkbox" id="cert_request_id_verify"/>
<label> I understand that my certificate contains my name a unique ID which will be visible on the certificate. I will give this ID only to those who I wish to visit the verification page on www.edXOnline.org to confirm that I passed Circuits and Electronics 6.002x. </label>
</li>
</ul>
</fieldset>
%if not took_survey:
<fieldset id="survey_fieldset">
<h1>Thank you for taking 6.002x! You could help us a lot by filling out this (optional) survey.</h1>
<p>This survey is collected only for research purposes. It is completely optional and will not affect your grade.</p>
${survey_fields.body(survey_list)}
</fieldset>
%endif
<input type="submit" id="" value="Generate Certificate" />
</form>
</div>
<%inherit file="main.html" />
<%namespace name="survey_fields" file="survey_fields.html"/>
<%block name="title"><title>Survey - MITx 6.002x</title></%block>
<%block name="headextra">
<script type="text/javascript" src="${ settings.LIB_URL }certificate_survey.js"></script>
</%block>
<%include file="navigation.html" args="active_page=''" />
<div id="survey">
<form id="survey_form">
%if not took_survey:
<fieldset id="survey_fieldset">
<h1>Thank you for taking 6.002x! You could help us a lot by filling out this (optional) survey.</h1>
<p>This survey is collected only for research purposes. It is completely optional and will not affect your grade.</p>
${survey_fields.body(survey_list)}
</fieldset>
%else:
<h1>You have already taken the survey.</h1>
<p>Thank you for your help! You can only take the survey once, and we already have your submission.</p>
%endif
<input type="submit" id="" value="Submit Survey" />
</form>
</div>
......@@ -122,107 +122,6 @@ $(function() {
"rationale":rationale});
return false;
});
%if settings.END_COURSE_ENABLED:
//This is used to scroll error divs to the user's attention. It can be removed when we switch to the branch with jQuery.ScrollTo
jQuery.fn.scrollMinimal = function(smooth) {
var cTop = this.offset().top;
var cHeight = this.outerHeight(true);
var windowTop = $(window).scrollTop();
var visibleHeight = $(window).height();
if (cTop < windowTop) {
$('body').animate({'scrollTop': cTop}, 'slow', 'swing');
} else if (cTop + cHeight > windowTop + visibleHeight) {
$('body').animate({'scrollTop': cTop - visibleHeight + cHeight}, 'slow', 'swing');
}
};
/*
There is a fieldset, #survey_fieldset, which contains an exit survey. This exist survey can be taken by iteself,
or as part of the certificate request. If they haven't taken the survey yet, it moves to the open modal dialogue.
Once it has been taken, it should dissapear.
*/
$("a.cert_request_link").click(function(){
//If they haven't taken the survey yet, we move it to the certificate request form
if ($("#survey_fieldset").length>0) {
$("#cert_request_survey_holder").prepend( $("#survey_fieldset") );
}
});
$("a.survey_link").click(function(){
// They survey may have been "stolen" away to the certificate request form
if ($("#survey_fieldset").length>0) {
$("#survey_survey_holder").prepend( $("#survey_fieldset") );
}
});
$("#cert_request_form").submit(function(){
var values = {'cert_request_honor_code_verify' : $("#cert_request_honor_code_verify").is(':checked'),
'cert_request_name_verify' : $("#cert_request_name_verify").is(':checked'),
'cert_request_id_verify' : $("#cert_request_id_verify").is(':checked'),
};
postJSON('/certificate_request', values, function(data) {
if (data.success) {
//Now we move the survey_survey_holder back to a safer container, and submit the survey
if ($("#survey_fieldset").length>0) {
$("#survey_survey_holder").prepend( $("#survey_fieldset").remove() );
$("#survey_form").submit();
}
$("#cert_request").html("<h1>Certificate Request Received</h1><p>Thank you! A certificate is being generated. You will be notified when it is ready to download.</p>");
$(".cert_request_link").text("Certificate is being generated...");
} else {
$("#cert_request_error").html(data.error).scrollMinimal();
}
});
log_event("certificate_request", values);
return false;
});
$("#survey_form").submit(function(){
var values = {};
//First we set the value of every input to null. This assures that even
//if a checkbox isn't checked or a question isn't answered, its name is
//still in the dictionary so we know the question was on the form.
var inputs = $("#survey_fieldset :input");
inputs.each( function(index, element) {
values[ element.getAttribute("name") ] = null;
});
//Grab the values from the survey_fieldset
var serializedArray = inputs.serializeArray();
for (var i = 0; i < serializedArray.length; i++) {
var key = serializedArray[i]['name'];
var value = serializedArray[i]['value'];
if ( key in values && values[key] != null) {
values[key].push( value );
} else {
values[key] = [value];
}
}
postJSON('/record_exit_survey', {'survey_results' : JSON.stringify(values)}, function(data) {
if (true || data.success) {
$("#survey").html("<h1>Survey Response Recorded</h1><p>Thank you for filling out the survey!</p>");
//Make sure that the survey fieldset is removed
$("#survey_fieldset").remove();
//Remove the links to take the survey
$(".survey_offer").hide();
} else {
$("#survey_error").html(data.error).scrollMinimal();
}
});
log_event("exit_survey_submission", values);
return false;
});
%endif
});
</script>
</%block>
......@@ -243,20 +142,23 @@ $(function() {
%if grade_sheet['grade']:
<p>Final Grade: <strong>${grade_sheet['grade']}</strong></p>
%if not certificate_requested:
<a rel="leanModal" class="cert_request_link" href="#cert_request">Request Certificate</a>
%elif certificate_download_url:
%if certificate_state == "requestable":
<a class="cert_request_link" href="/certificate_request">Request Certificate</a>
%elif certificate_state == "downloadable":
<a href="${certificate_download_url}" target="_blank">Download Certificate</a>
%else:
%elif certificate_state == "generating":
<a href="#">Certificate is being generated...</a>
%else:
<a href="#">No certificate available</a>
%endif
%else:
<p> No letter grade has been earned for this course </p>
%endif
%if not took_survey:
<div class="survey_offer">
<a class="survey_link" rel="leanModal" href="#survey">Take the survey</a>
<a class="survey_link" href="/exit_survey">Take the survey</a>
</div>
%endif
</div>
......@@ -436,57 +338,3 @@ $(function() {
</fieldset>
</form>
</div>
%if grade_sheet['grade']:
<div id="cert_request" class="leanModal_box">
<h1>Request a 6.002x Certificate</h1>
<p>You can request a certificate which verifies that you passed this course. When the certificate has been generated, you will receive an email notification that it can be downloaded from your Profile page.</p>
<p>The certificate will indicate that <strong>${name}</strong> has successfully completed the Fall 2012 offering of <strong>Circuits and Electronics 6.002x</strong>. Please ensure that this information is correct.</p>
<form id="cert_request_form">
<fieldset id="cert_request_fieldset">
<div id="cert_request_error"> </div>
<ul>
<li class="verify">
<input type="checkbox" id="cert_request_honor_code_verify"/>
<label> I certify that I have not violated the Honor Code for this course. </label>
</li>
<li class="verify">
<input type="checkbox" id="cert_request_name_verify"/>
<label> The name shown above is correct. </label>
</li>
<li class="verify">
<input type="checkbox" id="cert_request_id_verify"/>
<label> I understand that my certificate contains my name a unique ID which will be visible on the certificate. I will give this ID only to those who I wish to visit the verification page on www.edXOnline.org to confirm that I passed Circuits and Electronics 6.002x. </label>
</li>
</ul>
</fieldset>
<div id="cert_request_survey_holder"> </div>
<input type="submit" id="" value="Generate Certificate" />
</form>
</div>
%endif
%if not took_survey:
<div id="survey" class="leanModal_box">
<form id="survey_form">
<div id="survey_survey_holder">
<fieldset id="survey_fieldset">
<h1>Thank you for taking 6.002x! You could help us a lot by filling out this (optional) survey.</h1>
<p>This survey is collected only for research purposes. It is completely optional and will not affect your grade.</p>
<div id="survey_error"> </div>
${survey_fields.body(survey_list)}
</fieldset>
</div>
<input type="submit" id="" value="Record Survey Response" />
</form>
</div>
%endif
......@@ -44,7 +44,7 @@ urlpatterns = ('',
if settings.END_COURSE_ENABLED:
urlpatterns += (
url(r'^certificate_request$', 'certificates.views.certificate_request'),
url(r'^record_exit_survey$','student.views.record_exit_survey'),
url(r'^exit_survey$','student.views.record_exit_survey'),
)
if settings.PERFSTATS:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment