Skip to content

Commit

Permalink
Add buy button to program details page
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name authored and Kyrylo Omelchenko committed Dec 5, 2019
1 parent 8c7d942 commit 6d5df44
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 10 deletions.
8 changes: 7 additions & 1 deletion lms/djangoapps/courseware/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
from openedx.core.djangoapps.models.course_details import CourseDetails
from openedx.core.djangoapps.monitoring_utils import set_custom_metrics_for_course_key
from openedx.core.djangoapps.plugin_api.views import EdxFragmentView
from openedx.core.djangoapps.programs.utils import ProgramMarketingDataExtender
from openedx.core.djangoapps.programs.utils import ProgramMarketingDataExtender, ProgramProgressMeter
from openedx.core.djangoapps.self_paced.models import SelfPacedConfiguration
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
from openedx.core.djangoapps.util.user_messages import PageLevelMessages
Expand All @@ -103,6 +103,7 @@
from xmodule.modulestore.exceptions import ItemNotFoundError, NoPathToItem
from xmodule.tabs import CourseTabList
from xmodule.x_module import STUDENT_VIEW
from learner_dashboard.programs import courses_count

from ..entrance_exams import user_can_skip_entrance_exam
from ..module_render import get_module, get_module_by_usage_id, get_module_for_descriptor
Expand Down Expand Up @@ -898,6 +899,11 @@ def program_marketing(request, program_uuid):
if not program_data:
raise Http404

meter = ProgramProgressMeter(request.site, request.user, uuid=program_uuid)
course_data = meter.progress(programs=[program_data], count_only=False)[0]
total_courses_count = courses_count(program_data['courses'])
not_started_courses_count = courses_count(course_data['not_started'])

program = ProgramMarketingDataExtender(program_data, request.user).extend()
program['type_slug'] = slugify(program['type'])
skus = program.get('skus')
Expand Down
18 changes: 9 additions & 9 deletions lms/djangoapps/learner_dashboard/programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
from openedx.core.djangoapps.user_api.preferences.api import get_user_preferences


def courses_count(courses):
courses_count = 0
for course in courses:
courses_count += len(set(c['key'] for c in course['course_runs']))
return courses_count


class ProgramsFragmentView(EdxFragmentView):
"""
A fragment to program listing.
Expand Down Expand Up @@ -81,13 +88,6 @@ class ProgramDetailsFragmentView(EdxFragmentView):
"""
Render the program details fragment.
"""
@staticmethod
def _courses_count(courses):
courses_count = 0
for course in courses:
courses_count += len(set(c['key'] for c in course['course_runs']))
return courses_count

def render_to_fragment(self, request, program_uuid, **kwargs):
"""View details about a specific program."""
programs_config = kwargs.get('programs_config') or ProgramsApiConfig.current()
Expand All @@ -109,9 +109,9 @@ def render_to_fragment(self, request, program_uuid, **kwargs):
course_data = meter.progress(programs=[program_data], count_only=False)[0]
certificate_data = get_certificates(request.user, program_data)

total_courses_count = self._courses_count(program_data.pop('courses'))
total_courses_count = courses_count(program_data.pop('courses'))

not_started_courses_count = self._courses_count(course_data['not_started'])
not_started_courses_count = courses_count(course_data['not_started'])

if not_started_courses_count < total_courses_count:
program_data['price'] = '%.2f' % (
Expand Down
1 change: 1 addition & 0 deletions lms/djangoapps/shoppingcart/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from ipware.ip import get_ip
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.locations import SlashSeparatedCourseKey
from opaque_keys.edx.locator import CourseLocator

from course_modes.models import CourseMode
Expand Down
86 changes: 86 additions & 0 deletions lms/templates/courseware/program_marketing.html
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,90 @@ <h3 class="question">
% endfor
</div>
% endif

<div class="container courses-in-program" id="courses">
<h3 class="hd">
${Text(_('Courses in the {program_type}')).format(
program_type=program_type
)}
</h3>
% if (program['full_program_price'] and program['full_program_price'] != '0.00'):
<button class="btn buy-program" data-url="${add_to_cart_url}">
${Text(_('Buy all courses for ${full_program_price}')).format(full_program_price=full_program_price)}
</button>
% endif
<div class="course-cards">
% for course in courses:
% if course.get('course_runs'):
<div class="course-card">
<div class="course-content">
<div class="col-full-sm">
<h4 class="hd title">
<a href="/courses/${course['course_runs'][0]['key']}/about"> ${course['title']}</a>
<small>
<span>
<i class="fa fa-clock-o" aria-hidden="true"></i>
${Text(_('Starts {course_start_datetime}')).format(
course_start_datetime=datetime.strptime(course['course_runs'][0]['start'], '%Y-%m-%dT%H:%M:%SZ').strftime('%B %-d, %Y')
)}
</span>

% if course['course_runs'][0]['pacing_type'] == "instructor_paced":
<span>${_('Instructor - Paced')}</span>
% else:
<span>${_('Self - Paced')}</span>
% endif
</small>
</h4>
<p class="copy-meta">
% if course['course_runs'][0]['short_description']:
${course['course_runs'][0]['short_description']}
% endif
</p>
<p class="copy-meta-mobile">
% if course['course_runs'][0]['short_description']:
${course['course_runs'][0]['short_description'][:description_max_length]}
% if len(course['course_runs'][0]['short_description']) > description_max_length:
...
% endif
% endif
</p>
</div>
</div>
</div>
% endif
% endfor
</div>
<span class="pagingInfo"></span>
</div>

<section id="video-modal" class="modal modal-custom custom-video-modal">
<div class="inner-wrapper">
<iframe title="${_('YouTube Video')}" width="640" height="360"
src="${video_url}"
frameborder="0" allowfullscreen></iframe>
</div>
</section>
</div>
<div id="shoppingcart-popup">
<p id='msg'></p>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('button.buy-program').click(function(e) {
var addToCartUrl = $(e.target).data('url');
$.ajax({
url: addToCartUrl,
method: 'POST',
success: function(data) {
if (data.success) {
window.location = data.redirect_url;
} else {
$("#shoppingcart-popup #msg").html(data.msg);
$("#shoppingcart-popup").dialog({width: 400});
}
}
});
});
});
</script>

0 comments on commit 6d5df44

Please sign in to comment.