mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
portfolio: MDL-21030 - leap2a portfolio format support.
This commit includes: - leap2a portfolio format, and xml writer - proof of concept implementation in forum and assignment modules - a lot of refactoring of the portfolio formats in general: - addition of "abstract" formats - this is necessary for plugins to be able to support groups of formats - addition of the idea of portfolio formats conflicting with eachother - eg richhtml & plainhtml it touches modules other than assignment and forum, because the format api changed and now each place in moodle that exports portfolio content has to deal with the formats it supports slightly differently. At the moment the Mahara portfolio still doesn't support this format, because I haven't done the Mahara side yet. The "file download" plugin supports it though. Still todo: - Add support for the other places in Moodle (glossary, data, etc) - Write tests, once the rest of the portfolio tests have been updated to use the new DB mocking stuff - Fix a bunch of TODOs
This commit is contained in:
parent
9cbced1d2e
commit
59dd457e4b
26 changed files with 994 additions and 111 deletions
|
@ -3508,7 +3508,7 @@ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=fa
|
|||
if (empty($attachments)) {
|
||||
$button->set_formats(PORTFOLIO_FORMAT_PLAINHTML);
|
||||
} else {
|
||||
$button->set_formats(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML);
|
||||
$button->set_formats(PORTFOLIO_FORMAT_RICHHTML);
|
||||
}
|
||||
|
||||
$porfoliohtml = $button->to_html(PORTFOLIO_ADD_TEXT_LINK);
|
||||
|
@ -7978,7 +7978,8 @@ class forum_portfolio_caller extends portfolio_module_caller_base {
|
|||
private $forum;
|
||||
private $discussion;
|
||||
private $posts;
|
||||
private $keyedfiles;
|
||||
private $keyedfiles; // just using multifiles isn't enough if we're exporting a full thread
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
|
@ -8033,11 +8034,11 @@ class forum_portfolio_caller extends portfolio_module_caller_base {
|
|||
|
||||
$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);
|
||||
$this->set_file_and_format_data($this->attachment, $modcontext->id, 'forum_attachment', $this->post->id, 'timemodified', false);
|
||||
if (!empty($this->multifiles)) {
|
||||
$this->keyedfiles[$this->post->id] = $this->multifiles;
|
||||
} else if (!empty($this->singlefile)) {
|
||||
$this->keyedfiles[$this->post->id] = $this->singlefile;
|
||||
$this->keyedfiles[$this->post->id] = array($this->singlefile);
|
||||
}
|
||||
} else { // whole thread
|
||||
$fs = get_file_storage();
|
||||
|
@ -8050,14 +8051,17 @@ class forum_portfolio_caller extends portfolio_module_caller_base {
|
|||
$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_FILE, PORTFOLIO_FORMAT_RICHHTML);
|
||||
if (empty($this->multifiles) && !empty($this->singlefile)) {
|
||||
$this->multifiles = array($this->singlefile); // copy_files workaround
|
||||
}
|
||||
// depending on whether there are files or not, we might have to change richhtml/plainhtml
|
||||
if (!empty($this->multifiles)) {
|
||||
$this->add_format(PORTFOLIO_FORMAT_RICHHTML);
|
||||
} else {
|
||||
$this->supportedformats = array(PORTFOLIO_FORMAT_PLAINHTML);
|
||||
$this->add_format(PORTFOLIO_FORMAT_PLAINHTML);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @global object
|
||||
* @return string
|
||||
|
@ -8082,35 +8086,103 @@ class forum_portfolio_caller extends portfolio_module_caller_base {
|
|||
return array($navlinks, $this->cm);
|
||||
}
|
||||
/**
|
||||
* either a whole discussion
|
||||
* a single post, with or without attachment
|
||||
* or just an attachment with no post
|
||||
*
|
||||
* @global object
|
||||
* @global object
|
||||
* @uses PORTFOLIO_FORMAT_RICH
|
||||
* @return mixed
|
||||
*/
|
||||
function prepare_package() {
|
||||
global $CFG, $SESSION;
|
||||
// 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 = '';
|
||||
global $CFG;
|
||||
|
||||
// set up the leap2a writer if we need it
|
||||
$writingleap = false;
|
||||
if ($this->exporter->get('formatclass') == PORTFOLIO_FORMAT_LEAP2A) {
|
||||
$leapwriter = $this->exporter->get('format')->leap2a_writer();
|
||||
$writingleap = true;
|
||||
}
|
||||
if ($this->attachment) { // simplest case first - single file attachment
|
||||
$this->copy_files(array($this->singlefile), $this->attachment);
|
||||
if ($writingleap) { // if we're writing leap, make the manifest to go along with the file
|
||||
$entry = new portfolio_format_leap2a_entry($id, $this->attachment->get_filename(), 'resource', $this->attachment);
|
||||
$entry->add_category('offline', 'resource_type');
|
||||
$leapwriter->add_entry($entry);
|
||||
return $this->exporter->write_new_file($leapwriter->to_xml(), $this->exporter->get('format')->manifest_name(), true);
|
||||
}
|
||||
|
||||
} else if (empty($this->post)) { // exporting whole discussion
|
||||
$content = ''; // if we're just writing HTML, start a string to add each post to
|
||||
$ids = array(); // if we're writing leap2a, keep track of all entryids so we can add a selection element
|
||||
foreach ($this->posts as $post) {
|
||||
$content .= '<br /><br />' . $this->prepare_post($post);
|
||||
$posthtml = $this->prepare_post($post);
|
||||
if ($writingleap) {
|
||||
$ids[] = $this->prepare_post_leap2a($leapwriter, $post, $posthtml);
|
||||
} else {
|
||||
$content .= $posthtml . '<br /><br />';
|
||||
}
|
||||
}
|
||||
$this->copy_files($this->multifiles);
|
||||
return $this->get('exporter')->write_new_file($content, 'discussion.html', $manifest);
|
||||
$name = 'discussion.html';
|
||||
$manifest = ($this->exporter->get('format') instanceof PORTFOLIO_FORMAT_RICH);
|
||||
if ($writingleap) {
|
||||
// add on an extra 'selection' entry
|
||||
$selection = new portfolio_format_leap2a_entry('forumdiscussion' . $this->discussionid, get_string('discussion', 'forum'), 'selection');
|
||||
$leapwriter->add_entry($selection);
|
||||
$leapwriter->make_selection($selection, $ids, 'Grouping');
|
||||
$content = $leapwriter->to_xml();
|
||||
$name = $this->exporter->get('format')->manifest_name();
|
||||
}
|
||||
$this->get('exporter')->write_new_file($content, $name, $manifest);
|
||||
|
||||
} else { // exporting a single post
|
||||
$posthtml = $this->prepare_post($this->post);
|
||||
|
||||
$content = $posthtml;
|
||||
$name = 'post.html';
|
||||
$manifest = ($this->exporter->get('format') instanceof PORTFOLIO_FORMAT_RICH);
|
||||
|
||||
if ($writingleap) {
|
||||
$this->prepare_post_leap2a($leapwriter, $this->post, $posthtml);
|
||||
$content = $leapwriter->to_xml();
|
||||
$name = $this->exporter->get('format')->manifest_name();
|
||||
}
|
||||
$this->copy_files($this->multifiles);
|
||||
$this->get('exporter')->write_new_file($content, $name, $manifest);
|
||||
}
|
||||
if ($this->attachment) {
|
||||
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', $manifest);
|
||||
}
|
||||
|
||||
/**
|
||||
* helper function to add a leap2a entry element
|
||||
* that corresponds to a single forum post,
|
||||
* including any attachments
|
||||
*
|
||||
* the entry/ies are added directly to the leapwriter, which is passed by ref
|
||||
*
|
||||
* @param portfolio_format_leap2a_writer $leapwriter writer object to add entries to
|
||||
* @param object $post the stdclass object representing the database record
|
||||
* @param string $posthtml the content of the post (prepared by {@link prepare_post}
|
||||
*
|
||||
* @return int id of new entry
|
||||
*/
|
||||
private function prepare_post_leap2a(portfolio_format_leap2a_writer $leapwriter, $post, $posthtml) {
|
||||
$entry = new portfolio_format_leap2a_entry('forumpost' . $post->id, $post->subject, 'resource', $posthtml);
|
||||
if (is_array($this->keyedfiles) && array_key_exists($post->id, $this->keyedfiles) && is_array($this->keyedfiles[$post->id])) {
|
||||
foreach ($this->keyedfiles[$post->id] as $file) {
|
||||
// copying the file into the package area is handled elsewhere
|
||||
$entry->add_attachment($file);
|
||||
}
|
||||
}
|
||||
$entry->add_category('web', 'resource_type');
|
||||
$leapwriter->add_entry($entry);
|
||||
return $entry->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $files
|
||||
* @param bool $justone
|
||||
* @param mixed $justone false of id of single file to copy
|
||||
* @return bool|void
|
||||
*/
|
||||
private function copy_files($files, $justone=false) {
|
||||
|
@ -8119,7 +8191,7 @@ class forum_portfolio_caller extends portfolio_module_caller_base {
|
|||
}
|
||||
foreach ($files as $f) {
|
||||
if ($justone && $f->get_id() != $justone) {
|
||||
continue; // support multipe files later
|
||||
continue;
|
||||
}
|
||||
$this->get('exporter')->copy_existing_file($f);
|
||||
if ($justone && $f->get_id() == $justone) {
|
||||
|
@ -8134,7 +8206,7 @@ class forum_portfolio_caller extends portfolio_module_caller_base {
|
|||
* @param int $post
|
||||
* @return string
|
||||
*/
|
||||
private function prepare_post($post) {
|
||||
private function prepare_post($post, $fileoutputextras=null) {
|
||||
global $DB;
|
||||
static $users;
|
||||
if (empty($users)) {
|
||||
|
@ -8175,12 +8247,12 @@ class forum_portfolio_caller extends portfolio_module_caller_base {
|
|||
|
||||
$output .= $formattedtext;
|
||||
|
||||
if (is_array($this->keyedfiles) && array_key_exists($post->id, $this->keyedfiles) && is_array($this->keyedfiles[$post->id])) {
|
||||
if (is_array($this->keyedfiles) && array_key_exists($post->id, $this->keyedfiles) && is_array($this->keyedfiles[$post->id]) && count($this->keyedfiles[$post->id]) > 0) {
|
||||
$output .= '<div class="attachments">';
|
||||
$output .= '<br /><b>' . get_string('attachments', 'forum') . '</b>:<br /><br />';
|
||||
$format = $this->get('exporter')->get('format');
|
||||
foreach ($this->keyedfiles[$post->id] as $file) {
|
||||
$output .= $format->file_output($file) . '<br/ >';
|
||||
$output .= $format->file_output($file) . '<br/ >';
|
||||
}
|
||||
$output .= "</div>";
|
||||
}
|
||||
|
@ -8238,6 +8310,10 @@ class forum_portfolio_caller extends portfolio_module_caller_base {
|
|||
public static function display_name() {
|
||||
return get_string('modulename', 'forum');
|
||||
}
|
||||
|
||||
public static function base_supported_formats() {
|
||||
return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML, PORTFOLIO_FORMAT_PLAINHTML, PORTFOLIO_FORMAT_LEAP2A);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue