|
By skodak:
MDL-16493 file and picture element migrated; waiting for file picker to mature, presets not finished yet
|
| 22 |
// // |
// // |
| 23 |
/////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////// |
| 24 |
|
|
| 25 |
require_once($CFG->dirroot.'/mod/data/field/file/field.class.php'); |
class data_field_picture extends data_field_base { |
|
// Base class is 'file' |
|
|
|
|
|
class data_field_picture extends data_field_file { |
|
| 26 |
var $type = 'picture'; |
var $type = 'picture'; |
| 27 |
var $previewwidth = 50; |
var $previewwidth = 50; |
| 28 |
var $previewheight = 50; |
var $previewheight = 50; |
| 29 |
|
|
|
function data_field_picture($field=0, $data=0) { |
|
|
parent::data_field_base($field, $data); |
|
|
} |
|
|
|
|
| 30 |
function display_add_field($recordid=0) { |
function display_add_field($recordid=0) { |
| 31 |
global $CFG, $DB; |
global $CFG, $DB; |
| 32 |
|
|
| 33 |
$filepath = ''; |
$file = false; |
| 34 |
$filename = ''; |
$content = false; |
| 35 |
$description = ''; |
$displayname = ''; |
| 36 |
|
$alttext = ''; |
| 37 |
|
$fs = get_file_storage(); |
| 38 |
if ($recordid) { |
if ($recordid) { |
| 39 |
if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { |
if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { |
| 40 |
$filename = $content->content; |
if (!empty($content->content)) { |
| 41 |
$description = $content->content1; |
if ($file = $FS->get_file($this->context->id, 'data_content', $content->id, '/', $content->content)) { |
| 42 |
|
if (empty($content->content1)) { |
| 43 |
|
$displayname = $file->get_filename(); |
| 44 |
|
} else { |
| 45 |
|
$displayname = $content->content1; |
| 46 |
|
} |
| 47 |
|
} |
| 48 |
} |
} |
| 49 |
$path = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid; |
$alttext = $content->content1; |
|
require_once($CFG->libdir.'/filelib.php'); |
|
|
$filepath = get_file_url("$path/$filename"); |
|
| 50 |
} |
} |
| 51 |
|
} |
| 52 |
|
|
| 53 |
$str = '<div title="'.s($this->field->description).'">'; |
$str = '<div title="'.s($this->field->description).'">'; |
| 54 |
$str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>'; |
$str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>'; |
| 55 |
$str .= '<input type="hidden" name ="field_'.$this->field->id.'_file" id="field_'.$this->field->id.'_file" value="fakevalue" />'; |
$str .= '<input type="hidden" name ="field_'.$this->field->id.'_file" id="field_'.$this->field->id.'_file" value="fakevalue" />'; |
| 56 |
$str .= '<label for="field_'.$this->field->id.'">'.get_string('picture','data'). '</label> <input type="file" name ="field_'.$this->field->id.'" id="field_'.$this->field->id.'" /><br />'; |
$str .= '<label for="field_'.$this->field->id.'">'.get_string('picture','data'). '</label> <input type="file" name ="field_'.$this->field->id.'" id="field_'.$this->field->id.'" /><br />'; |
| 57 |
$str .= '<label for="field_'.$this->field->id.'_filename">'.get_string('alttext','data') .'</label> <input type="text" name="field_' |
$str .= '<label for="field_'.$this->field->id.'_alttext">'.get_string('alttext','data') .'</label> <input type="text" name="field_' |
| 58 |
.$this->field->id.'_filename" id="field_'.$this->field->id.'_filename" value="'.s($description).'" /><br />'; |
.$this->field->id.'_alttext" id="field_'.$this->field->id.'_alttext" value="'.s($alttext).'" /><br />'; |
| 59 |
$str .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.s($this->field->param3).'" />'; |
//$str .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.s($this->field->param3).'" />'; |
| 60 |
if ($filepath) { |
if ($file) { |
| 61 |
$str .= '<img width="'.s($this->previewwidth).'" height="'.s($this->previewheight).'" src="'.$filepath.'" alt="" />'; |
$browser = get_file_browser(); |
| 62 |
|
$src = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', $this->context->id.'/data_content/'.$content->id.'/'.$file->get_filename()); |
| 63 |
|
$str .= '<img width="'.s($this->previewwidth).'" height="'.s($this->previewheight).'" src="'.$src.'" alt="" />'; |
| 64 |
} |
} |
| 65 |
$str .= '</fieldset>'; |
$str .= '</fieldset>'; |
| 66 |
$str .= '</div>'; |
$str .= '</div>'; |
| 67 |
|
|
| 68 |
return $str; |
return $str; |
| 69 |
} |
} |
| 70 |
|
|
| 90 |
function display_browse_field($recordid, $template) { |
function display_browse_field($recordid, $template) { |
| 91 |
global $CFG, $DB; |
global $CFG, $DB; |
| 92 |
|
|
| 93 |
if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { |
if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { |
| 94 |
if (isset($content->content)) { |
return false; |
|
$contents[0] = $content->content; |
|
|
$contents[1] = $content->content1; |
|
| 95 |
} |
} |
| 96 |
if (empty($contents[0])) { |
|
| 97 |
// Nothing to show |
if (empty($content->content)) { |
| 98 |
return ''; |
return ''; |
| 99 |
} |
} |
| 100 |
$alt = empty($contents[1])? '':$contents[1]; |
|
| 101 |
$title = empty($contents[1])? '':$contents[1]; |
$browser = get_file_browser(); |
| 102 |
$src = $contents[0]; |
|
| 103 |
$path = $this->data->course.'/'.$CFG->moddata.'/data/'.$this->data->id.'/'.$this->field->id.'/'.$recordid; |
$alt = $content->content1; |
| 104 |
|
$title = $alt; |
|
$thumbnaillocation = $CFG->dataroot .'/'. $path .'/thumb/'.$src; |
|
|
require_once($CFG->libdir.'/filelib.php'); |
|
|
$source = get_file_url("$path/$src"); |
|
|
$thumbnailsource = file_exists($thumbnaillocation) ? get_file_url("$path/thumb/$src") : $source; |
|
| 105 |
|
|
| 106 |
if ($template == 'listtemplate') { |
if ($template == 'listtemplate') { |
| 107 |
$width = $this->field->param4 ? ' width="'.s($this->field->param4).'" ' : ' '; |
$src = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/data_content/'.$content->id.'/'.'thumb_'.$content->content); |
| 108 |
$height = $this->field->param5 ? ' height="'.s($this->field->param5).'" ' : ' '; |
// no need to add width/height, because the thumb is resized properly |
| 109 |
$str = '<a href="view.php?d='.$this->field->dataid.'&rid='.$recordid.'"><img '. |
$str = '<a href="view.php?d='.$this->field->dataid.'&rid='.$recordid.'"><img src="'.$src.'" alt="'.s($alt).'" title="'.s($title).'" style="border:0px" /></a>'; |
| 110 |
$width.$height.' src="'.$thumbnailsource.'" alt="'.s($alt).'" title="'.s($title).'" style="border:0px" /></a>'; |
|
| 111 |
} else { |
} else { |
| 112 |
|
$src = $browser->encodepath($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/data_content/'.$content->id.'/'.$content->content); |
| 113 |
$width = $this->field->param1 ? ' width="'.s($this->field->param1).'" ':' '; |
$width = $this->field->param1 ? ' width="'.s($this->field->param1).'" ':' '; |
| 114 |
$height = $this->field->param2 ? ' height="'.s($this->field->param2).'" ':' '; |
$height = $this->field->param2 ? ' height="'.s($this->field->param2).'" ':' '; |
| 115 |
$str = '<a href="'.$source.'"><img '.$width.$height.' src="'.$source.'" alt="'.s($alt).'" title="'.s($title).'" style="border:0px" /></a>'; |
$str = '<a href="'.$src.'"><img '.$width.$height.' src="'.$src.'" alt="'.s($alt).'" title="'.s($title).'" style="border:0px" /></a>'; |
| 116 |
} |
} |
| 117 |
|
|
| 118 |
return $str; |
return $str; |
| 119 |
} |
} |
|
return false; |
|
|
} |
|
| 120 |
|
|
| 121 |
function update_field() { |
function update_field() { |
| 122 |
global $DB; |
global $DB; |
| 132 |
if ($oldfield && ($oldfield->param4 != $this->field->param4 || $oldfield->param5 != $this->field->param5)) { |
if ($oldfield && ($oldfield->param4 != $this->field->param4 || $oldfield->param5 != $this->field->param5)) { |
| 133 |
// Check through all existing records and update the thumbnail |
// Check through all existing records and update the thumbnail |
| 134 |
if ($contents = $DB->get_records('data_content', array('fieldid'=>$this->field->id))) { |
if ($contents = $DB->get_records('data_content', array('fieldid'=>$this->field->id))) { |
| 135 |
|
$fs = get_file_storage(); |
| 136 |
if (count($contents) > 20) { |
if (count($contents) > 20) { |
| 137 |
notify(get_string('resizingimages', 'data'), 'notifysuccess'); |
notify(get_string('resizingimages', 'data'), 'notifysuccess'); |
| 138 |
echo "\n\n"; |
echo "\n\n"; |
| 140 |
ob_flush(); |
ob_flush(); |
| 141 |
} |
} |
| 142 |
foreach ($contents as $content) { |
foreach ($contents as $content) { |
| 143 |
|
if (!$file = $fs->get_file($this->context->id, 'data_content', $content->id, '/', $content->content)) { |
| 144 |
|
continue; |
| 145 |
|
} |
| 146 |
|
if ($thumbfile = $fs->get_file($this->context->id, 'data_content', $content->id, '/', 'thumb_'.$content->content)) { |
| 147 |
|
$thumbfile->delete(); |
| 148 |
|
} |
| 149 |
@set_time_limit(300); |
@set_time_limit(300); |
| 150 |
// Might be slow! |
// Might be slow! |
| 151 |
$this->update_thumbnail($content); |
$this->update_thumbnail($content, $file); |
| 152 |
} |
} |
| 153 |
} |
} |
| 154 |
} |
} |
| 156 |
} |
} |
| 157 |
|
|
| 158 |
function update_content($recordid, $value, $name) { |
function update_content($recordid, $value, $name) { |
| 159 |
global $DB; |
global $CFG, $DB; |
| 160 |
|
|
| 161 |
parent::update_content($recordid, $value, $name); |
if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { |
| 162 |
$content = $DB->get_record('data_content',array('fieldid'=>$this->field->id, 'recordid'=>$recordid)); |
// Quickly make one now! |
| 163 |
$this->update_thumbnail($content); |
$content = new object(); |
| 164 |
|
$content->fieldid = $this->field->id; |
| 165 |
|
$content->recordid = $recordid; |
| 166 |
|
if ($id = $DB->insert_record('data_content', $content)) { |
| 167 |
|
print_error('cannotinsertempty', 'data'); |
| 168 |
|
} |
| 169 |
|
$content = $DB->get_record('data_content', array('id'=>$id)); |
| 170 |
|
} |
| 171 |
|
|
| 172 |
|
$names = explode('_', $name); |
| 173 |
|
switch ($names[2]) { |
| 174 |
|
case 'file': |
| 175 |
|
// file just uploaded |
| 176 |
|
$tmpfile = $_FILES[$names[0].'_'.$names[1]]; |
| 177 |
|
$filename = $tmpfile['name']; |
| 178 |
|
$pathanme = $tmpfile['tmp_name']; |
| 179 |
|
if ($filename){ |
| 180 |
|
$fs = get_file_storage(); |
| 181 |
|
// TODO: uploaded file processing will be in file picker ;-) |
| 182 |
|
$fs->delete_area_files($this->context->id, 'data_content', $content->id); |
| 183 |
|
$file_record = array('contextid'=>$this->context->id, 'filearea'=>'data_content', 'itemid'=>$content->id, 'filepath'=>'/', 'filename'=>$filename); |
| 184 |
|
if ($file = $fs->create_file_from_pathname($file_record, $pathanme)) { |
| 185 |
|
$content->content = $file->get_filename(); |
| 186 |
|
$DB->update_record('data_content', $content); |
| 187 |
// Regenerate the thumbnail |
// Regenerate the thumbnail |
| 188 |
|
$this->update_thumbnail($content, $file); |
| 189 |
} |
} |
|
|
|
|
function update_thumbnail($content) { |
|
|
// (Re)generate thumbnail image according to the dimensions specified in the field settings. |
|
|
// If thumbnail width and height are BOTH not specified then no thumbnail is generated, and |
|
|
// additionally an attempted delete of the existing thumbnail takes place. |
|
|
global $CFG; |
|
|
require_once($CFG->libdir . '/gdlib.php'); |
|
|
$datalocation = $CFG->dataroot .'/'.$this->data->course.'/'.$CFG->moddata.'/data/'. |
|
|
$this->data->id.'/'.$this->field->id.'/'.$content->recordid; |
|
|
$originalfile = $datalocation.'/'.$content->content; |
|
|
if (!file_exists($originalfile)) { |
|
|
return; |
|
|
} |
|
|
if (!file_exists($datalocation.'/thumb')) { |
|
|
mkdir($datalocation.'/thumb', 0777); |
|
|
// robertall: Why hardcode 0777?? |
|
|
} |
|
|
$thumbnaillocation = $datalocation.'/thumb/'.$content->content; |
|
|
$imageinfo = GetImageSize($originalfile); |
|
|
$image->width = $imageinfo[0]; |
|
|
$image->height = $imageinfo[1]; |
|
|
$image->type = $imageinfo[2]; |
|
|
if (!$image->width || !$image->height) { |
|
|
// Should not happen |
|
|
return; |
|
|
} |
|
|
switch ($image->type) { |
|
|
case 1: |
|
|
if (function_exists('ImageCreateFromGIF')) { |
|
|
$im = ImageCreateFromGIF($originalfile); |
|
|
} else { |
|
|
return; |
|
| 190 |
} |
} |
| 191 |
break; |
break; |
| 192 |
case 2: |
|
| 193 |
if (function_exists('ImageCreateFromJPEG')) { |
case 'alttext': |
| 194 |
$im = ImageCreateFromJPEG($originalfile); |
// only changing alt tag |
| 195 |
} else { |
$content->content1 = clean_param($value, PARAM_NOTAGS); |
| 196 |
return; |
$DB->update_record('data_content', $content); |
|
} |
|
| 197 |
break; |
break; |
| 198 |
case 3: |
|
| 199 |
if (function_exists('ImageCreateFromPNG')) { |
default: |
|
$im = ImageCreateFromPNG($originalfile); |
|
|
} else { |
|
|
return; |
|
|
} |
|
| 200 |
break; |
break; |
| 201 |
} |
} |
|
$thumbwidth = isset($this->field->param4)?$this->field->param4:''; |
|
|
$thumbheight = isset($this->field->param5)?$this->field->param5:''; |
|
|
if ($thumbwidth || $thumbheight) { |
|
|
// Only if either width OR height specified do we want a thumbnail |
|
|
$wcrop = $image->width; |
|
|
$hcrop = $image->height; |
|
|
if ($thumbwidth && !$thumbheight) { |
|
|
$thumbheight = $image->height * $thumbwidth / $image->width; |
|
|
} else if($thumbheight && !$thumbwidth) { |
|
|
$thumbwidth = $image->width * $thumbheight / $image->height; |
|
|
} else { |
|
|
// BOTH are set - may need to crop if aspect ratio differs |
|
|
$hratio = $image->height / $thumbheight; |
|
|
$wratio = $image->width / $thumbwidth; |
|
|
if ($wratio > $hratio) { |
|
|
// Crop the width |
|
|
$wcrop = intval($thumbwidth * $hratio); |
|
|
} elseif($hratio > $wratio) { |
|
|
//Crop the height |
|
|
$hcrop = intval($thumbheight * $wratio); |
|
|
} |
|
| 202 |
} |
} |
| 203 |
|
|
| 204 |
// At this point both $thumbwidth and $thumbheight are set, and $wcrop and $hcrop |
function update_thumbnail($content, $file) { |
| 205 |
|
// (Re)generate thumbnail image according to the dimensions specified in the field settings. |
| 206 |
if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) { |
// If thumbnail width and height are BOTH not specified then no thumbnail is generated, and |
| 207 |
$im1 = ImageCreateTrueColor($thumbwidth,$thumbheight); |
// additionally an attempted delete of the existing thumbnail takes place. |
| 208 |
} else { |
$fs = get_file_storage(); |
| 209 |
$im1 = ImageCreate($thumbwidth,$thumbheight); |
$file_record = array('contextid'=>$file->get_contextid(), 'filearea'=>$file->get_filearea(), |
| 210 |
} |
'itemid'=>$file->get_itemid(), 'filepath'=>$file->get_filepath(), |
| 211 |
if ($image->type == 3) { |
'filename'=>'thumb_'.$file->get_filename(), 'userid'=>$file->get_userid()); |
| 212 |
// Prevent alpha blending for PNG images |
try { |
| 213 |
imagealphablending($im1, false); |
// this may fail for various reasons |
| 214 |
} |
$fs->convert_image($file_record, $file, $this->field->param4, $this->field->param5, true); |
| 215 |
$cx = $image->width / 2; |
return true; |
| 216 |
$cy = $image->height / 2; |
} catch (Exception $e) { |
| 217 |
|
return false; |
|
// These "half" measurements use the "crop" values rather than the actual dimensions |
|
|
$halfwidth = floor($wcrop * 0.5); |
|
|
$halfheight = floor($hcrop * 0.5); |
|
|
|
|
|
ImageCopyBicubic($im1, $im, 0, 0, $cx-$halfwidth, $cy-$halfheight, |
|
|
$thumbwidth, $thumbheight, $halfwidth*2, $halfheight*2); |
|
|
|
|
|
if ($image->type == 3) { |
|
|
// Save alpha transparency for PNG images |
|
|
imagesavealpha($im1, true); |
|
|
} |
|
|
if (function_exists('ImageJpeg') && $image->type != 3) { |
|
|
@touch($thumbnaillocation); |
|
|
// Helps in Safe mode |
|
|
if (ImageJpeg($im1, $thumbnaillocation, 90)) { |
|
|
@chmod($thumbnaillocation, 0666); |
|
|
// robertall: Why hardcode 0666?? |
|
|
} |
|
|
} elseif (function_exists('ImagePng') && $image->type == 3) { |
|
|
@touch($thumbnaillocation); |
|
|
// Helps in Safe mode |
|
|
if (ImagePng($im1, $thumbnaillocation, 9)) { |
|
|
@chmod($thumbnaillocation, 0666); |
|
|
// robertall: Why hardcode 0666?? |
|
| 218 |
} |
} |
| 219 |
} |
} |
| 220 |
} else { |
|
| 221 |
// Try to remove the thumbnail - we don't want thumbnailing active |
function notemptyfield($value, $name) { |
| 222 |
@unlink($thumbnaillocation); |
$names = explode('_',$name); |
| 223 |
|
if ($names[2] == 'file') { |
| 224 |
|
$filename = $_FILES[$names[0].'_'.$names[1]]; |
| 225 |
|
return !empty($filename['name']); |
| 226 |
|
// if there's a file in $_FILES, not empty |
| 227 |
} |
} |
| 228 |
|
return false; |
| 229 |
} |
} |
| 230 |
|
|
| 231 |
function text_export_supported() { |
function text_export_supported() { |
| 232 |
return false; |
return false; |
| 233 |
} |
} |
| 234 |
|
|
| 235 |
|
function file_ok($path) { |
| 236 |
|
return true; |
| 237 |
|
} |
| 238 |
} |
} |
| 239 |
|
|
| 240 |
?> |
?> |