|
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_file extends data_field_base { 00026 var $type = 'file'; 00027 00028 function display_add_field($recordid=0) { 00029 global $CFG, $DB, $OUTPUT, $PAGE, $USER; 00030 00031 $file = false; 00032 $content = false; 00033 $displayname = ''; 00034 $fs = get_file_storage(); 00035 $context = $PAGE->context; 00036 $itemid = null; 00037 00038 // editing an existing database entry 00039 if ($recordid){ 00040 if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 00041 00042 file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id); 00043 00044 if (!empty($content->content)) { 00045 if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { 00046 $usercontext = get_context_instance(CONTEXT_USER, $USER->id); 00047 if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid, 'id DESC', false)) { 00048 return false; 00049 } 00050 if (empty($content->content1)) { 00051 // Print icon if file already exists 00052 $src = moodle_url::make_draftfile_url($itemid, '/', $file->get_filename()); 00053 $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>'; 00054 00055 } else { 00056 $displayname = 'no file added'; 00057 } 00058 } 00059 } 00060 } 00061 } else { 00062 $itemid = file_get_unused_draft_itemid(); 00063 } 00064 00065 $html = ''; 00066 // database entry label 00067 $html .= '<div title="'.s($this->field->description).'">'; 00068 $html .= '<fieldset><legend><span class="accesshide">'.$this->field->name.'</span></legend>'; 00069 00070 // itemid element 00071 $html .= '<input type="hidden" name="field_'.$this->field->id.'_file" value="'.$itemid.'" />'; 00072 00073 $options = new stdClass(); 00074 $options->maxbytes = $this->field->param3; 00075 $options->itemid = $itemid; 00076 $options->accepted_types = '*'; 00077 $options->return_types = FILE_INTERNAL; 00078 $options->context = $PAGE->context; 00079 00080 $fp = new file_picker($options); 00081 // print out file picker 00082 $html .= $OUTPUT->render($fp); 00083 00084 $html .= '</fieldset>'; 00085 $html .= '</div>'; 00086 00087 $module = array('name'=>'data_filepicker', 'fullpath'=>'/mod/data/data.js', 'requires'=>array('core_filepicker')); 00088 $PAGE->requires->js_init_call('M.data_filepicker.init', array($fp->options), true, $module); 00089 00090 return $html; 00091 } 00092 00093 function display_search_field($value = '') { 00094 return '<input type="text" size="16" name="f_'.$this->field->id.'" value="'.$value.'" />'; 00095 } 00096 00097 function generate_sql($tablealias, $value) { 00098 global $DB; 00099 00100 static $i=0; 00101 $i++; 00102 $name = "df_file_$i"; 00103 return array(" ({$tablealias}.fieldid = {$this->field->id} AND ".$DB->sql_like("{$tablealias}.content", ":$name", false).") ", array($name=>"%$value%")); 00104 } 00105 00106 function parse_search_field() { 00107 return optional_param('f_'.$this->field->id, '', PARAM_NOTAGS); 00108 } 00109 00110 function get_file($recordid, $content=null) { 00111 global $DB; 00112 if (empty($content)) { 00113 if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 00114 return null; 00115 } 00116 } 00117 $fs = get_file_storage(); 00118 if (!$file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) { 00119 return null; 00120 } 00121 00122 return $file; 00123 } 00124 00125 function display_browse_field($recordid, $template) { 00126 global $CFG, $DB, $OUTPUT; 00127 00128 if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 00129 return ''; 00130 } 00131 00132 if (empty($content->content)) { 00133 return ''; 00134 } 00135 00136 if (!$file = $this->get_file($recordid, $content)) { 00137 return ''; 00138 } 00139 00140 $name = empty($content->content1) ? $file->get_filename() : $content->content1; 00141 $src = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$this->context->id.'/mod_data/content/'.$content->id.'/'.$file->get_filename()); 00142 $width = $this->field->param1 ? ' width = "'.s($this->field->param1).'" ':' '; 00143 $height = $this->field->param2 ? ' height = "'.s($this->field->param2).'" ':' '; 00144 00145 $str = '<img src="'.$OUTPUT->pix_url(file_mimetype_icon($file->get_mimetype())).'" height="16" width="16" alt="'.$file->get_mimetype().'" /> '. 00146 '<a href="'.$src.'" >'.s($name).'</a>'; 00147 return $str; 00148 } 00149 00150 00151 // content: "a##b" where a is the file name, b is the display name 00152 function update_content($recordid, $value, $name) { 00153 global $CFG, $DB, $USER; 00154 $fs = get_file_storage(); 00155 00156 if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) { 00157 00158 // Quickly make one now! 00159 $content = new stdClass(); 00160 $content->fieldid = $this->field->id; 00161 $content->recordid = $recordid; 00162 $id = $DB->insert_record('data_content', $content); 00163 $content = $DB->get_record('data_content', array('id'=>$id)); 00164 } 00165 00166 // delete existing files 00167 $fs->delete_area_files($this->context->id, 'mod_data', 'content', $content->id); 00168 00169 $usercontext = get_context_instance(CONTEXT_USER, $USER->id); 00170 $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value, 'timecreated DESC'); 00171 00172 if (count($files)<2) { 00173 // no file 00174 } else { 00175 foreach ($files as $draftfile) { 00176 if (!$draftfile->is_directory()) { 00177 $file_record = array( 00178 'contextid' => $this->context->id, 00179 'component' => 'mod_data', 00180 'filearea' => 'content', 00181 'itemid' => $content->id, 00182 'filepath' => '/', 00183 'filename' => $draftfile->get_filename(), 00184 ); 00185 00186 $content->content = $file_record['filename']; 00187 00188 $fs->create_file_from_storedfile($file_record, $draftfile); 00189 $DB->update_record('data_content', $content); 00190 00191 // Break from the loop now to avoid overwriting the uploaded file record 00192 break; 00193 } 00194 } 00195 } 00196 } 00197 00198 function text_export_supported() { 00199 return false; 00200 } 00201 00202 function file_ok($path) { 00203 return true; 00204 } 00205 00206 } 00207 00208