Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-34808 qformat examview Add phpunit tests to examview import format

  • Loading branch information...
commit 4b4373ed3d774d300cee8a8106f02d8a1be29951 1 parent 76c9fcc
Jean-Michel Vedrine authored

Showing 1 changed file with 64 additions and 39 deletions. Show diff stats Hide diff stats

  1. 103  question/format/examview/format.php
103  question/format/examview/format.php
@@ -17,8 +17,7 @@
17 17
 /**
18 18
  * Examview question importer.
19 19
  *
20  
- * @package    qformat
21  
- * @subpackage examview
  20
+ * @package    qformat_examview
22 21
  * @copyright  2005 Howard Miller
23 22
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 23
  */
@@ -35,7 +34,7 @@
35 34
  * @copyright  2005 Howard Miller
36 35
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 36
  */
38  
-class qformat_examview extends qformat_default {
  37
+class qformat_examview extends qformat_based_on_xml {
39 38
 
40 39
     public $qtypes = array(
41 40
         'tf' => TRUEFALSE,
@@ -49,8 +48,8 @@ class qformat_examview extends qformat_default {
49 48
         'es' => ESSAY,
50 49
         'ca' => 99,
51 50
         'ot' => 99,
52  
-        'sa' => SHORTANSWER
53  
-        );
  51
+        'sa' => SHORTANSWER,
  52
+    );
54 53
 
55 54
     public $matching_questions = array();
56 55
 
@@ -63,6 +62,28 @@ public function mime_type() {
63 62
     }
64 63
 
65 64
     /**
  65
+     * Some softwares put entities in exported files.
  66
+     * This method try to clean up known problems.
  67
+     * @param string str string to correct
  68
+     * @return string the corrected string
  69
+     */
  70
+    public function cleaninput($str) {
  71
+
  72
+        $html_code_list = array(
  73
+            "'" => "'",
  74
+            "’" => "'",
  75
+            "“" => "\"",
  76
+            "”" => "\"",
  77
+            "–" => "-",
  78
+            "—" => "-",
  79
+        );
  80
+        $str = strtr($str, $html_code_list);
  81
+        // Use textlib entities_to_utf8 function to convert only numerical entities.
  82
+        $str = textlib::entities_to_utf8( $str, false);
  83
+        return $str;
  84
+    }
  85
+
  86
+    /**
66 87
      * unxmlise reconstructs part of the xml data structure in order
67 88
      * to identify the actual data therein
68 89
      * @param array $xml section of the xml data structure
@@ -87,13 +108,6 @@ protected function unxmlise( $xml ) {
87 108
         $text = strip_tags($text);
88 109
         return $text;
89 110
     }
90  
-    protected function text_field($text) {
91  
-        return array(
92  
-            'text' => htmlspecialchars(trim($text), ENT_NOQUOTES),
93  
-            'format' => FORMAT_HTML,
94  
-            'files' => array(),
95  
-        );
96  
-    }
97 111
 
98 112
     protected function add_blank_combined_feedback($question) {
99 113
         $question->correctfeedback['text'] = '';
@@ -108,7 +122,7 @@ protected function add_blank_combined_feedback($question) {
108 122
         return $question;
109 123
     }
110 124
 
111  
-    protected function parse_matching_groups($matching_groups) {
  125
+    public function parse_matching_groups($matching_groups) {
112 126
         if (empty($matching_groups)) {
113 127
             return;
114 128
         }
@@ -136,8 +150,7 @@ protected function parse_ma($qrec, $groupname) {
136 150
         $phrase = trim($this->unxmlise($qrec['text']['0']['#']));
137 151
         $answer = trim($this->unxmlise($qrec['answer']['0']['#']));
138 152
         $answer = strip_tags( $answer );
139  
-        $match_group->subquestions[] = $phrase;
140  
-        $match_group->subanswers[] = $match_group->subchoices[$answer];
  153
+        $match_group->mappings[$phrase] = $match_group->subchoices[$answer];
141 154
         $this->matching_questions[$groupname] = $match_group;
142 155
         return null;
143 156
     }
@@ -146,6 +159,7 @@ protected function process_matches(&$questions) {
146 159
         if (empty($this->matching_questions)) {
147 160
             return;
148 161
         }
  162
+
149 163
         foreach ($this->matching_questions as $match_group) {
150 164
             $question = $this->defaultquestion();
151 165
             $htmltext = s($match_group->questiontext);
@@ -157,12 +171,17 @@ protected function process_matches(&$questions) {
157 171
             $question = $this->add_blank_combined_feedback($question);
158 172
             $question->subquestions = array();
159 173
             $question->subanswers = array();
160  
-            foreach ($match_group->subquestions as $key => $value) {
161  
-                $htmltext = s($value);
162  
-                $question->subquestions[] = $this->text_field($htmltext);
163  
-
164  
-                $htmltext = s($match_group->subanswers[$key]);
165  
-                $question->subanswers[] = $htmltext;
  174
+            foreach ($match_group->subchoices as $subchoice) {
  175
+                $fiber = array_keys ($match_group->mappings, $subchoice);
  176
+                $subquestion = '';
  177
+                foreach ($fiber as $subquestion) {
  178
+                    $question->subquestions[] = $this->text_field($subquestion);
  179
+                    $question->subanswers[] = $subchoice;
  180
+                }
  181
+                if ($subquestion == '') { // Then in this case, $subchoice is a distractor.
  182
+                    $question->subquestions[] = $this->text_field('');
  183
+                    $question->subanswers[] = $subchoice;
  184
+                }
166 185
             }
167 186
             $questions[] = $question;
168 187
         }
@@ -172,7 +191,7 @@ protected function cleanunicode($text) {
172 191
         return str_replace('’', "'", $text);
173 192
     }
174 193
 
175  
-    protected function readquestions($lines) {
  194
+    public function readquestions($lines) {
176 195
         // Parses an array of lines into an array of questions,
177 196
         // where each item is a question object as defined by
178 197
         // readquestion().
@@ -209,9 +228,11 @@ public function readquestion($qrec) {
209 228
             $question->qtype = null;
210 229
         }
211 230
         $question->single = 1;
  231
+
212 232
         // Only one answer is allowed.
213 233
         $htmltext = $this->unxmlise($qrec['#']['text'][0]['#']);
214  
-        $question->questiontext = $htmltext;
  234
+
  235
+        $question->questiontext = $this->cleaninput($htmltext);
215 236
         $question->questiontextformat = FORMAT_HTML;
216 237
         $question->questiontextfiles = array();
217 238
         $question->name = shorten_text( $question->questiontext, 250 );
@@ -251,11 +272,11 @@ protected function parse_tf_yn($qrec, $question) {
251 272
         $question->answer = $choices[$answer];
252 273
         $question->correctanswer = $question->answer;
253 274
         if ($question->answer == 1) {
254  
-            $question->feedbacktrue = $this->text_field('Correct');
255  
-            $question->feedbackfalse = $this->text_field('Incorrect');
  275
+            $question->feedbacktrue = $this->text_field(get_string('correct', 'question'));
  276
+            $question->feedbackfalse = $this->text_field(get_string('incorrect', 'question'));
256 277
         } else {
257  
-            $question->feedbacktrue = $this->text_field('Incorrect');
258  
-            $question->feedbackfalse = $this->text_field('Correct');
  278
+            $question->feedbacktrue = $this->text_field(get_string('incorrect', 'question'));
  279
+            $question->feedbackfalse = $this->text_field(get_string('correct', 'question'));
259 280
         }
260 281
         return $question;
261 282
     }
@@ -268,13 +289,13 @@ protected function parse_mc($qrec, $question) {
268 289
         foreach ($choices as $key => $value) {
269 290
             if (strpos(trim($key), 'choice-') !== false) {
270 291
 
271  
-                $question->answer[$key] = $this->text_field(s($this->unxmlise($value[0]['#'])));
  292
+                $question->answer[] = $this->text_field(s($this->unxmlise($value[0]['#'])));
272 293
                 if (strcmp($key, $answer) == 0) {
273  
-                    $question->fraction[$key] = 1;
274  
-                    $question->feedback[$key] = $this->text_field('Correct');
  294
+                    $question->fraction[] = 1;
  295
+                    $question->feedback[] = $this->text_field(get_string('correct', 'question'));
275 296
                 } else {
276  
-                    $question->fraction[$key] = 0;
277  
-                    $question->feedback[$key] = $this->text_field('Incorrect');
  297
+                    $question->fraction[] = 0;
  298
+                    $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
278 299
                 }
279 300
             }
280 301
         }
@@ -290,11 +311,15 @@ protected function parse_co($qrec, $question) {
290 311
         foreach ($answers as $key => $value) {
291 312
             $value = trim($value);
292 313
             if (strlen($value) > 0) {
293  
-                $question->answer[$key] = $value;
294  
-                $question->fraction[$key] = 1;
295  
-                $question->feedback[$key] = $this->text_field("Correct");
  314
+                $question->answer[] = $value;
  315
+                $question->fraction[] = 1;
  316
+                $question->feedback[] = $this->text_field(get_string('correct', 'question'));
296 317
             }
297 318
         }
  319
+        $question->answer[] = '*';
  320
+        $question->fraction[] = 0;
  321
+        $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
  322
+
298 323
         return $question;
299 324
     }
300 325
 
@@ -318,10 +343,10 @@ protected function parse_nr($qrec, $question) {
318 343
             $value = trim($value);
319 344
             if (is_numeric($value)) {
320 345
                 $errormargin = 0;
321  
-                $question->answer[$key] = $value;
322  
-                $question->fraction[$key] = 1;
323  
-                $question->feedback[$key] = $this->text_field("Correct");
324  
-                $question->tolerance[$key] = $errormargin;
  346
+                $question->answer[] = $value;
  347
+                $question->fraction[] = 1;
  348
+                $question->feedback[] = $this->text_field(get_string('correct', 'question'));
  349
+                $question->tolerance[] = $errormargin;
325 350
             }
326 351
         }
327 352
         return $question;

0 notes on commit 4b4373e

Please sign in to comment.
Something went wrong with that request. Please try again.