Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'MDL-34808_22' of git://github.com/jmvedrine/moodle into…

… MOODLE_22_STABLE
commit 65329711051cded156cfef0ca81951e78f7da375 2 parents 3c061d5 + 4b4373e
@danpoltawski danpoltawski authored
Showing with 64 additions and 39 deletions.
  1. +64 −39 question/format/examview/format.php
View
103 question/format/examview/format.php
@@ -17,8 +17,7 @@
/**
* Examview question importer.
*
- * @package qformat
- * @subpackage examview
+ * @package qformat_examview
* @copyright 2005 Howard Miller
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@@ -35,7 +34,7 @@
* @copyright 2005 Howard Miller
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-class qformat_examview extends qformat_default {
+class qformat_examview extends qformat_based_on_xml {
public $qtypes = array(
'tf' => TRUEFALSE,
@@ -49,8 +48,8 @@ class qformat_examview extends qformat_default {
'es' => ESSAY,
'ca' => 99,
'ot' => 99,
- 'sa' => SHORTANSWER
- );
+ 'sa' => SHORTANSWER,
+ );
public $matching_questions = array();
@@ -63,6 +62,28 @@ public function mime_type() {
}
/**
+ * Some softwares put entities in exported files.
+ * This method try to clean up known problems.
+ * @param string str string to correct
+ * @return string the corrected string
+ */
+ public function cleaninput($str) {
+
+ $html_code_list = array(
+ "'" => "'",
+ "’" => "'",
+ "“" => "\"",
+ "”" => "\"",
+ "–" => "-",
+ "—" => "-",
+ );
+ $str = strtr($str, $html_code_list);
+ // Use textlib entities_to_utf8 function to convert only numerical entities.
+ $str = textlib::entities_to_utf8( $str, false);
+ return $str;
+ }
+
+ /**
* unxmlise reconstructs part of the xml data structure in order
* to identify the actual data therein
* @param array $xml section of the xml data structure
@@ -87,13 +108,6 @@ protected function unxmlise( $xml ) {
$text = strip_tags($text);
return $text;
}
- protected function text_field($text) {
- return array(
- 'text' => htmlspecialchars(trim($text), ENT_NOQUOTES),
- 'format' => FORMAT_HTML,
- 'files' => array(),
- );
- }
protected function add_blank_combined_feedback($question) {
$question->correctfeedback['text'] = '';
@@ -108,7 +122,7 @@ protected function add_blank_combined_feedback($question) {
return $question;
}
- protected function parse_matching_groups($matching_groups) {
+ public function parse_matching_groups($matching_groups) {
if (empty($matching_groups)) {
return;
}
@@ -136,8 +150,7 @@ protected function parse_ma($qrec, $groupname) {
$phrase = trim($this->unxmlise($qrec['text']['0']['#']));
$answer = trim($this->unxmlise($qrec['answer']['0']['#']));
$answer = strip_tags( $answer );
- $match_group->subquestions[] = $phrase;
- $match_group->subanswers[] = $match_group->subchoices[$answer];
+ $match_group->mappings[$phrase] = $match_group->subchoices[$answer];
$this->matching_questions[$groupname] = $match_group;
return null;
}
@@ -146,6 +159,7 @@ protected function process_matches(&$questions) {
if (empty($this->matching_questions)) {
return;
}
+
foreach ($this->matching_questions as $match_group) {
$question = $this->defaultquestion();
$htmltext = s($match_group->questiontext);
@@ -157,12 +171,17 @@ protected function process_matches(&$questions) {
$question = $this->add_blank_combined_feedback($question);
$question->subquestions = array();
$question->subanswers = array();
- foreach ($match_group->subquestions as $key => $value) {
- $htmltext = s($value);
- $question->subquestions[] = $this->text_field($htmltext);
-
- $htmltext = s($match_group->subanswers[$key]);
- $question->subanswers[] = $htmltext;
+ foreach ($match_group->subchoices as $subchoice) {
+ $fiber = array_keys ($match_group->mappings, $subchoice);
+ $subquestion = '';
+ foreach ($fiber as $subquestion) {
+ $question->subquestions[] = $this->text_field($subquestion);
+ $question->subanswers[] = $subchoice;
+ }
+ if ($subquestion == '') { // Then in this case, $subchoice is a distractor.
+ $question->subquestions[] = $this->text_field('');
+ $question->subanswers[] = $subchoice;
+ }
}
$questions[] = $question;
}
@@ -172,7 +191,7 @@ protected function cleanunicode($text) {
return str_replace('’', "'", $text);
}
- protected function readquestions($lines) {
+ public function readquestions($lines) {
// Parses an array of lines into an array of questions,
// where each item is a question object as defined by
// readquestion().
@@ -209,9 +228,11 @@ public function readquestion($qrec) {
$question->qtype = null;
}
$question->single = 1;
+
// Only one answer is allowed.
$htmltext = $this->unxmlise($qrec['#']['text'][0]['#']);
- $question->questiontext = $htmltext;
+
+ $question->questiontext = $this->cleaninput($htmltext);
$question->questiontextformat = FORMAT_HTML;
$question->questiontextfiles = array();
$question->name = shorten_text( $question->questiontext, 250 );
@@ -251,11 +272,11 @@ protected function parse_tf_yn($qrec, $question) {
$question->answer = $choices[$answer];
$question->correctanswer = $question->answer;
if ($question->answer == 1) {
- $question->feedbacktrue = $this->text_field('Correct');
- $question->feedbackfalse = $this->text_field('Incorrect');
+ $question->feedbacktrue = $this->text_field(get_string('correct', 'question'));
+ $question->feedbackfalse = $this->text_field(get_string('incorrect', 'question'));
} else {
- $question->feedbacktrue = $this->text_field('Incorrect');
- $question->feedbackfalse = $this->text_field('Correct');
+ $question->feedbacktrue = $this->text_field(get_string('incorrect', 'question'));
+ $question->feedbackfalse = $this->text_field(get_string('correct', 'question'));
}
return $question;
}
@@ -268,13 +289,13 @@ protected function parse_mc($qrec, $question) {
foreach ($choices as $key => $value) {
if (strpos(trim($key), 'choice-') !== false) {
- $question->answer[$key] = $this->text_field(s($this->unxmlise($value[0]['#'])));
+ $question->answer[] = $this->text_field(s($this->unxmlise($value[0]['#'])));
if (strcmp($key, $answer) == 0) {
- $question->fraction[$key] = 1;
- $question->feedback[$key] = $this->text_field('Correct');
+ $question->fraction[] = 1;
+ $question->feedback[] = $this->text_field(get_string('correct', 'question'));
} else {
- $question->fraction[$key] = 0;
- $question->feedback[$key] = $this->text_field('Incorrect');
+ $question->fraction[] = 0;
+ $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
}
}
}
@@ -290,11 +311,15 @@ protected function parse_co($qrec, $question) {
foreach ($answers as $key => $value) {
$value = trim($value);
if (strlen($value) > 0) {
- $question->answer[$key] = $value;
- $question->fraction[$key] = 1;
- $question->feedback[$key] = $this->text_field("Correct");
+ $question->answer[] = $value;
+ $question->fraction[] = 1;
+ $question->feedback[] = $this->text_field(get_string('correct', 'question'));
}
}
+ $question->answer[] = '*';
+ $question->fraction[] = 0;
+ $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
+
return $question;
}
@@ -318,10 +343,10 @@ protected function parse_nr($qrec, $question) {
$value = trim($value);
if (is_numeric($value)) {
$errormargin = 0;
- $question->answer[$key] = $value;
- $question->fraction[$key] = 1;
- $question->feedback[$key] = $this->text_field("Correct");
- $question->tolerance[$key] = $errormargin;
+ $question->answer[] = $value;
+ $question->fraction[] = 1;
+ $question->feedback[] = $this->text_field(get_string('correct', 'question'));
+ $question->tolerance[] = $errormargin;
}
}
return $question;
Please sign in to comment.
Something went wrong with that request. Please try again.