Get the block tree and display it on the outline page
This commit is contained in:
@@ -45,6 +45,7 @@ from lms.djangoapps.grades.new.course_grade import CourseGradeFactory
|
||||
from lms.djangoapps.instructor.enrollment import uses_shib
|
||||
from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification
|
||||
from lms.djangoapps.ccx.custom_exception import CCXLocatorValidationException
|
||||
from lms.djangoapps.course_api.blocks.api import get_blocks
|
||||
|
||||
import shoppingcart
|
||||
import survey.utils
|
||||
@@ -1664,15 +1665,44 @@ class CourseOutlineFragmentView(FragmentView):
|
||||
Course outline fragment to be shown in the unified course view.
|
||||
"""
|
||||
|
||||
def populate_children(self, block, all_blocks):
|
||||
"""
|
||||
For a passed block, replace each id in its children array with the full representation of that child,
|
||||
which will be looked up by id in the passed all_blocks dict.
|
||||
Recursively do the same replacement for children of those children.
|
||||
"""
|
||||
children = block.get('children') or []
|
||||
|
||||
for i in range(len(children)):
|
||||
child_id = block['children'][i]
|
||||
child_detail = self.populate_children(all_blocks[child_id], all_blocks)
|
||||
block['children'][i] = child_detail
|
||||
|
||||
return block
|
||||
|
||||
def render_fragment(self, request, course_id=None):
|
||||
"""
|
||||
Renders the course outline as a fragment.
|
||||
"""
|
||||
course_key = CourseKey.from_string(course_id)
|
||||
course = get_course_with_access(request.user, 'load', course_key, check_if_enrolled=True)
|
||||
course_usage_key = modulestore().make_course_usage_key(course_key)
|
||||
all_blocks = get_blocks(
|
||||
request,
|
||||
course_usage_key,
|
||||
user=request.user,
|
||||
nav_depth=3,
|
||||
requested_fields=['children', 'display_name', 'type'],
|
||||
block_types_filter=['course', 'chapter', 'vertical', 'sequential']
|
||||
)
|
||||
|
||||
course_block_tree = all_blocks['blocks'][all_blocks['root']] # Get the root of the block tree
|
||||
|
||||
context = {
|
||||
'csrf': csrf(request)['csrf_token'],
|
||||
'course': course,
|
||||
# Recurse through the block tree, fleshing out each child object
|
||||
'blocks': self.populate_children(course_block_tree, all_blocks['blocks'])
|
||||
}
|
||||
html = render_to_string('courseware/course-outline.html', context)
|
||||
return Fragment(html)
|
||||
|
||||
@@ -4,9 +4,24 @@
|
||||
|
||||
<%!
|
||||
import json
|
||||
import pprint
|
||||
from django.utils.translation import ugettext as _
|
||||
%>
|
||||
|
||||
<section class="course-outline" id="course-outline">
|
||||
<p>Hello, world!</p>
|
||||
<ul>
|
||||
% for unit in blocks.get('children') or []:
|
||||
<li>${ unit['display_name'] }</li>
|
||||
<ul>
|
||||
% for section in unit.get('children') or []:
|
||||
<li>${ section['display_name'] }</li>
|
||||
<ul>
|
||||
% for subsection in section.get('children') or []:
|
||||
<li>${ subsection['display_name'] }</li>
|
||||
% endfor
|
||||
</ul>
|
||||
% endfor
|
||||
</ul>
|
||||
% endfor
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
Reference in New Issue
Block a user