Hi,
I have developed a block with an option that allows me to restore a course from another site. At the remote site I have activated the automatic copies that I save on an NFS disk that I mount at the site where the block that allows restoring resides. In essence, the development consists of activating a web service in the remote site and a client of that service to obtain the user's courses in that site to build a form where to select the remote course to restore and the place to restore it.
Once the remote course has been selected, the development temporarily moves it to the filespace of the local course where it can be restored, unpacks it in / temp / backup of moodledata and proceeds to restore it.
In general, the program launched from an option of the developed block works well but I have two problems:
** Some remote courses are not restored and not necessarily large courses. Most of the time it restores without problems but sometimes (less) it does not. It occurs for courses large or small in size. Although I have checked the php LOGS I have not found the cause.
** The main problem is that I cannot show the progress bar of the restoration operation, which causes some users not to know the status of the same and refresh the browser or try to restore the same course from another browser.
This is the code developed where I would like to know if I am programming something wrong for those few courses that cannot be restored regardless of their size and especially why the progress bar is not displayed.
function restorebackup($data, $backup){
global $DB, $CFG, $USER, $PAGE;
echo "\n<center><h3><b>Preparando la restauración. Espere un momento...</b></h3></center>";
//Establecemos la ruta por defecto a la carpeta compartida entre todos los servicios
$config = get_config('block_adminprofesorado');
$directoriocompar = $config->pathcomapartida.$backup['cursoacademico'].'/';
//Rutas para utilizal el bloque en WINDOWS
$rutawin = $config->pathcomapartidaw;
$rutawinbusqueda = $config->rutawinbusqueda;
//Movemos el fichero de la particion compartida al area de backup del curso destino
//Esta ruta hay que modificarla por la ruta del directorio compartido #########################################
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$from_zip_file = $rutawin . $backup['filename'];
}else{
$from_zip_file = $directoriocompar . $backup['filename'];
}
$context = context_course::instance($data->Nuevo_curso);
$fs = get_file_storage();
$file_record = array('contextid'=>$context->id, 'component'=>'backup', 'userid'=>$USER->id,'filearea'=>'course',
'itemid'=>0, 'filepath'=>'/', 'filename'=>$backup['filename'],
'timecreated'=>time(), 'timemodified'=>time());
$file=$fs->create_file_from_pathname($file_record, $from_zip_file);
// Obtenemos el campo contenhash para buscar el directorio donde se encuentra el fichero
$componentnuevo = 'backup';
$fileareanuevo = 'course';
$contenthash=$DB->get_record('files', array('filename'=>$backup['filename'],'component'=>$componentnuevo,'userid'=>$USER->id,'filearea'=>$fileareanuevo));
$rutafiledir = $CFG->dataroot .'/filedir';
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
//LO QUE QUIERAS HACER SI ES WINDOWS
$rutabackup = shell_exec('forfiles /p '.$rutawinbusqueda.' /s /m '.$contenthash->contenthash.' /c "cmd /c echo @path"');
// Quitamos las comillas dobles de la ruta
$str = str_replace('"', "", $rutabackup, $count);
//Eliminamos los espacios en blanco al principio y final de la cadena
$pathss = trim($str);
} else {
// HAY QUE CAMBIAR LOS COMANDOS PARA QUE FUNCIONE EN LINUX ##############################################################################
//LO QUE QUIERAS HACER SI ES LINUX (O AL MENOS NO WINDOWS)
$rutabackup = shell_exec('find '.$rutafiledir.' -name '. $contenthash->contenthash);
//Eliminamos los espacios en blanco al principio y final de la cadena
$pathss = trim($rutabackup, " \t\n\r\0\x0B");
}
// Iniciamos el backup desde el area de backup del curso
$rand = $USER->id;
while (strlen($rand) < 10) {
$rand = '0' . $rand;
}
$rand .= rand();
check_dir_exists($CFG->dataroot . '/temp/backup');
$packer = get_file_packer('application/vnd.moodle.backup');
$packer->extract_to_pathname($pathss, $CFG->dataroot . '/temp/backup/' . $rand, null, null);
$rutatemp = $rand;
//Capturamos el campo Summary del Curso para volver a colocarlo al final de la restauracion
$summarycursonuevo = $DB->get_field('course', 'summary', array('id'=>$data->Nuevo_curso));
//echo ('Campo sumario: '.$summarycursonuevo);
//Borramos todo el contenido del curso antes de restaurarlo con las siguientes lineas ##########################
$tiporestauracion = backup::TARGET_EXISTING_DELETING;
if ($tiporestauracion == backup::TARGET_CURRENT_DELETING || $tiporestauracion == backup::TARGET_EXISTING_DELETING) {
$options = array();
$options['keep_roles_and_enrolments'] = new restore_course_generic_setting('keep_roles_and_enrolments', base_setting::IS_BOOLEAN, true);
$options['keep_groups_and_groupings'] = new restore_course_generic_setting('keep_groups_and_groupings', base_setting::IS_BOOLEAN, true);
//necesita que le pasemos el ID del curso actual y el array de opciones para excluir las matriculas y grupos de usuarios
restore_dbops::delete_course_content($data->Nuevo_curso, $options);
}
$renderer = $PAGE->get_renderer('core','backup');
$slowprogress = new \core\progress\display_if_slow('<center><b>Restaurando curso.... esta operación puede tardar unos minutos dependiendo del tamaño del curso origen</center></b>');
$slowprogress->start_progress('', 10);
$slowprogress->start_progress('', 1, 1);
raise_memory_limit(MEMORY_EXTRA);
$controller = new restore_controller($rutatemp, $data->Nuevo_curso, backup::INTERACTIVE_NO,
backup::MODE_AUTOMATED, $USER->id, backup::TARGET_EXISTING_DELETING);
$progressbar=$controller->set_progress($slowprogress);
$slowprogress->end_progress();
$slowprogress->end_progress();
$controller->execute_precheck();
$controller->execute_plan();
//Volvemos a añadir el campo summary al curso
$restoresumario = new stdclass;
$restoresumario->id = $data->Nuevo_curso;
$restoresumario->summary = $summarycursonuevo;
$sqlsumarionuevo = $DB->update_record('course', $restoresumario);
}
It remains to be said that the courses to restore completely delete the content of the local course where it is restored and I do not allow the restoration moodle interface to be displayed that allows the user to select which activities or resources to restore. For me this part is not necessary because the entire remote course is restored without user data.
Thanks.