if ($oldversion < 2012052100.00) {
- // Define field referencefileid to be added to files
+ // Define field referencefileid to be added to files.
$table = new xmldb_table('files');
- // Define field referencefileid to be added to files
+ // Define field referencefileid to be added to files.
$field = new xmldb_field('referencefileid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'sortorder');
- // Conditionally launch add field referencefileid
+
+ // Conditionally launch add field referencefileid.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
- // Define field referencelastsync to be added to files
+ // Define field referencelastsync to be added to files.
$field = new xmldb_field('referencelastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencefileid');
- // Conditionally launch add field referencelastsync
+
+ // Conditionally launch add field referencelastsync.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
- // Define field referencelifetime to be added to files
- $field = new xmldb_field('referencelastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencefileid');
- // Conditionally launch add field referencelifetime
+ // Define field referencelifetime to be added to files.
+ $field = new xmldb_field('referencelifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencelastsync');
+
+ // Conditionally launch add field referencelifetime.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Adding fields to table files_reference.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
- $table->add_field('fileid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('repositoryid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('lastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('lifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
FROM {files} f
LEFT JOIN {files_reference} r
ON f.referencefileid = r.id
- WHERE r.reference = ?";
+ WHERE r.reference = ? AND (f.component <> ? OR f.filearea <> ?)";
- $rs = $DB->get_recordset_sql($sql, array($str));
+ $rs = $DB->get_recordset_sql($sql, array($str, 'user', 'draft'));
$files = array();
foreach ($rs as $filerecord) {
$file = $this->get_file_instance($filerecord);
FROM {files} f
LEFT JOIN {files_reference} r
ON f.referencefileid = r.id
- WHERE r.reference = ?";
+ WHERE r.reference = ? AND (f.component <> ? OR f.filearea <> ?)";
- $count = $DB->count_records_sql($sql, array($str));
+ $count = $DB->count_records_sql($sql, array($str, 'user', 'draft'));
return $count;
}
FROM {files} f
LEFT JOIN {files_reference} r
ON f.referencefileid = r.id
- WHERE r.reference = ?";
+ WHERE r.reference = ? AND (f.component <> ? OR f.filearea <> ?)";
- $rs = $DB->get_recordset_sql($sql, array($reference));
+ $rs = $DB->get_recordset_sql($sql, array($reference, 'user', 'draft'));
$files = array();
foreach ($rs as $filerecord) {
$file = $this->get_file_instance($filerecord);
FROM {files} f
LEFT JOIN {files_reference} r
ON f.referencefileid = r.id
- WHERE r.reference = ?";
+ WHERE r.reference = ? AND (f.component <> ? OR f.filearea <> ?)";
- $count = $DB->count_records_sql($sql, array($reference));
+ $count = $DB->count_records_sql($sql, array($reference, 'user', 'draft'));
return $count;
}
* @param string $filename file name
*/
public function rename($filepath, $filename) {
+ if ($this->fs->file_exists($this->get_contextid(), $this->get_component(), $this->get_filearea(), $this->get_itemid(), $filepath, $filename)) {
+ throw new file_exception('storedfilenotcreated', '', 'file exists, cannot rename');
+ }
$filerecord = new stdClass;
$filerecord->filepath = $filepath;
$filerecord->filename = $filename;
$contenthash = $originalfile->get_contenthash();
$newpath = '/test/';
$newname = 'newtest.txt';
+ // try break it
+ $this->setExpectedException('file_exception');
+ // this shall throw exception
+ $originalfile->rename($filepath, $filename);
+ // this should work
$originalfile->rename($newpath, $newname);
-
$file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $newpath, $newname);
$this->assertInstanceOf('stored_file', $file);
$this->assertEquals($contenthash, $file->get_contenthash());
$usercontext = context_user::instance($user->id);
$USER = $DB->get_record('user', array('id'=>$user->id));
+ $repositorypluginname = 'user';
+
+ $args = array();
+ $args['type'] = $repositorypluginname;
+ $repos = repository::get_instances($args);
+ $userrepository = reset($repos);
+ $this->assertInstanceOf('repository', $userrepository);
$fs = get_file_storage();
+
$syscontext = context_system::instance();
$component = 'core';
$filearea = 'unittest';
'source' => $sourcefield,
);
$ref = $fs->pack_reference($filerecord);
-
$originalfile = $fs->create_file_from_string($filerecord, 'Test content');
-
$fileid = $originalfile->get_id();
$this->assertInstanceOf('stored_file', $originalfile);
+ // create a user private file
+ $userfilerecord = new stdClass;
+ $userfilerecord->contextid = $usercontext->id;
+ $userfilerecord->component = 'user';
+ $userfilerecord->filearea = 'private';
+ $userfilerecord->itemid = 0;
+ $userfilerecord->filepath = '/';
+ $userfilerecord->filename = 'userfile.txt';
+ $userfilerecord->source = 'test';
+ $userfile = $fs->create_file_from_string($userfilerecord, 'User file content');
+ $userfileref = $fs->pack_reference($userfilerecord);
+
+ $filerefrecord = clone((object)$filerecord);
+ $filerefrecord->filename = 'testref.txt';
+ // create a file reference
+ $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref);
+ $this->assertInstanceOf('stored_file', $fileref);
+ $this->assertEquals($userrepository->id, $fileref->repository->id);
+ $this->assertEquals($userfile->get_contenthash(), $fileref->get_contenthash());
+ $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
+ $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
+
$draftitemid = 0;
file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid);
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
- $this->assertEquals(2, count($draftfiles));
+ $this->assertEquals(3, count($draftfiles));
$draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename);
$source = unserialize($draftfile->get_source());
$this->assertEquals($ref, $source->original);
$this->assertEquals($sourcefield, $source->source);
+ $draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename);
+ $this->assertInstanceOf('stored_file', $draftfileref);
+ $this->assertEquals(true, $draftfileref->is_external_file());
// change some information
$author = 'Dongsheng Cai';
$draftfile->set_author($author);
- $newsourcefield = 'Get from flickr';
+ $newsourcefield = 'Get from Flickr';
$license = 'GPLv3';
$draftfile->set_license($license);
// if you want to really just change source field, do this: