You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/services/exercise_user_pdf_service.rb

89 lines
3.3 KiB

6 years ago
class ExerciseUserPdfService
include ExercisesHelper
include ApplicationHelper
include StudentWorksHelper
5 years ago
attr_reader :exercise, :ex_user, :request_url
6 years ago
5 years ago
def initialize(exercise, ex_user,request_url)
6 years ago
@exercise = exercise
@ex_user = ex_user
5 years ago
@request_url = request_url
6 years ago
@ex_user_user = @ex_user.user
@course = @exercise.course
end
def filename
user_course = @course.course_members.find_by(user_id:@ex_user_user.id).course_group_name
exercise_user_name = user_course + "_" + exercise.exercise_name + "_" + @ex_user_user.real_name
"#{exercise_user_name.strip}.pdf"
end
def prepare_binding
load_data
binding
end
def ex_pdf
generate_pdf
end
private
def generate_pdf
file = File.open(Rails.root.join('app/templates/exercise_export/exercise_user.html.erb'))
html = ERB.new(file.read).result(prepare_binding)
kit = PDFKit.new(html)
base_css = %w(app/templates/exercise_export/exercise_export.css)
base_css.each { |css| kit.stylesheets << Rails.root.join(css) }
# #-----正式需删掉
6 years ago
# aa = File.open(Rails.root.join("public/123.html"),"w+")
# aa.syswrite(kit.source)
# #正式需删掉-------
6 years ago
file = Tempfile.new(filename)
kit.to_pdf(file.path)
file
end
def load_data
6 years ago
@exercise_questions = exercise.exercise_questions
@exercise_ques_count = @exercise_questions.size # 全部的题目数
6 years ago
@exercise_ques_scores = @exercise_questions.pluck(:question_score).sum
#单选题的数量及分数
exercise_single_ques = @exercise_questions.find_by_custom("question_type",0)
6 years ago
@exercise_single_ques_count = exercise_single_ques.size
6 years ago
@exercise_single_ques_scores = exercise_single_ques.pluck(:question_score).sum
#多选题的数量及分数
exercise_double_ques = @exercise_questions.find_by_custom("question_type",1)
6 years ago
@exercise_double_ques_count = exercise_double_ques.size
6 years ago
@exercise_double_ques_scores = exercise_double_ques.pluck(:question_score).sum
# 判断题数量及分数
exercise_ques_judge = @exercise_questions.find_by_custom("question_type",2)
6 years ago
@exercise_ques_judge_count = exercise_ques_judge.size
6 years ago
@exercise_ques_judge_scores = exercise_ques_judge.pluck(:question_score).sum
#填空题数量及分数
exercise_ques_null = @exercise_questions.find_by_custom("question_type",3)
6 years ago
@exercise_ques_null_count = exercise_ques_null.size
6 years ago
@exercise_ques_null_scores = exercise_ques_null.pluck(:question_score).sum
#简答题数量及分数
exercise_ques_main = @exercise_questions.find_by_custom("question_type",4)
6 years ago
@exercise_ques_main_count = exercise_ques_main.size
6 years ago
@exercise_ques_main_scores = exercise_ques_main.pluck(:question_score).sum
#实训题数量及分数
exercise_ques_shixun = @exercise_questions.find_by_custom("question_type",5)
6 years ago
@exercise_ques_shixun_count = exercise_ques_shixun.size
6 years ago
@exercise_ques_shixun_scores = exercise_ques_shixun.pluck(:question_score).sum
6 years ago
@exercise_questions = @exercise_questions&.includes(:exercise_choices,:exercise_shixun_challenges,:exercise_answers,:exercise_shixun_answers,:exercise_answer_comments,:exercise_standard_answers)
6 years ago
get_each_student_exercise(exercise.id,@exercise_questions,@ex_user_user.id)
6 years ago
6 years ago
end
end