From cf1c1d539863a5bc2898a193476b8ec1564ebb8c Mon Sep 17 00:00:00 2001 From: principe Date: Mon, 9 Oct 2023 11:11:01 +0200 Subject: [PATCH] Fixed figures refs and added sections in OnlineExams --- src/Exams.jl | 28 ++++++++++++++++++---------- test/example_moodle.jl | 2 +- test/problems.jl | 5 +++-- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/Exams.jl b/src/Exams.jl index adc04d5..937d91e 100644 --- a/src/Exams.jl +++ b/src/Exams.jl @@ -385,9 +385,10 @@ struct OnlineExam <: Exam functions::Vector{Function} arguments::Vector{Vector{Tuple}} format::QuestionFormat + sections::Symbol figfiles::Vector{String} - function OnlineExam(num_permutations;languages=[ENG],headings=MoodleHeadings,name="exam",template=MoodleTemplate,format=OnlineQuestion()) - new(name,num_permutations,languages,headings,template,Vector{LatexFigure}(undef,0),Vector{Function}(undef,0),Vector{Vector{Tuple}}(undef,0),format,Vector{String}(undef,0)) + function OnlineExam(num_permutations;languages=[ENG],headings=MoodleHeadings,name="exam",template=MoodleTemplate,format=OnlineQuestion(),sections=:SingleSection) + new(name,num_permutations,languages,headings,template,Vector{LatexFigure}(undef,0),Vector{Function}(undef,0),Vector{Vector{Tuple}}(undef,0),format,sections,Vector{String}(undef,0)) end end @@ -464,8 +465,9 @@ function generate_tex_files(exam::OnlineExam) io_tex = open(filename*".tex","w"); render(io_tex,replace(exam.template,"\\end{document}"=>""),exam.headings[lang]) last_page="" - write(io_tex,begin_quiz(lang,exam.name)) + exam.sections==:SingleSection && write(io_tex,begin_quiz(lang,exam.name)) for i = 1:get_num_permutations(exam) + exam.sections==:MultipleSections && write(io_tex,begin_quiz(lang,exam.name*"_P$i")) exam.format.int_params[1] = i exam.format.int_params[2] = 0 # Loop over problems @@ -477,8 +479,9 @@ function generate_tex_files(exam::OnlineExam) write(io_tex, problem); write(io_tex, end_cloze()) end + exam.sections==:MultipleSections && write(io_tex, end_quiz()) end - write(io_tex, end_quiz()) + exam.sections==:SingleSection && write(io_tex, end_quiz()) write(io_tex, last_page) write(io_tex, latex_tail); close(io_tex); @@ -486,11 +489,14 @@ function generate_tex_files(exam::OnlineExam) io_tex = open(filename*".tex","r"); st=read(io_tex,String) st=replace(replace(st,"{ {"=>"{"),"} }"=>"}") - mf=match(r"includegraphics.*{(.*)}",st) + mf=collect(eachmatch(r"includegraphics.*{(.*)}",st)) mp=match(r"graphicspath{(.*)}",st) - push!(exam.figfiles,mf.captures...) # To delete them after compile - for s in mf.captures - run(`openssl enc -base64 -A -in $(mp.captures[1])/$s.png -out $s.enc`) + for c in mf + s=c.captures[1] + if findfirst(x->x==s,exam.figfiles) === nothing + push!(exam.figfiles,s) # Add it to the list of figures + run(`openssl enc -base64 -A -in $(mp.captures[1])/$s.png -out $s.enc`) + end end process_images = false end @@ -641,12 +647,14 @@ function format_figure!(format::InlinedFigure,str::Vector{String}) @assert length(str)>0 "Empty string vector in format_figure!(FloatingFigure,...)" w=format.width for (i,s) in enumerate(str) - if occursin("includegraphics",s) + if occursin("includegraphics",s) # t=replace(s,"\\centering"=>"\n") t=replace(s,r"\\caption{.*}\n"=>"") t=replace(t,r"\\label{.*}\n"=>"") str[i]="\n\n\\begin{center}\n"*replace(t,"width"=>"width=$w\\textwidth")*"\\end{center}\n\n" - break + # break + elseif occursin("\\ref{",s) + s=replace(s,r"[ ~]*\\ref{.*}"=>"") end end return "\n" diff --git a/test/example_moodle.jl b/test/example_moodle.jl index 9f2eedc..a702265 100644 --- a/test/example_moodle.jl +++ b/test/example_moodle.jl @@ -9,7 +9,7 @@ function generate_quiz(nump) headings[CAT]["GraphicsPath"] = "{../assets/}" headings[ENG]["GraphicsPath"] = "{../assets/}" - quiz = OnlineExam(nump,languages=[SPA],headings=headings,name="OnlineExamTest") + quiz = OnlineExam(nump,languages=[SPA],headings=headings,name="OnlineExamTest",sections=:MultipleSections) mass=[1kg,2kg]; h0=[1m,2m]; v0=[1m/s,2m/s]; diff --git a/test/problems.jl b/test/problems.jl index 4a3301f..1a8673c 100644 --- a/test/problems.jl +++ b/test/problems.jl @@ -11,12 +11,13 @@ function free_fall(mass,h0,v0,language=nothing,format=nothing) v=v0-g*t if language==SPA - statement = "Un cuerpo de masa $mass_out es lanzado hacia arriba desde una altura $h0_out a una velocidad $v0_out. La aceleración local de la gravedad es $g_out. Determinar:" + statement = "Un cuerpo de masa $mass_out es lanzado hacia arriba desde una altura $h0_out a una velocidad $v0_out como se muestra en la figura~\\ref{fig:caidalibre}. La aceleración local de la gravedad es $g_out. Determinar:" figure1 = figure("ball","fig:caidalibre","Cuerpo en caída libre"); question1 = question(format,"el tiempo que tarda en caer al suelo (en unitname),", t,"s"); question2 = question(format,"la velocidad que alcanza al tocar el suelo (en unitname),", v,"km/h"); elseif language==CAT - statement = "Un cos de massa $mass_out és llançat cap amunt des d'una alçada $h0_out a una velocitat $v0_out. L'acceleració local de la gravetat és $g_out. Determinar:" + statement = "Un cos de massa $mass_out és llançat cap amunt des d'una alçada $h0_out a una velocitat $v0_out, com es mostra a la figura~\\ref{fig:caidalibre}. L'acceleració local de la gravetat és $g_out. Determinar:" + figure1 = figure("ball","fig:caidalibre","Cos en caiguda lliure"); question1 = question(format,"el temps que triga a caure a terra (en unitname),", t,"s"); question2 = question(format,"la velocitat que arriba a tenir al tocar a terra (en unitname),", v,"km/h"); elseif language==ENG