Skip to content

Commit

Permalink
MDL-66808 mod_forum: Added export options
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies committed Oct 17, 2019
1 parent c77052f commit c98f448
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
9 changes: 9 additions & 0 deletions mod/forum/classes/form/export_form.php
Expand Up @@ -76,6 +76,15 @@ public function definition() {
$options[$format->name] = $format->displayname;
}
$mform->addElement('select', 'format', 'Format', $options);

$mform->addElement('header', 'optionsheader', get_string('exportoptions', 'mod_forum'));

$mform->addElement('checkbox', 'striphtml', '', get_string('exportstriphtml', 'mod_forum'));
$mform->addHelpButton('striphtml', 'exportstriphtml', 'mod_forum');

$mform->addElement('checkbox', 'humandates', '', get_string('exporthumandates', 'mod_forum'));
$mform->addHelpButton('humandates', 'exporthumandates', 'mod_forum');

$this->add_action_buttons(true, get_string('export', 'mod_forum'));
}
}
48 changes: 35 additions & 13 deletions mod/forum/export.php
Expand Up @@ -62,8 +62,6 @@
if ($form->is_cancelled()) {
redirect(new moodle_url('/mod/forum/view.php', ['id' => $cm->id]));
} else if ($data = $form->get_data()) {
require_sesskey();

$dataformat = $data->format;

$discussionvault = $vaultfactory->get_discussion_vault();
Expand All @@ -89,25 +87,49 @@
$capabilitymanager->can_view_any_private_reply($USER));
}

$fields = ['id', 'discussion', 'parent', 'userid', 'created', 'modified', 'mailed', 'subject', 'message',
'messageformat', 'messagetrust', 'attachment', 'totalscore', 'mailnow', 'deleted', 'privatereplyto'];
$striphtml = !empty($data->striphtml);
$humandates = !empty($data->humandates);

$fields = ['id', 'discussion', 'parent', 'userid', 'created', 'modified', 'mailed', 'subject', 'message'];
if (!$striphtml) {
$fields[] = 'messageformat';
}
$fields = array_merge($fields, ['messagetrust', 'attachment', 'totalscore', 'mailnow', 'deleted', 'privatereplyto']);

$datamapper = $legacydatamapperfactory->get_post_data_mapper();
$exportdata = new ArrayObject($datamapper->to_legacy_objects($posts));
$iterator = $exportdata->getIterator();

require_once($CFG->libdir . '/dataformatlib.php');
$filename = clean_filename('discussion');
download_as_dataformat($filename, $dataformat, $fields, $iterator, function($exportdata) use ($fields) {
$data = $exportdata;
foreach ($fields as $field) {
// Convert any boolean fields to their integer equivalent for output.
if (is_bool($data->$field)) {
$data->$field = (int) $data->$field;
download_as_dataformat(
$filename,
$dataformat,
$fields,
$iterator,
function($exportdata) use ($fields, $striphtml, $humandates) {
$data = $exportdata;
if ($striphtml) {
// The following call to html_to_text uses the option that strips out
// all URLs, but format_text complains if it finds @@PLUGINFILE@@ tokens.
// So, we need to replace @@PLUGINFILE@@ with a real URL, but it doesn't
// matter what. We use http://example.com/.
$data->message = str_replace('@@PLUGINFILE@@/', 'http://example.com/', $data->message);
$data->message = html_to_text(format_text($data->message, $data->messageformat), 0, false);
unset($data->messageformat);
}
if ($humandates) {
$data->created = userdate($data->created);
$data->modified = userdate($data->modified);
}
foreach ($fields as $field) {
// Convert any boolean fields to their integer equivalent for output.
if (is_bool($data->$field)) {
$data->$field = (int) $data->$field;
}
}
}
return $data;
});
return $data;
});
die;
}

Expand Down
7 changes: 6 additions & 1 deletion mod/forum/lang/en/forum.php
Expand Up @@ -262,8 +262,13 @@
$string['everyoneissubscribed'] = 'Everyone is subscribed to this forum';
$string['existingsubscribers'] = 'Existing subscribers';
$string['export'] = 'Export';
$string['exportdiscussion'] = 'Export whole discussion to portfolio';
$string['exportattachmentname'] = 'Export attachment {$a} to portfolio';
$string['exportdiscussion'] = 'Export whole discussion to portfolio';
$string['exportstriphtml'] = 'Strip HTML';
$string['exportstriphtml_help'] = 'Select this option if you want the HTML tags to be stripped from the Whether to export raw HTML or strip HTML codes.';
$string['exportoptions'] = 'Export options';
$string['exporthumandates'] = 'Human-readable dates';
$string['exporthumandates_help'] = 'Select this option if you want dates to be exported in a human readable format. Otherwise, dates will be exported as timestamps.';
$string['firstpost'] = 'First post';
$string['favourites'] = 'Starred';
$string['favouriteupdated'] = 'Your star option has been updated.';
Expand Down

0 comments on commit c98f448

Please sign in to comment.