MDL-22145 backup - implemented storage into proper file areas

This commit is contained in:
Eloy Lafuente 2010-04-27 21:42:54 +00:00
parent 184aa32810
commit cd0034d860
4 changed files with 65 additions and 16 deletions

View File

@ -284,6 +284,7 @@ abstract class backup_controller_dbops extends backup_dbops {
$bc = self::load_controller($backupid); // Load controller
// Details info
$detailsinfo['id'] = $bc->get_id();
$detailsinfo['backup_id'] = $bc->get_backupid();
$detailsinfo['type'] = $bc->get_type();
$detailsinfo['format'] = $bc->get_format();
@ -292,6 +293,7 @@ abstract class backup_controller_dbops extends backup_dbops {
$detailsinfo['execution'] = $bc->get_execution();
$detailsinfo['executiontime'] = $bc->get_executiontime();
$detailsinfo['userid'] = $bc->get_userid();
$detailsinfo['courseid'] = $bc->get_courseid();
// Init content placeholders

View File

@ -177,30 +177,76 @@ abstract class backup_helper {
$hasusers = (bool)$sinfo['users']->value; // Backup has users
$isannon = (bool)$sinfo['anonymize']->value; // Backup is annonymzed
$backupmode= $dinfo[0]->mode; // Backup mode backup::MODE_GENERAL/IMPORT/HUB
$backuptype= $dinfo[0]->type; // Backup type backup::TYPE_1ACTIVITY/SECTION/COURSE
$userid = $dinfo[0]->userid; // User->id executing the backup
$id = $dinfo[0]->id; // Id of activity/section/course (depends of type)
$courseid = $dinfo[0]->courseid; // Id of the course
// Backups of type IMPORT aren't stored ever
if ($backupmode == backup::MODE_IMPORT) {
return true;
}
// Calculate file storage options of id being backup
$ctxid = 0;
$filearea = '';
$itemid = 0;
switch ($backuptype) {
case backup::TYPE_1ACTIVITY:
$ctxid = get_context_instance(CONTEXT_MODULE, $id)->id;
$filearea = 'activity_backup';
$itemid = 0;
break;
case backup::TYPE_1SECTION:
$ctxid = get_context_instance(CONTEXT_COURSE, $courseid)->id;
$filearea = 'section_backup';
$itemid = $id;
break;
case backup::TYPE_1COURSE:
$ctxid = get_context_instance(CONTEXT_COURSE, $courseid)->id;
$filearea = 'course_backup';
$itemid = 0;
break;
}
// Backups of type HUB (by definition never have user info)
// are sent to user's "user_tohub" file area. The upload process
// will be responsible for cleaning that filearea once finished
if ($backupmode == backup::MODE_HUB) {
$ctxid = get_context_instance(CONTEXT_USER, $userid)->id;
$fs = get_file_storage();
$fr = array(
'contextid' => $ctxid,
'filearea' => 'user_tohub',
'itemid' => 0,
'filepath' => '/',
'filename' => basename($filepath),
'userid' => $userid,
'timecreated' => time(),
'timemodified'=> time());
return $fs->create_file_from_pathname($fr, $filepath);
$filearea = 'user_tohub';
$itemid = 0;
}
// Backups without user info are sent to user's "user_backup"
// file area. Maintenance of such area is responsibility of
// the user via corresponding file manager frontend
if ($backupmode == backup::MODE_GENERAL && !$hasusers) {
$ctxid = get_context_instance(CONTEXT_USER, $userid)->id;
$filearea = 'user_backup';
$itemid = 0;
}
// Let's send the file to file storage, everything already defined
$fs = get_file_storage();
$fr = array(
'contextid' => $ctxid,
'filearea' => $filearea,
'itemid' => $itemid,
'filepath' => '/',
'filename' => basename($filepath),
'userid' => $userid,
'timecreated' => time(),
'timemodified'=> time());
// If file already exists, delete if before
// creating it again. This is BC behaviour - copy()
// overwrites by default
if ($fs->file_exists($fr['contextid'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename'])) {
$pathnamehash = $fs->get_pathname_hash($fr['contextid'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename']);
$sf = $fs->get_file_by_hash($pathnamehash);
$sf->delete();
}
return $fs->create_file_from_pathname($fr, $filepath);
}
}

View File

@ -131,8 +131,8 @@ abstract class base_setting {
switch ($this->status) {
case self::LOCKED_BY_PERMISSION:
throw new base_setting_exception('setting_locked_by_permission');
case self::LOCKED_BY_HIERARCHY:
throw new base_setting_exception('setting_locked_by_hierarchy');
case self::LOCKED_BY_CONFIG:
throw new base_setting_exception('setting_locked_by_config');
}
}
$oldvalue = $this->value;
@ -152,6 +152,7 @@ abstract class base_setting {
}
public function set_status($status) {
print_object('setting '. $this->name . ' to status ' . $status);
$status = $this->validate_status($status);
$oldstatus = $this->status;
$this->status = $status;

View File

@ -147,14 +147,14 @@ class setting_test extends UnitTestCase {
$this->assertEqual($e->errorcode, 'setting_locked_by_permission');
}
// Try to change value of locked setting by permission
$bs = new mock_base_setting('test', base_setting::IS_BOOLEAN, null, null, base_setting::LOCKED_BY_HIERARCHY);
// Try to change value of locked setting by config
$bs = new mock_base_setting('test', base_setting::IS_BOOLEAN, null, null, base_setting::LOCKED_BY_CONFIG);
try {
$bs->set_value(true);
$this->assertTrue(false, 'base_setting_exception expected');
} catch (exception $e) {
$this->assertTrue($e instanceof base_setting_exception);
$this->assertEqual($e->errorcode, 'setting_locked_by_hierarchy');
$this->assertEqual($e->errorcode, 'setting_locked_by_config');
}
// Try to add same setting twice