c71e3902e31fe20950772feb7967e5bfc9b897a7
[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                 if (!$DB->set_field("lesson_pages", "nextpageid", $newid, array("id"=>$prevpageid))) {
185                     print_error('cannotupdatepage', 'lesson');
186                 }
187             }
188             $prevpageid = $newid;
190             //Do some output
191             if (($i+1) % 10 == 0) {
192                 if (!defined('RESTORE_SILENTLY')) {
193                     echo ".";
194                     if (($i+1) % 200 == 0) {
195                         echo "<br/>";
196                     }
197                 }
198                 backup_flush(300);
199             }
201             if ($newid) {
202                 //We have the newid, update backup_ids (restore logs will use it!!)
203                 backup_putid($restore->backup_unique_code,"lesson_pages", $oldid, $newid);
204                 //We have to restore the lesson_answers table now (a page level table)
205                 $status = lesson_answers_restore($lessonid,$newid,$page_info,$restore,$userdata);
206                 
207                 //Need to update useranswer field (which has answer id's in it)
208                 //for matching and multi-answer multi-choice questions
209                 if ($userdata) { // first check to see if we even have to do this
210                     // if multi-answer multi-choice question or matching
211                     if (($page->qtype == 3 && $page->qoption) ||
212                          $page->qtype == 5) {
213                         // get all the attempt records for this page
214                         if ($attempts = $DB->get_records("lesson_attempts", array("pageid"=>$newid))) {
215                             foreach ($attempts as $attempt) {
216                                 unset($newuseranswer);
217                                 if ($attempt->useranswer != NULL) {
218                                     // explode the user answer.  Each element in
219                                     // $useranswer is an old answer id, so needs to be updated
220                                     $useranswer = explode(",", $attempt->useranswer);
221                                     foreach ($useranswer as $oldanswerid) {
222                                          $backupdata = backup_getid($restore->backup_unique_code,"lesson_answers",$oldanswerid);
223                                          $newuseranswer[] = $backupdata->new_id;
224                                     }
225                                     // get the useranswer in the right format
226                                     $attempt->useranswer = implode(",", $newuseranswer);
227                                     // update it
228                                     $DB->update_record("lesson_attempts", $attempt);
229                                 }
230                             }
231                         }
232                     }
233                 }        
234                 
235                 // backup branch table info for branch tables.
236                 if ($status && $userdata) {
237                     if (!lesson_branch_restore($lessonid,$newid,$page_info,$restore)) {
238                         return false;
239                     }
240                 }
241             } else {
242                 $status = false;
243             }
244         }
246         //We've restored all the pages and answers, we now need to fix the jumps in the
247         //answer records if they are absolute
248         if ($answers = $DB->get_records("lesson_answers", array("lessonid"=>$lessonid))) {
249             foreach ($answers as $answer) {
250                 if ($answer->jumpto > 0) {
251                     // change the absolute page id
252                     $page = backup_getid($restore->backup_unique_code,"lesson_pages",$answer->jumpto);
253                     if ($page) {
254                         if (!$DB->set_field("lesson_answers", "jumpto", $page->new_id, array("id"=>$answer->id))) {
255                             print_error('cannotresetjump', 'lesson');
256                         }
257                     }
258                 }
259             }
260         }
261         return $status;
262     }
265     //This function restores the lesson_answers
266     function lesson_answers_restore($lessonid,$pageid,$info,$restore,$userdata=false) {
267         global $CFG, $DB;
269         $status = true;
271         //Get the lesson_answers array (optional)
272         if (isset($info['#']['ANSWERS']['0']['#']['ANSWER'])) {
273             // The following chunk of code is a fix for matching questions made
274             // pre moodle 1.5.  Matching questions need two answer fields designated
275             // for correct and wrong responses before the rest of the answer fields.
276             if ($restore->backup_version <= 2004083124) {  // Backup version for 1.4.5+
277                 if ($ismatching = $DB->get_record('lesson_pages', array('id'=>$pageid))) {  // get the page we just inserted
278                     if ($ismatching->qtype == 5) { // check to make sure it is a matching question
279                         $time = time();  // this may need to be changed
280                         // make our 2 response answers
281                         $newanswer->lessonid = $lessonid;
282                         $newanswer->pageid = $pageid;
283                         $newanswer->timecreated = $time;
284                         $newanswer->timemodified = 0;
285                         $DB->insert_record('lesson_answers', $newanswer);
286                         $DB->insert_record('lesson_answers', $newanswer); // TODO: why is this here twice?
287                     }
288                 }
289             }
291             $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
293             //Iterate over lesson_answers
294             for($i = 0; $i < sizeof($answers); $i++) {
295                 $answer_info = $answers[$i];
296                 //traverse_xmlize($rub_info);                                  //Debug
297                 //print_object ($GLOBALS['traverse_array']);                   //Debug
298                 //$GLOBALS['traverse_array']="";                               //Debug
300                 //We'll need this later!!
301                 $oldid = backup_todb($answer_info['#']['ID']['0']['#']);
303                 //Now, build the lesson_answers record structure
304                 $answer->lessonid = $lessonid;
305                 $answer->pageid = $pageid;
306                 // the absolute jumps will need fixing later
307                 $answer->jumpto = backup_todb($answer_info['#']['JUMPTO']['0']['#']);
308                 $answer->grade = backup_todb($answer_info['#']['GRADE']['0']['#']);
309                 $answer->score = backup_todb($answer_info['#']['SCORE']['0']['#']);
310                 $answer->flags = backup_todb($answer_info['#']['FLAGS']['0']['#']);
311                 $answer->timecreated = backup_todb($answer_info['#']['TIMECREATED']['0']['#']);
312                 $answer->timemodified = backup_todb($answer_info['#']['TIMEMODIFIED']['0']['#']);
313                 $answer->answer = backup_todb($answer_info['#']['ANSWERTEXT']['0']['#']);
314                 $answer->response = backup_todb($answer_info['#']['RESPONSE']['0']['#']);
316                 //The structure is equal to the db, so insert the lesson_answers
317                 $newid = $DB->insert_record ("lesson_answers",$answer);
319                 //Do some output
320                 if (($i+1) % 10 == 0) {
321                     if (!defined('RESTORE_SILENTLY')) {
322                         echo ".";
323                         if (($i+1) % 200 == 0) {
324                             echo "<br/>";
325                         }
326                     }
327                     backup_flush(300);
328                 }
330                 if ($newid) {
331                     // need to store the id so we can update the useranswer
332                     // field in attempts.  This is done in the lesson_pages_restore_mods
333                     backup_putid($restore->backup_unique_code,"lesson_answers", $oldid, $newid);                                 
335                     if ($userdata) {
336                         //We have to restore the lesson_attempts table now (a answers level table)
337                         $status = lesson_attempts_restore($lessonid, $pageid, $newid, $answer_info, $restore);
338                     }
339                 } else {
340                     $status = false;
341                 }
342             }
343         }
344         return $status;
345     }
348     //This function restores the attempts
349     function lesson_attempts_restore($lessonid, $pageid, $answerid, $info, $restore) {
350         global $CFG, $DB;
352         $status = true;
354         //Get the attempts array (optional)
355         if (isset($info['#']['ATTEMPTS']['0']['#']['ATTEMPT'])) {
356             $attempts = $info['#']['ATTEMPTS']['0']['#']['ATTEMPT'];
357             //Iterate over attempts
358             for($i = 0; $i < sizeof($attempts); $i++) {
359                 $attempt_info = $attempts[$i];
360                 //traverse_xmlize($sub_info);                                                         //Debug
361                 //print_object ($GLOBALS['traverse_array']);                                          //Debug
362                 //$GLOBALS['traverse_array']="";                                                      //Debug
364                 //We'll need this later!!
365                 $olduserid = backup_todb($attempt_info['#']['USERID']['0']['#']);
367                 //Now, build the lesson_attempts record structure
368                 $attempt->lessonid = $lessonid;
369                 $attempt->pageid = $pageid;
370                 $attempt->answerid = $answerid;
371                 $attempt->userid = backup_todb($attempt_info['#']['USERID']['0']['#']);
372                 $attempt->retry = backup_todb($attempt_info['#']['RETRY']['0']['#']);
373                 $attempt->correct = backup_todb($attempt_info['#']['CORRECT']['0']['#']);
374                 $attempt->useranswer = backup_todb($attempt_info['#']['USERANSWER']['0']['#']);
375                 $attempt->timeseen = backup_todb($attempt_info['#']['TIMESEEN']['0']['#']);
377                 //We have to recode the userid field
378                 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
379                 if ($user) {
380                     $attempt->userid = $user->new_id;
381                 }
383                 //The structure is equal to the db, so insert the lesson_attempt
384                 $newid = $DB->insert_record ("lesson_attempts",$attempt);
386                 //Do some output
387                 if (($i+1) % 50 == 0) {
388                     if (!defined('RESTORE_SILENTLY')) {
389                         echo ".";
390                         if (($i+1) % 1000 == 0) {
391                             echo "<br/>";
392                         }
393                     }
394                     backup_flush(300);
395                 }
396             }
397         }
399     return $status;
400     }
402     //This function restores the lesson_grades
403     function lesson_grades_restore_mods($lessonid, $info, $restore) {
404         global $CFG, $DB;
406         $status = true;
408         //Get the grades array (optional)
409         if (isset($info['MOD']['#']['GRADES']['0']['#']['GRADE'])) {
410             $grades = $info['MOD']['#']['GRADES']['0']['#']['GRADE'];
412             //Iterate over grades
413             for($i = 0; $i < sizeof($grades); $i++) {
414                 $grade_info = $grades[$i];
415                 //traverse_xmlize($grade_info);                         //Debug
416                 //print_object ($GLOBALS['traverse_array']);            //Debug
417                 //$GLOBALS['traverse_array']="";                        //Debug
419                 //We'll need this later!!
420                 $olduserid = backup_todb($grade_info['#']['USERID']['0']['#']);
422                 //Now, build the lesson_GRADES record structure
423                 $grade->lessonid = $lessonid;
424                 $grade->userid = backup_todb($grade_info['#']['USERID']['0']['#']);
425                 $grade->grade = backup_todb($grade_info['#']['GRADE_VALUE']['0']['#']);
426                 $grade->late = backup_todb($grade_info['#']['LATE']['0']['#']);
427                 $grade->completed = backup_todb($grade_info['#']['COMPLETED']['0']['#']);
429                 //We have to recode the userid field
430                 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
431                 if ($user) {
432                     $grade->userid = $user->new_id;
433                 }
435                 //The structure is equal to the db, so insert the lesson_grade
436                 $newid = $DB->insert_record ("lesson_grades",$grade);
438                 //Do some output
439                 if (($i+1) % 50 == 0) {
440                     if (!defined('RESTORE_SILENTLY')) {
441                         echo ".";
442                         if (($i+1) % 1000 == 0) {
443                             echo "<br/>";
444                         }
445                     }
446                     backup_flush(300);
447                 }
449                 if (!$newid) {
450                     $status = false;
451                 }
452             }
453         }
455         return $status;
456     }
457     
458     
459     
460     //This function restores the lesson_branch
461     function lesson_branch_restore($lessonid, $pageid, $info, $restore) {
462         global $CFG, $DB;
464         $status = true;
466         //Get the branch array (optional)
467         if (isset($info['#']['BRANCHES']['0']['#']['BRANCH'])) {
468             $branches = $info['#']['BRANCHES']['0']['#']['BRANCH'];
469             //Iterate over branches
470             for($i = 0; $i < sizeof($branches); $i++) {
471                 $branch_info = $branches[$i];
472                 //traverse_xmlize($branch_info);                                                         //Debug
473                 //print_object ($GLOBALS['traverse_array']);                                          //Debug
474                 //$GLOBALS['traverse_array']="";                                                      //Debug
476                 //We'll need this later!!
477                 $olduserid = backup_todb($branch_info['#']['USERID']['0']['#']);
479                 //Now, build the lesson_attempts record structure
480                 $branch->lessonid = $lessonid;
481                 $branch->userid = backup_todb($branch_info['#']['USERID']['0']['#']);
482                 $branch->pageid = $pageid;
483                 $branch->retry = backup_todb($branch_info['#']['RETRY']['0']['#']);
484                 $branch->flag = backup_todb($branch_info['#']['FLAG']['0']['#']);
485                 $branch->timeseen = backup_todb($branch_info['#']['TIMESEEN']['0']['#']);
487                 //We have to recode the userid field
488                 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
489                 if ($user) {
490                     $branch->userid = $user->new_id;
491                 }
493                 //The structure is equal to the db, so insert the lesson_attempt
494                 $newid = $DB->insert_record ("lesson_branch",$branch);
496                 //Do some output
497                 if (($i+1) % 50 == 0) {
498                     if (!defined('RESTORE_SILENTLY')) {
499                         echo ".";
500                         if (($i+1) % 1000 == 0) {
501                             echo "<br/>";
502                         }
503                     }
504                     backup_flush(300);
505                 }
506             }
507         }
509     return $status;
510     }
512     //This function restores the lesson_timer
513     function lesson_timer_restore_mods($lessonid, $info, $restore) {
514         global $CFG, $DB;
516         $status = true;
517         //Get the timer array (optional)
518         if (isset($info['MOD']['#']['TIMES']['0']['#']['TIME'])) {
519             $times = $info['MOD']['#']['TIMES']['0']['#']['TIME'];
520             //Iterate over times
521             for($i = 0; $i < sizeof($times); $i++) {
522                 $time_info = $times[$i];
523                 //traverse_xmlize($time_info);                         //Debug
524                 //print_object ($GLOBALS['traverse_array']);            //Debug
525                 //$GLOBALS['traverse_array']="";                        //Debug
527                 //We'll need this later!!
528                 $olduserid = backup_todb($time_info['#']['USERID']['0']['#']);
530                 //Now, build the lesson_time record structure
531                 $time->lessonid = $lessonid;
532                 $time->userid = backup_todb($time_info['#']['USERID']['0']['#']);
533                 $time->starttime = backup_todb($time_info['#']['STARTTIME']['0']['#']);
534                 $time->lessontime = backup_todb($time_info['#']['LESSONTIME']['0']['#']);
536                 //We have to recode the userid field
537                 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
538                 if ($user) {
539                     $time->userid = $user->new_id;
540                 }
542                 //The structure is equal to the db, so insert the lesson_grade
543                 $newid = $DB->insert_record ("lesson_timer",$time);
545                 //Do some output
546                 if (($i+1) % 50 == 0) {
547                     if (!defined('RESTORE_SILENTLY')) {
548                         echo ".";
549                         if (($i+1) % 1000 == 0) {
550                             echo "<br/>";
551                         }
552                     }
553                     backup_flush(300);
554                 }
556                 if (!$newid) {
557                     $status = false;
558                 }
559             }
560         }
562         return $status;
563     }
565     //This function restores the lesson_high_scores
566     function lesson_high_scores_restore_mods($lessonid, $info, $restore) {
567         global $CFG, $DB;
569         $status = true;
571         //Get the highscores array (optional)
572         if (isset($info['MOD']['#']['HIGHSCORES']['0']['#']['HIGHSCORE'])) {
573             $highscores = $info['MOD']['#']['HIGHSCORES']['0']['#']['HIGHSCORE'];
574             //Iterate over highscores
575             for($i = 0; $i < sizeof($highscores); $i++) {
576                 $highscore_info = $highscores[$i];
577                 //traverse_xmlize($highscore_info);                     //Debug
578                 //print_object ($GLOBALS['traverse_array']);            //Debug
579                 //$GLOBALS['traverse_array']="";                        //Debug
581                 //We'll need this later!!
582                 $olduserid = backup_todb($highscore_info['#']['USERID']['0']['#']);
584                 //Now, build the lesson_highscores record structure
585                 $highscore->lessonid = $lessonid;
586                 $highscore->userid = backup_todb($highscore_info['#']['USERID']['0']['#']);
587                 $highscore->gradeid = backup_todb($highscore_info['#']['GRADEID']['0']['#']);
588                 $highscore->nickname = backup_todb($highscore_info['#']['NICKNAME']['0']['#']);
590                 //We have to recode the userid field
591                 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
592                 if ($user) {
593                     $highscore->userid = $user->new_id;
594                 }
595                 
596                 //The structure is equal to the db, so insert the lesson_grade
597                 $newid = $DB->insert_record ("lesson_high_scores",$highscore);
599                 //Do some output
600                 if (($i+1) % 50 == 0) {
601                     if (!defined('RESTORE_SILENTLY')) {
602                         echo ".";
603                         if (($i+1) % 1000 == 0) {
604                             echo "<br/>";
605                         }
606                     }
607                     backup_flush(300);
608                 }
610                 if (!$newid) {
611                     $status = false;
612                 }
613             }
614         }
616         return $status;
617     }
618     
619     //This function restores the lesson_default
620     function lesson_default_restore_mods($info, $restore) {
621         global $CFG, $DB;
623         $status = true;
625         //Get the default array (optional)
626         if (isset($info['MOD']['#']['DEFAULTS'])) {
627             $defaults = $info['MOD']['#']['DEFAULTS'];
629             //Iterate over defaults (should only be 1!)
630             for($i = 0; $i < sizeof($defaults); $i++) {
631                 $default_info = $defaults[$i];
632                 //traverse_xmlize($default_info);                       //Debug
633                 //print_object ($GLOBALS['traverse_array']);            //Debug
634                 //$GLOBALS['traverse_array']="";                        //Debug
636                 //Now, build the lesson_default record structure
637                 $default->course = $restore->course_id;
638                 $default->practice = backup_todb($default_info['#']['PRACTICE']['0']['#']);
639                 $default->modattempts = backup_todb($default_info['#']['MODATTEMPTS']['0']['#']);
640                 $default->usepassword = backup_todb($default_info['#']['USEPASSWORD']['0']['#']);
641                 $default->password = backup_todb($default_info['#']['PASSWORD']['0']['#']);
642                 $default->conditions = backup_todb($default_info['#']['CONDITIONS']['0']['#']);
643                 $default->grade = backup_todb($default_info['#']['GRADE']['0']['#']);
644                 $default->custom = backup_todb($default_info['#']['CUSTOM']['0']['#']);
645                 $default->ongoing = backup_todb($default_info['#']['ONGOING']['0']['#']);
646                 $default->usemaxgrade = backup_todb($default_info['#']['USEMAXGRADE']['0']['#']);
647                 $default->maxanswers = backup_todb($default_info['#']['MAXANSWERS']['0']['#']);
648                 $default->maxattempts = backup_todb($default_info['#']['MAXATTEMPTS']['0']['#']);
649                 $default->review = backup_todb($default_info['#']['REVIEW']['0']['#']);
650                 $default->nextpagedefault = backup_todb($default_info['#']['NEXTPAGEDEFAULT']['0']['#']);
651                 $default->feedback = backup_todb($default_info['#']['FEEDBACK']['0']['#']);
652                 $default->minquestions = backup_todb($default_info['#']['MINQUESTIONS']['0']['#']);
653                 $default->maxpages = backup_todb($default_info['#']['MAXPAGES']['0']['#']);
654                 $default->timed = backup_todb($default_info['#']['TIMED']['0']['#']);
655                 $default->maxtime = backup_todb($default_info['#']['MAXTIME']['0']['#']);
656                 $default->retake = backup_todb($default_info['#']['RETAKE']['0']['#']);
657                 $default->mediaheight = backup_todb($default_info['#']['MEDIAHEIGHT']['0']['#']);
658                 $default->mediawidth = backup_todb($default_info['#']['MEDIAWIDTH']['0']['#']);
659                 $default->mediaclose = backup_todb($default_info['#']['MEDIACLOSE']['0']['#']);
660                 $default->slideshow = backup_todb($default_info['#']['SLIDESHOW']['0']['#']);
661                 $default->width = backup_todb($default_info['#']['WIDTH']['0']['#']);
662                 $default->height = backup_todb($default_info['#']['HEIGHT']['0']['#']);
663                 $default->bgcolor = backup_todb($default_info['#']['BGCOLOR']['0']['#']);
664                 $default->displayleft = backup_todb($default_info['#']['DISPLAYLEFT']['0']['#']);
665                 $default->displayleftif = backup_todb($default_info['#']['DISPLAYLEFTIF']['0']['#']);
666                 $default->progressbar = backup_todb($default_info['#']['PROGRESSBAR']['0']['#']);
667                 $default->highscores = backup_todb($default_info['#']['HIGHSCORES']['0']['#']);
668                 $default->maxhighscores = backup_todb($default_info['#']['MAXHIGHSCORES']['0']['#']);
670                 //The structure is equal to the db, so insert the lesson_grade
671                 $newid = $DB->insert_record ("lesson_default",$default);
672                 
673                 if ($newid) {
674                     backup_putid($restore->backup_unique_code,'lesson_default',
675                                  $restore->course_id, $newid);
676                 }
677                 
678                 //Do some output
679                 if (($i+1) % 50 == 0) {
680                     if (!defined('RESTORE_SILENTLY')) {
681                         echo ".";
682                         if (($i+1) % 1000 == 0) {
683                             echo "<br/>";
684                         }
685                     }
686                     backup_flush(300);
687                 }
689                 if (!$newid) {
690                     $status = false;
691                 }
692             }
693         }
695         return $status;
696     }
698     //Return a content decoded to support interactivities linking. Every module
699     //should have its own. They are called automatically from
700     //lesson_decode_content_links_caller() function in each module
701     //in the restore process
702     function lesson_decode_content_links ($content,$restore) {
703         global $CFG;
704             
705         $result = $content;
706                 
707         //Link to the list of lessons
708                 
709         $searchstring='/\$@(LESSONINDEX)\*([0-9]+)@\$/';
710         //We look for it
711         preg_match_all($searchstring,$content,$foundset);
712         //If found, then we are going to look for its new id (in backup tables)
713         if ($foundset[0]) {
714             //print_object($foundset);                                     //Debug
715             //Iterate over foundset[2]. They are the old_ids
716             foreach($foundset[2] as $old_id) {
717                 //We get the needed variables here (course id)
718                 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
719                 //Personalize the searchstring
720                 $searchstring='/\$@(LESSONINDEX)\*('.$old_id.')@\$/';
721                 //If it is a link to this course, update the link to its new location
722                 if($rec->new_id) {
723                     //Now replace it
724                     $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/lesson/index.php?id='.$rec->new_id,$result);
725                 } else { 
726                     //It's a foreign link so leave it as original
727                     $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/lesson/index.php?id='.$old_id,$result);
728                 }
729             }
730         }
732         //Link to lesson view by moduleid
734         $searchstring='/\$@(LESSONVIEWBYID)\*([0-9]+)@\$/';
735         //We look for it
736         preg_match_all($searchstring,$result,$foundset);
737         //If found, then we are going to look for its new id (in backup tables)
738         if ($foundset[0]) {
739             //print_object($foundset);                                     //Debug
740             //Iterate over foundset[2]. They are the old_ids
741             foreach($foundset[2] as $old_id) {
742                 //We get the needed variables here (course_modules id)
743                 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
744                 //Personalize the searchstring
745                 $searchstring='/\$@(LESSONVIEWBYID)\*('.$old_id.')@\$/';
746                 //If it is a link to this course, update the link to its new location
747                 if($rec->new_id) {
748                     //Now replace it
749                     $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/lesson/view.php?id='.$rec->new_id,$result);
750                 } else {
751                     //It's a foreign link so leave it as original
752                     $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/lesson/view.php?id='.$old_id,$result);
753                 }
754             }
755         }
757         return $result;
758     }
760     //This function makes all the necessary calls to xxxx_decode_content_links()
761     //function in each module, passing them the desired contents to be decoded
762     //from backup format to destination site/course in order to mantain inter-activities
763     //working in the backup/restore process. It's called from restore_decode_content_links()
764     //function in restore process
765     function lesson_decode_content_links_caller($restore) {
766         global $CFG, $DB;
767         $status = true;
768         
769         //Process every lesson PAGE in the course
770         if ($pages = $DB->get_records_sql("SELECT p.id, p.contents
771                                              FROM {lesson_pages} p,
772                                                   {lesson} l
773                                             WHERE l.course = ? AND
774                                                   p.lessonid = l.id", array($restore->course_id))) {
775             //Iterate over each page->message
776             $i = 0;   //Counter to send some output to the browser to avoid timeouts
777             foreach ($pages as $page) {
778                 //Increment counter
779                 $i++;
780                 $content = $page->contents;
781                 $result = restore_decode_content_links_worker($content,$restore);
782                 if ($result != $content) {
783                     //Update record
784                     $page->contents = $result;
785                     $status = $DB->update_record("lesson_pages",$page);
786                     if (debugging()) {
787                         if (!defined('RESTORE_SILENTLY')) {
788                             echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
789                         }
790                     }
791                 }
792                 //Do some output
793                 if (($i+1) % 5 == 0) {
794                     if (!defined('RESTORE_SILENTLY')) {
795                         echo ".";
796                         if (($i+1) % 100 == 0) {
797                             echo "<br />";
798                         }
799                     }
800                     backup_flush(300);
801                 }
802             }
803         }
805         // Remap activity links
806         if ($lessons = $DB->get_records_select('lesson', "activitylink <> 0 AND course = ?", array($restore->course_id), '', 'id, activitylink')) {
807             foreach ($lessons as $lesson) {
808                 if ($newcmid = backup_getid($restore->backup_unique_code, 'course_modules', $lesson->activitylink)) {
809                     $status = $status and $DB->set_field('lesson', 'activitylink', $newcmid->new_id, array('id'=>$lesson->id));
810                 }
811             }
812         }
814         return $status;
815     }
817     //This function returns a log record with all the necessay transformations
818     //done. It's used by restore_log_module() to restore modules log.
819     function lesson_restore_logs($restore,$log) {
821         $status = false;
823         //Depending of the action, we recode different things
824         switch ($log->action) {
825         case "add":
826             if ($log->cmid) {
827                 //Get the new_id of the module (to recode the info field)
828                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
829                 if ($mod) {
830                     $log->url = "view.php?id=".$log->cmid;
831                     $log->info = $mod->new_id;
832                     $status = true;
833                 }
834             }
835             break;
836         case "update":
837             if ($log->cmid) {
838                 //Get the new_id of the module (to recode the info field)
839                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
840                 if ($mod) {
841                     $log->url = "view.php?id=".$log->cmid;
842                     $log->info = $mod->new_id;
843                     $status = true;
844                 }
845             }
846             break;
847         case "view all":
848             $log->url = "index.php?id=".$log->course;
849             $status = true;
850             break;
851         case "start":
852             if ($log->cmid) {
853                 //Get the new_id of the module (to recode the info field)
854                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
855                 if ($mod) {
856                     $log->url = "view.php?id=".$log->cmid;
857                     $log->info = $mod->new_id;
858                     $status = true;
859                 }
860             }
861             break;
862         case "end":
863             if ($log->cmid) {
864                 //Get the new_id of the module (to recode the info field)
865                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
866                 if ($mod) {
867                     $log->url = "view.php?id=".$log->cmid;
868                     $log->info = $mod->new_id;
869                     $status = true;
870                 }
871             }
872             break;
873         case "view":
874             if ($log->cmid) {
875                 //Get the new_id of the page (to recode the url field)
876                 $pag = backup_getid($restore->backup_unique_code,"lesson_pages",$log->info);
877                 if ($pag) {
878                     $log->url = "view.php?id=".$log->cmid."&action=navigation&pageid=".$pag->new_id;
879                     $log->info = $pag->new_id;
880                     $status = true;
881                 }
882             }
883             break;
884         default:
885             if (!defined('RESTORE_SILENTLY')) {
886                 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br/>";                 //Debug
887             }
888             break;
889         }
891         if ($status) {
892             $status = $log;
893         }
894         return $status;
895     }
896 ?>