2020-12-07 01:57:34 +08:00
|
|
|
# frozen_string_literal: true
|
2021-09-23 00:25:11 +08:00
|
|
|
|
2020-12-07 01:57:34 +08:00
|
|
|
#
|
|
|
|
# Copyright (C) 2020 - present Instructure, Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Canvas.
|
|
|
|
#
|
|
|
|
# Canvas is free software: you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License along
|
|
|
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
module QuizMathDataFixup
|
|
|
|
def fixup_quiz_questions_with_bad_math(quiz_or_bank, check_date: nil, question_bank: false)
|
|
|
|
changed = false
|
2021-11-11 23:30:33 +08:00
|
|
|
questions = if question_bank
|
|
|
|
quiz_or_bank.assessment_questions
|
|
|
|
else
|
|
|
|
quiz_or_bank.quiz_questions
|
|
|
|
end
|
2020-12-08 06:11:54 +08:00
|
|
|
questions = questions.where('updated_at>?', check_date) if check_date
|
2020-12-07 01:57:34 +08:00
|
|
|
questions.find_each do |quiz_question|
|
2021-11-11 04:01:30 +08:00
|
|
|
old_data = quiz_question.question_data.to_hash
|
|
|
|
new_data = fixup_question_data(quiz_question.question_data.to_hash.symbolize_keys)
|
|
|
|
quiz_question.write_attribute(:question_data, new_data) if new_data != old_data
|
|
|
|
if quiz_question.changed?
|
|
|
|
stat = question_bank ? 'updated_math_qb_question' : 'updated_math_question'
|
|
|
|
InstStatsd::Statsd.increment(stat)
|
|
|
|
changed = true
|
|
|
|
quiz_question.save!
|
2020-12-07 01:57:34 +08:00
|
|
|
end
|
2021-11-11 04:01:30 +08:00
|
|
|
rescue => e
|
|
|
|
Canvas::Errors.capture(e)
|
2020-12-07 01:57:34 +08:00
|
|
|
end
|
|
|
|
qstat = question_bank ? 'updated_math_question_bank' : 'updated_math_quiz'
|
|
|
|
InstStatsd::Statsd.increment(qstat) if changed
|
|
|
|
quiz_or_bank
|
|
|
|
end
|
|
|
|
|
|
|
|
def fixup_submission_questions_with_bad_math(submission)
|
|
|
|
submission.questions&.each_with_index do |question, index|
|
2021-11-11 04:01:30 +08:00
|
|
|
data = fixup_question_data(question)
|
|
|
|
submission.questions[index] = data
|
|
|
|
rescue => e
|
|
|
|
Canvas::Errors.capture(e)
|
2020-12-07 01:57:34 +08:00
|
|
|
end
|
|
|
|
begin
|
|
|
|
submission.save! if submission.changed?
|
|
|
|
rescue => e
|
|
|
|
Canvas::Errors.capture(e)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-07 06:38:48 +08:00
|
|
|
def fixup_question_data(data)
|
2020-12-08 06:11:54 +08:00
|
|
|
%i[neutral_comments_html correct_comments_html incorrect_comments_html].each do |key|
|
|
|
|
data[key] = fixup_html(data[key]) if data[key].present?
|
2020-12-07 09:10:17 +08:00
|
|
|
end
|
2020-12-10 08:04:08 +08:00
|
|
|
|
2020-12-09 08:01:55 +08:00
|
|
|
data[:question_text] = fixup_html(data[:question_text]) if data[:question_text].present?
|
|
|
|
|
2020-12-10 08:04:08 +08:00
|
|
|
data[:answers].map(&:symbolize_keys).each_with_index do |answer, index|
|
2020-12-08 06:11:54 +08:00
|
|
|
%i[html comments_html].each do |key|
|
2020-12-09 05:48:49 +08:00
|
|
|
# if there's html, the text field is used as the title attribute/tooltip
|
|
|
|
# clear it out if we updated the html because it's probably hosed.
|
|
|
|
if answer[key].present?
|
2020-12-08 06:11:54 +08:00
|
|
|
answer[key] = fixup_html(answer[key])
|
|
|
|
|
|
|
|
text_key = key.to_s.sub(/html/, 'text')
|
|
|
|
answer[text_key] = '' if answer[text_key].present?
|
|
|
|
end
|
|
|
|
end
|
2020-12-07 09:10:17 +08:00
|
|
|
data[:answers][index] = answer
|
2020-12-07 01:57:34 +08:00
|
|
|
end
|
2020-12-07 06:38:48 +08:00
|
|
|
data
|
2020-12-07 01:57:34 +08:00
|
|
|
end
|
|
|
|
|
2020-12-09 08:01:55 +08:00
|
|
|
def fixup_html(html_str)
|
|
|
|
return html_str unless html_str
|
2021-09-23 00:25:11 +08:00
|
|
|
|
2021-01-12 02:24:13 +08:00
|
|
|
html = Nokogiri::HTML5.fragment(html_str)
|
2020-12-07 09:10:17 +08:00
|
|
|
if html.children.length == 1 && html.children[0].node_type == Nokogiri::XML::Node::TEXT_NODE
|
2020-12-08 06:11:54 +08:00
|
|
|
# look for an equation_images URL in the text and extract the latex
|
2021-11-11 02:56:52 +08:00
|
|
|
m = %r{equation_images/([^\s]+)}.match(html.content)
|
2020-12-07 09:10:17 +08:00
|
|
|
if m && m[1]
|
|
|
|
code = URI.unescape(URI.unescape(m[1]))
|
|
|
|
html =
|
2020-12-08 06:11:54 +08:00
|
|
|
"<img class='equation_image' src='/equation_images/#{m[1]}' alt='LaTeX: #{code}' title='#{
|
2020-12-07 09:10:17 +08:00
|
|
|
code
|
2020-12-08 06:11:54 +08:00
|
|
|
}' data-equation-content='#{code}'/>"
|
|
|
|
else
|
|
|
|
# look for \(inline latex\) and extract it
|
|
|
|
m = html.content.match(/\\\(((?!\\\)).+)\\\)/)
|
|
|
|
if m && m[1]
|
|
|
|
code = URI.unescape(URI.unescape(m[1]))
|
|
|
|
html =
|
|
|
|
"<img class='equation_image' src='/equation_images/#{m[1]}' alt='LaTeX: #{
|
|
|
|
code
|
|
|
|
}' title='#{code}' data-equation-content='#{code}'/>"
|
|
|
|
end
|
2020-12-07 09:10:17 +08:00
|
|
|
end
|
2020-12-08 06:11:54 +08:00
|
|
|
html.search('[id^="MathJax"]').each(&:remove)
|
|
|
|
return html.to_s
|
2020-12-07 09:10:17 +08:00
|
|
|
end
|
|
|
|
html.search('.math_equation_latex').each do |latex|
|
|
|
|
# find MathJax generated children, extract the eq's mathml
|
|
|
|
# incase we need it later, then remove them
|
|
|
|
mjnodes =
|
|
|
|
html.search('[class^="MathJax"]')
|
2020-12-07 01:57:34 +08:00
|
|
|
|
2021-11-19 00:01:01 +08:00
|
|
|
unless mjnodes.empty?
|
2020-12-07 09:10:17 +08:00
|
|
|
n = mjnodes.filter('[data-mathml]')[0]
|
|
|
|
mml = n.attribute('data-mathml') if n
|
|
|
|
mjnodes.each(&:remove)
|
|
|
|
end
|
2021-11-19 00:01:01 +08:00
|
|
|
if !latex.content.empty?
|
2020-12-11 04:31:40 +08:00
|
|
|
if latex.content !~ /^(:?\\\(|\$\$).+(:?\\\)|\$\$)$/ && latex.content !~ /[\\+-^=<>]|{.+}/
|
2020-12-09 08:01:55 +08:00
|
|
|
# the content is not delimineted latex,
|
2020-12-11 04:31:40 +08:00
|
|
|
# and doesn't even _look like_ latex
|
|
|
|
# remove math_equation_latex from the class then leave it alone
|
|
|
|
|
|
|
|
latex.attribute('class').value =
|
|
|
|
latex.attribute('class').value.sub('math_equation_latex', '').strip
|
2020-12-09 08:01:55 +08:00
|
|
|
else
|
|
|
|
code = latex.content.gsub(/(^\\\(|\\\)$)/, '')
|
|
|
|
escaped = URI.escape(URI.escape(code))
|
|
|
|
latex.replace(
|
|
|
|
"<img class='equation_image' src='/equation_images/#{escaped}' alt='LaTeX: #{
|
|
|
|
code
|
|
|
|
}' title='#{code}' data-equation-content='#{code}'/>"
|
|
|
|
)
|
|
|
|
end
|
2020-12-07 09:10:17 +08:00
|
|
|
elsif mml
|
|
|
|
latex.replace(
|
2020-12-11 04:31:40 +08:00
|
|
|
"<span class='math_equation_mml'><math xmlns='http://www.w3.org/1998/Math/MathML'>#{
|
|
|
|
mml
|
|
|
|
}</math></span>"
|
2020-12-07 09:10:17 +08:00
|
|
|
)
|
2020-12-07 01:57:34 +08:00
|
|
|
end
|
|
|
|
end
|
2020-12-07 09:10:17 +08:00
|
|
|
|
2020-12-08 06:11:54 +08:00
|
|
|
html.search('[id^="MathJax"]').each(&:remove)
|
2020-12-07 09:10:17 +08:00
|
|
|
html.search('span.hidden-readable').each(&:remove)
|
|
|
|
|
2021-11-19 00:01:01 +08:00
|
|
|
return html_str if html.content.empty? && html.search('img.equation_image').empty?
|
2021-09-23 00:25:11 +08:00
|
|
|
|
2020-12-07 09:10:17 +08:00
|
|
|
html.to_s
|
2020-12-07 01:57:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_or_fix_quizzes(batch_of_ids)
|
|
|
|
Quizzes::Quiz.where(id: batch_of_ids).find_each { |q| fixup_quiz_questions_with_bad_math(q) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_or_fix_question_banks(batch_of_ids)
|
2020-12-08 06:11:54 +08:00
|
|
|
AssessmentQuestionBank.where(id: batch_of_ids).find_each do |q|
|
|
|
|
fixup_quiz_questions_with_bad_math(q, question_bank: true)
|
|
|
|
end
|
2020-12-07 01:57:34 +08:00
|
|
|
end
|
|
|
|
end
|