-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwvc-convert
executable file
·72 lines (51 loc) · 1.92 KB
/
wvc-convert
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
#!/usr/local/bin/node
// Modules
var fs = require("fs");
var path = require("path");
var program = require("commander");
program
.option("-i, --input <in>", "Source JSON file")
.option("-o, --output <out>", "Destination dir for 'moodle_xml' dir. Default: current working dir")
.parse(process.argv);
if (!program.output) program.output = "./";
var jsonPath = path.join(process.cwd(), program.input);
var outputDir = path.join(program.output, "moodle_xml");
if (outputDir[0] != "/") outputDir = path.join(process.cwd(), outputDir);
if (jsonPath[0] != "/") jsonPath = path.join(process.cwd(), jsonPath);
var jsonFile = fs.readFileSync(jsonPath);
var quizzes = JSON.parse(jsonFile);
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir);
quizzes.forEach(function (quiz) {
if (!quiz.questions) return;
var xml = [];
xml.push("<?xml version=\"1.0\" ?>");
xml.push("<quiz>");
var i = 0;
quiz.questions.forEach(function (question) {
i++;
xml.push("\t<question type=\"multichoice\">");
xml.push("\t\t<name>");
xml.push("\t\t\t<text>Question " + i + "</text>");
xml.push("\t\t</name>");
xml.push("\t\t<questiontext format=\"html\">");
xml.push("\t\t\t<text><![CDATA[ " + question.question + " ]]></text>");
xml.push("\t\t</questiontext>");
if (question.answers) {
question.answers.forEach(function (answer) {
var frac = answer.correct ? 100 : 0;
xml.push("\t\t<answer fraction=\"" + frac + "\">");
xml.push("\t\t\t<text>" + answer.choice + "</text>");
xml.push("\t\t</answer>");
xml.push("\t\t<feedback>");
xml.push("\t\t\t<text><![CDATA[ " + answer.feedback + " ]]></text>");
xml.push("\t\t</feedback>");
});
}
xml.push("\t\t<shuffleanswers>1</shuffleanswers>");
xml.push("\t\t<single>true</single>");
xml.push("\t</question>");
});
xml.push("</quiz>");
var title = quiz.title.replace(/\s|\//g, "_");
fs.writeFileSync(path.join(outputDir, title + ".xml"), xml.join("\n"));
});