2 //This php script contains all the stuff to backup/restore
5 //This is the "graphical" structure of the choice mod:
8 // (CL,pk->id)----------|
13 // (UL,pk->id, fk->choiceid) |
18 // (UL,pk->id, fk->choiceid, fk->optionid)
20 // Meaning: pk->primary key field of the table
21 // fk->foreign key to link with parent
22 // nt->nested field (recursive data)
23 // CL->course level info
24 // UL->user level info
25 // files->table may have files)
27 //-----------------------------------------------------------
29 //This function executes all the restore procedure about this mod
30 function choice_restore_mods($mod,$restore) {
36 //Get record from backup_ids
37 $data = backup_getid($restore->backup_unique_code
,$mod->modtype
,$mod->id
);
40 //Now get completed xmlized object
42 // if necessary, write to restorelog and adjust date/time fields
43 if ($restore->course_startdateoffset
) {
44 restore_log_date_changes('Choice', $restore, $info['MOD']['#'], array('TIMEOPEN', 'TIMECLOSE'));
46 //traverse_xmlize($info); //Debug
47 //print_object ($GLOBALS['traverse_array']); //Debug
48 //$GLOBALS['traverse_array']=""; //Debug
50 //Now, build the CHOICE record structure
51 $choice->course
= $restore->course_id
;
52 $choice->name
= backup_todb($info['MOD']['#']['NAME']['0']['#']);
53 $choice->text
= backup_todb($info['MOD']['#']['TEXT']['0']['#']);
54 $choice->format
= backup_todb($info['MOD']['#']['FORMAT']['0']['#']);
55 $choice->publish
= backup_todb($info['MOD']['#']['PUBLISH']['0']['#']);
56 $choice->showresults
= isset($info['MOD']['#']['SHOWRESULTS']['0']['#'])?
backup_todb($info['MOD']['#']['SHOWRESULTS']['0']['#']):'';
57 $choice->display
= backup_todb($info['MOD']['#']['DISPLAY']['0']['#']);
58 $choice->allowupdate
= backup_todb($info['MOD']['#']['ALLOWUPDATE']['0']['#']);
59 $choice->showunanswered
= backup_todb($info['MOD']['#']['SHOWUNANSWERED']['0']['#']);
60 $choice->limitanswers
= backup_todb($info['MOD']['#']['LIMITANSWERS']['0']['#']);
61 $choice->timeopen
= backup_todb($info['MOD']['#']['TIMEOPEN']['0']['#']);
62 $choice->timeclose
= backup_todb($info['MOD']['#']['TIMECLOSE']['0']['#']);
63 $choice->timemodified
= backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
65 //To mantain compatibilty, in 1.4 the publish setting meaning has changed. We
66 //have to modify some things it if the release field isn't present in the backup file.
67 if (! isset($info['MOD']['#']['SHOWRESULTS']['0']['#'])) { //check for previous versions
68 if (! isset($info['MOD']['#']['RELEASE']['0']['#'])) { //It's a pre-14 backup filea
69 //Set the allowupdate field
70 if ($choice->publish
== 0) {
71 $choice->allowupdate
= 1;
73 //Set the showresults field as defined by the old publish field
74 if ($choice->publish
> 0) {
75 $choice->showresults
= 1;
77 //Recode the publish field to its 1.4 meaning
78 if ($choice->publish
> 0) {
81 } else { //it's post 1.4 pre 1.6
82 //convert old release values into new showanswer column.
83 $choice->showresults
= backup_todb($info['MOD']['#']['RELEASE']['0']['#']);
86 //The structure is equal to the db, so insert the choice
87 $newid = insert_record ("choice",$choice);
90 //We have the newid, update backup_ids
91 backup_putid($restore->backup_unique_code
,$mod->modtype
,
94 //Check to see how answers (curently choice_options) are stored in the table
95 //If answer1 - answer6 exist, this is a pre 1.5 version of choice
96 if (isset($info['MOD']['#']['ANSWER1']['0']['#']) ||
97 isset($info['MOD']['#']['ANSWER2']['0']['#']) ||
98 isset($info['MOD']['#']['ANSWER3']['0']['#']) ||
99 isset($info['MOD']['#']['ANSWER4']['0']['#']) ||
100 isset($info['MOD']['#']['ANSWER5']['0']['#']) ||
101 isset($info['MOD']['#']['ANSWER6']['0']['#']) ) {
103 //This is a pre 1.5 choice backup, special work begins
105 $options[1] = backup_todb($info['MOD']['#']['ANSWER1']['0']['#']);
106 $options[2] = backup_todb($info['MOD']['#']['ANSWER2']['0']['#']);
107 $options[3] = backup_todb($info['MOD']['#']['ANSWER3']['0']['#']);
108 $options[4] = backup_todb($info['MOD']['#']['ANSWER4']['0']['#']);
109 $options[5] = backup_todb($info['MOD']['#']['ANSWER5']['0']['#']);
110 $options[6] = backup_todb($info['MOD']['#']['ANSWER6']['0']['#']);
112 for($i = 1; $i < 7; $i++
) { //insert old answers (in 1.4) as choice_options (1.5) to db.
113 if (!empty($options[$i])) { //make sure this option has something in it!
114 $option->choiceid
= $newid;
115 $option->text
= $options[$i];
116 $option->timemodified
= $choice->timemodified
;
117 $newoptionid = insert_record ("choice_options",$option);
118 //Save this choice_option to backup_ids
119 backup_putid($restore->backup_unique_code
,"choice_options",$i,$newoptionid);
122 } else { //Now we are in a "standard" 1.5 choice, so restore choice_options normally
123 $status = choice_options_restore_mods($newid,$info,$restore);
126 //now restore the answers for this choice.
127 if (restore_userdata_selected($restore,'choice',$mod->id
)) {
128 //Restore choice_answers
129 $status = choice_answers_restore_mods($newid,$info,$restore);
136 if (!defined('RESTORE_SILENTLY')) {
137 echo "<li>".get_string("modulename","choice")." \"".format_string(stripslashes($choice->name
),true)."\"</li>";
147 function choice_options_restore_mods($choiceid,$info,$restore) {
153 $options = $info['MOD']['#']['OPTIONS']['0']['#']['OPTION'];
155 //Iterate over options
156 for($i = 0; $i < sizeof($options); $i++
) {
157 $opt_info = $options[$i];
158 //traverse_xmlize($opt_info); //Debug
159 //print_object ($GLOBALS['traverse_array']); //Debug
160 //$GLOBALS['traverse_array']=""; //Debug
162 //We'll need this later!!
163 $oldid = backup_todb($opt_info['#']['ID']['0']['#']);
164 $olduserid = isset($opt_info['#']['USERID']['0']['#'])?
backup_todb($opt_info['#']['USERID']['0']['#']):'';
166 //Now, build the CHOICE_OPTIONS record structure
167 $option->choiceid
= $choiceid;
168 $option->text
= backup_todb($opt_info['#']['TEXT']['0']['#']);
169 $option->maxanswers
= backup_todb($opt_info['#']['MAXANSWERS']['0']['#']);
170 $option->timemodified
= backup_todb($opt_info['#']['TIMEMODIFIED']['0']['#']);
172 //The structure is equal to the db, so insert the choice_options
173 $newid = insert_record ("choice_options",$option);
176 if (($i+
1) %
50 == 0) {
177 if (!defined('RESTORE_SILENTLY')) {
179 if (($i+
1) %
1000 == 0) {
187 //We have the newid, update backup_ids
188 backup_putid($restore->backup_unique_code
,"choice_options",$oldid,
198 //This function restores the choice_answers
199 function choice_answers_restore_mods($choiceid,$info,$restore) {
205 $answers = $info['MOD']['#']['ANSWERS']['0']['#']['ANSWER'];
207 //Iterate over answers
208 for($i = 0; $i < sizeof($answers); $i++
) {
209 $ans_info = $answers[$i];
210 //traverse_xmlize($sub_info); //Debug
211 //print_object ($GLOBALS['traverse_array']); //Debug
212 //$GLOBALS['traverse_array']=""; //Debug
214 //We'll need this later!!
215 $oldid = backup_todb($ans_info['#']['ID']['0']['#']);
216 $olduserid = backup_todb($ans_info['#']['USERID']['0']['#']);
218 //Now, build the CHOICE_ANSWERS record structure
219 $answer->choiceid
= $choiceid;
220 $answer->userid
= backup_todb($ans_info['#']['USERID']['0']['#']);
221 $answer->optionid
= backup_todb($ans_info['#']['OPTIONID']['0']['#']);
222 $answer->timemodified
= backup_todb($ans_info['#']['TIMEMODIFIED']['0']['#']);
224 //If the answer contains CHOICE_ANSWER, it's a pre 1.5 backup
225 if (!empty($ans_info['#']['CHOICE_ANSWER']['0']['#'])) {
226 //optionid was, in pre 1.5 backups, choice_answer
227 $answer->optionid
= backup_todb($ans_info['#']['CHOICE_ANSWER']['0']['#']);
230 //We have to recode the optionid field
231 $option = backup_getid($restore->backup_unique_code
,"choice_options",$answer->optionid
);
233 $answer->optionid
= $option->new_id
;
236 //We have to recode the userid field
237 $user = backup_getid($restore->backup_unique_code
,"user",$answer->userid
);
239 $answer->userid
= $user->new_id
;
242 //The structure is equal to the db, so insert the choice_answers
243 $newid = insert_record ("choice_answers",$answer);
246 if (($i+
1) %
50 == 0) {
247 if (!defined('RESTORE_SILENTLY')) {
249 if (($i+
1) %
1000 == 0) {
257 //We have the newid, update backup_ids
258 backup_putid($restore->backup_unique_code
,"choice_answers",$oldid,
268 //Return a content decoded to support interactivities linking. Every module
269 //should have its own. They are called automatically from
270 //choice_decode_content_links_caller() function in each module
271 //in the restore process
272 function choice_decode_content_links ($content,$restore) {
278 //Link to the list of choices
280 $searchstring='/\$@(CHOICEINDEX)\*([0-9]+)@\$/';
282 preg_match_all($searchstring,$content,$foundset);
283 //If found, then we are going to look for its new id (in backup tables)
285 //print_object($foundset); //Debug
286 //Iterate over foundset[2]. They are the old_ids
287 foreach($foundset[2] as $old_id) {
288 //We get the needed variables here (course id)
289 $rec = backup_getid($restore->backup_unique_code
,"course",$old_id);
290 //Personalize the searchstring
291 $searchstring='/\$@(CHOICEINDEX)\*('.$old_id.')@\$/';
292 //If it is a link to this course, update the link to its new location
295 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/choice/index.php?id='.$rec->new_id
,$result);
297 //It's a foreign link so leave it as original
298 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/choice/index.php?id='.$old_id,$result);
303 //Link to choice view by moduleid
305 $searchstring='/\$@(CHOICEVIEWBYID)\*([0-9]+)@\$/';
307 preg_match_all($searchstring,$result,$foundset);
308 //If found, then we are going to look for its new id (in backup tables)
310 //print_object($foundset); //Debug
311 //Iterate over foundset[2]. They are the old_ids
312 foreach($foundset[2] as $old_id) {
313 //We get the needed variables here (course_modules id)
314 $rec = backup_getid($restore->backup_unique_code
,"course_modules",$old_id);
315 //Personalize the searchstring
316 $searchstring='/\$@(CHOICEVIEWBYID)\*('.$old_id.')@\$/';
317 //If it is a link to this course, update the link to its new location
320 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/choice/view.php?id='.$rec->new_id
,$result);
322 //It's a foreign link so leave it as original
323 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/choice/view.php?id='.$old_id,$result);
331 //This function makes all the necessary calls to xxxx_decode_content_links()
332 //function in each module, passing them the desired contents to be decoded
333 //from backup format to destination site/course in order to mantain inter-activities
334 //working in the backup/restore process. It's called from restore_decode_content_links()
335 //function in restore process
336 function choice_decode_content_links_caller($restore) {
340 if ($choices = get_records_sql ("SELECT c.id, c.text
341 FROM {$CFG->prefix}choice c
342 WHERE c.course = $restore->course_id")) {
343 //Iterate over each choice->text
344 $i = 0; //Counter to send some output to the browser to avoid timeouts
345 foreach ($choices as $choice) {
348 $content = $choice->text
;
349 $result = restore_decode_content_links_worker($content,$restore);
350 if ($result != $content) {
352 $choice->text
= addslashes($result);
353 $status = update_record("choice",$choice);
355 if (!defined('RESTORE_SILENTLY')) {
356 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
361 if (($i+
1) %
5 == 0) {
362 if (!defined('RESTORE_SILENTLY')) {
364 if (($i+
1) %
100 == 0) {
376 //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
377 //some texts in the module
378 function choice_restore_wiki2markdown ($restore) {
384 //Convert choice->text
385 if ($records = get_records_sql ("SELECT c.id, c.text, c.format
386 FROM {$CFG->prefix}choice c,
387 {$CFG->prefix}backup_ids b
388 WHERE c.course = $restore->course_id AND
389 c.format = ".FORMAT_WIKI
. " AND
390 b.backup_code = $restore->backup_unique_code AND
391 b.table_name = 'choice' AND
393 foreach ($records as $record) {
395 $record->text
= restore_decode_wiki_content($record->text
, $restore);
396 //Convert to Markdown
397 $wtm = new WikiToMarkdown();
398 $record->text
= $wtm->convert($record->text
, $restore->course_id
);
399 $record->format
= FORMAT_MARKDOWN
;
400 $status = update_record('choice', addslashes_object($record));
403 if (($i+
1) %
1 == 0) {
404 if (!defined('RESTORE_SILENTLY')) {
406 if (($i+
1) %
20 == 0) {
418 //This function returns a log record with all the necessay transformations
419 //done. It's used by restore_log_module() to restore modules log.
420 function choice_restore_logs($restore,$log) {
424 //Depending of the action, we recode different things
425 switch ($log->action
) {
428 //Get the new_id of the module (to recode the info field)
429 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
431 $log->url
= "view.php?id=".$log->cmid
;
432 $log->info
= $mod->new_id
;
439 //Get the new_id of the module (to recode the info field)
440 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
442 $log->url
= "view.php?id=".$log->cmid
;
443 $log->info
= $mod->new_id
;
450 //Get the new_id of the module (to recode the info field)
451 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
453 $log->url
= "view.php?id=".$log->cmid
;
454 $log->info
= $mod->new_id
;
461 //Get the new_id of the module (to recode the info field)
462 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
464 $log->url
= "view.php?id=".$log->cmid
;
465 $log->info
= $mod->new_id
;
472 //Get the new_id of the module (to recode the info field)
473 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
475 $log->url
= "view.php?id=".$log->cmid
;
476 $log->info
= $mod->new_id
;
482 $log->url
= "index.php?id=".$log->course
;
487 //Get the new_id of the module (to recode the info field)
488 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
490 $log->url
= "report.php?id=".$log->cmid
;
491 $log->info
= $mod->new_id
;
497 if (!defined('RESTORE_SILENTLY')) {
498 echo "action (".$log->module
."-".$log->action
.") unknown. Not restored<br />"; //Debug