class qformat_learnwise in Quiz 6.6
Same name and namespace in other branches
- 6.5 includes/moodle/question/format/learnwise/format.php \qformat_learnwise
@package questionbank @subpackage importexport
Hierarchy
- class \qformat_default
- class \qformat_learnwise
Expanded class hierarchy of qformat_learnwise
File
- includes/
moodle/ question/ format/ learnwise/ format.php, line 13
View source
class qformat_learnwise extends qformat_default {
function provide_import() {
return true;
}
function readquestions($lines) {
$questions = array();
$currentquestion = array();
foreach ($lines as $line) {
$line = trim($line);
$currentquestion[] = $line;
if ($question = $this
->readquestion($currentquestion)) {
$questions[] = $question;
$currentquestion = array();
}
}
return $questions;
}
function readquestion($lines) {
$text = implode(' ', $lines);
$text = str_replace(array(
'\\t',
'\\n',
'\\r',
'\'',
), array(
'',
'',
'',
'\\\'',
), $text);
$startpos = strpos($text, '<question type');
$endpos = strpos($text, '</question>');
if ($startpos === false || $endpos === false) {
return false;
}
preg_match("/<question type=[\"\\']([^\"\\']+)[\"\\']>/i", $text, $matches);
$type = strtolower($matches[1]);
// multichoice or multianswerchoice
$questiontext = $this
->unhtmlentities($this
->stringbetween($text, '<text>', '</text>'));
$questionhint = $this
->unhtmlentities($this
->stringbetween($text, '<hint>', '</hint>'));
$questionaward = $this
->stringbetween($text, '<award>', '</award>');
$optionlist = $this
->stringbetween($text, '<answer>', '</answer>');
$optionlist = explode('<option', $optionlist);
$n = 0;
$optionscorrect = array();
$optionstext = array();
if ($type == 'multichoice') {
foreach ($optionlist as $option) {
$correct = $this
->stringbetween($option, ' correct="', '">');
$answer = $this
->stringbetween($option, '">', '</option>');
$optionscorrect[$n] = $correct;
$optionstext[$n] = $this
->unhtmlentities($answer);
++$n;
}
}
else {
if ($type == 'multianswerchoice') {
$numcorrect = 0;
$totalaward = 0;
$optionsaward = array();
foreach ($optionlist as $option) {
preg_match("/correct=\"([^\"]*)\"/i", $option, $correctmatch);
preg_match("/award=\"([^\"]*)\"/i", $option, $awardmatch);
$correct = $correctmatch[1];
$award = $awardmatch[1];
if ($correct == 'yes') {
$totalaward += $award;
++$numcorrect;
}
$answer = $this
->stringbetween($option, '">', '</option>');
$optionscorrect[$n] = $correct;
$optionstext[$n] = $this
->unhtmlentities($answer);
$optionsaward[$n] = $award;
++$n;
}
}
else {
echo "<p>I don't understand this question type (type = <strong>{$type}</strong>).</p>\n";
}
}
$question = $this
->defaultquestion();
$question->qtype = MULTICHOICE;
$question->name = substr($questiontext, 0, 30);
if (strlen($questiontext) > 30) {
$question->name .= '...';
}
$question->questiontext = $questiontext;
$question->single = $type == 'multichoice' ? 1 : 0;
$question->feedback[] = '';
$question->fraction = array();
$question->answer = array();
for ($n = 0; $n < count($optionstext); ++$n) {
if ($optionstext[$n]) {
if (!isset($numcorrect)) {
// single answer
if ($optionscorrect[$n] == 'yes') {
$fraction = (int) $questionaward;
}
else {
$fraction = 0;
}
}
else {
// mulitple answers
if ($optionscorrect[$n] == 'yes') {
$fraction = $optionsaward[$n] / $totalaward;
}
else {
$fraction = -$optionsaward[$n] / count($optionstext);
}
}
$question->fraction[] = $fraction;
$question->answer[] = $optionstext[$n];
$question->feedback[] = '';
// no feedback in this type
}
}
return $question;
}
function stringbetween($text, $start, $end) {
$startpos = strpos($text, $start) + strlen($start);
$endpos = strpos($text, $end);
if ($startpos <= $endpos) {
return substr($text, $startpos, $endpos - $startpos);
}
}
function unhtmlentities($string) {
$transtable = get_html_translation_table(HTML_ENTITIES);
$transtable = array_flip($transtable);
return strtr($string, $transtable);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
qformat_default:: |
property | |||
qformat_default:: |
property | |||
qformat_default:: |
property | |||
qformat_default:: |
property | |||
qformat_default:: |
property | |||
qformat_default:: |
property | |||
qformat_default:: |
property | |||
qformat_default:: |
property | |||
qformat_default:: |
property | |||
qformat_default:: |
property | |||
qformat_default:: |
property | |||
qformat_default:: |
property | |||
qformat_default:: |
property | |||
qformat_default:: |
property | |||
qformat_default:: |
property | |||
qformat_default:: |
property | |||
qformat_default:: |
function | Count all non-category questions in the questions array. | ||
qformat_default:: |
function | find and/or create the category described by a delimited list e.g. $course$/tom/dick/harry or tom/dick/harry | ||
qformat_default:: |
function | return an "empty" question Somewhere to specify question parameters that are not handled by import but are required db fields. This should not be overridden. | ||
qformat_default:: |
function | Handle parsing error | ||
qformat_default:: |
function | Do an post-processing that may be required | ||
qformat_default:: |
function | Do any pre-processing that may be required | 1 | |
qformat_default:: |
function | Do the export For most types this should not need to be overrided | 1 | |
qformat_default:: |
function | Return the files extension appropriate for this type override if you don't want .txt | 3 | |
qformat_default:: |
function | where question specifies a moodle (text) format this performs the conversion. | ||
qformat_default:: |
function | get the category as a path (e.g., tom/dick/harry) | ||
qformat_default:: |
function | Import an image file encoded in base64 format | ||
qformat_default:: |
function | Override if any post-processing is required | 2 | |
qformat_default:: |
function | Perform any required pre-processing | 2 | |
qformat_default:: |
function | Process the file This method should not normally be overidden | 1 | |
qformat_default:: |
function | Enable any processing to be done on the content just prior to the file being saved default is to do nothing | 2 | |
qformat_default:: |
function | 4 | ||
qformat_default:: |
function | get directory into which export is going | ||
qformat_default:: |
function | Return complete file within an array, one item per line | 1 | |
qformat_default:: |
function | set the category | ||
qformat_default:: |
function | set catfromfile | ||
qformat_default:: |
function | set cattofile | ||
qformat_default:: |
function | set contextfromfile | ||
qformat_default:: |
function | set an array of contexts. | ||
qformat_default:: |
function | set contexttofile | ||
qformat_default:: |
function | set the course class variable | ||
qformat_default:: |
function | set the filename | ||
qformat_default:: |
function | set matchgrades | ||
qformat_default:: |
function | Set the specific questions to export. Should not include questions with parents (sub questions of cloze question type). Only used for question export. | ||
qformat_default:: |
function | set the "real" filename (this is what the user typed, regardless of wha happened next) | ||
qformat_default:: |
function | set stoponerror | ||
qformat_default:: |
function | |||
qformat_default:: |
function | Provide export functionality for plugin questiontypes Do not override | ||
qformat_default:: |
function | Import for questiontype plugins Do not override. | ||
qformat_default:: |
function | convert a single question object into text output in the given format. This must be overriden | 4 | |
qformat_learnwise:: |
function |
Overrides qformat_default:: |
||
qformat_learnwise:: |
function |
Given the data known to define a question in
this format, this function converts it into a question
object suitable for processing and insertion into Moodle. Overrides qformat_default:: |
||
qformat_learnwise:: |
function |
Parses an array of lines into an array of questions,
where each item is a question object as defined by
readquestion(). Questions are defined as anything
between blank lines. Overrides qformat_default:: |
||
qformat_learnwise:: |
function | |||
qformat_learnwise:: |
function |