class LTIDescriptor(LTIFields, MetadataOnlyEditingDescriptor, EmptyDataRawDescriptor): """ Descriptor for LTI Xmodule. """ module_class = LTIModule grade_handler = module_attr('grade_handler') preview_handler = module_attr('preview_handler')
class SplitTestDescriptor(SplitTestFields, SequenceDescriptor): # the editing interface can be the same as for sequences -- just a container module_class = SplitTestModule filename_extension = "xml" child_descriptor = module_attr('child_descriptor') log_child_render = module_attr('log_child_render') get_content_titles = module_attr('get_content_titles') def definition_to_xml(self, resource_fs): xml_object = etree.Element('split_test') # TODO: also save the experiment id and the condition map for child in self.get_children(): xml_object.append( etree.fromstring(child.export_to_xml(resource_fs))) return xml_object def has_dynamic_children(self): """ Grading needs to know that only one of the children is actually "real". This makes it use module.get_child_descriptors(). """ return True
class SplitTestDescriptor(SplitTestFields, SequenceDescriptor): # the editing interface can be the same as for sequences -- just a container module_class = SplitTestModule filename_extension = "xml" child_descriptor = module_attr('child_descriptor') log_child_render = module_attr('log_child_render') get_content_titles = module_attr('get_content_titles') def definition_to_xml(self, resource_fs): xml_object = etree.Element('split_test') renderable_groups = {} # json.dumps doesn't know how to handle Location objects for group in self.group_id_to_child: renderable_groups[group] = self.group_id_to_child[ group].to_deprecated_string() xml_object.set('group_id_to_child', json.dumps(renderable_groups)) xml_object.set('user_partition_id', str(self.user_partition_id)) for child in self.get_children(): self.runtime.add_block_as_child_node(child, xml_object) return xml_object @classmethod def definition_from_xml(cls, xml_object, system): children = [] raw_group_id_to_child = xml_object.attrib.get('group_id_to_child', None) user_partition_id = xml_object.attrib.get('user_partition_id', None) try: group_id_to_child = json.loads(raw_group_id_to_child) except ValueError: msg = "group_id_to_child is not valid json" log.exception(msg) system.error_tracker(msg) for child in xml_object: try: descriptor = system.process_xml(etree.tostring(child)) children.append(descriptor.scope_ids.usage_id) except Exception: msg = "Unable to load child when parsing split_test module." log.exception(msg) system.error_tracker(msg) return ({ 'group_id_to_child': group_id_to_child, 'user_partition_id': user_partition_id }, children) def has_dynamic_children(self): """ Grading needs to know that only one of the children is actually "real". This makes it use module.get_child_descriptors(). """ return True
class LTIDescriptor(LTIFields, MetadataOnlyEditingDescriptor, EmptyDataRawDescriptor): """ Descriptor for LTI Xmodule. """ module_class = LTIModule grade_handler = module_attr('grade_handler') preview_handler = module_attr('preview_handler') lti_2_0_result_rest_handler = module_attr('lti_2_0_result_rest_handler') clear_user_module_score = module_attr('clear_user_module_score') get_outcome_service_url = module_attr('get_outcome_service_url')
class LTIDescriptor(LTIFields, MetadataOnlyEditingDescriptor, EmptyDataRawDescriptor): """ Descriptor for LTI Xmodule. """ def max_score(self): return self.weight if self.has_score else None module_class = LTIModule resources_dir = None grade_handler = module_attr('grade_handler') preview_handler = module_attr('preview_handler') lti_2_0_result_rest_handler = module_attr('lti_2_0_result_rest_handler') clear_user_module_score = module_attr('clear_user_module_score') get_outcome_service_url = module_attr('get_outcome_service_url')
class StudioEditableDescriptor: """ Helper mixin for supporting Studio editing of xmodules. This class is only intended to be used with an XModule Descriptor. This class assumes that the associated XModule will have an "author_view" method for returning an editable preview view of the module. """ author_view = module_attr(AUTHOR_VIEW) has_author_view = True
class VideoDescriptor(VideoFields, VideoStudioViewHandlers, TabsEditingDescriptor, EmptyDataRawDescriptor): """ Descriptor for `VideoModule`. """ module_class = VideoModule transcript = module_attr('transcript') tabs = [{ 'name': _("Basic"), 'template': "video/transcripts.html", 'current': True }, { 'name': _("Advanced"), 'template': "tabs/metadata-edit-tab.html" }] def __init__(self, *args, **kwargs): """ Mostly handles backward compatibility issues. `source` is deprecated field. a) If `source` exists and `source` is not `html5_sources`: show `source` field on front-end as not-editable but clearable. Dropdown is a new field `download_video` and it has value True. b) If `source` is cleared it is not shown anymore. c) If `source` exists and `source` in `html5_sources`, do not show `source` field. `download_video` field has value True. """ super(VideoDescriptor, self).__init__(*args, **kwargs) # For backwards compatibility -- if we've got XML data, parse it out and set the metadata fields if self.data: field_data = self._parse_video_xml(self.data) self._field_data.set_many(self, field_data) del self.data editable_fields = super(VideoDescriptor, self).editable_metadata_fields self.source_visible = False if self.source: # If `source` field value exist in the `html5_sources` field values, # then delete `source` field value and use value from `html5_sources` field. if self.source in self.html5_sources: self.source = '' # Delete source field value. self.download_video = True else: # Otherwise, `source` field value will be used. self.source_visible = True download_video = editable_fields['download_video'] if not download_video['explicitly_set']: self.download_video = True # for backward compatibility. # If course was existed and was not re-imported by the moment of adding `download_track` field, # we should enable `download_track` if following is true: download_track = editable_fields['download_track'] if not download_track['explicitly_set'] and self.track: self.download_track = True def editor_saved(self, user, old_metadata, old_content): """ Used to update video subtitles. """ manage_video_subtitles_save(self, user, old_metadata if old_metadata else None, generate_translation=True) def save_with_metadata(self, user): """ Save module with updated metadata to database." """ self.save() self.runtime.modulestore.update_item(self, user.id if user else None) @property def editable_metadata_fields(self): editable_fields = super(VideoDescriptor, self).editable_metadata_fields if self.source_visible: editable_fields['source']['non_editable'] = True else: editable_fields.pop('source') languages = [{ 'label': label, 'code': lang } for lang, label in settings.ALL_LANGUAGES if lang != u'en'] languages.sort(key=lambda l: l['label']) editable_fields['transcripts']['languages'] = languages editable_fields['transcripts']['type'] = 'VideoTranslations' editable_fields['transcripts']['urlRoot'] = self.runtime.handler_url( self, 'studio_transcript', 'translation').rstrip('/?') editable_fields['handout']['type'] = 'FileUploader' return editable_fields @classmethod def from_xml(cls, xml_data, system, id_generator): """ Creates an instance of this descriptor from the supplied xml_data. This may be overridden by subclasses xml_data: A string of xml that will be translated into data and children for this module system: A DescriptorSystem for interacting with external resources org and course are optional strings that will be used in the generated modules url identifiers """ xml_object = etree.fromstring(xml_data) url_name = xml_object.get('url_name', xml_object.get('slug')) block_type = 'video' definition_id = id_generator.create_definition(block_type, url_name) usage_id = id_generator.create_usage(definition_id) if is_pointer_tag(xml_object): filepath = cls._format_filepath(xml_object.tag, name_to_pathname(url_name)) xml_data = etree.tostring( cls.load_file(filepath, system.resources_fs, usage_id)) field_data = cls._parse_video_xml(xml_data) kvs = InheritanceKeyValueStore(initial_values=field_data) field_data = KvsFieldData(kvs) video = system.construct_xblock_from_class( cls, # We're loading a descriptor, so student_id is meaningless # We also don't have separate notions of definition and usage ids yet, # so we use the location for both ScopeIds(None, block_type, definition_id, usage_id), field_data, ) return video def definition_to_xml(self, resource_fs): """ Returns an xml string representing this module. """ xml = etree.Element('video') youtube_string = create_youtube_string(self) # Mild workaround to ensure that tests pass -- if a field # is set to its default value, we don't need to write it out. if youtube_string and youtube_string != '1.00:OEoXaMPEzfM': xml.set('youtube', unicode(youtube_string)) xml.set('url_name', self.url_name) attrs = { 'display_name': self.display_name, 'show_captions': json.dumps(self.show_captions), 'start_time': self.start_time, 'end_time': self.end_time, 'sub': self.sub, 'download_track': json.dumps(self.download_track), 'download_video': json.dumps(self.download_video), } for key, value in attrs.items(): # Mild workaround to ensure that tests pass -- if a field # is set to its default value, we don't write it out. if value: if key in self.fields and self.fields[key].is_set_on(self): xml.set(key, unicode(value)) for source in self.html5_sources: ele = etree.Element('source') ele.set('src', source) xml.append(ele) if self.track: ele = etree.Element('track') ele.set('src', self.track) xml.append(ele) if self.handout: ele = etree.Element('handout') ele.set('src', self.handout) xml.append(ele) # sorting for easy testing of resulting xml for transcript_language in sorted(self.transcripts.keys()): ele = etree.Element('transcript') ele.set('language', transcript_language) ele.set('src', self.transcripts[transcript_language]) xml.append(ele) return xml def get_context(self): """ Extend context by data for transcript basic tab. """ _context = super(VideoDescriptor, self).get_context() metadata_fields = copy.deepcopy(self.editable_metadata_fields) display_name = metadata_fields['display_name'] video_url = metadata_fields['html5_sources'] youtube_id_1_0 = metadata_fields['youtube_id_1_0'] def get_youtube_link(video_id): if video_id: return 'http://youtu.be/{0}'.format(video_id) else: return '' _ = self.runtime.service(self, "i18n").ugettext video_url.update({ 'help': _('The URL for your video. This can be a YouTube URL or a link to an .mp4, .ogg, or .webm video file hosted elsewhere on the Internet.' ), 'display_name': _('Default Video URL'), 'field_name': 'video_url', 'type': 'VideoList', 'default_value': [get_youtube_link(youtube_id_1_0['default_value'])] }) youtube_id_1_0_value = get_youtube_link(youtube_id_1_0['value']) if youtube_id_1_0_value: video_url['value'].insert(0, youtube_id_1_0_value) metadata = {'display_name': display_name, 'video_url': video_url} _context.update({'transcripts_basic_tab_metadata': metadata}) return _context @classmethod def _parse_youtube(cls, data): """ Parses a string of Youtube IDs such as "1.0:AXdE34_U,1.5:VO3SxfeD" into a dictionary. Necessary for backwards compatibility with XML-based courses. """ ret = {'0.75': '', '1.00': '', '1.25': '', '1.50': ''} videos = data.split(',') for video in videos: pieces = video.split(':') try: speed = '%.2f' % float(pieces[0]) # normalize speed # Handle the fact that youtube IDs got double-quoted for a period of time. # Note: we pass in "VideoFields.youtube_id_1_0" so we deserialize as a String-- # it doesn't matter what the actual speed is for the purposes of deserializing. youtube_id = deserialize_field(cls.youtube_id_1_0, pieces[1]) ret[speed] = youtube_id except (ValueError, IndexError): log.warning('Invalid YouTube ID: %s', video) return ret @classmethod def _parse_video_xml(cls, xml_data): """ Parse video fields out of xml_data. The fields are set if they are present in the XML. """ xml = etree.fromstring(xml_data) field_data = {} # Convert between key types for certain attributes -- # necessary for backwards compatibility. conversions = { # example: 'start_time': cls._example_convert_start_time } # Convert between key names for certain attributes -- # necessary for backwards compatibility. compat_keys = {'from': 'start_time', 'to': 'end_time'} sources = xml.findall('source') if sources: field_data['html5_sources'] = [ele.get('src') for ele in sources] track = xml.find('track') if track is not None: field_data['track'] = track.get('src') handout = xml.find('handout') if handout is not None: field_data['handout'] = handout.get('src') transcripts = xml.findall('transcript') if transcripts: field_data['transcripts'] = { tr.get('language'): tr.get('src') for tr in transcripts } for attr, value in xml.items(): if attr in compat_keys: attr = compat_keys[attr] if attr in cls.metadata_to_strip + ('url_name', 'name'): continue if attr == 'youtube': speeds = cls._parse_youtube(value) for speed, youtube_id in speeds.items(): # should have made these youtube_id_1_00 for # cleanliness, but hindsight doesn't need glasses normalized_speed = speed[:-1] if speed.endswith( '0') else speed # If the user has specified html5 sources, make sure we don't use the default video if youtube_id != '' or 'html5_sources' in field_data: field_data['youtube_id_{0}'.format( normalized_speed.replace('.', '_'))] = youtube_id else: # Convert XML attrs into Python values. if attr in conversions: value = conversions[attr](value) else: # We export values with json.dumps (well, except for Strings, but # for about a month we did it for Strings also). value = deserialize_field(cls.fields[attr], value) field_data[attr] = value # For backwards compatibility: Add `source` if XML doesn't have `download_video` # attribute. if 'download_video' not in field_data and sources: field_data['source'] = field_data['html5_sources'][0] # For backwards compatibility: if XML doesn't have `download_track` attribute, # it means that it is an old format. So, if `track` has some value, # `download_track` needs to have value `True`. if 'download_track' not in field_data and track is not None: field_data['download_track'] = True return field_data
class PeerGradingDescriptor(PeerGradingFields, RawDescriptor): """ Module for adding peer grading questions """ mako_template = "widgets/raw-edit.html" module_class = PeerGradingModule filename_extension = "xml" has_score = True always_recalculate_grades = True #Specify whether or not to pass in open ended interface needs_open_ended_interface = True metadata_translations = { 'is_graded': 'graded', 'attempts': 'max_attempts', 'due_data': 'due' } @property def non_editable_metadata_fields(self): non_editable_fields = super(PeerGradingDescriptor, self).non_editable_metadata_fields non_editable_fields.extend( [PeerGradingFields.due, PeerGradingFields.graceperiod]) return non_editable_fields def get_required_module_descriptors(self): """ Returns a list of XModuleDescriptor instances upon which this module depends, but are not children of this module. """ # If use_for_single_location is True, this is linked to an open ended problem. if self.use_for_single_location: # Try to load the linked module. # If we can't load it, return empty list to avoid exceptions on progress page. try: linked_module = self.system.load_item(self.link_to_location) return [linked_module] except (NoPathToItem, ItemNotFoundError): error_message = ("Cannot find the combined open ended module " "at location {0} being linked to from peer " "grading module {1}").format( self.link_to_location, self.location) log.error(error_message) return [] else: return [] # Proxy to PeerGradingModule so that external callers don't have to know if they're working # with a module or a descriptor closed = module_attr('closed') get_instance_state = module_attr('get_instance_state') get_next_submission = module_attr('get_next_submission') graded = module_attr('graded') is_student_calibrated = module_attr('is_student_calibrated') peer_grading = module_attr('peer_grading') peer_grading_closed = module_attr('peer_grading_closed') peer_grading_problem = module_attr('peer_grading_problem') peer_gs = module_attr('peer_gs') query_data_for_location = module_attr('query_data_for_location') save_calibration_essay = module_attr('save_calibration_essay') save_grade = module_attr('save_grade') show_calibration_essay = module_attr('show_calibration_essay') use_for_single_location_local = module_attr( 'use_for_single_location_local') _find_corresponding_module_for_location = module_attr( '_find_corresponding_module_for_location')
class SplitTestDescriptor(SplitTestFields, SequenceDescriptor, StudioEditableDescriptor): # the editing interface can be the same as for sequences -- just a container module_class = SplitTestModule filename_extension = "xml" mako_template = "widgets/metadata-only-edit.html" child_descriptor = module_attr('child_descriptor') log_child_render = module_attr('log_child_render') get_content_titles = module_attr('get_content_titles') def definition_to_xml(self, resource_fs): xml_object = etree.Element('split_test') renderable_groups = {} # json.dumps doesn't know how to handle Location objects for group in self.group_id_to_child: renderable_groups[group] = self.group_id_to_child[group].to_deprecated_string() xml_object.set('group_id_to_child', json.dumps(renderable_groups)) xml_object.set('user_partition_id', str(self.user_partition_id)) for child in self.get_children(): self.runtime.add_block_as_child_node(child, xml_object) return xml_object @classmethod def definition_from_xml(cls, xml_object, system): children = [] raw_group_id_to_child = xml_object.attrib.get('group_id_to_child', None) user_partition_id = xml_object.attrib.get('user_partition_id', None) try: group_id_to_child = json.loads(raw_group_id_to_child) except ValueError: msg = "group_id_to_child is not valid json" log.exception(msg) system.error_tracker(msg) for child in xml_object: try: descriptor = system.process_xml(etree.tostring(child)) children.append(descriptor.scope_ids.usage_id) except Exception: msg = "Unable to load child when parsing split_test module." log.exception(msg) system.error_tracker(msg) return ({ 'group_id_to_child': group_id_to_child, 'user_partition_id': user_partition_id }, children) def get_context(self): _context = super(SplitTestDescriptor, self).get_context() _context.update({ 'selected_partition': self.get_selected_partition() }) return _context def has_dynamic_children(self): """ Grading needs to know that only one of the children is actually "real". This makes it use module.get_child_descriptors(). """ return True def editor_saved(self, user, old_metadata, old_content): """ Used to create default verticals for the groups. Assumes that a mutable modulestore is being used. """ # Any existing value of user_partition_id will be in "old_content" instead of "old_metadata" # because it is Scope.content. if 'user_partition_id' not in old_content or old_content['user_partition_id'] != self.user_partition_id: selected_partition = self.get_selected_partition() if selected_partition is not None: self.group_id_mapping = {} # pylint: disable=attribute-defined-outside-init for group in selected_partition.groups: self._create_vertical_for_group(group, user.id) # Don't need to call update_item in the modulestore because the caller of this method will do it. else: # If children referenced in group_id_to_child have been deleted, remove them from the map. for str_group_id, usage_key in self.group_id_to_child.items(): if usage_key not in self.children: # pylint: disable=no-member del self.group_id_to_child[str_group_id] @property def editable_metadata_fields(self): # Update the list of partitions based on the currently available user_partitions. SplitTestFields.build_partition_values(self.user_partitions, self.get_selected_partition()) editable_fields = super(SplitTestDescriptor, self).editable_metadata_fields # Explicitly add user_partition_id, which does not automatically get picked up because it is Scope.content. # Note that this means it will be saved by the Studio editor as "metadata", but the field will # still update correctly. editable_fields[SplitTestFields.user_partition_id.name] = self._create_metadata_editor_info( SplitTestFields.user_partition_id ) return editable_fields @property def non_editable_metadata_fields(self): non_editable_fields = super(SplitTestDescriptor, self).non_editable_metadata_fields non_editable_fields.extend([ SplitTestDescriptor.due, SplitTestDescriptor.user_partitions ]) return non_editable_fields def get_selected_partition(self): """ Returns the partition that this split module is currently using, or None if the currently selected partition ID does not match any of the defined partitions. """ for user_partition in self.user_partitions: if user_partition.id == self.user_partition_id: return user_partition return None def active_and_inactive_children(self): """ Returns two values: 1. The active children of this split test, in the order of the groups. 2. The remaining (inactive) children, in the order they were added to the split test. """ children = self.get_children() user_partition = self.get_selected_partition() if not user_partition: return [], children def get_child_descriptor(location): """ Returns the child descriptor which matches the specified location, or None if one is not found. """ for child in children: if child.location == location: return child return None # Compute the active children in the order specified by the user partition active_children = [] for group in user_partition.groups: group_id = unicode(group.id) child_location = self.group_id_to_child.get(group_id, None) child = get_child_descriptor(child_location) if child: active_children.append(child) # Compute the inactive children in the order they were added to the split test inactive_children = [child for child in children if child not in active_children] return active_children, inactive_children def validation_messages(self): """ Returns a list of validation messages describing the current state of the block. Each message includes a message type indicating whether the message represents information, a warning or an error. """ _ = self.runtime.service(self, "i18n").ugettext # pylint: disable=redefined-outer-name messages = [] if self.user_partition_id < 0: messages.append(ValidationMessage( self, _(u"The experiment is not associated with a group configuration."), ValidationMessageType.warning, 'edit-button', _(u"Select a Group Configuration") )) else: user_partition = self.get_selected_partition() if not user_partition: messages.append(ValidationMessage( self, _(u"The experiment uses a deleted group configuration. Select a valid group configuration or delete this experiment."), ValidationMessageType.error )) else: [active_children, inactive_children] = self.active_and_inactive_children() if len(active_children) < len(user_partition.groups): messages.append(ValidationMessage( self, _(u"The experiment does not contain all of the groups in the configuration."), ValidationMessageType.error, 'add-missing-groups-button', _(u"Add Missing Groups") )) if len(inactive_children) > 0: messages.append(ValidationMessage( self, _(u"The experiment has an inactive group. Move content into active groups, then delete the inactive group."), ValidationMessageType.warning )) return messages @XBlock.handler def add_missing_groups(self, request, suffix=''): # pylint: disable=unused-argument """ Create verticals for any missing groups in the split test instance. Called from Studio view. """ user_partition = self.get_selected_partition() changed = False for group in user_partition.groups: str_group_id = unicode(group.id) if str_group_id not in self.group_id_to_child: user_id = self.runtime.service(self, 'user').user_id self._create_vertical_for_group(group, user_id) changed = True if changed: # TODO user.id - to be fixed by Publishing team self.system.modulestore.update_item(self, None) return Response() @property def group_configuration_url(self): assert hasattr(self.system, 'modulestore') and hasattr(self.system.modulestore, 'get_course'), \ "modulestore has to be available" course_module = self.system.modulestore.get_course(self.location.course_key) group_configuration_url = None if 'split_test' in course_module.advanced_modules: user_partition = self.get_selected_partition() if user_partition: group_configuration_url = "{url}#{configuration_id}".format( url='/group_configurations/' + unicode(self.location.course_key), configuration_id=str(user_partition.id) ) return group_configuration_url def _create_vertical_for_group(self, group, user_id): """ Creates a vertical to associate with the group. This appends the new vertical to the end of children, and updates group_id_to_child. A mutable modulestore is needed to call this method (will need to update after mixed modulestore work, currently relies on mongo's create_item method). """ assert hasattr(self.system, 'modulestore') and hasattr(self.system.modulestore, 'create_item'), \ "editor_saved should only be called when a mutable modulestore is available" modulestore = self.system.modulestore dest_usage_key = self.location.replace(category="vertical", name=uuid4().hex) metadata = {'display_name': DEFAULT_GROUP_NAME.format(group_id=group.id)} modulestore.create_item( user_id, self.location.course_key, dest_usage_key.block_type, block_id=dest_usage_key.block_id, definition_data=None, metadata=metadata, runtime=self.system, ) self.children.append(dest_usage_key) # pylint: disable=no-member self.group_id_to_child[unicode(group.id)] = dest_usage_key @property def general_validation_message(self): """ Message for either error or warning validation message/s. Returns message and type. Priority given to error type message. """ validation_messages = self.validation_messages() if validation_messages: has_error = any(message.message_type == ValidationMessageType.error for message in validation_messages) return { 'message': _(u"This content experiment has issues that affect content visibility."), 'type': ValidationMessageType.error if has_error else ValidationMessageType.warning, } return None
class CapaDescriptor(CapaFields, RawDescriptor): """ Module implementing problems in the LON-CAPA format, as implemented by capa.capa_problem """ INDEX_CONTENT_TYPE = 'CAPA' module_class = CapaModule resources_dir = None has_score = True show_in_read_only_mode = True template_dir_name = 'problem' mako_template = "widgets/problem-edit.html" js = {'coffee': [resource_string(__name__, 'js/src/problem/edit.coffee')]} js_module_name = "MarkdownEditingDescriptor" has_author_view = True css = { 'scss': [ resource_string(__name__, 'css/editor/edit.scss'), resource_string(__name__, 'css/problem/edit.scss') ] } # The capa format specifies that what we call max_attempts in the code # is the attribute `attempts`. This will do that conversion metadata_translations = dict(RawDescriptor.metadata_translations) metadata_translations['attempts'] = 'max_attempts' @classmethod def filter_templates(cls, template, course): """ Filter template that contains 'latex' from templates. Show them only if use_latex_compiler is set to True in course settings. """ return 'latex' not in template['template_id'] or course.use_latex_compiler def get_context(self): _context = RawDescriptor.get_context(self) _context.update({ 'markdown': self.markdown, 'enable_markdown': self.markdown is not None, 'enable_latex_compiler': self.use_latex_compiler, }) return _context # VS[compat] # TODO (cpennington): Delete this method once all fall 2012 course are being # edited in the cms @classmethod def backcompat_paths(cls, path): dog_stats_api.increment( DEPRECATION_VSCOMPAT_EVENT, tags=["location:capa_descriptor_backcompat_paths"] ) return [ 'problems/' + path[8:], path[8:], ] @property def non_editable_metadata_fields(self): non_editable_fields = super(CapaDescriptor, self).non_editable_metadata_fields non_editable_fields.extend([ CapaDescriptor.due, CapaDescriptor.graceperiod, CapaDescriptor.force_save_button, CapaDescriptor.markdown, CapaDescriptor.text_customization, CapaDescriptor.use_latex_compiler, ]) return non_editable_fields @property def problem_types(self): """ Low-level problem type introspection for content libraries filtering by problem type """ tree = etree.XML(self.data) registered_tags = responsetypes.registry.registered_tags() return set([node.tag for node in tree.iter() if node.tag in registered_tags]) def index_dictionary(self): """ Return dictionary prepared with module content and type for indexing. """ xblock_body = super(CapaDescriptor, self).index_dictionary() # Removing solutions and hints, as well as script and style capa_content = re.sub( re.compile( r""" <solution>.*?</solution> | <script>.*?</script> | <style>.*?</style> | <[a-z]*hint.*?>.*?</[a-z]*hint> """, re.DOTALL | re.VERBOSE), "", self.data ) capa_content = escape_html_characters(capa_content) capa_body = { "capa_content": capa_content, "display_name": self.display_name, } if "content" in xblock_body: xblock_body["content"].update(capa_body) else: xblock_body["content"] = capa_body xblock_body["content_type"] = self.INDEX_CONTENT_TYPE xblock_body["problem_types"] = list(self.problem_types) return xblock_body def has_support(self, view, functionality): """ Override the XBlock.has_support method to return appropriate value for the multi-device functionality. Returns whether the given view has support for the given functionality. """ if functionality == "multi_device": return all( responsetypes.registry.get_class_for_tag(tag).multi_device_support for tag in self.problem_types ) return False # Proxy to CapaModule for access to any of its attributes answer_available = module_attr('answer_available') check_button_name = module_attr('check_button_name') check_button_checking_name = module_attr('check_button_checking_name') check_problem = module_attr('check_problem') choose_new_seed = module_attr('choose_new_seed') closed = module_attr('closed') get_answer = module_attr('get_answer') get_problem = module_attr('get_problem') get_problem_html = module_attr('get_problem_html') get_state_for_lcp = module_attr('get_state_for_lcp') handle_input_ajax = module_attr('handle_input_ajax') hint_button = module_attr('hint_button') handle_problem_html_error = module_attr('handle_problem_html_error') handle_ungraded_response = module_attr('handle_ungraded_response') is_attempted = module_attr('is_attempted') is_correct = module_attr('is_correct') is_past_due = module_attr('is_past_due') is_submitted = module_attr('is_submitted') lcp = module_attr('lcp') make_dict_of_responses = module_attr('make_dict_of_responses') new_lcp = module_attr('new_lcp') publish_grade = module_attr('publish_grade') rescore_problem = module_attr('rescore_problem') reset_problem = module_attr('reset_problem') save_problem = module_attr('save_problem') set_state_from_lcp = module_attr('set_state_from_lcp') should_show_check_button = module_attr('should_show_check_button') should_show_reset_button = module_attr('should_show_reset_button') should_show_save_button = module_attr('should_show_save_button') update_score = module_attr('update_score')
class CapaDescriptor(CapaFields, RawDescriptor): """ Module implementing problems in the LON-CAPA format, as implemented by capa.capa_problem """ INDEX_CONTENT_TYPE = 'CAPA' module_class = CapaModule has_score = True show_in_read_only_mode = True template_dir_name = 'problem' mako_template = "widgets/problem-edit.html" js = {'coffee': [resource_string(__name__, 'js/src/problem/edit.coffee')]} js_module_name = "MarkdownEditingDescriptor" css = { 'scss': [ resource_string(__name__, 'css/editor/edit.scss'), resource_string(__name__, 'css/problem/edit.scss') ] } # The capa format specifies that what we call max_attempts in the code # is the attribute `attempts`. This will do that conversion metadata_translations = dict(RawDescriptor.metadata_translations) metadata_translations['attempts'] = 'max_attempts' @classmethod def filter_templates(cls, template, course): """ Filter template that contains 'latex' from templates. Show them only if use_latex_compiler is set to True in course settings. """ return 'latex' not in template['template_id'] or course.use_latex_compiler def get_context(self): _context = RawDescriptor.get_context(self) _context.update({ 'markdown': self.markdown, 'enable_markdown': self.markdown is not None, 'enable_latex_compiler': self.use_latex_compiler, }) return _context # VS[compat] # TODO (cpennington): Delete this method once all fall 2012 course are being # edited in the cms @classmethod def backcompat_paths(cls, path): dog_stats_api.increment( DEPRECATION_VSCOMPAT_EVENT, tags=["location:capa_descriptor_backcompat_paths"] ) return [ 'problems/' + path[8:], path[8:], ] @property def non_editable_metadata_fields(self): non_editable_fields = super(CapaDescriptor, self).non_editable_metadata_fields non_editable_fields.extend([ CapaDescriptor.due, CapaDescriptor.graceperiod, CapaDescriptor.force_save_button, CapaDescriptor.markdown, CapaDescriptor.text_customization, CapaDescriptor.use_latex_compiler, ]) return non_editable_fields @property def problem_types(self): """ Low-level problem type introspection for content libraries filtering by problem type """ tree = etree.XML(self.data) # pylint: disable=no-member registered_tags = responsetypes.registry.registered_tags() return set([node.tag for node in tree.iter() if node.tag in registered_tags]) @property def has_responsive_ui(self): """ Returns whether this module has support for responsive UI. """ return self.lcp.has_responsive_ui def index_dictionary(self): """ Return dictionary prepared with module content and type for indexing. """ result = super(CapaDescriptor, self).index_dictionary() if not result: result = {} index = { 'content_type': self.INDEX_CONTENT_TYPE, 'problem_types': list(self.problem_types), "display_name": self.display_name } result.update(index) return result # Proxy to CapaModule for access to any of its attributes answer_available = module_attr('answer_available') check_button_name = module_attr('check_button_name') check_button_checking_name = module_attr('check_button_checking_name') check_problem = module_attr('check_problem') choose_new_seed = module_attr('choose_new_seed') closed = module_attr('closed') get_answer = module_attr('get_answer') get_problem = module_attr('get_problem') get_problem_html = module_attr('get_problem_html') get_state_for_lcp = module_attr('get_state_for_lcp') handle_input_ajax = module_attr('handle_input_ajax') hint_button = module_attr('hint_button') handle_problem_html_error = module_attr('handle_problem_html_error') handle_ungraded_response = module_attr('handle_ungraded_response') is_attempted = module_attr('is_attempted') is_correct = module_attr('is_correct') is_past_due = module_attr('is_past_due') is_submitted = module_attr('is_submitted') lcp = module_attr('lcp') make_dict_of_responses = module_attr('make_dict_of_responses') new_lcp = module_attr('new_lcp') publish_grade = module_attr('publish_grade') rescore_problem = module_attr('rescore_problem') reset_problem = module_attr('reset_problem') save_problem = module_attr('save_problem') set_state_from_lcp = module_attr('set_state_from_lcp') should_show_check_button = module_attr('should_show_check_button') should_show_reset_button = module_attr('should_show_reset_button') should_show_save_button = module_attr('should_show_save_button') update_score = module_attr('update_score')
class VideoDescriptor(VideoFields, VideoTranscriptsMixin, VideoStudioViewHandlers, TabsEditingDescriptor, EmptyDataRawDescriptor): """ Descriptor for `VideoModule`. """ module_class = VideoModule transcript = module_attr('transcript') tabs = [{ 'name': _("Basic"), 'template': "video/transcripts.html", 'current': True }, { 'name': _("Advanced"), 'template': "tabs/metadata-edit-tab.html" }] def __init__(self, *args, **kwargs): """ Mostly handles backward compatibility issues. `source` is deprecated field. a) If `source` exists and `source` is not `html5_sources`: show `source` field on front-end as not-editable but clearable. Dropdown is a new field `download_video` and it has value True. b) If `source` is cleared it is not shown anymore. c) If `source` exists and `source` in `html5_sources`, do not show `source` field. `download_video` field has value True. """ super(VideoDescriptor, self).__init__(*args, **kwargs) # For backwards compatibility -- if we've got XML data, parse it out and set the metadata fields if self.data: field_data = self._parse_video_xml(etree.fromstring(self.data)) self._field_data.set_many(self, field_data) del self.data self.source_visible = False if self.source: # If `source` field value exist in the `html5_sources` field values, # then delete `source` field value and use value from `html5_sources` field. if self.source in self.html5_sources: self.source = '' # Delete source field value. self.download_video = True else: # Otherwise, `source` field value will be used. self.source_visible = True if not self.fields['download_video'].is_set_on(self): self.download_video = True # Set download_video field to default value if its not explicitly set for backward compatibility. if not self.fields['download_video'].is_set_on(self): self.download_video = self.download_video # for backward compatibility. # If course was existed and was not re-imported by the moment of adding `download_track` field, # we should enable `download_track` if following is true: if not self.fields['download_track'].is_set_on(self) and self.track: self.download_track = True def editor_saved(self, user, old_metadata, old_content): """ Used to update video values during `self`:save method from CMS. old_metadata: dict, values of fields of `self` with scope=settings which were explicitly set by user. old_content, same as `old_metadata` but for scope=content. Due to nature of code flow in item.py::_save_item, before current function is called, fields of `self` instance have been already updated, but not yet saved. To obtain values, which were changed by user input, one should compare own_metadata(self) and old_medatada. Video player has two tabs, and due to nature of sync between tabs, metadata from Basic tab is always sent when video player is edited and saved first time, for example: {'youtube_id_1_0': u'3_yD_cEKoCk', 'display_name': u'Video', 'sub': u'3_yD_cEKoCk', 'html5_sources': []}, that's why these fields will always present in old_metadata after first save. This should be fixed. At consequent save requests html5_sources are always sent too, disregard of their change by user. That means that html5_sources are always in list of fields that were changed (`metadata` param in save_item). This should be fixed too. """ metadata_was_changed_by_user = old_metadata != own_metadata(self) if metadata_was_changed_by_user: manage_video_subtitles_save(self, user, old_metadata if old_metadata else None, generate_translation=True) def save_with_metadata(self, user): """ Save module with updated metadata to database." """ self.save() self.runtime.modulestore.update_item(self, user.id) @property def editable_metadata_fields(self): editable_fields = super(VideoDescriptor, self).editable_metadata_fields settings_service = self.runtime.service(self, 'settings') if settings_service: xb_settings = settings_service.get_settings_bucket(self) if not xb_settings.get("licensing_enabled", False) and "license" in editable_fields: del editable_fields["license"] if self.source_visible: editable_fields['source']['non_editable'] = True else: editable_fields.pop('source') languages = [{ 'label': label, 'code': lang } for lang, label in settings.ALL_LANGUAGES if lang != u'en'] languages.sort(key=lambda l: l['label']) languages.insert(0, {'label': 'Table of Contents', 'code': 'table'}) editable_fields['transcripts']['languages'] = languages editable_fields['transcripts']['type'] = 'VideoTranslations' editable_fields['transcripts']['urlRoot'] = self.runtime.handler_url( self, 'studio_transcript', 'translation').rstrip('/?') editable_fields['handout']['type'] = 'FileUploader' return editable_fields @classmethod def from_xml(cls, xml_data, system, id_generator): """ Creates an instance of this descriptor from the supplied xml_data. This may be overridden by subclasses xml_data: A string of xml that will be translated into data and children for this module system: A DescriptorSystem for interacting with external resources id_generator is used to generate course-specific urls and identifiers """ xml_object = etree.fromstring(xml_data) url_name = xml_object.get('url_name', xml_object.get('slug')) block_type = 'video' definition_id = id_generator.create_definition(block_type, url_name) usage_id = id_generator.create_usage(definition_id) if is_pointer_tag(xml_object): filepath = cls._format_filepath(xml_object.tag, name_to_pathname(url_name)) xml_object = cls.load_file(filepath, system.resources_fs, usage_id) system.parse_asides(xml_object, definition_id, usage_id, id_generator) field_data = cls._parse_video_xml(xml_object, id_generator) kvs = InheritanceKeyValueStore(initial_values=field_data) field_data = KvsFieldData(kvs) video = system.construct_xblock_from_class( cls, # We're loading a descriptor, so student_id is meaningless # We also don't have separate notions of definition and usage ids yet, # so we use the location for both ScopeIds(None, block_type, definition_id, usage_id), field_data, ) return video def definition_to_xml(self, resource_fs): """ Returns an xml string representing this module. """ xml = etree.Element('video') youtube_string = create_youtube_string(self) # Mild workaround to ensure that tests pass -- if a field # is set to its default value, we don't need to write it out. if youtube_string and youtube_string != '1.00:3_yD_cEKoCk': xml.set('youtube', unicode(youtube_string)) xml.set('url_name', self.url_name) attrs = { 'display_name': self.display_name, 'show_captions': json.dumps(self.show_captions), 'start_time': self.start_time, 'end_time': self.end_time, 'sub': self.sub, 'download_track': json.dumps(self.download_track), 'download_video': json.dumps(self.download_video), } for key, value in attrs.items(): # Mild workaround to ensure that tests pass -- if a field # is set to its default value, we don't write it out. if value: if key in self.fields and self.fields[key].is_set_on(self): xml.set(key, unicode(value)) for source in self.html5_sources: ele = etree.Element('source') ele.set('src', source) xml.append(ele) if self.track: ele = etree.Element('track') ele.set('src', self.track) xml.append(ele) if self.handout: ele = etree.Element('handout') ele.set('src', self.handout) xml.append(ele) # sorting for easy testing of resulting xml for transcript_language in sorted(self.transcripts.keys()): ele = etree.Element('transcript') ele.set('language', transcript_language) ele.set('src', self.transcripts[transcript_language]) xml.append(ele) if self.edx_video_id and edxval_api: try: xml.append(edxval_api.export_to_xml(self.edx_video_id)) except edxval_api.ValVideoNotFoundError: pass # handle license specifically self.add_license_to_xml(xml) return xml def get_context(self): """ Extend context by data for transcript basic tab. """ _context = super(VideoDescriptor, self).get_context() metadata_fields = copy.deepcopy(self.editable_metadata_fields) display_name = metadata_fields['display_name'] video_url = metadata_fields['html5_sources'] youtube_id_1_0 = metadata_fields['youtube_id_1_0'] def get_youtube_link(video_id): # First try a lookup in VAL. If we have a YouTube entry there, it overrides the # one passed in. if self.edx_video_id and edxval_api: val_youtube_id = edxval_api.get_url_for_profile( self.edx_video_id, "youtube") if val_youtube_id: video_id = val_youtube_id if video_id: return 'http://youtu.be/{0}'.format(video_id) else: return '' _ = self.runtime.service(self, "i18n").ugettext video_url.update({ 'help': _('The URL for your video. This can be a YouTube URL or a link to an .mp4, .ogg, or .webm video file hosted elsewhere on the Internet.' ), 'display_name': _('Default Video URL'), 'field_name': 'video_url', 'type': 'VideoList', 'default_value': [get_youtube_link(youtube_id_1_0['default_value'])] }) youtube_id_1_0_value = get_youtube_link(youtube_id_1_0['value']) if youtube_id_1_0_value: video_url['value'].insert(0, youtube_id_1_0_value) metadata = {'display_name': display_name, 'video_url': video_url} _context.update({'transcripts_basic_tab_metadata': metadata}) return _context @classmethod def _parse_youtube(cls, data): """ Parses a string of Youtube IDs such as "1.0:AXdE34_U,1.5:VO3SxfeD" into a dictionary. Necessary for backwards compatibility with XML-based courses. """ ret = {'0.75': '', '1.00': '', '1.25': '', '1.50': ''} videos = data.split(',') for video in videos: pieces = video.split(':') try: speed = '%.2f' % float(pieces[0]) # normalize speed # Handle the fact that youtube IDs got double-quoted for a period of time. # Note: we pass in "VideoFields.youtube_id_1_0" so we deserialize as a String-- # it doesn't matter what the actual speed is for the purposes of deserializing. youtube_id = deserialize_field(cls.youtube_id_1_0, pieces[1]) ret[speed] = youtube_id except (ValueError, IndexError): log.warning('Invalid YouTube ID: %s', video) return ret @classmethod def _parse_video_xml(cls, xml, id_generator=None): """ Parse video fields out of xml_data. The fields are set if they are present in the XML. Arguments: id_generator is used to generate course-specific urls and identifiers """ field_data = {} # Convert between key types for certain attributes -- # necessary for backwards compatibility. conversions = { # example: 'start_time': cls._example_convert_start_time } # Convert between key names for certain attributes -- # necessary for backwards compatibility. compat_keys = {'from': 'start_time', 'to': 'end_time'} sources = xml.findall('source') if sources: field_data['html5_sources'] = [ele.get('src') for ele in sources] track = xml.find('track') if track is not None: field_data['track'] = track.get('src') handout = xml.find('handout') if handout is not None: field_data['handout'] = handout.get('src') transcripts = xml.findall('transcript') if transcripts: field_data['transcripts'] = { tr.get('language'): tr.get('src') for tr in transcripts } for attr, value in xml.items(): if attr in compat_keys: attr = compat_keys[attr] if attr in cls.metadata_to_strip + ('url_name', 'name'): continue if attr == 'youtube': speeds = cls._parse_youtube(value) for speed, youtube_id in speeds.items(): # should have made these youtube_id_1_00 for # cleanliness, but hindsight doesn't need glasses normalized_speed = speed[:-1] if speed.endswith( '0') else speed # If the user has specified html5 sources, make sure we don't use the default video if youtube_id != '' or 'html5_sources' in field_data: field_data['youtube_id_{0}'.format( normalized_speed.replace('.', '_'))] = youtube_id elif attr in conversions: field_data[attr] = conversions[attr](value) elif attr not in cls.fields: field_data.setdefault('xml_attributes', {})[attr] = value else: # We export values with json.dumps (well, except for Strings, but # for about a month we did it for Strings also). field_data[attr] = deserialize_field(cls.fields[attr], value) # For backwards compatibility: Add `source` if XML doesn't have `download_video` # attribute. if 'download_video' not in field_data and sources: field_data['source'] = field_data['html5_sources'][0] # For backwards compatibility: if XML doesn't have `download_track` attribute, # it means that it is an old format. So, if `track` has some value, # `download_track` needs to have value `True`. if 'download_track' not in field_data and track is not None: field_data['download_track'] = True video_asset_elem = xml.find('video_asset') if (edxval_api and video_asset_elem is not None and 'edx_video_id' in field_data): # Allow ValCannotCreateError to escape edxval_api.import_from_xml(video_asset_elem, field_data['edx_video_id'], course_id=getattr( id_generator, 'target_course_id', None)) # load license if it exists field_data = LicenseMixin.parse_license_from_xml(field_data, xml) return field_data def index_dictionary(self): xblock_body = super(VideoDescriptor, self).index_dictionary() video_body = { "display_name": self.display_name, } def _update_transcript_for_index(language=None): """ Find video transcript - if not found, don't update index """ try: transcript = self.get_transcript(transcript_format='txt', lang=language)[0].replace( "\n", " ") transcript_index_name = "transcript_{}".format( language if language else self.transcript_language) video_body.update({transcript_index_name: transcript}) except NotFoundError: pass if self.sub: _update_transcript_for_index() # check to see if there are transcripts in other languages besides default transcript if self.transcripts: for language in self.transcripts.keys(): _update_transcript_for_index(language) if "content" in xblock_body: xblock_body["content"].update(video_body) else: xblock_body["content"] = video_body xblock_body["content_type"] = "Video" return xblock_body
class SplitTestDescriptor(SplitTestFields, SequenceDescriptor): # the editing interface can be the same as for sequences -- just a container module_class = SplitTestModule filename_extension = "xml" mako_template = "widgets/split-edit.html" css = {'scss': [resource_string(__name__, 'css/split_test/edit.scss')]} child_descriptor = module_attr('child_descriptor') log_child_render = module_attr('log_child_render') get_content_titles = module_attr('get_content_titles') def definition_to_xml(self, resource_fs): xml_object = etree.Element('split_test') renderable_groups = {} # json.dumps doesn't know how to handle Location objects for group in self.group_id_to_child: renderable_groups[group] = self.group_id_to_child[ group].to_deprecated_string() xml_object.set('group_id_to_child', json.dumps(renderable_groups)) xml_object.set('user_partition_id', str(self.user_partition_id)) for child in self.get_children(): self.runtime.add_block_as_child_node(child, xml_object) return xml_object @classmethod def definition_from_xml(cls, xml_object, system): children = [] raw_group_id_to_child = xml_object.attrib.get('group_id_to_child', None) user_partition_id = xml_object.attrib.get('user_partition_id', None) try: group_id_to_child = json.loads(raw_group_id_to_child) except ValueError: msg = "group_id_to_child is not valid json" log.exception(msg) system.error_tracker(msg) for child in xml_object: try: descriptor = system.process_xml(etree.tostring(child)) children.append(descriptor.scope_ids.usage_id) except Exception: msg = "Unable to load child when parsing split_test module." log.exception(msg) system.error_tracker(msg) return ({ 'group_id_to_child': group_id_to_child, 'user_partition_id': user_partition_id }, children) def get_context(self): _context = super(SplitTestDescriptor, self).get_context() _context.update({ 'disable_user_partition_editing': self._disable_user_partition_editing(), 'selected_partition': self._get_selected_partition() }) return _context def has_dynamic_children(self): """ Grading needs to know that only one of the children is actually "real". This makes it use module.get_child_descriptors(). """ return True def editor_saved(self, user, old_metadata, old_content): """ Used to create default verticals for the groups. Assumes that a mutable modulestore is being used. """ # Any existing value of user_partition_id will be in "old_content" instead of "old_metadata" # because it is Scope.content. if 'user_partition_id' not in old_content or old_content[ 'user_partition_id'] != self.user_partition_id: selected_partition = self._get_selected_partition() if selected_partition is not None: assert hasattr(self.system, 'modulestore') and hasattr(self.system.modulestore, 'create_and_save_xmodule'), \ "editor_saved should only be called when a mutable modulestore is available" modulestore = self.system.modulestore group_id_mapping = {} for group in selected_partition.groups: dest_usage_key = self.location.replace(category="vertical", name=uuid4().hex) metadata = {'display_name': group.name} modulestore.create_and_save_xmodule( dest_usage_key, definition_data=None, metadata=metadata, system=self.system, ) self.children.append(dest_usage_key) # pylint: disable=no-member group_id_mapping[unicode(group.id)] = dest_usage_key self.group_id_to_child = group_id_mapping # Don't need to call update_item in the modulestore because the caller of this method will do it. @property def editable_metadata_fields(self): # Update the list of partitions based on the currently available user_partitions. SplitTestFields.build_partition_values(self.user_partitions) editable_fields = super(SplitTestDescriptor, self).editable_metadata_fields if not self._disable_user_partition_editing(): # Explicitly add user_partition_id, which does not automatically get picked up because it is Scope.content. # Note that this means it will be saved by the Studio editor as "metadata", but the field will # still update correctly. editable_fields[SplitTestFields.user_partition_id. name] = self._create_metadata_editor_info( SplitTestFields.user_partition_id) return editable_fields @property def non_editable_metadata_fields(self): non_editable_fields = super(SplitTestDescriptor, self).non_editable_metadata_fields non_editable_fields.extend( [SplitTestDescriptor.due, SplitTestDescriptor.user_partitions]) return non_editable_fields def _disable_user_partition_editing(self): """ If user_partition_id has been set to anything besides the default value, disable editing. """ return self.user_partition_id != SplitTestFields.user_partition_id.default def _get_selected_partition(self): """ Returns the partition that this split module is currently using, or None if the currently selected partition ID does not match any of the defined partitions. """ for user_partition in self.user_partitions: if user_partition.id == self.user_partition_id: return user_partition return None def validation_message(self): """ Returns a validation message describing the current state of the block, as well as a message type indicating whether the message represents information, a warning or an error. """ _ = self.runtime.service(self, "i18n").ugettext # pylint: disable=redefined-outer-name if self.user_partition_id < 0: return _( u"You must select a group configuration for this content experiment." ), ValidationMessageType.warning user_partition = self._get_selected_partition() if not user_partition: return \ _(u"This content experiment will not be shown to students because it refers to a group configuration that has been deleted. You can delete this experiment or reinstate the group configuration to repair it."), \ ValidationMessageType.error groups = user_partition.groups if not len(groups) == len(self.get_children()): return _( u"This content experiment is in an invalid state and cannot be repaired. Please delete and recreate." ), ValidationMessageType.error return _( u"This content experiment uses group configuration '{experiment_name}'." ).format(experiment_name=user_partition.name ), ValidationMessageType.information
class CapaDescriptor(CapaFields, RawDescriptor): """ Module implementing problems in the LON-CAPA format, as implemented by capa.capa_problem """ INDEX_CONTENT_TYPE = 'CAPA' module_class = CapaModule resources_dir = None has_score = True show_in_read_only_mode = True template_dir_name = 'problem' mako_template = "widgets/problem-edit.html" js = {'js': [resource_string(__name__, 'js/src/problem/edit.js')]} js_module_name = "MarkdownEditingDescriptor" has_author_view = True css = { 'scss': [ resource_string(__name__, 'css/editor/edit.scss'), resource_string(__name__, 'css/problem/edit.scss') ] } # The capa format specifies that what we call max_attempts in the code # is the attribute `attempts`. This will do that conversion metadata_translations = dict(RawDescriptor.metadata_translations) metadata_translations['attempts'] = 'max_attempts' @classmethod def filter_templates(cls, template, course): """ Filter template that contains 'latex' from templates. Show them only if use_latex_compiler is set to True in course settings. """ return 'latex' not in template['template_id'] or course.use_latex_compiler def get_context(self): _context = RawDescriptor.get_context(self) _context.update({ 'markdown': self.markdown, 'enable_markdown': self.markdown is not None, 'enable_latex_compiler': self.use_latex_compiler, }) return _context # VS[compat] # TODO (cpennington): Delete this method once all fall 2012 course are being # edited in the cms @classmethod def backcompat_paths(cls, path): dog_stats_api.increment( DEPRECATION_VSCOMPAT_EVENT, tags=["location:capa_descriptor_backcompat_paths"] ) return [ 'problems/' + path[8:], path[8:], ] @property def non_editable_metadata_fields(self): non_editable_fields = super(CapaDescriptor, self).non_editable_metadata_fields non_editable_fields.extend([ CapaDescriptor.due, CapaDescriptor.graceperiod, CapaDescriptor.force_save_button, CapaDescriptor.markdown, CapaDescriptor.use_latex_compiler, CapaDescriptor.show_correctness, ]) return non_editable_fields @property def problem_types(self): """ Low-level problem type introspection for content libraries filtering by problem type """ try: tree = etree.XML(self.data) except etree.XMLSyntaxError: log.error('Error parsing problem types from xml for capa module {}'.format(self.display_name)) return None # short-term fix to prevent errors (TNL-5057). Will be more properly addressed in TNL-4525. registered_tags = responsetypes.registry.registered_tags() return {node.tag for node in tree.iter() if node.tag in registered_tags} def index_dictionary(self): """ Return dictionary prepared with module content and type for indexing. """ xblock_body = super(CapaDescriptor, self).index_dictionary() # Removing solutions and hints, as well as script and style capa_content = re.sub( re.compile( r""" <solution>.*?</solution> | <script>.*?</script> | <style>.*?</style> | <[a-z]*hint.*?>.*?</[a-z]*hint> """, re.DOTALL | re.VERBOSE), "", self.data ) capa_content = escape_html_characters(capa_content) capa_body = { "capa_content": capa_content, "display_name": self.display_name, } if "content" in xblock_body: xblock_body["content"].update(capa_body) else: xblock_body["content"] = capa_body xblock_body["content_type"] = self.INDEX_CONTENT_TYPE xblock_body["problem_types"] = list(self.problem_types) return xblock_body def has_support(self, view, functionality): """ Override the XBlock.has_support method to return appropriate value for the multi-device functionality. Returns whether the given view has support for the given functionality. """ if functionality == "multi_device": types = self.problem_types # Avoid calculating this property twice return types is not None and all( responsetypes.registry.get_class_for_tag(tag).multi_device_support for tag in types ) return False def max_score(self): """ Return the problem's max score """ from capa.capa_problem import LoncapaProblem, LoncapaSystem capa_system = LoncapaSystem( ajax_url=None, anonymous_student_id=None, cache=None, can_execute_unsafe_code=None, get_python_lib_zip=None, DEBUG=None, filestore=self.runtime.resources_fs, i18n=self.runtime.service(self, "i18n"), node_path=None, render_template=None, seed=None, STATIC_URL=None, xqueue=None, matlab_api_key=None, ) lcp = LoncapaProblem( problem_text=self.data, id=self.location.html_id(), capa_system=capa_system, capa_module=self, state={}, seed=1, minimal_init=True, ) return lcp.get_max_score() def generate_report_data(self, user_state_iterator, limit_responses=None): """ Return a list of student responses to this block in a readable way. Arguments: user_state_iterator: iterator over UserStateClient objects. E.g. the result of user_state_client.iter_all_for_block(block_key) limit_responses (int|None): maximum number of responses to include. Set to None (default) to include all. Returns: each call returns a tuple like: ("username", { "Question": "2 + 2 equals how many?", "Answer": "Four", "Answer ID": "98e6a8e915904d5389821a94e48babcf_10_1" }) """ from capa.capa_problem import LoncapaProblem, LoncapaSystem if self.category != 'problem': raise NotImplementedError() if limit_responses == 0: # Don't even start collecting answers return capa_system = LoncapaSystem( ajax_url=None, # TODO set anonymous_student_id to the anonymous ID of the user which answered each problem # Anonymous ID is required for Matlab, CodeResponse, and some custom problems that include # '$anonymous_student_id' in their XML. # For the purposes of this report, we don't need to support those use cases. anonymous_student_id=None, cache=None, can_execute_unsafe_code=lambda: None, get_python_lib_zip=(lambda: get_python_lib_zip(contentstore, self.runtime.course_id)), DEBUG=None, filestore=self.runtime.resources_fs, i18n=self.runtime.service(self, "i18n"), node_path=None, render_template=None, seed=1, STATIC_URL=None, xqueue=None, matlab_api_key=None, ) _ = capa_system.i18n.ugettext count = 0 for user_state in user_state_iterator: if 'student_answers' not in user_state.state: continue lcp = LoncapaProblem( problem_text=self.data, id=self.location.html_id(), capa_system=capa_system, # We choose to run without a fully initialized CapaModule capa_module=None, state={ 'done': user_state.state.get('done'), 'correct_map': user_state.state.get('correct_map'), 'student_answers': user_state.state.get('student_answers'), 'has_saved_answers': user_state.state.get('has_saved_answers'), 'input_state': user_state.state.get('input_state'), 'seed': user_state.state.get('seed'), }, seed=user_state.state.get('seed'), # extract_tree=False allows us to work without a fully initialized CapaModule # We'll still be able to find particular data in the XML when we need it extract_tree=False, ) for answer_id, orig_answers in lcp.student_answers.items(): # Some types of problems have data in lcp.student_answers that isn't in lcp.problem_data. # E.g. formulae do this to store the MathML version of the answer. # We exclude these rows from the report because we only need the text-only answer. if answer_id.endswith('_dynamath'): continue if limit_responses and count >= limit_responses: # End the iterator here return question_text = lcp.find_question_label(answer_id) answer_text = lcp.find_answer_text(answer_id, current_answer=orig_answers) correct_answer_text = lcp.find_correct_answer_text(answer_id) count += 1 report = { _("Answer ID"): answer_id, _("Question"): question_text, _("Answer"): answer_text, } if correct_answer_text is not None: report[_("Correct Answer")] = correct_answer_text yield (user_state.username, report) # Proxy to CapaModule for access to any of its attributes answer_available = module_attr('answer_available') submit_button_name = module_attr('submit_button_name') submit_button_submitting_name = module_attr('submit_button_submitting_name') submit_problem = module_attr('submit_problem') choose_new_seed = module_attr('choose_new_seed') closed = module_attr('closed') get_answer = module_attr('get_answer') get_problem = module_attr('get_problem') get_problem_html = module_attr('get_problem_html') get_state_for_lcp = module_attr('get_state_for_lcp') handle_input_ajax = module_attr('handle_input_ajax') hint_button = module_attr('hint_button') handle_problem_html_error = module_attr('handle_problem_html_error') handle_ungraded_response = module_attr('handle_ungraded_response') has_submitted_answer = module_attr('has_submitted_answer') is_attempted = module_attr('is_attempted') is_correct = module_attr('is_correct') is_past_due = module_attr('is_past_due') is_submitted = module_attr('is_submitted') lcp = module_attr('lcp') make_dict_of_responses = module_attr('make_dict_of_responses') new_lcp = module_attr('new_lcp') publish_grade = module_attr('publish_grade') rescore = module_attr('rescore') reset_problem = module_attr('reset_problem') save_problem = module_attr('save_problem') set_score = module_attr('set_score') set_state_from_lcp = module_attr('set_state_from_lcp') should_show_submit_button = module_attr('should_show_submit_button') should_show_reset_button = module_attr('should_show_reset_button') should_show_save_button = module_attr('should_show_save_button') update_score = module_attr('update_score')
class VideoDescriptor(VideoFields, VideoTranscriptsMixin, VideoStudioViewHandlers, TabsEditingDescriptor, EmptyDataRawDescriptor, LicenseMixin): """ Descriptor for `VideoModule`. """ module_class = VideoModule transcript = module_attr('transcript') show_in_read_only_mode = True tabs = [ { 'name': _("Basic"), 'template': "video/transcripts.html", 'current': True }, { 'name': _("Advanced"), 'template': "tabs/metadata-edit-tab.html" } ] def __init__(self, *args, **kwargs): """ Mostly handles backward compatibility issues. `source` is deprecated field. a) If `source` exists and `source` is not `html5_sources`: show `source` field on front-end as not-editable but clearable. Dropdown is a new field `download_video` and it has value True. b) If `source` is cleared it is not shown anymore. c) If `source` exists and `source` in `html5_sources`, do not show `source` field. `download_video` field has value True. """ super(VideoDescriptor, self).__init__(*args, **kwargs) # For backwards compatibility -- if we've got XML data, parse it out and set the metadata fields if self.data: field_data = self._parse_video_xml(etree.fromstring(self.data)) self._field_data.set_many(self, field_data) del self.data self.source_visible = False if self.source: # If `source` field value exist in the `html5_sources` field values, # then delete `source` field value and use value from `html5_sources` field. if self.source in self.html5_sources: self.source = '' # Delete source field value. self.download_video = True else: # Otherwise, `source` field value will be used. self.source_visible = True if not self.fields['download_video'].is_set_on(self): self.download_video = True # Force download_video field to default value if it's not explicitly set for backward compatibility. if not self.fields['download_video'].is_set_on(self): self.download_video = self.download_video self.force_save_fields(['download_video']) # for backward compatibility. # If course was existed and was not re-imported by the moment of adding `download_track` field, # we should enable `download_track` if following is true: if not self.fields['download_track'].is_set_on(self) and self.track: self.download_track = True def validate(self): """ Validates the state of this video Module Instance. This is the override of the general XBlock method, and it will also ask its superclass to validate. """ validation = super(VideoDescriptor, self).validate() if not isinstance(validation, StudioValidation): validation = StudioValidation.copy(validation) no_transcript_lang = [] for lang_code, transcript in self.transcripts.items(): if not transcript: no_transcript_lang.append([label for code, label in settings.ALL_LANGUAGES if code == lang_code][0]) if no_transcript_lang: ungettext = self.runtime.service(self, "i18n").ungettext validation.set_summary( StudioValidationMessage( StudioValidationMessage.WARNING, ungettext( 'There is no transcript file associated with the {lang} language.', 'There are no transcript files associated with the {lang} languages.', len(no_transcript_lang) ).format(lang=', '.join(no_transcript_lang)) ) ) return validation def editor_saved(self, user, old_metadata, old_content): """ Used to update video values during `self`:save method from CMS. old_metadata: dict, values of fields of `self` with scope=settings which were explicitly set by user. old_content, same as `old_metadata` but for scope=content. Due to nature of code flow in item.py::_save_item, before current function is called, fields of `self` instance have been already updated, but not yet saved. To obtain values, which were changed by user input, one should compare own_metadata(self) and old_medatada. Video player has two tabs, and due to nature of sync between tabs, metadata from Basic tab is always sent when video player is edited and saved first time, for example: {'youtube_id_1_0': u'3_yD_cEKoCk', 'display_name': u'Video', 'sub': u'3_yD_cEKoCk', 'html5_sources': []}, that's why these fields will always present in old_metadata after first save. This should be fixed. At consequent save requests html5_sources are always sent too, disregard of their change by user. That means that html5_sources are always in list of fields that were changed (`metadata` param in save_item). This should be fixed too. """ metadata_was_changed_by_user = old_metadata != own_metadata(self) # There is an edge case when old_metadata and own_metadata are same and we are importing transcript from youtube # then there is a syncing issue where html5_subs are not syncing with youtube sub, We can make sync better by # checking if transcript is present for the video and if any html5_ids transcript is not present then trigger # the manage_video_subtitles_save to create the missing transcript with particular html5_id. if not metadata_was_changed_by_user and self.sub and hasattr(self, 'html5_sources'): html5_ids = get_html5_ids(self.html5_sources) for subs_id in html5_ids: try: Transcript.asset(self.location, subs_id) except NotFoundError: # If a transcript does not not exist with particular html5_id then there is no need to check other # html5_ids because we have to create a new transcript with this missing html5_id by turning on # metadata_was_changed_by_user flag. metadata_was_changed_by_user = True break if metadata_was_changed_by_user: self.edx_video_id = self.edx_video_id and self.edx_video_id.strip() # We want to override `youtube_id_1_0` with val youtube profile in the first place when someone adds/edits # an `edx_video_id` or its underlying YT val profile. Without this, override will only happen when a user # saves the video second time. This is because of the syncing of basic and advanced video settings which # also syncs val youtube id from basic tab's `Video Url` to advanced tab's `Youtube ID`. if self.edx_video_id and edxval_api: val_youtube_id = edxval_api.get_url_for_profile(self.edx_video_id, 'youtube') if val_youtube_id and self.youtube_id_1_0 != val_youtube_id: self.youtube_id_1_0 = val_youtube_id manage_video_subtitles_save( self, user, old_metadata if old_metadata else None, generate_translation=True ) def save_with_metadata(self, user): """ Save module with updated metadata to database." """ self.save() self.runtime.modulestore.update_item(self, user.id) @property def editable_metadata_fields(self): editable_fields = super(VideoDescriptor, self).editable_metadata_fields settings_service = self.runtime.service(self, 'settings') if settings_service: xb_settings = settings_service.get_settings_bucket(self) if not xb_settings.get("licensing_enabled", False) and "license" in editable_fields: del editable_fields["license"] if self.source_visible: editable_fields['source']['non_editable'] = True else: editable_fields.pop('source') languages = [{'label': label, 'code': lang} for lang, label in settings.ALL_LANGUAGES] languages.sort(key=lambda l: l['label']) editable_fields['transcripts']['languages'] = languages editable_fields['transcripts']['type'] = 'VideoTranslations' editable_fields['transcripts']['urlRoot'] = self.runtime.handler_url( self, 'studio_transcript', 'translation' ).rstrip('/?') editable_fields['handout']['type'] = 'FileUploader' return editable_fields @classmethod def from_xml(cls, xml_data, system, id_generator): """ Creates an instance of this descriptor from the supplied xml_data. This may be overridden by subclasses xml_data: A string of xml that will be translated into data and children for this module system: A DescriptorSystem for interacting with external resources id_generator is used to generate course-specific urls and identifiers """ xml_object = etree.fromstring(xml_data) url_name = xml_object.get('url_name', xml_object.get('slug')) block_type = 'video' definition_id = id_generator.create_definition(block_type, url_name) usage_id = id_generator.create_usage(definition_id) if is_pointer_tag(xml_object): filepath = cls._format_filepath(xml_object.tag, name_to_pathname(url_name)) xml_object = cls.load_file(filepath, system.resources_fs, usage_id) system.parse_asides(xml_object, definition_id, usage_id, id_generator) field_data = cls._parse_video_xml(xml_object, id_generator) kvs = InheritanceKeyValueStore(initial_values=field_data) field_data = KvsFieldData(kvs) video = system.construct_xblock_from_class( cls, # We're loading a descriptor, so student_id is meaningless # We also don't have separate notions of definition and usage ids yet, # so we use the location for both ScopeIds(None, block_type, definition_id, usage_id), field_data, ) return video def definition_to_xml(self, resource_fs): """ Returns an xml string representing this module. """ xml = etree.Element('video') youtube_string = create_youtube_string(self) # Mild workaround to ensure that tests pass -- if a field # is set to its default value, we don't need to write it out. if youtube_string and youtube_string != '1.00:3_yD_cEKoCk': xml.set('youtube', unicode(youtube_string)) xml.set('url_name', self.url_name) attrs = { 'display_name': self.display_name, 'show_captions': json.dumps(self.show_captions), 'start_time': self.start_time, 'end_time': self.end_time, 'sub': self.sub, 'download_track': json.dumps(self.download_track), 'download_video': json.dumps(self.download_video), } for key, value in attrs.items(): # Mild workaround to ensure that tests pass -- if a field # is set to its default value, we don't write it out. if value: if key in self.fields and self.fields[key].is_set_on(self): try: xml.set(key, unicode(value)) except UnicodeDecodeError: exception_message = format_xml_exception_message(self.location, key, value) log.exception(exception_message) # If exception is UnicodeDecodeError set value using unicode 'utf-8' scheme. log.info("Setting xml value using 'utf-8' scheme.") xml.set(key, unicode(value, 'utf-8')) except ValueError: exception_message = format_xml_exception_message(self.location, key, value) log.exception(exception_message) raise for source in self.html5_sources: ele = etree.Element('source') ele.set('src', source) xml.append(ele) if self.track: ele = etree.Element('track') ele.set('src', self.track) xml.append(ele) if self.handout: ele = etree.Element('handout') ele.set('src', self.handout) xml.append(ele) if self.transcripts is not None: # sorting for easy testing of resulting xml for transcript_language in sorted(self.transcripts.keys()): ele = etree.Element('transcript') ele.set('language', transcript_language) ele.set('src', self.transcripts[transcript_language]) xml.append(ele) if self.edx_video_id and edxval_api: try: xml.append(edxval_api.export_to_xml( self.edx_video_id, unicode(self.runtime.course_id.for_branch(None))) ) except edxval_api.ValVideoNotFoundError: pass # handle license specifically self.add_license_to_xml(xml) return xml def create_youtube_url(self, youtube_id): """ Args: youtube_id: The ID of the video to create a link for Returns: A full youtube url to the video whose ID is passed in """ if youtube_id: return u'https://www.youtube.com/watch?v={0}'.format(youtube_id) else: return u'' def get_context(self): """ Extend context by data for transcript basic tab. """ _context = super(VideoDescriptor, self).get_context() metadata_fields = copy.deepcopy(self.editable_metadata_fields) display_name = metadata_fields['display_name'] video_url = metadata_fields['html5_sources'] video_id = metadata_fields['edx_video_id'] youtube_id_1_0 = metadata_fields['youtube_id_1_0'] def get_youtube_link(video_id): """ Returns the fully-qualified YouTube URL for the given video identifier """ # First try a lookup in VAL. If we have a YouTube entry there, it overrides the # one passed in. if self.edx_video_id and edxval_api: val_youtube_id = edxval_api.get_url_for_profile(self.edx_video_id, "youtube") if val_youtube_id: video_id = val_youtube_id return self.create_youtube_url(video_id) _ = self.runtime.service(self, "i18n").ugettext video_url.update({ 'help': _('The URL for your video. This can be a YouTube URL or a link to an .mp4, .ogg, or .webm video file hosted elsewhere on the Internet.'), # pylint: disable=line-too-long 'display_name': _('Default Video URL'), 'field_name': 'video_url', 'type': 'VideoList', 'default_value': [get_youtube_link(youtube_id_1_0['default_value'])] }) source_url = self.create_youtube_url(youtube_id_1_0['value']) # First try a lookup in VAL. If any video encoding is found given the video id then # override the source_url with it. if self.edx_video_id and edxval_api: val_profiles = ['youtube', 'desktop_webm', 'desktop_mp4'] if HLSPlaybackEnabledFlag.feature_enabled(self.runtime.course_id.for_branch(None)): val_profiles.append('hls') # Get video encodings for val profiles. val_video_encodings = edxval_api.get_urls_for_profiles(self.edx_video_id, val_profiles) # VAL's youtube source has greater priority over external youtube source. if val_video_encodings.get('youtube'): source_url = self.create_youtube_url(val_video_encodings['youtube']) # If no youtube source is provided externally or in VAl, update source_url in order: hls > mp4 and webm if not source_url: if val_video_encodings.get('hls'): source_url = val_video_encodings['hls'] elif val_video_encodings.get('desktop_mp4'): source_url = val_video_encodings['desktop_mp4'] elif val_video_encodings.get('desktop_webm'): source_url = val_video_encodings['desktop_webm'] # Only add if html5 sources do not already contain source_url. if source_url and source_url not in video_url['value']: video_url['value'].insert(0, source_url) metadata = { 'display_name': display_name, 'video_url': video_url, 'edx_video_id': video_id } _context.update({'transcripts_basic_tab_metadata': metadata}) return _context @classmethod def _parse_youtube(cls, data): """ Parses a string of Youtube IDs such as "1.0:AXdE34_U,1.5:VO3SxfeD" into a dictionary. Necessary for backwards compatibility with XML-based courses. """ ret = {'0.75': '', '1.00': '', '1.25': '', '1.50': ''} videos = data.split(',') for video in videos: pieces = video.split(':') try: speed = '%.2f' % float(pieces[0]) # normalize speed # Handle the fact that youtube IDs got double-quoted for a period of time. # Note: we pass in "VideoFields.youtube_id_1_0" so we deserialize as a String-- # it doesn't matter what the actual speed is for the purposes of deserializing. youtube_id = deserialize_field(cls.youtube_id_1_0, pieces[1]) ret[speed] = youtube_id except (ValueError, IndexError): log.warning('Invalid YouTube ID: %s', video) return ret @classmethod def _parse_video_xml(cls, xml, id_generator=None): """ Parse video fields out of xml_data. The fields are set if they are present in the XML. Arguments: id_generator is used to generate course-specific urls and identifiers """ field_data = {} # Convert between key types for certain attributes -- # necessary for backwards compatibility. conversions = { # example: 'start_time': cls._example_convert_start_time } # Convert between key names for certain attributes -- # necessary for backwards compatibility. compat_keys = { 'from': 'start_time', 'to': 'end_time' } sources = xml.findall('source') if sources: field_data['html5_sources'] = [ele.get('src') for ele in sources] track = xml.find('track') if track is not None: field_data['track'] = track.get('src') handout = xml.find('handout') if handout is not None: field_data['handout'] = handout.get('src') transcripts = xml.findall('transcript') if transcripts: field_data['transcripts'] = {tr.get('language'): tr.get('src') for tr in transcripts} for attr, value in xml.items(): if attr in compat_keys: attr = compat_keys[attr] if attr in cls.metadata_to_strip + ('url_name', 'name'): continue if attr == 'youtube': speeds = cls._parse_youtube(value) for speed, youtube_id in speeds.items(): # should have made these youtube_id_1_00 for # cleanliness, but hindsight doesn't need glasses normalized_speed = speed[:-1] if speed.endswith('0') else speed # If the user has specified html5 sources, make sure we don't use the default video if youtube_id != '' or 'html5_sources' in field_data: field_data['youtube_id_{0}'.format(normalized_speed.replace('.', '_'))] = youtube_id elif attr in conversions: field_data[attr] = conversions[attr](value) elif attr not in cls.fields: field_data.setdefault('xml_attributes', {})[attr] = value else: # We export values with json.dumps (well, except for Strings, but # for about a month we did it for Strings also). field_data[attr] = deserialize_field(cls.fields[attr], value) course_id = getattr(id_generator, 'target_course_id', None) # Update the handout location with current course_id if 'handout' in field_data.keys() and course_id: handout_location = StaticContent.get_location_from_path(field_data['handout']) if isinstance(handout_location, AssetLocator): handout_new_location = StaticContent.compute_location(course_id, handout_location.path) field_data['handout'] = StaticContent.serialize_asset_key_with_slash(handout_new_location) # For backwards compatibility: Add `source` if XML doesn't have `download_video` # attribute. if 'download_video' not in field_data and sources: field_data['source'] = field_data['html5_sources'][0] # For backwards compatibility: if XML doesn't have `download_track` attribute, # it means that it is an old format. So, if `track` has some value, # `download_track` needs to have value `True`. if 'download_track' not in field_data and track is not None: field_data['download_track'] = True video_asset_elem = xml.find('video_asset') if ( edxval_api and video_asset_elem is not None and 'edx_video_id' in field_data ): # Allow ValCannotCreateError to escape edxval_api.import_from_xml( video_asset_elem, field_data['edx_video_id'], course_id=course_id ) # load license if it exists field_data = LicenseMixin.parse_license_from_xml(field_data, xml) return field_data def index_dictionary(self): xblock_body = super(VideoDescriptor, self).index_dictionary() video_body = { "display_name": self.display_name, } def _update_transcript_for_index(language=None): """ Find video transcript - if not found, don't update index """ try: transcripts = self.get_transcripts_info() transcript = self.get_transcript( transcripts, transcript_format='txt', lang=language )[0].replace("\n", " ") transcript_index_name = "transcript_{}".format(language if language else self.transcript_language) video_body.update({transcript_index_name: transcript}) except NotFoundError: pass if self.sub: _update_transcript_for_index() # Check to see if there are transcripts in other languages besides default transcript if self.transcripts: for language in self.transcripts.keys(): _update_transcript_for_index(language) if "content" in xblock_body: xblock_body["content"].update(video_body) else: xblock_body["content"] = video_body xblock_body["content_type"] = "Video" return xblock_body @property def request_cache(self): """ Returns the request_cache from the runtime. """ return self.runtime.service(self, "request_cache") @memoize_in_request_cache('request_cache') def get_cached_val_data_for_course(self, video_profile_names, course_id): """ Returns the VAL data for the requested video profiles for the given course. """ return edxval_api.get_video_info_for_course_and_profiles(unicode(course_id), video_profile_names) def student_view_data(self, context=None): """ Returns a JSON representation of the student_view of this XModule. The contract of the JSON content is between the caller and the particular XModule. """ context = context or {} # If the "only_on_web" field is set on this video, do not return the rest of the video's data # in this json view, since this video is to be accessed only through its web view." if self.only_on_web: return {"only_on_web": True} encoded_videos = {} val_video_data = {} # Check in VAL data first if edx_video_id exists if self.edx_video_id: video_profile_names = context.get("profiles", ["mobile_low"]) # get and cache bulk VAL data for course val_course_data = self.get_cached_val_data_for_course(video_profile_names, self.location.course_key) val_video_data = val_course_data.get(self.edx_video_id, {}) # Get the encoded videos if data from VAL is found if val_video_data: encoded_videos = val_video_data.get('profiles', {}) # If information for this edx_video_id is not found in the bulk course data, make a # separate request for this individual edx_video_id, unless cache misses are disabled. # This is useful/required for videos that don't have a course designated, such as the introductory video # that is shared across many courses. However, this results in a separate database request so watch # out for any performance hit if many such videos exist in a course. Set the 'allow_cache_miss' parameter # to False to disable this fall back. elif context.get("allow_cache_miss", "True").lower() == "true": try: val_video_data = edxval_api.get_video_info(self.edx_video_id) # Unfortunately, the VAL API is inconsistent in how it returns the encodings, so remap here. for enc_vid in val_video_data.pop('encoded_videos'): if enc_vid['profile'] in video_profile_names: encoded_videos[enc_vid['profile']] = {key: enc_vid[key] for key in ["url", "file_size"]} except edxval_api.ValVideoNotFoundError: pass # Fall back to other video URLs in the video module if not found in VAL if not encoded_videos: video_url = self.html5_sources[0] if self.html5_sources else self.source if video_url: encoded_videos["fallback"] = { "url": video_url, "file_size": 0, # File size is unknown for fallback URLs } # Include youtube link if there is no encoding for mobile- ie only a fallback URL or no encodings at all # We are including a fallback URL for older versions of the mobile app that don't handle Youtube urls if self.youtube_id_1_0: encoded_videos["youtube"] = { "url": self.create_youtube_url(self.youtube_id_1_0), "file_size": 0, # File size is not relevant for external link } transcripts_info = self.get_transcripts_info() transcripts = { lang: self.runtime.handler_url(self, 'transcript', 'download', query="lang=" + lang, thirdparty=True) for lang in self.available_translations(transcripts_info) } return { "only_on_web": self.only_on_web, "duration": val_video_data.get('duration', None), "transcripts": transcripts, "encoded_videos": encoded_videos, }
class CapaDescriptor(TimedCapaFields, CapaFields, RawDescriptor): """ Module implementing problems in the LON-CAPA format, as implemented by capa.capa_problem """ INDEX_CONTENT_TYPE = 'CAPA' module_class = CapaModule resources_dir = None has_score = True show_in_read_only_mode = True template_dir_name = 'problem' mako_template = "widgets/problem-edit.html" js = {'js': [resource_string(__name__, 'js/src/problem/edit.js')]} js_module_name = "MarkdownEditingDescriptor" has_author_view = True css = { 'scss': [ resource_string(__name__, 'css/editor/edit.scss'), resource_string(__name__, 'css/problem/edit.scss') ] } # The capa format specifies that what we call max_attempts in the code # is the attribute `attempts`. This will do that conversion metadata_translations = dict(RawDescriptor.metadata_translations) metadata_translations['attempts'] = 'max_attempts' @classmethod def filter_templates(cls, template, course): """ Filter template that contains 'latex' from templates. Show them only if use_latex_compiler is set to True in course settings. """ return 'latex' not in template[ 'template_id'] or course.use_latex_compiler def get_context(self): _context = RawDescriptor.get_context(self) _context.update({ 'markdown': self.markdown, 'enable_markdown': self.markdown is not None, 'enable_latex_compiler': self.use_latex_compiler, }) return _context # VS[compat] # TODO (cpennington): Delete this method once all fall 2012 course are being # edited in the cms @classmethod def backcompat_paths(cls, path): dog_stats_api.increment( DEPRECATION_VSCOMPAT_EVENT, tags=["location:capa_descriptor_backcompat_paths"]) return [ 'problems/' + path[8:], path[8:], ] @property def non_editable_metadata_fields(self): non_editable_fields = super(CapaDescriptor, self).non_editable_metadata_fields non_editable_fields.extend([ CapaDescriptor.due, CapaDescriptor.graceperiod, CapaDescriptor.force_save_button, CapaDescriptor.markdown, CapaDescriptor.use_latex_compiler, CapaDescriptor.show_correctness, ]) return non_editable_fields @property def problem_types(self): """ Low-level problem type introspection for content libraries filtering by problem type """ try: tree = etree.XML(self.data) except etree.XMLSyntaxError: log.error( 'Error parsing problem types from xml for capa module {}'. format(self.display_name)) return None # short-term fix to prevent errors (TNL-5057). Will be more properly addressed in TNL-4525. registered_tags = responsetypes.registry.registered_tags() return { node.tag for node in tree.iter() if node.tag in registered_tags } def index_dictionary(self): """ Return dictionary prepared with module content and type for indexing. """ xblock_body = super(CapaDescriptor, self).index_dictionary() # Removing solutions and hints, as well as script and style capa_content = re.sub( re.compile( r""" <solution>.*?</solution> | <script>.*?</script> | <style>.*?</style> | <[a-z]*hint.*?>.*?</[a-z]*hint> """, re.DOTALL | re.VERBOSE), "", self.data) capa_content = escape_html_characters(capa_content) capa_body = { "capa_content": capa_content, "display_name": self.display_name, } if "content" in xblock_body: xblock_body["content"].update(capa_body) else: xblock_body["content"] = capa_body xblock_body["content_type"] = self.INDEX_CONTENT_TYPE xblock_body["problem_types"] = list(self.problem_types) return xblock_body def has_support(self, view, functionality): """ Override the XBlock.has_support method to return appropriate value for the multi-device functionality. Returns whether the given view has support for the given functionality. """ if functionality == "multi_device": types = self.problem_types # Avoid calculating this property twice return types is not None and all( responsetypes.registry.get_class_for_tag( tag).multi_device_support for tag in types) return False def max_score(self): """ Return the problem's max score """ from capa.capa_problem import LoncapaProblem, LoncapaSystem capa_system = LoncapaSystem( ajax_url=None, anonymous_student_id=None, cache=None, can_execute_unsafe_code=None, get_python_lib_zip=None, DEBUG=None, filestore=self.runtime.resources_fs, i18n=self.runtime.service(self, "i18n"), node_path=None, render_template=None, seed=None, STATIC_URL=None, xqueue=None, matlab_api_key=None, ) lcp = LoncapaProblem( problem_text=self.data, id=self.location.html_id(), capa_system=capa_system, capa_module=self, state={}, seed=1, minimal_init=True, ) return lcp.get_max_score() # Proxy to CapaModule for access to any of its attributes answer_available = module_attr('answer_available') submit_button_name = module_attr('submit_button_name') submit_button_submitting_name = module_attr( 'submit_button_submitting_name') submit_problem = module_attr('submit_problem') choose_new_seed = module_attr('choose_new_seed') closed = module_attr('closed') get_answer = module_attr('get_answer') get_problem = module_attr('get_problem') get_problem_html = module_attr('get_problem_html') get_state_for_lcp = module_attr('get_state_for_lcp') handle_input_ajax = module_attr('handle_input_ajax') hint_button = module_attr('hint_button') handle_problem_html_error = module_attr('handle_problem_html_error') handle_ungraded_response = module_attr('handle_ungraded_response') is_attempted = module_attr('is_attempted') is_correct = module_attr('is_correct') is_past_due = module_attr('is_past_due') is_submitted = module_attr('is_submitted') lcp = module_attr('lcp') make_dict_of_responses = module_attr('make_dict_of_responses') new_lcp = module_attr('new_lcp') publish_grade = module_attr('publish_grade') rescore_problem = module_attr('rescore_problem') reset_problem = module_attr('reset_problem') save_problem = module_attr('save_problem') set_state_from_lcp = module_attr('set_state_from_lcp') should_show_submit_button = module_attr('should_show_submit_button') should_show_reset_button = module_attr('should_show_reset_button') should_show_save_button = module_attr('should_show_save_button') update_score = module_attr('update_score')
class CapaDescriptor(CapaFields, RawDescriptor): """ Module implementing problems in the LON-CAPA format, as implemented by capa.capa_problem """ module_class = CapaModule has_score = True template_dir_name = 'problem' mako_template = "widgets/problem-edit.html" js = {'coffee': [resource_string(__name__, 'js/src/problem/edit.coffee')]} js_module_name = "MarkdownEditingDescriptor" css = { 'scss': [ resource_string(__name__, 'css/editor/edit.scss'), resource_string(__name__, 'css/problem/edit.scss') ] } # Capa modules have some additional metadata: # TODO (vshnayder): do problems have any other metadata? Do they # actually use type and points? metadata_attributes = RawDescriptor.metadata_attributes + ('type', 'points') # The capa format specifies that what we call max_attempts in the code # is the attribute `attempts`. This will do that conversion metadata_translations = dict(RawDescriptor.metadata_translations) metadata_translations['attempts'] = 'max_attempts' def get_context(self): _context = RawDescriptor.get_context(self) _context.update({'markdown': self.markdown, 'enable_markdown': self.markdown is not None}) return _context # VS[compat] # TODO (cpennington): Delete this method once all fall 2012 course are being # edited in the cms @classmethod def backcompat_paths(cls, path): return [ 'problems/' + path[8:], path[8:], ] @property def non_editable_metadata_fields(self): non_editable_fields = super(CapaDescriptor, self).non_editable_metadata_fields non_editable_fields.extend([CapaDescriptor.due, CapaDescriptor.graceperiod, CapaDescriptor.force_save_button, CapaDescriptor.markdown, CapaDescriptor.text_customization]) return non_editable_fields # Proxy to CapaModule for access to any of its attributes answer_available = module_attr('answer_available') check_button_name = module_attr('check_button_name') check_problem = module_attr('check_problem') choose_new_seed = module_attr('choose_new_seed') closed = module_attr('closed') get_answer = module_attr('get_answer') get_problem = module_attr('get_problem') get_problem_html = module_attr('get_problem_html') get_state_for_lcp = module_attr('get_state_for_lcp') handle_input_ajax = module_attr('handle_input_ajax') handle_problem_html_error = module_attr('handle_problem_html_error') handle_ungraded_response = module_attr('handle_ungraded_response') is_attempted = module_attr('is_attempted') is_correct = module_attr('is_correct') is_past_due = module_attr('is_past_due') is_submitted = module_attr('is_submitted') lcp = module_attr('lcp') make_dict_of_responses = module_attr('make_dict_of_responses') new_lcp = module_attr('new_lcp') publish_grade = module_attr('publish_grade') rescore_problem = module_attr('rescore_problem') reset_problem = module_attr('reset_problem') save_problem = module_attr('save_problem') set_state_from_lcp = module_attr('set_state_from_lcp') should_show_check_button = module_attr('should_show_check_button') should_show_reset_button = module_attr('should_show_reset_button') should_show_save_button = module_attr('should_show_save_button') update_score = module_attr('update_score')