Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / data / field / file / field.class.php
blob8d98ec5cd21b1c80b34d41ba258cdb8e950b5154
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 {
26 var $type = 'file';
28 function data_field_file($field=0, $data=0) {
29 parent::data_field_base($field, $data);
32 function display_add_field($recordid=0) {
33 global $CFG;
34 if ($recordid){
35 if ($content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)) {
36 $contents[0] = $content->content;
37 $contents[1] = $content->content1;
38 } else {
39 $contents[0] = '';
40 $contents[1] = '';
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);
47 } else {
48 $src = '';
49 $name = '';
50 $displayname = '';
51 $source = '';
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>';
62 $str .= '</div>';
63 if ($recordid and isset($content) and !empty($content->content)) {
64 // Print icon
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>';
70 return $str;
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) {
86 global $CFG;
87 if (!$content = get_record('data_content', 'fieldid', $this->field->id, 'recordid', $recordid)) {
88 return false;
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)) {
93 return '';
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.'" />&nbsp;'.
101 '<a href="'.$source.'/'.$src.'" >'.$name.'</a>';
102 return $str;
106 // content: "a##b" where a is the file name, b is the display name
107 function update_content($recordid, $value, $name) {
108 global $CFG;
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);
121 switch ($names[2]) {
122 case 'file':
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
129 if ($filename){
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);
139 break;
141 case 'filename':
142 // only changing alt tag
143 $content->content1 = clean_param($value, PARAM_NOTAGS);
144 update_record('data_content', $content);
145 break;
147 default:
148 break;
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
159 return false;
162 function text_export_supported() {
163 return false;