2 //This php script contains all the stuff to backup questions
4 //This is the "graphical" structure of the question database:
5 //To see, put your terminal to 160cc
7 // The following holds student-independent information about the questions
13 // |.......................................
16 // | -------question_datasets------ .
17 // | | (CL,pk->id,fk->question, | .
18 // | | fk->dataset_definition) | .
22 // | | question_dataset_definitions
23 // | | (CL,pk->id,fk->category)
25 // (CL,pk->id,fk->category,files) |
26 // | question_dataset_items
27 // | (CL,pk->id,fk->definition)
28 // | question_rqp_type
31 // -------------------------------------------------------------------------------------------------------------- |
32 // | | | | | | | question_rqp
33 // | | | | | | |--(CL,pk->id,fk->question)
34 // | | | | question_calculated | |
35 // question_truefalse | question_multichoice | (CL,pl->id,fk->question) | |
36 // (CL,pk->id,fk->question) | (CL,pk->id,fk->question) | . | | question_randomsamatch
37 // . | . | . | |--(CL,pk->id,fk->question)
38 // . question_shortanswer . question_numerical . question_multianswer. |
39 // . (CL,pk->id,fk->question) . (CL,pk->id,fk->question) . (CL,pk->id,fk->question) |
40 // . . . . . . | question_match
41 // . . . . . . |--(CL,pk->id,fk->question)
45 // . . . . . . | question_match_sub
46 // ........................................................................................ |--(CL,pk->id,fk->question)
49 // . | question_numerical_units
50 // question_answers |--(CL,pk->id,fk->question)
51 // (CL,pk->id,fk->question)----------------------------------------------------------
54 // The following holds the information about student interaction with the questions
57 // (UL,pk->id,fk->attempt,question)
61 // (UL,pk->id,fk->attempt,question)
63 // question_rqp_states
64 // (UL,pk->id,fk->stateid)
66 // Meaning: pk->primary key field of the table
67 // fk->foreign key to link with parent
68 // nt->nested field (recursive data)
69 // SL->site level info
70 // CL->course level info
71 // UL->user level info
72 // files->table may have files
74 //-----------------------------------------------------------
76 require_once("$CFG->libdir/questionlib.php");
78 function backup_question_categories($bf,$preferences) {
84 //First, we get the used categories from backup_ids
85 $categories = question_category_ids_by_backup ($preferences->backup_unique_code
);
90 $status = fwrite($bf,start_tag("QUESTION_CATEGORIES",2,true));
91 //Iterate over each category
92 foreach ($categories as $cat) {
94 $status = fwrite ($bf,start_tag("QUESTION_CATEGORY",3,true));
95 //Get category data from question_categories
96 $category = get_record ("question_categories","id",$cat->old_id
);
97 //Print category contents
98 fwrite($bf,full_tag("ID",4,false,$category->id
));
99 fwrite($bf,full_tag("NAME",4,false,$category->name
));
100 fwrite($bf,full_tag("INFO",4,false,$category->info
));
101 fwrite($bf,full_tag("PUBLISH",4,false,$category->publish
));
102 fwrite($bf,full_tag("STAMP",4,false,$category->stamp
));
103 fwrite($bf,full_tag("PARENT",4,false,$category->parent
));
104 fwrite($bf,full_tag("SORTORDER",4,false,$category->sortorder
));
105 //Now, backup their questions
106 $status = backup_question($bf,$preferences,$category->id
);
108 $status = fwrite ($bf,end_tag("QUESTION_CATEGORY",3,true));
111 $status = fwrite ($bf,end_tag("QUESTION_CATEGORIES",2,true));
117 //This function backups all the questions in selected category and their
119 function backup_question($bf,$preferences,$category, $level = 4) {
121 global $CFG, $QTYPES;
125 // We'll fetch the questions sorted by parent so that questions with no parents
126 // (these are the ones which could be parents themselves) are backed up first. This
127 // is important for the recoding of the parent field during the restore process
128 $questions = get_records("question","category",$category,"parent ASC, id");
129 //If there are questions
132 $status = fwrite ($bf,start_tag("QUESTIONS",$level,true));
134 //Iterate over each question
135 foreach ($questions as $question) {
137 $status = fwrite ($bf,start_tag("QUESTION",$level +
1,true));
138 //Print question contents
139 fwrite ($bf,full_tag("ID",$level +
2,false,$question->id
));
140 fwrite ($bf,full_tag("PARENT",$level +
2,false,$question->parent
));
141 fwrite ($bf,full_tag("NAME",$level +
2,false,$question->name
));
142 fwrite ($bf,full_tag("QUESTIONTEXT",$level +
2,false,$question->questiontext
));
143 fwrite ($bf,full_tag("QUESTIONTEXTFORMAT",$level +
2,false,$question->questiontextformat
));
144 fwrite ($bf,full_tag("IMAGE",$level +
2,false,$question->image
));
145 fwrite ($bf,full_tag("DEFAULTGRADE",$level +
2,false,$question->defaultgrade
));
146 fwrite ($bf,full_tag("PENALTY",$level +
2,false,$question->penalty
));
147 fwrite ($bf,full_tag("QTYPE",$level +
2,false,$question->qtype
));
148 fwrite ($bf,full_tag("LENGTH",$level +
2,false,$question->length
));
149 fwrite ($bf,full_tag("STAMP",$level +
2,false,$question->stamp
));
150 fwrite ($bf,full_tag("VERSION",$level +
2,false,$question->version
));
151 fwrite ($bf,full_tag("HIDDEN",$level +
2,false,$question->hidden
));
152 // Backup question type specific data
153 $status = $QTYPES[$question->qtype
]->backup($bf,$preferences,$question->id
, $level +
2);
155 $status = fwrite ($bf,end_tag("QUESTION",$level +
1,true));
158 if ($counter %
10 == 0) {
160 if ($counter %
200 == 0) {
167 $status = fwrite ($bf,end_tag("QUESTIONS",$level,true));
172 //This function backups the answers data in some question types
173 //(truefalse, shortanswer,multichoice,numerical,calculated)
174 function question_backup_answers($bf,$preferences,$question, $level = 6) {
180 $answers = get_records("question_answers","question",$question,"id");
181 //If there are answers
183 $status = fwrite ($bf,start_tag("ANSWERS",$level,true));
184 //Iterate over each answer
185 foreach ($answers as $answer) {
186 $status = fwrite ($bf,start_tag("ANSWER",$level +
1,true));
187 //Print answer contents
188 fwrite ($bf,full_tag("ID",$level +
2,false,$answer->id
));
189 fwrite ($bf,full_tag("ANSWER_TEXT",$level +
2,false,$answer->answer
));
190 fwrite ($bf,full_tag("FRACTION",$level +
2,false,$answer->fraction
));
191 fwrite ($bf,full_tag("FEEDBACK",$level +
2,false,$answer->feedback
));
192 $status = fwrite ($bf,end_tag("ANSWER",$level +
1,true));
194 $status = fwrite ($bf,end_tag("ANSWERS",$level,true));
199 //This function backups question_numerical_units from different question types
200 function question_backup_numerical_units($bf,$preferences,$question,$level=7) {
206 $numerical_units = get_records("question_numerical_units","question",$question,"id");
207 //If there are numericals_units
208 if ($numerical_units) {
209 $status = fwrite ($bf,start_tag("NUMERICAL_UNITS",$level,true));
210 //Iterate over each numerical_unit
211 foreach ($numerical_units as $numerical_unit) {
212 $status = fwrite ($bf,start_tag("NUMERICAL_UNIT",$level+
1,true));
213 //Print numerical_unit contents
214 fwrite ($bf,full_tag("MULTIPLIER",$level+
2,false,$numerical_unit->multiplier
));
215 fwrite ($bf,full_tag("UNIT",$level+
2,false,$numerical_unit->unit
));
216 //Now backup numerical_units
217 $status = fwrite ($bf,end_tag("NUMERICAL_UNIT",$level+
1,true));
219 $status = fwrite ($bf,end_tag("NUMERICAL_UNITS",$level,true));
226 //This function backups dataset_definitions (via question_datasets) from different question types
227 function question_backup_datasets($bf,$preferences,$question,$level=7) {
233 //First, we get the used datasets for this question
234 $question_datasets = get_records("question_datasets","question",$question,"id");
235 //If there are question_datasets
236 if ($question_datasets) {
237 $status = $status &&fwrite ($bf,start_tag("DATASET_DEFINITIONS",$level,true));
238 //Iterate over each question_dataset
239 foreach ($question_datasets as $question_dataset) {
241 //Get dataset_definition
242 if ($def = get_record("question_dataset_definitions","id",$question_dataset->datasetdefinition
)) {;
243 $status = $status &&fwrite ($bf,start_tag("DATASET_DEFINITION",$level+
1,true));
244 //Print question_dataset contents
245 fwrite ($bf,full_tag("CATEGORY",$level+
2,false,$def->category
));
246 fwrite ($bf,full_tag("NAME",$level+
2,false,$def->name
));
247 fwrite ($bf,full_tag("TYPE",$level+
2,false,$def->type
));
248 fwrite ($bf,full_tag("OPTIONS",$level+
2,false,$def->options
));
249 fwrite ($bf,full_tag("ITEMCOUNT",$level+
2,false,$def->itemcount
));
250 //Now backup dataset_entries
251 $status = question_backup_dataset_items($bf,$preferences,$def->id
,$level+
2);
252 //End dataset definition
253 $status = $status &&fwrite ($bf,end_tag("DATASET_DEFINITION",$level+
1,true));
256 $status = $status &&fwrite ($bf,end_tag("DATASET_DEFINITIONS",$level,true));
263 //This function backups datases_items from dataset_definitions
264 function question_backup_dataset_items($bf,$preferences,$datasetdefinition,$level=9) {
270 //First, we get the datasets_items for this dataset_definition
271 $dataset_items = get_records("question_dataset_items","definition",$datasetdefinition,"id");
272 //If there are dataset_items
273 if ($dataset_items) {
274 $status = $status &&fwrite ($bf,start_tag("DATASET_ITEMS",$level,true));
275 //Iterate over each dataset_item
276 foreach ($dataset_items as $dataset_item) {
277 $status = $status &&fwrite ($bf,start_tag("DATASET_ITEM",$level+
1,true));
278 //Print question_dataset contents
279 fwrite ($bf,full_tag("NUMBER",$level+
2,false,$dataset_item->number
));
280 fwrite ($bf,full_tag("VALUE",$level+
2,false,$dataset_item->value
));
281 //End dataset definition
282 $status = $status &&fwrite ($bf,end_tag("DATASET_ITEM",$level+
1,true));
284 $status = $status &&fwrite ($bf,end_tag("DATASET_ITEMS",$level,true));
292 //Backup question_states contents (executed from backup_quiz_attempts)
293 function backup_question_states ($bf,$preferences,$attempt, $level = 6) {
299 $question_states = get_records("question_states","attempt",$attempt,"id");
300 //If there are states
301 if ($question_states) {
303 $status = fwrite ($bf,start_tag("STATES",$level,true));
304 //Iterate over each state
305 foreach ($question_states as $state) {
307 $status = fwrite ($bf,start_tag("STATE",$level +
1,true));
308 //Print state contents
309 fwrite ($bf,full_tag("ID",$level +
2,false,$state->id
));
310 fwrite ($bf,full_tag("QUESTION",$level +
2,false,$state->question
));
311 fwrite ($bf,full_tag("ORIGINALQUESTION",$level +
2,false,$state->originalquestion
));
312 fwrite ($bf,full_tag("SEQ_NUMBER",$level +
2,false,$state->seq_number
));
313 fwrite ($bf,full_tag("ANSWER",$level +
2,false,$state->answer
));
314 fwrite ($bf,full_tag("TIMESTAMP",$level +
2,false,$state->timestamp
));
315 fwrite ($bf,full_tag("EVENT",$level +
2,false,$state->event
));
316 fwrite ($bf,full_tag("GRADE",$level +
2,false,$state->grade
));
317 fwrite ($bf,full_tag("RAW_GRADE",$level +
2,false,$state->raw_grade
));
318 fwrite ($bf,full_tag("PENALTY",$level +
2,false,$state->penalty
));
319 // now back up question type specific state information
320 $status = backup_question_rqp_state ($bf,$preferences,$state->id
, $level +
2);
322 $status = fwrite ($bf,end_tag("STATE",$level +
1,true));
325 $status = fwrite ($bf,end_tag("STATES",$level,true));
329 //Backup question_sessions contents (executed from backup_quiz_attempts)
330 function backup_question_sessions ($bf,$preferences,$attempt, $level = 6) {
335 $question_sessions = get_records("question_sessions","attemptid",$attempt,"id");
336 //If there are sessions
337 if ($question_sessions) {
338 //Write start tag (the funny name 'newest states' has historical reasons)
339 $status = fwrite ($bf,start_tag("NEWEST_STATES",$level,true));
340 //Iterate over each newest_state
341 foreach ($question_sessions as $newest_state) {
343 $status = fwrite ($bf,start_tag("NEWEST_STATE",$level +
1,true));
344 //Print newest_state contents
345 fwrite ($bf,full_tag("ID",$level +
2,false,$newest_state->id
));
346 fwrite ($bf,full_tag("QUESTIONID",$level +
2,false,$newest_state->questionid
));
347 fwrite ($bf,full_tag("NEWEST",$level +
2,false,$newest_state->newest
));
348 fwrite ($bf,full_tag("NEWGRADED",$level +
2,false,$newest_state->newgraded
));
349 fwrite ($bf,full_tag("SUMPENALTY",$level +
2,false,$newest_state->sumpenalty
));
350 fwrite ($bf,full_tag("COMMENT",$level +
2,false,$newest_state->comment
));
352 $status = fwrite ($bf,end_tag("NEWEST_STATE",$level +
1,true));
355 $status = fwrite ($bf,end_tag("NEWEST_STATES",$level,true));
360 //Backup question_rqp_state contents (executed from backup_question_states)
361 function backup_question_rqp_state ($bf,$preferences,$state, $level = 8) {
367 $rqp_state = get_record("question_rqp_states","stateid",$state);
368 //If there is a state
371 $status = fwrite ($bf,start_tag("RQP_STATE",$level,true));
372 //Print state contents
373 fwrite ($bf,full_tag("RESPONSES",$level +
1,false,$rqp_state->responses
));
374 fwrite ($bf,full_tag("PERSISTENT_DATA",$level +
1,false,$rqp_state->persistent_data
));
375 fwrite ($bf,full_tag("TEMPLATE_VARS",$level +
1,false,$rqp_state->template_vars
));
377 $status = fwrite ($bf,end_tag("RQP_STATE",$level,true));
382 //Returns an array of categories id
383 function question_category_ids_by_backup ($backup_unique_code) {
387 return get_records_sql ("SELECT a.old_id, a.backup_code
388 FROM {$CFG->prefix}backup_ids a
389 WHERE a.backup_code = '$backup_unique_code' AND
390 a.table_name = 'question_categories'");
393 function question_ids_by_backup ($backup_unique_code) {
397 return get_records_sql ("SELECT q.id, q.category
398 FROM {$CFG->prefix}backup_ids a,
399 {$CFG->prefix}question q
400 WHERE a.backup_code = '$backup_unique_code' AND
401 q.category = a.old_id AND
402 a.table_name = 'question_categories'");
405 //Delete category ids from backup_ids table
406 function delete_category_ids ($backup_unique_code) {
409 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
410 WHERE backup_code = '$backup_unique_code'",false);