Esempio n. 1
0
def get_course_position(course_module):
    """
    Return the user's current place in the course.

    If this is the user's first time, leads to COURSE/CHAPTER/SECTION.
    If this isn't the users's first time, leads to COURSE/CHAPTER.

    If there is no current position in the course or chapter, then selects
    the first child.
    """
    urlargs = {'course_id': six.text_type(course_module.id)}
    chapter = get_current_child(course_module, min_depth=1)
    if chapter is None:
        log.debug("No chapter found when loading current position in course")
        return None

    urlargs['chapter'] = chapter.url_name
    if course_module.position is not None:
        return {
            'display_name': Text(chapter.display_name_with_default),
            'url': reverse('courseware_chapter', kwargs=urlargs),
        }

    # Relying on default of returning first child
    section = get_current_child(chapter, min_depth=1)
    if section is None:
        log.debug("No section found when loading current position in course")
        return None

    urlargs['section'] = section.url_name
    return {
        'display_name': Text(section.display_name_with_default),
        'url': reverse('courseware_section', kwargs=urlargs)
    }
Esempio n. 2
0
    def _last_visited_module_path(self, request, course):
        """
        Returns the path from the last module visited by the current user in the given course up to
        the course module. If there is no such visit, the first item deep enough down the course
        tree is used.
        """
        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
            course.id, request.user, course, depth=2)

        course_module = get_module_for_descriptor(request.user,
                                                  request,
                                                  course,
                                                  field_data_cache,
                                                  course.id,
                                                  course=course)

        path = [course_module]
        chapter = get_current_child(course_module, min_depth=2)
        if chapter is not None:
            path.append(chapter)
            section = get_current_child(chapter, min_depth=1)
            if section is not None:
                path.append(section)

        path.reverse()
        return path
Esempio n. 3
0
    def test_get_current_child(self):
        mock_xmodule = mock.MagicMock()
        assert get_current_child(mock_xmodule) is None

        mock_xmodule.position = -1
        mock_xmodule.get_display_items.return_value = ['one', 'two', 'three']
        assert get_current_child(mock_xmodule) == 'one'

        mock_xmodule.position = 2
        assert get_current_child(mock_xmodule) == 'two'
        assert get_current_child(mock_xmodule, requested_child='first') == 'one'
        assert get_current_child(mock_xmodule, requested_child='last') == 'three'

        mock_xmodule.position = 3
        mock_xmodule.get_display_items.return_value = []
        assert get_current_child(mock_xmodule) is None
Esempio n. 4
0
    def test_get_current_child(self):
        mock_xmodule = mock.MagicMock()
        self.assertIsNone(get_current_child(mock_xmodule))

        mock_xmodule.position = -1
        mock_xmodule.get_display_items.return_value = ['one', 'two', 'three']
        self.assertEqual(get_current_child(mock_xmodule), 'one')

        mock_xmodule.position = 2
        self.assertEqual(get_current_child(mock_xmodule), 'two')
        self.assertEqual(get_current_child(mock_xmodule, requested_child='first'), 'one')
        self.assertEqual(get_current_child(mock_xmodule, requested_child='last'), 'three')

        mock_xmodule.position = 3
        mock_xmodule.get_display_items.return_value = []
        self.assertIsNone(get_current_child(mock_xmodule))