Commit | Line | Data |
---|---|---|
795b6945 | 1 | <?php |
df9a165e | 2 | //This function iterates over all modules in backup file, searching for a |
3 | //MODNAME_refresh_events() to execute. Perhaps it should ve moved to central Moodle... | |
4 | function restore_refresh_events($restore) { | |
f04873a9 | 5 | |
df9a165e | 6 | global $CFG; |
7 | $status = true; | |
f04873a9 | 8 | |
df9a165e | 9 | //Take all modules in backup |
10 | $modules = $restore->mods; | |
11 | //Iterate | |
12 | foreach($modules as $name => $module) { | |
13 | //Only if the module is being restored | |
10b7946f | 14 | if (isset($module->restore) && $module->restore == 1) { |
df9a165e | 15 | //Include module library |
16 | include_once("$CFG->dirroot/mod/$name/lib.php"); | |
17 | //If module_refresh_events exists | |
18 | $function_name = $name."_refresh_events"; | |
19 | if (function_exists($function_name)) { | |
20 | $status = $function_name($restore->course_id); | |
21 | } | |
22 | } | |
23 | } | |
24 | return $status; | |
25 | } | |
26 | ||
aada726c EL |
27 | //Called to set up any course-format specific data that may be in the file |
28 | function restore_set_format_data($restore,$xml_file) { | |
9db7dab2 | 29 | global $CFG, $DB; |
4e060ab9 | 30 | |
56eea82e | 31 | $status = true; |
aada726c EL |
32 | //Check it exists |
33 | if (!file_exists($xml_file)) { | |
34 | return false; | |
87b144ae | 35 | } |
aada726c EL |
36 | //Load data from XML to info |
37 | if(!($info = restore_read_xml_formatdata($xml_file))) { | |
38 | return false; | |
87b144ae | 39 | } |
40 | ||
aada726c EL |
41 | //Process format data if there is any |
42 | if (isset($info->format_data)) { | |
43 | if(!$format=$DB->get_field('course','format', array('id'=>$restore->course_id))) { | |
44 | return false; | |
87b144ae | 45 | } |
aada726c EL |
46 | // If there was any data then it must have a restore method |
47 | $file=$CFG->dirroot."/course/format/$format/restorelib.php"; | |
48 | if(!file_exists($file)) { | |
49 | return false; | |
56eea82e | 50 | } |
aada726c EL |
51 | require_once($file); |
52 | $function=$format.'_restore_format_data'; | |
53 | if(!function_exists($function)) { | |
54 | return false; | |
55 | } | |
56 | return $function($restore,$info->format_data); | |
56eea82e | 57 | } |
c85607f0 | 58 | |
aada726c EL |
59 | // If we got here then there's no data, but that's cool |
60 | return true; | |
61 | } | |
d45b28ef | 62 | |
aada726c EL |
63 | //This function creates all the structures messages and contacts |
64 | function restore_create_messages($restore,$xml_file) { | |
9db7dab2 | 65 | global $CFG, $DB; |
868e94e7 | 66 | |
67 | $status = true; | |
68 | //Check it exists | |
69 | if (!file_exists($xml_file)) { | |
aada726c | 70 | $status = false; |
bb41dadd | 71 | } |
aada726c EL |
72 | //Get info from xml |
73 | if ($status) { | |
74 | //info will contain the id and name of every table | |
75 | //(message, message_read and message_contacts) | |
76 | //in backup_ids->info will be the real info (serialized) | |
77 | $info = restore_read_xml_messages($restore,$xml_file); | |
bb41dadd | 78 | |
aada726c EL |
79 | //If we have info, then process messages & contacts |
80 | if ($info > 0) { | |
81 | //Count how many we have | |
82 | $unreadcount = $DB->count_records ('backup_ids', array('backup_code'=>$restore->backup_unique_code, 'table_name'=>'message')); | |
83 | $readcount = $DB->count_records ('backup_ids', array('backup_code'=>$restore->backup_unique_code, 'table_name'=>'message_read')); | |
84 | $contactcount = $DB->count_records ('backup_ids', array('backup_code'=>$restore->backup_unique_code, 'table_name'=>'message_contacts')); | |
85 | if ($unreadcount || $readcount || $contactcount) { | |
86 | //Start ul | |
87 | if (!defined('RESTORE_SILENTLY')) { | |
88 | echo '<ul>'; | |
89 | } | |
90 | //Number of records to get in every chunk | |
91 | $recordset_size = 4; | |
f04873a9 | 92 | |
aada726c EL |
93 | //Process unread |
94 | if ($unreadcount) { | |
95 | if (!defined('RESTORE_SILENTLY')) { | |
96 | echo '<li>'.get_string('unreadmessages','message').'</li>'; | |
4c81f551 | 97 | } |
aada726c EL |
98 | $counter = 0; |
99 | while ($counter < $unreadcount) { | |
100 | //Fetch recordset_size records in each iteration | |
101 | $recs = $DB->get_records("backup_ids", array('table_name'=>'message', 'backup_code'=>$restore->backup_unique_code),"old_id","old_id",$counter,$recordset_size); | |
102 | if ($recs) { | |
103 | foreach ($recs as $rec) { | |
104 | //Get the full record from backup_ids | |
105 | $data = backup_getid($restore->backup_unique_code,"message",$rec->old_id); | |
106 | if ($data) { | |
107 | //Now get completed xmlized object | |
108 | $info = $data->info; | |
109 | //traverse_xmlize($info); //Debug | |
110 | //print_object ($GLOBALS['traverse_array']); //Debug | |
111 | //$GLOBALS['traverse_array']=""; //Debug | |
112 | //Now build the MESSAGE record structure | |
e894efc3 | 113 | $dbrec = new stdClass(); |
aada726c EL |
114 | $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']); |
115 | $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']); | |
116 | $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']); | |
117 | $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']); | |
118 | $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']); | |
119 | $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']); | |
120 | //We have to recode the useridfrom field | |
121 | $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom); | |
122 | if ($user) { | |
123 | //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug | |
124 | $dbrec->useridfrom = $user->new_id; | |
125 | } | |
126 | //We have to recode the useridto field | |
127 | $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto); | |
128 | if ($user) { | |
129 | //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug | |
130 | $dbrec->useridto = $user->new_id; | |
131 | } | |
132 | //Check if the record doesn't exist in DB! | |
133 | $exist = $DB->get_record('message', array('useridfrom'=>$dbrec->useridfrom, | |
134 | 'useridto'=>$dbrec->useridto, | |
135 | 'timecreated'=>$dbrec->timecreated)); | |
136 | if (!$exist) { | |
137 | //Not exist. Insert | |
138 | $status = $DB->insert_record('message',$dbrec); | |
139 | } else { | |
140 | //Duplicate. Do nothing | |
141 | } | |
142 | } | |
143 | //Do some output | |
144 | $counter++; | |
145 | if ($counter % 10 == 0) { | |
146 | if (!defined('RESTORE_SILENTLY')) { | |
147 | echo "."; | |
148 | if ($counter % 200 == 0) { | |
149 | echo "<br />"; | |
150 | } | |
151 | } | |
152 | backup_flush(300); | |
153 | } | |
154 | } | |
155 | } | |
f04873a9 | 156 | } |
4c81f551 | 157 | } |
f04873a9 | 158 | |
aada726c EL |
159 | //Process read |
160 | if ($readcount) { | |
161 | if (!defined('RESTORE_SILENTLY')) { | |
162 | echo '<li>'.get_string('readmessages','message').'</li>'; | |
34f07866 | 163 | } |
aada726c EL |
164 | $counter = 0; |
165 | while ($counter < $readcount) { | |
166 | //Fetch recordset_size records in each iteration | |
167 | $recs = $DB->get_records("backup_ids", array('table_name'=>'message_read', 'backup_code'=>$restore->backup_unique_code),"old_id","old_id",$counter,$recordset_size); | |
168 | if ($recs) { | |
169 | foreach ($recs as $rec) { | |
170 | //Get the full record from backup_ids | |
171 | $data = backup_getid($restore->backup_unique_code,"message_read",$rec->old_id); | |
172 | if ($data) { | |
173 | //Now get completed xmlized object | |
174 | $info = $data->info; | |
175 | //traverse_xmlize($info); //Debug | |
176 | //print_object ($GLOBALS['traverse_array']); //Debug | |
177 | //$GLOBALS['traverse_array']=""; //Debug | |
178 | //Now build the MESSAGE_READ record structure | |
179 | $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']); | |
180 | $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']); | |
181 | $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']); | |
182 | $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']); | |
183 | $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']); | |
184 | $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']); | |
185 | $dbrec->timeread = backup_todb($info['MESSAGE']['#']['TIMEREAD']['0']['#']); | |
186 | $dbrec->mailed = backup_todb($info['MESSAGE']['#']['MAILED']['0']['#']); | |
187 | //We have to recode the useridfrom field | |
188 | $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom); | |
189 | if ($user) { | |
190 | //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />"; //Debug | |
191 | $dbrec->useridfrom = $user->new_id; | |
192 | } | |
193 | //We have to recode the useridto field | |
194 | $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto); | |
195 | if ($user) { | |
196 | //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />"; //Debug | |
197 | $dbrec->useridto = $user->new_id; | |
198 | } | |
199 | //Check if the record doesn't exist in DB! | |
200 | $exist = $DB->get_record('message_read', array('useridfrom'=>$dbrec->useridfrom, | |
201 | 'useridto'=>$dbrec->useridto, | |
202 | 'timecreated'=>$dbrec->timecreated)); | |
203 | if (!$exist) { | |
204 | //Not exist. Insert | |
205 | $status = $DB->insert_record('message_read',$dbrec); | |
206 | } else { | |
207 | //Duplicate. Do nothing | |
208 | } | |
209 | } | |
210 | //Do some output | |
211 | $counter++; | |
212 | if ($counter % 10 == 0) { | |
213 | if (!defined('RESTORE_SILENTLY')) { | |
214 | echo "."; | |
215 | if ($counter % 200 == 0) { | |
216 | echo "<br />"; | |
217 | } | |
218 | } | |
219 | backup_flush(300); | |
b486ef1a | 220 | } |
b486ef1a | 221 | } |
222 | } | |
1ad590e1 | 223 | } |
224 | } | |
f04873a9 | 225 | |
aada726c EL |
226 | //Process contacts |
227 | if ($contactcount) { | |
228 | if (!defined('RESTORE_SILENTLY')) { | |
229 | echo '<li>'.moodle_strtolower(get_string('contacts','message')).'</li>'; | |
1ad590e1 | 230 | } |
aada726c EL |
231 | $counter = 0; |
232 | while ($counter < $contactcount) { | |
233 | //Fetch recordset_size records in each iteration | |
234 | $recs = $DB->get_records("backup_ids", array('table_name'=>'message_contacts', 'backup_code'=>$restore->backup_unique_code),"old_id","old_id",$counter,$recordset_size); | |
235 | if ($recs) { | |
236 | foreach ($recs as $rec) { | |
237 | //Get the full record from backup_ids | |
238 | $data = backup_getid($restore->backup_unique_code,"message_contacts",$rec->old_id); | |
239 | if ($data) { | |
240 | //Now get completed xmlized object | |
241 | $info = $data->info; | |
242 | //traverse_xmlize($info); //Debug | |
243 | //print_object ($GLOBALS['traverse_array']); //Debug | |
244 | //$GLOBALS['traverse_array']=""; //Debug | |
245 | //Now build the MESSAGE_CONTACTS record structure | |
246 | $dbrec->userid = backup_todb($info['CONTACT']['#']['USERID']['0']['#']); | |
247 | $dbrec->contactid = backup_todb($info['CONTACT']['#']['CONTACTID']['0']['#']); | |
248 | $dbrec->blocked = backup_todb($info['CONTACT']['#']['BLOCKED']['0']['#']); | |
249 | //We have to recode the userid field | |
250 | $user = backup_getid($restore->backup_unique_code,"user",$dbrec->userid); | |
251 | if ($user) { | |
252 | //echo "User ".$dbrec->userid." to user ".$user->new_id."<br />"; //Debug | |
253 | $dbrec->userid = $user->new_id; | |
254 | } | |
255 | //We have to recode the contactid field | |
256 | $user = backup_getid($restore->backup_unique_code,"user",$dbrec->contactid); | |
257 | if ($user) { | |
258 | //echo "User ".$dbrec->contactid." to user ".$user->new_id."<br />"; //Debug | |
259 | $dbrec->contactid = $user->new_id; | |
260 | } | |
261 | //Check if the record doesn't exist in DB! | |
262 | $exist = $DB->get_record('message_contacts', array('userid'=>$dbrec->userid, | |
263 | 'contactid'=>$dbrec->contactid)); | |
264 | if (!$exist) { | |
265 | //Not exist. Insert | |
266 | $status = $DB->insert_record('message_contacts',$dbrec); | |
267 | } else { | |
268 | //Duplicate. Do nothing | |
269 | } | |
270 | } | |
271 | //Do some output | |
272 | $counter++; | |
273 | if ($counter % 10 == 0) { | |
274 | if (!defined('RESTORE_SILENTLY')) { | |
275 | echo "."; | |
276 | if ($counter % 200 == 0) { | |
277 | echo "<br />"; | |
278 | } | |
279 | } | |
280 | backup_flush(300); | |
281 | } | |
282 | } | |
283 | } | |
f04873a9 | 284 | } |
1ad590e1 | 285 | } |
aada726c EL |
286 | if (!defined('RESTORE_SILENTLY')) { |
287 | //End ul | |
288 | echo '</ul>'; | |
1ad590e1 | 289 | } |
aada726c | 290 | } |
3ecf46a1 | 291 | } |
292 | } | |
293 | ||
aada726c EL |
294 | return $status; |
295 | } | |
f04873a9 | 296 | |
aada726c EL |
297 | //This function creates all the structures for blogs and blog tags |
298 | function restore_create_blogs($restore,$xml_file) { | |
299 | global $CFG, $DB; | |
f04873a9 | 300 | |
aada726c EL |
301 | $status = true; |
302 | //Check it exists | |
303 | if (!file_exists($xml_file)) { | |
304 | $status = false; | |
305 | } | |
306 | //Get info from xml | |
307 | if ($status) { | |
308 | //info will contain the number of blogs in the backup file | |
309 | //in backup_ids->info will be the real info (serialized) | |
310 | $info = restore_read_xml_blogs($restore,$xml_file); | |
4e781c7b | 311 | |
aada726c EL |
312 | //If we have info, then process blogs & blog_tags |
313 | if ($info > 0) { | |
314 | //Count how many we have | |
315 | $blogcount = $DB->count_records('backup_ids', array('backup_code'=>$restore->backup_unique_code, 'table_name'=>'blog')); | |
316 | if ($blogcount) { | |
317 | //Number of records to get in every chunk | |
318 | $recordset_size = 4; | |
82bd6a5e | 319 | |
aada726c EL |
320 | //Process blog |
321 | if ($blogcount) { | |
322 | $counter = 0; | |
323 | while ($counter < $blogcount) { | |
324 | //Fetch recordset_size records in each iteration | |
325 | $recs = $DB->get_records("backup_ids", array("table_name"=>'blog', 'backup_code'=>$restore->backup_unique_code),"old_id","old_id",$counter,$recordset_size); | |
326 | if ($recs) { | |
327 | foreach ($recs as $rec) { | |
328 | //Get the full record from backup_ids | |
329 | $data = backup_getid($restore->backup_unique_code,"blog",$rec->old_id); | |
330 | if ($data) { | |
331 | //Now get completed xmlized object | |
332 | $info = $data->info; | |
333 | //traverse_xmlize($info); //Debug | |
334 | //print_object ($GLOBALS['traverse_array']); //Debug | |
335 | //$GLOBALS['traverse_array']=""; //Debug | |
336 | //Now build the BLOG record structure | |
e894efc3 | 337 | $dbrec = new stdClass(); |
aada726c EL |
338 | $dbrec->module = backup_todb($info['BLOG']['#']['MODULE']['0']['#']); |
339 | $dbrec->userid = backup_todb($info['BLOG']['#']['USERID']['0']['#']); | |
340 | $dbrec->courseid = backup_todb($info['BLOG']['#']['COURSEID']['0']['#']); | |
341 | $dbrec->groupid = backup_todb($info['BLOG']['#']['GROUPID']['0']['#']); | |
342 | $dbrec->moduleid = backup_todb($info['BLOG']['#']['MODULEID']['0']['#']); | |
343 | $dbrec->coursemoduleid = backup_todb($info['BLOG']['#']['COURSEMODULEID']['0']['#']); | |
344 | $dbrec->subject = backup_todb($info['BLOG']['#']['SUBJECT']['0']['#']); | |
345 | $dbrec->summary = backup_todb($info['BLOG']['#']['SUMMARY']['0']['#']); | |
346 | $dbrec->content = backup_todb($info['BLOG']['#']['CONTENT']['0']['#']); | |
347 | $dbrec->uniquehash = backup_todb($info['BLOG']['#']['UNIQUEHASH']['0']['#']); | |
348 | $dbrec->rating = backup_todb($info['BLOG']['#']['RATING']['0']['#']); | |
349 | $dbrec->format = backup_todb($info['BLOG']['#']['FORMAT']['0']['#']); | |
350 | $dbrec->attachment = backup_todb($info['BLOG']['#']['ATTACHMENT']['0']['#']); | |
351 | $dbrec->publishstate = backup_todb($info['BLOG']['#']['PUBLISHSTATE']['0']['#']); | |
352 | $dbrec->lastmodified = backup_todb($info['BLOG']['#']['LASTMODIFIED']['0']['#']); | |
353 | $dbrec->created = backup_todb($info['BLOG']['#']['CREATED']['0']['#']); | |
354 | $dbrec->usermodified = backup_todb($info['BLOG']['#']['USERMODIFIED']['0']['#']); | |
82bd6a5e | 355 | |
aada726c EL |
356 | //We have to recode the userid field |
357 | $user = backup_getid($restore->backup_unique_code,"user",$dbrec->userid); | |
358 | if ($user) { | |
359 | //echo "User ".$dbrec->userid." to user ".$user->new_id."<br />"; //Debug | |
360 | $dbrec->userid = $user->new_id; | |
361 | } | |
f04873a9 | 362 | |
aada726c EL |
363 | //Check if the record doesn't exist in DB! |
364 | $exist = $DB->get_record('post', array('userid'=>$dbrec->userid, | |
365 | 'subject'=>$dbrec->subject, | |
366 | 'created'=>$dbrec->created)); | |
367 | $newblogid = 0; | |
368 | if (!$exist) { | |
369 | //Not exist. Insert | |
370 | $newblogid = $DB->insert_record('post',$dbrec); | |
371 | } | |
868e94e7 | 372 | |
aada726c EL |
373 | //Going to restore related tags. Check they are enabled and we have inserted a blog |
374 | if ($CFG->usetags && $newblogid) { | |
375 | //Look for tags in this blog | |
376 | if (isset($info['BLOG']['#']['BLOG_TAGS']['0']['#']['BLOG_TAG'])) { | |
377 | $tagsarr = $info['BLOG']['#']['BLOG_TAGS']['0']['#']['BLOG_TAG']; | |
378 | //Iterate over tags | |
379 | $tags = array(); | |
2b9ce62e | 380 | $sizetagsarr = sizeof($tagsarr); |
80c12897 | 381 | for ($i = 0; $i < $sizetagsarr; $i++) { |
aada726c EL |
382 | $tag_info = $tagsarr[$i]; |
383 | ///traverse_xmlize($tag_info); //Debug | |
384 | ///print_object ($GLOBALS['traverse_array']); //Debug | |
385 | ///$GLOBALS['traverse_array']=""; //Debug | |
868e94e7 | 386 | |
aada726c EL |
387 | $name = backup_todb($tag_info['#']['NAME']['0']['#']); |
388 | $rawname = backup_todb($tag_info['#']['RAWNAME']['0']['#']); | |
cee6968f | 389 | |
aada726c EL |
390 | $tags[] = $rawname; //Rawname is all we need |
391 | } | |
392 | tag_set('post', $newblogid, $tags); //Add all the tags in one API call | |
393 | } | |
394 | } | |
395 | } | |
396 | //Do some output | |
397 | $counter++; | |
398 | if ($counter % 10 == 0) { | |
399 | if (!defined('RESTORE_SILENTLY')) { | |
400 | echo "."; | |
401 | if ($counter % 200 == 0) { | |
402 | echo "<br />"; | |
403 | } | |
404 | } | |
405 | backup_flush(300); | |
406 | } | |
407 | } | |
408 | } | |
409 | } | |
410 | } | |
ae628043 | 411 | } |
ae628043 | 412 | } |
aada726c | 413 | } |
ae628043 | 414 | |
aada726c EL |
415 | return $status; |
416 | } | |
f04873a9 | 417 | |
aada726c EL |
418 | //This function creates all the course events |
419 | function restore_create_events($restore,$xml_file) { | |
420 | global $DB; | |
795b6945 | 421 | |
aada726c | 422 | global $CFG, $SESSION; |
f04873a9 | 423 | |
aada726c EL |
424 | $status = true; |
425 | //Check it exists | |
426 | if (!file_exists($xml_file)) { | |
427 | $status = false; | |
428 | } | |
429 | //Get info from xml | |
430 | if ($status) { | |
431 | //events will contain the old_id of every event | |
432 | //in backup_ids->info will be the real info (serialized) | |
433 | $events = restore_read_xml_events($restore,$xml_file); | |
434 | } | |
3bee1ead | 435 | |
aada726c EL |
436 | //Get admin->id for later use |
437 | $admin = get_admin(); | |
438 | $adminid = $admin->id; | |
3bee1ead | 439 | |
aada726c EL |
440 | //Now, if we have anything in events, we have to restore that |
441 | //events | |
442 | if ($events) { | |
443 | if ($events !== true) { | |
444 | //Iterate over each event | |
445 | foreach ($events as $event) { | |
446 | //Get record from backup_ids | |
447 | $data = backup_getid($restore->backup_unique_code,"event",$event->id); | |
448 | //Init variables | |
449 | $create_event = false; | |
3bee1ead | 450 | |
aada726c EL |
451 | if ($data) { |
452 | //Now get completed xmlized object | |
453 | $info = $data->info; | |
454 | //traverse_xmlize($info); //Debug | |
455 | //print_object ($GLOBALS['traverse_array']); //Debug | |
456 | //$GLOBALS['traverse_array']=""; //Debug | |
3bee1ead | 457 | |
aada726c EL |
458 | //if necessary, write to restorelog and adjust date/time fields |
459 | if ($restore->course_startdateoffset) { | |
460 | restore_log_date_changes('Events', $restore, $info['EVENT']['#'], array('TIMESTART')); | |
461 | } | |
3bee1ead | 462 | |
aada726c EL |
463 | //Now build the EVENT record structure |
464 | $eve->name = backup_todb($info['EVENT']['#']['NAME']['0']['#']); | |
465 | $eve->description = backup_todb($info['EVENT']['#']['DESCRIPTION']['0']['#']); | |
466 | $eve->format = backup_todb($info['EVENT']['#']['FORMAT']['0']['#']); | |
467 | $eve->courseid = $restore->course_id; | |
468 | $eve->groupid = backup_todb($info['EVENT']['#']['GROUPID']['0']['#']); | |
469 | $eve->userid = backup_todb($info['EVENT']['#']['USERID']['0']['#']); | |
470 | $eve->repeatid = backup_todb($info['EVENT']['#']['REPEATID']['0']['#']); | |
471 | $eve->modulename = ""; | |
472 | if (!empty($info['EVENT']['#']['MODULENAME'])) { | |
473 | $eve->modulename = backup_todb($info['EVENT']['#']['MODULENAME']['0']['#']); | |
474 | } | |
475 | $eve->instance = 0; | |
476 | $eve->eventtype = backup_todb($info['EVENT']['#']['EVENTTYPE']['0']['#']); | |
477 | $eve->timestart = backup_todb($info['EVENT']['#']['TIMESTART']['0']['#']); | |
478 | $eve->timeduration = backup_todb($info['EVENT']['#']['TIMEDURATION']['0']['#']); | |
479 | $eve->visible = backup_todb($info['EVENT']['#']['VISIBLE']['0']['#']); | |
480 | $eve->timemodified = backup_todb($info['EVENT']['#']['TIMEMODIFIED']['0']['#']); | |
868e94e7 | 481 | |
aada726c EL |
482 | //Now search if that event exists (by name, description, timestart fields) in |
483 | //restore->course_id course | |
484 | //Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides. | |
485 | $compare_description_clause = $DB->sql_compare_text('description') . "=" . $DB->sql_compare_text("'" . $eve->description . "'"); | |
486 | $eve_db = $DB->get_record_select('event', | |
487 | "courseid = ? AND name = ? AND $compare_description_clause AND timestart = ?", | |
488 | array($eve->courseid, $eve->name, $eve->timestart)); | |
489 | //If it doesn't exist, create | |
490 | if (!$eve_db) { | |
491 | $create_event = true; | |
492 | } | |
493 | //If we must create the event | |
494 | if ($create_event) { | |
cee6968f | 495 | |
aada726c EL |
496 | //We must recode the userid |
497 | $user = backup_getid($restore->backup_unique_code,"user",$eve->userid); | |
498 | if ($user) { | |
499 | $eve->userid = $user->new_id; | |
500 | } else { | |
501 | //Assign it to admin | |
502 | $eve->userid = $adminid; | |
503 | } | |
868e94e7 | 504 | |
aada726c EL |
505 | //We have to recode the groupid field |
506 | $group = backup_getid($restore->backup_unique_code,"groups",$eve->groupid); | |
507 | if ($group) { | |
508 | $eve->groupid = $group->new_id; | |
509 | } else { | |
510 | //Assign it to group 0 | |
511 | $eve->groupid = 0; | |
512 | } | |
f04873a9 | 513 | |
aada726c EL |
514 | //The structure is equal to the db, so insert the event |
515 | $newid = $DB->insert_record ("event",$eve); | |
7f9bd149 | 516 | |
aada726c EL |
517 | //We must recode the repeatid if the event has it |
518 | //The repeatid now refers to the id of the original event. (see Bug#5956) | |
519 | if ($newid && !empty($eve->repeatid)) { | |
520 | $repeat_rec = backup_getid($restore->backup_unique_code,"event_repeatid",$eve->repeatid); | |
521 | if ($repeat_rec) { //Exists, so use it... | |
522 | $eve->repeatid = $repeat_rec->new_id; | |
523 | } else { //Doesn't exists, calculate the next and save it | |
524 | $oldrepeatid = $eve->repeatid; | |
525 | $eve->repeatid = $newid; | |
526 | backup_putid($restore->backup_unique_code,"event_repeatid", $oldrepeatid, $eve->repeatid); | |
527 | } | |
528 | $eve->id = $newid; | |
529 | // update the record to contain the correct repeatid | |
530 | $DB->update_record('event',$eve); | |
531 | } | |
532 | } else { | |
533 | //get current event id | |
534 | $newid = $eve_db->id; | |
535 | } | |
536 | if ($newid) { | |
537 | //We have the newid, update backup_ids | |
538 | backup_putid($restore->backup_unique_code,"event", | |
539 | $event->id, $newid); | |
540 | } | |
541 | } | |
7f9bd149 | 542 | } |
543 | } | |
aada726c EL |
544 | } else { |
545 | $status = false; | |
546 | } | |
547 | return $status; | |
548 | } | |
7f9bd149 | 549 | |
f90666aa | 550 | function restore_execute(&$restore,$info,$course_header,&$errorstr) { |
9549b986 | 551 | global $CFG, $USER, $DB, $OUTPUT; |
f04873a9 | 552 | |
f04873a9 | 553 | $status = true; |
554 | ||
f90666aa | 555 | //Now create events as needed |
556 | if ($status) { | |
557 | if (!defined('RESTORE_SILENTLY')) { | |
ac211373 | 558 | echo "<li>".get_string("creatingevents"); |
f90666aa | 559 | } |
560 | if (!$status = restore_create_events($restore,$xml_file)) { | |
561 | if (!defined('RESTORE_SILENTLY')) { | |
9549b986 | 562 | echo $OUTPUT->notification("Could not restore course events!"); |
f90666aa | 563 | } else { |
564 | $errorstr = "Could not restore course events!"; | |
565 | return false; | |
566 | } | |
567 | } | |
ac211373 | 568 | if (!defined('RESTORE_SILENTLY')) { |
569 | echo '</li>'; | |
570 | } | |
f90666aa | 571 | } |
572 | ||
64acc5a5 | 573 | if ($status) { |
ae628043 | 574 | //If we are deleting and bringing into a course or making a new course, same situation |
64acc5a5 | 575 | if ($restore->restoreto == RESTORETO_CURRENT_DELETING || |
576 | $restore->restoreto == RESTORETO_EXISTING_DELETING || | |
577 | $restore->restoreto == RESTORETO_NEW_COURSE) { | |
ae628043 | 578 | if (!defined('RESTORE_SILENTLY')) { |
ac211373 | 579 | echo '<li>'.get_string('courseformatdata'); |
ae628043 | 580 | } |
581 | if (!$status = restore_set_format_data($restore, $xml_file)) { | |
582 | $error = "Error while setting the course format data"; | |
583 | if (!defined('RESTORE_SILENTLY')) { | |
9549b986 | 584 | echo $OUTPUT->notification($error); |
ae628043 | 585 | } else { |
586 | $errorstr=$error; | |
587 | return false; | |
588 | } | |
589 | } | |
ac211373 | 590 | if (!defined('RESTORE_SILENTLY')) { |
591 | echo '</li>'; | |
592 | } | |
ae628043 | 593 | } |
594 | } | |
595 | ||
f90666aa | 596 | //Now, if all is OK, adjust activity events |
597 | if ($status) { | |
598 | if (!defined('RESTORE_SILENTLY')) { | |
ac211373 | 599 | echo "<li>".get_string("refreshingevents"); |
f90666aa | 600 | } |
601 | if (!$status = restore_refresh_events($restore)) { | |
602 | if (!defined('RESTORE_SILENTLY')) { | |
9549b986 | 603 | echo $OUTPUT->notification("Could not refresh events for activities!"); |
f90666aa | 604 | } else { |
605 | $errorstr = "Could not refresh events for activities!"; | |
606 | return false; | |
607 | } | |
608 | } | |
ac211373 | 609 | if (!defined('RESTORE_SILENTLY')) { |
610 | echo '</li>'; | |
611 | } | |
f90666aa | 612 | } |
0f66aced | 613 | } |