2 //This php script contains all the stuff to restore hotpot mods
4 //-----------------------------------------------------------
5 // This is the "graphical" structure of the hotpot mod:
6 //-----------------------------------------------------------
11 // +--------------+--------------+
14 // hotpot_attempts hotpot_questions
15 // (UL, pk->id, (UL, pk->id,
16 // fk->hotpot) fk->hotpot, text)
19 // +--------------+--------------+ |
24 // fk->attempt, question, |
25 // correct, wrong, ignored) |
28 // +-----------+-----------+
33 // Meaning: pk->primary key field of the table
34 // fk->foreign key to link with parent
35 // nt->nested field (recursive data)
36 // CL->course level info
37 // UL->user level info
38 // files->table may have files
40 //-----------------------------------------------------------
41 // It is not necessary to backup "questions", "responses"
42 // and "strings", because they can be restored from the
43 // "details" field of the "attempts" records
44 //-----------------------------------------------------------
46 require_once ("$CFG->dirroot/mod/hotpot/lib.php");
48 //This function executes all the restore procedure about this mod
49 function hotpot_restore_mods($mod, $restore) {
51 // this function is called by "restore_create_modules" (in "backup/restorelib.php")
52 // which is called by "backup/restore_execute.html" (included by "backup/restore.php")
55 // id : id field in 'modtype' table
58 // $restore is an object
59 // backup_unique_code : xxxxxxxxxx
60 // file : '/full/path/to/backupfile.zip'
61 // mods : an array of $modinfo's (see below)
62 // restoreto : 0=existing course (replace), 1=existing course (append), 2=new course
63 // users : 0=all, 1=course, 2=none
65 // user_files : 0=no, 1=yes
66 // course_files : 0=no, 1=yes
67 // course_id : id of course into which data is to be restored
68 // deleting : true if 'restoreto'==0, otherwise false
69 // original_wwwroot : 'http://your.server.com/moodle'
71 // $modinfo is an array
72 // 'modname' : array( 'restore'=> 0=no 1=yes, 'userinfo' => 0=no 1=yes)
76 //Get data record for this instance of the mod
77 $data = backup_getid($restore->backup_unique_code
, $mod->modtype
, $mod->id
);
81 // backup_code => xxxxxxxxxx,
82 // table_name => 'hotpot',
85 // info => array of info for this instance of the mod
87 // short cut to xmlized info
88 $info = &$data->info
['MOD']['#'];
90 // build the new record
92 $hotpot->course
= $restore->course_id
;
94 // don't include these fields in the hotpot record
95 $excluded_TAGS = array('MODTYPE', 'ID', 'COURSE', 'ATTEMPT_DATA');
98 $TAGS = array_keys($info);
99 foreach ($TAGS as $TAG) {
101 if (!in_array($TAG, $excluded_TAGS)) {
102 $tag = strtolower($TAG);
103 $hotpot->$tag = backup_todb($info[$TAG][0]['#']);
108 $hotpot->id
= insert_record ('hotpot', $hotpot);
109 if (is_numeric($hotpot->id
)) {
112 echo '<ul><li>'.get_string('modulename', 'hotpot').' "'.$hotpot->name
.'"<br>';
115 // save the new id (required for log retore later on)
116 backup_putid($restore->backup_unique_code
, $mod->modtype
, $mod->id
, $hotpot->id
);
118 // backup user info, if required
119 if ($restore->mods
[$mod->modtype
]->userinfo
) {
121 // are we overwriting a course?
122 if ($restore->deleting
) {
124 // remove previous attempts, questions and responses for this quiz
125 $select = "hotpot='$hotpot->id'";
126 if ($attempts = get_records_select('hotpot_attempts', $select)) {
127 $ids = implode(',', array_keys($attempts));
128 delete_records_select('hotpot_responses', "attempt IN ($ids)");
130 delete_records_select('hotpot_questions', $select);
131 delete_records_select('hotpot_attempts', $select);
134 // don't transfer these fields to the attempt records
135 $excluded_TAGS = array('hotpot');
138 while ($status && isset($info['ATTEMPT_DATA'][$i]['#'])) {
141 while ($status && isset($info['ATTEMPT_DATA'][$i]['#']['ATTEMPT'][$ii]['#'])) {
143 // shortcut to user info record
144 $info_record = &$info['ATTEMPT_DATA'][$i]['#']['ATTEMPT'][$ii]['#'];
147 $attempt->hotpot
= $hotpot->id
;
149 $TAGS = array_keys($info_record);
150 foreach ($TAGS as $TAG) {
152 if (!in_array($TAG, $excluded_TAGS)) {
154 $value = backup_todb($info_record[$TAG][0]['#']);
156 if ($TAG=='USERID') {
157 $user = backup_getid($restore->backup_unique_code
, 'user', $value);
159 $value = $user->new_id
;
161 $status = false; // this shouldn't happen
165 $tag = strtolower($TAG);
166 $attempt->$tag = $value;
168 } // end foreach $TAGS
170 // store old attempt id
171 $attempt->old_id
= $attempt->id
;
174 // add the attempt record
175 $attempt->id
= insert_record ('hotpot_attempts', $attempt);
176 if (is_numeric($attempt->id
)) {
178 // save the new id (required for log retore later on)
179 backup_putid($restore->backup_unique_code
, 'hotpot_attempts', $attempt->old_id
, $attempt->id
);
181 // remove slashes added by backup_todb(), otherwise xmlize() will complain
182 $attempt->details
= stripslashes($attempt->details
);
184 // add questions and responses in attempt $attempt->details
185 hotpot_add_attempt_details($attempt);
187 } else { // failed to insert $attempt record
191 // do some output, if required
203 } // end while $info_record
206 } // end while $info_records
213 // could not add hotpot record
218 // could not get $data for this hotpot quiz
225 //This function returns a log record with all the necessay transformations
226 //done. It's used by restore_log_module() to restore modules log.
227 function hotpot_restore_logs($restore, $log) {
232 switch ($log->action
) {
238 //Get the new_id of the module (to recode the info field)
239 $mod = backup_getid($restore->backup_unique_code
, $log->module
, $log->info
);
241 $log->url
= "view.php?id=".$log->cmid
;
242 $log->info
= $mod->new_id
;
249 $log->url
= "index.php?id=".$log->course
;
255 //Get the new_id of the module (to recode the info field)
256 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
258 $log->url
= "report.php?id=".$log->cmid
;
259 $log->info
= $mod->new_id
;
269 //Get the new_id of the module (to recode the info field)
270 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
272 //Extract the attempt id from the url field
273 $attemptid = substr(strrchr($log->url
,"="),1);
274 //Get the new_id of the attempt (to recode the url field)
275 $attempt = backup_getid($restore->backup_unique_code
,"hotpot_attempts",$attemptid);
277 $log->url
= "review.php?id=".$log->cmid
."&attempt=".$attempt->new_id
;
278 $log->info
= $mod->new_id
;
286 // Oops, unknown $log->action
287 print "<p>action (".$log->module
."-".$log->action
.") unknown. Not restored</p>";
292 return $status ?
$log : false;