Incorrect variable name used for parameter.
[moodle-linuxchix.git] / mod / hotpot / restorelib.php
blobd0282d4a6345ad69d470d4cdb934a55f4ee6630e
1 <?PHP //$Id$
2 //This php script contains all the stuff to restore hotpot mods
4 //-----------------------------------------------------------
5 // This is the "graphical" structure of the hotpot mod:
6 //-----------------------------------------------------------
7 //
8 // hotpot
9 // (CL, pk->id, files)
10 // |
11 // +--------------+--------------+
12 // | |
13 // | |
14 // hotpot_attempts hotpot_questions
15 // (UL, pk->id, (UL, pk->id,
16 // fk->hotpot) fk->hotpot, text)
17 // | | |
18 // | | |
19 // +--------------+--------------+ |
20 // | |
21 // | |
22 // hotpot_responses |
23 // (UL, pk->id, |
24 // fk->attempt, question, |
25 // correct, wrong, ignored) |
26 // | |
27 // | |
28 // +-----------+-----------+
29 // |
30 // hotpot_strings
31 // (UL, pk->id)
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")
54 // $mod is an object
55 // id : id field in 'modtype' table
56 // modtype : 'hotpot'
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
64 // logs : 0=no, 1=yes
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)
74 $status = true;
76 //Get data record for this instance of the mod
77 $data = backup_getid($restore->backup_unique_code, $mod->modtype, $mod->id);
78 if ($data) {
80 // $data is an object
81 // backup_code => xxxxxxxxxx,
82 // table_name => 'hotpot',
83 // old_id => xxx,
84 // new_id => NULL,
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
91 $hotpot = NULL;
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');
97 // fill in the fields
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]['#']);
107 // insert the record
108 $hotpot->id = insert_record ('hotpot', $hotpot);
109 if (is_numeric($hotpot->id)) {
111 // Do some output
112 echo '<ul><li>'.get_string('modulename', 'hotpot').' &quot;'.$hotpot->name.'&quot;<br>';
113 backup_flush(300);
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');
137 $i = 0;
138 while ($status && isset($info['ATTEMPT_DATA'][$i]['#'])) {
140 $ii = 0;
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]['#'];
146 $attempt = NULL;
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);
158 if ($user) {
159 $value = $user->new_id;
160 } else {
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;
172 unset($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
188 $status = false;
191 // do some output, if required
192 if ($status) {
193 if ($ii%10==0) {
194 echo '.';
195 if ($ii%200==0) {
196 echo '<br>';
197 backup_flush(300);
202 $ii++;
203 } // end while $info_record
205 $i++;
206 } // end while $info_records
209 // Finalize ul
210 echo "</li></ul>";
212 } else {
213 // could not add hotpot record
214 $status = false;
217 } else {
218 // could not get $data for this hotpot quiz
219 $status = false;
222 return $status;
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) {
229 // assume the worst
230 $status = false;
232 switch ($log->action) {
234 case "add":
235 case "update":
236 case "view":
237 if ($log->cmid) {
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);
240 if ($mod) {
241 $log->url = "view.php?id=".$log->cmid;
242 $log->info = $mod->new_id;
243 $status = true;
246 break;
248 case "view all":
249 $log->url = "index.php?id=".$log->course;
250 $status = true;
251 break;
253 case "report":
254 if ($log->cmid) {
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);
257 if ($mod) {
258 $log->url = "report.php?id=".$log->cmid;
259 $log->info = $mod->new_id;
260 $status = true;
263 break;
265 case "attempt":
266 case "submit":
267 case "review":
268 if ($log->cmid) {
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);
271 if ($mod) {
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);
276 if ($attempt) {
277 $log->url = "review.php?id=".$log->cmid."&attempt=".$attempt->new_id;
278 $log->info = $mod->new_id;
279 $status = true;
283 break;
285 default:
286 // Oops, unknown $log->action
287 print "<p>action (".$log->module."-".$log->action.") unknown. Not restored</p>";
288 break;
290 } // end switch
292 return $status ? $log : false;