MDL-31515 Allow for subsequent files to overwrite old files in database activity...
[moodle.git] / mod / data / field / file / field.class.php
1 <?php
2 ///////////////////////////////////////////////////////////////////////////
3 //                                                                       //
4 // NOTICE OF COPYRIGHT                                                   //
5 //                                                                       //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
7 //          http://moodle.org                                            //
8 //                                                                       //
9 // Copyright (C) 1999-onwards Moodle Pty Ltd  http://moodle.com          //
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 ///////////////////////////////////////////////////////////////////////////
25 class data_field_file extends data_field_base {
26     var $type = 'file';
28     function display_add_field($recordid=0) {
29         global $CFG, $DB, $OUTPUT, $PAGE, $USER;
31         $file        = false;
32         $content     = false;
33         $displayname = '';
34         $fs = get_file_storage();
35         $context = $PAGE->context;
36         $itemid = null;
38         // editing an existing database entry
39         if ($recordid){
40             if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
42                 file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id);
44                 if (!empty($content->content)) {
45                     if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
46                         $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
47                         if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid, 'id DESC', false)) {
48                             return false;
49                         }
50                         if (empty($content->content1)) {
51                             // Print icon if file already exists
52                             $src = moodle_url::make_draftfile_url($itemid, '/', $file->get_filename());
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>';
55                         } else {
56                             $displayname = 'no file added';
57                         }
58                     }
59                 }
60             }
61         } else {
62             $itemid = file_get_unused_draft_itemid();
63         }
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>';
70         // itemid element
71         $html .= '<input type="hidden" name="field_'.$this->field->id.'_file" value="'.$itemid.'" />';
73         $options = new stdClass();
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;
80         $fp = new file_picker($options);
81         // print out file picker
82         $html .= $OUTPUT->render($fp);
84         $html .= '</fieldset>';
85         $html .= '</div>';
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);
90         return $html;
91     }
93     function display_search_field($value = '') {
94         return '<input type="text" size="16" name="f_'.$this->field->id.'" value="'.$value.'" />';
95     }
97     function generate_sql($tablealias, $value) {
98         global $DB;
100         static $i=0;
101         $i++;
102         $name = "df_file_$i";
103         return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%"));
104     }
106     function parse_search_field() {
107         return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
108     }
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();
118         if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
119             return null;
120         }
122         return $file;
123     }
125     function display_browse_field($recordid, $template) {
126         global $CFG, $DB, $OUTPUT;
128         if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
129             return '';
130         }
132         if (empty($content->content)) {
133             return '';
134         }
136         if (!$file = $this->get_file($recordid, $content)) {
137             return '';
138         }
140         $name   = empty($content->content1) ? $file->get_filename() : $content->content1;
141         $src    = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_data/content/'.$content->id.'/'.$file->get_filename());
142         $width  = $this->field->param1 ? ' width  = "'.s($this->field->param1).'" ':' ';
143         $height = $this->field->param2 ? ' height = "'.s($this->field->param2).'" ':' ';
145         $str = '<img src="'.$OUTPUT->pix_url(file_mimetype_icon($file->get_mimetype())).'" height="16" width="16" alt="'.$file->get_mimetype().'" />&nbsp;'.
146                '<a href="'.$src.'" >'.s($name).'</a>';
147         return $str;
148     }
151     // content: "a##b" where a is the file name, b is the display name
152     function update_content($recordid, $value, $name) {
153         global $CFG, $DB, $USER;
154         $fs = get_file_storage();
156         if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
158         // Quickly make one now!
159             $content = new stdClass();
160             $content->fieldid  = $this->field->id;
161             $content->recordid = $recordid;
162             $id = $DB->insert_record('data_content', $content);
163             $content = $DB->get_record('data_content', array('id'=>$id));
164         }
166         // delete existing files
167         $fs->delete_area_files($this->context->id, 'mod_data', 'content', $content->id);
169         $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
170         $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value, 'timecreated DESC');
172         if (count($files)<2) {
173             // no file
174         } else {
175             foreach ($files as $draftfile) {
176                 if (!$draftfile->is_directory()) {
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                     );
186                     $content->content = $file_record['filename'];
188                     $fs->create_file_from_storedfile($file_record, $draftfile);
189                     $DB->update_record('data_content', $content);
191                     // Break from the loop now to avoid overwriting the uploaded file record
192                     break;
193                 }
194             }
195         }
196     }
198     function text_export_supported() {
199         return false;
200     }
202     function file_ok($path) {
203         return true;
204     }