Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / lesson / format.php
blobc547c7b01f8503c8781aa63bb78b4d219ddb7ab2
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.
6 * @version $Id$
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package lesson
9 **/
11 // Included by import.php
13 class qformat_default {
15 var $displayerrors = true;
16 var $category = NULL;
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
30 return true;
33 function importprocess($filename, $lesson, $pageid) {
34 /// Processes a given file. There's probably little need to change this
35 $timenow = time();
37 if (! $lines = $this->readdata($filename)) {
38 notify("File could not be read, or was empty");
39 return false;
42 if (! $questions = $this->readquestions($lines)) { // Extract all the questions
43 notify("There are no questions in this file!");
44 return false;
47 notify(get_string('importcount', 'lesson', sizeof($questions)));
49 $count = 0;
51 foreach ($questions as $question) { // Process and store each question
52 switch ($question->qtype) {
53 // the good ones
54 case SHORTANSWER :
55 case NUMERICAL :
56 case TRUEFALSE :
57 case MULTICHOICE :
58 case MATCH :
59 $count++;
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) {
66 case SHORTANSWER :
67 if (isset($question->usecase)) {
68 $newpage->qoption = $question->usecase;
70 break;
71 case MULTICHOICE :
72 if (isset($question->single)) {
73 $newpage->qoption = !$question->single;
75 break;
77 $newpage->timecreated = $timenow;
78 if ($question->name != $question->questiontext) {
79 $newpage->title = $question->name;
80 } else {
81 $newpage->title = "Page $count";
83 $newpage->contents = $question->questiontext;
85 // set up page links
86 if ($pageid) {
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");
102 } else {
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);
110 if (!$newpageid) {
111 error("Insert page: new first page not inserted");
113 } else {
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);
118 if (!$newpageid) {
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);
141 return false;
144 if (!empty($result->notice)) {
145 notify($result->notice);
146 return true;
148 break;
149 // the Bad ones
150 default :
151 notify(get_string('unsupportedqtype', 'lesson', $question->qtype));
155 return true;
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]);
168 } else {
169 return $filearray;
172 return false;
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) {
185 $line = trim($line);
186 if (empty($line)) {
187 if (!empty($currentquestion)) {
188 if ($question = $this->readquestion($currentquestion)) {
189 $questions[] = $question;
191 $currentquestion = array();
193 } else {
194 $currentquestion[] = $line;
198 if (!empty($currentquestion)) { // There may be a final question
199 if ($question = $this->readquestion($currentquestion)) {
200 $questions[] = $question;
204 return $questions;
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>";
215 return NULL;
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;
227 return $question;
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.
235 return true;