-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmake_qti.php
More file actions
263 lines (238 loc) · 10.8 KB
/
Copy pathmake_qti.php
File metadata and controls
263 lines (238 loc) · 10.8 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
<?php
// When turning plain text into HTML to go into XML we need
// to double encode the less-thans and greater-thans
// Since the PHP XML serialization won't double-encode
// HTMLentities - we need to do it here
function xml_double_encode_string($str) {
$from = array('&', '\'', '"', '<', '>');
$to = array('&', ''', '"', '&lt;', '&gt;');
$to = array('&', ''', '"', '<', '>');
return(str_replace($from, $to, $str));
}
// Since the pattern of
// $mattext = $material->addChild("mattext");
// $material->mattext = $questext;
// Will escape everything < > " " except for an & we pre-escape the
// ampersands
function plain_to_html_in_xml($questext) {
return str_replace("&","&",$questext);
}
/**
* From http://stackoverflow.com/questions/3957360/generating-xml-document-in-php-escape-characters
* @param $arr1 the single string that shall be masked
* @return the resulting string with the masked characters
*/
function html_in_xml_replace_char($arr1)
{
if (strpos ($arr1,'&')!== FALSE) { //test if the character appears
$arr1=preg_replace('/&/','&', $arr1); // do this first
}
// just encode the
if (strpos ($arr1,'>')!== FALSE) {
$arr1=preg_replace('/>/','>', $arr1);
}
if (strpos ($arr1,'<')!== FALSE) {
$arr1=preg_replace('/</','<', $arr1);
}
if (strpos ($arr1,'"')!== FALSE) {
$arr1=preg_replace('/"/','"', $arr1);
}
if (strpos ($arr1,'\'')!== FALSE) {
$arr1=preg_replace('/\'/',''', $arr1);
}
return $arr1;
}
// Sadly, some LMS's get confused with text/plain even thought it is the default
// across QTI examples :)
$htmlhack = isset($_SESSION['htmlhack']);
// Somehow simplexml_load_file() seems to get confused now and then..
$data = file_get_contents(getcwd().'/xml/assessment.xml');
$QTI = simplexml_load_string($data);
$uuid = uniqid();
$offset=100;
unset($QTI->assessment);
$QTI->addChild("assessment");
$QTI->assessment->addAttribute("ident", $uuid);
$title = isset($_SESSION['title']) ? $_SESSION['title'] : 'Converted by the Gift2QTI Converter';
$QTI->assessment->addAttribute("title", $title);
$section = $QTI->assessment->addChild('section');
$section->addAttribute("ident", "root_section");
foreach($questions as $question) {
$item = $section->addChild("item");
$item->addAttribute("title",$question->name);
$item->addAttribute("ident",$uuid.'_'.$offset++);
$itemmetadata = $item->addChild("itemmetadata");
$qtimetadata = $itemmetadata->addChild("qtimetadata");
$qtimetadatafield = $qtimetadata->addChild("qtimetadatafield");
$qtimetadatafield->addChild("fieldlabel", "question_type");
$qtimetadatafield->addChild("fieldentry", $question->type);
$qtimetadatafield = $qtimetadata->addChild("qtimetadatafield");
$qtimetadatafield->addChild("fieldlabel", "points_possible");
$qtimetadatafield->addChild("fieldentry", "1");
$qtimetadatafield = $qtimetadata->addChild("qtimetadatafield");
$qtimetadatafield->addChild("fieldlabel", "assessment_question_identifierref");
$qtimetadatafield->addChild("fieldentry", $uuid.'_'.$offset++);
$presentation = $item->addChild("presentation");
$material = $presentation->addChild("material");
$questext = $question->question;
$mattext = $material->addChild("mattext");
if ( isset($question->html) && $question->html ) {
$material->mattext = $questext;
$mattext->addAttribute("texttype", 'text/html');
} else { // Plain text
$questext = ltrim($questext);
// Already in htmlentities :( TODO: Fix this
$questext = html_entity_decode($questext);
if ( $htmlhack ) {
$material->mattext = xml_double_encode_string($questext);
$mattext->addAttribute("texttype", 'text/html');
} else {
$material->mattext = $questext;
$mattext->addAttribute("texttype", 'text/plain');
}
}
if ( $question->type == 'true_false_question' ) {
$response_lid = $presentation->addChild('response_lid');
$response_lid->addAttribute("ident", "response1");
$response_lid->addAttribute("rcardinality", "Single");
$render_choice = $response_lid->addChild('render_choice');
$trueval = $offset++;
$response_label = $render_choice->addChild('response_label');
$response_label->addAttribute('ident', $trueval);
$material = $response_label->addChild("material");
$mattext = $material->addChild("mattext", "True");
$mattext->addAttribute("texttype", "text/plain");
$falseval = $offset++;
$response_label = $render_choice->addChild('response_label');
$response_label->addAttribute('ident', $falseval);
$material = $response_label->addChild("material");
$mattext = $material->addChild("mattext", "False");
$mattext->addAttribute("texttype", "text/plain");
$resprocessing = $item->addChild("resprocessing");
$outcomes = $resprocessing->addChild("outcomes");
$decvar = $outcomes->addChild("decvar");
$decvar->addAttribute("maxvalue", "100");
$decvar->addAttribute("minvalue", "0");
$decvar->addAttribute("varname", "SCORE");
$decvar->addAttribute("vartype", "Decimal");
$respcondition = $resprocessing->addChild("respcondition");
$respcondition->addAttribute("continue", "No");
$conditionvar = $respcondition->addChild("conditionvar");
$ans = strtolower($question->answer);
$val = strpos($ans,"t") === 0 ? $trueval : $falseval;
$varequal = $conditionvar->addChild("varequal",$val);
$varequal->addAttribute("respident", "response1");
$setvar = $respcondition->addChild("setvar", 100);
$setvar->addAttribute("action", "Set");
$setvar->addAttribute("varname", "SCORE");
continue; // XXX
}
if ( $question->type == 'multiple_choice_question' ||
$question->type == 'multiple_answers_question' ||
$question->type == 'short_answer_question' ) {
$response_lid = $presentation->addChild('response_lid');
$response_lid->addAttribute("ident", "response1");
$render_choice = $response_lid->addChild('render_choice');
$render_choice->addAttribute("shuffle", "Yes");
$correct = array();;
$incorrect = array();;
foreach ( $question->parsed_answer as $parsed_answer ) {
$val = $offset++;
if ( $parsed_answer[0] === true ) {
$correct[] = $val;
} else {
$incorrect[] = $val;
}
$response_label = $render_choice->addChild('response_label');
$response_label->addAttribute('ident', $val);
$material = $response_label->addChild("material");
$mattext = $material->addChild("mattext");
// In GIFT land, all answers are plaintext
if ( $htmlhack ) {
// Both HTML Entities and Ampersand encoding
$material->mattext = xml_double_encode_string($parsed_answer[1]);
$mattext->addAttribute("texttype", "text/html");
} else { // The right way
$material->mattext = $parsed_answer[1];
$mattext->addAttribute("texttype", "text/plain");
}
}
$resprocessing = $item->addChild("resprocessing");
$outcomes = $resprocessing->addChild("outcomes");
$decvar = $outcomes->addChild("decvar");
$decvar->addAttribute("maxvalue", "100");
$decvar->addAttribute("minvalue", "0");
$decvar->addAttribute("varname", "SCORE");
$decvar->addAttribute("vartype", "Decimal");
$respcondition = $resprocessing->addChild("respcondition");
$respcondition->addAttribute("continue", "No");
$conditionvar = $respcondition->addChild("conditionvar");
if ( $question->type == 'multiple_choice_question' ) {
$response_lid->addAttribute("rcardinality", "Single");
$varequal = $conditionvar->addChild("varequal",$correct[0]);
$varequal->addAttribute("respident", "response1");
} else if ( $question->type == 'short_answer_question' ) {
$response_lid->addAttribute("rcardinality", "Single");
foreach( $correct as $cor ) {
$varequal = $conditionvar->addChild("varequal",$cor);
$varequal->addAttribute("respident", "response1");
}
} else { // 'multiple_answers_question'
$response_lid->addAttribute("rcardinality", "Multiple");
$and = $conditionvar->addChild("and");
foreach( $correct as $cor ) {
$varequal = $and->addChild("varequal",$cor);
$varequal->addAttribute("respident", "response1");
}
foreach( $incorrect as $incor ) {
$not = $and->addChild("not");
$varequal = $not->addChild("varequal",$incor);
$varequal->addAttribute("respident", "response1");
}
}
$setvar = $respcondition->addChild("setvar", 100);
$setvar->addAttribute("action", "Set");
$setvar->addAttribute("varname", "SCORE");
}
if ( $question->type == 'essay_question' ) {
$response_str = $presentation->addChild('response_str');
$response_str->addAttribute("ident", "response1");
$response_str->addAttribute("rcardinality", "Single");
$render_fib = $response_str->addChild('render_fib');
$response_label = $render_fib->addChild('response_label');
$response_label->addAttribute('ident', $offset++);
$response_label->addAttribute('rshuffle', "No");
$resprocessing = $item->addChild("resprocessing");
$outcomes = $resprocessing->addChild("outcomes");
$decvar = $outcomes->addChild("decvar");
$decvar->addAttribute("maxvalue", "100");
$decvar->addAttribute("minvalue", "0");
$decvar->addAttribute("varname", "SCORE");
$decvar->addAttribute("vartype", "Decimal");
$respcondition = $resprocessing->addChild("respcondition");
$respcondition->addAttribute("continue", "No");
$conditionvar = $respcondition->addChild("conditionvar");
$other = $conditionvar->addChild("other");
continue; // XXX
}
}
$DOM = new DOMDocument('1.0');
$DOM->preserveWhiteSpace = false;
$DOM->formatOutput = true;
$DOM->loadXML($QTI->asXML());
echo "\nValidating (may take a few seconds)...\n";
libxml_use_internal_errors(true);
if ( isset($_SESSION['novalidate']) ) {
echo "\nContent validation bypassed...\n";
return;
}
if ( ! $DOM->schemaValidate('xml/ims_qtiasiv1p2p1.xsd') ) {
echo "\nWarning: Quiz XML Not Valid\n";
$errors = libxml_get_errors();
foreach ($errors as $error) {
echo "Error:", libxml_display_error($error), "\n";
}
libxml_clear_errors();
} else {
echo "\nQuiz XML validated\n";
}