MDL-28438 Aiken question import broken since MDL-24594.
[moodle.git] / question / format / aiken / 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
17/**
18 * Aiken format question importer.
19 *
20 * @package qformat
21 * @subpackage aiken
22 * @copyright 2003 Tom Robb <tom@robb.net>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
f0c1799e 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
29
47de80c7 30/**
31 * Aiken format - a simple format for creating multiple choice questions (with
32 * only one correct choice, and no feedback).
33 *
34 * The format looks like this:
35 *
36 * Question text
37 * A) Choice #1
38 * B) Choice #2
39 * C) Choice #3
40 * D) Choice #4
41 * ANSWER: B
42 *
43 * That is,
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.
48 *
49 * Be sure to word "All of the above" type choices like "All of these" in
50 * case choices are being shuffled.
d3603157
TH
51 *
52 * @copyright 2003 Tom Robb <tom@robb.net>
53 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47de80c7 54 */
f0c1799e 55class qformat_aiken extends qformat_default {
56
c7df5006
TH
57 public function provide_import() {
58 return true;
59 }
f0c1799e 60
c7df5006 61 public function readquestions($lines) {
f0c1799e 62 $questions = array();
63 $question = $this->defaultquestion();
aeb15530 64 $endchar = chr(13);
f0c1799e 65 foreach ($lines as $line) {
47de80c7 66 $stp = strpos($line, $endchar, 0);
67 $newlines = explode($endchar, $line);
f0c1799e 68 $foundQ = 0;
c951bf6e 69 $linescount = count($newlines);
70 for ($i=0; $i < $linescount; $i++) {
5712be1c 71 $nowline = trim($newlines[$i]);
47de80c7 72 // Go through the array and build an object called $question
73 // When done, add $question to $questions
5712be1c 74 if (strlen($nowline) < 2) {
f0c1799e 75 continue;
76 }
47de80c7 77 if (preg_match('/^[A-Z][).][ \t]/', $nowline)) {
78 // A choice. Trim off the label and space, then save
45bdcf11
TH
79 $question->answer[] = $this->text_field(
80 htmlspecialchars(trim(substr($nowline, 2)), ENT_NOQUOTES));
f0c1799e 81 $question->fraction[] = 0;
45bdcf11
TH
82 $question->feedback[] = $this->text_field('');
83 } else if (preg_match('/^ANSWER:/', $nowline)) {
47de80c7 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.
5712be1c 88 $rightans = ord($ans) - ord('A');
f0c1799e 89 $question->fraction[$rightans] = 1;
90 $questions[] = $question;
47de80c7 91
92 // Clear array for next question set
f0c1799e 93 $question = $this->defaultquestion();
94 continue;
95 } else {
47de80c7 96 // Must be the first line of a new question, since no recognised prefix.
f0c1799e 97 $question->qtype = MULTICHOICE;
1e20dea9 98 $question->name = shorten_text(s($nowline), 50);
45bdcf11
TH
99 $question->questiontext = htmlspecialchars(trim($nowline), ENT_NOQUOTES);
100 $question->questiontextformat = FORMAT_HTML;
101 $question->generalfeedback = '';
102 $question->generalfeedbackformat = FORMAT_HTML;
f0c1799e 103 $question->single = 1;
45bdcf11
TH
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('');
f0c1799e 110 }
111 }
112 }
113 return $questions;
114 }
115
45bdcf11
TH
116 protected function text_field($text) {
117 return array(
118 'text' => htmlspecialchars(trim($text), ENT_NOQUOTES),
119 'format' => FORMAT_HTML,
120 'files' => array(),
121 );
122 }
123
c7df5006 124 public function readquestion($lines) {
f0c1799e 125 //this is no longer needed but might still be called by default.php
126 return;
127 }
128}
129
aeb15530 130