MDL-20700 coding style cleanup - cvs keywords removed, closign php tag removed, trail...
[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) {
d68ccdba 34 global $DB, $OUTPUT;
646fc290 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)) {
d68ccdba 40 echo $OUTPUT->notification("File could not be read, or was empty");
394c97c8 41 return false;
42 }
43
44 if (! $questions = $this->readquestions($lines)) { // Extract all the questions
d68ccdba 45 echo $OUTPUT->notification("There are no questions in this file!");
394c97c8 46 return false;
47 }
90455bb3 48
d68ccdba 49 echo $OUTPUT->notification(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
6d40f12e 96 $newpageid = $DB->insert_record("lesson_pages", $newpage);
394c97c8 97 // update the linked list
6d40f12e 98 $DB->set_field("lesson_pages", "nextpageid", $newpageid, array("id" => $pageid));
394c97c8 99
100 } else {
101 // new page is the first page
102 // get the existing (first) page (if any)
646fc290 103 $params = array ("lessonid" => $lesson->id, "prevpageid" => 0);
104 if (!$page = $DB->get_record_select("lesson_pages", "lessonid = :lessonid AND prevpageid = :prevpageid", $params)) {
394c97c8 105 // there are no existing pages
106 $newpage->prevpageid = 0; // this is a first page
107 $newpage->nextpageid = 0; // this is the only page
646fc290 108 $newpageid = $DB->insert_record("lesson_pages", $newpage);
394c97c8 109 } else {
110 // there are existing pages put this at the start
111 $newpage->prevpageid = 0; // this is a first page
112 $newpage->nextpageid = $page->id;
646fc290 113 $newpageid = $DB->insert_record("lesson_pages", $newpage);
394c97c8 114 // update the linked list
6d40f12e 115 $DB->set_field("lesson_pages", "prevpageid", $newpageid, array("id" => $page->id));
394c97c8 116 }
117 }
118 // reset $pageid and put the page ID in $question, used in save_question_option()
119 $pageid = $newpageid;
120 $question->id = $newpageid;
121
122 $this->questionids[] = $question->id;
123
124 // Now to save all the answers and type-specific options
125
126 $question->lessonid = $lesson->id; // needed for foreign key
90455bb3 127 $question->qtype = $this->qtypeconvert[$question->qtype];
394c97c8 128 $result = lesson_save_question_options($question);
129
130 if (!empty($result->error)) {
d68ccdba 131 echo $OUTPUT->notification($result->error);
394c97c8 132 return false;
133 }
134
135 if (!empty($result->notice)) {
d68ccdba 136 echo $OUTPUT->notification($result->notice);
394c97c8 137 return true;
138 }
139 break;
140 // the Bad ones
141 default :
d68ccdba 142 echo $OUTPUT->notification(get_string('unsupportedqtype', 'lesson', $question->qtype));
394c97c8 143 }
144
145 }
146 return true;
147 }
148
149
150 function readdata($filename) {
151 /// Returns complete file with an array, one item per line
152
153 if (is_readable($filename)) {
154 $filearray = file($filename);
155
156 /// Check for Macintosh OS line returns (ie file on one line), and fix
6dbcacee 157 if (preg_match("/\r/", $filearray[0]) AND !preg_match("/\n/", $filearray[0])) {
394c97c8 158 return explode("\r", $filearray[0]);
159 } else {
160 return $filearray;
161 }
162 }
163 return false;
164 }
165
166 function readquestions($lines) {
167 /// Parses an array of lines into an array of questions,
168 /// where each item is a question object as defined by
169 /// readquestion(). Questions are defined as anything
170 /// between blank lines.
171
172 $questions = array();
173 $currentquestion = array();
174
175 foreach ($lines as $line) {
176 $line = trim($line);
177 if (empty($line)) {
178 if (!empty($currentquestion)) {
179 if ($question = $this->readquestion($currentquestion)) {
180 $questions[] = $question;
181 }
182 $currentquestion = array();
183 }
184 } else {
185 $currentquestion[] = $line;
186 }
187 }
188
189 if (!empty($currentquestion)) { // There may be a final question
190 if ($question = $this->readquestion($currentquestion)) {
191 $questions[] = $question;
192 }
193 }
194
195 return $questions;
196 }
197
198
199 function readquestion($lines) {
200 /// Given an array of lines known to define a question in
201 /// this format, this function converts it into a question
202 /// object suitable for processing and insertion into Moodle.
203
204 echo "<p>This flash question format has not yet been completed!</p>";
205
206 return NULL;
207 }
208
90455bb3 209 function defaultquestion() {
210 // returns an "empty" question
211 // Somewhere to specify question parameters that are not handled
212 // by import but are required db fields.
213 // This should not be overridden.
508fe4d8 214 global $CFG;
215
90455bb3 216 $question = new stdClass();
508fe4d8 217 $question->shuffleanswers = $CFG->quiz_shuffleanswers;
218 $question->defaultgrade = 1;
219 $question->image = "";
220 $question->usecase = 0;
221 $question->multiplier = array();
222 $question->generalfeedback = '';
223 $question->correctfeedback = '';
224 $question->partiallycorrectfeedback = '';
225 $question->incorrectfeedback = '';
226 $question->answernumbering = 'abc';
227 $question->penalty = 0.1;
228 $question->length = 1;
90455bb3 229 $question->qoption = 0;
230 $question->layout = 1;
231
232 return $question;
233 }
394c97c8 234
235 function importpostprocess() {
236 /// Does any post-processing that may be desired
237 /// Argument is a simple array of question ids that
238 /// have just been added.
239
240 return true;
241 }
242
243}
244
245?>