weekly release 2.3.3+
[moodle.git] / question / format / learnwise / format.php
CommitLineData
14e31fa3 1<?php
d3603157
TH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
41a89a07 17/**
d3603157
TH
18 * Examview question importer.
19 *
20 * @package qformat
21 * @subpackage learnwise
22 * @copyright 2005 Alton College, Hampshire, UK
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41a89a07 24 */
f5565b69 25
d3603157 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
29
d3603157
TH
30/**
31 * Examview question importer.
32 *
33 * Alton College, Hampshire, UK - Tom Flannaghan, Andrew Walker
34 *
5e8a85aa 35 * Imports learnwise multiple choice questions (single and multiple answers)
d3603157
TH
36 * currently ignores the deduct attribute for multiple answer questions
37 * deductions are currently simply found by dividing the award for the incorrect
38 * answer by the total number of options
39 *
40 * @copyright 2005 Alton College, Hampshire, UK
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
f5565b69 43class qformat_learnwise extends qformat_default {
44
45 function provide_import() {
d3603157 46 return true;
f5565b69 47 }
48
4d188926 49 protected function readquestions($lines) {
f5565b69 50 $questions = array();
51 $currentquestion = array();
2befe778 52
f5565b69 53 foreach($lines as $line) {
54 $line = trim($line);
55 $currentquestion[] = $line;
2befe778 56
f5565b69 57 if ($question = $this->readquestion($currentquestion)) {
58 $questions[] = $question;
59 $currentquestion = array();
60 }
2befe778 61 }
f5565b69 62 return $questions;
63 }
64
65 function readquestion($lines) {
66 $text = implode(' ', $lines);
67 $text = str_replace(array('\t','\n','\r','\''), array('','','','\\\''), $text);
68
69 $startpos = strpos($text, '<question type');
70 $endpos = strpos($text, '</question>');
71 if ($startpos === false || $endpos === false) {
72 return false;
73 }
74
75 preg_match("/<question type=[\"\']([^\"\']+)[\"\']>/i", $text, $matches);
76 $type = strtolower($matches[1]); // multichoice or multianswerchoice
77
78 $questiontext = $this->unhtmlentities($this->stringbetween($text, '<text>', '</text>'));
79 $questionhint = $this->unhtmlentities($this->stringbetween($text, '<hint>', '</hint>'));
80 $questionaward = $this->stringbetween($text, '<award>', '</award>');
81 $optionlist = $this->stringbetween($text, '<answer>', '</answer>');
82
83 $optionlist = explode('<option', $optionlist);
84
85 $n = 0;
86
87 $optionscorrect = array();
88 $optionstext = array();
89
90 if ($type == 'multichoice') {
91 foreach ($optionlist as $option) {
92 $correct = $this->stringbetween($option, ' correct="', '">');
93 $answer = $this->stringbetween($option, '">', '</option>');
aeb15530 94 $optionscorrect[$n] = $correct;
f5565b69 95 $optionstext[$n] = $this->unhtmlentities($answer);
96 ++$n;
97 }
98 } else if ($type == 'multianswerchoice') {
99 $numcorrect = 0;
100 $totalaward = 0;
101
102 $optionsaward = array();
103
104 foreach ($optionlist as $option) {
105 preg_match("/correct=\"([^\"]*)\"/i", $option, $correctmatch);
106 preg_match("/award=\"([^\"]*)\"/i", $option, $awardmatch);
107
108 $correct = $correctmatch[1];
109 $award = $awardmatch[1];
110 if ($correct == 'yes') {
111 $totalaward += $award;
112 ++$numcorrect;
113 }
114
115 $answer = $this->stringbetween($option, '">', '</option>');
116
aeb15530 117 $optionscorrect[$n] = $correct;
f5565b69 118 $optionstext[$n] = $this->unhtmlentities($answer);
119 $optionsaward[$n] = $award;
120 ++$n;
121 }
122
123 } else {
124 echo "<p>I don't understand this question type (type = <strong>$type</strong>).</p>\n";
125 }
126
127 $question = $this->defaultquestion();
128 $question->qtype = MULTICHOICE;
5df0a6b4 129 $question->name = $this->create_default_question_name($questiontext, get_string('questionname', 'question'));
f5565b69 130
131 $question->questiontext = $questiontext;
132 $question->single = ($type == 'multichoice') ? 1 : 0;
133 $question->feedback[] = '';
2befe778 134
f5565b69 135 $question->fraction = array();
136 $question->answer = array();
137 for ($n = 0; $n < count($optionstext); ++$n) {
138 if ($optionstext[$n]) {
139 if (!isset($numcorrect)) { // single answer
140 if ($optionscorrect[$n] == 'yes') {
aeb15530 141 $fraction = (int) $questionaward;
f5565b69 142 } else {
143 $fraction = 0;
aeb15530 144 }
f5565b69 145 } else { // mulitple answers
146 if ($optionscorrect[$n] == 'yes') {
aeb15530 147 $fraction = $optionsaward[$n] / $totalaward;
f5565b69 148 } else {
149 $fraction = -$optionsaward[$n] / count($optionstext);
150 }
151 }
152 $question->fraction[] = $fraction;
153 $question->answer[] = $optionstext[$n];
154 $question->feedback[] = ''; // no feedback in this type
155 }
156 }
157
158 return $question;
159 }
160
161 function stringbetween($text, $start, $end) {
162 $startpos = strpos($text, $start) + strlen($start);
163 $endpos = strpos($text, $end);
2befe778 164
f5565b69 165 if ($startpos <= $endpos) {
166 return substr($text, $startpos, $endpos - $startpos);
167 }
168 }
169
170 function unhtmlentities($string) {
171 $transtable = get_html_translation_table(HTML_ENTITIES);
172 $transtable = array_flip($transtable);
173 return strtr($string, $transtable);
aeb15530 174 }
f5565b69 175
176}
177
aeb15530 178