Skip to content

Commit

Permalink
MDL-34808 qformat examview Add phpunit tests to examview import format
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Michel Vedrine committed Aug 19, 2012
1 parent 7033316 commit 1ff3389
Show file tree
Hide file tree
Showing 4 changed files with 531 additions and 43 deletions.
103 changes: 64 additions & 39 deletions question/format/examview/format.php
Expand Up @@ -17,8 +17,7 @@
/** /**
* Examview question importer. * Examview question importer.
* *
* @package qformat * @package qformat_examview
* @subpackage examview
* @copyright 2005 Howard Miller * @copyright 2005 Howard Miller
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
Expand All @@ -35,7 +34,7 @@
* @copyright 2005 Howard Miller * @copyright 2005 Howard Miller
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @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( public $qtypes = array(
'tf' => TRUEFALSE, 'tf' => TRUEFALSE,
Expand All @@ -49,8 +48,8 @@ class qformat_examview extends qformat_default {
'es' => ESSAY, 'es' => ESSAY,
'ca' => 99, 'ca' => 99,
'ot' => 99, 'ot' => 99,
'sa' => SHORTANSWER 'sa' => SHORTANSWER,
); );


public $matching_questions = array(); public $matching_questions = array();


Expand All @@ -62,6 +61,28 @@ public function mime_type() {
return 'application/xml'; return 'application/xml';
} }


/**
* 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 * unxmlise reconstructs part of the xml data structure in order
* to identify the actual data therein * to identify the actual data therein
Expand All @@ -87,13 +108,6 @@ protected function unxmlise( $xml ) {
$text = strip_tags($text); $text = strip_tags($text);
return $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) { protected function add_blank_combined_feedback($question) {
$question->correctfeedback['text'] = ''; $question->correctfeedback['text'] = '';
Expand All @@ -108,7 +122,7 @@ protected function add_blank_combined_feedback($question) {
return $question; return $question;
} }


protected function parse_matching_groups($matching_groups) { public function parse_matching_groups($matching_groups) {
if (empty($matching_groups)) { if (empty($matching_groups)) {
return; return;
} }
Expand Down Expand Up @@ -136,8 +150,7 @@ protected function parse_ma($qrec, $groupname) {
$phrase = trim($this->unxmlise($qrec['text']['0']['#'])); $phrase = trim($this->unxmlise($qrec['text']['0']['#']));
$answer = trim($this->unxmlise($qrec['answer']['0']['#'])); $answer = trim($this->unxmlise($qrec['answer']['0']['#']));
$answer = strip_tags( $answer ); $answer = strip_tags( $answer );
$match_group->subquestions[] = $phrase; $match_group->mappings[$phrase] = $match_group->subchoices[$answer];
$match_group->subanswers[] = $match_group->subchoices[$answer];
$this->matching_questions[$groupname] = $match_group; $this->matching_questions[$groupname] = $match_group;
return null; return null;
} }
Expand All @@ -146,6 +159,7 @@ protected function process_matches(&$questions) {
if (empty($this->matching_questions)) { if (empty($this->matching_questions)) {
return; return;
} }

foreach ($this->matching_questions as $match_group) { foreach ($this->matching_questions as $match_group) {
$question = $this->defaultquestion(); $question = $this->defaultquestion();
$htmltext = s($match_group->questiontext); $htmltext = s($match_group->questiontext);
Expand All @@ -157,12 +171,17 @@ protected function process_matches(&$questions) {
$question = $this->add_blank_combined_feedback($question); $question = $this->add_blank_combined_feedback($question);
$question->subquestions = array(); $question->subquestions = array();
$question->subanswers = array(); $question->subanswers = array();
foreach ($match_group->subquestions as $key => $value) { foreach ($match_group->subchoices as $subchoice) {
$htmltext = s($value); $fiber = array_keys ($match_group->mappings, $subchoice);
$question->subquestions[] = $this->text_field($htmltext); $subquestion = '';

foreach ($fiber as $subquestion) {
$htmltext = s($match_group->subanswers[$key]); $question->subquestions[] = $this->text_field($subquestion);
$question->subanswers[] = $htmltext; $question->subanswers[] = $subchoice;
}
if ($subquestion == '') { // Then in this case, $subchoice is a distractor.
$question->subquestions[] = $this->text_field('');
$question->subanswers[] = $subchoice;
}
} }
$questions[] = $question; $questions[] = $question;
} }
Expand All @@ -172,7 +191,7 @@ protected function cleanunicode($text) {
return str_replace('’', "'", $text); return str_replace('’', "'", $text);
} }


protected function readquestions($lines) { public function readquestions($lines) {
// Parses an array of lines into an array of questions, // Parses an array of lines into an array of questions,
// where each item is a question object as defined by // where each item is a question object as defined by
// readquestion(). // readquestion().
Expand Down Expand Up @@ -209,9 +228,11 @@ public function readquestion($qrec) {
$question->qtype = null; $question->qtype = null;
} }
$question->single = 1; $question->single = 1;

// Only one answer is allowed. // Only one answer is allowed.
$htmltext = $this->unxmlise($qrec['#']['text'][0]['#']); $htmltext = $this->unxmlise($qrec['#']['text'][0]['#']);
$question->questiontext = $htmltext;
$question->questiontext = $this->cleaninput($htmltext);
$question->questiontextformat = FORMAT_HTML; $question->questiontextformat = FORMAT_HTML;
$question->questiontextfiles = array(); $question->questiontextfiles = array();
$question->name = shorten_text( $question->questiontext, 250 ); $question->name = shorten_text( $question->questiontext, 250 );
Expand Down Expand Up @@ -251,11 +272,11 @@ protected function parse_tf_yn($qrec, $question) {
$question->answer = $choices[$answer]; $question->answer = $choices[$answer];
$question->correctanswer = $question->answer; $question->correctanswer = $question->answer;
if ($question->answer == 1) { if ($question->answer == 1) {
$question->feedbacktrue = $this->text_field('Correct'); $question->feedbacktrue = $this->text_field(get_string('correct', 'question'));
$question->feedbackfalse = $this->text_field('Incorrect'); $question->feedbackfalse = $this->text_field(get_string('incorrect', 'question'));
} else { } else {
$question->feedbacktrue = $this->text_field('Incorrect'); $question->feedbacktrue = $this->text_field(get_string('incorrect', 'question'));
$question->feedbackfalse = $this->text_field('Correct'); $question->feedbackfalse = $this->text_field(get_string('correct', 'question'));
} }
return $question; return $question;
} }
Expand All @@ -268,13 +289,13 @@ protected function parse_mc($qrec, $question) {
foreach ($choices as $key => $value) { foreach ($choices as $key => $value) {
if (strpos(trim($key), 'choice-') !== false) { 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) { if (strcmp($key, $answer) == 0) {
$question->fraction[$key] = 1; $question->fraction[] = 1;
$question->feedback[$key] = $this->text_field('Correct'); $question->feedback[] = $this->text_field(get_string('correct', 'question'));
} else { } else {
$question->fraction[$key] = 0; $question->fraction[] = 0;
$question->feedback[$key] = $this->text_field('Incorrect'); $question->feedback[] = $this->text_field(get_string('incorrect', 'question'));
} }
} }
} }
Expand All @@ -290,11 +311,15 @@ protected function parse_co($qrec, $question) {
foreach ($answers as $key => $value) { foreach ($answers as $key => $value) {
$value = trim($value); $value = trim($value);
if (strlen($value) > 0) { if (strlen($value) > 0) {
$question->answer[$key] = $value; $question->answer[] = $value;
$question->fraction[$key] = 1; $question->fraction[] = 1;
$question->feedback[$key] = $this->text_field("Correct"); $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; return $question;
} }


Expand All @@ -318,10 +343,10 @@ protected function parse_nr($qrec, $question) {
$value = trim($value); $value = trim($value);
if (is_numeric($value)) { if (is_numeric($value)) {
$errormargin = 0; $errormargin = 0;
$question->answer[$key] = $value; $question->answer[] = $value;
$question->fraction[$key] = 1; $question->fraction[] = 1;
$question->feedback[$key] = $this->text_field("Correct"); $question->feedback[] = $this->text_field(get_string('correct', 'question'));
$question->tolerance[$key] = $errormargin; $question->tolerance[] = $errormargin;
} }
} }
return $question; return $question;
Expand Down
3 changes: 1 addition & 2 deletions question/format/examview/lang/en/qformat_examview.php
Expand Up @@ -17,8 +17,7 @@
/** /**
* Strings for component 'qformat_examview', language 'en', branch 'MOODLE_20_STABLE' * Strings for component 'qformat_examview', language 'en', branch 'MOODLE_20_STABLE'
* *
* @package qformat * @package qformat_examview
* @subpackage examview
* @copyright 2010 Helen Foster * @copyright 2010 Helen Foster
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
Expand Down

0 comments on commit 1ff3389

Please sign in to comment.