/** * Returns file link * @param object $file * @param object $folder * @param object $context * @return string html link */ function folder_get_file_link($file, $folder, $context) { global $CFG; $strfile = get_string('file'); $strdownload = get_string('download'); $icon = mimeinfo_from_type("icon", $file->get_mimetype()); $urlbase = "{$CFG->wwwroot}/pluginfile.php"; $path = '/' . $context->id . '/folder_content/' . $folder->revision . $file->get_filepath() . $file->get_filename(); $viewurl = file_encode_url($urlbase, $path, false); $downloadurl = file_encode_url($urlbase, $path, true); $downloadurl = " <a href=\"{$downloadurl}\" title=\"" . get_string('downloadfile') . "\"><img src=\"{$CFG->pixpath}/t/down.gif\" class=\"iconsmall\" alt=\"{$strdownload}\" /></a>"; return "<a href=\"{$viewurl}\" title=\"\"><img src=\"{$CFG->pixpath}/f/{$icon}\" class=\"icon\" alt=\"{$strfile}\" /> " . s($file->get_filename()) . '</a>' . $downloadurl; }
function print_student_answer($userid, $return = false) { global $CFG, $USER; $fs = get_file_storage(); $browser = get_file_browser(); $output = ''; if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $userid, "timemodified", false)) { foreach ($files as $file) { $filename = $file->get_filename(); $found = true; $mimetype = $file->get_mimetype(); $icon = mimeinfo_from_type('icon', $mimetype); $path = $browser->encodepath($CFG->wwwroot . '/pluginfile.php', '/' . $this->context->id . '/assignment_submission/' . $userid . '/' . $filename); $output .= '<a href="' . $path . '" ><img class="icon" src="' . $CFG->pixpath . '/f/' . $icon . '" alt="' . $icon . '" />' . s($filename) . '</a><br />'; } } $output = '<div class="files">' . $output . '</div>'; return $output; }
function displaydir($file_info) { global $CFG; $children = $file_info->get_children(); $parent_info = $file_info->get_parent(); $strname = get_string('name'); $strsize = get_string('size'); $strmodified = get_string('modified'); $strfolder = get_string('folder'); $strfile = get_string('file'); $strdownload = get_string('download'); $strdelete = get_string('delete'); $straction = get_string('action'); $path = array(); $params = $file_info->get_params_rawencoded(); $params = implode('&', $params); $path[] = $file_info->get_visible_name(); $level = $parent_info; while ($level) { $params = $level->get_params_rawencoded(); $params = implode('&', $params); $path[] = '<a href="index.php?' . $params . '">' . $level->get_visible_name() . '</a>'; $level = $level->get_parent(); } $path = array_reverse($path); $path = implode(' / ', $path); echo $path . ' /'; echo "<div>"; echo "<hr/>"; echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"740\" class=\"files\">"; echo "<tr>"; echo "<th class=\"header\" scope=\"col\"></th>"; echo "<th class=\"header name\" scope=\"col\">{$strname}</th>"; echo "<th class=\"header size\" scope=\"col\">{$strsize}</th>"; echo "<th class=\"header date\" scope=\"col\">{$strmodified}</th>"; echo "<th class=\"header commands\" scope=\"col\">{$straction}</th>"; echo "</tr>\n"; $parentwritable = $file_info->is_writable(); if ($parent_info) { $params = $parent_info->get_params_rawencoded(); $params = implode('&', $params); echo "<tr class=\"folder\">"; print_cell(); print_cell('left', '<a href="index.php?' . $params . '"><img src="' . $CFG->pixpath . '/f/parent.gif" class="icon" alt="" /> ' . get_string('parentfolder') . '</a>', 'name'); print_cell(); print_cell(); print_cell(); echo "</tr>"; } if ($children) { foreach ($children as $child_info) { $filename = $child_info->get_visible_name(); $filesize = $child_info->get_filesize(); $filesize = $filesize ? display_size($filesize) : ''; $filedate = $child_info->get_timemodified(); $filedate = $filedate ? userdate($filedate) : ''; $mimetype = $child_info->get_mimetype(); $params = $child_info->get_params_rawencoded(); $params = implode('&', $params); if ($child_info->is_directory()) { echo "<tr class=\"folder\">"; print_cell(); print_cell("left", "<a href=\"index.php?{$params}\"><img src=\"{$CFG->pixpath}/f/folder.gif\" class=\"icon\" alt=\"{$strfolder}\" /> " . s($filename) . "</a>", 'name'); print_cell("right", $filesize, 'size'); print_cell("right", $filedate, 'date'); if ($parentwritable) { print_cell("right", "<a href=\"index.php?{$params}&sesskey=" . sesskey() . "&delete=1\"><img src=\"{$CFG->pixpath}/t/delete.gif\" class=\"iconsmall\" alt=\"{$strdelete}\" /></a>", 'command'); } else { print_cell(); } echo "</tr>"; } else { $icon = mimeinfo_from_type("icon", $mimetype); if ($downloadurl = $child_info->get_url(true)) { $downloadurl = " <a href=\"{$downloadurl}\" title=\"" . get_string('downloadfile') . "\"><img src=\"{$CFG->pixpath}/t/down.gif\" class=\"iconsmall\" alt=\"{$strdownload}\" /></a>"; } else { $downloadurl = ''; } if ($viewurl = $child_info->get_url()) { $viewurl = " " . link_to_popup_window($viewurl, "display", "<img src=\"{$CFG->pixpath}/t/preview.gif\" class=\"iconsmall\" alt=\"{$strfile}\" /> ", 480, 640, get_string('viewfileinpopup'), null, true); } else { $viewurl = ''; } echo "<tr class=\"file\">"; print_cell(); print_cell("left", "<img src=\"{$CFG->pixpath}/f/{$icon}\" class=\"icon\" alt=\"{$strfile}\" /> " . s($filename) . $downloadurl . $viewurl, 'name'); print_cell("right", $filesize, 'size'); print_cell("right", $filedate, 'date'); if ($parentwritable) { print_cell("right", "<a href=\"index.php?{$params}&sesskey=" . sesskey() . "&delete=1\"><img src=\"{$CFG->pixpath}/t/delete.gif\" class=\"iconsmall\" alt=\"{$strdelete}\" /></a>", 'command'); } else { print_cell(); } echo "</tr>"; } } } echo "</table>"; echo "</div>"; echo "<hr/>"; }
function _get_draftfiles($draftid, $suffix) { global $USER, $CFG; $html = ''; if (!($context = get_context_instance(CONTEXT_USER, $USER->id))) { } $contextid = $context->id; $filearea = 'user_draft'; $browser = get_file_browser(); $fs = get_file_storage(); $filepath = '/'; if (!($directory = $fs->get_file($context->id, 'user_draft', $draftid, $filepath, '.'))) { $directory = new virtual_root_file($context->id, 'user_draft', $draftid); $filepath = $directory->get_filepath(); } $files = $fs->get_directory_files($context->id, 'user_draft', $draftid, $directory->get_filepath()); $parent = $directory->get_parent_directory(); $html .= '<ul class="file-list" id="draftfiles-' . $suffix . '">'; foreach ($files as $file) { $filename = $file->get_filename(); $filenameurl = rawurlencode($filename); $filepath = $file->get_filepath(); $filesize = $file->get_filesize(); $filesize = $filesize ? display_size($filesize) : ''; $mimetype = $file->get_mimetype(); $icon = mimeinfo_from_type('icon', $mimetype); $viewurl = $browser->encodepath("{$CFG->wwwroot}/draftfile.php", "/{$contextid}/user_draft/{$draftid}" . $filepath . $filename, false, false); $html .= '<li>'; $html .= "<a href=\"{$viewurl}\"><img src=\"{$CFG->pixpath}/f/{$icon}\" class=\"icon\" /> " . s($filename) . " ({$filesize})</a> "; $html .= "<a href=\"###\" onclick='rm_{$suffix}(" . $file->get_itemid() . ", \"" . $filename . "\", this)'><img src=\"{$CFG->pixpath}/t/delete.gif\" class=\"iconsmall\" /></a>"; $html .= '</li>'; } $html .= '</ul>'; return $html; }
function toHtml() { global $CFG, $OUTPUT; $htmltable = new html_table(); $htmltable->head = array(get_string('deleteupload', 'wiki'), get_string('uploadname', 'wiki'), get_string('uploadactions', 'wiki')); $fs = get_file_storage(); $files = $fs->get_area_files($this->_fileinfo['contextid'], 'mod_wiki', 'attachments', $this->_fileinfo['itemid']); //TODO: verify where this is coming from, all params must be validated (skodak) if (count($files) < 2) { return get_string('noattachments', 'wiki'); } //get tags foreach (array('image', 'attach', 'link') as $tag) { $tags[$tag] = wiki_parser_get_token($this->_format, $tag); } foreach ($files as $file) { if (!$file->is_directory()) { $checkbox = '<input type="checkbox" name="' . $this->_attributes['name'] . '[]" value="' . $file->get_pathnamehash() . '"'; if (in_array($file->get_pathnamehash(), $this->_value)) { $checkbox .= ' checked="checked"'; } $checkbox .= " />"; //actions $icon = mimeinfo_from_type('icon', $file->get_mimetype()); $file_url = file_encode_url($CFG->wwwroot . '/pluginfile.php', "/{$this->_contextid}/mod_wiki/attachments/{$this->_fileareaitemid}/" . $file->get_filename()); $action_icons = ""; if (!empty($tags['attach'])) { $action_icons .= "<a href=\"javascript:void(0)\" class=\"wiki-attachment-attach\" " . $this->printInsertTags($tags['attach'], $file->get_filename()) . " title=\"" . get_string('attachmentattach', 'wiki') . "\"><img src=\"" . $OUTPUT->pix_url('f/pdf')->out() . "\" alt=\"Attach\" /></a>"; //TODO: localize } $action_icons .= " <a href=\"javascript:void(0)\" class=\"wiki-attachment-link\" " . $this->printInsertTags($tags['link'], $file_url) . " title=\"" . get_string('attachmentlink', 'wiki') . "\"><img src=\"" . $OUTPUT->pix_url('f/web')->out() . "\" alt=\"Link\" /></a>"; if ($icon == 'image') { $action_icons .= " <a href=\"javascript:void(0)\" class=\"wiki-attachment-image\" " . $this->printInsertTags($tags['image'], $file->get_filename()) . " title=\"" . get_string('attachmentimage', 'wiki') . "\"><img src=\"" . $OUTPUT->pix_url('f/image')->out() . "\" alt=\"Image\" /></a>"; //TODO: localize } $htmltable->data[] = array($checkbox, '<a href="' . $file_url . '">' . $file->get_filename() . '</a>', $action_icons); } } return html_writer::table($htmltable); }
/** * Returns the relative icon path for a given mime type * * This function should be used in conjuction with $OUTPUT->old_icon_url to produce * a return the full path to an icon. * * <code> * $mimetype = 'image/jpg'; * $icon = $OUTPUT->old_icon_url(file_mimetype_icon($mimetype)); * echo '<img src="'.$icon.'" alt="'.$mimetype.'" />'; * </code> * * @todo When an $OUTPUT->icon method is available this function should be altered * to conform with that. * * @param string $mimetype The mimetype to fetch an icon for * @param int $size The size of the icon. Not yet implemented * @return string The relative path to the icon */ function file_mimetype_icon($mimetype, $size = null) { $icon = mimeinfo_from_type('icon', $mimetype); $icon = substr($icon, 0, strrpos($icon, '.')); if ($size != null && is_int($size)) { $icon .= '-' . $size; } return 'f/' . $icon; }
function display_browse_field($recordid, $template) { global $CFG, $DB; if (!($content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid)))) { return ''; } if (empty($content->content)) { return ''; } $browser = get_file_browser(); if (!($file = $this->get_file($recordid, $content))) { return ''; } $name = empty($content->content1) ? $file->get_filename() : $content->content1; $icon = mimeinfo_from_type('icon', $file->get_mimetype()); $src = $browser->encodepath($CFG->wwwroot . '/pluginfile.php', '/' . $this->context->id . '/data_content/' . $content->id . '/' . $file->get_filename()); $width = $this->field->param1 ? ' width = "' . s($this->field->param1) . '" ' : ' '; $height = $this->field->param2 ? ' height = "' . s($this->field->param2) . '" ' : ' '; $str = '<img src="' . $CFG->pixpath . '/f/' . $icon . '" height="16" width="16" alt="' . $icon . '" /> ' . '<a href="' . $src . '" >' . s($name) . '</a>'; return $str; }
/** * Obtains descriptions for file types (e.g. 'Microsoft Word document') from the * mimetypes.php language file. * * @param mixed $obj - instance of stored_file or file_info or array/stdClass with field * 'filename' and 'mimetype', or just a string with mimetype (though it is recommended to * have filename); In case of array/stdClass the field 'mimetype' is optional. * @param bool $capitalise If true, capitalises first character of result * @return string Text description */ function get_mimetype_description($obj, $capitalise = false) { $filename = $mimetype = ''; if (is_object($obj) && method_exists($obj, 'get_filename') && method_exists($obj, 'get_mimetype')) { // this is an instance of stored_file $mimetype = $obj->get_mimetype(); $filename = $obj->get_filename(); } else { if (is_object($obj) && method_exists($obj, 'get_visible_name') && method_exists($obj, 'get_mimetype')) { // this is an instance of file_info $mimetype = $obj->get_mimetype(); $filename = $obj->get_visible_name(); } else { if (is_array($obj) || is_object($obj)) { $obj = (array) $obj; if (!empty($obj['filename'])) { $filename = $obj['filename']; } if (!empty($obj['mimetype'])) { $mimetype = $obj['mimetype']; } } else { $mimetype = $obj; } } } $mimetypefromext = mimeinfo('type', $filename); if (empty($mimetype) || $mimetypefromext !== 'document/unknown') { // if file has a known extension, overwrite the specified mimetype $mimetype = $mimetypefromext; } $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); if (empty($extension)) { $mimetypestr = mimeinfo_from_type('string', $mimetype); $extension = str_replace('.', '', mimeinfo_from_type('extension', $mimetype)); } else { $mimetypestr = mimeinfo('string', $filename); } $chunks = explode('/', $mimetype, 2); $chunks[] = ''; $attr = array('mimetype' => $mimetype, 'ext' => $extension, 'mimetype1' => $chunks[0], 'mimetype2' => $chunks[1]); $a = array(); foreach ($attr as $key => $value) { $a[$key] = $value; $a[strtoupper($key)] = strtoupper($value); $a[ucfirst($key)] = ucfirst($value); } // MIME types may include + symbol but this is not permitted in string ids. $safemimetype = str_replace('+', '_', $mimetype); $safemimetypestr = str_replace('+', '_', $mimetypestr); $customdescription = mimeinfo('customdescription', $filename); if ($customdescription) { // Call format_string on the custom description so that multilang // filter can be used (if enabled on system context). We use system // context because it is possible that the page context might not have // been defined yet. $result = format_string($customdescription, true, array('context' => context_system::instance())); } else { if (get_string_manager()->string_exists($safemimetype, 'mimetypes')) { $result = get_string($safemimetype, 'mimetypes', (object) $a); } else { if (get_string_manager()->string_exists($safemimetypestr, 'mimetypes')) { $result = get_string($safemimetypestr, 'mimetypes', (object) $a); } else { if (get_string_manager()->string_exists('default', 'mimetypes')) { $result = get_string('default', 'mimetypes', (object) $a); } else { $result = $mimetype; } } } } if ($capitalise) { $result = ucfirst($result); } return $result; }
/** * Return mimetype by given file pathname * * If file has a known extension, we return the mimetype based on extension. * Otherwise (when possible) we try to get the mimetype from file contents. * * @param string $pathname full path to the file * @param string $filename correct file name with extension, if omitted will be taken from $path * @return string */ public static function mimetype($pathname, $filename = null) { if (empty($filename)) { $filename = $pathname; } $type = mimeinfo('type', $filename); if ($type === 'document/unknown' && class_exists('finfo') && file_exists($pathname)) { $finfo = new finfo(FILEINFO_MIME_TYPE); $type = mimeinfo_from_type('type', $finfo->file($pathname)); } return $type; }
function _get_draftfiles($draftid, $suffix) { global $USER, $OUTPUT, $CFG; $context = get_context_instance(CONTEXT_USER, $USER->id); $fs = get_file_storage(); $html = '<ul class="file-list" id="draftfiles-' . $suffix . '">'; if ($files = $fs->get_directory_files($context->id, 'user_draft', $draftid, '/', true)) { foreach ($files as $file) { if ($file->is_directory()) { continue; } $filename = $file->get_filename(); $filepath = $file->get_filepath(); $fullname = ltrim($filepath . $filename, '/'); $filesize = $file->get_filesize(); $filesize = $filesize ? display_size($filesize) : ''; $icon = mimeinfo_from_type('icon', $file->get_mimetype()); $icon = str_replace('.gif', '', $icon); //TODO: temp icon fix $viewurl = file_encode_url("{$CFG->wwwroot}/draftfile.php", "/{$context->id}/user_draft/{$draftid}/" . $fullname, false, false); $html .= '<li>'; $html .= "<a href=\"{$viewurl}\"><img src=\"" . $OUTPUT->old_icon_url('f/' . $icon) . "\" class=\"icon\" /> " . s($fullname) . " ({$filesize})</a> "; // TODO: maybe better use file->id here - but then make 100% it is from my own draftfiles ;-) // anyway this does not work for subdirectories $html .= "<a href=\"###\" onclick='rm_file(" . $file->get_itemid() . ", \"" . addslashes_js($fullname) . "\", this)'><img src=\"" . $OUTPUT->old_icon_url('t/delete') . "\" class=\"iconsmall\" /></a>"; $html .= '</li>'; } } $html .= '</ul>'; return $html; }
/** * Return mimetype by given file pathname * * This method uses fileinfo module to get mimetype using magic bytes if file exists. * If not, it will get mimetype based on filename * * @param string $pathname * @return string */ public static function mimetype($pathname) { if (file_exists($pathname)) { $finfo = new finfo(FILEINFO_MIME_TYPE); return mimeinfo_from_type('type', $finfo->file($pathname)); } else { return mimeinfo('type', $pathname); } }
$filenameurl = rawurlencode($filename); $filepath = $file->get_filepath(); $filesize = $file->get_filesize(); $filesize = $filesize ? display_size($filesize) : ''; $mimetype = $file->get_mimetype(); if ($file->is_directory()) { if ($subdirs) { $dirname = explode('/', trim($filepath, '/')); $dirname = array_pop($dirname); echo '<div class="folder">'; echo "<a href=\"draftfiles.php?itemid={$itemid}&filepath={$filepath}&subdirs={$subdirs}&maxbytes={$maxbytes}\"><img src=\"{$CFG->pixpath}/f/folder.gif\" class=\"icon\" alt=\"{$strfolder}\" /> " . s($dirname) . "</a> "; echo "<a href=\"draftfiles.php?itemid={$itemid}&filepath={$filepath}&delete={$filenameurl}&subdirs={$subdirs}&maxbytes={$maxbytes}\"><img src=\"{$CFG->pixpath}/t/delete.gif\" class=\"iconsmall\" alt=\"{$strdelete}\" /></a>"; echo '</div>'; } } else { $icon = mimeinfo_from_type('icon', $mimetype); $viewurl = $browser->encodepath("{$CFG->wwwroot}/draftfile.php", "/{$contextid}/user_draft/{$itemid}" . $filepath . $filename, false, false); echo '<div class="file">'; echo "<a href=\"{$viewurl}\"><img src=\"{$CFG->pixpath}/f/{$icon}\" class=\"icon\" alt=\"{$strfile}\" /> " . s($filename) . " ({$filesize})</a> "; echo "<a href=\"draftfiles.php?itemid={$itemid}&filepath={$filepath}&delete={$filenameurl}&subdirs={$subdirs}&maxbytes={$maxbytes}\"><img src=\"{$CFG->pixpath}/t/delete.gif\" class=\"iconsmall\" alt=\"{$strdelete}\" /></a>"; echo '</div>'; } } echo '</div>'; if ($maxbytes == 0 or $maxbytes > $totalbytes) { echo '<form enctype="multipart/form-data" method="post" action="draftfiles.php"><div>'; if ($maxbytes) { echo '<input type="hidden" name="MAX_FILE_SIZE" value="' . ($maxbytes - $totalbytes) . '" />'; } echo '<input type="hidden" name="itemid" value="' . $itemid . '" />'; echo '<input type="hidden" name="filepath" value="' . s($filepath) . '" />';
/** * if return=html, then return a html string. * if return=text, then return a text-only string. * otherwise, print HTML for non-images, and return image HTML */ function blog_print_attachments($blogentry, $return = NULL) { global $CFG; require_once $CFG->libdir . '/filelib.php'; $fs = get_file_storage(); $browser = get_file_browser(); $files = $fs->get_area_files(SYSCONTEXTID, 'blog', $blogentry->id); $imagereturn = ""; $output = ""; $strattachment = get_string("attachment", "forum"); foreach ($files as $file) { if ($file->is_directory()) { continue; } $filename = $file->get_filename(); $ffurl = $browser->encodepath($CFG->wwwroot . '/pluginfile.php', '/' . SYSCONTEXTID . '/blog/' . $blogentry->id . '/' . $filename); $type = $file->get_mimetype(); $icon = mimeinfo_from_type("icon", $type); $type = mimeinfo_from_type("type", $type); $image = "<img src=\"{$CFG->pixpath}/f/{$icon}\" class=\"icon\" alt=\"\" />"; if ($return == "html") { $output .= "<a href=\"{$ffurl}\">{$image}</a> "; $output .= "<a href=\"{$ffurl}\">{$filename}</a><br />"; } else { if ($return == "text") { $output .= "{$strattachment} {$filename}:\n{$ffurl}\n"; } else { if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) { // Image attachments don't get printed as links $imagereturn .= "<br /><img src=\"{$ffurl}\" alt=\"\" />"; } else { echo "<a href=\"{$ffurl}\">{$image}</a> "; echo filter_text("<a href=\"{$ffurl}\">{$filename}</a><br />"); } } } } if ($return) { return $output; } return $imagereturn; }
/** * if return=html, then return a html string. * if return=text, then return a text-only string. * otherwise, print HTML for non-images, and return image HTML * if attachment is an image, $align set its aligment. * @param object $entry * @param object $cm * @param string $type html, txt, empty * @param string $align left or right * @return image string or nothing depending on $type param */ function glossary_print_attachments($entry, $cm, $type = NULL, $align = "left") { global $CFG, $DB; if (!($context = get_context_instance(CONTEXT_MODULE, $cm->id))) { return ''; } if ($entry->sourceglossaryid == $cm->instance) { if (!($maincm = get_coursemodule_from_instance('glossary', $entry->glossaryid))) { return ''; } $filecontext = get_context_instance(CONTEXT_MODULE, $maincm->id); } else { $filecontext = $context; } $strattachment = get_string('attachment', 'glossary'); $fs = get_file_storage(); $browser = get_file_browser(); $imagereturn = ''; $output = ''; if ($files = $fs->get_area_files($filecontext->id, 'glossary_attachment', $entry->id, "timemodified", false)) { foreach ($files as $file) { $filename = $file->get_filename(); $mimetype = $file->get_mimetype(); $icon = mimeinfo_from_type('icon', $mimetype); $iconimage = '<img src="' . $CFG->pixpath . '/f/' . $icon . '" class="icon" alt="' . $icon . '" />'; $path = $browser->encodepath($CFG->wwwroot . '/pluginfile.php', '/' . $context->id . '/glossary_attachment/' . $entry->id . '/' . $filename); if ($type == 'html') { $output .= "<a href=\"{$path}\">{$iconimage}</a> "; $output .= "<a href=\"{$path}\">" . s($filename) . "</a>"; $output .= "<br />"; } else { if ($type == 'text') { $output .= "{$strattachment} " . s($filename) . ":\n{$path}\n"; } else { if (in_array($mimetype, array('image/gif', 'image/jpeg', 'image/png'))) { // Image attachments don't get printed as links $imagereturn .= "<br /><img src=\"{$path}\" alt=\"\" />"; } else { $output .= "<a href=\"{$path}\">{$iconimage}</a> "; $output .= filter_text("<a href=\"{$path}\">" . s($filename) . "</a>"); $output .= '<br />'; } } } } } if ($type) { return $output; } else { echo $output; return $imagereturn; } }
/** * Obtains descriptions for file types (e.g. 'Microsoft Word document') from the * mimetypes.php language file. * * @param mixed $obj - instance of stored_file or file_info or array/stdClass with field * 'filename' and 'mimetype', or just a string with mimetype (though it is recommended to * have filename); In case of array/stdClass the field 'mimetype' is optional. * @param bool $capitalise If true, capitalises first character of result * @return string Text description */ function get_mimetype_description($obj, $capitalise = false) { $filename = $mimetype = ''; if (is_object($obj) && method_exists($obj, 'get_filename') && method_exists($obj, 'get_mimetype')) { // this is an instance of stored_file $mimetype = $obj->get_mimetype(); $filename = $obj->get_filename(); } else { if (is_object($obj) && method_exists($obj, 'get_visible_name') && method_exists($obj, 'get_mimetype')) { // this is an instance of file_info $mimetype = $obj->get_mimetype(); $filename = $obj->get_visible_name(); } else { if (is_array($obj) || is_object($obj)) { $obj = (array) $obj; if (!empty($obj['filename'])) { $filename = $obj['filename']; } if (!empty($obj['mimetype'])) { $mimetype = $obj['mimetype']; } } else { $mimetype = $obj; } } } $mimetypefromext = mimeinfo('type', $filename); if (empty($mimetype) || $mimetypefromext !== 'document/unknown') { // if file has a known extension, overwrite the specified mimetype $mimetype = $mimetypefromext; } $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); if (empty($extension)) { $mimetypestr = mimeinfo_from_type('string', $mimetype); $extension = str_replace('.', '', mimeinfo_from_type('extension', $mimetype)); } else { $mimetypestr = mimeinfo('string', $filename); } $chunks = explode('/', $mimetype, 2); $chunks[] = ''; $attr = array('mimetype' => $mimetype, 'ext' => $extension, 'mimetype1' => $chunks[0], 'mimetype2' => $chunks[1]); $a = array(); foreach ($attr as $key => $value) { $a[$key] = $value; $a[strtoupper($key)] = strtoupper($value); $a[ucfirst($key)] = ucfirst($value); } if (get_string_manager()->string_exists($mimetype, 'mimetypes')) { $result = get_string($mimetype, 'mimetypes', (object) $a); } else { if (get_string_manager()->string_exists($mimetypestr, 'mimetypes')) { $result = get_string($mimetypestr, 'mimetypes', (object) $a); } else { if (get_string_manager()->string_exists('default', 'mimetypes')) { $result = get_string('default', 'mimetypes', (object) $a); } else { $result = $mimetype; } } } if ($capitalise) { $result = ucfirst($result); } return $result; }
/** * Renders a list of files attached to the submission * * If format==html, then format a html string. If format==text, then format a text-only string. * Otherwise, returns html for non-images and html to display the image inline. * * @param int $submissionid submission identifier * @param string format the format of the returned string - html|text * @return string formatted text to be echoed */ protected function helper_submission_attachments($submissionid, $format = 'html') { global $CFG; require_once $CFG->libdir . '/filelib.php'; $fs = get_file_storage(); $ctx = $this->page->context; $files = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid); $outputimgs = ''; // images to be displayed inline $outputfiles = ''; // list of attachment files foreach ($files as $file) { if ($file->is_directory()) { continue; } $filepath = $file->get_filepath(); $filename = $file->get_filename(); $fileurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $ctx->id . '/mod_workshop/submission_attachment/' . $submissionid . $filepath . $filename, true); $type = $file->get_mimetype(); $type = mimeinfo_from_type('type', $type); $image = html_writer::empty_tag('img', array('src' => $this->output->pix_url(file_mimetype_icon($type)), 'alt' => $type, 'class' => 'icon')); $linkhtml = html_writer::link($fileurl, $image) . substr($filepath, 1) . html_writer::link($fileurl, $filename); $linktxt = "{$filename} [{$fileurl}]"; if ($format == 'html') { if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) { $preview = html_writer::empty_tag('img', array('src' => $fileurl, 'alt' => '', 'class' => 'preview')); $preview = html_writer::tag('a', $preview, array('href' => $fileurl)); $outputimgs .= $this->output->container($preview); } else { $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type)); } } else { if ($format == 'text') { $outputfiles .= $linktxt . PHP_EOL; } } } if ($format == 'html') { if ($outputimgs) { $outputimgs = $this->output->container($outputimgs, 'images'); } if ($outputfiles) { $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files')); } return $this->output->container($outputimgs . $outputfiles, 'attachments'); } else { return $outputfiles; } }
function print_responsefiles($userid, $return = false) { global $CFG, $USER; $mode = optional_param('mode', '', PARAM_ALPHA); $offset = optional_param('offset', 0, PARAM_INT); $output = ''; $candelete = $this->can_manage_responsefiles(); $strdelete = get_string('delete'); $fs = get_file_storage(); $browser = get_file_browser(); if ($files = $fs->get_area_files($this->context->id, 'assignment_response', $userid, "timemodified", false)) { foreach ($files as $file) { $filename = $file->get_filename(); $found = true; $mimetype = $file->get_mimetype(); $icon = mimeinfo_from_type('icon', $mimetype); $path = $browser->encodepath($CFG->wwwroot . '/pluginfile.php', '/' . $this->context->id . '/assignment_response/' . $userid . '/' . $filename); $output .= '<a href="' . $path . '" ><img src="' . $CFG->pixpath . '/f/' . $icon . '" alt="' . $icon . '" />' . $filename . '</a>'; if ($candelete) { $delurl = "{$CFG->wwwroot}/mod/assignment/delete.php?id={$this->cm->id}&file=" . rawurlencode($filename) . "&userid={$userid}&mode={$mode}&offset={$offset}&action=response"; $output .= '<a href="' . $delurl . '"> ' . '<img title="' . $strdelete . '" src="' . $CFG->pixpath . '/t/delete.gif" class="iconsmall" alt=""/></a> '; } $output .= ' '; } $output = '<div class="responsefiles">' . $output . '</div>'; } if ($return) { return $output; } echo $output; }
/** * Returns the relative icon path for a given mime type * * This function should be used in conjunction with $OUTPUT->pix_url to produce * a return the full path to an icon. * * <code> * $mimetype = 'image/jpg'; * $icon = $OUTPUT->pix_url(file_mimetype_icon($mimetype)); * echo '<img src="'.$icon.'" alt="'.$mimetype.'" />'; * </code> * * @todo When an $OUTPUT->icon method is available this function should be altered * to conform with that. * * @param string $mimetype The mimetype to fetch an icon for * @param int $size The size of the icon. Not yet implemented * @return string The relative path to the icon */ function file_mimetype_icon($mimetype, $size = NULL) { global $CFG; $icon = mimeinfo_from_type('icon', $mimetype); if ($size) { if (file_exists("{$CFG->dirroot}/pix/f/{$icon}-{$size}.png") or file_exists("{$CFG->dirroot}/pix/f/{$icon}-{$size}.gif")) { $icon = "{$icon}-{$size}"; } } return 'f/' . $icon; }
/** * Returns attachments as formated text/html optionally with separate images * @param object $post * @param object $cm * @param string type - html/text/separateimages * @return mixed string or array of (html text withouth images and image HTML) */ function forum_print_attachments($post, $cm, $type) { global $CFG, $DB, $USER; if (empty($post->attachment)) { return $type !== 'separateimages' ? '' : array('', ''); } if (!in_array($type, array('separateimages', 'html', 'text'))) { return $type !== 'separateimages' ? '' : array('', ''); } if (!($context = get_context_instance(CONTEXT_MODULE, $cm->id))) { return $type !== 'separateimages' ? '' : array('', ''); } $strattachment = get_string('attachment', 'forum'); $fs = get_file_storage(); $browser = get_file_browser(); $imagereturn = ''; $output = ''; $canexport = has_capability('mod/forum:exportpost', $context) || $post->userid == $USER->id && has_capability('mod/forum:exportownpost', $context); if ($files = $fs->get_area_files($context->id, 'forum_attachment', $post->id, "timemodified", false)) { $button = new portfolio_add_button(); foreach ($files as $file) { $filename = $file->get_filename(); $mimetype = $file->get_mimetype(); $icon = mimeinfo_from_type('icon', $mimetype); $iconimage = '<img src="' . $CFG->pixpath . '/f/' . $icon . '" class="icon" alt="' . $icon . '" />'; $path = $browser->encodepath($CFG->wwwroot . '/pluginfile.php', '/' . $context->id . '/forum_attachment/' . $post->id . '/' . $filename); if ($type == 'html') { $output .= "<a href=\"{$path}\">{$iconimage}</a> "; $output .= "<a href=\"{$path}\">" . s($filename) . "</a>"; if ($canexport) { $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id, 'attachment' => $file->get_id())); $button->set_formats(portfolio_format_from_file($file)); $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK); } $output .= "<br />"; } else { if ($type == 'text') { $output .= "{$strattachment} " . s($filename) . ":\n{$path}\n"; } else { //'returnimages' if (in_array($mimetype, array('image/gif', 'image/jpeg', 'image/png'))) { // Image attachments don't get printed as links $imagereturn .= "<br /><img src=\"{$path}\" alt=\"\" />"; if ($canexport) { $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id, 'attachment' => $file->get_id())); $button->set_formats(portfolio_format_from_file($file)); $imagereturn .= $button->to_html(PORTFOLIO_ADD_ICON_LINK); } } else { $output .= "<a href=\"{$path}\">{$iconimage}</a> "; $output .= filter_text("<a href=\"{$path}\">" . s($filename) . "</a>"); if ($canexport) { $button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id, 'attachment' => $file->get_id())); $button->set_formats(portfolio_format_from_file($file)); $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK); } $output .= '<br />'; } } } } } if ($type !== 'separateimages') { return $output; } else { return array($output, $imagereturn); } }
/** * if return=html, then return a html string. * if return=text, then return a text-only string. * otherwise, print HTML for non-images, and return image HTML * * @param bool $return Whether to return or print the generated code * @return void */ public function print_attachments($return = false) { global $CFG, $OUTPUT; require_once $CFG->libdir . '/filelib.php'; $fs = get_file_storage(); $syscontext = get_context_instance(CONTEXT_SYSTEM); $files = $fs->get_area_files($syscontext->id, 'blog', 'attachment', $this->id); $imagereturn = ""; $output = ""; $strattachment = get_string("attachment", "forum"); foreach ($files as $file) { if ($file->is_directory()) { continue; } $filename = $file->get_filename(); $ffurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . SYSCONTEXTID . '/blog/attachment/' . $this->id . '/' . $filename); $mimetype = $file->get_mimetype(); $icon = mimeinfo_from_type("icon", $mimetype); $type = mimeinfo_from_type("type", $mimetype); $image = $OUTPUT->pix_icon("f/{$icon}", $filename, 'moodle', array('class' => 'icon')); if ($return == "html") { $output .= html_writer::link($ffurl, $image); $output .= html_writer::link($ffurl, $filename); } else { if ($return == "text") { $output .= "{$strattachment} {$filename}:\n{$ffurl}\n"; } else { if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) { // Image attachments don't get printed as links $imagereturn .= "<br />" . $OUTPUT->pix_icon($ffurl, $filename); } else { $imagereturn .= html_writer::link($ffurl, $image); $imagereturn .= format_text(html_writer::link($ffurl, $filename), FORMAT_HTML, array('context' => $syscontext)); } } } } if ($return) { return $output; } return $imagereturn; }
/** * Produces a list of links to the files uploaded by a user * * @param $userid int optional id of the user. If 0 then $USER->id is used. * @param $return boolean optional defaults to false. If true the list is returned rather than printed * @return string optional */ function print_user_files($userid = 0, $return = false) { global $CFG, $USER; if (!$userid) { if (!isloggedin()) { return ''; } $userid = $USER->id; } $output = ''; $fs = get_file_storage(); $browser = get_file_browser(); $found = false; if ($files = $fs->get_area_files($this->context->id, 'assignment_submission', $userid, "timemodified", false)) { $button = new portfolio_add_button(); foreach ($files as $file) { $filename = $file->get_filename(); $found = true; $mimetype = $file->get_mimetype(); $icon = mimeinfo_from_type('icon', $mimetype); $path = $browser->encodepath($CFG->wwwroot . '/pluginfile.php', '/' . $this->context->id . '/assignment_submission/' . $userid . '/' . $filename); $output .= '<a href="' . $path . '" ><img src="' . $CFG->pixpath . '/f/' . $icon . '" class="icon" alt="' . $icon . '" />' . s($filename) . '</a>'; if ($this->portfolio_exportable() && has_capability('mod/assignment:exportownsubmission', $this->context)) { $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'fileid' => $file->get_id())); $button->set_formats(portfolio_format_from_file($file)); $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK); } $output .= '<br />'; } if (count($files) > 1 && $this->portfolio_exportable() && has_capability('mod/assignment:exportownsubmission', $this->context)) { $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id)); $button->set_formats(PORTFOLIO_PORMAT_FILE); $output .= '<br />' . $button->to_html(); } } $output = '<div class="files">' . $output . '</div>'; if ($return) { return $output; } echo $output; }