Skip to content

Commit

Permalink
Fixed figures refs and added sections in OnlineExams
Browse files Browse the repository at this point in the history
  • Loading branch information
principejavier committed Oct 9, 2023
1 parent ec4c363 commit cf1c1d5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
28 changes: 18 additions & 10 deletions src/Exams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -477,20 +479,24 @@ 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);
if process_images
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
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion test/example_moodle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
5 changes: 3 additions & 2 deletions test/problems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cf1c1d5

Please sign in to comment.