MDL-23479 backup - lesson revised & old code deleted (but restore logs)
[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();
61     //This function returns a log record with all the necessay transformations
62     //done. It's used by restore_log_module() to restore modules log.
63     function lesson_restore_logs($restore,$log) {
65         $status = false;
67         //Depending of the action, we recode different things
68         switch ($log->action) {
69         case "add":
70             if ($log->cmid) {
71                 //Get the new_id of the module (to recode the info field)
72                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
73                 if ($mod) {
74                     $log->url = "view.php?id=".$log->cmid;
75                     $log->info = $mod->new_id;
76                     $status = true;
77                 }
78             }
79             break;
80         case "update":
81             if ($log->cmid) {
82                 //Get the new_id of the module (to recode the info field)
83                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
84                 if ($mod) {
85                     $log->url = "view.php?id=".$log->cmid;
86                     $log->info = $mod->new_id;
87                     $status = true;
88                 }
89             }
90             break;
91         case "view all":
92             $log->url = "index.php?id=".$log->course;
93             $status = true;
94             break;
95         case "start":
96             if ($log->cmid) {
97                 //Get the new_id of the module (to recode the info field)
98                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
99                 if ($mod) {
100                     $log->url = "view.php?id=".$log->cmid;
101                     $log->info = $mod->new_id;
102                     $status = true;
103                 }
104             }
105             break;
106         case "end":
107             if ($log->cmid) {
108                 //Get the new_id of the module (to recode the info field)
109                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
110                 if ($mod) {
111                     $log->url = "view.php?id=".$log->cmid;
112                     $log->info = $mod->new_id;
113                     $status = true;
114                 }
115             }
116             break;
117         case "view":
118             if ($log->cmid) {
119                 //Get the new_id of the page (to recode the url field)
120                 $pag = backup_getid($restore->backup_unique_code,"lesson_pages",$log->info);
121                 if ($pag) {
122                     $log->url = "view.php?id=".$log->cmid."&action=navigation&pageid=".$pag->new_id;
123                     $log->info = $pag->new_id;
124                     $status = true;
125                 }
126             }
127             break;
128         default:
129             if (!defined('RESTORE_SILENTLY')) {
130                 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br/>";                 //Debug
131             }
132             break;
133         }
135         if ($status) {
136             $status = $log;
137         }
138         return $status;
139     }