MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / lesson / restorelib.php
blob38e4363d7f22f3afb228b5e79d98ab63643aad62
1 <?php //$Id$
2 /**
3 * This php script contains all the stuff to restore lesson mods
5 * @version $Id$
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package lesson
8 **/
10 //This is the "graphical" structure of the lesson mod:
12 // lesson_default lesson ----------------------------|--------------------------|--------------------------|
13 // (UL, pk->id,fk->courseid) (CL,pk->id) | | |
14 // | | | |
15 // | lesson_grades lesson_high_scores lesson_timer
16 // | (UL, pk->id,fk->lessonid) (UL, pk->id,fk->lessonid) (UL, pk->id,fk->lessonid)
17 // |
18 // |
19 // lesson_pages---------------------------|
20 // (CL,pk->id,fk->lessonid) |
21 // | |
22 // | lesson_branch
23 // | (UL, pk->id,fk->pageid)
24 // lesson_answers
25 // (CL,pk->id,fk->pageid)
26 // |
27 // |
28 // |
29 // lesson_attempts
30 // (UL,pk->id,fk->answerid)
32 // Meaning: pk->primary key field of the table
33 // fk->foreign key to link with parent
34 // nt->nested field (recursive data)
35 // CL->course level info
36 // UL->user level info
37 // files->table may have files)
39 //-----------------------------------------------------------
41 //This function executes all the restore procedure about this mod
42 function lesson_restore_mods($mod,$restore) {
44 global $CFG;
46 $status = true;
48 //Get record from backup_ids
49 $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
51 if ($data) {
52 //Now get completed xmlized object
53 $info = $data->info;
54 //if necessary, write to restorelog and adjust date/time fields
55 if ($restore->course_startdateoffset) {
56 restore_log_date_changes('Lesson', $restore, $info['MOD']['#'], array('AVAILABLE', 'DEADLINE'));
58 //traverse_xmlize($info); //Debug
59 //print_object ($GLOBALS['traverse_array']); //Debug
60 //$GLOBALS['traverse_array']=""; //Debug
62 //Now, build the lesson record structure
63 $lesson->course = $restore->course_id;
64 $lesson->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
65 $lesson->practice = backup_todb($info['MOD']['#']['PRACTICE']['0']['#']);
66 $lesson->modattempts = backup_todb($info['MOD']['#']['MODATTEMPTS']['0']['#']);
67 $lesson->usepassword = backup_todb($info['MOD']['#']['USEPASSWORD']['0']['#']);
68 $lesson->password = backup_todb($info['MOD']['#']['PASSWORD']['0']['#']);
69 $lesson->dependency = isset($info['MOD']['#']['DEPENDENCY']['0']['#'])?backup_todb($info['MOD']['#']['DEPENDENCY']['0']['#']):'';
70 $lesson->conditions = isset($info['MOD']['#']['CONDITIONS']['0']['#'])?backup_todb($info['MOD']['#']['CONDITIONS']['0']['#']):'';
71 $lesson->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
72 $lesson->custom = backup_todb($info['MOD']['#']['CUSTOM']['0']['#']);
73 $lesson->ongoing = backup_todb($info['MOD']['#']['ONGOING']['0']['#']);
74 $lesson->usemaxgrade = backup_todb($info['MOD']['#']['USEMAXGRADE']['0']['#']);
75 $lesson->maxanswers = backup_todb($info['MOD']['#']['MAXANSWERS']['0']['#']);
76 $lesson->maxattempts = backup_todb($info['MOD']['#']['MAXATTEMPTS']['0']['#']);
77 $lesson->review = backup_todb($info['MOD']['#']['REVIEW']['0']['#']);
78 $lesson->nextpagedefault = backup_todb($info['MOD']['#']['NEXTPAGEDEFAULT']['0']['#']);
79 $lesson->feedback = isset($info['MOD']['#']['FEEDBACK']['0']['#'])?backup_todb($info['MOD']['#']['FEEDBACK']['0']['#']):'';
80 $lesson->minquestions = backup_todb($info['MOD']['#']['MINQUESTIONS']['0']['#']);
81 $lesson->maxpages = backup_todb($info['MOD']['#']['MAXPAGES']['0']['#']);
82 $lesson->timed = backup_todb($info['MOD']['#']['TIMED']['0']['#']);
83 $lesson->maxtime = backup_todb($info['MOD']['#']['MAXTIME']['0']['#']);
84 $lesson->retake = backup_todb($info['MOD']['#']['RETAKE']['0']['#']);
85 $lesson->activitylink = isset($info['MOD']['#']['ACTIVITYLINK']['0']['#'])?backup_todb($info['MOD']['#']['ACTIVITYLINK']['0']['#']):'';
86 $lesson->mediafile = isset($info['MOD']['#']['MEDIAFILE']['0']['#'])?backup_todb($info['MOD']['#']['MEDIAFILE']['0']['#']):'';
87 $lesson->mediaheight = isset($info['MOD']['#']['MEDIAHEIGHT']['0']['#'])?backup_todb($info['MOD']['#']['MEDIAHEIGHT']['0']['#']):'';
88 $lesson->mediawidth = isset($info['MOD']['#']['MEDIAWIDTH']['0']['#'])?backup_todb($info['MOD']['#']['MEDIAWIDTH']['0']['#']):'';
89 $lesson->mediaclose = isset($info['MOD']['#']['MEDIACLOSE']['0']['#'])?backup_todb($info['MOD']['#']['MEDIACLOSE']['0']['#']):'';
90 $lesson->slideshow = backup_todb($info['MOD']['#']['SLIDESHOW']['0']['#']);
91 $lesson->width = backup_todb($info['MOD']['#']['WIDTH']['0']['#']);
92 $lesson->height = backup_todb($info['MOD']['#']['HEIGHT']['0']['#']);
93 $lesson->bgcolor = backup_todb($info['MOD']['#']['BGCOLOR']['0']['#']);
94 $lesson->displayleft = isset($info['MOD']['#']['DISPLAYLEFT']['0']['#'])?backup_todb($info['MOD']['#']['DISPLAYLEFT']['0']['#']):'';
95 $lesson->displayleftif = isset($info['MOD']['#']['DISPLAYLEFTIF']['0']['#'])?backup_todb($info['MOD']['#']['DISPLAYLEFTIF']['0']['#']):'';
96 $lesson->progressbar = isset($info['MOD']['#']['PROGRESSBAR']['0']['#'])?backup_todb($info['MOD']['#']['PROGRESSBAR']['0']['#']):'';
97 $lesson->highscores = backup_todb($info['MOD']['#']['SHOWHIGHSCORES']['0']['#']);
98 $lesson->maxhighscores = backup_todb($info['MOD']['#']['MAXHIGHSCORES']['0']['#']);
99 $lesson->available = backup_todb($info['MOD']['#']['AVAILABLE']['0']['#']);
100 $lesson->deadline = backup_todb($info['MOD']['#']['DEADLINE']['0']['#']);
101 $lesson->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
103 //The structure is equal to the db, so insert the lesson
104 $newid = insert_record("lesson", $lesson);
106 //Do some output
107 if (!defined('RESTORE_SILENTLY')) {
108 echo "<li>".get_string("modulename","lesson")." \"".format_string(stripslashes($lesson->name),true)."\"</li>";
110 backup_flush(300);
112 if ($newid) {
113 //We have the newid, update backup_ids
114 backup_putid($restore->backup_unique_code,$mod->modtype,
115 $mod->id, $newid);
116 //We have to restore the lesson pages which are held in their logical order...
117 $userdata = restore_userdata_selected($restore,"lesson",$mod->id);
118 $status = lesson_pages_restore_mods($newid,$info,$restore,$userdata);
119 //...and the user grades, high scores, and timer (if required)
120 if ($status) {
121 if ($userdata) {
122 if(!lesson_grades_restore_mods($newid,$info,$restore)) {
123 return false;
125 if (!lesson_high_scores_restore_mods($newid,$info,$restore)) {
126 return false;
128 if (!lesson_timer_restore_mods($newid,$info,$restore)) {
129 return false;
132 // restore the default for the course. Only do this once by checking for an id for lesson_default
133 $lessondefault = backup_getid($restore->backup_unique_code,'lesson_default',$restore->course_id);
134 if (!$lessondefault) {
135 $status = lesson_default_restore_mods($info,$restore);
139 } else {
140 $status = false;
142 } else {
143 $status = false;
145 return $status;
148 //This function restores the lesson_pages
149 function lesson_pages_restore_mods($lessonid,$info,$restore,$userdata=false) {
151 global $CFG;
153 $status = true;
155 //Get the lesson_elements array
156 $pages = $info['MOD']['#']['PAGES']['0']['#']['PAGE'];
158 //Iterate over lesson pages (they are held in their logical order)
159 $prevpageid = 0;
160 for($i = 0; $i < sizeof($pages); $i++) {
161 $page_info = $pages[$i];
162 //traverse_xmlize($ele_info); //Debug
163 //print_object ($GLOBALS['traverse_array']); //Debug
164 //$GLOBALS['traverse_array']=""; //Debug
166 //We'll need this later!!
167 $oldid = backup_todb($page_info['#']['PAGEID']['0']['#']);
169 //Now, build the lesson_pages record structure
170 $page->lessonid = $lessonid;
171 $page->prevpageid = $prevpageid;
172 $page->qtype = backup_todb($page_info['#']['QTYPE']['0']['#']);
173 $page->qoption = backup_todb($page_info['#']['QOPTION']['0']['#']);
174 $page->layout = backup_todb($page_info['#']['LAYOUT']['0']['#']);
175 $page->display = backup_todb($page_info['#']['DISPLAY']['0']['#']);
176 $page->timecreated = backup_todb($page_info['#']['TIMECREATED']['0']['#']);
177 $page->timemodified = backup_todb($page_info['#']['TIMEMODIFIED']['0']['#']);
178 $page->title = backup_todb($page_info['#']['TITLE']['0']['#']);
179 $page->contents = backup_todb($page_info['#']['CONTENTS']['0']['#']);
181 //The structure is equal to the db, so insert the lesson_pages
182 $newid = insert_record ("lesson_pages",$page);
184 //Fix the forwards link of the previous page
185 if ($prevpageid) {
186 if (!set_field("lesson_pages", "nextpageid", $newid, "id", $prevpageid)) {
187 error("Lesson restorelib: unable to update link");
190 $prevpageid = $newid;
192 //Do some output
193 if (($i+1) % 10 == 0) {
194 if (!defined('RESTORE_SILENTLY')) {
195 echo ".";
196 if (($i+1) % 200 == 0) {
197 echo "<br/>";
200 backup_flush(300);
203 if ($newid) {
204 //We have the newid, update backup_ids (restore logs will use it!!)
205 backup_putid($restore->backup_unique_code,"lesson_pages", $oldid, $newid);
206 //We have to restore the lesson_answers table now (a page level table)
207 $status = lesson_answers_restore($lessonid,$newid,$page_info,$restore,$userdata);
209 //Need to update useranswer field (which has answer id's in it)
210 //for matching and multi-answer multi-choice questions
211 if ($userdata) { // first check to see if we even have to do this
212 // if multi-answer multi-choice question or matching
213 if (($page->qtype == 3 && $page->qoption) ||
214 $page->qtype == 5) {
215 // get all the attempt records for this page
216 if ($attempts = get_records("lesson_attempts", "pageid", $newid)) {
217 foreach ($attempts as $attempt) {
218 unset($newuseranswer);
219 if ($attempt->useranswer != NULL) {
220 // explode the user answer. Each element in
221 // $useranswer is an old answer id, so needs to be updated
222 $useranswer = explode(",", $attempt->useranswer);
223 foreach ($useranswer as $oldanswerid) {
224 $backupdata = backup_getid($restore->backup_unique_code,"lesson_answers",$oldanswerid);
225 $newuseranswer[] = $backupdata->new_id;
227 // get the useranswer in the right format
228 $attempt->useranswer = implode(",", $newuseranswer);
229 // update it
230 update_record("lesson_attempts", $attempt);
237 // backup branch table info for branch tables.
238 if ($status && $userdata) {
239 if (!lesson_branch_restore($lessonid,$newid,$page_info,$restore)) {
240 return false;
243 } else {
244 $status = false;
248 //We've restored all the pages and answers, we now need to fix the jumps in the
249 //answer records if they are absolute
250 if ($answers = get_records("lesson_answers", "lessonid", $lessonid)) {
251 foreach ($answers as $answer) {
252 if ($answer->jumpto > 0) {
253 // change the absolute page id
254 $page = backup_getid($restore->backup_unique_code,"lesson_pages",$answer->jumpto);
255 if ($page) {
256 if (!set_field("lesson_answers", "jumpto", $page->new_id, "id", $answer->id)) {
257 error("Lesson restorelib: unable to reset jump");
263 return $status;
267 //This function restores the lesson_answers
268 function lesson_answers_restore($lessonid,$pageid,$info,$restore,$userdata=false) {
270 global $CFG;
272 $status = true;
274 //Get the lesson_answers array (optional)
275 if (isset($info['#']['ANSWERS']['0']['#']['ANSWER'])) {
276 // The following chunk of code is a fix for matching questions made
277 // pre moodle 1.5. Matching questions need two answer fields designated
278 // for correct and wrong responses before the rest of the answer fields.
279 if ($restore->backup_version <= 2004083124) { // Backup version for 1.4.5+
280 if ($ismatching = get_record('lesson_pages', 'id', $pageid)) { // get the page we just inserted
281 if ($ismatching->qtype == 5) { // check to make sure it is a matching question
282 $time = time(); // this may need to be changed
283 // make our 2 response answers
284 $newanswer->lessonid = $lessonid;
285 $newanswer->pageid = $pageid;
286 $newanswer->timecreated = $time;
287 $newanswer->timemodified = 0;
288 insert_record('lesson_answers', $newanswer);
289 insert_record('lesson_answers', $newanswer);
294 $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
296 //Iterate over lesson_answers
297 for($i = 0; $i < sizeof($answers); $i++) {
298 $answer_info = $answers[$i];
299 //traverse_xmlize($rub_info); //Debug
300 //print_object ($GLOBALS['traverse_array']); //Debug
301 //$GLOBALS['traverse_array']=""; //Debug
303 //We'll need this later!!
304 $oldid = backup_todb($answer_info['#']['ID']['0']['#']);
306 //Now, build the lesson_answers record structure
307 $answer->lessonid = $lessonid;
308 $answer->pageid = $pageid;
309 // the absolute jumps will need fixing later
310 $answer->jumpto = backup_todb($answer_info['#']['JUMPTO']['0']['#']);
311 $answer->grade = backup_todb($answer_info['#']['GRADE']['0']['#']);
312 $answer->score = backup_todb($answer_info['#']['SCORE']['0']['#']);
313 $answer->flags = backup_todb($answer_info['#']['FLAGS']['0']['#']);
314 $answer->timecreated = backup_todb($answer_info['#']['TIMECREATED']['0']['#']);
315 $answer->timemodified = backup_todb($answer_info['#']['TIMEMODIFIED']['0']['#']);
316 $answer->answer = backup_todb($answer_info['#']['ANSWERTEXT']['0']['#']);
317 $answer->response = backup_todb($answer_info['#']['RESPONSE']['0']['#']);
319 //The structure is equal to the db, so insert the lesson_answers
320 $newid = insert_record ("lesson_answers",$answer);
322 //Do some output
323 if (($i+1) % 10 == 0) {
324 if (!defined('RESTORE_SILENTLY')) {
325 echo ".";
326 if (($i+1) % 200 == 0) {
327 echo "<br/>";
330 backup_flush(300);
333 if ($newid) {
334 // need to store the id so we can update the useranswer
335 // field in attempts. This is done in the lesson_pages_restore_mods
336 backup_putid($restore->backup_unique_code,"lesson_answers", $oldid, $newid);
338 if ($userdata) {
339 //We have to restore the lesson_attempts table now (a answers level table)
340 $status = lesson_attempts_restore($lessonid, $pageid, $newid, $answer_info, $restore);
342 } else {
343 $status = false;
347 return $status;
351 //This function restores the attempts
352 function lesson_attempts_restore($lessonid, $pageid, $answerid, $info, $restore) {
354 global $CFG;
356 $status = true;
358 //Get the attempts array (optional)
359 if (isset($info['#']['ATTEMPTS']['0']['#']['ATTEMPT'])) {
360 $attempts = $info['#']['ATTEMPTS']['0']['#']['ATTEMPT'];
361 //Iterate over attempts
362 for($i = 0; $i < sizeof($attempts); $i++) {
363 $attempt_info = $attempts[$i];
364 //traverse_xmlize($sub_info); //Debug
365 //print_object ($GLOBALS['traverse_array']); //Debug
366 //$GLOBALS['traverse_array']=""; //Debug
368 //We'll need this later!!
369 $olduserid = backup_todb($attempt_info['#']['USERID']['0']['#']);
371 //Now, build the lesson_attempts record structure
372 $attempt->lessonid = $lessonid;
373 $attempt->pageid = $pageid;
374 $attempt->answerid = $answerid;
375 $attempt->userid = backup_todb($attempt_info['#']['USERID']['0']['#']);
376 $attempt->retry = backup_todb($attempt_info['#']['RETRY']['0']['#']);
377 $attempt->correct = backup_todb($attempt_info['#']['CORRECT']['0']['#']);
378 $attempt->useranswer = backup_todb($attempt_info['#']['USERANSWER']['0']['#']);
379 $attempt->timeseen = backup_todb($attempt_info['#']['TIMESEEN']['0']['#']);
381 //We have to recode the userid field
382 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
383 if ($user) {
384 $attempt->userid = $user->new_id;
387 //The structure is equal to the db, so insert the lesson_attempt
388 $newid = insert_record ("lesson_attempts",$attempt);
390 //Do some output
391 if (($i+1) % 50 == 0) {
392 if (!defined('RESTORE_SILENTLY')) {
393 echo ".";
394 if (($i+1) % 1000 == 0) {
395 echo "<br/>";
398 backup_flush(300);
403 return $status;
406 //This function restores the lesson_grades
407 function lesson_grades_restore_mods($lessonid, $info, $restore) {
409 global $CFG;
411 $status = true;
413 //Get the grades array (optional)
414 if (isset($info['MOD']['#']['GRADES']['0']['#']['GRADE'])) {
415 $grades = $info['MOD']['#']['GRADES']['0']['#']['GRADE'];
417 //Iterate over grades
418 for($i = 0; $i < sizeof($grades); $i++) {
419 $grade_info = $grades[$i];
420 //traverse_xmlize($grade_info); //Debug
421 //print_object ($GLOBALS['traverse_array']); //Debug
422 //$GLOBALS['traverse_array']=""; //Debug
424 //We'll need this later!!
425 $olduserid = backup_todb($grade_info['#']['USERID']['0']['#']);
427 //Now, build the lesson_GRADES record structure
428 $grade->lessonid = $lessonid;
429 $grade->userid = backup_todb($grade_info['#']['USERID']['0']['#']);
430 $grade->grade = backup_todb($grade_info['#']['GRADE_VALUE']['0']['#']);
431 $grade->late = backup_todb($grade_info['#']['LATE']['0']['#']);
432 $grade->completed = backup_todb($grade_info['#']['COMPLETED']['0']['#']);
434 //We have to recode the userid field
435 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
436 if ($user) {
437 $grade->userid = $user->new_id;
440 //The structure is equal to the db, so insert the lesson_grade
441 $newid = insert_record ("lesson_grades",$grade);
443 //Do some output
444 if (($i+1) % 50 == 0) {
445 if (!defined('RESTORE_SILENTLY')) {
446 echo ".";
447 if (($i+1) % 1000 == 0) {
448 echo "<br/>";
451 backup_flush(300);
454 if (!$newid) {
455 $status = false;
460 return $status;
465 //This function restores the lesson_branch
466 function lesson_branch_restore($lessonid, $pageid, $info, $restore) {
468 global $CFG;
470 $status = true;
472 //Get the branch array (optional)
473 if (isset($info['#']['BRANCHES']['0']['#']['BRANCH'])) {
474 $branches = $info['#']['BRANCHES']['0']['#']['BRANCH'];
475 //Iterate over branches
476 for($i = 0; $i < sizeof($branches); $i++) {
477 $branch_info = $branches[$i];
478 //traverse_xmlize($branch_info); //Debug
479 //print_object ($GLOBALS['traverse_array']); //Debug
480 //$GLOBALS['traverse_array']=""; //Debug
482 //We'll need this later!!
483 $olduserid = backup_todb($branch_info['#']['USERID']['0']['#']);
485 //Now, build the lesson_attempts record structure
486 $branch->lessonid = $lessonid;
487 $branch->userid = backup_todb($branch_info['#']['USERID']['0']['#']);
488 $branch->pageid = $pageid;
489 $branch->retry = backup_todb($branch_info['#']['RETRY']['0']['#']);
490 $branch->flag = backup_todb($branch_info['#']['FLAG']['0']['#']);
491 $branch->timeseen = backup_todb($branch_info['#']['TIMESEEN']['0']['#']);
493 //We have to recode the userid field
494 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
495 if ($user) {
496 $branch->userid = $user->new_id;
499 //The structure is equal to the db, so insert the lesson_attempt
500 $newid = insert_record ("lesson_branch",$branch);
502 //Do some output
503 if (($i+1) % 50 == 0) {
504 if (!defined('RESTORE_SILENTLY')) {
505 echo ".";
506 if (($i+1) % 1000 == 0) {
507 echo "<br/>";
510 backup_flush(300);
515 return $status;
518 //This function restores the lesson_timer
519 function lesson_timer_restore_mods($lessonid, $info, $restore) {
521 global $CFG;
523 $status = true;
524 //Get the timer array (optional)
525 if (isset($info['MOD']['#']['TIMES']['0']['#']['TIME'])) {
526 $times = $info['MOD']['#']['TIMES']['0']['#']['TIME'];
527 //Iterate over times
528 for($i = 0; $i < sizeof($times); $i++) {
529 $time_info = $times[$i];
530 //traverse_xmlize($time_info); //Debug
531 //print_object ($GLOBALS['traverse_array']); //Debug
532 //$GLOBALS['traverse_array']=""; //Debug
534 //We'll need this later!!
535 $olduserid = backup_todb($time_info['#']['USERID']['0']['#']);
537 //Now, build the lesson_time record structure
538 $time->lessonid = $lessonid;
539 $time->userid = backup_todb($time_info['#']['USERID']['0']['#']);
540 $time->starttime = backup_todb($time_info['#']['STARTTIME']['0']['#']);
541 $time->lessontime = backup_todb($time_info['#']['LESSONTIME']['0']['#']);
543 //We have to recode the userid field
544 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
545 if ($user) {
546 $time->userid = $user->new_id;
549 //The structure is equal to the db, so insert the lesson_grade
550 $newid = insert_record ("lesson_timer",$time);
552 //Do some output
553 if (($i+1) % 50 == 0) {
554 if (!defined('RESTORE_SILENTLY')) {
555 echo ".";
556 if (($i+1) % 1000 == 0) {
557 echo "<br/>";
560 backup_flush(300);
563 if (!$newid) {
564 $status = false;
569 return $status;
572 //This function restores the lesson_high_scores
573 function lesson_high_scores_restore_mods($lessonid, $info, $restore) {
575 global $CFG;
577 $status = true;
579 //Get the highscores array (optional)
580 if (isset($info['MOD']['#']['HIGHSCORES']['0']['#']['HIGHSCORE'])) {
581 $highscores = $info['MOD']['#']['HIGHSCORES']['0']['#']['HIGHSCORE'];
582 //Iterate over highscores
583 for($i = 0; $i < sizeof($highscores); $i++) {
584 $highscore_info = $highscores[$i];
585 //traverse_xmlize($highscore_info); //Debug
586 //print_object ($GLOBALS['traverse_array']); //Debug
587 //$GLOBALS['traverse_array']=""; //Debug
589 //We'll need this later!!
590 $olduserid = backup_todb($highscore_info['#']['USERID']['0']['#']);
592 //Now, build the lesson_highscores record structure
593 $highscore->lessonid = $lessonid;
594 $highscore->userid = backup_todb($highscore_info['#']['USERID']['0']['#']);
595 $highscore->gradeid = backup_todb($highscore_info['#']['GRADEID']['0']['#']);
596 $highscore->nickname = backup_todb($highscore_info['#']['NICKNAME']['0']['#']);
598 //We have to recode the userid field
599 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
600 if ($user) {
601 $highscore->userid = $user->new_id;
604 //The structure is equal to the db, so insert the lesson_grade
605 $newid = insert_record ("lesson_high_scores",$highscore);
607 //Do some output
608 if (($i+1) % 50 == 0) {
609 if (!defined('RESTORE_SILENTLY')) {
610 echo ".";
611 if (($i+1) % 1000 == 0) {
612 echo "<br/>";
615 backup_flush(300);
618 if (!$newid) {
619 $status = false;
624 return $status;
627 //This function restores the lesson_default
628 function lesson_default_restore_mods($info, $restore) {
630 global $CFG;
632 $status = true;
634 //Get the default array (optional)
635 if (isset($info['MOD']['#']['DEFAULTS'])) {
636 $defaults = $info['MOD']['#']['DEFAULTS'];
638 //Iterate over defaults (should only be 1!)
639 for($i = 0; $i < sizeof($defaults); $i++) {
640 $default_info = $defaults[$i];
641 //traverse_xmlize($default_info); //Debug
642 //print_object ($GLOBALS['traverse_array']); //Debug
643 //$GLOBALS['traverse_array']=""; //Debug
645 //Now, build the lesson_default record structure
646 $default->course = $restore->course_id;
647 $default->practice = backup_todb($default_info['#']['PRACTICE']['0']['#']);
648 $default->modattempts = backup_todb($default_info['#']['MODATTEMPTS']['0']['#']);
649 $default->usepassword = backup_todb($default_info['#']['USEPASSWORD']['0']['#']);
650 $default->password = backup_todb($default_info['#']['PASSWORD']['0']['#']);
651 $default->conditions = backup_todb($default_info['#']['CONDITIONS']['0']['#']);
652 $default->grade = backup_todb($default_info['#']['GRADE']['0']['#']);
653 $default->custom = backup_todb($default_info['#']['CUSTOM']['0']['#']);
654 $default->ongoing = backup_todb($default_info['#']['ONGOING']['0']['#']);
655 $default->usemaxgrade = backup_todb($default_info['#']['USEMAXGRADE']['0']['#']);
656 $default->maxanswers = backup_todb($default_info['#']['MAXANSWERS']['0']['#']);
657 $default->maxattempts = backup_todb($default_info['#']['MAXATTEMPTS']['0']['#']);
658 $default->review = backup_todb($default_info['#']['REVIEW']['0']['#']);
659 $default->nextpagedefault = backup_todb($default_info['#']['NEXTPAGEDEFAULT']['0']['#']);
660 $default->feedback = backup_todb($default_info['#']['FEEDBACK']['0']['#']);
661 $default->minquestions = backup_todb($default_info['#']['MINQUESTIONS']['0']['#']);
662 $default->maxpages = backup_todb($default_info['#']['MAXPAGES']['0']['#']);
663 $default->timed = backup_todb($default_info['#']['TIMED']['0']['#']);
664 $default->maxtime = backup_todb($default_info['#']['MAXTIME']['0']['#']);
665 $default->retake = backup_todb($default_info['#']['RETAKE']['0']['#']);
666 $default->mediaheight = backup_todb($default_info['#']['MEDIAHEIGHT']['0']['#']);
667 $default->mediawidth = backup_todb($default_info['#']['MEDIAWIDTH']['0']['#']);
668 $default->mediaclose = backup_todb($default_info['#']['MEDIACLOSE']['0']['#']);
669 $default->slideshow = backup_todb($default_info['#']['SLIDESHOW']['0']['#']);
670 $default->width = backup_todb($default_info['#']['WIDTH']['0']['#']);
671 $default->height = backup_todb($default_info['#']['HEIGHT']['0']['#']);
672 $default->bgcolor = backup_todb($default_info['#']['BGCOLOR']['0']['#']);
673 $default->displayleft = backup_todb($default_info['#']['DISPLAYLEFT']['0']['#']);
674 $default->displayleftif = backup_todb($default_info['#']['DISPLAYLEFTIF']['0']['#']);
675 $default->progressbar = backup_todb($default_info['#']['PROGRESSBAR']['0']['#']);
676 $default->highscores = backup_todb($default_info['#']['HIGHSCORES']['0']['#']);
677 $default->maxhighscores = backup_todb($default_info['#']['MAXHIGHSCORES']['0']['#']);
679 //The structure is equal to the db, so insert the lesson_grade
680 $newid = insert_record ("lesson_default",$default);
682 if ($newid) {
683 backup_putid($restore->backup_unique_code,'lesson_default',
684 $restore->course_id, $newid);
687 //Do some output
688 if (($i+1) % 50 == 0) {
689 if (!defined('RESTORE_SILENTLY')) {
690 echo ".";
691 if (($i+1) % 1000 == 0) {
692 echo "<br/>";
695 backup_flush(300);
698 if (!$newid) {
699 $status = false;
704 return $status;
707 //Return a content decoded to support interactivities linking. Every module
708 //should have its own. They are called automatically from
709 //lesson_decode_content_links_caller() function in each module
710 //in the restore process
711 function lesson_decode_content_links ($content,$restore) {
713 global $CFG;
715 $result = $content;
717 //Link to the list of lessons
719 $searchstring='/\$@(LESSONINDEX)\*([0-9]+)@\$/';
720 //We look for it
721 preg_match_all($searchstring,$content,$foundset);
722 //If found, then we are going to look for its new id (in backup tables)
723 if ($foundset[0]) {
724 //print_object($foundset); //Debug
725 //Iterate over foundset[2]. They are the old_ids
726 foreach($foundset[2] as $old_id) {
727 //We get the needed variables here (course id)
728 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
729 //Personalize the searchstring
730 $searchstring='/\$@(LESSONINDEX)\*('.$old_id.')@\$/';
731 //If it is a link to this course, update the link to its new location
732 if($rec->new_id) {
733 //Now replace it
734 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/lesson/index.php?id='.$rec->new_id,$result);
735 } else {
736 //It's a foreign link so leave it as original
737 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/lesson/index.php?id='.$old_id,$result);
742 //Link to lesson view by moduleid
744 $searchstring='/\$@(LESSONVIEWBYID)\*([0-9]+)@\$/';
745 //We look for it
746 preg_match_all($searchstring,$result,$foundset);
747 //If found, then we are going to look for its new id (in backup tables)
748 if ($foundset[0]) {
749 //print_object($foundset); //Debug
750 //Iterate over foundset[2]. They are the old_ids
751 foreach($foundset[2] as $old_id) {
752 //We get the needed variables here (course_modules id)
753 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
754 //Personalize the searchstring
755 $searchstring='/\$@(LESSONVIEWBYID)\*('.$old_id.')@\$/';
756 //If it is a link to this course, update the link to its new location
757 if($rec->new_id) {
758 //Now replace it
759 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/lesson/view.php?id='.$rec->new_id,$result);
760 } else {
761 //It's a foreign link so leave it as original
762 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/lesson/view.php?id='.$old_id,$result);
767 return $result;
770 //This function makes all the necessary calls to xxxx_decode_content_links()
771 //function in each module, passing them the desired contents to be decoded
772 //from backup format to destination site/course in order to mantain inter-activities
773 //working in the backup/restore process. It's called from restore_decode_content_links()
774 //function in restore process
775 function lesson_decode_content_links_caller($restore) {
776 global $CFG;
777 $status = true;
779 //Process every lesson PAGE in the course
780 if ($pages = get_records_sql ("SELECT p.id, p.contents
781 FROM {$CFG->prefix}lesson_pages p,
782 {$CFG->prefix}lesson l
783 WHERE l.course = $restore->course_id AND
784 p.lessonid = l.id")) {
785 //Iterate over each page->message
786 $i = 0; //Counter to send some output to the browser to avoid timeouts
787 foreach ($pages as $page) {
788 //Increment counter
789 $i++;
790 $content = $page->contents;
791 $result = restore_decode_content_links_worker($content,$restore);
792 if ($result != $content) {
793 //Update record
794 $page->contents = addslashes($result);
795 $status = update_record("lesson_pages",$page);
796 if (debugging()) {
797 if (!defined('RESTORE_SILENTLY')) {
798 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
802 //Do some output
803 if (($i+1) % 5 == 0) {
804 if (!defined('RESTORE_SILENTLY')) {
805 echo ".";
806 if (($i+1) % 100 == 0) {
807 echo "<br />";
810 backup_flush(300);
815 // Remap activity links
816 if ($lessons = get_records_select('lesson', "activitylink != 0 AND course = $restore->course_id", '', 'id, activitylink')) {
817 foreach ($lessons as $lesson) {
818 if ($newcmid = backup_getid($restore->backup_unique_code, 'course_modules', $lesson->activitylink)) {
819 $status = $status and set_field('lesson', 'activitylink', $newcmid->new_id, 'id', $lesson->id);
824 return $status;
827 //This function returns a log record with all the necessay transformations
828 //done. It's used by restore_log_module() to restore modules log.
829 function lesson_restore_logs($restore,$log) {
831 $status = false;
833 //Depending of the action, we recode different things
834 switch ($log->action) {
835 case "add":
836 if ($log->cmid) {
837 //Get the new_id of the module (to recode the info field)
838 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
839 if ($mod) {
840 $log->url = "view.php?id=".$log->cmid;
841 $log->info = $mod->new_id;
842 $status = true;
845 break;
846 case "update":
847 if ($log->cmid) {
848 //Get the new_id of the module (to recode the info field)
849 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
850 if ($mod) {
851 $log->url = "view.php?id=".$log->cmid;
852 $log->info = $mod->new_id;
853 $status = true;
856 break;
857 case "view all":
858 $log->url = "index.php?id=".$log->course;
859 $status = true;
860 break;
861 case "start":
862 if ($log->cmid) {
863 //Get the new_id of the module (to recode the info field)
864 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
865 if ($mod) {
866 $log->url = "view.php?id=".$log->cmid;
867 $log->info = $mod->new_id;
868 $status = true;
871 break;
872 case "end":
873 if ($log->cmid) {
874 //Get the new_id of the module (to recode the info field)
875 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
876 if ($mod) {
877 $log->url = "view.php?id=".$log->cmid;
878 $log->info = $mod->new_id;
879 $status = true;
882 break;
883 case "view":
884 if ($log->cmid) {
885 //Get the new_id of the page (to recode the url field)
886 $pag = backup_getid($restore->backup_unique_code,"lesson_pages",$log->info);
887 if ($pag) {
888 $log->url = "view.php?id=".$log->cmid."&action=navigation&pageid=".$pag->new_id;
889 $log->info = $pag->new_id;
890 $status = true;
893 break;
894 default:
895 if (!defined('RESTORE_SILENTLY')) {
896 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br/>"; //Debug
898 break;
901 if ($status) {
902 $status = $log;
904 return $status;