MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / scorm / restorelib.php
blob0c6c4863703996cd2d842bb798c5cd3b325bca6f
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 = backup_todb($info['MOD']['#']['MAXGRADE']['0']['#']);
53 if (!is_int($scorm->maxgrade)) {
54 $scorm->maxgrade = 0;
56 $scorm->updatefreq = backup_todb($info['MOD']['#']['UPDATEFREQ']['0']['#']);
57 if (!is_int($scorm->updatefreq)) {
58 $scorm->updatefreq = 0;
60 $scorm->maxattempt = backup_todb($info['MOD']['#']['MAXATTEMPT']['0']['#']);
61 if (!is_int($scorm->maxattempt)) {
62 $scorm->maxattempt = 0;
64 $scorm->grademethod = backup_todb($info['MOD']['#']['GRADEMETHOD']['0']['#']);
65 if (!is_int($scorm->grademethod)) {
66 $scorm->grademethod = 0;
68 if ($restore->backup_version < 2005041500) {
69 $scorm->datadir = substr(backup_todb($info['MOD']['#']['DATADIR']['0']['#']),1);
70 } else {
71 $scorm->datadir = backup_todb($info['MOD']['#']['ID']['0']['#']);
73 $oldlaunch = backup_todb($info['MOD']['#']['LAUNCH']['0']['#']);
74 if ($restore->backup_version < 2006102600) {
75 $scorm->skipview = 1;
76 } else {
77 $scorm->skipview = backup_todb($info['MOD']['#']['SKIPVIEW']['0']['#']);
79 $scorm->summary = backup_todb($info['MOD']['#']['SUMMARY']['0']['#']);
80 $scorm->hidebrowse = backup_todb($info['MOD']['#']['HIDEBROWSE']['0']['#']);
81 $scorm->hidetoc = backup_todb($info['MOD']['#']['HIDETOC']['0']['#']);
82 $scorm->hidenav = backup_todb($info['MOD']['#']['HIDENAV']['0']['#']);
83 $scorm->auto = backup_todb($info['MOD']['#']['AUTO']['0']['#']);
84 if ($restore->backup_version < 2005040200) {
85 $oldpopup = backup_todb($info['MOD']['#']['POPUP']['0']['#']);
86 if (!empty($oldpopup)) {
87 $scorm->popup = 1;
88 // Parse old popup field
89 $options = array();
90 $oldoptions = explode(',',$scorm->popup);
91 foreach ($oldoptions as $oldoption) {
92 list($element,$value) = explode('=',$oldoption);
93 $element = trim($element);
94 $value = trim($value);
95 switch ($element) {
96 case 'width':
97 $scorm->width = $value;
98 break;
99 case 'height':
100 $scorm->height = $value;
101 break;
102 default:
103 $options[] = $element.'='.$value;
104 break;
107 $scorm->options = implode($options,',');
108 } else {
109 $scorm->popup = 0;
110 $scorm->options = '';
111 $scorm->width = '100%';
112 $scorm->height = 500;
114 } else {
115 $scorm->popup = backup_todb($info['MOD']['#']['POPUP']['0']['#']);
116 $scorm->width = backup_todb($info['MOD']['#']['WIDTH']['0']['#']);
117 if ($scorm->width == 0) {
118 $scorm->width = '100%';
120 $scorm->height = backup_todb($info['MOD']['#']['HEIGHT']['0']['#']);
121 if ($scorm->height == 0) {
122 $scorm->height = 500;
125 $scorm->timemodified = time();
127 //The structure is equal to the db, so insert the scorm
128 $newid = insert_record ("scorm",$scorm);
129 //Do some output
130 if (!defined('RESTORE_SILENTLY')) {
131 echo "<li>".get_string("modulename","scorm")." \"".format_string(stripslashes($scorm->name),true)."\"</li>";
133 backup_flush(300);
135 if ($newid) {
136 //We have the newid, update backup_ids
137 backup_putid($restore->backup_unique_code,$mod->modtype,
138 $mod->id, $newid);
139 $scorm->id = $newid;
140 //Now copy moddata associated files
141 $status = scorm_restore_files ($scorm, $restore);
143 if ($status) {
144 $status = scorm_scoes_restore_mods ($newid,$info,$restore,$mod->id);
145 if ($status) {
146 $launchsco = backup_getid($restore->backup_unique_code,"scorm_scoes",$oldlaunch);
147 $scorm->launch = $launchsco->new_id;
148 update_record('scorm',$scorm);
152 } else {
153 $status = false;
155 } else {
156 $status = false;
159 return $status;
162 //This function restores the scorm_scoes
163 function scorm_scoes_restore_mods($scorm_id,$info,$restore,$oldmodid) {
165 global $CFG;
167 $status = true;
169 //Get the sco array
170 $scoes = $info['MOD']['#']['SCOES']['0']['#']['SCO'];
172 //Iterate over scoes
173 for($i = 0; $i < sizeof($scoes); $i++) {
174 $sub_info = $scoes[$i];
176 //We'll need this later!!
177 $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
179 //Now, build the scorm_scoes record structure
180 $sco->scorm = $scorm_id;
181 $sco->manifest = backup_todb($sub_info['#']['MANIFEST']['0']['#']);
182 $sco->organization = backup_todb($sub_info['#']['ORGANIZATION']['0']['#']);
183 $sco->parent = backup_todb($sub_info['#']['PARENT']['0']['#']);
184 $sco->identifier = backup_todb($sub_info['#']['IDENTIFIER']['0']['#']);
185 $sco->launch = backup_todb($sub_info['#']['LAUNCH']['0']['#']);
186 $sco->title = backup_todb($sub_info['#']['TITLE']['0']['#']);
187 if ($restore->backup_version < 2005031300) {
188 $sco->scormtype = backup_todb($sub_info['#']['TYPE']['0']['#']);
189 } else {
190 $sco->scormtype = backup_todb($sub_info['#']['SCORMTYPE']['0']['#']);
193 //The structure is equal to the db, so insert the scorm_scoes
194 $newid = insert_record ("scorm_scoes",$sco);
196 if ($newid) {
197 //We have the newid, update backup_ids
198 backup_putid($restore->backup_unique_code,"scorm_scoes", $oldid, $newid);
199 } else {
200 $status = false;
204 //Now check if want to restore user data and do it.
205 scorm_scoes_seq_objective_restore_mods ($newid,$info,$restore);
206 scorm_scoes_seq_rolluprule_restore_mods ($newid,$info,$restore);
207 scorm_scoes_seq_ruleconds_restore_mods ($newid,$info,$restore);
208 if (restore_userdata_selected($restore,'scorm',$oldmodid)) {
209 //Restore scorm_scoes
210 if ($status) {
211 if ($restore->backup_version < 2005031300) {
212 $status = scorm_scoes_tracks_restore_mods_pre15 ($scorm_id,$info,$restore);
213 } else {
214 $status = scorm_scoes_tracks_restore_mods ($scorm_id,$info,$restore);
219 return $status;
223 function scorm_scoes_seq_objective_restore_mods($sco_id,$info,$restore) {
225 global $CFG;
227 $status = true;
229 //Get the discussions array
230 $objectives = array();
232 if (!empty($info['MOD']['#']['SEQ_OBJECTIVES']['0']['#']['SEQ_OBJECTIVE'])) {
233 $objectives = $info['MOD']['#']['SEQ_OBJECTIVES']['0']['#']['SEQ_OBJECTIVE'];
236 //Iterate over discussions
237 for($i = 0; $i < sizeof($objectives); $i++) {
238 $obj_info = $objectives[$i];
239 //Debug
241 //We'll need this later!!
242 $oldid = backup_todb($obj_info['#']['ID']['0']['#']);
243 //Now, build the FORUM_DISCUSSIONS record structure
244 $objective->scoid = $sco_id;
245 $objective->primaryobj = backup_todb($obj_info['#']['PRIMARYOBJ']['0']['#']);
246 $objective->objectiveid = backup_todb($obj_info['#']['OBJECTIVEID']['0']['#']);
247 $objective->satisfiedbymeasure = backup_todb($obj_info['#']['SATISFIEDBYMEASURE']['0']['#']);
248 $objective->minnormalizedmeasure = backup_todb($obj_info['#']['MINNORMALIZEDMEASURE']['0']['#']);
250 //The structure is equal to the db, so insert the forum_discussions
251 $newid = insert_record ("scorm_seq_objective",$objective);
253 if ($newid) {
254 //We have the newid, update backup_ids
255 backup_putid($restore->backup_unique_code,"scorm_seq_objective", $oldid, $newid);
256 } else {
257 $status = false;
260 //Do some output
261 if (($i+1) % 50 == 0) {
262 if (!defined('RESTORE_SILENTLY')) {
263 echo ".";
264 if (($i+1) % 1000 == 0) {
265 echo "<br />";
268 backup_flush(300);
271 //Now restore the scorm_seq_mapinfo for each objective
273 $status = scorm_seq_mapinfo_restore_mods ($sco_id,$newid,$obj_info,$restore);
277 return $status;
282 function scorm_scoes_seq_rolluprule_restore_mods($sco_id,$info,$restore) {
284 global $CFG;
286 $status = true;
288 //Get the discussions array
289 $rolluprules = array();
291 if (!empty($info['MOD']['#']['SEQ_ROLLUPRULES']['0']['#']['SEQ_ROLLUPRULE'])) {
292 $rolluprules = $info['MOD']['#']['SEQ_ROLLUPRULES']['0']['#']['SEQ_ROLLUPRULE'];
295 //Iterate over discussions
296 for($i = 0; $i < sizeof($rolluprules); $i++) {
297 $rol_info = $rolluprules[$i];
298 //Debug
300 //We'll need this later!!
301 $oldid = backup_todb($rol_info['#']['ID']['0']['#']);
302 //Now, build the FORUM_DISCUSSIONS record structure
303 $rolluprule->scoid = $sco_id;
304 $rolluprule->childactivityset = backup_todb($rol_info['#']['CHILDACTIVITYSET']['0']['#']);
305 $rolluprule->minimumrule = backup_todb($rol_info['#']['MINIMUMCOUNT']['0']['#']);
306 $rolluprule->minimumpercent = backup_todb($rol_info['#']['MINIMUMPERCENT']['0']['#']);
307 $rolluprule->conditioncombination = backup_todb($rol_info['#']['CONDITIONCOMBINATION']['0']['#']);
308 $rolluprule->action = backup_todb($rol_info['#']['ACTION']['0']['#']);
310 //The structure is equal to the db, so insert the forum_discussions
311 $newid = insert_record ("scorm_seq_rolluprule",$rolluprule);
313 if ($newid) {
314 //We have the newid, update backup_ids
315 backup_putid($restore->backup_unique_code,"scorm_seq_rolluprule", $oldid, $newid);
316 } else {
317 $status = false;
320 //Do some output
321 if (($i+1) % 50 == 0) {
322 if (!defined('RESTORE_SILENTLY')) {
323 echo ".";
324 if (($i+1) % 1000 == 0) {
325 echo "<br />";
328 backup_flush(300);
331 //Now restore the scorm_seq_mapinfo for each objective
333 $status = scorm_seq_rolluprulecond_restore_mods ($sco_id, $newid,$obj_info,$restore);
337 return $status;
340 function scorm_scoes_seq_ruleconds_restore_mods($sco_id,$info,$restore) {
342 global $CFG;
344 $status = true;
346 //Get the discussions array
347 $ruleconds = array();
349 if (!empty($info['MOD']['#']['SEQ_RULECONDS']['0']['#']['SEQ_RULECOND'])) {
350 $ruleconds = $info['MOD']['#']['SEQ_RULECONDS']['0']['#']['SEQ_RULECOND'];
353 //Iterate over discussions
354 for($i = 0; $i < sizeof($ruleconds); $i++) {
355 $rul_info = $ruleconds[$i];
356 //Debug
358 //We'll need this later!!
359 $oldid = backup_todb($rul_info['#']['ID']['0']['#']);
361 $rulecond->scoid = $sco_id;
362 $rulecond->conditioncombination = backup_todb($rul_info['#']['CONDITIONCOMBINATION']['0']['#']);
363 $rulecond->minimumpercent = backup_todb($rul_info['#']['RULETYPE']['0']['#']);
364 $rulecond->action = backup_todb($rul_info['#']['ACTION']['0']['#']);
366 //The structure is equal to the db, so insert the forum_discussions
367 $newid = insert_record ("scorm_seq_ruleconds",$rulecond);
369 if ($newid) {
370 //We have the newid, update backup_ids
371 backup_putid($restore->backup_unique_code,"scorm_seq_ruleconds", $oldid, $newid);
372 } else {
373 $status = false;
376 //Do some output
377 if (($i+1) % 50 == 0) {
378 if (!defined('RESTORE_SILENTLY')) {
379 echo ".";
380 if (($i+1) % 1000 == 0) {
381 echo "<br />";
384 backup_flush(300);
387 //Now restore the scorm_seq_mapinfo for each objective
389 $status = scorm_seq_rulecond_restore_mods ($sco_id, $newid,$obj_info,$restore);
393 return $status;
396 function scorm_scoes_seq_rulecond_restore_mods($sco_id,$rulecondid,$info,$restore) {
398 global $CFG;
400 $status = true;
402 //Get the discussions array
403 $rulecondsd = array();
405 if (!empty($info['MOD']['#']['SEQ_RULECOND_DATAS']['0']['#']['SEQ_RULECOND_DATA'])) {
406 $rulecondsd = $info['MOD']['#']['SEQ_RULECOND_DATAS']['0']['#']['SEQ_RULECOND_DATA'];
410 for($i = 0; $i < sizeof($rulecondsd); $i++) {
411 $ruld_info = $rulecondsd[$i];
412 //Debug
414 //We'll need this later!!
415 $oldid = backup_todb($rul_info['#']['ID']['0']['#']);
417 $rulecondd->scoid = $sco_id;
418 $rulecondd->ruleconditions = $rulecondid;
419 $rulecondd->refrencedobjective = backup_todb($ruld_info['#']['REFRENCEDOBJECTIVE']['0']['#']);
420 $rulecondd->measurethreshold = backup_todb($ruld_info['#']['MEASURETHRESHOLD']['0']['#']);
421 $rulecondd->operator = backup_todb($ruld_info['#']['OPERATOR']['0']['#']);
422 $rulecondd->cond = backup_todb($ruld_info['#']['COND']['0']['#']);
424 //The structure is equal to the db, so insert the forum_discussions
425 $newid = insert_record ("scorm_seq_rulecond",$rulecondd);
427 if ($newid) {
428 //We have the newid, update backup_ids
429 backup_putid($restore->backup_unique_code,"scorm_seq_rulecond", $oldid, $newid);
430 } else {
431 $status = false;
434 //Do some output
435 if (($i+1) % 50 == 0) {
436 if (!defined('RESTORE_SILENTLY')) {
437 echo ".";
438 if (($i+1) % 1000 == 0) {
439 echo "<br />";
442 backup_flush(300);
445 //Now restore the scorm_seq_mapinfo for each objective
449 return $status;
452 function scorm_scoes_seq_rolluprulecond_restore_mods($sco_id,$rolluprule,$info,$restore) {
454 global $CFG;
456 $status = true;
458 //Get the discussions array
459 $rollupruleconds = array();
461 if (!empty($info['MOD']['#']['SEQ_ROLLUPRULECONDS']['0']['#']['SEQ_ROLLUPRULECOND'])) {
462 $rollupruleconds = $info['MOD']['#']['SEQ_ROLLUPRULECONDS']['0']['#']['SEQ_ROLLUPRULECOND'];
466 for($i = 0; $i < sizeof($rollupruleconds); $i++) {
467 $rulc_info = $rollupruleconds[$i];
468 //Debug
470 //We'll need this later!!
471 $oldid = backup_todb($rulc_info['#']['ID']['0']['#']);
473 $rolluprulecond->scoid = $sco_id;
474 $rolluprulecond->ruleconditions = $rolluprule;
475 $rolluprulecond->cond = backup_todb($rulc_info['#']['COND']['0']['#']);
476 $rolluprulecond->operator = backup_todb($rulc_info['#']['OPERATOR']['0']['#']);
479 //The structure is equal to the db, so insert the forum_discussions
480 $newid = insert_record ("scorm_seq_rolluprulecond",$rolluprulecond);
482 if ($newid) {
483 //We have the newid, update backup_ids
484 backup_putid($restore->backup_unique_code,"scorm_seq_rolluprulecond", $oldid, $newid);
485 } else {
486 $status = false;
489 //Do some output
490 if (($i+1) % 50 == 0) {
491 if (!defined('RESTORE_SILENTLY')) {
492 echo ".";
493 if (($i+1) % 1000 == 0) {
494 echo "<br />";
497 backup_flush(300);
500 //Now restore the scorm_seq_mapinfo for each objective
504 return $status;
507 function scorm_scoes_seq_mapinfo_restore_mods($sco_id,$objectiveid,$info,$restore) {
509 global $CFG;
511 $status = true;
513 //Get the discussions array
514 $mapinfos = array();
516 if (!empty($info['MOD']['#']['SEQ_MAPINFO']['0']['#']['SEQ_MAPINF'])) {
517 $mapinfos = $info['MOD']['#']['SEQ_MAPINFO']['0']['#']['SEQ_MAPINF'];
521 for($i = 0; $i < sizeof($mapinfos); $i++) {
522 $map_info = $mapinfos[$i];
523 //Debug
525 //We'll need this later!!
526 $oldid = backup_todb($map_info['#']['ID']['0']['#']);
528 $mapinfo->scoid = $sco_id;
529 $mapinfo->objectiveid = $ojectiveid;
530 $mapinfo->targetobjectiveid = backup_todb($map_info['#']['TARGETOBJECTIVEID']['0']['#']);
531 $mapinfo->readsatisfiedstatus = backup_todb($map_info['#']['READSATISFIEDSTATUS']['0']['#']);
532 $mapinfo->readnormalizedmeasure = backup_todb($map_info['#']['READNORMALIZEDMEASURE']['0']['#']);
533 $mapinfo->writesatisfiedstatus = backup_todb($map_info['#']['WRITESATISFIEDSTATUS']['0']['#']);
534 $mapinfo->writenormalizedmeasure = backup_todb($map_info['#']['WRITENORMALIZEDMEASURE']['0']['#']);
537 //The structure is equal to the db, so insert the forum_discussions
538 $newid = insert_record ("scorm_seq_mapinfo",$mapinfo);
540 if ($newid) {
541 //We have the newid, update backup_ids
542 backup_putid($restore->backup_unique_code,"scorm_seq_mapinfo", $oldid, $newid);
543 } else {
544 $status = false;
547 //Do some output
548 if (($i+1) % 50 == 0) {
549 if (!defined('RESTORE_SILENTLY')) {
550 echo ".";
551 if (($i+1) % 1000 == 0) {
552 echo "<br />";
558 return $status;
561 //This function restores the scorm_scoes_track
562 function scorm_scoes_tracks_restore_mods($scorm_id,$info,$restore) {
564 global $CFG;
566 $status = true;
567 $scotracks = NULL;
569 //Get the sco array
570 if (!empty($info['MOD']['#']['SCO_TRACKS']['0']['#']['SCO_TRACK']))
571 $scotracks = $info['MOD']['#']['SCO_TRACKS']['0']['#']['SCO_TRACK'];
573 //Iterate over sco_users
574 for($i = 0; $i < sizeof($scotracks); $i++) {
575 $sub_info = $scotracks[$i];
576 unset($scotrack);
578 //Now, build the scorm_scoes_track record structure
579 $scotrack->scormid = $scorm_id;
580 $scotrack->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
581 $scotrack->scoid = backup_todb($sub_info['#']['SCOID']['0']['#']);
582 $scotrack->element = backup_todb($sub_info['#']['ELEMENT']['0']['#']);
583 $scotrack->attempt = backup_todb($sub_info['#']['ATTEMPT']['0']['#']);
584 $scotrack->value = backup_todb($sub_info['#']['VALUE']['0']['#']);
586 //We have to recode the userid field
587 $user = backup_getid($restore->backup_unique_code,"user",$scotrack->userid);
588 if (!empty($user)) {
589 $scotrack->userid = $user->new_id;
592 //We have to recode the scoid field
593 $sco = backup_getid($restore->backup_unique_code,"scorm_scoes",$scotrack->scoid);
594 if ($sco != NULL) {
595 $scotrack->scoid = $sco->new_id;
598 $scotrack->timemodified = time();
599 //The structure is equal to the db, so insert the scorm_scoes_track
600 $newid = insert_record ("scorm_scoes_track",$scotrack);
602 //Do some output
603 if (($i+1) % 50 == 0) {
604 if (!defined('RESTORE_SILENTLY')) {
605 echo ".";
606 if (($i+1) % 1000 == 0) {
607 echo "<br />";
610 backup_flush(300);
615 return $status;
618 //This function restores the scorm_scoes_track from Moodle 1.4
619 function scorm_scoes_tracks_restore_mods_pre15 ($scorm_id,$info,$restore) {
621 global $CFG;
623 $status = true;
624 $scousers = NULL;
626 //Get the sco array
627 if (!empty($info['MOD']['#']['SCO_USERS']['0']['#']['SCO_USER'])) {
628 $scousers = $info['MOD']['#']['SCO_USERS']['0']['#']['SCO_USER'];
631 $oldelements = array ('CMI_CORE_LESSON_LOCATION',
632 'CMI_CORE_LESSON_STATUS',
633 'CMI_CORE_EXIT',
634 'CMI_CORE_TOTAL_TIME',
635 'CMI_CORE_SCORE_RAW',
636 'CMI_SUSPEND_DATA');
637 $newelements = array ('cmi.core.lesson_location',
638 'cmi.core.lesson_status',
639 'cmi.core.exit',
640 'cmi.core.total_time',
641 'cmi.core.score.raw',
642 'cmi.suspend_data');
644 //Iterate over sco_users
645 for ($i = 0; $i < sizeof($scousers); $i++) {
646 $sub_info = $scousers[$i];
647 unset($scotrack);
649 //We'll need this later!!
650 $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
651 $oldscoid = backup_todb($sub_info['#']['SCOID']['0']['#']);
652 $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
654 //Now, build the scorm_scoes_track record structure
655 $scotrack->scormid = $scorm_id;
656 $scotrack->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
657 $scotrack->scoid = backup_todb($sub_info['#']['SCOID']['0']['#']);
658 $pos = 0;
659 foreach ($oldelements as $oldelement) {
660 $elementvalue = backup_todb($sub_info['#'][$oldelement]['0']['#']);
661 if (!empty($elementvalue)) {
662 $scotrack->element = $newelements[$pos];
663 $scotrack->value = backup_todb($sub_info['#'][strtoupper($oldelement)]['0']['#']);
665 //We have to recode the userid field
666 $user = backup_getid($restore->backup_unique_code,"user",$scotrack->userid);
667 if (!empty($user)) {
668 $scotrack->userid = $user->new_id;
671 //We have to recode the scoid field
672 $sco = backup_getid($restore->backup_unique_code,"scorm_scoes",$scotrack->scoid);
673 if (!empty($sco)) {
674 $scotrack->scoid = $sco->new_id;
677 //The structure is equal to the db, so insert the scorm_scoes_track
678 $newid = insert_record ("scorm_scoes_track",$scotrack);
680 $pos++;
683 //Do some output
684 if (($i+1) % 50 == 0) {
685 if (!defined('RESTORE_SILENTLY')) {
686 echo ".";
687 if (($i+1) % 1000 == 0) {
688 echo "<br />";
691 backup_flush(300);
696 return $status;
699 //This function copies the scorm related info from backup temp dir to course moddata folder,
700 //creating it if needed
701 function scorm_restore_files ($package, $restore) {
703 global $CFG;
705 $status = true;
706 $todo = false;
707 $moddata_path = "";
708 $scorm_path = "";
709 $temp_path = "";
711 //First, we check to "course_id" exists and create is as necessary
712 //in CFG->dataroot
713 $dest_dir = $CFG->dataroot."/".$restore->course_id;
714 $status = check_dir_exists($dest_dir,true);
716 //First, locate course's moddata directory
717 $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
719 //Check it exists and create it
720 $status = check_dir_exists($moddata_path,true);
722 //Now, locate scorm directory
723 if ($status) {
724 $scorm_path = $moddata_path."/scorm";
725 //Check it exists and create it
726 $status = check_dir_exists($scorm_path,true);
729 //Now locate the temp dir we are restoring from
730 if ($status) {
731 $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
732 "/moddata/scorm/".$package->datadir;
733 //Check it exists
734 if (is_dir($temp_path)) {
735 $todo = true;
739 //If todo, we create the neccesary dirs in course moddata/scorm
740 if ($status and $todo) {
741 //Make scorm package directory path
742 $this_scorm_path = $scorm_path."/".$package->id;
743 $status = backup_copy_file($temp_path, $this_scorm_path);
746 return $status;
749 //Return a content decoded to support interactivities linking. Every module
750 //should have its own. They are called automatically from
751 //scorm_decode_content_links_caller() function in each module
752 //in the restore process
753 function scorm_decode_content_links ($content,$restore) {
755 global $CFG;
757 $result = $content;
759 //Link to the list of scorms
761 $searchstring='/\$@(SCORMINDEX)\*([0-9]+)@\$/';
762 //We look for it
763 preg_match_all($searchstring,$content,$foundset);
764 //If found, then we are going to look for its new id (in backup tables)
765 if ($foundset[0]) {
766 //print_object($foundset); //Debug
767 //Iterate over foundset[2]. They are the old_ids
768 foreach($foundset[2] as $old_id) {
769 //We get the needed variables here (course id)
770 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
771 //Personalize the searchstring
772 $searchstring='/\$@(SCORMINDEX)\*('.$old_id.')@\$/';
773 //If it is a link to this course, update the link to its new location
774 if($rec->new_id) {
775 //Now replace it
776 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/scorm/index.php?id='.$rec->new_id,$result);
777 } else {
778 //It's a foreign link so leave it as original
779 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/scorm/index.php?id='.$old_id,$result);
784 //Link to scorm view by moduleid
786 $searchstring='/\$@(SCORMVIEWBYID)\*([0-9]+)@\$/';
787 //We look for it
788 preg_match_all($searchstring,$result,$foundset);
789 //If found, then we are going to look for its new id (in backup tables)
790 if ($foundset[0]) {
791 //print_object($foundset); //Debug
792 //Iterate over foundset[2]. They are the old_ids
793 foreach($foundset[2] as $old_id) {
794 //We get the needed variables here (course_modules id)
795 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
796 //Personalize the searchstring
797 $searchstring='/\$@(SCORMVIEWBYID)\*('.$old_id.')@\$/';
798 //If it is a link to this course, update the link to its new location
799 if($rec->new_id) {
800 //Now replace it
801 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/scorm/view.php?id='.$rec->new_id,$result);
802 } else {
803 //It's a foreign link so leave it as original
804 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/scorm/view.php?id='.$old_id,$result);
809 return $result;
812 //This function makes all the necessary calls to xxxx_decode_content_links()
813 //function in each module, passing them the desired contents to be decoded
814 //from backup format to destination site/course in order to mantain inter-activities
815 //working in the backup/restore process. It's called from restore_decode_content_links()
816 //function in restore process
817 function scorm_decode_content_links_caller($restore) {
818 global $CFG;
819 $status = true;
821 if ($scorms = get_records_sql ("SELECT s.id, s.summary
822 FROM {$CFG->prefix}scorm s
823 WHERE s.course = $restore->course_id")) {
825 $i = 0; //Counter to send some output to the browser to avoid timeouts
826 foreach ($scorms as $scorm) {
827 //Increment counter
828 $i++;
829 $content = $scorm->summary;
830 $result = restore_decode_content_links_worker($content,$restore);
832 if ($result != $content) {
833 //Update record
834 $scorm->summary = addslashes($result);
835 $status = update_record("scorm",$scorm);
836 if (debugging()) {
837 if (!defined('RESTORE_SILENTLY')) {
838 echo '<br /><hr />'.htmlentities($content).'<br />changed to<br />'.htmlentities($result).'<hr /><br />';
842 //Do some output
843 if (($i+1) % 5 == 0) {
844 if (!defined('RESTORE_SILENTLY')) {
845 echo ".";
846 if (($i+1) % 100 == 0) {
847 echo "<br />";
850 backup_flush(300);
854 return $status;
857 //This function returns a log record with all the necessay transformations
858 //done. It's used by restore_log_module() to restore modules log.
859 function scorm_restore_logs($restore,$log) {
861 $status = true;
863 return $status;