3 // Based on default.php, included by ../import.php
5 * @package questionbank
6 * @subpackage importexport
9 require_once("$CFG->libdir/xmlize.php");
11 class qformat_examview
extends qformat_default
{
28 var $matching_questions = array();
30 function provide_import() {
35 * unxmlise reconstructs part of the xml data structure in order
36 * to identify the actual data therein
37 * @param array $xml section of the xml data structure
38 * @return string data with evrything else removed
40 function unxmlise( $xml ) {
41 // if it's not an array then it's probably just data
42 if (!is_array($xml)) {
43 $text = s(addslashes($xml));
46 // otherwise parse the array
48 foreach ($xml as $tag=>$data) {
49 // if tag is '@' then it's attributes and we don't care
51 $text = $text . $this->unxmlise( $data );
56 // currently we throw the tags we found
57 $text = strip_tags($text);
61 function parse_matching_groups($matching_groups)
63 if (empty($matching_groups)) {
66 foreach($matching_groups as $match_group) {
68 $groupname = trim($match_group['@']['name']);
69 $questiontext = $this->unxmlise($match_group['#']['text'][0]['#']);
70 $newgroup->questiontext
= trim($questiontext);
71 $newgroup->subchoices
= array();
72 $newgroup->subquestions
= array();
73 $newgroup->subanswers
= array();
74 $choices = $match_group['#']['choices']['0']['#'];
75 foreach($choices as $key => $value) {
76 if (strpos(trim($key),'choice-') !== FALSE) {
77 $key = strtoupper(trim(str_replace('choice-', '', $key)));
78 $newgroup->subchoices
[$key] = trim($value['0']['#']);
81 $this->matching_questions
[$groupname] = $newgroup;
85 function parse_ma($qrec, $groupname)
87 $match_group = $this->matching_questions
[$groupname];
88 $phrase = trim($this->unxmlise($qrec['text']['0']['#']));
89 $answer = trim($this->unxmlise($qrec['answer']['0']['#']));
90 $answer = strip_tags( $answer );
91 $match_group->subquestions
[] = $phrase;
92 $match_group->subanswers
[] = $match_group->subchoices
[$answer];
93 $this->matching_questions
[$groupname] = $match_group;
97 function process_matches(&$questions)
99 if (empty($this->matching_questions
)) {
102 foreach($this->matching_questions
as $match_group) {
103 $question = $this->defaultquestion();
104 $htmltext = s(addslashes($match_group->questiontext
));
105 $question->questiontext
= $htmltext;
106 $question->name
= $question->questiontext
;
107 $question->qtype
= MATCH
;
108 $question->subquestions
= array();
109 $question->subanswers
= array();
110 foreach($match_group->subquestions
as $key => $value) {
111 $htmltext = s(addslashes($value));
112 $question->subquestions
[] = $htmltext;
114 $htmltext = s(addslashes($match_group->subanswers
[$key]));
115 $question->subanswers
[] = $htmltext;
117 $questions[] = $question;
121 function cleanUnicode($text) {
122 return str_replace('’', "'", $text);
125 function readquestions($lines) {
126 /// Parses an array of lines into an array of questions,
127 /// where each item is a question object as defined by
130 $questions = array();
131 $currentquestion = array();
133 $text = implode($lines, ' ');
134 $text = $this->cleanUnicode($text);
136 $xml = xmlize($text, 0);
137 if (!empty($xml['examview']['#']['matching-group'])) {
138 $this->parse_matching_groups($xml['examview']['#']['matching-group']);
141 $questionNode = $xml['examview']['#']['question'];
142 foreach($questionNode as $currentquestion) {
143 if ($question = $this->readquestion($currentquestion)) {
144 $questions[] = $question;
148 $this->process_matches($questions);
153 function readquestion($qrec)
156 $type = trim($qrec['@']['type']);
157 $question = $this->defaultquestion();
158 if (array_key_exists($type, $this->qtypes
)) {
159 $question->qtype
= $this->qtypes
[$type];
162 $question->qtype
= null;
164 $question->single
= 1;
165 // Only one answer is allowed
166 $htmltext = $this->unxmlise($qrec['#']['text'][0]['#']);
167 $question->questiontext
= $htmltext;
168 $question->name
= shorten_text( $question->questiontext
, 250 );
170 switch ($question->qtype
) {
172 $question = $this->parse_mc($qrec['#'], $question);
175 $groupname = trim($qrec['@']['group']);
176 $question = $this->parse_ma($qrec['#'], $groupname);
179 $question = $this->parse_tf_yn($qrec['#'], $question);
182 $question = $this->parse_co($qrec['#'], $question);
185 $question = $this->parse_sa($qrec['#'], $question);
188 $question = $this->parse_nr($qrec['#'], $question);
192 print("<p>Question type ".$type." import not supported for ".$question->questiontext
."<p>");
195 // end switch ($question->qtype)
201 function parse_tf_yn($qrec, $question)
203 $choices = array('T' => 1, 'Y' => 1, 'F' => 0, 'N' => 0 );
204 $answer = trim($qrec['answer'][0]['#']);
205 $question->answer
= $choices[$answer];
206 $question->correctanswer
= $question->answer
;
207 if ($question->answer
== 1) {
208 $question->feedbacktrue
= 'Correct';
209 $question->feedbackfalse
= 'Incorrect';
211 $question->feedbacktrue
= 'Incorrect';
212 $question->feedbackfalse
= 'Correct';
217 function parse_mc($qrec, $question)
219 $answer = 'choice-'.strtolower(trim($qrec['answer'][0]['#']));
221 $choices = $qrec['choices'][0]['#'];
222 foreach($choices as $key => $value) {
223 if (strpos(trim($key),'choice-') !== FALSE) {
225 $question->answer
[$key] = s($this->unxmlise($value[0]['#']));
226 if (strcmp($key, $answer) == 0) {
227 $question->fraction
[$key] = 1;
228 $question->feedback
[$key] = 'Correct';
230 $question->fraction
[$key] = 0;
231 $question->feedback
[$key] = 'Incorrect';
238 function parse_co($qrec, $question)
240 $question->usecase
= 0;
241 $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
242 $answer = strip_tags( $answer );
243 $answers = explode("\n",$answer);
245 foreach($answers as $key => $value) {
246 $value = trim($value);
247 if (strlen($value) > 0) {
248 $question->answer
[$key] = addslashes($value);
249 $question->fraction
[$key] = 1;
250 $question->feedback
[$key] = "Correct";
256 function parse_sa($qrec, $question) {
257 $feedback = trim($this->unxmlise($qrec['answer'][0]['#']));
258 $question->feedback
= $feedback;
259 $question->fraction
= 0;
263 function parse_nr($qrec, $question)
265 $answer = trim($this->unxmlise($qrec['answer'][0]['#']));
266 $answer = strip_tags( $answer );
267 $answers = explode("\n",$answer);
269 foreach($answers as $key => $value) {
270 $value = trim($value);
271 if (is_numeric($value)) {
273 $question->answer
[$key] = $value;
274 $question->fraction
[$key] = 1;
275 $question->feedback
[$key] = "Correct";
276 $question->min
[$key] = $question->answer
[$key] - $errormargin;
277 $question->max
[$key] = $question->answer
[$key] +
$errormargin;