Added user activity support
[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->width = backup_todb($info['MOD']['#']['WIDTH']['0']['#']);
56             $scorm->height = backup_todb($info['MOD']['#']['HEIGHT']['0']['#']);
57             $scorm->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
59             //The structure is equal to the db, so insert the scorm
60             $newid = insert_record ("scorm",$scorm);
61             //Do some output
62             echo "<li>".get_string("modulename","scorm")." \"".format_string(stripslashes($scorm->name),true)."\"</li>";
63             backup_flush(300);
65             if ($newid) {
66                 //We have the newid, update backup_ids
67                 backup_putid($restore->backup_unique_code,$mod->modtype,
68                              $mod->id, $newid);
70                 //Now copy moddata associated files
71                 $status = scorm_restore_files ($scorm, $restore);
73                 if ($status)
74                     $status = scorm_scoes_restore_mods ($newid,$info,$restore);
76             } else {
77                 $status = false;
78             }
79         } else {
80             $status = false;
81         }
83         return $status;
84     }
86     //This function restores the scorm_scoes
87     function scorm_scoes_restore_mods($scorm_id,$info,$restore) {
89         global $CFG;
91         $status = true;
93         //Get the sco array
94         $scoes = $info['MOD']['#']['SCOES']['0']['#']['SCO'];
96         //Iterate over scoes
97         for($i = 0; $i < sizeof($scoes); $i++) {
98             $sub_info = $scoes[$i];
100             //We'll need this later!!
101             $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
103             //Now, build the scorm_scoes record structure
104             $sco->scorm = $scorm_id;
105             $sco->manifest = backup_todb($sub_info['#']['MANIFEST']['0']['#']);
106             $sco->organization = backup_todb($sub_info['#']['ORGANIZATION']['0']['#']);
107             $sco->parent = backup_todb($sub_info['#']['PARENT']['0']['#']);
108             $sco->identifier = backup_todb($sub_info['#']['IDENTIFIER']['0']['#']);
109             $sco->launch = backup_todb($sub_info['#']['LAUNCH']['0']['#']);
110             $sco->scormtype = backup_todb($sub_info['#']['SCORMTYPE']['0']['#']);
111             $sco->title = backup_todb($sub_info['#']['TITLE']['0']['#']);
112             $sco->title = backup_todb($sub_info['#']['PREREQUISITES']['0']['#']);
113             $sco->title = backup_todb($sub_info['#']['MAXTIMEALLOWED']['0']['#']);
114             $sco->title = backup_todb($sub_info['#']['TIMELIMITACTION']['0']['#']);
115             $sco->datafromlms = backup_todb($sub_info['#']['DATAFROMLMS']['0']['#']);
116             $sco->title = backup_todb($sub_info['#']['MASTERYSCORE']['0']['#']);
117             $sco->next = backup_todb($sub_info['#']['NEXT']['0']['#']);
118             $sco->previous = backup_todb($sub_info['#']['PREVIOUS']['0']['#']);
120             //The structure is equal to the db, so insert the scorm_scoes
121             $newid = insert_record ("scorm_scoes",$sco);
123             //Now check if want to restore user data and do it.
124             if ($restore->mods['scorm']->userinfo) {
125                 //Restore scorm_scoes
126                 if ($status) {
127                     $status = scorm_scoes_tracks_restore_mods ($scorm_id,$info,$restore);
128                 }
129             }
131             //Do some output
132             if (($i+1) % 50 == 0) {
133                 echo ".";
134                 if (($i+1) % 1000 == 0) {
135                     echo "<br />";
136                 }
137                 backup_flush(300);
138             }
140             if ($newid) {
141                 //We have the newid, update backup_ids
142                 backup_putid($restore->backup_unique_code,"scorm_scoes", $oldid, $newid);
143             } else {
144                 $status = false;
145             }
146         }
148         return $status;
149     }
151     //This function restores the scorm_scoes_track
152     function scorm_scoes_tracks_restore_mods($scorm_id,$info,$restore) {
154         global $CFG;
156         $status = true;
157         $sco_tracks = NULL;
159         //Get the sco array
160         if (!empty($info['MOD']['#']['SCO_TRACKS']['0']['#']['SCO_TRACK']))
161             $sco_tracks = $info['MOD']['#']['SCO_TRACKS']['0']['#']['SCO_TRACK'];
163         //Iterate over sco_users
164         for($i = 0; $i < sizeof($sco_tracks); $i++) {
165             $sco_track = $sco_tracks[$i];
167             //We'll need this later!!
168             $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
169             $oldscoid = backup_todb($sub_info['#']['SCOID']['0']['#']);
170             $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
172             //Now, build the scorm_scoes_track record structure
173             $sco_user->scormid = $scorm_id;
174             $sco_user->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
175             $sco_user->scoid = backup_todb($sub_info['#']['SCOID']['0']['#']);
176             $sco_user->element = backup_todb($sub_info['#']['ELEMENT']['0']['#']);
177             $sco_user->value = backup_todb($sub_info['#']['VALUE']['0']['#']);
179             //We have to recode the userid field
180             $user = backup_getid($restore->backup_unique_code,"user",$sco_track->userid);
181             if ($user) {
182                 $sco_track->userid = $user->new_id;
183             }
185             //We have to recode the scoid field
186             $sco = backup_getid($restore->backup_unique_code,"scorm_scoes",$sco_track->scoid);
187             if ($sco) {
188                 $sco_track->scoid = $sco->new_id;
189             }
191             //The structure is equal to the db, so insert the scorm_scoes_track
192             $newid = insert_record ("scorm_scoes_track",$sco_track);
194             //Do some output
195             if (($i+1) % 50 == 0) {
196                 echo ".";
197                 if (($i+1) % 1000 == 0) {
198                     echo "<br />";
199                 }
200                 backup_flush(300);
201             }
203         }
205         return $status;
206     }
208     //This function copies the scorm related info from backup temp dir to course moddata folder,
209     //creating it if needed
210     function scorm_restore_files ($package, $restore) {
212         global $CFG;
214         $status = true;
215         $todo = false;
216         $moddata_path = "";
217         $scorm_path = "";
218         $temp_path = "";
220         //First, we check to "course_id" exists and create is as necessary
221         //in CFG->dataroot
222         $dest_dir = $CFG->dataroot."/".$restore->course_id;
223         $status = check_dir_exists($dest_dir,true);
225         //First, locate course's moddata directory
226         $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
228         //Check it exists and create it
229         $status = check_dir_exists($moddata_path,true);
231         //Now, locate scorm directory
232         if ($status) {
233             $scorm_path = $moddata_path."/scorm";
234             //Check it exists and create it
235             $status = check_dir_exists($scorm_path,true);
236         }
238         //Now locate the temp dir we are restoring from
239         if ($status) {
240             $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
241                          "/moddata/scorm/".$package->id;
242             //Check it exists
243             if (is_dir($temp_path)) {
244                 $todo = true;
245             }
246         }
248         //If todo, we create the neccesary dirs in course moddata/scorm
249         if ($status and $todo) {
250             //Make scorm package directory path
251             $this_scorm_path = $scorm_path."/".$package->id;
253             $status = backup_copy_file($temp_path, $this_scorm_path);
254         }
256         return $status;
257     }
259     //This function returns a log record with all the necessay transformations
260     //done. It's used by restore_log_module() to restore modules log.
261     function scorm_restore_logs($restore,$log) {
263         $status = true;
265         return $status;
266     }
267 ?>