Fixes to the PaintWeb cron task.
[moodle/mihaisucan.git] / mod / data / restorelib.php
blob387864bdbb1ff1154bd41a62a3e13a6487846c16
1 <?php // $Id
2 //This php script contains all the stuff to backup/restore data mod
4 //This is the "graphical" structure of the data mod:
5 //
6 // data
7 // (CL,pk->id)
8 // |
9 // |
10 // |
11 // ---------------------------------------------------------------------------------
12 // | |
13 //data_records (UL,pk->id, fk->data) data_fields (pk->id, fk->data)
14 // | |
15 // | |
16 // ----------------------------------------------------------------------------- |
17 // | | | |
18 //data_ratings(fk->recordid, pk->id) data_comments (fk->recordid, pk->id) | |
19 // data_content(pk->id, fk->recordid, fk->fieldid)
23 // Meaning: pk->primary key field of the table
24 // fk->foreign key to link with parent
25 // nt->nested field (recursive data)
26 // CL->course level info
27 // UL->user level info
28 // files->table may have files)
30 //-----------------------------------------------------------
32 //Backup data files because we've selected to backup user info
33 //and files are user info's level
35 $fieldids = array(); //array in the format of $fieldids[$oldid]=$newid. This is needed because of double dependencies of multiple tables.
38 //Return a content encoded to support interactivities linking. Every module
39 function data_restore_mods($mod,$restore) {
41 global $CFG;
43 $status = true;
45 $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
47 if ($data) {
48 //Now get completed xmlized object
49 $info = $data->info;
50 // if necessary, write to restorelog and adjust date/time fields
51 if ($restore->course_startdateoffset) {
52 restore_log_date_changes('Database', $restore, $info['MOD']['#'], array('TIMEAVAILABLEFROM', 'TIMEAVAILABLETO','TIMEVIEWFROM', 'TIMEVIEWTO'));
54 //traverse_xmlize($info); //Debug
55 //print_object ($GLOBALS['traverse_array']); //Debug
56 //$GLOBALS['traverse_array']=""; //Debug
58 $database->course = $restore->course_id;
60 $database->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
61 $database->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
62 // Only relevant for restoring backups from 1.6 in a 1.7 install.
63 if (isset($info['MOD']['#']['RATINGS']['0']['#'])) {
64 $database->ratings = backup_todb($info['MOD']['#']['RATINGS']['0']['#']);
66 $database->comments = backup_todb($info['MOD']['#']['COMMENTS']['0']['#']);
67 $database->timeavailablefrom = backup_todb($info['MOD']['#']['TIMEAVAILABLEFROM']['0']['#']);
68 $database->timeavailableto = backup_todb($info['MOD']['#']['TIMEAVAILABLETO']['0']['#']);
69 $database->timeviewfrom = backup_todb($info['MOD']['#']['TIMEVIEWFROM']['0']['#']);
70 $database->timeviewto = backup_todb($info['MOD']['#']['TIMEVIEWTO']['0']['#']);
71 // Only relevant for restoring backups from 1.6 in a 1.7 install.
72 if (isset($info['MOD']['#']['PARTICIPANTS']['0']['#'])) {
73 $database->participants = backup_todb($info['MOD']['#']['PARTICIPANTS']['0']['#']);
75 $database->requiredentries = backup_todb($info['MOD']['#']['REQUIREDENTRIES']['0']['#']);
76 $database->requiredentriestoview = backup_todb($info['MOD']['#']['REQUIREDENTRIESTOVIEW']['0']['#']);
77 $database->maxentries = backup_todb($info['MOD']['#']['MAXENTRIES']['0']['#']);
78 $database->rssarticles = backup_todb($info['MOD']['#']['RSSARTICLES']['0']['#']);
79 $database->singletemplate = backup_todb($info['MOD']['#']['SINGLETEMPLATE']['0']['#']);
80 $database->listtemplate = backup_todb($info['MOD']['#']['LISTTEMPLATE']['0']['#']);
81 $database->listtemplateheader = backup_todb($info['MOD']['#']['LISTTEMPLATEHEADER']['0']['#']);
82 $database->listtemplatefooter = backup_todb($info['MOD']['#']['LISTTEMPLATEFOOTER']['0']['#']);
83 $database->addtemplate = backup_todb($info['MOD']['#']['ADDTEMPLATE']['0']['#']);
84 $database->rsstemplate = backup_todb($info['MOD']['#']['RSSTEMPLATE']['0']['#']);
85 $database->rsstitletemplate = backup_todb($info['MOD']['#']['RSSTITLETEMPLATE']['0']['#']);
86 $database->csstemplate = backup_todb($info['MOD']['#']['CSSTEMPLATE']['0']['#']);
87 $database->jstemplate = backup_todb($info['MOD']['#']['JSTEMPLATE']['0']['#']);
88 $database->approval = backup_todb($info['MOD']['#']['APPROVAL']['0']['#']);
89 $database->scale = backup_todb($info['MOD']['#']['SCALE']['0']['#']);
90 $database->assessed = backup_todb($info['MOD']['#']['ASSESSED']['0']['#']);
91 // Only relevant for restoring backups from 1.6 in a 1.7 install.
92 if (isset($info['MOD']['#']['ASSESSPUBLIC']['0']['#'])) {
93 $database->assesspublic = backup_todb($info['MOD']['#']['ASSESSPUBLIC']['0']['#']);
95 $database->defaultsort = backup_todb($info['MOD']['#']['DEFAULTSORT']['0']['#']);
96 $database->defaultsortdir = backup_todb($info['MOD']['#']['DEFAULTSORTDIR']['0']['#']);
97 $database->editany = backup_todb($info['MOD']['#']['EDITANY']['0']['#']);
98 $database->notification = backup_todb($info['MOD']['#']['NOTIFICATION']['0']['#']);
100 $newid = insert_record ('data', $database);
102 //Do some output
103 if (!defined('RESTORE_SILENTLY')) {
104 echo "<li>".get_string("modulename","data")." \"".format_string(stripslashes($database->name),true)."\"</li>";
107 if ($newid) {
108 //We have the newid, update backup_ids
109 backup_putid($restore->backup_unique_code,$mod->modtype,
110 $mod->id, $newid);
111 //Now check if want to restore user data and do it.
112 if (function_exists('restore_userdata_selected')) {
113 // Moodle 1.6
114 $restore_userdata_selected = restore_userdata_selected($restore, 'data', $mod->id);
115 } else {
116 // Moodle 1.5
117 $restore_userdata_selected = $restore->mods['data']->userinfo;
120 global $fieldids;
121 //Restore data_fields first!!! need to hold an array of [oldid]=>newid due to double dependencies
122 $status = $status and data_fields_restore_mods ($mod->id, $newid, $info, $restore);
124 // now use the new field in the defaultsort
125 $newdefaultsort = empty($fieldids[$database->defaultsort]) ? 0 : $fieldids[$database->defaultsort];
126 set_field('data', 'defaultsort', $newdefaultsort, 'id', $newid);
128 if ($restore_userdata_selected) {
129 $status = $status and data_records_restore_mods ($mod->id, $newid, $info, $restore);
132 // If the backup contained $data->participants, $data->assesspublic
133 // and $data->groupmode, we need to convert the data to use Roles.
134 // It means the backup was made pre Moodle 1.7. We check the
135 // backup_version to make sure.
136 if (isset($database->participants) && isset($database->assesspublic)) {
138 if (!$teacherroles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW)) {
139 notice('Default teacher role was not found. Roles and permissions '.
140 'for your database modules will have to be manually set.');
142 if (!$studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
143 notice('Default student role was not found. Roles and permissions '.
144 'for all your database modules will have to be manually set.');
146 if (!$guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
147 notice('Default guest role was not found. Roles and permissions '.
148 'for all your database modules will have to be manually set.');
150 require_once($CFG->dirroot.'/mod/data/lib.php');
151 data_convert_to_roles($database, $teacherroles, $studentroles,
152 $restore->mods['data']->instances[$mod->id]->restored_as_course_module);
155 } else {
156 $status = false;
158 } else {
159 $status = false;
162 return $status;
165 function data_fields_restore_mods ($old_data_id, $new_data_id, $info, $restore) {
167 global $CFG, $fieldids;
170 $fields = $info['MOD']['#']['FIELDS']['0']['#']['FIELD'];
172 for ($i = 0; $i < sizeof($fields); $i++) {
174 $fie_info = $fields[$i];
175 $oldid = backup_todb($fie_info['#']['ID']['0']['#']);
177 $field -> dataid = $new_data_id;
178 $field -> type = backup_todb($fie_info['#']['TYPE']['0']['#']);
179 $field -> name = backup_todb($fie_info['#']['NAME']['0']['#']);
180 $field -> description = backup_todb($fie_info['#']['DESCRIPTION']['0']['#']);
181 $field -> param1 = backup_todb($fie_info['#']['PARAM1']['0']['#']);
182 $field -> param2 = backup_todb($fie_info['#']['PARAM2']['0']['#']);
183 $field -> param3 = backup_todb($fie_info['#']['PARAM3']['0']['#']);
184 $field -> param4 = backup_todb($fie_info['#']['PARAM4']['0']['#']);
185 $field -> param5 = backup_todb($fie_info['#']['PARAM5']['0']['#']);
186 $field -> param6 = backup_todb($fie_info['#']['PARAM6']['0']['#']);
187 $field -> param7 = backup_todb($fie_info['#']['PARAM7']['0']['#']);
188 $field -> param8 = backup_todb($fie_info['#']['PARAM8']['0']['#']);
189 $field -> param9 = backup_todb($fie_info['#']['PARAM9']['0']['#']);
190 $field -> param10 = backup_todb($fie_info['#']['PARAM10']['0']['#']);
192 $newid = insert_record ("data_fields",$field);
194 $fieldids[$oldid] = $newid; //so we can use them in sub tables that depends on both fieldid and recordid
196 //Do some output
197 if (($i+1) % 50 == 0) {
198 if (!defined('RESTORE_SILENTLY')) {
199 echo ".";
200 if (($i+1) % 1000 == 0) {
201 echo "<br />";
204 backup_flush(300);
207 if ($newid) {
208 //We have the newid, update backup_ids
209 $status = backup_putid($restore->backup_unique_code,"data_fields",$oldid, $newid);
210 } else {
211 $status = false;
215 return $status;
219 function data_records_restore_mods ($old_data_id, $new_data_id, $info, $restore) {
221 global $CFG, $fieldids;
223 $status = true;
225 $records = $info['MOD']['#']['RECORDS']['0']['#']['RECORD'];
227 for ($i = 0; $i < sizeof($records); $i++) {
229 $rec_info = $records[$i];
230 $oldid = backup_todb($rec_info['#']['ID']['0']['#']);
232 $record = new object();
233 $record -> dataid = $new_data_id;
234 $record -> userid = backup_todb($rec_info['#']['USERID']['0']['#']);
235 $record -> groupid = backup_todb($rec_info['#']['GROUPID']['0']['#']);
236 $record -> timecreated = backup_todb($rec_info['#']['TIMECREATED']['0']['#']);
237 $record -> timemodified = backup_todb($rec_info['#']['TIMEMODIFIED']['0']['#']);
238 $record -> approved = backup_todb($rec_info['#']['APPROVED']['0']['#']);
239 $user = backup_getid($restore->backup_unique_code,"user",$record->userid);
240 $group= restore_group_getid($restore, $record->groupid);
242 if ($user) {
243 $record->userid = $user->new_id;
245 if ($group) {
246 $record->groupid= $group->new_id;
249 $newid = insert_record ("data_records",$record);
251 //Do some output
252 if (($i+1) % 50 == 0) {
253 if (!defined('RESTORE_SILENTLY')) {
254 echo ".";
255 if (($i+1) % 1000 == 0) {
256 echo "<br />";
259 backup_flush(300);
262 if ($newid) {
263 //We have the newid, update backup_ids
264 $status = $status and backup_putid($restore->backup_unique_code,"data_records",$oldid, $newid);
266 $status = $status and data_content_restore_mods ($oldid, $newid, $old_data_id, $new_data_id, $rec_info, $restore);
267 $status = $status and data_ratings_restore_mods ($oldid, $newid, $info, $rec_info);
268 $status = $status and data_comments_restore_mods ($oldid, $newid, $info, $rec_info);
270 } else {
271 $status = false;
274 return $status;
277 function data_content_restore_mods ($old_record_id, $new_record_id, $old_data_id, $new_data_id, $recinfo, $restore) {
279 global $CFG, $fieldids;
281 $status = true;
283 $contents = $recinfo['#']['CONTENTS']['0']['#']['CONTENT'];
285 for ($i = 0; $i < sizeof($contents); $i++) {
287 $con_info = $contents[$i];
288 $oldid = backup_todb($con_info['#']['ID']['0']['#']);
289 $oldfieldid = backup_todb($con_info['#']['FIELDID']['0']['#']);
290 $oldrecordid = backup_todb($con_info['#']['RECORDID']['0']['#']);
292 $content -> recordid = $new_record_id;
293 $content -> fieldid = $fieldids[$oldfieldid];
294 $content -> content = backup_todb($con_info['#']['CONTENT']['0']['#']);
295 $content -> content1 = backup_todb($con_info['#']['CONTENT1']['0']['#']);
296 $content -> content2 = backup_todb($con_info['#']['CONTENT2']['0']['#']);
297 $content -> content3 = backup_todb($con_info['#']['CONTENT3']['0']['#']);
298 $content -> content4 = backup_todb($con_info['#']['CONTENT4']['0']['#']);
299 $newid = insert_record ("data_content",$content);
301 //Do some output
302 if (($i+1) % 50 == 0) {
303 if (!defined('RESTORE_SILENTLY')) {
304 echo ".";
305 if (($i+1) % 1000 == 0) {
306 echo "<br />";
309 backup_flush(300);
312 if ($newid) {
313 //We have the newid, update backup_ids
315 $status = $status and data_restore_files ($old_data_id, $new_data_id, $oldfieldid, $content->fieldid, $oldrecordid, $content->recordid, $recinfo, $restore);
316 $status = $status and backup_putid($restore->backup_unique_code,"data_content",$oldid, $newid);
317 } else {
318 $status = false;
321 return $status;
325 function data_restore_files ($old_data_id, $new_data_id, $old_field_id, $new_field_id, $old_record_id, $new_record_id, $recinfo, $restore) {
327 global $CFG, $db;
329 $status = true;
330 $todo = false;
331 $moddata_path = "";
332 $data_path = "";
333 $temp_path = "";
335 //First, we check to "course_id" exists and create is as necessary
336 //in CFG->dataroot
337 $dest_dir = $CFG->dataroot."/".$restore->course_id;
338 $status = check_dir_exists($dest_dir,true);
340 //Now, locate course's moddata directory
341 $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
343 //Check it exists and create it
344 $status = check_dir_exists($moddata_path,true);
346 //Now, locate data directory
347 if ($status) {
348 $data_path = $moddata_path."/data";
349 //Check it exists and create it
350 $status = check_dir_exists($data_path,true);
353 //Now locate the temp dir we are gong to restore
354 if ($status) {
355 $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
356 "/moddata/data/".$old_data_id."/".$old_field_id."/".$old_record_id;
357 $todo = check_dir_exists($temp_path);
360 //If todo, we create the neccesary dirs in course moddata/data
361 if ($status and $todo) {
362 //First this data id
363 $this_data_path = $data_path."/".$new_data_id;
365 $status = check_dir_exists($this_data_path,true);
366 //Now this user id
367 $this_field_path = $this_data_path."/".$new_field_id;
368 $status = check_dir_exists($this_field_path,true);
369 $this_record_path = $this_field_path = $this_field_path."/".$new_record_id;
370 $status = check_dir_exists($this_record_path,true);
371 //And now, copy temp_path to user_data_path
373 $status = @backup_copy_file($temp_path, $this_record_path);
376 return $status;
379 function data_ratings_restore_mods ($oldid, $newid, $info, $rec_info) {
381 global $CFG;
383 $status = true;
385 $ratings= $rec_info['#']['RATINGS']['0']['#']['RATING'];
387 if (empty($ratings)) { // no ratings to restore
388 return true;
390 for ($i = 0; $i < sizeof($ratings); $i++) {
392 $rat_info = $ratings[$i];
394 $rating -> recordid = $newid;
395 $rating -> userid = backup_todb($rat_info['#']['USERID']['0']['#']);
396 $rating -> rating = backup_todb($rat_info['#']['RATING']['0']['#']);
398 if (! insert_record ("data_ratings",$rating)) {
399 $status = false;
402 return $status;
405 function data_comments_restore_mods ($oldid, $newid, $info, $rec_info) {
407 global $CFG;
409 $status = true;
411 $comments= $rec_info['#']['COMMENTS']['0']['#']['COMMENT'];
413 if (empty($comments)) { // no comments to restore
414 return true;
417 for ($i = 0; $i < sizeof($comments); $i++) {
419 $com_info = $comments[$i];
421 $comment -> recordid = $newid;
422 $comment -> userid = backup_todb($com_info['#']['USERID']['0']['#']);
423 $comment -> content = backup_todb($com_info['#']['CONTENT']['0']['#']);
424 $comment -> created = backup_todb($com_info['#']['CREATED']['0']['#']);
425 $comment -> modified = backup_todb($com_info['#']['MODIFIED']['0']['#']);
426 if (! insert_record ("data_comments",$comment)) {
427 $status = false;
431 return $status;
436 * Returns a content decoded to support interactivities linking. Every module
437 * should have its own. They are called automatically from
438 * xxxx_decode_content_links_caller() function in each module
439 * in the restore process.
441 * @param string $content the content to be decoded
442 * @param object $restore the preferences used in restore
443 * @return string the decoded string
445 function data_decode_content_links ($content,$restore) {
447 global $CFG;
449 $result = $content;
451 /// Link to the list of datas
453 $searchstring='/\$@(DATAINDEX)\*([0-9]+)@\$/';
454 /// We look for it
455 preg_match_all($searchstring,$content,$foundset);
456 /// If found, then we are going to look for its new id (in backup tables)
457 if ($foundset[0]) {
458 /// print_object($foundset); //Debug
459 /// Iterate over foundset[2]. They are the old_ids
460 foreach($foundset[2] as $old_id) {
461 /// We get the needed variables here (course id)
462 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
463 /// Personalize the searchstring
464 $searchstring='/\$@(DATAINDEX)\*('.$old_id.')@\$/';
465 /// If it is a link to this course, update the link to its new location
466 if($rec->new_id) {
467 /// Now replace it
468 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/data/index.php?id='.$rec->new_id,$result);
469 } else {
470 /// It's a foreign link so leave it as original
471 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/data/index.php?id='.$old_id,$result);
476 /// Link to data view by moduleid
478 $searchstring='/\$@(DATAVIEWBYID)\*([0-9]+)@\$/';
479 /// We look for it
480 preg_match_all($searchstring,$result,$foundset);
481 /// If found, then we are going to look for its new id (in backup tables)
482 if ($foundset[0]) {
483 /// print_object($foundset); //Debug
484 /// Iterate over foundset[2]. They are the old_ids
485 foreach($foundset[2] as $old_id) {
486 /// We get the needed variables here (course_modules id)
487 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
488 /// Personalize the searchstring
489 $searchstring='/\$@(DATAVIEWBYID)\*('.$old_id.')@\$/';
490 /// If it is a link to this course, update the link to its new location
491 if($rec->new_id) {
492 /// Now replace it
493 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/data/view.php?id='.$rec->new_id,$result);
494 } else {
495 /// It's a foreign link so leave it as original
496 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/data/view.php?id='.$old_id,$result);
501 /// Link to data view by dataid
503 $searchstring='/\$@(DATAVIEWBYD)\*([0-9]+)@\$/';
504 /// We look for it
505 preg_match_all($searchstring,$result,$foundset);
506 /// If found, then we are going to look for its new id (in backup tables)
507 if ($foundset[0]) {
508 /// print_object($foundset); //Debug
509 /// Iterate over foundset[2]. They are the old_ids
510 foreach($foundset[2] as $old_id) {
511 /// We get the needed variables here (data id)
512 $rec = backup_getid($restore->backup_unique_code,"data",$old_id);
513 /// Personalize the searchstring
514 $searchstring='/\$@(DATAVIEWBYD)\*('.$old_id.')@\$/';
515 /// If it is a link to this course, update the link to its new location
516 if($rec->new_id) {
517 /// Now replace it
518 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/data/view.php?d='.$rec->new_id,$result);
519 } else {
520 /// It's a foreign link so leave it as original
521 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/data/view.php?d='.$old_id,$result);
526 /// Link to data record (element)
528 $searchstring='/\$@(DATAVIEWRECORD)\*([0-9]+)\*([0-9]+)@\$/';
529 /// We look for it
530 preg_match_all($searchstring,$result,$foundset);
531 /// If found, then we are going to look for its new id (in backup tables)
532 if ($foundset[0]) {
533 /// print_object($foundset); //Debug
534 /// Iterate over foundset[2] and foundset[3]. They are the old_ids
535 foreach($foundset[2] as $key => $old_id) {
536 $old_id2 = $foundset[3][$key];
537 /// We get the needed variables here (data id and record id)
538 $rec = backup_getid($restore->backup_unique_code,"data",$old_id);
539 $rec2 = backup_getid($restore->backup_unique_code,"data_records",$old_id2);
540 /// Personalize the searchstring
541 $searchstring='/\$@(DATAVIEWRECORD)\*('.$old_id.')\*('.$old_id2.')@\$/';
542 /// If it is a link to this course, update the link to its new location
543 if($rec->new_id && $rec2->new_id) {
544 /// Now replace it
545 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/data/view.php?d='.$rec->new_id.'&amp;rid='.$rec2->new_id,$result);
546 } else {
547 /// It's a foreign link so leave it as original
548 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/data/view.php?d='.$old_id.'&amp;rid='.$old_id2,$result);
553 return $result;
557 * This function makes all the necessary calls to xxxx_decode_content_links()
558 * function in each module, passing them the desired contents to be decoded
559 * from backup format to destination site/course in order to mantain inter-activities
560 * working in the backup/restore process. It's called from restore_decode_content_links()
561 * function in restore process
563 * @param object $restore the preferences used in restore
564 * @return boolean status of the execution
566 function data_decode_content_links_caller($restore) {
568 global $CFG;
569 $status = true;
571 /// Process every DATA (intro, all HTML templates) in the course
572 /// Supported fields for main table:
573 $supportedfields = array('intro','singletemplate','listtemplate',
574 'listtemplateheader','addtemplate','rsstemplate','rsstitletemplate');
575 if ($datas = get_records_sql ("SELECT d.id, ".implode(',',$supportedfields)."
576 FROM {$CFG->prefix}data d
577 WHERE d.course = $restore->course_id")) {
578 /// Iterate over each data
579 $i = 0; //Counter to send some output to the browser to avoid timeouts
580 foreach ($datas as $data) {
581 /// Increment counter
582 $i++;
584 /// Make a new copy of the data object with nothing in, to use if
585 /// changes are necessary (allows us to do update_record without
586 /// worrying about every single field being included and needing
587 /// slashes).
588 $newdata = new stdClass;
589 $newdata->id=$data->id;
591 /// Loop through handling each supported field
592 $changed = false;
593 foreach($supportedfields as $field) {
594 $result = restore_decode_content_links_worker($data->{$field},$restore);
595 if ($result != $data->{$field}) {
596 $newdata->{$field} = addslashes($result);
597 $changed = true;
598 if (debugging()) {
599 if (!defined('RESTORE_SILENTLY')) {
600 echo '<br /><hr />'.s($data->{$field}).'<br />changed to<br />'.s($result).'<hr /><br />';
606 /// Update record if any field changed
607 if($changed) {
608 $status = update_record("data",$newdata);
611 /// Do some output
612 if (($i+1) % 5 == 0) {
613 if (!defined('RESTORE_SILENTLY')) {
614 echo ".";
615 if (($i+1) % 100 == 0) {
616 echo "<br />";
619 backup_flush(300);
624 /// Process every COMMENT (content) in the course
625 if ($comments = get_records_sql ("SELECT dc.id, dc.content
626 FROM {$CFG->prefix}data d,
627 {$CFG->prefix}data_records dr,
628 {$CFG->prefix}data_comments dc
629 WHERE d.course = $restore->course_id
630 AND dr.dataid = d.id
631 AND dc.recordid = dr.id")) {
632 /// Iterate over each data_comments->content
633 $i = 0; //Counter to send some output to the browser to avoid timeouts
634 foreach ($comments as $comment) {
635 /// Increment counter
636 $i++;
637 $content = $comment->content;
638 $result = restore_decode_content_links_worker($content,$restore);
639 if ($result != $content) {
640 /// Update record
641 $comment->content = addslashes($result);
642 $status = update_record("data_comments",$comment);
643 if (debugging()) {
644 if (!defined('RESTORE_SILENTLY')) {
645 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
649 /// Do some output
650 if (($i+1) % 5 == 0) {
651 if (!defined('RESTORE_SILENTLY')) {
652 echo ".";
653 if (($i+1) % 100 == 0) {
654 echo "<br />";
657 backup_flush(300);
662 /// Process every CONTENT (content, content1, content2, content3, content4) in the course
663 if ($contents = get_records_sql ("SELECT dc.id, dc.content, dc.content1, dc.content2, dc.content3, dc.content4
664 FROM {$CFG->prefix}data d,
665 {$CFG->prefix}data_records dr,
666 {$CFG->prefix}data_content dc
667 WHERE d.course = $restore->course_id
668 AND dr.dataid = d.id
669 AND dc.recordid = dr.id")) {
670 /// Iterate over each data_content->content, content1, content2, content3 and content4
671 $i = 0; //Counter to send some output to the browser to avoid timeouts
672 foreach ($contents as $cnt) {
673 /// Increment counter
674 $i++;
675 $content = $cnt->content;
676 $content1 = $cnt->content1;
677 $content2 = $cnt->content2;
678 $content3 = $cnt->content3;
679 $content4 = $cnt->content4;
680 $result = restore_decode_content_links_worker($content,$restore);
681 $result1 = restore_decode_content_links_worker($content1,$restore);
682 $result2 = restore_decode_content_links_worker($content2,$restore);
683 $result3 = restore_decode_content_links_worker($content3,$restore);
684 $result4 = restore_decode_content_links_worker($content4,$restore);
685 if ($result != $content || $result1 != $content1 || $result2 != $content2 ||
686 $result3 != $content3 || $result4 != $content4) {
687 /// Unset fields to update only the necessary ones
688 unset($cnt->content);
689 unset($cnt->content1);
690 unset($cnt->content2);
691 unset($cnt->content3);
692 unset($cnt->content4);
693 /// Conditionally set the fields
694 if ($result != $content) {
695 $cnt->content = addslashes($result);
697 if ($result1 != $content1) {
698 $cnt->content1 = addslashes($result1);
700 if ($result2 != $content2) {
701 $cnt->content2 = addslashes($result2);
703 if ($result3 != $content3) {
704 $cnt->content3 = addslashes($result3);
706 if ($result4 != $content4) {
707 $cnt->content4 = addslashes($result4);
709 /// Update record with the changed fields
710 $status = update_record("data_content",$cnt);
711 if (debugging()) {
712 if (!defined('RESTORE_SILENTLY')) {
713 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
717 /// Do some output
718 if (($i+1) % 5 == 0) {
719 if (!defined('RESTORE_SILENTLY')) {
720 echo ".";
721 if (($i+1) % 100 == 0) {
722 echo "<br />";
725 backup_flush(300);
730 return $status;