mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
field.class.php
executable file
·287 lines (242 loc) · 11.8 KB
/
field.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php ///Class file for text field, extends field_picture
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 2005 Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
/// Please refer to lib.php for method comments
global $CFG;
//load base class
require_once($CFG->dirroot.'/mod/data/lib.php');
class data_field_file extends data_field_base {// extends
function data_field_file($fid=0){
parent::data_field_base($fid);
}
/* the content field is store as this
* content = a ## b where a is the filename,
* b is the display file name
*/
var $type = 'file';
var $id;
function insert_field($dataid, $type='file', $name, $des) {
$newfield = new object;
$newfield->dataid = $dataid;
$newfield->type = $type;
$newfield->name = $name;
$newfield->description = $des;
if (!insert_record('data_fields',$newfield)){
notify('Insertion of new field failed!');
}
}
function display_add_field($id, $rid=0){
global $CFG, $course;
if (!$field = get_record('data_fields','id',$id)){
notify("that is not a valid field id!");
exit;
}
if ($rid){
$datacontent = get_record('data_content','fieldid',$id,'recordid',$rid);
if (isset($datacontent->content)){
$content = $datacontent->content;
$contents[0] = $datacontent->content;
$contents[1] = $datacontent->content1;
}else {
$contents = array();
$contents[0]='';
$contents[1]='';
}
$src = empty($contents[0])? '':$contents[0];
$name = empty($contents[1])? $src:$contents[1];
$displayname = empty($contents[1])? '':$contents[1];
if ($CFG->slasharguments) {
$source = $CFG->wwwroot.'/file.php/'.$course->id.'/'.$CFG->moddata.'/data/'.$field->dataid.'/'.$field->id.'/'.$rid;
} else {
$source = $CFG->wwwroot.'/file.php?file=/'.$course->id.'/'.$CFG->moddata.'/data/'.$field->dataid.'/'.$field->id.'/'.$rid;
}
}
else {
$displayname = '';
$name = '';
$src = '';
$source = '';
}
$str = '';
/*
if ($field->description){
$str .= '<img src="'.$CFG->pixpath.'/help.gif" alt="'.$field->description.'" title="'.$field->description.'"> ';
}
*/
$str .= '<div title="' . $field->description . '">';
$str .= '<input type="hidden" name ="field_'.$field->id.'_0" value="fakevalue" />';
$str .= get_string('file','data'). ': <input type="file" name ="field_'.$field->id.'" id="field_'.$field->id.'" title="'.$field->description.'" /><br />';
$str .= get_string('optionalfilename','data').': <input type="text" name="field_'
.$field->id.'_1" id="field_'.$field->id.'_1" value="'.$displayname.'" /><br />';
$str .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.$field->param3.'" />';
$str .= '</div>';
//print icon
if ($rid and isset($content)){
require_once($CFG->libdir.'/filelib.php');
$icon = mimeinfo('icon', $src);
$str .= '<img src="'.$CFG->pixpath.'/f/'.$icon.'" height="16" width="16" alt="'.$icon.'" /> '.
'<a href="'.$source.'/'.$src.'" >'.$name.'</a>';
}
return $str;
}
function display_edit_field($id, $mode=0){
parent::display_edit_field($id, $mode);
}
function display_browse_field($fieldid, $recordid, $template) {
global $CFG, $USER;
$field = get_record('data_fields', 'id', $fieldid);
$data = get_record('data', 'id', $field->dataid);
if ($content = get_record('data_content', 'fieldid', $fieldid, 'recordid', $recordid)){
$contents[0] = $content->content;
$contents[1] = $content->content1;
$src = empty($contents[0])? '':$contents[0];
$name = empty($contents[1])? $src:$contents[1];
if ($CFG->slasharguments) {
$source = $CFG->wwwroot.'/file.php/'.$data->course.'/'.$CFG->moddata.'/data/'.$field->dataid.'/'.$field->id.'/'.$recordid;
} else {
$source = $CFG->wwwroot.'/file.php?file=/'.$data->course.'/'.$CFG->moddata.'/data/'.$field->dataid.'/'.$field->id.'/'.$recordid;
}
$width = $field->param1 ? ' width = "'.$field->param1.'" ':' ';
$height = $field->param2 ? ' height = "'.$field->param2.'" ':' ';
require_once($CFG->libdir.'/filelib.php');
$icon = mimeinfo('icon', $src);
$str = '<img src="'.$CFG->pixpath.'/f/'.$icon.'" height="16" width="16" alt="'.$icon.'" /> '.
'<a href="'.$source.'/'.$src.'" >'.$name.'</a>';
return $str;
}
return false;
}
function update($fieldobject){
if (!update_record('data_fields',$fieldobject)){
notify ('upate failed');
}
}
function store_data_content($fieldid, $recordid, $value, $name=''){
global $CFG, $course;
$content = new object;
$content->fieldid = $fieldid;
$content->recordid = $recordid;
$field = get_record('data_fields','id',$fieldid);
$names = explode('_',$name);
switch ($names[2]){
case 0: //file just uploaded
$filename = $_FILES[$names[0].'_'.$names[1]];
$filename = $filename['name'];
$dir = $course->id.'/'.$CFG->moddata.'/data/'.$field->dataid.'/'.$field->id.'/'.$recordid;
/************ FILE MANAGER HERE ***************/
require_once('../../lib/uploadlib.php');
$um = new upload_manager($names[0].'_'.$names[1],true,false,$course,false,$field->param3);
if ($um->process_file_uploads($dir)) { //write to content
$newfile_name = $um->get_new_filename();
$content->content = $newfile_name;
insert_record('data_content',$content);
}
break;
case 1: //only changing alt tag
if ($oldcontent = get_record('data_content','fieldid', $fieldid, 'recordid', $recordid)){
$content->id = $oldcontent ->id;
$content->content1 = clean_param($value, PARAM_NOTAGS);
update_record('data_content',$content);
}
break;
default:
break;
} //close switch
}
function update_data_content($fieldid, $recordid, $value, $name){
//if data_content already exit, we update
global $CFG,$course;
if ($oldcontent = get_record('data_content','fieldid', $fieldid, 'recordid', $recordid)){
$content = new object;
$content->fieldid = $fieldid;
$content->recordid = $recordid;
$content->id = $oldcontent->id;
$names = explode('_',$name);
$field = get_record('data_fields','id',$fieldid);
switch ($names[2]){
case 0: //file just uploaded
$filename = $_FILES[$names[0].'_'.$names[1]];
$filename = $filename['name'];
$dir = $course->id.'/'.$CFG->moddata.'/data/'.$field->dataid.'/'.$field->id.'/'.$recordid;
//only use the manager if file is present, to avoid "are you sure you selected a file to upload" msg
if ($filename){
/************ FILE MANAGER HERE ***************/
require_once('../../lib/uploadlib.php');
$um = new upload_manager($names[0].'_'.$names[1],true,false,$course,false,$field->param3);
if ($um->process_file_uploads($dir)) {
//write to content
$newfile_name = $um->get_new_filename();
$content->content = $newfile_name;
$content->content1 = $oldcontent->content1;
update_record('data_content',$content);
}
}
break;
case 1: //only changing alt tag
$content->content = $oldcontent->content;
$content->content1 = clean_param($value, PARAM_NOTAGS);
update_record('data_content', $content);
break;
default:
break;
} //close switch
}
else { //make 1 if there isn't one already
$this->store_data_content($fieldid, $recordid, $value, $name);
}
}
function notemptyfield($value, $name){
$names = explode('_',$name);
if ($names[2] == '0'){
$filename = $_FILES[$names[0].'_'.$names[1]];
return !empty($filename['name']); //if there's a file in $_FILES, not empty
}
return false;
}
function delete_data_contents() {
// Delete the content files first.
$sql = 'SELECT dc.content AS content, '.
'df.dataid AS dataid, '.
'dc.recordid AS recordid '.
'FROM data_fields AS df, '.
'data_content AS dc '.
"WHERE df.id = {$this->id} ".
"AND dc.fieldid = {$this->id}";
if (!$results = get_records_sql($sql)) {
return false;
}
foreach ($results as $result) {
$this->delete_data_content_files($result->dataid, $result->recordid, $result->content);
}
// Then delete the data_content records.
return delete_records('data_content', 'fieldid', $this->id);
}
function delete_data_content_files($dataid, $recordid, $content){
global $CFG, $course;
$filepath = $CFG->dataroot.'/'.$course->id.'/'.$CFG->moddata.'/data/'.$dataid.'/'.$this->id.'/'.$recordid;
unlink($filepath . '/' . $content);
rmdir($filepath);
notify (get_string('file', 'data') . ' ' . $content . ' ' . get_string('deleted', 'data'));
}
}
?>