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