MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / choice / restorelib.php
blob44a6d4a7d8c7c4cf6c3d3134ae914fe96e12e253
1 <?php //$Id$
2 //This php script contains all the stuff to backup/restore
3 //choice mods
5 //This is the "graphical" structure of the choice mod:
6 //
7 // choice
8 // (CL,pk->id)----------|
9 // | |
10 // | |
11 // | |
12 // choice_options |
13 // (UL,pk->id, fk->choiceid) |
14 // | |
15 // | |
16 // | |
17 // choice_answers |
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) {
32 global $CFG;
34 $status = true;
36 //Get record from backup_ids
37 $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
39 if ($data) {
40 //Now get completed xmlized object
41 $info = $data->info;
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) {
79 $choice->publish--;
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);
89 if ($newid) {
90 //We have the newid, update backup_ids
91 backup_putid($restore->backup_unique_code,$mod->modtype,
92 $mod->id, $newid);
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
104 $options = array();
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);
131 } else {
132 $status = false;
135 //Do some output
136 if (!defined('RESTORE_SILENTLY')) {
137 echo "<li>".get_string("modulename","choice")." \"".format_string(stripslashes($choice->name),true)."\"</li>";
139 backup_flush(300);
141 } else {
142 $status = false;
144 return $status;
147 function choice_options_restore_mods($choiceid,$info,$restore) {
149 global $CFG;
151 $status = true;
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);
175 //Do some output
176 if (($i+1) % 50 == 0) {
177 if (!defined('RESTORE_SILENTLY')) {
178 echo ".";
179 if (($i+1) % 1000 == 0) {
180 echo "<br />";
183 backup_flush(300);
186 if ($newid) {
187 //We have the newid, update backup_ids
188 backup_putid($restore->backup_unique_code,"choice_options",$oldid,
189 $newid);
190 } else {
191 $status = false;
195 return $status;
198 //This function restores the choice_answers
199 function choice_answers_restore_mods($choiceid,$info,$restore) {
201 global $CFG;
203 $status = true;
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);
232 if ($option) {
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);
238 if ($user) {
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);
245 //Do some output
246 if (($i+1) % 50 == 0) {
247 if (!defined('RESTORE_SILENTLY')) {
248 echo ".";
249 if (($i+1) % 1000 == 0) {
250 echo "<br />";
253 backup_flush(300);
256 if ($newid) {
257 //We have the newid, update backup_ids
258 backup_putid($restore->backup_unique_code,"choice_answers",$oldid,
259 $newid);
260 } else {
261 $status = false;
265 return $status;
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) {
274 global $CFG;
276 $result = $content;
278 //Link to the list of choices
280 $searchstring='/\$@(CHOICEINDEX)\*([0-9]+)@\$/';
281 //We look for it
282 preg_match_all($searchstring,$content,$foundset);
283 //If found, then we are going to look for its new id (in backup tables)
284 if ($foundset[0]) {
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
293 if($rec->new_id) {
294 //Now replace it
295 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/choice/index.php?id='.$rec->new_id,$result);
296 } else {
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]+)@\$/';
306 //We look for it
307 preg_match_all($searchstring,$result,$foundset);
308 //If found, then we are going to look for its new id (in backup tables)
309 if ($foundset[0]) {
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
318 if($rec->new_id) {
319 //Now replace it
320 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/choice/view.php?id='.$rec->new_id,$result);
321 } else {
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);
328 return $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) {
337 global $CFG;
338 $status = true;
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) {
346 //Increment counter
347 $i++;
348 $content = $choice->text;
349 $result = restore_decode_content_links_worker($content,$restore);
350 if ($result != $content) {
351 //Update record
352 $choice->text = addslashes($result);
353 $status = update_record("choice",$choice);
354 if (debugging()) {
355 if (!defined('RESTORE_SILENTLY')) {
356 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
360 //Do some output
361 if (($i+1) % 5 == 0) {
362 if (!defined('RESTORE_SILENTLY')) {
363 echo ".";
364 if (($i+1) % 100 == 0) {
365 echo "<br />";
368 backup_flush(300);
373 return $status;
376 //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
377 //some texts in the module
378 function choice_restore_wiki2markdown ($restore) {
380 global $CFG;
382 $status = true;
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
392 b.new_id = c.id")) {
393 foreach ($records as $record) {
394 //Rebuild wiki links
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));
401 //Do some output
402 $i++;
403 if (($i+1) % 1 == 0) {
404 if (!defined('RESTORE_SILENTLY')) {
405 echo ".";
406 if (($i+1) % 20 == 0) {
407 echo "<br />";
410 backup_flush(300);
415 return $status;
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) {
422 $status = false;
424 //Depending of the action, we recode different things
425 switch ($log->action) {
426 case "add":
427 if ($log->cmid) {
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);
430 if ($mod) {
431 $log->url = "view.php?id=".$log->cmid;
432 $log->info = $mod->new_id;
433 $status = true;
436 break;
437 case "update":
438 if ($log->cmid) {
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);
441 if ($mod) {
442 $log->url = "view.php?id=".$log->cmid;
443 $log->info = $mod->new_id;
444 $status = true;
447 break;
448 case "choose":
449 if ($log->cmid) {
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);
452 if ($mod) {
453 $log->url = "view.php?id=".$log->cmid;
454 $log->info = $mod->new_id;
455 $status = true;
458 break;
459 case "choose again":
460 if ($log->cmid) {
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);
463 if ($mod) {
464 $log->url = "view.php?id=".$log->cmid;
465 $log->info = $mod->new_id;
466 $status = true;
469 break;
470 case "view":
471 if ($log->cmid) {
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);
474 if ($mod) {
475 $log->url = "view.php?id=".$log->cmid;
476 $log->info = $mod->new_id;
477 $status = true;
480 break;
481 case "view all":
482 $log->url = "index.php?id=".$log->course;
483 $status = true;
484 break;
485 case "report":
486 if ($log->cmid) {
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);
489 if ($mod) {
490 $log->url = "report.php?id=".$log->cmid;
491 $log->info = $mod->new_id;
492 $status = true;
495 break;
496 default:
497 if (!defined('RESTORE_SILENTLY')) {
498 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
500 break;
503 if ($status) {
504 $status = $log;
506 return $status;