Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / question / format / examview / format.php
blob32bf0e57338425c63d812e828c28b8e1bc7bcc90
1 <?php // $Id$
3 // Based on default.php, included by ../import.php
4 /**
5 * @package questionbank
6 * @subpackage importexport
7 */
9 require_once("$CFG->libdir/xmlize.php");
11 class qformat_examview extends qformat_default {
13 var $qtypes = array(
14 'tf' => TRUEFALSE,
15 'mc' => MULTICHOICE,
16 'yn' => TRUEFALSE,
17 'co' => SHORTANSWER,
18 'ma' => MATCH,
19 'mtf' => 99,
20 'nr' => NUMERICAL,
21 'pr' => 99,
22 'es' => 99,
23 'ca' => 99,
24 'ot' => 99,
25 'sa' => ESSAY
28 var $matching_questions = array();
30 function provide_import() {
31 return true;
34 /**
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));
45 else {
46 // otherwise parse the array
47 $text = '';
48 foreach ($xml as $tag=>$data) {
49 // if tag is '@' then it's attributes and we don't care
50 if ($tag!=='@') {
51 $text = $text . $this->unxmlise( $data );
56 // currently we throw the tags we found
57 $text = strip_tags($text);
58 return $text;
61 function parse_matching_groups($matching_groups)
63 if (empty($matching_groups)) {
64 return;
66 foreach($matching_groups as $match_group) {
67 $newgroup = NULL;
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;
94 return NULL;
97 function process_matches(&$questions)
99 if (empty($this->matching_questions)) {
100 return;
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('&#x2019;', "'", $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
128 /// readquestion().
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);
149 return $questions;
151 // end readquestions
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];
161 else {
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) {
171 case MULTICHOICE:
172 $question = $this->parse_mc($qrec['#'], $question);
173 break;
174 case MATCH:
175 $groupname = trim($qrec['@']['group']);
176 $question = $this->parse_ma($qrec['#'], $groupname);
177 break;
178 case TRUEFALSE:
179 $question = $this->parse_tf_yn($qrec['#'], $question);
180 break;
181 case SHORTANSWER:
182 $question = $this->parse_co($qrec['#'], $question);
183 break;
184 case ESSAY:
185 $question = $this->parse_sa($qrec['#'], $question);
186 break;
187 case NUMERICAL:
188 $question = $this->parse_nr($qrec['#'], $question);
189 break;
190 break;
191 default:
192 print("<p>Question type ".$type." import not supported for ".$question->questiontext."<p>");
193 $question = NULL;
195 // end switch ($question->qtype)
197 return $question;
199 // end readquestion
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';
210 } else {
211 $question->feedbacktrue = 'Incorrect';
212 $question->feedbackfalse = 'Correct';
214 return $question;
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';
229 } else {
230 $question->fraction[$key] = 0;
231 $question->feedback[$key] = 'Incorrect';
235 return $question;
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";
253 return $question;
256 function parse_sa($qrec, $question) {
257 $feedback = trim($this->unxmlise($qrec['answer'][0]['#']));
258 $question->feedback = $feedback;
259 $question->fraction = 0;
260 return $question;
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)) {
272 $errormargin = 0;
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;
280 return $question;
284 // end class