mirror of
https://github.com/moodle/moodle.git
synced 2025-08-07 01:46:45 +02:00
MDL-16587 bit of a large refactor of the portfolio formats. still more to come.
This commit is contained in:
parent
a813ddc975
commit
6be1dcae99
16 changed files with 295 additions and 141 deletions
|
@ -1709,7 +1709,7 @@ class assignment_base {
|
|||
$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, 'file' => $file->get_id()));
|
||||
$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);
|
||||
}
|
||||
|
@ -3138,9 +3138,19 @@ function assignment_get_extra_capabilities() {
|
|||
require_once($CFG->libdir . '/portfoliolib.php');
|
||||
class assignment_portfolio_caller extends portfolio_module_caller_base {
|
||||
|
||||
/**
|
||||
* the assignment subclass
|
||||
*/
|
||||
private $assignment;
|
||||
|
||||
/**
|
||||
* the file to include when waking up to load the assignment subclass def
|
||||
*/
|
||||
private $assignmentfile;
|
||||
private $files;
|
||||
|
||||
/**
|
||||
* callback arg for a single file export
|
||||
*/
|
||||
protected $fileid;
|
||||
|
||||
public static function expected_callbackargs() {
|
||||
|
@ -3171,17 +3181,7 @@ class assignment_portfolio_caller extends portfolio_module_caller_base {
|
|||
throw new portfolio_caller_exception('notexportable', 'portfolio', $this->get_return_url());
|
||||
}
|
||||
|
||||
$fs = get_file_storage();
|
||||
if ($this->fileid) {
|
||||
$f = $fs->get_file_by_id($this->fileid);
|
||||
$this->files = array($f);
|
||||
} else {
|
||||
$this->files = $fs->get_area_files($this->assignment->context->id, 'assignment_submission', $this->user->id, '', false);
|
||||
}
|
||||
if (is_array($this->files) && count($this->files) == 1) {
|
||||
$f = array_values($this->files);
|
||||
$this->supportedformats = array(portfolio_format_from_file($f[0]));
|
||||
}
|
||||
$this->set_file_and_format_data($this->fileid, $this->assignment->context->id, 'assignment_submission', $this->user->id);
|
||||
if (empty($this->supportedformats) && is_callable(array($this->assignment, 'portfolio_supported_formats'))) {
|
||||
$this->supportedformats = $this->assignment->portfolio_supported_formats();
|
||||
}
|
||||
|
@ -3192,9 +3192,7 @@ class assignment_portfolio_caller extends portfolio_module_caller_base {
|
|||
if (is_callable(array($this->assignment, 'portfolio_prepare_package'))) {
|
||||
return $this->assignment->portfolio_prepare_package($this->exporter, $this->user->id);
|
||||
}
|
||||
foreach ($this->files as $file) {
|
||||
$this->exporter->copy_existing_file($file);
|
||||
}
|
||||
return $this->prepare_package_file();
|
||||
}
|
||||
|
||||
public function get_sha1() {
|
||||
|
@ -3202,23 +3200,14 @@ class assignment_portfolio_caller extends portfolio_module_caller_base {
|
|||
if (is_callable(array($this->assignment, 'portfolio_get_sha1'))) {
|
||||
return $this->assignment->portfolio_get_sha1($this->user->id);
|
||||
}
|
||||
$sha1s = array();
|
||||
foreach ($this->files as $file) {
|
||||
$sha1s[] = $file->get_contenthash();
|
||||
}
|
||||
asort($sha1s);
|
||||
return sha1(implode('', $sha1s));
|
||||
|
||||
return $this->get_sha1_file();
|
||||
}
|
||||
|
||||
public function expected_time() {
|
||||
if (is_callable(array($this->assignment, 'portfolio_get_expected_time'))) {
|
||||
return $this->assignment->portfolio_get_expected_time();
|
||||
}
|
||||
if (is_array($this->files)) {
|
||||
return portfolio_expected_time_file($this->files);
|
||||
}
|
||||
return PORTFOLIO_TIME_LOW;
|
||||
return $this->expected_time_file();
|
||||
}
|
||||
|
||||
public function check_permissions() {
|
||||
|
|
|
@ -275,11 +275,11 @@ class assignment_online extends assignment_base {
|
|||
|
||||
function portfolio_prepare_package($exporter, $userid=0) {
|
||||
$submission = $this->get_submission($userid);
|
||||
$exporter->write_new_file(format_text($submission->data1, $submission->data2), 'assignment.html');
|
||||
$exporter->write_new_file(format_text($submission->data1, $submission->data2), 'assignment.html', false);
|
||||
}
|
||||
|
||||
function portfolio_supported_formats() {
|
||||
return array(PORTFOLIO_FORMAT_HTML);
|
||||
return array(PORTFOLIO_FORMAT_PLAINHTML);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -358,7 +358,7 @@ class assignment_upload extends assignment_base {
|
|||
}
|
||||
|
||||
if (has_capability('mod/assignment:exportownsubmission', $this->context)) {
|
||||
$button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'file' => $file->get_id()), '/mod/assignment/lib.php');
|
||||
$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);
|
||||
}
|
||||
|
|
|
@ -880,7 +880,7 @@ class chat_portfolio_caller extends portfolio_module_caller_base {
|
|||
}
|
||||
|
||||
public static function supported_formats() {
|
||||
return array(PORTFOLIO_FORMAT_HTML);
|
||||
return array(PORTFOLIO_FORMAT_PLAINHTML);
|
||||
}
|
||||
|
||||
public function expected_time() {
|
||||
|
@ -922,7 +922,7 @@ class chat_portfolio_caller extends portfolio_module_caller_base {
|
|||
}
|
||||
$content = preg_replace('/\<img[^>]*\>/', '', $content);
|
||||
|
||||
$this->exporter->write_new_file($content, clean_filename($this->cm->name . '-session.html'));
|
||||
$this->exporter->write_new_file($content, clean_filename($this->cm->name . '-session.html'), false);
|
||||
}
|
||||
|
||||
public static function display_name() {
|
||||
|
|
|
@ -1028,15 +1028,7 @@ function data_print_template($template, $records, $data, $search='',$page=0, $re
|
|||
require_once($CFG->libdir . '/portfoliolib.php');
|
||||
$button = new portfolio_add_button();
|
||||
$button->set_callback_options('data_portfolio_caller', array('id' => $cm->id, 'recordid' => $record->id));
|
||||
$formats = array(PORTFOLIO_FORMAT_HTML);
|
||||
if (count($fields) == 1) {
|
||||
$ftmp = array_values($fields);
|
||||
$singlefield = $ftmp[0];
|
||||
if (is_callable(array($singlefield, 'get_file'))) { // TODO this used to be if ($singlefield instanceof data_field_file) { - see MDL-16493
|
||||
$f = $singlefield->get_file($record->id);
|
||||
$formats= array(portfolio_format_from_file($f));
|
||||
}
|
||||
}
|
||||
list($formats, $files) = data_portfolio_caller::formats($fields, $record);
|
||||
$button->set_formats($formats);
|
||||
$replacement[] = $button->to_html(PORTFOLIO_ADD_ICON_LINK);
|
||||
} else {
|
||||
|
@ -2600,19 +2592,15 @@ class data_portfolio_caller extends portfolio_module_caller_base {
|
|||
$this->singlerecord = $DB->get_record('data_records', array('id' => $this->recordid));
|
||||
$this->singlerecord->content = $DB->get_records('data_content', array('recordid' => $this->singlerecord->id));
|
||||
$this->exporttype = 'single';
|
||||
$formats = array(PORTFOLIO_FORMAT_HTML);
|
||||
if (count($this->fields) == 1) {
|
||||
$ftmp = array_values($this->fields);
|
||||
$singlefield = $ftmp[0];
|
||||
if (is_callable(array($singlefield, 'get_file'))) { // TODO This used to be if ($singlefield instanceof data_field_file) { see MDL-16493
|
||||
$f = $singlefield->get_file($this->singlerecord->id);
|
||||
$formats= array(portfolio_format_from_file($f));
|
||||
$this->exporttype = 'singlefile';
|
||||
$this->singlefield = $singlefield;
|
||||
$this->singlefile = $this->singlefield->get_file($this->singlerecord->id);
|
||||
}
|
||||
}
|
||||
|
||||
list($formats, $files) = self::formats($this->fields, $this->singlerecord);
|
||||
$this->supportedformats = $formats;
|
||||
if (count($files) == 1 && count($this->fields) == 1) {
|
||||
$this->singlefile = $files[0];
|
||||
$this->exporttype = 'singlefile';
|
||||
} else if (count($files) > 0) {
|
||||
$this->multifiles = $files;
|
||||
}
|
||||
} else {
|
||||
// all records as csv or whatever
|
||||
$this->exportdata = data_get_exportdata($this->cm->instance, $this->fields, $this->selectedfields);
|
||||
|
@ -2686,7 +2674,11 @@ class data_portfolio_caller extends portfolio_module_caller_base {
|
|||
throw new portfolio_caller_exception('notimplemented', 'portfolio', '', $this->exporttype);
|
||||
break;
|
||||
}
|
||||
return $this->exporter->write_new_file($content, $filename);
|
||||
return $this->exporter->write_new_file(
|
||||
$content,
|
||||
$filename,
|
||||
($this->exporter->get('format') instanceof PORTFOLIO_FORMAT_RICH) // if we have associate files, this is a 'manifest'
|
||||
);
|
||||
}
|
||||
|
||||
public function check_permissions() {
|
||||
|
@ -2716,13 +2708,18 @@ class data_portfolio_caller extends portfolio_module_caller_base {
|
|||
$context = get_context_instance(CONTEXT_MODULE, $this->cm->id);
|
||||
|
||||
// Then we generate strings to replace for normal tags
|
||||
$format = $this->get('exporter')->get('format');
|
||||
foreach ($this->fields as $field) {
|
||||
$patterns[]='[['.$field->field->name.']]';
|
||||
if (is_callable(array($field, 'get_file'))) { // TODO this used to be if ($field instanceof data_field_file) { - see MDL-16493
|
||||
if (is_callable(array($field, 'get_file'))) {
|
||||
// TODO this used to be:
|
||||
// if ($field instanceof data_field_file) {
|
||||
// - see MDL-16493
|
||||
if (!$file = $field->get_file($this->singlerecord->id)) {
|
||||
$replacement[] = '';
|
||||
continue; // probably left empty
|
||||
}
|
||||
$replacement[] = $file->get_filename();
|
||||
$replacement[] = $format->file_output($file);
|
||||
$this->get('exporter')->copy_existing_file($file);
|
||||
} else {
|
||||
$replacement[] = $field->display_browse_field($this->singlerecord->id, 'singletemplate');
|
||||
|
@ -2754,5 +2751,21 @@ class data_portfolio_caller extends portfolio_module_caller_base {
|
|||
// actual replacement of the tags
|
||||
return str_ireplace($patterns, $replacement, $this->data->singletemplate);
|
||||
}
|
||||
|
||||
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_RICHHTML);
|
||||
}
|
||||
return array($formats, $includedfiles);
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
|
@ -3178,7 +3178,9 @@ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=fa
|
|||
$button = new portfolio_add_button();
|
||||
$button->set_callback_options('forum_portfolio_caller', array('postid' => $post->id));
|
||||
if (empty($attachments)) {
|
||||
$button->set_formats(PORTFOLIO_FORMAT_HTML);
|
||||
$button->set_formats(PORTFOLIO_FORMAT_PLAINHTML);
|
||||
} else {
|
||||
$button->set_formats(PORTFOLIO_FORMAT_RICHHTML);
|
||||
}
|
||||
$commands[] = $button->to_html(PORTFOLIO_ADD_TEXT_LINK);
|
||||
}
|
||||
|
@ -7229,9 +7231,8 @@ class forum_portfolio_caller extends portfolio_module_caller_base {
|
|||
private $post;
|
||||
private $forum;
|
||||
private $discussion;
|
||||
private $postfiles;
|
||||
private $allfiles;
|
||||
private $posts;
|
||||
private $keyedfiles;
|
||||
|
||||
public static function expected_callbackargs() {
|
||||
return array(
|
||||
|
@ -7278,22 +7279,32 @@ class forum_portfolio_caller extends portfolio_module_caller_base {
|
|||
throw new portfolio_caller_exception('invalidcoursemodule');
|
||||
}
|
||||
|
||||
$fs = get_file_storage();
|
||||
if ($this->attachment) {
|
||||
if (!$f = $fs->get_file_by_id($this->attachment)) {
|
||||
throw new portfolio_caller_exception('noattachments', 'forum');
|
||||
$modcontext = get_context_instance(CONTEXT_MODULE, $this->cm->id);
|
||||
if ($this->post) {
|
||||
$this->set_file_and_format_data($this->attachment, $modcontext->id, 'forum_attachment', $this->post->id);
|
||||
if (!empty($this->multifiles)) {
|
||||
$this->keyedfiles[$this->post->id] = $this->multifiles;
|
||||
} else if (!empty($this->singlefile)) {
|
||||
$this->keyedfiles[$this->post->id] = $this->singlefile;
|
||||
}
|
||||
$this->postfiles = array($f);
|
||||
$this->supportedformats = array(portfolio_format_from_file($f));
|
||||
} elseif ($this->post) {
|
||||
$this->postfiles = $fs->get_area_files(get_context_instance(CONTEXT_MODULE, $this->cm->id)->id, 'forum_attachment', $this->post->id, "timemodified", false);
|
||||
$this->supportedformats = array(PORTFOLIO_FORMAT_HTML);
|
||||
} else {
|
||||
} else { // whole thread
|
||||
$fs = get_file_storage();
|
||||
$this->posts = forum_get_all_discussion_posts($this->discussion->id, 'p.created ASC');
|
||||
$this->multifiles = array();
|
||||
foreach ($this->posts as $post) {
|
||||
$this->allfiles[$post->id] = $fs->get_area_files(get_context_instance(CONTEXT_MODULE, $this->cm->id)->id, 'forum_attachment', $post->id, "timemodified", false);
|
||||
if (!$this->keyedfiles[$post->id] = $fs->get_area_files($modcontext->id, 'forum_attachment', $post->id, "timemodified", false)) {
|
||||
continue;
|
||||
}
|
||||
$this->multifiles = array_merge($this->multifiles, array_values($this->keyedfiles[$post->id]));
|
||||
}
|
||||
}
|
||||
if ($this->attachment) {
|
||||
// do nothing
|
||||
} else if (!empty($this->multifiles) || !empty($this->singlefile)) {
|
||||
$this->supportedformats = array(PORTFOLIO_FORMAT_RICHHTML);
|
||||
} else {
|
||||
$this->supportedformats = array(PORTFOLIO_FORMAT_PLAINHTML);
|
||||
}
|
||||
}
|
||||
|
||||
function get_return_url() {
|
||||
|
@ -7318,20 +7329,21 @@ class forum_portfolio_caller extends portfolio_module_caller_base {
|
|||
// either a whole discussion
|
||||
// a single post, with or without attachment
|
||||
// or just an attachment with no post
|
||||
$manifest = ($this->exporter->get('format') instanceof PORTFOLIO_FORMAT_RICH);
|
||||
if (!$this->post) { // whole discussion
|
||||
$content = '';
|
||||
foreach ($this->posts as $post) {
|
||||
$content .= '<br /><br />' . $this->prepare_post($post);
|
||||
$this->copy_files($this->allfiles[$post->id]);
|
||||
}
|
||||
return $this->get('exporter')->write_new_file($content, 'discussion.html');
|
||||
$this->copy_files($this->multifiles);
|
||||
return $this->get('exporter')->write_new_file($content, 'discussion.html', $manifest);
|
||||
}
|
||||
$this->copy_files($this->postfiles, $this->attachment);
|
||||
if ($this->attachment) {
|
||||
return; // all we need to do
|
||||
return $this->copy_files(array($this->singlefile), $this->attachment); // all we need to do
|
||||
}
|
||||
$this->copy_files($this->multifiles, $this->attachment);
|
||||
$post = $this->prepare_post($this->post);
|
||||
$this->get('exporter')->write_new_file($post, 'post.html');
|
||||
$this->get('exporter')->write_new_file($post, 'post.html', $manifest);
|
||||
}
|
||||
|
||||
private function copy_files($files, $justone=false) {
|
||||
|
@ -7392,17 +7404,12 @@ class forum_portfolio_caller extends portfolio_module_caller_base {
|
|||
|
||||
$output .= $formattedtext;
|
||||
|
||||
if (!empty($this->postfiles)) {
|
||||
$attachments = $this->postfiles;
|
||||
} else if (!empty($this->allfiles) && array_key_exists($post->userid, $this->allfiles)) {
|
||||
$attachments = $this->allfiles[$post->userid];
|
||||
}
|
||||
if (!empty($attachments)) {
|
||||
$post->course = $this->get('course')->id;
|
||||
if (array_key_exists($post->id, $this->keyedfiles) && is_array($this->keyedfiles[$post->id])) {
|
||||
$output .= '<div class="attachments">';
|
||||
$output .= '<br /><b>' . get_string('attachments', 'forum') . '</b>:<br /><br />';
|
||||
foreach ($attachments as $file) {
|
||||
$output .= $file->get_filename() . '<br />';
|
||||
$format = $this->get('exporter')->get('format');
|
||||
foreach ($this->keyedfiles[$post->id] as $file) {
|
||||
$output .= $format->file_output($file) . '<br/ >';
|
||||
}
|
||||
$output .= "</div>";
|
||||
}
|
||||
|
@ -7413,50 +7420,31 @@ class forum_portfolio_caller extends portfolio_module_caller_base {
|
|||
}
|
||||
|
||||
function get_sha1() {
|
||||
$filesha = '';
|
||||
try {
|
||||
$filesha = $this->get_sha1_file();
|
||||
} catch (portfolio_caller_exception $e) { } // no files
|
||||
|
||||
if ($this->post) {
|
||||
$attachsha1 = $this->files_sha1($this->postfiles, $this->attachment);
|
||||
return sha1($attachsha1 . ',' . $this->post->subject . ',' . $this->post->message);
|
||||
return sha1($filesha . ',' . $this->post->subject . ',' . $this->post->message);
|
||||
} else {
|
||||
$sha1s = array();
|
||||
$sha1s = array($filesha);
|
||||
foreach ($this->posts as $post) {
|
||||
$sha1s[] = $this->files_sha1($this->allfiles[$post->id]);
|
||||
$sha1s[] = sha1($post->subject . ',' . $post->message);
|
||||
}
|
||||
return sha1(implode(',', $sha1s));
|
||||
}
|
||||
}
|
||||
|
||||
function files_sha1($files, $justone=false) {
|
||||
if (empty($files)) {
|
||||
return;
|
||||
}
|
||||
$sha1s = array();
|
||||
foreach ($files as $file) {
|
||||
if ($justone && $file->get_id() == $justone) {
|
||||
return $file->get_contenthash(); // all we have to do
|
||||
}
|
||||
$sha1s[] = $file->get_contenthash();
|
||||
}
|
||||
asort($sha1s);
|
||||
return sha1(implode('', $sha1s));
|
||||
}
|
||||
|
||||
function expected_time() {
|
||||
// default...
|
||||
$time = PORTFOLIO_TIME_LOW; // generally just means one post with no attachments
|
||||
if ($this->postfiles) {
|
||||
$time = portfolio_expected_time_file($this->postfiles);
|
||||
} else if ($this->allfiles) {
|
||||
// we have something two dimensional...
|
||||
$files = array();
|
||||
foreach ($this->allfiles as $post => $postfiles) {
|
||||
$files = array_merge($files, $postfiles);
|
||||
$filetime = $this->expected_time_file();
|
||||
if ($this->posts) {
|
||||
$posttime = portfolio_expected_time_db(count($this->posts));
|
||||
if ($filetime < $posttime) {
|
||||
return $posttime;
|
||||
}
|
||||
$time = portfolio_expected_time_file($files);
|
||||
} else if ($this->posts) {
|
||||
$time = portfolio_expected_time_db(count($this->posts));
|
||||
}
|
||||
return $time;
|
||||
return $filetime;
|
||||
}
|
||||
|
||||
function check_permissions() {
|
||||
|
|
|
@ -2446,7 +2446,7 @@ class glossary_csv_portfolio_caller extends portfolio_module_caller_base {
|
|||
}
|
||||
}
|
||||
$csv = glossary_generate_export_csv($entries, $aliases, $categories);
|
||||
return $this->exporter->write_new_file($csv, clean_filename($this->cm->name) . '.csv');
|
||||
return $this->exporter->write_new_file($csv, clean_filename($this->cm->name) . '.csv', false);
|
||||
}
|
||||
|
||||
public function check_permissions() {
|
||||
|
@ -2486,7 +2486,7 @@ class glossary_entry_portfolio_caller extends portfolio_module_caller_base {
|
|||
// in case we don't have USER this will make the entry be printed
|
||||
$this->entry->approved = true;
|
||||
}
|
||||
$this->supportedformats = array(PORTFOLIO_FORMAT_HTML);
|
||||
$this->supportedformats = array(PORTFOLIO_FORMAT_PLAINHTML);
|
||||
}
|
||||
|
||||
public function expected_time() {
|
||||
|
@ -2509,7 +2509,7 @@ class glossary_entry_portfolio_caller extends portfolio_module_caller_base {
|
|||
$entry = clone $this->entry;
|
||||
glossary_print_entry($this->get('course'), $this->cm, $this->glossary, $entry, null, null, false);
|
||||
$content = ob_get_clean();
|
||||
return $this->exporter->write_new_file($content, clean_filename($this->entry->concept) . '.html');
|
||||
return $this->exporter->write_new_file($content, clean_filename($this->entry->concept) . '.html', false);
|
||||
}
|
||||
|
||||
public function get_sha1() {
|
||||
|
|
|
@ -259,7 +259,7 @@ class resource_base {
|
|||
$formatoptions = (object)array('noclean' => true);
|
||||
$format = (($text) ? FORMAT_MOODLE : FORMAT_HTML);
|
||||
$content = format_text($this->resource->alltext, $format, $formatoptions, $this->course->id);
|
||||
return $exporter->write_new_file($content, $filename);
|
||||
return $exporter->write_new_file($content, $filename, false);
|
||||
}
|
||||
|
||||
function portfolio_get_sha1_online($text=false) {
|
||||
|
@ -739,7 +739,7 @@ class resource_portfolio_caller extends portfolio_module_caller_base {
|
|||
$format = PORTFOLIO_FORMAT_FILE;
|
||||
switch ($type) {
|
||||
case 'html':
|
||||
$format = PORTFOLIO_FORMAT_HTML;
|
||||
$format = PORTFOLIO_FORMAT_PLAINHTML;
|
||||
case 'text':
|
||||
$format = PORTFOLIO_FORMAT_TEXT;
|
||||
case 'file':
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue