/** * 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, $OUTPUT; 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(); $path = file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $this->context->id . '/assignment_submission/' . $userid . '/' . $filename); $output .= '<a href="' . $path . '" ><img src="' . $OUTPUT->old_icon_url(file_mimetype_icon($mimetype)) . '" class="icon" alt="' . $mimetype . '" />' . 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; }
public static function formats($fields, $record) { $formats = array(PORTFOLIO_FORMAT_PLAINHTML); $includedfiles = array(); foreach ($fields as $singlefield) { if (is_callable(array($singlefield, 'get_file'))) { $includedfiles[] = $singlefield->get_file($record->id); } } if (count($includedfiles) == 1 && count($fields) == 1) { $formats = array(portfolio_format_from_file($includedfiles[0])); } else { if (count($includedfiles) > 0) { $formats = array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML); } } return array($formats, $includedfiles); }
/** * 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; $mode = optional_param('mode', '', PARAM_ALPHA); $offset = optional_param('offset', 0, PARAM_INT); if (!$userid) { if (!isloggedin()) { return ''; } $userid = $USER->id; } $output = ''; $submission = $this->get_submission($userid); $candelete = $this->can_delete_files($submission); $strdelete = get_string('delete'); if ($this->drafts_tracked() and $this->isopen() and !$this->is_finalized($submission) and !empty($mode)) { // only during grading $output .= '<strong>' . get_string('draft', 'assignment') . ':</strong><br />'; } if ($this->notes_allowed() and !empty($submission->data1) and !empty($mode)) { // only during grading $npurl = $CFG->wwwroot . "/mod/assignment/type/upload/notes.php?id={$this->cm->id}&userid={$userid}&offset={$offset}&mode=single"; $output .= '<a href="' . $npurl . '">' . get_string('notes', 'assignment') . '</a><br />'; } $fs = get_file_storage(); $browser = get_file_browser(); 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(); $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 ($candelete) { $delurl = "{$CFG->wwwroot}/mod/assignment/delete.php?id={$this->cm->id}&file=" . rawurlencode($filename) . "&userid={$submission->userid}&mode={$mode}&offset={$offset}"; $output .= '<a href="' . $delurl . '"> ' . '<img title="' . $strdelete . '" src="' . $CFG->pixpath . '/t/delete.gif" class="iconsmall" alt="" /></a> '; } if (has_capability('mod/assignment:exportownsubmission', $this->context)) { $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'fileid' => $file->get_id()), '/mod/assignment/lib.php'); $button->set_formats(portfolio_format_from_file($file)); $output .= $button->to_html(PORTFOLIO_ADD_ICON_LINK); } $output .= '<br />'; } if (count($files) > 1 && has_capability('mod/assignment:exportownsubmission', $this->context)) { $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id), '/mod/assignment/lib.php'); $button->set_formats(PORTFOLIO_FORMAT_FILE); $output .= $button->to_html(); } } if ($this->drafts_tracked() and $this->isopen() and has_capability('mod/assignment:grade', $this->context) and $mode != '') { // we do not want it on view.php page if ($this->can_unfinalize($submission)) { $options = array('id' => $this->cm->id, 'userid' => $userid, 'action' => 'unfinalize', 'mode' => $mode, 'offset' => $offset); $output .= print_single_button('upload.php', $options, get_string('unfinalize', 'assignment'), 'post', '_self', true); } else { if ($this->can_finalize($submission)) { $options = array('id' => $this->cm->id, 'userid' => $userid, 'action' => 'finalizeclose', 'mode' => $mode, 'offset' => $offset); $output .= print_single_button('upload.php', $options, get_string('finalize', 'assignment'), 'post', '_self', true); } } } $output = '<div class="files">' . $output . '</div>'; if ($return) { return $output; } echo $output; }
/** * 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); } }
public function set_file_and_format_data($ids = null) { $args = func_get_args(); array_shift($args); // shift off $ids if (empty($ids) && count($args) == 0) { return; } $files = array(); $fs = get_file_storage(); if (!empty($ids)) { if (is_numeric($ids) || $ids instanceof stored_file) { $ids = array($ids); } foreach ($ids as $id) { if ($id instanceof stored_file) { $files[] = $id; } else { $files[] = $fs->get_file_by_id($id); } } } else { if (count($args) != 0) { if (count($args) != 3) { throw new portfolio_caller_exception('invalidfileareaargs', 'portfolio'); } $files = array_values(call_user_func_array(array($fs, 'get_area_files'), $args)); } } switch (count($files)) { case 0: return; case 1: $this->singlefile = $files[0]; $this->supportedformats = array(portfolio_format_from_file($this->singlefile)); return; default: $this->multifiles = $files; } }