|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00003 // // 00004 // NOTICE OF COPYRIGHT // 00005 // // 00006 // Moodle - Modular Object-Oriented Dynamic Learning Environment // 00007 // http://moodle.org // 00008 // // 00009 // Copyright (C) 1999-onwards Moodle Pty Ltd http://moodle.com // 00010 // // 00011 // This program is free software; you can redistribute it and/or modify // 00012 // it under the terms of the GNU General Public License as published by // 00013 // the Free Software Foundation; either version 2 of the License, or // 00014 // (at your option) any later version. // 00015 // // 00016 // This program is distributed in the hope that it will be useful, // 00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of // 00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 00019 // GNU General Public License for more details: // 00020 // // 00021 // http://www.gnu.org/copyleft/gpl.html // 00022 // // 00024 00025 class data_field_picture extends data_field_base { 00026 var $type = 'picture'; 00027 var $previewwidth = 50; 00028 var $previewheight = 50; 00029 00030 function display_add_field($recordid=0) { 00031 global $CFG, $DB, $OUTPUT, $USER, $PAGE; 00032 00033 $file = false; 00034 $content = false; 00035 $displayname = ''; 00036 $alttext = ''; 00037 $itemid = null; 00038 $fs = get_file_storage(); 00039 00040 if ($recordid) { 00041 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 00042 file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id); 00043 if (!empty($content->content)) { 00044 if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { 00045 $usercontext = get_context_instance(CONTEXT_USER, $USER->id); 00046 if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid, 'id DESC', false)) { 00047 return false; 00048 } 00049 if ($thumbfile = $fs->get_file($usercontext->id, 'user', 'draft', $itemid, '/', 'thumb_'.$content->content)) { 00050 $thumbfile->delete(); 00051 } 00052 if (empty($content->content1)) { 00053 // Print icon if file already exists 00054 $src = moodle_url::make_draftfile_url($itemid, '/', $file->get_filename()); 00055 $displayname = '<img src="'.$OUTPUT->pix_url(file_mimetype_icon($file->get_mimetype())).'" class="icon" alt="'.$file->get_mimetype().'" />'. '<a href="'.$src.'" >'.s($file->get_filename()).'</a>'; 00056 00057 } else { 00058 $displayname = get_string('nofilesattached', 'repository'); 00059 } 00060 } 00061 } 00062 $alttext = $content->content1; 00063 } 00064 } else { 00065 $itemid = file_get_unused_draft_itemid(); 00066 } 00067 00068 $str = '<div title="'.s($this->field->description).'">'; 00069 $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>'; 00070 if ($file) { 00071 $src = file_encode_url($CFG->wwwroot.'/pluginfile.php/', $this->context->id.'/mod_data/content/'.$content->id.'/'.$file->get_filename()); 00072 $str .= '<img width="'.s($this->previewwidth).'" height="'.s($this->previewheight).'" src="'.$src.'" alt="" />'; 00073 } 00074 00075 $options = new stdClass(); 00076 $options->maxbytes = $this->field->param3; 00077 $options->itemid = $itemid; 00078 $options->accepted_types = array('image'); 00079 $options->return_types = FILE_INTERNAL; 00080 $options->context = $PAGE->context; 00081 if (!empty($file)) { 00082 $options->filename = $file->get_filename(); 00083 $options->filepath = '/'; 00084 } 00085 $fp = new file_picker($options); 00086 $str .= $OUTPUT->render($fp); 00087 00088 00089 $str .= '<div class="mdl-left">'; 00090 $str .= '<input type="hidden" name="field_'.$this->field->id.'_file" value="'.$itemid.'" />'; 00091 $str .= '<label for="field_'.$this->field->id.'_alttext">'.get_string('alttext','data') .'</label> <input type="text" name="field_' 00092 .$this->field->id.'_alttext" id="field_'.$this->field->id.'_alttext" value="'.s($alttext).'" />'; 00093 $str .= '</div>'; 00094 00095 $str .= '</fieldset>'; 00096 $str .= '</div>'; 00097 00098 $module = array('name'=>'data_imagepicker', 'fullpath'=>'/mod/data/data.js', 'requires'=>array('core_filepicker')); 00099 $PAGE->requires->js_init_call('M.data_imagepicker.init', array($fp->options), true, $module); 00100 return $str; 00101 } 00102 00103 // TODO delete this function and instead subclass data_field_file - see MDL-16493 00104 00105 function get_file($recordid, $content=null) { 00106 global $DB; 00107 if (empty($content)) { 00108 if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 00109 return null; 00110 } 00111 } 00112 $fs = get_file_storage(); 00113 if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { 00114 return null; 00115 } 00116 00117 return $file; 00118 } 00119 00120 function display_search_field($value = '') { 00121 return '<input type="text" size="16" name="f_'.$this->field->id.'" value="'.$value.'" />'; 00122 } 00123 00124 function parse_search_field() { 00125 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS); 00126 } 00127 00128 function generate_sql($tablealias, $value) { 00129 global $DB; 00130 00131 static $i=0; 00132 $i++; 00133 $name = "df_picture_$i"; 00134 return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%")); 00135 } 00136 00137 function display_browse_field($recordid, $template) { 00138 global $CFG, $DB; 00139 00140 if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 00141 return false; 00142 } 00143 00144 if (empty($content->content)) { 00145 return ''; 00146 } 00147 00148 $alt = $content->content1; 00149 $title = $alt; 00150 00151 if ($template == 'listtemplate') { 00152 $src = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_data/content/'.$content->id.'/'.'thumb_'.$content->content); 00153 // no need to add width/height, because the thumb is resized properly 00154 $str = '<a href="view.php?d='.$this->field->dataid.'&rid='.$recordid.'"><img src="'.$src.'" alt="'.s($alt).'" title="'.s($title).'" style="border:0px" /></a>'; 00155 00156 } else { 00157 $src = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_data/content/'.$content->id.'/'.$content->content); 00158 $width = $this->field->param1 ? ' width="'.s($this->field->param1).'" ':' '; 00159 $height = $this->field->param2 ? ' height="'.s($this->field->param2).'" ':' '; 00160 $str = '<a href="'.$src.'"><img '.$width.$height.' src="'.$src.'" alt="'.s($alt).'" title="'.s($title).'" style="border:0px" /></a>'; 00161 } 00162 00163 return $str; 00164 } 00165 00166 function update_field() { 00167 global $DB, $OUTPUT; 00168 00169 // Get the old field data so that we can check whether the thumbnail dimensions have changed 00170 $oldfield = $DB->get_record('data_fields', array('id'=>$this->field->id)); 00171 $DB->update_record('data_fields', $this->field); 00172 00173 // Have the thumbnail dimensions changed? 00174 if ($oldfield && ($oldfield->param4 != $this->field->param4 || $oldfield->param5 != $this->field->param5)) { 00175 // Check through all existing records and update the thumbnail 00176 if ($contents = $DB->get_records('data_content', array('fieldid'=>$this->field->id))) { 00177 $fs = get_file_storage(); 00178 if (count($contents) > 20) { 00179 echo $OUTPUT->notification(get_string('resizingimages', 'data'), 'notifysuccess'); 00180 echo "\n\n"; 00181 // To make sure that ob_flush() has the desired effect 00182 ob_flush(); 00183 } 00184 foreach ($contents as $content) { 00185 if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { 00186 continue; 00187 } 00188 if ($thumbfile = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', 'thumb_'.$content->content)) { 00189 $thumbfile->delete(); 00190 } 00191 @set_time_limit(300); 00192 // Might be slow! 00193 $this->update_thumbnail($content, $file); 00194 } 00195 } 00196 } 00197 return true; 00198 } 00199 00200 function update_content($recordid, $value, $name) { 00201 global $CFG, $DB, $USER; 00202 00203 if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 00204 // Quickly make one now! 00205 $content = new stdClass(); 00206 $content->fieldid = $this->field->id; 00207 $content->recordid = $recordid; 00208 $id = $DB->insert_record('data_content', $content); 00209 $content = $DB->get_record('data_content', array('id'=>$id)); 00210 } 00211 00212 $names = explode('_', $name); 00213 switch ($names[2]) { 00214 case 'file': 00215 $fs = get_file_storage(); 00216 $fs->delete_area_files($this->context->id, 'mod_data', 'content', $content->id); 00217 $usercontext = get_context_instance(CONTEXT_USER, $USER->id); 00218 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value); 00219 if (count($files)<2) { 00220 // no file 00221 } else { 00222 $count = 0; 00223 foreach ($files as $draftfile) { 00224 $file_record = array('contextid'=>$this->context->id, 'component'=>'mod_data', 'filearea'=>'content', 'itemid'=>$content->id, 'filepath'=>'/'); 00225 if (!$draftfile->is_directory()) { 00226 $file_record['filename'] = $draftfile->get_filename(); 00227 00228 $content->content = $draftfile->get_filename(); 00229 00230 $file = $fs->create_file_from_storedfile($file_record, $draftfile); 00231 $DB->update_record('data_content', $content); 00232 $this->update_thumbnail($content, $file); 00233 00234 if ($count > 0) { 00235 break; 00236 } else { 00237 $count++; 00238 } 00239 } 00240 } 00241 } 00242 00243 break; 00244 00245 case 'alttext': 00246 // only changing alt tag 00247 $content->content1 = clean_param($value, PARAM_NOTAGS); 00248 $DB->update_record('data_content', $content); 00249 break; 00250 00251 default: 00252 break; 00253 } 00254 } 00255 00256 function update_thumbnail($content, $file) { 00257 // (Re)generate thumbnail image according to the dimensions specified in the field settings. 00258 // If thumbnail width and height are BOTH not specified then no thumbnail is generated, and 00259 // additionally an attempted delete of the existing thumbnail takes place. 00260 $fs = get_file_storage(); 00261 $file_record = array('contextid'=>$file->get_contextid(), 'component'=>$file->get_component(), 'filearea'=>$file->get_filearea(), 00262 'itemid'=>$file->get_itemid(), 'filepath'=>$file->get_filepath(), 00263 'filename'=>'thumb_'.$file->get_filename(), 'userid'=>$file->get_userid()); 00264 try { 00265 // this may fail for various reasons 00266 $fs->convert_image($file_record, $file, $this->field->param4, $this->field->param5, true); 00267 return true; 00268 } catch (Exception $e) { 00269 debugging($e->getMessage()); 00270 return false; 00271 } 00272 } 00273 00274 function text_export_supported() { 00275 return false; 00276 } 00277 00278 function file_ok($path) { 00279 return true; 00280 } 00281 } 00282 00283