def get_course_info_section_module(request, user, course, section_key): """ This returns the course info module for a given section_key. Valid keys: - handouts - guest_handouts - updates - guest_updates """ usage_key = get_course_info_usage_key(course, section_key) # Use an empty cache field_data_cache = FieldDataCache([], course.id, user) return get_module( 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 answer_entrance_exam_problem(course, request, problem, user=None, value=1, max_value=1): """ 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 value (int): raw_earned value of the problem max_value (int): raw_possible value of the problem """ if not user: user = request.user grade_dict = {'value': value, '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.scope_ids.usage_id, field_data_cache, ) module.system.publish(problem, 'grade', grade_dict)
def get_course_module(self, course): request = RequestFactory().request() field_data_cache = FieldDataCache([], course.id, self.user) return get_module(self.user, request, course.location, field_data_cache, course=course)
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 get_module_for_user(self, user, course, problem): """Helper function to get useful module at self.location in self.course_id for user""" mock_request = mock.MagicMock() mock_request.user = user field_data_cache = FieldDataCache.cache_for_descriptor_descendents( course.id, user, course, depth=2) module = module_render.get_module( # pylint: disable=protected-access user, mock_request, problem.location, field_data_cache, ) return module
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) module = get_module( user, request, problem.scope_ids.usage_id, field_data_cache, ) module.system.publish(problem, 'grade', grade_dict)
def get_course_about_section(request, course, section_key): """ This returns the snippet of html to be rendered on the course about page, given the key for the section. Valid keys: - overview - about_sidebar_html - short_description - description - key_dates (includes start, end, exams, etc) - video - course_staff_short - course_staff_extended - requirements - syllabus - textbook - faq - effort - more_info - ocw_links """ # Many of these are stored as html files instead of some semantic # markup. This can change without effecting this interface when we find a # good format for defining so many snippets of text/html. html_sections = { 'short_description', 'description', 'key_dates', 'video', 'course_staff_short', 'course_staff_extended', 'requirements', 'syllabus', 'textbook', 'faq', 'more_info', 'overview', 'effort', 'end_date', 'prerequisites', 'about_sidebar_html', 'ocw_links' } if section_key in html_sections: try: loc = course.location.replace(category='about', name=section_key) # Use an empty cache field_data_cache = FieldDataCache([], course.id, request.user) about_module = get_module( request.user, request, loc, field_data_cache, log_if_not_found=False, wrap_xmodule_display=False, static_asset_path=course.static_asset_path, course=course ) html = '' if about_module is not None: try: html = about_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=%s, section_key=%s", course, section_key ) return html except ItemNotFoundError: log.warning( u"Missing about section %s in course %s", section_key, text_type(course.location) ) return None raise KeyError("Invalid about key " + str(section_key))