MDL-19067 removed obsoleted string (never used in any STABLE branch)
[moodle.git] / mod / lesson / format.php
CommitLineData
5491947a 1<?php // $Id$
2/**
3 * format.php - Default format class for file imports/exports. Doesn't do
4 * everything on it's own -- it needs to be extended.
5 *
6 * @version $Id$
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package lesson
9 **/
394c97c8 10
11// Included by import.php
12
90455bb3 13class qformat_default {
394c97c8 14
15 var $displayerrors = true;
16 var $category = NULL;
17 var $questionids = array();
90455bb3 18 var $qtypeconvert = array(NUMERICAL => LESSON_NUMERICAL,
19 MULTICHOICE => LESSON_MULTICHOICE,
20 TRUEFALSE => LESSON_TRUEFALSE,
21 SHORTANSWER => LESSON_SHORTANSWER,
22 MATCH => LESSON_MATCHING
23 );
394c97c8 24
25/// Importing functions
26
27 function importpreprocess() {
28 /// Does any pre-processing that may be desired
29
30 return true;
31 }
32
33 function importprocess($filename, $lesson, $pageid) {
646fc290 34 global $DB;
35
394c97c8 36 /// Processes a given file. There's probably little need to change this
37 $timenow = time();
38
39 if (! $lines = $this->readdata($filename)) {
40 notify("File could not be read, or was empty");
41 return false;
42 }
43
44 if (! $questions = $this->readquestions($lines)) { // Extract all the questions
45 notify("There are no questions in this file!");
46 return false;
47 }
90455bb3 48
49 notify(get_string('importcount', 'lesson', sizeof($questions)));
394c97c8 50
51 $count = 0;
52
53 foreach ($questions as $question) { // Process and store each question
54 switch ($question->qtype) {
55 // the good ones
56 case SHORTANSWER :
57 case NUMERICAL :
58 case TRUEFALSE :
59 case MULTICHOICE :
60 case MATCH :
61 $count++;
62
294ce987 63 echo "<hr><p><b>$count</b>. ".$question->questiontext."</p>";
f7ffb898 64 $newpage = new stdClass;
394c97c8 65 $newpage->lessonid = $lesson->id;
90455bb3 66 $newpage->qtype = $this->qtypeconvert[$question->qtype];
394c97c8 67 switch ($question->qtype) {
68 case SHORTANSWER :
90455bb3 69 if (isset($question->usecase)) {
70 $newpage->qoption = $question->usecase;
71 }
394c97c8 72 break;
73 case MULTICHOICE :
74 if (isset($question->single)) {
75 $newpage->qoption = !$question->single;
76 }
77 break;
78 }
79 $newpage->timecreated = $timenow;
80 if ($question->name != $question->questiontext) {
81 $newpage->title = $question->name;
82 } else {
83 $newpage->title = "Page $count";
84 }
85 $newpage->contents = $question->questiontext;
86
87 // set up page links
88 if ($pageid) {
89 // the new page follows on from this page
646fc290 90 if (!$page = $DB->get_record("lesson_pages", array("id" => $pageid))) {
771dc7b2 91 print_error('invalidpageid', 'lesson');
394c97c8 92 }
93 $newpage->prevpageid = $pageid;
94 $newpage->nextpageid = $page->nextpageid;
95 // insert the page and reset $pageid
646fc290 96 if (!$newpageid = $DB->insert_record("lesson_pages", $newpage)) {
1858ba0c 97 print_error('cannotinsertpage', 'lesson');
394c97c8 98 }
99 // update the linked list
646fc290 100 if (!$DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid))) {
1858ba0c 101 print_error('cannotupdateanswer', 'lesson');
394c97c8 102 }
103
104 } else {
105 // new page is the first page
106 // get the existing (first) page (if any)
646fc290 107 $params = array ("lessonid" => $lesson->id, "prevpageid" => 0);
108 if (!$page = $DB->get_record_select("lesson_pages", "lessonid = :lessonid AND prevpageid = :prevpageid", $params)) {
394c97c8 109 // there are no existing pages
110 $newpage->prevpageid = 0; // this is a first page
111 $newpage->nextpageid = 0; // this is the only page
646fc290 112 $newpageid = $DB->insert_record("lesson_pages", $newpage);
394c97c8 113 if (!$newpageid) {
1858ba0c 114 print_error('cannotinsertpage', 'lesson');
394c97c8 115 }
116 } else {
117 // there are existing pages put this at the start
118 $newpage->prevpageid = 0; // this is a first page
119 $newpage->nextpageid = $page->id;
646fc290 120 $newpageid = $DB->insert_record("lesson_pages", $newpage);
394c97c8 121 if (!$newpageid) {
1858ba0c 122 print_error('cannotinsertpage', 'lesson');
394c97c8 123 }
124 // update the linked list
646fc290 125 if (!$DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->id))) {
1858ba0c 126 print_error('cannotupdatelink', 'lesson');
394c97c8 127 }
128 }
129 }
130 // reset $pageid and put the page ID in $question, used in save_question_option()
131 $pageid = $newpageid;
132 $question->id = $newpageid;
133
134 $this->questionids[] = $question->id;
135
136 // Now to save all the answers and type-specific options
137
138 $question->lessonid = $lesson->id; // needed for foreign key
90455bb3 139 $question->qtype = $this->qtypeconvert[$question->qtype];
394c97c8 140 $result = lesson_save_question_options($question);
141
142 if (!empty($result->error)) {
143 notify($result->error);
144 return false;
145 }
146
147 if (!empty($result->notice)) {
148 notify($result->notice);
149 return true;
150 }
151 break;
152 // the Bad ones
153 default :
90455bb3 154 notify(get_string('unsupportedqtype', 'lesson', $question->qtype));
394c97c8 155 }
156
157 }
158 return true;
159 }
160
161
162 function readdata($filename) {
163 /// Returns complete file with an array, one item per line
164
165 if (is_readable($filename)) {
166 $filearray = file($filename);
167
168 /// Check for Macintosh OS line returns (ie file on one line), and fix
169 if (ereg("\r", $filearray[0]) AND !ereg("\n", $filearray[0])) {
170 return explode("\r", $filearray[0]);
171 } else {
172 return $filearray;
173 }
174 }
175 return false;
176 }
177
178 function readquestions($lines) {
179 /// Parses an array of lines into an array of questions,
180 /// where each item is a question object as defined by
181 /// readquestion(). Questions are defined as anything
182 /// between blank lines.
183
184 $questions = array();
185 $currentquestion = array();
186
187 foreach ($lines as $line) {
188 $line = trim($line);
189 if (empty($line)) {
190 if (!empty($currentquestion)) {
191 if ($question = $this->readquestion($currentquestion)) {
192 $questions[] = $question;
193 }
194 $currentquestion = array();
195 }
196 } else {
197 $currentquestion[] = $line;
198 }
199 }
200
201 if (!empty($currentquestion)) { // There may be a final question
202 if ($question = $this->readquestion($currentquestion)) {
203 $questions[] = $question;
204 }
205 }
206
207 return $questions;
208 }
209
210
211 function readquestion($lines) {
212 /// Given an array of lines known to define a question in
213 /// this format, this function converts it into a question
214 /// object suitable for processing and insertion into Moodle.
215
216 echo "<p>This flash question format has not yet been completed!</p>";
217
218 return NULL;
219 }
220
90455bb3 221 function defaultquestion() {
222 // returns an "empty" question
223 // Somewhere to specify question parameters that are not handled
224 // by import but are required db fields.
225 // This should not be overridden.
508fe4d8 226 global $CFG;
227
90455bb3 228 $question = new stdClass();
508fe4d8 229 $question->shuffleanswers = $CFG->quiz_shuffleanswers;
230 $question->defaultgrade = 1;
231 $question->image = "";
232 $question->usecase = 0;
233 $question->multiplier = array();
234 $question->generalfeedback = '';
235 $question->correctfeedback = '';
236 $question->partiallycorrectfeedback = '';
237 $question->incorrectfeedback = '';
238 $question->answernumbering = 'abc';
239 $question->penalty = 0.1;
240 $question->length = 1;
90455bb3 241 $question->qoption = 0;
242 $question->layout = 1;
243
244 return $question;
245 }
394c97c8 246
247 function importpostprocess() {
248 /// Does any post-processing that may be desired
249 /// Argument is a simple array of question ids that
250 /// have just been added.
251
252 return true;
253 }
254
255}
256
257?>