2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Aiken format question importer.
22 * @copyright 2003 Tom Robb <tom@robb.net>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
31 * Aiken format - a simple format for creating multiple choice questions (with
32 * only one correct choice, and no feedback).
34 * The format looks like this:
44 * + question text all one one line.
45 * + then a number of choices, one to a line. Each line must comprise a letter,
46 * then ')' or '.', then a space, then the choice text.
47 * + Then a line of the form 'ANSWER: X' to indicate the correct answer.
49 * Be sure to word "All of the above" type choices like "All of these" in
50 * case choices are being shuffled.
52 * @copyright 2003 Tom Robb <tom@robb.net>
53 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
55 class qformat_aiken extends qformat_default {
57 public function provide_import() {
61 public function readquestions($lines) {
63 $question = $this->defaultquestion();
65 foreach ($lines as $line) {
66 $stp = strpos($line, $endchar, 0);
67 $newlines = explode($endchar, $line);
69 $linescount = count($newlines);
70 for ($i=0; $i < $linescount; $i++) {
71 $nowline = trim($newlines[$i]);
72 // Go through the array and build an object called $question
73 // When done, add $question to $questions
74 if (strlen($nowline) < 2) {
77 if (preg_match('/^[A-Z][).][ \t]/', $nowline)) {
78 // A choice. Trim off the label and space, then save
79 $question->answer[] = $this->text_field(
80 htmlspecialchars(trim(substr($nowline, 2)), ENT_NOQUOTES));
81 $question->fraction[] = 0;
82 $question->feedback[] = $this->text_field('');
83 } else if (preg_match('/^ANSWER:/', $nowline)) {
84 // The line that indicates the correct answer. This question is finised.
85 $ans = trim(substr($nowline, strpos($nowline, ':') + 1));
86 $ans = substr($ans, 0, 1);
87 // We want to map A to 0, B to 1, etc.
88 $rightans = ord($ans) - ord('A');
89 $question->fraction[$rightans] = 1;
90 $questions[] = $question;
92 // Clear array for next question set
93 $question = $this->defaultquestion();
96 // Must be the first line of a new question, since no recognised prefix.
97 $question->qtype = MULTICHOICE;
98 $question->name = shorten_text(s($nowline), 50);
99 $question->questiontext = htmlspecialchars(trim($nowline), ENT_NOQUOTES);
100 $question->questiontextformat = FORMAT_HTML;
101 $question->generalfeedback = '';
102 $question->generalfeedbackformat = FORMAT_HTML;
103 $question->single = 1;
104 $question->answer = array();
105 $question->fraction = array();
106 $question->feedback = array();
107 $question->correctfeedback = $this->text_field('');
108 $question->partiallycorrectfeedback = $this->text_field('');
109 $question->incorrectfeedback = $this->text_field('');
116 protected function text_field($text) {
118 'text' => htmlspecialchars(trim($text), ENT_NOQUOTES),
119 'format' => FORMAT_HTML,
124 public function readquestion($lines) {
125 //this is no longer needed but might still be called by default.php