Commit 679b19c9 by David Ormsbee

Merge pull request #240 from MITx/unenroll

Unenroll
parents b7e00ad1 660d11dc
......@@ -94,6 +94,8 @@ class CourseEnrollment(models.Model):
user = models.ForeignKey(User)
course_id = models.CharField(max_length=255, db_index=True)
created = models.DateTimeField(auto_now_add=True, null=True, db_index=True)
class Meta:
unique_together = (('user', 'course_id'), )
......
from collections import defaultdict
import json
import logging
import urllib
import itertools
......@@ -8,7 +9,7 @@ from django.core.context_processors import csrf
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.http import Http404
from django.http import Http404, HttpResponse
from django.shortcuts import redirect
from mitxmako.shortcuts import render_to_response, render_to_string
#from django.views.decorators.csrf import ensure_csrf_cookie
......@@ -271,14 +272,37 @@ def course_about(request, course_id):
@login_required
@ensure_csrf_cookie
def enroll(request, course_id):
course = check_course(course_id, course_must_be_open=False)
def change_enrollment(request):
if request.method != "POST":
raise Http404
course_id = request.POST.get("course_id", None)
if course_id == None:
return HttpResponse(json.dumps({'success': False, 'error': 'There was an error receiving the course id.'}))
action = request.POST.get("enrollment_action" , "")
user = request.user
enrollment, created = CourseEnrollment.objects.get_or_create(user=user, course_id=course.id)
if action == "enroll":
# Make sure the course exists
# We don't do this check on unenroll, or a bad course id can't be unenrolled from
course = check_course(course_id, course_must_be_open=False)
enrollment, created = CourseEnrollment.objects.get_or_create(user=user, course_id=course.id)
return HttpResponse(json.dumps({'success': True}))
elif action == "unenroll":
try:
enrollment = CourseEnrollment.objects.get(user=user, course_id=course_id)
enrollment.delete()
return HttpResponse(json.dumps({'success': True}))
except CourseEnrollment.DoesNotExist:
return HttpResponse(json.dumps({'success': False, 'error': 'You are not enrolled for this course.'}))
else:
return HttpResponse(json.dumps({'success': False, 'error': 'Invalid enrollment_action.'}))
return HttpResponse(json.dumps({'success': False, 'error': 'We weren\'t able to unenroll you. Please try again.'}))
return redirect(reverse('dashboard'))
@ensure_csrf_cookie
......@@ -295,5 +319,3 @@ def university_profile(request, org_id):
template_file = "university_profile/{0}.html".format(org_id).lower()
return render_to_response(template_file, context)
......@@ -147,7 +147,7 @@
@include clearfix;
height: 120px;
margin-right: flex-gutter();
margin-bottom: 25px;
margin-bottom: 10px;
overflow: hidden;
position: relative;
width: flex-grid(12);
......@@ -383,5 +383,19 @@
}
}
}
a.unenroll {
float: right;
font-style: italic;
color: #a0a0a0;
text-decoration: underline;
font-size: .8em;
@include inline-block;
margin-bottom: 40px;
&:hover {
color: #333;
}
}
}
}
......@@ -61,11 +61,12 @@
</div>
</div>
<div class="complete">
<p><span class="completeness">60%</span> compleat</p>
<p><span class="completeness">60%</span> complete</p>
</div>
</section>
</section>
</article>
<a href="#unenroll-modal" class="unenroll" rel="leanModal" data-course-id="${course.id}" data-course-number="${course.number}">Unenroll</a>
% endfor
% else:
......@@ -77,3 +78,52 @@
</section>
</section>
<section id="unenroll-modal" class="modal unenroll-modal">
<div class="inner-wrapper">
<header>
<h2>Are you sure you want to unenroll from <span id="unenroll_course_number"></span>?</h2>
<hr>
</header>
<form id="unenroll_form" method="post" data-remote="true" action="${reverse('change_enrollment')}">
<input name="course_id" id="unenroll_course_id" type="hidden" />
<input name="enrollment_action" type="hidden" value="unenroll" />
<div class="submit">
<input name="submit" type="submit" value="Unenroll" />
</div>
</form>
<div class="close-modal">
<div class="inner">
<p>&#10005;</p>
</div>
</div>
</div>
</section>
<script type="text/javascript">
(function() {
$(".unenroll").click(function(event) {
$("#unenroll_course_id").val( $(event.target).data("course-id") );
$("#unenroll_course_number").text( $(event.target).data("course-number") );
});
$(document).delegate('#unenroll_form', 'ajax:success', function(data, json, xhr) {
if(json.success) {
location.href="${reverse('dashboard')}";
} else {
if($('#unenroll_error').length == 0) {
$('#unenroll_form').prepend('<div id="unenroll_error" class="modal-form-error"></div>');
}
$('#unenroll_error').text(json.error).stop().css("display", "block");
}
});
})(this)
</script>
......@@ -25,7 +25,7 @@
%if registered:
<span class="register disabled">You are registered for this course (${course.number}).</span>
%else:
<a href="${reverse('enroll', args=[course.id])}" class="register">Register for ${course.number}</a>
<a href="#" class="register submit_registration">Register for ${course.number}</a>
%endif
%else:
<a href="#signup-modal" class="register" rel="leanModal" data-notice="You must Sign Up in order to register">Register for ${course.number}</a>
......@@ -107,4 +107,30 @@
</section>
</section>
%if not registered:
<div display="hidden">
<form id="enroll_form" method="post" data-remote="true" action="${reverse('change_enrollment')}">
<input name="course_id" type="hidden" value="${course.id}">
<input name="enrollment_action" type="hidden" value="enroll">
<div class="submit">
<input name="submit" type="submit" value="enroll">
</div>
</form>
</div>
<script type="text/javascript">
(function() {
$(".submit_registration").click(function() {
$("#enroll_form").submit();
});
$(document).delegate('#enroll_form', 'ajax:success', function(data, json, xhr) {
if(json.success) {
location.href="${reverse('dashboard')}";
}
});
})(this)
</script>
%endif
<%include file="../video_modal.html" />
......@@ -99,12 +99,13 @@ if settings.COURSEWARE_ENABLED:
# url(r'^edit_circuit/(?P<circuit>[^/]*)$', 'circuit.views.edit_circuit'),
# url(r'^save_circuit/(?P<circuit>[^/]*)$', 'circuit.views.save_circuit'),
url(r'^courses/?$', 'courseware.views.courses', name="courses"),
url(r'^courses/?$', 'courseware.views.courses', name="courses"),
url(r'^change_enrollment$',
'courseware.views.change_enrollment', name="change_enrollment"),
#About the course
url(r'^courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/about$',
'courseware.views.course_about', name="about_course"),
url(r'^courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/enroll$',
'courseware.views.enroll', name="enroll"),
#Inside the course
url(r'^courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/info$',
......
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