-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmoodle.sty
More file actions
5305 lines (5148 loc) · 213 KB
/
moodle.sty
File metadata and controls
5305 lines (5148 loc) · 213 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%
%% This is file `moodle.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% moodle.dtx (with options: `package')
%%
%% This is a generated file.
%%
%% Copyright 2016 by Anders O.F. Hendrickson (anders.o.f.hendrickson AT gmail DOT com)
%% and 2019-2021 by Matthieu Guerquin-Kern (guerquin-kern AT crans DOT org).
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%% http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{moodle}
[2023/01/28 v1.0 Moodle quiz XML generation]
%% \begin{figure}[bp]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newif\ifmoodle@draftmode
\newif\ifmoodle@handout
\newif\ifmoodle@stampmode
\newif\ifmoodle@tikz
\newif\ifmoodle@tikzloaded
\newif\ifmoodle@svg
\newif\ifmoodle@section
\newif\ifmoodle@subsection
\newif\ifmoodle@numbered
\newif\ifmoodle@international
\newif\ifmoodle@feedbackLeft% contribution of Juergen Vollmer, 2021-03-05
\newif\ifmoodle@samepage% contribution of Romano Giannetti, 2021-03-07
\newif\ifmoodle@pluginfile%
%% Moodle version
\RequirePackage{xkeyval}%For key-handling
\newcount\moodle@LMSmajor% major version number of the LMS targeted
\newcount\moodle@LMSminor% minor version number of the LMS targeted
%%DECLARATION OF OPTIONS
\DeclareOptionX{draft}{\moodle@draftmodetrue}%
\DeclareOptionX{handout}{\moodle@handouttrue}%
\DeclareOptionX{final}{\moodle@draftmodefalse}%
\DeclareOptionX{nostamp}{\moodle@stampmodefalse}%
\DeclareOptionX{tikz}{\moodle@tikztrue}%
\DeclareOptionX{svg}{\moodle@svgtrue}%
\DeclareOptionX{section}{\moodle@sectiontrue\moodle@numberedtrue}%
\DeclareOptionX{section*}{\moodle@sectiontrue\moodle@numberedfalse}%
\DeclareOptionX{subsection}{\moodle@sectionfalse\moodle@numberedtrue}%
\DeclareOptionX{subsection*}{\moodle@sectionfalse\moodle@numberedfalse}%
\DeclareOptionX{feedbackleft}{\moodle@feedbackLefttrue}% contribution of Juergen Vollmer, 2021-03-05
\DeclareOptionX{feedbackright}{\moodle@feedbackLeftfalse}% default
\DeclareOptionX{samepage}{\moodle@samepagetrue}% contribution of Romano Giannetti, 2021-03-07
\DeclareOptionX{LMS}[0.0]{%
\filename@parse{#1}%
\moodle@LMSmajor=\filename@base
\moodle@LMSminor=\filename@ext
}%
\DeclareOptionX{pluginfile}{\moodle@pluginfiletrue}%
\moodle@draftmodefalse
\moodle@handoutfalse
\moodle@stampmodetrue
\moodle@tikzfalse
\moodle@tikzloadedfalse
\moodle@svgfalse
\moodle@subsectiontrue
\moodle@numberedfalse
\moodle@feedbackLeftfalse% contribution of Juergen Vollmer, 2021-03-05
\moodle@samepagefalse% contribution of Romano Giannetti, 2021-03-07
\moodle@pluginfilefalse
\ProcessOptionsX*% the star allows to inherit 'draft' and 'final' from the class
\ifmoodle@samepage
\def\moodle@begin@samepage{\minipage[t]{\linewidth}}
\def\moodle@end@samepage{\endminipage\vskip 0pt plus 50pt\relax}
\else
% defining to relax is safer versus adding spurious spaces
\def\moodle@begin@samepage{\relax}
\def\moodle@end@samepage{\relax}
\fi
\RequirePackage{environ} %To be able to take environment body as a macro argument
\RequirePackage{amssymb} %For \checkmark symbol
\@ifpackageloaded{iftex}{}{\RequirePackage{iftex}}
\@ifundefined{ifeTeX}{% version of 'iftex' prior to November 2019.
\RequirePackage{etex}{}
}{
\ifeTeX\else
\RequirePackage{etex}{} % Recent (2015+) TeX engines should be e-TeX.
%This is needed for expansion control, detokenization, etc.
\fi
}
\RequirePackage{etoolbox}%List management
\RequirePackage{xpatch} %To patch commands easily in \HTML\ mode
\RequirePackage{array} %For formatting tables in the LaTeX mode of Clozes
\RequirePackage{ifplatform} % To choose Ghostscript commands
\@ifundefined{ifpdf}{\RequirePackage{ifpdf}}{}% old iftex would not define the conditional
\RequirePackage{shellesc} %Luatex-compatible way of getting system access
\RequirePackage{readprov} %To gather information on the package (version, date, ...)
\RequirePackage{fancybox} %For fancy LaTeX tags
\RequirePackage{getitems} %To gather the header and items
\ifmoodle@handout
\RequirePackage[seed=42]{randomlist} %To randomize answers in matching questions
\fi
\ifmoodle@svg
\RequirePackage{graphicx} %To include graphics
\fi
\GetFileInfo{moodle.sty}%
\edef\moodledate{\filedate}%
\edef\moodleversion{\fileversion}%
\let\xa=\expandafter
\def\@star{*}%
\def\@hundred{100}%
\def\@fifty{50}%
\def\@moodle@empty{}%
\def\@relax{\relax}%
\def\@moodle@par{\par}%
\@ifundefined{ifTUTeX}{
\newif\ifTUTeX % a new conditional starts as false
\ifnum 0\ifXeTeX 1\fi\ifLuaTeX 1\fi>0\relax
\TUTeXtrue
\fi
}{}%
\newif\ifpdfoutput % a new conditional starts as false
\ifnum 0\ifTUTeX 1\fi\ifPDFTeX\ifpdf 1\fi\fi>0\relax
\pdfoutputtrue
\fi
\def\jobnamewithsuffixtomacro#1#2{%
\filenamewithsuffixtomacro{#1}{\jobname}{#2}%
}
\def\@jn@quote{"}%
\def\filenamewithsuffixtomacro#1#2#3{%
% #1 = macro to create
% #2 = filename to add suffix to
% #3 = suffix to add
\edef\jn@suffix{#3}%
\def\jn@macro{#1}%
\xa\testforquote#2\@jn@rdelim
}
\def\testforquote#1#2\@jn@rdelim{%
\def\jn@test@i{#1}%
\ifx\jn@test@i\@jn@quote
% Involves quotes
\edef\jn@next{"\jn@stripquotes#1#2\jn@suffix"}%
\else
\edef\jn@next{#1#2\jn@suffix}%
\fi
\xa\xdef\jn@macro{\jn@next}%
}
\def\jn@stripquotes"#1"{#1}%
\jobnamewithsuffixtomacro{\outputfilename}{-moodle.xml}
\newwrite\moodle@outfile
\def\openmoodleout{%
\immediate\openout\moodle@outfile=\outputfilename\relax
\ifPDFTeX % latin1-based engines (pdflatex or latex)
\writetomoodle{<?xml version="1.0" encoding="iso-8859-1"?>}%
\else
\ifTUTeX % UTF8-based engines (XeTeX or LuaTeX)
\writetomoodle{<?xml version="1.0" encoding="UTF-8"?>}%
\else % what shall we do?
\writetomoodle{<?xml version="1.0" encoding="UTF-8"?>}%
%\stop
\fi
\fi
\ifmoodle@stampmode
\def\moodle@stamp{This is a question bank made for Moodle }
\ifnum\the\moodle@LMSmajor\the\moodle@LMSminor=\z@\else
\g@addto@macro{\moodle@stamp}{\the\moodle@LMSmajor.\the\moodle@LMSminor.\space}
\fi
\writetomoodle{<!-- \moodle@stamp -->}%
\newcount\hour\hour=\time
\divide\hour by 60\relax
\newcount\minute\minute=\hour
\multiply\minute by -60\relax
\advance\minute by \time\relax
\def\moodle@stamp{It was generated on \the\year-\two@digits\month-\two@digits\day\space \the\hour:\the\minute}
\ifPDFTeX % pdflatex or latex
\ifpdf % pdflatex
\g@addto@macro{\moodle@stamp}{ by pdfLaTeX }%
\else % latex
\g@addto@macro{\moodle@stamp}{ by LaTeX }%
\fi
\else
\ifXeTeX % xetex
\g@addto@macro{\moodle@stamp}{ by XeLaTeX }%
\else
\ifLuaTeX % luatex
\g@addto@macro{\moodle@stamp}{ by LuaLaTeX }%
\else
\g@addto@macro{\moodle@stamp}{ a TeX engine }%
\fi
\fi
\fi
\writetomoodle{<!-- \moodle@stamp running -->}%
\def\moodle@stamp{on \platformname}%
\g@addto@macro{\moodle@stamp}{ with the package moodle \moodleversion\space (\moodledate) }%
\writetomoodle{<!-- \moodle@stamp -->}%
\fi
\immediate\write\moodle@outfile{}%
\writetomoodle{<quiz>}%
}%
\def\closemoodleout{%
\writetomoodle{ }%
\writetomoodle{</quiz>}%
\immediate\closeout\moodle@outfile
}%
\newcount\moodle@indentnum
\def\calculateindent#1{%
\bgroup
\moodle@indentnum=\number#1\relax
\gdef\moodle@indent{}%
\calculateindent@int
\egroup
}%
\def\calculateindent@int{%
\ifnum\moodle@indentnum>0\relax
\g@addto@macro{\moodle@indent}{\otherspace}%
\advance\moodle@indentnum by -1\relax
\expandafter
\calculateindent@int
\fi
}%
\newcommand\writetomoodle[2][0]{%
\edef\test@ii{#2}%
\ifnum#1=0\relax
\immediate\write\moodle@outfile{\test@ii}%
\else
\calculateindent{#1}%
\immediate\write\moodle@outfile{\moodle@indent\trim@pre@space{\test@ii}}%
\fi
}%
{\catcode`\$=12\catcode`\ =12% in this context we cannot indent with spaces...
\gdef\moodle@write@category@xml#1{%
\@moodle@ifgeneratexml{%
\writetomoodle{ }%
\writetomoodle{<question type="category">}%
\writetomoodle{ <category>}%
\writetomoodle{ <text>$course$/top/#1</text>}%
\writetomoodle{ </category>}%
\writetomoodle{</question>}%
\writetomoodle{ }%
}{}%
}}%
\newcommand*\@enumeratename{enumerate}%
\newdimen\moodle@totalmarks
\gdef\moodle@currentcategory{}%
\newenvironment{quiz}[2][]{%
\setkeys{moodle}{#1}%
\gdef\setcategory##1{%
% At first call (end of \begin{quiz}) enumerate is not started yet
\ifx\@currenvir\@enumeratename
% In case no question is defined between two calls of \setcategory
\def\@noitemerr{}%\@latex@warning{Empty question list}
\end{enumerate}%
\fi
\gdef\moodle@currentcategory{##1}%
\moodle@write@category@xml{##1}%
\ifmoodle@section
\ifmoodle@numbered
\section{##1}%
\else
\section*{##1}%
\fi
\else
\ifmoodle@numbered
\subsection{##1}%
\else
\subsection*{##1}%
\fi
\fi
\begin{enumerate}\renewcommand\labelenumi{(\theenumi)}%
}%
\gdef\setsubcategory##1{%
\def\@noitemerr{}%\@latex@warning{Empty question list}
\end{enumerate}%
\moodle@write@category@xml{\moodle@currentcategory/##1}%
\ifmoodle@section
\ifmoodle@numbered
\subsection{##1}%
\else
\subsection*{##1}%
\fi
\else
\ifmoodle@numbered
\subsubsection{##1}%
\else
\subsubsection*{##1}%
\fi
\fi
\begin{enumerate}\renewcommand\labelenumi{(\theenumi)}%
}%
\setcategory{#2}%
\moodle@totalmarks=0pt%
}{%
\end{enumerate}%
\emph{\GetTranslation{Total of marks}: \strip@pt\moodle@totalmarks}%
\let\setcategory\relax
\let\setsubcategory\relax
}%
\def\passvalueaftergroup#1{%
\xa\xa\xa\gdef\xa\xa\csname moodle@remember@\string#1\endcsname\xa{\xa\def\xa#1\xa{#1}}%
\xa\aftergroup\csname moodle@remember@\string#1\endcsname
}
\long\def\@moodle@ifgeneratexml#1#2{%
% If we are generating \XML, do #1; otherwise do #2.
\tikzifexternalizing{%
% This run of LaTeX is currently ONLY generating a TikZ image
% to be saved in an external file. We do NOT want to waste time
% generating \XML, and moreover trying to do so would cause errors
% because of file dependencies.
#2%
}{%
\ifmoodle@draftmode
#2%
\else
#1%
\fi
}%
}
\AfterEndPreamble{
\@moodle@ifgeneratexml{%
\openmoodleout%
}{}%
}
\AtEndDocument{
\@moodle@ifgeneratexml{%
\closemoodleout%
}{}%
}
\def\moodleset#1{\setkeys{moodle}{#1}}%
\def\generate@moodle@write@code{%
\@ifnextchar*\generate@moodle@write@data\generate@moodle@write@html
}%
\def\generate@moodle@write@html#1<#2>#3{%
% #1 = NAME for \moodle@writeNAME
% #2 = \HTML\ tag
% #3 = what, exactly, to write
\xa\gdef\csname moodle@write#1\endcsname{%
\xa\def\xa\test@iii\xa{#3}%
\ifx\test@iii\@moodle@empty
\writetomoodle[2]{ <#2 format="html"><text/></#2>}%
\else
\xa\converttohtmlmacro\xa\moodle@htmltowrite\xa{#3}%
\writetomoodle[2]{ <#2 format="html">}%
\writetomoodle[4]{ <text><![CDATA[<p>\moodle@htmltowrite</p>]]></text>}%
\ifmoodle@pluginfile
\writetomoodle[0]{\htmlize@embeddedfiletags}%
\fi
\writetomoodle[2]{ </#2>}%
\fi
}%
}%
\def\generate@moodle@write@html@noptag#1<#2>#3{%
% No <P>..</P> introduced
% #1 = NAME for \moodle@writeNAME
% #2 = \HTML\ tag
% #3 = what, exactly, to write
\xa\gdef\csname moodle@write#1\endcsname{%
\xa\def\xa\test@iii\xa{#3}%
\ifx\test@iii\@moodle@empty
\writetomoodle[2]{ <#2 format="html"><text/></#2>}%
\else
\xa\converttohtmlmacro\xa\moodle@htmltowrite\xa{#3}%
\writetomoodle[2]{ <#2 format="html">}%
\writetomoodle[4]{ <text><![CDATA[\moodle@htmltowrite]]></text>}%
\ifmoodle@pluginfile
\writetomoodle[0]{\htmlize@embeddedfiletags}%
\fi
\writetomoodle[2]{ </#2>}%
\fi
}%
}%
\def\generate@moodle@write@data*#1<#2>#3{%
% #1 = NAME for \moodle@writeNAME
% #2 = \HTML\ tag
% #3 = what, exactly, to write
\xa\gdef\csname moodle@write#1\endcsname{%
\writetomoodle[2]{ <#2>#3</#2>}%
}%
}%
\def\moodle@writetags{%
\xa\xdef\xa\test@iii\xa{\moodle@tags}%
\ifx\test@iii\@moodle@empty\relax\else
\writetomoodle[2]{ <tags>}%
\renewcommand*{\do}[1]{%
\def\moodle@tagtext{##1}%
\xa\converttohtmlmacro\xa\moodle@htmltowrite\xa{\moodle@tagtext}%
\writetomoodle[4]{ <tag><text><![CDATA[\moodle@htmltowrite]]></text></tag>}%
}
\xa\docsvlist\xa{\moodle@tags}%
\writetomoodle[2]{ </tags>}%
\fi
}%
\newif\ifmoodle@firsttag
\moodle@firsttagtrue
\def\moodle@latex@writetags{%
\xa\xdef\xa\test@iii\xa{\moodle@tags}%
\ifx\test@iii\@moodle@empty\relax\else
\hfill \GetTranslation{tags}: %
\renewcommand*{\do}[1]{\ifmoodle@firsttag\moodle@firsttagfalse\else, \fi\texttt{##1}}%
\xa\docsvlist\xa{\test@iii}%
\fi
}%
%% QUESTIONNAME
\define@cmdkey{moodle}[moodle@]{questionname}{}%
\generate@moodle@write@html@noptag{questionname}<name>{\moodle@questionname}%
%% QUESTIONTEXT
%I tried to use questiontext as a key, but it doesn't seem to work.
%The trouble is that xkeyval has trouble parsing a key with a \par token followed by a comma within brackets,
%like \setkeys{moodle}{questiontext={ABC\par [D,E]}}
%It's not worth trying to fix.
\long\def\questiontext#1{%
%\converttohtmlmacro\myoutput{#1}%
%\let\moodle@questiontext=\myoutput%
\def\moodle@questiontext{#1}%
}%
\generate@moodle@write@code{questiontext}<questiontext>{\moodle@questiontext}%{%
%% PENALTY FOR WRONG ATTEMPT
\define@cmdkey{moodle}[moodle@]{penalty}[0.10]{}%
\generate@moodle@write@code*{penalty}<penalty>{\moodle@penalty}%
\moodleset{penalty=0.10}%
%% FEEDBACK
% Moodle allows for feedback tailored to each question,
% and feedback tailored to each right or wrong answer.
% We shall use the key 'feedback' to record both kinds of feedback,
% relying on TeX's grouping mechanism to keep them apart.
% When it comes time to write them to \XML,
% \moodle@writegeneralfeedback uses the \HTML\ tag <generalfeedback>
% whereas \moodle@writefeedback uses the tag <feedback>.
% Note that the general feedback is NOT inherited by each answer!
\define@cmdkey{moodle}[moodle@]{feedback}[]{}%
\generate@moodle@write@code{generalfeedback}<generalfeedback>{\moodle@feedback}%
\generate@moodle@write@code{feedback}<feedback>{\moodle@feedback}%
\moodleset{feedback={}}%
%% DEFAULT GRADE
%The next line creates \moodle@defaultgrade,
%which is how many points the quiz question is worth.
%Key calls like [default grade=7] set \moodle@defaultgrade.
\define@cmdkey{moodle}[moodle@]{default grade}[1.0]{}%
%Next, makes 'points' a synonym for 'default grade'
\define@key{moodle}{points}[1.0]{\xa\def\csname moodle@default grade\endcsname{#1}}%
\generate@moodle@write@code*{defaultgrade}<defaultgrade>{\csname moodle@default grade\endcsname}%
\moodleset{default grade=1.0} %This sets the default.
%% HIDDEN
\define@boolkey{moodle}[moodle@]{hidden}[true]{}%
\generate@moodle@write@code*{hidden}<hidden>{\ifmoodle@hidden 1\else 0\fi}%
\moodleset{hidden=false}%
\def\moodle@writecommondata{%
\moodle@writequestionname%
\moodle@writequestiontext%
\moodle@writedefaultgrade%
\moodle@writegeneralfeedback%
\moodle@writepenalty%
\moodle@writehidden%
}%
%% TAGS
%The next line creates \moodle@tags,
%which defines a "tag" (i.e., keyword) for the question.
%Key calls like [tags={random}] set \moodle@tags.
\define@cmdkey{moodle}[moodle@]{tags}[]{}%
\moodleset{tags={}}%
%% FRACTION -- how much this answer is worth out of 100 percent
\define@cmdkey{moodle}[moodle@]{fraction}[100]{}%
%We do not create \moodle@writefraction, because the fraction occurs in
%the \XML\ within the answer tag, like <answer fraction="75">.
\moodleset{fraction=100} %This sets the default.
%% FRACTIONTOL -- the tolerance for fractions with respect to valid values
\define@cmdkey{moodle}[moodle@]{fractiontol}[0.1]{}%
\moodleset{fractiontol=0.1} %This sets the default.
%% SINGLE and MULTIPLE -- for multichoice, is there 1 right answer or more than 1?
\define@boolkey{moodle}[moodle@]{single}[true]{}%
\generate@moodle@write@code*{single}<single>{\ifmoodle@single true\else false\fi}%
\moodleset{single=true}%
%The key 'multiple' is an antonym to 'single'.
\define@boolkey{moodle}[moodle@]{multiple}[true]{\ifmoodle@multiple\moodle@singlefalse\else\moodle@singletrue\fi}%
%% SHUFFLE ANSWERS
\define@boolkey{moodle}[moodle@]{shuffle}[true]{}%
\generate@moodle@write@code*{shuffle}<shuffleanswers>{\ifmoodle@shuffle 1\else 0\fi}%
\moodleset{shuffle=true}%
%% ALLORNOTHING -- for multichoice with multiple answers where all the points are given
\define@boolkey{moodle}[moodle@]{allornothing}[true]{}%
\moodleset{allornothing=false}%
%% SANCTION -- how much shall incorrect choices by sanctioned in multichoice questions (single)
\define@cmdkey{moodle}[moodle@]{sanction}[]{}%
\moodleset{sanction=0} %This sets the default.
%% TODO: CORRECTFEEDBACK
%% TODO: PARTIALLYCORRECTFEEDBACK
%% TODO: INCORRECTFEEDBACK
%% TODO: NUMCORRECT key
%% NUMBERING -- for numbering of multichoice questions
\define@choicekey{moodle}{numbering}%
{alpha,alph,Alpha,Alph,arabic,roman,Roman,%
abc,ABCD,123,iii,IIII,none}[abc]{%
\def\moodle@numbering{#1}%
\def\test@@i{#1}%
\ifx\test@@i\@moodle@alpha
\def\moodle@numbering{abc}\fi
\ifx\test@@i\@moodle@alph
\def\moodle@numbering{abc}\fi
\ifx\test@@i\@moodle@Alpha
\def\moodle@numbering{ABCD}\fi
\ifx\test@@i\@moodle@Alph
\def\moodle@numbering{ABCD}\fi
\ifx\test@@i\@moodle@arabic
\def\moodle@numbering{123}\fi
\ifx\test@@i\@moodle@roman
\def\moodle@numbering{iii}\fi
\ifx\test@@i\@moodle@Roman
\def\moodle@numbering{IIII}\fi
}%
%'answer numbering' will be a synonym to 'numbering'
\define@key{moodle}{answer numbering}[abc]{\setkeys{moodle}{numbering={#1}}}%
\generate@moodle@write@code*{answernumbering}<answernumbering>{\moodle@numbering}%
%N.B. if we did not set the default here, then \moodle@numbering would be undefined, causing problems.
\moodleset{answer numbering=abc}%
\def\@moodle@alpha{alpha}%
\def\@moodle@Alpha{Alpha}%
\def\@moodle@alph{alph}%
\def\@moodle@Alph{Alph}%
\def\@moodle@arabic{arabic}%
\def\@moodle@roman{roman}%
\def\@moodle@Roman{Roman}%
\def\@moodle@abc{abc}%
\def\@moodle@ABCD{ABCD}%
\def\@moodle@arabicnumbers{123}%
\def\@moodle@iii{iii}%
\def\@moodle@IIII{IIII}%
\def\@moodle@none{none}%
\def\moodle@obeynumberingstyle{%
\renewcommand\labelenumii{\theenumii.}% follow Moodle's labeling convention
\ifx\moodle@numbering\@moodle@abc
\renewcommand\theenumii{\alph{enumii}}%
\fi
\ifx\moodle@numbering\@moodle@ABCD
\renewcommand\theenumii{\Alph{enumii}}%
\fi
\ifx\moodle@numbering\@moodle@arabicnumbers
\renewcommand\theenumii{\arabic{enumii}}%
\fi
\ifx\moodle@numbering\@moodle@iii
\renewcommand\theenumii{\roman{enumii}}%
\fi
\ifx\moodle@numbering\@moodle@IIII
\renewcommand\theenumii{\Roman{enumii}}%
\fi
\ifx\moodle@numbering\@moodle@none
\renewcommand\labelenumii{$\bullet$~}%
\fi
}
%TODO: * In the PDF, how should 'none' in a multi look different from
% short answer or numerical options?
% * Instead of \theenumi and \labelenumi,
% use \@enumdepth to automatically set the correct depth.
%% DISPLAY MODE -- affects Cloze multiple choice questions only.
% 0 = inline, 1 = vertical, 2 = horizontal
\def\moodle@multi@mode{\ifmoodle@single 0\else 1\fi}%
\define@key{moodle}{inline}[]{\def\moodle@multi@mode{0}}%
\define@key{moodle}{vertical}[]{\def\moodle@multi@mode{1}}%
\define@key{moodle}{horizontal}[]{\def\moodle@multi@mode{2}}%
%% TOLERANCE
\define@cmdkey{moodle}[moodle@]{tolerance}[0]{}%
\moodleset{tolerance=0}%
%There is no \moodle@writetolerance, because in the \XML\ the
%tolerance is given within the answer tag,
%like <answer fraction=100 tolerance=0.03>.
%% CASE SENSITIVE
\define@boolkey{moodle}[moodle@]{usecase}[true]{}%
\generate@moodle@write@code*{usecase}<usecase>{\ifmoodle@usecase 1\else 0\fi}%
% We make 'case sensitive' a synonym for 'usecase'.
\define@boolkey{moodle}[moodle@]{case sensitive}[true]{\ifcsname moodle@case sensitive\endcsname \moodle@usecasetrue\else\moodle@usecasefalse\fi}%
\moodleset{usecase=false}%
%% DRAG-AND-DROP FORMAT
\define@boolkey{moodle}[moodle@]{draganddrop}[true]{}%
% We make 'dd' and 'draganddrop' and 'drag and drop' synonyms for 'draganddrop'.
\define@boolkey{moodle}[moodle@]{dd}[true]{\ifmoodle@dd\moodle@draganddroptrue\else\moodle@draganddropfalse\fi}%
\define@boolkey{moodle}[moodle@]{drag and drop}[true]{\moodle@ddsynonym}%
\def\moodle@ddsynonym{%
\csname ifmoodle@drag and drop\endcsname
\moodle@draganddroptrue
\else
\moodle@draganddropfalse
\fi
}
\moodleset{draganddrop=false}%
%% EDITOR
\def\@moodle@html{html}%
\def\@moodle@htmlfile{html+file}%
\def\@moodle@text{text}%
\def\@moodle@plain{plain}%
\def\@moodle@monospaced{monospaced}%
\def\@moodle@file{file}%
\def\@moodle@noinline{noinline}%
\define@choicekey{moodle}{response format}%
{html,html+file,text,monospaced,file}[html]%
{\def\test@i{#1}%
\ifx\test@i\@moodle@html
% \HTML\ Editor
\def\moodle@responseformat{editor}%
\fi
\ifx\test@i\@moodle@htmlfile
% \HTML\ Editor with File Picker
\def\moodle@responseformat{editorfilepicker}%
\fi
\ifx\test@i\@moodle@text
% Plain text
\def\moodle@responseformat{plain}%
\fi
\ifx\test@i\@moodle@plain
% Plain text
\def\moodle@responseformat{plain}%
\fi
\ifx\test@i\@moodle@monospaced
% Plain text, monospaced font
\def\moodle@responseformat{monospaced}%
\fi
\ifx\test@i\@moodle@file
% No inline text (i.e., attachments only)
\def\moodle@responseformat{noinline}%
\fi
\ifx\test@i\@moodle@noinline
% No inline text (i.e., attachments only)
\def\moodle@responseformat{noinline}%
\fi
}%
\generate@moodle@write@code*{responseformat}<responseformat>{\moodle@responseformat}%
\moodleset{response format=html}%
%N.B. if we did not set a default, then \moodle@responseformat would be undefined, causing problems.
%% RESPONSE REQUIRED
\define@boolkey{moodle}[moodle@]{response required}[true]{}%
% TODO: Make synonym 'required'
\generate@moodle@write@code*{responserequired}<responserequired>{\csname ifmoodle@response required\endcsname 1\else 0\fi}%
\moodleset{response required=false}%
%% RESPONSEFIELDLINES
\define@cmdkey{moodle}[moodle@]{response field lines}[15]{}%
\generate@moodle@write@code*{responsefieldlines}<responsefieldlines>{\csname moodle@response field lines\endcsname}%
%Make synonyms 'input box size' or 'height' or 'lines'?
\moodleset{response field lines=15}% N.B. if we do not set a default, then \moodle@responseformat will be undefined, causing problems.
%% ATTACHMENTS ALLOWED
\def\@moodle@unlimited{unlimited}%
\define@choicekey{moodle}{attachments allowed}{0,1,2,3,unlimited}[1]{%
\def\test@i{#1}%
\ifx\test@i\@moodle@unlimited
\def\moodle@attachmentsallowed{-1}%
\else
\def\moodle@attachmentsallowed{#1}%
\fi
}
\generate@moodle@write@code*{attachmentsallowed}<attachments>{\moodle@attachmentsallowed}
\moodleset{attachments allowed=0}%
%% ATTACHMENTS REQUIRED
\define@choicekey{moodle}{attachments required}{0,1,2,3}[1]{\def\moodle@attachmentsrequired{#1}}%
\generate@moodle@write@code*{attachmentsrequired}<attachmentsrequired>{\moodle@attachmentsrequired}
\moodleset{attachments required=0}%
%% RESPONSE TEMPLATE
\define@key{moodle}{template}{\long\def\moodle@responsetemplate{#1}}%
\generate@moodle@write@html@noptag{responsetemplate}<responsetemplate>{\moodle@responsetemplate}
\moodleset{template={}}%
%% SHOWNUMCORRECT
\define@boolkey{moodle}[moodle@]{shownumcorrect}[true]{}%
\gdef\moodle@writeshownumcorrect{%
\if\moodle@shownumcorrect
\writetomoodle[4]{ <shownumcorrect/>}%
\fi
}%
\moodleset{shownumcorrect=false}%
%% CLEARWRONG
\define@boolkey{moodle}[moodle@]{clearwrong}[true]{}%
\gdef\moodle@writeclearwrong{%
\if\moodle@clearwrong
\writetomoodle[4]{ <clearwrong/>}%
\fi
}%
\moodleset{clearwrong=false}%
\def\moodle@answers@xml{}%
\gdef\moodle@writeanswers{%
\writetomoodle{\moodle@answers@xml}%
}%
\newcommand\addto@xml[3][0]{%
% #1 = spaces to indent (default=0)
% #2 = macro containing \XML\ code (possibly empty)
% #3 = \XML\ text to be appended to that macro (will be \edef'd)
\calculateindent{#1}%
\edef\xml@to@add{\moodle@indent\trim@pre@space{#3}}%
\ifx#2\@moodle@empty
\edef\newxml{\noexpand#2\xml@to@add}%
\else
\edef\newxml{\noexpand#2^^J\xml@to@add}%
\fi
\xa\xa\xa\def\xa\xa\xa#2\xa\xa\xa{\newxml}%
}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CALCULATED %%%%%%%%%%%%%%%%%%%%%%%%%%
\ifmoodle@feedbackLeft% contribution of Juergen Vollmer, 2021-03-05
\newcommand{\moodle@preFeedback}{\\}%
\else%
\newcommand{\moodle@preFeedback}{\hfill}%
\fi
\def\moodle@makelatextagbox#1{%
\Ovalbox{\tiny #1}
%\ovalbox{\tiny #1}
%\shadowbox{\tiny #1}
}%
\def\moodle@makelatextag@qtype#1{%
\doublebox{\tiny \textsc{\GetTranslation{#1}}}
}%
\def\moodle@makelatextag@value#1#2{%
\moodle@makelatextagbox{\GetTranslation{#2}~\csname moodle@#1\endcsname}%
}%
\def\moodle@makelatextag@key#1{%
\moodle@makelatextagbox{\GetTranslation{#1}}
}%
\def\moodle@makelatextag@other#1{%
\moodle@makelatextagbox{\GetTranslation{#1}}
}%
\def\moodle@makefrontend#1#2{%
\NewEnviron{#1}[2][]{%
\bgroup
\setkeys{moodle}{##1,questionname={##2}}%
\global\advance\moodle@totalmarks by \csname moodle@default grade\endcsname pt
\expandafter\gatheritems\xa{\BODY}%
\let\moodle@questionheader=\gatheredheader
%First, the LaTeX processing
\item \moodle@begin@samepage \textbf{\moodle@questionname}
\ifmoodle@handout
\moodle@makelatextag@qtype{#1}
\else
\moodle@latex@writetags
\par
\noindent
\moodle@makelatextag@qtype{#1}%
\moodle@makelatextag@value{default grade}{marked out of}
\moodle@makelatextag@value{penalty}{penalty}%
\fi
#2\par
\noindent
\moodle@questionheader
\edef\moodle@generalfeedback{\expandonce\moodle@feedback}
\csname moodle@#1@latexprocessing\endcsname
\moodle@end@samepage
%Now, writing information to XML
\@moodle@ifgeneratexml{%
\xa\questiontext\xa{\moodle@questionheader}% Save the question text.
\csname write#1question\endcsname
\bgroup
\gdef\moodle@answers@xml{}%
\setkeys{moodle}{feedback={}}%
\xa\loopthroughitemswithcommand\xa{\csname save#1answer\endcsname}%
\passvalueaftergroup{\moodle@answers@xml}%
\egroup
\moodle@writeanswers%
\moodle@writetags%
\writetomoodle{</question>}%
}{}%
\egroup
}%
}
\AtBeginEnvironment{quiz}{% protect existing description outside of quiz
\let\description\relax% remove the meaning of existing \description and \enddescription
\let\enddescription\relax
\NewEnviron{description}[2][]{%
\bgroup
\setkeys{moodle}{#1,questionname={#2}}%
\let\moodle@questiontext=\BODY
\trim@spaces@in\moodle@questiontext
\ifx\moodle@questiontext\@empty\relax\else%
%First, the LaTeX processing.
\item \moodle@begin@samepage\textbf{\moodle@questionname}
\ifmoodle@handout\else
\moodle@latex@writetags
\par
\noindent
\fi
\moodle@makelatextag@qtype{description}\par
\noindent
\moodle@questiontext\par
\ifmoodle@handout\else
\ifx\moodle@feedback\@empty\relax\else
\fbox{\parbox{.96\linewidth}{\emph{\moodle@feedback}}}%
\fi
\fi
\moodle@end@samepage
%Now, writing information to memory.
\@moodle@ifgeneratexml{%
\writetomoodle{<question type="description">}%
\moodle@writecommondata
\moodle@writetags%
\writetomoodle{</question>}%
}{}%
\fi
\egroup
}%
}%
\def\moodle@essay@latexprocessing{%
% Moodle cannot automatically grade an essay,
% but if the user puts \item's in, we can list them in an itemize as notes.
\par\noindent \emph{\GetTranslation{Information for graders}:}
\ifnum\c@numgathereditems>0\relax
\begin{itemize} \setlength\itemsep{0pt}\setlength\parskip{0pt}%
\loopthroughitemswithcommand{\moodle@print@essay@answer}%
\end{itemize}%
\fi
\ifx\moodle@generalfeedback\@empty\relax\else%
\fbox{\parbox{.96\linewidth}{\emph{\moodle@generalfeedback}}}%
\fi
}
\NewEnviron{essay}[2][]{%
\bgroup
\setkeys{moodle}{#1,questionname={#2}}%
\global\advance\moodle@totalmarks by \csname moodle@default grade\endcsname pt%
\moodle@checkresponsefieldlines
\expandafter\gatheritems\expandafter{\BODY}%
\let\moodle@questionheader=\gatheredheader
%First, the LaTeX processing.
\item \moodle@begin@samepage\textbf{\moodle@questionname}
\ifmoodle@handout
\moodle@makelatextag@qtype{essay}
\else
\moodle@latex@writetags
\par
\noindent
\moodle@makelatextag@qtype{essay}
\moodle@makelatextag@value{default grade}{marked out of}
\moodle@makelatextag@value{penalty}{penalty}
\xa\moodle@makelatextag@key\xa{\moodle@responseformat}
\fi
\par
\noindent
\moodle@questionheader
\long\def\@lempty{}%
\ifx\moodle@responsetemplate\@lempty\else
\par\noindent\emph{\GetTranslation{Response template}:}
\par\noindent\fbox{\parbox{.96\linewidth}{\moodle@responsetemplate}}\par
\fi
\edef\moodle@generalfeedback{\expandonce\moodle@feedback}
\ifmoodle@handout
\par\noindent
\fbox{\parbox[t][\csname moodle@response field lines\endcsname\baselineskip]{.96\linewidth}{\phantom{Moodle}}}%
\else
\csname moodle@essay@latexprocessing\endcsname
\fi
\moodle@end@samepage
%Now, writing information to memory.
\@moodle@ifgeneratexml{%
\xa\questiontext\xa{\moodle@questionheader}% Save the question text.
\writeessayquestion
\bgroup
\gdef\moodle@answers@xml{}%
%
\ifnum\c@numgathereditems=0\relax
\addto@xml[2]\moodle@answers@xml{<graderinfo format="html"><text/></graderinfo>}%
\else
\addto@xml[2]\moodle@answers@xml{<graderinfo format="html"><text><![CDATA[}%
\ifnum\c@numgathereditems>1\relax
\addto@xml[4]\moodle@answers@xml{<ul>}%
\fi
\loopthroughitemswithcommand{\moodle@savegraderinfo}%
\ifnum\c@numgathereditems>1\relax
\addto@xml[4]\moodle@answers@xml{</ul>}%
\fi
\addto@xml[2]\moodle@answers@xml{]]></text></graderinfo>}%
\fi
%
\passvalueaftergroup{\moodle@answers@xml}%
\egroup
\moodle@writeanswers% The 'answers' \XML\ really contains the grader info.
\moodle@writeresponsetemplate%
\moodle@writetags%
\writetomoodle{</question>}%
}{}%
\egroup
}%
\def\moodle@checkresponsefieldlines{%
\newcount\a\a=\number\csname moodle@response field lines\endcsname
\newcount\b\b=5%
\ifnum\the\a>40\relax% if the value was more than 40
\a=40%
\fi
\ifnum\the\a<5\relax% if the value was less than 5
\a=5%
\fi
\divide\a by\b% integer division by 5
\multiply\a by\b% multiply by 5
\ifnum\a=\csname moodle@response field lines\endcsname\relax% equality holds if we had 5, 10, 15, 20, 25, 30, or 40
\else
\ifnum\csname moodle@response field lines\endcsname>5\relax%
\ifnum\csname moodle@response field lines\endcsname<40\relax%
\advance\a by\b% approximate with the next multiple of 5
\fi
\fi
\PackageWarning{moodle}{"response field lines" admits only multiples of 5 between 5 and 40
(You tried to set \csname moodle@response field lines\endcsname). The value
\the\a\space will be used.}%
\setkeys{moodle}{response field lines=\the\a}%
\fi
}%
%%%% TODO
%%%% To make essay work will be tough.
%%%% Every line from \ifnum\c@numgathereditems=0\relax through its \else and \fi,
%%%% with the exception of
%%%% \xa\loopthroughitemswithcommand\xa{\csname save#1answer\endcsname}%
%%%% , does not exist in our current \moodle@makefrontend code.
%%%% How can we cope?
%%%%
%%%% Idea: change \moodle@makefrontend so that
%%%% 1. if \c@numgathereditems=0, we don't do anything.
%%%% 2. it calls a preamble and postamble around the \loopthroughitemswithcommand.
%%%% Like this:
%%%%
%%%% \@moodle@ifgeneratexml{%
%%%% \xa\questiontext\xa{\moodle@questionheader}% Save the question text.
%%%% \bgroup
%%%% \gdef\moodle@answers@xml{}%
%%%% \setkeys{moodle}{feedback={}}%
%%%% \@ifundefined{moodle@#1@answers@preamble}{}{}%
%%%% \csname moodle@#1@answers@preamble\endcsname
%%%% \ifnum\c@numgathereditems=0\relax
%%%% \relax
%%%% \else
%%%% \xa\loopthroughitemswithcommand\xa{\csname save#1answer\endcsname}%
%%%% \fi
%%%% \@ifundefined{moodle@#1@answers@postamble}{}{}%
%%%% \csname moodle@#1@answers@postamble\endcsname
%%%% \passvalueaftergroup{\moodle@answers@xml}%
%%%% \egroup
%%%% \csname write#1question\endcsname
%%%% }{}%
%%%% The \@ifundefined lines should automatically define the
%%%% \...@preamble \...@postamble macros to be \relax if they don't exist already.
\gdef\writeessayquestion{%
\writetomoodle{<question type="essay">}%
\moodle@writecommondata%
\moodle@writeresponserequired%
\moodle@writeresponseformat%
\moodle@writeresponsefieldlines%
\moodle@writeattachmentsallowed%
\moodle@writeattachmentsrequired%
}%
\long\def\moodle@print@essay@answer#1{%