Incorrect variable name used for parameter.
[moodle-linuxchix.git] / mod / lesson / format.php
blobe40dcebc233b54255cf3eb864b787a4f81933934
1 <?PHP // $Id$
3 ////////////////////////////////////////////////////////////////////
4 /// format.php - Default format class for file imports/exports. //
5 /// //
6 /// Doesn't do everything on it's own -- it needs to be extended. //
7 ////////////////////////////////////////////////////////////////////
9 // Included by import.php
11 class quiz_default_format {
13 var $displayerrors = true;
14 var $category = NULL;
15 var $questionids = array();
17 /// Importing functions
19 function importpreprocess() {
20 /// Does any pre-processing that may be desired
22 return true;
25 function importprocess($filename, $lesson, $pageid) {
26 /// Processes a given file. There's probably little need to change this
27 $timenow = time();
29 if (! $lines = $this->readdata($filename)) {
30 notify("File could not be read, or was empty");
31 return false;
34 if (! $questions = $this->readquestions($lines)) { // Extract all the questions
35 notify("There are no questions in this file!");
36 return false;
39 notify("Importing ".count($questions)." questions");
41 $count = 0;
43 foreach ($questions as $question) { // Process and store each question
44 switch ($question->qtype) {
45 // the good ones
46 case SHORTANSWER :
47 case NUMERICAL :
48 case TRUEFALSE :
49 case MULTICHOICE :
50 case MATCH :
51 $count++;
53 echo "<hr><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
54 $newpage = new stdClass;
55 $newpage->lessonid = $lesson->id;
56 $newpage->qtype = $question->qtype;
57 switch ($question->qtype) {
58 case SHORTANSWER :
59 $newpage->qoption = $question->usecase;
60 break;
61 case MULTICHOICE :
62 if (isset($question->single)) {
63 $newpage->qoption = !$question->single;
65 break;
67 $newpage->timecreated = $timenow;
68 if ($question->name != $question->questiontext) {
69 $newpage->title = $question->name;
70 } else {
71 $newpage->title = "Page $count";
73 $newpage->contents = $question->questiontext;
75 // set up page links
76 if ($pageid) {
77 // the new page follows on from this page
78 if (!$page = get_record("lesson_pages", "id", $pageid)) {
79 error ("Format: Page $pageid not found");
81 $newpage->prevpageid = $pageid;
82 $newpage->nextpageid = $page->nextpageid;
83 // insert the page and reset $pageid
84 if (!$newpageid = insert_record("lesson_pages", $newpage)) {
85 error("Format: Could not insert new page!");
87 // update the linked list
88 if (!set_field("lesson_pages", "nextpageid", $newpageid, "id", $pageid)) {
89 error("Format: unable to update link");
92 } else {
93 // new page is the first page
94 // get the existing (first) page (if any)
95 if (!$page = get_record_select("lesson_pages", "lessonid = $lesson->id AND prevpageid = 0")) {
96 // there are no existing pages
97 $newpage->prevpageid = 0; // this is a first page
98 $newpage->nextpageid = 0; // this is the only page
99 $newpageid = insert_record("lesson_pages", $newpage);
100 if (!$newpageid) {
101 error("Insert page: new first page not inserted");
103 } else {
104 // there are existing pages put this at the start
105 $newpage->prevpageid = 0; // this is a first page
106 $newpage->nextpageid = $page->id;
107 $newpageid = insert_record("lesson_pages", $newpage);
108 if (!$newpageid) {
109 error("Insert page: first page not inserted");
111 // update the linked list
112 if (!set_field("lesson_pages", "prevpageid", $newpageid, "id", $page->id)) {
113 error("Insert page: unable to update link");
117 // reset $pageid and put the page ID in $question, used in save_question_option()
118 $pageid = $newpageid;
119 $question->id = $newpageid;
121 $this->questionids[] = $question->id;
123 // Now to save all the answers and type-specific options
125 $question->lessonid = $lesson->id; // needed for foreign key
126 $result = lesson_save_question_options($question);
128 if (!empty($result->error)) {
129 notify($result->error);
130 return false;
133 if (!empty($result->notice)) {
134 notify($result->notice);
135 return true;
137 break;
138 // the Bad ones
139 default :
140 echo "<p>Unsupported question type ($question->qtype)!</p>";
144 return true;
148 function readdata($filename) {
149 /// Returns complete file with an array, one item per line
151 if (is_readable($filename)) {
152 $filearray = file($filename);
154 /// Check for Macintosh OS line returns (ie file on one line), and fix
155 if (ereg("\r", $filearray[0]) AND !ereg("\n", $filearray[0])) {
156 return explode("\r", $filearray[0]);
157 } else {
158 return $filearray;
161 return false;
164 function readquestions($lines) {
165 /// Parses an array of lines into an array of questions,
166 /// where each item is a question object as defined by
167 /// readquestion(). Questions are defined as anything
168 /// between blank lines.
170 $questions = array();
171 $currentquestion = array();
173 foreach ($lines as $line) {
174 $line = trim($line);
175 if (empty($line)) {
176 if (!empty($currentquestion)) {
177 if ($question = $this->readquestion($currentquestion)) {
178 $questions[] = $question;
180 $currentquestion = array();
182 } else {
183 $currentquestion[] = $line;
187 if (!empty($currentquestion)) { // There may be a final question
188 if ($question = $this->readquestion($currentquestion)) {
189 $questions[] = $question;
193 return $questions;
197 function readquestion($lines) {
198 /// Given an array of lines known to define a question in
199 /// this format, this function converts it into a question
200 /// object suitable for processing and insertion into Moodle.
202 echo "<p>This flash question format has not yet been completed!</p>";
204 return NULL;
208 function importpostprocess() {
209 /// Does any post-processing that may be desired
210 /// Argument is a simple array of question ids that
211 /// have just been added.
213 return true;