MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / data / field / file / field.class.php
blob37bb83065c08a534af5fb735e4fa23b7147b5682
1 <?php // $Id$
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 {
27 var $type = 'file';
29 function data_field_file($field=0, $data=0) {
30 parent::data_field_base($field, $data);
33 function display_add_field($recordid=0) {
34 global $CFG;
36 if ($recordid){
37 if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)) {
38 $contents[0] = $content->content;
39 $contents[1] = $content->content1;
40 } else {
41 $contents[0] = '';
42 $contents[1] = '';
45 $src = empty($contents[0]) ? '' : $contents[0];
46 $name = empty($contents[1]) ? $src : $contents[1];
47 $displayname = empty($contents[1]) ? '' : $contents[1];
49 $path = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid;
51 if ($CFG->slasharguments) {
52 $source = $CFG->wwwroot.'/file.php/'.$path;
53 } else {
54 $source = $CFG->wwwroot.'/file.php?file=/'.$path;
56 } else {
57 $src = '';
58 $name = '';
59 $displayname = '';
60 $source = '';
63 $str = '<div title="' . s($this->field->description) . '">';
64 $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>';
65 $str .= '<input type="hidden" name ="field_'.$this->field->id.'_file" value="fakevalue" />';
66 $str .= get_string('file','data'). ' <input type="file" name ="field_'.$this->field->id.'" id="field_'.
67 $this->field->id.'" title="'.s($this->field->description).'" /><br />';
68 $str .= get_string('optionalfilename','data').' <input type="text" name="field_' .$this->field->id.'_filename"
69 id="field_'.$this->field->id.'_filename" value="'.s($displayname).'" /><br />';
70 $str .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.s($this->field->param3).'" />';
71 $str .= '</fieldset>';
72 $str .= '</div>';
74 if ($recordid and isset($content)){ // Print icon
75 require_once($CFG->libdir.'/filelib.php');
76 $icon = mimeinfo('icon', $src);
77 $str .= '<img src="'.$CFG->pixpath.'/f/'.$icon.'" class="icon" alt="'.$icon.'" />'.
78 '<a href="'.$source.'/'.$src.'" >'.$name.'</a>';
80 return $str;
83 function display_search_field($value = '') {
84 return '<input type="text" size="16" name="f_'.$this->field->id.'" value="'.$value.'" />';
87 function generate_sql($tablealias, $value) {
88 return " ({$tablealias}.fieldid = {$this->field->id} AND {$tablealias}.content LIKE '%{$value}%') ";
92 function parse_search_field() {
93 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS);
96 function display_browse_field($recordid, $template) {
98 global $CFG;
100 if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)){
101 $contents[0] = $content->content;
102 $contents[1] = $content->content1;
104 $src = empty($contents[0])? '':$contents[0];
105 $name = empty($contents[1])? $src:$contents[1];
107 $path = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid;
109 if ($CFG->slasharguments) {
110 $source = $CFG->wwwroot.'/file.php/'.$path;
111 } else {
112 $source = $CFG->wwwroot.'/file.php?file=/'.$path;
115 $width = $this->field->param1 ? ' width = "'.s($this->field->param1).'" ':' ';
116 $height = $this->field->param2 ? ' height = "'.s($this->field->param2).'" ':' ';
118 require_once($CFG->libdir.'/filelib.php');
119 $icon = mimeinfo('icon', $src);
120 $str = '<img src="'.$CFG->pixpath.'/f/'.$icon.'" height="16" width="16" alt="'.$icon.'" />&nbsp;'.
121 '<a href="'.$source.'/'.$src.'" >'.$name.'</a>';
122 return $str;
124 return false;
128 /// content = a ## b where a is the filename, b is the display file name
129 function update_content($recordid, $value, $name) {
130 global $CFG;
132 if (!$oldcontent = get_record('data_content','fieldid', $this->field->id, 'recordid', $recordid)) {
133 /// Quickly make one now!
134 $oldcontent = new object;
135 $oldcontent->fieldid = $this->field->id;
136 $oldcontent->recordid = $recordid;
137 if ($oldcontent->id = insert_record('data_content', $oldcontent)) {
138 error('Could not make an empty record!');
142 $content = new object;
143 $content->id = $oldcontent->id;
145 $names = explode('_',$name);
146 switch ($names[2]) {
147 case 'file': //file just uploaded
148 //$course = get_course('course', 'id', $this->data->course);
150 $filename = $_FILES[$names[0].'_'.$names[1]];
151 $filename = $filename['name'];
152 $dir = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid;
154 //only use the manager if file is present, to avoid "are you sure you selected a file to upload" msg
155 if ($filename){
156 require_once($CFG->libdir.'/uploadlib.php');
157 //FIX ME: $course not defined heer!!
158 $um = new upload_manager($names[0].'_'.$names[1],true,false,$this->data->course,false,$this->field->param3);
159 if ($um->process_file_uploads($dir)) {
160 $newfile_name = $um->get_new_filename();
161 $content->content = $newfile_name;
162 update_record('data_content',$content);
165 break;
167 case 'filename': //only changing alt tag
168 $content->content1 = clean_param($value, PARAM_NOTAGS);
169 update_record('data_content', $content);
170 break;
172 default:
173 break;
177 function notemptyfield($value, $name){
178 $names = explode('_',$name);
179 if ($names[2] == 'file'){
180 $filename = $_FILES[$names[0].'_'.$names[1]];
181 return !empty($filename['name']); //if there's a file in $_FILES, not empty
183 return false;