Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/restorelib.php
Go to the documentation of this file.
00001 <?php
00002     //This function iterates over all modules in backup file, searching for a
00003     //MODNAME_refresh_events() to execute. Perhaps it should ve moved to central Moodle...
00004     function restore_refresh_events($restore) {
00005 
00006         global $CFG;
00007         $status = true;
00008 
00009         //Take all modules in backup
00010         $modules = $restore->mods;
00011         //Iterate
00012         foreach($modules as $name => $module) {
00013             //Only if the module is being restored
00014             if (isset($module->restore) && $module->restore == 1) {
00015                 //Include module library
00016                 include_once("$CFG->dirroot/mod/$name/lib.php");
00017                 //If module_refresh_events exists
00018                 $function_name = $name."_refresh_events";
00019                 if (function_exists($function_name)) {
00020                     $status = $function_name($restore->course_id);
00021                 }
00022             }
00023         }
00024         return $status;
00025     }
00026 
00027     //Called to set up any course-format specific data that may be in the file
00028     function restore_set_format_data($restore,$xml_file) {
00029         global $CFG, $DB;
00030 
00031         $status = true;
00032         //Check it exists
00033         if (!file_exists($xml_file)) {
00034             return false;
00035         }
00036         //Load data from XML to info
00037         if(!($info = restore_read_xml_formatdata($xml_file))) {
00038                 return false;
00039         }
00040 
00041         //Process format data if there is any
00042         if (isset($info->format_data)) {
00043                 if(!$format=$DB->get_field('course','format', array('id'=>$restore->course_id))) {
00044                     return false;
00045                 }
00046                 // If there was any data then it must have a restore method
00047                 $file=$CFG->dirroot."/course/format/$format/restorelib.php";
00048                 if(!file_exists($file)) {
00049                     return false;
00050                 }
00051                 require_once($file);
00052                 $function=$format.'_restore_format_data';
00053                 if(!function_exists($function)) {
00054                     return false;
00055                 }
00056                 return $function($restore,$info->format_data);
00057         }
00058 
00059         // If we got here then there's no data, but that's cool
00060         return true;
00061     }
00062 
00063     //This function creates all the structures messages and contacts
00064     function restore_create_messages($restore,$xml_file) {
00065         global $CFG, $DB;
00066 
00067         $status = true;
00068         //Check it exists
00069         if (!file_exists($xml_file)) {
00070             $status = false;
00071         }
00072         //Get info from xml
00073         if ($status) {
00074             //info will contain the id and name of every table
00075             //(message, message_read and message_contacts)
00076             //in backup_ids->info will be the real info (serialized)
00077             $info = restore_read_xml_messages($restore,$xml_file);
00078 
00079             //If we have info, then process messages & contacts
00080             if ($info > 0) {
00081                 //Count how many we have
00082                 $unreadcount  = $DB->count_records ('backup_ids', array('backup_code'=>$restore->backup_unique_code, 'table_name'=>'message'));
00083                 $readcount    = $DB->count_records ('backup_ids', array('backup_code'=>$restore->backup_unique_code, 'table_name'=>'message_read'));
00084                 $contactcount = $DB->count_records ('backup_ids', array('backup_code'=>$restore->backup_unique_code, 'table_name'=>'message_contacts'));
00085                 if ($unreadcount || $readcount || $contactcount) {
00086                     //Start ul
00087                     if (!defined('RESTORE_SILENTLY')) {
00088                         echo '<ul>';
00089                     }
00090                     //Number of records to get in every chunk
00091                     $recordset_size = 4;
00092 
00093                     //Process unread
00094                     if ($unreadcount) {
00095                         if (!defined('RESTORE_SILENTLY')) {
00096                             echo '<li>'.get_string('unreadmessages','message').'</li>';
00097                         }
00098                         $counter = 0;
00099                         while ($counter < $unreadcount) {
00100                             //Fetch recordset_size records in each iteration
00101                             $recs = $DB->get_records("backup_ids", array('table_name'=>'message', 'backup_code'=>$restore->backup_unique_code),"old_id","old_id",$counter,$recordset_size);
00102                             if ($recs) {
00103                                 foreach ($recs as $rec) {
00104                                     //Get the full record from backup_ids
00105                                     $data = backup_getid($restore->backup_unique_code,"message",$rec->old_id);
00106                                     if ($data) {
00107                                         //Now get completed xmlized object
00108                                         $info = $data->info;
00109                                         //traverse_xmlize($info);                            //Debug
00110                                         //print_object ($GLOBALS['traverse_array']);         //Debug
00111                                         //$GLOBALS['traverse_array']="";                     //Debug
00112                                         //Now build the MESSAGE record structure
00113                                         $dbrec = new stdClass();
00114                                         $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
00115                                         $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
00116                                         $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
00117                                         $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
00118                                         $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
00119                                         $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
00120                                         //We have to recode the useridfrom field
00121                                         $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
00122                                         if ($user) {
00123                                             //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />";   //Debug
00124                                             $dbrec->useridfrom = $user->new_id;
00125                                         }
00126                                         //We have to recode the useridto field
00127                                         $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
00128                                         if ($user) {
00129                                             //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />";   //Debug
00130                                             $dbrec->useridto = $user->new_id;
00131                                         }
00132                                         //Check if the record doesn't exist in DB!
00133                                         $exist = $DB->get_record('message', array('useridfrom'=>$dbrec->useridfrom,
00134                                                                                   'useridto'=>$dbrec->useridto,
00135                                                                                   'timecreated'=>$dbrec->timecreated));
00136                                         if (!$exist) {
00137                                             //Not exist. Insert
00138                                             $status = $DB->insert_record('message',$dbrec);
00139                                         } else {
00140                                             //Duplicate. Do nothing
00141                                         }
00142                                     }
00143                                     //Do some output
00144                                     $counter++;
00145                                     if ($counter % 10 == 0) {
00146                                         if (!defined('RESTORE_SILENTLY')) {
00147                                             echo ".";
00148                                             if ($counter % 200 == 0) {
00149                                                 echo "<br />";
00150                                             }
00151                                         }
00152                                         backup_flush(300);
00153                                     }
00154                                 }
00155                             }
00156                         }
00157                     }
00158 
00159                     //Process read
00160                     if ($readcount) {
00161                         if (!defined('RESTORE_SILENTLY')) {
00162                             echo '<li>'.get_string('readmessages','message').'</li>';
00163                         }
00164                         $counter = 0;
00165                         while ($counter < $readcount) {
00166                             //Fetch recordset_size records in each iteration
00167                             $recs = $DB->get_records("backup_ids", array('table_name'=>'message_read', 'backup_code'=>$restore->backup_unique_code),"old_id","old_id",$counter,$recordset_size);
00168                             if ($recs) {
00169                                 foreach ($recs as $rec) {
00170                                     //Get the full record from backup_ids
00171                                     $data = backup_getid($restore->backup_unique_code,"message_read",$rec->old_id);
00172                                     if ($data) {
00173                                         //Now get completed xmlized object
00174                                         $info = $data->info;
00175                                         //traverse_xmlize($info);                            //Debug
00176                                         //print_object ($GLOBALS['traverse_array']);         //Debug
00177                                         //$GLOBALS['traverse_array']="";                     //Debug
00178                                         //Now build the MESSAGE_READ record structure
00179                                         $dbrec->useridfrom = backup_todb($info['MESSAGE']['#']['USERIDFROM']['0']['#']);
00180                                         $dbrec->useridto = backup_todb($info['MESSAGE']['#']['USERIDTO']['0']['#']);
00181                                         $dbrec->message = backup_todb($info['MESSAGE']['#']['MESSAGE']['0']['#']);
00182                                         $dbrec->format = backup_todb($info['MESSAGE']['#']['FORMAT']['0']['#']);
00183                                         $dbrec->timecreated = backup_todb($info['MESSAGE']['#']['TIMECREATED']['0']['#']);
00184                                         $dbrec->messagetype = backup_todb($info['MESSAGE']['#']['MESSAGETYPE']['0']['#']);
00185                                         $dbrec->timeread = backup_todb($info['MESSAGE']['#']['TIMEREAD']['0']['#']);
00186                                         $dbrec->mailed = backup_todb($info['MESSAGE']['#']['MAILED']['0']['#']);
00187                                         //We have to recode the useridfrom field
00188                                         $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridfrom);
00189                                         if ($user) {
00190                                             //echo "User ".$dbrec->useridfrom." to user ".$user->new_id."<br />";   //Debug
00191                                             $dbrec->useridfrom = $user->new_id;
00192                                         }
00193                                         //We have to recode the useridto field
00194                                         $user = backup_getid($restore->backup_unique_code,"user",$dbrec->useridto);
00195                                         if ($user) {
00196                                             //echo "User ".$dbrec->useridto." to user ".$user->new_id."<br />";   //Debug
00197                                             $dbrec->useridto = $user->new_id;
00198                                         }
00199                                         //Check if the record doesn't exist in DB!
00200                                         $exist = $DB->get_record('message_read', array('useridfrom'=>$dbrec->useridfrom,
00201                                                                                        'useridto'=>$dbrec->useridto,
00202                                                                                        'timecreated'=>$dbrec->timecreated));
00203                                         if (!$exist) {
00204                                             //Not exist. Insert
00205                                             $status = $DB->insert_record('message_read',$dbrec);
00206                                         } else {
00207                                             //Duplicate. Do nothing
00208                                         }
00209                                     }
00210                                     //Do some output
00211                                     $counter++;
00212                                     if ($counter % 10 == 0) {
00213                                         if (!defined('RESTORE_SILENTLY')) {
00214                                             echo ".";
00215                                             if ($counter % 200 == 0) {
00216                                                 echo "<br />";
00217                                             }
00218                                         }
00219                                         backup_flush(300);
00220                                     }
00221                                 }
00222                             }
00223                         }
00224                     }
00225 
00226                     //Process contacts
00227                     if ($contactcount) {
00228                         if (!defined('RESTORE_SILENTLY')) {
00229                             echo '<li>'.moodle_strtolower(get_string('contacts','message')).'</li>';
00230                         }
00231                         $counter = 0;
00232                         while ($counter < $contactcount) {
00233                             //Fetch recordset_size records in each iteration
00234                             $recs = $DB->get_records("backup_ids", array('table_name'=>'message_contacts', 'backup_code'=>$restore->backup_unique_code),"old_id","old_id",$counter,$recordset_size);
00235                             if ($recs) {
00236                                 foreach ($recs as $rec) {
00237                                     //Get the full record from backup_ids
00238                                     $data = backup_getid($restore->backup_unique_code,"message_contacts",$rec->old_id);
00239                                     if ($data) {
00240                                         //Now get completed xmlized object
00241                                         $info = $data->info;
00242                                         //traverse_xmlize($info);                            //Debug
00243                                         //print_object ($GLOBALS['traverse_array']);         //Debug
00244                                         //$GLOBALS['traverse_array']="";                     //Debug
00245                                         //Now build the MESSAGE_CONTACTS record structure
00246                                         $dbrec->userid = backup_todb($info['CONTACT']['#']['USERID']['0']['#']);
00247                                         $dbrec->contactid = backup_todb($info['CONTACT']['#']['CONTACTID']['0']['#']);
00248                                         $dbrec->blocked = backup_todb($info['CONTACT']['#']['BLOCKED']['0']['#']);
00249                                         //We have to recode the userid field
00250                                         $user = backup_getid($restore->backup_unique_code,"user",$dbrec->userid);
00251                                         if ($user) {
00252                                             //echo "User ".$dbrec->userid." to user ".$user->new_id."<br />";   //Debug
00253                                             $dbrec->userid = $user->new_id;
00254                                         }
00255                                         //We have to recode the contactid field
00256                                         $user = backup_getid($restore->backup_unique_code,"user",$dbrec->contactid);
00257                                         if ($user) {
00258                                             //echo "User ".$dbrec->contactid." to user ".$user->new_id."<br />";   //Debug
00259                                             $dbrec->contactid = $user->new_id;
00260                                         }
00261                                         //Check if the record doesn't exist in DB!
00262                                         $exist = $DB->get_record('message_contacts', array('userid'=>$dbrec->userid,
00263                                                                                            'contactid'=>$dbrec->contactid));
00264                                         if (!$exist) {
00265                                             //Not exist. Insert
00266                                             $status = $DB->insert_record('message_contacts',$dbrec);
00267                                         } else {
00268                                             //Duplicate. Do nothing
00269                                         }
00270                                     }
00271                                     //Do some output
00272                                     $counter++;
00273                                     if ($counter % 10 == 0) {
00274                                         if (!defined('RESTORE_SILENTLY')) {
00275                                             echo ".";
00276                                             if ($counter % 200 == 0) {
00277                                                 echo "<br />";
00278                                             }
00279                                         }
00280                                         backup_flush(300);
00281                                     }
00282                                 }
00283                             }
00284                         }
00285                     }
00286                     if (!defined('RESTORE_SILENTLY')) {
00287                         //End ul
00288                         echo '</ul>';
00289                     }
00290                 }
00291             }
00292         }
00293 
00294        return $status;
00295     }
00296 
00297     //This function creates all the structures for blogs and blog tags
00298     function restore_create_blogs($restore,$xml_file) {
00299         global $CFG, $DB;
00300 
00301         $status = true;
00302         //Check it exists
00303         if (!file_exists($xml_file)) {
00304             $status = false;
00305         }
00306         //Get info from xml
00307         if ($status) {
00308             //info will contain the number of blogs in the backup file
00309             //in backup_ids->info will be the real info (serialized)
00310             $info = restore_read_xml_blogs($restore,$xml_file);
00311 
00312             //If we have info, then process blogs & blog_tags
00313             if ($info > 0) {
00314                 //Count how many we have
00315                 $blogcount = $DB->count_records('backup_ids', array('backup_code'=>$restore->backup_unique_code, 'table_name'=>'blog'));
00316                 if ($blogcount) {
00317                     //Number of records to get in every chunk
00318                     $recordset_size = 4;
00319 
00320                     //Process blog
00321                     if ($blogcount) {
00322                         $counter = 0;
00323                         while ($counter < $blogcount) {
00324                             //Fetch recordset_size records in each iteration
00325                             $recs = $DB->get_records("backup_ids", array("table_name"=>'blog', 'backup_code'=>$restore->backup_unique_code),"old_id","old_id",$counter,$recordset_size);
00326                             if ($recs) {
00327                                 foreach ($recs as $rec) {
00328                                     //Get the full record from backup_ids
00329                                     $data = backup_getid($restore->backup_unique_code,"blog",$rec->old_id);
00330                                     if ($data) {
00331                                         //Now get completed xmlized object
00332                                         $info = $data->info;
00333                                         //traverse_xmlize($info);                            //Debug
00334                                         //print_object ($GLOBALS['traverse_array']);         //Debug
00335                                         //$GLOBALS['traverse_array']="";                     //Debug
00336                                         //Now build the BLOG record structure
00337                                         $dbrec = new stdClass();
00338                                         $dbrec->module = backup_todb($info['BLOG']['#']['MODULE']['0']['#']);
00339                                         $dbrec->userid = backup_todb($info['BLOG']['#']['USERID']['0']['#']);
00340                                         $dbrec->courseid = backup_todb($info['BLOG']['#']['COURSEID']['0']['#']);
00341                                         $dbrec->groupid = backup_todb($info['BLOG']['#']['GROUPID']['0']['#']);
00342                                         $dbrec->moduleid = backup_todb($info['BLOG']['#']['MODULEID']['0']['#']);
00343                                         $dbrec->coursemoduleid = backup_todb($info['BLOG']['#']['COURSEMODULEID']['0']['#']);
00344                                         $dbrec->subject = backup_todb($info['BLOG']['#']['SUBJECT']['0']['#']);
00345                                         $dbrec->summary = backup_todb($info['BLOG']['#']['SUMMARY']['0']['#']);
00346                                         $dbrec->content = backup_todb($info['BLOG']['#']['CONTENT']['0']['#']);
00347                                         $dbrec->uniquehash = backup_todb($info['BLOG']['#']['UNIQUEHASH']['0']['#']);
00348                                         $dbrec->rating = backup_todb($info['BLOG']['#']['RATING']['0']['#']);
00349                                         $dbrec->format = backup_todb($info['BLOG']['#']['FORMAT']['0']['#']);
00350                                         $dbrec->attachment = backup_todb($info['BLOG']['#']['ATTACHMENT']['0']['#']);
00351                                         $dbrec->publishstate = backup_todb($info['BLOG']['#']['PUBLISHSTATE']['0']['#']);
00352                                         $dbrec->lastmodified = backup_todb($info['BLOG']['#']['LASTMODIFIED']['0']['#']);
00353                                         $dbrec->created = backup_todb($info['BLOG']['#']['CREATED']['0']['#']);
00354                                         $dbrec->usermodified = backup_todb($info['BLOG']['#']['USERMODIFIED']['0']['#']);
00355 
00356                                         //We have to recode the userid field
00357                                         $user = backup_getid($restore->backup_unique_code,"user",$dbrec->userid);
00358                                         if ($user) {
00359                                             //echo "User ".$dbrec->userid." to user ".$user->new_id."<br />";   //Debug
00360                                             $dbrec->userid = $user->new_id;
00361                                         }
00362 
00363                                         //Check if the record doesn't exist in DB!
00364                                         $exist = $DB->get_record('post', array('userid'=>$dbrec->userid,
00365                                                                                'subject'=>$dbrec->subject,
00366                                                                                'created'=>$dbrec->created));
00367                                         $newblogid = 0;
00368                                         if (!$exist) {
00369                                             //Not exist. Insert
00370                                             $newblogid = $DB->insert_record('post',$dbrec);
00371                                         }
00372 
00373                                         //Going to restore related tags. Check they are enabled and we have inserted a blog
00374                                         if ($CFG->usetags && $newblogid) {
00375                                             //Look for tags in this blog
00376                                             if (isset($info['BLOG']['#']['BLOG_TAGS']['0']['#']['BLOG_TAG'])) {
00377                                                 $tagsarr = $info['BLOG']['#']['BLOG_TAGS']['0']['#']['BLOG_TAG'];
00378                                                 //Iterate over tags
00379                                                 $tags = array();
00380                                                 $sizetagsarr = sizeof($tagsarr);
00381                                                 for ($i = 0; $i < $sizetagsarr; $i++) {
00382                                                     $tag_info = $tagsarr[$i];
00386 
00387                                                     $name = backup_todb($tag_info['#']['NAME']['0']['#']);
00388                                                     $rawname = backup_todb($tag_info['#']['RAWNAME']['0']['#']);
00389 
00390                                                     $tags[] = $rawname;  //Rawname is all we need
00391                                                 }
00392                                                 tag_set('post', $newblogid, $tags); //Add all the tags in one API call
00393                                             }
00394                                         }
00395                                     }
00396                                     //Do some output
00397                                     $counter++;
00398                                     if ($counter % 10 == 0) {
00399                                         if (!defined('RESTORE_SILENTLY')) {
00400                                             echo ".";
00401                                             if ($counter % 200 == 0) {
00402                                                 echo "<br />";
00403                                             }
00404                                         }
00405                                         backup_flush(300);
00406                                     }
00407                                 }
00408                             }
00409                         }
00410                     }
00411                 }
00412             }
00413         }
00414 
00415         return $status;
00416     }
00417 
00418     //This function creates all the course events
00419     function restore_create_events($restore,$xml_file) {
00420         global $DB;
00421 
00422         global $CFG, $SESSION;
00423 
00424         $status = true;
00425         //Check it exists
00426         if (!file_exists($xml_file)) {
00427             $status = false;
00428         }
00429         //Get info from xml
00430         if ($status) {
00431             //events will contain the old_id of every event
00432             //in backup_ids->info will be the real info (serialized)
00433             $events = restore_read_xml_events($restore,$xml_file);
00434         }
00435 
00436         //Get admin->id for later use
00437         $admin = get_admin();
00438         $adminid = $admin->id;
00439 
00440         //Now, if we have anything in events, we have to restore that
00441         //events
00442         if ($events) {
00443             if ($events !== true) {
00444                 //Iterate over each event
00445                 foreach ($events as $event) {
00446                     //Get record from backup_ids
00447                     $data = backup_getid($restore->backup_unique_code,"event",$event->id);
00448                     //Init variables
00449                     $create_event = false;
00450 
00451                     if ($data) {
00452                         //Now get completed xmlized object
00453                         $info = $data->info;
00454                         //traverse_xmlize($info);                                                                     //Debug
00455                         //print_object ($GLOBALS['traverse_array']);                                                  //Debug
00456                         //$GLOBALS['traverse_array']="";                                                              //Debug
00457 
00458                         //if necessary, write to restorelog and adjust date/time fields
00459                         if ($restore->course_startdateoffset) {
00460                             restore_log_date_changes('Events', $restore, $info['EVENT']['#'], array('TIMESTART'));
00461                         }
00462 
00463                         //Now build the EVENT record structure
00464                         $eve = new stdClass();
00465                         $eve->name = backup_todb($info['EVENT']['#']['NAME']['0']['#']);
00466                         $eve->description = backup_todb($info['EVENT']['#']['DESCRIPTION']['0']['#']);
00467                         $eve->format = backup_todb($info['EVENT']['#']['FORMAT']['0']['#']);
00468                         $eve->courseid = $restore->course_id;
00469                         $eve->groupid = backup_todb($info['EVENT']['#']['GROUPID']['0']['#']);
00470                         $eve->userid = backup_todb($info['EVENT']['#']['USERID']['0']['#']);
00471                         $eve->repeatid = backup_todb($info['EVENT']['#']['REPEATID']['0']['#']);
00472                         $eve->modulename = "";
00473                         if (!empty($info['EVENT']['#']['MODULENAME'])) {
00474                             $eve->modulename = backup_todb($info['EVENT']['#']['MODULENAME']['0']['#']);
00475                         }
00476                         $eve->instance = 0;
00477                         $eve->eventtype = backup_todb($info['EVENT']['#']['EVENTTYPE']['0']['#']);
00478                         $eve->timestart = backup_todb($info['EVENT']['#']['TIMESTART']['0']['#']);
00479                         $eve->timeduration = backup_todb($info['EVENT']['#']['TIMEDURATION']['0']['#']);
00480                         $eve->visible = backup_todb($info['EVENT']['#']['VISIBLE']['0']['#']);
00481                         $eve->timemodified = backup_todb($info['EVENT']['#']['TIMEMODIFIED']['0']['#']);
00482 
00483                         //Now search if that event exists (by name, description, timestart fields) in
00484                         //restore->course_id course
00485                         //Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides.
00486                         $compare_description_clause = $DB->sql_compare_text('description')  . "=" .  $DB->sql_compare_text("'" . $eve->description . "'");
00487                         $eve_db = $DB->get_record_select('event',
00488                             "courseid = ? AND name = ? AND $compare_description_clause AND timestart = ?",
00489                             array($eve->courseid, $eve->name, $eve->timestart));
00490                         //If it doesn't exist, create
00491                         if (!$eve_db) {
00492                             $create_event = true;
00493                         }
00494                         //If we must create the event
00495                         if ($create_event) {
00496 
00497                             //We must recode the userid
00498                             $user = backup_getid($restore->backup_unique_code,"user",$eve->userid);
00499                             if ($user) {
00500                                 $eve->userid = $user->new_id;
00501                             } else {
00502                                 //Assign it to admin
00503                                 $eve->userid = $adminid;
00504                             }
00505 
00506                             //We have to recode the groupid field
00507                             $group = backup_getid($restore->backup_unique_code,"groups",$eve->groupid);
00508                             if ($group) {
00509                                 $eve->groupid = $group->new_id;
00510                             } else {
00511                                 //Assign it to group 0
00512                                 $eve->groupid = 0;
00513                             }
00514 
00515                             //The structure is equal to the db, so insert the event
00516                             $newid = $DB->insert_record ("event",$eve);
00517 
00518                             //We must recode the repeatid if the event has it
00519                             //The repeatid now refers to the id of the original event. (see Bug#5956)
00520                             if ($newid && !empty($eve->repeatid)) {
00521                                 $repeat_rec = backup_getid($restore->backup_unique_code,"event_repeatid",$eve->repeatid);
00522                                 if ($repeat_rec) {    //Exists, so use it...
00523                                     $eve->repeatid = $repeat_rec->new_id;
00524                                 } else {              //Doesn't exists, calculate the next and save it
00525                                     $oldrepeatid = $eve->repeatid;
00526                                     $eve->repeatid = $newid;
00527                                     backup_putid($restore->backup_unique_code,"event_repeatid", $oldrepeatid, $eve->repeatid);
00528                                 }
00529                                 $eve->id = $newid;
00530                                 // update the record to contain the correct repeatid
00531                                 $DB->update_record('event',$eve);
00532                             }
00533                         } else {
00534                             //get current event id
00535                             $newid = $eve_db->id;
00536                         }
00537                         if ($newid) {
00538                             //We have the newid, update backup_ids
00539                             backup_putid($restore->backup_unique_code,"event",
00540                                          $event->id, $newid);
00541                         }
00542                     }
00543                 }
00544             }
00545         } else {
00546             $status = false;
00547         }
00548         return $status;
00549     }
00550 
00551     function restore_execute(&$restore,$info,$course_header,&$errorstr) {
00552         global $CFG, $USER, $DB, $OUTPUT;
00553 
00554         $status = true;
00555 
00556         //Now create events as needed
00557         if ($status) {
00558             if (!defined('RESTORE_SILENTLY')) {
00559                 echo "<li>".get_string("creatingevents");
00560             }
00561             if (!$status = restore_create_events($restore,$xml_file)) {
00562                 if (!defined('RESTORE_SILENTLY')) {
00563                     echo $OUTPUT->notification("Could not restore course events!");
00564                 } else {
00565                     $errorstr = "Could not restore course events!";
00566                     return false;
00567                 }
00568             }
00569             if (!defined('RESTORE_SILENTLY')) {
00570                 echo '</li>';
00571             }
00572         }
00573 
00574         if ($status) {
00575             //If we are deleting and bringing into a course or making a new course, same situation
00576             if ($restore->restoreto == RESTORETO_CURRENT_DELETING ||
00577                 $restore->restoreto == RESTORETO_EXISTING_DELETING ||
00578                 $restore->restoreto == RESTORETO_NEW_COURSE) {
00579                 if (!defined('RESTORE_SILENTLY')) {
00580                     echo '<li>'.get_string('courseformatdata');
00581                 }
00582                 if (!$status = restore_set_format_data($restore, $xml_file)) {
00583                         $error = "Error while setting the course format data";
00584                     if (!defined('RESTORE_SILENTLY')) {
00585                         echo $OUTPUT->notification($error);
00586                     } else {
00587                         $errorstr=$error;
00588                         return false;
00589                     }
00590                 }
00591                 if (!defined('RESTORE_SILENTLY')) {
00592                     echo '</li>';
00593                 }
00594             }
00595         }
00596 
00597         //Now, if all is OK, adjust activity events
00598         if ($status) {
00599             if (!defined('RESTORE_SILENTLY')) {
00600                 echo "<li>".get_string("refreshingevents");
00601             }
00602             if (!$status = restore_refresh_events($restore)) {
00603                 if (!defined('RESTORE_SILENTLY')) {
00604                     echo $OUTPUT->notification("Could not refresh events for activities!");
00605                 } else {
00606                     $errorstr = "Could not refresh events for activities!";
00607                     return false;
00608                 }
00609             }
00610             if (!defined('RESTORE_SILENTLY')) {
00611                 echo '</li>';
00612             }
00613         }
00614     }
 All Data Structures Namespaces Files Functions Variables Enumerations