Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]
1 <?php 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 /////////////////////////////////////////////////////////////////////////// 24 25 class data_field_picture extends data_field_base { 26 var $type = 'picture'; 27 var $previewwidth = 50; 28 var $previewheight = 50; 29 30 function display_add_field($recordid = 0, $formdata = null) { 31 global $CFG, $DB, $OUTPUT, $USER, $PAGE; 32 33 $file = false; 34 $content = false; 35 $alttext = ''; 36 $itemid = null; 37 $fs = get_file_storage(); 38 39 if ($formdata) { 40 $fieldname = 'field_' . $this->field->id . '_file'; 41 $itemid = clean_param($formdata->$fieldname, PARAM_INT); 42 $fieldname = 'field_' . $this->field->id . '_alttext'; 43 if (isset($formdata->$fieldname)) { 44 $alttext = $formdata->$fieldname; 45 } 46 } else if ($recordid) { 47 if (!$content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) { 48 // Quickly make one now! 49 $content = new stdClass(); 50 $content->fieldid = $this->field->id; 51 $content->recordid = $recordid; 52 $id = $DB->insert_record('data_content', $content); 53 $content = $DB->get_record('data_content', array('id' => $id)); 54 } 55 file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id); 56 if (!empty($content->content)) { 57 if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { 58 $usercontext = context_user::instance($USER->id); 59 60 if ($thumbfile = $fs->get_file($usercontext->id, 'user', 'draft', $itemid, '/', 'thumb_'.$content->content)) { 61 $thumbfile->delete(); 62 } 63 } 64 } 65 $alttext = $content->content1; 66 } else { 67 $itemid = file_get_unused_draft_itemid(); 68 } 69 $str = '<div title="' . s($this->field->description) . '">'; 70 $str .= '<fieldset><legend><span class="accesshide">'.$this->field->name; 71 72 if ($this->field->required) { 73 $str .= ' ' . get_string('requiredelement', 'form') . '</span></legend>'; 74 $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form')); 75 $str .= html_writer::div($image, 'inline-req'); 76 } else { 77 $str .= '</span></legend>'; 78 } 79 $str .= '<noscript>'; 80 if ($file) { 81 $src = file_encode_url($CFG->wwwroot.'/pluginfile.php/', $this->context->id.'/mod_data/content/'.$content->id.'/'.$file->get_filename()); 82 $str .= '<img width="'.s($this->previewwidth).'" height="'.s($this->previewheight).'" src="'.$src.'" alt="" />'; 83 } 84 $str .= '</noscript>'; 85 86 $options = new stdClass(); 87 $options->maxbytes = $this->field->param3; 88 $options->maxfiles = 1; // Only one picture permitted. 89 $options->itemid = $itemid; 90 $options->accepted_types = array('web_image'); 91 $options->return_types = FILE_INTERNAL; 92 $options->context = $PAGE->context; 93 if (!empty($file)) { 94 $options->filename = $file->get_filename(); 95 $options->filepath = '/'; 96 } 97 98 $fm = new form_filemanager($options); 99 // Print out file manager. 100 101 $output = $PAGE->get_renderer('core', 'files'); 102 $str .= '<div class="mod-data-input">'; 103 $str .= $output->render($fm); 104 105 $str .= '<div class="mdl-left">'; 106 $str .= '<input type="hidden" name="field_' . $this->field->id . '_file" value="' . s($itemid) . '" />'; 107 $str .= '<label for="field_' . $this->field->id . '_alttext">' . 108 get_string('alttext', 'data') . 109 '</label> <input type="text" class="form-control" name="field_' . 110 $this->field->id . '_alttext" id="field_' . $this->field->id . '_alttext" value="' . s($alttext) . '" />'; 111 $str .= '</div>'; 112 $str .= '</div>'; 113 114 $str .= '</fieldset>'; 115 $str .= '</div>'; 116 117 return $str; 118 } 119 120 /** 121 * Validate the image field type parameters. 122 * 123 * This will check for valid numeric values in the width and height fields. 124 * 125 * @param stdClass $fieldinput the field input data 126 * @return array array of error messages if width or height parameters are not numeric 127 * @throws coding_exception 128 */ 129 public function validate(stdClass $fieldinput): array { 130 $errors = []; 131 // These are the params we have to check if they are numeric, because they represent width and height of the image 132 // in single and list view. 133 $widthandheightparams = ['param1', 'param2', 'param4', 'param5']; 134 135 foreach ($widthandheightparams as $param) { 136 if (!empty($fieldinput->$param) && !is_numeric($fieldinput->$param)) { 137 $errors[$param] = get_string('error_invalid' . $param, 'datafield_picture'); 138 } 139 } 140 return $errors; 141 } 142 143 // TODO delete this function and instead subclass data_field_file - see MDL-16493 144 145 function get_file($recordid, $content=null) { 146 global $DB; 147 if (empty($content)) { 148 if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 149 return null; 150 } 151 } 152 $fs = get_file_storage(); 153 if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { 154 return null; 155 } 156 157 return $file; 158 } 159 160 function display_search_field($value = '') { 161 return '<label class="accesshide" for="f_' . $this->field->id . '">' . get_string('fieldname', 'data') . '</label>' . 162 '<input type="text" size="16" id="f_' . $this->field->id . '" name="f_' . $this->field->id . '" ' . 163 'value="' . s($value) . '" class="form-control"/>'; 164 } 165 166 public function parse_search_field($defaults = null) { 167 $param = 'f_'.$this->field->id; 168 if (empty($defaults[$param])) { 169 $defaults = array($param => ''); 170 } 171 return optional_param($param, $defaults[$param], PARAM_NOTAGS); 172 } 173 174 function generate_sql($tablealias, $value) { 175 global $DB; 176 177 static $i=0; 178 $i++; 179 $name = "df_picture_$i"; 180 return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%")); 181 } 182 183 function display_browse_field($recordid, $template) { 184 global $CFG, $DB; 185 186 if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 187 return false; 188 } 189 190 if (empty($content->content)) { 191 return ''; 192 } 193 194 $alt = $content->content1; 195 $title = $alt; 196 197 if ($template == 'listtemplate') { 198 $src = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_data/content/'.$content->id.'/'.'thumb_'.$content->content); 199 // no need to add width/height, because the thumb is resized properly 200 $str = '<a href="view.php?d='.$this->field->dataid.'&rid='.$recordid.'"><img src="'.$src.'" alt="'.s($alt).'" title="'.s($title).'" class="list_picture"/></a>'; 201 202 } else { 203 $src = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_data/content/'.$content->id.'/'.$content->content); 204 $width = $this->field->param1 ? ' width="'.s($this->field->param1).'" ':' '; 205 $height = $this->field->param2 ? ' height="'.s($this->field->param2).'" ':' '; 206 $str = '<a href="'.$src.'"><img '.$width.$height.' src="'.$src.'" alt="'.s($alt).'" title="'.s($title).'" class="list_picture"/></a>'; 207 } 208 209 return $str; 210 } 211 212 function update_field() { 213 global $DB, $OUTPUT; 214 215 // Get the old field data so that we can check whether the thumbnail dimensions have changed 216 $oldfield = $DB->get_record('data_fields', array('id'=>$this->field->id)); 217 $DB->update_record('data_fields', $this->field); 218 219 // Have the thumbnail dimensions changed? 220 if ($oldfield && ($oldfield->param4 != $this->field->param4 || $oldfield->param5 != $this->field->param5)) { 221 // Check through all existing records and update the thumbnail 222 if ($contents = $DB->get_records('data_content', array('fieldid'=>$this->field->id))) { 223 $fs = get_file_storage(); 224 if (count($contents) > 20) { 225 echo $OUTPUT->notification(get_string('resizingimages', 'data'), 'notifysuccess'); 226 echo "\n\n"; 227 // To make sure that ob_flush() has the desired effect 228 ob_flush(); 229 } 230 foreach ($contents as $content) { 231 if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { 232 continue; 233 } 234 if ($thumbfile = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', 'thumb_'.$content->content)) { 235 $thumbfile->delete(); 236 } 237 core_php_time_limit::raise(300); 238 // Might be slow! 239 $this->update_thumbnail($content, $file); 240 } 241 } 242 } 243 return true; 244 } 245 246 function update_content($recordid, $value, $name='') { 247 global $CFG, $DB, $USER; 248 249 // Should always be available since it is set by display_add_field before initializing the draft area. 250 $content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid)); 251 if (!$content) { 252 $content = (object)array('fieldid' => $this->field->id, 'recordid' => $recordid); 253 $content->id = $DB->insert_record('data_content', $content); 254 } 255 256 $names = explode('_', $name); 257 switch ($names[2]) { 258 case 'file': 259 $fs = get_file_storage(); 260 file_save_draft_area_files($value, $this->context->id, 'mod_data', 'content', $content->id); 261 $usercontext = context_user::instance($USER->id); 262 $files = $fs->get_area_files( 263 $this->context->id, 264 'mod_data', 'content', 265 $content->id, 266 'itemid, filepath, filename', 267 false); 268 269 // We expect no or just one file (maxfiles = 1 option is set for the form_filemanager). 270 if (count($files) == 0) { 271 $content->content = null; 272 } else { 273 $file = array_values($files)[0]; 274 275 if (count($files) > 1) { 276 // This should not happen with a consistent database. Inform admins/developers about the inconsistency. 277 debugging('more then one file found in mod_data instance {$this->data->id} picture field (field id: {$this->field->id}) area during update data record {$recordid} (content id: {$content->id})', DEBUG_NORMAL); 278 } 279 280 if ($file->get_imageinfo() === false) { 281 $url = new moodle_url('/mod/data/edit.php', array('d' => $this->field->dataid)); 282 redirect($url, get_string('invalidfiletype', 'error', $file->get_filename())); 283 } 284 $content->content = $file->get_filename(); 285 $this->update_thumbnail($content, $file); 286 } 287 $DB->update_record('data_content', $content); 288 289 break; 290 291 case 'alttext': 292 // only changing alt tag 293 $content->content1 = clean_param($value, PARAM_NOTAGS); 294 $DB->update_record('data_content', $content); 295 break; 296 297 default: 298 break; 299 } 300 } 301 302 function update_thumbnail($content, $file) { 303 // (Re)generate thumbnail image according to the dimensions specified in the field settings. 304 // If thumbnail width and height are BOTH not specified then no thumbnail is generated, and 305 // additionally an attempted delete of the existing thumbnail takes place. 306 $fs = get_file_storage(); 307 $file_record = array('contextid'=>$file->get_contextid(), 'component'=>$file->get_component(), 'filearea'=>$file->get_filearea(), 308 'itemid'=>$file->get_itemid(), 'filepath'=>$file->get_filepath(), 309 'filename'=>'thumb_'.$file->get_filename(), 'userid'=>$file->get_userid()); 310 try { 311 // this may fail for various reasons 312 $fs->convert_image($file_record, $file, (int) $this->field->param4, (int) $this->field->param5, true); 313 return true; 314 } catch (Exception $e) { 315 debugging($e->getMessage()); 316 return false; 317 } 318 } 319 320 function text_export_supported() { 321 return false; 322 } 323 324 function file_ok($path) { 325 return true; 326 } 327 328 /** 329 * Custom notempty function 330 * 331 * @param string $value 332 * @param string $name 333 * @return bool 334 */ 335 function notemptyfield($value, $name) { 336 global $USER; 337 338 $names = explode('_', $name); 339 if ($names[2] == 'file') { 340 $usercontext = context_user::instance($USER->id); 341 $fs = get_file_storage(); 342 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value); 343 return count($files) >= 2; 344 } 345 return false; 346 } 347 348 /** 349 * Return the plugin configs for external functions. 350 * 351 * @return array the list of config parameters 352 * @since Moodle 3.3 353 */ 354 public function get_config_for_external() { 355 // Return all the config parameters. 356 $configs = []; 357 for ($i = 1; $i <= 10; $i++) { 358 $configs["param$i"] = $this->field->{"param$i"}; 359 } 360 return $configs; 361 } 362 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body