def create_question(self, title_text=None, points=True): env = self.state.document.settings.env env.question_count += 1 # Create base element and data. node = aplus_nodes.html(u'div', { u'class': u' '.join(self.get_classes()), }) data = { u'type': self.grader_field_type(), u'extra_info': self.get_extra_info(), } key = self.options.get('key', None) if key: data[u'key'] = yaml_writer.ensure_unicode(key) # Add title. if not title_text is None: data[u'title'] = title_text elif env.questionnaire_is_feedback: data[u'title'] = title_text = u'' else: data[u'title|i18n'] = translations.opt('question', postfix=u" {:d}".format( env.question_count)) title_text = u"{} {:d}".format(translations.get(env, 'question'), env.question_count) if title_text: title = aplus_nodes.html(u'label', {}) title.append(nodes.Text(title_text)) node.append(title) # Add configuration. if points and len(self.arguments) > 0: data[u'points'] = int(self.arguments[0]) if 'required' in self.options: data[u'required'] = True node.set_yaml(data, u'question') return env, node, data
def generate_options(self, env, node): options = [] for i, key in enumerate([ 'agreement4', 'agreement3', 'agreement2', 'agreement1', 'agreement0' ]): options.append({ u'value': 4 - i, u'label|i18n': translations.opt(key), }) choice = aplus_nodes.html(u'div', {u'class': u'radio'}) label = aplus_nodes.html(u'label', {}) label.append( aplus_nodes.html( u'input', { u'type': u'radio', u'name': u'field_{:d}'.format(env.question_count - 1), u'value': 4 - i, })) label.append(nodes.Text(translations.get(env, key))) choice.append(label) node.append(choice) return options
def run(self): self.assert_has_content() key, difficulty, points = self.extract_exercise_arguments() # Parse options. classes = [u'exercise'] is_feedback = False if 'chapter-feedback' in self.options: classes.append(u'chapter-feedback') is_feedback = True if 'weekly-feedback' in self.options: classes.append(u'weekly-feedback') is_feedback = True if 'appendix-feedback' in self.options: classes.append(u'appendix-feedback') is_feedback = True if 'course-feedback' in self.options: classes.append(u'course-feedback-questionnaire') is_feedback = True if 'feedback' in self.options: is_feedback = True if is_feedback: key = u'feedback' category = u'feedback' classes.append(u'feedback') else: category = u'questionnaire' if difficulty: classes.append(u'difficulty-' + difficulty) env = self.state.document.settings.env name = u"{}_{}".format(env.docname.replace(u'/', u'_'), key) override = env.config.override env.questionnaire_is_feedback = is_feedback env.question_count = 0 # Create document elements. node = aplus_nodes.html( u'div', { u'class': u' '.join(classes), u'data-aplus-exercise': u'yes', u'data-aplus-quiz': u'yes', }) form = aplus_nodes.html(u'form', { u'action': key, u'method': u'post', }) nested_parse_with_titles(self.state, self.content, form) submit = aplus_nodes.html(u'input', { u'type': u'submit', u'value': translations.get(env, u'submit'), u'class': u'btn btn-primary', }, skip_html=True) form.append(submit) node.append(form) # Write configuration file. data = { u'key': name, u'category': category, u'max_points': points, u'difficulty': difficulty or '', u'max_submissions': self.options.get( 'submissions', 0 if is_feedback else env.config.questionnaire_default_submissions), u'min_group_size': 1 if is_feedback else env.config.default_min_group_size, u'max_group_size': 1 if is_feedback else env.config.default_max_group_size, u'points_to_pass': self.options.get('points-to-pass', 0), u'feedback': is_feedback, u'view_type': u'access.types.stdsync.createForm', u'title|i18n': translations.opt('feedback') if is_feedback else translations.opt( 'exercise', postfix=u" {}".format(key)), u'fieldgroups': [{ u'title': '', u'fields': (u'#!children', None), }], } if not 'no-override' in self.options and category in override: data.update(override[category]) if 'url' in data: data['url'] = data['url'].format(key=name) if "pick_randomly" in self.options: pick_randomly = self.options.get('pick_randomly', 0) if pick_randomly < 1: raise SphinxError( u'Number of fields to sample randomly should greater than zero.' ) data[u'fieldgroups'][0]['pick_randomly'] = pick_randomly form.write_yaml(env, name, data, 'exercise') return [node]