Skip to content

Commit

Permalink
Restructure code and add markdown downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
LaRita Robinson committed Feb 25, 2025
1 parent 64b697d commit 0f35dba
Show file tree
Hide file tree
Showing 22 changed files with 588 additions and 172 deletions.
31 changes: 26 additions & 5 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def index
# conversations with the client, we're looking to only export classic (as you can migrate a
# classic question to new format). This filename is another "helpful clue" and introduces
# later considerations for what the file format might be.
filename = "questions-#{now.strftime('%Y-%m-%d_%H:%M:%S:%L')}.classic-question-canvas.qti.xml"
filename = "#{export_filename(now)}.classic-question-canvas.qti.xml"
@questions = Question.filter(**filter_values)

if any_question_has_images?
Expand All @@ -35,14 +35,35 @@ def index
end
end

def text_download
questions = Question.where(id: Bookmark.select(:question_id))
content = questions.map { |question| PlainTextFormatterService.new(question).format }.join('')
send_data content, filename: 'questions.txt', type: 'text/plain'
# download bookmarked questions
def download
@questions = Question.where(id: Bookmark.select(:question_id))
case params[:format]
when 'md'
md_download
when 'txt'
text_download
else
redirect_to authenticated_root_path, alert: t('.alert')
end
end

private

def export_filename(now = Time.current)
"questions-#{now.strftime('%Y-%m-%d_%H:%M:%S:%L')}"
end

def text_download
content = @questions.map { |question| QuestionFormatter::PlainTextService.new(question).format_content }.join('')
send_data content, filename: "#{export_filename}.txt", type: 'text/plain'
end

def md_download
content = @questions.map { |question| QuestionFormatter::MarkdownService.new(question).format_content }.join('')
send_data content, filename: "#{export_filename}.md", type: 'text/plain'
end

def any_question_has_images?
@questions.any? { |question| question.images.any? }
end
Expand Down
10 changes: 9 additions & 1 deletion app/javascript/components/ui/Search/SearchBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,21 @@ const SearchBar = (props) => {
View Bookmarks
</a>
<a
href={'/questions/text_download?format=txt'}
href={'/questions/download?format=txt'}
className={`btn btn-primary p-2 m-2 ${!hasBookmarks ? 'disabled' : ''}`}
role='button'
aria-disabled={!hasBookmarks}
>
Export as Plain Text
</a>
<a
href={'/questions/download?format=md'}
className={`btn btn-primary p-2 m-2 ${!hasBookmarks ? 'disabled' : ''}`}
role='button'
aria-disabled={!hasBookmarks}
>
Export as Markdown
</a>
<a
href={`/.xml?${bookmarkedQuestionIds.map(id => `bookmarked_question_ids[]=${encodeURIComponent(id)}`).join('&')}`}
className={`btn btn-primary p-2 m-2 ${!hasBookmarks ? 'disabled' : ''}`}
Expand Down
2 changes: 1 addition & 1 deletion app/models/question/bow_tie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# => true
class Question::BowTie < Question
self.type_name = "Bow Tie"
self.model_exporter = 'bow_tie'
self.model_exporter = 'bowtie_type'

# NOTE: We're not storing this in a JSONB data type, but instead favoring a text field. The need
# for the data to be used in the application, beyond export of data, is minimal.
Expand Down
2 changes: 1 addition & 1 deletion app/models/question/categorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Question::Categorization < Question
include MatchingQuestionBehavior

self.type_name = "Categorization"
self.model_exporter = 'categorization'
self.model_exporter = 'categorization_type'
self.export_as_xml = true
self.choice_cardinality_is_multiple = true

Expand Down
2 changes: 1 addition & 1 deletion app/models/question/drag_and_drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# data: [{ answer: "Aardvark", correct: true }, { answer: "Blue", correct: false }, { answer: "Yellow", correct:false }, { answer: "Cat", correct: true }])
class Question::DragAndDrop < Question
self.type_name = "Drag and Drop"
self.model_exporter = 'drag_and_drop'
self.model_exporter = 'traditional_type'

##
# Represents the mapping process of a CSV Row to the underlying {Question::DragAndDrop}.
Expand Down
2 changes: 1 addition & 1 deletion app/models/question/essay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Question::Essay < Question
include MarkdownQuestionBehavior

self.type_name = "Essay"
self.model_exporter = 'essay'
self.model_exporter = 'essay_type'
self.export_as_xml = true

class ImportCsvRow < MarkdownQuestionBehavior::ImportCsvRow
Expand Down
2 changes: 1 addition & 1 deletion app/models/question/matching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Question::Matching < Question
include MatchingQuestionBehavior

self.type_name = "Matching"
self.model_exporter = 'matching'
self.model_exporter = 'matching_type'
self.export_as_xml = true
self.choice_cardinality_is_multiple = false

Expand Down
2 changes: 1 addition & 1 deletion app/models/question/select_all_that_apply.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# duplication than more complicated inheritance.
class Question::SelectAllThatApply < Question
self.type_name = "Select All That Apply"
self.model_exporter = 'select_all_that_apply'
self.model_exporter = 'traditional_type'

##
# Represents the mapping process of a CSV Row to the underlying {Question::SelectAllThatApply}.
Expand Down
2 changes: 1 addition & 1 deletion app/models/question/stimulus_case_study.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Question::StimulusCaseStudy < Question
self.type_label = "Case Study"
self.type_name = "Stimulus Case Study"
self.model_exporter = 'stimulus_case_study'
self.model_exporter = 'stimulus_type'
self.has_parts = true

has_many :as_parent_question_aggregations,
Expand Down
2 changes: 1 addition & 1 deletion app/models/question/traditional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Question::Traditional < Question
#
# @see https://github.com/notch8/viva/issues/261
self.type_name = "Multiple Choice"
self.model_exporter = 'multiple_choice'
self.model_exporter = 'traditional_type'

##
# Represents the mapping process of a CSV Row to the underlying {Question::Traditional}.
Expand Down
2 changes: 1 addition & 1 deletion app/models/question/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Question::Upload < Question
include MarkdownQuestionBehavior

self.type_name = "Upload"
self.model_exporter = 'upload'
self.model_exporter = 'essay_type'
self.export_as_xml = true

class ImportCsvRow < MarkdownQuestionBehavior::ImportCsvRow
Expand Down
126 changes: 0 additions & 126 deletions app/services/base_formatter_service.rb

This file was deleted.

17 changes: 0 additions & 17 deletions app/services/plain_text_formatter_service.rb

This file was deleted.

Loading

0 comments on commit 0f35dba

Please sign in to comment.