def test_entrance_exam_gating(self):
        """
        Unit Test: test_entrance_exam_gating
        """
        # This user helps to cover a discovered bug in the milestone fulfillment logic
        chaos_user = UserFactory()
        locked_toc = toc_for_course(self.request, self.course,
                                    self.entrance_exam.url_name,
                                    self.exam_1.url_name,
                                    self.field_data_cache)
        for toc_section in self.expected_locked_toc:
            self.assertIn(toc_section, locked_toc)

        # Set up the chaos user
        # pylint: disable=maybe-no-member,no-member
        grade_dict = {'value': 1, 'max_value': 1, 'user_id': chaos_user.id}
        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
            self.course.id, chaos_user, self.course, depth=2)
        # pylint: disable=protected-access
        module = get_module(
            chaos_user,
            self.request,
            self.problem_1.scope_ids.usage_id,
            field_data_cache,
        )._xmodule
        module.system.publish(self.problem_1, 'grade', grade_dict)

        # pylint: disable=maybe-no-member,no-member
        grade_dict = {
            'value': 1,
            'max_value': 1,
            'user_id': self.request.user.id
        }
        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
            self.course.id, self.request.user, self.course, depth=2)
        # pylint: disable=protected-access
        module = get_module(
            self.request.user,
            self.request,
            self.problem_1.scope_ids.usage_id,
            field_data_cache,
        )._xmodule
        module.system.publish(self.problem_1, 'grade', grade_dict)

        module = get_module(
            self.request.user,
            self.request,
            self.problem_2.scope_ids.usage_id,
            field_data_cache,
        )._xmodule  # pylint: disable=protected-access
        module.system.publish(self.problem_2, 'grade', grade_dict)
        unlocked_toc = toc_for_course(self.request, self.course,
                                      self.entrance_exam.url_name,
                                      self.exam_1.url_name,
                                      self.field_data_cache)

        for toc_section in self.expected_unlocked_toc:
            self.assertIn(toc_section, unlocked_toc)
    def test_entrance_exam_content_info(self):
        """
        test entrance exam content info method
        """
        exam_chapter, is_exam_passed = get_entrance_exam_content_info(
            self.request, self.course)
        if settings.FEATURES.get('ENTRANCE_EXAMS', False):
            self.assertEqual(exam_chapter.url_name,
                             self.entrance_exam.url_name)
            self.assertEqual(is_exam_passed, False)

            # Pass the entrance exam
            # pylint: disable=maybe-no-member,no-member
            grade_dict = {
                'value': 1,
                'max_value': 1,
                'user_id': self.request.user.id
            }
            field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
                self.course.id, self.request.user, self.course, depth=2)
            # pylint: disable=protected-access
            module = get_module(
                self.request.user,
                self.request,
                self.problem_1.scope_ids.usage_id,
                field_data_cache,
            )._xmodule
            module.system.publish(self.problem_1, 'grade', grade_dict)

            exam_chapter, is_exam_passed = get_entrance_exam_content_info(
                self.request, self.course)
            self.assertEqual(exam_chapter, None)
            self.assertEqual(is_exam_passed, True)
Example #3
0
def get_course_info_section_module(request, course, section_key):
    """
    This returns the course info module for a given section_key.

    Valid keys:
    - handouts
    - guest_handouts
    - updates
    - guest_updates
    """
    usage_key = course.id.make_usage_key('course_info', section_key)

    # Use an empty cache
    field_data_cache = FieldDataCache([], course.id, request.user)

    return get_module(
        request.user,
        request,
        usage_key,
        field_data_cache,
        log_if_not_found=False,
        wrap_xmodule_display=False,
        static_asset_path=course.static_asset_path,
        course=course
    )
Example #4
0
def answer_problem(course, request, problem, score=1, max_value=1):
    """
    Records a correct answer for the given problem.

    Arguments:
        course (Course): Course object, the course the required problem is in
        request (Request): request Object
        problem (xblock): xblock object, the problem to be answered
    """

    user = request.user
    grade_dict = {'value': score, 'max_value': max_value, 'user_id': user.id}
    field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
        course.id,
        user,
        course,
        depth=2
    )
    # pylint: disable=protected-access
    module = get_module(
        user,
        request,
        problem.scope_ids.usage_id,
        field_data_cache,
    )._xmodule
    module.system.publish(problem, 'grade', grade_dict)
Example #5
0
def get_course_info_section_module(request, course, section_key):
    """
    This returns the course info module for a given section_key.

    Valid keys:
    - handouts
    - guest_handouts
    - updates
    - guest_updates
    """
    usage_key = course.id.make_usage_key('course_info', section_key)

    # Use an empty cache
    field_data_cache = FieldDataCache([], course.id, request.user)

    return get_module(
        request.user,
        request,
        usage_key,
        field_data_cache,
        log_if_not_found=False,
        wrap_xmodule_display=False,
        static_asset_path=course.static_asset_path,
        course=course
    )
    def test_module_render_with_jump_to_id(self):
        """
        This test validates that the /jump_to_id/<id> shorthand for intracourse linking works assertIn
        expected. Note there's a HTML element in the 'toy' course with the url_name 'toyjumpto' which
        defines this linkage
        """
        mock_request = MagicMock()
        mock_request.user = self.mock_user

        course = get_course_with_access(self.mock_user, self.course_id, 'load')

        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
            self.course_id, self.mock_user, course, depth=2)

        module = render.get_module(
            self.mock_user,
            mock_request,
            Location('i4x', 'edX', 'toy', 'html', 'toyjumpto'),
            field_data_cache,
            self.course_id
        )

        # get the rendered HTML output which should have the rewritten link
        html = module.render('student_view').content

        # See if the url got rewritten to the target link
        # note if the URL mapping changes then this assertion will break
        self.assertIn('/courses/' + self.course_id + '/jump_to_id/vertical_test', html)
Example #7
0
def answer_problem(course, request, problem, score=1):
    """
    Records a correct answer for the given problem.

    Arguments:
        course (Course): Course object, the course the required problem is in
        request (Request): request Object
        problem (xblock): xblock object, the problem to be answered
    """

    user = request.user
    grade_dict = {'value': score, 'max_value': 1, 'user_id': user.id}
    field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
        course.id,
        user,
        course,
        depth=2
    )
    # pylint: disable=protected-access
    module = get_module(
        user,
        request,
        problem.scope_ids.usage_id,
        field_data_cache,
    )._xmodule
    module.system.publish(problem, 'grade', grade_dict)
Example #8
0
    def test_entrance_exam_score(self):
        """
        test entrance exam score. we will hit the method get_entrance_exam_score to verify exam score.
        """
        exam_score = get_entrance_exam_score(self.request, self.course)
        self.assertEqual(exam_score, 0)

        # Pass the entrance exam
        # pylint: disable=maybe-no-member,no-member
        grade_dict = {'value': 1, 'max_value': 2, 'user_id': self.request.user.id}
        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
            self.course.id,
            self.request.user,
            self.course,
            depth=2
        )
        # pylint: disable=protected-access
        module = get_module(
            self.request.user,
            self.request,
            self.problem_1.scope_ids.usage_id,
            field_data_cache,
        )._xmodule
        module.system.publish(self.problem_1, 'grade', grade_dict)

        exam_score = get_entrance_exam_score(self.request, self.course)
        # 50 percent exam score should be achieved.
        self.assertEqual(exam_score * 100, 50)
Example #9
0
    def test_entrance_exam_passed_message_and_course_content(self):
        """
        Unit Test: exam passing message and rest of the course section should be present
        when user achieves the entrance exam milestone/pass the exam.
        """
        url = reverse(
            'courseware_section',
            kwargs={
                'course_id': unicode(self.course.id),
                'chapter': self.entrance_exam.location.name,
                'section': self.exam_1.location.name
            }
        )

        # pylint: disable=maybe-no-member,no-member
        grade_dict = {'value': 1, 'max_value': 1, 'user_id': self.request.user.id}
        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
            self.course.id,
            self.request.user,
            self.course,
            depth=2
        )
        # pylint: disable=protected-access
        module = get_module(
            self.request.user,
            self.request,
            self.problem_1.scope_ids.usage_id,
            field_data_cache,
        )._xmodule
        module.system.publish(self.problem_1, 'grade', grade_dict)

        resp = self.client.get(url)
        self.assertNotIn('To access course materials, you must score', resp.content)
        self.assertIn('You have passed the entrance exam.', resp.content)
        self.assertIn('Lesson 1', resp.content)
Example #10
0
def _get_course_child(request, user, course_key, content_id):
    """
    Return a course xmodule/xblock to the caller
    """
    content_descriptor = None
    content_key = None
    content = None
    try:
        content_key = UsageKey.from_string(content_id)
    except InvalidKeyError:
        try:
            content_key = Location.from_deprecated_string(content_id)
        except (InvalidKeyError, InvalidLocationError):
            pass
    if content_key:
        store = modulestore()
        content_descriptor = store.get_item(content_key)
    if content_descriptor:
        field_data_cache = FieldDataCache([content_descriptor], course_key, user)
        content = module_render.get_module(
            user,
            request,
            content_key,
            field_data_cache,
            course_key)
    return content_descriptor, content_key, content
Example #11
0
    def test_entrance_exam_content_info(self):
        """
        test entrance exam content info method
        """
        exam_chapter, is_exam_passed = get_entrance_exam_content_info(self.request, self.course)
        self.assertEqual(exam_chapter.url_name, self.entrance_exam.url_name)
        self.assertEqual(is_exam_passed, False)

        # Pass the entrance exam
        # pylint: disable=maybe-no-member,no-member
        grade_dict = {'value': 1, 'max_value': 1, 'user_id': self.request.user.id}
        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
            self.course.id,
            self.request.user,
            self.course,
            depth=2
        )
        # pylint: disable=protected-access
        module = get_module(
            self.request.user,
            self.request,
            self.problem_1.scope_ids.usage_id,
            field_data_cache,
        )._xmodule
        module.system.publish(self.problem_1, 'grade', grade_dict)

        exam_chapter, is_exam_passed = get_entrance_exam_content_info(self.request, self.course)
        self.assertEqual(exam_chapter, None)
        self.assertEqual(is_exam_passed, True)
Example #12
0
    def test_module_render_with_jump_to_id(self):
        """
        This test validates that the /jump_to_id/<id> shorthand for intracourse linking works assertIn
        expected. Note there's a HTML element in the 'toy' course with the url_name 'toyjumpto' which
        defines this linkage
        """
        mock_request = MagicMock()
        mock_request.user = self.mock_user

        course = get_course_with_access(self.mock_user, self.course_id, 'load')

        model_data_cache = ModelDataCache.cache_for_descriptor_descendents(
            self.course_id, self.mock_user, course, depth=2)

        module = render.get_module(
            self.mock_user,
            mock_request,
            ['i4x', 'edX', 'toy', 'html', 'toyjumpto'],
            model_data_cache,
            self.course_id
        )

        # get the rendered HTML output which should have the rewritten link
        html = module.get_html()

        # See if the url got rewritten to the target link
        # note if the URL mapping changes then this assertion will break
        self.assertIn('/courses/'+self.course_id+'/jump_to_id/vertical_test', html)
Example #13
0
def answer_entrance_exam_problem(course, request, problem, user=None):
    """
    Takes a required milestone `problem` in a `course` and fulfills it.

    Args:
        course (Course): Course object, the course the required problem is in
        request (Request): request Object
        problem (xblock): xblock object, the problem to be fulfilled
        user (User): User object in case it is different from request.user
    """
    if not user:
        user = request.user

    grade_dict = {'value': 1, 'max_value': 1, 'user_id': user.id}
    field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
        course.id,
        user,
        course,
        depth=2
    )
    # pylint: disable=protected-access
    module = get_module(
        user,
        request,
        problem.scope_ids.usage_id,
        field_data_cache,
    )._xmodule
    module.system.publish(problem, 'grade', grade_dict)
Example #14
0
def answer_problem(course, request, problem, score=1, max_value=1):
    """
    Records an answer for the given problem.

    Arguments:
        course (Course): Course object, the course the required problem is in
        request (Request): request Object
        problem (xblock): xblock object, the problem to be answered
        score (float): The new score for the problem
        max_value (float): The new maximum score for the problem
    """

    user = request.user
    grade_dict = {'value': score, 'max_value': max_value, 'user_id': user.id}
    field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
        course.id,
        user,
        course,
        depth=2
    )
    module = get_module(
        user,
        request,
        problem.location,
        field_data_cache,
    )
    module.runtime.publish(problem, 'grade', grade_dict)
Example #15
0
def _get_course(request, user, course_id, depth=0, load_content=False):
    """
    Utility method to obtain course components
    """
    course_descriptor = None
    course_key = None
    course_content = None
    try:
        course_key = CourseKey.from_string(course_id)
    except InvalidKeyError:
        try:
            course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)
        except InvalidKeyError:
            pass
    if course_key:
        try:
            course_descriptor = get_course(course_key, depth=depth)
        except ValueError:
            pass
    if course_descriptor and load_content:
        field_data_cache = FieldDataCache([course_descriptor], course_key, user)
        course_content = module_render.get_module(
            user, request, course_descriptor.location, field_data_cache, course_key
        )
    return course_descriptor, course_key, course_content
    def test_module_render_with_jump_to_id(self):
        """
        This test validates that the /jump_to_id/<id> shorthand for intracourse linking works assertIn
        expected. Note there's a HTML element in the 'toy' course with the url_name 'toyjumpto' which
        defines this linkage
        """
        mock_request = MagicMock()
        mock_request.user = self.mock_user

        course = get_course_with_access(self.mock_user, "load", self.course_key)

        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
            self.course_key, self.mock_user, course, depth=2
        )

        module = render.get_module(
            self.mock_user, mock_request, self.course_key.make_usage_key("html", "toyjumpto"), field_data_cache
        )

        # get the rendered HTML output which should have the rewritten link
        html = module.render(STUDENT_VIEW).content

        # See if the url got rewritten to the target link
        # note if the URL mapping changes then this assertion will break
        self.assertIn("/courses/" + self.course_key.to_deprecated_string() + "/jump_to_id/vertical_test", html)
    def test_entrance_exam_passed_message_and_course_content(self):
        """
        Unit Test: exam passing message and rest of the course section should be present
        when user achieves the entrance exam milestone/pass the exam.
        """
        url = reverse('courseware_section',
                      kwargs={
                          'course_id': unicode(self.course.id),
                          'chapter': self.entrance_exam.location.name,
                          'section': self.exam_1.location.name
                      })

        # pylint: disable=maybe-no-member,no-member
        grade_dict = {
            'value': 1,
            'max_value': 1,
            'user_id': self.request.user.id
        }
        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
            self.course.id, self.request.user, self.course, depth=2)
        # pylint: disable=protected-access
        module = get_module(
            self.request.user,
            self.request,
            self.problem_1.scope_ids.usage_id,
            field_data_cache,
        )._xmodule
        module.system.publish(self.problem_1, 'grade', grade_dict)

        resp = self.client.get(url)
        if settings.FEATURES.get('ENTRANCE_EXAMS', False):
            self.assertNotIn('To access course materials, you must score',
                             resp.content)
            self.assertIn('You have passed the entrance exam.', resp.content)
            self.assertIn('Lesson 1', resp.content)
    def test_entrance_exam_score(self):
        """
        test entrance exam score. we will hit the method get_entrance_exam_score to verify exam score.
        """
        exam_score = get_entrance_exam_score(self.request, self.course)
        self.assertEqual(exam_score, 0)

        # Pass the entrance exam
        # pylint: disable=maybe-no-member,no-member
        grade_dict = {
            'value': 1,
            'max_value': 2,
            'user_id': self.request.user.id
        }
        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
            self.course.id, self.request.user, self.course, depth=2)
        # pylint: disable=protected-access
        module = get_module(
            self.request.user,
            self.request,
            self.problem_1.scope_ids.usage_id,
            field_data_cache,
        )._xmodule
        module.system.publish(self.problem_1, 'grade', grade_dict)

        exam_score = get_entrance_exam_score(self.request, self.course)
        # 50 percent exam score should be achieved.
        self.assertEqual(exam_score * 100, 50)
Example #19
0
def answer_problem(course, request, problem, score=1, max_value=1):
    """
    Records an answer for the given problem.

    Arguments:
        course (Course): Course object, the course the required problem is in
        request (Request): request Object
        problem (xblock): xblock object, the problem to be answered
        score (float): The new score for the problem
        max_value (float): The new maximum score for the problem
    """

    user = request.user
    grade_dict = {'value': score, 'max_value': max_value, 'user_id': user.id}
    field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
        course.id,
        user,
        course,
        depth=2
    )
    module = get_module(
        user,
        request,
        problem.location,
        field_data_cache,
    )
    module.runtime.publish(problem, 'grade', grade_dict)
    def test_module_render_with_jump_to_id(self):
        """
        This test validates that the /jump_to_id/<id> shorthand for intracourse linking works assertIn
        expected. Note there's a HTML element in the 'toy' course with the url_name 'toyjumpto' which
        defines this linkage
        """
        mock_request = MagicMock()
        mock_request.user = self.mock_user

        course = get_course_with_access(self.mock_user, self.course_id, "load")

        model_data_cache = ModelDataCache.cache_for_descriptor_descendents(
            self.course_id, self.mock_user, course, depth=2
        )

        module = render.get_module(
            self.mock_user, mock_request, ["i4x", "edX", "toy", "html", "toyjumpto"], model_data_cache, self.course_id
        )

        # get the rendered HTML output which should have the rewritten link
        html = module.get_html()

        # See if the url got rewritten to the target link
        # note if the URL mapping changes then this assertion will break
        self.assertIn("/courses/" + self.course_id + "/jump_to_id/vertical_test", html)
 def test_rerandomization_set(self):
     descriptor = ItemFactory.create(
         category='problem',
         data=self.problem_xml,
         display_name='Option Response Problem2',
         rerandomize='always',
     )
     location = descriptor.location
     field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
         self.course.id,
         self.user,
         descriptor
     )
     module = render.get_module(
         self.user,
         self.request,
         location,
         field_data_cache,
     )
     result_fragment = module.render(STUDENT_VIEW)
     self.assertIn('Staff Analytics Info', result_fragment.content)
     self.assertIn(
         'The analytics cannot be displayed for this question as it uses randomization.',
         result_fragment.content
     )
    def test_xmodule_display_wrapper_disabled(self):
        module = render.get_module(
            self.user, self.request, self.location, self.model_data_cache, self.course.id, wrap_xmodule_display=False
        )
        result_fragment = module.runtime.render(module, None, "student_view")

        self.assertNotIn('section class="xmodule_display xmodule_HtmlModule"', result_fragment.content)
Example #23
0
def get_course_info_section(request, course, section_key):
    """
    This returns the snippet of html to be rendered on the course info page,
    given the key for the section.

    Valid keys:
    - handouts
    - guest_handouts
    - updates
    - guest_updates
    """
    usage_key = course.id.make_usage_key('course_info', section_key)

    # Use an empty cache
    field_data_cache = FieldDataCache([], course.id, request.user)
    info_module = get_module(request.user,
                             request,
                             usage_key,
                             field_data_cache,
                             log_if_not_found=False,
                             wrap_xmodule_display=False,
                             static_asset_path=course.static_asset_path)

    html = ''

    if info_module is not None:
        try:
            html = info_module.render(STUDENT_VIEW).content
        except Exception:  # pylint: disable=broad-except
            html = render_to_string('courseware/error-message.html', None)
            log.exception(
                u"Error rendering course={course}, section_key={section_key}".
                format(course=course, section_key=section_key))

    return html
    def _get_module(self, course_id, descriptor, location):
        """
        Get the module from the course from which to pattern match (or not) the 'View in Studio' buttons
        """
        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(course_id, self.staff_user, descriptor)

        return render.get_module(self.staff_user, self.request, location, field_data_cache)
    def test_module_render_with_jump_to_id(self):
        """
        This test validates that the /jump_to_id/<id> shorthand for intracourse linking works assertIn
        expected. Note there's a HTML element in the 'toy' course with the url_name 'toyjumpto' which
        defines this linkage
        """
        mock_request = MagicMock()
        mock_request.user = self.mock_user

        course = get_course_with_access(self.mock_user, 'load',
                                        self.course_key)

        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
            self.course_key, self.mock_user, course, depth=2)

        module = render.get_module(
            self.mock_user, mock_request,
            self.course_key.make_usage_key('html', 'toyjumpto'),
            field_data_cache, self.course_key)

        # get the rendered HTML output which should have the rewritten link
        html = module.render('student_view').content

        # See if the url got rewritten to the target link
        # note if the URL mapping changes then this assertion will break
        self.assertIn(
            '/courses/' + self.course_key.to_deprecated_string() +
            '/jump_to_id/vertical_test', html)
Example #26
0
def get_module_for_student(student, course, location):
    """Return the module for the (student, location) using a DummyRequest."""
    request = DummyRequest()
    request.user = student

    descriptor = modulestore().get_instance(course.id, location, depth=0)
    field_data_cache = FieldDataCache([descriptor], course.id, student)
    return get_module(student, request, location, field_data_cache, course.id)
    def test_course_link_rewrite(self):
        module = render.get_module(self.user, self.request, self.location, self.field_data_cache)
        result_fragment = module.render(STUDENT_VIEW)

        self.assertIn(
            "/courses/{course_id}/bar/content".format(course_id=self.course.id.to_deprecated_string()),
            result_fragment.content,
        )
 def test_inline_analytics_disabled(self):
     module = render.get_module(
         self.user,
         self.request,
         self.location,
         self.field_data_cache,
     )
     result_fragment = module.render(STUDENT_VIEW)
     self.assertNotIn('Staff Analytics Info', result_fragment.content)
 def test_histogram_disabled(self):
     module = render.get_module(
         self.user,
         self.request,
         self.location,
         self.field_data_cache,
     )
     result_fragment = module.render(STUDENT_VIEW)
     self.assertNotIn('histrogram', result_fragment.content)
 def test_staff_debug_info_enabled(self):
     module = render.get_module(
         self.user,
         self.request,
         self.location,
         self.field_data_cache,
     )
     result_fragment = module.render(STUDENT_VIEW)
     self.assertIn('Staff Debug', result_fragment.content)
Example #31
0
def get_module_for_student(student, usage_key, request=None, course=None):
    """Return the module for the (student, location) using a DummyRequest."""
    if request is None:
        request = DummyRequest()
        request.user = student

    descriptor = modulestore().get_item(usage_key, depth=0)
    field_data_cache = FieldDataCache([descriptor], usage_key.course_key, student)
    return get_module(student, request, usage_key, field_data_cache, course=course)
    def test_xmodule_display_wrapper_disabled(self):
        module = render.get_module(
            self.user, self.request, self.location, self.field_data_cache, wrap_xmodule_display=False
        )
        result_fragment = module.render(STUDENT_VIEW)

        self.assertNotIn(
            'div class="xblock xblock-student_view xmodule_display xmodule_HtmlModule"', result_fragment.content
        )
 def test_inline_analytics_staff_access(self):
     module = render.get_module(
         self.staff,
         self.request,
         self.location,
         self.field_data_cache_staff,
     )
     result_fragment = module.render(STUDENT_VIEW)
     self.assertIn('Staff Analytics Info', result_fragment.content)
Example #34
0
 def test_staff_debug_info_enabled(self):
     module = render.get_module(
         self.user,
         self.request,
         self.location,
         self.field_data_cache,
     )
     result_fragment = module.render(STUDENT_VIEW)
     self.assertIn('Staff Debug', result_fragment.content)
    def get_module_for_user(self, user):
        """Helper function to get useful module at self.location in self.course_id for user"""
        mock_request = MagicMock()
        mock_request.user = user
        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(self.course.id, user, self.course, depth=2)

        return render.get_module(  # pylint: disable=protected-access
            user, mock_request, self.lti.location, field_data_cache
        )._xmodule
Example #36
0
 def test_histogram_disabled(self):
     module = render.get_module(
         self.user,
         self.request,
         self.location,
         self.field_data_cache,
     )
     result_fragment = module.render(STUDENT_VIEW)
     self.assertNotIn('histrogram', result_fragment.content)
    def test_histogram_enabled_for_unscored_xmodules(self):
        """Histograms should not display for xmodules which are not scored."""

        html_descriptor = ItemFactory.create(category="html", data="Here are some course details.")
        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(self.course.id, self.user, self.descriptor)
        with patch("xmodule_modifiers.grade_histogram") as mock_grade_histogram:
            mock_grade_histogram.return_value = []
            module = render.get_module(self.user, self.request, html_descriptor.location, field_data_cache)
            module.render(STUDENT_VIEW)
            self.assertFalse(mock_grade_histogram.called)
    def test_static_badlink_rewrite(self):
        module = render.get_module(self.user, self.request, self.location, self.model_data_cache, self.course.id)
        result_fragment = module.runtime.render(module, None, "student_view")

        self.assertIn(
            "/c4x/{org}/{course}/asset/_file.jpg".format(
                org=self.course.location.org, course=self.course.location.course
            ),
            result_fragment.content,
        )
 def test_histogram_disabled(self):
     module = render.get_module(
         self.user,
         self.request,
         self.location,
         self.field_data_cache,
         self.course.id,
     )
     result_fragment = module.render('student_view')
     self.assertNotIn('histrogram', result_fragment.content)
    def test_static_badlink_rewrite(self):
        module = render.get_module(self.user, self.request, self.location, self.field_data_cache)
        result_fragment = module.render(STUDENT_VIEW)

        self.assertIn(
            "/c4x/{org}/{course}/asset/_file.jpg".format(
                org=self.course.location.org, course=self.course.location.course
            ),
            result_fragment.content,
        )
Example #41
0
def get_module_for_student(student, usage_key, request=None):
    """Return the module for the (student, location) using a DummyRequest."""
    if request is None:
        request = DummyRequest()
        request.user = student

    descriptor = modulestore().get_item(usage_key, depth=0)
    field_data_cache = FieldDataCache([descriptor], usage_key.course_key,
                                      student)
    return get_module(student, request, usage_key, field_data_cache)
 def test_staff_debug_info_enabled(self):
     module = render.get_module(
         self.user,
         self.request,
         self.location,
         self.field_data_cache,
         self.course.id,
     )
     result_fragment = module.render('student_view')
     self.assertIn('Staff Debug', result_fragment.content)
 def test_staff_debug_info_enabled(self):
     module = render.get_module(
         self.user,
         self.request,
         self.location,
         self.field_data_cache,
         self.course.id,
     )
     result_fragment = module.render('student_view')
     self.assertIn('Staff Debug', result_fragment.content)
 def test_histogram_disabled(self):
     module = render.get_module(
         self.user,
         self.request,
         self.location,
         self.field_data_cache,
         self.course.id,
     )
     result_fragment = module.render('student_view')
     self.assertNotIn('histrogram', result_fragment.content)
    def get_module_for_user(self, user):
        """Helper function to get useful module at self.location in self.course_id for user"""
        mock_request = MagicMock()
        mock_request.user = user
        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
            self.course.id, user, self.course, depth=2)

        return render.get_module(  # pylint: disable=protected-access
            user, mock_request, self.problem.location, field_data_cache,
            self.course.id)._xmodule
Example #46
0
    def test_xmodule_display_wrapper_disabled(self):
        module = render.get_module(
            self.user,
            self.request,
            self.location,
            self.field_data_cache,
            wrap_xmodule_display=False,
        )
        result_fragment = module.render(STUDENT_VIEW)

        self.assertNotIn('div class="xblock xblock-student_view xmodule_display xmodule_HtmlModule"', result_fragment.content)
 def test_static_asset_path_use(self):
     """
     when a course is loaded with do_import_static=False (see xml_importer.py), then
     static_asset_path is set as an lms kv in course.  That should make static paths
     not be mangled (ie not changed to c4x://).
     """
     module = render.get_module(
         self.user, self.request, self.location, self.field_data_cache, static_asset_path="toy_course_dir"
     )
     result_fragment = module.render(STUDENT_VIEW)
     self.assertIn('href="/static/toy_course_dir', result_fragment.content)
Example #48
0
    def test_histogram(self):
        module = render.get_module(
            self.user,
            self.request,
            self.location,
            self.field_data_cache,
            self.course.id,
        )
        result_fragment = module.runtime.render(module, None, 'student_view')

        self.assertIn('Staff Debug', result_fragment.content)
Example #49
0
    def test_xmodule_display_wrapper_disabled(self):
        module = render.get_module(
            self.user,
            self.request,
            self.location,
            self.model_data_cache,
            self.course.id,
            wrap_xmodule_display=False,
        )
        result_fragment = module.runtime.render(module, None, 'student_view')

        self.assertNotIn('section class="xmodule_display xmodule_HtmlModule"', result_fragment.content)
Example #50
0
    def test_xmodule_display_wrapper_enabled(self):
        module = render.get_module(
            self.user,
            self.request,
            self.location,
            self.field_data_cache,
            self.course.id,
            wrap_xmodule_display=True,
        )
        result_fragment = module.render('student_view')

        self.assertIn('div class="xblock xblock-student_view xmodule_display xmodule_HtmlModule"', result_fragment.content)
Example #51
0
    def _get_module(self, course_id, descriptor, location):
        """
        Get the module from the course from which to pattern match (or not) the 'View in Studio' buttons
        """
        field_data_cache = FieldDataCache.cache_for_descriptor_descendents(
            course_id, self.staff_user, descriptor)

        return render.get_module(
            self.staff_user,
            self.request,
            location,
            field_data_cache,
        )
Example #52
0
    def test_course_link_rewrite(self):
        module = render.get_module(
            self.user,
            self.request,
            self.location,
            self.field_data_cache,
        )
        result_fragment = module.render(STUDENT_VIEW)

        self.assertIn(
            '/courses/{course_id}/bar/content'.format(
                course_id=self.course.id.to_deprecated_string()),
            result_fragment.content)
Example #53
0
    def test_course_link_rewrite(self):
        module = render.get_module(
            self.user,
            self.request,
            self.location,
            self.field_data_cache,
            self.course.id,
        )
        result_fragment = module.runtime.render(module, None, 'student_view')

        self.assertIn(
            '/courses/{course_id}/bar/content'.format(
                course_id=self.course.id), result_fragment.content)
 def test_unsupported_response_type(self):
     module = render.get_module(
         self.user,
         self.request,
         self.location,
         self.field_data_cache,
     )
     result_fragment = module.render(STUDENT_VIEW)
     self.assertIn('Staff Analytics Info', result_fragment.content)
     self.assertIn(
         'The analytics cannot be displayed for this type of question.',
         result_fragment.content
     )
Example #55
0
def add_hint(request, course_id, field, course=None):
    """
    Add a new hint.  `request.POST`:
    op
    field
    problem - The problem id
    answer - The answer to which a hint will be added
    hint - The text of the hint
    """

    problem_id = request.POST['problem']
    problem_key = course_id.make_usage_key_from_deprecated_string(problem_id)
    answer = request.POST['answer']
    hint_text = request.POST['hint']

    # Validate the answer.  This requires initializing the xmodules, which
    # is annoying.
    try:
        descriptor = modulestore().get_item(problem_key)
        descriptors = [descriptor]
    except ItemNotFoundError:
        descriptors = []
    field_data_cache = model_data.FieldDataCache(descriptors, course_id,
                                                 request.user)
    hinter_module = module_render.get_module(request.user,
                                             request,
                                             problem_key,
                                             field_data_cache,
                                             course_id,
                                             course=course)
    if not hinter_module.validate_answer(answer):
        # Invalid answer.  Don't add it to the database, or else the
        # hinter will crash when we encounter it.
        return 'Error - the answer you specified is not properly formatted: ' + str(
            answer)

    this_problem = XModuleUserStateSummaryField.objects.get(
        field_name=field, usage_id=problem_key)

    hint_pk_entry = XModuleUserStateSummaryField.objects.get(
        field_name='hint_pk', usage_id=problem_key)
    this_pk = int(hint_pk_entry.value)
    hint_pk_entry.value = this_pk + 1
    hint_pk_entry.save()

    problem_dict = json.loads(this_problem.value)
    if answer not in problem_dict:
        problem_dict[answer] = {}
    problem_dict[answer][this_pk] = [hint_text, 1]
    this_problem.value = json.dumps(problem_dict)
    this_problem.save()