From c98f4488662fd301748a4f88893691b51a559cfa Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Thu, 10 Oct 2019 05:04:38 +1100 Subject: [PATCH] MDL-66808 mod_forum: Added export options --- mod/forum/classes/form/export_form.php | 9 +++++ mod/forum/export.php | 48 +++++++++++++++++++------- mod/forum/lang/en/forum.php | 7 +++- 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/mod/forum/classes/form/export_form.php b/mod/forum/classes/form/export_form.php index 905e5e63e39..6c799b6b04f 100644 --- a/mod/forum/classes/form/export_form.php +++ b/mod/forum/classes/form/export_form.php @@ -76,6 +76,15 @@ class export_form extends \moodleform { $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')); } } diff --git a/mod/forum/export.php b/mod/forum/export.php index 419997754a6..636cfc8208d 100644 --- a/mod/forum/export.php +++ b/mod/forum/export.php @@ -62,8 +62,6 @@ $form = new mod_forum\form\export_form($url->out(false), [ 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(); @@ -89,8 +87,14 @@ if ($form->is_cancelled()) { $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)); @@ -98,16 +102,34 @@ if ($form->is_cancelled()) { 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); } - } - return $data; - }); + 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; + }); die; } diff --git a/mod/forum/lang/en/forum.php b/mod/forum/lang/en/forum.php index da3424c2451..7322095079c 100644 --- a/mod/forum/lang/en/forum.php +++ b/mod/forum/lang/en/forum.php @@ -262,8 +262,13 @@ $string['everyoneisnowsubscribed'] = 'Everyone is now subscribed to this forum'; $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.';