5e3a937b3e060e2571bf4fdcdd4a3c2f23c8b803
[moodle.git] / mod / scorm / restorelib.php
1 <?php //$Id$
2     //This php script contains all the stuff to backup/restore
3     //reservation mods
5     //This is the "graphical" structure of the scorm mod:
6     //
7     //                      scorm                                      
8     //                   (CL,pk->id)---------------------
9     //                        |                         |
10     //                        |                         |
11     //                        |                         |
12     //                   scorm_scoes                    |       
13     //             (UL,pk->id, fk->scorm)               |
14     //                        |                         |
15     //                        |                         |
16     //                        |                         |
17     //                scorm_scoes_track                 |
18     //  (UL,pk->id, fk->scormid, fk->scoid, fk->userid)--       
19     //
20     // Meaning: pk->primary key field of the table
21     //          fk->foreign key to link with parent
22     //          nt->nested field (recursive data)
23     //          CL->course level info
24     //          UL->user level info
25     //          files->table may have files)
26     //
27     //-----------------------------------------------------------
29     //This function executes all the restore procedure about this mod
30     function scorm_restore_mods($mod,$restore) {
32         global $CFG;
34         $status = true;
36         //Get record from backup_ids
37         $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
39         if ($data) {
40             //Now get completed xmlized object
41             $info = $data->info;
42             //traverse_xmlize($info);                                                                     //Debug
43             //print_object ($GLOBALS['traverse_array']);                                                  //Debug
44             //$GLOBALS['traverse_array']="";                                                              //Debug
46             //Now, build the SCORM record structure
47             $scorm->course = $restore->course_id;
48             $scorm->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
49             $scorm->reference = backup_todb($info['MOD']['#']['REFERENCE']['0']['#']);
50             $scorm->reference = backup_todb($info['MOD']['#']['MAXGRADE']['0']['#']);
51             $scorm->reference = backup_todb($info['MOD']['#']['GRADEMETHOD']['0']['#']);
52             $scorm->launch = backup_todb($info['MOD']['#']['LAUNCH']['0']['#']);
53             $scorm->summary = backup_todb($info['MOD']['#']['SUMMARY']['0']['#']);
54             $scorm->auto = backup_todb($info['MOD']['#']['AUTO']['0']['#']);
55             $scorm->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
57             //The structure is equal to the db, so insert the scorm
58             $newid = insert_record ("scorm",$scorm);
59             //Do some output     
60             echo "<li>".get_string("modulename","scorm")." \"".format_string(stripslashes($scorm->name),true)."\"</li>";
61             backup_flush(300);
63             if ($newid) {
64                 //We have the newid, update backup_ids
65                 backup_putid($restore->backup_unique_code,$mod->modtype,
66                              $mod->id, $newid);
67                              
68                 //Now copy moddata associated files
69                 $status = scorm_restore_files ($scorm, $restore);
70                 
71                 if ($status)
72                     $status = scorm_scoes_restore_mods ($newid,$info,$restore);
73                     
74             } else {
75                 $status = false;
76             }
77         } else {
78             $status = false;
79         }
81         return $status;
82     }
84     //This function restores the scorm_scoes
85     function scorm_scoes_restore_mods($scorm_id,$info,$restore) {
87         global $CFG;
89         $status = true;
91         //Get the sco array
92         $scoes = $info['MOD']['#']['SCOES']['0']['#']['SCO'];
94         //Iterate over scoes
95         for($i = 0; $i < sizeof($scoes); $i++) {
96             $sub_info = $scoes[$i];
98             //We'll need this later!!
99             $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
100             
101             //Now, build the scorm_scoes record structure
102             $sco->scorm = $scorm_id;
103             $sco->manifest = backup_todb($sub_info['#']['MANIFEST']['0']['#']);
104             $sco->organization = backup_todb($sub_info['#']['ORGANIZATION']['0']['#']);
105             $sco->parent = backup_todb($sub_info['#']['PARENT']['0']['#']);
106             $sco->identifier = backup_todb($sub_info['#']['IDENTIFIER']['0']['#']);
107             $sco->launch = backup_todb($sub_info['#']['LAUNCH']['0']['#']);
108             $sco->scormtype = backup_todb($sub_info['#']['SCORMTYPE']['0']['#']);
109             $sco->title = backup_todb($sub_info['#']['TITLE']['0']['#']);
110             $sco->title = backup_todb($sub_info['#']['PREREQUISITES']['0']['#']);
111             $sco->title = backup_todb($sub_info['#']['MAXTIMEALLOWED']['0']['#']);
112             $sco->title = backup_todb($sub_info['#']['TIMELIMITACTION']['0']['#']);
113             $sco->datafromlms = backup_todb($sub_info['#']['DATAFROMLMS']['0']['#']);
114             $sco->title = backup_todb($sub_info['#']['MASTERYSCORE']['0']['#']);
115             $sco->next = backup_todb($sub_info['#']['NEXT']['0']['#']);
116             $sco->previous = backup_todb($sub_info['#']['PREVIOUS']['0']['#']);
118             //The structure is equal to the db, so insert the scorm_scoes
119             $newid = insert_record ("scorm_scoes",$sco);
120             
121             //Now check if want to restore user data and do it.
122             if ($restore->mods['scorm']->userinfo) {
123                 //Restore scorm_scoes
124                 if ($status) {
125                     $status = scorm_scoes_tracks_restore_mods ($scorm_id,$info,$restore);
126                 }
127             }
129             //Do some output
130             if (($i+1) % 50 == 0) {
131                 echo ".";
132                 if (($i+1) % 1000 == 0) {
133                     echo "<br />";
134                 }
135                 backup_flush(300);
136             }
137             
138             if ($newid) {
139                 //We have the newid, update backup_ids
140                 backup_putid($restore->backup_unique_code,"scorm_scoes", $oldid, $newid);
141             } else {
142                 $status = false;
143             }
144         }
146         return $status;
147     }
148     
149     //This function restores the scorm_scoes_track
150     function scorm_scoes_tracks_restore_mods($scorm_id,$info,$restore) {
152         global $CFG;
154         $status = true;
155         $sco_tracks = NULL;
157         //Get the sco array
158         if (!empty($info['MOD']['#']['SCO_TRACKS']['0']['#']['SCO_TRACK']))
159             $sco_tracks = $info['MOD']['#']['SCO_TRACKS']['0']['#']['SCO_TRACK'];
161         //Iterate over sco_users
162         for($i = 0; $i < sizeof($sco_tracks); $i++) {
163             $sco_track = $sco_tracks[$i];
165             //We'll need this later!!
166             $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
167             $oldscoid = backup_todb($sub_info['#']['SCOID']['0']['#']);
168             $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
170             //Now, build the scorm_scoes_track record structure
171             $sco_user->scormid = $scorm_id;
172             $sco_user->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
173             $sco_user->scoid = backup_todb($sub_info['#']['SCOID']['0']['#']);
174             $sco_user->element = backup_todb($sub_info['#']['ELEMENT']['0']['#']);
175             $sco_user->value = backup_todb($sub_info['#']['VALUE']['0']['#']);
177             //We have to recode the userid field
178             $user = backup_getid($restore->backup_unique_code,"user",$sco_track->userid);
179             if ($user) {
180                 $sco_track->userid = $user->new_id;
181             }
182             
183             //We have to recode the scoid field
184             $sco = backup_getid($restore->backup_unique_code,"scorm_scoes",$sco_track->scoid);
185             if ($sco) {
186                 $sco_track->scoid = $sco->new_id;
187             }
189             //The structure is equal to the db, so insert the scorm_scoes_track
190             $newid = insert_record ("scorm_scoes_track",$sco_track);
192             //Do some output
193             if (($i+1) % 50 == 0) {
194                 echo ".";
195                 if (($i+1) % 1000 == 0) {
196                     echo "<br />";
197                 }
198                 backup_flush(300);
199             }
201         }
203         return $status;
204     }
205     
206     //This function copies the scorm related info from backup temp dir to course moddata folder,
207     //creating it if needed
208     function scorm_restore_files ($package, $restore) {
210         global $CFG;
212         $status = true;
213         $todo = false;
214         $moddata_path = "";
215         $scorm_path = "";
216         $temp_path = "";
218         //First, we check to "course_id" exists and create is as necessary
219         //in CFG->dataroot
220         $dest_dir = $CFG->dataroot."/".$restore->course_id;
221         $status = check_dir_exists($dest_dir,true);
223         //First, locate course's moddata directory
224         $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
225   
226         //Check it exists and create it
227         $status = check_dir_exists($moddata_path,true);
229         //Now, locate scorm directory
230         if ($status) {
231             $scorm_path = $moddata_path."/scorm";
232             //Check it exists and create it
233             $status = check_dir_exists($scorm_path,true);
234         }
236         //Now locate the temp dir we are restoring from
237         if ($status) {
238             $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
239                          "/moddata/scorm/".$package->id;
240             //Check it exists
241             if (is_dir($temp_path)) {
242                 $todo = true;
243             }
244         }
246         //If todo, we create the neccesary dirs in course moddata/scorm
247         if ($status and $todo) {
248             //Make scorm package directory path
249             $this_scorm_path = $scorm_path."/".$package->id;
250        
251             $status = backup_copy_file($temp_path, $this_scorm_path);
252         }
254         return $status;
255     }
256     
257     //This function returns a log record with all the necessay transformations
258     //done. It's used by restore_log_module() to restore modules log.
259     function scorm_restore_logs($restore,$log) {
260                     
261         $status = true;
262                     
263         return $status;
264     }
265 ?>