context;
$itemid = null;
// editing an existing database entry
if ($recordid){
if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id);
if (!empty($content->content)) {
if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
$usercontext = context_user::instance($USER->id);
if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid, 'id DESC', false)) {
return false;
}
if (empty($content->content1)) {
// Print icon if file already exists
$src = moodle_url::make_draftfile_url($itemid, '/', $file->get_filename());
$displayname = $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')). ''.s($file->get_filename()).'';
} else {
$displayname = 'no file added';
}
}
}
}
} else {
$itemid = file_get_unused_draft_itemid();
}
$html = '';
// database entry label
$html .= '
';
$html .= '
';
$html .= '';
return $html;
}
function display_search_field($value = '') {
return '' .
'';
}
function generate_sql($tablealias, $value) {
global $DB;
static $i=0;
$i++;
$name = "df_file_$i";
return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
}
function parse_search_field() {
return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
}
function get_file($recordid, $content=null) {
global $DB;
if (empty($content)) {
if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
return null;
}
}
$fs = get_file_storage();
if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
return null;
}
return $file;
}
function display_browse_field($recordid, $template) {
global $CFG, $DB, $OUTPUT;
if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
return '';
}
if (empty($content->content)) {
return '';
}
if (!$file = $this->get_file($recordid, $content)) {
return '';
}
$name = empty($content->content1) ? $file->get_filename() : $content->content1;
$src = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_data/content/'.$content->id.'/'.$file->get_filename());
$width = $this->field->param1 ? ' width = "'.s($this->field->param1).'" ':' ';
$height = $this->field->param2 ? ' height = "'.s($this->field->param2).'" ':' ';
$str = $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('width' => 16, 'height' => 16)). ' '.
''.s($name).'';
return $str;
}
// content: "a##b" where a is the file name, b is the display name
function update_content($recordid, $value, $name='') {
global $CFG, $DB, $USER;
$fs = get_file_storage();
if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
// Quickly make one now!
$content = new stdClass();
$content->fieldid = $this->field->id;
$content->recordid = $recordid;
$id = $DB->insert_record('data_content', $content);
$content = $DB->get_record('data_content', array('id'=>$id));
}
// delete existing files
$fs->delete_area_files($this->context->id, 'mod_data', 'content', $content->id);
$usercontext = context_user::instance($USER->id);
$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value, 'timecreated DESC');
if (count($files)<2) {
// no file
} else {
foreach ($files as $draftfile) {
if (!$draftfile->is_directory()) {
$file_record = array(
'contextid' => $this->context->id,
'component' => 'mod_data',
'filearea' => 'content',
'itemid' => $content->id,
'filepath' => '/',
'filename' => $draftfile->get_filename(),
);
$content->content = $file_record['filename'];
$fs->create_file_from_storedfile($file_record, $draftfile);
$DB->update_record('data_content', $content);
// Break from the loop now to avoid overwriting the uploaded file record
break;
}
}
}
}
function text_export_supported() {
return false;
}
function file_ok($path) {
return true;
}
}