Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / question / backuplib.php
blob3637f944f27dadc6723b0410a3cd6aeaaff6369b
1 <?php //$Id$
2 /**
3 * Question bank backup code.
5 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
6 * @package questionbank
7 *//** */
9 //This is the "graphical" structure of the question database:
10 //To see, put your terminal to 160cc
12 // The following holds student-independent information about the questions
14 // question_categories
15 // (CL,pk->id)
16 // |
17 // |
18 // |.......................................
19 // | .
20 // | .
21 // | -------question_datasets------ .
22 // | | (CL,pk->id,fk->question, | .
23 // | | fk->dataset_definition) | .
24 // | | | .
25 // | | | .
26 // | | | .
27 // | | question_dataset_definitions
28 // | | (CL,pk->id,fk->category)
29 // question |
30 // (CL,pk->id,fk->category,files) |
31 // | question_dataset_items
32 // | (CL,pk->id,fk->definition)
33 // |
34 // |
35 // |
36 // --------------------------------------------------------------------------------------------------------------
37 // | | | | | | |
38 // | | | | | | |
39 // | | | | question_calculated | |
40 // question_truefalse | question_multichoice | (CL,pl->id,fk->question) | |
41 // (CL,pk->id,fk->question) | (CL,pk->id,fk->question) | . | | question_randomsamatch
42 // . | . | . | |--(CL,pk->id,fk->question)
43 // . question_shortanswer . question_numerical . question_multianswer. |
44 // . (CL,pk->id,fk->question) . (CL,pk->id,fk->question) . (CL,pk->id,fk->question) |
45 // . . . . . . | question_match
46 // . . . . . . |--(CL,pk->id,fk->question)
47 // . . . . . . | .
48 // . . . . . . | .
49 // . . . . . . | .
50 // . . . . . . | question_match_sub
51 // ........................................................................................ |--(CL,pk->id,fk->question)
52 // . |
53 // . |
54 // . | question_numerical_units
55 // question_answers |--(CL,pk->id,fk->question)
56 // (CL,pk->id,fk->question)----------------------------------------------------------
59 // The following holds the information about student interaction with the questions
61 // question_sessions
62 // (UL,pk->id,fk->attempt,question)
63 // .
64 // .
65 // question_states
66 // (UL,pk->id,fk->attempt,question)
68 // Meaning: pk->primary key field of the table
69 // fk->foreign key to link with parent
70 // nt->nested field (recursive data)
71 // SL->site level info
72 // CL->course level info
73 // UL->user level info
74 // files->table may have files
76 //-----------------------------------------------------------
78 require_once("$CFG->libdir/questionlib.php");
80 function backup_question_categories($bf,$preferences) {
82 global $CFG;
84 $status = true;
86 //First, we get the used categories from backup_ids
87 $categories = question_category_ids_by_backup ($preferences->backup_unique_code);
89 //If we've categories
90 if ($categories) {
91 //Write start tag
92 $status = fwrite($bf,start_tag("QUESTION_CATEGORIES",2,true));
93 //Iterate over each category
94 foreach ($categories as $cat) {
95 //Start category
96 $status = fwrite ($bf,start_tag("QUESTION_CATEGORY",3,true));
97 //Get category data from question_categories
98 $category = get_record ("question_categories","id",$cat->old_id);
99 //Print category contents
100 fwrite($bf,full_tag("ID",4,false,$category->id));
101 fwrite($bf,full_tag("NAME",4,false,$category->name));
102 fwrite($bf,full_tag("INFO",4,false,$category->info));
103 fwrite($bf,full_tag("PUBLISH",4,false,$category->publish));
104 fwrite($bf,full_tag("STAMP",4,false,$category->stamp));
105 fwrite($bf,full_tag("PARENT",4,false,$category->parent));
106 fwrite($bf,full_tag("SORTORDER",4,false,$category->sortorder));
107 //Now, backup their questions
108 $status = backup_question($bf,$preferences,$category->id);
109 //End category
110 $status = fwrite ($bf,end_tag("QUESTION_CATEGORY",3,true));
112 //Write end tag
113 $status = fwrite ($bf,end_tag("QUESTION_CATEGORIES",2,true));
116 return $status;
119 //This function backups all the questions in selected category and their
120 //asociated data
121 function backup_question($bf,$preferences,$category, $level = 4) {
123 global $CFG, $QTYPES;
125 $status = true;
127 // We'll fetch the questions sorted by parent so that questions with no parents
128 // (these are the ones which could be parents themselves) are backed up first. This
129 // is important for the recoding of the parent field during the restore process
130 $questions = get_records("question","category",$category,"parent ASC, id");
131 //If there are questions
132 if ($questions) {
133 //Write start tag
134 $status = fwrite ($bf,start_tag("QUESTIONS",$level,true));
135 $counter = 0;
136 //Iterate over each question
137 foreach ($questions as $question) {
138 //Start question
139 $status = fwrite ($bf,start_tag("QUESTION",$level + 1,true));
140 //Print question contents
141 fwrite ($bf,full_tag("ID",$level + 2,false,$question->id));
142 fwrite ($bf,full_tag("PARENT",$level + 2,false,$question->parent));
143 fwrite ($bf,full_tag("NAME",$level + 2,false,$question->name));
144 fwrite ($bf,full_tag("QUESTIONTEXT",$level + 2,false,$question->questiontext));
145 fwrite ($bf,full_tag("QUESTIONTEXTFORMAT",$level + 2,false,$question->questiontextformat));
146 fwrite ($bf,full_tag("IMAGE",$level + 2,false,$question->image));
147 fwrite ($bf,full_tag("GENERALFEEDBACK",$level + 2,false,$question->generalfeedback));
148 fwrite ($bf,full_tag("DEFAULTGRADE",$level + 2,false,$question->defaultgrade));
149 fwrite ($bf,full_tag("PENALTY",$level + 2,false,$question->penalty));
150 fwrite ($bf,full_tag("QTYPE",$level + 2,false,$question->qtype));
151 fwrite ($bf,full_tag("LENGTH",$level + 2,false,$question->length));
152 fwrite ($bf,full_tag("STAMP",$level + 2,false,$question->stamp));
153 fwrite ($bf,full_tag("VERSION",$level + 2,false,$question->version));
154 fwrite ($bf,full_tag("HIDDEN",$level + 2,false,$question->hidden));
155 // Backup question type specific data
156 $status = $QTYPES[$question->qtype]->backup($bf,$preferences,$question->id, $level + 2);
157 //End question
158 $status = fwrite ($bf,end_tag("QUESTION",$level + 1,true));
159 //Do some output
160 $counter++;
161 if ($counter % 10 == 0) {
162 echo ".";
163 if ($counter % 200 == 0) {
164 echo "<br />";
166 backup_flush(300);
169 //Write end tag
170 $status = fwrite ($bf,end_tag("QUESTIONS",$level,true));
172 return $status;
175 //This function backups the answers data in some question types
176 //(truefalse, shortanswer,multichoice,numerical,calculated)
177 function question_backup_answers($bf,$preferences,$question, $level = 6) {
179 global $CFG;
181 $status = true;
183 $answers = get_records("question_answers","question",$question,"id");
184 //If there are answers
185 if ($answers) {
186 $status = fwrite ($bf,start_tag("ANSWERS",$level,true));
187 //Iterate over each answer
188 foreach ($answers as $answer) {
189 $status = fwrite ($bf,start_tag("ANSWER",$level + 1,true));
190 //Print answer contents
191 fwrite ($bf,full_tag("ID",$level + 2,false,$answer->id));
192 fwrite ($bf,full_tag("ANSWER_TEXT",$level + 2,false,$answer->answer));
193 fwrite ($bf,full_tag("FRACTION",$level + 2,false,$answer->fraction));
194 fwrite ($bf,full_tag("FEEDBACK",$level + 2,false,$answer->feedback));
195 $status = fwrite ($bf,end_tag("ANSWER",$level + 1,true));
197 $status = fwrite ($bf,end_tag("ANSWERS",$level,true));
199 return $status;
202 //This function backups question_numerical_units from different question types
203 function question_backup_numerical_units($bf,$preferences,$question,$level=7) {
205 global $CFG;
207 $status = true;
209 $numerical_units = get_records("question_numerical_units","question",$question,"id");
210 //If there are numericals_units
211 if ($numerical_units) {
212 $status = fwrite ($bf,start_tag("NUMERICAL_UNITS",$level,true));
213 //Iterate over each numerical_unit
214 foreach ($numerical_units as $numerical_unit) {
215 $status = fwrite ($bf,start_tag("NUMERICAL_UNIT",$level+1,true));
216 //Print numerical_unit contents
217 fwrite ($bf,full_tag("MULTIPLIER",$level+2,false,$numerical_unit->multiplier));
218 fwrite ($bf,full_tag("UNIT",$level+2,false,$numerical_unit->unit));
219 //Now backup numerical_units
220 $status = fwrite ($bf,end_tag("NUMERICAL_UNIT",$level+1,true));
222 $status = fwrite ($bf,end_tag("NUMERICAL_UNITS",$level,true));
225 return $status;
229 //This function backups dataset_definitions (via question_datasets) from different question types
230 function question_backup_datasets($bf,$preferences,$question,$level=7) {
232 global $CFG;
234 $status = true;
236 //First, we get the used datasets for this question
237 $question_datasets = get_records("question_datasets","question",$question,"id");
238 //If there are question_datasets
239 if ($question_datasets) {
240 $status = $status &&fwrite ($bf,start_tag("DATASET_DEFINITIONS",$level,true));
241 //Iterate over each question_dataset
242 foreach ($question_datasets as $question_dataset) {
243 $def = NULL;
244 //Get dataset_definition
245 if ($def = get_record("question_dataset_definitions","id",$question_dataset->datasetdefinition)) {;
246 $status = $status &&fwrite ($bf,start_tag("DATASET_DEFINITION",$level+1,true));
247 //Print question_dataset contents
248 fwrite ($bf,full_tag("CATEGORY",$level+2,false,$def->category));
249 fwrite ($bf,full_tag("NAME",$level+2,false,$def->name));
250 fwrite ($bf,full_tag("TYPE",$level+2,false,$def->type));
251 fwrite ($bf,full_tag("OPTIONS",$level+2,false,$def->options));
252 fwrite ($bf,full_tag("ITEMCOUNT",$level+2,false,$def->itemcount));
253 //Now backup dataset_entries
254 $status = question_backup_dataset_items($bf,$preferences,$def->id,$level+2);
255 //End dataset definition
256 $status = $status &&fwrite ($bf,end_tag("DATASET_DEFINITION",$level+1,true));
259 $status = $status &&fwrite ($bf,end_tag("DATASET_DEFINITIONS",$level,true));
262 return $status;
266 //This function backups datases_items from dataset_definitions
267 function question_backup_dataset_items($bf,$preferences,$datasetdefinition,$level=9) {
269 global $CFG;
271 $status = true;
273 //First, we get the datasets_items for this dataset_definition
274 $dataset_items = get_records("question_dataset_items","definition",$datasetdefinition,"id");
275 //If there are dataset_items
276 if ($dataset_items) {
277 $status = $status &&fwrite ($bf,start_tag("DATASET_ITEMS",$level,true));
278 //Iterate over each dataset_item
279 foreach ($dataset_items as $dataset_item) {
280 $status = $status &&fwrite ($bf,start_tag("DATASET_ITEM",$level+1,true));
281 //Print question_dataset contents
282 fwrite ($bf,full_tag("NUMBER",$level+2,false,$dataset_item->itemnumber));
283 fwrite ($bf,full_tag("VALUE",$level+2,false,$dataset_item->value));
284 //End dataset definition
285 $status = $status &&fwrite ($bf,end_tag("DATASET_ITEM",$level+1,true));
287 $status = $status &&fwrite ($bf,end_tag("DATASET_ITEMS",$level,true));
290 return $status;
295 //Backup question_states contents (executed from backup_quiz_attempts)
296 function backup_question_states ($bf,$preferences,$attempt, $level = 6) {
298 global $CFG;
300 $status = true;
302 $question_states = get_records("question_states","attempt",$attempt,"id");
303 //If there are states
304 if ($question_states) {
305 //Write start tag
306 $status = fwrite ($bf,start_tag("STATES",$level,true));
307 //Iterate over each state
308 foreach ($question_states as $state) {
309 //Start state
310 $status = fwrite ($bf,start_tag("STATE",$level + 1,true));
311 //Print state contents
312 fwrite ($bf,full_tag("ID",$level + 2,false,$state->id));
313 fwrite ($bf,full_tag("QUESTION",$level + 2,false,$state->question));
314 fwrite ($bf,full_tag("ORIGINALQUESTION",$level + 2,false,$state->originalquestion));
315 fwrite ($bf,full_tag("SEQ_NUMBER",$level + 2,false,$state->seq_number));
316 fwrite ($bf,full_tag("ANSWER",$level + 2,false,$state->answer));
317 fwrite ($bf,full_tag("TIMESTAMP",$level + 2,false,$state->timestamp));
318 fwrite ($bf,full_tag("EVENT",$level + 2,false,$state->event));
319 fwrite ($bf,full_tag("GRADE",$level + 2,false,$state->grade));
320 fwrite ($bf,full_tag("RAW_GRADE",$level + 2,false,$state->raw_grade));
321 fwrite ($bf,full_tag("PENALTY",$level + 2,false,$state->penalty));
322 //End state
323 $status = fwrite ($bf,end_tag("STATE",$level + 1,true));
325 //Write end tag
326 $status = fwrite ($bf,end_tag("STATES",$level,true));
330 //Backup question_sessions contents (executed from backup_quiz_attempts)
331 function backup_question_sessions ($bf,$preferences,$attempt, $level = 6) {
332 global $CFG;
334 $status = true;
336 $question_sessions = get_records("question_sessions","attemptid",$attempt,"id");
337 //If there are sessions
338 if ($question_sessions) {
339 //Write start tag (the funny name 'newest states' has historical reasons)
340 $status = fwrite ($bf,start_tag("NEWEST_STATES",$level,true));
341 //Iterate over each newest_state
342 foreach ($question_sessions as $newest_state) {
343 //Start newest_state
344 $status = fwrite ($bf,start_tag("NEWEST_STATE",$level + 1,true));
345 //Print newest_state contents
346 fwrite ($bf,full_tag("ID",$level + 2,false,$newest_state->id));
347 fwrite ($bf,full_tag("QUESTIONID",$level + 2,false,$newest_state->questionid));
348 fwrite ($bf,full_tag("NEWEST",$level + 2,false,$newest_state->newest));
349 fwrite ($bf,full_tag("NEWGRADED",$level + 2,false,$newest_state->newgraded));
350 fwrite ($bf,full_tag("SUMPENALTY",$level + 2,false,$newest_state->sumpenalty));
351 fwrite ($bf,full_tag("MANUALCOMMENT",$level + 2,false,$newest_state->manualcomment));
352 //End newest_state
353 $status = fwrite ($bf,end_tag("NEWEST_STATE",$level + 1,true));
355 //Write end tag
356 $status = fwrite ($bf,end_tag("NEWEST_STATES",$level,true));
358 return $status;
361 //Returns an array of categories id
362 function question_category_ids_by_backup ($backup_unique_code) {
364 global $CFG;
366 return get_records_sql ("SELECT a.old_id, a.backup_code
367 FROM {$CFG->prefix}backup_ids a
368 WHERE a.backup_code = '$backup_unique_code' AND
369 a.table_name = 'question_categories'");
372 function question_ids_by_backup ($backup_unique_code) {
374 global $CFG;
376 return get_records_sql ("SELECT q.id, q.category
377 FROM {$CFG->prefix}backup_ids a,
378 {$CFG->prefix}question q
379 WHERE a.backup_code = '$backup_unique_code' AND
380 q.category = a.old_id AND
381 a.table_name = 'question_categories'");
384 //Delete category ids from backup_ids table
385 function delete_category_ids ($backup_unique_code) {
386 global $CFG;
387 $status = true;
388 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
389 WHERE backup_code = '$backup_unique_code'",false);
390 return $status;