Updated the 19 build version to 20101023
[moodle.git] / mod / scorm / restorelib.php
bloba7f4b44e7978cf1e2c61ed6e07b90b39385d2f46
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)--
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)
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->version = backup_todb($info['MOD']['#']['VERSION']['0']['#']);
51 $scorm->md5hash = backup_todb($info['MOD']['#']['MD5HASH']['0']['#']);
52 $scorm->maxgrade = (double)backup_todb($info['MOD']['#']['MAXGRADE']['0']['#']);
53 $scorm->updatefreq = (int)backup_todb($info['MOD']['#']['UPDATEFREQ']['0']['#']);
54 $scorm->maxattempt = (int)backup_todb($info['MOD']['#']['MAXATTEMPT']['0']['#']);
55 $scorm->grademethod = (int)backup_todb($info['MOD']['#']['GRADEMETHOD']['0']['#']);
56 $scorm->whatgrade = (int)backup_todb($info['MOD']['#']['WHATGRADE']['0']['#']);
57 if ($restore->backup_version < 2005041500) {
58 $scorm->datadir = substr(backup_todb($info['MOD']['#']['DATADIR']['0']['#']),1);
59 } else {
60 $scorm->datadir = backup_todb($info['MOD']['#']['ID']['0']['#']);
62 $oldlaunch = backup_todb($info['MOD']['#']['LAUNCH']['0']['#']);
63 if ($restore->backup_version < 2006102600) {
64 $scorm->skipview = 1;
65 } else {
66 $scorm->skipview = backup_todb($info['MOD']['#']['SKIPVIEW']['0']['#']);
68 $scorm->summary = backup_todb($info['MOD']['#']['SUMMARY']['0']['#']);
69 $scorm->hidebrowse = backup_todb($info['MOD']['#']['HIDEBROWSE']['0']['#']);
70 $scorm->hidetoc = backup_todb($info['MOD']['#']['HIDETOC']['0']['#']);
71 $scorm->hidenav = backup_todb($info['MOD']['#']['HIDENAV']['0']['#']);
72 $scorm->auto = backup_todb($info['MOD']['#']['AUTO']['0']['#']);
73 if ($restore->backup_version < 2005040200) {
74 $oldpopup = backup_todb($info['MOD']['#']['POPUP']['0']['#']);
75 if (!empty($oldpopup)) {
76 $scorm->popup = 1;
77 // Parse old popup field
78 $options = array();
79 $oldoptions = explode(',',$scorm->popup);
80 foreach ($oldoptions as $oldoption) {
81 list($element,$value) = explode('=',$oldoption);
82 $element = trim($element);
83 $value = trim($value);
84 switch ($element) {
85 case 'width':
86 $scorm->width = $value;
87 break;
88 case 'height':
89 $scorm->height = $value;
90 break;
91 default:
92 $options[] = $element.'='.$value;
93 break;
96 $scorm->options = implode($options,',');
97 } else {
98 $scorm->popup = 0;
99 $scorm->options = '';
100 $scorm->width = '100%';
101 $scorm->height = 500;
103 } else {
104 $scorm->popup = backup_todb($info['MOD']['#']['POPUP']['0']['#']);
105 $scorm->width = backup_todb($info['MOD']['#']['WIDTH']['0']['#']);
106 if ($scorm->width == 0) {
107 $scorm->width = '100%';
109 $scorm->height = backup_todb($info['MOD']['#']['HEIGHT']['0']['#']);
110 if ($scorm->height == 0) {
111 $scorm->height = 500;
113 if (!empty($info['MOD']['#']['OPTIONS']['0']['#'])) {
114 $scorm->options = backup_todb($info['MOD']['#']['OPTIONS']['0']['#']);
117 $scorm->timemodified = time();
119 //The structure is equal to the db, so insert the scorm
120 $newid = insert_record ("scorm",$scorm);
121 //Do some output
122 if (!defined('RESTORE_SILENTLY')) {
123 echo "<li>".get_string("modulename","scorm")." \"".format_string(stripslashes($scorm->name),true)."\"</li>";
125 backup_flush(300);
127 if ($newid) {
128 //We have the newid, update backup_ids
129 backup_putid($restore->backup_unique_code,$mod->modtype,
130 $mod->id, $newid);
131 $scorm->id = $newid;
132 //Now copy moddata associated files
133 $status = scorm_restore_files ($scorm, $restore);
135 if ($status) {
136 $status = scorm_scoes_restore_mods ($newid,$info,$restore,$mod->id);
137 if ($status) {
138 $launchsco = backup_getid($restore->backup_unique_code,"scorm_scoes",$oldlaunch);
139 $scorm->launch = $launchsco->new_id;
140 update_record('scorm',$scorm);
144 } else {
145 $status = false;
147 } else {
148 $status = false;
151 return $status;
154 //This function restores the scorm_scoes
155 function scorm_scoes_restore_mods($scorm_id,$info,$restore,$oldmodid) {
157 global $CFG;
159 $status = true;
161 //Get the sco array
162 $scoes = $info['MOD']['#']['SCOES']['0']['#']['SCO'];
164 //Iterate over scoes
165 for($i = 0; $i < sizeof($scoes); $i++) {
166 $sub_info = $scoes[$i];
168 //We'll need this later!!
169 $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
171 //Now, build the scorm_scoes record structure
172 $sco->scorm = $scorm_id;
173 $sco->manifest = backup_todb($sub_info['#']['MANIFEST']['0']['#']);
174 $sco->organization = backup_todb($sub_info['#']['ORGANIZATION']['0']['#']);
175 $sco->parent = backup_todb($sub_info['#']['PARENT']['0']['#']);
176 $sco->identifier = backup_todb($sub_info['#']['IDENTIFIER']['0']['#']);
177 $sco->launch = backup_todb($sub_info['#']['LAUNCH']['0']['#']);
178 $sco->title = backup_todb($sub_info['#']['TITLE']['0']['#']);
179 if ($restore->backup_version < 2005031300) {
180 $sco->scormtype = backup_todb($sub_info['#']['TYPE']['0']['#']);
181 } else {
182 $sco->scormtype = backup_todb($sub_info['#']['SCORMTYPE']['0']['#']);
185 //The structure is equal to the db, so insert the scorm_scoes
186 $newid = insert_record ("scorm_scoes",$sco);
188 if ($newid) {
189 //We have the newid, update backup_ids
190 backup_putid($restore->backup_unique_code,"scorm_scoes", $oldid, $newid);
191 } else {
192 $status = false;
194 if ($status && !empty($sub_info['#']['SCO_DATAS'])) {
195 $status = scorm_scoes_data_restore_mods($newid, $sub_info['#']['SCO_DATAS'], $restore);
199 //Now check if want to restore user data and do it.
200 scorm_scoes_seq_objective_restore_mods ($newid,$info,$restore);
201 scorm_scoes_seq_rolluprule_restore_mods ($newid,$info,$restore);
202 scorm_scoes_seq_ruleconds_restore_mods ($newid,$info,$restore);
203 if (restore_userdata_selected($restore,'scorm',$oldmodid)) {
204 //Restore scorm_scoes
205 if ($status) {
206 if ($restore->backup_version < 2005031300) {
207 $status = scorm_scoes_tracks_restore_mods_pre15 ($scorm_id,$info,$restore);
208 } else {
209 $status = scorm_scoes_tracks_restore_mods ($scorm_id,$info,$restore);
214 return $status;
217 //Restore scorm_scoes_data contents (executed from scorm_scoes_restore_mods)
218 function scorm_scoes_data_restore_mods ($new_scoe_id, $scoes_old_datas, $restore) {
219 global $CFG;
221 $status = true;
223 $scoe_old_datas = $scoes_old_datas['0']['#']['SCO_DATA'];
225 //Iterate over scoe_data
226 for($i = 0; $i < sizeof($scoe_old_datas); $i++) {
227 $scoe_old_data = $scoe_old_datas[$i];
228 unset($scoe_new_data);
230 $scoe_new_data->scoid = $new_scoe_id;
231 $scoe_new_data->name = backup_todb($scoe_old_data['#']['NAME']['0']['#']);
232 $scoe_new_data->value = backup_todb($scoe_old_data['#']['VALUE']['0']['#']);
234 //The structure is equal to the db, so insert the scorm_scoes_data table
235 $new_scoe_data_id = insert_record ("scorm_scoes_data",$scoe_new_data);
237 $old_scoe_data_id = backup_todb($scoe_old_data['#']['ID']['0']['#']);
239 if ($new_scoe_data_id) {
240 //We have the newid, update backup_ids
241 backup_putid($restore->backup_unique_code,"scorm_scoes_data", $old_scoe_data_id, $new_scoe_data_id);
242 } else {
243 $status = false;
246 return $status;
249 function scorm_scoes_seq_objective_restore_mods($sco_id,$info,$restore) {
251 global $CFG;
253 $status = true;
255 //Get the discussions array
256 $objectives = array();
258 if (!empty($info['MOD']['#']['SEQ_OBJECTIVES']['0']['#']['SEQ_OBJECTIVE'])) {
259 $objectives = $info['MOD']['#']['SEQ_OBJECTIVES']['0']['#']['SEQ_OBJECTIVE'];
262 //Iterate over discussions
263 for($i = 0; $i < sizeof($objectives); $i++) {
264 $obj_info = $objectives[$i];
265 //Debug
267 //We'll need this later!!
268 $oldid = backup_todb($obj_info['#']['ID']['0']['#']);
269 //Now, build the FORUM_DISCUSSIONS record structure
270 $objective->scoid = $sco_id;
271 $objective->primaryobj = backup_todb($obj_info['#']['PRIMARYOBJ']['0']['#']);
272 $objective->objectiveid = backup_todb($obj_info['#']['OBJECTIVEID']['0']['#']);
273 $objective->satisfiedbymeasure = backup_todb($obj_info['#']['SATISFIEDBYMEASURE']['0']['#']);
274 $objective->minnormalizedmeasure = backup_todb($obj_info['#']['MINNORMALIZEDMEASURE']['0']['#']);
276 //The structure is equal to the db, so insert the forum_discussions
277 $newid = insert_record ("scorm_seq_objective",$objective);
279 if ($newid) {
280 //We have the newid, update backup_ids
281 backup_putid($restore->backup_unique_code,"scorm_seq_objective", $oldid, $newid);
282 } else {
283 $status = false;
286 //Do some output
287 if (($i+1) % 50 == 0) {
288 if (!defined('RESTORE_SILENTLY')) {
289 echo ".";
290 if (($i+1) % 1000 == 0) {
291 echo "<br />";
294 backup_flush(300);
297 //Now restore the scorm_seq_mapinfo for each objective
299 $status = scorm_seq_mapinfo_restore_mods ($sco_id,$newid,$obj_info,$restore);
303 return $status;
308 function scorm_scoes_seq_rolluprule_restore_mods($sco_id,$info,$restore) {
310 global $CFG;
312 $status = true;
314 //Get the discussions array
315 $rolluprules = array();
317 if (!empty($info['MOD']['#']['SEQ_ROLLUPRULES']['0']['#']['SEQ_ROLLUPRULE'])) {
318 $rolluprules = $info['MOD']['#']['SEQ_ROLLUPRULES']['0']['#']['SEQ_ROLLUPRULE'];
321 //Iterate over discussions
322 for($i = 0; $i < sizeof($rolluprules); $i++) {
323 $rol_info = $rolluprules[$i];
324 //Debug
326 //We'll need this later!!
327 $oldid = backup_todb($rol_info['#']['ID']['0']['#']);
328 //Now, build the FORUM_DISCUSSIONS record structure
329 $rolluprule->scoid = $sco_id;
330 $rolluprule->childactivityset = backup_todb($rol_info['#']['CHILDACTIVITYSET']['0']['#']);
331 $rolluprule->minimumrule = backup_todb($rol_info['#']['MINIMUMCOUNT']['0']['#']);
332 $rolluprule->minimumpercent = backup_todb($rol_info['#']['MINIMUMPERCENT']['0']['#']);
333 $rolluprule->conditioncombination = backup_todb($rol_info['#']['CONDITIONCOMBINATION']['0']['#']);
334 $rolluprule->action = backup_todb($rol_info['#']['ACTION']['0']['#']);
336 //The structure is equal to the db, so insert the forum_discussions
337 $newid = insert_record ("scorm_seq_rolluprule",$rolluprule);
339 if ($newid) {
340 //We have the newid, update backup_ids
341 backup_putid($restore->backup_unique_code,"scorm_seq_rolluprule", $oldid, $newid);
342 } else {
343 $status = false;
346 //Do some output
347 if (($i+1) % 50 == 0) {
348 if (!defined('RESTORE_SILENTLY')) {
349 echo ".";
350 if (($i+1) % 1000 == 0) {
351 echo "<br />";
354 backup_flush(300);
357 //Now restore the scorm_seq_mapinfo for each objective
359 $status = scorm_seq_rolluprulecond_restore_mods ($sco_id, $newid,$obj_info,$restore);
363 return $status;
366 function scorm_scoes_seq_ruleconds_restore_mods($sco_id,$info,$restore) {
368 global $CFG;
370 $status = true;
372 //Get the discussions array
373 $ruleconds = array();
375 if (!empty($info['MOD']['#']['SEQ_RULECONDS']['0']['#']['SEQ_RULECOND'])) {
376 $ruleconds = $info['MOD']['#']['SEQ_RULECONDS']['0']['#']['SEQ_RULECOND'];
379 //Iterate over discussions
380 for($i = 0; $i < sizeof($ruleconds); $i++) {
381 $rul_info = $ruleconds[$i];
382 //Debug
384 //We'll need this later!!
385 $oldid = backup_todb($rul_info['#']['ID']['0']['#']);
387 $rulecond->scoid = $sco_id;
388 $rulecond->conditioncombination = backup_todb($rul_info['#']['CONDITIONCOMBINATION']['0']['#']);
389 $rulecond->minimumpercent = backup_todb($rul_info['#']['RULETYPE']['0']['#']);
390 $rulecond->action = backup_todb($rul_info['#']['ACTION']['0']['#']);
392 //The structure is equal to the db, so insert the forum_discussions
393 $newid = insert_record ("scorm_seq_ruleconds",$rulecond);
395 if ($newid) {
396 //We have the newid, update backup_ids
397 backup_putid($restore->backup_unique_code,"scorm_seq_ruleconds", $oldid, $newid);
398 } else {
399 $status = false;
402 //Do some output
403 if (($i+1) % 50 == 0) {
404 if (!defined('RESTORE_SILENTLY')) {
405 echo ".";
406 if (($i+1) % 1000 == 0) {
407 echo "<br />";
410 backup_flush(300);
413 //Now restore the scorm_seq_mapinfo for each objective
415 $status = scorm_seq_rulecond_restore_mods ($sco_id, $newid,$obj_info,$restore);
419 return $status;
422 function scorm_scoes_seq_rulecond_restore_mods($sco_id,$rulecondid,$info,$restore) {
424 global $CFG;
426 $status = true;
428 //Get the discussions array
429 $rulecondsd = array();
431 if (!empty($info['MOD']['#']['SEQ_RULECOND_DATAS']['0']['#']['SEQ_RULECOND_DATA'])) {
432 $rulecondsd = $info['MOD']['#']['SEQ_RULECOND_DATAS']['0']['#']['SEQ_RULECOND_DATA'];
436 for($i = 0; $i < sizeof($rulecondsd); $i++) {
437 $ruld_info = $rulecondsd[$i];
438 //Debug
440 //We'll need this later!!
441 $oldid = backup_todb($rul_info['#']['ID']['0']['#']);
443 $rulecondd->scoid = $sco_id;
444 $rulecondd->ruleconditions = $rulecondid;
445 $rulecondd->refrencedobjective = backup_todb($ruld_info['#']['REFRENCEDOBJECTIVE']['0']['#']);
446 $rulecondd->measurethreshold = backup_todb($ruld_info['#']['MEASURETHRESHOLD']['0']['#']);
447 $rulecondd->operator = backup_todb($ruld_info['#']['OPERATOR']['0']['#']);
448 $rulecondd->cond = backup_todb($ruld_info['#']['COND']['0']['#']);
450 //The structure is equal to the db, so insert the forum_discussions
451 $newid = insert_record ("scorm_seq_rulecond",$rulecondd);
453 if ($newid) {
454 //We have the newid, update backup_ids
455 backup_putid($restore->backup_unique_code,"scorm_seq_rulecond", $oldid, $newid);
456 } else {
457 $status = false;
460 //Do some output
461 if (($i+1) % 50 == 0) {
462 if (!defined('RESTORE_SILENTLY')) {
463 echo ".";
464 if (($i+1) % 1000 == 0) {
465 echo "<br />";
468 backup_flush(300);
471 //Now restore the scorm_seq_mapinfo for each objective
475 return $status;
478 function scorm_scoes_seq_rolluprulecond_restore_mods($sco_id,$rolluprule,$info,$restore) {
480 global $CFG;
482 $status = true;
484 //Get the discussions array
485 $rollupruleconds = array();
487 if (!empty($info['MOD']['#']['SEQ_ROLLUPRULECONDS']['0']['#']['SEQ_ROLLUPRULECOND'])) {
488 $rollupruleconds = $info['MOD']['#']['SEQ_ROLLUPRULECONDS']['0']['#']['SEQ_ROLLUPRULECOND'];
492 for($i = 0; $i < sizeof($rollupruleconds); $i++) {
493 $rulc_info = $rollupruleconds[$i];
494 //Debug
496 //We'll need this later!!
497 $oldid = backup_todb($rulc_info['#']['ID']['0']['#']);
499 $rolluprulecond->scoid = $sco_id;
500 $rolluprulecond->ruleconditions = $rolluprule;
501 $rolluprulecond->cond = backup_todb($rulc_info['#']['COND']['0']['#']);
502 $rolluprulecond->operator = backup_todb($rulc_info['#']['OPERATOR']['0']['#']);
505 //The structure is equal to the db, so insert the forum_discussions
506 $newid = insert_record ("scorm_seq_rolluprulecond",$rolluprulecond);
508 if ($newid) {
509 //We have the newid, update backup_ids
510 backup_putid($restore->backup_unique_code,"scorm_seq_rolluprulecond", $oldid, $newid);
511 } else {
512 $status = false;
515 //Do some output
516 if (($i+1) % 50 == 0) {
517 if (!defined('RESTORE_SILENTLY')) {
518 echo ".";
519 if (($i+1) % 1000 == 0) {
520 echo "<br />";
523 backup_flush(300);
526 //Now restore the scorm_seq_mapinfo for each objective
530 return $status;
533 function scorm_scoes_seq_mapinfo_restore_mods($sco_id,$objectiveid,$info,$restore) {
535 global $CFG;
537 $status = true;
539 //Get the discussions array
540 $mapinfos = array();
541 //backward compatibility with old backups.
542 if (!empty($info['MOD']['#']['SEQ_MAPINFO']['0']['#']['SEQ_MAPINF'])) {
543 $mapinfos = $info['MOD']['#']['SEQ_MAPINFO']['0']['#']['SEQ_MAPINF'];
545 //correct way of getting data.
546 if (!empty($info['MOD']['#']['SEQ_MAPINFOS']['0']['#']['SEQ_MAPINFO'])) {
547 $mapinfos = $info['MOD']['#']['SEQ_MAPINFOS']['0']['#']['SEQ_MAPINFO'];
550 for($i = 0; $i < sizeof($mapinfos); $i++) {
551 $map_info = $mapinfos[$i];
552 //Debug
554 //We'll need this later!!
555 $oldid = backup_todb($map_info['#']['ID']['0']['#']);
557 $mapinfo->scoid = $sco_id;
558 $mapinfo->objectiveid = $ojectiveid;
559 $mapinfo->targetobjectiveid = backup_todb($map_info['#']['TARGETOBJECTIVEID']['0']['#']);
560 $mapinfo->readsatisfiedstatus = backup_todb($map_info['#']['READSATISFIEDSTATUS']['0']['#']);
561 $mapinfo->readnormalizedmeasure = backup_todb($map_info['#']['READNORMALIZEDMEASURE']['0']['#']);
562 $mapinfo->writesatisfiedstatus = backup_todb($map_info['#']['WRITESATISFIEDSTATUS']['0']['#']);
563 $mapinfo->writenormalizedmeasure = backup_todb($map_info['#']['WRITENORMALIZEDMEASURE']['0']['#']);
566 //The structure is equal to the db, so insert the forum_discussions
567 $newid = insert_record ("scorm_seq_mapinfo",$mapinfo);
569 if ($newid) {
570 //We have the newid, update backup_ids
571 backup_putid($restore->backup_unique_code,"scorm_seq_mapinfo", $oldid, $newid);
572 } else {
573 $status = false;
576 //Do some output
577 if (($i+1) % 50 == 0) {
578 if (!defined('RESTORE_SILENTLY')) {
579 echo ".";
580 if (($i+1) % 1000 == 0) {
581 echo "<br />";
587 return $status;
590 //This function restores the scorm_scoes_track
591 function scorm_scoes_tracks_restore_mods($scorm_id,$info,$restore) {
593 global $CFG;
595 $status = true;
596 $scotracks = NULL;
598 //Get the sco array
599 if (!empty($info['MOD']['#']['SCO_TRACKS']['0']['#']['SCO_TRACK']))
600 $scotracks = $info['MOD']['#']['SCO_TRACKS']['0']['#']['SCO_TRACK'];
602 //Iterate over sco_users
603 for($i = 0; $i < sizeof($scotracks); $i++) {
604 $sub_info = $scotracks[$i];
605 unset($scotrack);
607 //Now, build the scorm_scoes_track record structure
608 $scotrack->scormid = $scorm_id;
609 $scotrack->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
610 $scotrack->scoid = backup_todb($sub_info['#']['SCOID']['0']['#']);
611 $scotrack->element = backup_todb($sub_info['#']['ELEMENT']['0']['#']);
612 $scotrack->attempt = backup_todb($sub_info['#']['ATTEMPT']['0']['#']);
613 $scotrack->value = backup_todb($sub_info['#']['VALUE']['0']['#']);
615 //We have to recode the userid field
616 $user = backup_getid($restore->backup_unique_code,"user",$scotrack->userid);
617 if (!empty($user)) {
618 $scotrack->userid = $user->new_id;
621 //We have to recode the scoid field
622 $sco = backup_getid($restore->backup_unique_code,"scorm_scoes",$scotrack->scoid);
623 if ($sco != NULL) {
624 $scotrack->scoid = $sco->new_id;
627 $scotrack->timemodified = time();
628 //The structure is equal to the db, so insert the scorm_scoes_track
629 $newid = insert_record ("scorm_scoes_track",$scotrack);
631 //Do some output
632 if (($i+1) % 50 == 0) {
633 if (!defined('RESTORE_SILENTLY')) {
634 echo ".";
635 if (($i+1) % 1000 == 0) {
636 echo "<br />";
639 backup_flush(300);
644 return $status;
647 //This function restores the scorm_scoes_track from Moodle 1.4
648 function scorm_scoes_tracks_restore_mods_pre15 ($scorm_id,$info,$restore) {
650 global $CFG;
652 $status = true;
653 $scousers = NULL;
655 //Get the sco array
656 if (!empty($info['MOD']['#']['SCO_USERS']['0']['#']['SCO_USER'])) {
657 $scousers = $info['MOD']['#']['SCO_USERS']['0']['#']['SCO_USER'];
660 $oldelements = array ('CMI_CORE_LESSON_LOCATION',
661 'CMI_CORE_LESSON_STATUS',
662 'CMI_CORE_EXIT',
663 'CMI_CORE_TOTAL_TIME',
664 'CMI_CORE_SCORE_RAW',
665 'CMI_SUSPEND_DATA');
666 $newelements = array ('cmi.core.lesson_location',
667 'cmi.core.lesson_status',
668 'cmi.core.exit',
669 'cmi.core.total_time',
670 'cmi.core.score.raw',
671 'cmi.suspend_data');
673 //Iterate over sco_users
674 for ($i = 0; $i < sizeof($scousers); $i++) {
675 $sub_info = $scousers[$i];
676 unset($scotrack);
678 //We'll need this later!!
679 $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
680 $oldscoid = backup_todb($sub_info['#']['SCOID']['0']['#']);
681 $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
683 //Now, build the scorm_scoes_track record structure
684 $scotrack->scormid = $scorm_id;
685 $scotrack->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
686 $scotrack->scoid = backup_todb($sub_info['#']['SCOID']['0']['#']);
687 $pos = 0;
688 foreach ($oldelements as $oldelement) {
689 $elementvalue = backup_todb($sub_info['#'][$oldelement]['0']['#']);
690 if (!empty($elementvalue)) {
691 $scotrack->element = $newelements[$pos];
692 $scotrack->value = backup_todb($sub_info['#'][strtoupper($oldelement)]['0']['#']);
694 //We have to recode the userid field
695 $user = backup_getid($restore->backup_unique_code,"user",$scotrack->userid);
696 if (!empty($user)) {
697 $scotrack->userid = $user->new_id;
700 //We have to recode the scoid field
701 $sco = backup_getid($restore->backup_unique_code,"scorm_scoes",$scotrack->scoid);
702 if (!empty($sco)) {
703 $scotrack->scoid = $sco->new_id;
706 //The structure is equal to the db, so insert the scorm_scoes_track
707 $newid = insert_record ("scorm_scoes_track",$scotrack);
709 $pos++;
712 //Do some output
713 if (($i+1) % 50 == 0) {
714 if (!defined('RESTORE_SILENTLY')) {
715 echo ".";
716 if (($i+1) % 1000 == 0) {
717 echo "<br />";
720 backup_flush(300);
725 return $status;
728 //This function copies the scorm related info from backup temp dir to course moddata folder,
729 //creating it if needed
730 function scorm_restore_files ($package, $restore) {
732 global $CFG;
734 $status = true;
735 $todo = false;
736 $moddata_path = "";
737 $scorm_path = "";
738 $temp_path = "";
740 //First, we check to "course_id" exists and create is as necessary
741 //in CFG->dataroot
742 $dest_dir = $CFG->dataroot."/".$restore->course_id;
743 $status = check_dir_exists($dest_dir,true);
745 //First, locate course's moddata directory
746 $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
748 //Check it exists and create it
749 $status = check_dir_exists($moddata_path,true);
751 //Now, locate scorm directory
752 if ($status) {
753 $scorm_path = $moddata_path."/scorm";
754 //Check it exists and create it
755 $status = check_dir_exists($scorm_path,true);
758 //Now locate the temp dir we are restoring from
759 if ($status) {
760 $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
761 "/moddata/scorm/".$package->datadir;
762 //Check it exists
763 if (is_dir($temp_path)) {
764 $todo = true;
768 //If todo, we create the neccesary dirs in course moddata/scorm
769 if ($status and $todo) {
770 //Make scorm package directory path
771 $this_scorm_path = $scorm_path."/".$package->id;
772 $status = backup_copy_file($temp_path, $this_scorm_path);
775 return $status;
778 //Return a content decoded to support interactivities linking. Every module
779 //should have its own. They are called automatically from
780 //scorm_decode_content_links_caller() function in each module
781 //in the restore process
782 function scorm_decode_content_links ($content,$restore) {
784 global $CFG;
786 $result = $content;
788 //Link to the list of scorms
790 $searchstring='/\$@(SCORMINDEX)\*([0-9]+)@\$/';
791 //We look for it
792 preg_match_all($searchstring,$content,$foundset);
793 //If found, then we are going to look for its new id (in backup tables)
794 if ($foundset[0]) {
795 //print_object($foundset); //Debug
796 //Iterate over foundset[2]. They are the old_ids
797 foreach($foundset[2] as $old_id) {
798 //We get the needed variables here (course id)
799 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
800 //Personalize the searchstring
801 $searchstring='/\$@(SCORMINDEX)\*('.$old_id.')@\$/';
802 //If it is a link to this course, update the link to its new location
803 if($rec->new_id) {
804 //Now replace it
805 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/scorm/index.php?id='.$rec->new_id,$result);
806 } else {
807 //It's a foreign link so leave it as original
808 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/scorm/index.php?id='.$old_id,$result);
813 //Link to scorm view by moduleid
815 $searchstring='/\$@(SCORMVIEWBYID)\*([0-9]+)@\$/';
816 //We look for it
817 preg_match_all($searchstring,$result,$foundset);
818 //If found, then we are going to look for its new id (in backup tables)
819 if ($foundset[0]) {
820 //print_object($foundset); //Debug
821 //Iterate over foundset[2]. They are the old_ids
822 foreach($foundset[2] as $old_id) {
823 //We get the needed variables here (course_modules id)
824 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
825 //Personalize the searchstring
826 $searchstring='/\$@(SCORMVIEWBYID)\*('.$old_id.')@\$/';
827 //If it is a link to this course, update the link to its new location
828 if($rec->new_id) {
829 //Now replace it
830 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/scorm/view.php?id='.$rec->new_id,$result);
831 } else {
832 //It's a foreign link so leave it as original
833 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/scorm/view.php?id='.$old_id,$result);
838 return $result;
841 //This function makes all the necessary calls to xxxx_decode_content_links()
842 //function in each module, passing them the desired contents to be decoded
843 //from backup format to destination site/course in order to mantain inter-activities
844 //working in the backup/restore process. It's called from restore_decode_content_links()
845 //function in restore process
846 function scorm_decode_content_links_caller($restore) {
847 global $CFG;
848 $status = true;
850 if ($scorms = get_records_sql ("SELECT s.id, s.summary
851 FROM {$CFG->prefix}scorm s
852 WHERE s.course = $restore->course_id")) {
854 $i = 0; //Counter to send some output to the browser to avoid timeouts
855 foreach ($scorms as $scorm) {
856 //Increment counter
857 $i++;
858 $content = $scorm->summary;
859 $result = restore_decode_content_links_worker($content,$restore);
861 if ($result != $content) {
862 //Update record
863 $scorm->summary = addslashes_js($result);
864 $status = update_record("scorm",$scorm);
865 if (debugging()) {
866 if (!defined('RESTORE_SILENTLY')) {
867 echo '<br /><hr />'.htmlentities($content).'<br />changed to<br />'.htmlentities($result).'<hr /><br />';
871 //Do some output
872 if (($i+1) % 5 == 0) {
873 if (!defined('RESTORE_SILENTLY')) {
874 echo ".";
875 if (($i+1) % 100 == 0) {
876 echo "<br />";
879 backup_flush(300);
883 return $status;
886 //This function returns a log record with all the necessay transformations
887 //done. It's used by restore_log_module() to restore modules log.
888 function scorm_restore_logs($restore,$log) {
890 $status = true;
892 return $status;