Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / survey / restorelib.php
blob90525d53d7a79794eba5e595b8c93a23f840694f
1 <?php //$Id$
2 //This php script contains all the stuff to backup/restore
3 //survey mods
5 //This is the "graphical" structure of the survey mod:
6 // --------------------
7 // survey | survey_questions |
8 // (CL,pk->id) |(CL,pk->id,?????) |
9 // | --------------------
10 // |
11 // -----------------------------------
12 // | |
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) {
28 global $CFG,$db;
30 $status = true;
32 //Get record from backup_ids
33 $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
35 if ($data) {
36 //Now get completed xmlized object
37 $info = $data->info;
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);
55 //Do some output
56 if (!defined('RESTORE_SILENTLY')) {
57 echo "<li>".get_string("modulename","survey")." \"".format_string(stripslashes($survey->name),true)."\"</li>";
59 backup_flush(300);
61 if ($newid) {
62 //We have the newid, update backup_ids
63 backup_putid($restore->backup_unique_code,$mod->modtype,
64 $mod->id, $newid);
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
70 if ($status) {
71 $status = survey_analysis_restore_mods ($newid,$info,$restore);
74 } else {
75 $status = false;
77 } else {
78 $status = false;
81 return $status;
84 //This function restores the survey_answers
85 function survey_answers_restore_mods($survey_id,$info,$restore) {
87 global $CFG;
89 $status = true;
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);
115 if ($user) {
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);
122 //Do some output
123 if (($i+1) % 50 == 0) {
124 if (!defined('RESTORE_SILENTLY')) {
125 echo ".";
126 if (($i+1) % 1000 == 0) {
127 echo "<br />";
130 backup_flush(300);
133 if ($newid) {
134 //We have the newid, update backup_ids
135 backup_putid($restore->backup_unique_code,"survey_answers",$oldid,
136 $newid);
137 } else {
138 $status = false;
142 return $status;
145 //This function restores the survey_analysis
146 function survey_analysis_restore_mods($survey_id,$info,$restore) {
148 global $CFG;
150 $status = true;
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);
173 if ($user) {
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);
180 //Do some output
181 if (($i+1) % 50 == 0) {
182 if (!defined('RESTORE_SILENTLY')) {
183 echo ".";
184 if (($i+1) % 1000 == 0) {
185 echo "<br />";
188 backup_flush(300);
191 if ($newid) {
192 //We have the newid, update backup_ids
193 backup_putid($restore->backup_unique_code,"survey_analysis",$oldid,
194 $newid);
195 } else {
196 $status = false;
200 return $status;
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) {
209 global $CFG;
211 $result = $content;
213 //Link to the list of serveys
215 $searchstring='/\$@(SURVEYINDEX)\*([0-9]+)@\$/';
216 //We look for it
217 preg_match_all($searchstring,$content,$foundset);
218 //If found, then we are going to look for its new id (in backup tables)
219 if ($foundset[0]) {
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
228 if($rec->new_id) {
229 //Now replace it
230 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/servey/index.php?id='.$rec->new_id,$result);
231 } else {
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]+)@\$/';
241 //We look for it
242 preg_match_all($searchstring,$result,$foundset);
243 //If found, then we are going to look for its new id (in backup tables)
244 if ($foundset[0]) {
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
253 if($rec->new_id) {
254 //Now replace it
255 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/servey/view.php?id='.$rec->new_id,$result);
256 } else {
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);
263 return $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) {
272 global $CFG;
273 $status = true;
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) {
281 //Increment counter
282 $i++;
283 $content = $survey->intro;
284 $result = restore_decode_content_links_worker($content,$restore);
285 if ($result != $content) {
286 //Update record
287 $survey->intro = addslashes($result);
288 $status = update_record("survey",$survey);
289 if (debugging()) {
290 if (!defined('RESTORE_SILENTLY')) {
291 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
295 //Do some output
296 if (($i+1) % 5 == 0) {
297 if (!defined('RESTORE_SILENTLY')) {
298 echo ".";
299 if (($i+1) % 100 == 0) {
300 echo "<br />";
303 backup_flush(300);
308 return $status;
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) {
315 $status = false;
317 //Depending of the action, we recode different things
318 switch ($log->action) {
319 case "add":
320 if ($log->cmid) {
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);
323 if ($mod) {
324 $log->url = "view.php?id=".$log->cmid;
325 $log->info = $mod->new_id;
326 $status = true;
329 break;
330 case "submit":
331 if ($log->cmid) {
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);
334 if ($mod) {
335 $log->url = "view.php?id=".$log->cmid;
336 $log->info = $mod->new_id;
337 $status = true;
340 break;
341 case "update":
342 if ($log->cmid) {
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);
345 if ($mod) {
346 $log->url = "view.php?id=".$log->cmid;
347 $log->info = $mod->new_id;
348 $status = true;
351 break;
352 case "view form":
353 if ($log->cmid) {
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);
356 if ($mod) {
357 $log->url = "view.php?id=".$log->cmid;
358 $log->info = $mod->new_id;
359 $status = true;
362 break;
363 case "view graph":
364 if ($log->cmid) {
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);
367 if ($mod) {
368 $log->url = "view.php?id=".$log->cmid;
369 $log->info = $mod->new_id;
370 $status = true;
373 break;
374 case "view report":
375 if ($log->cmid) {
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);
378 if ($mod) {
379 $log->url = "report.php?id=".$log->cmid;
380 $log->info = $mod->new_id;
381 $status = true;
384 break;
385 case "view all":
386 $log->url = "index.php?id=".$log->course;
387 $status = true;
388 break;
389 case "download":
390 if ($log->cmid) {
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);
393 if ($mod) {
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;
398 $status = true;
401 break;
402 default:
403 if (!defined('RESTORE_SILENTLY')) {
404 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
406 break;
409 if ($status) {
410 $status = $log;
412 return $status;