From 4be657c1badfa8ec3781da54aa8236e5e17c4847 Mon Sep 17 00:00:00 2001 From: Brian Jacobel Date: Mon, 6 Feb 2017 16:27:33 -0500 Subject: [PATCH] Get the block tree and display it on the outline page --- lms/djangoapps/courseware/views/views.py | 30 ++++++++++++++++++++ lms/templates/courseware/course-outline.html | 17 ++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lms/djangoapps/courseware/views/views.py b/lms/djangoapps/courseware/views/views.py index 57a6e88251..88c365f46a 100644 --- a/lms/djangoapps/courseware/views/views.py +++ b/lms/djangoapps/courseware/views/views.py @@ -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) diff --git a/lms/templates/courseware/course-outline.html b/lms/templates/courseware/course-outline.html index 09ff7acfbf..66559d7052 100644 --- a/lms/templates/courseware/course-outline.html +++ b/lms/templates/courseware/course-outline.html @@ -4,9 +4,24 @@ <%! import json +import pprint from django.utils.translation import ugettext as _ %>
-

Hello, world!

+