Fixing drift between CVS and git
[moodle.git] / mod / lesson / restorelib.php
1 <?php //$Id$
2 /**
3  * This php script contains all the stuff to restore lesson mods
4  *
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: 
11     //
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)
31     //
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)
38     //
39     //-----------------------------------------------------------
41     //This function executes all the restore procedure about this mod
42     function lesson_restore_mods($mod,$restore) {
43         global $CFG, $DB;
45         $status = true;
47         //Get record from backup_ids
48         $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
50         if ($data) {
51             //Now get completed xmlized object
52             $info = $data->info;
53             //if necessary, write to restorelog and adjust date/time fields
54             if ($restore->course_startdateoffset) {
55                 restore_log_date_changes('Lesson', $restore, $info['MOD']['#'], array('AVAILABLE', 'DEADLINE'));
56             }
57             //traverse_xmlize($info);                                                              //Debug
58             //print_object ($GLOBALS['traverse_array']);                                           //Debug
59             //$GLOBALS['traverse_array']="";                                                       //Debug
61             //Now, build the lesson record structure
62             $lesson->course = $restore->course_id;
63             $lesson->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
64             $lesson->practice = backup_todb($info['MOD']['#']['PRACTICE']['0']['#']);
65             $lesson->modattempts = backup_todb($info['MOD']['#']['MODATTEMPTS']['0']['#']);
66             $lesson->usepassword = backup_todb($info['MOD']['#']['USEPASSWORD']['0']['#']);
67             $lesson->password = backup_todb($info['MOD']['#']['PASSWORD']['0']['#']);            
68             $lesson->dependency = isset($info['MOD']['#']['DEPENDENCY']['0']['#'])?backup_todb($info['MOD']['#']['DEPENDENCY']['0']['#']):'';
69             $lesson->conditions = isset($info['MOD']['#']['CONDITIONS']['0']['#'])?backup_todb($info['MOD']['#']['CONDITIONS']['0']['#']):'';
70             $lesson->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
71             $lesson->custom = backup_todb($info['MOD']['#']['CUSTOM']['0']['#']);
72             $lesson->ongoing = backup_todb($info['MOD']['#']['ONGOING']['0']['#']);
73             $lesson->usemaxgrade = backup_todb($info['MOD']['#']['USEMAXGRADE']['0']['#']);
74             $lesson->maxanswers = backup_todb($info['MOD']['#']['MAXANSWERS']['0']['#']);
75             $lesson->maxattempts = backup_todb($info['MOD']['#']['MAXATTEMPTS']['0']['#']);
76             $lesson->review = backup_todb($info['MOD']['#']['REVIEW']['0']['#']);
77             $lesson->nextpagedefault = backup_todb($info['MOD']['#']['NEXTPAGEDEFAULT']['0']['#']);
78             $lesson->feedback = isset($info['MOD']['#']['FEEDBACK']['0']['#'])?backup_todb($info['MOD']['#']['FEEDBACK']['0']['#']):'';
79             $lesson->minquestions = backup_todb($info['MOD']['#']['MINQUESTIONS']['0']['#']);
80             $lesson->maxpages = backup_todb($info['MOD']['#']['MAXPAGES']['0']['#']);
81             $lesson->timed = backup_todb($info['MOD']['#']['TIMED']['0']['#']);
82             $lesson->maxtime = backup_todb($info['MOD']['#']['MAXTIME']['0']['#']);
83             $lesson->retake = backup_todb($info['MOD']['#']['RETAKE']['0']['#']);
84             $lesson->activitylink = isset($info['MOD']['#']['ACTIVITYLINK']['0']['#'])?backup_todb($info['MOD']['#']['ACTIVITYLINK']['0']['#']):'';
85             $lesson->mediafile = isset($info['MOD']['#']['MEDIAFILE']['0']['#'])?backup_todb($info['MOD']['#']['MEDIAFILE']['0']['#']):'';
86             $lesson->mediaheight = isset($info['MOD']['#']['MEDIAHEIGHT']['0']['#'])?backup_todb($info['MOD']['#']['MEDIAHEIGHT']['0']['#']):'';
87             $lesson->mediawidth = isset($info['MOD']['#']['MEDIAWIDTH']['0']['#'])?backup_todb($info['MOD']['#']['MEDIAWIDTH']['0']['#']):'';
88             $lesson->mediaclose = isset($info['MOD']['#']['MEDIACLOSE']['0']['#'])?backup_todb($info['MOD']['#']['MEDIACLOSE']['0']['#']):'';
89             $lesson->slideshow = backup_todb($info['MOD']['#']['SLIDESHOW']['0']['#']);
90             $lesson->width = backup_todb($info['MOD']['#']['WIDTH']['0']['#']);
91             $lesson->height = backup_todb($info['MOD']['#']['HEIGHT']['0']['#']);
92             $lesson->bgcolor = backup_todb($info['MOD']['#']['BGCOLOR']['0']['#']);
93             $lesson->displayleft = isset($info['MOD']['#']['DISPLAYLEFT']['0']['#'])?backup_todb($info['MOD']['#']['DISPLAYLEFT']['0']['#']):'';
94             $lesson->displayleftif = isset($info['MOD']['#']['DISPLAYLEFTIF']['0']['#'])?backup_todb($info['MOD']['#']['DISPLAYLEFTIF']['0']['#']):'';
95             $lesson->progressbar = isset($info['MOD']['#']['PROGRESSBAR']['0']['#'])?backup_todb($info['MOD']['#']['PROGRESSBAR']['0']['#']):'';
96             $lesson->highscores = backup_todb($info['MOD']['#']['SHOWHIGHSCORES']['0']['#']);
97             $lesson->maxhighscores = backup_todb($info['MOD']['#']['MAXHIGHSCORES']['0']['#']);
98             $lesson->available = backup_todb($info['MOD']['#']['AVAILABLE']['0']['#']);
99             $lesson->deadline = backup_todb($info['MOD']['#']['DEADLINE']['0']['#']);
100             $lesson->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
102             //The structure is equal to the db, so insert the lesson
103             $newid = $DB->insert_record("lesson", $lesson);
105             //Do some output
106             if (!defined('RESTORE_SILENTLY')) {
107                 echo "<li>".get_string("modulename","lesson")." \"".format_string($lesson->name,true)."\"</li>";
108             }
109             backup_flush(300);
111             if ($newid) {
112                 //We have the newid, update backup_ids
113                 backup_putid($restore->backup_unique_code,$mod->modtype,
114                              $mod->id, $newid);
115                 //We have to restore the lesson pages which are held in their logical order...
116                 $userdata = restore_userdata_selected($restore,"lesson",$mod->id);
117                 $status = lesson_pages_restore_mods($newid,$info,$restore,$userdata);
118                 //...and the user grades, high scores, and timer (if required)
119                 if ($status) {
120                     if ($userdata) {
121                         if(!lesson_grades_restore_mods($newid,$info,$restore)) {
122                             return false;
123                         }
124                         if (!lesson_high_scores_restore_mods($newid,$info,$restore)) {
125                             return false;
126                         }
127                         if (!lesson_timer_restore_mods($newid,$info,$restore)) {
128                             return false;
129                         }
130                     }
131                     // restore the default for the course.  Only do this once by checking for an id for lesson_default
132                     $lessondefault = backup_getid($restore->backup_unique_code,'lesson_default',$restore->course_id);
133                     if (!$lessondefault) {
134                         $status = lesson_default_restore_mods($info,$restore);
135                     }
136                     
137                 }
138             } else {
139                 $status = false;
140             }
141         } else {
142             $status = false;
143         }
144         return $status;
145     }
147     //This function restores the lesson_pages
148     function lesson_pages_restore_mods($lessonid,$info,$restore,$userdata=false) {
149         global $CFG, $DB;
151         $status = true;
153         //Get the lesson_elements array
154         $pages = $info['MOD']['#']['PAGES']['0']['#']['PAGE'];
156         //Iterate over lesson pages (they are held in their logical order)
157         $prevpageid = 0;
158         for($i = 0; $i < sizeof($pages); $i++) {
159             $page_info = $pages[$i];
160             //traverse_xmlize($ele_info);                                                          //Debug
161             //print_object ($GLOBALS['traverse_array']);                                           //Debug
162             //$GLOBALS['traverse_array']="";                                                       //Debug
164             //We'll need this later!!
165             $oldid = backup_todb($page_info['#']['PAGEID']['0']['#']);
167             //Now, build the lesson_pages record structure
168             $page->lessonid = $lessonid;
169             $page->prevpageid = $prevpageid;
170             $page->qtype = backup_todb($page_info['#']['QTYPE']['0']['#']);
171             $page->qoption = backup_todb($page_info['#']['QOPTION']['0']['#']);
172             $page->layout = backup_todb($page_info['#']['LAYOUT']['0']['#']);
173             $page->display = backup_todb($page_info['#']['DISPLAY']['0']['#']);
174             $page->timecreated = backup_todb($page_info['#']['TIMECREATED']['0']['#']);
175             $page->timemodified = backup_todb($page_info['#']['TIMEMODIFIED']['0']['#']);
176             $page->title = backup_todb($page_info['#']['TITLE']['0']['#']);
177             $page->contents = backup_todb($page_info['#']['CONTENTS']['0']['#']);
179             //The structure is equal to the db, so insert the lesson_pages
180             $newid = $DB->insert_record ("lesson_pages",$page);
182             //Fix the forwards link of the previous page
183             if ($prevpageid) {
184                 $DB->set_field("lesson_pages", "nextpageid", $newid, array("id"=>$prevpageid));
185             }
186             $prevpageid = $newid;
188             //Do some output
189             if (($i+1) % 10 == 0) {
190                 if (!defined('RESTORE_SILENTLY')) {
191                     echo ".";
192                     if (($i+1) % 200 == 0) {
193                         echo "<br/>";
194                     }
195                 }
196                 backup_flush(300);
197             }
199             if ($newid) {
200                 //We have the newid, update backup_ids (restore logs will use it!!)
201                 backup_putid($restore->backup_unique_code,"lesson_pages", $oldid, $newid);
202                 //We have to restore the lesson_answers table now (a page level table)
203                 $status = lesson_answers_restore($lessonid,$newid,$page_info,$restore,$userdata);
204                 
205                 //Need to update useranswer field (which has answer id's in it)
206                 //for matching and multi-answer multi-choice questions
207                 if ($userdata) { // first check to see if we even have to do this
208                     // if multi-answer multi-choice question or matching
209                     if (($page->qtype == 3 && $page->qoption) ||
210                          $page->qtype == 5) {
211                         // get all the attempt records for this page
212                         if ($attempts = $DB->get_records("lesson_attempts", array("pageid"=>$newid))) {
213                             foreach ($attempts as $attempt) {
214                                 unset($newuseranswer);
215                                 if ($attempt->useranswer != NULL) {
216                                     // explode the user answer.  Each element in
217                                     // $useranswer is an old answer id, so needs to be updated
218                                     $useranswer = explode(",", $attempt->useranswer);
219                                     foreach ($useranswer as $oldanswerid) {
220                                          $backupdata = backup_getid($restore->backup_unique_code,"lesson_answers",$oldanswerid);
221                                          $newuseranswer[] = $backupdata->new_id;
222                                     }
223                                     // get the useranswer in the right format
224                                     $attempt->useranswer = implode(",", $newuseranswer);
225                                     // update it
226                                     $DB->update_record("lesson_attempts", $attempt);
227                                 }
228                             }
229                         }
230                     }
231                 }        
232                 
233                 // backup branch table info for branch tables.
234                 if ($status && $userdata) {
235                     if (!lesson_branch_restore($lessonid,$newid,$page_info,$restore)) {
236                         return false;
237                     }
238                 }
239             } else {
240                 $status = false;
241             }
242         }
244         //We've restored all the pages and answers, we now need to fix the jumps in the
245         //answer records if they are absolute
246         if ($answers = $DB->get_records("lesson_answers", array("lessonid"=>$lessonid))) {
247             foreach ($answers as $answer) {
248                 if ($answer->jumpto > 0) {
249                     // change the absolute page id
250                     $page = backup_getid($restore->backup_unique_code,"lesson_pages",$answer->jumpto);
251                     if ($page) {
252                         $DB->set_field("lesson_answers", "jumpto", $page->new_id, array("id"=>$answer->id));
253                     }
254                 }
255             }
256         }
257         return $status;
258     }
261     //This function restores the lesson_answers
262     function lesson_answers_restore($lessonid,$pageid,$info,$restore,$userdata=false) {
263         global $CFG, $DB;
265         $status = true;
267         //Get the lesson_answers array (optional)
268         if (isset($info['#']['ANSWERS']['0']['#']['ANSWER'])) {
269             // The following chunk of code is a fix for matching questions made
270             // pre moodle 1.5.  Matching questions need two answer fields designated
271             // for correct and wrong responses before the rest of the answer fields.
272             if ($restore->backup_version <= 2004083124) {  // Backup version for 1.4.5+
273                 if ($ismatching = $DB->get_record('lesson_pages', array('id'=>$pageid))) {  // get the page we just inserted
274                     if ($ismatching->qtype == 5) { // check to make sure it is a matching question
275                         $time = time();  // this may need to be changed
276                         // make our 2 response answers
277                         $newanswer->lessonid = $lessonid;
278                         $newanswer->pageid = $pageid;
279                         $newanswer->timecreated = $time;
280                         $newanswer->timemodified = 0;
281                         $DB->insert_record('lesson_answers', $newanswer);
282                         $DB->insert_record('lesson_answers', $newanswer); // TODO: why is this here twice?
283                     }
284                 }
285             }
287             $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
289             //Iterate over lesson_answers
290             for($i = 0; $i < sizeof($answers); $i++) {
291                 $answer_info = $answers[$i];
292                 //traverse_xmlize($rub_info);                                  //Debug
293                 //print_object ($GLOBALS['traverse_array']);                   //Debug
294                 //$GLOBALS['traverse_array']="";                               //Debug
296                 //We'll need this later!!
297                 $oldid = backup_todb($answer_info['#']['ID']['0']['#']);
299                 //Now, build the lesson_answers record structure
300                 $answer->lessonid = $lessonid;
301                 $answer->pageid = $pageid;
302                 // the absolute jumps will need fixing later
303                 $answer->jumpto = backup_todb($answer_info['#']['JUMPTO']['0']['#']);
304                 $answer->grade = backup_todb($answer_info['#']['GRADE']['0']['#']);
305                 $answer->score = backup_todb($answer_info['#']['SCORE']['0']['#']);
306                 $answer->flags = backup_todb($answer_info['#']['FLAGS']['0']['#']);
307                 $answer->timecreated = backup_todb($answer_info['#']['TIMECREATED']['0']['#']);
308                 $answer->timemodified = backup_todb($answer_info['#']['TIMEMODIFIED']['0']['#']);
309                 $answer->answer = backup_todb($answer_info['#']['ANSWERTEXT']['0']['#']);
310                 $answer->response = backup_todb($answer_info['#']['RESPONSE']['0']['#']);
312                 //The structure is equal to the db, so insert the lesson_answers
313                 $newid = $DB->insert_record ("lesson_answers",$answer);
315                 //Do some output
316                 if (($i+1) % 10 == 0) {
317                     if (!defined('RESTORE_SILENTLY')) {
318                         echo ".";
319                         if (($i+1) % 200 == 0) {
320                             echo "<br/>";
321                         }
322                     }
323                     backup_flush(300);
324                 }
326                 if ($newid) {
327                     // need to store the id so we can update the useranswer
328                     // field in attempts.  This is done in the lesson_pages_restore_mods
329                     backup_putid($restore->backup_unique_code,"lesson_answers", $oldid, $newid);                                 
331                     if ($userdata) {
332                         //We have to restore the lesson_attempts table now (a answers level table)
333                         $status = lesson_attempts_restore($lessonid, $pageid, $newid, $answer_info, $restore);
334                     }
335                 } else {
336                     $status = false;
337                 }
338             }
339         }
340         return $status;
341     }
344     //This function restores the attempts
345     function lesson_attempts_restore($lessonid, $pageid, $answerid, $info, $restore) {
346         global $CFG, $DB;
348         $status = true;
350         //Get the attempts array (optional)
351         if (isset($info['#']['ATTEMPTS']['0']['#']['ATTEMPT'])) {
352             $attempts = $info['#']['ATTEMPTS']['0']['#']['ATTEMPT'];
353             //Iterate over attempts
354             for($i = 0; $i < sizeof($attempts); $i++) {
355                 $attempt_info = $attempts[$i];
356                 //traverse_xmlize($sub_info);                                                         //Debug
357                 //print_object ($GLOBALS['traverse_array']);                                          //Debug
358                 //$GLOBALS['traverse_array']="";                                                      //Debug
360                 //We'll need this later!!
361                 $olduserid = backup_todb($attempt_info['#']['USERID']['0']['#']);
363                 //Now, build the lesson_attempts record structure
364                 $attempt->lessonid = $lessonid;
365                 $attempt->pageid = $pageid;
366                 $attempt->answerid = $answerid;
367                 $attempt->userid = backup_todb($attempt_info['#']['USERID']['0']['#']);
368                 $attempt->retry = backup_todb($attempt_info['#']['RETRY']['0']['#']);
369                 $attempt->correct = backup_todb($attempt_info['#']['CORRECT']['0']['#']);
370                 $attempt->useranswer = backup_todb($attempt_info['#']['USERANSWER']['0']['#']);
371                 $attempt->timeseen = backup_todb($attempt_info['#']['TIMESEEN']['0']['#']);
373                 //We have to recode the userid field
374                 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
375                 if ($user) {
376                     $attempt->userid = $user->new_id;
377                 }
379                 //The structure is equal to the db, so insert the lesson_attempt
380                 $newid = $DB->insert_record ("lesson_attempts",$attempt);
382                 //Do some output
383                 if (($i+1) % 50 == 0) {
384                     if (!defined('RESTORE_SILENTLY')) {
385                         echo ".";
386                         if (($i+1) % 1000 == 0) {
387                             echo "<br/>";
388                         }
389                     }
390                     backup_flush(300);
391                 }
392             }
393         }
395     return $status;
396     }
398     //This function restores the lesson_grades
399     function lesson_grades_restore_mods($lessonid, $info, $restore) {
400         global $CFG, $DB;
402         $status = true;
404         //Get the grades array (optional)
405         if (isset($info['MOD']['#']['GRADES']['0']['#']['GRADE'])) {
406             $grades = $info['MOD']['#']['GRADES']['0']['#']['GRADE'];
408             //Iterate over grades
409             for($i = 0; $i < sizeof($grades); $i++) {
410                 $grade_info = $grades[$i];
411                 //traverse_xmlize($grade_info);                         //Debug
412                 //print_object ($GLOBALS['traverse_array']);            //Debug
413                 //$GLOBALS['traverse_array']="";                        //Debug
415                 //We'll need this later!!
416                 $olduserid = backup_todb($grade_info['#']['USERID']['0']['#']);
418                 //Now, build the lesson_GRADES record structure
419                 $grade->lessonid = $lessonid;
420                 $grade->userid = backup_todb($grade_info['#']['USERID']['0']['#']);
421                 $grade->grade = backup_todb($grade_info['#']['GRADE_VALUE']['0']['#']);
422                 $grade->late = backup_todb($grade_info['#']['LATE']['0']['#']);
423                 $grade->completed = backup_todb($grade_info['#']['COMPLETED']['0']['#']);
425                 //We have to recode the userid field
426                 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
427                 if ($user) {
428                     $grade->userid = $user->new_id;
429                 }
431                 //The structure is equal to the db, so insert the lesson_grade
432                 $newid = $DB->insert_record ("lesson_grades",$grade);
434                 //Do some output
435                 if (($i+1) % 50 == 0) {
436                     if (!defined('RESTORE_SILENTLY')) {
437                         echo ".";
438                         if (($i+1) % 1000 == 0) {
439                             echo "<br/>";
440                         }
441                     }
442                     backup_flush(300);
443                 }
445                 if (!$newid) {
446                     $status = false;
447                 }
448             }
449         }
451         return $status;
452     }
453     
454     
455     
456     //This function restores the lesson_branch
457     function lesson_branch_restore($lessonid, $pageid, $info, $restore) {
458         global $CFG, $DB;
460         $status = true;
462         //Get the branch array (optional)
463         if (isset($info['#']['BRANCHES']['0']['#']['BRANCH'])) {
464             $branches = $info['#']['BRANCHES']['0']['#']['BRANCH'];
465             //Iterate over branches
466             for($i = 0; $i < sizeof($branches); $i++) {
467                 $branch_info = $branches[$i];
468                 //traverse_xmlize($branch_info);                                                         //Debug
469                 //print_object ($GLOBALS['traverse_array']);                                          //Debug
470                 //$GLOBALS['traverse_array']="";                                                      //Debug
472                 //We'll need this later!!
473                 $olduserid = backup_todb($branch_info['#']['USERID']['0']['#']);
475                 //Now, build the lesson_attempts record structure
476                 $branch->lessonid = $lessonid;
477                 $branch->userid = backup_todb($branch_info['#']['USERID']['0']['#']);
478                 $branch->pageid = $pageid;
479                 $branch->retry = backup_todb($branch_info['#']['RETRY']['0']['#']);
480                 $branch->flag = backup_todb($branch_info['#']['FLAG']['0']['#']);
481                 $branch->timeseen = backup_todb($branch_info['#']['TIMESEEN']['0']['#']);
483                 //We have to recode the userid field
484                 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
485                 if ($user) {
486                     $branch->userid = $user->new_id;
487                 }
489                 //The structure is equal to the db, so insert the lesson_attempt
490                 $newid = $DB->insert_record ("lesson_branch",$branch);
492                 //Do some output
493                 if (($i+1) % 50 == 0) {
494                     if (!defined('RESTORE_SILENTLY')) {
495                         echo ".";
496                         if (($i+1) % 1000 == 0) {
497                             echo "<br/>";
498                         }
499                     }
500                     backup_flush(300);
501                 }
502             }
503         }
505     return $status;
506     }
508     //This function restores the lesson_timer
509     function lesson_timer_restore_mods($lessonid, $info, $restore) {
510         global $CFG, $DB;
512         $status = true;
513         //Get the timer array (optional)
514         if (isset($info['MOD']['#']['TIMES']['0']['#']['TIME'])) {
515             $times = $info['MOD']['#']['TIMES']['0']['#']['TIME'];
516             //Iterate over times
517             for($i = 0; $i < sizeof($times); $i++) {
518                 $time_info = $times[$i];
519                 //traverse_xmlize($time_info);                         //Debug
520                 //print_object ($GLOBALS['traverse_array']);            //Debug
521                 //$GLOBALS['traverse_array']="";                        //Debug
523                 //We'll need this later!!
524                 $olduserid = backup_todb($time_info['#']['USERID']['0']['#']);
526                 //Now, build the lesson_time record structure
527                 $time->lessonid = $lessonid;
528                 $time->userid = backup_todb($time_info['#']['USERID']['0']['#']);
529                 $time->starttime = backup_todb($time_info['#']['STARTTIME']['0']['#']);
530                 $time->lessontime = backup_todb($time_info['#']['LESSONTIME']['0']['#']);
532                 //We have to recode the userid field
533                 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
534                 if ($user) {
535                     $time->userid = $user->new_id;
536                 }
538                 //The structure is equal to the db, so insert the lesson_grade
539                 $newid = $DB->insert_record ("lesson_timer",$time);
541                 //Do some output
542                 if (($i+1) % 50 == 0) {
543                     if (!defined('RESTORE_SILENTLY')) {
544                         echo ".";
545                         if (($i+1) % 1000 == 0) {
546                             echo "<br/>";
547                         }
548                     }
549                     backup_flush(300);
550                 }
552                 if (!$newid) {
553                     $status = false;
554                 }
555             }
556         }
558         return $status;
559     }
561     //This function restores the lesson_high_scores
562     function lesson_high_scores_restore_mods($lessonid, $info, $restore) {
563         global $CFG, $DB;
565         $status = true;
567         //Get the highscores array (optional)
568         if (isset($info['MOD']['#']['HIGHSCORES']['0']['#']['HIGHSCORE'])) {
569             $highscores = $info['MOD']['#']['HIGHSCORES']['0']['#']['HIGHSCORE'];
570             //Iterate over highscores
571             for($i = 0; $i < sizeof($highscores); $i++) {
572                 $highscore_info = $highscores[$i];
573                 //traverse_xmlize($highscore_info);                     //Debug
574                 //print_object ($GLOBALS['traverse_array']);            //Debug
575                 //$GLOBALS['traverse_array']="";                        //Debug
577                 //We'll need this later!!
578                 $olduserid = backup_todb($highscore_info['#']['USERID']['0']['#']);
580                 //Now, build the lesson_highscores record structure
581                 $highscore->lessonid = $lessonid;
582                 $highscore->userid = backup_todb($highscore_info['#']['USERID']['0']['#']);
583                 $highscore->gradeid = backup_todb($highscore_info['#']['GRADEID']['0']['#']);
584                 $highscore->nickname = backup_todb($highscore_info['#']['NICKNAME']['0']['#']);
586                 //We have to recode the userid field
587                 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
588                 if ($user) {
589                     $highscore->userid = $user->new_id;
590                 }
591                 
592                 //The structure is equal to the db, so insert the lesson_grade
593                 $newid = $DB->insert_record ("lesson_high_scores",$highscore);
595                 //Do some output
596                 if (($i+1) % 50 == 0) {
597                     if (!defined('RESTORE_SILENTLY')) {
598                         echo ".";
599                         if (($i+1) % 1000 == 0) {
600                             echo "<br/>";
601                         }
602                     }
603                     backup_flush(300);
604                 }
606                 if (!$newid) {
607                     $status = false;
608                 }
609             }
610         }
612         return $status;
613     }
614     
615     //This function restores the lesson_default
616     function lesson_default_restore_mods($info, $restore) {
617         global $CFG, $DB;
619         $status = true;
621         //Get the default array (optional)
622         if (isset($info['MOD']['#']['DEFAULTS'])) {
623             $defaults = $info['MOD']['#']['DEFAULTS'];
625             //Iterate over defaults (should only be 1!)
626             for($i = 0; $i < sizeof($defaults); $i++) {
627                 $default_info = $defaults[$i];
628                 //traverse_xmlize($default_info);                       //Debug
629                 //print_object ($GLOBALS['traverse_array']);            //Debug
630                 //$GLOBALS['traverse_array']="";                        //Debug
632                 //Now, build the lesson_default record structure
633                 $default->course = $restore->course_id;
634                 $default->practice = backup_todb($default_info['#']['PRACTICE']['0']['#']);
635                 $default->modattempts = backup_todb($default_info['#']['MODATTEMPTS']['0']['#']);
636                 $default->usepassword = backup_todb($default_info['#']['USEPASSWORD']['0']['#']);
637                 $default->password = backup_todb($default_info['#']['PASSWORD']['0']['#']);
638                 $default->conditions = backup_todb($default_info['#']['CONDITIONS']['0']['#']);
639                 $default->grade = backup_todb($default_info['#']['GRADE']['0']['#']);
640                 $default->custom = backup_todb($default_info['#']['CUSTOM']['0']['#']);
641                 $default->ongoing = backup_todb($default_info['#']['ONGOING']['0']['#']);
642                 $default->usemaxgrade = backup_todb($default_info['#']['USEMAXGRADE']['0']['#']);
643                 $default->maxanswers = backup_todb($default_info['#']['MAXANSWERS']['0']['#']);
644                 $default->maxattempts = backup_todb($default_info['#']['MAXATTEMPTS']['0']['#']);
645                 $default->review = backup_todb($default_info['#']['REVIEW']['0']['#']);
646                 $default->nextpagedefault = backup_todb($default_info['#']['NEXTPAGEDEFAULT']['0']['#']);
647                 $default->feedback = backup_todb($default_info['#']['FEEDBACK']['0']['#']);
648                 $default->minquestions = backup_todb($default_info['#']['MINQUESTIONS']['0']['#']);
649                 $default->maxpages = backup_todb($default_info['#']['MAXPAGES']['0']['#']);
650                 $default->timed = backup_todb($default_info['#']['TIMED']['0']['#']);
651                 $default->maxtime = backup_todb($default_info['#']['MAXTIME']['0']['#']);
652                 $default->retake = backup_todb($default_info['#']['RETAKE']['0']['#']);
653                 $default->mediaheight = backup_todb($default_info['#']['MEDIAHEIGHT']['0']['#']);
654                 $default->mediawidth = backup_todb($default_info['#']['MEDIAWIDTH']['0']['#']);
655                 $default->mediaclose = backup_todb($default_info['#']['MEDIACLOSE']['0']['#']);
656                 $default->slideshow = backup_todb($default_info['#']['SLIDESHOW']['0']['#']);
657                 $default->width = backup_todb($default_info['#']['WIDTH']['0']['#']);
658                 $default->height = backup_todb($default_info['#']['HEIGHT']['0']['#']);
659                 $default->bgcolor = backup_todb($default_info['#']['BGCOLOR']['0']['#']);
660                 $default->displayleft = backup_todb($default_info['#']['DISPLAYLEFT']['0']['#']);
661                 $default->displayleftif = backup_todb($default_info['#']['DISPLAYLEFTIF']['0']['#']);
662                 $default->progressbar = backup_todb($default_info['#']['PROGRESSBAR']['0']['#']);
663                 $default->highscores = backup_todb($default_info['#']['HIGHSCORES']['0']['#']);
664                 $default->maxhighscores = backup_todb($default_info['#']['MAXHIGHSCORES']['0']['#']);
666                 //The structure is equal to the db, so insert the lesson_grade
667                 $newid = $DB->insert_record ("lesson_default",$default);
668                 
669                 if ($newid) {
670                     backup_putid($restore->backup_unique_code,'lesson_default',
671                                  $restore->course_id, $newid);
672                 }
673                 
674                 //Do some output
675                 if (($i+1) % 50 == 0) {
676                     if (!defined('RESTORE_SILENTLY')) {
677                         echo ".";
678                         if (($i+1) % 1000 == 0) {
679                             echo "<br/>";
680                         }
681                     }
682                     backup_flush(300);
683                 }
685                 if (!$newid) {
686                     $status = false;
687                 }
688             }
689         }
691         return $status;
692     }
694     //Return a content decoded to support interactivities linking. Every module
695     //should have its own. They are called automatically from
696     //lesson_decode_content_links_caller() function in each module
697     //in the restore process
698     function lesson_decode_content_links ($content,$restore) {
699         global $CFG;
700             
701         $result = $content;
702                 
703         //Link to the list of lessons
704                 
705         $searchstring='/\$@(LESSONINDEX)\*([0-9]+)@\$/';
706         //We look for it
707         preg_match_all($searchstring,$content,$foundset);
708         //If found, then we are going to look for its new id (in backup tables)
709         if ($foundset[0]) {
710             //print_object($foundset);                                     //Debug
711             //Iterate over foundset[2]. They are the old_ids
712             foreach($foundset[2] as $old_id) {
713                 //We get the needed variables here (course id)
714                 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
715                 //Personalize the searchstring
716                 $searchstring='/\$@(LESSONINDEX)\*('.$old_id.')@\$/';
717                 //If it is a link to this course, update the link to its new location
718                 if($rec->new_id) {
719                     //Now replace it
720                     $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/lesson/index.php?id='.$rec->new_id,$result);
721                 } else { 
722                     //It's a foreign link so leave it as original
723                     $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/lesson/index.php?id='.$old_id,$result);
724                 }
725             }
726         }
728         //Link to lesson view by moduleid
730         $searchstring='/\$@(LESSONVIEWBYID)\*([0-9]+)@\$/';
731         //We look for it
732         preg_match_all($searchstring,$result,$foundset);
733         //If found, then we are going to look for its new id (in backup tables)
734         if ($foundset[0]) {
735             //print_object($foundset);                                     //Debug
736             //Iterate over foundset[2]. They are the old_ids
737             foreach($foundset[2] as $old_id) {
738                 //We get the needed variables here (course_modules id)
739                 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
740                 //Personalize the searchstring
741                 $searchstring='/\$@(LESSONVIEWBYID)\*('.$old_id.')@\$/';
742                 //If it is a link to this course, update the link to its new location
743                 if($rec->new_id) {
744                     //Now replace it
745                     $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/lesson/view.php?id='.$rec->new_id,$result);
746                 } else {
747                     //It's a foreign link so leave it as original
748                     $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/lesson/view.php?id='.$old_id,$result);
749                 }
750             }
751         }
753         return $result;
754     }
756     //This function makes all the necessary calls to xxxx_decode_content_links()
757     //function in each module, passing them the desired contents to be decoded
758     //from backup format to destination site/course in order to mantain inter-activities
759     //working in the backup/restore process. It's called from restore_decode_content_links()
760     //function in restore process
761     function lesson_decode_content_links_caller($restore) {
762         global $CFG, $DB;
763         $status = true;
764         
765         //Process every lesson PAGE in the course
766         if ($pages = $DB->get_records_sql("SELECT p.id, p.contents
767                                              FROM {lesson_pages} p,
768                                                   {lesson} l
769                                             WHERE l.course = ? AND
770                                                   p.lessonid = l.id", array($restore->course_id))) {
771             //Iterate over each page->message
772             $i = 0;   //Counter to send some output to the browser to avoid timeouts
773             foreach ($pages as $page) {
774                 //Increment counter
775                 $i++;
776                 $content = $page->contents;
777                 $result = restore_decode_content_links_worker($content,$restore);
778                 if ($result != $content) {
779                     //Update record
780                     $page->contents = $result;
781                     $status = $DB->update_record("lesson_pages",$page);
782                     if (debugging()) {
783                         if (!defined('RESTORE_SILENTLY')) {
784                             echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
785                         }
786                     }
787                 }
788                 //Do some output
789                 if (($i+1) % 5 == 0) {
790                     if (!defined('RESTORE_SILENTLY')) {
791                         echo ".";
792                         if (($i+1) % 100 == 0) {
793                             echo "<br />";
794                         }
795                     }
796                     backup_flush(300);
797                 }
798             }
799         }
801         // Remap activity links
802         if ($lessons = $DB->get_records_select('lesson', "activitylink <> 0 AND course = ?", array($restore->course_id), '', 'id, activitylink')) {
803             foreach ($lessons as $lesson) {
804                 if ($newcmid = backup_getid($restore->backup_unique_code, 'course_modules', $lesson->activitylink)) {
805                     $status = $status and $DB->set_field('lesson', 'activitylink', $newcmid->new_id, array('id'=>$lesson->id));
806                 }
807             }
808         }
810         return $status;
811     }
813     //This function returns a log record with all the necessay transformations
814     //done. It's used by restore_log_module() to restore modules log.
815     function lesson_restore_logs($restore,$log) {
817         $status = false;
819         //Depending of the action, we recode different things
820         switch ($log->action) {
821         case "add":
822             if ($log->cmid) {
823                 //Get the new_id of the module (to recode the info field)
824                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
825                 if ($mod) {
826                     $log->url = "view.php?id=".$log->cmid;
827                     $log->info = $mod->new_id;
828                     $status = true;
829                 }
830             }
831             break;
832         case "update":
833             if ($log->cmid) {
834                 //Get the new_id of the module (to recode the info field)
835                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
836                 if ($mod) {
837                     $log->url = "view.php?id=".$log->cmid;
838                     $log->info = $mod->new_id;
839                     $status = true;
840                 }
841             }
842             break;
843         case "view all":
844             $log->url = "index.php?id=".$log->course;
845             $status = true;
846             break;
847         case "start":
848             if ($log->cmid) {
849                 //Get the new_id of the module (to recode the info field)
850                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
851                 if ($mod) {
852                     $log->url = "view.php?id=".$log->cmid;
853                     $log->info = $mod->new_id;
854                     $status = true;
855                 }
856             }
857             break;
858         case "end":
859             if ($log->cmid) {
860                 //Get the new_id of the module (to recode the info field)
861                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
862                 if ($mod) {
863                     $log->url = "view.php?id=".$log->cmid;
864                     $log->info = $mod->new_id;
865                     $status = true;
866                 }
867             }
868             break;
869         case "view":
870             if ($log->cmid) {
871                 //Get the new_id of the page (to recode the url field)
872                 $pag = backup_getid($restore->backup_unique_code,"lesson_pages",$log->info);
873                 if ($pag) {
874                     $log->url = "view.php?id=".$log->cmid."&action=navigation&pageid=".$pag->new_id;
875                     $log->info = $pag->new_id;
876                     $status = true;
877                 }
878             }
879             break;
880         default:
881             if (!defined('RESTORE_SILENTLY')) {
882                 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br/>";                 //Debug
883             }
884             break;
885         }
887         if ($status) {
888             $status = $log;
889         }
890         return $status;
891     }
892 ?>