2 //This php script contains all the stuff to backup/restore
5 //This is the "graphical" structure of the survey mod:
6 // --------------------
7 // survey | survey_questions |
8 // (CL,pk->id) |(CL,pk->id,?????) |
9 // | --------------------
11 // -----------------------------------
13 // survey_analysis survey_answers
14 // (UL,pk->id, fk->survey) (UL,pk->id, fk->survey)
16 // Meaning: pk->primary key field of the table
17 // fk->foreign key to link with parent
18 // nt->nested field (recursive data)
19 // CL->course level info
20 // UL->user level info
21 // files->table may have files)
23 //-----------------------------------------------------------
26 function survey_restore_mods($mod,$restore) {
32 //Get record from backup_ids
33 $data = backup_getid($restore->backup_unique_code
,$mod->modtype
,$mod->id
);
36 //Now get completed xmlized object
38 //traverse_xmlize($info); //Debug
39 //print_object ($GLOBALS['traverse_array']); //Debug
40 //$GLOBALS['traverse_array']=""; //Debug
42 //Now, build the SURVEY record structure
43 $survey->course
= $restore->course_id
;
44 $survey->template
= backup_todb($info['MOD']['#']['TEMPLATE']['0']['#']);
45 $survey->days
= backup_todb($info['MOD']['#']['DAYS']['0']['#']);
46 $survey->timecreated
= backup_todb($info['MOD']['#']['TIMECREATED']['0']['#']);
47 $survey->timemodified
= backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
48 $survey->name
= backup_todb($info['MOD']['#']['NAME']['0']['#']);
49 $survey->intro
= backup_todb($info['MOD']['#']['INTRO']['0']['#']);
50 $survey->questions
= backup_todb($info['MOD']['#']['QUESTIONS']['0']['#']);
52 //The structure is equal to the db, so insert the survey
53 $newid = insert_record ("survey",$survey);
56 if (!defined('RESTORE_SILENTLY')) {
57 echo "<li>".get_string("modulename","survey")." \"".format_string(stripslashes($survey->name
),true)."\"</li>";
62 //We have the newid, update backup_ids
63 backup_putid($restore->backup_unique_code
,$mod->modtype
,
65 //Now check if want to restore user data and do it.
66 if (restore_userdata_selected($restore,'survey',$mod->id
)) {
67 //Restore survey_answers
68 $status = survey_answers_restore_mods ($newid,$info,$restore);
69 //Restore survey_analysis
71 $status = survey_analysis_restore_mods ($newid,$info,$restore);
84 //This function restores the survey_answers
85 function survey_answers_restore_mods($survey_id,$info,$restore) {
91 //Get the answers array
92 $answers = $info['MOD']['#']['ANSWERS']['0']['#']['ANSWER'];
94 //Iterate over answers
95 for($i = 0; $i < sizeof($answers); $i++
) {
96 $sub_info = $answers[$i];
97 //traverse_xmlize($sub_info); //Debug
98 //print_object ($GLOBALS['traverse_array']); //Debug
99 //$GLOBALS['traverse_array']=""; //Debug
101 //We'll need this later!!
102 $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
103 $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
105 //Now, build the SURVEY_ANSWERS record structure
106 $answer->survey
= $survey_id;
107 $answer->userid
= backup_todb($sub_info['#']['USERID']['0']['#']);
108 $answer->question
= backup_todb($sub_info['#']['QUESTION']['0']['#']);
109 $answer->time
= backup_todb($sub_info['#']['TIME']['0']['#']);
110 $answer->answer1
= backup_todb($sub_info['#']['ANSWER1']['0']['#']);
111 $answer->answer2
= backup_todb($sub_info['#']['ANSWER2']['0']['#']);
113 //We have to recode the userid field
114 $user = backup_getid($restore->backup_unique_code
,"user",$answer->userid
);
116 $answer->userid
= $user->new_id
;
119 //The structure is equal to the db, so insert the survey_answers
120 $newid = insert_record ("survey_answers",$answer);
123 if (($i+
1) %
50 == 0) {
124 if (!defined('RESTORE_SILENTLY')) {
126 if (($i+
1) %
1000 == 0) {
134 //We have the newid, update backup_ids
135 backup_putid($restore->backup_unique_code
,"survey_answers",$oldid,
145 //This function restores the survey_analysis
146 function survey_analysis_restore_mods($survey_id,$info,$restore) {
152 //Get the analysis array
153 $analysis = $info['MOD']['#']['ANALYSIS']['0']['#']['ANALYS'];
155 //Iterate over analysis
156 for($i = 0; $i < sizeof($analysis); $i++
) {
157 $sub_info = $analysis[$i];
158 //traverse_xmlize($sub_info); //Debug
159 //print_object ($GLOBALS['traverse_array']); //Debug
160 //$GLOBALS['traverse_array']=""; //Debug
162 //We'll need this later!!
163 $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
164 $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
166 //Now, build the SURVEY_ANALYSIS record structure
167 $analys->survey
= $survey_id;
168 $analys->userid
= backup_todb($sub_info['#']['USERID']['0']['#']);
169 $analys->notes
= backup_todb($sub_info['#']['NOTES']['0']['#']);
171 //We have to recode the userid field
172 $user = backup_getid($restore->backup_unique_code
,"user",$analys->userid
);
174 $analys->userid
= $user->new_id
;
177 //The structure is equal to the db, so insert the survey_analysis
178 $newid = insert_record ("survey_analysis",$analys);
181 if (($i+
1) %
50 == 0) {
182 if (!defined('RESTORE_SILENTLY')) {
184 if (($i+
1) %
1000 == 0) {
192 //We have the newid, update backup_ids
193 backup_putid($restore->backup_unique_code
,"survey_analysis",$oldid,
203 //Return a content decoded to support interactivities linking. Every module
204 //should have its own. They are called automatically from
205 //servey_decode_content_links_caller() function in each module
206 //in the restore process
207 function servey_decode_content_links ($content,$restore) {
213 //Link to the list of serveys
215 $searchstring='/\$@(SURVEYINDEX)\*([0-9]+)@\$/';
217 preg_match_all($searchstring,$content,$foundset);
218 //If found, then we are going to look for its new id (in backup tables)
220 //print_object($foundset); //Debug
221 //Iterate over foundset[2]. They are the old_ids
222 foreach($foundset[2] as $old_id) {
223 //We get the needed variables here (course id)
224 $rec = backup_getid($restore->backup_unique_code
,"course",$old_id);
225 //Personalize the searchstring
226 $searchstring='/\$@(SURVEYINDEX)\*('.$old_id.')@\$/';
227 //If it is a link to this course, update the link to its new location
230 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/servey/index.php?id='.$rec->new_id
,$result);
232 //It's a foreign link so leave it as original
233 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/servey/index.php?id='.$old_id,$result);
238 //Link to servey view by moduleid
240 $searchstring='/\$@(SURVEYVIEWBYID)\*([0-9]+)@\$/';
242 preg_match_all($searchstring,$result,$foundset);
243 //If found, then we are going to look for its new id (in backup tables)
245 //print_object($foundset); //Debug
246 //Iterate over foundset[2]. They are the old_ids
247 foreach($foundset[2] as $old_id) {
248 //We get the needed variables here (course_modules id)
249 $rec = backup_getid($restore->backup_unique_code
,"course_modules",$old_id);
250 //Personalize the searchstring
251 $searchstring='/\$@(SURVEYVIEWBYID)\*('.$old_id.')@\$/';
252 //If it is a link to this course, update the link to its new location
255 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/servey/view.php?id='.$rec->new_id
,$result);
257 //It's a foreign link so leave it as original
258 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/servey/view.php?id='.$old_id,$result);
266 //This function makes all the necessary calls to xxxx_decode_content_links()
267 //function in each module, passing them the desired contents to be decoded
268 //from backup format to destination site/course in order to mantain inter-activities
269 //working in the backup/restore process. It's called from restore_decode_content_links()
270 //function in restore process
271 function survey_decode_content_links_caller($restore) {
275 if ($surveys = get_records_sql ("SELECT s.id, s.intro
276 FROM {$CFG->prefix}survey s
277 WHERE s.course = $restore->course_id")) {
278 //Iterate over each survey->intro
279 $i = 0; //Counter to send some output to the browser to avoid timeouts
280 foreach ($surveys as $survey) {
283 $content = $survey->intro
;
284 $result = restore_decode_content_links_worker($content,$restore);
285 if ($result != $content) {
287 $survey->intro
= addslashes($result);
288 $status = update_record("survey",$survey);
290 if (!defined('RESTORE_SILENTLY')) {
291 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
296 if (($i+
1) %
5 == 0) {
297 if (!defined('RESTORE_SILENTLY')) {
299 if (($i+
1) %
100 == 0) {
311 //This function returns a log record with all the necessay transformations
312 //done. It's used by restore_log_module() to restore modules log.
313 function survey_restore_logs($restore,$log) {
317 //Depending of the action, we recode different things
318 switch ($log->action
) {
321 //Get the new_id of the module (to recode the info field)
322 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
324 $log->url
= "view.php?id=".$log->cmid
;
325 $log->info
= $mod->new_id
;
332 //Get the new_id of the module (to recode the info field)
333 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
335 $log->url
= "view.php?id=".$log->cmid
;
336 $log->info
= $mod->new_id
;
343 //Get the new_id of the module (to recode the info field)
344 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
346 $log->url
= "view.php?id=".$log->cmid
;
347 $log->info
= $mod->new_id
;
354 //Get the new_id of the module (to recode the info field)
355 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
357 $log->url
= "view.php?id=".$log->cmid
;
358 $log->info
= $mod->new_id
;
365 //Get the new_id of the module (to recode the info field)
366 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
368 $log->url
= "view.php?id=".$log->cmid
;
369 $log->info
= $mod->new_id
;
376 //Get the new_id of the module (to recode the info field)
377 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
379 $log->url
= "report.php?id=".$log->cmid
;
380 $log->info
= $mod->new_id
;
386 $log->url
= "index.php?id=".$log->course
;
391 //Get the new_id of the module (to recode the info field)
392 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
394 //Rebuild the url, extracting the type (txt, xls)
395 $filetype = substr($log->url
,-3);
396 $log->url
= "download.php?id=".$log->cmid
."&type=".$filetype;
397 $log->info
= $mod->new_id
;
403 if (!defined('RESTORE_SILENTLY')) {
404 echo "action (".$log->module
."-".$log->action
.") unknown. Not restored<br />"; //Debug