3 * format.php - Default format class for file imports/exports. Doesn't do
4 * everything on it's own -- it needs to be extended.
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 // Included by import.php
13 class qformat_default
{
15 var $displayerrors = true;
17 var $questionids = array();
18 var $qtypeconvert = array(NUMERICAL
=> LESSON_NUMERICAL
,
19 MULTICHOICE
=> LESSON_MULTICHOICE
,
20 TRUEFALSE
=> LESSON_TRUEFALSE
,
21 SHORTANSWER
=> LESSON_SHORTANSWER
,
22 MATCH
=> LESSON_MATCHING
25 /// Importing functions
27 function importpreprocess() {
28 /// Does any pre-processing that may be desired
33 function importprocess($filename, $lesson, $pageid) {
34 /// Processes a given file. There's probably little need to change this
37 if (! $lines = $this->readdata($filename)) {
38 notify("File could not be read, or was empty");
42 if (! $questions = $this->readquestions($lines)) { // Extract all the questions
43 notify("There are no questions in this file!");
47 notify(get_string('importcount', 'lesson', sizeof($questions)));
51 foreach ($questions as $question) { // Process and store each question
52 switch ($question->qtype
) {
61 echo "<hr><p><b>$count</b>. ".stripslashes($question->questiontext
)."</p>";
62 $newpage = new stdClass
;
63 $newpage->lessonid
= $lesson->id
;
64 $newpage->qtype
= $this->qtypeconvert
[$question->qtype
];
65 switch ($question->qtype
) {
67 if (isset($question->usecase
)) {
68 $newpage->qoption
= $question->usecase
;
72 if (isset($question->single
)) {
73 $newpage->qoption
= !$question->single
;
77 $newpage->timecreated
= $timenow;
78 if ($question->name
!= $question->questiontext
) {
79 $newpage->title
= $question->name
;
81 $newpage->title
= "Page $count";
83 $newpage->contents
= $question->questiontext
;
87 // the new page follows on from this page
88 if (!$page = get_record("lesson_pages", "id", $pageid)) {
89 error ("Format: Page $pageid not found");
91 $newpage->prevpageid
= $pageid;
92 $newpage->nextpageid
= $page->nextpageid
;
93 // insert the page and reset $pageid
94 if (!$newpageid = insert_record("lesson_pages", $newpage)) {
95 error("Format: Could not insert new page!");
97 // update the linked list
98 if (!set_field("lesson_pages", "nextpageid", $newpageid, "id", $pageid)) {
99 error("Format: unable to update link");
103 // new page is the first page
104 // get the existing (first) page (if any)
105 if (!$page = get_record_select("lesson_pages", "lessonid = $lesson->id AND prevpageid = 0")) {
106 // there are no existing pages
107 $newpage->prevpageid
= 0; // this is a first page
108 $newpage->nextpageid
= 0; // this is the only page
109 $newpageid = insert_record("lesson_pages", $newpage);
111 error("Insert page: new first page not inserted");
114 // there are existing pages put this at the start
115 $newpage->prevpageid
= 0; // this is a first page
116 $newpage->nextpageid
= $page->id
;
117 $newpageid = insert_record("lesson_pages", $newpage);
119 error("Insert page: first page not inserted");
121 // update the linked list
122 if (!set_field("lesson_pages", "prevpageid", $newpageid, "id", $page->id
)) {
123 error("Insert page: unable to update link");
127 // reset $pageid and put the page ID in $question, used in save_question_option()
128 $pageid = $newpageid;
129 $question->id
= $newpageid;
131 $this->questionids
[] = $question->id
;
133 // Now to save all the answers and type-specific options
135 $question->lessonid
= $lesson->id
; // needed for foreign key
136 $question->qtype
= $this->qtypeconvert
[$question->qtype
];
137 $result = lesson_save_question_options($question);
139 if (!empty($result->error
)) {
140 notify($result->error
);
144 if (!empty($result->notice
)) {
145 notify($result->notice
);
151 notify(get_string('unsupportedqtype', 'lesson', $question->qtype
));
159 function readdata($filename) {
160 /// Returns complete file with an array, one item per line
162 if (is_readable($filename)) {
163 $filearray = file($filename);
165 /// Check for Macintosh OS line returns (ie file on one line), and fix
166 if (ereg("\r", $filearray[0]) AND !ereg("\n", $filearray[0])) {
167 return explode("\r", $filearray[0]);
175 function readquestions($lines) {
176 /// Parses an array of lines into an array of questions,
177 /// where each item is a question object as defined by
178 /// readquestion(). Questions are defined as anything
179 /// between blank lines.
181 $questions = array();
182 $currentquestion = array();
184 foreach ($lines as $line) {
187 if (!empty($currentquestion)) {
188 if ($question = $this->readquestion($currentquestion)) {
189 $questions[] = $question;
191 $currentquestion = array();
194 $currentquestion[] = $line;
198 if (!empty($currentquestion)) { // There may be a final question
199 if ($question = $this->readquestion($currentquestion)) {
200 $questions[] = $question;
208 function readquestion($lines) {
209 /// Given an array of lines known to define a question in
210 /// this format, this function converts it into a question
211 /// object suitable for processing and insertion into Moodle.
213 echo "<p>This flash question format has not yet been completed!</p>";
218 function defaultquestion() {
219 // returns an "empty" question
220 // Somewhere to specify question parameters that are not handled
221 // by import but are required db fields.
222 // This should not be overridden.
223 $question = new stdClass();
224 $question->qoption
= 0;
225 $question->layout
= 1;
230 function importpostprocess() {
231 /// Does any post-processing that may be desired
232 /// Argument is a simple array of question ids that
233 /// have just been added.