-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into i347-full-text-search
- Loading branch information
Showing
24 changed files
with
931 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# frozen_string_literal: true | ||
require 'nokogiri' | ||
|
||
module QuestionFormatter | ||
class BaseService | ||
## | ||
# Service to handle formatting questions for downloads | ||
class_attribute :output_format, default: nil | ||
attr_reader :question, :subq | ||
|
||
def initialize(question, subq = false) | ||
@question = question | ||
@subq = subq | ||
end | ||
|
||
def format_content | ||
format_by_type + divider_line | ||
end | ||
|
||
# These methods are protected instead of private so they can be called by other instances | ||
protected | ||
|
||
def essay_type | ||
format_question_header + format_essay_content | ||
end | ||
|
||
def traditional_type | ||
format_question_header + format_answers(@question.data) { |answer, index| format_traditional_answer(answer, index) } | ||
end | ||
|
||
def matching_type | ||
format_question_header + format_answers(@question.data) { |answer, index| format_matching_answer(answer, index) } | ||
end | ||
|
||
def categorization_type | ||
format_question_header + format_categories(@question.data) | ||
end | ||
|
||
def bowtie_type | ||
format_question_header + format_bowtie_sections | ||
end | ||
|
||
def stimulus_type | ||
output = @question.child_questions.map { |sub_question| format_sub_question(sub_question) } | ||
# remove extra line breaks | ||
output[-1] = output[-1].chomp if output.any? | ||
"#{format_question_header}#{output.join('')}" | ||
end | ||
|
||
def format_sub_question(sub_question) | ||
case sub_question.type | ||
when "Question::Scenario" | ||
format_scenario(sub_question) | ||
else | ||
"#{self.class.new(sub_question, true).format_by_type}\n" | ||
end | ||
end | ||
|
||
def format_by_type | ||
method = @question.class.model_exporter | ||
begin | ||
send(method) | ||
rescue | ||
"Question type: #{question_type} requires a valid export format method" | ||
end | ||
end | ||
|
||
private | ||
|
||
def divider_line | ||
raise NotImplementedError, "Subclasses must implement divider_line" | ||
end | ||
|
||
def format_scenario(question) | ||
raise NotImplementedError, "Subclasses must implement format_scenario" | ||
end | ||
|
||
def format_question_header | ||
raise NotImplementedError, "Subclasses must implement format_question_header" | ||
end | ||
|
||
def format_essay_content | ||
plain_text = format_html(@question.data['html']) | ||
"Text: #{plain_text}\n" | ||
end | ||
|
||
def format_answers(data) | ||
data.map.with_index { |answer, index| yield(answer, index) }.join('') | ||
end | ||
|
||
def format_traditional_answer(answer, index) | ||
"#{index + 1}) #{answer['correct'] ? 'Correct' : 'Incorrect'}: #{answer['answer']}\n" | ||
end | ||
|
||
def format_matching_answer(answer, index) | ||
"#{index + 1}) #{answer['answer']}\n Correct Match: #{answer['correct'].first}\n" | ||
end | ||
|
||
def format_categories(data) | ||
raise NotImplementedError, "Subclasses must implement format_categories" | ||
end | ||
|
||
def format_bowtie_sections | ||
sections = ['center', 'left', 'right'].map do |section| | ||
answers = @question.data[section]['answers'].map.with_index do |answer, index| | ||
format_traditional_answer(answer, index) | ||
end.join('') | ||
"#{section.capitalize}\n#{answers}" | ||
end | ||
sections.join("\n") | ||
end | ||
|
||
def question_type | ||
@question.class.type_name | ||
end | ||
|
||
def format_html(html) | ||
rich_text = Nokogiri::HTML(html) | ||
rich_text.css('a').each { |link| link.replace("#{link.text} (#{link['href']})") } | ||
rich_text.css('p').each { |p| p.replace("#{p.text}\n") } | ||
rich_text.css('li').each { |li| li.replace("- #{li.text}\n") } | ||
rich_text.text.strip | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
## | ||
# Service to handle formatting questions into markdown | ||
module QuestionFormatter | ||
class MarkdownService < BaseService | ||
self.output_format = 'md' | ||
|
||
private | ||
|
||
def divider_line | ||
"\n---\n\n" | ||
end | ||
|
||
def format_scenario(sub_question) | ||
"**Scenario:** #{sub_question.text}\n\n" | ||
end | ||
|
||
def format_question_header | ||
headers = case @subq | ||
when true | ||
"### Subquestion Type: #{question_type}\n**Subquestion:** #{@question.text}\n\n" | ||
else | ||
"## QUESTION TYPE: #{question_type}\n**QUESTION:** #{@question.text}\n\n" | ||
end | ||
headers | ||
end | ||
|
||
def format_essay_content | ||
plain_text = format_html(@question.data['html']) | ||
"**Text:** #{plain_text}\n" | ||
end | ||
|
||
def format_categories(data) | ||
data.map do |category| | ||
items = category['correct'].map.with_index { |item, index| "#{index + 1}) #{item}\n" }.join('') | ||
"**Category:** #{category['answer']}\n#{items}" | ||
end.join("\n") | ||
end | ||
end | ||
end |
Oops, something went wrong.