MDL-31515 Allow for subsequent files to overwrite old files in database activity...
[moodle.git] / mod / data / field / file / field.class.php
CommitLineData
1adbd2c3 1<?php
3d4b223a 2///////////////////////////////////////////////////////////////////////////
3// //
4// NOTICE OF COPYRIGHT //
5// //
6// Moodle - Modular Object-Oriented Dynamic Learning Environment //
7// http://moodle.org //
8// //
0997e51a 9// Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
3d4b223a 10// //
11// This program is free software; you can redistribute it and/or modify //
12// it under the terms of the GNU General Public License as published by //
13// the Free Software Foundation; either version 2 of the License, or //
14// (at your option) any later version. //
15// //
16// This program is distributed in the hope that it will be useful, //
17// but WITHOUT ANY WARRANTY; without even the implied warranty of //
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19// GNU General Public License for more details: //
20// //
21// http://www.gnu.org/copyleft/gpl.html //
22// //
23///////////////////////////////////////////////////////////////////////////
24
0997e51a 25class data_field_file extends data_field_base {
3d4b223a 26 var $type = 'file';
3d4b223a 27
0997e51a 28 function display_add_field($recordid=0) {
26032c2d 29 global $CFG, $DB, $OUTPUT, $PAGE, $USER;
8429163d 30
31 $file = false;
32 $content = false;
33 $displayname = '';
34 $fs = get_file_storage();
26032c2d
DC
35 $context = $PAGE->context;
36 $itemid = null;
37
38 // editing an existing database entry
0997e51a 39 if ($recordid){
a656d951 40 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
26032c2d 41
64f93798 42 file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id);
26032c2d 43
8429163d 44 if (!empty($content->content)) {
64f93798 45 if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
26032c2d 46 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
64f93798 47 if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid, 'id DESC', false)) {
26032c2d
DC
48 return false;
49 }
8429163d 50 if (empty($content->content1)) {
26032c2d 51 // Print icon if file already exists
50a8bd6c 52 $src = moodle_url::make_draftfile_url($itemid, '/', $file->get_filename());
26032c2d
DC
53 $displayname = '<img src="'.$OUTPUT->pix_url(file_mimetype_icon($file->get_mimetype())).'" class="icon" alt="'.$file->get_mimetype().'" />'. '<a href="'.$src.'" >'.s($file->get_filename()).'</a>';
54
8429163d 55 } else {
26032c2d 56 $displayname = 'no file added';
8429163d 57 }
58 }
59 }
3d4b223a 60 }
26032c2d
DC
61 } else {
62 $itemid = file_get_unused_draft_itemid();
3d4b223a 63 }
8429163d 64
e30d75cc
DC
65 $html = '';
66 // database entry label
67 $html .= '<div title="'.s($this->field->description).'">';
68 $html .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
69
70 // itemid element
71 $html .= '<input type="hidden" name="field_'.$this->field->id.'_file" value="'.$itemid.'" />';
26032c2d 72
6bdfef5d 73 $options = new stdClass();
e4256380
DC
74 $options->maxbytes = $this->field->param3;
75 $options->itemid = $itemid;
76 $options->accepted_types = '*';
77 $options->return_types = FILE_INTERNAL;
78 $options->context = $PAGE->context;
79
80 $fp = new file_picker($options);
81 // print out file picker
82 $html .= $OUTPUT->render($fp);
26032c2d 83
e30d75cc
DC
84 $html .= '</fieldset>';
85 $html .= '</div>';
26032c2d 86
e4256380
DC
87 $module = array('name'=>'data_filepicker', 'fullpath'=>'/mod/data/data.js', 'requires'=>array('core_filepicker'));
88 $PAGE->requires->js_init_call('M.data_filepicker.init', array($fp->options), true, $module);
89
e30d75cc 90 return $html;
3d4b223a 91 }
92
7900ecb0 93 function display_search_field($value = '') {
2858af42 94 return '<input type="text" size="16" name="f_'.$this->field->id.'" value="'.$value.'" />';
7900ecb0 95 }
2858af42 96
7900ecb0 97 function generate_sql($tablealias, $value) {
e3487936 98 global $DB;
99
e3487936 100 static $i=0;
101 $i++;
102 $name = "df_file_$i";
800bb0f7 103 return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
7900ecb0 104 }
7900ecb0 105
106 function parse_search_field() {
107 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
108 }
109
276378e6 110 function get_file($recordid, $content=null) {
111 global $DB;
112 if (empty($content)) {
113 if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
114 return null;
115 }
116 }
117 $fs = get_file_storage();
64f93798 118 if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
276378e6 119 return null;
120 }
121
122 return $file;
123 }
124
0997e51a 125 function display_browse_field($recordid, $template) {
26032c2d 126 global $CFG, $DB, $OUTPUT;
a656d951 127
128 if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
8429163d 129 return '';
1dfbf846 130 }
8429163d 131
1dfbf846 132 if (empty($content->content)) {
133 return '';
134 }
8429163d 135
276378e6 136 if (!$file = $this->get_file($recordid, $content)) {
8429163d 137 return '';
138 }
139
140 $name = empty($content->content1) ? $file->get_filename() : $content->content1;
64f93798 141 $src = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_data/content/'.$content->id.'/'.$file->get_filename());
8429163d 142 $width = $this->field->param1 ? ' width = "'.s($this->field->param1).'" ':' ';
143 $height = $this->field->param2 ? ' height = "'.s($this->field->param2).'" ':' ';
144
b5d0cafc 145 $str = '<img src="'.$OUTPUT->pix_url(file_mimetype_icon($file->get_mimetype())).'" height="16" width="16" alt="'.$file->get_mimetype().'" />&nbsp;'.
8429163d 146 '<a href="'.$src.'" >'.s($name).'</a>';
1dfbf846 147 return $str;
3d4b223a 148 }
149
150
2858af42 151 // content: "a##b" where a is the file name, b is the display name
0997e51a 152 function update_content($recordid, $value, $name) {
26032c2d 153 global $CFG, $DB, $USER;
e4256380 154 $fs = get_file_storage();
a656d951 155
8429163d 156 if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
e4256380 157
2858af42 158 // Quickly make one now!
39790bd8 159 $content = new stdClass();
8429163d 160 $content->fieldid = $this->field->id;
161 $content->recordid = $recordid;
7826abc7 162 $id = $DB->insert_record('data_content', $content);
8429163d 163 $content = $DB->get_record('data_content', array('id'=>$id));
0997e51a 164 }
8429163d 165
e4256380 166 // delete existing files
64f93798 167 $fs->delete_area_files($this->context->id, 'mod_data', 'content', $content->id);
e4256380 168
26032c2d 169 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
f0f41c5e 170 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value, 'timecreated DESC');
26032c2d
DC
171
172 if (count($files)<2) {
173 // no file
174 } else {
26032c2d 175 foreach ($files as $draftfile) {
26032c2d 176 if (!$draftfile->is_directory()) {
f0f41c5e
ARN
177 $file_record = array(
178 'contextid' => $this->context->id,
179 'component' => 'mod_data',
180 'filearea' => 'content',
181 'itemid' => $content->id,
182 'filepath' => '/',
183 'filename' => $draftfile->get_filename(),
184 );
0997e51a 185
f0f41c5e 186 $content->content = $file_record['filename'];
0997e51a 187
26032c2d
DC
188 $fs->create_file_from_storedfile($file_record, $draftfile);
189 $DB->update_record('data_content', $content);
190
f0f41c5e
ARN
191 // Break from the loop now to avoid overwriting the uploaded file record
192 break;
26032c2d
DC
193 }
194 }
3d4b223a 195 }
196 }
197
2858af42 198 function text_export_supported() {
199 return false;
200 }
201
8429163d 202 function file_ok($path) {
203 return true;
204 }
205
3d4b223a 206}
207
1adbd2c3 208