mayankjoin3

m1

Feb 24th, 2026
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.36 KB | None | 0 0
  1. import os
  2. import re
  3. import base64
  4.  
  5. start = 111705
  6.  
  7. ans = {
  8.     'Q01': 'C', 'Q02': 'B', 'Q03': 'A', 'Q04': 'A', 'Q05': 'B',
  9.     'Q06': 'A', 'Q07': 'A', 'Q08': 'A', 'Q09': 'C', 'Q10': 'A',
  10.     'Q11': 'B', 'Q12': 'A', 'Q13': 'A', 'Q14': 'D', 'Q15': 'A',
  11.     'Q16': 'B', 'Q17': 'C', 'Q18': 'A', 'Q19': 'C', 'Q20': 'A',
  12.     'Q21': 'B', 'Q22': 'B', 'Q23': 'A', 'Q24': 'B', 'Q25': 'C'
  13. }
  14.  
  15. IMG_DIR = "img"
  16. OUT_FILE = "moodle_questions.xml"
  17.  
  18. # Expected filename pattern: microtk_jan-apr_2025_001.png ... 025.png
  19. # We'll sort by the trailing number before extension if present; otherwise lexicographic.
  20. num_pat = re.compile(r"(\d+)(?=\.[A-Za-z]+$)")
  21.  
  22. def sort_key(fn: str):
  23.     m = num_pat.search(fn)
  24.     return (int(m.group(1)) if m else 10**9, fn)
  25.  
  26. files = [f for f in os.listdir(IMG_DIR) if os.path.isfile(os.path.join(IMG_DIR, f))]
  27. files = [f for f in files if f.lower().endswith((".png", ".jpg", ".jpeg", ".gif", ".webp"))]
  28. files.sort(key=sort_key)
  29.  
  30. if len(files) != 25:
  31.     raise SystemExit(f"Expected 25 images in '{IMG_DIR}', found {len(files)}: {files}")
  32.  
  33. def b64_of(path: str) -> str:
  34.     with open(path, "rb") as f:
  35.         return base64.b64encode(f.read()).decode("ascii")
  36.  
  37. def answer_block(letter: str, correct_letter: str) -> str:
  38.     frac = "100" if letter == correct_letter else "-25"
  39.     return f'''    <answer fraction="{frac}" format="html">
  40.      <text><![CDATA[<p>Option {letter}</p>]]></text>
  41.      <feedback format="html">
  42.        <text></text>
  43.      </feedback>
  44.    </answer>'''
  45.  
  46. template = """<!-- question: {qid}  -->
  47.  <question type="multichoice">
  48.    <name>
  49.      <text>{qname}</text>
  50.    </name>
  51.    <questiontext format="html">
  52.      <text><![CDATA[<p><img src="@@PLUGINFILE@@/{fname}"></p>]]></text>
  53. <file name="{fname}" path="/" encoding="base64">{b64}</file>
  54.    </questiontext>
  55.    <generalfeedback format="html">
  56.      <text></text>
  57.    </generalfeedback>
  58.    <defaultgrade>1.0000000</defaultgrade>
  59.    <penalty>0.0000000</penalty>
  60.    <hidden>0</hidden>
  61.    <idnumber></idnumber>
  62.    <single>true</single>
  63.    <shuffleanswers>false</shuffleanswers>
  64.    <answernumbering>ABCD</answernumbering>
  65.    <showstandardinstruction>0</showstandardinstruction>
  66.    <correctfeedback format="html">
  67.      <text><![CDATA[<p>Your answer is correct.</p>]]></text>
  68.    </correctfeedback>
  69.    <incorrectfeedback format="html">
  70.      <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
  71.    </incorrectfeedback>
  72.    <shownumcorrect/>
  73. {answers}
  74.  </question>
  75. """
  76.  
  77. xml_parts = ['<?xml version="1.0" encoding="UTF-8"?>\n<quiz>\n']
  78.  
  79. qid = start
  80. for i, fname in enumerate(files, start=1):
  81.     qname = f"Q{i:02d}"
  82.     correct = ans[qname].upper()
  83.     if correct not in ("A", "B", "C", "D"):
  84.         raise SystemExit(f"Invalid correct option for {qname}: {correct}")
  85.  
  86.     b64 = b64_of(os.path.join(IMG_DIR, fname))
  87.  
  88.     answers = "\n".join([
  89.         answer_block("A", correct),
  90.         answer_block("B", correct),
  91.         answer_block("C", correct),
  92.         answer_block("D", correct),
  93.     ])
  94.  
  95.     xml_parts.append(template.format(
  96.         qid=qid,
  97.         qname=qname,
  98.         fname=fname,
  99.         b64=b64,
  100.         answers=answers
  101.     ))
  102.     xml_parts.append("\n")
  103.     qid += 1
  104.  
  105. xml_parts.append("</quiz>\n")
  106.  
  107. with open(OUT_FILE, "w", encoding="utf-8") as f:
  108.     f.write("".join(xml_parts))
  109.  
  110. print(f" Generated: {OUT_FILE}")
Add Comment
Please, Sign In to add comment