Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Class file field for database activity 19 * 20 * @package datafield_file 21 * @copyright 2005 Martin Dougiamas 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 class data_field_file extends data_field_base { 25 var $type = 'file'; 26 27 public function supports_preview(): bool { 28 return true; 29 } 30 31 public function get_data_content_preview(int $recordid): stdClass { 32 return (object)[ 33 'id' => 0, 34 'fieldid' => $this->field->id, 35 'recordid' => $recordid, 36 'content' => 'samplefile.csv', 37 'content1' => 'samplefile.csv', 38 'content2' => null, 39 'content3' => null, 40 'content4' => null, 41 ]; 42 } 43 44 function display_add_field($recordid = 0, $formdata = null) { 45 global $CFG, $DB, $OUTPUT, $PAGE; 46 47 // Necessary for the constants used in args. 48 require_once($CFG->dirroot . '/repository/lib.php'); 49 50 $itemid = null; 51 52 // editing an existing database entry 53 if ($formdata) { 54 $fieldname = 'field_' . $this->field->id . '_file'; 55 $itemid = clean_param($formdata->$fieldname, PARAM_INT); 56 } else if ($recordid) { 57 if (!$content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) { 58 // Quickly make one now! 59 $content = new stdClass(); 60 $content->fieldid = $this->field->id; 61 $content->recordid = $recordid; 62 $id = $DB->insert_record('data_content', $content); 63 $content = $DB->get_record('data_content', array('id' => $id)); 64 } 65 file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id); 66 67 } else { 68 $itemid = file_get_unused_draft_itemid(); 69 } 70 71 // database entry label 72 $html = '<div title="' . s($this->field->description) . '">'; 73 $html .= '<fieldset><legend><span class="accesshide">'.$this->field->name; 74 75 if ($this->field->required) { 76 $html .= ' ' . get_string('requiredelement', 'form') . '</span></legend>'; 77 $image = $OUTPUT->pix_icon('req', get_string('requiredelement', 'form')); 78 $html .= html_writer::div($image, 'inline-req'); 79 } else { 80 $html .= '</span></legend>'; 81 } 82 83 // itemid element 84 $html .= '<input type="hidden" name="field_'.$this->field->id.'_file" value="'.s($itemid).'" />'; 85 86 $options = new stdClass(); 87 $options->maxbytes = $this->field->param3; 88 $options->maxfiles = 1; // Limit to one file for the moment, this may be changed if requested as a feature in the future. 89 $options->itemid = $itemid; 90 $options->accepted_types = '*'; 91 $options->return_types = FILE_INTERNAL | FILE_CONTROLLED_LINK; 92 $options->context = $PAGE->context; 93 94 $fm = new form_filemanager($options); 95 // Print out file manager. 96 97 $output = $PAGE->get_renderer('core', 'files'); 98 $html .= '<div class="mod-data-input">'; 99 $html .= $output->render($fm); 100 $html .= '</div>'; 101 $html .= '</fieldset>'; 102 $html .= '</div>'; 103 104 return $html; 105 } 106 107 function display_search_field($value = '') { 108 return '<label class="accesshide" for="f_' . $this->field->id . '">' . $this->field->name . '</label>' . 109 '<input type="text" size="16" id="f_'.$this->field->id.'" name="f_'.$this->field->id.'" ' . 110 'value="'.s($value).'" class="form-control"/>'; 111 } 112 113 function generate_sql($tablealias, $value) { 114 global $DB; 115 116 static $i=0; 117 $i++; 118 $name = "df_file_$i"; 119 return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%")); 120 } 121 122 public function parse_search_field($defaults = null) { 123 $param = 'f_'.$this->field->id; 124 if (empty($defaults[$param])) { 125 $defaults = array($param => ''); 126 } 127 return optional_param($param, $defaults[$param], PARAM_NOTAGS); 128 } 129 130 function get_file($recordid, $content=null) { 131 global $DB; 132 if (empty($content)) { 133 if (!$content = $this->get_data_content($recordid)) { 134 return null; 135 } 136 } 137 $fs = get_file_storage(); 138 if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { 139 return null; 140 } 141 142 return $file; 143 } 144 145 function display_browse_field($recordid, $template) { 146 global $OUTPUT; 147 148 $content = $this->get_data_content($recordid); 149 150 if (!$content || empty($content->content)) { 151 return ''; 152 } 153 154 $file = null; 155 $url = ''; 156 $name = !empty($content->content1) ? $content->content1 : $content->content; 157 158 if ($this->preview) { 159 $file = (object)[ 160 'filename' => $content->content, 161 'mimetype' => 'text/csv', 162 ]; 163 $name = $content->content; 164 } else { 165 $file = $this->get_file($recordid, $content); 166 if (!$file) { 167 return ''; 168 } 169 $fileurl = moodle_url::make_pluginfile_url( 170 $file->get_contextid(), 171 $file->get_component(), 172 $file->get_filearea(), 173 $file->get_itemid(), 174 $file->get_filepath(), 175 $file->get_filename() 176 ); 177 $url = $fileurl->out(); 178 } 179 180 $icon = $OUTPUT->pix_icon( 181 file_file_icon($file), 182 get_mimetype_description($file), 183 'moodle', 184 ['width' => 16, 'height' => 16] 185 ); 186 187 return $icon . ' <a class="data-field-link" href="'.$url.'" >' . s($name) . '</a>'; 188 } 189 190 191 // content: "a##b" where a is the file name, b is the display name 192 function update_content($recordid, $value, $name='') { 193 global $CFG, $DB, $USER; 194 $fs = get_file_storage(); 195 196 // Should always be available since it is set by display_add_field before initializing the draft area. 197 $content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid)); 198 if (!$content) { 199 $content = (object)array('fieldid' => $this->field->id, 'recordid' => $recordid); 200 $content->id = $DB->insert_record('data_content', $content); 201 } 202 203 file_save_draft_area_files($value, $this->context->id, 'mod_data', 'content', $content->id); 204 205 $usercontext = context_user::instance($USER->id); 206 $files = $fs->get_area_files($this->context->id, 'mod_data', 'content', $content->id, 'itemid, filepath, filename', false); 207 208 // We expect no or just one file (maxfiles = 1 option is set for the form_filemanager). 209 if (count($files) == 0) { 210 $content->content = null; 211 } else { 212 $content->content = array_values($files)[0]->get_filename(); 213 if (count($files) > 1) { 214 // This should not happen with a consistent database. Inform admins/developers about the inconsistency. 215 debugging('more then one file found in mod_data instance {$this->data->id} file field (field id: {$this->field->id}) area during update data record {$recordid} (content id: {$content->id})', DEBUG_NORMAL); 216 } 217 } 218 $DB->update_record('data_content', $content); 219 } 220 221 /** 222 * Here we export the text value of a file field which is the filename of the exported file. 223 * 224 * @param stdClass $record the record which is being exported 225 * @return string the value which will be stored in the exported file for this field 226 */ 227 public function export_text_value(stdClass $record): string { 228 return !empty($record->content) ? $record->content : ''; 229 } 230 231 /** 232 * Specifies that this field type supports the export of files. 233 * 234 * @return bool true which means that file export is being supported by this field type 235 */ 236 public function file_export_supported(): bool { 237 return true; 238 } 239 240 /** 241 * Specifies that this field type supports the import of files. 242 * 243 * @return bool true which means that file import is being supported by this field type 244 */ 245 public function file_import_supported(): bool { 246 return true; 247 } 248 249 /** 250 * Provides the necessary code for importing a file when importing the content of a mod_data instance. 251 * 252 * @param int $contentid the id of the mod_data content record 253 * @param string $filecontent the content of the file to import as string 254 * @param string $filename the filename the imported file should get 255 * @return void 256 */ 257 public function import_file_value(int $contentid, string $filecontent, string $filename): void { 258 $filerecord = [ 259 'contextid' => $this->context->id, 260 'component' => 'mod_data', 261 'filearea' => 'content', 262 'itemid' => $contentid, 263 'filepath' => '/', 264 'filename' => $filename, 265 ]; 266 $fs = get_file_storage(); 267 $fs->create_file_from_string($filerecord, $filecontent); 268 } 269 270 /** 271 * Exports the file content for file export. 272 * 273 * @param stdClass $record the data content record the file belongs to 274 * @return null|string The file content of the stored file or null if no file should be exported for this record 275 */ 276 public function export_file_value(stdClass $record): null|string { 277 $file = $this->get_file($record->id); 278 return $file ? $file->get_content() : null; 279 } 280 281 function file_ok($path) { 282 return true; 283 } 284 285 /** 286 * Custom notempty function 287 * 288 * @param string $value 289 * @param string $name 290 * @return bool 291 */ 292 function notemptyfield($value, $name) { 293 global $USER; 294 295 $names = explode('_', $name); 296 if ($names[2] == 'file') { 297 $usercontext = context_user::instance($USER->id); 298 $fs = get_file_storage(); 299 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value); 300 return count($files) >= 2; 301 } 302 return false; 303 } 304 305 /** 306 * Return the plugin configs for external functions. 307 * 308 * @return array the list of config parameters 309 * @since Moodle 3.3 310 */ 311 public function get_config_for_external() { 312 // Return all the config parameters. 313 $configs = []; 314 for ($i = 1; $i <= 10; $i++) { 315 $configs["param$i"] = $this->field->{"param$i"}; 316 } 317 return $configs; 318 } 319 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body