2 ///////////////////////////////////////////////////////////////////////////
4 // NOTICE OF COPYRIGHT //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
9 // Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com //
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. //
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: //
21 // http://www.gnu.org/copyleft/gpl.html //
23 ///////////////////////////////////////////////////////////////////////////
25 class data_field_file
extends data_field_base
{
28 function data_field_file($field=0, $data=0) {
29 parent
::data_field_base($field, $data);
32 function display_add_field($recordid=0) {
35 if ($content = get_record('data_content', 'fieldid', $this->field
->id
, 'recordid', $recordid)) {
36 $contents[0] = $content->content
;
37 $contents[1] = $content->content1
;
42 $src = empty($contents[0]) ?
'' : $contents[0];
43 $name = empty($contents[1]) ?
$src : $contents[1];
44 $displayname = empty($contents[1]) ?
'' : $contents[1];
45 require_once($CFG->libdir
.'/filelib.php');
46 $source = get_file_url($this->data
->course
.'/'.$CFG->moddata
.'/data/'.$this->data
->id
.'/'.$this->field
->id
.'/'.$recordid);
53 $str = '<div title="' . s($this->field
->description
) . '">';
54 $str .= '<fieldset><legend><span class="accesshide">'.$this->field
->name
.'</span></legend>';
55 $str .= '<input type="hidden" name ="field_'.$this->field
->id
.'_file" value="fakevalue" />';
56 $str .= get_string('file','data'). ' <input type="file" name ="field_'.$this->field
->id
.'" id="field_'.
57 $this->field
->id
.'" title="'.s($this->field
->description
).'" /><br />';
58 $str .= get_string('optionalfilename','data').' <input type="text" name="field_' .$this->field
->id
.'_filename"
59 id="field_'.$this->field
->id
.'_filename" value="'.s($displayname).'" /><br />';
60 $str .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.s($this->field
->param3
).'" />';
61 $str .= '</fieldset>';
63 if ($recordid and isset($content) and !empty($content->content
)) {
65 require_once($CFG->libdir
.'/filelib.php');
66 $icon = mimeinfo('icon', $src);
67 $str .= '<img src="'.$CFG->pixpath
.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />'.
68 '<a href="'.$source.'/'.$src.'" >'.$name.'</a>';
73 function display_search_field($value = '') {
74 return '<input type="text" size="16" name="f_'.$this->field
->id
.'" value="'.$value.'" />';
77 function generate_sql($tablealias, $value) {
78 return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content LIKE '%{$value}%') ";
81 function parse_search_field() {
82 return optional_param('f_'.$this->field
->id
, '', PARAM_NOTAGS
);
85 function display_browse_field($recordid, $template) {
87 if (!$content = get_record('data_content', 'fieldid', $this->field
->id
, 'recordid', $recordid)) {
90 $width = $this->field
->param1 ?
' width = "'.s($this->field
->param1
).'" ':' ';
91 $height = $this->field
->param2 ?
' height = "'.s($this->field
->param2
).'" ':' ';
92 if (empty($content->content
)) {
95 require_once($CFG->libdir
.'/filelib.php');
96 $src = $content->content
;
97 $name = empty($content->content1
) ?
$src : $content->content1
;
98 $source = get_file_url($this->data
->course
.'/'.$CFG->moddata
.'/data/'.$this->data
->id
.'/'.$this->field
->id
.'/'.$recordid);
99 $icon = mimeinfo('icon', $src);
100 $str = '<img src="'.$CFG->pixpath
.'/f/'.$icon.'" height="16" width="16" alt="'.$icon.'" /> '.
101 '<a href="'.$source.'/'.$src.'" >'.$name.'</a>';
106 // content: "a##b" where a is the file name, b is the display name
107 function update_content($recordid, $value, $name) {
109 if (!$oldcontent = get_record('data_content','fieldid', $this->field
->id
, 'recordid', $recordid)) {
110 // Quickly make one now!
111 $oldcontent = new object;
112 $oldcontent->fieldid
= $this->field
->id
;
113 $oldcontent->recordid
= $recordid;
114 if ($oldcontent->id
= insert_record('data_content', $oldcontent)) {
115 error('Could not make an empty record!');
118 $content = new object;
119 $content->id
= $oldcontent->id
;
120 $names = explode('_',$name);
123 // file just uploaded
124 # $course = get_course('course', 'id', $this->data->course);
125 $filename = $_FILES[$names[0].'_'.$names[1]];
126 $filename = $filename['name'];
127 $dir = $this->data
->course
.'/'.$CFG->moddata
.'/data/'.$this->data
->id
.'/'.$this->field
->id
.'/'.$recordid;
128 // only use the manager if file is present, to avoid "are you sure you selected a file to upload" msg
130 require_once($CFG->libdir
.'/uploadlib.php');
131 // FIX ME: $course not defined here
132 $um = new upload_manager($names[0].'_'.$names[1],true,false,$this->data
->course
,false,$this->field
->param3
);
133 if ($um->process_file_uploads($dir)) {
134 $newfile_name = $um->get_new_filename();
135 $content->content
= $newfile_name;
136 update_record('data_content',$content);
142 // only changing alt tag
143 $content->content1
= clean_param($value, PARAM_NOTAGS
);
144 update_record('data_content', $content);
152 function notemptyfield($value, $name) {
153 $names = explode('_',$name);
154 if ($names[2] == 'file') {
155 $filename = $_FILES[$names[0].'_'.$names[1]];
156 return !empty($filename['name']);
157 // if there's a file in $_FILES, not empty
162 function text_export_supported() {