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[] = htmlspecialchars(trim(substr($nowline, 2)), ENT_NOQUOTES);
80 $question->fraction[] = 0;
81 $question->feedback[] = '';
84 if (preg_match('/^ANSWER:/', $nowline)) {
85 // The line that indicates the correct answer. This question is finised.
86 $ans = trim(substr($nowline, strpos($nowline, ':') + 1));
87 $ans = substr($ans, 0, 1);
88 // We want to map A to 0, B to 1, etc.
89 $rightans = ord($ans) - ord('A');
90 $question->fraction[$rightans] = 1;
91 $questions[] = $question;
93 // Clear array for next question set
94 $question = $this->defaultquestion();
97 // Must be the first line of a new question, since no recognised prefix.
98 $question->qtype = MULTICHOICE;
99 $question->name = shorten_text(s($nowline), 50);
100 $question->questiontext = s($nowline);
101 $question->single = 1;
102 $question->feedback[] = '';
109 public function readquestion($lines) {
110 //this is no longer needed but might still be called by default.php