cfb660006e31cef1318a431e9231c16a277634b8
[moodle.git] / mod / lesson / restorelib.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * This php script contains all the stuff to restore lesson mods
20  *
21  * @package    mod
22  * @subpackage lesson
23  * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or late
25  **/
27     //This is the "graphical" structure of the lesson mod:
28     //
29     //           lesson ----------------------------|--------------------------|--------------------------|
30     //        (CL,pk->id)                           |                          |                          |
31     //              |                               |                          |                          |
32     //              |                         lesson_grades              lesson_high_scores         lesson_timer
33     //              |                  (UL, pk->id,fk->lessonid)    (UL, pk->id,fk->lessonid)   (UL, pk->id,fk->lessonid)
34     //              |
35     //              |
36     //       lesson_pages---------------------------|
37     //   (CL,pk->id,fk->lessonid)                   |
38     //              |                               |
39     //              |                         lesson_branch
40     //              |                   (UL, pk->id,fk->pageid)
41     //        lesson_answers
42     //     (CL,pk->id,fk->pageid)
43     //              |
44     //              |
45     //              |
46     //        lesson_attempts
47     //   (UL,pk->id,fk->answerid)
48     //
49     // Meaning: pk->primary key field of the table
50     //          fk->foreign key to link with parent
51     //          nt->nested field (recursive data)
52     //          CL->course level info
53     //          UL->user level info
54     //          files->table may have files)
55     //
56     //-----------------------------------------------------------
58 defined('MOODLE_INTERNAL') || die();
60     //This function executes all the restore procedure about this mod
61     function lesson_restore_mods($mod,$restore) {
62         global $CFG, $DB;
64         $status = true;
66         //Get record from backup_ids
67         $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
69         if ($data) {
70             //Now get completed xmlized object
71             $info = $data->info;
72             //if necessary, write to restorelog and adjust date/time fields
73             if ($restore->course_startdateoffset) {
74                 restore_log_date_changes('Lesson', $restore, $info['MOD']['#'], array('AVAILABLE', 'DEADLINE'));
75             }
76             //traverse_xmlize($info);                                                              //Debug
77             //print_object ($GLOBALS['traverse_array']);                                           //Debug
78             //$GLOBALS['traverse_array']="";                                                       //Debug
80             //Now, build the lesson record structure
81             $lesson->course = $restore->course_id;
82             $lesson->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
83             $lesson->practice = backup_todb($info['MOD']['#']['PRACTICE']['0']['#']);
84             $lesson->modattempts = backup_todb($info['MOD']['#']['MODATTEMPTS']['0']['#']);
85             $lesson->usepassword = backup_todb($info['MOD']['#']['USEPASSWORD']['0']['#']);
86             $lesson->password = backup_todb($info['MOD']['#']['PASSWORD']['0']['#']);
87             $lesson->dependency = isset($info['MOD']['#']['DEPENDENCY']['0']['#'])?backup_todb($info['MOD']['#']['DEPENDENCY']['0']['#']):'';
88             $lesson->conditions = isset($info['MOD']['#']['CONDITIONS']['0']['#'])?backup_todb($info['MOD']['#']['CONDITIONS']['0']['#']):'';
89             $lesson->grade = backup_todb($info['MOD']['#']['GRADE']['0']['#']);
90             $lesson->custom = backup_todb($info['MOD']['#']['CUSTOM']['0']['#']);
91             $lesson->ongoing = backup_todb($info['MOD']['#']['ONGOING']['0']['#']);
92             $lesson->usemaxgrade = backup_todb($info['MOD']['#']['USEMAXGRADE']['0']['#']);
93             $lesson->maxanswers = backup_todb($info['MOD']['#']['MAXANSWERS']['0']['#']);
94             $lesson->maxattempts = backup_todb($info['MOD']['#']['MAXATTEMPTS']['0']['#']);
95             $lesson->review = backup_todb($info['MOD']['#']['REVIEW']['0']['#']);
96             $lesson->nextpagedefault = backup_todb($info['MOD']['#']['NEXTPAGEDEFAULT']['0']['#']);
97             $lesson->feedback = isset($info['MOD']['#']['FEEDBACK']['0']['#'])?backup_todb($info['MOD']['#']['FEEDBACK']['0']['#']):'';
98             $lesson->minquestions = backup_todb($info['MOD']['#']['MINQUESTIONS']['0']['#']);
99             $lesson->maxpages = backup_todb($info['MOD']['#']['MAXPAGES']['0']['#']);
100             $lesson->timed = backup_todb($info['MOD']['#']['TIMED']['0']['#']);
101             $lesson->maxtime = backup_todb($info['MOD']['#']['MAXTIME']['0']['#']);
102             $lesson->retake = backup_todb($info['MOD']['#']['RETAKE']['0']['#']);
103             $lesson->activitylink = isset($info['MOD']['#']['ACTIVITYLINK']['0']['#'])?backup_todb($info['MOD']['#']['ACTIVITYLINK']['0']['#']):'';
104             $lesson->mediafile = isset($info['MOD']['#']['MEDIAFILE']['0']['#'])?backup_todb($info['MOD']['#']['MEDIAFILE']['0']['#']):'';
105             $lesson->mediaheight = isset($info['MOD']['#']['MEDIAHEIGHT']['0']['#'])?backup_todb($info['MOD']['#']['MEDIAHEIGHT']['0']['#']):'';
106             $lesson->mediawidth = isset($info['MOD']['#']['MEDIAWIDTH']['0']['#'])?backup_todb($info['MOD']['#']['MEDIAWIDTH']['0']['#']):'';
107             $lesson->mediaclose = isset($info['MOD']['#']['MEDIACLOSE']['0']['#'])?backup_todb($info['MOD']['#']['MEDIACLOSE']['0']['#']):'';
108             $lesson->slideshow = backup_todb($info['MOD']['#']['SLIDESHOW']['0']['#']);
109             $lesson->width = backup_todb($info['MOD']['#']['WIDTH']['0']['#']);
110             $lesson->height = backup_todb($info['MOD']['#']['HEIGHT']['0']['#']);
111             $lesson->bgcolor = backup_todb($info['MOD']['#']['BGCOLOR']['0']['#']);
112             $lesson->displayleft = isset($info['MOD']['#']['DISPLAYLEFT']['0']['#'])?backup_todb($info['MOD']['#']['DISPLAYLEFT']['0']['#']):'';
113             $lesson->displayleftif = isset($info['MOD']['#']['DISPLAYLEFTIF']['0']['#'])?backup_todb($info['MOD']['#']['DISPLAYLEFTIF']['0']['#']):'';
114             $lesson->progressbar = isset($info['MOD']['#']['PROGRESSBAR']['0']['#'])?backup_todb($info['MOD']['#']['PROGRESSBAR']['0']['#']):'';
115             $lesson->highscores = backup_todb($info['MOD']['#']['SHOWHIGHSCORES']['0']['#']);
116             $lesson->maxhighscores = backup_todb($info['MOD']['#']['MAXHIGHSCORES']['0']['#']);
117             $lesson->available = backup_todb($info['MOD']['#']['AVAILABLE']['0']['#']);
118             $lesson->deadline = backup_todb($info['MOD']['#']['DEADLINE']['0']['#']);
119             $lesson->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
121             //The structure is equal to the db, so insert the lesson
122             $newid = $DB->insert_record("lesson", $lesson);
124             //Do some output
125             if (!defined('RESTORE_SILENTLY')) {
126                 echo "<li>".get_string("modulename","lesson")." \"".format_string($lesson->name,true)."\"</li>";
127             }
128             backup_flush(300);
130             if ($newid) {
131                 //We have the newid, update backup_ids
132                 backup_putid($restore->backup_unique_code,$mod->modtype,
133                              $mod->id, $newid);
134                 //We have to restore the lesson pages which are held in their logical order...
135                 $userdata = restore_userdata_selected($restore,"lesson",$mod->id);
136                 $status = lesson_pages_restore_mods($newid,$info,$restore,$userdata);
137                 //...and the user grades, high scores, and timer (if required)
138                 if ($status) {
139                     if ($userdata) {
140                         if(!lesson_grades_restore_mods($newid,$info,$restore)) {
141                             return false;
142                         }
143                         if (!lesson_high_scores_restore_mods($newid,$info,$restore)) {
144                             return false;
145                         }
146                         if (!lesson_timer_restore_mods($newid,$info,$restore)) {
147                             return false;
148                         }
149                     }
150                 }
151             } else {
152                 $status = false;
153             }
154         } else {
155             $status = false;
156         }
157         return $status;
158     }
160     //This function restores the lesson_pages
161     function lesson_pages_restore_mods($lessonid,$info,$restore,$userdata=false) {
162         global $CFG, $DB;
164         $status = true;
166         //Get the lesson_elements array
167         $pages = $info['MOD']['#']['PAGES']['0']['#']['PAGE'];
169         //Iterate over lesson pages (they are held in their logical order)
170         $prevpageid = 0;
171         for($i = 0; $i < sizeof($pages); $i++) {
172             $page_info = $pages[$i];
173             //traverse_xmlize($ele_info);                                                          //Debug
174             //print_object ($GLOBALS['traverse_array']);                                           //Debug
175             //$GLOBALS['traverse_array']="";                                                       //Debug
177             //We'll need this later!!
178             $oldid = backup_todb($page_info['#']['PAGEID']['0']['#']);
180             //Now, build the lesson_pages record structure
181             $page->lessonid = $lessonid;
182             $page->prevpageid = $prevpageid;
183             $page->qtype = backup_todb($page_info['#']['QTYPE']['0']['#']);
184             $page->qoption = backup_todb($page_info['#']['QOPTION']['0']['#']);
185             $page->layout = backup_todb($page_info['#']['LAYOUT']['0']['#']);
186             $page->display = backup_todb($page_info['#']['DISPLAY']['0']['#']);
187             $page->timecreated = backup_todb($page_info['#']['TIMECREATED']['0']['#']);
188             $page->timemodified = backup_todb($page_info['#']['TIMEMODIFIED']['0']['#']);
189             $page->title = backup_todb($page_info['#']['TITLE']['0']['#']);
190             $page->contents = backup_todb($page_info['#']['CONTENTS']['0']['#']);
192             //The structure is equal to the db, so insert the lesson_pages
193             $newid = $DB->insert_record ("lesson_pages",$page);
195             //Fix the forwards link of the previous page
196             if ($prevpageid) {
197                 $DB->set_field("lesson_pages", "nextpageid", $newid, array("id"=>$prevpageid));
198             }
199             $prevpageid = $newid;
201             //Do some output
202             if (($i+1) % 10 == 0) {
203                 if (!defined('RESTORE_SILENTLY')) {
204                     echo ".";
205                     if (($i+1) % 200 == 0) {
206                         echo "<br/>";
207                     }
208                 }
209                 backup_flush(300);
210             }
212             if ($newid) {
213                 //We have the newid, update backup_ids (restore logs will use it!!)
214                 backup_putid($restore->backup_unique_code,"lesson_pages", $oldid, $newid);
215                 //We have to restore the lesson_answers table now (a page level table)
216                 $status = lesson_answers_restore($lessonid,$newid,$page_info,$restore,$userdata);
218                 //Need to update useranswer field (which has answer id's in it)
219                 //for matching and multi-answer multi-choice questions
220                 if ($userdata) { // first check to see if we even have to do this
221                     // if multi-answer multi-choice question or matching
222                     if (($page->qtype == 3 && $page->qoption) ||
223                          $page->qtype == 5) {
224                         // get all the attempt records for this page
225                         if ($attempts = $DB->get_records("lesson_attempts", array("pageid"=>$newid))) {
226                             foreach ($attempts as $attempt) {
227                                 unset($newuseranswer);
228                                 if ($attempt->useranswer != NULL) {
229                                     // explode the user answer.  Each element in
230                                     // $useranswer is an old answer id, so needs to be updated
231                                     $useranswer = explode(",", $attempt->useranswer);
232                                     foreach ($useranswer as $oldanswerid) {
233                                          $backupdata = backup_getid($restore->backup_unique_code,"lesson_answers",$oldanswerid);
234                                          $newuseranswer[] = $backupdata->new_id;
235                                     }
236                                     // get the useranswer in the right format
237                                     $attempt->useranswer = implode(",", $newuseranswer);
238                                     // update it
239                                     $DB->update_record("lesson_attempts", $attempt);
240                                 }
241                             }
242                         }
243                     }
244                 }
246                 // backup branch table info for branch tables.
247                 if ($status && $userdata) {
248                     if (!lesson_branch_restore($lessonid,$newid,$page_info,$restore)) {
249                         return false;
250                     }
251                 }
252             } else {
253                 $status = false;
254             }
255         }
257         //We've restored all the pages and answers, we now need to fix the jumps in the
258         //answer records if they are absolute
259         if ($answers = $DB->get_records("lesson_answers", array("lessonid"=>$lessonid))) {
260             foreach ($answers as $answer) {
261                 if ($answer->jumpto > 0) {
262                     // change the absolute page id
263                     $page = backup_getid($restore->backup_unique_code,"lesson_pages",$answer->jumpto);
264                     if ($page) {
265                         $DB->set_field("lesson_answers", "jumpto", $page->new_id, array("id"=>$answer->id));
266                     }
267                 }
268             }
269         }
270         return $status;
271     }
274     //This function restores the lesson_answers
275     function lesson_answers_restore($lessonid,$pageid,$info,$restore,$userdata=false) {
276         global $CFG, $DB;
278         $status = true;
280         //Get the lesson_answers array (optional)
281         if (isset($info['#']['ANSWERS']['0']['#']['ANSWER'])) {
282             // The following chunk of code is a fix for matching questions made
283             // pre moodle 1.5.  Matching questions need two answer fields designated
284             // for correct and wrong responses before the rest of the answer fields.
285             if ($restore->backup_version <= 2004083124) {  // Backup version for 1.4.5+
286                 if ($ismatching = $DB->get_record('lesson_pages', array('id'=>$pageid))) {  // get the page we just inserted
287                     if ($ismatching->qtype == 5) { // check to make sure it is a matching question
288                         $time = time();  // this may need to be changed
289                         // make our 2 response answers
290                         $newanswer->lessonid = $lessonid;
291                         $newanswer->pageid = $pageid;
292                         $newanswer->timecreated = $time;
293                         $newanswer->timemodified = 0;
294                         $DB->insert_record('lesson_answers', $newanswer);
295                         $DB->insert_record('lesson_answers', $newanswer); // TODO: why is this here twice?
296                     }
297                 }
298             }
300             $answers = $info['#']['ANSWERS']['0']['#']['ANSWER'];
302             //Iterate over lesson_answers
303             for($i = 0; $i < sizeof($answers); $i++) {
304                 $answer_info = $answers[$i];
305                 //traverse_xmlize($rub_info);                                  //Debug
306                 //print_object ($GLOBALS['traverse_array']);                   //Debug
307                 //$GLOBALS['traverse_array']="";                               //Debug
309                 //We'll need this later!!
310                 $oldid = backup_todb($answer_info['#']['ID']['0']['#']);
312                 //Now, build the lesson_answers record structure
313                 $answer->lessonid = $lessonid;
314                 $answer->pageid = $pageid;
315                 // the absolute jumps will need fixing later
316                 $answer->jumpto = backup_todb($answer_info['#']['JUMPTO']['0']['#']);
317                 $answer->grade = backup_todb($answer_info['#']['GRADE']['0']['#']);
318                 $answer->score = backup_todb($answer_info['#']['SCORE']['0']['#']);
319                 $answer->flags = backup_todb($answer_info['#']['FLAGS']['0']['#']);
320                 $answer->timecreated = backup_todb($answer_info['#']['TIMECREATED']['0']['#']);
321                 $answer->timemodified = backup_todb($answer_info['#']['TIMEMODIFIED']['0']['#']);
322                 $answer->answer = backup_todb($answer_info['#']['ANSWERTEXT']['0']['#']);
323                 $answer->response = backup_todb($answer_info['#']['RESPONSE']['0']['#']);
325                 //The structure is equal to the db, so insert the lesson_answers
326                 $newid = $DB->insert_record ("lesson_answers",$answer);
328                 //Do some output
329                 if (($i+1) % 10 == 0) {
330                     if (!defined('RESTORE_SILENTLY')) {
331                         echo ".";
332                         if (($i+1) % 200 == 0) {
333                             echo "<br/>";
334                         }
335                     }
336                     backup_flush(300);
337                 }
339                 if ($newid) {
340                     // need to store the id so we can update the useranswer
341                     // field in attempts.  This is done in the lesson_pages_restore_mods
342                     backup_putid($restore->backup_unique_code,"lesson_answers", $oldid, $newid);
344                     if ($userdata) {
345                         //We have to restore the lesson_attempts table now (a answers level table)
346                         $status = lesson_attempts_restore($lessonid, $pageid, $newid, $answer_info, $restore);
347                     }
348                 } else {
349                     $status = false;
350                 }
351             }
352         }
353         return $status;
354     }
357     //This function restores the attempts
358     function lesson_attempts_restore($lessonid, $pageid, $answerid, $info, $restore) {
359         global $CFG, $DB;
361         $status = true;
363         //Get the attempts array (optional)
364         if (isset($info['#']['ATTEMPTS']['0']['#']['ATTEMPT'])) {
365             $attempts = $info['#']['ATTEMPTS']['0']['#']['ATTEMPT'];
366             //Iterate over attempts
367             for($i = 0; $i < sizeof($attempts); $i++) {
368                 $attempt_info = $attempts[$i];
369                 //traverse_xmlize($sub_info);                                                         //Debug
370                 //print_object ($GLOBALS['traverse_array']);                                          //Debug
371                 //$GLOBALS['traverse_array']="";                                                      //Debug
373                 //We'll need this later!!
374                 $olduserid = backup_todb($attempt_info['#']['USERID']['0']['#']);
376                 //Now, build the lesson_attempts record structure
377                 $attempt->lessonid = $lessonid;
378                 $attempt->pageid = $pageid;
379                 $attempt->answerid = $answerid;
380                 $attempt->userid = backup_todb($attempt_info['#']['USERID']['0']['#']);
381                 $attempt->retry = backup_todb($attempt_info['#']['RETRY']['0']['#']);
382                 $attempt->correct = backup_todb($attempt_info['#']['CORRECT']['0']['#']);
383                 $attempt->useranswer = backup_todb($attempt_info['#']['USERANSWER']['0']['#']);
384                 $attempt->timeseen = backup_todb($attempt_info['#']['TIMESEEN']['0']['#']);
386                 //We have to recode the userid field
387                 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
388                 if ($user) {
389                     $attempt->userid = $user->new_id;
390                 }
392                 //The structure is equal to the db, so insert the lesson_attempt
393                 $newid = $DB->insert_record ("lesson_attempts",$attempt);
395                 //Do some output
396                 if (($i+1) % 50 == 0) {
397                     if (!defined('RESTORE_SILENTLY')) {
398                         echo ".";
399                         if (($i+1) % 1000 == 0) {
400                             echo "<br/>";
401                         }
402                     }
403                     backup_flush(300);
404                 }
405             }
406         }
408     return $status;
409     }
411     //This function restores the lesson_grades
412     function lesson_grades_restore_mods($lessonid, $info, $restore) {
413         global $CFG, $DB;
415         $status = true;
417         //Get the grades array (optional)
418         if (isset($info['MOD']['#']['GRADES']['0']['#']['GRADE'])) {
419             $grades = $info['MOD']['#']['GRADES']['0']['#']['GRADE'];
421             //Iterate over grades
422             for($i = 0; $i < sizeof($grades); $i++) {
423                 $grade_info = $grades[$i];
424                 //traverse_xmlize($grade_info);                         //Debug
425                 //print_object ($GLOBALS['traverse_array']);            //Debug
426                 //$GLOBALS['traverse_array']="";                        //Debug
428                 //We'll need this later!!
429                 $olduserid = backup_todb($grade_info['#']['USERID']['0']['#']);
431                 //Now, build the lesson_GRADES record structure
432                 $grade->lessonid = $lessonid;
433                 $grade->userid = backup_todb($grade_info['#']['USERID']['0']['#']);
434                 $grade->grade = backup_todb($grade_info['#']['GRADE_VALUE']['0']['#']);
435                 $grade->late = backup_todb($grade_info['#']['LATE']['0']['#']);
436                 $grade->completed = backup_todb($grade_info['#']['COMPLETED']['0']['#']);
438                 //We have to recode the userid field
439                 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
440                 if ($user) {
441                     $grade->userid = $user->new_id;
442                 }
444                 //The structure is equal to the db, so insert the lesson_grade
445                 $newid = $DB->insert_record ("lesson_grades",$grade);
447                 //Do some output
448                 if (($i+1) % 50 == 0) {
449                     if (!defined('RESTORE_SILENTLY')) {
450                         echo ".";
451                         if (($i+1) % 1000 == 0) {
452                             echo "<br/>";
453                         }
454                     }
455                     backup_flush(300);
456                 }
458                 if (!$newid) {
459                     $status = false;
460                 }
461             }
462         }
464         return $status;
465     }
469     //This function restores the lesson_branch
470     function lesson_branch_restore($lessonid, $pageid, $info, $restore) {
471         global $CFG, $DB;
473         $status = true;
475         //Get the branch array (optional)
476         if (isset($info['#']['BRANCHES']['0']['#']['BRANCH'])) {
477             $branches = $info['#']['BRANCHES']['0']['#']['BRANCH'];
478             //Iterate over branches
479             for($i = 0; $i < sizeof($branches); $i++) {
480                 $branch_info = $branches[$i];
481                 //traverse_xmlize($branch_info);                                                         //Debug
482                 //print_object ($GLOBALS['traverse_array']);                                          //Debug
483                 //$GLOBALS['traverse_array']="";                                                      //Debug
485                 //We'll need this later!!
486                 $olduserid = backup_todb($branch_info['#']['USERID']['0']['#']);
488                 //Now, build the lesson_attempts record structure
489                 $branch->lessonid = $lessonid;
490                 $branch->userid = backup_todb($branch_info['#']['USERID']['0']['#']);
491                 $branch->pageid = $pageid;
492                 $branch->retry = backup_todb($branch_info['#']['RETRY']['0']['#']);
493                 $branch->flag = backup_todb($branch_info['#']['FLAG']['0']['#']);
494                 $branch->timeseen = backup_todb($branch_info['#']['TIMESEEN']['0']['#']);
496                 //We have to recode the userid field
497                 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
498                 if ($user) {
499                     $branch->userid = $user->new_id;
500                 }
502                 //The structure is equal to the db, so insert the lesson_attempt
503                 $newid = $DB->insert_record ("lesson_branch",$branch);
505                 //Do some output
506                 if (($i+1) % 50 == 0) {
507                     if (!defined('RESTORE_SILENTLY')) {
508                         echo ".";
509                         if (($i+1) % 1000 == 0) {
510                             echo "<br/>";
511                         }
512                     }
513                     backup_flush(300);
514                 }
515             }
516         }
518     return $status;
519     }
521     //This function restores the lesson_timer
522     function lesson_timer_restore_mods($lessonid, $info, $restore) {
523         global $CFG, $DB;
525         $status = true;
526         //Get the timer array (optional)
527         if (isset($info['MOD']['#']['TIMES']['0']['#']['TIME'])) {
528             $times = $info['MOD']['#']['TIMES']['0']['#']['TIME'];
529             //Iterate over times
530             for($i = 0; $i < sizeof($times); $i++) {
531                 $time_info = $times[$i];
532                 //traverse_xmlize($time_info);                         //Debug
533                 //print_object ($GLOBALS['traverse_array']);            //Debug
534                 //$GLOBALS['traverse_array']="";                        //Debug
536                 //We'll need this later!!
537                 $olduserid = backup_todb($time_info['#']['USERID']['0']['#']);
539                 //Now, build the lesson_time record structure
540                 $time->lessonid = $lessonid;
541                 $time->userid = backup_todb($time_info['#']['USERID']['0']['#']);
542                 $time->starttime = backup_todb($time_info['#']['STARTTIME']['0']['#']);
543                 $time->lessontime = backup_todb($time_info['#']['LESSONTIME']['0']['#']);
545                 //We have to recode the userid field
546                 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
547                 if ($user) {
548                     $time->userid = $user->new_id;
549                 }
551                 //The structure is equal to the db, so insert the lesson_grade
552                 $newid = $DB->insert_record ("lesson_timer",$time);
554                 //Do some output
555                 if (($i+1) % 50 == 0) {
556                     if (!defined('RESTORE_SILENTLY')) {
557                         echo ".";
558                         if (($i+1) % 1000 == 0) {
559                             echo "<br/>";
560                         }
561                     }
562                     backup_flush(300);
563                 }
565                 if (!$newid) {
566                     $status = false;
567                 }
568             }
569         }
571         return $status;
572     }
574     //This function restores the lesson_high_scores
575     function lesson_high_scores_restore_mods($lessonid, $info, $restore) {
576         global $CFG, $DB;
578         $status = true;
580         //Get the highscores array (optional)
581         if (isset($info['MOD']['#']['HIGHSCORES']['0']['#']['HIGHSCORE'])) {
582             $highscores = $info['MOD']['#']['HIGHSCORES']['0']['#']['HIGHSCORE'];
583             //Iterate over highscores
584             for($i = 0; $i < sizeof($highscores); $i++) {
585                 $highscore_info = $highscores[$i];
586                 //traverse_xmlize($highscore_info);                     //Debug
587                 //print_object ($GLOBALS['traverse_array']);            //Debug
588                 //$GLOBALS['traverse_array']="";                        //Debug
590                 //We'll need this later!!
591                 $olduserid = backup_todb($highscore_info['#']['USERID']['0']['#']);
593                 //Now, build the lesson_highscores record structure
594                 $highscore->lessonid = $lessonid;
595                 $highscore->userid = backup_todb($highscore_info['#']['USERID']['0']['#']);
596                 $highscore->gradeid = backup_todb($highscore_info['#']['GRADEID']['0']['#']);
597                 $highscore->nickname = backup_todb($highscore_info['#']['NICKNAME']['0']['#']);
599                 //We have to recode the userid field
600                 $user = backup_getid($restore->backup_unique_code,"user",$olduserid);
601                 if ($user) {
602                     $highscore->userid = $user->new_id;
603                 }
605                 //The structure is equal to the db, so insert the lesson_grade
606                 $newid = $DB->insert_record ("lesson_high_scores",$highscore);
608                 //Do some output
609                 if (($i+1) % 50 == 0) {
610                     if (!defined('RESTORE_SILENTLY')) {
611                         echo ".";
612                         if (($i+1) % 1000 == 0) {
613                             echo "<br/>";
614                         }
615                     }
616                     backup_flush(300);
617                 }
619                 if (!$newid) {
620                     $status = false;
621                 }
622             }
623         }
625         return $status;
626     }
628     //Return a content decoded to support interactivities linking. Every module
629     //should have its own. They are called automatically from
630     //lesson_decode_content_links_caller() function in each module
631     //in the restore process
632     function lesson_decode_content_links ($content,$restore) {
633         global $CFG;
635         $result = $content;
637         //Link to the list of lessons
639         $searchstring='/\$@(LESSONINDEX)\*([0-9]+)@\$/';
640         //We look for it
641         preg_match_all($searchstring,$content,$foundset);
642         //If found, then we are going to look for its new id (in backup tables)
643         if ($foundset[0]) {
644             //print_object($foundset);                                     //Debug
645             //Iterate over foundset[2]. They are the old_ids
646             foreach($foundset[2] as $old_id) {
647                 //We get the needed variables here (course id)
648                 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
649                 //Personalize the searchstring
650                 $searchstring='/\$@(LESSONINDEX)\*('.$old_id.')@\$/';
651                 //If it is a link to this course, update the link to its new location
652                 if($rec->new_id) {
653                     //Now replace it
654                     $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/lesson/index.php?id='.$rec->new_id,$result);
655                 } else {
656                     //It's a foreign link so leave it as original
657                     $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/lesson/index.php?id='.$old_id,$result);
658                 }
659             }
660         }
662         //Link to lesson view by moduleid
664         $searchstring='/\$@(LESSONVIEWBYID)\*([0-9]+)@\$/';
665         //We look for it
666         preg_match_all($searchstring,$result,$foundset);
667         //If found, then we are going to look for its new id (in backup tables)
668         if ($foundset[0]) {
669             //print_object($foundset);                                     //Debug
670             //Iterate over foundset[2]. They are the old_ids
671             foreach($foundset[2] as $old_id) {
672                 //We get the needed variables here (course_modules id)
673                 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
674                 //Personalize the searchstring
675                 $searchstring='/\$@(LESSONVIEWBYID)\*('.$old_id.')@\$/';
676                 //If it is a link to this course, update the link to its new location
677                 if($rec->new_id) {
678                     //Now replace it
679                     $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/lesson/view.php?id='.$rec->new_id,$result);
680                 } else {
681                     //It's a foreign link so leave it as original
682                     $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/lesson/view.php?id='.$old_id,$result);
683                 }
684             }
685         }
687         return $result;
688     }
690     //This function makes all the necessary calls to xxxx_decode_content_links()
691     //function in each module, passing them the desired contents to be decoded
692     //from backup format to destination site/course in order to mantain inter-activities
693     //working in the backup/restore process. It's called from restore_decode_content_links()
694     //function in restore process
695     function lesson_decode_content_links_caller($restore) {
696         global $CFG, $DB;
697         $status = true;
699         //Process every lesson PAGE in the course
700         if ($pages = $DB->get_records_sql("SELECT p.id, p.contents
701                                              FROM {lesson_pages} p,
702                                                   {lesson} l
703                                             WHERE l.course = ? AND
704                                                   p.lessonid = l.id", array($restore->course_id))) {
705             //Iterate over each page->message
706             $i = 0;   //Counter to send some output to the browser to avoid timeouts
707             foreach ($pages as $page) {
708                 //Increment counter
709                 $i++;
710                 $content = $page->contents;
711                 $result = restore_decode_content_links_worker($content,$restore);
712                 if ($result != $content) {
713                     //Update record
714                     $page->contents = $result;
715                     $status = $DB->update_record("lesson_pages",$page);
716                     if (debugging()) {
717                         if (!defined('RESTORE_SILENTLY')) {
718                             echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
719                         }
720                     }
721                 }
722                 //Do some output
723                 if (($i+1) % 5 == 0) {
724                     if (!defined('RESTORE_SILENTLY')) {
725                         echo ".";
726                         if (($i+1) % 100 == 0) {
727                             echo "<br />";
728                         }
729                     }
730                     backup_flush(300);
731                 }
732             }
733         }
735         // Remap activity links
736         if ($lessons = $DB->get_records_select('lesson', "activitylink <> 0 AND course = ?", array($restore->course_id), '', 'id, activitylink')) {
737             foreach ($lessons as $lesson) {
738                 if ($newcmid = backup_getid($restore->backup_unique_code, 'course_modules', $lesson->activitylink)) {
739                     $status = $status and $DB->set_field('lesson', 'activitylink', $newcmid->new_id, array('id'=>$lesson->id));
740                 }
741             }
742         }
744         return $status;
745     }
747     //This function returns a log record with all the necessay transformations
748     //done. It's used by restore_log_module() to restore modules log.
749     function lesson_restore_logs($restore,$log) {
751         $status = false;
753         //Depending of the action, we recode different things
754         switch ($log->action) {
755         case "add":
756             if ($log->cmid) {
757                 //Get the new_id of the module (to recode the info field)
758                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
759                 if ($mod) {
760                     $log->url = "view.php?id=".$log->cmid;
761                     $log->info = $mod->new_id;
762                     $status = true;
763                 }
764             }
765             break;
766         case "update":
767             if ($log->cmid) {
768                 //Get the new_id of the module (to recode the info field)
769                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
770                 if ($mod) {
771                     $log->url = "view.php?id=".$log->cmid;
772                     $log->info = $mod->new_id;
773                     $status = true;
774                 }
775             }
776             break;
777         case "view all":
778             $log->url = "index.php?id=".$log->course;
779             $status = true;
780             break;
781         case "start":
782             if ($log->cmid) {
783                 //Get the new_id of the module (to recode the info field)
784                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
785                 if ($mod) {
786                     $log->url = "view.php?id=".$log->cmid;
787                     $log->info = $mod->new_id;
788                     $status = true;
789                 }
790             }
791             break;
792         case "end":
793             if ($log->cmid) {
794                 //Get the new_id of the module (to recode the info field)
795                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
796                 if ($mod) {
797                     $log->url = "view.php?id=".$log->cmid;
798                     $log->info = $mod->new_id;
799                     $status = true;
800                 }
801             }
802             break;
803         case "view":
804             if ($log->cmid) {
805                 //Get the new_id of the page (to recode the url field)
806                 $pag = backup_getid($restore->backup_unique_code,"lesson_pages",$log->info);
807                 if ($pag) {
808                     $log->url = "view.php?id=".$log->cmid."&action=navigation&pageid=".$pag->new_id;
809                     $log->info = $pag->new_id;
810                     $status = true;
811                 }
812             }
813             break;
814         default:
815             if (!defined('RESTORE_SILENTLY')) {
816                 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br/>";                 //Debug
817             }
818             break;
819         }
821         if ($status) {
822             $status = $log;
823         }
824         return $status;
825     }